Skip to main content

Interrupts & Exception Handling

Interrupts are fundamental to how Linux handles hardware events, system calls, and exceptional conditions. Understanding the interrupt subsystem is crucial for debugging performance issues and writing high-performance systems code. The analogy: Imagine you’re a chef cooking an elaborate meal (the running process). A kitchen timer goes off (hardware interrupt) — you must stop what you’re doing, turn off the oven, and then decide: do you handle the hot dish right now (hardirq), or set it aside on the counter to plate later when you have a moment (softirq/workqueue)? The key insight is the same as in the kernel: acknowledge the interrupt immediately, but defer the heavy work. If you spend too long handling the timer, all your other dishes burn. This “top-half / bottom-half” split is the single most important concept in interrupt handling. Get it wrong and you get latency spikes, dropped packets, and unresponsive systems.
Interview Frequency: High (especially for performance-critical roles)
Key Topics: IRQ handling, softirqs, tasklets, workqueues, interrupt coalescing
Time to Master: 12-14 hours

Why Interrupts Matter

Every time a network packet arrives, a disk I/O completes, or a timer fires, an interrupt is involved. Understanding interrupts explains:
  • Why context switches happen: Interrupts can preempt any code
  • Network performance: Interrupt coalescing and NAPI
  • CPU affinity effects: IRQ pinning and load balancing
  • Latency sources: Interrupt storms and processing time
A real-world example: At 10Gbps with 64-byte packets, a NIC can generate over 14 million interrupts per second. If each interrupt takes 5 microseconds of CPU time, that’s 70 seconds of CPU time per second — more than one full core just handling interrupts. This is why NAPI (polling mode) was invented, and why interrupt affinity tuning is a daily task for infrastructure engineers at companies like Cloudflare and Datadog.

Interrupt Architecture


Interrupt Types

Exceptions vs Interrupts

Exception Categories


Interrupt Descriptor Table (IDT)

The IDT maps interrupt vectors to handlers:

Viewing IDT Information


Hardware IRQ Handling

IRQ Handler Registration

Example: Network Driver IRQ Handler


Softirqs: High-Priority Deferred Work

The analogy: If hardirqs are the smoke alarm (drop everything, acknowledge immediately), softirqs are the cleanup crew that arrives after the alarm stops. They run with interrupts re-enabled, so new alarms can still ring, but they still cannot take a nap (no sleeping) because they need to be fast. Softirqs are how the kernel processes network packets in bulk, completes block I/O, and runs timer callbacks — all the heavy lifting deferred from the hardirq handler.

Softirq Types

Softirq Properties

Viewing Softirq Activity


Tasklets: Dynamic Deferred Work

Tasklets are built on top of softirqs but more flexible:

Tasklet vs Softirq


Workqueues: Process Context Deferred Work

When you need to sleep or allocate memory:

Workqueue Types

Concurrency Managed Workqueue (cmwq)


Choosing the Right Deferred Work


Interrupt Affinity and Performance

Setting IRQ Affinity

Performance Tuning Patterns

Measuring Interrupt Latency


NAPI: Network Interrupt Coalescing

NAPI (New API) reduces interrupt overhead for high-throughput networking:

NAPI API


Threaded IRQs

For handlers that need more flexibility:

Interview Questions

Answer:Hardirq context (top half):
  • Runs with interrupts disabled on local CPU
  • Must be extremely fast (microseconds)
  • Cannot sleep or allocate memory with GFP_KERNEL
  • Preempts everything including kernel code
Softirq context (bottom half):
  • Runs with interrupts enabled
  • Can be preempted by hardirqs
  • Still cannot sleep (atomic context)
  • Used for deferred processing (networking, block I/O)
Key insight: Split processing into minimal hardirq work (acknowledge, disable, schedule) and heavier softirq work (process data).
Answer:Use workqueue when:
  • You need to sleep (mutex, blocking I/O)
  • You need to allocate memory with GFP_KERNEL
  • The work is not time-critical
  • You need to call functions that might block
Use tasklet when:
  • Work must be done quickly after interrupt
  • You don’t need to sleep
  • Work is small and fast
  • You want serialization (same tasklet won’t run concurrently)
Example: Network driver TX completion → tasklet (fast, no sleeping). Firmware loading → workqueue (needs file I/O, can sleep).
Answer:Problem: At high packet rates, per-packet interrupts cause:
  • High CPU overhead (context switch per packet)
  • Cache thrashing
  • Interrupt storms (CPU spends all time in IRQ handlers)
NAPI solution:
  1. First packet triggers interrupt
  2. Disable further interrupts for that queue
  3. Switch to polling mode (softirq)
  4. Process packets in batches (budget-based)
  5. Re-enable interrupts when queue is empty
Benefits:
  • Amortized interrupt cost across many packets
  • Better cache locality (process batch together)
  • Natural back-pressure (stop polling when overwhelmed)
  • Scales to millions of packets/second
Answer:Symptoms:
  • High CPU usage in si (softirq) or hi (hardirq)
  • System unresponsive
  • /proc/interrupts shows rapidly increasing counts
Debugging steps:
Common causes:
  • Faulty hardware generating spurious interrupts
  • Driver bug not acknowledging interrupt properly
  • Shared IRQ with misbehaving device
  • Misconfigured interrupt coalescing

Practice Exercises

1

Interrupt Statistics

Write a script that monitors /proc/interrupts and alerts when any IRQ rate exceeds a threshold
2

Affinity Configuration

Set up IRQ affinity for a multi-queue NIC to optimize for either throughput or latency
3

eBPF Tracing

Write a bpftrace script to trace interrupt handler latency and identify slow handlers
4

Workqueue Analysis

Use workqueue:* tracepoints to analyze work item execution patterns

Summary


Debugging Tips

Common Misconception: “Setting IRQ affinity is enough to control interrupt placement.” In practice, irqbalance daemon may override your manual settings. Always check systemctl status irqbalance and stop it if you need manual control. Also, some NIC drivers pin MSI-X interrupts internally, overriding smp_affinity writes.

Common Misconceptions


Key Takeaways

  1. Split interrupt handling: Minimal work in hardirq, bulk processing in softirq/workqueue
  2. Choose the right mechanism: Workqueue if you need to sleep, tasklet/softirq otherwise
  3. IRQ affinity matters: Align with NUMA topology and application threads
  4. NAPI is essential: For any high-performance networking
  5. Monitor interrupt rates: High rates indicate potential issues

Next Steps