Thread (26 messages) 26 messages, 4 authors, 4d ago

Re: [PATCH RFC 02/11] rust: sync: atomic: Add Atomic<u{8,16}>

flat view

From: "Gary Guo" <gary@garyguo.net>
Date: 2026-10-11 13:04:59
Also in: driver-core, linux-kselftest, lkml, rust-for-linux

On Sun Oct 11, 2026 at 1:52 PM BST, Thomas Weißschuh wrote:
Add generic atomic support for small unsigned integers that have an
`AtomicImpl` with the same size and alignment.

Also extend the unit tests.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
AtomicType is something that I'm getting rid of next cycle. The plan is to
convert atomic to use `kernel::mem::AsRepr`.

Best,
Gary
quoted hunk ↗ jump to hunk
---
 rust/kernel/sync/atomic/predefine.rs | 44 ++++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 9 deletions(-)
diff --git a/rust/kernel/sync/atomic/predefine.rs b/rust/kernel/sync/atomic/predefine.rs
index 3d63f40791fa..4c1fb5e08dc2 100644
--- a/rust/kernel/sync/atomic/predefine.rs
+++ b/rust/kernel/sync/atomic/predefine.rs
@@ -113,6 +113,32 @@ fn rhs_into_delta(rhs: isize) -> isize_atomic_repr {
     }
 }
 
+// SAFETY: `u8` and `i8` has the same size and alignment, and `u8` is round-trip transmutable to
+// `i8`.
+unsafe impl super::AtomicType for u8 {
+    type Repr = i8;
+}
+
+// SAFETY: The wrapping add result of two `i8`s is a valid `u8`.
+unsafe impl super::AtomicAdd<u8> for u8 {
+    fn rhs_into_delta(rhs: u8) -> i8 {
+        rhs as i8
+    }
+}
+
+// SAFETY: `u16` and `i16` has the same size and alignment, and `u16` is round-trip transmutable to
+// `i16`.
+unsafe impl super::AtomicType for u16 {
+    type Repr = i16;
+}
+
+// SAFETY: The wrapping add result of two `i16`s is a valid `u16`.
+unsafe impl super::AtomicAdd<u16> for u16 {
+    fn rhs_into_delta(rhs: u16) -> i16 {
+        rhs as i16
+    }
+}
+
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help