Skip to content

Commit 5138ea2

Browse files
fbqjannau
authored andcommitted
rust: sync: atomic: Add Atomic<{usize,isize}>
Add generic atomic support for `usize` and `isize`. Note that instead of mapping directly to `atomic_long_t`, the represention type (`AtomicType::Repr`) is selected based on CONFIG_64BIT. This reduces the necessity of creating `atomic_long_*` helpers, which could save the binary size of kernel if inline helpers are not available. To do so, an internal type `isize_atomic_repr` is defined, it's `i32` in 32bit kernel and `i64` in 64bit kernel. Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Andreas Hindborg <a.hindborg@kernel.org> Reviewed-by: Benno Lossin <lossin@kernel.org> Reviewed-by: Elle Rhumsaa <elle@weathered-steel.dev> Link: https://lore.kernel.org/all/20250719030827.61357-9-boqun.feng@gmail.com/
1 parent b8c011e commit 5138ea2

1 file changed

Lines changed: 49 additions & 4 deletions

File tree

rust/kernel/sync/atomic/predefine.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
//! Pre-defined atomic types
44
5+
use crate::static_assert;
6+
use core::mem::{align_of, size_of};
7+
58
// SAFETY: `i32` has the same size and alignment with itself, and is round-trip transmutable to
69
// itself.
710
unsafe impl super::AtomicType for i32 {
@@ -28,6 +31,35 @@ unsafe impl super::AtomicAdd<i64> for i64 {
2831
}
2932
}
3033

34+
// Defines an internal type that always maps to the integer type which has the same size alignment
35+
// as `isize` and `usize`, and `isize` and `usize` are always bi-directional transmutable to
36+
// `isize_atomic_repr`, which also always implements `AtomicImpl`.
37+
#[allow(non_camel_case_types)]
38+
#[cfg(not(CONFIG_64BIT))]
39+
type isize_atomic_repr = i32;
40+
#[allow(non_camel_case_types)]
41+
#[cfg(CONFIG_64BIT)]
42+
type isize_atomic_repr = i64;
43+
44+
// Ensure size and alignment requirements are checked.
45+
static_assert!(size_of::<isize>() == size_of::<isize_atomic_repr>());
46+
static_assert!(align_of::<isize>() == align_of::<isize_atomic_repr>());
47+
static_assert!(size_of::<usize>() == size_of::<isize_atomic_repr>());
48+
static_assert!(align_of::<usize>() == align_of::<isize_atomic_repr>());
49+
50+
// SAFETY: `isize` has the same size and alignment with `isize_atomic_repr`, and is round-trip
51+
// transmutable to `isize_atomic_repr`.
52+
unsafe impl super::AtomicType for isize {
53+
type Repr = isize_atomic_repr;
54+
}
55+
56+
// SAFETY: The wrapping add result of two `isize_atomic_repr`s is a valid `usize`.
57+
unsafe impl super::AtomicAdd<isize> for isize {
58+
fn rhs_into_delta(rhs: isize) -> isize_atomic_repr {
59+
rhs as isize_atomic_repr
60+
}
61+
}
62+
3163
// SAFETY: `u32` and `i32` has the same size and alignment, and `u32` is round-trip transmutable to
3264
// `i32`.
3365
unsafe impl super::AtomicType for u32 {
@@ -54,6 +86,19 @@ unsafe impl super::AtomicAdd<u64> for u64 {
5486
}
5587
}
5688

89+
// SAFETY: `usize` has the same size and alignment with `isize_atomic_repr`, and is round-trip
90+
// transmutable to `isize_atomic_repr`.
91+
unsafe impl super::AtomicType for usize {
92+
type Repr = isize_atomic_repr;
93+
}
94+
95+
// SAFETY: The wrapping add result of two `isize_atomic_repr`s is a valid `usize`.
96+
unsafe impl super::AtomicAdd<usize> for usize {
97+
fn rhs_into_delta(rhs: usize) -> isize_atomic_repr {
98+
rhs as isize_atomic_repr
99+
}
100+
}
101+
57102
use crate::macros::kunit_tests;
58103

59104
#[kunit_tests(rust_atomics)]
@@ -73,7 +118,7 @@ mod tests {
73118

74119
#[test]
75120
fn atomic_basic_tests() {
76-
for_each_type!(42 in [i32, i64, u32, u64] |v| {
121+
for_each_type!(42 in [i32, i64, u32, u64, isize, usize] |v| {
77122
let x = Atomic::new(v);
78123

79124
assert_eq!(v, x.load(Relaxed));
@@ -82,7 +127,7 @@ mod tests {
82127

83128
#[test]
84129
fn atomic_xchg_tests() {
85-
for_each_type!(42 in [i32, i64, u32, u64] |v| {
130+
for_each_type!(42 in [i32, i64, u32, u64, isize, usize] |v| {
86131
let x = Atomic::new(v);
87132

88133
let old = v;
@@ -95,7 +140,7 @@ mod tests {
95140

96141
#[test]
97142
fn atomic_cmpxchg_tests() {
98-
for_each_type!(42 in [i32, i64, u32, u64] |v| {
143+
for_each_type!(42 in [i32, i64, u32, u64, isize, usize] |v| {
99144
let x = Atomic::new(v);
100145

101146
let old = v;
@@ -110,7 +155,7 @@ mod tests {
110155

111156
#[test]
112157
fn atomic_arithmetic_tests() {
113-
for_each_type!(42 in [i32, i64, u32, u64] |v| {
158+
for_each_type!(42 in [i32, i64, u32, u64, isize, usize] |v| {
114159
let x = Atomic::new(v);
115160

116161
assert_eq!(v, x.fetch_add(12, Full));

0 commit comments

Comments
 (0)