关于对 softirq、work_queue、tasklet 学习后的一点总结
本文基于linux版本:4.14.111
简单的总结下 softirq、work_queue、tasklet 三种中断下半部的工作原理及区别,并附上三种形式的简单实例。
一、运行原理
① softirq:
void __do_softirq(void)
{
int max_restart = MAX_SOFTIRQ_RESTART; ///< 10
struct softirq_action *h;
...
pending = local_softirq_pending(); ///< 获取到当前的 pending 结果,也就是各个 softirq 的位或结果
__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET); ///< 标识进入了 softirq,并禁止 preempt
while ((softirq_bit = ffs(pending))) {
...
/* 取出相应的 action 并执行 */
h->action(h);
....
} if (pending) {
/* 如果 softirq 整体运行完一遍后仍有 softirq 请求,那么将再次 restart 运行,最多运行 10 遍 */
if (time_before(jiffies, end) && !need_resched() &&
--max_restart)
goto restart; /* 超过了 10 遍之后,不再 restart 运行,将请求交给处理 softirq 的内核线程,之后开启调度不占用过多时间片 */
wakeup_softirqd();
}
...
}
关于处理 softirq 请求的内核线程:
static void wakeup_softirqd(void)
{
struct task_struct *tsk = __this_cpu_read(ksoftirqd); if (tsk && tsk->state != TASK_RUNNING)
wake_up_process(tsk); ///< 唤醒 ksoftirqd
} static void run_ksoftirqd(unsigned int cpu)
{
local_irq_disable();
if (local_softirq_pending()) {
__do_softirq(); ///< 执行 __do_softirq
local_irq_enable();
cond_resched_rcu_qs();
return;
}
local_irq_enable();
}
可见,ksoftirqd 的处理方式也同样是通过调用 __do_softirq 来运行 softirq。
softirq 的触发方式:
1) 通过 __do_softirq 主动触发,通常在硬中断退出时,即 irq_exit,也会通过调用 invoke_softirq() -> __do_softirq 来触发软中断来执行下半部的 ISR(Interrupt Service Routines);
2) 通过 ksoftirqd 被动触发;
② work_queue:依赖的就是内核线程,会在之后的文章中详细说明一下,并会在此处附上链接(以 SPI Flash 驱动中的 kthread 相关操作为例)。
③ tasklet:是 softirq 中的一个 action,可理解为是一个特殊的 softirq,并在 softirq_init 时就得到初始化,其回调函数为 tasklet_action,这里不在阐述,同样运行在中断上下文。
二、工作方式的区别
softirq 与 tasklet 运行在中断上下文,运行期间不可出现 sleep 休眠、阻塞等操作,work_queue 运行在进程上下文,可以进行休眠、阻塞、发生调度等。
三、测试实例及运行结果
① softirq:
1) 第一部分是对内核的修改,因为 softirq 只能静态创建,修改文件为 /kernel/softirq.c 补丁文件如下:
const char * const softirq_to_name[NR_SOFTIRQS] = {
"HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
- "TASKLET", "SCHED", "HRTIMER", "RCU"
+ "TASKLET", "SCHED", "HRTIMER", "RCU", "LANCE_TEST"
};
/*
@@ -441,6 +441,7 @@ void raise_softirq(unsigned int nr)
raise_softirq_irqoff(nr);
local_irq_restore(flags);
}
+EXPORT_SYMBOL(raise_softirq);
+static __latent_entropy void softirq_test_action(struct softirq_action *a)
+{
+ printk("This is softirq test.\n");
+}
+
void __init softirq_init(void)
{
int cpu;
@@ -652,6 +658,8 @@ void __init softirq_init(void)
open_softirq(TASKLET_SOFTIRQ, tasklet_action);
open_softirq(HI_SOFTIRQ, tasklet_hi_action);
+
+ open_softirq(LANCE_TEST, softirq_test_action);
}
2) 编写测试模块:
#include <linux/export.h>
#include <linux/kernel_stat.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/notifier.h>
#include <linux/percpu.h>
#include <linux/cpu.h>
#include <linux/freezer.h>
#include <linux/kthread.h>
#include <linux/rcupdate.h>
#include <linux/ftrace.h>
#include <linux/smp.h>
#include <linux/smpboot.h>
#include <linux/tick.h>
#include <linux/irq.h> #include <linux/module.h>
#include <linux/init.h>
#include <linux/delay.h> static int softirq_test_init(void)
{
raise_softirq(LANCE_TEST);
msleep();
raise_softirq(LANCE_TEST); return ;
} static void softirq_test_exit(void)
{
} module_init(softirq_test_init);
module_exit(softirq_test_exit);
MODULE_LICENSE("GPL");
3) 安装模块后测试结果:
cat proc/softirqs
CPU0 CPU1 CPU2 CPU3
LANCE_TEST: / # insmod softirq.ko
[ 9544.236966] This is softirq test.
[ 9545.252842] This is softirq test.
② work_queue:
1) 编写测试模块:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/time.h>
#include <linux/delay.h> static struct work_struct lance_work = {};
static struct workqueue_struct *queue; void work_queue_cb(void)
{
printk(KERN_EMERG "This is work_queue test.\n");
printk("Cur in interrupt context? %s.\n", (in_interrupt() ? "Yes" : "No"));
} static int work_queue_init(void)
{
queue = create_workqueue("work_queue_lance");
INIT_WORK(&lance_work, (typeof(lance_work.func))work_queue_cb);
queue_work(queue, &lance_work); msleep();
queue_work(queue, &lance_work); return ;
} static void work_queue_exit(void)
{
destroy_workqueue(queue);
} module_init(work_queue_init);
module_exit(work_queue_exit);
MODULE_LICENSE("GPL");
2) 安装模块后测试结果:
/ # insmod work_queue.ko
[12633.889983] This is work_queue test.
[12633.893577] Cur in interrupt context? No.
[12634.917039] This is work_queue test.
/ # [12634.920623] Cur in interrupt context? No.
③ tasklet:
1) 编写测试模块:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/interrupt.h> static struct tasklet_struct tasklet = {};
static struct timer_list timer = {}; static void task_func(unsigned long data)
{
printk("This is tasklet test.\n");
printk("Cur in interrupt context? %s.\n", (in_interrupt() ? "Yes" : "No"));
//msleep(1000); ///< 会导致崩溃
} void timer_handler(unsigned long data)
{
tasklet_schedule(&tasklet);
mod_timer(&timer, jiffies+msecs_to_jiffies());
} static int tasklet_test_init(void)
{
tasklet_init(&tasklet, task_func, );
init_timer(&timer); timer.function = timer_handler;
timer.expires = jiffies + HZ;
add_timer(&timer); return ;
} static void tasklet_test_exit(void)
{
del_timer(&timer);
tasklet_disable(&tasklet);
} module_init(tasklet_test_init);
module_exit(tasklet_test_exit);
MODULE_LICENSE("GPL");
2) 安装模块后测试结果:
/ # insmod tasklet.ko
/ # [12477.508765] This is tasklet test.
[12477.512089] Cur in interrupt context? Yes.
[12479.524783] This is tasklet test.
[12479.528108] Cur in interrupt context? Yes.
四、文末总结下在编写一个驱动时, 如果选用这三种工作方式
从根本上来说,如果有休眠阻塞的需要,work_queue 是唯一的选择;
否则最好用 tasklet,如果必须专注于性能的提高,那么就要考虑 softirq,但使用难度较大一些,要注意程序的可重入性。
关于对 softirq、work_queue、tasklet 学习后的一点总结的更多相关文章
- 【原创】Linux中断子系统(三)-softirq和tasklet
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- phpunit测试学习 1:一点简单的扼要有用的东西的总结 一点入门认识
16:45 2015/12/8phpunit测试学习 1:一点简单的扼要有用的东西的总结 一点入门认识 具体的入门安装和入门实践请参照文中的推荐博客或网上其他博客推荐博客,我感觉这几篇博客写得很不错 ...
- 【转载】从创业者角度看《印度合伙人 Padman》后的一点感受
***************************** 这部电影看简介是真实事件改编的,当时除了电影本身的精彩和主角宠妻狂魔之外,印象最深的就是感觉到主角的创业者心态是一步步在生活中被培养的.特别 ...
- 学习javascript 的一点感想
原文:学习javascript 的一点感想 //动态性是指,在一个Javascript对象中,要为一个属性赋值,我们不必事先创建一个字段,只需要在使用的时候做赋值操作即可,如下例:var obj=ne ...
- 2020Java程序员架构师面试宝典,学习后面试必过,震惊,本人通过这篇教程,拿到了0个offer
1. 引言 Java后端学习路线 <吐血整理>顶级程序员工具集 https://github.com/AobingJava/JavaFamily 跟上Java8 经历阿里.头条.腾讯等知名 ...
- SpringCloud学习后获取的地址
关于SpringCloud + Docker 学习地址: (1) https://yq.aliyun.com/articles/57265 (2) https://yq.aliyun.com/team ...
- AE-分享<学习后,制作的视频实例>小视频-与大家交流!
- 从创业者角度看《印度合伙人 Padman》后的一点感受
最近对印度电影颇有兴趣,周末在家看了<印度合伙人 Padman>.本文试着从一名创业者视角,谈谈个人的一点看法. 0.故事简介 引用自 https://movie.douban.com/s ...
- 学习Git的一点心得以及如何把本地修改、删除的代码上传到github中
一:学习Github的资料如下:https://git.oschina.net/progit/ 这是一个学习Git的中文网站,如果诸位能够静下心来阅读,不要求阅读太多,只需要阅读前三章,就可以掌握Gi ...
随机推荐
- artTemplate--模板使用自定义函数(1)
案例 因为公司业务需要频繁调用接口,后端返回的都是json树对象,需要有些特殊的方法做大量判断和数据处理,显然目前简单语法已经不能满足业务需要了,需要自己定制一些 方法来处理业务逻辑. 例如后台返回的 ...
- <状压DP>solution-HDU5691_Sitting in Line
Sitting in Line Problem Description 度度熊是他同时代中最伟大的数学家,一切数字都要听命于他.现在,又到了度度熊和他的数字仆人们玩排排坐游戏的时候了.游戏的规则十分简 ...
- cmd 重定向
关于cmd 命令的重定向输出 2>&1 mycommand >mylog.txt 2>&1 应该是最经典的用法了. 命令的结果可以通过" %> &qu ...
- appcompat_v7 res values-v21 error
[2014-11-03 11:30:25 - AndroidApp] appcompat_v7/res/values-v21/styles_base.xml:75: error: Error retr ...
- 应用层vc实现三种文件监视方法
http://hi.baidu.com/sadusaga/item/daa0d4b764c6dd76254b09cc http://bbs.csdn.net/topics/280032788 http ...
- 教你5分钟做个手机APP[视频]
天天宅在家里,没什么事做,录个教学视频吧! 发到了视频网站上去根本没人看,伤心ing啊! 不知cnblogs上面是否让我发! 先上一张效果图看看哈: 如果播放不正常请点这里:https://www.b ...
- Codeforces 1197E Count The Rectangles(树状数组+扫描线)
题意: 给你n条平行于坐标轴的线,问你能组成多少个矩形,坐标绝对值均小于5000 保证线之间不会重合或者退化 思路: 从下到上扫描每一条纵坐标为y的水平的线,然后扫描所有竖直的线并标记与它相交的线,保 ...
- learn about sqlserver files and filegroup
The filegroup is similar as tablespace in Oracle. At first, I will show that hot to check file amd f ...
- Java程序员都需要懂的「反射」
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 今天来简单写一下Java的反射.本来没打算写反射 ...
- 机器学习(ML)十三之批量归一化、RESNET、Densenet
批量归一化 批量归一化(batch normalization)层,它能让较深的神经网络的训练变得更加容易.对图像处理的输入数据做了标准化处理:处理后的任意一个特征在数据集中所有样本上的均值为0.标准 ...