Skip to content

Commit 0602140

Browse files
Andreas Hindborgfbq
authored andcommitted
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 <[email protected]> Signed-off-by: Andreas Hindborg <[email protected]> Reviewed-by: Lyude Paul <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 4e3e396 commit 0602140

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

rust/kernel/time/hrtimer.rs

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

429429
mod arc;
430430
pub use arc::ArcHrTimerHandle;
431+
mod pin;
432+
pub use pin::PinHrTimerHandle;

rust/kernel/time/hrtimer/pin.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
Pin<&'a T>: RawHrTimerCallback<CallbackTarget<'a> = Self>,
57+
{
58+
type TimerHandle = PinHrTimerHandle<'a, T>;
59+
60+
unsafe fn start(self, expires: Ktime) -> Self::TimerHandle {
61+
// Cast to pointer
62+
let self_ptr: *const T = <Self as core::ops::Deref>::deref(&self);
63+
64+
// SAFETY:
65+
// - As we derive `self_ptr` from a reference above, it must point to a
66+
// valid `T`.
67+
// - We keep `self` alive by wrapping it in a handle below.
68+
unsafe { T::start(self_ptr, expires) };
69+
70+
PinHrTimerHandle { inner: self }
71+
}
72+
}
73+
74+
impl<'a, T> RawHrTimerCallback for Pin<&'a T>
75+
where
76+
T: HasHrTimer<T>,
77+
T: HrTimerCallback<Pointer<'a> = Self>,
78+
{
79+
type CallbackTarget<'b> = Self;
80+
81+
unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
82+
// `HrTimer` is `repr(C)`
83+
let timer_ptr = ptr as *mut HrTimer<T>;
84+
85+
// SAFETY: By the safety requirement of this function, `timer_ptr`
86+
// points to a `HrTimer<T>` contained in an `T`.
87+
let receiver_ptr = unsafe { T::timer_container_of(timer_ptr) };
88+
89+
// SAFETY: By the safety requirement of this function, `timer_ptr`
90+
// points to a `HrTimer<T>` contained in an `T`.
91+
let receiver_ref = unsafe { &*receiver_ptr };
92+
93+
// SAFETY: `receiver_ref` only exists as pinned, so it is safe to pin it
94+
// here.
95+
let receiver_pin = unsafe { Pin::new_unchecked(receiver_ref) };
96+
97+
T::run(receiver_pin).into_c()
98+
}
99+
}

0 commit comments

Comments
 (0)