Chris Low-level systems developer
← Back to all posts

The Half of the Interrupt That Waits Its Turn

ISA

The mechanism is Linux kernel design, not instruction-set law: the softirq type list, the 2ms and 10-restart governors, and the tasklet SCHED/RUN bit contract are quoted from the v6.5 kernel source, not from an architecture manual.

MICROARCH

The article's mechanism is kernel scheduling policy: how Linux decides where deferred work runs. It measures and cites nothing about processor implementation.

FIRMWARE

The whole mechanism is kernel code: softirqs run on hardirq exit, tasklets run on the TASKLET softirq, and ksoftirqd is a kernel thread. Nothing below the kernel boundary takes part.

KERNEL

The article reads the deferred-work machinery straight from the kernel: the ten softirq types in kernel/softirq.c priority order, the 2ms and 10-restart governors, the tasklet SCHED/RUN bit contract in include/linux/interrupt.h, and the ksoftirqd handoff. This is the kernel's own interrupt plumbing.

ALGORITHM

No algorithm is presented: the post describes a scheduling policy and a bit-level contract, not a procedure with an invariant to prove.

A packet arrives on the wire. The NIC raises its interrupt line, and the hardirq handler starts running. It runs with interrupts off and no process to its name. It cannot sleep, it cannot wait on anything, and every other interrupt in the machine is lining up behind it. So it does the minimum: acknowledge the device, capture the data, mark the work as pending [3]. Then it leaves. The rest of the work does not run in the handler. It runs in the bottom half.

Linux keeps ten softirq types in a fixed priority order: HI, TIMER, NET_TX, NET_RX, BLOCK, IRQ_POLL, TASKLET, SCHED, HRTIMER, and RCU last, with a comment in the source that RCU should always stay last [1]. When the hardirq handler exits and no interrupt context is nested, the kernel walks the pending bitmask in priority order, lowest bit first. The handler loop carries two governors: two milliseconds (MAX_SOFTIRQ_TIME) and ten restarts (MAX_SOFTIRQ_RESTART), and the comment says these limits were found by experimentation, balancing latency against fairness [1]. When either budget is spent, the kernel wakes ksoftirqd, a per-CPU thread, which finishes the leftover work as ordinary scheduled work. A softirq therefore has two personalities: under light load it runs eagerly, almost as fast as the hardirq that raised it; under heavy load it demotes itself to a thread instead of starving the machine.

Operators at a Bell System telephone switchboard in December 1943, patching overseas calls by hand
December 1943: the operator answers the instant the lamp lights and patches the circuit; the conversation happens on the wire she connected. The hardirq handler answers the interrupt; the bottom half carries the rest.
Photo: U.S. National Archives, no known copyright restrictions, via Wikimedia Commons

Tasklets are the friendlier face of the same machinery, built on the TASKLET and HI softirqs [2]. Their whole contract is test-and-set. tasklet_schedule sets the SCHED bit, so scheduling a tasklet twice collapses into a single run. tasklet_trylock sets the RUN bit, so the same tasklet can never execute on two CPUs at once; if it is already running, the action function puts it back on the list and re-raises the softirq, so the work is honored later instead of being lost [2]. The per-CPU tasklet list is stolen under disabled interrupts, then each tasklet runs with interrupts enabled: a tasklet can be preempted by a hardirq, but never by a second copy of itself. One flag for scheduled, one for running, and re-queue as the honest answer to contention.

The whole contract, in three lines:

static inline void tasklet_schedule(struct tasklet_struct *t)
{
	if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
		__tasklet_schedule(t);
}

There used to be a simpler version of all this. The old bottom halves numbered 32, ran globally synchronized, and only one was allowed to run anywhere in the system at a time [3]. That was the bottleneck, and during 2.5 the BH interface was thrown away; every user moved to softirqs, tasklets, or the workqueue. The sibling that survives alongside them is the workqueue, which exists for the one property softirqs and tasklets still lack: the ability to sleep. So the three mechanisms are not three ways to do the same thing. They are a ladder: the hardirq does the minimum, the softirq and the tasklet take the urgent remainder that still cannot sleep, and the workqueue takes the part that needs to sleep. Each rung exists because the rung above it is missing exactly one property.

Sources

  1. 01
    torvalds/linux v6.5 kernel/softirq.cLinux kernel

    Ten softirq types in priority order with RCU last (L550-561); the 2ms and 10-restart governors found by experimentation, balancing latency against fairness (L461-475); the ksoftirqd handoff when the budgets are spent (L566-578).

  2. 02
    torvalds/linux v6.5 include/linux/interrupt.hLinux kernel

    The tasklet contract: tasklet_schedule sets TASKLET_STATE_SCHED so a double schedule collapses into one run (L705-709); tasklet_trylock sets TASKLET_STATE_RUN so one tasklet never runs on two CPUs at once (L687-690).

  3. 03
    Linux Kernel Development, 3rd edition, Chapter 8Robert Love, Addison-Wesley

    The old bottom halves: 32 of them, globally synchronized, only one running anywhere in the system at a time; the BH interface removed during 2.5, with every user converted to softirqs, tasklets, or workqueues.

Stuck on a board or a crash? Fixed-price firmware work: crash triage $250/symptom, bare-metal bring-up from $500, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app