Skip to content
systemcoding.dev
Kernels

Top Halves and Bottom Halves: Why Interrupt Work Is Split

Linux splits interrupt work into a fast top half and a deferred bottom half. See what belongs in each, the four mechanisms, and how they trade latency and cost.

6 min read
Summarize in AI
Flat illustration of a program timeline interrupted by an interrupt service routine, with the Linux, RISC-V and C logos.

A gigabit network card can deliver over 80,000 full-size packets per second. If each interrupt handler spent 20 microseconds parsing its packet while the line stayed masked, a CPU would spend 1.6 seconds of every second in handlers, and the machine would never run anything else. The Linux answer is to split interrupt processing in two: a top half that runs immediately and does the bare minimum, and a bottom half that does the rest later, when interrupts are enabled again and the CPU can be shared with everything else. This post explains the split, what goes where, and the four mechanisms Linux 6.12 provides for the bottom half.

Two halves, two jobs

The top half is the interrupt handler registered with request_irq(). It runs in hard interrupt context, usually with the interrupt line masked and, on a normal kernel, with local interrupts disabled. Its job is to answer the hardware: find out what happened, acknowledge it so the device stops signaling, and take the data that will be lost if it is not read now, such as a received byte in a one-byte FIFO.

The bottom half is everything else: copying data, parsing protocol headers, updating file-system state, waking user processes, calling into other subsystems. It runs after the top half returns, with interrupts enabled, so other devices can interrupt it. How long it may take depends on the mechanism, but it is allowed to be much longer than the top half.

A useful test for any line of interrupt code: if skipping it for a few hundred microseconds would lose data or leave the hardware in an unhappy state, it belongs in the top half. If it would only delay processing, it belongs in the bottom half.

What a split looks like

A UART receives bytes into a 16-byte hardware FIFO. The top half must empty the FIFO before it overflows. The bottom half pushes the bytes up through the line discipline and wakes the reader:

uartsplit.cc
static irqreturn_t uart_isr(int irq, void *dev_id)
{
	struct uart *u = dev_id;
	u32 st = readl(u->base + UART_STATUS);
	unsigned long flags;

	if (!(st & UART_RX_READY))
		return IRQ_NONE;

	spin_lock_irqsave(&u->lock, flags);
	while (readl(u->base + UART_STATUS) & UART_RX_READY)
		kfifo_put(&u->rx_fifo, readl(u->base + UART_DATA) & 0xff);
	spin_unlock_irqrestore(&u->lock, flags);

	writel(UART_RX_READY, u->base + UART_ACK);
	tasklet_schedule(&u->rx_tasklet);	/* or queue_work(), see below */
	return IRQ_HANDLED;
}

static void uart_rx_bottom(struct tasklet_struct *t)
{
	struct uart *u = container_of(t, struct uart, rx_tasklet);
	u8 byte;

	while (kfifo_get(&u->rx_fifo, &byte))
		tty_insert_flip_char(&u->port, byte, TTY_NORMAL);
	tty_flip_buffer_push(&u->port);
}

The top half drains the hardware into a software FIFO (kfifo), which is a lock-protected ring buffer in kernel memory, then schedules the bottom half. The bottom half hands the data to the tty layer at its own pace. If a burst of bytes arrives while the bottom half is still running, the top half adds more bytes to the FIFO and the bottom half picks them up on its next pass.

The four mechanisms

Linux has four ways to run a bottom half. They differ in context, concurrency and cost.

MechanismContextCan sleepConcurrencyDefined by
SoftirqSoftirq, after the interruptNoSame softirq may run on many CPUs at onceFixed list in the kernel
TaskletSoftirqNoOne instance of a given tasklet at a timeDriver, at run time
WorkqueueKernel thread (process context)YesControlled by queue settingsDriver, at run time
Threaded IRQDedicated kernel thread per IRQYesOne thread per registrationDriver, at registration

Softirqs are the oldest and lowest-level mechanism. There are ten of them, listed in enum order in include/linux/interrupt.h: HI_SOFTIRQ, TIMER_SOFTIRQ, NET_TX_SOFTIRQ, NET_RX_SOFTIRQ, BLOCK_SOFTIRQ, IRQ_POLL_SOFTIRQ, TASKLET_SOFTIRQ, SCHED_SOFTIRQ, HRTIMER_SOFTIRQ and RCU_SOFTIRQ. They are defined at compile time and reserved for core subsystems that need the highest throughput: the network stack, the block layer, timers. A driver does not add one.

