Linux 软中断
本文转载自: http://blog.chinaunix.net/uid-9620812-id-3833377.html,如有需要,请移步访问。
---------------------------------------我是分割线----------------------------------------
一、软中断注册    
和硬中断类似,软中断也有类似的中断向量表,只不过是用“软件”实现的。    
struct softirq_action softirq_vec[32]是软中断向量表   
(文件linux_2_6_24/kernel/softirq.c)
- struct softirq_action
- {
- void (*action)(struct softirq_action *); //钩子函数
- void *data; //钩子函数的形参
- };
内核用到的中断向量(其实就是数组下标)如下所示   
(文件linux_2_6_24/include/linux/interrupt.h)
- enum{
- HI_SOFTIRQ=0, //高优先级的tasklet
- TIMER_SOFTIRQ, //内核定时器
- NET_TX_SOFTIRQ, //网络发送
- NET_RX_SOFTIRQ, //网络接收
- BLOCK_SOFTIRQ, //???
- TASKLET_SOFTIRQ, //普通的tasklet
- SCHED_SOFTIRQ
- }
内核注册一个软中断用函数,本质上就是就是初始化数组某一元素
- void open_softirq(int nr, void (*action)(struct softirq_action*), void *data)
- { //nr 即软中断向量编号
- softirq_vec[nr].data = data;
- softirq_vec[nr].action = action;
- }
内核注册软中断的地方有:
- start_kernel()
- -->init_timers()
- -->open_softirq(TIMER_SOFTIRQ,run_timer_softirq,NULL)
- -->softirq_init()
- -->open_softirq(TASKLET_SOFTIRQ, tasklet_action,NULL)
- -->open_softirq(HI_SOFTIRQ,tasklet_hi_action,NULL)
- -->do_initcall()
- -->net_dev_init()
- -->open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
- -->open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
- -->blk_dev_init()
- -->open_softirq(BLOCK_SOFTIRQ, blk_done_softirq,NULL)
二、软中断触发    
前面注册完了,现在开始触发。    
内核用一个数据结构来标记曾经有“软中断”发生过(或者说成软中断被触发过)    
 __softirq_pending 共32bit,即每个bit对应软中断的一个向量,实际使用了6个bit    
第n个bit置1,即softirq_vec[n]有软中断发生。
- typedef struct {
- unsigned int __softirq_pending; /* set_bit is used on this */
- unsigned int __last_jiffy_stamp;
- } ____cacheline_aligned irq_cpustat_t;
- extern irq_cpustat_t irq_stat[]; /* defined in asm/hardirq.h */
- #define __IRQ_STAT(cpu, member) (irq_stat[cpu].member)
- #define local_softirq_pending() \
- __IRQ_STAT(smp_processor_id(), __softirq_pending)
- #define set_softirq_pending(x) (local_softirq_pending() = (x))
- #define or_softirq_pending(x) (local_softirq_pending() |= (x))
- #define __raise_softirq_irqoff(nr) do { or_softirq_pending(1UL << (nr)); } while (0)
常用的软中断触发函数
- void raise_softirq_irqoff(int nr){ //nr 即软中断向量编号
- __raise_softirq_irqoff(nr);
- }
但这只是“触发”软中断,软中断并不会立即被处理   
三、软中断处理    
函数_ _do_softirq是一次性按照向量表从高到低循环处理所有软中断(潜台词,软中断不可嵌套)    
_ _do_softirq()的调用时机:    
1. irq_exit() 硬件中断处理完,返回时调用
- do_IRQ() -->irq_exit()
- -->local_softirq_pending()
- -->_ _do_softirq()
2. ksoftirqd() 用于辅助处理软中断的内核线程,每一个CPU上都运行着一个ksoftirqd。
- start_kernel() --> kernel_init() -->do_pre_smp_initcalls()
- -->spawn_ksoftirqd() -->cpu_callback()
- -->kthread_create(ksoftirqd, hcpu, "ksoftirqd/%d", hotcpu)
- -->ksoftirqd()
- -->local_softirq_pending()
- -->_ _do_softirq()
3. local_bh_enable()时,发现有待处理的软中断且当时没处在软硬中断上下文中
- local_bh_enable()
- -->local_softirq_pending()
- -->_ _do_softirq()
处理过程代码详解
- asmlinkage void __do_softirq(void)
- {
- struct softirq_action *h;
- __u32 pending;
- int max_restart = MAX_SOFTIRQ_RESTART;
- int cpu;
- pending = local_softirq_pending();
- account_system_vtime(current);
- /*软中断处理中,禁止软中断再次进入,软中断处理是不可重入的*/
- __local_bh_disable((unsigned long)__builtin_return_address(0));
- trace_softirq_enter();
- cpu = smp_processor_id();
- restart:
- /* Reset the pending bitmask before enabling irqs
- 下面首先清除pending,以便系统可以激活其它软件中断,
- 然后使能外部中断
- 系统在下面的处理中,将使能外部中断以提高系统的响应,
- 注意,必须先清除pending,再使能外部中断,否则死锁*/
- set_softirq_pending(0);
- local_irq_enable();
- h = softirq_vec;
- /*下面按照从高到低调用open_softirq注册的句柄*/
- do {
- if (pending & 1) {
- h->action(h); //关键的一句,tasklet、内核timer、网络中断都是在这里搞的
- rcu_bh_qsctr_inc(cpu);
- }
- h++;
- pending >>= 1;
- } while (pending);
- local_irq_disable();
- pending = local_softirq_pending();
- if (pending && --max_restart)
- goto restart;
- if (pending)
- wakeup_softirqd();
- trace_softirq_exit();
- account_system_vtime(current);
- _local_bh_enable();
- }
Linux 软中断的更多相关文章
- linux软中断与硬中断实现原理概述
		linux软中断与硬中断实现原理概述. 1.软中断通过open_softirq注册一个软中断处理函数,即在软中断向量表softirq_vec数组中添加新的软中断处理action函数. 2.调用rais ... 
