arm_linux: Add missing memory barrier to atomic_cmpxchg #1234
Open
taiki-e wants to merge 3 commits into
Open
Conversation
2 tasks
tgross35
approved these changes
Jul 21, 2026
Contributor
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.)