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

10.2 LSE atomics (FEAT_LSE)

FEAT_LSE (Large System Extensions) adds single-instruction read-modify-write operations. It is mandatory from v8.1: ID_AA64ISAR0_EL1.Atomic (bits [23:20]) must read 0b0010 (LSE) or 0b0011 (LSE plus LSE128). The motivation is in the name. On a large system, an LDXR/STXR loop must pull the line into the requester’s cache in a Unique state, and under contention the line moves between cores while most STXRs fail. A single atomic instruction can instead be sent to the data. It is performed once, at a point where the line is already serialised, and it cannot fail and retry.

The instruction families

FamilyOperation (old = value in memory)Returns
CAS Ws, Wt, [Xn]if old == Ws then write Wt; Ws := oldold value in Ws
CASP2×32 or 2×64-bit compare-and-swap of a register pair (10.3)old pair
SWP Ws, Wt, [Xn]write Ws; Wt := oldold value
LDADD Ws, Wt, [Xn]write old + Ws; Wt := oldold value
LDCLRold AND NOT Ws (bit clear)old value
LDEORold XOR Wsold value
LDSETold OR Wsold value
LDSMAX/LDSMINsigned max/minold value
LDUMAX/LDUMINunsigned max/minold value

Every form except CASP has B and H (byte, halfword) variants, and all have 32/64-bit register forms. There is no LDSUB (use LDADD with a negated operand) and no LDAND (use LDCLR with an inverted mask).

ST<op> (STADD, STCLR, STSET, …) are aliases, not separate instructions. They are LD<op> with Rt = XZR/WZR and no acquire (A == 0). The disassembler prints STADD only when both conditions hold. The point of the alias is to tell hardware that the old value is not needed, so the operation can be posted like a store (“fire and forget”). This matters for far atomics, below.

Ordering variants

Each instruction has four ordering forms, written as a suffix before the size letter: none, A (acquire), L (release), AL (acquire and release), for example LDADDAL, CASAH, SWPLB. The acquire is RCsc acquire, the same as LDAR, and the release is the same as STLR (7.7).

Two rules are easy to get wrong:

  1. Acquire disappears with a zero-register destination. DDI0487 states that “if the destination register is not one of WZR or XZR, LDADDA and LDADDAL load from memory with acquire semantics”. It is the same for CASA/CASAL when Rs is the zero register. LDADDAL wzr-style code therefore has release semantics only. If you need a full barrier around a value-discarding atomic, use a real destination register or add a DMB.
  2. A failed CAS need not write. If the comparison fails, the architecture permits the old value to be written back but does not require it. Do not reason about CASL’s release ordering on the failure path, because there may be no store to which it attaches. The acquire on the read of a CASA/CASAL applies whether or not the compare succeeds, which is what lets a failed CASA in a lock loop see the critical-section writes of the previous owner.

When the operation is performed, the read and the write are a single atomic access. No other write to the location can come between them in the coherence order. For a CAS, this holds only when the write is performed. A CAS whose Rs and Rt are the same register can never change memory.

Atomics are explicit memory accesses. A DMB orders them like the loads and stores they contain. For a value-returning atomic, the read part creates address, data and control dependencies through the destination register in the usual way (7.4). An ST<op> returns nothing, so it cannot head a dependency chain.

Alignment and memory types

LSE atomics must be naturally aligned. Without FEAT_LSE2, a misaligned atomic always takes an Alignment fault, regardless of SCTLR_ELx.A. With FEAT_LSE2, an access that stays inside an aligned 16-byte quantity is allowed (10.3).

Memory-type support has the same shape as for exclusives (10.1):

  • Normal Inner/Outer Write-Back Shareable is supported.
  • Non-cacheable and Device are IMPLEMENTATION DEFINED, because the target (interconnect home node, memory controller or peripheral) must perform the atomic. An unsupported target may produce a Data Abort with DFSC 0b110101 (Unsupported Exclusive or Atomic access) or an External abort.

With the MMU off, data accesses are Device-nGnRnE (6.5), so a CASA-based lock in early boot is just as non-portable as an exclusive one.

Near and far atomics

Architecturally, an atomic is just an atomic. Where it is executed is an implementation choice, and the choice is invisible except in performance and in which faults or errors are possible.

  • Near atomic. The PE obtains the line in a Unique state in its own L1 or L2, performs the ALU operation locally, and leaves the line dirty in its cache. This is the same traffic as a successful exclusive pair, without the failure window. It is the right choice when the same core will touch the location again soon.
  • Far atomic. The PE sends the operation to the interconnect as an atomic transaction. In AMBA CHI these are AtomicStore (no return, which is what ST<op> maps to), AtomicLoad (LD<op>), AtomicSwap (SWP) and AtomicCompare (CAS). The home node (HN-F, the point of coherency and serialisation for that address, usually alongside the system-level cache slice) performs the operation, after snooping out any cached copies. Alternatively, it forwards the operation to the subordinate (memory controller or device) if that is where the data lives. This is the right choice for a highly contended counter or lock, because the line stops moving between cores.

Which one a given core uses is IMPLEMENTATION DEFINED. Typical policies are: near if the line already hits Unique or Exclusive in L1, far otherwise. Some cores offer an IMPLEMENTATION DEFINED control (an IMP_CPUECTLR_EL1-style field) to force near or far, or to choose by memory type. Check the core TRM, and check that the interconnect supports far atomics at all. A CHI requester connected to an interconnect or bridge without atomic support must be configured to execute atomics near, or atomics to Non-cacheable memory will fault.

Ordering of far atomics

