</>EmbeddedInterview

What are top halves and bottom halves?

MediumLinux Kernel & Device Drivers

Short answer: Interrupt handling is split in two so that the hard IRQ handler stays short. The top half is the handler registered with request_irq: it runs in hard-interrupt context with its line masked, acknowledges the device, grabs urgent data (e.g. a status register or a received byte) and schedules the rest. The bottom half is deferred work that runs later with interrupts enabled: softirqs, tasklets (deprecated), workqueues and threaded IRQs.

Why split: while a hard IRQ handler runs, its line (and on many systems, other interrupts on that CPU) is blocked, increasing interrupt latency for everyone; it also cannot sleep, so anything involving I2C/SPI, mutexes or memory allocation with GFP_KERNEL must be deferred.

Typical flow for a network or DMA device

IRQ → top half: read & clear status, disable further device IRQs, napi_schedule() → return IRQ_HANDLED
     → softirq (NET_RX): NAPI poll processes up to "budget" packets, re-enables device IRQs when done

Choosing a bottom half

Need Use
Must sleep (I2C/SPI, mutex, GFP_KERNEL) threaded IRQ or workqueue
Very high rate, per-CPU, no sleeping softirq (subsystem-level: networking, block, timers) or BH workqueue
Delay before running delayed_work (schedule_delayed_work)
Run on a schedulable RT priority threaded IRQ (irq/NN-name thread, SCHED_FIFO 50 by default)

Return values of the top half: IRQ_HANDLED, IRQ_NONE (not my device: important for shared interrupt lines and spurious-IRQ detection) and IRQ_WAKE_THREAD (run the threaded handler).