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.7 Acquire/release as one-way barriers

A DMB (7.5) is a two-way fence. It orders a class of accesses before it against a class of accesses after it. Acquire and release instructions attach the ordering to one access, and only in one direction. The result is usually cheaper and often closer to what the algorithm needs. These instructions do not give completion, so they never replace a DSB (7.6).

The three ordering rules

The Arm ARM states the Load-Acquire, Load-AcquirePC and Store-Release rules as clauses of barrier-ordered-before (bob) in the axiomatic model of 7.3. The relevant lines of aarch64.cat, abridged:

let bob = po; [dmb.full]; po
        | [L]; po; [A]        (* Store-Release then Load-Acquire  *)
        | [A | Q]; po         (* Load-Acquire(PC) before everything later *)
        | po; [L]             (* everything earlier before Store-Release *)
        | ...

Here A is a Load-Acquire (LDAR, LDAXR, the A forms of CAS/SWP/LDADD…), Q is a Load-AcquirePC (LDAPR, LDAPUR), and L is a Store-Release (STLR, STLXR, the L forms of the atomics).

RuleOrderedNot ordered
Acquire (LDAR, LDAPR)the acquire read before every program-order-later load and storeearlier accesses may still be performed after the acquire
Release (STLR)every program-order-earlier load and store before the release writelater accesses may be performed before the release
RCsc (STLR then LDAR)the release write is observed before the later LDAR is satisfiedSTLR then LDAPR: no ordering

So acquire is a fence that stops later accesses from moving up above it, and release is a fence that stops earlier accesses from moving down below it. A critical section bracketed by an acquire and a release can absorb surrounding accesses (“roach motel”), but nothing can leak out of it.

Some consequences that are easy to get wrong:

  • STLR orders earlier loads as well as earlier stores. A release is therefore not equivalent to DMB ISHST; STR. The nearest two-way equivalent is DMB ISH; STR, which is strictly stronger because it also orders the earlier accesses against everything after the STR.
  • LDAR is not equivalent to LDR; DMB ISHLD. The DMB ISHLD form also orders every earlier load against later accesses, which is stronger in one way. It does not give the [L]; po; [A] ordering against an earlier STLR, which is weaker in another.
  • The ordering applies to all memory types, Device included (7.9), and to the acquire/release forms of the exclusives and LSE atomics (10.1, 10.2).
  • Nothing here concerns completion. STLR does not wait for its write, or for any earlier write, to become visible before execution continues. It only constrains the order in which observers can see them. Anything with a non-memory side effect still needs a DSB: before WFI/WFE/SEV, after TLBI or cache maintenance, or before relying on a peripheral having seen a write.

Message passing with acquire/release

// P0: producer                 // P1: consumer
    str   w1, [x0]      // data     1:  ldar  w2, [x3]      // flag
    mov   w4, #1                        cbz   w2, 1b
    stlr  w4, [x3]      // flag         ldr   w5, [x0]      // data

The outcome “P1 sees flag = 1 and data = 0” is forbidden. po; [L] orders the data write before the flag write. The flag write is read by P1 (rfe), and [A]; po orders the flag read before the data read. That is an ordered cycle in the model of 7.3. On the consumer side, an address dependency would also work (7.4), and so would LDR; DMB ISHLD. LDAR (or LDAPR) states the intent and does not depend on the compiler preserving a dependency.

Since the 2017 revision Armv8 is other-multi-copy atomic for all stores (7.2). Older texts describe STLR as “multi-copy atomic” in contrast to plain stores. That distinction no longer exists. What remains special about STLR is the ordering it imposes, not how its write propagates.

RCsc vs RCpc: why LDAPR exists

LDAR is RCsc (release consistency, sequentially consistent). A Store-Release followed by a Load-Acquire to a different address cannot be reordered, so the store-buffering test with STLR/LDAR on both sides forbids the 0/0 outcome:

// SB+rel-acq       P0                    P1
                    stlr w1, [x0]  // x   stlr w1, [x2]  // y
                    ldar w3, [x2]  // y   ldar w3, [x0]  // x
// exists (0:X3=0 /\ 1:X3=0): forbidden with LDAR, allowed with LDAPR

That property costs hardware something real. An LDAR cannot be satisfied while an earlier STLR is still waiting in the store buffer, so a release followed by an acquire tends to drain the store buffer. C11/C++11 memory_order_acquire and memory_order_release only require RCpc (“processor consistent”): an acquire load does not have to be ordered after an unrelated earlier release store. Only memory_order_seq_cst needs the RCsc pairing.

LDAPR (Load-AcquirePC) is LDAR without the [L]; po; [A] clause. It keeps [Q]; po: every later access is still ordered after it. It just no longer waits for an earlier STLR.

FeatureInstructionsAvailability
base v8.0LDAR, LDARB, LDARH, STLR, STLRB, STLRH, LDAXR/STLXR and pair formsmandatory in v8.0
FEAT_LRCPCLDAPR, LDAPRB, LDAPRH (base register only)permitted from v8.2, mandatory from v8.3
FEAT_LRCPC2LDAPUR, LDAPURB/H/SB/SH/SW, STLUR, STLURB, STLURH: signed 9-bit unscaled immediate offsetmandatory from v8.4
FEAT_LRCPC3LDIAPP/STILP, writeback forms, SIMD&FP LDAP1/STL1see 10.3

Discovery is through ID_AA64ISAR1_EL1.LRCPC: 0b0001 means FEAT_LRCPC, 0b0010 adds FEAT_LRCPC2, and 0b0011 adds FEAT_LRCPC3. STLUR is an ordinary Store-Release with an offset. The “PC” only weakens the load side, so a separate “release-PC” store does not exist.

