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
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
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
Q: Explain the difference between hardirq and softirq context
Q: Explain the difference between hardirq and softirq context
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
- Runs with interrupts enabled
- Can be preempted by hardirqs
- Still cannot sleep (atomic context)
- Used for deferred processing (networking, block I/O)
Q: When would you use a workqueue vs a tasklet?
Q: When would you use a workqueue vs a tasklet?
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
- 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)
Q: How does NAPI improve network performance?
Q: How does NAPI improve network performance?
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)
- First packet triggers interrupt
- Disable further interrupts for that queue
- Switch to polling mode (softirq)
- Process packets in batches (budget-based)
- Re-enable interrupts when queue is empty
- 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
Q: How would you debug an interrupt storm?
Q: How would you debug an interrupt storm?
Answer:Symptoms:Common causes:
- High CPU usage in
si(softirq) orhi(hardirq) - System unresponsive
/proc/interruptsshows rapidly increasing counts
- 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 threshold2
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 patternsSummary
Debugging Tips
Common Misconceptions
Key Takeaways
- Split interrupt handling: Minimal work in hardirq, bulk processing in softirq/workqueue
- Choose the right mechanism: Workqueue if you need to sleep, tasklet/softirq otherwise
- IRQ affinity matters: Align with NUMA topology and application threads
- NAPI is essential: For any high-performance networking
- Monitor interrupt rates: High rates indicate potential issues
Next Steps
- Kernel Synchronization → - Locking mechanisms for concurrent access
- Networking Stack → - See NAPI in action
- eBPF → - Trace interrupt handlers in production