[PATCH v2 1/2] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW

Boqun Feng posted 2 patches 2 weeks, 4 days ago
[PATCH v2 1/2] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW
Posted by Boqun Feng 2 weeks, 4 days ago
Currently, since all the architectures that support Rust all have
CONFIG_ARCH_SUPPORTS_ATOMIC_RMW selected, the helpers of atomic
load/store on i8 and i16 relies on CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y.
It's generally fine since most of architectures support that.

The plan for CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architectures is adding
their (probably lock-based) atomic load/store for i8 and i16 as their
atomic_{read,set}() and atomic64_{read,set}() counterpart when they
plans to support Rust.

Hence use a statis_assert!() to check this and remind the future us the
need of the helpers. This is more clear than the #[cfg] on impl blocks
of i8 and i16.

Suggested-by: Dirk Behme <dirk.behme@gmail.com>
Suggested-by: Benno Lossin <lossin@kernel.org>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
---
 rust/kernel/sync/atomic/internal.rs | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
index 0dac58bca2b3..ef516bcb02ee 100644
--- a/rust/kernel/sync/atomic/internal.rs
+++ b/rust/kernel/sync/atomic/internal.rs
@@ -37,16 +37,23 @@ pub trait AtomicImpl: Sized + Send + Copy + private::Sealed {
     type Delta;
 }
 
-// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
-// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
-#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
+// The current helpers of load/store of atomic `i8` and `i16` use `{WRITE,READ}_ONCE()` hence the
+// atomicity is only guaranteed against read-modify-write operations if the architecture supports
+// native atomic RmW.
+//
+// In the future when a CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architecture plans to support Rust, the
+// load/store helpers that guarantee atomicity against RmW operations (usually via a lock) need to
+// be added.
+crate::static_assert!(
+    cfg!(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW),
+    "The current implementation of atomic i8/i16/ptr relies on the architecure being \
+    ARCH_SUPPORTS_ATOMIC_RMW"
+);
+
 impl AtomicImpl for i8 {
     type Delta = Self;
 }
 
-// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
-// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
-#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
 impl AtomicImpl for i16 {
     type Delta = Self;
 }
-- 
2.51.0
Re: [PATCH v2 1/2] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW
Posted by Gary Guo 2 weeks, 4 days ago
On Tue Jan 20, 2026 at 2:05 PM GMT, Boqun Feng wrote:
> Currently, since all the architectures that support Rust all have
> CONFIG_ARCH_SUPPORTS_ATOMIC_RMW selected, the helpers of atomic
> load/store on i8 and i16 relies on CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y.
> It's generally fine since most of architectures support that.
>
> The plan for CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architectures is adding
> their (probably lock-based) atomic load/store for i8 and i16 as their
> atomic_{read,set}() and atomic64_{read,set}() counterpart when they
> plans to support Rust.
>
> Hence use a statis_assert!() to check this and remind the future us the
> need of the helpers. This is more clear than the #[cfg] on impl blocks
> of i8 and i16.
>
> Suggested-by: Dirk Behme <dirk.behme@gmail.com>
> Suggested-by: Benno Lossin <lossin@kernel.org>
> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/sync/atomic/internal.rs | 19 +++++++++++++------
>  1 file changed, 13 insertions(+), 6 deletions(-)
>
> diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
> index 0dac58bca2b3..ef516bcb02ee 100644
> --- a/rust/kernel/sync/atomic/internal.rs
> +++ b/rust/kernel/sync/atomic/internal.rs
> @@ -37,16 +37,23 @@ pub trait AtomicImpl: Sized + Send + Copy + private::Sealed {
>      type Delta;
>  }
>  
> -// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
> -// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
> -#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
> +// The current helpers of load/store of atomic `i8` and `i16` use `{WRITE,READ}_ONCE()` hence the
> +// atomicity is only guaranteed against read-modify-write operations if the architecture supports
> +// native atomic RmW.
> +//
> +// In the future when a CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architecture plans to support Rust, the
> +// load/store helpers that guarantee atomicity against RmW operations (usually via a lock) need to
> +// be added.
> +crate::static_assert!(
> +    cfg!(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW),
> +    "The current implementation of atomic i8/i16/ptr relies on the architecure being \
> +    ARCH_SUPPORTS_ATOMIC_RMW"

The printed string when assertion fails will have 5 spaces between "being" and
"ARCH", although it probably doesn't matter..

Best,
Gary

> +);
> +
>  impl AtomicImpl for i8 {
>      type Delta = Self;
>  }
>  
> -// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
> -// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
> -#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
>  impl AtomicImpl for i16 {
>      type Delta = Self;
>  }
Re: [PATCH v2 1/2] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW
Posted by Boqun Feng 2 weeks, 4 days ago
On Tue, Jan 20, 2026 at 04:54:51PM +0000, Gary Guo wrote:
> On Tue Jan 20, 2026 at 2:05 PM GMT, Boqun Feng wrote:
> > Currently, since all the architectures that support Rust all have
> > CONFIG_ARCH_SUPPORTS_ATOMIC_RMW selected, the helpers of atomic
> > load/store on i8 and i16 relies on CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y.
> > It's generally fine since most of architectures support that.
> >
> > The plan for CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architectures is adding
> > their (probably lock-based) atomic load/store for i8 and i16 as their
> > atomic_{read,set}() and atomic64_{read,set}() counterpart when they
> > plans to support Rust.
> >
> > Hence use a statis_assert!() to check this and remind the future us the
> > need of the helpers. This is more clear than the #[cfg] on impl blocks
> > of i8 and i16.
> >
> > Suggested-by: Dirk Behme <dirk.behme@gmail.com>
> > Suggested-by: Benno Lossin <lossin@kernel.org>
> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
> 
> Reviewed-by: Gary Guo <gary@garyguo.net>
> 