The offset forms matter more than they look. Before LRCPC2, an acquire or release of a structure field needed an ADD to form the address, because LDAR/STLR accept only [Xn]. Compilers emit LDAPUR/STLUR for atomic_load_explicit(&s->f, memory_order_acquire) when the target allows it.

The C11/C++11 mapping

This is the mapping GCC and LLVM use for AArch64 (the “Cambridge mapping”, with the RCpc refinement):

C/C++ operationA64 (without LSE / LRCPC)A64 with FEAT_LSE, FEAT_LRCPC
load relaxedLDRLDR
load acquire (and consume, which compilers promote to acquire)LDARLDAPR / LDAPUR
load seq_cstLDARLDAR (must stay RCsc)
store relaxedSTRSTR
store release / seq_cstSTLRSTLR / STLUR
RMW relaxedLDXR/STXR loopLDADD, SWP, CAS
RMW acquireLDAXR/STXRLDADDA, SWPA, CASA
RMW releaseLDXR/STLXRLDADDL, SWPL, CASL
RMW acq_rel / seq_cstLDAXR/STLXRLDADDAL, SWPAL, CASAL
atomic_thread_fence(acquire)DMB ISHLDDMB ISHLD
atomic_thread_fence(release / acq_rel / seq_cst)DMB ISHDMB ISH

Three points in this table are deliberate:

  1. seq_cst loads and stores need no fence. STLR followed by LDAR already has the RCsc ordering, so the classic “seq_cst store then seq_cst load” (Dekker) works with no DMB. For that reason a seq_cst load can never be lowered to LDAPR.
  2. A release fence is DMB ISH, not DMB ISHST. A release fence must order earlier loads against later stores, and DMB ISHST orders only store against store.
  3. Everything uses ISH. The C11 model’s threads are assumed to be PEs in one Inner Shareable domain. If the other side is a DMA master, an SCP/MCP outside the Inner Shareable domain, or memory that is Device or Normal Non-cacheable (always Outer Shareable, 6.4), stdatomic.h is not the right tool. Write explicit DMB OSH* or DSB sequences (7.10).

Bring-up note: In TF-A-style C, __atomic_load_n(p, __ATOMIC_ACQUIRE) on a v8.2 build without +rcpc compiles to LDAR. Building for -march=armv8.3-a or later, or adding +rcpc, changes it to LDAPR. A core that does not implement FEAT_LRCPC then takes an UNDEFINED exception. Pin -march to what ID_AA64ISAR1_EL1 actually reports on the slowest core in the system, not to the newest core on the SoC (1.1).

Security note: A release store publishing a pointer, together with an acquire load consuming it, is the minimum that stops a reader from seeing an initialised pointer to uninitialised contents. A reader using plain LDR with no dependency can see stale memory behind a fresh pointer, for example an attacker-controlled previous allocation. That is a publication bug: each individual access is single-copy atomic, yet the reader still uses stale data.

Checkpoint

Q7.7.1 Two PEs each run STLR W1, [own_flag] followed by an acquire load of the other PE’s flag. The final condition “both loads read 0” must be forbidden. Which load instruction choice achieves that, and why?

  • A. LDAPR on both sides, because every access after an acquire is ordered after it
  • B. LDR on both sides, because Armv8 is other-multi-copy atomic
  • C. LDAR on both sides, because a Store-Release is ordered before a program-order-later Load-Acquire (RCsc)
  • D. Either LDAR or LDAPR, because the release already orders all earlier accesses
Answer

C. This is SB. Forbidding 0/0 needs each PE’s store ordered before its later load, which is exactly the [L]; po; [A] clause of bob that LDAR carries and LDAPR drops. Option D is the tempting wrong answer: a release orders earlier accesses before itself and says nothing about later loads. Multi-copy atomicity (B) does not stop store buffering.

Read more: Arm ARM (DDI0487), Load-Acquire, Load-AcquirePC, and Store-Release; the definition of Barrier-ordered-before

Q7.7.2 A compiler lowers atomic_thread_fence(memory_order_release) to DMB ISH instead of the cheaper DMB ISHST. Why is DMB ISHST insufficient?

  • A. A release fence must also order earlier loads before later stores, and DMB ISHST orders only stores against stores
  • B. DMB ISHST does not order accesses to Normal Cacheable memory
  • C. DMB ISHST waits for completion, which is too slow and gives no extra ordering
  • D. DMB ISHST orders only the Inner Shareable domain, and release requires Outer Shareable
Answer

A. Release semantics forbid an earlier read from being satisfied after the releasing store becomes visible. Otherwise a reader that acquires could overwrite data that the releasing thread is still about to read (the load-buffering shape). DMB ISHST only orders write against write. Option D is wrong: compilers use ISH for every C11 fence. C is wrong because no DMB waits for completion.

Read more: Arm ARM (DDI0487), Data Memory Barrier; C/C++ to AArch64 mappings in the herdtools/Cambridge papers

Q7.7.3 Firmware posts a command for a PE by writing the command buffer and then doing STLR on a flag in Normal Cacheable memory. The other PE is in WFE. The producer executes SEV immediately after the STLR. Occasionally the consumer wakes, reads the old flag, and goes back to sleep forever. What is missing?

  • A. Nothing. STLR guarantees the flag is visible before any later instruction executes
  • B. An ISB between STLR and SEV
  • C. Replace STLR with DMB ISH; STR
  • D. A DSB ISH between the STLR and the SEV, because release orders memory effects but does not make the store complete before the non-memory event
Answer

D. SEV is not a memory access, so neither release semantics nor a DMB orders it. Only a DSB makes the flag write complete before the event is sent. Option A confuses ordering with completion. C has the same flaw in a different form. B synchronises the instruction context, not memory.

Read more: Arm ARM (DDI0487), Data Synchronization Barrier; Wait for Event mechanism and Send event