← Back to all chapters

Chapter 30Down the Rabbit Hole: The Store Buffer That Lets Both Threads Into the Room

8 min read
rabbit-holeconcurrencycomputer-architecture

A program I'd have bet money can't misbehave. Two threads, x and y both 0:

thread 0:  x = 1;  r1 = y;
thread 1:  y = 1;  r2 = x;

Can both r1 and r2 be 0? My gut says no: whichever write is first, the other thread's read comes after it. I ran it two million times in an x86 binary on my laptop. Both reads were 0 in 1,989,118 runs. That's 99.5%. Not a glitch; the normal result.

The outbox on every desk#

Ana and Ben share a whiteboard, each with an outbox tray. To write on the board, Ana drops a note in her outbox and carries on; a runner copies notes onto the board, oldest first. When Ana looks at the board she sees only what's already up. Her tray is invisible to Ben, and his to her.

Now a meeting-room rule: put up your "I want the room" sign, check whether the other's is up, and if not, take the room. Ana's sign sits in her outbox; she sees no Ben sign. Ben's sits in his; no Ana sign. Both walk in. The rule was sound; the outboxes broke it. It's my two-thread program, and the entry protocol of Dekker's and Peterson's algorithms.

core 0core 1store bufferstore bufferx = 1y = 1memoryx = 0 y = 0load y: 0load x: 0loads skip the buffersdrain (dashed) comes later
Each core's store is parked in its own buffer, so each load goes straight to memory and finds the old value. Schematic.

Why the outbox exists#

Every Intel and AMD core has one: a per-core FIFO store buffer. Stores are slow. A core must own a line exclusively before writing it, so it sends a "read for ownership" and waits: roughly 15 to 20 cycles from L2, 40 to 80 from L3, a few hundred from DRAM or another socket. Also, cores execute speculatively, so a store can't touch the cache until it retires; something must hold it. Letting retired stores sit there while later loads go ahead is nearly free performance.

Skylake has 56 entries, Golden Cove 114. A Little's-law check (my back-of-envelope, not a vendor number): one store per cycle and about 50 cycles to drain an L3 miss wants about 50 entries.

The one reordering x86 allows#

x86-TSO ("total store order") is sequential consistency with one hole. Intel's manual:

Reads may be reordered with older writes to different locations but not with older writes to the same location.

Load-load, store-store (the buffer is FIFO) and load-store stay ordered. Only a later load can pass an earlier store to a different address. Same address is fine thanks to store-to-load forwarding: a load checks its own core's buffer first, newest match wins.

1/4Start
core 0 buffer: (empty)     core 1 buffer: (empty)
memory:  x=0  y=0
r1 = ?   r2 = ?

Everything zero.

Both reads see 0. Step through.

The rule, written down#

In 2009 Owens, Sarkar and Sewell pinned it down as an abstract machine: threads with FIFO write buffers above one shared memory and a global lock, proved equivalent to an axiomatic TSO model. In the axiomatic version the hole is a missing line. With program order <p and the single global memory order <m, SC keeps all four pairs; TSO keeps three:

L1 <p L2  =>  L1 <m L2     (load then load: kept)
L  <p S   =>  L  <m S      (load then store: kept)
S1 <p S2  =>  S1 <m S2     (store then store: kept)
                           (store then load: missing)
then a loadthen a storeload firststore firstload, load: keptload, store: keptstore, store: keptstore, load:not kept
The four ordering edges. SC keeps all four; x86-TSO drops the store-then-load one.

Plus a value rule, where the buffer lives: a load returns the latest store to its address that is already earlier in memory order or earlier in its own thread. That second clause is forwarding.

Check by hand. Events: a (write x), b (read y), c (write y), d (read x). Both reads seeing 0 needs b before c and d before a. Under SC, program order also forces a before b and c before d: a, b, c, d, a is a cycle, so no order exists. Under TSO those store-then-load edges aren't required, and b, d, a, c is legal. SC allows three of the four outcomes; TSO allows all four.

Putting the missing edge back#

MFENCE, or any LOCK-prefixed instruction, means "empty your outbox before you look". Put one between store and load and the cycle returns. I compiled C11 atomics with Clang 21:

atomic_store(&x, v);                                 // x86: xchg  %edi, x(%rip)
atomic_store_explicit(&x, v, memory_order_release);  // x86: mov   %edi, x(%rip)
atomic_load(&x);                                     // x86: mov   x(%rip), %eax
atomic_thread_fence(memory_order_seq_cst);           // x86: lock orl $0,-64(%rsp)

Release stores, acquire loads and seq_cst loads are plain mov: free, since TSO gives every ordering they need. Only a seq_cst store or fence costs, because only it asks for the store-then-load edge.

