Linux kernel--interrupt mechanism

Source: Internet
Author: User

Interrupt mechanismWhy do I need to interrupt?

If you let the kernel periodically poll the device for processing the device, it will do a lot of work, because the peripherals are generally slower than CPU , while CPU cannot wait for an external event. So it can be a clever way for the device to proactively notify the kernel when it needs the kernel, which is the interruption.

Interrupt handlers

The kernel executes a function when responding to a specific interrupt -- Interrupt handler. The difference between an interrupt handler and other kernel functions is that the interrupt handler is called by the kernel to respond to interrupts, and they run in a special context that we call the interrupt context.

interrupt handlers are normal. C code. What is special is that the interrupt handler runs in the context of the interrupt , and its behavior is subject to certain limitations :
1) cannot send or receive data to user space
2) cannot use functions that may cause blocking
3) cannot use functions that may cause scheduling

Registration and release interrupts

Device Driver Utilization Request_irq () registers an interrupt handler and activates the given medium break. a return of 0 indicates success , or an error code is returned.

when uninstalling a device driver, you need to unregister the appropriate interrupt handler and release the break, and you need to call free_irq-- If there is no interrupt handler on the given interrupt line, the handler for the response is unregistered and the disconnection is disabled.

Upper and lower half-part mechanism

We expect the interrupt handler to run fast and want to get it done with a lot of work, these two goals are mutually restrictive, how to solve -- The upper and lower half of the mechanism.

we cut the interrupt handling into two halves. The interrupt handler is the upper half-the acceptance is interrupted, and he starts execution immediately, but only for a strictly time-bound job. The work that can be allowed to be done later is postponed to the lower part, after which, at the right time, the lower half is executed by the terminal. The top half is simple and fast, and it is forbidden to interrupt some or all of the execution. The lower half is executed later, and all interrupts can be responded to during execution. This design can improve the system's responsiveness by keeping the system as short as possible when it is in an interrupted shielding state. the upper half has an interrupt handler mechanism, while the lower half implements a soft interrupt implementation,Tasklet Implementation and Work queue implementation.

We use a network card to explain these two halves. When the network card receives the packet, notifies the kernel, triggers the interrupt, the so-called upper part is, reads the packet to the memory in time, prevents because the delay causes the loss, this is very urgent work. After reading the memory, the processing of the data is no longer urgent, the kernel can go to run the program before the interruption, and the processing of the network packets to the next half of processing.

Upper and lower half division principle

1) If a task is sensitive to time, it is executed in the interrupt handler;

2) If a task is related to the hardware, it is executed in the interrupt handler;

3) If a task is guaranteed not to be interrupted by another interrupt, it is executed in the interrupt handler;

4) all other tasks, consider placing in the lower half of the execution.

Soft interrupt of realization mechanism in the second part

soft interrupts as a representation of the lower part of the mechanism, are along withSMP(Share Memoryprocessor) came into being, and it wasTaskletthe basis for implementation (Taskletin fact, only on the basis of a soft interrupt to add a certain mechanism). Soft interrupts are generally"deferred function"and sometimes includesTasklet(ask the reader to infer from context when it is encountered whether it containsTasklet). It occurs because the difference between the upper and lower half of the above is satisfied, so that the time-insensitive task is deferred, and the soft interrupt executes the remaining task that the interrupt handler leaves it to complete, and can be used in multipleCPUThe overall system efficiency can be higher with the parallel execution. Its features include:

a ) is not executed immediately after it has been generated, it must wait for the kernel to be dispatched. Soft interrupts cannot be interrupted by themselves and can only be interrupted by hardware interrupts (top half).

b ) can run concurrently on multiple CPU (even if the same type is available). So a soft interrupt must be designed as a reentrant function (allowing multiple CPUs to operate simultaneously), so a spin lock is required to protect its data structure.

the Tasklet of the realization mechanism of the lower half part

Tasklet is implemented by a soft interrupt, so it is also a soft interrupt itself.

soft interrupts are handled in the form of polling. If it happens to be the last interrupt, you must loop through all of the interrupt types before you can finally execute the corresponding handler function. Obviously the developers in order to ensure the efficiency of polling, so limit the number of interrupts is A.

