arm_linux: Add missing memory barrier to atomic_cmpxchg - #1234
Conversation
There was a problem hiding this comment.
This looks reasonable to me, couple questions for my understanding:
- This only shows up when multiple atomics are involved, right (hence seqcst) ? I think the single-atomic pathing was fine here but can't convince myself of it.
- Is there a C implementation somewhere that we can reference?
Also I'm planning to add the middle bit of your PR message into the third commit's description so there's some context in the history, feel free to do this yourself if you prefer.
ARM is an architecture with a weak memory model, so the absence of a fence here affects SeqCst, AcqRel, and Acquire. Actually, the code that encountered this bug that led to this investigation used Acquire load (which, in pre-v6 Arm, this is implemented in CAS(0, 0)).
It appears that libgcc has the same bug: https://github.com/gcc-mirror/gcc/blob/32657f29f918712ad7110bd54d8ebb3bf6b0a2a8/libgcc/config/arm/linux-atomic.c#L209 @Amanieu, who added these to compiler-builtins in #115, might know more about this. |
|
The implementation here should match libgcc, I would be more comfortable if this was first reported upstream to get a confirmation. |
This PR contains three commits. The main fix is the last one, and the first two address minor issues such as documentation that I noticed when writing the main fix. I would suggest reviewing by commit, but I can split them into separate PRs if necessary.
Currently, atomic_cmpxchg returns the value obtained from the first relaxed load as-is without calling the kuser helper when comparison fails:
compiler-builtins/compiler-builtins/src/sync/arm_linux.rs
Lines 130 to 134 in 5af7a65
This is equivalent to
compare_exchange(SeqCst, Relaxed), but the__syncbuiltins are expected to have SeqCst semantics in both the success and failure paths, so this is unsound.This PR fixes this by adding a memory barrier to failure path:
compiler-builtins/compiler-builtins/src/sync/arm_linux.rs
Lines 134 to 141 in c0cdefb
Context and Disclosure: This bug had been observed for several years as a rare stress test failure in portable-atomic's CI when testing pre-v6 ARM Linux on ARM hardware (https://gist.github.com/taiki-e/31962f6efe8cc9bc06aa6869bbceb656). In my past investigations, I suspected the bug was with the algorithm used in portable-atomic, so I was unable to identify the cause, but I recently identified the cause by investigating a wider scope with the help of LLMs. (All changes in this PR were written by me.)