Chinaunix首页 | 论坛 | 博客
  • 博客访问: 759554
  • 博文数量: 247
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 501
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-12 21:53
个人简介

系统未建立

文章分类

全部博文(247)

文章存档

2021年(1)

2020年(3)

2019年(5)

2018年(3)

2017年(44)

2016年(75)

2015年(52)

2014年(63)

2013年(1)

我的朋友

分类: LINUX

2019-01-25 09:25:44

      Linux 的softirq机制是与SMP紧密不可分的。为此,整个softirq机制的设计与实现中自始自终都贯彻了一个思想:“谁触发,谁执行”(Who marks,Who runs),也即触发软中断的那个CPU负责执行它所触发的软中断,而且每个CPU都由它自己的软中断触发与控制机制。这个设计思想也使得softirq 机制充分利用了SMP系统的性能和特点。 多个softirq可以并行执行,甚至同一个softirq可以在多个processor上同时执行。
 
一、softirq的实现
     每个softirq在内核中通过struct softirq_action来表示,另外,通过全局属组softirq_vec标识当前内核支持的所有的softirq。
  1. /* softirq mask and active fields moved to irq_cpustat_t in
  2.  * asm/hardirq.h to get better cache usage. KAO
  3.  */

  4. struct softirq_action
  5. {
  6.     void    (*action)(struct softirq_action *);
  7. };

  8. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
Linux内核最多可以支持32个softirq(思考:为什么是32个?),但当前只实现了10个,如下:
  1. enum
  2. {
  3.     HI_SOFTIRQ=0,
  4.     TIMER_SOFTIRQ,
  5.     NET_TX_SOFTIRQ,
  6.     NET_RX_SOFTIRQ,
  7.     BLOCK_SOFTIRQ,
  8.     BLOCK_IOPOLL_SOFTIRQ,
  9.     TASKLET_SOFTIRQ,
  10.     SCHED_SOFTIRQ,
  11.     HRTIMER_SOFTIRQ,
  12.     RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */

  13.     NR_SOFTIRQS
  14. };
二、softirq处理函数
 
    struct softirq_action结构体中,只有一个函数指针成员action,即指向用户定义的softirq处理函数。当执行时,可以通过如下代码:
                     softirq_vec[i]->action(i);
    一个注册的softirq在执行之前必须被激活,术语称为"raise the softirq"。被激活的softirq通常并不会立即执行,一般会在之后的某个时刻检查当前系统中是否有被pending的softirq,如果有就去执行,Linux内核中检查是否有softirq挂起的检查点主要有以下三类:
(1)硬件中断代码返回的时候
  1. /*
  2.  * Exit an interrupt context. Process softirqs if needed and possible:
  3.  */
  4. void irq_exit(void)
  5. {
  6.     account_system_vtime(current);
  7.     trace_hardirq_exit();
  8.     sub_preempt_count(IRQ_EXIT_OFFSET);
  9.     if (!in_interrupt() && local_softirq_pending())
  10.         invoke_softirq();

  11.     rcu_irq_exit();
  12. #ifdef CONFIG_NO_HZ
  13.     /* Make sure that timer wheel updates are propagated */
  14.     if (idle_cpu(smp_processor_id()) && !in_interrupt() && !need_resched())
  15.         tick_nohz_stop_sched_tick(0);
  16. #endif
  17.     preempt_enable_no_resched();
  18. }
(2)ksoftirqd内核服务线程运行的时候
  1. static int run_ksoftirqd(void * __bind_cpu)
  2. {
  3.     ... ...
  4.         while (local_softirq_pending()) {
  5.             /* Preempt disable stops cpu going offline.
  6.              If already offline, we'll be on wrong CPU:
  7.              don't process */
  8.             if (cpu_is_offline((long)__bind_cpu))
  9.                 goto wait_to_die;
  10.             do_softirq();
  11.             preempt_enable_no_resched();
  12.             cond_resched();
  13.             preempt_disable();
  14.             rcu_note_context_switch((long)__bind_cpu);
  15.         }
  16.         preempt_enable();
  17.         set_current_state(TASK_INTERRUPTIBLE);
  18.     }
  19.     __set_current_state(TASK_RUNNING);
  20.     return 0;
  21. ... ...
  22. }
(3)在一些内核子系统中显示的去检查挂起的softirq
  1. int netif_rx_ni(struct sk_buff *skb)
  2. {
  3.     int err;

  4.     preempt_disable();
  5.     err = netif_rx(skb);
  6.     if (local_softirq_pending())
  7.         do_softirq();
  8.     preempt_enable();

  9.     return err;
  10. }
