When referring to Linux Kernel Interrupt handlers as I know there are two stages of interrupt executions first is Top Half and second Bottom Half.
I know Top Half will be executed immediately on occurrence of interrupt from HW but my doubt is when and how bottom half is executed ?
The bottom half is a routine that is scheduled by the top half to be executed later, at a safer time. The use of the term bottom half in the 2.4 kernel can be a bit confusing, in that it can mean either the second half of an interrupt handler or one of the mechanisms used to implement this second half, or both.
There are three main types of bottom halves: namely tasklets, workqueues and kernel threads. Tasklets are used to queue up work to be done at a later time. Tasklets can be run in parallel, but the same tasklet cannot be run on multiple CPUs at the same time.
Work queues are a very simple yet useful method of queueing work to later be performed in process context. We will get to them later. Consequently, today 2.6 has three bottom-half mechanisms in the kernel: softirqs, tasklets, and work queues.
Device drivers typically have a top and bottom half. The top half gets data and commands from the kernel, and places them into a data structure shared with the bottom half. The bottom half gets called when there is an interrupt, and places answers in the common data structure.
¿ when and how bottom half is executed ?
When: it is executed AFTER the interrupt handler, and in fact, its execution is triggered by the interrupt handler itself. Sometimes it executes just right after the interrupt handler, sometimes not.
How: if your bottom half is implemented by a tasklet, its execution is scheduled by using the task_schedule()
function, normally called from inside the interrupt handler. This function does not execute the tasklet, but informs the scheduler to queue the tasklet function for later execution.
The bottom halves are implemented as tasklets (deferred interrupt context), workqueues (process context) and softirqs (rarely, only 9 of those in Linux kernel).
The timer interrupt handler checks which of the 9 softirqs are to be executed (scheduler, hrtimers, network rx/tx, tasklets, etc.). If there is any pending softirq, (say a list of tasklets that the top-half has notified) then those get executed. As for any tasklet, this is true for any other softirq too. Also, because tasklet is a kind of softirq it can only be executed on the same CPU core.
On the contrary, the workqueues are executed when the corresponding process subsequently context switches in. Hence, unlike tasklets, these can sleep and can be scheduled on other CPU cores too.
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