Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

Soft interrupt analysis has been busy lately and there is no time to summarize some of the kernel-related things. Last updated blog to the Linux kernel interrupt subsystem. This time summarize the soft interrupt, which is softirq. Later, some tasklets and work queue mechanisms will be summarized.

1. Why do you want a soft break?

When writing a driver, after an interrupt is generated, the kernel may need to do a lot of work in the interrupt handler. But the processing of the interrupt handler is to turn off the interrupt. This means that in response to an interrupt, the system cannot respond to other external interrupts again. This consequence can result in the possibility of losing an external interrupt. Therefore, the Linux kernel has designed an architecture. The tasks that the interrupt function needs to process are divided into two parts, some of which are executed in the interrupt handler function. At this time, the system closes the interrupt. The other part is executed in the software interrupt. At this time, the interrupt is turned on and the system can respond to the external interrupt.

There are various books on the theory of software interruption, not to mention. To truly understand the role of software interruption, it must be analyzed from the perspective of the code. When we do our work, we are professional. When a person knows nothing in a certain field, we call him a white, even, non-Apple computer. Xiaobai’s mind is full of problems. Slowly, when these doubts are explained, Xiaobai is off white. At this point, we have an understanding of the basic framework of this field, but there is still a certain gap with professional. Once again, it will gradually reach the professional realm in this field.

2. When is the trigger processing software interrupt?

Having said so much nonsense, hurry up to the point. When you first know about soft interruptions, there are definitely a lot of questions in your mind. The first is where is the software interrupt triggered? The answer to this question is: after a hardware interrupt is processed. The following function pushes the interrupt handler after processing the hardware interrupt, which triggers the processing of the software interrupt in irq_exit.

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

It should be noted here that invoke_softirq must satisfy two conditions to be called, one is not in the hardware interrupt processing or in the software interrupt processing, the second is that the software interrupt must be in the pending state. The second one is well understood. If there is a software interrupt, it will be processed. If it is not, it will not be processed. The first one is not easy to understand.

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

In the process data structure of the Linux system, there is such a data structure.

#define preempt_count()(current_thread_info()->preempt_count),

Use preempt_count to indicate whether it is in interrupt processing or software interrupt processing.

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

The 8 to 23 bits of preempt_count record the count of interrupt processing and software interrupt processing. If there is a count, the system is in the process of hardware interrupt or software interrupt processing. The system is designed to avoid software interrupts being called in interrupt nesting and to achieve the inability of software interrupts to be reentered on a single CPU. For the ARM architecture CPU, there is no problem of invoking the software interrupt in the interrupt nesting, because the CPU of the ARM architecture closes the interrupt during the processing of the hardware interrupt. The hardware interrupt will be enabled only after entering the soft interrupt processing. If there is a hardware interrupt nesting during the software interrupt processing, the soft interrupt will not be called again. The cause hardware interrupt is re-entered during the software interrupt processing. At this point preempt_count has recorded a software interrupt! For other architecture CPUs, it is possible to start the hardware interrupt before the hardware interrupt is triggered, that is, when the hardware interrupt is still being processed. Interrupt nesting may occur, and software interrupts are not allowed in interrupt nesting. Processed. Why? My understanding is that when interrupt nesting occurs, it indicates that this time is when the system is suddenly busy. The first priority of the kernel is to quickly complete the processing of the interrupt and exit the interrupt nesting. Avoid multiple nesting, where there is time to handle software interrupts, so software interrupts are deferred until all interrupt processing is complete to trigger a software interrupt.

3. The process of software interruption

As I mentioned before, a big purpose of soft interrupts is to avoid interrupt processing, and to handle too many operations and lose interrupts. At the same time, interrupts need to take into account that one thing is that the interrupt processing process will affect the system response time. If an interrupt is processed for one second, you will definitely feel the stuck serial port. On the other hand, we must also consider the priority of the interrupt handling operation. After all, it is a hardware-triggered transaction, which is related to the efficiency of the network and block devices. The Linux kernel must consider balancing these three aspects in terms of outages. And the __do_softirq function I want to analyze below is just like playing Tai Chi between the three, and it's all right!

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

