理解] 为什么我的spin_lock_irqsave()没有锁住时钟中断?(这里的用户态汇编检测中断标志位的代码很值得参考)

LZ:

尝试用spin_lock_irqsave(),发现没有禁掉时钟中断,不知道我哪里理解错了?
kernel版本:

1.uname -a 结果:
2.
3.Linux localhost.localdomain 2.6.18 #1 Sat Jul 19 13:06:00 EDT 2008 i686 i686 i386 GNU/Linux
复制代码


我的代码:

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


加载module以后,通过这样的命令查看墙上时钟:

1.#date; sleep 2; date
2.
3.输出结果:
4.Sun Apr 19 12:59:42 EDT 2009
5.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.
6.#define IF_MASK 0x00000200
7.
8.static spinlock_t my_spinlock;
9.
10.static unsigned int is_interrupt_enable()
11.{
12.        unsigned long my_eflags;
13.        asm volatile ("pushfl /n/t"
14.        "popl %0"
15.        :"=a"(my_eflags));
16.
17.        return (((my_eflags & IF_MASK) == 0) ? 0 : 1);
18.}
19.
20.static int __init init_test()
21.{
22.        unsigned long flags;
23.        spin_lock_init(&my_spinlock);
24.
25.        spin_lock_irqsave(&my_spinlock, flags);
26.        printk("from kernelspace init: interrupt was enable : %d/n", is_interrupt_enable());
27.        return 0;
28.}
29.
30.static void __exit init_exit()
31.{
32.        return;
33.}
34.
35.MODULE_LICENSE("GPL");
36.module_init(init_test);
37.module_exit(init_exit);
复制代码


编译以后生成my_spin_lock.ko

然后再写一段userspace program : test.c

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


编译以后生成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被打开了。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/do2jiang/archive/2010/04/15/5487219.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spin_lock() 和 spin_lock_irqsave() 都是 Linux 内核中用于保护共享资源的自旋锁函数,它们的区别在于是否对中断进行处理。 spin_lock() 函数会获取自旋锁,并禁用本地 CPU 的中断。这意味着,如果在获取自旋锁的过程中发生了中断中断处理程序将无法运行,直到自旋锁被释放。因此,spin_lock() 主要用于在中断被禁用的情况下保护共享资源,以防止其他 CPU 并发访问。 而 spin_lock_irqsave() 函数则会获取自旋锁,并保存本地 CPU 的中断。这意味着,在获取自旋锁的过程中,中断可以被触发,但是中断处理程序不能访问被保护的共享资源,因为它们也会尝试获取自旋锁。因此,spin_lock_irqsave() 主要用于在中断被启用的情况下保护共享资源。 在使用 spin_lock_irqsave() 函数时,需要在获取自旋锁的同时保存中断,并在释放自旋锁的同时恢复中断,以避免出现中断被篡改的情况。常见的用法是: ```c spinlock_t my_lock; unsigned long flags; spin_lock_irqsave(&my_lock, flags); // 获取自旋锁并保存中断 // 在这里进行对共享资源的访问 spin_unlock_irqrestore(&my_lock, flags); // 释放自旋锁并恢复中断 ``` 总之,spin_lock() 适用于在禁用中断的情况下保护共享资源,而 spin_lock_irqsave() 适用于在启用中断的情况下保护共享资源,它们的使用方式略有不同,需要根据具体情况选择合适的函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值