In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. Here's the comparison:


Softirqs:

  Softirqs are statically allocated at compile time. It is represented by the softirq_action structure, which is defined in <linux/interrupt.h>:

struct softirq_action {
void (*action)(struct softirq_action *);
}

  A 32-entry array of this structure is declared in kernel/softirq.c:

static struct softirq_action softirq_vec[NR_SOFTIRQS];

  But in the current kernel, only nine exist:(as we will discuss later, tasklets are built off softirqs)

  The prototype of a softirq handler looks like

void softirq_handler(struct softirq_action *)

  A softirq never preempts another softirq. The only event that can preempt a softirq is an interrupt handler.

Executing Softirqs:

  A registered softirq must be marked before it will execute. This is called raising the softirq.

  Softirq execution occurs in __do_softirq(), which is invoked by do_softirq(). If there are pending softirqs, __do_softirq() loops over each one, invoking its handler. Let's look at a simplified variant of the important part of __do_softirq():

u32 pending;

pending = local_softirq_pending();
if (pending) {
struct softirq_action *h; /* reset the pending bitmask */
set_softirq_pending(); h = softirq_vec;
do {
if (pending & )
h->action(h);
h++;
pending >>= ;
} while (pending);
}

Using Softirqs:

  Softirqs are reserved for the most timing-critical and important bottom-half processing on the system.  Currently, only two subsystems - networking and block devices - directly use softirqs.

  Registering Your Handler:

  The softirq handler is registered at run-time via open_softirq():

/* in net/core/dev.c */
/* two parameters: the sfotirq's index and its handler function */
open_softirq(NET_TX_SOFTIRQ, net_tx_action);

  Raising Your Softirq:

  To mark it pending, call raise_softirq():    

raise_softirq(NET_TX_SOFTIRQ);

  Then it is run at the next invocation of do_softirq().

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);
}

  I have a look at __do_softirq() and I think it's too long to show here, so I just pass it :)

  In general, pending softirqs are checked for and executed in the following places:

    In the return from hardware interrupt code path;

    In the ksoftirqd kernel thread;

    In any code that explicitly checks for and executes pending softirqs, such as the networking subsystem.


Tasklets:

  Tasklets are built on top of softirqs and it's more popular. The difference is that two of the same type of tasklet cannot run simultaneously on different processors but softirqs can.

  As discussed, tasklets are represented by two softirqs: HI_SOFTIRQ and TASKLET_SOFTIRQ.

  The tasklet sturcture is declared in <linux/interrupt.h>:

struct tasklet_struct {
struct tasklet_struct *next;
unsigned long state;
atomic_t count;
void (*func)(unsigned long); /* tasklet handler function */
unsigned long data; /* argument to the tasklet function */
};

Schedulling Tasklets:

  Tasklets are scheduled via the tasklet_schedule() and tasklet_hi_schedule()(for high-priority tasklets):

static inline vid tasklet_schedule(struct tasklet_struct *t)
{
if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
__tasklet_schedule(t);
}

  Here's the __tasklet_schedule():

void __tasklet_schedule(struct tasklet_struct *t)
{
unsigned long flags; /* save the state of interrupt system, and then disable local interrupts. */
local_irq_save(flags);
t->next = NULL;
/* add the tasklet to be scheduled to the tail of the tasklet_vec linked list */
*__get_cpu_var(tasklet_vec).tail = t;
__get_cpu_var(tasklet-vec).tail = &(t->next);
/* raise the TASKLET_SOFTIRQ, so do_softirq() executes this tasklet in the near future */
local_irq_restore(flags);
}

  Then the do_softirq() will execute the associated handlers tasklet_action() soon.  

 ksoftirqd:

  Softirq (and thus tasklet) processing is aided by a set of per-processor kernel threads. The kernel processes softirqs most commonly on return from handling an interrupt.

  There is one thread per processor. The threads are each named ksoftirqd/n where n is the processor number.


Work Queue:

  Work queues defer work into a kernel thread - this bottom half always runs in process context. Therefore, work queues are schedulable and can therefore sleep.

  In its most basic form, the work queue subsystem is an interface fro creating kernel threads, which are called worker threads, to handle work queued from elsewhere.

  The default worker threads are called events/n where n is the processor number.

