1nic_ack(nic);2napi_schedule(&napi);3return IRQ_HANDLED;NET_RX SOFTIRQidleKWORKERidleRX IRQonCPUIRQSonRECV()asleepSLOT 0SLOT 1SLOT 2SLOT 3IRQackIRQ
Back: the kernel's code (top half, softirq, kworker)Front: the card, CPU, interrupts switch and timer; the receive ring; the programTokens: the packet, the IRQs, the ack, the softirq note and a work itemAt the back, the kernel's code: the network card's interrupt handler (the top half), the network receive softirq and a kworker thread. In front, the card, the CPU with its interrupts switch and a timer on one board, the receive ring in memory, and a program asleep in recv(). The packet lands in the ring by DMA; its interrupt switches the CPU's interrupts off for three short lines, which acknowledge the card and leave a note for the softirq. The softirq then does the real work with interrupts on, so the timer is served at once. 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.

The idea

  1. 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

The bottom half

Deferred work

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.