Saturday, May 19, 2012

Bottom Halves

The purpose of bottom halves is to perform any interrupt-related work not performed by the interrupt handler.  The reason to limit the amount of work in interrupt handler because the handler runs with the current interrupt line disabled on all processors.  Some interrupt handler that register with IRQF_DISABLED run with all interrupt lines disabled on all processors.  Minimizing the time spent while intrrupt lines are disable improve system performance and responsiveness.  Another reason is that intrrupt processing was triggered asynchornously.  It is desirable to defer works as much as possible and return to the work being disrupted.

Defer work "later" means "no now".  In other words, defer work to any point in the future when the system is less busy.  The importance is to run these works when the interrupt lines are enabled.

The original infrastructure provided in Linux for implementing bottom halves is called BH.  BH interface was simple.  It provided a statically created list of 32 bottom halves for the entire system.  The top half sleect the specific bottom half to run by setting a but in the 32-bit register.  Each BH is globally synchornized which means no two BH could run at the same time, even on different processors.  This simple way is inflexible and also a bottleneck.

Later on, task queue was introcduced as a replacement for BH.  The kernel defines a family of queues.  Each queue contained a linked list of functions to call.  The functions in the queue are run at specific time depending on the queue they were in.  Drivers registered their bottom halves in the appropriate queue.  This is still inflexible to replace BH entirely and also not a light-weight enough mechanism to replace performance critical susbsystems such as metworking.

During Linux 2.3 kernel development, softirq and tasklets were introduced which can replace the BH interface.  It is non-trivial to convert BH to softirq and tasklets becasue BHs are globally synchronized.  Softirq are a list of statically defined bottom halves that can run simultaneously on any processor, even 2 of same types concurrently. Tasklets are dynamically created bottom halves based on softirq.   However, 2 of same types cannot run together on the same processor.  Therefore, tasklets are good trade-off between flexibility and performance.

Softirq are used when performance is critical.  However, care must be taken as 2 of same softirqs can run concurrently.  Also softirq must be statically registered at compile time.  Ont the other hands, tasklet code can be registered dynamically.

In 2.5 kernel, work queue was intoduced.  Work queu is a simple method of queuing work to be later performed in process context.  Currently 3 methods exist for deferring work - softirq, tasklet and work queue.  BH and taskqueu interface have been removed.

No comments: