Generic Linux architectural discussions
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Add atomic bool support
@ 2026-01-01  3:49 FUJITA Tomonori
  2026-01-01  3:49 ` [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2026-01-01  3:49 UTC (permalink / raw)
  To: boqun.feng, ojeda
  Cc: a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin, tmgross,
	acourbot, rust-for-linux, linux-arch

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.

[1] https://doc.rust-lang.org/reference/types/boolean.html

v2:
- remove AtomicImpl
- remove Safety comment about the bit patterns
- remove from_ptr() comment in cover letter
v1: https://lore.kernel.org/rust-for-linux/20251230045028.1773445-1-fujita.tomonori@gmail.com/


FUJITA Tomonori (2):
  rust: sync: atomic: Add atomic bool support via i8 representation
  rust: sync: atomic: Add atomic bool tests

 rust/kernel/sync/atomic/internal.rs  |  1 +
 rust/kernel/sync/atomic/predefine.rs | 27 +++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)


base-commit: dafb6d4cabd044ccd7e49cea29363e8526edc071
-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
  2026-01-01  3:49 [PATCH v2 0/2] Add atomic bool support FUJITA Tomonori
@ 2026-01-01  3:49 ` FUJITA Tomonori
  2026-01-03  7:51   ` Boqun Feng
  2026-01-01  3:49 ` [PATCH v2 2/2] rust: sync: atomic: Add atomic bool tests FUJITA Tomonori
  2026-01-02 11:20 ` [PATCH v2 0/2] Add atomic bool support Gary Guo
  2 siblings, 1 reply; 7+ messages in thread
From: FUJITA Tomonori @ 2026-01-01  3:49 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.

Link: https://doc.rust-lang.org/reference/types/boolean.html [1]
Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/sync/atomic/internal.rs  |  1 +
 rust/kernel/sync/atomic/predefine.rs | 11 +++++++++++
 2 files changed, 12 insertions(+)

diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
index 0dac58bca2b3..31ac4c83b1a5 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 {}
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

* [PATCH v2 2/2] rust: sync: atomic: Add atomic bool tests
  2026-01-01  3:49 [PATCH v2 0/2] Add atomic bool support FUJITA Tomonori
  2026-01-01  3:49 ` [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
@ 2026-01-01  3:49 ` FUJITA Tomonori
  2026-01-02 11:20 ` [PATCH v2 0/2] Add atomic bool support Gary Guo
  2 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2026-01-01  3:49 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 v2 0/2] Add atomic bool support
  2026-01-01  3:49 [PATCH v2 0/2] Add atomic bool support FUJITA Tomonori
  2026-01-01  3:49 ` [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
  2026-01-01  3:49 ` [PATCH v2 2/2] rust: sync: atomic: Add atomic bool tests FUJITA Tomonori
@ 2026-01-02 11:20 ` Gary Guo
  2026-01-03  7:52   ` Boqun Feng
  2 siblings, 1 reply; 7+ messages in thread
From: Gary Guo @ 2026-01-02 11:20 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 Thu,  1 Jan 2026 12:49:20 +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.
> 
> [1] https://doc.rust-lang.org/reference/types/boolean.html
> 
> v2:
> - remove AtomicImpl
> - remove Safety comment about the bit patterns
> - remove from_ptr() comment in cover letter
> v1: https://lore.kernel.org/rust-for-linux/20251230045028.1773445-1-fujita.tomonori@gmail.com/
> 

Reviewed-by: Gary Guo <gary@garyguo.net>

Thanks!

- Gary

