* [PATCH v4 0/2] rust: sync: atomic flag helpers
@ 2026-01-15 2:12 FUJITA Tomonori
2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: FUJITA Tomonori @ 2026-01-15 2:12 UTC (permalink / raw)
To: boqun.feng, ojeda, peterz, will
Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin,
mark.rutland, tmgross, rust-for-linux
This series improves the Rust atomic-boolean support.
The first patch adds a Flag enum (Clear/Set) and implements AtomicType
for it, so Atomic<Flag> can be used when RMW operations (xchg/cmpxchg)
are needed and byte-sized RMWs may be inefficient.
The second patch adds AtomicFlag as a thin wrapper around Atomic<Flag>
to provide a simple bool-based load/store/xchg API.
v4:
- Make Flag backing type architecture-dependent
- Make AtomicFlag::new inline
- Add Flag::new and modify AtomicFlag::new use it
v3: https://lore.kernel.org/rust-for-linux/20260111050558.3147975-1-fujita.tomonori@gmail.com/
- Add AtomicFlag struct provding a simple bool API
v2: https://lore.kernel.org/rust-for-linux/20260108125005.2945800-1-fujita.tomonori@gmail.com/
- Update the description
- Add bidirectional From conversions between Flag and bool
- Use intra-doc links
- Fix level header for Examples
- Fix a typo
- Add a newline after the `use`s block in examples
v1: https://lore.kernel.org/rust-for-linux/20260101102718.2073674-1-fujita.tomonori@gmail.com/
FUJITA Tomonori (2):
rust: sync: atomic: Add performance-optimal-integer-backed Flag for
atomic booleans
rust: sync: atomic: Add AtomicFlag bool wrapper for easier use
rust/kernel/sync/atomic.rs | 114 +++++++++++++++++++++++++++++++++++++
1 file changed, 114 insertions(+)
base-commit: 7bd94324bf04ca1161d94fc193adf782b0cfc0a9
--
2.43.0
^ permalink raw reply [flat|nested] 11+ messages in thread* [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-15 2:12 [PATCH v4 0/2] rust: sync: atomic flag helpers FUJITA Tomonori @ 2026-01-15 2:12 ` FUJITA Tomonori 2026-01-15 13:22 ` Gary Guo 2026-01-19 1:22 ` Boqun Feng 2026-01-15 2:12 ` [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori 2026-01-15 6:41 ` [PATCH v4 0/2] rust: sync: atomic flag helpers Alice Ryhl 2 siblings, 2 replies; 11+ messages in thread From: FUJITA Tomonori @ 2026-01-15 2:12 UTC (permalink / raw) To: boqun.feng, ojeda, peterz, will Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux Add a new Flag enum (Clear/Set) and implement AtomicType for it, so users can use Atomic<Flag> for boolean flags. The backing integer type is an implementation detail; it may vary by architecture and change in the future. Document when Atomic<Flag> is generally preferable to Atomic<bool>: in particular, when RMW operations such as xchg()/cmpxchg() may be used and minimizing memory usage is not the top priority. On some architectures without byte-sized RMW instructions, Atomic<bool> can be slower for RMW operations. Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> --- rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index ca9cab77abf0..58f57903460f 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -566,3 +566,74 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) unsafe { from_repr(ret) } } } + +/// An atomic flag type intended to be backed by a performance-optimal integer type. +/// +/// The backing integer type is an implementation detail; it may vary by architecture and change +/// in the future. +/// +/// [`Atomic<Flag>`] is generally preferable to [`Atomic<bool>`] when you need read-modify-write +/// (RMW) operations (e.g. [`Atomic::xchg()`]/[`Atomic::cmpxchg()`]) or when [`Atomic<bool>`] does +/// not save memory due to padding. On some architectures that do not support byte-sized atomic +/// RMW operations, RMW operations on [`Atomic<bool>`] are slower. +/// +/// If you only use [`Atomic::load()`]/[`Atomic::store()`], either [`Atomic<bool>`] or +/// [`Atomic<Flag>`] is fine. +/// +/// # Examples +/// +/// ``` +/// use kernel::sync::atomic::{Atomic, Flag, Relaxed}; +/// +/// let flag = Atomic::new(Flag::Clear); +/// assert_eq!(Flag::Clear, flag.load(Relaxed)); +/// flag.store(Flag::Set, Relaxed); +/// assert_eq!(Flag::Set, flag.load(Relaxed)); +/// ``` +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))] +#[cfg_attr( + not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)), + repr(i32) +)] +pub enum Flag { + /// The flag is clear. + Clear = 0, + /// The flag is set. + Set = 1, +} + +// SAFETY: `Flag` and `FlagRepr` have the same size and alignment, and `Flag` is +// round-trip transmutable to the selected representation (`i8` or `i32`). +unsafe impl AtomicType for Flag { + #[cfg(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64))] + type Repr = i8; + #[cfg(not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)))] + type Repr = i32; +} + +impl Flag { + /// Creates a new [`Flag`] from a [`bool`]. + #[inline(always)] + pub const fn new(b: bool) -> Self { + if b { + Flag::Set + } else { + Flag::Clear + } + } +} + +impl From<Flag> for bool { + #[inline(always)] + fn from(f: Flag) -> Self { + f == Flag::Set + } +} + +impl From<bool> for Flag { + #[inline(always)] + fn from(b: bool) -> Self { + Flag::new(b) + } +} -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori @ 2026-01-15 13:22 ` Gary Guo 2026-01-19 1:22 ` Boqun Feng 1 sibling, 0 replies; 11+ messages in thread From: Gary Guo @ 2026-01-15 13:22 UTC (permalink / raw) To: FUJITA Tomonori, boqun.feng, ojeda, peterz, will Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Thu Jan 15, 2026 at 2:12 AM GMT, FUJITA Tomonori wrote: > Add a new Flag enum (Clear/Set) and implement AtomicType for it, so > users can use Atomic<Flag> for boolean flags. > > The backing integer type is an implementation detail; it may vary by > architecture and change in the future. > > Document when Atomic<Flag> is generally preferable to Atomic<bool>: in > particular, when RMW operations such as xchg()/cmpxchg() may be used > and minimizing memory usage is not the top priority. On some > architectures without byte-sized RMW instructions, Atomic<bool> can be > slower for RMW operations. > > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> > --- > rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 71 insertions(+) ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori 2026-01-15 13:22 ` Gary Guo @ 2026-01-19 1:22 ` Boqun Feng 2026-01-19 3:10 ` FUJITA Tomonori 1 sibling, 1 reply; 11+ messages in thread From: Boqun Feng @ 2026-01-19 1:22 UTC (permalink / raw) To: FUJITA Tomonori Cc: ojeda, peterz, will, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Thu, Jan 15, 2026 at 11:12:29AM +0900, FUJITA Tomonori wrote: > Add a new Flag enum (Clear/Set) and implement AtomicType for it, so > users can use Atomic<Flag> for boolean flags. > > The backing integer type is an implementation detail; it may vary by > architecture and change in the future. > > Document when Atomic<Flag> is generally preferable to Atomic<bool>: in > particular, when RMW operations such as xchg()/cmpxchg() may be used > and minimizing memory usage is not the top priority. On some > architectures without byte-sized RMW instructions, Atomic<bool> can be > slower for RMW operations. > > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > --- > rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 71 insertions(+) > > diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs > index ca9cab77abf0..58f57903460f 100644 > --- a/rust/kernel/sync/atomic.rs > +++ b/rust/kernel/sync/atomic.rs I prefer we can move `Flag` into atomic/predefine.rs, I queued in rust-sync with the following version, please let me know whether it works, thank you all! ------------------>8 Subject: [PATCH] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans Add a new Flag enum (Clear/Set) and implement AtomicType for it, so users can use Atomic<Flag> for boolean flags. The backing integer type is an implementation detail; it may vary by architecture and change in the future. Document when Atomic<Flag> is generally preferable to Atomic<bool>: in particular, when RMW operations such as xchg()/cmpxchg() may be used and minimizing memory usage is not the top priority. On some architectures without byte-sized RMW instructions, Atomic<bool> can be slower for RMW operations. [boqun: Move Flag into atomic/predefine.rs] Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://patch.msgid.link/20260115021230.3297420-2-fujita.tomonori@gmail.com --- rust/kernel/sync/atomic.rs | 1 + rust/kernel/sync/atomic/predefine.rs | 71 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 4aebeacb961a..915c625a8bc0 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -22,6 +22,7 @@ pub use internal::AtomicImpl; pub use ordering::{Acquire, Full, Relaxed, Release}; +pub use predefine::Flag; pub(crate) use internal::{AtomicArithmeticOps, AtomicBasicOps, AtomicExchangeOps}; diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs index 42067c6a266c..261e6d7f5341 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -122,6 +122,77 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { } } +/// An atomic flag type intended to be backed by a performance-optimal integer type. +/// +/// The backing integer type is an implementation detail; it may vary by architecture and change +/// in the future. +/// +/// [`Atomic<Flag>`] is generally preferable to [`Atomic<bool>`] when you need read-modify-write +/// (RMW) operations (e.g. [`Atomic::xchg()`]/[`Atomic::cmpxchg()`]) or when [`Atomic<bool>`] does +/// not save memory due to padding. On some architectures that do not support byte-sized atomic +/// RMW operations, RMW operations on [`Atomic<bool>`] are slower. +/// +/// If you only use [`Atomic::load()`]/[`Atomic::store()`], either [`Atomic<bool>`] or +/// [`Atomic<Flag>`] is fine. +/// +/// # Examples +/// +/// ``` +/// use kernel::sync::atomic::{Atomic, Flag, Relaxed}; +/// +/// let flag = Atomic::new(Flag::Clear); +/// assert_eq!(Flag::Clear, flag.load(Relaxed)); +/// flag.store(Flag::Set, Relaxed); +/// assert_eq!(Flag::Set, flag.load(Relaxed)); +/// ``` +#[derive(Clone, Copy, PartialEq, Eq)] +#[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))] +#[cfg_attr( + not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)), + repr(i32) +)] +pub enum Flag { + /// The flag is clear. + Clear = 0, + /// The flag is set. + Set = 1, +} + +// SAFETY: `Flag` and `Repr` have the same size and alignment, and `Flag` is round-trip +// transmutable to the selected representation (`i8` or `i32`). +unsafe impl super::AtomicType for Flag { + #[cfg(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64))] + type Repr = i8; + #[cfg(not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)))] + type Repr = i32; +} + +impl Flag { + /// Creates a new [`Flag`] from a [`bool`]. + #[inline(always)] + pub const fn new(b: bool) -> Self { + if b { + Flag::Set + } else { + Flag::Clear + } + } +} + +impl From<Flag> for bool { + #[inline(always)] + fn from(f: Flag) -> Self { + f == Flag::Set + } +} + +impl From<bool> for Flag { + #[inline(always)] + fn from(b: bool) -> Self { + Flag::new(b) + } +} + use crate::macros::kunit_tests; #[kunit_tests(rust_atomics)] -- 2.51.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-19 1:22 ` Boqun Feng @ 2026-01-19 3:10 ` FUJITA Tomonori 2026-01-19 23:08 ` FUJITA Tomonori 0 siblings, 1 reply; 11+ messages in thread From: FUJITA Tomonori @ 2026-01-19 3:10 UTC (permalink / raw) To: boqun.feng Cc: fujita.tomonori, ojeda, peterz, will, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Mon, 19 Jan 2026 09:22:10 +0800 Boqun Feng <boqun.feng@gmail.com> wrote: > On Thu, Jan 15, 2026 at 11:12:29AM +0900, FUJITA Tomonori wrote: >> Add a new Flag enum (Clear/Set) and implement AtomicType for it, so >> users can use Atomic<Flag> for boolean flags. >> >> The backing integer type is an implementation detail; it may vary by >> architecture and change in the future. >> >> Document when Atomic<Flag> is generally preferable to Atomic<bool>: in >> particular, when RMW operations such as xchg()/cmpxchg() may be used >> and minimizing memory usage is not the top priority. On some >> architectures without byte-sized RMW instructions, Atomic<bool> can be >> slower for RMW operations. >> >> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> >> --- >> rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ >> 1 file changed, 71 insertions(+) >> >> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs >> index ca9cab77abf0..58f57903460f 100644 >> --- a/rust/kernel/sync/atomic.rs >> +++ b/rust/kernel/sync/atomic.rs > > I prefer we can move `Flag` into atomic/predefine.rs, I queued in > rust-sync with the following version, please let me know whether it > works, thank you all! Works for me, thanks! ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-19 3:10 ` FUJITA Tomonori @ 2026-01-19 23:08 ` FUJITA Tomonori 2026-01-20 2:56 ` Boqun Feng 0 siblings, 1 reply; 11+ messages in thread From: FUJITA Tomonori @ 2026-01-19 23:08 UTC (permalink / raw) To: boqun.feng Cc: ojeda, peterz, will, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Mon, 19 Jan 2026 12:10:25 +0900 (JST) FUJITA Tomonori <fujita.tomonori@gmail.com> wrote: > On Mon, 19 Jan 2026 09:22:10 +0800 > Boqun Feng <boqun.feng@gmail.com> wrote: > >> On Thu, Jan 15, 2026 at 11:12:29AM +0900, FUJITA Tomonori wrote: >>> Add a new Flag enum (Clear/Set) and implement AtomicType for it, so >>> users can use Atomic<Flag> for boolean flags. >>> >>> The backing integer type is an implementation detail; it may vary by >>> architecture and change in the future. >>> >>> Document when Atomic<Flag> is generally preferable to Atomic<bool>: in >>> particular, when RMW operations such as xchg()/cmpxchg() may be used >>> and minimizing memory usage is not the top priority. On some >>> architectures without byte-sized RMW instructions, Atomic<bool> can be >>> slower for RMW operations. >>> >>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> >>> --- >>> rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ >>> 1 file changed, 71 insertions(+) >>> >>> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs >>> index ca9cab77abf0..58f57903460f 100644 >>> --- a/rust/kernel/sync/atomic.rs >>> +++ b/rust/kernel/sync/atomic.rs >> >> I prefer we can move `Flag` into atomic/predefine.rs, I queued in >> rust-sync with the following version, please let me know whether it >> works, thank you all! > > Works for me, thanks! If we like to keep the intra-doc-links, a workaround like the followings seems to be necessary. diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs index f6c7538cb47b..92e312c06595 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -156,6 +156,13 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { /// flag.store(Flag::Set, Relaxed); /// assert_eq!(Flag::Set, flag.load(Relaxed)); /// ``` +/// +/// [`Atomic<Flag>`]: kernel::sync::atomic::Atomic +/// [`Atomic<bool>`]: kernel::sync::atomic::Atomic +/// [`Atomic::xchg()`]: kernel::sync::atomic::Atomic::xchg +/// [`Atomic::cmpxchg()`]: kernel::sync::atomic::Atomic::cmpxchg +/// [`Atomic::load()`]: kernel::sync::atomic::Atomic::load +/// [`Atomic::store()`]: kernel::sync::atomic::Atomic::store #[derive(Clone, Copy, PartialEq, Eq)] #[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))] #[cfg_attr( ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans 2026-01-19 23:08 ` FUJITA Tomonori @ 2026-01-20 2:56 ` Boqun Feng 0 siblings, 0 replies; 11+ messages in thread From: Boqun Feng @ 2026-01-20 2:56 UTC (permalink / raw) To: FUJITA Tomonori Cc: ojeda, peterz, will, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Tue, Jan 20, 2026 at 08:08:51AM +0900, FUJITA Tomonori wrote: > On Mon, 19 Jan 2026 12:10:25 +0900 (JST) > FUJITA Tomonori <fujita.tomonori@gmail.com> wrote: > > > On Mon, 19 Jan 2026 09:22:10 +0800 > > Boqun Feng <boqun.feng@gmail.com> wrote: > > > >> On Thu, Jan 15, 2026 at 11:12:29AM +0900, FUJITA Tomonori wrote: > >>> Add a new Flag enum (Clear/Set) and implement AtomicType for it, so > >>> users can use Atomic<Flag> for boolean flags. > >>> > >>> The backing integer type is an implementation detail; it may vary by > >>> architecture and change in the future. > >>> > >>> Document when Atomic<Flag> is generally preferable to Atomic<bool>: in > >>> particular, when RMW operations such as xchg()/cmpxchg() may be used > >>> and minimizing memory usage is not the top priority. On some > >>> architectures without byte-sized RMW instructions, Atomic<bool> can be > >>> slower for RMW operations. > >>> > >>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> > >>> --- > >>> rust/kernel/sync/atomic.rs | 71 ++++++++++++++++++++++++++++++++++++++ > >>> 1 file changed, 71 insertions(+) > >>> > >>> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs > >>> index ca9cab77abf0..58f57903460f 100644 > >>> --- a/rust/kernel/sync/atomic.rs > >>> +++ b/rust/kernel/sync/atomic.rs > >> > >> I prefer we can move `Flag` into atomic/predefine.rs, I queued in > >> rust-sync with the following version, please let me know whether it > >> works, thank you all! > > > > Works for me, thanks! > > If we like to keep the intra-doc-links, a workaround like the > followings seems to be necessary. > > diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs > index f6c7538cb47b..92e312c06595 100644 > --- a/rust/kernel/sync/atomic/predefine.rs > +++ b/rust/kernel/sync/atomic/predefine.rs > @@ -156,6 +156,13 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { > /// flag.store(Flag::Set, Relaxed); > /// assert_eq!(Flag::Set, flag.load(Relaxed)); > /// ``` > +/// > +/// [`Atomic<Flag>`]: kernel::sync::atomic::Atomic > +/// [`Atomic<bool>`]: kernel::sync::atomic::Atomic > +/// [`Atomic::xchg()`]: kernel::sync::atomic::Atomic::xchg > +/// [`Atomic::cmpxchg()`]: kernel::sync::atomic::Atomic::cmpxchg > +/// [`Atomic::load()`]: kernel::sync::atomic::Atomic::load > +/// [`Atomic::store()`]: kernel::sync::atomic::Atomic::store > #[derive(Clone, Copy, PartialEq, Eq)] > #[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))] > #[cfg_attr( > Good point! I was missing that, however I think we could use `super` instead of `kernel::sync::atomic` in this case. Already pushed to rust-sync with the following changes, thanks! Regards, Boqun ------------->8 diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs index f6c7538cb47b..5faa2fe2f4b6 100644 --- a/rust/kernel/sync/atomic/predefine.rs +++ b/rust/kernel/sync/atomic/predefine.rs @@ -156,6 +156,13 @@ fn rhs_into_delta(rhs: usize) -> isize_atomic_repr { /// flag.store(Flag::Set, Relaxed); /// assert_eq!(Flag::Set, flag.load(Relaxed)); /// ``` +/// +/// [`Atomic<Flag>`]: super::Atomic +/// [`Atomic<bool>`]: super::Atomic +/// [`Atomic::xchg()`]: super::Atomic::xchg +/// [`Atomic::cmpxchg()`]: super::Atomic::cmpxchg +/// [`Atomic::load()`]: super::Atomic::load +/// [`Atomic::store()`]: super::Atomic::store #[derive(Clone, Copy, PartialEq, Eq)] #[cfg_attr(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64), repr(i8))] #[cfg_attr( ^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use 2026-01-15 2:12 [PATCH v4 0/2] rust: sync: atomic flag helpers FUJITA Tomonori 2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori @ 2026-01-15 2:12 ` FUJITA Tomonori 2026-01-15 13:22 ` Gary Guo 2026-01-15 6:41 ` [PATCH v4 0/2] rust: sync: atomic flag helpers Alice Ryhl 2 siblings, 1 reply; 11+ messages in thread From: FUJITA Tomonori @ 2026-01-15 2:12 UTC (permalink / raw) To: boqun.feng, ojeda, peterz, will Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux Provide AtomicFlag as a thin wrapper around Atomic<Flag> so users can work with a simple bool API for load/store/xchg while keeping ordering controls. Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> --- rust/kernel/sync/atomic.rs | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs index 58f57903460f..720fe2a5fb40 100644 --- a/rust/kernel/sync/atomic.rs +++ b/rust/kernel/sync/atomic.rs @@ -637,3 +637,46 @@ fn from(b: bool) -> Self { Flag::new(b) } } + +/// A convenience wrapper around [`Atomic<Flag>`] that exposes a [`bool`] API. +pub struct AtomicFlag(Atomic<Flag>); + +impl AtomicFlag { + /// Creates a new atomic flag. + #[inline(always)] + pub const fn new(b: bool) -> Self { + AtomicFlag(Atomic::new(Flag::new(b))) + } + + /// Loads the value from the atomic flag. + #[inline(always)] + pub fn load<Ordering: ordering::AcquireOrRelaxed>(&self, o: Ordering) -> bool { + self.0.load(o).into() + } + + /// Stores a value to the atomic flag. + #[inline(always)] + pub fn store<Ordering: ordering::ReleaseOrRelaxed>(&self, b: bool, o: Ordering) { + self.0.store(b.into(), o) + } + + /// Stores a value to the atomic flag and returns the previous value. + #[inline(always)] + pub fn xchg<Ordering: ordering::Ordering>(&self, b: bool, o: Ordering) -> bool { + self.0.xchg(b.into(), o).into() + } + + /// Store a value to the atomic flag if the current value is equal to `old`. + #[inline(always)] + pub fn cmpxchg<Ordering: ordering::Ordering>( + &self, + old: bool, + new: bool, + o: Ordering, + ) -> Result<bool, bool> { + match self.0.cmpxchg(old.into(), new.into(), o) { + Ok(v) => Ok(v.into()), + Err(v) => Err(v.into()), + } + } +} -- 2.43.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use 2026-01-15 2:12 ` [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori @ 2026-01-15 13:22 ` Gary Guo 0 siblings, 0 replies; 11+ messages in thread From: Gary Guo @ 2026-01-15 13:22 UTC (permalink / raw) To: FUJITA Tomonori, boqun.feng, ojeda, peterz, will Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Thu Jan 15, 2026 at 2:12 AM GMT, FUJITA Tomonori wrote: > Provide AtomicFlag as a thin wrapper around Atomic<Flag> so users can > work with a simple bool API for load/store/xchg while keeping ordering > controls. > > Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> > --- > rust/kernel/sync/atomic.rs | 43 ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 43 insertions(+) ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v4 0/2] rust: sync: atomic flag helpers 2026-01-15 2:12 [PATCH v4 0/2] rust: sync: atomic flag helpers FUJITA Tomonori 2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori 2026-01-15 2:12 ` [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori @ 2026-01-15 6:41 ` Alice Ryhl 2026-01-15 8:59 ` FUJITA Tomonori 2 siblings, 1 reply; 11+ messages in thread From: Alice Ryhl @ 2026-01-15 6:41 UTC (permalink / raw) To: FUJITA Tomonori Cc: boqun.feng, ojeda, peterz, will, a.hindborg, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Thu, Jan 15, 2026 at 3:13 AM FUJITA Tomonori <fujita.tomonori@gmail.com> wrote: > > This series improves the Rust atomic-boolean support. > > The first patch adds a Flag enum (Clear/Set) and implements AtomicType > for it, so Atomic<Flag> can be used when RMW operations (xchg/cmpxchg) > are needed and byte-sized RMWs may be inefficient. > > The second patch adds AtomicFlag as a thin wrapper around Atomic<Flag> > to provide a simple bool-based load/store/xchg API. You don't have to do it, but a nice follow-up would be to use this for the AtomicTracker in rust/kernel/list. Reviewed-by: Alice Ryhl <aliceryhl@google.com> ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v4 0/2] rust: sync: atomic flag helpers 2026-01-15 6:41 ` [PATCH v4 0/2] rust: sync: atomic flag helpers Alice Ryhl @ 2026-01-15 8:59 ` FUJITA Tomonori 0 siblings, 0 replies; 11+ messages in thread From: FUJITA Tomonori @ 2026-01-15 8:59 UTC (permalink / raw) To: aliceryhl Cc: fujita.tomonori, boqun.feng, ojeda, peterz, will, a.hindborg, bjorn3_gh, dakr, gary, lossin, mark.rutland, tmgross, rust-for-linux On Thu, 15 Jan 2026 07:41:04 +0100 Alice Ryhl <aliceryhl@google.com> wrote: > On Thu, Jan 15, 2026 at 3:13 AM FUJITA Tomonori > <fujita.tomonori@gmail.com> wrote: >> >> This series improves the Rust atomic-boolean support. >> >> The first patch adds a Flag enum (Clear/Set) and implements AtomicType >> for it, so Atomic<Flag> can be used when RMW operations (xchg/cmpxchg) >> are needed and byte-sized RMWs may be inefficient. >> >> The second patch adds AtomicFlag as a thin wrapper around Atomic<Flag> >> to provide a simple bool-based load/store/xchg API. > > You don't have to do it, but a nice follow-up would be to use this for > the AtomicTracker in rust/kernel/list. Sure. Once this series is merged into Boqun’s tree, I’ll send a follow-up patch to switch AtomicTracker over to AtomicFlag. Thanks for the review! ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2026-01-20 2:56 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-01-15 2:12 [PATCH v4 0/2] rust: sync: atomic flag helpers FUJITA Tomonori 2026-01-15 2:12 ` [PATCH v4 1/2] rust: sync: atomic: Add performance-optimal-integer-backed Flag for atomic booleans FUJITA Tomonori 2026-01-15 13:22 ` Gary Guo 2026-01-19 1:22 ` Boqun Feng 2026-01-19 3:10 ` FUJITA Tomonori 2026-01-19 23:08 ` FUJITA Tomonori 2026-01-20 2:56 ` Boqun Feng 2026-01-15 2:12 ` [PATCH v4 2/2] rust: sync: atomic: Add AtomicFlag bool wrapper for easier use FUJITA Tomonori 2026-01-15 13:22 ` Gary Guo 2026-01-15 6:41 ` [PATCH v4 0/2] rust: sync: atomic flag helpers Alice Ryhl 2026-01-15 8:59 ` FUJITA Tomonori
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox