From: Boqun Feng <boqun.feng@gmail.com>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: ojeda@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com,
bjorn3_gh@protonmail.com, dakr@kernel.org, gary@garyguo.net,
lossin@kernel.org, tmgross@umich.edu, acourbot@nvidia.com,
rust-for-linux@vger.kernel.org, linux-arch@vger.kernel.org
Subject: Re: [PATCH v1 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
Date: Wed, 31 Dec 2025 06:45:34 +0800 [thread overview]
Message-ID: <aVRWDgb29OkHAGnY@tardis-2.local> (raw)
In-Reply-To: <20251230045028.1773445-2-fujita.tomonori@gmail.com>
On Tue, Dec 30, 2025 at 01:50:27PM +0900, FUJITA Tomonori wrote:
> Add `bool` support, `Atomic<bool>` by using `i8` as its underlying
> representation.
>
> Rust specifies that `bool` has size 1 and alignment 1 [1], so it
> matches `i8` on layout; keep `static_assert!()` checks to enforce this
> assumption at build time.
>
> Implement `AtomicImpl` for `bool` under
> `CONFIG_ARCH_SUPPORTS_ATOMIC_RMW`, consistent with the existing
> `i8/i16` gating.
>
> Document the additional safety requirement for
> `Atomic::<bool>::from_ptr`: only bit patterns 0 (false) and 1 (true)
> are valid.
>
> Link: https://doc.rust-lang.org/reference/types/boolean.html [1]
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
> rust/kernel/sync/atomic.rs | 1 +
> rust/kernel/sync/atomic/internal.rs | 8 ++++++++
> rust/kernel/sync/atomic/predefine.rs | 11 +++++++++++
> 3 files changed, 20 insertions(+)
>
> diff --git a/rust/kernel/sync/atomic.rs b/rust/kernel/sync/atomic.rs
> index 4aebeacb961a..2c998cbd300e 100644
> --- a/rust/kernel/sync/atomic.rs
> +++ b/rust/kernel/sync/atomic.rs
> @@ -158,6 +158,7 @@ pub const fn new(v: T) -> Self {
> ///
> /// - `ptr` is aligned to `align_of::<T>()`.
> /// - `ptr` is valid for reads and writes for `'a`.
> + /// - If `T` is `bool`, only the bit patterns 0 (`false`) and 1 (`true`) are valid.
This line is unnecessary, since "`ptr` is valid for ..." means `*ptr`
has to have the valid binary representive of `T`.
> /// - For the duration of `'a`, other accesses to `*ptr` must not cause data races (defined
> /// by [`LKMM`]) against atomic operations on the returned reference. Note that if all other
> /// accesses are atomic, then this safety requirement is trivially fulfilled.
> diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
> index 0dac58bca2b3..0e12955082e5 100644
> --- a/rust/kernel/sync/atomic/internal.rs
> +++ b/rust/kernel/sync/atomic/internal.rs
> @@ -16,6 +16,7 @@ pub trait Sealed {}
> // The C side supports atomic primitives only for `i32` and `i64` (`atomic_t` and `atomic64_t`),
> // while the Rust side also layers provides atomic support for `i8` and `i16`
> // on top of lower-level C primitives.
> +impl private::Sealed for bool {}
> impl private::Sealed for i8 {}
> impl private::Sealed for i16 {}
> impl private::Sealed for i32 {}
> @@ -37,6 +38,13 @@ 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)]
> +impl AtomicImpl for bool {
> + type Delta = Self;
> +}
I don't think you need this impl block.
Regards,
Boqun
> +
> // 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)]
> diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
> index 248d26555ccf..3fc99174b086 100644
> --- a/rust/kernel/sync/atomic/predefine.rs
> +++ b/rust/kernel/sync/atomic/predefine.rs
> @@ -5,6 +5,17 @@
> use crate::static_assert;
> use core::mem::{align_of, size_of};
>
> +// Ensure size and alignment requirements are checked.
> +static_assert!(size_of::<bool>() == size_of::<i8>());
> +static_assert!(align_of::<bool>() == align_of::<i8>());
> +
> +// SAFETY: `bool` has the same size and alignment as `i8`, and Rust guarantees that `bool` has
> +// only two valid bit patterns: 0 (false) and 1 (true). Those are valid `i8` values, so `bool` is
> +// round-trip transmutable to `i8`.
> +unsafe impl super::AtomicType for bool {
> + type Repr = i8;
> +}
> +
> // SAFETY: `i8` has the same size and alignment with itself, and is round-trip transmutable to
> // itself.
> unsafe impl super::AtomicType for i8 {
> --
> 2.43.0
>
next prev parent reply other threads:[~2025-12-30 22:45 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-30 4:50 [PATCH v1 0/2] Add atomic bool support FUJITA Tomonori
2025-12-30 4:50 ` [PATCH v1 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
2025-12-30 22:45 ` Boqun Feng [this message]
2025-12-31 12:21 ` FUJITA Tomonori
2025-12-30 4:50 ` [PATCH v1 2/2] rust: sync: atomic: Add atomic bool tests FUJITA Tomonori
2025-12-31 15:34 ` [PATCH v1 0/2] Add atomic bool support Gary Guo
2026-01-01 3:27 ` 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=aVRWDgb29OkHAGnY@tardis-2.local \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=dakr@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.