The idea
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.
The top half
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.
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.
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 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 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.
The bottom half
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.
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.
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.
Deferred work
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.
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.
What happens
;; step 1/11 · why-split
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.
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.