Linux interrupts the lower half of the tasklet mechanism

Source: Internet
Author: User

Platform: Linux2.6.18

One, soft interrupt

1.1 in File <linux/interrupt.h>

1.1.1 The soft interrupt type used by the current kernel

1 enum 2 {    //3     hi_softirq=0,4    TIMER _SOFTIRQ,5    Net_tx_softirq,6    NET_RX_SOFTIRQ,7      Block_softirq,8    TASKLET_SOFTIRQ9 };

1.1.2 Soft Interrupt Structural body softirq_action

1 structsoftirq_action2{//if MY_SOFTIRQ points to an item in the Softirq_vec array3    //how the kernel calls the soft interrupt handler action: My_softirq->action (MY_SOFTIRQ);4    //The pass parameter MY_SOFTIRQ can be used to add new fields to the struct in the future, without altering the form of the action parameter.5     //to pass the new domain6     void(*action) (structSoftirq_action *);7     void*data;8};

1.2 in File kernel/softirq.c

1.2.1 32 Soft interrupt arrays

static struct softirq_action softirq_vec[32] __cacheline_aligned_in_smp;

1.2.2 The registration function of the soft interrupt handler Open_softirq

1 void OPEN_SOFTIRQ (intvoid (*action) (structvoid *data)2  {3     softirq_vec[nr].data = data; 4     Softirq_vec[nr].action = action; 5 }

1.2.3 __init softirq_init (void) source code

1 void __init softirq_init (void)2{3    OPEN_SOFTIRQ ( TASKLET_SOFTIRQ, Tasklet_action, NULL); 4     Open_softirq (HI_SOFTIRQ, tasklet_hi_action, NULL); 5 }

1.2.4 Tasklet is implemented through soft interrupts.

HI_SOFTIRQ and TASKLET_SOFTIRQ are key to Tasklet's implementation through soft interrupts, as shown in relationship 2-1.

Figure 1-1 Tasklet the connection between data structures implemented by soft interrupts

The Tasklet mechanism creates HI_SOFTIRQ or TASKLET_SOFTIRQ two types of soft interrupts, and then executes the corresponding Tasklet handler tasklet_hi_action or tasklet_action according to the soft interrupt scheduling process.

It can be concluded that Tasklet is a kind of lower half mechanism realized by soft interrupt.

1.2.5 Performing soft interrupt Do_softirq ()

The key function executed in the DO_SOFTIRQ () function is __DO_SOFTIRQ (), which is part of the critical code in parsing __do_softirq ().

1 #defineMax_softirq_restart 102 intMax_restart =Max_softirq_restart;3Pending = Local_softirq_pending ();//pending 32-bit bitmap to save soft interrupts, type corresponding bit is 1 o'clock wait for processing4 Restart:5     /*Reset the pending bitmask before enabling IRQs*/6Set_softirq_pending (0);//The actual soft-break bitmap is zeroed first. Q 1: For Tasklet's HI_SOFTIRQ 7//or TASKLET_SOFTIRQ type of soft interrupt at the same time there are multiple cases, so clear 0 is reasonable? Of course 8//Is there a case of soft interrupts with multiple HI_SOFTIRQ or TASKLET_SOFTIRQ types at the same time?9     // answer 1: the entire list of Tasklet_vec is processed when the tasklet_action handler is called in a DO_SOFTIRQ. TenLocal_irq_enable ();//Post-Open local interrupt Oneh =Softirq_vec; A      Do { -         if(Pending &1) {//the type corresponds to a bit of 1 o'clock satisfying the condition, entering processing -H->action (h);//The way in which this kernel dispatches interrupt handlers is explained in 1.2 the rcu_bh_qsctr_inc (CPU); -         } -h++; -Pending >>=1; +} while(pending); - local_irq_disable (); +Pending = Local_softirq_pending ();// Q 2: at what time did HI_SOFTIRQ and TASKLET_SOFTIRQ trigger?  A     //does the DO_SOFTIRQ () have no processing of HI_SOFTIRQ or TASKLET_SOFTIRQ soft interrupts exist?  at     //If so, how is it handled?  -     // Answer 2: The timing of the trigger is now known to be: Tasklet_schedule scheduling, and the tasklet_action processing functionwhen the tasklet is not processed. 
-    //problem 2, the analysis under the face of the tasklet_action handler may determine existence. How to deal with the current analysis of a possible -    //is handled by the Tasklet_action Tasklet returned to Tasklet_vec, and all triggered -    //TASKLET_SOFTIRQ Soft Interrupt. So, the max_softirq_restart repetition of the DO_SOFTIRQ function in this time -    //, it will continue to be processed. (Am I right?2014 November 2) in if(Pending &&--max_restart)//in the process of processing soft interrupts, there is a new soft interrupt hangs, it is again dispatched, but - //no more than max_softirq_restart times. to GotoRestart

1.1.1 Trigger Soft interrupt raise_softirq (), Raise_softirq_irqoff ()

In 2.5 refers to the soft interrupt of the 32-bit bitmap, if the nth bit is set to 1, then the nth bit corresponds to the type of soft interrupt waiting to be processed. and the corresponding position 1 processing, it is triggered by the soft interrupt. The analysis function Raise_softirq (), Raise_softirq_irqoff () shows that the core operation of two functions is __raise_softirq_irqoff (NR); Its complete function is to set the NR corresponding soft interrupt bitmap 1.

1 #define __raise_softirq_irqoff (NR) do {or_softirq_pending (1UL << (NR)),} while (0)2# Define or_softirq_pending (x)  (local_softirq_pending () |= (x))

Second, the tasklet mechanism

2.1 In File <linux/interrupt.h>

2.1.1 Tasklet Structural Body

1 structtasklet_struct2 {3     structTasklet_struct *Next;4UnsignedLongState ;5 atomic_t count;6     void(*func) (unsignedLong);7UnsignedLongdata;8 };9 enumTen { OneTasklet_state_sched,/*Tasklet is scheduled for execution*/ ATasklet_state_run/*Tasklet is running (SMP only)*/ -};

2.2 In File kernel/softirq.c

2.2.12 single-processor data structures Tasklet_vec and Tasklet_hi_vec

1 /*tasklets*/2 structTasklet_head3 {4     structTasklet_struct *list;5 };6 //a linked list of tasklet_struct structures7 StaticDEFINE_PER_CPU (structTasklet_head, Tasklet_vec) ={NULL};8 StaticDEFINE_PER_CPU (structTasklet_head, Tasklet_hi_vec) = {NULL};

2.2.2 Dispatch Tasklet

The only difference between the dispatch function Tasklet_schedule and Tasklet_hi_schedule is the use of TASKLET_SOFTIRQ and HI_SOFTIRQ respectively, defined in file <linux/interrupt.h>, First check whether the state of the Tasklet is tasklet_state_sched, if it has been dispatched, return; otherwise, tasklet_state_sched location 1, then enter __tasklet_schedule and __tasklet_hi _schedule, the analysis __tasklet_schedule is as follows.

1 voidFastcall __tasklet_schedule (structTasklet_struct *t)2 {3UnsignedLongflags;4 5 Local_irq_save (flags);6T->next = __get_cpu_var (tasklet_vec). List;//Insert the Tasklet of the parameter T into the Tasklet_vec list header7__get_cpu_var (tasklet_vec). List =T;8Raise_softirq_irqoff (TASKLET_SOFTIRQ);//trigger TASKLET_SOFTIRQ Soft interrupt, corresponding to 1.2.5 section of the question 29 Local_irq_restore (flags);Ten}

2.2.3 Tasklet Processing Program

The handler tasklet_action and Tasklet_hi_action functions are similar, as shown in flow 2-1 of the tasklet_action function.

Figure 2-1 Tasklet_action Flowchart

Third, tasklet some understanding through soft interrupt implementation

The following is a specific analysis of TASKLET_SOFTIRQ types.

1) The TASKLET_SOFTIRQ soft interrupt is defined in the soft interrupt, and the tasklet_action handler function is called in the DO_SOFTIRQ function to enter the Tasklet handler.

2) in the tasklet_action processing function, according to figure 2-1 Tasklet_action flowchart, the Tasklet_vec chain table in accordance with the following three conditions of the lower half of the tasklet is processed.

The three conditions are: ⑴tasklet_state_run bit is 0;⑵count=0;⑶tasklet_state_sched bit is 1. The implication is that the dispatched Tasklet is processed if it is not running on another processor and is not banned. Tasklet_hi_action (tasklet_action) handler Operation Tasklet_hi_vec (TASKLET_VEC) linked list. Related data structures are shown in relation to 3-1.

Fig. 3-1 correlation between soft interrupt and tasklet mechanism data structure

Linux interrupts the lower half of the tasklet mechanism

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.