Skip to contentSkip to conversation

Add READ_ONCE and WRITE_ONCE to Rust

PATCH In review Alice Ryhl 9 participants 44 messages Dec 31, 2025 – Jan 21

Subsystems (8)
VFS Library code arm64 Rust +4 more subsystems
lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

cover Add READ_ONCE and WRITE_ONCE to Rust

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.

A new config option CONFIG_ARCH_USE_CUSTOM_READ_ONCE is introduced so that Rust is able to use conditional compilation to implement READ_ONCE in terms of either a volatile read, or by calling into a C helper function, depending on the architecture.

This series is intended to be merged through ATOMIC INFRASTRUCTURE.

---
Alice Ryhl (5):
arch: add CONFIG_ARCH_USE_CUSTOM_READ_ONCE for arm64/alpha
rust: sync: add READ_ONCE and WRITE_ONCE
rust: sync: support using bool with READ_ONCE
rust: hrtimer: use READ_ONCE instead of read_volatile
rust: fs: use READ_ONCE instead of read_volatile

 MAINTAINERS                     |   2 +
 arch/Kconfig                    |  11 +++
 arch/alpha/Kconfig              |   1 +
 arch/alpha/include/asm/rwonce.h |   4 +-
 arch/arm64/Kconfig              |   1 +
 arch/arm64/include/asm/rwonce.h |   4 +-
 rust/helpers/helpers.c          |   1 +
 rust/helpers/rwonce.c           |  34 +++++++
 rust/kernel/fs/file.rs          |   8 +-
 rust/kernel/sync.rs             |   2 +
 rust/kernel/sync/rwonce.rs      | 207 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/time/hrtimer.rs     |   8 +-
 12 files changed, 268 insertions(+), 15 deletions(-)

---
base-commit: f8f9c1f4d0c7a64600e2ca312dec824a0bc2f1da
change-id: 20251230-rwonce-1e8d2ee0bcf9

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author maintainer
#1 lore raw

PATCH 1/5 arch: add CONFIG_ARCH_USE_CUSTOM_READ_ONCE for arm64/alpha

By exposing this variable as a config option, conditional compilation in Rust code may rely on it to determine whether it should use a volatile read or call a C helper function to perform a READ_ONCE operation.

This config option is also added on alpha for consistency, even if Rust does not support alpha right now.

---
 arch/Kconfig                    | 11 +++++++++++
 arch/alpha/Kconfig              |  1 +
 arch/alpha/include/asm/rwonce.h |  4 ++--
 arch/arm64/Kconfig              |  1 +
 arch/arm64/include/asm/rwonce.h |  4 ++--
 5 files changed, 17 insertions(+), 4 deletions(-)
arch/Kconfig +11 -0
index 31220f512b16d5cfbc259935c2d3675b60c1e25c..683176bb09e50e31f398bb92678283e5de66b282 100644
@@ -229,6 +229,17 @@ config HAVE_EFFICIENT_UNALIGNED_ACCESS
 	  See Documentation/core-api/unaligned-memory-access.rst for more
 	  information on the topic of unaligned memory accesses.
 
+config ARCH_USE_CUSTOM_READ_ONCE
+	bool
+	help
+	  Some architectures reorder address-dependent volatile loads,
+	  which means that the default implementation of READ_ONCE that
+	  relies on a volatile load is not appropriate.
+
+	  This symbol should be selected by an architecture if it
+	  redefines READ_ONCE to use a different implementation than a
+	  volatile load.
+
 config ARCH_USE_BUILTIN_BSWAP
 	bool
 	help
arch/alpha/Kconfig +1 -0
index 80367f2cf821ceb4fc29485b7b21b37d5c310765..1d5d48153ba0087554221e9412a6af0c672d3f5c 100644
@@ -11,6 +11,7 @@ config ALPHA
 	select ARCH_NO_PREEMPT
 	select ARCH_NO_SG_CHAIN
 	select ARCH_USE_CMPXCHG_LOCKREF
+	select ARCH_USE_CUSTOM_READ_ONCE if SMP
 	select FORCE_PCI
 	select PCI_DOMAINS if PCI
 	select PCI_SYSCALL if PCI
arch/alpha/include/asm/rwonce.h +2 -2
index 35542bcf92b3a883df353784bcb2d243475ccd91..c9f21aa0764625b24e8957923926d09a2eb97e7c 100644
@@ -5,7 +5,7 @@
 #ifndef __ASM_RWONCE_H
 #define __ASM_RWONCE_H
 
-#ifdef CONFIG_SMP
+#ifdef CONFIG_ARCH_USE_CUSTOM_READ_ONCE
 
 #include <asm/barrier.h>
 
@@ -28,7 +28,7 @@
 	(typeof(x))__x;							\
 })
 
-#endif /* CONFIG_SMP */
+#endif /* CONFIG_ARCH_USE_CUSTOM_READ_ONCE */
 
 #include <asm-generic/rwonce.h>
 
arch/arm64/Kconfig +1 -0
index 93173f0a09c7deb07b46ab4f16a1a0e4320dfbf1..cd16053c8302479458a05c23ba9cfb73ee50232c 100644
@@ -89,6 +89,7 @@ config ARM64
 	select ARCH_KEEP_MEMBLOCK
 	select ARCH_MHP_MEMMAP_ON_MEMORY_ENABLE
 	select ARCH_USE_CMPXCHG_LOCKREF
+	select ARCH_USE_CUSTOM_READ_ONCE if LTO
 	select ARCH_USE_GNU_PROPERTY
 	select ARCH_USE_MEMTEST
 	select ARCH_USE_QUEUED_RWLOCKS
arch/arm64/include/asm/rwonce.h +2 -2
index 78beceec10cda47b319db29d9f79d2a5df35e92d..5da6b2d6a12399a520f7a3310014de723baa278a 100644
@@ -5,7 +5,7 @@
 #ifndef __ASM_RWONCE_H
 #define __ASM_RWONCE_H
 
-#if defined(CONFIG_LTO) && !defined(__ASSEMBLER__)
+#if defined(CONFIG_ARCH_USE_CUSTOM_READ_ONCE) && !defined(__ASSEMBLER__)
 
 #include <linux/compiler_types.h>
 #include <asm/alternative-macros.h>
@@ -62,7 +62,7 @@
 })
 
 #endif	/* !BUILD_VDSO */
-#endif	/* CONFIG_LTO && !__ASSEMBLER__ */
+#endif	/* CONFIG_ARCH_USE_CUSTOM_READ_ONCE && !__ASSEMBLER__ */
 
 #include <asm-generic/rwonce.h>
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
author maintainer
#2 lore raw

PATCH 2/5 rust: sync: add READ_ONCE and WRITE_ONCE

In reply to Alice Ryhl

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.

---
 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(+)
MAINTAINERS +2 -0
index 12f49de7fe036c2439c00f9f4c67b2219d72a4c3..1d0cae158fe2cc7d99b6a64c11176b635e2d14e4 100644
@@ -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 <[email protected]>
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4b473971e6210c9577860d2e2b08..28b79ca7844fb744e5ad128238824921c055ec82 100644
@@ -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"
rust/helpers/rwonce.c new file +34 -0
new file mode 100644
index 0000000000000000000000000000000000000000..55c621678cd632e728cb925b6a4a2e34e2fc4884
@@ -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
rust/kernel/sync.rs +2 -0
index 5df87e2bd212e192b8a67644bd99f05b9d4afd75..a5bf7bdc3fa8a044786eafae39fe8844aeeef057 100644
@@ -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`.
rust/kernel/sync/rwonce.rs new file +188 -0

188 lines not on this page · raw message

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
maintainer
#3 lore raw

Re: [PATCH 2/5] rust: sync: add READ_ONCE and WRITE_ONCE

Quoted 132 lines · Alice Ryhl

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.

---
 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(+)
MAINTAINERS +2 -0
index 12f49de7fe036c2439c00f9f4c67b2219d72a4c3..1d0cae158fe2cc7d99b6a64c11176b635e2d14e4 100644
@@ -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 <[email protected]>
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4b473971e6210c9577860d2e2b08..28b79ca7844fb744e5ad128238824921c055ec82 100644
@@ -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"
rust/helpers/rwonce.c new file +34 -0
new file mode 100644
index 0000000000000000000000000000000000000000..55c621678cd632e728cb925b6a4a2e34e2fc4884
@@ -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
rust/kernel/sync.rs +2 -0
index 5df87e2bd212e192b8a67644bd99f05b9d4afd75..a5bf7bdc3fa8a044786eafae39fe8844aeeef057 100644
@@ -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`.
rust/kernel/sync/rwonce.rs new file +7 -0
new file mode 100644
index 0000000000000000000000000000000000000000..a1660e43c9ef94011812d1816713cf031a73de1d
@@ -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)

rust/kernel/sync/rwonce.rs line 17

Quoted 10 lines
rust/kernel/sync/rwonce.rs new file +10 -0
+
+/// 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

#4 lore raw

Re: [PATCH 2/5] rust: sync: add READ_ONCE and WRITE_ONCE

Quoted 140 lines · Andreas Hindborg
Quoted 138 lines · Alice Ryhl

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.

---
 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(+)
MAINTAINERS +2 -0
index 12f49de7fe036c2439c00f9f4c67b2219d72a4c3..1d0cae158fe2cc7d99b6a64c11176b635e2d14e4 100644
@@ -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 <[email protected]>
rust/helpers/helpers.c +1 -0
index 79c72762ad9c4b473971e6210c9577860d2e2b08..28b79ca7844fb744e5ad128238824921c055ec82 100644
@@ -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"
rust/helpers/rwonce.c new file +34 -0
new file mode 100644
index 0000000000000000000000000000000000000000..55c621678cd632e728cb925b6a4a2e34e2fc4884
@@ -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
rust/kernel/sync.rs +2 -0
index 5df87e2bd212e192b8a67644bd99f05b9d4afd75..a5bf7bdc3fa8a044786eafae39fe8844aeeef057 100644
@@ -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`.
rust/kernel/sync/rwonce.rs new file +13 -0
new file mode 100644
index 0000000000000000000000000000000000000000..a1660e43c9ef94011812d1816713cf031a73de1d
@@ -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.
Quoted 9 lines

rust/kernel/sync/rwonce.rs line 17

Quoted 4 lines
rust/kernel/sync/rwonce.rs new file +4 -0
+#[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.

We should really do a atomic_load and atomic_store (implemented by Atomic::from_ptr()) to those usages:

    unsafe atomic_load<T: AtomicType, O: AcquireOrRelaxed>(ptr: *mut T, ordering: O) -> T {
        Atomic::from_ptr(ptr).load(ordering)
    }

    unsafe atomic_store<T: AtomicType, O: AcquireOrRelaxed>(ptr: *mut T, v: T, ordering: O) {
        Atomic::from_ptr(ptr).store(v, ordering);
    }

This unifies the underlying implementation and clarify the atomicity of these operations.

Regards,
Boqun

Quoted 4 lines

Best regards,
Andreas Hindborg

author maintainer
#5 lore raw

PATCH 3/5 rust: sync: support using bool with READ_ONCE

In reply to Alice Ryhl

Normally it is undefined behavior for a bool to take any value other than 0 or 1. However, in the case of READ_ONCE(some_bool) is used, this UB seems dangerous and unnecessary. I can easily imagine some Rust code that looks like this:

	if READ_ONCE(&raw const (*my_c_struct).my_bool_field) {
	    ...
	}

And by making an analogy to what the equivalent C code is, anyone writing this probably just meant to treat any non-zero value as true.

For WRITE_ONCE no special logic is required.

---
 rust/kernel/sync/rwonce.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
rust/kernel/sync/rwonce.rs +19 -0
index a1660e43c9ef94011812d1816713cf031a73de1d..73477f53131926996614df573b2d50fff98e624f 100644
@@ -163,6 +163,7 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
 // sizes, so picking the wrong helper should lead to a build error.
 
 impl_rw_once_type! {
+    bool, read_once_bool, write_once_1;
     u8,   read_once_1, write_once_1;
     i8,   read_once_1, write_once_1;
     u16,  read_once_2, write_once_2;
@@ -186,3 +187,21 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
     usize, read_once_8, write_once_8;
     isize, read_once_8, write_once_8;
 }
+
+/// Read an integer as a boolean once.
+///
+/// Returns `true` if the value behind the pointer is non-zero. Otherwise returns `false`.
+///
+/// # Safety
+///
+/// It must be safe to `READ_ONCE` the `ptr` with type `u8`.
+#[inline(always)]
+#[track_caller]
+unsafe fn read_once_bool(ptr: *const bool) -> bool {
+    // Implement `read_once_bool` in terms of `read_once_1`. The arch-specific logic is inside
+    // of `read_once_1`.
+    //
+    // SAFETY: It is safe to `READ_ONCE` the `ptr` with type `u8`.
+    let byte = unsafe { read_once_1(ptr.cast::<u8>()) };
+    byte != 0u8
+}
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
maintainer
#6 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

Quoted 11 lines · On Wed, 31 Dec 2025 12:22:27 +0000 Alice Ryhl <[email protected]> wrote:

Normally it is undefined behavior for a bool to take any value other than 0 or 1. However, in the case of READ_ONCE(some_bool) is used, this UB seems dangerous and unnecessary. I can easily imagine some Rust code that looks like this:

	if READ_ONCE(&raw const (*my_c_struct).my_bool_field) {
	    ...
	}

And by making an analogy to what the equivalent C code is, anyone writing this probably just meant to treat any non-zero value as true.

In C, bool can only hold value false and true, too, and putting any other value there is going to be UB.

The C language provides automatic cast so when you write an integer to it, non-zero values will cause true to be written. However, you're not allowed to cast it into a char ptr and write other values into it.

So I think there shouldn't be any special treatment to boolean type in this regard.

Best,
Gary

Quoted 43 lines

For WRITE_ONCE no special logic is required.

---
 rust/kernel/sync/rwonce.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
rust/kernel/sync/rwonce.rs +19 -0
index a1660e43c9ef94011812d1816713cf031a73de1d..73477f53131926996614df573b2d50fff98e624f 100644
@@ -163,6 +163,7 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
 // sizes, so picking the wrong helper should lead to a build error.
 
 impl_rw_once_type! {
+    bool, read_once_bool, write_once_1;
     u8,   read_once_1, write_once_1;
     i8,   read_once_1, write_once_1;
     u16,  read_once_2, write_once_2;
@@ -186,3 +187,21 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
     usize, read_once_8, write_once_8;
     isize, read_once_8, write_once_8;
 }
+
+/// Read an integer as a boolean once.
+///
+/// Returns `true` if the value behind the pointer is non-zero. Otherwise returns `false`.
+///
+/// # Safety
+///
+/// It must be safe to `READ_ONCE` the `ptr` with type `u8`.
+#[inline(always)]
+#[track_caller]
+unsafe fn read_once_bool(ptr: *const bool) -> bool {
+    // Implement `read_once_bool` in terms of `read_once_1`. The arch-specific logic is inside
+    // of `read_once_1`.
+    //
+    // SAFETY: It is safe to `READ_ONCE` the `ptr` with type `u8`.
+    let byte = unsafe { read_once_1(ptr.cast::<u8>()) };
+    byte != 0u8
+}
#7 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

In reply to Alice Ryhl
Quoted 43 lines · Alice Ryhl

Normally it is undefined behavior for a bool to take any value other than 0 or 1. However, in the case of READ_ONCE(some_bool) is used, this UB seems dangerous and unnecessary. I can easily imagine some Rust code that looks like this:

	if READ_ONCE(&raw const (*my_c_struct).my_bool_field) {
	    ...
	}

And by making an analogy to what the equivalent C code is, anyone writing this probably just meant to treat any non-zero value as true.

For WRITE_ONCE no special logic is required.

---
 rust/kernel/sync/rwonce.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
rust/kernel/sync/rwonce.rs +9 -0
index a1660e43c9ef94011812d1816713cf031a73de1d..73477f53131926996614df573b2d50fff98e624f 100644
@@ -163,6 +163,7 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
 // sizes, so picking the wrong helper should lead to a build error.
 
 impl_rw_once_type! {
+    bool, read_once_bool, write_once_1;
     u8,   read_once_1, write_once_1;
     i8,   read_once_1, write_once_1;
     u16,  read_once_2, write_once_2;
@@ -186,3 +187,21 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
     usize, read_once_8, write_once_8;
     isize, read_once_8, write_once_8;
 }
+
+/// Read an integer as a boolean once.
+///
+/// Returns `true` if the value behind the pointer is non-zero. Otherwise returns `false`.
+///
+/// # Safety
+///
+/// It must be safe to `READ_ONCE` the `ptr` with type `u8`.

rust/kernel/sync/rwonce.rs unsafe fn write_once(ptr: *mut Self, val: Self) { · line 207

Quoted 10 lines
rust/kernel/sync/rwonce.rs +10 -0
+#[inline(always)]
+#[track_caller]
+unsafe fn read_once_bool(ptr: *const bool) -> bool {
+    // Implement `read_once_bool` in terms of `read_once_1`. The arch-specific logic is inside
+    // of `read_once_1`.
+    //
+    // SAFETY: It is safe to `READ_ONCE` the `ptr` with type `u8`.
+    let byte = unsafe { read_once_1(ptr.cast::<u8>()) };
+    byte != 0u8
+}

Does this hardcode that sizeof(_Bool) == 1? There are ABIs where this is not the case.

author maintainer
#8 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Alice Ryhl
rust/kernel/sync/rwonce.rs +12 -0
+/// Read an integer as a boolean once.
+///
+/// Returns `true` if the value behind the pointer is non-zero. Otherwise returns `false`.
+///
+/// # Safety
+///
+/// It must be safe to `READ_ONCE` the `ptr` with type `u8`.
+#[inline(always)]
+#[track_caller]
+unsafe fn read_once_bool(ptr: *const bool) -> bool {
+    // Implement `read_once_bool` in terms of `read_once_1`. The arch-specific logic is inside
+    // of `read_once_1`.
Quoted 8 lines

rust/kernel/sync/rwonce.rs unsafe fn write_once(ptr: *mut Self, val: Self) { · line 207

Quoted 5 lines
rust/kernel/sync/rwonce.rs +5 -0
+    //
+    // SAFETY: It is safe to `READ_ONCE` the `ptr` with type `u8`.
+    let byte = unsafe { read_once_1(ptr.cast::<u8>()) };
+    byte != 0u8
+}

Does this hardcode that sizeof(_Bool) == 1? There are ABIs where this is not the case.

Hm, it hardcodes that the Rust type called bool is sizeof(_) == 1. Presumably bindgen will not translate _Bool to bool when it appears in C types on such platforms. But I don't really know - I have not looked into this case.

Alice

maintainer
#9 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

In reply to Peter Zijlstra
Quoted 49 lines · On Tue, 6 Jan 2026 13:43:26 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 48 lines · Alice Ryhl

Normally it is undefined behavior for a bool to take any value other than 0 or 1. However, in the case of READ_ONCE(some_bool) is used, this UB seems dangerous and unnecessary. I can easily imagine some Rust code that looks like this:

	if READ_ONCE(&raw const (*my_c_struct).my_bool_field) {
	    ...
	}

And by making an analogy to what the equivalent C code is, anyone writing this probably just meant to treat any non-zero value as true.

For WRITE_ONCE no special logic is required.

---
 rust/kernel/sync/rwonce.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
rust/kernel/sync/rwonce.rs +14 -0
index a1660e43c9ef94011812d1816713cf031a73de1d..73477f53131926996614df573b2d50fff98e624f 100644
@@ -163,6 +163,7 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
 // sizes, so picking the wrong helper should lead to a build error.
 
 impl_rw_once_type! {
+    bool, read_once_bool, write_once_1;
     u8,   read_once_1, write_once_1;
     i8,   read_once_1, write_once_1;
     u16,  read_once_2, write_once_2;
@@ -186,3 +187,21 @@ unsafe fn write_once(ptr: *mut Self, val: Self) {
     usize, read_once_8, write_once_8;
     isize, read_once_8, write_once_8;
 }
+
+/// Read an integer as a boolean once.
+///
+/// Returns `true` if the value behind the pointer is non-zero. Otherwise returns `false`.
+///
+/// # Safety
+///
+/// It must be safe to `READ_ONCE` the `ptr` with type `u8`.
+#[inline(always)]
+#[track_caller]
+unsafe fn read_once_bool(ptr: *const bool) -> bool {
+    // Implement `read_once_bool` in terms of `read_once_1`. The arch-specific logic is inside
+    // of `read_once_1`.
Quoted 8 lines

rust/kernel/sync/rwonce.rs unsafe fn write_once(ptr: *mut Self, val: Self) { · line 207

Quoted 5 lines
rust/kernel/sync/rwonce.rs +5 -0
+    //
+    // SAFETY: It is safe to `READ_ONCE` the `ptr` with type `u8`.
+    let byte = unsafe { read_once_1(ptr.cast::<u8>()) };
+    byte != 0u8
+}  

Does this hardcode that sizeof(_Bool) == 1? There are ABIs where this is not the case.

Hi Peter,

Do you have a concrete example on which ABI/arch this is not true?

I know that the C spec doesn't mandate _Bool and char are of the same size but we have tons of assumptions that is not guaranteed by standard C..

Best,
Gary

#10 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

Quoted 6 lines · Gary Guo
Quoted 2 lines · On Tue, 6 Jan 2026 13:43:26 +0100 Peter Zijlstra <[email protected]> wrote:

Does this hardcode that sizeof(_Bool) == 1? There are ABIs where this is not the case.

Quoted 6 lines

Hi Peter,

Do you have a concrete example on which ABI/arch this is not true?

I know that the C spec doesn't mandate _Bool and char are of the same size but we have tons of assumptions that is not guaranteed by standard C..

Darwin/PowerPC famously has sizeof(_Bool) == 4

Win32: Visual C++ 4.2 (and earlier) had sizeof(bool)==4 (they mapped bool to int), while Visual C++ 5.0 introduced a native _Bool and moved to 1 byte.

Early RISC CPUs (MIPS, PowerPC, Alpha) had severe penalties for byte access and their compilers would've had sizeof(bool)=={4,8}.

I think AVR/Arduino also has sizeof(bool) == sizeof(int) which is 2.

maintainer
#11 lore raw

Re: [PATCH 3/5] rust: sync: support using bool with READ_ONCE

Quoted 16 lines · On Wed, 7 Jan 2026 09:33:27 +0100 Peter Zijlstra <[email protected]> wrote:
Quoted 6 lines · Gary Guo
Quoted 2 lines · On Tue, 6 Jan 2026 13:43:26 +0100 Peter Zijlstra <[email protected]> wrote:

Does this hardcode that sizeof(_Bool) == 1? There are ABIs where this is not the case.

Quoted 6 lines

Hi Peter,

Do you have a concrete example on which ABI/arch this is not true?

I know that the C spec doesn't mandate _Bool and char are of the same size but we have tons of assumptions that is not guaranteed by standard C..

Darwin/PowerPC famously has sizeof(_Bool) == 4

Quoted 10 lines

Win32: Visual C++ 4.2 (and earlier) had sizeof(bool)==4 (they mapped bool to int), while Visual C++ 5.0 introduced a native _Bool and moved to 1 byte.

Early RISC CPUs (MIPS, PowerPC, Alpha) had severe penalties for byte access and their compilers would've had sizeof(bool)=={4,8}.

I think AVR/Arduino also has sizeof(bool) == sizeof(int) which is 2.

It sounds like that none of these matter for the kernel?

In which case I think it's good to keep the assertion; if someone is ought to introduce a new arch where _Bool (or Rust bool) is not 1 then we should know about it.

Best,
Gary

author maintainer
#12 lore raw

PATCH 4/5 rust: hrtimer: use READ_ONCE instead of read_volatile

In reply to Alice Ryhl

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +3 -5
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }
     }
 }
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#13 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +6 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}
rust/kernel/time/hrtimer.rs +1 -8
index 856d2d929a00..61e656a65216 100644
@@ -237,14 +237,7 @@ pub fn expires(&self) -> HrTimerInstant<T>
 
         // SAFETY:
         // - Timers cannot have negative ktime_t values as their expiration time.
-        // - There's no actual locking here, a racy read is fine and expected
-        unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
-        }
+        unsafe { Instant::from_ktime(bindings::hrtimer_get_expires(c_timer_ptr)) }
     }
 }
 
#14 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer) +{
+ return hrtimer_get_expires(timer);
+}

Quoted 21 lines
rust/kernel/time/hrtimer.rs +1 -8
index 856d2d929a00..61e656a65216 100644
@@ -237,14 +237,7 @@ pub fn expires(&self) -> HrTimerInstant<T>
 
         // SAFETY:
         // - Timers cannot have negative ktime_t values as their expiration time.
-        // - There's no actual locking here, a racy read is fine and expected
-        unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
-        }
+        unsafe { Instant::from_ktime(bindings::hrtimer_get_expires(c_timer_ptr)) }
     }
 }
 
maintainer
#15 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 4 lines
rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

Best regards,
Andreas Hindborg

#16 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 4 lines
rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

maintainer
#17 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 72 lines · FUJITA Tomonori
Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 4 lines
rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 6 lines
Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

Best regards,
Andreas Hindborg

#18 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 2 lines · Andreas Hindborg

FUJITA Tomonori <[email protected]> writes:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 1 line

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 8 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

I took a look into this, the current C code is probably fine (i.e. without READ_ONCE() or WRITE_ONCE()) because the accesses are

1) protected by timer base locking or
2) in a timer callback which provides exclusive accesses to .expires as well. Note that hrtimer_cancel() doesn't need to access .expires, so a timer callback racing with a hrtimer_cancel() is fine.

(I may miss one or two cases, but most of the cases are fine)

The problem in Rust code is that HrTimer::expires() is a pub function, so in 2) a HrTimer::expires() can race with hrtimer_forward(), which causes data races.

We either change hrtimer C code to support such a usage (against data races) or change the usage of this HrTimer::expires() function. Using READ_ONCE() here won't work. (Yes, we could say assuming all plain writes on .expires in C are atomic as some other code does, but hrtimer doesn't rely on this, so I don't think we should either)

Regards,
Boqun

Quoted 5 lines

Best regards,
Andreas Hindborg

maintainer
#19 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 36 lines · Boqun Feng
Quoted 2 lines · Andreas Hindborg

FUJITA Tomonori <[email protected]> writes:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines
Quoted 1 line

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 8 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

I took a look into this, the current C code is probably fine (i.e. without READ_ONCE() or WRITE_ONCE()) because the accesses are

1) protected by timer base locking or
2) in a timer callback which provides exclusive accesses to .expires as well. Note that hrtimer_cancel() doesn't need to access .expires, so a timer callback racing with a hrtimer_cancel() is fine.

(I may miss one or two cases, but most of the cases are fine)

The problem in Rust code is that HrTimer::expires() is a pub function, so in 2) a HrTimer::expires() can race with hrtimer_forward(), which causes data races.

Quoted 5 lines

We either change hrtimer C code to support such a usage (against data races) or change the usage of this HrTimer::expires() function. Using READ_ONCE() here won't work. (Yes, we could say assuming all plain writes on .expires in C are atomic as some other code does, but hrtimer doesn't rely on this, so I don't think we should either)

I don't think we should change the C code, I think the Rust API is simply wrong. The function should have same constraints as forward_now, ie. call while having exclusive access to the timer (during setup for instance), or in callback context.

We should change it to take self: Pin<&mut Self> and add it on HrTimerCallbackContext as well.

@Tomo, do you know of any users of this function?

Best regards,
Andreas Hindborg

#20 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

In reply to Andreas Hindborg
Quoted 78 lines · On Wed, 07 Jan 2026 11:11:43 +0100 Andreas Hindborg <[email protected]>…
Quoted 76 lines · FUJITA Tomonori
Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 4 lines
rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 7 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

The C side updates node.expires without WRITE_ONCE()/atomics so a Rust-side READ_ONCE() can still observe a torn value; I think that this is still a data race / UB from Rust's perspective.

And since expires is 64-bit, WRITE_ONCE() on 32-bit architectures does not inherently guarantee tear-free stores either.

I think that the expires() method should follow the same safety requirements as raw_forward(): it should only be considered safe when holding exclusive access to hrtimer or within the context of the timer callback. Under those conditions, it would be fine to call C's hrtimer_get_expires().

maintainer
#21 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 93 lines · FUJITA Tomonori
Quoted 78 lines · On Wed, 07 Jan 2026 11:11:43 +0100 Andreas Hindborg <[email protected]>…
Quoted 76 lines · FUJITA Tomonori
Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 4 lines
rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 7 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

The C side updates node.expires without WRITE_ONCE()/atomics so a Rust-side READ_ONCE() can still observe a torn value; I think that this is still a data race / UB from Rust's perspective.

Quoted 8 lines

And since expires is 64-bit, WRITE_ONCE() on 32-bit architectures does not inherently guarantee tear-free stores either.

I think that the expires() method should follow the same safety requirements as raw_forward(): it should only be considered safe when holding exclusive access to hrtimer or within the context of the timer callback. Under those conditions, it would be fine to call C's hrtimer_get_expires().

We can make it safe, please see my comment here [1].

Best regards,
Andreas Hindborg

[1] https://lore.kernel.org/r/[email protected]

#22 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 104 lines · On Wed, 07 Jan 2026 19:21:11 +0100 Andreas Hindborg <[email protected]>…
Quoted 93 lines · FUJITA Tomonori
Quoted 78 lines · On Wed, 07 Jan 2026 11:11:43 +0100 Andreas Hindborg <[email protected]>…
Quoted 76 lines · FUJITA Tomonori
Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

Review

Quoted 4 lines
(quoted diff) +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 7 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

The C side updates node.expires without WRITE_ONCE()/atomics so a Rust-side READ_ONCE() can still observe a torn value; I think that this is still a data race / UB from Rust's perspective.

Quoted 8 lines

And since expires is 64-bit, WRITE_ONCE() on 32-bit architectures does not inherently guarantee tear-free stores either.

I think that the expires() method should follow the same safety requirements as raw_forward(): it should only be considered safe when holding exclusive access to hrtimer or within the context of the timer callback. Under those conditions, it would be fine to call C's hrtimer_get_expires().

Quoted 6 lines

We can make it safe, please see my comment here [1].

Best regards,
Andreas Hindborg

[1] https://lore.kernel.org/r/[email protected]

I agree. My point was that expire() can be safe only under the same constraints as forward()/forward_now() so the API should require Pin<&mut Self> and expose it on HrTimerCallbackContext.

maintainer
#23 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 109 lines · FUJITA Tomonori
Quoted 106 lines · On Wed, 07 Jan 2026 19:21:11 +0100 Andreas Hindborg <[email protected]>…
Quoted 93 lines · FUJITA Tomonori
Quoted 78 lines · On Wed, 07 Jan 2026 11:11:43 +0100 Andreas Hindborg <[email protected]>…
Quoted 76 lines · FUJITA Tomonori
Quoted 69 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…
Quoted 59 lines · FUJITA Tomonori
Quoted 44 lines · On Thu, 01 Jan 2026 11:11:23 +0900 (JST) FUJITA Tomonori…
Quoted 14 lines · On Wed, 31 Dec 2025 12:22:28 +0000 Alice Ryhl <[email protected]> wrote:

Using READ_ONCE is the correct way to read the node.expires field.

---
 rust/kernel/time/hrtimer.rs | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 856d2d929a00892dc8eaec63cebdf547817953d3..e2b7a26f8aade972356c3eb5f6489bcda3e2e849 100644
@@ -239,11 +239,9 @@ pub fn expires(&self) -> HrTimerInstant<T>
         // - Timers cannot have negative ktime_t values as their expiration time.
         // - There's no actual locking here, a racy read is fine and expected

rust/kernel/time/hrtimer.rs pub fn expires(&self) -> HrTimerInstant<T> · line 245

Quoted 10 lines
rust/kernel/time/hrtimer.rs +3 -5
         unsafe {
-            Instant::from_ktime(
-                // This `read_volatile` is intended to correspond to a READ_ONCE call.
-                // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-                core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.expires)),
-            )
+            Instant::from_ktime(kernel::sync::READ_ONCE(
+                &raw const (*c_timer_ptr).node.expires,
+            ))
         }

Do we actually need READ_ONCE() here? I'm not sure but would it be better to call the C-side API?

rust/helpers/time.c +1 -0
index 67a36ccc3ec4..73162dea2a29 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 void rust_helper_fsleep(unsigned long usecs)

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines
rust/helpers/time.c +5 -0
@@ -38,3 +39,8 @@ void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return timer->node.expires;
+}

Sorry, of course this should be:

Review

Quoted 4 lines
(quoted diff) +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

What does "racy read" mean here?

Quoted 7 lines
Quoted 2 lines

The C side doesn't use WRITE_ONCE() or READ_ONCE for node.expires. How would using READ_ONCE() on the Rust side make a difference?

Data races like this are UB in Rust. As far as I understand, using this READ_ONCE implementation or a relaxed atomic read would make the read well defined. I am not aware if this is only the case if all writes to the location from C also use atomic operations or WRITE_ONCE. @Boqun?

The C side updates node.expires without WRITE_ONCE()/atomics so a Rust-side READ_ONCE() can still observe a torn value; I think that this is still a data race / UB from Rust's perspective.

Quoted 8 lines

And since expires is 64-bit, WRITE_ONCE() on 32-bit architectures does not inherently guarantee tear-free stores either.

I think that the expires() method should follow the same safety requirements as raw_forward(): it should only be considered safe when holding exclusive access to hrtimer or within the context of the timer callback. Under those conditions, it would be fine to call C's hrtimer_get_expires().

We can make it safe, please see my comment here [1].

Quoted 8 lines
Quoted 4 lines

Best regards,
Andreas Hindborg

[1] https://lore.kernel.org/r/[email protected]

I agree. My point was that expire() can be safe only under the same constraints as forward()/forward_now() so the API should require Pin<&mut Self> and expose it on HrTimerCallbackContext.

Do you want to send a patch?

Best regards,
Andreas Hindborg

maintainer
#24 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

In reply to Andreas Hindborg
Quoted 13 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines · FUJITA Tomonori

Sorry, of course this should be:

rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 1 line

I do not think it makes a difference that the read is done by C code.

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

Best,
Gary

author maintainer
#25 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 16 lines · Gary Guo
Quoted 13 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines · FUJITA Tomonori

Sorry, of course this should be:

rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 4 lines
Quoted 1 line

I do not think it makes a difference that the read is done by C code.

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

I maintain my position that if this is what you recommend C code does, it's confusing to not make the same recommendation for Rust abstractions to the same thing.

After all, nothing is stopping you from calling atomic_read() in C too.

Alice

#26 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 19 lines · Alice Ryhl
Quoted 18 lines · Gary Guo
Quoted 14 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines · FUJITA Tomonori

Sorry, of course this should be:

rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

Quoted 9 lines
Quoted 2 lines

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

I maintain my position that if this is what you recommend C code does, it's confusing to not make the same recommendation for Rust abstractions to the same thing.

After all, nothing is stopping you from calling atomic_read() in C too.

Hi Alice and everyone!

I'm having trouble fully understanding the latest reply, so maybe what I'm saying is actually what you just said.

Anyway, we should use READ_ONCE in both the C and Rust code. Relying on the compiler for that is no longer OK. We shouldn't be shy about fixing the C side (not that I think you have been, so far!).

thanks,

Signature · 1 line
John Hubbard
#27 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 30 lines · John Hubbard
Quoted 19 lines · Alice Ryhl
Quoted 18 lines · Gary Guo
Quoted 14 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines · FUJITA Tomonori

Sorry, of course this should be:

rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

Quoted 9 lines
Quoted 2 lines

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

I maintain my position that if this is what you recommend C code does, it's confusing to not make the same recommendation for Rust abstractions to the same thing.

After all, nothing is stopping you from calling atomic_read() in C too.

Quoted 9 lines

Hi Alice and everyone!

I'm having trouble fully understanding the latest reply, so maybe what I'm saying is actually what you just said.

Anyway, we should use READ_ONCE in both the C and Rust code. Relying on the compiler for that is no longer OK. We shouldn't be shy about fixing the C side (not that I think you have been, so far!).

Agreed on most of it, except that we should be more explicit in Rust, by using atomic_load[1] instead of READ_ONCE().

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

Regards,
Boqun

Quoted 4 lines

thanks,

Signature · 1 line
John Hubbard
#28 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

Quoted 6 lines · Boqun Feng
Quoted 5 lines · John Hubbard
Quoted 4 lines · Alice Ryhl
Quoted 3 lines · Gary Guo
Quoted 1 line · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

"FUJITA Tomonori" <[email protected]> writes:

...

Quoted 25 lines
Quoted 15 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

I do not think it makes a difference that the read is done by C code.

Quoted 9 lines
Quoted 2 lines

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

I maintain my position that if this is what you recommend C code does, it's confusing to not make the same recommendation for Rust abstractions to the same thing.

After all, nothing is stopping you from calling atomic_read() in C too.

Quoted 9 lines

Hi Alice and everyone!

I'm having trouble fully understanding the latest reply, so maybe what I'm saying is actually what you just said.

Anyway, we should use READ_ONCE in both the C and Rust code. Relying on the compiler for that is no longer OK. We shouldn't be shy about fixing the C side (not that I think you have been, so far!).

Quoted 5 lines

Agreed on most of it, except that we should be more explicit in Rust, by using atomic_load[1] instead of READ_ONCE().

[1]: https://lore.kernel.org/rust-for-linux/[email protected]/

I see. That does put things in a much clearer state, yes.

thanks,

Signature · 1 line
John Hubbard
#29 lore raw

Re: [PATCH 4/5] rust: hrtimer: use READ_ONCE instead of read_volatile

In reply to Alice Ryhl
Quoted 17 lines · Alice Ryhl
Quoted 16 lines · Gary Guo
Quoted 13 lines · On Tue, 06 Jan 2026 13:37:34 +0100 Andreas Hindborg <[email protected]>…

rust/helpers/time.c void rust_helper_udelay(unsigned long usec) · line 46

Quoted 8 lines · FUJITA Tomonori

Sorry, of course this should be:

rust/helpers/time.c +4 -0
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}

This is a potentially racy read. As far as I recall, we determined that using read_once is the proper way to handle the situation.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I do not think it makes a difference that the read is done by C code.

If that's the case I think the C code should be fixed by inserting the READ_ONCE?

I maintain my position that if this is what you recommend C code does, it's confusing to not make the same recommendation for Rust abstractions to the same thing.

The problem here is that C code should use atomic operation here, and it can be done via READ_ONCE() in C, and in Rust, it should be done by Atomic::from_ptr().load().

The recommendation is not "using READ_ONCE()" for C, it should be "using reads that are atomic here", and that's why introducing READ_ONCE() in Rust is a bad idea, because what we need here is an atomic operation not a "magical thing that C relies on so we are fine".

Quoted 3 lines

After all, nothing is stopping you from calling atomic_read() in C too.

Actually using atomic_read() in C should also work, it'll be technically correct as well.

Regards,
Boqun

Quoted 1 line

Alice

author maintainer
#30 lore raw

PATCH 5/5 rust: fs: use READ_ONCE instead of read_volatile

In reply to Alice Ryhl

Using READ_ONCE is the correct way to read the f_flags field.

---
 rust/kernel/fs/file.rs | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
rust/kernel/fs/file.rs +2 -6
index 23ee689bd2400565223181645157d832a836589f..6b07f08e7012f512e53743266096ce0076d29e1c 100644
@@ -335,12 +335,8 @@ pub fn cred(&self) -> &Credential {
     /// The flags are a combination of the constants in [`flags`].
     #[inline]
     pub fn flags(&self) -> u32 {
-        // This `read_volatile` is intended to correspond to a READ_ONCE call.
-        //
-        // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount.
-        //
-        // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-        unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() }
+        // SAFETY: The `f_flags` field of `struct file` is readable with `READ_ONCE`.
+        unsafe { kernel::sync::READ_ONCE(&raw const (*self.as_ptr()).f_flags) }
     }
 }
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#31 lore raw

Re: [PATCH 5/5] rust: fs: use READ_ONCE instead of read_volatile

Quoted 13 lines · Alice Ryhl

Using READ_ONCE is the correct way to read the f_flags field.

---
 rust/kernel/fs/file.rs | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
rust/kernel/fs/file.rs +0 -0
index 23ee689bd2400565223181645157d832a836589f..6b07f08e7012f512e53743266096ce0076d29e1c 100644
@@ -335,12 +335,8 @@ pub fn cred(&self) -> &Credential {
     /// The flags are a combination of the constants in [`flags`].

rust/kernel/fs/file.rs pub fn cred(&self) -> &Credential { · line 339

Quoted 10 lines
rust/kernel/fs/file.rs +2 -6
     #[inline]
     pub fn flags(&self) -> u32 {
-        // This `read_volatile` is intended to correspond to a READ_ONCE call.
-        //
-        // SAFETY: The file is valid because the shared reference guarantees a nonzero refcount.
-        //
-        // FIXME(read_once): Replace with `read_once` when available on the Rust side.
-        unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() }
+        // SAFETY: The `f_flags` field of `struct file` is readable with `READ_ONCE`.
+        unsafe { kernel::sync::READ_ONCE(&raw const (*self.as_ptr()).f_flags) }

Not a question directly to this patch, but for FS folks: I see we read and write f_flags normally (i.e. without *_ONCE() or any atomic), and I don't see any synchronization between these read and write (maybe I'm missing something?), if read and write can happen at the same time, it's data race. So I assume we must have some assumption on the atomicity of these accesses to f_flags, could you may share or confirm this? Thanks

Regards,
Boqun

Quoted 7 lines
rust/kernel/fs/file.rs +0 -0
     }
 }
 
Signature · 2 lines
-- 
2.52.0.351.gbe84eed79e-goog
maintainer
#32 lore raw
In reply to Alice Ryhl
Quoted 11 lines · On Wed, 31 Dec 2025 12:22:24 +0000 Alice Ryhl <[email protected]> wrote:

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.

A new config option CONFIG_ARCH_USE_CUSTOM_READ_ONCE is introduced so that Rust is able to use conditional compilation to implement READ_ONCE in terms of either a volatile read, or by calling into a C helper function, depending on the architecture.

This series is intended to be merged through ATOMIC INFRASTRUCTURE.

Hi Alice,

I would prefer not to expose the READ_ONCE/WRITE_ONCE functions, at least not with their atomic semantics.

Both callsites that you have converted should be using

Atomic::from_ptr().load(Relaxed)

Please refer to the documentation of Atomic about this. Fujita has a series that expand the type to u8/u16 if you need narrower accesses.

Best,
Gary

Quoted 28 lines

---
Alice Ryhl (5):
arch: add CONFIG_ARCH_USE_CUSTOM_READ_ONCE for arm64/alpha
rust: sync: add READ_ONCE and WRITE_ONCE
rust: sync: support using bool with READ_ONCE
rust: hrtimer: use READ_ONCE instead of read_volatile
rust: fs: use READ_ONCE instead of read_volatile

 MAINTAINERS                     |   2 +
 arch/Kconfig                    |  11 +++
 arch/alpha/Kconfig              |   1 +
 arch/alpha/include/asm/rwonce.h |   4 +-
 arch/arm64/Kconfig              |   1 +
 arch/arm64/include/asm/rwonce.h |   4 +-
 rust/helpers/helpers.c          |   1 +
 rust/helpers/rwonce.c           |  34 +++++++
 rust/kernel/fs/file.rs          |   8 +-
 rust/kernel/sync.rs             |   2 +
 rust/kernel/sync/rwonce.rs      | 207 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/time/hrtimer.rs     |   8 +-
 12 files changed, 268 insertions(+), 15 deletions(-)

---
base-commit: f8f9c1f4d0c7a64600e2ca312dec824a0bc2f1da
change-id: 20251230-rwonce-1e8d2ee0bcf9

Best regards,

author maintainer
#33 lore raw
Quoted 20 lines · Gary Guo
Quoted 11 lines · On Wed, 31 Dec 2025 12:22:24 +0000 Alice Ryhl <[email protected]> wrote:

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.

A new config option CONFIG_ARCH_USE_CUSTOM_READ_ONCE is introduced so that Rust is able to use conditional compilation to implement READ_ONCE in terms of either a volatile read, or by calling into a C helper function, depending on the architecture.

This series is intended to be merged through ATOMIC INFRASTRUCTURE.

Hi Alice,

I would prefer not to expose the READ_ONCE/WRITE_ONCE functions, at least not with their atomic semantics.

Quoted 6 lines

Both callsites that you have converted should be using

Atomic::from_ptr().load(Relaxed)

Please refer to the documentation of Atomic about this. Fujita has a series that expand the type to u8/u16 if you need narrower accesses.

Why? If we say that we're using the LKMM, then it seems confusing to not have a READ_ONCE() for cases where we interact with C code, and that C code documents that READ_ONCE() should be used.

Alice

#34 lore raw
Quoted 23 lines · Alice Ryhl
Quoted 22 lines · Gary Guo
Quoted 11 lines · On Wed, 31 Dec 2025 12:22:24 +0000 Alice Ryhl <[email protected]> wrote:

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.

A new config option CONFIG_ARCH_USE_CUSTOM_READ_ONCE is introduced so that Rust is able to use conditional compilation to implement READ_ONCE in terms of either a volatile read, or by calling into a C helper function, depending on the architecture.

This series is intended to be merged through ATOMIC INFRASTRUCTURE.

Hi Alice,

I would prefer not to expose the READ_ONCE/WRITE_ONCE functions, at least not with their atomic semantics.

Both callsites that you have converted should be using

Quoted 9 lines
Quoted 4 lines

Atomic::from_ptr().load(Relaxed)

Please refer to the documentation of Atomic about this. Fujita has a series that expand the type to u8/u16 if you need narrower accesses.

Why? If we say that we're using the LKMM, then it seems confusing to not have a READ_ONCE() for cases where we interact with C code, and that C code documents that READ_ONCE() should be used.

The problem of READ_ONCE() and WRITE_ONCE() is that the semantics is complicated. Sometimes they are used for atomicity, sometimes they are used for preventing data race. So yes, we are using LKMM in Rust as well, but whenever possible, we need to clarify the intentation of the API, using Atomic::from_ptr().load(Relaxed) helps on that front.

IMO, READ_ONCE()/WRITE_ONCE() is like a "band aid" solution to a few problems, having it would prevent us from developing a more clear view for concurrent programming.

Regards,
Boqun

Quoted 1 line

Alice

maintainer
#35 lore raw
Quoted 40 lines · Boqun Feng
Quoted 23 lines · Alice Ryhl
Quoted 22 lines · Gary Guo
Quoted 11 lines · On Wed, 31 Dec 2025 12:22:24 +0000 Alice Ryhl <[email protected]> wrote:

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.

A new config option CONFIG_ARCH_USE_CUSTOM_READ_ONCE is introduced so that Rust is able to use conditional compilation to implement READ_ONCE in terms of either a volatile read, or by calling into a C helper function, depending on the architecture.

This series is intended to be merged through ATOMIC INFRASTRUCTURE.

Hi Alice,

I would prefer not to expose the READ_ONCE/WRITE_ONCE functions, at least not with their atomic semantics.

Both callsites that you have converted should be using

Quoted 9 lines
Quoted 4 lines

Atomic::from_ptr().load(Relaxed)

Please refer to the documentation of Atomic about this. Fujita has a series that expand the type to u8/u16 if you need narrower accesses.

Why? If we say that we're using the LKMM, then it seems confusing to not have a READ_ONCE() for cases where we interact with C code, and that C code documents that READ_ONCE() should be used.

The problem of READ_ONCE() and WRITE_ONCE() is that the semantics is complicated. Sometimes they are used for atomicity, sometimes they are used for preventing data race. So yes, we are using LKMM in Rust as well, but whenever possible, we need to clarify the intentation of the API, using Atomic::from_ptr().load(Relaxed) helps on that front.

Quoted 3 lines

IMO, READ_ONCE()/WRITE_ONCE() is like a "band aid" solution to a few problems, having it would prevent us from developing a more clear view for concurrent programming.

What is the semantics of a non-atomic write in C code under lock racing with a READ_ONCE/atomic relaxed read in Rust? That is the hrtimer case.

Best regards,
Andreas Hindborg

#36 lore raw
Quoted 2 lines · Andreas Hindborg

"Boqun Feng" <[email protected]> writes:

[...]

Quoted 21 lines
Quoted 21 lines
Quoted 5 lines
Quoted 5 lines

I would prefer not to expose the READ_ONCE/WRITE_ONCE functions, at least not with their atomic semantics.

Both callsites that you have converted should be using

Quoted 9 lines
Quoted 4 lines

Atomic::from_ptr().load(Relaxed)

Please refer to the documentation of Atomic about this. Fujita has a series that expand the type to u8/u16 if you need narrower accesses.

Why? If we say that we're using the LKMM, then it seems confusing to not have a READ_ONCE() for cases where we interact with C code, and that C code documents that READ_ONCE() should be used.

The problem of READ_ONCE() and WRITE_ONCE() is that the semantics is complicated. Sometimes they are used for atomicity, sometimes they are used for preventing data race. So yes, we are using LKMM in Rust as well, but whenever possible, we need to clarify the intentation of the API, using Atomic::from_ptr().load(Relaxed) helps on that front.

Quoted 7 lines
Quoted 3 lines

IMO, READ_ONCE()/WRITE_ONCE() is like a "band aid" solution to a few problems, having it would prevent us from developing a more clear view for concurrent programming.

What is the semantics of a non-atomic write in C code under lock racing with a READ_ONCE/atomic relaxed read in Rust? That is the hrtimer case.

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

Regards,
Boqun

Quoted 6 lines

Best regards,
Andreas Hindborg

#37 lore raw
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

#38 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

Quoted 5 lines

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Agreed!

In that vein, any objections to the patch shown below?

Thanx, Paul

------------------------------------------------------------------------

lib/Kconfig.kcsan +2 -2
index 4ce4b0c0109cb..e827e24ab5d42 100644
@@ -199,7 +199,7 @@ config KCSAN_WEAK_MEMORY
 
 config KCSAN_REPORT_VALUE_CHANGE_ONLY
 	bool "Only report races where watcher observed a data value change"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  If enabled and a conflicting write is observed via a watchpoint, but
@@ -208,7 +208,7 @@ config KCSAN_REPORT_VALUE_CHANGE_ONLY
 
 config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
 	bool "Assume that plain aligned writes up to word size are atomic"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  Assume that plain aligned writes up to word size are atomic by
maintainer
#39 lore raw
Quoted 13 lines · Paul E. McKenney' via kasan-dev
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

Quoted 5 lines
Quoted 1 line

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Agreed!

In that vein, any objections to the patch shown below?

I'd be in favor, as that's what we did in the very initial version of KCSAN (we started strict and then loosened things up).

However, the fallout will be even more perceived "noise", despite being legitimate data races. These config knobs were added after much discussion in 2019/2020, somewhere around this discussion (I think that's the one that spawned KCSAN_REPORT_VALUE_CHANGE_ONLY, can't find the source for KCSAN_ASSUME_PLAIN_WRITES_ATOMIC):
https://lore.kernel.org/all/CAHk-=wgu-QXU83ai4XBnh7JJUo2NBW41XhLWf=7wrydR4=ZP0g@mail.gmail.com/

While the situation has gotten better since 2020, we still have latent data races that need some thought (given papering over things blindly with *ONCE is not right either). My recommendation these days is to just set CONFIG_KCSAN_STRICT=y for those who care (although I'd wish everyone cared the same amount :-)).

Should you feel the below change is appropriate for 2026, feel free to carry it (consider this my Ack).

However, I wasn't thinking of tightening the screws until the current set of known data races has gotten to a manageable amount (say below
50)
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce
Then again, on syzbot the config can remain unchanged.

Thanks,
-- Marco

Quoted 27 lines

Thanx, Paul

------------------------------------------------------------------------

lib/Kconfig.kcsan +2 -2
index 4ce4b0c0109cb..e827e24ab5d42 100644
@@ -199,7 +199,7 @@ config KCSAN_WEAK_MEMORY
 
 config KCSAN_REPORT_VALUE_CHANGE_ONLY
        bool "Only report races where watcher observed a data value change"
-       default y
+       default n
        depends on !KCSAN_STRICT
        help
          If enabled and a conflicting write is observed via a watchpoint, but
@@ -208,7 +208,7 @@ config KCSAN_REPORT_VALUE_CHANGE_ONLY
 
 config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
        bool "Assume that plain aligned writes up to word size are atomic"
-       default y
+       default n
        depends on !KCSAN_STRICT
        help
          Assume that plain aligned writes up to word size are atomic by
#40 lore raw
Quoted 24 lines · Marco Elver
Quoted 13 lines · Paul E. McKenney' via kasan-dev
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

Quoted 5 lines
Quoted 1 line

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Agreed!

In that vein, any objections to the patch shown below?

I'd be in favor, as that's what we did in the very initial version of KCSAN (we started strict and then loosened things up).

Quoted 6 lines

However, the fallout will be even more perceived "noise", despite being legitimate data races. These config knobs were added after much discussion in 2019/2020, somewhere around this discussion (I think that's the one that spawned KCSAN_REPORT_VALUE_CHANGE_ONLY, can't find the source for KCSAN_ASSUME_PLAIN_WRITES_ATOMIC):
https://lore.kernel.org/all/CAHk-=wgu-QXU83ai4XBnh7JJUo2NBW41XhLWf=7wrydR4=ZP0g@mail.gmail.com/

Fair point!

Quoted 6 lines

While the situation has gotten better since 2020, we still have latent data races that need some thought (given papering over things blindly with *ONCE is not right either). My recommendation these days is to just set CONFIG_KCSAN_STRICT=y for those who care (although I'd wish everyone cared the same amount :-)).

Quoted 8 lines

Should you feel the below change is appropriate for 2026, feel free to carry it (consider this my Ack).

However, I wasn't thinking of tightening the screws until the current set of known data races has gotten to a manageable amount (say below
50)
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce
Then again, on syzbot the config can remain unchanged.

Is there an easy way to map from a report to the SHA-1 that the corresponding test ran against? Probably me being blind, but I am not seeing it. Though I do very much like the symbolic names in those stack traces!

Thanx, Paul

Quoted 30 lines

Thanks,
-- Marco

Quoted 27 lines

Thanx, Paul

------------------------------------------------------------------------

lib/Kconfig.kcsan +2 -2
index 4ce4b0c0109cb..e827e24ab5d42 100644
@@ -199,7 +199,7 @@ config KCSAN_WEAK_MEMORY
 
 config KCSAN_REPORT_VALUE_CHANGE_ONLY
        bool "Only report races where watcher observed a data value change"
-       default y
+       default n
        depends on !KCSAN_STRICT
        help
          If enabled and a conflicting write is observed via a watchpoint, but
@@ -208,7 +208,7 @@ config KCSAN_REPORT_VALUE_CHANGE_ONLY
 
 config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
        bool "Assume that plain aligned writes up to word size are atomic"
-       default y
+       default n
        depends on !KCSAN_STRICT
        help
          Assume that plain aligned writes up to word size are atomic by
maintainer
#41 lore raw
Quoted 50 lines · Paul E. McKenney
Quoted 24 lines · Marco Elver
Quoted 13 lines · Paul E. McKenney' via kasan-dev
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

Quoted 5 lines
Quoted 1 line

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Agreed!

In that vein, any objections to the patch shown below?

I'd be in favor, as that's what we did in the very initial version of KCSAN (we started strict and then loosened things up).

Quoted 6 lines

However, the fallout will be even more perceived "noise", despite being legitimate data races. These config knobs were added after much discussion in 2019/2020, somewhere around this discussion (I think that's the one that spawned KCSAN_REPORT_VALUE_CHANGE_ONLY, can't find the source for KCSAN_ASSUME_PLAIN_WRITES_ATOMIC):
https://lore.kernel.org/all/CAHk-=wgu-QXU83ai4XBnh7JJUo2NBW41XhLWf=7wrydR4=ZP0g@mail.gmail.com/

Fair point!

Quoted 6 lines

While the situation has gotten better since 2020, we still have latent data races that need some thought (given papering over things blindly with *ONCE is not right either). My recommendation these days is to just set CONFIG_KCSAN_STRICT=y for those who care (although I'd wish everyone cared the same amount :-)).

Quoted 8 lines

Should you feel the below change is appropriate for 2026, feel free to carry it (consider this my Ack).

However, I wasn't thinking of tightening the screws until the current set of known data races has gotten to a manageable amount (say below
50)
https://syzkaller.appspot.com/upstream?manager=ci2-upstream-kcsan-gce
Then again, on syzbot the config can remain unchanged.

Quoted 4 lines

Is there an easy way to map from a report to the SHA-1 that the corresponding test ran against? Probably me being blind, but I am not seeing it. Though I do very much like the symbolic names in those stack traces!

When viewing a report page, at the bottom in the "Crashes" table it's in the "Commit" column.

#42 lore raw
In reply to Paul E. McKenney
Quoted 13 lines · Paul E. McKenney
Quoted 12 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

Quoted 5 lines
Quoted 1 line

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Agreed!

In that vein, any objections to the patch shown below?

Not really; although it would of course be nice if that were accompanied with a pile of cleanup patches taking out the worst offenders or somesuch ;-)

Quoted 24 lines

------------------------------------------------------------------------

lib/Kconfig.kcsan +2 -2
index 4ce4b0c0109cb..e827e24ab5d42 100644
@@ -199,7 +199,7 @@ config KCSAN_WEAK_MEMORY
 
 config KCSAN_REPORT_VALUE_CHANGE_ONLY
 	bool "Only report races where watcher observed a data value change"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  If enabled and a conflicting write is observed via a watchpoint, but
@@ -208,7 +208,7 @@ config KCSAN_REPORT_VALUE_CHANGE_ONLY
 
 config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
 	bool "Assume that plain aligned writes up to word size are atomic"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  Assume that plain aligned writes up to word size are atomic by
#43 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Paul E. McKenney
Quoted 13 lines · Peter Zijlstra
Quoted 5 lines · Boqun Feng

Some C code believes a plain write to a properly aligned location is atomic (see KCSAN_ASSUME_PLAIN_WRITES_ATOMIC, and no, this doesn't mean it's recommended to assume such), and I guess that's the case for hrtimer, if it's not much a trouble you can replace the plain write with WRITE_ONCE() on C side ;-)

GCC used to provide this guarantee, some of the older code was written on that. GCC no longer provides that guarantee (there are known cases where it breaks and all that) and newer code should not rely on this.

All such places *SHOULD* be updated to use READ_ONCE/WRITE_ONCE.

Quoted 7 lines
Quoted 3 lines

Agreed!

In that vein, any objections to the patch shown below?

Not really; although it would of course be nice if that were accompanied with a pile of cleanup patches taking out the worst offenders or somesuch ;-)

Careful what you ask for. You might get it... ;-)

Thanx, Paul

Quoted 24 lines
Quoted 24 lines

------------------------------------------------------------------------

lib/Kconfig.kcsan +2 -2
index 4ce4b0c0109cb..e827e24ab5d42 100644
@@ -199,7 +199,7 @@ config KCSAN_WEAK_MEMORY
 
 config KCSAN_REPORT_VALUE_CHANGE_ONLY
 	bool "Only report races where watcher observed a data value change"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  If enabled and a conflicting write is observed via a watchpoint, but
@@ -208,7 +208,7 @@ config KCSAN_REPORT_VALUE_CHANGE_ONLY
 
 config KCSAN_ASSUME_PLAIN_WRITES_ATOMIC
 	bool "Assume that plain aligned writes up to word size are atomic"
-	default y
+	default n
 	depends on !KCSAN_STRICT
 	help
 	  Assume that plain aligned writes up to word size are atomic by