Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: sync: atomic: Add i8/i16 load and store support

Add atomic operation support for i8 and i16 types using volatile
read/write and smp_load_acquire/smp_store_release helpers.

[boqun: Adjust [1] to avoid introduction of
impl_atomic_only_load_and_store_ops!() in the middle]

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com>
Link: https://lore.kernel.org/all/20251228120546.1602275-1-fujita.tomonori@gmail.com/ [1]
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://patch.msgid.link/20251211113826.1299077-4-fujita.tomonori@gmail.com

authored by

FUJITA Tomonori and committed by
Boqun Feng
b33796d5 cf4c3bc1

+32 -7
+19 -6
rust/kernel/sync/atomic/internal.rs
··· 13 13 pub trait Sealed {} 14 14 } 15 15 16 - // `i32` and `i64` are only supported atomic implementations. 16 + // The C side supports atomic primitives only for `i32` and `i64` (`atomic_t` and `atomic64_t`), 17 + // while the Rust side also layers provides atomic support for `i8` and `i16` 18 + // on top of lower-level C primitives. 19 + impl private::Sealed for i8 {} 20 + impl private::Sealed for i16 {} 17 21 impl private::Sealed for i32 {} 18 22 impl private::Sealed for i64 {} 19 23 20 24 /// A marker trait for types that implement atomic operations with C side primitives. 21 25 /// 22 - /// This trait is sealed, and only types that have directly mapping to the C side atomics should 23 - /// impl this: 26 + /// This trait is sealed, and only types that map directly to the C side atomics 27 + /// or can be implemented with lower-level C primitives are allowed to implement this: 24 28 /// 25 - /// - `i32` maps to `atomic_t`. 26 - /// - `i64` maps to `atomic64_t`. 29 + /// - `i8` and `i16` are implemented with lower-level C primitives. 30 + /// - `i32` map to `atomic_t` 31 + /// - `i64` map to `atomic64_t` 27 32 pub trait AtomicImpl: Sized + Send + Copy + private::Sealed { 28 33 /// The type of the delta in arithmetic or logical operations. 29 34 /// 30 35 /// For example, in `atomic_add(ptr, v)`, it's the type of `v`. Usually it's the same type of 31 36 /// [`Self`], but it may be different for the atomic pointer type. 32 37 type Delta; 38 + } 39 + 40 + impl AtomicImpl for i8 { 41 + type Delta = Self; 42 + } 43 + 44 + impl AtomicImpl for i16 { 45 + type Delta = Self; 33 46 } 34 47 35 48 // `atomic_t` implements atomic operations on `i32`. ··· 256 243 } 257 244 258 245 declare_and_impl_atomic_methods!( 259 - [ i32 => atomic, i64 => atomic64 ] 246 + [ i8 => atomic_i8, i16 => atomic_i16, i32 => atomic, i64 => atomic64 ] 260 247 /// Basic atomic operations 261 248 pub trait AtomicBasicOps { 262 249 /// Atomic read (load).
+13 -1
rust/kernel/sync/atomic/predefine.rs
··· 5 5 use crate::static_assert; 6 6 use core::mem::{align_of, size_of}; 7 7 8 + // SAFETY: `i8` has the same size and alignment with itself, and is round-trip transmutable to 9 + // itself. 10 + unsafe impl super::AtomicType for i8 { 11 + type Repr = i8; 12 + } 13 + 14 + // SAFETY: `i16` has the same size and alignment with itself, and is round-trip transmutable to 15 + // itself. 16 + unsafe impl super::AtomicType for i16 { 17 + type Repr = i16; 18 + } 19 + 8 20 // SAFETY: `i32` has the same size and alignment with itself, and is round-trip transmutable to 9 21 // itself. 10 22 unsafe impl super::AtomicType for i32 { ··· 130 118 131 119 #[test] 132 120 fn atomic_basic_tests() { 133 - for_each_type!(42 in [i32, i64, u32, u64, isize, usize] |v| { 121 + for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| { 134 122 let x = Atomic::new(v); 135 123 136 124 assert_eq!(v, x.load(Relaxed));