Interrupt service routine

Also known as: interrupt handler

On this page 7

An interrupt service routine (ISR), also called an interrupt handler, is the function that the CPU executes in response to an interrupt, suspending the current work to service a hardware or software event immediately. In operating systems, ISRs are the point where drivers & devices meet the processor: a network card, timer or storage controller signals the CPU, and the ISR decides what the event means and what must happen next.

Because an ISR preempts whatever was running, including other kernel code, it must be short, fast and carefully restricted in what it may do.

How the CPU enters an interrupt service routine

When a device asserts an interrupt request, the interrupt controller forwards a vector number to a CPU core. The core finishes the current instruction, saves the minimal processor context, switches to kernel mode if necessary and looks up the handler address in the interrupt vector table, called the interrupt descriptor table on x86.

The kernel’s generic entry code then saves the remaining registers and calls the driver-specific ISR registered for that interrupt line. When the routine returns, the kernel acknowledges the interrupt to the controller, restores the context and resumes the interrupted code, or schedules a different task if the ISR made one runnable.

Interrupts can also nest: a higher-priority interrupt may preempt a running ISR unless the kernel masks interrupts on that core. Many kernels, including modern Linux, run handlers with local interrupts disabled to keep stack use and locking simple, which is another reason every handler must finish within a few microseconds.

StagePerformed byTypical cost
Device raises requestHardware and interrupt controllerNanoseconds
Context save and vector lookupCPU microcode and entry codeTens to hundreds of cycles
Driver ISR runsKernel-mode driverA few microseconds at most
End of interrupt signalKernel interrupt layerTens of cycles
Return or rescheduleKernel entry and schedulerHundreds of cycles
Stages of interrupt handling from signal to return

What an ISR is allowed to do

An ISR runs in interrupt context, which has no associated process and cannot be put to sleep. Any operation that might block, such as allocating memory with a waiting flag, acquiring a mutex or copying from user memory, is forbidden inside the handler.

A correct ISR therefore does the minimum urgent work: it reads the device status, acknowledges or silences the interrupt source, moves small amounts of data and schedules deferred processing. Shared data is protected with spinlocks and by briefly disabling local interrupts rather than with sleeping locks.

  • Allowed: reading and writing device registers, updating ring buffer indexes, waking a waiting thread, queuing deferred work.
  • Not allowed: sleeping, waiting for I/O, calling functions that may page fault, holding a lock that process context takes without disabling interrupts.
  • Strongly discouraged: long loops, heavy computation and extensive logging, which all increase interrupt latency for every other device.

Top half and bottom half processing

Kernels split interrupt work into a top half, the ISR itself, and a bottom half that runs later with interrupts enabled. This design keeps the time spent with interrupts masked short while still allowing complex processing such as protocol handling.

MechanismKernelMay sleep
Softirq and taskletLinuxNo
Threaded IRQ handlerLinuxYes
WorkqueueLinuxYes
Deferred procedure callWindowsNo
Work itemWindowsYes
Deferred processing mechanisms in common kernels

On Windows, an ISR runs at an elevated interrupt request level called DIRQL, and the deferred procedure call runs at DISPATCH_LEVEL, still unable to wait. Work that needs to block must move further down to a work item running at PASSIVE_LEVEL, which mirrors the Linux path from softirq to workqueue.

Interrupt service routines compared with polling

Interrupt-driven I/O and polling are the two basic ways a driver learns that a device needs attention. Neither is universally better; the right choice depends on the event rate and latency target.

CriterionInterrupt service routinePolling
CPU use at low event rateVery lowWasted on empty checks
CPU use at very high rateHigh overhead per eventEfficient batching
LatencyLow, bounded by interrupt latencyDepends on poll interval
Typical useKeyboards, timers, most storageFast NVMe and network paths under load
Interrupts versus polling

Modern drivers often combine both approaches. The Linux NAPI network model takes one interrupt, disables further interrupts from the device and then polls the receive queue until it is empty, avoiding an interrupt storm at millions of packets per second. Message signaled interrupts further reduce overhead by allowing one vector per queue and per core.

Common ISR mistakes and their symptoms

Faults in interrupt handlers are among the hardest driver bugs to diagnose, because they depend on timing and can freeze the whole machine. The table lists frequent errors.

MistakeSymptomFix
Not acknowledging the deviceInterrupt fires endlessly, system hangsClear the device status register
Sleeping in the handlerKernel warning or crashMove work to a threaded handler
Lock taken without disabling interruptsDeadlock under loadUse the interrupt-safe lock variant
Claiming a shared line wronglyOther devices stop respondingReturn not-handled when the device is idle
Typical ISR defects, symptoms and fixes

On shared legacy lines, several devices use the same interrupt, so each ISR must check its own device first and report whether it handled the event. Kernels detect lines that fire with no handler claiming them and may disable the line to keep the system alive.

A non-technical view

Picture a chef cooking several dishes when the doorbell rings. The chef does not finish every dish first; the chef pauses briefly, signs for the delivery, puts the box on the counter and goes back to cooking. Unpacking the box happens later.

An interrupt service routine is that quick trip to the door. When a device such as a keyboard or network card needs attention, the computer stops for a moment, handles the urgent part and returns to its work. Keeping that pause short is what keeps the whole computer responsive.

A concrete example

A team building an industrial data logger on embedded Linux finds that sensor samples arriving every 100 microseconds are occasionally lost. Measurements show that the driver’s ISR copies a 4 KiB block and computes a checksum, taking about 60 microseconds with interrupts masked, so a second interrupt sometimes arrives before the first finishes.

The engineers reduce the ISR to reading the status register, recording the buffer index and waking a threaded handler, which then copies data and verifies the checksum. ISR time falls to roughly 3 microseconds, worst-case interrupt latency for other devices drops sharply and the logger records every sample without loss over a 72-hour test.