


Three ways to interrupt the lower half of Linux drivers
What is the lower half of the interrupt
When an interrupt is generated, the interrupt handler will be entered.
But the interrupt handler must be fast, asynchronous, and simple to respond quickly to the hardware and complete those time-critical operations.
Therefore, for other tasks that have relatively loose time requirements, they should be postponed until the interrupt is activated before running .
In this way, the entire interrupt processing process is divided into two parts:
The first part is the interrupt handler ( The upper half), the kernel completes the immediate response to hardware interrupts through its asynchronous execution. The other part of the interrupt processing flow, bottom half(bottom half)
Next The half task is mainly to perform interrupt-related work, which is not completed by the interrupt service routine itself.

Lower half It is not necessary to specify an exact time, just postpone these tasks a little longer so that they can be done when the system is not too busyAnd After the interruption and recovery, can be executed.
The main difference between the upper and lower halves:
The upper half refers to Interrupt handler, The lower half refers to some tasks that can be postponed although they are related to interrupts.
The upper half of the interrupt cannot be interrupted by the same type of interrupt, while the lower half can still be interrupted by interrupts.
#Usually the lower half will run as soon as the interrupt handler returns. The first half is simple and fast, disable some or all interruptions during execution.
The second half will be executed later, and all interrupts can be responded to during execution.
Linux
, there are three main ways to implement the lower half of the interrupt:
Soft interrupt tasklet ##Work queue
softirq
softirq is a softirq, the code is located in
kernel/softirq.cIn the file;
softirq_action structure:

softirq.c A softirq vector array
softirq_vec is defined:
static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp; enum { HI_SOFTIRQ=0, /*用于高优先级的tasklet*/ TIMER_SOFTIRQ, /*用于定时器的下半部*/ NET_TX_SOFTIRQ, /*用于网络层发包*/ NET_RX_SOFTIRQ, /*用于网络层收报*/ BLOCK_SOFTIRQ, BLOCK_IOPOLL_SOFTIRQ, TASKLET_SOFTIRQ, /*用于低优先级的tasklet*/ SCHED_SOFTIRQ, HRTIMER_SOFTIRQ, RCU_SOFTIRQ, /* Preferable RCU should always be the last softirq */ NR_SOFTIRQS };
NR_SOFTIRQS, which is an enumeration constant.
include/linux/interrupt.h .
Several key points in using soft interrupts:
一个软中断不会抢占另外一个软中断。 惟一可以抢占软中断的是中断处理程序。 其他的软中断可以在其他处理器上同时执行。
相关接口
注册软中断
void open_softirq(int nr, void (*action)(struct softirq_action *))
即注册对应类型的处理函数到全局数组softirq_vec
中。
触发软中断
void raise_softirq(unsigned int nr)
实际上即以软中断类型nr
作为偏移量会置位irq_stat[cpu_id]
的成员变量__softirq_pending
.
__softirq_pending
字段中的每一个bit
,对应着某一个软中断,某个bit
被置位,说明有相应的软中断等待处理。
这也是同一类型软中断可以在多个cpu
上并行运行的根本原因。
软中断实例
以一个按键驱动的中断处理为例,将按键驱动的中断处理分成上下两部分:
The upper part: Read the key value and trigger the soft interrupt The lower part: Wake up Process

Soft interrupt registration, in the driver's entry function, register the soft interrupt:

Added enumeration constants:

As you can see, using soft interrupts requires modifying the kernel, and adding an enumeration is a bit cumbersome.
Therefore, we usually do not recommend increasing the number of soft interrupts without authorization. If new soft interrupts are needed, try to implement them as soft interrupt-based tasklets
form.
tasklet
##tasklet is the use of soft interruptA kind of lower half mechanism implemented.
Is it better to usesoft interrupt or tasklet?
softirq or tasklet is actually very simple:
通常你应该用 tasklet
。就像我们在前面看到的,软中断资源有限,也麻烦,而且软中断的使用者屈指可数。它只在那些执行频率很高和连续性要求很高的情况下才需要。而tasklet却有更广泛的用途。大多数情况下用 tasklet
效果都不错,而且它们还非常容易使用。因为tasklet是通过软中断实现的,所以它们本身也是软中断。
tasklet使用
tasklet
的使用步骤如下:
1、编写tasklet
处理函数(下半部)
void my_tasklet_fun (unsigned long data)
2、声明tasklet
//静态 DECLARE_TASKLET(my_tasklet,my_tasklet_fun,data); //动态 Struct tasklet_struct xxx; tasklet_init(&xxx,tasklet_handler,dev)
3、调度 tasklet
tasklet_schedule(&my_tasklet);
登记my_tasklet
, 然后允许系统在合适的时间调度它。
tasklet实例
以按键中断驱动为例:

First use DECLARE_TASKLET
to statically declare a tasklet
, specify its lower half function as btn_tasklet_func
, and interrupt the service After the function (upper half) obtains the key value, it calls tasklet_schedule
scheduling.
work queue
work queue
is work queue, It is also a type that interrupts the lower half.
Work queue
Defer the second half of the work to a kernel thread for execution - work
always runs in the process context .
Two important points:
Use work queues if the deferred work requires sleep
. Otherwise, usesoftirq
ortasklets
.##Work queues is suitable for allocating a large amount of memory and obtaining a semaphore. , or perform blocked
I/O.

work queue is similar to tasklet:

The above is the detailed content of Three ways to interrupt the lower half of Linux drivers. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.
