標籤:
本文轉載自: http://blog.chinaunix.net/uid-9620812-id-3833377.html,如有需要,請移步訪問。
Technorati 標籤: Linux 非強制中斷
---------------------------------------我是分割線----------------------------------------
一、非強制中斷註冊
和硬中斷類似,非強制中斷也有類似的中斷向量表,只不過是用“軟體”實現的。
struct softirq_action softirq_vec[32]是非強制中斷向量表
(檔案linux_2_6_24/kernel/softirq.c)
- struct softirq_action
- {
- void (*action)(struct softirq_action *); //鉤子函數
- void *data; //鉤子函數的形參
- };
核心用到的中斷向量(其實就是數組下標)如下所示
(檔案linux_2_6_24/include/linux/interrupt.h)
- enum{
- HI_SOFTIRQ=0, //高優先順序的tasklet
- TIMER_SOFTIRQ, //核心定時器
- NET_TX_SOFTIRQ, //網路發送
- NET_RX_SOFTIRQ, //網路接收
- BLOCK_SOFTIRQ, //???
- TASKLET_SOFTIRQ, //普通的tasklet
- SCHED_SOFTIRQ
- }
核心註冊一個非強制中斷用函數,本質上就是就是初始化數組某一元素
- void open_softirq(int nr, void (*action)(struct softirq_action*), void *data)
- { //nr 即非強制中斷向量編號
- softirq_vec[nr].data = data;
- softirq_vec[nr].action = action;
- }
核心註冊非強制中斷的地方有:
- start_kernel()
- -->init_timers()
- -->open_softirq(TIMER_SOFTIRQ,run_timer_softirq,NULL)
- -->softirq_init()
- -->open_softirq(TASKLET_SOFTIRQ, tasklet_action,NULL)
- -->open_softirq(HI_SOFTIRQ,tasklet_hi_action,NULL)
- -->do_initcall()
- -->net_dev_init()
- -->open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
- -->open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
- -->blk_dev_init()
- -->open_softirq(BLOCK_SOFTIRQ, blk_done_softirq,NULL)
二、非強制中斷觸發
前面註冊完了,現在開始觸發。
核心用一個資料結構來標記曾經有“非強制中斷”發生過(或者說成非強制中斷被觸發過)
__softirq_pending 共32bit,即每個bit對應非強制中斷的一個向量,實際使用了6個bit
第n個bit置1,即softirq_vec[n]有非強制中斷發生。
- typedef struct {
- unsigned int __softirq_pending; /* set_bit is used on this */
- unsigned int __last_jiffy_stamp;
- } ____cacheline_aligned irq_cpustat_t;
- extern irq_cpustat_t irq_stat[]; /* defined in asm/hardirq.h */
- #define __IRQ_STAT(cpu, member) (irq_stat[cpu].member)
- #define local_softirq_pending() \
- __IRQ_STAT(smp_processor_id(), __softirq_pending)
- #define set_softirq_pending(x) (local_softirq_pending() = (x))
- #define or_softirq_pending(x) (local_softirq_pending() |= (x))
- #define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
常用的非強制中斷觸發函數
- void raise_softirq_irqoff(int nr){ //nr 即非強制中斷向量編號
- __raise_softirq_irqoff(nr);
- }
但這隻是“觸發”非強制中斷,非強制中斷並不會立即被處理
三、非強制中斷處理
函數_ _do_softirq是一次性按照向量表從高到低迴圈處理所有非強制中斷(潛台詞,非強制中斷不可嵌套)
_ _do_softirq()的調用時機:
1. irq_exit() 硬體中斷處理完,返回時調用
- do_IRQ() -->irq_exit()
- -->local_softirq_pending()
- -->_ _do_softirq()
2. ksoftirqd() 用於輔助處理非強制中斷的核心線程,每一個CPU上都運行著一個ksoftirqd。
- start_kernel() --> kernel_init() -->do_pre_smp_initcalls()
- -->spawn_ksoftirqd() -->cpu_callback()
- -->kthread_create(ksoftirqd, hcpu, "ksoftirqd/%d", hotcpu)
- -->ksoftirqd()
- -->local_softirq_pending()
- -->_ _do_softirq()
3. local_bh_enable()時,發現有待處理的非強制中斷且當時沒處在軟硬中斷上下文中
- local_bh_enable()
- -->local_softirq_pending()
- -->_ _do_softirq()
處理過程代碼詳解
- asmlinkage void __do_softirq(void)
- {
- struct softirq_action *h;
- __u32 pending;
- int max_restart = MAX_SOFTIRQ_RESTART;
- int cpu;
- pending = local_softirq_pending();
- account_system_vtime(current);
- /*非強制中斷處理中,禁止非強制中斷再次進入,非強制中斷處理是不可重新進入的*/
- __local_bh_disable((unsigned long)__builtin_return_address(0));
- trace_softirq_enter();
- cpu = smp_processor_id();
- restart:
- /* Reset the pending bitmask before enabling irqs
- 下面首先清除pending,以便系統可以啟用其它軟體中斷,
- 然後使能外部中斷
- 系統在下面的處理中,將使能外部中斷以提高系統的響應,
- 注意,必須先清除pending,再使能外部中斷,否則死結*/
- set_softirq_pending(0);
- local_irq_enable();
- h = softirq_vec;
- /*下面按照從高到低調用open_softirq註冊的控制代碼*/
- do {
- if (pending & 1) {
- h->action(h); //關鍵的一句,tasklet、核心timer、網路中斷都是在這裡搞的
- rcu_bh_qsctr_inc(cpu);
- }
- h++;
- pending >>= 1;
- } while (pending);
- local_irq_disable();
- pending = local_softirq_pending();
- if (pending && --max_restart)
- goto restart;
- if (pending)
- wakeup_softirqd();
- trace_softirq_exit();
- account_system_vtime(current);
- _local_bh_enable();
- }
Linux 非強制中斷