Linux Kernel Tutorials - Tasklets and Softirqs

Softirq and Tasklets

In last chapter we have discussed about interrupts and interrupt handling , bottom halves and top halves.In this chapter we will discuss different types of bottom halves available in Latest linux kernel.

As bottom halves perform the task which was deferred by top halves and during bottom halves all the interrupt lines are enabled. To perform bottom halves task latest kernel provide following bottom halves techniques.

  1. Softirq
  2. tasklets
  3. Work Queues

Lets discuss each of these types of bottom halves in details.

1)Softirq

Softirqs are a set of statically defined bottom halves which can run simultaneously on any of the processor.

Even two of the same type can run on any processor concurrently.

Because two softirqs can run concurrently , It is the responsibility of programmer to take care of synchronization between softirqs .

A softirq never preempts other softirq.

Only interrupt handler can preempt softirq.

softirq runs with interrupt enabled and can’t sleep as it runs in interrupt context.

 

Implementation

softirq statically created at compile time.

it is represented by softirq_action structure defined in <linux/interrupt.h>

struct softirq_action{

void (*action) (struct softirq_action *);

}

A 32 bit entry of this structure is defined in kernel/softirq.c

kernel enforce a limit of 32 registered softirq in the latest versions.

Softirq handler

softirq handler defines a function which will run when kernel schedule the softirq to run. below is the prototype of softirq handler.

void softirq_handler(struct softirq_action *);

action is the function which will actually run on scheduling of softirqs and perform all task of bottom halves.

Executing softirq

An interrupt handler or we can say top halves marks its softirq before returning. When CPU will get time , softirq will get chance to execute.

Declaring a softirq

softirq are statically declared at compile time via an enum in <linux/interrupt.h>. This enum index is used by kernel as a relative priority. This index starts from zero. softirq with the index 0 has highest priority . For creating a softirq we need to add our entry to this enum.

Registering softirq

softirq handler is registered at run time by open_softirq() , which takes two arguments : softirq index and its handler function.

 

Raising Softirq

after a handler is added to the enum list and registered via open_softirq() , it is ready to run .softirq are raised by using raise_softirq() call. after raising softirq will get chance to execute whenever system is idle.

Real usage of softirq

softirq are used in time critical and important bottom halves processing. We use softirq in mostly networking devices like Ethernet , NIC and block devices like USB, Hard disk etc.

 

 

TAsklets

Tasklets is another bottom-half mechanism which is built on top of softirq.

same tasklets cannot run on more than one processor at same time.

so in tasklets we need not to worry about synchronization among them.

Tasklets has very simple interface as compared to softirq.

tasklets cannot sleep as it runs in interrupt context.

Tasklets implementation

as already discussed that tasklets are implemented on top of softirq. Tasklets are represented by two softirqs : Hi_SOFTIRQ and TASKLET_SOFTIRQ. HI_SOFTIRQ has high priority than TASKLET_SOFTIRQ.

taslklets are defined by tasklet_struct structure which is declared in <linux/interrupt.h>

diagram

 

scheduling tasklets

TAsklets are scheduled via the tasklets_schedule() and tasklets_hi_schedule() functions which receives pointer to tasklet_struct structure as an argument.

Declaring your tasklets

tasklets can be created dynamically and statically depending upon requirement.

statically creation

DECLARE_TASKLET(name,func,data)

dynamically creation

tasklet_init(t,tasklet_handler,dev);

writing tasklet handler

tasklet handler is the function which runs upon scheduling of tasklets. below is the prototype for tasklet handler

void tasklet_handler(unsigned long data)

scheduling tasklet

tasklets are scheduled tasklet_schedule() which takes pointer to tasklet_struct.

after tasklet is scheduled , it runs once at some time in the near future.

Real usage of tasklets

tasklets are the most widely used bottom-half. its a most preffered way for implementing bottom half for a normal hardware .

 

adana elektrikci

', 'auto'); ga('send', 'pageview');