From: "tip-bot2 for FUJITA Tomonori" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: FUJITA Tomonori <fujita.tomonori@gmail.com>,
Boqun Feng <boqun@kernel.org>,
"Peter Zijlstra (Intel)" <peterz@infradead.org>,
Gary Guo <gary@garyguo.net>,
x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: locking/core] rust: sync: atomic: Add performance-optimal Flag type for atomic booleans
Date: Mon, 09 Mar 2026 19:48:25 -0000 [thread overview]
Message-ID: <177308570583.1647592.7698949895096043794.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20260303201701.12204-9-boqun@kernel.org>
The following commit has been merged into the locking/core branch of tip:
Commit-ID: ec6fc66ac39b1a6c0b06a828eff8d21928e56b60
Gitweb: https://git.kernel.org/tip/ec6fc66ac39b1a6c0b06a828eff8d21928e56b60
Author: FUJITA Tomonori <fujita.tomonori@gmail.com>
AuthorDate: Tue, 03 Mar 2026 12:16:56 -08:00
Committer: Peter Zijlstra <peterz@infradead.org>
CommitterDate: Sun, 08 Mar 2026 11:06:49 +01:00
rust: sync: atomic: Add performance-optimal Flag type for atomic booleans
Add AtomicFlag type for boolean flags.
Document when AtomicFlag 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>
Signed-off-by: Boqun Feng <boqun@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20260129122622.3896144-2-tomo@aliasing.net
Link: https://patch.msgid.link/20260303201701.12204-9-boqun@kernel.org
---
rust/kernel/sync/atomic.rs | 125 ++++++++++++++++++++++++++-
rust/kernel/sync/atomic/predefine.rs | 17 ++++-
2 files changed, 142 insertions(+)
diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
index f4c3ab1..f80cebc 100644
--- a/rust/kernel/sync/atomic.rs
+++ b/rust/kernel/sync/atomic.rs
@@ -578,3 +578,128 @@ where
unsafe { from_repr(ret) }
}
}
+
+#[cfg(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64))]
+#[repr(C)]
+#[derive(Clone, Copy)]
+struct Flag {
+ bool_field: bool,
+}
+
+/// # Invariants
+///
+/// `padding` must be all zeroes.
+#[cfg(not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)))]
+#[repr(C, align(4))]
+#[derive(Clone, Copy)]
+struct Flag {
+ #[cfg(target_endian = "big")]
+ padding: [u8; 3],
+ bool_field: bool,
+ #[cfg(target_endian = "little")]
+ padding: [u8; 3],
+}
+
+impl Flag {
+ #[inline(always)]
+ const fn new(b: bool) -> Self {
+ // INVARIANT: `padding` is all zeroes.
+ Self {
+ bool_field: b,
+ #[cfg(not(any(CONFIG_X86_64, CONFIG_UML, CONFIG_ARM, CONFIG_ARM64)))]
+ padding: [0; 3],
+ }
+ }
+}
+
+// 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 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;
+}
+
+/// An atomic flag type intended to be backed by performance-optimal integer type.
+///
+/// The backing integer type is an implementation detail; it may vary by architecture and change
+/// in the future.
+///
+/// [`AtomicFlag`] 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()`], [`Atomic<bool>`] is fine.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::sync::atomic::{AtomicFlag, Relaxed};
+///
+/// let flag = AtomicFlag::new(false);
+/// assert_eq!(false, flag.load(Relaxed));
+/// flag.store(true, Relaxed);
+/// assert_eq!(true, flag.load(Relaxed));
+/// ```
+pub struct AtomicFlag(Atomic<Flag>);
+
+impl AtomicFlag {
+ /// Creates a new atomic flag.
+ #[inline(always)]
+ pub const fn new(b: bool) -> Self {
+ Self(Atomic::new(Flag::new(b)))
+ }
+
+ /// Returns a mutable reference to the underlying flag as a [`bool`].
+ ///
+ /// This is safe because the mutable reference of the atomic flag guarantees exclusive access.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use kernel::sync::atomic::{AtomicFlag, Relaxed};
+ ///
+ /// let mut atomic_flag = AtomicFlag::new(false);
+ /// assert_eq!(false, atomic_flag.load(Relaxed));
+ /// *atomic_flag.get_mut() = true;
+ /// assert_eq!(true, atomic_flag.load(Relaxed));
+ /// ```
+ #[inline(always)]
+ pub fn get_mut(&mut self) -> &mut bool {
+ &mut self.0.get_mut().bool_field
+ }
+
+ /// Loads the value from the atomic flag.
+ #[inline(always)]
+ pub fn load<Ordering: ordering::AcquireOrRelaxed>(&self, o: Ordering) -> bool {
+ self.0.load(o).bool_field
+ }
+
+ /// Stores a value to the atomic flag.
+ #[inline(always)]
+ pub fn store<Ordering: ordering::ReleaseOrRelaxed>(&self, v: bool, o: Ordering) {
+ self.0.store(Flag::new(v), o);
+ }
+
+ /// Stores a value to the atomic flag and returns the previous value.
+ #[inline(always)]
+ pub fn xchg<Ordering: ordering::Ordering>(&self, new: bool, o: Ordering) -> bool {
+ self.0.xchg(Flag::new(new), o).bool_field
+ }
+
+ /// 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(Flag::new(old), Flag::new(new), o) {
+ Ok(_) => Ok(old),
+ Err(f) => Err(f.bool_field),
+ }
+ }
+}
diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
index 6f2c605..ceb3cae 100644
--- a/rust/kernel/sync/atomic/predefine.rs
+++ b/rust/kernel/sync/atomic/predefine.rs
@@ -272,4 +272,21 @@ mod tests {
);
assert_eq!(x.load(Relaxed), &raw const u);
}
+
+ #[test]
+ fn atomic_flag_tests() {
+ let mut flag = AtomicFlag::new(false);
+
+ assert_eq!(false, flag.load(Relaxed));
+
+ *flag.get_mut() = true;
+ assert_eq!(true, flag.load(Relaxed));
+
+ assert_eq!(true, flag.xchg(false, Relaxed));
+ assert_eq!(false, flag.load(Relaxed));
+
+ *flag.get_mut() = true;
+ assert_eq!(Ok(true), flag.cmpxchg(true, false, Full));
+ assert_eq!(false, flag.load(Relaxed));
+ }
}
next prev parent reply other threads:[~2026-03-09 19:48 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-03 20:16 [GIT PULL] [PATCH 00/13] Rust atomic changes for v7.1 Boqun Feng
2026-03-03 20:16 ` [PATCH 01/13] rust: sync: atomic: Remove bound `T: Sync` for `Atomic::from_ptr()` Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 02/13] rust: sync: atomic: Add example for Atomic::get_mut() Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for FUJITA Tomonori
2026-03-03 20:16 ` [PATCH 03/13] rust: helpers: Generify the definitions of rust_helper_*_{read,set}* Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 04/13] rust: helpers: Generify the definitions of rust_helper_*_xchg* Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 05/13] rust: helpers: Generify the definitions of rust_helper_*_cmpxchg* Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 06/13] rust: sync: atomic: Clarify the need of CONFIG_ARCH_SUPPORTS_ATOMIC_RMW Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 07/13] rust: sync: atomic: Add Atomic<*{mut,const} T> support Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 08/13] rust: sync: atomic: Add performance-optimal Flag type for atomic booleans Boqun Feng
2026-03-09 19:48 ` tip-bot2 for FUJITA Tomonori [this message]
2026-03-03 20:16 ` [PATCH 09/13] rust: list: Use AtomicFlag in AtomicTracker Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for FUJITA Tomonori
2026-03-03 20:16 ` [PATCH 10/13] rust: sync: atomic: Add atomic operation helpers over raw pointers Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Boqun Feng
2026-03-03 20:16 ` [PATCH 11/13] rust: sync: atomic: Add fetch_sub() Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Andreas Hindborg
2026-03-03 20:17 ` [PATCH 12/13] rust: sync: atomic: Update documentation for `fetch_add()` Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Andreas Hindborg
2026-03-03 20:17 ` [PATCH 13/13] rust: atomic: Update a safety comment in impl of `fetch_add()` Boqun Feng
2026-03-09 19:48 ` [tip: locking/core] " tip-bot2 for Andreas Hindborg
2026-03-09 15:54 ` [GIT PULL] [PATCH 00/13] Rust atomic changes for v7.1 Boqun Feng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=177308570583.1647592.7698949895096043794.tip-bot2@tip-bot2 \
--to=tip-bot2@linutronix.de \
--cc=boqun@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-tip-commits@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®