LKD: Chapter 8 Bottom Halves and Deferring Work的更多相关文章

  1. LKD: Chapter 7 Interrupts and Interrupt Handlers

    Recently I realized my English is still far from good. So in order to improve my English, I must not ...

  2. LKD: Chapter 9 An Introduction to Kernel Synchronization

    This chapter introduces some conception about kernel synchronization generally. Critical Regions: Co ...

  3. LKD: Chapter 6 Kernel Data Structures

    这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees. Linked Lists:(<linux/list.h>) 在lin ...

  4. LKD: Chapter 5 System Call

    在Linux中,处理器所作的事可以归纳为3种情况: 1.In user-space, executing user code in a process; 2.In kernel-space, in p ...

  5. linux内核申请内存函数

    kmap函数:    把某块高端内存映射到页表,然后返回给用户一个填好vitual字段的page结构    建立永久地址映射,不是简单的返回virtual字段的pageioremap:    驱动程序 ...

  6. kernel笔记——中断

    cpu与磁盘.网卡.键盘等外围设备(相对于cpu和内存而言)交互时,cpu下发I/O请求到这些设备后,相对cpu的处理能力而言,磁盘.网卡等设备需要较长时间完成请求处理. 那么在请求发出到处理完成这段 ...

  7. linux工作队列

    工作队列一般用来做滞后的工作,比如在中断里面要做很多事,但是比较耗时,这时就可以把耗时的工作放到工作队列.说白了就是系统延时调度的一个自定义函数. 工作队列是实现延迟的新机制,从 2.5 版本 Lin ...

  8. 软中断与硬中断 & 中断抢占 中断嵌套

    参考了这篇文章:http://blog.csdn.net/zhangskd/article/details/21992933 从本质上来讲,中断是一种电信号,当设备有某种事件发生时,它就会产生中断,通 ...

  9. 专家解读Linux操作系统内核中的GCC特性

    专家解读Linux操作系统内核中的GCC特性   Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...

随机推荐

  1. Hibernate 学习笔记 - 2

    五.映射一对多关联关系 1. 单向多对一 即 单向 n-1 1)单向 n-1 关联只需从 n 的一端可以访问 1 的一端 ① 域模型: 从 Order 到 Customer 的多对一单向关联需要在Or ...

  2. 当谈到 GitLab CI 的时候,我们该聊些什么(上篇)

    "微服务"这个概念近两年非常热,正在慢慢改变 DevOps 的思路.微服务架构把一个庞大的业务系统拆解开来,每一个组件变得更加独立自治.松耦合.但是,同时也伴随着部署单元粒度越来越 ...

  3. 从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建

    从零开始搭建框架SSM+Redis+Mysql(二)之MAVEN项目搭建 废话不说,直接撸步骤!!! 1.创建主项目:ncc-parent 选择maven创建项目,注意在创建项目中,packing选择 ...

  4. FormView控件下DropDownList是否可以绑定

    在网站下,FormView控件下是可以通过绑定DropDownList的SelectedValue属性来绑定字段来显示 举例: 1 <asp:DropDownList ID="cboU ...

  5. win10 uwp 隐藏实时可视化

    新的vs有个功能,实时可视化 但是他会挡我们界面,想要隐藏 点击转到实时可视化,就是点击横线看到,接着就可以看到下面的选项 点击在应用程序中显示运行时,就是不选中 很简单就看到,没有那个 本作品采用知 ...

  6. 细品 - 逻辑回归(LR)

    1. LR的直观表述 1.1 直观表述 今天我们来深入了解一个人见人爱,花见花开,工业界为之疯狂,学术界..额,好像学术界用的不多哎.不过没关系,就算学术界用的不多也遮不住它NB的光芒,它就是LR模型 ...

  7. maven的介绍

    刚来通信行业的国企上班,面试的时候很尴尬的问道"maven是干什么的?"""maven是项目管理工具吗?是怎么管理的?(理解类似于协同等办公OA一样的软件了)& ...

  8. 【JAVA零基础入门系列】Day9 Java中的那个大数值

    什么是大数值?用脚趾头想也知道,当然是"大"的数值(233).Java中有两个用于表示大数值的类,BigInteger和BigDecimal,那到底能表示多大的数值呢?理论上,可以 ...

  9. 忘记root密码,进入单用户模式修改密码

    进入单用户模式 rhel61.在系统数秒时,按下键,进入到系统引导菜单 中2.选择系统后 按“e”键 选择kernel后 按“e”键 后空格 1+回车 b:启动系统 进入到单用户模式 rhel71.在 ...

  10. 阿里巴巴Java开发手册思维导图

    趁着有时间把阿里巴巴Java开发手册又看了一遍了,很多时候觉得看完之后,发现自己好像一点都不记得了里面的内容了.只能把大概内容画一遍在脑子里形成一张图方便记忆,这样就更能够记得自己的看完的内容了.其中 ...