Linux Kernel之Deferred work(Softirq、tasklet、Work queues)来龙去脉浅析

 

我们由Linux Kernel中断子系统来龙去脉浅析中可以知道Linux Kernel在处理完一个interrupt后就可能会检查是否有sortirq,如果有,且不在interrupt context中,那么就执行softirq,这也是我们在第5章中断子系统中分析中断的处理全过程的时候未能完成分析的部分,本章将继往开来,将softirq的来龙去脉分析清楚。我们现在再此全面看看asm_do_IRQ到此做了什么:

1.       调用irq_enter处理开始处理该中断的准备工作,其核心的部分在于irq_enter=>__irq_enter=> add_preempt_count(HARDIRQ_OFFSET);,简单来说就是记录下Linux Kernel已经在处理interrupt中。

2.       asm_do_IRQàdesc_handle_irq=>desc->handle_irq(irq, desc);真正处理中断。这里handle_irqhandle_level_irqhandle_edge_irq,但是无论是哪一个最后都要调用handle_IRQ_event来处理中断,handle_IRQ_event在处理完中断后退出前会调用local_irq_disable();关闭ARM global interrupt

3.       调用irq_exit做完成中断处理的善后工作,通过sub_preempt_count(IRQ_EXIT_OFFSET);注:IRQ_EXIT_OFFSET等于HARDIRQ_OFFSET),这是前面的反向操作。此外,我们需要重点关注的是:   

if (!in_interrupt() && local_softirq_pending())

             invoke_softirq();

就是说如果我们不是在interrtupt处理中或是softirq的处理中,且有softirq pending,那么我们就是开始处理softirq

我们通常将softirq叫做interrupt handlingbottom half,之前的interrupt handling、也就是我们在Linux Kernel中断子系统来龙去脉浅析中断子系统中详细分析的叫做top half

本章节我们的任务首先就是从invoke_softirq出发搞清楚softirqtasklet的来龙去脉,并且讨论在设计某个具体的device driver时如何使用softirq以及相关的注意事项。但是再分析这个之前我们有必要先仔细解释一下上面我们看到的如:add_preempt_countsub_preempt_countin_interrupt是什么意思,否则我们无法理解asm_do_IRQ这段code

 

1.1                  struct thread_info概念的介绍

Linux Kernel通过struct task_struct管理每个process相关information、状态等,而其中的一些low levelprocess information记录在struct thread_info中,从struct  thread_info下的task pointer可以找到该processstruct task_struct。从struct task_struct中的stackstack pointer register)也可以找到该struct thread_info

每个process运行在Kernel Mode时,其stack8K bytesthe bottom of stack是这个8K bytes的高地址,the top of the stack存储了该processstruct thread_info,所以Kernel Mode下每个processstack最大只能是8K bytes sizeof(struct thread_info),详见如下:

union thread_union {

  struct thread_info thread_info;

  unsigned long stack[THREAD_SIZE/sizeof(long)];

};

arm linux中(include/asm-arm/thread_info.h:#define THREAD_SIZE            8192

1.1.1             当前processcurrent macro

这个是我们设计device driver时经常使用到的,但是有没有分析过其current的来龙去脉呢?

include/asm-arm/current.h

static inline struct task_struct *get_current(void) __attribute_const__;

static inline struct task_struct *get_current(void)

{

  return current_thread_info()->task;

}

#define current (get_current())

1.1.2             current_thread_info

include/asm-arm/thread_info.h

static inline struct thread_info *current_thread_info(void)

{

  register unsigned long sp asm ("sp");  //sp就是stack pointer register

  return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));

}

1.1.3             preempt_count以及add_preempt_countsub_preempt_count

include/linux/preempt.h

#define add_preempt_count(val)  do { preempt_count() += (val); } while (0)

#define sub_preempt_count(val)  do { preempt_count() -= (val); } while (0)

#define preempt_count()     (current_thread_info()->preempt_count)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值