標籤:
Tasklet機制是一種較為特殊的非強制中斷。Tasklet一詞的原意是“小片任務”的意思,這裡是指一小段可執行檔代碼,且通常以函數的形式出現。非強制中斷向量HI_SOFTIRQ和TASKLET_SOFTIRQ均是用tasklet機制來實現的。
從某種程度上講,tasklet機制是Linux核心對BH機制的一種擴充。在2.4核心引入了softirq機制後,原有的BH機制正是通過tasklet機制這個橋樑來納入softirq機制的整體架構中的。正是由於這種曆史的延伸關係,使得tasklet機制與一般意義上的非強制中斷有所不同,而呈現出以下兩個顯著的特點:
1. 與一般的非強制中斷不同,某一段tasklet代碼在某個時刻只能在一個CPU上運行,而不像一般的非強制中斷服務函數(即softirq_action結構中的action函數指標)那樣——在同一時刻可以被多個CPU並發地執行。
2. 與BH機制不同,不同的tasklet代碼在同一時刻可以在多個CPU上並發地執行,而不像BH機制那樣必須嚴格地序列化執行(也即在同一時刻系統中只能有一個CPU執行BH函數)。
非強制中斷是中斷下半部總體的說法,Tasklet機制是非強制中斷的特殊例子,非強制中斷向量HI_SOFTIRQ和TASKLET_SOFTIRQ均是用tasklet機制來實現的。 最後BH是通過Tasklet機制融入非強制中斷架構的。
一、非強制中斷的初始化
1、初始化Tasklet機制
void __init softirq_init(){int i;for (i=0; i<32; i++)tasklet_init(bh_task_vec+i, bh_action, i);open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL);open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL);}
struct tasklet_struct{struct tasklet_struct *next;unsigned long state;atomic_t count;void (*func)(unsigned long);unsigned long data;};
struct tasklet_struct bh_task_vec[32];
enum{HI_SOFTIRQ=0,NET_TX_SOFTIRQ,NET_RX_SOFTIRQ,TASKLET_SOFTIRQ};
tasklet_init函數如下:
void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data){t->func = func;t->data = data;//0~32t->state = 0;atomic_set(&t->count, 0);}
open_softirq函數如下:
void open_softirq(int nr, void (*action)(struct softirq_action*), void *data){unsigned long flags;int i;spin_lock_irqsave(&softirq_mask_lock, flags);softirq_vec[nr].data = data;softirq_vec[nr].action = action;for (i=0; i<NR_CPUS; i++)//NR_CPUS為1softirq_mask(i) |= (1<<nr);//irq_stat[0].__softirq_mask第1位和第4位都置1spin_unlock_irqrestore(&softirq_mask_lock, flags);}
struct softirq_action{void(*action)(struct softirq_action *);void*data;};
static struct softirq_action softirq_vec[32] __cacheline_aligned;
#define softirq_mask(cpu)__IRQ_STAT((cpu), __softirq_mask)
#define __IRQ_STAT(cpu, member)((void)(cpu), irq_stat[0].member)
typedef struct {unsigned int __softirq_active;unsigned int __softirq_mask;unsigned int __local_irq_count;unsigned int __local_bh_count;unsigned int __syscall_count;unsigned int __nmi_count;/* arch dependent */} ____cacheline_aligned irq_cpustat_t;
extern irq_cpustat_t irq_stat[];
2、初始化bh
void __init sched_init(void){/* * We have to do a little magic to get the first * process right in SMP mode. */......init_bh(TIMER_BH, timer_bh);init_bh(TQUEUE_BH, tqueue_bh);init_bh(IMMEDIATE_BH, immediate_bh);/* * The boot idle thread does lazy MMU switching as well: */atomic_inc(&init_mm.mm_count);enter_lazy_tlb(&init_mm, current, cpu);}
enum {TIMER_BH = 0,TQUEUE_BH,DIGI_BH,SERIAL_BH,RISCOM8_BH,SPECIALIX_BH,AURORA_BH,ESP_BH,SCSI_BH,IMMEDIATE_BH,CYCLADES_BH,CM206_BH,JS_BH,MACSERIAL_BH,ISICOM_BH};
void init_bh(int nr, void (*routine)(void)){bh_base[nr] = routine;mb();}
二、將tasklet_struct結構的(bh_task_vec+0)鏈入tasklet_hi_vec[0]
在時鐘處理函數中,do_timer中執行了mark_bh(TIMER_BH),代碼如下:
static inline void mark_bh(int nr){tasklet_hi_schedule(bh_task_vec+nr);}
static inline void tasklet_hi_schedule(struct tasklet_struct *t){if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {int cpu = smp_processor_id();unsigned long flags;local_irq_save(flags);t->next = tasklet_hi_vec[cpu].list;//將tasklet_struct結構的(bh_task_vec+0)鏈入tasklet_hi_vec[0]tasklet_hi_vec[cpu].list = t;__cpu_raise_softirq(cpu, HI_SOFTIRQ);local_irq_restore(flags);}}
struct tasklet_head{struct tasklet_struct *list;} __attribute__ ((__aligned__(SMP_CACHE_BYTES)));
extern struct tasklet_head tasklet_hi_vec[NR_CPUS];//NR_CPUS為0
static inline void __cpu_raise_softirq(int cpu, int nr){softirq_active(cpu) |= (1<<nr);//irq_stat[0].__softirq_active第1位置1}
三、非強制中斷響應
核心每當在do_IRQ()中執行完一個通道中的中斷服務程式以後,以及每當從系統調用返回時,都要檢查是否有非強制中斷請求在等待執行。
if (softirq_active(cpu) & softirq_mask(cpu))//irq_stat[0].__softirq_active第1位置1 & irq_stat[0].__softirq_mask第1位和第4位都置1 結果不為0 do_softirq();
do_softirq代碼如下:
asmlinkage void do_softirq(){int cpu = smp_processor_id();__u32 active, mask;if (in_interrupt())//如果已經增加了irq_stat[0].__local_bh_count計數,還沒有減去,那麼直接返回,也就是說return;local_bh_disable();//增加irq_stat[0].__local_bh_count計數local_irq_disable();//cli關中斷mask = softirq_mask(cpu);active = softirq_active(cpu) & mask;if (active) {struct softirq_action *h;restart:/* Reset active bitmask before enabling irqs */softirq_active(cpu) &= ~active;local_irq_enable();//sti開中斷h = softirq_vec;mask &= ~active;do {if (active & 1)h->action(h);//執行tasklet_hi_actionh++;active >>= 1;} while (active);local_irq_disable();active = softirq_active(cpu);if ((active &= mask) != 0)goto retry;}local_bh_enable();////減少irq_stat[0].__local_bh_count計數/* Leave with locally disabled hard irqs. It is critical to close * window for infinite recursion, while we help local bh count, * it protected us. Now we are defenceless. */return;retry:goto restart;}
#define local_bh_disable()cpu_bh_disable(smp_processor_id())
#define cpu_bh_disable(cpu)do { local_bh_count(cpu)++; barrier(); } while (0)
#define local_bh_count(cpu)__IRQ_STAT((cpu), __local_bh_count)
#define __IRQ_STAT(cpu, member)((void)(cpu), irq_stat[0].member)
#define in_interrupt() ({ int __cpu = smp_processor_id(); (local_irq_count(__cpu) + local_bh_count(__cpu) != 0); }) 如果do_softirq執行的過程中,發生了中斷,在同一個cpu上,又執行了do_softirq,此時在in_interrupt時,會立即返回。所以防止了tasklet機制執行程式的嵌套。
tasklet_hi_action,函數如下:
static void tasklet_hi_action(struct softirq_action *a){int cpu = smp_processor_id();struct tasklet_struct *list;local_irq_disable();list = tasklet_hi_vec[cpu].list;tasklet_hi_vec[cpu].list = NULL;local_irq_enable();while (list != NULL) {struct tasklet_struct *t = list;list = list->next;if (tasklet_trylock(t)) {if (atomic_read(&t->count) == 0) {clear_bit(TASKLET_STATE_SCHED, &t->state);t->func(t->data);tasklet_unlock(t);continue;}tasklet_unlock(t);}local_irq_disable();t->next = tasklet_hi_vec[cpu].list;tasklet_hi_vec[cpu].list = t;__cpu_raise_softirq(cpu, HI_SOFTIRQ);local_irq_enable();}}
剛剛將tasklet_struct結構的(bh_task_vec+0)鏈入tasklet_hi_vec[0],這裡取出來,執行t->func(t->data),也就是bh_action(0),代碼如下:
static void bh_action(unsigned long nr){int cpu = smp_processor_id();if (!spin_trylock(&global_bh_lock))//全域鎖,同一時刻系統中只能有一個CPU執行BH函數goto resched;if (!hardirq_trylock(cpu))goto resched_unlock;if (bh_base[nr])bh_base[nr]();//最後執行的是bh_base[0],也就是timer_bhhardirq_endlock(cpu);spin_unlock(&global_bh_lock);return;resched_unlock:spin_unlock(&global_bh_lock);resched:mark_bh(nr);}
如果t->func(t->data) 不是bh_action(0),也就不是bh函數,那麼tasklets機制滿足:
1、某一段tasklet代碼在某個時刻只能在一個CPU上運行。
2、不同的tasklet代碼在同一時刻可以在多個CPU上並發地執行。
如果t->func(t->data) 是bh_action(0),也就是bh函數,那麼必須像BH機制那樣,嚴格地序列化執行(也即在同一時刻系統中只能有一個CPU執行BH函數)。
所以BH是通過Tasklet機制融入非強制中斷架構的。
Linux核心原始碼情景分析-中斷下半部(非強制中斷)