The compiler puts the cost on the rarer store (xchg, implicitly locked), not the load. And the fence isn't MFENCE: it's a locked OR of zero into a dead, private stack slot. On uops.info's isolated measurements MFENCE has throughput 33 cycles on Skylake versus 18 for XCHG, and also orders non-temporal and write-combining stores that ordinary code doesn't need. HotSpot's StoreLoad barrier for volatile stores is likewise a locked add on the stack.

  • MFENCE33
  • XCHG18
Reciprocal throughput in cycles on Skylake, uops.info isolated measurements.

Running it myself#

A small C harness: two threads, x and y on separate 128-byte lines, two million runs, count both-0. Each thread:

atomic_store_explicit(&x, 1, memory_order_relaxed);
if (mode == 1) atomic_thread_fence(memory_order_seq_cst);
r1 = atomic_load_explicit(&y, memory_order_relaxed);

Apple Silicon has a hardware TSO mode that Rosetta 2 enables for translated x86, so an x86 binary on my M4 Pro behaves like x86-TSO. I built both ways:

BinarySB, no fenceSB + seq_cst fenceMessage passing
native arm641,015,973 / 2,000,000 (50.8%)00
x86_64 under Rosetta (hardware TSO)1,989,118 / 2,000,000 (99.5%)00
  • x86 under Rosetta99.5%
  • arm64, one cache line99.96%
  • arm64, separate lines50.8%
  • any, with fence0%
Share of two million runs where both reads returned 0 (the table above, plus the one-cache-line note).
  • The rate is mostly the harness, not the chip. With x and y on one cache line, native gave 99.96%. Don't read 50.8% vs 99.5% as "ARM is half as weird".
  • One fence drove both to zero.
  • Message passing stayed at zero even on native ARM, though ARM's model allows it. My harness on my core just never showed it.
  • Caveat: this is Rosetta's TSO mode on an Apple core, not a real Intel or AMD part. It matches the model, but it isn't a measurement of an x86 store buffer.

Where people get bitten#

Anything shaped "write my flag, then read yours":

  • Dekker and Peterson with plain variables. Both enter, as in the meeting-room story.
  • "x86 is strong, so acquire/release is enough." I'd have gotten this wrong. A release store then an acquire load of a different location can still reorder on x86.
  • Wait/wake paths. Waiter sets state, checks condition; waker sets condition, checks waiter. Same test. Without a full barrier the waiter can sleep forever. Linux's smp_store_mb() is assign plus full barrier.
  • volatile in C/C++. Stops compiler reordering, still emits plain mov.
  • SFENCE or LFENCE. For ordinary memory they do nothing here. Only MFENCE or a locked instruction orders a store against a later load.
The microarchitectural version: Speculative Store Bypass

Real cores run loads early even before an older store's address is known, guessing no alias. Speculative Store Bypass (Spectre variant 4, CVE-2018-3639) abuses that guess: a load transiently reads a stale value it should have got from the store, and it leaks through cache timing. The architectural model is never violated; the result is squashed and redone. It just isn't the whole security story.

Why this particular deal#

Of the four orderings, store-then-load buys the most and breaks the least. It buys a lot because stores are the slow side and nothing in the same thread needs a store globally visible, only its value, which forwarding supplies. It breaks little because most synchronisation is "publish then set flag" (store-store) and "check flag then read" (load-load), both kept.

Take it apart to see: drain out of order and you get SPARC's PSO, where message passing breaks. Drop forwarding and a thread reads stale values of its own writes. Drop load-load order, as ARM and POWER do, and readers need acquire too. Remove the buffer and you're back at SC, stalling on every store miss.

The question I'm left with#

What does a hardware litmus run actually prove? My message-passing test showed nothing on ARM where the model allows it. A run tells you what that chip did under that harness, not what the model permits. A paper (arXiv:2310.12337) argues hardware runs are weak evidence for compiler-mapping correctness, which sits oddly with how often we validate models on hardware.

Second, what do fences cost? uops.info's isolated measurements put XCHG at 17 to 18 cycles of throughput. Agner Fog writes that a LOCK prefix "typically costs more than a hundred clock cycles", from older cores and contended cases. Both are right for their conditions: a fence waits for the buffer to drain, so cost depends on pending store misses, contention and core design. No agreed measurement exists, which makes strong-vs-weak model performance arguments hard to settle.

Next, if I go deeper: memory disambiguation, the predictor that lets a load skip an unresolved store, where the clean model ends and Spectre begins.

Sources#

© 2026 Bharat Kaurav. Built with Next.js & Tailwind.

>_ bk@netherwatcheresc to close

Welcome, reader. Type help to see what this shell can do.