下面重点分析以下do_softirq(),了解Linux内核到底是怎么来处理softirq的。
  1. asmlinkage void do_softirq(void)
  2. {
  3.     unsigned long flags;
  4.     struct thread_info *curctx;
  5.     union irq_ctx *irqctx;
  6.     u32 *isp;

  7.     if (in_interrupt()) /*这个函数需要仔细理解???*/
  8.         return;

  9.     local_irq_save(flags);

  10.     if (local_softirq_pending()) {
  11.         curctx = current_thread_info();
  12.         irqctx = __get_cpu_var(softirq_ctx);
  13.         irqctx->tinfo.task = curctx->task;
  14.         irqctx->tinfo.previous_esp = current_stack_pointer;

  15.         /* build the stack frame on the softirq stack */
  16.         isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));

  17.         call_on_stack(__do_softirq, isp);
  18.         /*
  19.          * Shouldnt happen, we returned above if in_interrupt():
  20.          */
  21.         WARN_ON_ONCE(softirq_count());
  22.     }

  23.     local_irq_restore(flags);
  24. }
do_softirq主要是完成了以下几个功能:
(1)检查当前processor上是否有pending的softirq
(2)如果有pending的softirq,为softirq的处理建立新的堆栈,即建立新的软中断上下文环境
(3)处理软中断__do_softirq
这里需要重点分析一下in_interrupt()函数的含义。在linux内核中,为了方便判断当前执行路径在哪个上下文环境中,定义了几个接口:
  1. #define hardirq_count() (preempt_count() & HARDIRQ_MASK)
  2. #define softirq_count() (preempt_count() & SOFTIRQ_MASK)
  3. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \
  4.      | NMI_MASK))
  5. /*
  6.  * Are we doing bottom half or hardware interrupt processing?
  7.  * Are we in a softirq context? Interrupt context?
  8.  */
  9. #define in_irq() (hardirq_count())
  10. #define in_softirq() (softirq_count())
  11. #define in_interrupt() (irq_count())
  12. /*
  13.  * Are we in NMI context?
  14.  */
  15. #define in_nmi() (preempt_count() & NMI_MASK)
从注释可以看出包括:硬件中断上下文,软件中断上下文,不可屏蔽上下文等。在这些宏中,都涉及到了preempt_count()这个宏,这个宏是一个比较重要的宏,在Linux源码中对其做了详细的注释:
  1. /*
  2.  * We put the hardirq and softirq counter into the preemption
  3.  * counter. The bitmask has the following meaning:
  4.  *
  5.  * - bits 0-7 are the preemption count (max preemption depth: 256)
  6.  * - bits 8-15 are the softirq count (max # of softirqs: 256)
  7.  *
  8.  * The hardirq count can in theory reach the same as NR_IRQS.
  9.  * In reality, the number of nested IRQS is limited to the stack
  10.  * size as well. For archs with over 1000 IRQS it is not practical
  11.  * to expect that they will all nest. We give a max of 10 bits for
  12.  * hardirq nesting. An arch may choose to give less than 10 bits.
  13.  * m68k expects it to be 8.
  14.  *
  15.  * - bits 16-25 are the hardirq count (max # of nested hardirqs: 1024)
  16.  * - bit 26 is the NMI_MASK
  17.  * - bit 28 is the PREEMPT_ACTIVE flag
  18.  *
  19.  * PREEMPT_MASK: 0x000000ff
  20.  * SOFTIRQ_MASK: 0x0000ff00
  21.  * HARDIRQ_MASK: 0x03ff0000
  22.  * NMI_MASK: 0x04000000
  23.  */
从注释可以看出,preempt_count各个bit位的含义:
(1)bit0~7位表示抢占计数,即支持最大的抢占深度为256
(2)bit8~15位表示软中断计数,即支持最大的软中断的个数为256,需要注意的是,由于软中断还受制于pending状态,一个32位的变量,因此实际最大只能支持32个软中断。
(3)bit16~25位表示硬件中断嵌套层数,即最大可支持的嵌套层次为1024,实际情况下这是不可能的,因为中断的嵌套层数还受制于中断处理的栈空间的大小。
    介绍了这么多,现在了重点分析下上面提到的in_interrupt到底表示什么意思?
  1. #define in_interrupt() (irq_count())

  2. #define irq_count() (preempt_count() & (HARDIRQ_MASK | SOFTIRQ_MASK \

  3. | NMI_MASK))
从其宏定义可以看出,in_interrupt宏的值是硬件中断嵌套层数,软中断计数以及可屏蔽中断三者之和。回到do_softirq的代码中,如果in_interrupt的值大于0,就不会处理软中断,意思是当有硬件中断嵌套,其他软中断以及不可屏蔽中断的情况下,不会去处理软中断。对于中断的嵌套层数以及不可屏蔽中断是比较好理解的,对于软中断,应该去分析以下,在什么地方软中断的计数会增加:
  1. __local_bh_disable((unsigned long)__builtin_return_address(0));
  2. static inline void __local_bh_disable(unsigned long ip)
  3. {
  4.     add_preempt_count(SOFTIRQ_OFFSET);
  5.     barrier();
  6. }
  7. # define add_preempt_count(val)    do { preempt_count() += (val); } while (0)
从代码可以看出,禁止中断下半部分的函数会增加软中断的计数,即当有软中断的do_softirq在进行处理时,如果此时被硬件中断打断,而且在硬件中断中又激活了优先级更高的软中断,当硬件中断退出时,那么当再去执行do_softirq时,此时in_interrupt > 0,岂不是死锁了!!!希望大家指教。
实际的处理函数为__do_softirq:
  1. asmlinkage void __do_softirq(void)
  2. {
  3.     struct softirq_action *h;
  4.     __u32 pending;
  5.     int max_restart = MAX_SOFTIRQ_RESTART; /*不启动ksoftirqd之前,最大的处理softirq的次数,经验值*/
  6.     int cpu;
  7.     /*取得当前被挂起的softirq,同时这里也解释了为什么Linux内核最多支持32个softirq,因为pending只有32bit*/
  8.     pending = local_softirq_pending()
  9.     account_system_vtime(current);

  10.     __local_bh_disable((unsigned long)__builtin_return_address(0));
  11.     lockdep_softirq_enter();

  12.     cpu = smp_processor_id();
  13. restart:
  14.     /* Reset the pending bitmask before enabling irqs */
  15.     set_softirq_pending(0);/*获取了pending的softirq之后,清空所有pending的softirq的标志*/

  16.     local_irq_enable();

  17.     h = softirq_vec;

  18.     do {
  19.         if (pending & 1) { /*从最低位开始,循环右移逐位处理pending的softirq*/
  20.             int prev_count = preempt_count();
  21.             kstat_incr_softirqs_this_cpu(h - softirq_vec);

  22.             trace_softirq_entry(h, softirq_vec);
  23.             h->action(h); /*执行softirq的处理函数*/
  24.             trace_softirq_exit(h, softirq_vec);
  25.             if (unlikely(prev_count != preempt_count())) {
  26.                 printk(KERN_ERR "huh, entered softirq %td %s %p"
  27.                  "with preempt_count %08x,"
  28.                  " exited with %08x?\n", h - softirq_vec,
  29.                  softirq_to_name[h - softirq_vec],
  30.                  h->action, prev_count, preempt_count());
  31.                 preempt_count() = prev_count;
  32.             }

  33.             rcu_bh_qs(cpu);
  34.         }
  35.         h++;
  36.         pending >>= 1;  /*循环右移*/
  37.     } while (pending);

  38.     local_irq_disable();

  39.     pending = local_softirq_pending();
  40.     if (pending && --max_restart)  /*启动ksoftirqd的阈值*/
  41.         goto restart;

  42.     if (pending)  /*启动ksoftirqd去处理softirq,此时说明pending的softirq比较多,比较频繁,上面的处理过程中,又不断有softirq被pending*/
  43.         wakeup_softirqd();

  44.     lockdep_softirq_exit();

  45.     account_system_vtime(current);
  46.     _local_bh_enable();
三、使用softirq
     softirq一般用在对实时性要求比较强的地方,当前的Linux内核中,只有两个子系统直接使用了softirq:网络子系统和块设备子系统。另外,增加新的softirq需要重新编译内核,因此,除非必须需要,最好考虑tasklet和kernel timer是否适合当前需要。
     如果必须需要使用softirq,那么需要考虑的一个重要的问题就是新增加的softirq的优先级,默认情况下,softirq的数值越小优先级越高,根据实际经验,新增加的softirq最好在BLOCK_SOFTIRQ和TASKLET_SOFTIRQ之间。
     softirq的处理函数通过open_softirq进行注册,此函数接收两个参数,一个是softirq的整数索引,另一个是该softirq对应的处理函数。例如在网络子系统中,注册了如下两个softirq及其处理函数:
    open_softirq(NET_TX_SOFTIRQ, net_tx_action);
    open_softirq(NET_RX_SOFTIRQ, net_rx_action);
    前面提到,软中断处理函数注册后,还需要将该软中断激活,此软中断才能被执行,激活操作是通过raise_softirq函数来实现,在网络子系统中激活代码如下:
  1. /* Called with irq disabled */
  2. static inline void ____napi_schedule(struct softnet_data *sd,
  3.                  struct napi_struct *napi)
  4. {
  5.     list_add_tail(&napi->poll_list, &sd->poll_list);
  6.     __raise_softirq_irqoff(NET_RX_SOFTIRQ);
  7. }
这里的__raise_softirq_irqoff和raise_softirq的区别是,前者在事先已经关中断的情况下可以被使用,后者自己完成中断的关闭和恢复。
 
阅读(4049) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~