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.
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.
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.
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.
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.
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
DECLARE_TASKLET(name, func, data)
or can also be allocated dynamically and initialized at runtime using tasklet_init(name, func, data)
Concurrency
reentrant
functions and must explicitly protect their data structures with spinlocks.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
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()
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)
softirq latency
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With