Interrupt handlers: top halves, bottom halves and deferred work
About this lesson
When a device interrupts, the CPU drops what it was doing and runs the kernel's handler, and while that handler runs, interrupts on that CPU are off: every other device has to wait. So Linux keeps the handler tiny (the top half) and does the real work later with interrupts on (the bottom half): in a softirq, or in a kworker thread when the work must sleep. Follow one network packet from the card, through a three-line handler, to the program waiting for it. The Hardware lesson on interrupts and I/O covers polling, interrupts and DMA, and Device drivers and the I/O stack shows where the handler sits in a driver.
Short now, the rest later
A device interrupt stops whatever the CPU is running. While its handler runs, interrupts on that CPU are off, so other devices must wait. So Linux splits the work: a short top half now, a bottom half later.
top half the interrupt handler itself (hard IRQ)
interrupts off on this CPU: be quick
acknowledge the device, note what is pending
bottom half the real work, later, interrupts on
softirq (cannot sleep)
workqueue (a kernel thread, may sleep)
xv6 and CS162 use the two names the other way round:
see the notes.Read the lesson step by step
Short now, the rest later
A device interrupt stops whatever the CPU is running. While its handler runs, interrupts on that CPU are off, so other devices must wait. So Linux splits the work: a short top half now, a bottom half later.
top half the interrupt handler itself (hard IRQ) interrupts off on this CPU: be quick acknowledge the device, note what is pending bottom half the real work, later, interrupts on softirq (cannot sleep) workqueue (a kernel thread, may sleep) xv6 and CS162 use the two names the other way round: see the notes.A packet arrives
A packet arrives at the network card. The card copies it straight into a ring of buffers in memory by DMA (direct memory access), with no work from the CPU. Then it raises its interrupt line: new data.
card receives a packet DMA card -> receive ring in memory, slot 0 (the CPU is not involved in the copy) IRQ the card raises its interrupt line program still asleep in recv(): no data yetThe CPU drops everything
The CPU finishes its current instruction, saves its place and switches to kernel mode. Through the interrupt table it jumps to the card's handler, the top half. Interrupts on this CPU are now off.
CPU finish the current instruction save its place, switch to kernel mode table interrupt number -> the card's handler interrupts OFF on this CPU the top half (an irq_handler_t): irqreturn_t nic_irq(int irq, void *dev) { nic_ack(nic); napi_schedule(&napi); return IRQ_HANDLED; }Line 1: tell the card
Line 1 tells the card its interrupt was seen and masks the card's receive interrupt. More packets may land in the ring, but they will not interrupt again until the bottom half has caught up.
line 1 nic_ack(nic); nic_ack is this driver's own helper: acknowledge "interrupt seen" mask card receive interrupt -> off NAPI rule: keep the interrupt masked after scheduling; unmask only after napi_complete_done()Line 2: leave a note
Line 2, napi_schedule, does not touch the packet. It only marks the network receive softirq as pending: a note that says run the bottom half soon. A softirq is a bottom half the kernel runs itself.
line 2 napi_schedule(&napi); puts this card on the CPU's poll list raises NET_RX_SOFTIRQ (number 3 of 10 kinds) softirq pending packet still in the ring, untouchedLine 3: interrupts back on
Line 3 returns IRQ_HANDLED: yes, this was my device. The top half ran three short lines and never waited for anything. The CPU leaves the handler and switches interrupts back on.
line 3 return IRQ_HANDLED; IRQ_HANDLED = 1 (IRQ_NONE = 0: not my device) interrupts ON again top half 3 lines, no waiting, no packet workThe bottom half runs
On its way out of the interrupt, the kernel runs any pending softirqs, now with interrupts on. The network receive softirq takes the packet from the ring and passes it up the network stack.
irq exit pending softirqs? yes: NET_RX interrupts ON while softirqs run NET_RX calls the card's poll function ring slot 0 -> network stack (IP, TCP/UDP) limit up to 10 rounds or 2 ms, then the rest goes to the ksoftirqd kernel threadA timer interrupt cuts in
Mid-way, the timer interrupts. Because interrupts are on, its top half runs at once, then the softirq carries on where it stopped. Had the packet work been inside one long handler, the timer would have waited.
timer raises its interrupt interrupts on -> timer top half runs at once (interrupts off just for its few lines) softirq paused, then resumes see it live: /proc/interrupts counts interrupts per CPU per device (LOC = local timer)The program wakes
The softirq puts the data on the socket's queue and wakes the program asleep in recv(). The ring is empty again, so it unmasks the card's receive interrupt, ready for the next packet.
socket data queued program woken: recv() returns the data ring empty -> napi_complete_done() card receive interrupt -> on againWork that must sleep
A softirq, like a top half, must never sleep: it belongs to no process. Work that may wait, for a lock or for memory, goes on a workqueue instead. A kernel thread called kworker runs it later, and it may sleep.
may it sleep? top half (hard IRQ) no softirq / tasklet no workqueue (kworker) yes: a real kernel thread schedule_work(&w); queue a work item (a struct with a function pointer) request_threaded_irq() top half returns IRQ_WAKE_THREAD and a kernel thread does the restWhy split the handler?
Interrupts-off time is waiting time for every other device. The top half keeps it to a few lines; the bottom half does the real work with interrupts on, as a softirq, or in a kworker thread when it must sleep.
how long the timer waits, as a Python simulation: # one CPU; times are made-up units TIMER_AT = 3 # the timer interrupt arrives here WHOLE = 40 # all the packet work in one handler TOP = 2 # top half only: ack + napi_schedule def timer_wait(irqs_off_until): # interrupts are off until the hard handler returns return max(0, irqs_off_until - TIMER_AT) print("one long handler: timer waits", timer_wait(WHOLE)) print("top half + softirq: timer waits", timer_wait(TOP)) prints: one long handler: timer waits 37 top half + softirq: timer waits 0
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01Why must a Linux top half (the hard interrupt handler) be short?
The kernel hacking guide puts it plainly: because a hard IRQ handler runs with interrupts disabled, it has to be fast. Every microsecond in it delays every other interrupt on that CPU.
02In the network example, what does napi_schedule() do inside the top half?
It only adds the card to the CPU's poll list and raises NET_RX_SOFTIRQ. The DMA copy already happened, and the packet is processed by the softirq after the handler returns.
03When does a pending softirq run, and with interrupts on or off?
Pending softirqs run as a hardware interrupt handler exits (and at a few other points). The kernel switches interrupts back on before calling them, which is why the timer could cut in during the softirq.
04A driver's deferred work must wait for a mutex. Where should it run?
Top halves, softirqs and tasklets must never sleep. A workqueue item runs in a kernel thread, which the scheduler can put to sleep and wake again, so it may wait for a lock or for memory.
05Why does the card's receive interrupt stay masked until the softirq has emptied the ring?
This is NAPI: one interrupt starts a polling pass that drains many packets, and the interrupt is unmasked only after napi_complete_done(). OSTEP ch. 36 explains how one interrupt per packet can livelock a busy machine.
Glossary
- Interrupt
- A signal from a device that makes the CPU stop what it is running and jump to a kernel handler chosen from the interrupt table.
- Top half
- In Linux, the interrupt handler itself (hard IRQ). It runs with interrupts off on that CPU, so it only does the urgent minimum. Some textbooks, such as xv6 and CS162, use the name the other way round.
- Bottom half
- In Linux, the deferred part of interrupt work, run later with interrupts on: a softirq, a tasklet or a workqueue item.
- Softirq
- A kind of bottom half built into the kernel (Linux has 10, such as NET_RX for received packets). Pending softirqs run when an interrupt handler exits. They must not sleep.
- Workqueue
- A queue of work items, each a function to run later. A kworker kernel thread runs them, so the work may sleep.
- DMA
- Direct memory access: a device copies data to or from memory without the CPU doing the copy.
- NAPI
- Linux's receive scheme for network cards: one interrupt, then poll the ring from a softirq with the card's interrupt masked until the ring is empty.
- Mask (an interrupt)
- Tell a device not to raise its interrupt for now. In the example the card keeps filling the ring by DMA while its receive interrupt is masked.
Notes and sources
- Top half and bottom half are used the other way round in some textbooks. In Linux, the top half is the interrupt handler and the bottom half is the deferred work. The xv6 book (MIT 6.1810) and Berkeley CS162 call the system-call side of a driver the top half and the interrupt handler the bottom half.
- nic_ack is a made-up helper standing for this driver's own register writes. irqreturn_t, IRQ_HANDLED and napi_schedule are real Linux kernel names; the test compiles the handler with stand-ins for the kernel headers.
- The kernel hacking guide says a hard interrupt handler runs with interrupts disabled and is never re-entered. Interrupts are switched per CPU (the kernel's local_irq routines act on the local CPU only), so other CPUs can still take interrupts meanwhile.
- Pending softirqs run when a hardware interrupt handler exits (and in a few other places). To stop them starving programs, the kernel runs at most 10 rounds or about 2 ms, then hands the rest to a per-CPU kernel thread, ksoftirqd.
- Masking the card while the softirq drains the ring is NAPI, Linux's answer to receive livelock: under a flood of packets, one interrupt per packet could leave the CPU doing nothing but handling interrupts (OSTEP ch. 36).
- The drawing shows an interrupt line, a wire from the card to the CPU. Modern PCIe network cards usually signal with MSI or MSI-X instead: the card writes a small message to a special memory address, and that write raises the interrupt. The effect on the CPU is the same.
- The times in the simulation are made-up units, chosen only to show the shape: what matters is how long interrupts stay off, not the exact numbers.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 36: I/O Devices
- Linux kernel documentation: Unreliable Guide To Hacking The Linux Kernel (hard IRQs, softirqs and tasklets)
- Linux kernel documentation: NAPI
- Linux kernel documentation: Workqueue
- Linux kernel documentation: Linux generic IRQ handling (request_threaded_irq)
- Linux source: kernel/softirq.c
- Linux source: include/linux/interrupt.h
- Linux source: include/linux/irqreturn.h
- Linux source: net/core/dev.c (napi scheduling and NET_RX_SOFTIRQ)
- Linux kernel documentation: The MSI Driver Guide HOWTO
- Linux man-pages: recv(2)
- Linux man-pages: proc_interrupts(5)
- MIT 6.1810, xv6: a simple, Unix-like teaching operating system (RISC-V), ch. 5: Interrupts and device drivers
- Berkeley CS162, Lecture 18 (device drivers: top half and bottom half)
Quiz Unlocks after the last step.