关于对 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 ...
随机推荐
- 跟Evan学Sprign编程思想 | Spring注解编程模式【译】
Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...
- windows 通过AppInit加载任意dll
windows操作系统允许将用户提供的dll加载到所有的进程的内存空间中.该功能可以用来做后门持久化.有点类似于linux的ld_preload环境变量.在进程启动的时候,操作系统会将用户提供的dll ...
- 时序数据库 Apache-IoTDB 源码解析之文件格式简介(三)
上一章聊到在车联网或物联网中对数据库的需求,以及 IoTDB 的整体架构,详情请见: 时序数据库 Apache-IoTDB 源码解析之系统架构(二) 打一波广告,欢迎大家访问IoTDB 仓库,求一波 ...
- python day01学习
1.python语言 # 89年 龟叔2.python的特点 # 优点 : 简明 简单 跨平台性好 # 缺点 : 慢 -执行速度相对其他语言慢 # 编程语言的分类: # 编译型语言: c c++ j ...
- SpringBoot整合ActiveMQ和开启持久化
一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- Linux/UNIX编程:实现简单 tee 命令
思路很简单,从标准输入文件描述符读入数据,然后同时向标准输出和参数指定的文件写出数据:如果加了 -a 选项,则以追加的方式向文件写出数据.还没了解 getopt() 函数就没判断参数是否合法. #in ...
- Window10和Ubuntu 18.04双系统安装的引导问题解决
作为码农 首先,建议了解下grub2的启动顺序和逻辑.可以参考这篇文章,grub.cfg详解. 从执行顺序倒推,如下如果全部执行成功,则会进入grub的启动菜单:如果最后一步,没有找到grub.cfg ...
- new 的实现原理
自己封装一个new <script> // 创建一个构造函数 function Father() { this.name = '小红'; this.eat = function () { ...
- Python3 (一) 基本类型
前言: 什么是代码? 代码是现实世界事物在计算机世界中的映射. 什么事写代码? 写代码是将现实世界中的事物用计算机语言来描述. 一.数字:整形与浮点型 整型:int 浮点型:float (没有单精度和 ...
- docker集合
docker集合 docker(1):容器技术简介 docker(2):docker的“前身”—lxc docker(3):docker简介 docker(4):docker的安装(centos7)和 ...