Skip to contentSkip to conversation

rust: hrtimer: Restrict expires() to safe contexts

PATCH In review FUJITA Tomonori 3 participants 10 messages Aug 7 – Aug 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 rust: hrtimer: Restrict expires() to safe contexts

From: FUJITA Tomonori <[email protected]>

HrTimer::expires() previously read node.expires via a volatile load, which can race with C-side updates. Rework the API so it is only callable with exclusive access or from the callback context.

Introduce expires_unchecked() with an explicit safety contract, switch HrTimer::expires() to Pin<&mut Self>, add HrTimerCallbackContext::expires(), and route the read through hrtimer_get_expires() via a Rust helper.

Fixes 4b0147494275 ("rust: hrtimer: Add HrTimer::expires()")

---
v4
- Add a patch to make HrTimer repr(transparent)
v3: https://lore.kernel.org/all/[email protected]/
- Change the signature of raw_expires()
- Rename raw_expires() to expires_unchecked()
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
- Add Fixes and Closes tags
- Fix and improve comments
v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/helpers/time.c         |  6 +++++
 rust/kernel/time/hrtimer.rs | 46 ++++++++++++++++++++++++++-----------
 2 files changed, 39 insertions(+), 13 deletions(-)
rust/helpers/time.c +6 -0
index 32f495970493..ef8999621399 100644
@@ -2,6 +2,7 @@
 
 #include <linux/delay.h>
 #include <linux/ktime.h>
+#include <linux/hrtimer.h>
 #include <linux/timekeeping.h>
 
 __rust_helper void rust_helper_fsleep(unsigned long usecs)
@@ -38,3 +39,8 @@ __rust_helper void rust_helper_udelay(unsigned long usec)
 {
 	udelay(usec);
 }
+
+__rust_helper ktime_t rust_helper_hrtimer_get_expires(const struct hrtimer *timer)
+{
+	return hrtimer_get_expires(timer);
+}
rust/kernel/time/hrtimer.rs +33 -13

66 lines not on this page · raw message

base-commit: dc01dfb37b34beeefcfe1c3055364d41a4070c7e

Signature · 1 line
2.43.0
#1 lore raw

PATCH v4 2/2 rust: hrtimer: Make HrTimer repr(transparent)

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +5 -1
index 1db84cd4cbe8..04f47af3541b 100644
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.
 #[pin_data]
-#[repr(C)]
+#[repr(transparent)]
 pub struct HrTimer<T> {
     #[pin]
     timer: Opaque<bindings::hrtimer>,
rust/kernel/time/hrtimer/arc.rs +1 -1
index 7be82bcb352a..09f748f2f28c 100644
@@ -80,7 +80,7 @@ impl<T> RawHrTimerCallback for Arc<T>
     type CallbackTarget<'a> = ArcBorrow<'a, T>;
 
     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
-        // `HrTimer` is `repr(C)`
+        // `HrTimer` is `repr(transparent)`
         let timer_ptr = ptr.cast::<super::HrTimer<T>>();
 
         // SAFETY: By C API contract `ptr` is the pointer we passed when
rust/kernel/time/hrtimer/pin.rs +1 -1
index 4d39ef781697..e86dfc63eb97 100644
@@ -83,7 +83,7 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a T>
     type CallbackTarget<'b> = Self;
 
     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
-        // `HrTimer` is `repr(C)`
+        // `HrTimer` is `repr(transparent)`
         let timer_ptr = ptr.cast::<HrTimer<T>>();
 
         // SAFETY: By the safety requirement of this function, `timer_ptr`
rust/kernel/time/hrtimer/pin_mut.rs +1 -1
index 9d9447d4d57e..65172c9e55e9 100644
@@ -86,7 +86,7 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a mut T>
     type CallbackTarget<'b> = Self;
 
     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
-        // `HrTimer` is `repr(C)`
+        // `HrTimer` is `repr(transparent)`
         let timer_ptr = ptr.cast::<HrTimer<T>>();
 
         // SAFETY: By the safety requirement of this function, `timer_ptr`
rust/kernel/time/hrtimer/tbox.rs +1 -1
index aa1ee31a7195..1dd68fcf2bd6 100644
@@ -103,7 +103,7 @@ impl<T, A> RawHrTimerCallback for Pin<Box<T, A>>
     type CallbackTarget<'a> = Pin<&'a mut T>;
 
     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
-        // `HrTimer` is `repr(C)`
+        // `HrTimer` is `repr(transparent)`
         let timer_ptr = ptr.cast::<super::HrTimer<T>>();
 
         // SAFETY: By C API contract `ptr` is the pointer we passed when
Signature · 1 line
2.43.0
maintainer Reviewed-by on v4 2/2
#2 lore raw

Re: [PATCH v4 2/2] rust: hrtimer: Make HrTimer repr(transparent)

Quoted 31 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 1db84cd4cbe8..04f47af3541b 100644

rust/kernel/time/hrtimer.rs line 424

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.

Missing bullet. With that fixed:

Best regards,
Andreas Hindborg

#3 lore raw

Re: [PATCH v4 2/2] rust: hrtimer: Make HrTimer repr(transparent)

Quoted 43 lines · On Mon, 10 Aug 2026 13:07:20 +0200 Andreas Hindborg <[email protected]>…
Quoted 31 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 1db84cd4cbe8..04f47af3541b 100644

rust/kernel/time/hrtimer.rs line 424

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.

Missing bullet. With that fixed:

Quoted 1 line

This is a plain // comment, not documentation. Did you mean that you want it documented as one of the # Invariants bullets instead?

maintainer
#4 lore raw

Re: [PATCH v4 2/2] rust: hrtimer: Make HrTimer repr(transparent)

Quoted 46 lines · FUJITA Tomonori
Quoted 43 lines · On Mon, 10 Aug 2026 13:07:20 +0200 Andreas Hindborg <[email protected]>…
Quoted 31 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 1db84cd4cbe8..04f47af3541b 100644

rust/kernel/time/hrtimer.rs line 424

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.

Missing bullet. With that fixed:

Quoted 4 lines
Quoted 1 line

This is a plain // comment, not documentation. Did you mean that you want it documented as one of the # Invariants bullets instead?

Ah, thanks for clarifying, I did not see that. No I guess it is fine. Maybe add a newline?

Best regards,
Andreas Hindborg

#5 lore raw

Re: [PATCH v4 2/2] rust: hrtimer: Make HrTimer repr(transparent)

Quoted 48 lines · On Mon, 10 Aug 2026 15:28:06 +0200 Andreas Hindborg <[email protected]>…
Quoted 46 lines · FUJITA Tomonori
Quoted 43 lines · On Mon, 10 Aug 2026 13:07:20 +0200 Andreas Hindborg <[email protected]>…
Quoted 31 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 1db84cd4cbe8..04f47af3541b 100644

rust/kernel/time/hrtimer.rs line 424

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.

Missing bullet. With that fixed:

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

This is a plain // comment, not documentation. Did you mean that you want it documented as one of the # Invariants bullets instead?

Ah, thanks for clarifying, I did not see that. No I guess it is fine. Maybe add a newline?

Documentation/rust/coding-guidelines.rst gives an example where a comment follows the documentation with no blank line in between.

The existing code follows that too, so I think we should stay consistent with the documented convention here.

maintainer
#6 lore raw

Re: [PATCH v4 2/2] rust: hrtimer: Make HrTimer repr(transparent)

Quoted 56 lines · FUJITA Tomonori
Quoted 53 lines · On Mon, 10 Aug 2026 15:28:06 +0200 Andreas Hindborg <[email protected]>…
Quoted 50 lines · FUJITA Tomonori
Quoted 43 lines · On Mon, 10 Aug 2026 13:07:20 +0200 Andreas Hindborg <[email protected]>…
Quoted 31 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimerCallbackContext acquires a &HrTimer<T> from a NonNull<HrTimer<T>> while a &mut HrTimer<T> can exist at the same time. This is sound only because HrTimer's sole field is Opaque<bindings::hrtimer>, which puts every byte behind an UnsafeCell. Adding a field to HrTimer that is not Opaque would make acquiring that shared reference unsound.

Make HrTimer repr(transparent), which prevents multiple fields, so that such a refactor fails to compile instead of silently introducing unsoundness. This does not guarantee the remaining field stays behind Opaque, but it rules out the likely way of getting there.

repr(transparent) cannot be combined with repr(C), so drop the latter.

---
 rust/kernel/time/hrtimer.rs         | 6 +++++-
 rust/kernel/time/hrtimer/arc.rs     | 2 +-
 rust/kernel/time/hrtimer/pin.rs     | 2 +-
 rust/kernel/time/hrtimer/pin_mut.rs | 2 +-
 rust/kernel/time/hrtimer/tbox.rs    | 2 +-
 5 files changed, 9 insertions(+), 5 deletions(-)
rust/kernel/time/hrtimer.rs +0 -0
index 1db84cd4cbe8..04f47af3541b 100644

rust/kernel/time/hrtimer.rs line 424

Quoted 7 lines
rust/kernel/time/hrtimer.rs +4 -0
@@ -418,8 +418,12 @@
 /// # Invariants
 ///
 /// * `self.timer` is initialized by `bindings::hrtimer_setup`.
+// `repr(transparent)` is not merely about layout. `HrTimerCallbackContext` acquires a
+// `&HrTimer<T>` while a `&mut HrTimer<T>` may exist, which is sound only because every byte of
+// this type sits inside `Opaque`. Being transparent rejects a second field at compile time,
+// but it does not enforce that the remaining field stays `Opaque`.

Missing bullet. With that fixed:

Quoted 1 line

This is a plain // comment, not documentation. Did you mean that you want it documented as one of the # Invariants bullets instead?

Quoted 8 lines
Quoted 2 lines

Ah, thanks for clarifying, I did not see that. No I guess it is fine. Maybe add a newline?

Documentation/rust/coding-guidelines.rst gives an example where a comment follows the documentation with no blank line in between.

The existing code follows that too, so I think we should stay consistent with the documented convention here.

Ok then.

Best regards,
Andreas Hindborg

maintainer Acked-by on v4 1/2
#7 lore raw
In reply to FUJITA Tomonori
Quoted 6 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimer::expires() previously read node.expires via a volatile load, which can race with C-side updates. Rework the API so it is only callable with exclusive access or from the callback context.

Quoted 8 lines

Introduce expires_unchecked() with an explicit safety contract, switch HrTimer::expires() to Pin<&mut Self>, add HrTimerCallbackContext::expires(), and route the read through hrtimer_get_expires() via a Rust helper.

Fixes 4b0147494275 ("rust: hrtimer: Add HrTimer::expires()")

This looks good to me now.

@Miguel, can you take this through rust or rust-fixes? Please add the missing bullet in the invariant on patch 2.

Best regards,
Andreas Hindborg

#8 lore raw
Quoted 17 lines · On Mon, 10 Aug 2026 13:11:00 +0200 Andreas Hindborg <[email protected]>…
Quoted 6 lines · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

HrTimer::expires() previously read node.expires via a volatile load, which can race with C-side updates. Rework the API so it is only callable with exclusive access or from the callback context.

Quoted 8 lines

Introduce expires_unchecked() with an explicit safety contract, switch HrTimer::expires() to Pin<&mut Self>, add HrTimerCallbackContext::expires(), and route the read through hrtimer_get_expires() via a Rust helper.

Fixes 4b0147494275 ("rust: hrtimer: Add HrTimer::expires()")
Quoted 6 lines

This looks good to me now.

@Miguel, can you take this through rust or rust-fixes? Please add the missing bullet in the invariant on patch 2.

Miguel, on the missing bullet Andreas mentioned: we agreed to leave it as is.

Is this still fine to pick up for this cycle?

#9 lore raw
Quoted 5 lines · FUJITA Tomonori

Miguel, on the missing bullet Andreas mentioned: we agreed to leave it as is.

Is this still fine to pick up for this cycle?

For context for others, I pinged Tomonori, Andreas, Boqun and Gary to discuss whether this still had a race or not.

Andreas decided to delay the patch for now, and Tomonori sent this so the discussion continues there:

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

Thanks!

Cheers,
Miguel