- 怎么理解Linux软中断?
		1.什么是中断 中断是系统用来响应硬件设备请求的一种机制,它会打断进程的正常调度和执行,然后调用内核中的中断处理程序来响应设备的请求. 2.为什么要有中断呢? "举个生活中的例子" ... 
- Linux软中断、tasklet和工作队列
		Linux内核中的软中断.tasklet和工作队列详解 引言 软中断.tasklet和工作队列并不是Linux内核中一直存在的机制,而是由更早版本的内核中的“下半部”(bottom half)演变而来 ... 
- linux 软中断过高性能优化案例
		案例如下: 发现cpu0上的软中断高达50% 
- Linux碎碎念
		在学习Linux过程中,有许多有用的小技巧.如果放在纸质的笔记本上,平时查阅会相当不方便.现在以一种“碎碎念”的方式,汇集整理在此,目前还不是很多,但随着学习.工作的深入,后续会陆陆续续添加更多的小技 ... 
- Intel 80x86 Linux Kernel Interrupt(中断)、Interrupt Priority、Interrupt nesting、Prohibit Things Whthin CPU In The Interrupt Off State
		目录 . 引言 . Linux 中断的概念 . 中断处理流程 . Linux 中断相关的源代码分析 . Linux 硬件中断 . Linux 软中断 . 中断优先级 . CPU在关中断状态下编程要注意 ... 
- 嵌入式系统Linux内核开发工程师必须掌握的三十道题(转)
		嵌入式系统Linux内核开发工程师必须掌握的三十道题 如果你能正确回答以下问题并理解相关知识点原理,那么你就可以算得上是基本合格的Linux内核开发工程师,试试看! 1) Linux中主要有哪几种内核 ... 
- Linux中断管理
		CPU和外设之间的交互,或CPU通过轮询机制查询,或外设通过中断机制主动上报. 对大部分外设中断比轮询效率高,但比如网卡驱动采取轮询比中断效率高. 这里重点关注ARM+Linux组合下中断管理,从底层 ... 
- Linux 驱动开发
		linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ... 
随机推荐
- BIOS  深入学习 转
			http://blog.csdn.net/lightseed/article/category/547391 
- pg 资料大全1
			https://github.com/ty4z2008/Qix/blob/master/pg.md?from=timeline&isappinstalled=0 PostgreSQL(数据库) ... 
- 特殊IP地址
			主机ID全为0:不分配给任何主机,仅用于表示某个网络的网络地址. 主机ID全为1:不分配给任何主机,仅用做广播地址. IP地址的32位全为1:即255.255.255.255,为有限广播地址(http ... 
- Common Linux log files name and usage--reference
			reference:http://www.coolcoder.in/2013/12/common-linux-log-files-name-and-usage.html if you spend lo ... 
- MySql 5.7安装配置
			最新的MySql免安装版配置: 1,解压缩: 2,添加系统环境变量,PATH=.......;C:\Program Files\MySQL\MySQL Server 5.7\bin 3,修改MySql ... 
- 搭建本地Git服务器6步走
			1. 在服务器上安装git和ssh 2. 在服务器上新建一个用户,比如就叫git sudo adduser git 3. 在服务器上新建一个目录来放置git仓库 mkdir gitrepo git i ... 
- java 解析XML文档
			Java 解析XML文档 一.解析XML文档方式: 1.DOM方式:将整个XML文档读取到内存中,按照XML文件的树状结构图进行解析. 2.SAX方式:基于事件的解析,只需要加载XML中的部分数据,优 ... 
- Oracle基础—表分区
			一:表分区的应用场景 用于管理包含大量数据的表. 二:表分区的优点 1.提高数据的可以性 2.减少管理负担 3.改善语句的性能 三:分区的方式:(区间分区.散列分区.列表分区.组合分区) 1.区间分区 ... 
- Ubuntu下安装QT
			环境 Ubuntu 9.10 qt4.7.3 gcc 4.4 Ubuntu中缺少 make 首先安装 sudo apt-get install make 如果不知道缺少啥,就按下面的装 1.sudo ... 
- FreeBSD 安裝 Tomcat JAVA JDK1.6 筆記
			首先是安裝軟體 cd /usr/ports/java/jdk16/ make 在這一步,需要你手動到sun.com上下載幾個安裝包,按提示下載好後加入到 /usr/ports/distfiles/,再 ... 
