Skip to content

A task ring pop can mix up two tasks when its slot gets reused #2078

Closed

Description

@Mantissagithub

What you did

I pulled ring_push and monk_step out of the C that Bend emits (bun bend2/main.ts bench/runtime/histogram/main.bend -o hist.c, at 266b02e) and ran them through two memory model checkers: GenMC 0.20 and herd7 7.58 with Arm's official model. The code is the same on main today (be236f3).

What happened

Each task in a ring is stored in two halves. To pop one, monk_step does three things:

  1. reads the top half (acquire)
  2. reads the bottom half
  3. bumps get to say "this slot is free now" (relaxed store, comp.ts:4459)

Nothing forces step 3 to wait for step 2. On ARM the CPU is allowed to make the get bump visible before the bottom-half read has finished. If a pusher sees that bump, it can write a new task into the same slot, and the late read picks up the new task's bottom half. The pop then runs the top of one task glued to the bottom of another.

Shortest way it happens, on a 2 slot ring:

  1. pusher writes task 1 into slot 0, then task 2 into slot 1
  2. popper reads task 1's top half from slot 0
  3. popper's "slot 0 is free" bump shows up early
  4. pusher sees it, writes task 3's bottom half into slot 0
  5. popper's bottom-half read lands: it gets top 1 + bottom 3

This is the ARM code clang 18 emits at -std=c11 -O3: ldar (top), ldr (bottom), str (get), and nothing ties the ldr to the str. Arm's model says the mixed result can happen:

AArch64 TornPop
{ lo=1; hi=1; get=0;
  0:X4=hi; 0:X5=lo; 0:X6=get; 1:X4=hi; 1:X5=lo; 1:X6=get; }
 P0           | P1           ;
 LDAR W0,[X4] | LDR W0,[X6]  ;
 LDR W1,[X5]  | CMP W0,#1    ;
 MOV W2,#1    | B.NE LC00    ;
 STR W2,[X6]  | MOV W1,#3    ;
              | STR W1,[X5]  ;
              | MOV W2,#3    ;
              | STLR W2,[X4] ;
              | LC00:        ;
exists (0:X0=1 /\ 0:X1=3 /\ 1:X0=1)
Observation TornPop Sometimes 1 2

Make that STR an STLR and it says Never.

There's a second way in that only matters under plain C11 rules: with two pushers, since get is passed around relaxed on both ends, an older push's write to the slot can land after a newer one's. That's why the push side needs an acquire too.

GenMC on the emitted code, with 1 to 3 pushers, rings of 2 and 4 slots, and both the host build and the device build's fences:

  • as is: mixed-up pop under ARMv8 every time, and under C11 rules whenever there are 2+ pushers
  • with the fix below: clean everywhere, up to 501k orderings checked
  • either half of the fix alone still fails somewhere

How likely is it: low. I haven't seen it on real hardware (I'm on x86, where it can't happen). The ring has to be completely full right then, because the slot being refilled has to be the one being popped. With 1024 slots that's 1023 tasks queued. It can happen though: the work pass does push into rings whose owners are popping them (task_deal through ring_flip). Same kind of thing as #975, except here Arm's model says the bad result is actually allowed on the code clang emits.

Which platforms:

  • x86 (Intel/AMD): safe. GenMC's x86 model finds nothing on the code as is (up to 229k orderings), since x86 never lets a store jump ahead of an earlier read.
  • ARM CPUs (Apple Silicon, Graviton, Linux arm64): affected, per GenMC's ARMv8 model and Arm's own model above.
  • NVIDIA GPUs: probably affected. The device build has no FENCE() between the bottom-half read and the get bump, and GPUs reorder more freely than ARM. My GenMC run of the device path (its fences written as C11 fences) fails too, but that's an approximation of PTX, not the real thing.
  • Apple GPUs (Metal): probably, same reasoning. There's no public formal model to check against.
  • Any platform, through the compiler: C11 lets clang reorder those two accesses itself. It doesn't at -O3 today, but nothing stops it.

The two-pusher case only exists under C11's rules: on ARM hardware the pusher's room check is a branch, and ARM keeps a write after the read that its branch depends on.

The fix

@@ ring_push
-  if (pos - a32_load(ring_get(H, r)) >= RING_LEN) {
+  if (pos - a32_load_acq(ring_get(H, r)) >= RING_LEN) {
@@ monk_step
-  a32_store(get, *get + 1);
+  a32_store_rel(get, *get + 1);

On x86 this compiles to exactly the same instructions as today. On ARM the store becomes stlr and the load ldar. On the GPU both go through FENCE().

bend version

bend 2.0.40

uname -sm

Linux x86_64

clang --version (the first line)

Ubuntu clang version 18.1.3 (1ubuntu1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions