From: Gary Guo <gary@garyguo.net>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: boqun.feng@gmail.com, ojeda@kernel.org, a.hindborg@kernel.org,
aliceryhl@google.com, bjorn3_gh@protonmail.com, dakr@kernel.org,
lossin@kernel.org, tmgross@umich.edu, acourbot@nvidia.com,
rust-for-linux@vger.kernel.org, linux-arch@vger.kernel.org
Subject: Re: [PATCH v1] rust: sync: atomic: Add i32-backed Flag for atomic booleans
Date: Thu, 1 Jan 2026 21:04:30 +0000 [thread overview]
Message-ID: <20260101210430.6b210dc6.gary@garyguo.net> (raw)
In-Reply-To: <20260101102718.2073674-1-fujita.tomonori@gmail.com>
On Thu, 1 Jan 2026 19:27:18 +0900
FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
> Add a new Flag enum (Clear/Set) with #[repr(i32)] and implement
> AtomicType for it, so users can use Atomic<Flag> for boolean flags.
>
> 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 | 35 +++++++++++++++++++++++++++++++++++
> 1 file changed, 35 insertions(+)
>
> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> index 4aebeacb961a..d98ab51ae4fc 100644
> --- a/rust/kernel/sync/atomic.rs
> +++ b/rust/kernel/sync/atomic.rs
> @@ -560,3 +560,38 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering)
> unsafe { from_repr(ret) }
> }
> }
> +
> +/// An atomic flag type backed by `i32`.
I would recommend that we document that the backing type is the
(perf-)optimal type on the target architecure, so arch can decide to use
i8 as backing type if they prefer.
> +///
> +/// `Atomic<Flag>` is generally preferable when you need an atomic boolean and you may use
> +/// read-modify-write operations (e.g. `xchg()`/`cmpxchg()`), and when minimizing memory usage is
> +/// not the top priority.
> +///
> +/// `Atomic<bool>` is backed by `u8`. On some architectures that do not support byte-sized RMW
> +/// instructions, this can make RMW operations slower.
> +///
> +/// If you only use `load()`/`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)]
> +#[repr(i32)]
> +pub enum Flag {
> + /// The flag is clear.
> + Clear = 0,
> + /// The flag is set.
> + Set = 1,
> +}
Maybe add `From` impls that convert this type to boolean?
Best,
Gary
> +
> +// SAFETY: `Flag` and `i32` has the same size and alignment, and it's round-trip
> +// transmutable to `i32`.
> +unsafe impl AtomicType for Flag {
> + type Repr = i32;
> +}
>
> base-commit: dafb6d4cabd044ccd7e49cea29363e8526edc071
next prev parent reply other threads:[~2026-01-01 21:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-01 10:27 [PATCH v1] rust: sync: atomic: Add i32-backed Flag for atomic booleans FUJITA Tomonori
2026-01-01 21:04 ` Gary Guo [this message]
2026-01-03 10:44 ` FUJITA Tomonori
2026-01-03 19:05 ` Gary Guo
2026-01-03 21:53 ` FUJITA Tomonori
2026-01-04 8:36 ` Boqun Feng
2026-01-04 12:07 ` Miguel Ojeda
2026-01-08 5:17 ` FUJITA Tomonori
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=20260101210430.6b210dc6.gary@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/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