]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rust: helpers: Add i8/i16 relaxed atomic helpers
authorFUJITA Tomonori <fujita.tomonori@gmail.com>
Thu, 11 Dec 2025 11:38:24 +0000 (20:38 +0900)
committerBoqun Feng <boqun.feng@gmail.com>
Fri, 9 Jan 2026 11:01:40 +0000 (19:01 +0800)
Add READ_ONCE/WRITE_ONCE based helpers for i8 and i16 types to support
relaxed atomic operations in Rust.

While relaxed operations could be implemented purely in Rust using
read_volatile() and write_volatile(), using C's READ_ONCE() and
WRITE_ONCE() macros ensures complete consistency with the kernel
memory model.

These helpers expose different symbol names than their C counterparts
so they are split into atomic_ext.c instead of atomic.c. The symbol
names; the names make the interface Rust/C clear, consistent with
i32/i64.

[boqun: Rename the functions from {load,store} to {read,set} to avoid
future adjustment]

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://patch.msgid.link/20251211113826.1299077-3-fujita.tomonori@gmail.com
rust/helpers/atomic_ext.c

index 1fb624147aa4a67b2601fe6dd083d6a0e438a2b7..02e05b4246ae450e01399c663ba140d1cf20e425 100644 (file)
@@ -1,22 +1,43 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <asm/barrier.h>
+#include <asm/rwonce.h>
+
+__rust_helper s8 rust_helper_atomic_i8_read(s8 *ptr)
+{
+       return READ_ONCE(*ptr);
+}
 
 __rust_helper s8 rust_helper_atomic_i8_read_acquire(s8 *ptr)
 {
        return smp_load_acquire(ptr);
 }
 
+__rust_helper s16 rust_helper_atomic_i16_read(s16 *ptr)
+{
+       return READ_ONCE(*ptr);
+}
+
 __rust_helper s16 rust_helper_atomic_i16_read_acquire(s16 *ptr)
 {
        return smp_load_acquire(ptr);
 }
 
+__rust_helper void rust_helper_atomic_i8_set(s8 *ptr, s8 val)
+{
+       WRITE_ONCE(*ptr, val);
+}
+
 __rust_helper void rust_helper_atomic_i8_set_release(s8 *ptr, s8 val)
 {
        smp_store_release(ptr, val);
 }
 
+__rust_helper void rust_helper_atomic_i16_set(s16 *ptr, s16 val)
+{
+       WRITE_ONCE(*ptr, val);
+}
+
 __rust_helper void rust_helper_atomic_i16_set_release(s16 *ptr, s16 val)
 {
        smp_store_release(ptr, val);