Linux Kernel implements interrupt and interrupt processing (2). Linux Kernel

Source: Internet
Author: User

Linux Kernel implements interrupt and interrupt processing (2). Linux Kernel

The first part is the step-by-step call !! : Http://www.cnblogs.com/lenomirei/p/5562086.html

The last time I said that the Linux kernel interrupt will divide the interruptTwo partsAfter processing, I talked about the previous part. This section describes the design ideas.

  • Implementation Mechanism in the lower half
    • Soft Interrupt
    • Tasklet: implemented through soft interrupt, but different from Soft Interrupt
    • Work queue

Before talking about the above implementation mechanisms, let's talk about an old method. Although the kernel of the current version is no longer used, the idea is still in use.

The earliest Linux system only provided the "bottom half" mechanism to implement the lower half, which is called BH. The implementation is simple and crude, and a global variable (32-bit integer) is set ), it indicates a chain list queue with 32 nodes. Which one is set to 1 to prove which bottom half can be executed.

 

 

  • Soft Interrupt

The first is to implement the lower half mechanism of Soft Interrupt. to implement this mechanism, you must first describe the implementation method of Soft Interrupt. The Soft Interrupt is actually statically allocated during compilation, kernel/softirq. c defines an array containing 32 struct.Static struct softirq_action softirq_vec [NR_SOFTIRQS]And there is a corresponding 32-bit integerU32 pendingIt is used to indicate the status of each Soft Interrupt (not to be too small, usually not so much at all, generally 9 to 10 is enough, why so little? It is rarely useful for Soft Interrupt Processing in the lower half. It will never be used for tasklet)

Put the Soft Interrupt into the 32-length struct array just now to complete the Soft Interrupt registration. To execute the Soft Interrupt, you must mark the registered Soft Interrupt first. This process is called triggering Soft Interrupt. Generally,The interrupt handler (that is, the upper part) will mark its soft interrupt before returning it.So you don't have to worry about it. The Soft Interrupt will be executed at the right time.

Suitable Time: 1. return from a hardware interrupt code; 2. In the ksoftirqd kernel thread; 3. In the code that displays the Soft Interrupt for checking and executing the code to be processed;

No matter the time above, the Soft Interrupt will eventually be executed. Calling do_softirq () This function will traverse cyclically (cyclically check every bit of pending, so the loop can only be executed for up to 32 times)

  • Tasklet

Because takslet is implemented using soft interrupt, tasklet itself is a soft interrupt. We use tasklet to implement the mechanism of the lower half, so the processing method is very similar to that of Soft Interrupt, tasklet is represented by the tasklet struct. Each struct represents a tasklet separately. Its definition is as follows:

 

1 struct tasklet_struct2 {3 stauct tasklet_struct * next; // The next node 4 unsigned long state in the linked list; // tasklet status 5 atomic_t count; // Reference Counter 6 void (* func) (unsigned long); // tasklet handler function 7 unsigned long data; // parameter 8 for tasklet handler function };

 

Among them, there are only three tasklet states: 0, TASKLET_STATE_SCHED, and TASKLET_STATE_RUN. They can only be set between the three states. 0 indicates that there is no waiting for scheduling, and SCHED indicates that the task has been scheduled, RUN indicates that the tasklet is running.

The scheduled tasklet struct is stored in the two single-processor data structures, which are tasklet_vec (Common-priority tasklet) and tasklet_hi_vec (high-priority tasklet). There is almost no difference, the scheduling steps are as follows:

The procedure is as follows:

 

 

In fact, tasklet is a simple interface for Soft Interrupt encapsulation ..

Each processor has a set of kernel threads that assist in handling soft interruptions (of course, tasklet). When will these soft interruptions be executed? The above section also describes soft interruptions, But there is such a problem, that is, if the Soft Interrupt is resumed, the Soft Interrupt will be continuously executed .. In this way, when the processor load is very serious, it will lead to user space process hunger. Another solution is not to immediately handle soft interruptions, but to wait for a while, however, this is obviously not good when the processor is relatively idle, because you can execute it immediately, but let the processor idle. As an improvement,When a large number of soft interruptions occur, the kernel will wake up a group of kernel threads to handle these loads.The key is,The priority of these threads with soft interruptions will be set to the lowest priority (nice value is 19)In this way, when the processor is busy, these soft interruptions will not compete with the user space process for processor resources, and will eventually be executed. When the processor is idle, it can be run directly.

  • Work queue

The work queue is another new form of pushing back the work. Unlike the previous two processing methods, it will give the work to a kernel thread for execution! Taste! Coming! It is handled by process context! You can just sleep !! (Interruptions do not allow sleep) so you can easily choose between the two methods.

Each processor has a corresponding worker thread.

 

1 struct workqueue_struct2 {3      struct cpu_work_queue_struct cpu_wq[NR_CPUS];4      struct list_head list;5      const char *name;6      int sinqlethread;7      int freezeable;8      int rt;9 };

 

 

1 struct detail {3 spinlck_t lock; // lock this structure 4 struct list_head worklist; // worklist 5 worker more_work; 6 struct work_struct * current_struct; 7 struct workqueue_struct * wq; // associated working queue structure 8 task_t * thread; // associated thread 9 };

 

Indicates the working Data Structure

 

1 struct work_struct2 {3      atomic_long_t data;4      struct list_head entry;5      work_func_t func;6 };

 

 

The structures of these jobs are linked to the linked list. When all the work on the linked list is completed, the thread will sleep.

The implementation method is also very simple,

Structure chart

 

  • Selection of lower half Mechanism

All three of them look good, So how should we choose?

If you have high requirements on sharing, although it is troublesome, you can still use Soft Interrupt, because you can perform various operations (although it is very troublesome to ensure this)

If you do not have such high requirements on sharing, we recommend that you use tasklet, because the two types of tasklet of the same type cannot be parallel at the same time.

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

* End of the entire drama *

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.