Tasklets are built on two of those softirqs (HI_SOFTIRQ and TASKLET_SOFTIRQ) and give drivers a dynamic way to use them. The kernel guarantees that a given tasklet never runs on two CPUs at once, which simplifies locking. They cannot sleep. Since Linux 6.9 they are deprecated in favor of BH workqueues, as another post here explains.

Workqueues hand work to a pool of kernel threads. Because the function runs in a thread, it can take mutexes, allocate with GFP_KERNEL, talk to I2C devices and sleep. The price is scheduling latency: the work waits for a thread to be scheduled, which depends on system load.

Threaded IRQs are a workqueue built into the interrupt layer: request_threaded_irq() creates a thread named irq/<n>-<name> that runs your second function when the first returns IRQ_WAKE_THREAD.

Choosing one

Ask in this order:

  1. Does the bottom half need to sleep? Yes: workqueue or threaded IRQ. These are the right answer for most drivers, especially for anything reading a bus such as I2C or SPI.

  2. Is it a hot path that is already designed around softirqs? A networking or block driver might use NAPI or the subsystem's own completion mechanism, not its own tasklet.

  3. Is it short, non-sleeping and latency-sensitive? A BH workqueue (Linux 6.9 and later) or, in older code, a tasklet.

  4. Otherwise: a workqueue.

New drivers rarely need more than a threaded IRQ or a workqueue.

How softirqs actually get run

Softirq execution is worth understanding because it explains behavior you can observe. When a top half calls tasklet_schedule() or raises a softirq, it sets a bit in a per-CPU pending mask. When the interrupt handler returns, irq_exit() checks that mask, and if any bit is set and we are not already in a softirq, calls invoke_softirq(), which runs __do_softirq() right there, on the same CPU, with interrupts enabled.

__do_softirq() loops over pending bits, running each handler, and re-checks the mask afterward. To avoid starving user tasks, the loop is limited: it stops after 10 restarts or 2 milliseconds, whichever comes first (MAX_SOFTIRQ_RESTART and MAX_SOFTIRQ_TIME in kernel/softirq.c). Remaining work is handed to the per-CPU thread ksoftirqd/N, which the scheduler runs like any other task. That is why top shows ksoftirqd consuming CPU under heavy network load.

You can see per-CPU softirq counts:

Shell
cat /proc/softirqs
Text
                    CPU0       CPU1       CPU2       CPU3
          HI:          1          0          0          2
       TIMER:    1204511    1198322    1201875    1199044
      NET_TX:        812          5         19         44
      NET_RX:     302144      14022      15830      16011
       BLOCK:      40122      39800      41055      40533
    IRQ_POLL:          0          0          0          0
     TASKLET:       2015         12         33         27
       SCHED:    1002455     998201    1001044     999812
     HRTIMER:          3          1          0          2
         RCU:     880222     879114     881332     880005

The sample shows NET_RX concentrated on CPU0, consistent with a single network interrupt pinned there. Your numbers differ.

What the split costs

Splitting adds latency: the bottom half runs later than the top half, and a threaded or workqueue bottom half has to wait to be scheduled. It also adds synchronization: data shared between the halves needs a lock safe against the other's context, and the split is where most subtle races in drivers live. A top half that fills a buffer and a bottom half that drains it needs, at minimum, one spinlock or a lock-free queue such as kfifo used with a single producer and consumer.

It is also a source of lost events. If the top half calls schedule_work() twice before the work runs, only one execution happens, since an already queued work item is not queued again. The bottom half must process all accumulated state, not just one item. A queue or counter between the halves is the usual cure.

Reading the contexts from the kernel's perspective

The kernel records which half you are in. in_hardirq() is true in the top half, in_serving_softirq() in a softirq or tasklet bottom half, and in_task() in a workqueue thread or threaded IRQ. A function that must adapt, such as one allocating memory, can use these, but a better design passes the information in. The rules about what may be called differ: hard IRQ and softirq may not sleep, thread contexts may.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.