notes

Interrupt affinity: spreading device lines off core 0

Name: provisional, minted 2026-10-11 (UTC) by lane/third-ten-longest-notes.

An appendix to notes/interrupts.md, moved there on 2026-10-11 (UTC) under §212 (a prose budget). It is the record of fix/interrupt-delivery (2026-07-29), the PLIC parity work and the std_net hang that turned out not to be an interrupt bug.

Until now every SPI targeted core 0: gic::enable wrote ITARGETSR[intid] = 1 (bit 0). Under SMP that funnels every device interrupt onto one core. The handler that turns an interrupt into a message runs on the core that took it (handle_interrupt calls interrupt_notify, which wakes the driver onto cpu::current), so the driver wake lands on core 0 too. Every disk and NIC completion, and the driver work it triggers, re-concentrates on core 0 no matter where the threads were spawned.

The fix distributes SPI lines across the online cores. The policy lives in arch::interrupt_controller::enable (it may read smp::online_count; it is arch glue, not a driver). Each SPI is assigned a target core the first time it is enabled, round-robin over the online cores. That assignment is stable (INTERRUPT_TARGET, an atomic per-INTID slot). Stability matters because the Interrupt capability's ACKNOWLEDGE re-enables the line on every completion; re-rolling the target each time would make the line hop cores on every interrupt. The mechanism stays in the driver: gic::enable(intid, target_cpu) writes ITARGETSR[intid] = 1 << target_cpu. PPIs and SGIs are per-core, so the target is ignored for them (the timer PPI, the reschedule SGI). Rule #2 holds: the GIC driver is told which core, it does not decide.

What this does and does not buy, measured honestly:

The riscv PLIC side, done the same way (parity §19)

The PLIC equivalent spreads device sources across harts, the same round-robin-with-a-stable-target shape as the GIC. The PLIC delivers a source to a context (a hart at a privilege level). The hart-to-context numbering is the board's, read out of the device tree's interrupts-extended at boot (arch::interrupt_controller::initialize_contexts, backed by isa::plic). It is 2*hart+1 for S-mode on QEMU virt, and 2*hart on the JH7110, whose disabled S7 monitor core contributes only an M context (see notes/visionfive2.md). The pieces are:

The lost update, step by step

The abstract phrase "read-modify-write" hides the bug, so here it is concretely. Say the disk is source 8 and the NIC is source 10. Both bits live in the same 32-bit enable word (sources 0..31 of that context). A driver touching its own source still writes the neighbor's bit back, because the only way to change one bit of that word is to store all 32.

Hart 0 is in thread context enabling the disk. Hart 1 is inside its handler masking the NIC. Without the lock they interleave:

step hart 0 (enable(8)) hart 1 (disable(10)) word in the PLIC
1 read → 0b0100 (bit 10) 0b0100_0000_0000
2 read → 0b0100 (bit 10) 0b0100_0000_0000
3 or in bit 8 0b0100_0000_0000
4 write bits 8,10 0b0101_0000_0000
5 clear bit 10 from ITS copy 0b0101_0000_0000
6 write 0b0000 0b0000_0000_0000

Hart 1's copy of the word was read at step 2, before hart 0's store at step 4. Its store at step 6 is computed from that stale copy, so it does not just clear bit 10, it un-sets bit 8 as collateral. Hart 0's enable is gone, and nothing anywhere reports an error: the PLIC has no notion of a disagreement, it just holds whichever word was written last.

Both directions of that race are bad, and they fail differently, which is why neither is tolerable:

This is also why the window is small but not negligible. It is the few instructions between the read and the write, hit only when two harts touch the same 32-source word at once. That is exactly the kind of race that passes a thousand test runs and then fails in front of an audience. The audit found it by reading for the pattern rather than by waiting for it to bite (arch-audit.md, finding 3).

The fix is one InterruptSafeMutualExclusionLock around the read and the write together, so steps 1-4 and 2-6 cannot interleave: whichever hart takes the lock second re-reads the word after the first hart's store and computes from current state. It is the smallest possible critical section (a read, an or/and-not, a write) and it is off the hot path, so it costs nothing measurable.

Proven the same way as the GIC: the riscv suite is green at the 4-hart boot with device sources spread across harts. That covers the disk read and the interrupt-driven redoxfs_server block server, both routed to whatever hart the round-robin picked, plus the SMP placement tests. As on aarch64, this distributes the interrupt and the wake it causes; it does not by itself parallelize the std_net pipeline, which needs the load-aware rendezvous wake (see below).

The std_net SMP hang was not an interrupt-delivery bug (resolved)

Recorded so it is not re-diagnosed as one, and left in place with its resolution because the wrong diagnosis was tempting and the right one took two agents to reach.

The std_net test (smoltcp in net_stack serving a std program's UdpSocket/TcpStream) used to hang under the 4-core boot on both ISAs, watchdog-killed at 60 s. In the same run the hand-built DHCP round trips (virtio_net, virtio_net_pci) passed and the interrupt-driven redoxfs_server block server passed. That asymmetry was the tell: interrupt delivery under SMP was sound, and the hang was specific to the heavier, longer, timer-driven smoltcp pipeline.

Two things were wrong, and only one of them was scheduling:

  1. A real deadlock. net_stack blocked on the NIC interrupt while smoltcp still had a retransmit pending, so neither side would move. The timer that would have retransmitted was never polled because the thread was parked in Interrupt::WAIT, and no packet was coming to wake it. That is a mutual-idle deadlock, not slowness, and no amount of core placement fixes it. net_stack now bounds its wait by smoltcp's own next-poll deadline.
  2. Serialization on one core. With the deadlock gone the pipeline ran, but slowly: its threads are woken by a mix of device interrupt and IPC rendezvous, and both wakes pinned to one core. DECISIONS §28 (SMP placement)'s wake split fixed the half that mattered (device-interrupt wakes go load-aware, IPC rendezvous wakes stay local, because a rendezvous partner is about to run on the caller's core and moving it only adds a migration).

An intermediate hypothesis of mine was wrong and worth keeping written down: I expected interrupt affinity alone to fix it. It cannot. Spreading interrupts across harts relocates where a wake lands; it does not parallelize a chain of request/response rendezvous, which is serial by construction. The agent disproved it by measurement rather than argument.

std_net now passes on both ISAs. It is also the longest honest test in the suite at roughly 300 to 344 s on aarch64, because it is a real DHCP lease plus TCP and UDP round trips through an emulated NIC under TCG. That length is legitimate work, not a symptom, which is precisely what makes it awkward for the watchdogs: see the per-test ceiling discussion in scheduler.md.