in order to increase the number of interrupt processing, by improving processing efficiency, resulting in Tasklet mechanism.

Tasklet The use of a non-discriminatory queue mechanism, when there is an interruption to execute, eliminating the pain of a circular check table. as a new mechanism, Tasklet can obviously take on more advantages. Just at this time the SMP is getting more and more fire, so the SMP mechanism is added in tasklet to ensure that the same kind of interruption can only be executed on the CPU. In the era of soft interruptions, this is clearly not considered. Therefore the same soft interrupt can be executed simultaneously on two CPUs , which is likely to cause a conflict.

summarized under Tasklet The advantages of:

( 1 ) no type quantity limit;

( 2 ) High efficiency, no need to cycle check table;

( 3 ) Support SMP mechanism;

It has the following characteristics:

1 ) A specific type of Tasklet can only run in one CPU , cannot be parallel, can only be executed serially.

2 ) multiple different types of Tasklet can be parallel in multiple CPU on.

3 soft interrupts are statically allocated and cannot be changed after the kernel has been compiled. However , Tasklet is much more flexible and can be changed at runtime (e.g. when adding modules).

Work queue for the lower half of the implementation mechanism

the deferred function we described above runs in the interrupt context (a checkpoint for soft interrupts is DO_IRQ exit), resulting in a number of problems: soft interrupts can not sleep, can not block. Because the interrupt context is out of the kernel state, there is no process switch, so if the soft interrupt is asleep or blocked, it will not be able to exit this state, causing the kernel to complete zombie. However, a blocking function cannot be implemented in an interrupt context and must be run in a process context, such as a function that accesses disk data blocks. Therefore, a blocking function cannot be implemented with a soft interrupt. However, they tend to have delayed characteristics.

the deferred function we described above runs in the interrupt context, which leads to some problems, which means that they cannot be suspended, that is, the soft interrupt cannot sleep and cannot be blocked because the interrupt context is out of the kernel state and there is no process switch, so if the soft interrupt is asleep or blocked, it will not be able to exit this state , causing the kernel to be zombie-wide. Therefore, a blocking function cannot be implemented with a soft interrupt. However, they tend to have delayed characteristics. and because it is serially executed, it can cause delays in other interrupt responses as long as one has a longer processing time. In order to accomplish these impossible tasks, a work queue appears, which can switch between different processes to do different things.

If the deferred task requires sleep, select the work queue and if you do not need to sleep, select the soft interrupt or Tasklet . The work queue can run in the process context, and it trusts the work to a kernel thread. The work queue is plainly a set of kernel threads to use as an interrupt daemon. Multiple interrupts can be placed in one thread, or each interrupt can be assigned one thread. We use struct workqueue_struct to represent worker threads, and worker threads are implemented with kernel threads. And how the worker thread performs the deferred work - - There is such a list, which is composed of struct work_struct , and this work_struct The job is described, and once the work is done, the corresponding work_struct The object is removed from the list, and the worker thread continues to hibernate when there are no more objects on the list. because the work queue is a thread, we can use all the methods that can be used in threads.

What is the role of Linux soft interrupts and work queues

Linux the soft interrupts and work queues in the interrupt are the bottom-half implementation mechanism in the upper and lower mechanism.

1. Soft interrupt is generally called " delay function " , it can not sleep, can not block, it is in the context of interruption, can not switch to the city, soft interrupts can not be interrupted by themselves, Can only be interrupted by a hardware interrupt (top half) and run concurrently on multiple CPUs . So a soft interrupt must be designed as a reentrant function, so a spin lock is required to protect its data structure.

2. the function in the work queue is in the context of the process, it can sleep, can be blocked, can switch between different processes, to accomplish different tasks.

Both the deferred function and the work queue do not have access to the user's process space, and the deferred function cannot have any running processes at execution time, and the function of the Task force has kernel process execution, and he cannot access the user space address.



Reference

Linux kernel design and implementation

Http://www.cnblogs.com/li-hao/archive/2012/01/12/2321084.html

http://blog.csdn.net/bluecloudmatrix/article/details/29180095

http://blog.csdn.net/dlutbrucezhang/article/details/8699776

http://blog.csdn.net/chdhust/article/details/12227383


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.