Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

7.3 Litmus tests

A litmus test is a tiny multi-threaded program plus a final condition on registers and memory. The question it asks is always the same: does the architecture allow this outcome? The five shapes on this page (MP, SB, LB, WRC, IRIW) cover almost every ordering bug you will meet in firmware. Each one is a pattern you can recognise in real code, and each has a minimal fix and a typical wrong fix. 7.11 runs them with herd7 and litmus7. Here we derive the answers from the axiomatic model.

The model in one screen

DDI0487 B2 defines the Arm memory model axiomatically. A candidate execution (a choice of rf and co, see 7.1) is allowed only if it satisfies three checks. This is an abridged rendering for explicit accesses only, in the style of the 2017 aarch64.cat:

obs = rfe | coe | fre                               (* Observed-by: external edges *)

dob = addr | data                                   (* Dependency-ordered-before *)
    | ctrl; [W]
    | (ctrl | addr; po); [ISB]; po; [R]
    | addr; po; [W]
    | (addr | data); rfi

aob = rmw | [range(rmw)]; rfi; [A | Q]              (* Atomic-ordered-before *)

bob = po; [dmb.full]; po                            (* Barrier-ordered-before *)
    | [R]; po; [dmb.ld]; po
    | [W]; po; [dmb.st]; po; [W]
    | [A | Q]; po                                   (* acquire / acquirePC *)
    | po; [L]                                       (* release *)
    | [L]; po; [A]                                  (* STLR then LDAR: RCsc *)

ob  = (obs | dob | aob | bob)+                      (* Ordered-before *)

acyclic  po-loc | rf | co | fr     as internal      (* per-location coherence *)
irreflexive ob                     as external      (* no cycle in ob *)
empty    rmw & (fre; coe)          as atomic        (* RMWs really are atomic *)

A is LDAR-class acquire, Q is LDAPR-class acquirePC, L is STLR-class release, dmb.full is DMB SY/ISH/OSH/NSH. The current herdtools7 aarch64.cat is much larger. It separates explicit and implicit effects (instruction fetch, translation table walks, MTE tags), adds TLBI, DSB and DC CVAU ordering, adds “pick” dependencies through conditional selects, and generalises the ISB clause. For the five tests below the abridged form gives the same answers.

The method is always the same. Write the cycle that the forbidden-looking outcome would need. If every edge in the cycle is in ob, the outcome is forbidden. If even one edge is plain po that no rule promotes, it is allowed. Other-multi-copy atomicity (7.2) is what lets the external rfe/fre/coe edges go straight into ob.

MP: message passing

P0                    | P1
STR  w1, [x_data]     | LDR  w2, [x_flag]     // r2 = 1 ?
STR  w1, [x_flag]     | LDR  w3, [x_data]     // r3 = 0 ?

Outcome r2=1, r3=0: P1 sees the flag but stale data.

Candidate cycle: Wdata -po-> Wflag -rfe-> Rflag -po-> Rdata -fre-> Wdata. Both po edges are plain, so the outcome is allowed, and it is commonly observed on out-of-order cores.

VariantResultWhy
DMB ISHST on P0, address dependency on P1ForbiddenP0 edge in bob (dmb.st, W→W), P1 edge in dob (addr)
DMB ISHST on P0, DMB ISHLD on P1Forbiddendmb.ld orders R→anything
STLR flag on P0, LDAR (or LDAPR) flag on P1Forbiddenpo; [L] and `[A
DMB ISHST on P0, control dependency on P1Allowedctrl orders only to writes. The data load may be satisfied speculatively before the branch resolves
DMB ISHST on P0, control dependency + ISB on P1Forbiddenctrl; [ISB]; po; [R]
DMB ISHLD on P0Alloweddmb.ld orders earlier reads. P0 has only writes

MP is the descriptor-plus-doorbell pattern, the mailbox, and every “write payload then set valid bit” protocol (7.10).

SB: store buffering

P0                    | P1
STR  w1, [x_a]        | STR  w1, [x_b]
LDR  w2, [x_b]        | LDR  w3, [x_a]        // r2 = 0 && r3 = 0 ?

Cycle: Wa -po-> Rb -fre-> Wb -po-> Ra -fre-> Wa. Plain: allowed. Each store can sit in its own PE’s store buffer while the load runs. This is the one reordering that even x86 allows.

The fix needs a W→R edge on both sides. Only these provide it:

  • a full DMB ISH (or SY), which is in bob via dmb.full;
  • STLR followed by LDAR, which is in bob via [L]; po; [A].

DMB ISHST does not work, because it is W→W only. DMB ISHLD does not work, because it orders from earlier reads. STLR + LDAPR does not work, because [L]; po; [Q] is deliberately not in bob (see 7.7). Dekker-style mutual exclusion, and “set my flag, then check yours” handshakes between EL3 and a secure partition or between a PE and an SCP, are all SB. A missing full barrier here is a classic bug in hand-rolled firmware locks (10.4).

LB: load buffering

P0                    | P1
LDR  w2, [x_a]        | LDR  w3, [x_b]        // r2 = 1 && r3 = 1 ?
STR  w1, [x_b]        | STR  w1, [x_a]

Cycle: Ra -po-> Wb -rfe-> Rb -po-> Wa -rfe-> Ra. Plain: allowed by the architecture. In the 6.1 picture, a store may be made visible once the earlier load is known not to fault, before the load’s data has actually returned. Many implementations never show it, but the model does not forbid it.

Any dependency from the load to the store forbids it: data and addr directly, and ctrl; [W] because stores are never made visible speculatively. So LB+datas, LB+addrs and LB+ctrls are all forbidden. LB is the shape behind “can my load return a value written by a store that comes after it in my own program order?”. Architecturally the answer is yes unless a dependency or barrier rules it out.

WRC: write-to-read causality

P0              | P1                    | P2
STR w1, [x_x]   | LDR w2, [x_x]  // =1  | LDR w4, [x_y]  // =1
                | <dep>                 | <dep>
                | STR w1, [x_y]         | LDR w5, [x_x]  // =0 ?

Cycle: Wx -rfe-> R1x -po-> Wy -rfe-> R2y -po-> R2x -fre-> Wx.

  • Plain: allowed, because the reorderings on P1 and P2 break the chain.
  • WRC+addrs (address dependency on P1 to the store, which is addr; po; [W] or a direct addr/data, and on P2 to the second load): forbidden. Every edge is in ob.

The interesting edge is the first rfe. P1 has seen P0’s write. Other-multi-copy atomicity says that if any observer other than P0 has seen it, all have. So P2, having seen P1’s later write through a dependency chain, must see x=1. Under the pre-2017 non-MCA model this was allowed unless P1 used a cumulative barrier (7.2). WRC is the shape of “core A publishes, core B forwards a pointer, core C consumes”.

IRIW: independent reads of independent writes

P0            | P1            | P2                   | P3
STR w1,[x_x]  | STR w1,[x_y]  | LDR w2,[x_x] // =1   | LDR w4,[x_y] // =1
              |               | LDR w3,[x_y] // =0   | LDR w5,[x_x] // =0 ?

The outcome means P2 thinks x was written first and P3 thinks y was written first. Cycle: Wx -rfe-> P2:Rx -po-> P2:Ry -fre-> Wy -rfe-> P3:Ry -po-> P3:Rx -fre-> Wx.

  • Plain: allowed, because each reader’s two loads can be reordered.
  • IRIW+addrs or IRIW+dmb.lds (ordering on both readers only): forbidden, with no barrier at all on the writers. This is the cleanest demonstration that Armv8 is other-MCA. On POWER, IRIW+addrs is allowed and needs sync.

Summary

TestPlainMinimal sufficient fixTypical insufficient fix
MPAllowedDMB ST writer + addr dep / DMB LD / LDAR readerControl dependency alone on the reader
SBAllowedFull DMB both sides, or STLR→LDARDMB ST, DMB LD, STLR→LDAPR
LBAllowedAny dependency (data/addr/ctrl) both sidesnone common
WRCAllowedDependencies (or DMB LD) on both readersBarrier on P0 only
IRIWAllowedDependencies or DMB LD on both readersBarriers on the writers

Coherence is free, and only per location

The internal check covers the single-location cases (CoRR, CoWR, CoRW, CoWW). On one PE, two reads of the same location never see values going backwards in Coherence order. A read never returns a value older than the PE’s own earlier write. None of this needs a barrier. Do not use these tests to argue about two different locations, even ones in the same cache line. The model has no notion of cache lines.

Validation note: “Allowed” is not “will be observed”. LB and some MP variants may never appear on an in-order core such as Cortex-A53, even after billions of iterations. Conversely, observing a forbidden outcome under litmus7 means either a silicon/interconnect bug or a test bug, such as a missing dependency in the generated code or the wrong memory type. Before you file a hardware bug, confirm the test against herd7 first (7.11).

Checkpoint

Q7.3.1 A reader polls a flag and then reads a payload. The writer uses DMB ISHST between payload and flag. The reader code is LDR w0, [flag]; CBZ w0, retry; LDR w1, [payload]. Is flag=1, payload=old allowed?

  • A. No, because the conditional branch creates a control dependency that orders the two loads
  • B. No, because DMB ISHST on the writer is cumulative and orders the reader too
  • C. Yes, because a control dependency orders only later stores; the payload load can be satisfied speculatively before the branch resolves
  • D. Yes, but only if the payload and the flag are in different cache lines
Answer

C. In the model, ctrl contributes only ctrl; [W] and ctrl; [ISB]; po; [R]. Adding ISB after the branch, an address dependency, DMB ISHLD, or LDAR on the flag would forbid the outcome. Option A is the common misconception. Option D is irrelevant, because the model has no cache lines.

Read more: Arm ARM (DDI0487), B2 “Dependency-ordered-before”; the “Barrier Litmus Tests” appendix

Q7.3.2 Two PEs each do STR to their own flag then LDR the other’s flag (SB), and both read 0. A developer adds STLR for the store and LDAPR for the load. Is the both-zero outcome now forbidden?

  • A. Yes, because release followed by any acquire is ordered
  • B. No, because [L]; po; [Q] is not in Barrier-ordered-before; only LDAR after STLR gives the W→R edge
  • C. Yes, because LDAPR orders all later accesses after it
  • D. No, because SB can only be fixed with DSB
Answer

B. RCpc acquire (LDAPR) is allowed to be satisfied before an earlier STLR to a different address is observed. That is the whole difference from LDAR. A full DMB ISH between the store and the load, or LDAR, fixes it. A DSB is sufficient but not necessary (D).

Read more: Arm ARM (DDI0487), B2 “Barrier-ordered-before”; LDAPR

Q7.3.3 In IRIW, the two readers each use DMB ISHLD between their loads, and the writers have no barriers. Which statement is right?

  • A. The disagreeing outcome is forbidden, because other-multi-copy atomicity puts the external edges in ob and the readers’ DMB LD closes the cycle
  • B. The outcome is still allowed, because the writers also need DMB ISHST
  • C. The outcome is forbidden only if all four PEs are in the same cluster
  • D. The outcome is allowed, because DMB LD does not order a load before a later load
Answer

A. Each writer issues one store, so there is nothing on the writer side to order. The cycle consists of rfe, reader bob, fre, rfe, reader bob, fre, all in ob. Option B is the reflex from non-MCA models. Option D is false: DMB LD orders earlier loads against all later accesses.

Read more: Arm ARM (DDI0487), B2 “Multi-copy atomicity”; herdtools7 aarch64.cat