Skip to content

jitv2: park a lone cpu for a worker's flush instead of halting it - #826

Merged
techomancer merged 3 commits into
techomancer:mainfrom
steigr:feature/fix-306-jitv2-flush-deadlock-20fb61
Nov 22, 2026
Merged

techomancer merged 3 commits into
techomancer:mainfrom
steigr:feature/fix-306-jitv2-flush-deadlock-20fb61

Conversation

@steigr

@steigr steigr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

What #306 reported

Under jitv2, two flushes can deadlock. A compile worker that fills its code arena leads a flush and stops the CPU with cpu.stop(), which joins the CPU thread. At the same moment the CPU side may flush too: the CPU running out of JIT pages, or j2 flush / j2 inline on from the monitor. That flush holds the Jitv2 lock and joins the worker. Each waits for the other.

The start-up half of the issue (a worker taking the lock to read the cache geometry) was already fixed in 9503188. What remained is a pool with a single CPU: the Indy, Indigo2, O2, a one-CPU Octane or Tezro, or an Origin node whose processor module has a pool to itself. Such a pool had no World, so its worker still halted the CPU.

The fix

  • Every pool now flushes through the world of its CPUs. attach_pool takes the machine's world, and every machine passes it, so the CPU's stop_others and the worker's try_lead take turns on the same world. This covers the Indy, Indigo2 and O2 (whose worlds carried only remap requests until now), a one-CPU Octane or Tezro, and every group share_pool builds.
  • A worker therefore parks the CPU instead of halting it, and gives way if the pool stops while it waits.
  • A CPU belongs to exactly one world. MipsCpu::new gives each CPU a World::solo until a machine replaces it (share_jitv2); that covers CPUs built outside a machine, as in tests.
  • j2 inline on stops the pool with Jitv2::stop_pool, which joins the workers with the lock released.
  • A worker's flush no longer restarts a CPU the monitor has stopped.

How I reproduced it

tools/jitv2-flush-race.py (new, described in TESTING.md) boots a guest under --ci with IRIS_JIT_ARENA_FLUSH_MB=1, so the workers lead flushes of their own throughout the boot. Meanwhile it loops stop / j2 flush / start at the monitor. A j2 flush that does not return within 60 s counts as the deadlock; the script then saves every thread's stack and kills iris. That is safe because the disk overlay of a --ci run lives in $TMPDIR.

The guest was an Indigo2 IP28 (R10000, Solid IMPACT) running IRIX 6.5.22, installed from original media, on a --ci copy-on-write overlay. The builds were release --features jitv2.

Build Run Result
upstream bd81b637 seed 306 hung: j2 flush never returned after 22 rounds (73 s, 17 worker-led flushes); guest at "The system is coming up."
upstream bd81b637 seed 306, again hung: after 1 round, during the PROM's power-on diagnostics
this branch seed 306, 600 s pass: 1115 rounds, 1234 worker-led flushes, guest reached IRIS console login:
this branch seed 7, faster loop, 600 s pass: 3333 rounds, 573 worker-led flushes, guest reached IRIS console login:

Stacks of the hung upstream iris (macOS sample), condensed:

monitor thread   CpuMonitor::execute_command → MipsExecutor::jitv2_flush_from_here
                 → Jitv2::flush_from_cpu_thread → CompileQueue::stop → pthread_join
jitv2-compile-0  CompileQueue::worker_loop → MipsCpu::halt → RawMutex::lock_slow
                 (the executor lock, which the monitor holds)

The flush runs were made on bd81b637. After rebasing onto db7e3836 (a gfifo loom model, outside these files), I reran only the targeted unit tests.

