Provide Rust atomic helpers over raw pointers
Subsystems (4)
cover Provide Rust atomic helpers over raw pointers
[1] indicates that there might be need for Rust to synchronize with C over atomic, currently the recommendation is using Atomic::from_ptr().op(), however, it's more convenient to have helper wrapper that directly perform operation over pointers. Hence add them for load()/store()/xchg()/cmpxgh().
While working on this, I also found an issue in from_ptr(), therefore fix it.
[1]: https://lore.kernel.org/rust-for-linux/[email protected]/
Regards,
Boqun
Boqun Feng (2):
rust: sync: atomic: Remove bound T: Sync for Atomci::from_ptr()
rust: sync: atomic: Add atomic operation helpers over raw pointers
rust/kernel/sync/atomic.rs | 109 ++++++++++++++++++++++++++- rust/kernel/sync/atomic/predefine.rs | 46 +++++++++++ 2 files changed, 151 insertions(+), 4 deletions(-)
Signature · 1 line
2.51.0
PATCH 1/2 rust: sync: atomic: Remove bound `T: Sync` for `Atomci::from_ptr()`
Originally, Atomic::from_ptr() requires T being a Sync because I thought having the ability to do from_ptr() meant multiplle &Atomic<T>s shared by different threads, which was identical (or similar) to multiple &Ts shared by different threads. Hence T was required to be Sync. However this is not true, since &Atomic<T> is not the same at &T. Moreover, having this bound makes Atomic::<*mut T>::from_ptr() impossible, which is definitely not intended. Therefore remove the T: Sync bound.
--- rust/kernel/sync/atomic.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
rust/kernel/sync/atomic.rs +1 -4
@@ -215,10 +215,7 @@ pub const fn new(v: T) -> Self {/// // no data race. /// unsafe { Atomic::from_ptr(foo_a_ptr) }.store(2, Release); /// ``` -pub unsafe fn from_ptr <'a>(ptr: *mut T ) -> &'aSelf -where -T :Sync , - { +pub unsafe fn from_ptr <'a>(ptr: *mut T ) -> &'aSelf {// CAST: `T` and `Atomic<T>` have the same size, alignment and bit validity. // SAFETY: Per function safety requirement, `ptr` is a valid pointer and the object will // live long enough. It's safe to return a `&Atomic<T>` because function safety requirement
Signature · 1 line
2.51.0
Re: [PATCH 1/2] rust: sync: atomic: Remove bound `T: Sync` for `Atomci::from_ptr()`
Quoted 11 lines · Boqun Feng
Originally,
Atomic::from_ptr()requiresTbeing aSyncbecause I thought having the ability to dofrom_ptr()meant multiplle&Atomic<T>s shared by different threads, which was identical (or similar) to multiple&Ts shared by different threads. HenceTwas required to beSync. However this is not true, since&Atomic<T>is not the same at&T. Moreover, having this bound makesAtomic::<*mut T>::from_ptr()impossible, which is definitely not intended. Therefore remove theT: Syncbound.Fixes 29c32c405e53 ("rust: sync: atomic: Add generic atomics")Signed-off-by Boqun Feng <[email protected]>
Alice Ryhl
maintainerRe: [PATCH 1/2] rust: sync: atomic: Remove bound `T: Sync` for `Atomci::from_ptr()`
In reply to Boqun FengQuoted 11 lines · Boqun Feng
Originally,
Atomic::from_ptr()requiresTbeing aSyncbecause I thought having the ability to dofrom_ptr()meant multiplle&Atomic<T>s shared by different threads, which was identical (or similar) to multiple&Ts shared by different threads. HenceTwas required to beSync. However this is not true, since&Atomic<T>is not the same at&T. Moreover, having this bound makesAtomic::<*mut T>::from_ptr()impossible, which is definitely not intended. Therefore remove theT: Syncbound.Fixes 29c32c405e53 ("rust: sync: atomic: Add generic atomics")Signed-off-by Boqun Feng <[email protected]>
there is a typo in patch title
Re: [PATCH 1/2] rust: sync: atomic: Remove bound `T: Sync` for `Atomci::from_ptr()`
In reply to Boqun FengQuoted 11 lines · Boqun Feng
Originally,
Atomic::from_ptr()requiresTbeing aSyncbecause I thought having the ability to dofrom_ptr()meant multiplle&Atomic<T>s shared by different threads, which was identical (or similar) to multiple&Ts shared by different threads. HenceTwas required to beSync. However this is not true, since&Atomic<T>is not the same at&T. Moreover, having this bound makesAtomic::<*mut T>::from_ptr()impossible, which is definitely not intended. Therefore remove theT: Syncbound.Fixes 29c32c405e53 ("rust: sync: atomic: Add generic atomics")Signed-off-by Boqun Feng <[email protected]>
Title has "Atomci" instead of "Atomic"
Quoted 3 lines
--- rust/kernel/sync/atomic.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
PATCH 2/2 rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Boqun FengIn order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an Atomic::from_ptr() to provide a &Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically a Atomic::from_ptr().op() wrapper.
Note: for naming, since atomic_xchg() and atomic_cmpxchg() has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named as xchg() and cmpxchg(). For atomic_load() and atomic_store(), their 32bit C counterparts are atomic_read() and atomic_set(), so keep the atomic_ prefix.
--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)
rust/kernel/sync/atomic.rs +104 -0
@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( } } } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`. +/// - If there is a concurrent store from kernel (C or Rust), it has to be atomic. +#[doc(alias( "READ_ONCE" , "smp_load_acquire" ))] +#[inline(always)] +pub unsafe fn atomic_load <T :AtomicType ,Ordering : ordering::AcquireOrRelaxed >( + ptr: *mut T , + o:Ordering , +) ->T +where +T ::Repr :AtomicBasicOps , +{ +// SAFETY: Per the function safety requirement, `ptr` is valid and aligned to +// `align_of::<T>()`, and all concurrent stores from kernel are atomic, hence no data race per +// LKMM. +unsafe {Atomic ::from_ptr (ptr) }.load (o) +} + +/// Atomic store over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_store(.., Relaxed)` maps to `WRITE_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Release)` maps to `smp_store_release()`. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to be atomic. +#[doc(alias( "WRITE_ONCE" , "smp_store_release" ))] +#[inline(always)] +pub unsafe fn atomic_store <T :AtomicType ,Ordering : ordering::ReleaseOrRelaxed >( + ptr: *mut T , + v:T , + o:Ordering , +)where +T ::Repr :AtomicBasicOps , +{ +// SAFETY: Per the function safety requirement, `ptr` is valid and aligned to +// `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race +// per LKMM. +unsafe {Atomic ::from_ptr (ptr) }.store (v, o); +} + +/// Atomic exchange over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().xchg(..)`, and can be used to work +/// with C side on synchronizations. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to be atomic. +#[inline(always)] +pub unsafe fn xchg <T :AtomicType ,Ordering : ordering::Ordering >( + ptr: *mut T , + new:T , + o:Ordering , +) ->T +where +T ::Repr :AtomicExchangeOps , +{ +// SAFETY: Per the function safety requirement, `ptr` is valid and aligned to +// `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race +// per LKMM. +unsafe {Atomic ::from_ptr (ptr) }.xchg (new, o) +} + +/// Atomic compare and exchange over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().cmpxchg(..)`, and can be used to work +/// with C side on synchronizations. +/// +/// # Safety +/// +/// - `ptr` is a valid pointer to `T` and aligned to `align_of::<T>()`. +/// - If there is a concurrent access from kernel (C or Rust), it has to be atomic. +#[doc(alias( "try_cmpxchg" ))] +#[inline(always)] +pub unsafe fn cmpxchg <T :AtomicType ,Ordering : ordering::Ordering >( + ptr: *mut T , + old:T , + new:T , + o:Ordering , +) ->Result <T ,T > +where +T ::Repr :AtomicExchangeOps , +{ +// SAFETY: Per the function safety requirement, `ptr` is valid and aligned to +// `align_of::<T>()`, and all concurrent accesses from kernel are atomic, hence no data race +// per LKMM. +unsafe {Atomic ::from_ptr (ptr) }.cmpxchg (old, new, o) +}
rust/kernel/sync/atomic/predefine.rs +46 -0
70 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 6 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.
Quoted 7 lines
Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>
Quoted 1 line
+/// -
atomic_store(.., Relaxed)maps toWRITE_ONCE()when using for inter-thread communication.
typo: "when used for"
Alice
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Boqun FengQuoted 1 line · Boqun Feng
In order to synchronize with C or external, atomic operations over raw
The sentence feels incomplete. Maybe "external memory"? Also "atomic operations over raw pointers" isn't a full setence.
Quoted 1 line
pointers, althought previously there is always an
Atomic::from_ptr()
You mean "already an"?
Quoted 9 lines
to provide a
&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.
I still have reservation on if this is actually needed. Directly reading from C should be rare enough that Atomic::from_ptr().op() isn't a big issue. To me, Atomic::from_ptr has the meaning of "we know this is a field that needs atomic access, but bindgen can't directly generate a Atomic<T>", and it will encourage one to check if this is actually true, while atomic_op doesn't feel the same.
That said, if it's decided that this is indeed needed, then
with the grammar in the commit message fixed.
Best,
Gary
Quoted 6 lines
Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 6 lines · Gary Guo
Quoted 1 line · Boqun Feng
In order to synchronize with C or external, atomic operations over raw
The sentence feels incomplete. Maybe "external memory"? Also "atomic operations over raw pointers" isn't a full setence.
Ah, my bad, should be "atomic operations over raw pointers are needed",
Quoted 4 lines
Quoted 1 line
pointers, althought previously there is always an
Atomic::from_ptr()You mean "already an"?
To me, it's kinda similar, but let's use "already"
Quoted 10 lines
Quoted 9 lines
to provide a
&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.
Quoted 7 lines
I still have reservation on if this is actually needed. Directly reading from C should be rare enough that
Atomic::from_ptr().op()isn't a big issue. To me,Atomic::from_ptrhas the meaning of "we know this is a field that needs atomic access, but bindgen can't directly generate aAtomic<T>", and it will encourage one to check if this is actually true, whileatomic_opdoesn't feel the same.
These are valid points, but personally I feel it's hard to prevent people to add these themselves ;)
Quoted 4 lines
That said, if it's decided that this is indeed needed, then
Reviewed-by Gary Guo <[email protected]>
Thank you.
Quoted 2 lines
with the grammar in the commit message fixed.
The new commit log now:
In order to synchronize with C or external memory, atomic operations over raw pointers are need. Although there is already an Atomic::from_ptr() to provide a &Atomic<T>, it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically an Atomic::from_ptr().op() wrapper.
Note: for naming, since atomic_xchg() and atomic_cmpxchg() have a conflict naming to 32bit C atomic xchg/cmpxchg, hence the helpers are just named as xchg() and cmpxchg(). For atomic_load() and atomic_store(), their 32bit C counterparts are atomic_read() and atomic_set(), so keep the atomic_ prefix.
Regards,
Boqun
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Boqun FengQuoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }
rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`.
I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
READ_ONCE is meant to be a dependency-ordering primitive, i.e. be more like memory_order_consume than it is memory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken. It is known to be brittle [1]. So the recommendation above is unsound; well, it's as unsound as implementing READ_ONCE with a volatile load.
While Alice's series tried to expose READ_ONCE as-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map to READ_ONCE.
Furthermore, LTO arm64 promotes READ_ONCE to an acquire (see arch/arm64/include/asm/rwonce.h):
/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by a READ_ONCE() invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
* READ_ONCE() definition with one that provides RCpc acquire semantics
* when building with LTO.
*/
So for all intents and purposes, the only sound mapping when pairing READ_ONCE() with an atomic load on the Rust side is to use Acquire ordering.
Gary Guo
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 39 lines · Marco Elver
Quoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.
On the Rust-side documentation we mentioned that Relaxed always preserve dependency ordering, so yes, it is closer to consume in the C11 model.
Quoted 3 lines
It is known to be brittle [1]. So the recommendation
above is unsound; well, it's as unsound as implementingREAD_ONCEwith a volatile load.
Sorry, which part of this is unsound? You mean that the dependency ordering is actually lost when it's not supposed to be? Even so, it'll be only a problem on specific users that uses Relaxed to carry ordering?
Users that use Relaxed for things that don't require any ordering would still be fine?
Quoted 8 lines
While Alice's series tried to expose
READ_ONCEas-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map toREAD_ONCE.
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Quoted 14 lines
Furthermore, LTO arm64 promotes
READ_ONCEto an acquire (seearch/arm64/include/asm/rwonce.h):/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by aREAD_ONCE()invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
*READ_ONCE()definition with one that provides RCpc acquire semantics
* when building with LTO.
*/
Quoted 3 lines
So for all intents and purposes, the only sound mapping when pairing
READ_ONCE()with an atomic load on the Rust side is to use Acquire ordering.
LLVM handles address dependency much saner than GCC does. It for example won't turn address comparing equal into meaning that the pointer can be interchanged (as provenance won't match). Currently only address comparision to NULL or static can have effect on pointer provenance.
Although, last time I asked if we can rely on this for address dependency, I didn't get an affirmitive answer -- but I think in practice it won't be lost (as currently implemented).
Furthermore, Rust code currently does not participate in LTO.
Best,
Gary
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 41 lines · Gary Guo
Quoted 39 lines · Marco Elver
Quoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
Quoted 7 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.
Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?
Quoted 7 lines
Quoted 3 lines
It is known to be brittle [1]. So the recommendation
above is unsound; well, it's as unsound as implementingREAD_ONCEwith a volatile load.Sorry, which part of this is unsound? You mean that the dependency ordering is actually lost when it's not supposed to be? Even so, it'll be only a problem on specific users that uses
Relaxedto carry ordering?
Correct.
Quoted 2 lines
Users that use
Relaxedfor things that don't require any ordering would still be fine?
Yes.
Quoted 5 lines
Quoted 5 lines
While Alice's series tried to expose
READ_ONCEas-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map toREAD_ONCE.
Quoted 7 lines
Quoted 2 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Quoted 18 lines
Quoted 14 lines
Furthermore, LTO arm64 promotes
READ_ONCEto an acquire (seearch/arm64/include/asm/rwonce.h):/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by aREAD_ONCE()invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
*READ_ONCE()definition with one that provides RCpc acquire semantics
* when building with LTO.
*/Quoted 3 lines
So for all intents and purposes, the only sound mapping when pairing
READ_ONCE()with an atomic load on the Rust side is to use Acquire ordering.
Quoted 8 lines
LLVM handles address dependency much saner than GCC does. It for example won't turn address comparing equal into meaning that the pointer can be interchanged (as provenance won't match). Currently only address comparision to NULL or static can have effect on pointer provenance.
Although, last time I asked if we can rely on this for address dependency, I didn't get an affirmitive answer -- but I think in practice it won't be lost (as currently implemented).
There is no guarantee here, and this can change with every new release. In most cases where it matters it works today, but the compiler (specifically LLVM) does break dependencies even if rarely [1].
Quoted 1 line
Furthermore, Rust code currently does not participate in LTO.
LTO is not the problem, aggressive compiler optimizations (as discussed in [1]) are. And Rust, by virtue of its strong type system, appears to give the compiler a lot more leeway how it optimizes code. So I think the Rust side is in greater danger here than the C with LTO side. But I'm speculating (pun intended) ...
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel free to ignore.
Gary Guo
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 45 lines · Marco Elver
Quoted 11 lines · Gary Guo
Quoted 8 lines
I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?Quoted 7 lines
Quoted 3 lines
It is known to be brittle [1]. So the recommendation
above is unsound; well, it's as unsound as implementingREAD_ONCEwith a volatile load.Sorry, which part of this is unsound? You mean that the dependency ordering is actually lost when it's not supposed to be? Even so, it'll be only a problem on specific users that uses
Relaxedto carry ordering?Correct.
Quoted 2 lines
Users that use
Relaxedfor things that don't require any ordering would still be fine?Yes.
Quoted 5 lines
Quoted 5 lines
While Alice's series tried to expose
READ_ONCEas-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map toREAD_ONCE.Quoted 7 lines
Quoted 2 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Quoted 4 lines
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Ultimately it's down to same LLVM IR as ClangBuiltLinux, so if it works for C, it'll work for Rust.
Quoted 24 lines
Quoted 24 lines
Quoted 15 lines
Furthermore, LTO arm64 promotes
READ_ONCEto an acquire (seearch/arm64/include/asm/rwonce.h):/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by aREAD_ONCE()invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
*READ_ONCE()definition with one that provides RCpc acquire semantics
* when building with LTO.
*/Quoted 3 lines
So for all intents and purposes, the only sound mapping when pairing
READ_ONCE()with an atomic load on the Rust side is to use Acquire ordering.LLVM handles address dependency much saner than GCC does. It for example won't turn address comparing equal into meaning that the pointer can be interchanged (as provenance won't match). Currently only address comparision to NULL or static can have effect on pointer provenance.
Quoted 8 lines
Quoted 3 lines
Although, last time I asked if we can rely on this for address dependency, I didn't get an affirmitive answer -- but I think in practice it won't be lost (as currently implemented).
There is no guarantee here, and this can change with every new release. In most cases where it matters it works today, but the compiler (specifically LLVM) does break dependencies even if rarely [1].
This is a 2022 slide, how much of it is still true today? Nikita has improved how LLVM handles pointers quite significant in the past few years, so this might not even apply anymore?
I'd like to see examples of LLVM still breaking address dependencies today, so at least I'm aware when writing code that depends on them.
Quoted 8 lines
Quoted 2 lines
Furthermore, Rust code currently does not participate in LTO.
LTO is not the problem, aggressive compiler optimizations (as discussed in [1]) are. And Rust, by virtue of its strong type system, appears to give the compiler a lot more leeway how it optimizes code. So I think the Rust side is in greater danger here than the C with LTO side. But I'm speculating (pun intended) ...
That's actually not the case. Rust people have long recognize that provenance is a thing and it actually matters. The pointers have a full set of provenance-aware APIs, and pointer-integer casts are discouraged.
Pointer comparison, for example, is explicitly defined as comparing address and ignore the provenance, so it's invalid for compiler to do GVN on pointers.
Implementation side, Rust is extremely conservative in optimizing anything that relates to the memory model currently and when pointers are involved, currently it's up to LLVM to do most of work. This is mostly due to the lack of full specification on the memory model, so may change in the future, but I am optimisitc overall.
Best,
Gary
Quoted 6 lines
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel free to ignore.
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Marco ElverQuoted 2 lines · Marco Elver
Quoted 1 line · Gary Guo
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 8 lines
rust/kernel/sync/atomic.rs +8
-0+ +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might
Thanks for bringing this up ;-)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
want restating in the documentation and/or commit log:
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.
Our C's atomic_long_read() is the same, that is it's like memory_order_consume instead memory_order_relaxed.
Quoted 8 lines
Quoted 3 lines
On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?
I wouldn't call it luck ;-) but we rely on the same thing that C has:
implementing by using READ_ONCE().
Quoted 28 lines
Quoted 7 lines
Quoted 3 lines
It is known to be brittle [1]. So the recommendation
above is unsound; well, it's as unsound as implementingREAD_ONCEwith a volatile load.Sorry, which part of this is unsound? You mean that the dependency ordering is actually lost when it's not supposed to be? Even so, it'll be only a problem on specific users that uses
Relaxedto carry ordering?Correct.
Quoted 2 lines
Users that use
Relaxedfor things that don't require any ordering would still be fine?Yes.
Quoted 5 lines
Quoted 5 lines
While Alice's series tried to expose
READ_ONCEas-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map toREAD_ONCE.Quoted 7 lines
Quoted 2 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Quoted 5 lines
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Which part of the implementation is different between C and Rust? We implement all Relaxed atomics in Rust the same way as C: using C's READ_ONCE() and WRITE_ONCE().
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Furthermore, LTO arm64 promotes
READ_ONCEto an acquire (seearch/arm64/include/asm/rwonce.h):
So are our C's atomic_read() and Rust's Atomic::load().
Quoted 38 lines
Quoted 16 lines
Quoted 12 lines
/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by aREAD_ONCE()invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
*READ_ONCE()definition with one that provides RCpc acquire semantics
* when building with LTO.
*/Quoted 3 lines
So for all intents and purposes, the only sound mapping when pairing
READ_ONCE()with an atomic load on the Rust side is to use Acquire ordering.Quoted 8 lines
LLVM handles address dependency much saner than GCC does. It for example won't turn address comparing equal into meaning that the pointer can be interchanged (as provenance won't match). Currently only address comparision to NULL or static can have effect on pointer provenance.
Although, last time I asked if we can rely on this for address dependency, I didn't get an affirmitive answer -- but I think in practice it won't be lost (as currently implemented).
There is no guarantee here, and this can change with every new release. In most cases where it matters it works today, but the compiler (specifically LLVM) does break dependencies even if rarely [1].
Quoted 1 line
Furthermore, Rust code currently does not participate in LTO.
LTO is not the problem, aggressive compiler optimizations (as discussed in [1]) are. And Rust, by virtue of its strong type system, appears to give the compiler a lot more leeway how it optimizes code. So I think the Rust side is in greater danger here than the C with LTO side. But I'm speculating (pun intended) ...
Quoted 5 lines
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel
Implementation-wise, READ_ONCE() is used the same as C for atomic_read(), so Rust and C are on the same boat.
Regards,
Boqun
Quoted 1 line
free to ignore.
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
On Tue, 20 Jan 2026 at 23:29, Boqun Feng <[email protected]> wrote:
[..]
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.Our C's
atomic_long_read()is the same, that is it's likememory_order_consumeinsteadmemory_order_relaxed.
I see; so it's Rust's Atomic::load(Relaxed) -> atomic_read() -> READ_ONCE (for most architectures).
Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?I wouldn't call it luck ;-) but we rely on the same thing that C has:
implementing by usingREAD_ONCE().
It's the age-old problem of wanting dependently-ordered atomics, but no compiler actually providing that. Implementing that via "volatile" is unsound, and always has been. But that's nothing new.
[...]
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Quoted 3 lines
Which part of the implementation is different between C and Rust? We implement all Relaxed atomics in Rust the same way as C: using C's
READ_ONCE()andWRITE_ONCE().
I should clarify: Even if the source of the load is "volatile" (through atomic_read() FFI) and carries through to Rust code, the compilers, despite sharing LLVM as the code generator, are different enough that making the assumption just because it works on the C side, it'll also work on the Rust side, appears to be a stretch for me. Gary claimed that Rust is more conservative -- in the absence of any guarantees, being able to quantify the problem would be nice though.
[..]
Quoted 8 lines
Quoted 5 lines
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel
Implementation-wise,
READ_ONCE()is used the same as C foratomic_read(), so Rust and C are on the same boat.
That's fair enough.
Longer term, I understand the need for claiming "it's all fine", but IMHO none of this is fine until compilers (both for C and Rust) promise the semantics that the LKMM wants. Nothing new per-se, the only new thing here that makes me anxious is that we do not understand the real impact of this lack of guarantee on Linux Rust code (the C side remains unclear, too, but has a lot more flight miles). Perhaps the work originally investigating broken dependency ordering in Clang, could be used to do a study on Rust in the kernel, too.
Alice Ryhl
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 64 lines · Marco Elver
On Tue, 20 Jan 2026 at 23:29, Boqun Feng <[email protected]> wrote:
[..]Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.Our C's
atomic_long_read()is the same, that is it's likememory_order_consumeinsteadmemory_order_relaxed.I see; so it's Rust's
Atomic::load(Relaxed)->atomic_read()->READ_ONCE(for most architectures).Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?I wouldn't call it luck ;-) but we rely on the same thing that C has:
implementing by usingREAD_ONCE().It's the age-old problem of wanting dependently-ordered atomics, but no compiler actually providing that. Implementing that via "volatile" is unsound, and always has been. But that's nothing new.
[...]
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Quoted 3 lines
Which part of the implementation is different between C and Rust? We implement all Relaxed atomics in Rust the same way as C: using C's
READ_ONCE()andWRITE_ONCE().I should clarify: Even if the source of the load is "volatile" (through
atomic_read()FFI) and carries through to Rust code, the compilers, despite sharing LLVM as the code generator, are different enough that making the assumption just because it works on the C side, it'll also work on the Rust side, appears to be a stretch for me. Gary claimed that Rust is more conservative -- in the absence of any guarantees, being able to quantify the problem would be nice though.[..]
Quoted 8 lines
Quoted 5 lines
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel
Implementation-wise,
READ_ONCE()is used the same as C foratomic_read(), so Rust and C are on the same boat.That's fair enough.
Quoted 8 lines
Longer term, I understand the need for claiming "it's all fine", but IMHO none of this is fine until compilers (both for C and Rust) promise the semantics that the LKMM wants. Nothing new per-se, the only new thing here that makes me anxious is that we do not understand the real impact of this lack of guarantee on Linux Rust code (the C side remains unclear, too, but has a lot more flight miles). Perhaps the work originally investigating broken dependency ordering in Clang, could be used to do a study on Rust in the kernel, too.
We did already have discussions with the Rust compiler folks about this topic, and they said that they are comfortable with Rust doing the exact same hacks as C since that should "work" in Rust for the same reasons it "works" in C.
Alice
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Marco ElverQuoted 44 lines · Marco Elver
On Tue, 20 Jan 2026 at 23:29, Boqun Feng <[email protected]> wrote:
[..]Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.Our C's
atomic_long_read()is the same, that is it's likememory_order_consumeinsteadmemory_order_relaxed.I see; so it's Rust's
Atomic::load(Relaxed)->atomic_read()->READ_ONCE(for most architectures).Quoted 10 lines
Quoted 7 lines
Quoted 2 lines
On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Alright, I missed this.
Is this actually enforced, or like the C side's use of "volatile", relies on luck?I wouldn't call it luck ;-) but we rely on the same thing that C has:
implementing by usingREAD_ONCE().It's the age-old problem of wanting dependently-ordered atomics, but no compiler actually providing that. Implementing that via "volatile" is unsound, and always has been. But that's nothing new.
[...]
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
I think this is a longstanding debate on whether we should actually depend on dependency ordering or just upgrade everything needs it to acquire. But this isn't really specific to Rust, and whatever is decided is global to the full LKMM.
Indeed, but the implementation on the C vs. Rust side differ substantially, so assuming it'll work on the Rust side just because "volatile" works more or less on the C side is a leap I wouldn't want to take in my codebase.
Quoted 3 lines
Which part of the implementation is different between C and Rust? We implement all Relaxed atomics in Rust the same way as C: using C's
READ_ONCE()andWRITE_ONCE().
Quoted 8 lines
I should clarify: Even if the source of the load is "volatile" (through
atomic_read()FFI) and carries through to Rust code, the compilers, despite sharing LLVM as the code generator, are different enough that making the assumption just because it works on the C side, it'll also work on the Rust side, appears to be a stretch for me. Gary claimed that Rust is more conservative -- in the absence of any guarantees, being able to quantify the problem would be nice though.
I don't disagree and share the similar concern as you do.
Quoted 12 lines
[..]
Quoted 8 lines
Quoted 5 lines
However, given "Relaxed" for the Rust side is already defined to "carry dependencies" then in isolation my original comment is moot and does not apply to this particular patch. At face value the promised semantics are ok, but the implementation (just like "volatile" for C) probably are not. But that appears to be beyond this patch, so feel
Implementation-wise,
READ_ONCE()is used the same as C foratomic_read(), so Rust and C are on the same boat.That's fair enough.
Quoted 8 lines
Longer term, I understand the need for claiming "it's all fine", but IMHO none of this is fine until compilers (both for C and Rust) promise the semantics that the LKMM wants. Nothing new per-se, the only new thing here that makes me anxious is that we do not understand the real impact of this lack of guarantee on Linux Rust code (the C side remains unclear, too, but has a lot more flight miles). Perhaps the work originally investigating broken dependency ordering in Clang, could be used to do a study on Rust in the kernel, too.
You mean this:
https://lpc.events/event/16/contributions/1174/
? If so, that'll be great! I believe if we could learn about how Rust compiler can mess up with dependency ordering, it'll be a very helpful resource to understand how we can work with Rust compiler to resolve it in a pratical way.
I believe that work was LLVM-based, so it should apply to Rust code as well, except that we may need to figure out what optmization the Rust front end would do at MIR -> LLVM IR time that could affect dependency orderings.
Regards,
Boqun
Alice Ryhl
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Gary GuoQuoted 40 lines · Gary Guo
Quoted 39 lines · Marco Elver
Quoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
Quoted 7 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.
Like in the other thread, I still think this is a mistake. Let's be explicit about intent and call things that they are.
https://lore.kernel.org/all/[email protected]/
Quoted 6 lines
If the idea is to add an explicit
Consumeordering on the Rust side to document the intent clearly, then I am actually somewhat in favour.This way, we can for example, map it to a
READ_ONCEin most cases, but we can also provide an option to upgrade such calls tosmp_load_acquirein certain cases when needed, e.g. LTO arm64.
It always maps to READ_ONCE(), no? It's just that on LTO arm64 the READ_ONCE() macro is implemented like smp_load_acquire().
Quoted 2 lines
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
On that point, my suggestion would be to use the standard LKMM naming such as rcu_dereference() or READ_ONCE().
I'm told that READ_ONCE() apparently has stronger guarantees than an atomic consume load, but I'm not clear on what they are.
Alice
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
On Wed, 21 Jan 2026 at 13:19, Alice Ryhl <[email protected]> wrote:
[...]
Quoted 9 lines
Quoted 2 lines
On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Like in the other thread, I still think this is a mistake. Let's be explicit about intent and call things that they are.
https://lore.kernel.org/all/[email protected]/Quoted 2 lines
If the idea is to add an explicit
Consumeordering on the Rust side to document the intent clearly, then I am actually somewhat in favour.
That'd be a mistake, too, as the semantics is not equivalent to "C++ consume" either, but arguably closer to it than "C++ relaxed" (I clearly got confused by the Linux Rust Relaxed != Normal Rust Relaxed).
It's also known that consume or any variant of it, has been deemed unimplementable, since the compiler would have to be able to reason about whole-program dependency chains.
Quoted 7 lines
Quoted 3 lines
This way, we can for example, map it to a
READ_ONCEin most cases, but we can also provide an option to upgrade such calls tosmp_load_acquirein certain cases when needed, e.g. LTO arm64.It always maps to
READ_ONCE(), no? It's just that on LTO arm64 theREAD_ONCE()macro is implemented likesmp_load_acquire().
Quoted 8 lines
Quoted 2 lines
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
On that point, my suggestion would be to use the standard LKMM naming such as
rcu_dereference()orREAD_ONCE().I'm told that
READ_ONCE()apparently has stronger guarantees than an atomic consume load, but I'm not clear on what they are.
It's also meant to enforce ordering through control-dependencies, such as:
if (READ_ONCE(x)) WRITE_ONCE(y, 1);
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
On Wed, Jan 21, 2026 at 01:36:04PM +0100, Marco Elver wrote:
[..]
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
On that point, my suggestion would be to use the standard LKMM naming such as
rcu_dereference()orREAD_ONCE().
I don't think we should confuse Rust users that READ_ONCE() has dependency orderings but atomc_load() doesn't. They are the same on the aspect. One of the reasons that I don't want to introduce rcu_dereference() and READ_ONCE() on Rust side is exactly this, they are the same at LKMM level, so should not be treated differently.
Quoted 7 lines
Quoted 3 lines
I'm told that
READ_ONCE()apparently has stronger guarantees than an atomic consume load, but I'm not clear on what they are.It's also meant to enforce ordering through control-dependencies, such as:
if (READ_ONCE(x)) WRITE_ONCE(y, 1);
Note that it also applies to atomic_read() and atomic_set() as well.
Regards,
Boqun
Alice Ryhl
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 6 lines · Boqun Feng
On Wed, Jan 21, 2026 at 01:36:04PM +0100, Marco Elver wrote:
[..]Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
On that point, my suggestion would be to use the standard LKMM naming such as
rcu_dereference()orREAD_ONCE().I don't think we should confuse Rust users that
READ_ONCE()has dependency orderings butatomc_load()doesn't. They are the same on the aspect. One of the reasons that I don't want to introducercu_dereference()andREAD_ONCE()on Rust side is exactly this, they are the same at LKMM level, so should not be treated differently.
That's okay with me - I just don't think "relaxed" is a good name for atomic_load() if that's the case.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
I'm told that
READ_ONCE()apparently has stronger guarantees than an atomic consume load, but I'm not clear on what they are.It's also meant to enforce ordering through control-dependencies, such as:
if (READ_ONCE(x)) WRITE_ONCE(y, 1);Note that it also applies to
atomic_read()andatomic_set()as well.
Just to be completely clear ... am I to understand this that READ_ONCE() and the LKMM's atomic_load() *are* the exact same thing? Because if so, then this was really confusing:
Quoted 6 lines
my argument was not about naming, it's
aboutREAD_ONCE()being more powerful than atomic load (no, not because of address dependency, they are the same on that, it's because of the behaviors of them regarding a current access on the same memory location)
https://lore.kernel.org/all/[email protected]/
Are they the *exact* same thing or not? Do you mean that they are the same under LKMM, but different under some other context?
Alice
Re: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
Quoted 3 lines · Alice Ryhl
Quoted 2 lines · Boqun Feng
On Wed, Jan 21, 2026 at 01:36:04PM +0100, Marco Elver wrote:
[..]
[...]
Quoted 14 lines
Quoted 2 lines
Note that it also applies to
atomic_read()andatomic_set()as well.Just to be completely clear ... am I to understand this that
READ_ONCE()and the LKMM'satomic_load()*are* the exact same thing? Because if so, then this was really confusing:Quoted 6 lines
my argument was not about naming, it's
aboutREAD_ONCE()being more powerful than atomic load (no, not because of address dependency, they are the same on that, it's because of the behaviors of them regarding a current access on the same memory location)
https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Are they the *exact* same thing or not? Do you mean that they are the same under LKMM, but different under some other context?
Right, they are the same thing under LKMM when used for inter-thread synchronization when they are atomic. But when READ_ONCE() (and __READ_ONCE()) used on types that are larger than machine word size, they are not guaranteed to be atomic, hence semantics-wise READ_ONCE() firstly guarantees "once" (volatile and no data race with WRITE_ONCE()) and then on certain types, it's atomic as well.
* In the case that we need atomicity, we should just use atomic_load().
* In the case that we don't need atomicity and no data race, we can just use read_volatile().
* In the case that there is a non-atomic concurrent write, that suggests we have a bug in C code or we simply should have some other synchronization. Or if we believe the conncurrent write has at last per-byte atomicity, then we can have READ_ONCE()-like function that returns a MaybeUninit to bear the possible tearing.
Regards,
Boqun
Quoted 2 lines
Alice
Gary Guo
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Alice RyhlQuoted 56 lines · Alice Ryhl
Quoted 40 lines · Gary Guo
Quoted 39 lines · Marco Elver
Quoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
Quoted 7 lines
Quoted 4 lines
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken.On the Rust-side documentation we mentioned that
Relaxedalways preserve dependency ordering, so yes, it is closer toconsumein the C11 model.Like in the other thread, I still think this is a mistake. Let's be explicit about intent and call things that they are.
https://lore.kernel.org/all/[email protected]/Quoted 3 lines
If the idea is to add an explicit
Consumeordering on the Rust side to document the intent clearly, then I am actually somewhat in favour.
Quoted 6 lines
Quoted 3 lines
This way, we can for example, map it to a
READ_ONCEin most cases, but we can also provide an option to upgrade such calls tosmp_load_acquirein certain cases when needed, e.g. LTO arm64.It always maps to
READ_ONCE(), no? It's just that on LTO arm64 theREAD_ONCE()macro is implemented likesmp_load_acquire().
If we split out two separate orderings then we can make things that don't need dependency ordering not be upgraded to smp_load_acquire and still be implemented using volatile read.
Quoted 9 lines
Quoted 3 lines
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
On that point, my suggestion would be to use the standard LKMM naming such as
rcu_dereference()orREAD_ONCE().I'm told that
READ_ONCE()apparently has stronger guarantees than an atomic consume load, but I'm not clear on what they are.
The semantic is different for a 64-bit read on 32-bit platforms; our Atomic::from_ptr().load() will be atomic (backed by atomic64 where READ_ONCE will tear) -- so if you actually want a atomicity then READ_ONCE can be a pitfall.
On the other hand, if you don't want atomicity (and dependency ordering), e.g. just doing MMIO read / reading DMA allocation where you only need the "once" semantics of READ_ONCE, then READ_ONCE provides you with too much guarantees that you don't care about.
We'd better not to mix them together, because confusion lead to bugs. I have described such an example in the HrTimer expires patch where the code assumes READ_ONCE() to be atomic and it actually could break in 32-bit systems, but probably nobody noticed because 32-bit systems using DRM is rare and the race condition is hard to trigger.
My suggestion is just use things with atomic in its name for anything that requires atomicity or ordering, and UB-free volatile access to read_volatile().
Best,
Gary
Gary Guo
maintainerRe: [PATCH 2/2] rust: sync: atomic: Add atomic operation helpers over raw pointers
In reply to Marco ElverQuoted 56 lines · Marco Elver
Quoted 24 lines · Boqun Feng
In order to synchronize with C or external, atomic operations over raw pointers, althought previously there is always an
Atomic::from_ptr()to provide a&Atomic<T>. However it's more convenient to have helpers that directly perform atomic operations on raw pointers. Hence a few are added, which are basically aAtomic::from_ptr().op()wrapper.Note: for naming, since
atomic_xchg()andatomic_cmpxchg()has a conflict naming to 32bit C atomic xchg/cmpxchg, hence they are just named asxchg()andcmpxchg(). Foratomic_load()andatomic_store(), their 32bit C counterparts areatomic_read()andatomic_set(), so keep theatomic_prefix.Signed-off-by Boqun Feng <[email protected]>--- rust/kernel/sync/atomic.rs | 104 +++++++++++++++++++++++++++ rust/kernel/sync/atomic/predefine.rs | 46 ++++++++++++ 2 files changed, 150 insertions(+)rust/kernel/sync/atomic.rs +0
-0@@ -611,3 +611,107 @@ pub fn cmpxchg<Ordering: ordering::Ordering>( }rust/kernel/sync/atomic.rs pub fn cmpxchg<Ordering: ordering::Ordering>( · line 621
Quoted 10 lines
rust/kernel/sync/atomic.rs +8
-0} } + +/// Atomic load over raw pointers. +/// +/// This function provides a short-cut of `Atomic::from_ptr().load(..)`, and can be used to work +/// with C side on synchronizations: +/// +/// - `atomic_load(.., Relaxed)` maps to `READ_ONCE()` when using for inter-thread communication. +/// - `atomic_load(.., Acquire)` maps to `smp_load_acquire()`. I'm late to the party and may have missed some discussion, but it might want restating in the documentation and/or commit log:
READ_ONCEis meant to be a dependency-ordering primitive, i.e. be more likememory_order_consumethan it ismemory_order_relaxed. This has, to the best of my knowledge, not changed; otherwise lots of kernel code would be broken. It is known to be brittle [1]. So the recommendation above is unsound; well, it's as unsound as implementingREAD_ONCEwith a volatile load.While Alice's series tried to expose
READ_ONCEas-is to the Rust side (via volatile), so that Rust inherits the exact same semantics (including its implementation flaw), the recommendation above is doubling down on the unsoundness by proposing Relaxed to map toREAD_ONCE.Furthermore, LTO arm64 promotes
READ_ONCEto an acquire (seearch/arm64/include/asm/rwonce.h):
Quoted 10 lines
/*
* When building with LTO, there is an increased risk of the compiler
* converting an address dependency headed by aREAD_ONCE()invocation
* into a control dependency and consequently allowing for harmful
* reordering by the CPU.
*
* Ensure that such transformations are harmless by overriding the generic
*READ_ONCE()definition with one that provides RCpc acquire semantics
* when building with LTO.
*/
Just to add on this part:
If the idea is to add an explicit Consume ordering on the Rust side to document the intent clearly, then I am actually somewhat in favour.
This way, we can for example, map it to a READ_ONCE in most cases, but we can also provide an option to upgrade such calls to smp_load_acquire in certain cases when needed, e.g. LTO arm64.
However this will mean that Rust code will have one more ordering than the C API, so I am keen on knowing how Boqun, Paul, Peter and others think about this.
Quoted 3 lines
So for all intents and purposes, the only sound mapping when pairing
READ_ONCE()with an atomic load on the Rust side is to use Acquire ordering.
Forget to reply to this part in my other email, but this is definitely not true. There're use cases for a fully relaxed load on pointer too (in hazard pointer impl, a few READ_ONCE need depedendency ordering, a few doesn't), not to mention that this API that Boqun is introducing works for just integers, too.
Best,
Gary