1. 什么是抢占?
抢占就是进城切换, 以thread_info->preempt_count标识。
thread_info->preempt_count一物多用:
bit0-7代表的是抢占的次数,最大抢占深度为256次,
bit8-15代表的是软中断的次数,最大也是256次,
bit16-19表示中断的次数,注释的大概意思是避免中断嵌套,但是也不能防止某些驱动中断嵌套使用中断,所以嵌套16层也是最大次数了。
bit20~23代表的NMI中断
/*
* PREEMPT_MASK: 0x000000ff //等于0时可抢占,抢占的嵌套次数最多是256
* SOFTIRQ_MASK: 0x0000ff00
* HARDIRQ_MASK: 0x000f0000
* NMI_MASK: 0x00f00000
* PREEMPT_NEED_RESCHED: 0x80000000
*/
#define PREEMPT_BITS 8
#define SOFTIRQ_BITS 8
#define HARDIRQ_BITS 4
#define NMI_BITS 4
#define PREEMPT_SHIFT 0
#define SOFTIRQ_SHIFT (PREEMPT_SHIFT + PREEMPT_BITS)
#define HARDIRQ_SHIFT (SOFTIRQ_SHIFT + SOFTIRQ_BITS)
#define NMI_SHIFT (HARDIRQ_SHIFT + HARDIRQ_BITS)
#define __IRQ_MASK(x) ((1UL << (x))-1)
#define PREEMPT_MASK (__IRQ_MASK(PREEMPT_BITS) << PREEMPT_SHIFT)
#define SOFTIRQ_MASK (__IRQ_MASK(SOFTIRQ_BITS) << SOFTIRQ_SHIFT)
#define HARDIRQ_MASK (__IRQ_MASK(HARDIRQ_BITS) << HARDIRQ_SHIFT)
#define NMI_MASK (__IRQ_MASK(NMI_BITS) << NMI_SHIFT)
#define PREEMPT_OFFSET (1UL << PREEMPT_SHIFT)
#define SOFTIRQ_OFFSET (1UL << SOFTIRQ_SHIFT)
#define HARDIRQ_OFFSET (1UL << HARDIRQ_SHIFT)
#define NMI_OFFSET (1UL << NMI_SHIFT)
#define hardirq_count() (preempt_count() & HARDIRQ_MASK)
#define softirq_count() (preempt_count() & SOFTIRQ_MASK)
#define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
| NMI_MASK))
/*
* Are we doing bottom half or hardware interrupt processing?
*
* in_irq() - We're in (hard) IRQ context
* in_softirq() - We have BH disabled, or are processing softirqs
* in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled
* in_serving_softirq() - We're in softirq context
* in_nmi() - We're in NMI context
* in_task() - We're in task context
*
* Note: due to the BH disabled confusion: in_softirq(),in_interrupt() really
* should not be used in new code.
*/
#define in_irq() (hardirq_count())
#define in_softirq() (softirq_count())
#define in_interrupt() (irq_count())
#define in_serving_softirq() (softirq_count() & SOFTIRQ_OFFSET)
#define in_nmi() (preempt_count() & NMI_MASK)
#define in_task() (!(preempt_count() & \
(NMI_MASK | HARDIRQ_MASK | SOFTIRQ_OFFSET)))
2.抢占的函数:
spin_lock()/spin_unlock()
disable_preempt()/enable_preempt()--禁止或使能内核抢占,调用下面的inc_preempt_count()/dec_preempt_count(),加了memory barrier。
inc_preempt_count()/dec_preempt_count()
get_cpu()/put_cpu()
3.调度点
a) 进程被阻塞时
b) 调整参数时,比如通过sched_setscheduler() ,nice()等函数调整进程的调度策略,静态优先级时
c) 睡眠进程被唤醒时,比如wake_up唤醒等待队列中的进程时,如果该进程具有更高优先级则会设置当前
进程TIF_NEED_RESCHED,如果允许内核态抢占,则会调度一次
d)中断处理完时,如果中断处理过程中设置了TIF_NEED_SCHED标志,中断返回时,不论是要返回内核态还是用户态,都会发生一次抢占.当然,在这也会检查有没有软中断需要处理。
e)执行了preempt_enable()函数。