Tests

  • New unit test a_cpu_flush_inside_a_worker_led_flush_takes_its_turn: it forces a worker flush (a test-only PoolBarrier hook) against a fake CPU that flushes the moment it is held. Without the world it times out after 20 s; with it, it passes.
  • New a_solo_world_holds_its_cpu_for_others_but_not_for_itself.
  • cargo test --workspace --features jitv2 -- --skip dev::kona::back::tiles passed in full on the previous base (lib 5510 passed, 0 failed); the Kona tile tests were skipped because their hang fix is on another branch. cargo check with default features is clean.

Not covered

  • I did not boot the Indy, O2, Octane, Tezro or Origin machines. Their wiring changed (they now pass their machine world to the pool), but only the Indigo2 was run end to end, and the multi-CPU paths were not exercised live.
  • The CPU running out of JIT pages on its own, rather than through j2 flush, uses the same flush_from_cpu_thread path but was not provoked separately. The j2 inline on path was not run live.
  • The loom models were not rerun.
  • No speed measurements; speed is unmeasured.

Fixes #306.

🤖 Generated with Claude Code

steigr and others added 3 commits November 22, 2026 02:39
A compile worker that filled its arena led a flush by calling
cpu.stop() on a CPU with a pool of its own, which joins the CPU thread.
If that CPU ran out of JIT pages in the meantime, it was in
flush_from_cpu_thread, holding Mutex<Jitv2> and joining the worker:
each waited for the other. j2 flush and j2 inline on from the monitor
hung the same way, on the executor or the Jitv2 lock. Machines with
several CPUs on one pool already took turns through World; a lone CPU's
pool had none.

Every pool now flushes through the world of its CPUs. attach_pool takes
the machine's world, and every machine passes it: the Indy, Indigo2 and
O2 (whose worlds carried only remap requests), the Octane and Tezro with
one CPU, and every pool share_pool builds, also a lone processor's or a
node whose module has a pool of its own. Each CPU on the pool shares it
with share_jitv2, which now sets the world on the pool as well, so the
CPU's stop_others and the worker's try_lead take turns on one world.
A CPU belongs to one world, since two would both hold it with
HOLD_WORLD. MipsCpu::new gives every CPU a World::solo until a machine
replaces it, for CPUs built without one.

The worker then parks the CPU and gives way if the pool stops, and the
CPU's own flush, inside stop_others, counts as parked while it waits its
turn. A worker's flush also no longer restarts a CPU the monitor has
stopped. j2 inline on stops the pool through Jitv2::stop_pool, which
joins with the lock released.

The start-up half of techomancer#306 (workers taking the lock to read the cache
geometry) was fixed in 9503188.

New test a_cpu_flush_inside_a_worker_led_flush_takes_its_turn forces a
worker flush (a test-only PoolBarrier hook) against a CPU that flushes
the moment it is held. Without the world it times out after 20 s; with
it, it passes in under 0.1 s.

Fixes techomancer#306.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tools/jitv2-flush-race.py boots a single-CPU guest under --ci with
IRIS_JIT_ARENA_FLUSH_MB=1, so the compile workers lead flushes of their
own throughout the boot, and loops stop / j2 flush / start at the
monitor. A j2 flush that does not return within --cmd-timeout is the
deadlock of techomancer#306; the script then saves every thread's stack (--stacks)
and kills iris, whose disk overlay --ci keeps in $TMPDIR.

On the Indigo2 R10000 with IRIX 6.5.22, upstream bd81b63 hung both
times (after 22 rounds in 73 s, and after 1 round during the PROM's
diagnostics): the monitor thread joining jitv2-compile-0 inside
CompileQueue::stop, the worker in MipsCpu::halt waiting for the
executor lock the monitor holds. With the fix it ran 1115 rounds in
600 s with 1234 worker-led flushes, and the guest reached its login
prompt.

TESTING.md describes it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@techomancer
techomancer merged commit 0afce53 into techomancer:main Nov 22, 2026
11 checks passed
@techomancer

Copy link
Copy Markdown
Owner

hopefully we will soon have double buffered arena ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

jitv2: two flushes during a worker's start-up can deadlock

2 participants