[理解] 為什麼我的spin_lock_irqsave()沒有鎖住時鐘中斷?

來源:互聯網
上載者:User

LZ:

 

嘗試用spin_lock_irqsave(),發現沒有禁掉時鐘中斷,不知道我哪裡理解錯了?
kernel版本:

  1. uname -a 結果:
  2. Linux localhost.localdomain 2.6.18 #1 Sat Jul 19 13:06:00 EDT 2008 i686 i686 i386 GNU/Linux

複製代碼

My Code:

  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/spinlock.h>
  5. static spinlock_t my_spinlock;
  6. static int __init init_test()
  7. {
  8.         unsigned long flags;
  9.         spin_lock_init(&my_spinlock);
  10.         spin_lock_irqsave(&my_spinlock, flags);
  11.         return 0;
  12. }
  13. static void __exit init_exit()
  14. {
  15.         return;
  16. }
  17. module_init(init_test);
  18. module_exit(init_exit);

複製代碼

載入module以後,通過這樣的命令查看牆上時鐘:

  1. #date; sleep 2; date
  2. 輸出結果:
  3. Sun Apr 19 12:59:42 EDT 2009
  4. Sun Apr 19 12:59:44 EDT 2009

複製代碼

牆上時鐘仍然在變化,說明時鐘中斷沒有被禁掉。
但我理解的spin_lock_irqsave()會通過cli關掉所有IRQ,並且我在module中也沒有spin_unlock_irqrestore(),所以按理說在載入了這個module之後,系統就不會響應任何外部可屏蔽中斷了。但實際結果卻不是這樣。

我哪裡理解錯了呢?請各位指點,多謝!

 

 

解答:

寫了一段kernel module和 userspace program來驗證:

kernel module: my_spin_lock.c

  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/module.h>
  4. #include <linux/spinlock.h>
  5. #define IF_MASK 0x00000200
  6. static spinlock_t my_spinlock;
  7. static unsigned int is_interrupt_enable()
  8. {
  9.         unsigned long my_eflags;
  10.         asm volatile ("pushfl /n/t"
  11.         "popl %0"
  12.         :"=a"(my_eflags));
  13.         return (((my_eflags & IF_MASK) == 0) ? 0 : 1);
  14. }
  15. static int __init init_test()
  16. {
  17.         unsigned long flags;
  18.         spin_lock_init(&my_spinlock);
  19.         spin_lock_irqsave(&my_spinlock, flags);
  20.         printk("from kernelspace init: interrupt was enable : %d/n", is_interrupt_enable());
  21.         return 0;
  22. }
  23. static void __exit init_exit()
  24. {
  25.         return;
  26. }
  27. MODULE_LICENSE("GPL");
  28. module_init(init_test);
  29. module_exit(init_exit);

複製代碼

編譯以後產生my_spin_lock.ko

然後再寫一段userspace program : test.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define IF_MASK 0x00000200
  4. #define CMD_LEN 100
  5. unsigned int is_interrupt_enable()
  6. {
  7.         unsigned long my_eflags;
  8.         asm volatile ("pushfl /n/t"
  9.         "popl %0 /n/t"
  10.         :"=a"(my_eflags));
  11.        
  12.         return (((my_eflags & IF_MASK) == 0) ? 0 : 1);
  13. }
  14. int main (int argc, char *argv[])
  15. {
  16.         if (argc != 2)
  17.         {
  18.                 printf("usersage: insmod your_kernel_module/n");
  19.                 return 0;       
  20.         }
  21.         char cmd[CMD_LEN];
  22.         memset(cmd, 0, sizeof(cmd));
  23.         sprintf(cmd, "insmod %s", argv[1]);
  24.         printf("from userspace, before insmod, inerrput is enable : %d/n", is_interrupt_enable());
  25.         system(cmd); /*insmod kernel module*/
  26.         printf("from userspace, after insmod, interrupt is enable : %d/n", is_interrupt_enable());       
  27.         memset(cmd, 0, sizeof(cmd));
  28.         sprintf(cmd, "rmmod %s", argv[1]);
  29.         system(cmd); /*rmmod kernel module*/
  30.        
  31.        
  32.         return 0;
  33. }

複製代碼

編譯以後產生test二進位檔案

然後下命令:

  1. ./test my_spin_lock.ko

複製代碼

運行結果:

QUOTE:from userspace, before insmod, interrupt is enable : 1
from kernelspace init: interrupt was enable : 0
from userspace, after insmod, interrupt is enable: 1

可見,運行了kernel module的init函數以後,interrupt確實被禁止了。然後返回到使用者態,interrupt被開啟了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.