Interrupt and interrupt processing for Linux kernel (ii)

Source: Internet
Author: User

Last said that the implementation of the Linux kernel interrupt will be broken into two parts to deal with, on the previous part, this time to talk about the next part of the design ideas

    • The realization mechanism of the lower half part
      • Soft interrupt
      • Tasklet: is implemented by soft interrupts, but differs from soft interrupts
      • Work queue

Talk about a few of the above implementation mechanism before the old method, the current version of the kernel, although it is no longer edible, but the idea continues to use

The earliest Linux only provided the "bottom half" mechanism to implement the lower half, called BH, to achieve simple rudeness, set a global variable (32-bit integer), a 32-node list of the queue, which is set to 1 to prove which bottom half can be executed.

    • Soft interrupt

First, the soft interrupt implementation of the lower half of the mechanism, in order to make this mechanism, we must first explain the soft interrupt implementation, soft interrupt is actually compiled during the static allocation, KERNEL/SOFTIRQ.C defined an array containing 32 structures static struct Softirq_action Softirq_vec[nr_softirqs], and there is a corresponding 32-bit integer u32 pending, which is used to denote the state of each soft interrupt (not too little, generally not so much at all, Generally 9 10 is enough, why so little? Rarely useful soft interrupts are processed in the lower half, where the tasklet can never be used with soft interrupts)

The soft interrupt is registered in the 32-length struct array, and the soft interrupt must be marked with a soft interrupt, which is called triggering a soft interrupt, and usually the interrupt handler (the upper part) marks its soft interrupt before returning . So you don't have to worry about it and then execute that soft interrupt at the right moment.

The right moment: 1. When returning from a hardware interrupt code, 2. In the KSOFTIRQD kernel thread; 3. In the code that shows the check and execution of the soft interrupt to be processed;

No matter which moment, the soft interrupt will eventually be executed, call DO_SOFTIRQ () the function will iterate (loop check each bit of pending, so the loop can only execute up to 32 times)

    • Tasklet

Because Takslet is implemented using soft interrupts, tasklet itself is a soft interrupt, we are through the tasklet to achieve the second half of the mechanism, so in the processing mode and soft interrupt is very similar, tasklet by the tasklet structure, Each structure represents a single tasklet, which is defined as follows

1 structtasklet_struct2 {3Stauct tasklet_struct *next;//the next node in the list4UnsignedLongState//the state of Tasklet5atomic_t count;//Reference Counter6      void(*func) (unsignedLong);//tasklet processing function7UnsignedLongData//parameters to the Tasklet processing function8}

There are only three kinds of tasklet state: 0,tasklet_state_sched,tasklet_state_run, can only be in these three kinds of values, 0 means nothing to wait for scheduling, SCHED said has been dispatched, Run indicates that the Tasklet is running.

The tasklet structure that has been dispatched is stored in two types of single-processor data structures, namely Tasklet_vec (normal priority Tasklet) and Tasklet_hi_vec (high-priority tasklet), with almost no difference, but with a different priority. The steps for scheduling are as follows

The steps to run are as follows:

In fact, Tasklet gives the feeling is a soft interrupt package simple interface just.

Each processor has a set of core threads that assist with soft interrupts (including Tasklet, of course), and when to perform these soft interrupts is also described in the soft interrupt section, but there is a problem with soft interrupts that continue to be soft interrupts if the soft interrupt continues to adjust. This is not very good when the processor load is very serious, will lead to user space process hunger, there is also a scheme, that is not immediately handle the soft interrupt, but wait for some time, but when the processor is more busy when it is not very good to do so, because it can be executed immediately you let the processor idle. As an improvement, when a large number of soft interrupts occur, the kernel wakes up a set of kernel threads to handle these loads , and the key is that the priority of those threads with soft interrupts is set to the lowest priority (the Nice value is max.) This will be when the processor is busy, these soft interrupts will not compete with the user space process processor resources, and will eventually be executed, the processor is idle time can also be directly run.

    • Work queue

The work queue is another relatively new form of pushing the work back, and unlike the two previous treatments, it will give the job to a kernel thread to execute, which means! Taste! Means is handled by the process context! You can sleep!! (interrupts are not allowed to sleep) so it's easy to make a choice between the two methods.

Each processor has a corresponding worker thread

Data structure representing the work

These work structures are linked to the list, and when all the work on the list is done, the thread will hibernate

The implementation is also very simple,

    1. The thread first sets itself to hibernate (just set, does not immediately go to hibernation) and joins itself in the waiting queue
    2. If the work list is empty, use the schedule () dispatch function to go to sleep state
    3. If there are objects in the list, the thread will not sleep, change its state to task_running, and then come out from the waiting queue.
    4. If the list is not empty, perform the work that the lower part of the back is supposed to do (the loop is always looking for ...). )

A structure diagram.

    • Selection of the lower half mechanism

These three kinds of looks are good, then how should choose?

If you have a high demand for sharing, although it is troublesome, but still use soft interrupt it, because can be various operations (although the protection of these very troublesome)

If you do not have the same high requirements for sharing, it is recommended to use Tasklet because two tasklet of the same type cannot be parallel at the same time

If you want to solve the problem in the lower part of the process context, use the work queue, of course, if you want to sleep, you have no choice.

* All the ending *

Interrupt and interrupt processing for Linux kernel (ii)

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.