下面是软中断的执行
- /*如果在这样的一个检查点(local_softirq_pending()不为0)
- 检测到挂起的软中断,内核调用下面函数处理*/
- asmlinkage void do_softirq(void)
- {
- __u32 pending;
- unsigned long flags;
- if (in_interrupt())
- return;
- local_irq_save(flags);
- pending = local_softirq_pending();
- if (pending)
- __do_softirq();
- local_irq_restore(flags);
- }
具体由__do_softirq函数实现
- /*读取本地CPU的软中断掩码并执行与每个设置位
- 相关的可延迟函数,__do_softirq只做固定次数的循环
- 然后就返回。如果还有其余挂起的软中断,那么
- 内核线程ksofirqd将会在预期的时间内处理他们*/
- asmlinkage void __do_softirq(void)
- {
- struct softirq_action *h;
- __u32 pending;
- /*把循环计数器的值初始化为10*/
- int max_restart = MAX_SOFTIRQ_RESTART;
- int cpu;
- /*把本地CPU(被local_softirq_pending选中的)软件中断的
- 位掩码复制到局部变量pending中*/
- pending = local_softirq_pending();
- account_system_vtime(current);
- /*增加软中断计数器的值*/
- __local_bh_disable((unsigned long)__builtin_return_address(0));
- lockdep_softirq_enter();
- cpu = smp_processor_id();
- restart:
- /* Reset the pending bitmask before enabling irqs */
- set_softirq_pending(0);/*清除本地CPU的软中断位图,
- 以便可以激活新的软中断*/
- /*激活本地中断*/
- local_irq_enable();
- h = softirq_vec;
- do {/*根据pending每一位的的设置,执行对应的软中断
- 处理函数*/
- if (pending & 1) {
- int prev_count = preempt_count();
- kstat_incr_softirqs_this_cpu(h - softirq_vec);
- trace_softirq_entry(h, softirq_vec);
- h->action(h);/*执行注册的具体的软中断函数*/
- trace_softirq_exit(h, softirq_vec);
- if (unlikely(prev_count != preempt_count())) {
- printk(KERN_ERR "huh, entered softirq %td %s %p"
- "with preempt_count %08x,"
- " exited with %08x?\n", h - softirq_vec,
- softirq_to_name[h - softirq_vec],
- h->action, prev_count, preempt_count());
- preempt_count() = prev_count;
- }
- rcu_bh_qs(cpu);
- }
- h++;
- pending >>= 1;
- } while (pending);
- local_irq_disable();
- pending = local_softirq_pending();
- if (pending && --max_restart)
- goto restart;
- if (pending)/*如果还有挂起的软中断,唤醒内核线程
- 来处理本地CPU的软中断*/
- wakeup_softirqd();
- lockdep_softirq_exit();
- account_system_vtime(current);
- _local_bh_enable();/*软中断计数器-1,因而重新激活可延迟函数*/
- }
到此,linux内核软中断的大致执行和实现基本上分析完了,中间有很多地方没有注释的,主要是考虑到需要别的实现机制以及有的比较易懂。能够自己看懂。
