Analysis of Soft Interrupt mechanism in Linux

Source: Internet
Author: User

 

What is a soft interrupt mechanism?

The Soft Interrupt mechanism in Linux is used in the lower half of the system with the strictest time requirements and the most important interruptions. In the system design process, we all know that the interrupt context cannot handle too many things and needs to be returned quickly. Otherwise, it will easily lead to the loss of the interrupt event, which leads to a problem: who will complete the reason for the service interruption? In the frontend and backend programs, as there is only one interrupt context and one task context, the interrupt context trigger event sets the tag bit, and the task context cyclically scans the tag bit to execute the corresponding action, that is, after the interruption occurs, the task is completed by the task. However, the task context is scanned, and the real-time performance cannot be guaranteed. In Linux and Windows, this continuous loop task is the Soft Interrupt daemon described in this article. In Windows, the time-consuming interrupt transaction is called the Interrupt Delay processing. In Linux, it is called the lower half of the interrupt. Obviously, the upper half of the interrupt is used to process idle actions such as the clear interrupt, then, the lower half of the interrupt is triggered when the service program is exited to complete the specific functions.

In Linux, the lower half of the interrupt is implemented based on the Soft Interrupt mechanism. Therefore, the implementation of the lower half of the Soft Interrupt mechanism is very simple. Based on the above description, we should also be clear about why we should define a soft interrupt mechanism. One sentence is to deal with time-demanding tasks, and such a requirement occurs in the lower half of the interruptions, therefore, the implementation adopts the Soft Interrupt mechanism.

Implementation principle of Soft Interrupt Mechanism

Shows the implementation principle of the Soft Interrupt mechanism:

 

The core elements that constitute the Soft Interrupt mechanism include:

1. Soft Interrupt Status Register Soft Interrupt state (irq_stat)

2. softirq_vec)

3. Soft Interrupt daemon

The Soft Interrupt work project simulates the actual interrupt processing process. When a soft interrupt occurs, you must first set the corresponding interrupt mark bit to trigger the interrupt transaction, then wake up the daemon thread to check the interrupt Status Register. If a Soft Interrupt transaction is found through the query, the soft interrupt service program action () is called through the soft interrupt vector table (). This is the Soft Interrupt process. The only difference from the hardware interrupt is the ing process from the interrupt mark to the interrupt service program. After a hardware interruption occurs, the CPU needs to map the hardware interruption request to a specific service program through a scale. This process is automatically completed by the hardware, but the soft interruption is not, it requires a daemon thread to implement this process, which is the interrupt of software simulation, so it is called Soft Interrupt.

A Soft Interrupt won't be used to seize another Soft Interrupt. Only a hardware interrupt can be used to seize the Soft Interrupt. Therefore, the soft interrupt can ensure strict time requirements.

In Linux, you can register up to 32 soft interrupts. Currently, the system uses six soft interrupts: timer processing, SCSI processing, network sending and receiving processing, and tasklet mechanism, the tasklet mechanism is used to implement the lower half,

The core data structure of Soft Interrupt is the interrupt vector table, which is defined as follows:

Struct softirq_action

{

Void (* Action) (struct softirq_action *);/* Soft Interrupt Service Program */

Void * data;/* service program input parameter */

};

Daemon is the core of the Soft Interrupt mechanism, and its implementation process is relatively simple. It queries the Soft Interrupt state irq_stat to determine whether an event occurs. If so, it is mapped to the soft interrupt vector table, you can call the registered action function. From this analysis, we can see that the execution context of the Soft Interrupt service program is Soft Interrupt daemon. In Linux, the Soft Interrupt daemon thread function is do_softirq ().

Triggering a Soft Interrupt transaction is implemented through raise_softirq (). This function sets the Soft Interrupt status bit when the interrupt is closed, and determines whether the daemon is directly awakened if it is not in the interrupt context.

The list of commonly used soft interrupt functions is as follows:

1. open_softirq: register a soft interrupt and register the Soft Interrupt service program to the soft interrupt vector table. 2. raise_softirq: sets the Soft Interrupt state bitmap to trigger Soft Interrupt transactions.

Tasklet mechanism Implementation Analysis

Tasklet is a soft interrupt. Considering the priority, it occupies the 0 and 5 soft interruptions in the vector table respectively. Shows the implementation principle of the tasklet mechanism:

 

When a Soft Interrupt event of a tasklet occurs, execute the Soft Interrupt Service Program of tasklet-action. The service program scans the task list of A tasklet and executes the specific service programs in the task. Here is an example to illustrate:

After a user reads and writes a USB device, a hardware interruption occurs, and the hardware interruption service program constructs a tasklet_struct. In this structure, it specifies the method function to complete the interruption task (the lower half executes the function ), then, attach tasklet_struct to the tasklet_struct linked list of tasklet. This step can be done through the tasklet_schedule function. Finally, the hardware interrupt service program exits and the CPU starts to schedule the Soft Interrupt daemon. The Soft Interrupt daemon will find an event in tasklet. It will execute tasklet-action, and then tasklet-action will scan the tasklet_struct linked list, execute the lower half of the specific USB interrupt service program. This is the entire process of applying tasklet to complete the implementation of the lower half of the interrupt.

The tasklet implementation in Linux is relatively simple, and it encapsulates an important data structure tasklet_struct. The main functions of tasklet are listed as follows:

1. tasklet_init: initialize a tasklet_struct. Of course, you can use the static initialization method. The macro is declare_tasklet.

2. tasklet_schedule: Schedule A tasklet and add the input tasklet_struct to the tasklet linked list.

The Soft Interrupt mechanism in Linux simulates the hardware interrupt process, and its design idea can be fully applied in other embedded OS.

This article from the Linux community website (www.linuxidc.com) original link: http://www.linuxidc.com/Linux/2009-03/19041p2.htm

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.