Deep understanding of Linux kernel 4: Soft Interrupt/tasklet/work queue

Source: Internet
Author: User

Soft Interrupt, tasklet, and working queue are not a mechanism that has always existed in the Linux kernel, but evolved from the "bottom half" (bottom half) of earlier kernel versions. There are actually five mechanisms in the lower half, but in the kernel of version 2.6, the lower half and the functions of the task queue disappear, leaving only the first three. This article focuses on the relationship between the three. (Function details will not appear in this article. For more information, click here)

(1) differences between the upper half and the lower half
The upper half refers to the interrupt processing program, while the lower half refers to some tasks that can be postponed even if they are related to the interrupt. For example, in network transmission, the event that the network adapter receives data packets does not need to be processed immediately. Therefore, the lower half is suitable for implementation; however, when a user clicks a keyboard, such an event must be immediately responded to, and should be interrupted.
The main difference between the two is that the interrupt cannot be interrupted by the same type, but the lower half can still be interrupted. The interrupt is very time-sensitive, while the lower half is basically delayed. Due to the difference between the two, you can refer to the following four items for whether a job is executed in the upper half or lower half:
A) if a task is very time sensitive, place it in the interrupt handler for execution.
B) if a task is related to hardware, place it in the interrupt handler for execution.
C) if a task is to be interrupted by other interruptions (especially the same interruptions), place it in the Interrupt Processing Program for execution.
D) all other tasks should be executed in the lower half.

(2) Why should I use Soft Interrupt?
As a representative of the lower half mechanism, Soft Interrupt came into being with the emergence of SMP (share memory processor, it is also the basis of tasklet implementation (tasklet actually only adds a certain mechanism on the basis of Soft Interrupt ). Soft Interrupt is generally a general term for "deletable functions", and sometimes includes tasklet (Please infer whether tasklet is included according to the context when you encounter it ). It is because it needs to meet the difference between the upper half and the lower half, so that time-insensitive tasks can be postponed and executed concurrently on multiple CPUs, this improves the overall system efficiency. Its features include:
A) It cannot be executed immediately after it is generated. It must wait for Kernel scheduling to be executed. Soft Interrupt cannot be interrupted by yourself, but can only be interrupted by hardware (upper half ).
B) It can run concurrently on multiple CPUs (even for the same type ). Therefore, the Soft Interrupt must be designed as a reentrant function (allowing multiple CPUs to operate at the same time). Therefore, the spin lock must be used to protect its data structure.

(3) Why use tasklet? (Difference between tasklet and Soft Interrupt)
Since soft interruptions must use reentrant functions, the complexity of design increases, increasing the burden for device drivers. If an application does not need to be executed concurrently on multiple CPUs, there is no need for Soft Interrupt. Therefore, tasklet was born to make up for the above two requirements. It has the following features:
A) a specific type of tasklet can only run on one CPU. It cannot run in parallel and can only be executed in serial mode.
B) Multiple tasklets of different types can be concurrently deployed on multiple CPUs.
C) Soft Interrupt is statically allocated and cannot be changed after the kernel is compiled. But tasklet is much more flexible and can be changed at runtime (for example, when adding modules ).
Tasklet is implemented based on two Soft Interrupt types. However, due to its special implementation mechanism (which will be detailed in section 4.3), tasklet is different from Soft Interrupt. This feature reduces the burden on Device Driver developers. tasklet is the best option if you do not need parallel features of soft interruptions.

(4) use of delayed functions (Soft Interrupt and tasklet)
Generally, four operations can be performed on the delayable function: initialization, activation, execution, and blocking. Blocking is not described here. The first three are important. Next we will compare the three steps of Soft Interrupt and tasklet.

(4.1) initialization
Initialization refers to all the work done before the deletable function is ready. Generally, there are two major steps: first, declare the delayable function to the kernel for the kernel to call it as needed, and then call the corresponding initialization function, initialize the corresponding descriptor using function pointers.
If it is a soft interrupt, It is performed during kernel initialization. Its descriptor is defined as follows:

Struct softirq_action
{
Void (* Action) (struct softirq_action
*);
Void
* Data;
};

In \ kernel \ softirq. file C contains an array of 32 descriptors static struct softirq_action softirq_vec [32]; but in fact, only the first six have been registered and used by the kernel (including the hi_softirq/tasklet_softirq used by tasklet and the net_tx_softirq/net_rx_softirq used by the network protocol stack, and the SCSI Storage and system timers ), the rest can be used by kernel developers. Functions are required:
Void open_softirq (int nr, void (* Action) (struct softirq_action *), void * Data)
Initialize the element whose index is Nr in the array. The required parameters are of course the action function pointer and data. For example, the network subsystem initializes the Soft Interrupt through the following two functions (net_tx_action/net_rx_action is two functions ):

Open_softirq (net_tx_softirq, net_tx_action );
Open_softirq (net_rx_softirq, net_rx_action );

After Initialization is complete, a one-to-one correspondence is actually completed: When net_tx_softirq Soft Interrupt is generated in the kernel, net_tx_action is called.
Tasklet can be defined at runtime, for example, when a module is loaded. There are two defining methods:
Static Declaration

Declare_tasket (name, func, data)
Declare_tasklet_disabled (name, func, data)

Dynamic statement

Void tasklet_init (struct tasklet_struct
* T,
Void (* func) (unsigned
Long), unsigned
Long data)

The parameters are descriptors respectively. The parameters of the function to be called and the function must be unsigned long. You also need to write a function pointer func similar to net_tx_action. The final initialization result is an actual descriptor, which is assumed to be my_tasklet (which will be used below ).

(4.2) Activation
Activation marks a delayable function as pending, indicating that the kernel can call this delayable function (even when it is interrupted, but the function will not be executed immediately). In this case, it can be analogous to a process in the task_running state. A process in this state is only prepared to be scheduled by the CPU, but may not be scheduled immediately.
The Soft Interrupt is activated using the raise_softirq () function. The received parameter is the array index Nr used during initialization.
Tasklet is activated using tasklet_schedule (). This function accepts the tasklet descriptor as a parameter, for example, the my_tasklet generated above:

Tasklet_schedule (& my_tasklet)

(4.3) Execution
Execution is the process in which the kernel runs the deletable function, but the execution only occurs at certain time points (called checkpoints). What are the specific checkpoints? For details, see in-depth p.177 ).
Each CPU has a 32-bit netmask _ softirq_pending, indicating the pending (activated) soft interruptions on the CPU. This mask can be obtained using the local_softirq_pending () Macro. All pending soft interruptions need to be processed using a loop of the do_softirq () function.
For tasklet, the following statements have been used to initialize the functions to be executed when the two soft interrupts tasklet_softirq/hi_softirq are encountered:

Open_softirq (tasklet_softirq, tasklet_action, null );
Open_softirq (hi_softirq, tasklet_hi_action, null );

Therefore, these two soft interruptions need to be treated differently. The internal implementation of tasklet_action and tasklet_hi_action is the reason why the Soft Interrupt and tasklet have different features (of course, because of their different descriptors, The tasklet descriptor is more complex than the Soft Interrupt, that is to say, the kernel designer has done a part of the restricted work to reduce the work of driver developers ).

(5) Why use the work queue? (Difference between work queue and Soft Interrupt)
The latency function we introduced above runs in the interrupt context (a checkpoint of Soft Interrupt is when do_irq exits), which causes some problems: Soft Interrupt cannot be sleep or congested. Because the interrupt context is out of the kernel state and there is no process switching, if the Soft Interrupt is sleep or congested, it will not be able to exit this State, resulting in the entire kernel freezing. However, blocking functions cannot be implemented in the interrupt context. They must run in the process context, for example, the function used to access disk data blocks. Therefore, blocking functions cannot be implemented with soft interruptions. However, they often have latency characteristics.
Therefore, the 2.6 kernel has a working queue running in the kernel state (replacing the task queue in the 2.4 kernel ). It also has some delayable functions (requires activation and delayed execution), but can switch between different processes to complete different tasks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.