__do_softirq function processing software interrupt process as shown in the following figure

4. First call the local_softirq_pending function to get the current bits of software interrupts.

5. Call __local_bh_disable to turn off the soft interrupt. In fact, it is to set the processing software interrupt flag, which can not reenter the __do_softirq function on the same CPU.

6. Reset the soft interrupt flag to 0, and set_softirq_pending resets the soft interrupt flag to 0, so that the software interrupt bit can be set again in the hardware interrupt after the interrupt is re-enabled.

7. Turn on hardware interrupt

8. Then in a loop, traverse each bit of the pending flag, if this bit is set, the handler for the software interrupt will be called. In this process, the hardware interrupt is turned on and the software interrupt can be interrupted at any time. This ensures that hardware interrupts are not lost.

9. Then close the hardware interrupt to see if there is another software interrupt in the pending state. If it is, and there is no cumulative repeated access to the software interrupt processing more than 10 times during this call to the __do_softirq function, you can call the software interrupt processing again. . If it exceeds 10 times, call wakeup_softirqd(); wake up a process in the kernel to handle the software interrupt. The 10 times limit was also set to avoid affecting system response time.

4. Handling soft interrupt kernel threads

Earlier I said that I can't let the CPU handle interrupt transactions for a long time. This will affect the response time of the system and seriously affect the interactive experience between the user and the system. So in the previous __do_softirq will be executed up to 10 times in a loop, then when there are still 10 soft interrupts in the pending state, what should I do at this time? The system will wake up a kernel process that is processed by the software interrupt, and handle the software interrupt in the pending process in the kernel process. It should be noted here that the location of the triggering software interrupt we analyzed earlier is actually in the interrupt context, and in the kernel thread of the soft interrupt is actually the context of the process.

The soft interrupt context mentioned here refers to the ksoftirqd process that the system establishes for each CPU.

After reading this function, I have to admire the exquisite design of this function! And I have learned more from it to understand the truth of being a human being. That is to be a man who wants to be overbearing, too modest and too flattering, but can not be arrogant, the principle of the problem should be fair, when it is necessary to take into account the interests of others, the benefits can not be alone. This is the same as the ksoftirqd process below. It is forbidden to preempt at this time. Other processes do not want to schedule it. However, if you take up too long CPU time, you will also consciously ask whether you should release the CPU to other processes.

Below we will analyze how this process reflects the above statement? There are two main loops in the soft interrupt kernel process. The outer loop processing is handled by a software interrupt, and sleeps without a software interrupt. The inner loop processing software is interrupted, and each cycle is tested once to occupy the CPU for a long time, and it is necessary to schedule the release of the CPU to other processes. The specific operation is explained in the comments.

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

Based on Linux soft interrupt mechanism and analysis of tasklet and work queue mechanism

ZGAR Vape Pods 1.0

ZGAR Vape Pods 1.0


ZGAR electronic cigarette uses high-tech R&D, food grade disposable pods and high-quality raw material. A new design of gradient our disposable vape is impressive.We equip with breathing lights in the vape pen and pods.


Our team has very high requirements for product quality, taste allocation and packaging design. Designers only use Hong Kong designers, e-cigarette liquid only imports from the United States, materials are food grade, and assembly factory wants medical grade without ground workshop.


We offer best price, high quality Pod System Vape,Pods Systems Touch Screen,Empty Pod System, Pod Vape System,Disposable Pod device,Vape Pods to all over the world.



ZGAR Vape 1.0 Pods,ZGAR Vape Pods 1.0 ,Pod Systems,Atomizer, E-cigarette, Empty Pod Vape Manufacturer and Supplier in China

ZGAR INTERNATIONAL(HK)CO., LIMITED , https://www.oemvape-pen.com