From: Boqun Feng <boqun@kernel.org>
To: FUJITA Tomonori <tomo@aliasing.net>
Cc: ojeda@kernel.org, peterz@infradead.org, will@kernel.org,
a.hindborg@kernel.org, aliceryhl@google.com,
bjorn3_gh@protonmail.com, dakr@kernel.org, gary@garyguo.net,
lossin@kernel.org, mark.rutland@arm.com, tmgross@umich.edu,
rust-for-linux@vger.kernel.org,
FUJITA Tomonori <fujita.tomonori@gmail.com>
Subject: Re: [PATCH v2 1/2] rust: sync: atomic: Add AtomicFlag::get_mut
Date: Tue, 27 Jan 2026 07:35:34 -0800 [thread overview]
Message-ID: <aXjbRq3Npqo7kr26@tardis.local> (raw)
In-Reply-To: <20260127125300.3656544-2-tomo@aliasing.net>
[For some unknown reasons, I cannot send my reply via gmail hence reply
via kernel.org account, I might switch from gmail later on]
On Tue, Jan 27, 2026 at 09:52:59PM +0900, FUJITA Tomonori wrote:
> From: FUJITA Tomonori <fujita.tomonori@gmail.com>
>
> AtomicFlag exposes a bool API, but it lacks a get_mut() equivalent to
> Atomic<T>::get_mut().
>
> Also add kunit tests for AtomicFlag.
>
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
> rust/kernel/sync/atomic.rs | 20 ++++++++++++++++++++
> rust/kernel/sync/atomic/predefine.rs | 17 +++++++++++++++++
> 2 files changed, 37 insertions(+)
>
> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> index 6c46335bdb8c..b6c01d9f3a46 100644
> --- a/rust/kernel/sync/atomic.rs
> +++ b/rust/kernel/sync/atomic.rs
> @@ -591,6 +591,26 @@ pub fn store<Ordering: ordering::ReleaseOrRelaxed>(&self, b: bool, o: Ordering)
> self.0.store(b.into(), o)
> }
>
> + /// 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.
> + pub fn get_mut(&mut self) -> &mut bool {
> + let byte_ptr = {
> + let ptr = self.0.as_ptr().cast::<u8>();
> + let offset = if cfg!(target_endian = "big") {
> + core::mem::size_of::<Flag>() - 1
> + } else {
> + 0
> + };
The idea is solid, but I want to avoid endian handling in the function,
I would prefer a "struct declaration" solution like:
#[cfg(target_endian = "big")]
#[repr(align(4))]
pub(super) struct FlagInner {
_pad: [i8; 3],
bool_field: bool,
}
#[cfg(target_endian = "little")]
#[repr(align(4))]
struct FlagInner {
bool_field: bool,
_pad: [i8; 3],
}
redefine `Flag` as `BoolFlag`
#[repr(i32)]
pub enum BoolFlag {
Clear = 0,
Set = 1,
}
and `Flag` becomes a union of `BoolFlag` and `FlagInner`:
/// # Invariants
/// `Flag` is either 0 or 1 in a i32 representation which implies
/// that `inner` is always valid as long as `_pad` stays 0.
pub union Flag {
pub(super) inner: FlagInner,
pub flag: BoolFlag,
}
// can static_assert that `Flag` and `BoolFlag` has the same
// alignement and size.
then
impl AtomicFlag {
pub fn get_mut(&mut self) -> &mut bool {
let flag = self.0.get_mut(); // <- &mut Flag
// INVARIANTS: flag.inner._pad cannot be modified via the
// returned reference.
// SAFETY: Per type invariants, `flag.inner.bool_field` is
// always a valid bool.
unsafe { &mut flag.inner.bool_field }
}
}
Thoughts?
Regards,
Boqun
> +
> + // SAFETY: `ptr` is valid for `size_of::<Flag>()` bytes; `offset` selects the LSB.
> + unsafe { ptr.add(offset) }
> + };
> +
> + // SAFETY: The LSB holds `0`/`1` for `Flag::Clear/Set`, and `bool` is `i8`-sized/aligned.
> + unsafe { &mut *byte_ptr.cast::<bool>() }
> + }
> +
> /// 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 {
> diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
> index 11bc67ab70a3..e413b9e9fe21 100644
> --- a/rust/kernel/sync/atomic/predefine.rs
> +++ b/rust/kernel/sync/atomic/predefine.rs
> @@ -351,6 +351,23 @@ fn atomic_bool_tests() {
> assert_eq!(Ok(false), x.cmpxchg(false, true, Full));
> }
>
> + #[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));
> + }
> +
> #[test]
> fn atomic_ptr_tests() {
> let mut v = 42;
> --
> 2.43.0
>
next prev parent reply other threads:[~2026-01-27 15:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-27 12:52 [PATCH v2 0/2] rust: add AtomicFlag::get_mut FUJITA Tomonori
2026-01-27 12:52 ` [PATCH v2 1/2] rust: sync: atomic: Add AtomicFlag::get_mut FUJITA Tomonori
2026-01-27 13:53 ` Gary Guo
2026-01-27 14:32 ` Miguel Ojeda
2026-01-28 3:47 ` FUJITA Tomonori
2026-01-28 3:55 ` Boqun Feng
2026-01-27 15:35 ` Boqun Feng [this message]
2026-01-27 15:42 ` Gary Guo
2026-01-27 15:59 ` Boqun Feng
2026-01-27 16:10 ` Gary Guo
2026-01-27 16:34 ` Boqun Feng
2026-01-28 3:32 ` FUJITA Tomonori
2026-01-28 3:38 ` Boqun Feng
2026-01-27 12:53 ` [PATCH v2 2/2] rust: list: Use AtomicFlag in AtomicTracker FUJITA Tomonori
2026-01-27 13:54 ` Gary Guo
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=aXjbRq3Npqo7kr26@tardis.local \
--to=boqun@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=lossin@kernel.org \
--cc=mark.rutland@arm.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
--cc=tomo@aliasing.net \
--cc=will@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