Servicing hardware and CPU events: the IDT & vectors (APIC/MSI-X/IPIs), interrupt vs fault vs trap vs abort, top-half/bottom-half split (softirq/tasklet/workqueue — which can sleep), NAPI, the can't-sleep rule, spin_lock_irqsave, and IRQ affinity.
How the CPU stops what it's doing to service hardware and CPU-detected events — and how Linux keeps interrupt handlers fast so the system stays responsive. Original, interview-focused notes.
Two families: an interrupt is asynchronous, raised by a device (a NIC has a
packet); an exception is synchronous, raised by the CPU while executing an instruction
(page fault, divide-by-zero, a syscall/trap). Both vector through the IDT to a handler
that runs in a special context where you cannot sleep.
| Type | Source | Example |
|---|---|---|
| Interrupt (IRQ) | External device, async | NIC RX, disk done, timer |
| Fault | CPU, correctable, re-runs the instruction | Page fault (map the page, retry) |
| Trap | CPU, intentional | syscall, breakpoint |
| Abort | CPU, unrecoverable | Machine-check |
An interrupt handler must be fast — while it runs, that interrupt (often more) is masked and no process runs on the CPU. So Linux splits the work:
copy_to_user.| Mechanism | Runs in | Note |
|---|---|---|
| softirq | Interrupt context (softirq) | Fixed set, high-perf (e.g. NET_RX); can run concurrently on CPUs. Can't sleep. |
| tasklet | Built on softirq | Simple deferral, serialized per tasklet. |
| workqueue | Kernel thread (process context) | Can sleep — use when the deferred work may block. |
NAPI is the classic example: under load the NIC switches from per-packet interrupts to
polling in softirq, avoiding an interrupt storm. ksoftirqd drains softirqs when they
overwhelm a CPU.
spin_lock_irqsave variant when sharing data with a handler).local_irq_save) to protect data touched by both a handler and
normal code; keep these windows tiny — masking hurts latency./proc/irq/<n>/smp_affinity pins an IRQ to CPUs; spreading NIC-queue
interrupts across cores (RSS) is core to high-throughput and low-latency tuning. See
Linux Basics.spin_lock_irqsave)