Skip to content

Commit 582523d

Browse files
author
Andreas Hindborg
committed
rust: hrtimer: implement UnsafeHrTimerPointer for Pin<&T>
Allow pinned references to structs that contain a `HrTimer` node to be scheduled with the `hrtimer` subsystem. Acked-by: Frederic Weisbecker <frederic@kernel.org> Acked-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20250309-hrtimer-v3-v6-12-rc2-v12-7-73586e2bd5f1@kernel.org Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent f93b0d8 commit 582523d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

rust/kernel/time/hrtimer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,5 @@ macro_rules! impl_has_hr_timer {
433433

434434
mod arc;
435435
pub use arc::ArcHrTimerHandle;
436+
mod pin;
437+
pub use pin::PinHrTimerHandle;

rust/kernel/time/hrtimer/pin.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
use super::HasHrTimer;
4+
use super::HrTimer;
5+
use super::HrTimerCallback;
6+
use super::HrTimerHandle;
7+
use super::RawHrTimerCallback;
8+
use super::UnsafeHrTimerPointer;
9+
use crate::time::Ktime;
10+
use core::pin::Pin;
11+
12+
/// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer might be
13+
/// running.
14+
pub struct PinHrTimerHandle<'a, T>
15+
where
16+
T: HasHrTimer<T>,
17+
{
18+
pub(crate) inner: Pin<&'a T>,
19+
}
20+
21+
// SAFETY: We cancel the timer when the handle is dropped. The implementation of
22+
// the `cancel` method will block if the timer handler is running.
23+
unsafe impl<'a, T> HrTimerHandle for PinHrTimerHandle<'a, T>
24+
where
25+
T: HasHrTimer<T>,
26+
{
27+
fn cancel(&mut self) -> bool {
28+
let self_ptr: *const T = self.inner.get_ref();
29+
30+
// SAFETY: As we got `self_ptr` from a reference above, it must point to
31+
// a valid `T`.
32+
let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self_ptr) };
33+
34+
// SAFETY: As `timer_ptr` is derived from a reference, it must point to
35+
// a valid and initialized `HrTimer`.
36+
unsafe { HrTimer::<T>::raw_cancel(timer_ptr) }
37+
}
38+
}
39+
40+
impl<'a, T> Drop for PinHrTimerHandle<'a, T>
41+
where
42+
T: HasHrTimer<T>,
43+
{
44+
fn drop(&mut self) {
45+
self.cancel();
46+
}
47+
}
48+
49+
// SAFETY: We capture the lifetime of `Self` when we create a `PinHrTimerHandle`,
50+
// so `Self` will outlive the handle.
51+
unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a T>
52+
where
53+
T: Send + Sync,
54+
T: HasHrTimer<T>,
55+
T: HrTimerCallback<Pointer<'a> = Self>,
56+
{
57+
type TimerHandle = PinHrTimerHandle<'a, T>;
58+
59+
unsafe fn start(self, expires: Ktime) -> Self::TimerHandle {
60+
// Cast to pointer
61+
let self_ptr: *const T = self.get_ref();
62+
63+
// SAFETY:
64+
// - As we derive `self_ptr` from a reference above, it must point to a
65+
// valid `T`.
66+
// - We keep `self` alive by wrapping it in a handle below.
67+
unsafe { T::start(self_ptr, expires) };
68+
69+
PinHrTimerHandle { inner: self }
70+
}
71+
}
72+
73+
impl<'a, T> RawHrTimerCallback for Pin<&'a T>
74+
where
75+
T: HasHrTimer<T>,
76+
T: HrTimerCallback<Pointer<'a> = Self>,
77+
{
78+
type CallbackTarget<'b> = Self;
79+
80+
unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
81+
// `HrTimer` is `repr(C)`
82+
let timer_ptr = ptr as *mut HrTimer<T>;
83+
84+
// SAFETY: By the safety requirement of this function, `timer_ptr`
85+
// points to a `HrTimer<T>` contained in an `T`.
86+
let receiver_ptr = unsafe { T::timer_container_of(timer_ptr) };
87+
88+
// SAFETY:
89+
// - By the safety requirement of this function, `timer_ptr`
90+
// points to a `HrTimer<T>` contained in an `T`.
91+
// - As per the safety requirements of the trait `HrTimerHandle`, the
92+
// `PinHrTimerHandle` associated with this timer is guaranteed to
93+
// be alive until this method returns. That handle borrows the `T`
94+
// behind `receiver_ptr`, thus guaranteeing the validity of
95+
// the reference created below.
96+
let receiver_ref = unsafe { &*receiver_ptr };
97+
98+
// SAFETY: `receiver_ref` only exists as pinned, so it is safe to pin it
99+
// here.
100+
let receiver_pin = unsafe { Pin::new_unchecked(receiver_ref) };
101+
102+
T::run(receiver_pin).into_c()
103+
}
104+
}

0 commit comments

Comments
 (0)