rust: hrtimer: Restrict expires() to safe contexts
Subsystems (2)
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.
---
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
@@ -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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -103,7 +103,7 @@ impl<T, A> RawHrTimerCallback for Pin<Box<T, A>>type CallbackTarget <'a> =Pin <&'amut 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
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.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- 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
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
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.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- 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
-0rust/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
Reviewed-by Andreas Hindborg <[email protected]>
This is a plain // comment, not documentation. Did you mean that you want it documented as one of the # Invariants bullets instead?
Andreas Hindborg
maintainerRe: [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.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- 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
-0rust/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
Reviewed-by Andreas Hindborg <[email protected]>This is a plain
//comment, not documentation. Did you mean that you want it documented as one of the# Invariantsbullets instead?
Ah, thanks for clarifying, I did not see that. No I guess it is fine. Maybe add a newline?
Best regards,
Andreas Hindborg
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.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- 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
-0rust/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
Reviewed-by Andreas Hindborg <[email protected]>This is a plain
//comment, not documentation. Did you mean that you want it documented as one of the# Invariantsbullets 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.
Andreas Hindborg
maintainerRe: [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.
Suggested-by Miguel Ojeda <[email protected]>Signed-off-by FUJITA Tomonori <[email protected]>--- 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
-0rust/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
Reviewed-by Andreas Hindborg <[email protected]>This is a plain
//comment, not documentation. Did you mean that you want it documented as one of the# Invariantsbullets 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.rstgives 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
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, switchHrTimer::expires()to Pin<&mut Self>, addHrTimerCallbackContext::expires(), and route the read throughhrtimer_get_expires()via a Rust helper.Fixes 4b0147494275 ("rust: hrtimer: Add HrTimer::expires()")Signed-off-by FUJITA Tomonori <[email protected]>
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
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, switchHrTimer::expires()to Pin<&mut Self>, addHrTimerCallbackContext::expires(), and route the read throughhrtimer_get_expires()via a Rust helper.Fixes 4b0147494275 ("rust: hrtimer: Add HrTimer::expires()")Signed-off-by FUJITA Tomonori <[email protected]>
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.
Acked-by Andreas Hindborg <[email protected]>
Miguel, on the missing bullet Andreas mentioned: we agreed to leave it as is.
Is this still fine to pick up for this cycle?
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