关于内核中spinlock的一些个人理解
由于2.6内核可以抢占,应该在驱动程序中使用 preempt_disable() 和 preempt_enable(),从而保护代码段不被抢占(禁止 IRQ 同时也就隐式地禁止了抢占)。
在这里,我主要把自己对内核中spinlock的一些理解写出来,并不是要告诉大家什么(因为我对我所说的也不能确定),而是希望大家对我的这些理解对的地方给我肯定,错误的地方给我指出。
和spinlock 相关的文件主要有两个,一个是include/linux/spinlock.h,主要是提供关于和硬件无关的spinlock的几个对外主函数,一个是 include/asm-XXX/spinlock.h,用来提供和硬件相关的功能函数。另外,在2.6的内核中,又多了一个文件, include/linux/preempt.h,为新增加的抢占式多任务功能提供一些服务。
spinlock的作用:spinlock系列函数主要用于保护临界数据(非常重要的数据)不被同时访问(给临界数据加锁),用以达到多任务的同步。如果一个数据当前不可访问,那么就一直等,直到可以访问为止。
spinlock 函数的使用前提:首先,spinklock函数只能使用在内核中,或者说只能使用在内核状态下,在2.6以前的内核是不可抢占的,也就是说,当运行于内核 状态下时,是不容许切换到其他进程的。而在2.6以后的内核中,编译内核的时候多了一个选项,可以配置内核是否可以被抢占,这也就是为什么在2.6的内核 中多了一个preempt.h的原因。
spinlock主要包含以下几个函数:
spin_lock
spin_unlock
spin_lock_irqsave
spin_lock_irq
spin_unlock_irqrestore
spin_unlock_irq
另 外还有其他很多,如关于读者写者的一套函数,关于bottom half一套函数(关于bottom half的代码我还没有读到),还有还提供了一套用bit实现加锁的函数,由于大概意思都相同,所以我这里就不说了(只想简单说说,没想到东西还挺多,我 的手都快冻僵了,江南的冬天真的受不了:)
spinlock函数根据机器的配置分为两套,单CPU和多CPU,先来看看单CPU的情况。
在 单CPU的情况下,spin_lock和spin_unlock函数都被定义成空操作(do { } while(0)),这是因为我们上面说的,内核不可以被抢占的原因。所以,在单CPU的情况下,只要你能够保证你要保护的临界数据不会在中断中用到的 话,那么你的数据已经是受保护的了,不需要做任何操作。在2.6内核中,这两个函数就不再这么简单了,因为内核也有可能被其他程序中断,所以要保护数据, 还要让调度程序暂时不调度此段程序,也就是说,暂时禁止抢占式任务调度功能,所以在上面两个函数中分别多了一个preempt_disable和preempt_enable 调用。spin_lock_irq的功能和上面的spin_lock提供的功能差不多,只不过它还多做了一步,就是把中断也关上,主要用于当前保护的数据 在可能的中断程序中也要用到的情况。spin_lock_irqsave和spin_lock_irq的功能一样,只不过调用这个函数以后可以把当前的中 断状态记下了,以备以后恢复。
在多CPU的环境下情况就比较复杂了,因为同时可能有几个程序在运行(是真正的同时),所以必须要定义一个 变量当作锁的功能,linux是这样规定的,当这个变量为1时,那么其保护的变量可以被访问,当其值为0时,那么其保护的临界数据不可以被访问,其中,要 改变变量锁的值也很有学问,就是不能让几个CPU同时去改,负责就会出现不同步的情况。如spin_lock在多cpu的时候就被?/
spin_lock()/spin_unlock(),
spin_lock_irq()/spin_unlock_irq(),
spin_lock_irqsave/spin_unlock_irqrestore()
spin_lock_bh()/spin_unlock_bh()
local_irq_disable/local_irq_enable
local_bh_disable/local_bh_enable
1 用户进程的内核态,此时有进程context,主要是代表进程在执行系统调用
等。
2 中断或者异常或者自陷等,从概念上说,此时没有进程context,不能进行
context switch。
3 bottom_half,从概念上说,此时也没有进程context。
4 同时,相同的执行路径还可能在其他的CPU上运行。
int setup_irq(unsigned int irq, struct irqaction * new)
{
int shared = 0;
unsigned long flags;
struct irqaction *old, **p;
irq_desc_t *desc = irq_desc + irq;
/*
* Some drivers like serial.c use request_irq() heavily,
* so we have to be careful not to interfere with a
* running system.
*/
if (new->flags & SA_SAMPLE_RANDOM) {
/*
* This function might sleep, we want to call it first,
* outside of the atomic block.
* Yes, this might clear the entropy pool if the wrong
* driver is attempted to be loaded, without actually
* installing a new handler, but is this really a problem,
* only the sysadmin is able to do this.
*/
rand_initialize_irq(irq);
}
/*
* The following block of code has to be executed atomically
*/
[1] spin_lock_irqsave(&desc->lock,flags);
p = &desc->action;
if ((old = *p) != NULL) {
/* Can't share interrupts unless both agree to */
if (!(old->flags & new->flags & SA_SHIRQ)) {
[2] spin_unlock_irqrestore(&desc->lock,flags);
return -EBUSY;
}
/* add new interrupt at end of irq queue */
do {
p = &old->next;
old = *p;
} while (old);
shared = 1;
}
*p = new;
if (!shared) {
desc->depth = 0;
desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT | IRQ_WAITING);
desc->handler->startup(irq);
}
[3] spin_unlock_irqrestore(&desc->lock,flags);
register_irq_proc(irq);
return 0;
}
asmlinkage unsigned int do_IRQ(struct pt_regs regs)
{
/*
* We ack quickly, we don't want the irq controller
* thinking we're snobs just because some other CPU has
* disabled global interrupts (we have already done the
* INT_ACK cycles, it's too late to try to pretend to the
* controller that we aren't taking the interrupt).
*
* 0 return value means that this irq is already being
* handled by some other CPU. (or is disabled)
*/
int irq = regs.orig_eax & 0xff; /* high bits used in ret_from_ code */
int cpu = smp_processor_id();
irq_desc_t *desc = irq_desc + irq;
struct irqaction * action;
unsigned int status;
kstat.irqs[cpu][irq]++;
[4] spin_lock(&desc->lock);
desc->handler->ack(irq);
/*
REPLAY is when Linux resends an IRQ that was dropped earlier
WAITING is used by probe to mark irqs that are being tested
*/
status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
status |= IRQ_PENDING; /* we _want_ to handle it */
/*
* If the IRQ is disabled for whatever reason, we cannot
* use the action we have.
*/
action = NULL;
if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
action = desc->action;
status &= ~IRQ_PENDING; /* we commit to handling */
status |= IRQ_INPROGRESS; /* we are handling it */
}
desc->status = status;
/*
* If there is no IRQ handler or it was disabled, exit early.
Since we set PENDING, if another processor is handling
a different instance of this same irq, the other processor
will take care of it.
*/
if (!action)
goto out;
/*
* Edge triggered interrupts need to remember
* pending events.
* This applies to any hw interrupts that allow a second
* instance of the same irq to arrive while we are in do_IRQ
* or in the handler. But the code here only handles the _second_
* instance of the irq, not the third or fourth. So it is mostly
* useful for irq hardware that does not mask cleanly in an
* SMP environment.
*/
for (;;) {
[5] spin_unlock(&desc->lock);
handle_IRQ_event(irq, ®s, action);
[6] spin_lock(&desc->lock);
if (!(desc->status & IRQ_PENDING))
break;
desc->status &= ~IRQ_PENDING;
}
desc->status &= ~IRQ_INPROGRESS;
out:
/*
* The ->end() handler has to deal with interrupts which got
* disabled while the handler was running.
*/
desc->handler->end(irq);
[7] spin_unlock(&desc->lock);
if (softirq_pending(cpu))
do_softirq();
return 1;
}
static void tasklet_hi_action(struct softirq_action *a)
{
int cpu = smp_processor_id();
struct tasklet_struct *list;
[8] local_irq_disable();
list = tasklet_hi_vec[cpu].list;
tasklet_hi_vec[cpu].list = NULL;
[9] local_irq_enable();
while (list) {
struct tasklet_struct *t = list;
list = list->next;
if (tasklet_trylock(t)) {
if (!atomic_read(&t->count)) {
if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
BUG();
t->func(t->data);
tasklet_unlock(t);
continue;
}
tasklet_unlock(t);
}
[10] local_irq_disable();
t->next = tasklet_hi_vec[cpu].list;
tasklet_hi_vec[cpu].list = t;
__cpu_raise_softirq(cpu, HI_SOFTIRQ);
[11] local_irq_enable();
}
}