> 
> FUJITA Tomonori (2):
>   rust: sync: atomic: Add atomic bool support via i8 representation
>   rust: sync: atomic: Add atomic bool tests
> 
>  rust/kernel/sync/atomic/internal.rs  |  1 +
>  rust/kernel/sync/atomic/predefine.rs | 27 +++++++++++++++++++++++++++
>  2 files changed, 28 insertions(+)
> 
> 
> base-commit: dafb6d4cabd044ccd7e49cea29363e8526edc071


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
  2026-01-01  3:49 ` [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
@ 2026-01-03  7:51   ` Boqun Feng
  2026-01-03  9:38     ` FUJITA Tomonori
  0 siblings, 1 reply; 7+ messages in thread
From: Boqun Feng @ 2026-01-03  7:51 UTC (permalink / raw)
  To: FUJITA Tomonori
  Cc: ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr, gary, lossin,
	tmgross, acourbot, rust-for-linux, linux-arch

On Thu, Jan 01, 2026 at 12:49:21PM +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.
> 
> Link: https://doc.rust-lang.org/reference/types/boolean.html [1]
> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
>  rust/kernel/sync/atomic/internal.rs  |  1 +
>  rust/kernel/sync/atomic/predefine.rs | 11 +++++++++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
> index 0dac58bca2b3..31ac4c83b1a5 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 {}

This line is not needed since bool doesn't need to an AtomicImpl.

Regards,
Boqun

>  impl private::Sealed for i8 {}
>  impl private::Sealed for i16 {}
>  impl private::Sealed for i32 {}
> 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 v2 0/2] Add atomic bool support
  2026-01-02 11:20 ` [PATCH v2 0/2] Add atomic bool support Gary Guo
@ 2026-01-03  7:52   ` Boqun Feng
  0 siblings, 0 replies; 7+ messages in thread
From: Boqun Feng @ 2026-01-03  7:52 UTC (permalink / raw)
  To: Gary Guo
  Cc: FUJITA Tomonori, ojeda, a.hindborg, aliceryhl, bjorn3_gh, dakr,
	lossin, tmgross, acourbot, rust-for-linux, linux-arch

On Fri, Jan 02, 2026 at 11:20:00AM +0000, Gary Guo wrote:
> On Thu,  1 Jan 2026 12:49:20 +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.
> > 
> > [1] https://doc.rust-lang.org/reference/types/boolean.html
> > 
> > v2:
> > - remove AtomicImpl
> > - remove Safety comment about the bit patterns
> > - remove from_ptr() comment in cover letter
> > v1: https://lore.kernel.org/rust-for-linux/20251230045028.1773445-1-fujita.tomonori@gmail.com/
> > 
> 
> Reviewed-by: Gary Guo <gary@garyguo.net>
> 

Queued with the unnecessary impl AtomicIpml removed, thank you both!

Regards,
Boqun

> Thanks!
> 
> - Gary
> 
> > 
> > FUJITA Tomonori (2):
> >   rust: sync: atomic: Add atomic bool support via i8 representation
> >   rust: sync: atomic: Add atomic bool tests
> > 
> >  rust/kernel/sync/atomic/internal.rs  |  1 +
> >  rust/kernel/sync/atomic/predefine.rs | 27 +++++++++++++++++++++++++++
> >  2 files changed, 28 insertions(+)
> > 
> > 
> > base-commit: dafb6d4cabd044ccd7e49cea29363e8526edc071
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation
  2026-01-03  7:51   ` Boqun Feng
@ 2026-01-03  9:38     ` FUJITA Tomonori
  0 siblings, 0 replies; 7+ messages in thread
From: FUJITA Tomonori @ 2026-01-03  9:38 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 Sat, 3 Jan 2026 15:51:33 +0800
Boqun Feng <boqun.feng@gmail.com> wrote:

> On Thu, Jan 01, 2026 at 12:49:21PM +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.
>> 
>> Link: https://doc.rust-lang.org/reference/types/boolean.html [1]
>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
>> ---
>>  rust/kernel/sync/atomic/internal.rs  |  1 +
>>  rust/kernel/sync/atomic/predefine.rs | 11 +++++++++++
>>  2 files changed, 12 insertions(+)
>> 
>> diff --git a/rust/kernel/sync/atomic/internal.rs b/rust/kernel/sync/atomic/internal.rs
>> index 0dac58bca2b3..31ac4c83b1a5 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 {}
> 
> This line is not needed since bool doesn't need to an AtomicImpl.

Oops, you are right.

Thanks!

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-01-03  9:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-01  3:49 [PATCH v2 0/2] Add atomic bool support FUJITA Tomonori
2026-01-01  3:49 ` [PATCH v2 1/2] rust: sync: atomic: Add atomic bool support via i8 representation FUJITA Tomonori
2026-01-03  7:51   ` Boqun Feng
2026-01-03  9:38     ` FUJITA Tomonori
2026-01-01  3:49 ` [PATCH v2 2/2] rust: sync: atomic: Add atomic bool tests FUJITA Tomonori
2026-01-02 11:20 ` [PATCH v2 0/2] Add atomic bool support Gary Guo
2026-01-03  7:52   ` Boqun Feng

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox