From: Andreas Hindborg <a.hindborg@kernel.org>
To: Alice Ryhl <aliceryhl@google.com>,
Boqun Feng <boqun.feng@gmail.com>, Will Deacon <will@kernel.org>,
Peter Zijlstra <peterz@infradead.org>
Cc: "Richard Henderson" <richard.henderson@linaro.org>,
"Matt Turner" <mattst88@gmail.com>,
"Magnus Lindholm" <linmag7@gmail.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Miguel Ojeda" <ojeda@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Mark Rutland" <mark.rutland@arm.com>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Frederic Weisbecker" <frederic@kernel.org>,
"Lyude Paul" <lyude@redhat.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Anna-Maria Behnsen" <anna-maria@linutronix.de>,
"John Stultz" <jstultz@google.com>,
"Stephen Boyd" <sboyd@kernel.org>,
"Alexander Viro" <viro@zeniv.linux.org.uk>,
"Christian Brauner" <brauner@kernel.org>,
"Jan Kara" <jack@suse.cz>,
linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org,
"Alice Ryhl" <aliceryhl@google.com>
Subject: Re: [PATCH 2/5] rust: sync: add READ_ONCE and WRITE_ONCE
Date: Tue, 06 Jan 2026 13:29:11 +0100 [thread overview]
Message-ID: <87ldiaj560.fsf@t14s.mail-host-address-is-not-set> (raw)
In-Reply-To: <20251231-rwonce-v1-2-702a10b85278@google.com>
"Alice Ryhl" <aliceryhl@google.com> writes:
> There are currently a few places in the kernel where we use volatile
> reads when we really should be using `READ_ONCE`. To make it possible to
> replace these with proper `READ_ONCE` calls, introduce a Rust version of
> `READ_ONCE`.
>
> I've written the code to use Rust's volatile ops directly when possible.
> This results in a small amount of code duplication, but I think it makes
> sense for READ_ONCE and WRITE_ONCE to be implemented in pure Rust when
> possible. Otherwise they would unconditionally be a function call unless
> you have a system where you can perform cross-language inlining.
>
> I considered these functions in the bindings crate instead of kernel
> crate. I actually think it would make a lot of sense. But it implies
> some annoying complications on old compilers since the #![feature()]
> invocations in kernel/lib.rs do not apply in the bindings crate.
>
> For now, we do not support using READ_ONCE on compound types even if
> they have the right size. This can be added later.
>
> This fails checkpatch due to a misordered MAINTAINERS entry, but this is
> a pre-existing problem.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
> MAINTAINERS | 2 +
> rust/helpers/helpers.c | 1 +
> rust/helpers/rwonce.c | 34 ++++++++
> rust/kernel/sync.rs | 2 +
> rust/kernel/sync/rwonce.rs | 188 +++++++++++++++++++++++++++++++++++++++++++++
> 5 files changed, 227 insertions(+)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 12f49de7fe036c2439c00f9f4c67b2219d72a4c3..1d0cae158fe2cc7d99b6a64c11176b635e2d14e4 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -4117,9 +4117,11 @@ F: arch/*/include/asm/atomic*.h
> F: include/*/atomic*.h
> F: include/linux/refcount.h
> F: scripts/atomic/
> +F: rust/helpers/rwonce.c
> F: rust/kernel/sync/atomic.rs
> F: rust/kernel/sync/atomic/
> F: rust/kernel/sync/refcount.rs
> +F: rust/kernel/sync/rwonce.rs
>
> ATTO EXPRESSSAS SAS/SATA RAID SCSI DRIVER
> M: Bradley Grove <linuxdrivers@attotech.com>
> diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
> index 79c72762ad9c4b473971e6210c9577860d2e2b08..28b79ca7844fb744e5ad128238824921c055ec82 100644
> --- a/rust/helpers/helpers.c
> +++ b/rust/helpers/helpers.c
> @@ -48,6 +48,7 @@
> #include "rcu.c"
> #include "refcount.c"
> #include "regulator.c"
> +#include "rwonce.c"
> #include "scatterlist.c"
> #include "security.c"
> #include "signal.c"
> diff --git a/rust/helpers/rwonce.c b/rust/helpers/rwonce.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..55c621678cd632e728cb925b6a4a2e34e2fc4884
> --- /dev/null
> +++ b/rust/helpers/rwonce.c
> @@ -0,0 +1,34 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +/*
> + * Copyright (C) 2025 Google LLC.
> + */
> +
> +#ifdef CONFIG_ARCH_USE_CUSTOM_READ_ONCE
> +
> +__rust_helper u8 rust_helper_read_once_1(const u8 *ptr)
> +{
> + return READ_ONCE(*ptr);
> +}
> +
> +__rust_helper u16 rust_helper_read_once_2(const u16 *ptr)
> +{
> + return READ_ONCE(*ptr);
> +}
> +
> +__rust_helper u32 rust_helper_read_once_4(const u32 *ptr)
> +{
> + return READ_ONCE(*ptr);
> +}
> +
> +__rust_helper u64 rust_helper_read_once_8(const u64 *ptr)
> +{
> + return READ_ONCE(*ptr);
> +}
> +
> +__rust_helper void *rust_helper_read_once_ptr(void * const *ptr)
> +{
> + return READ_ONCE(*ptr);
> +}
> +
> +#endif
> diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs
> index 5df87e2bd212e192b8a67644bd99f05b9d4afd75..a5bf7bdc3fa8a044786eafae39fe8844aeeef057 100644
> --- a/rust/kernel/sync.rs
> +++ b/rust/kernel/sync.rs
> @@ -20,6 +20,7 @@
> pub mod poll;
> pub mod rcu;
> mod refcount;
> +pub mod rwonce;
> mod set_once;
>
> pub use arc::{Arc, ArcBorrow, UniqueArc};
> @@ -30,6 +31,7 @@
> pub use lock::spinlock::{new_spinlock, SpinLock, SpinLockGuard};
> pub use locked_by::LockedBy;
> pub use refcount::Refcount;
> +pub use rwonce::{READ_ONCE, WRITE_ONCE};
> pub use set_once::SetOnce;
>
> /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`.
> diff --git a/rust/kernel/sync/rwonce.rs b/rust/kernel/sync/rwonce.rs
> new file mode 100644
> index 0000000000000000000000000000000000000000..a1660e43c9ef94011812d1816713cf031a73de1d
> --- /dev/null
> +++ b/rust/kernel/sync/rwonce.rs
> @@ -0,0 +1,188 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +// Copyright (C) 2025 Google LLC.
> +
> +//! Rust version of the raw `READ_ONCE`/`WRITE_ONCE` functions.
> +//!
> +//! C header: [`include/asm-generic/rwonce.h`](srctree/include/asm-generic/rwonce.h)
> +
> +/// Read the pointer once.
> +///
> +/// # Safety
> +///
> +/// It must be safe to `READ_ONCE` the `ptr` with this type.
> +#[inline(always)]
> +#[must_use]
> +#[track_caller]
> +#[expect(non_snake_case)]
I really do not think we need to have screaming snake case here. I
understand that this is what the macro looks like in C code, but we do
not need to carry that over.
Best regards,
Andreas Hindborg
next prev parent reply other threads:[~2026-01-06 12:29 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-31 12:22 [PATCH 0/5] Add READ_ONCE and WRITE_ONCE to Rust Alice Ryhl
2025-12-31 12:22 ` [PATCH 1/5] arch: add CONFIG_ARCH_USE_CUSTOM_READ_ONCE for arm64/alpha Alice Ryhl
2025-12-31 12:22 ` [PATCH 2/5] rust: sync: add READ_ONCE and WRITE_ONCE Alice Ryhl
2026-01-06 12:29 ` Andreas Hindborg [this message]
2026-01-06 12:53 ` Boqun Feng
2025-12-31 12:22 ` [PATCH 3/5] rust: sync: support using bool with READ_ONCE Alice Ryhl
2025-12-31 15:25 ` Gary Guo
2026-01-06 12:43 ` Peter Zijlstra
2026-01-06 12:51 ` Alice Ryhl
2026-01-06 18:12 ` Gary Guo
2026-01-07 8:33 ` Peter Zijlstra
2026-01-07 18:12 ` Gary Guo
2025-12-31 12:22 ` [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile Alice Ryhl
2026-01-01 2:11 ` FUJITA Tomonori
2026-01-01 4:00 ` FUJITA Tomonori
2026-01-06 12:37 ` Andreas Hindborg
2026-01-06 13:28 ` FUJITA Tomonori
2026-01-07 10:11 ` Andreas Hindborg
2026-01-07 11:22 ` FUJITA Tomonori
2026-01-07 18:21 ` Andreas Hindborg
2026-01-09 2:10 ` FUJITA Tomonori
2026-01-09 10:42 ` Andreas Hindborg
2026-01-07 11:51 ` Boqun Feng
2026-01-07 12:48 ` Andreas Hindborg
2026-01-06 15:23 ` Gary Guo
2026-01-06 18:43 ` Alice Ryhl
2026-01-07 0:47 ` John Hubbard
2026-01-07 1:08 ` Boqun Feng
2026-01-07 2:59 ` John Hubbard
2026-01-07 1:18 ` Boqun Feng
2025-12-31 12:22 ` [PATCH 5/5] rust: fs: " Alice Ryhl
2026-01-21 0:47 ` Boqun Feng
2025-12-31 15:12 ` [PATCH 0/5] Add READ_ONCE and WRITE_ONCE to Rust Gary Guo
2026-01-01 0:53 ` Alice Ryhl
2026-01-01 1:13 ` Boqun Feng
2026-01-06 12:41 ` Andreas Hindborg
2026-01-06 13:09 ` Boqun Feng
2026-01-06 14:56 ` Peter Zijlstra
2026-01-06 18:18 ` Paul E. McKenney
2026-01-06 19:28 ` Marco Elver
2026-01-09 2:09 ` Paul E. McKenney
2026-01-09 12:00 ` Marco Elver
2026-01-07 8:43 ` Peter Zijlstra
2026-01-07 19:17 ` Paul E. McKenney
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87ldiaj560.fsf@t14s.mail-host-address-is-not-set \
--to=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=anna-maria@linutronix.de \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=brauner@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dakr@kernel.org \
--cc=frederic@kernel.org \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=jack@suse.cz \
--cc=jstultz@google.com \
--cc=linmag7@gmail.com \
--cc=linux-alpha@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=lyude@redhat.com \
--cc=mark.rutland@arm.com \
--cc=mattst88@gmail.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=richard.henderson@linaro.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=sboyd@kernel.org \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
--cc=viro@zeniv.linux.org.uk \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox