Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SoftIRQs and Tasklets

Tags:

While studying Linux interrupt handling I found that Tasklets and SoftIRQs are two different methods of performing "bottom half" (lesser priority work). I understand this (quite genuine need).

Difference being, SoftIRQs are re-entarant while a Tasklet is NOT. That same SoftIRQ can run on different CPUs while this is NOT the case with Tasklets.

Though I understand this from surface but I fail in understanding the requirements of the two features. In what case(s) we may use these facilities ? How to recognize that I should use Tasklets now and SoftIRQs then.

Also what do we mean by Tasklets are made upon SoftIRQs ? In one of the books I read in LKML there were debates upon removing Tasklets. I got completely confused why one would bring in such a feature ? Some shortsightedness (No offense meant) ?

Any pointers on this will help a lot.

like image 262
ultimate cause Avatar asked Aug 21 '11 08:08

ultimate cause


People also ask

What are the differences between Softirq Tasklet and Workqueue?

Workqueue functions run in the context of a kernel process, but tasklet functions run in the software interrupt context. This means that workqueue functions must not be atomic as tasklet functions. Tasklets always run on the processor from which they were originally submitted.

What is Tasklets?

Tasklets are a deferral scheme that you can schedule for a registered function to run later. The top half (the interrupt handler) performs a small amount of work, and then schedules the tasklet to execute later at the bottom half.

What is Softirqs?

SoftIrqs allow the critical part of servicing hardware interrupts to be as short as possible; instead of having to process the entire hw interrupt on the spot, the important data is read off the device into RAM or otherwise, and then a SoftIrq is started to finish the work.

What is bottom half and top half?

The so-called top half is the routine that actually responds to the interrupt—the one you register with request_irq. The bottom half is a routine that is scheduled by the top half to be executed later, at a safer time.


2 Answers

include/linux/interrupt.h

/* PLEASE, avoid to allocate new softirqs, if you need not _really_ high    frequency threaded job scheduling. For almost all the purposes    tasklets are more than enough. F.e. all serial device BHs et    al. should be converted to tasklets, not to softirqs.  */  enum {         HI_SOFTIRQ=0,    /* High Priority */         TIMER_SOFTIRQ,         NET_TX_SOFTIRQ,         NET_RX_SOFTIRQ,         BLOCK_SOFTIRQ,         BLOCK_IOPOLL_SOFTIRQ,         TASKLET_SOFTIRQ,         SCHED_SOFTIRQ,         HRTIMER_SOFTIRQ,         RCU_SOFTIRQ,    /* Preferable RCU should always be the last softirq */          NR_SOFTIRQS }; 

The key differences between softirq and tasklet are:

Allocation

  • Softirqs are statically allocated at compile-time. Unlike tasklets, you cannot dynamically register and destroy softirqs.
  • Tasklets can be statically allocated using DECLARE_TASKLET(name, func, data) or can also be allocated dynamically and initialized at runtime using tasklet_init(name, func, data)

Concurrency

  • Softirqs can run concurrently on several CPUs, even if they are of the same type because softirqs are reentrant functions and must explicitly protect their data structures with spinlocks.
  • Tasklets are non-reentrant and tasklets of the same type are always serialized: in other words, the same type of tasklet cannot be executed by two CPUs at the same time. However, tasklets of different types can be executed concurrently on several CPUs.

Processing

  • Softirqs are activated by means of the raise_softirq(). The pending softirqs are processed by do_softirq() and ksoftirqd kernel thread after being enabled by local_bh_enable() or by spin_unlock_bh()
  • Tasklets are a bottom-half mechanism built on top of softirqs i.e. tasklets are represented by two softirqs: HI_SOFTIRQ and TASKLET_SOFTIRQ. Tasklets are actually run from a softirq. The only real difference in these types is that the HI_SOFTIRQ based tasklets run prior to the TASKLET_SOFTIRQ tasklets. So, tasklet_schedule() basically calls raise_softirq(TASKLET_SOFTIRQ)
  • Note that softirqs (and hence tasklets and timers) are run on return from hardware interrupts, or on return from a system call. Also as soon as the thread that raised the softirq ends, that single softirq (and on other) is run to minimize softirq latency.
like image 173
manav m-n Avatar answered Nov 04 '22 16:11

manav m-n


Sofirqs are re-entrant , that is the different CPU can take the same softirq and execute it while the Tasklets are serialized that is the same CPU which is running the tasklet has the right to complete it , no other CPU can take it(in case of scheduling). refer this excellent article.

Also you can enable/disable the defer processing by using the local_bh_enable() on the local CPU which actually makes the _ _local_bh_count non zero.

Also read this book (free downloadable) Page number 131 - which explains the difference as well as explaination using the code example with a fake/dummy device - roller.

like image 31
Raulp Avatar answered Nov 04 '22 14:11

Raulp