Each processor has a set of kernel threads that enable the processor Soft Interrupt (and tasklet. When there are a lot of soft interruptions in the kernel, these kernel processes will assist in processing them.
Reasons for introducing ksoftirq kernel threads:
For Soft Interrupt, the kernel will choose to handle it at several special times. However, it is the most common process for the interrupt handler to return. Soft interruptions may be triggered at a high frequency. What's more, processing functions may be repeatedly triggered in a font shape, which will lead to insufficient processing time for the user space process, therefore, it is in hunger. It is unacceptable to simply adopt policies that do not immediately handle soft interruptions that are re-triggered.
Initial solution:
1) As long as there are soft interruptions that are triggered and awaiting processing, this execution will be responsible for handling them. The soft interruptions that are re-triggered will also be processed before the return of this execution. This ensures that the Soft Interrupt of the kernel is processed in real time. The key is that the soft interrupt that is re-triggered will be processed immediately. When the load is high, if a large number of soft interruptions are triggered, they will be repeatedly triggered. The system may always handle soft interruptions and cannot complete other tasks.
2) do not handle soft interruptions that are re-triggered. During the return from the interrupt, the kernel will also check all pending soft interrupts and process them as usual. However, any soft interrupt that is manually re-triggered will not be processed immediately, and they will be handled at the next Soft Interrupt execution time. This is usually the time when the next interruption returns. However, in idle systems, it is better to handle soft interruptions immediately. Although it can ensure that the user space is not in hunger, it makes soft interruptions endure hunger, and does not make good use of idle system resources.
Improvement:
The final solution implemented in the kernel will not immediately handle soft interruptions that are re-triggered. 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. These threads run at the lowest priority (Nice value is 19), which prevents them from robbing resources with other important tasks. But they will eventually be executed, so this compromise can ensure that user programs will not be hungry when there is a high volume of soft interruptions. Correspondingly, it can also ensure that "excessive" soft interruptions will eventually be handled.
Each processor has such a thread. All threads are named ksoftirq/n. The difference is that N corresponds to the processor number. There are two such threads on a dual-CPU machine, namely ksoftirqd/0 and ksoftirqd/1. To ensure that there are idle processors, they will handle soft interruptions, so each processor is allocated with such a thread. Once the thread is initialized, it will execute an endless loop like the following:
- In <softirq. C (kernel)>
- Static int ksoftirqd (void * _ bind_cpu)
- {
- Set_user_nice (current, 19 );
- Current-> flags | = pf_nofreeze;
- Set_current_state (task_interruptible );
- While (! Kthread_should_stop ()){
- Preempt_disable ();
- If (! Local_softirq_pending ()){
- Preempt_enable_no_resched ();
- Schedule ();
- Preempt_disable ();
- }
- _ Set_current_state (task_running );
- While (local_softirq_pending ()){
- /* Preempt disable stops CPU going offline.
- If already offline, we'll be on wrong CPU:
- Don't process */
- If (cpu_is_offline (long) _ bind_cpu ))
- Goto wait_to_die;
- Do_softirq ();
- Preempt_enable_no_resched ();
- Cond_resched ();
- Preempt_disable ();
- }
- Preempt_enable ();
- Set_current_state (task_interruptible );
- }
- _ Set_current_state (task_running );
- Return 0;
- Wait_to_die:
- Preempt_enable ();
- /* Wait For kthread_stop */
- Set_current_state (task_interruptible );
- While (! Kthread_should_stop ()){
- Schedule ();
- Set_current_state (task_interruptible );
- }
- _ Set_current_state (task_running );
- Return 0;
- }
Ksoftirq calls do_softirq to process the soft interruptions to be processed (discovered by the softirq_pending () function. By repeating this operation, the Soft Interrupt triggered again will also be executed. If necessary, schedule () is called after each iteration to give more important processes a processing opportunity. After all the operations to be executed are completed, the kernel thread sets itself to the task_interruptible state, arousing the scheduler to select other executable processes for running.
As long as the do_softirq () function finds that the executed kernel thread re-triggers itself, the Soft Interrupt kernel thread will be awakened.