Linux Interrupt Soft Interrupt

Source: Internet
Author: User

In the analysis of interrupts of the Linux kernel, the soft interrupt should first be clarified such a derivation relationship:

IRQ ==> SOFTIRQ ==> tasklet ==> bottom half ==> task Queue------------------------|==> Timer

Interruption is the original motive force. CTSS relies on clock interrupts to periodically reschedule a program that can run. The peripheral notifies the CPU to handle the related tasks by interrupting. An interrupt handler is a special, stand-alone, operational entity in the kernel. This entity, in a way, is similar to a process or thread.

Since interrupts need to be processed quickly, a soft interrupt softirq is derived to deal with what is not done with interrupts. For example, in the driver of the network card, in the interrupt environment, just put the package in a queue, and then by the soft interrupt to pass the package to the process, or transfer packets.

Linux SOFTIRQ is non-reentrant, so on a single CPU system, only one soft interrupt can be run at a time. On a multi-CPU system, you can have multiple SOFTIRQ running at the same time. SOFTIRQ can handle more tasks, such as protocol stacks, file systems, etc., can be put into a soft interrupt inside processing.

Tasklet is an extension on the SOFTIRQ. It is a piece of code that can be executed, however, a tasklet can only be executed on one CPU at a time (one tasklet can have only one active entity, and one SOFTIRQ may have multiple active entities, but Softirq's active entities are non-reentrant).

Bottom half is a soft interrupt mechanism used by Linux 2.2.x systems, and it is no longer necessary because of the poor extensibility on SMP. BH is also an executable code fragment, but the global can only have one BH in operation, more than one BH is linear execution, so on the SMP system, waste is more serious.

The task queue is an extension on the BH mechanism. is also an executable code fragment. The aim is to break the number limit of BH (only 32). But its implementation is also linear, so on SMP, there is no advantage.

The timer is also an extension on BH. Each timer is an executable fragment, but it is more flexible, so if you have a high-priority task, consider using a timer.

When writing device drivers, the tasklet mechanism is a relatively common mechanism that is typically used to reduce the time to interrupt processing and to convert tasks that should have been done in an interrupt service program into a soft interrupt completion.

In order to minimize interrupt loss due to excessive processing time, sometimes we need to put some tasks that are not very urgent in the interrupt processing to be executed later, and let the interrupt handler return as soon as possible. In older versions of Linux, interrupt processing is typically divided into top half handler, bottom half handler. Using the top half handler to handle the tasks that interrupts must handle, and bottom half handler processing is not an urgent task.

However, after linux2.6 Linux has taken another mechanism, is soft interrupt to replace the bottom half handler processing. And the tasklet mechanism is to use soft interrupt to complete the processing of driving bottom half. Soft interrupts in Linux2.6 are usually fixed in several ways: HI_SOFTIRQ (high-priority tasklet, a special Tasklet), TIMER_SOFTIRQ (Timers), NET_TX_SOFTIRQ (Network port forwarding), Net_rx_ SOFTIRQ (Network port reception), BLOCK_SOFTIRQ (block device), TASKLET_SOFTIRQ (General Tasklet). Of course, you can also add their own soft interrupts by directly modifying the kernel, but generally this is unreasonable, the priority of soft interrupts is relatively high, if not in the kernel processing frequent tasks are not recommended to use. It is usually enough to drive the user to use Tasklet.

The relationship between soft interrupts and Tasklet is as follows:

As you can see, KSOFTIRQD is a kernel thread running in the background, it will cycle through the list of soft interrupt vectors, if the soft interrupt vector is found to be suspended (pend), the corresponding processing function is executed, for Tasklet, this handler function is Tasklet_action, This handler is hooked up when the soft interrupt is initialized at system startup.

The Tasklet_action function iterates through a global list of Tasklet_vec (this list has one for each CPU of the SMP system), and the elements in this list are tasklet_struct. This structure is as follows:

struct TASKLET_STRUCT

{

struct Tasklet_struct *next;

unsigned long state;

atomic_t count;

void (*func) (unsigned long);

unsigned long data;

};

Each structure is a function pointer that points to your own defined function. When we want to use Tasklet, we first define a TASKLET_STRUCT structure and initialize it to execute the function pointer, then attach it to the Task_vec linked list, and a soft interrupt can wait to be executed.

This is probably the case, and for Linux-powered authors there's really no need to care about that, the key is how we use the Tasklet mechanism.

The following interfaces are available in Linux:

Declare_tasklet (name,function,data): This interface Initializes a tasklet, where name is the name of the Tasklet and function is the functions that perform tasklet; data is unsigned lon The function parameter of type G.

static inline void Tasklet_schedule (struct tasklet_struct *t): This interface connects the defined Tasklet to the Tasklet_vec linked list of the CPU, specifically which CPU Taskle The T_vec list is determined based on which CPU the current thread is running on. This function not only hooks up the tasklet, but also a soft tasklet soft interrupt that suspends the tasklet corresponding interrupt vector (pend).

After two work is done, basically can, the tasklet mechanism is not complex, it is easy to make the program as soon as possible to interrupt the response to avoid loss of interruption.

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.