Thanks!

> > ---
> >  rust/kernel/sync/atomic/internal.rs | 19 +++++++++++++------
> >  1 file changed, 13 insertions(+), 6 deletions(-)
> >
> > diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
> > index 0dac58bca2b3..ef516bcb02ee 100644
> > --- a/rust/kernel/sync/atomic/internal.rs
> > +++ b/rust/kernel/sync/atomic/internal.rs
> > @@ -37,16 +37,23 @@ pub trait AtomicImpl: Sized + Send + Copy + private::Sealed {
> >      type Delta;
> >  }
> >  
> > -// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
> > -// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
> > -#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
> > +// The current helpers of load/store of atomic `i8` and `i16` use `{WRITE,READ}_ONCE()` hence the
> > +// atomicity is only guaranteed against read-modify-write operations if the architecture supports
> > +// native atomic RmW.
> > +//
> > +// In the future when a CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architecture plans to support Rust, the
> > +// load/store helpers that guarantee atomicity against RmW operations (usually via a lock) need to
> > +// be added.
> > +crate::static_assert!(
> > +    cfg!(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW),
> > +    "The current implementation of atomic i8/i16/ptr relies on the architecure being \
> > +    ARCH_SUPPORTS_ATOMIC_RMW"
> 
> The printed string when assertion fails will have 5 spaces between "being" and
> "ARCH", although it probably doesn't matter..
> 

Are you sure? My test result shows:

ERROR:root:error[E0080]: evaluation panicked: The current implementation of atomic i8/i16/ptr relies on the architecure being ARCH_SUPPORTS_ATOMIC_RMW

similar is the following playground example:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5dd0098247503be792bc35cda8f2630f

Regards,
Boqun

> Best,
> Gary
> 
> > +);
> > +
> >  impl AtomicImpl for i8 {
> >      type Delta = Self;
> >  }
> >  
> > -// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
> > -// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
> > -#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
> >  impl AtomicImpl for i16 {
> >      type Delta = Self;
> >  }
>
Re: [PATCH v2 1/2] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW
Posted by Gary Guo 2 weeks, 4 days ago
On Tue Jan 20, 2026 at 9:03 PM GMT, Boqun Feng wrote:
> On Tue, Jan 20, 2026 at 04:54:51PM +0000, Gary Guo wrote:
>> On Tue Jan 20, 2026 at 2:05 PM GMT, Boqun Feng wrote:
>> > Currently, since all the architectures that support Rust all have
>> > CONFIG_ARCH_SUPPORTS_ATOMIC_RMW selected, the helpers of atomic
>> > load/store on i8 and i16 relies on CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=y.
>> > It's generally fine since most of architectures support that.
>> >
>> > The plan for CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architectures is adding
>> > their (probably lock-based) atomic load/store for i8 and i16 as their
>> > atomic_{read,set}() and atomic64_{read,set}() counterpart when they
>> > plans to support Rust.
>> >
>> > Hence use a statis_assert!() to check this and remind the future us the
>> > need of the helpers. This is more clear than the #[cfg] on impl blocks
>> > of i8 and i16.
>> >
>> > Suggested-by: Dirk Behme <dirk.behme@gmail.com>
>> > Suggested-by: Benno Lossin <lossin@kernel.org>
>> > Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
>> 
>> Reviewed-by: Gary Guo <gary@garyguo.net>
>> 
>
> Thanks!
>
>> > ---
>> >  rust/kernel/sync/atomic/internal.rs | 19 +++++++++++++------
>> >  1 file changed, 13 insertions(+), 6 deletions(-)
>> >
>> > diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
>> > index 0dac58bca2b3..ef516bcb02ee 100644
>> > --- a/rust/kernel/sync/atomic/internal.rs
>> > +++ b/rust/kernel/sync/atomic/internal.rs
>> > @@ -37,16 +37,23 @@ pub trait AtomicImpl: Sized + Send + Copy + private::Sealed {
>> >      type Delta;
>> >  }
>> >  
>> > -// The current helpers of load/store uses `{WRITE,READ}_ONCE()` hence the atomicity is only
>> > -// guaranteed against read-modify-write operations if the architecture supports native atomic RmW.
>> > -#[cfg(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW)]
>> > +// The current helpers of load/store of atomic `i8` and `i16` use `{WRITE,READ}_ONCE()` hence the
>> > +// atomicity is only guaranteed against read-modify-write operations if the architecture supports
>> > +// native atomic RmW.
>> > +//
>> > +// In the future when a CONFIG_ARCH_SUPPORTS_ATOMIC_RMW=n architecture plans to support Rust, the
>> > +// load/store helpers that guarantee atomicity against RmW operations (usually via a lock) need to
>> > +// be added.
>> > +crate::static_assert!(
>> > +    cfg!(CONFIG_ARCH_SUPPORTS_ATOMIC_RMW),
>> > +    "The current implementation of atomic i8/i16/ptr relies on the architecure being \
>> > +    ARCH_SUPPORTS_ATOMIC_RMW"
>> 
>> The printed string when assertion fails will have 5 spaces between "being" and
>> "ARCH", although it probably doesn't matter..
>> 
>
> Are you sure? My test result shows:
>
> ERROR:root:error[E0080]: evaluation panicked: The current implementation of atomic i8/i16/ptr relies on the architecure being ARCH_SUPPORTS_ATOMIC_RMW
>
> similar is the following playground example:
>
> https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5dd0098247503be792bc35cda8f2630f

TIL! I thought Rust and C has the same behaviour with this respect.

I have been using `indoc!` to strip the additional whitespaces, but I suppose
it's only for multiline strings but no line continuation.

Best,
Gary