A far atomic gets exactly the same architectural ordering as a near one. The PE is responsible for this, not the interconnect:

  • For LD<op>A/CASA, later accesses cannot be performed before the returned value is available, which for a far atomic means a round trip to the home node.
  • For L forms, the PE must ensure that all earlier accesses are observed before the atomic is issued.
  • A plain STADD (an AtomicStore) can be posted. A later DMB orders it against later accesses, as for an ordinary store. A DSB waits for it to complete, which for a far atomic means the home node has performed it and responded.

The point to internalise is that “far” does not weaken anything. It only moves the point of serialisation. What changes in practice is latency: an LDADDAL performed far on a cross-chiplet address may take hundreds of nanoseconds, because the acquire waits for the round trip across CHI C2C (8.3).

Bring-up note: far atomics introduce a failure mode that exclusives do not have. The home node, or a CXL/C2C bridge behind it, must implement every atomic opcode, and an unsupported one comes back as a transaction error (an External abort, often an asynchronous SError for STADD). If a statistics counter in Non-cacheable shared memory starts producing SErrors only after the interconnect is reconfigured, check the near/far policy and the bridge’s atomic-support parameters first.

LSE versus exclusives

ExclusivesLSE
Can fail and retryyes (spuriously, too)no (CAS can fail the compare, but is still a single access)
Arbitrary operationyes (any code between)fixed ALU ops, or CAS-loop
Contended scalingpoor (line moves, retries)good, especially far
Debugger single-stepcan livelockfine
Can head a WFE waityes (LDXR arms the monitor)no, combine with LDXR (10.4)

Toolchains choose at build time (-march=armv8.1-a or +lse), or at run time with outline atomics (GCC -moutline-atomics, the default for AArch64 Linux targets since GCC 10.1), which tests HWCAP_ATOMICS. Firmware built with -march=armv8-a silently uses exclusives everywhere.

Validation note: a lost-update test is the most direct check of atomic correctness across the coherency fabric. N cores, each on a different cluster or chiplet, perform M LDADDALs each on one shared counter, then compare the total with N×M. Run it three ways: on a Write-Back line, on a Non-cacheable line, and with the near/far policy forced each way, if the core allows. A short count means the home node or a bridge broke atomicity. An SError or a DFSC 0b110101 abort means an unsupported target. Use the core PMU’s atomic or MEM_ACCESS events and the CMN PMU’s per-opcode request counters to confirm that the expected Atomic* transactions arrived at the HN-F (see 8.10).

Checkpoint

Q10.2.1 A developer replaces LDADDAL x1, x2, [x0] with STADDL x1, [x0] because the old value is unused. The atomic increment marks the end of a producer’s critical section, which must be ordered before later reads in the producer. What changed?

  • A. Nothing: STADDL is an alias of LDADDAL with XZR as destination
  • B. The acquire semantics are lost, so later loads may now be performed before the atomic. STADDL is LDADDL with XZR, which only has release semantics
  • C. The release semantics are lost, so earlier critical-section stores may be reordered after the atomic
  • D. The operation becomes non-atomic, because ST<op> forms are performed as a separate read and write
Answer

B. ST<op> aliases only exist for A == 0, so STADDL is LDADDL with a zero destination. It keeps the release, so C is wrong, but it never had acquire. Even LDADDAL with a zero destination loses its acquire. The operation is still a single atomic access, so D is wrong. If later reads must not pass it, keep a real destination register or add DMB ISH.

Read more: Arm ARM (DDI0487), LDADD, LDADDA, LDADDAL, LDADDL (STADD alias conditions)

Q10.2.2 On a CHI system, core 0 executes LDADDA w1, w2, [x0] and the core chooses to perform it far, at the home node. Which statement about its ordering is correct?

  • A. Far atomics are performed outside the PE, so acquire semantics only apply between PEs on the same home node
  • B. A far atomic is ordered only by a following DSB, because a DMB cannot observe interconnect-level operations
  • C. Later loads by core 0 cannot be performed until the old value returns from the home node, just as for a near atomic
  • D. Far atomics are only single-copy atomic with respect to other far atomics, not to ordinary stores from other cores
Answer

C. Where the atomic is performed is IMPLEMENTATION DEFINED and invisible to the memory model. The PE must provide the same acquire, which here means waiting for the response. The home node serialises all accesses to the line, including ordinary stores, which it handles by snooping, so D is wrong. DMB orders far atomics like any explicit access, so B is wrong. B confuses ordering with completion.

Read more: Arm ARM (DDI0487), Atomic memory operation instructions and the definition of Ordered-before; AMBA CHI (IHI0050), Atomic transactions

Q10.2.3 A lock-acquire loop is 1: mov w1, wzr; casa w1, w2, [x0]; cbnz w1, 1b. A reviewer argues that when the CASA fails the compare, it “performs no access, so it can’t synchronise with the previous owner’s STLR”. What is the correct response?

  • A. The read of a failed CASA still has acquire semantics, so once a CASA succeeds or reads the released value, the previous owner’s critical-section writes are visible. A failed compare also guarantees nothing is written in a way software can rely on
  • B. The reviewer is right, and a DMB ISH is needed after every failed CASA
  • C. The reviewer is right for CASA but not for CASAL, whose release covers the failure path
  • D. The reviewer is wrong because a failed CAS always writes the old value back, creating a store that orders the sequence
Answer

A. A CAS always reads, and the acquire applies to that read when Rs is not the zero register (here the destination is w1). The loop only exits on a successful CASA, which is an acquire read of the lock word written by the owner’s release. D states as mandatory something that is only permitted. C is backwards: release attaches to the write, and on failure there may be no write.

Read more: Arm ARM (DDI0487), CAS, CASA, CASAL, CASL