* [PATCH v1 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
2025-12-30 4:50 [PATCH v1 0/2] Add atomic bool support FUJITA Tomonori
@ 2025-12-30 4:50 ` FUJITA Tomonori
2025-12-30 22:45 ` Boqun Feng
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
2 siblings, 1 reply; 7+ messages in thread
From: FUJITA Tomonori @ 2025-12-30 4:50 UTC (permalink / raw)
To: boqun.feng, ojeda
Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
acourbot, rust-for-linux, linux-arch
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.
/// - 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;
+}
+
// 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
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v1 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
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
2025-12-31 12:21 ` FUJITA Tomonori
0 siblings, 1 reply; 7+ messages in thread
From: Boqun Feng @ 2025-12-30 22:45 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin,
tmgross, acourbot, rust-for-linux, linux-arch
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
>
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v1 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
2025-12-30 22:45 ` Boqun Feng
@ 2025-12-31 12:21 ` FUJITA Tomonori
0 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2025-12-31 12:21 UTC (permalink / raw)
To: boqun.feng
Cc: fujita.tomonori, ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr,
gary, lossin, tmgross, acourbot, rust-for-linux, linux-arch
On Wed, 31 Dec 2025 06:45:34 +0800
Boqun Feng <boqun.feng@gmail.com> wrote:
> 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`.
Understood, I will drop this in v2.
>> /// - 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.
You are right. Only the backing atomic types need to implement this. I
will drop in v2.
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v1 2/2] rust: sync: atomic: Add atomic bool tests
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 4:50 ` FUJITA Tomonori
2025-12-31 15:34 ` [PATCH v1 0/2] Add atomic bool support Gary Guo
2 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2025-12-30 4:50 UTC (permalink / raw)
To: boqun.feng, ojeda
Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
acourbot, rust-for-linux, linux-arch
Add tests for Atomic<bool> operations.
Atomic<bool> does not fit into the existing u8/16/32/64 tests so
introduce a dedicated tests for it.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
rust/kernel/sync/atomic/predefine.rs | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
index 3fc99174b086..42067c6a266c 100644
--- a/rust/kernel/sync/atomic/predefine.rs
+++ b/rust/kernel/sync/atomic/predefine.rs
@@ -199,4 +199,20 @@ fn atomic_arithmetic_tests() {
assert_eq!(v + 25, x.load(Relaxed));
});
}
+
+ #[test]
+ fn atomic_bool_tests() {
+ let x = Atomic::new(false);
+
+ assert_eq!(false, x.load(Relaxed));
+ x.store(true, Relaxed);
+ assert_eq!(true, x.load(Relaxed));
+
+ assert_eq!(true, x.xchg(false, Relaxed));
+ assert_eq!(false, x.load(Relaxed));
+
+ assert_eq!(Err(false), x.cmpxchg(true, true, Relaxed));
+ assert_eq!(false, x.load(Relaxed));
+ assert_eq!(Ok(false), x.cmpxchg(false, true, Full));
+ }
}
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v1 0/2] Add atomic bool support
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 4:50 ` [PATCH v1 2/2] rust: sync: atomic: Add atomic bool tests FUJITA Tomonori
@ 2025-12-31 15:34 ` Gary Guo
2026-01-01 3:27 ` FUJITA Tomonori
2 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2025-12-31 15:34 UTC (permalink / raw)
To: FUJITA Tomonori
Cc: boqun.feng, ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr, lossin,
tmgross, acourbot, rust-for-linux, linux-arch
On Tue, 30 Dec 2025 13:50:26 +0900
FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
> This adds `bool` support to the Rust LKMM atomics.
>
> Rust specifies that `bool` has size 1 and alignment 1 [1], so it can
> be represented using an `i8` backing type.
>
> Since `bool` only permits the bit patterns 0x00 and 0x01, the first
> patch also documents an additional safety preconditions for unsafe
> `Atomic::<T>::from_ptr`.
>
> `from_ptr()` exists to operate on C-side storage so I don't think it
> makes sense for bool. We could restrict from_ptr() via a marker trait.
The C-side does have `bool` type, too (`_Bool` has been there since C99 and
we have typedef'd it to `bool`). User might still want to perform relaxed
load/store on these C-side bool storage.
I think it's fine to leave out this line too (given Boqun also points out
that the additional safety precondition is not needed for `from_ptr).
Best,
Gary
>
> [1] https://doc.rust-lang.org/reference/types/boolean.html
>
>
> FUJITA Tomonori (2):
> rust: sync: atomic: Add atomic bool support via i8 representation
> rust: sync: atomic: Add atomic bool tests
>
> rust/kernel/sync/atomic.rs | 1 +
> rust/kernel/sync/atomic/internal.rs | 8 ++++++++
> rust/kernel/sync/atomic/predefine.rs | 27 +++++++++++++++++++++++++++
> 3 files changed, 36 insertions(+)
>
> base-commit: 13ade169e801a423bc1a5a5c3c6ac680a144a608
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v1 0/2] Add atomic bool support
2025-12-31 15:34 ` [PATCH v1 0/2] Add atomic bool support Gary Guo
@ 2026-01-01 3:27 ` FUJITA Tomonori
0 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2026-01-01 3:27 UTC (permalink / raw)
To: gary
Cc: fujita.tomonori, boqun.feng, ojeda, a.hindborg, aliceryhl,
bjorn3_gh, dakr, lossin, tmgross, acourbot, rust-for-linux,
linux-arch
On Wed, 31 Dec 2025 15:34:31 +0000
Gary Guo <gary@garyguo.net> wrote:
> On Tue, 30 Dec 2025 13:50:26 +0900
> FUJITA Tomonori <fujita.tomonori@gmail.com> wrote:
>
>> This adds `bool` support to the Rust LKMM atomics.
>>
>> Rust specifies that `bool` has size 1 and alignment 1 [1], so it can
>> be represented using an `i8` backing type.
>>
>> Since `bool` only permits the bit patterns 0x00 and 0x01, the first
>> patch also documents an additional safety preconditions for unsafe
>> `Atomic::<T>::from_ptr`.
>>
>> `from_ptr()` exists to operate on C-side storage so I don't think it
>> makes sense for bool. We could restrict from_ptr() via a marker trait.
>
> The C-side does have `bool` type, too (`_Bool` has been there since C99 and
> we have typedef'd it to `bool`). User might still want to perform relaxed
> load/store on these C-side bool storage.
>
> I think it's fine to leave out this line too (given Boqun also points out
> that the additional safety precondition is not needed for `from_ptr).
I see. I'll drop the description in v2.
Thanks!
^ permalink raw reply [flat|nested] 7+ messages in thread