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 LK开发者_运维问答ML 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.
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 usingtasklet_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 bydo_softirq()
andksoftirqd
kernel thread after being enabled bylocal_bh_enable()
or byspin_unlock_bh()
- Tasklets are a bottom-half mechanism built on top of softirqs i.e. tasklets are represented by two softirqs:
HI_SOFTIRQ
andTASKLET_SOFTIRQ
. Tasklets are actually run from a softirq. The only real difference in these types is that theHI_SOFTIRQ
based tasklets run prior to theTASKLET_SOFTIRQ
tasklets. So,tasklet_schedule()
basically callsraise_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
.
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.
Softirqs are statically allocated at compile- time. Unlike tasklets, you cannot dynamically register and destroy softirqs.Tasklets are similar to softirqs (working) however, they have a simpler interface. Softirqs are required only for very high frequency and highly threaded uses , whereas , tasklets do just fine in any other case.
Tasklets are implemented on top of softirq's, so they are softirq's. they are represented by two softirq's "HI_SOFTIRQ & TASKLET_SOFTIRQ" difference is priority. Even though they are implemented on top of softirq's they differ in:
Tasklets can be created/destroyed statically or dynamically but softirq's are only by static way.
Two different tasklets can run concurrently on same cpu. But two of the same type of tasklets can not run on same cpu. Whereas softirq's are in the other way.
Softirq's are reserved for most time critical & important bottom half processing on system.
精彩评论