Skip to content

Commit cc39ac9

Browse files
Andreas Hindborgfbq
authored andcommitted
rust: hrtimer: implement UnsafeHrTimerPointer for Pin<&mut T>
Allow pinned mutable 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 0602140 commit cc39ac9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

rust/kernel/time/hrtimer.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,5 @@ mod arc;
430430
pub use arc::ArcHrTimerHandle;
431431
mod pin;
432432
pub use pin::PinHrTimerHandle;
433+
mod pin_mut;
434+
pub use pin_mut::PinMutHrTimerHandle;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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<&mut HasHrTimer>`. When the handle exists, the timer might
13+
/// be running.
14+
pub struct PinMutHrTimerHandle<'a, T>
15+
where
16+
T: HasHrTimer<T>,
17+
{
18+
pub(crate) inner: Pin<&'a mut 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 PinMutHrTimerHandle<'a, T>
24+
where
25+
T: HasHrTimer<T>,
26+
{
27+
fn cancel(&mut self) -> bool {
28+
// SAFETY: We are not moving out of `self` or handing out mutable
29+
// references to `self`.
30+
let self_ptr = unsafe { self.inner.as_mut().get_unchecked_mut() as *mut T };
31+
32+
// SAFETY: As we got `self_ptr` from a reference above, it must point to
33+
// a valid `T`.
34+
let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self_ptr) };
35+
36+
// SAFETY: As `timer_ptr` is derived from a reference, it must point to
37+
// a valid and initialized `HrTimer`.
38+
unsafe { HrTimer::<T>::raw_cancel(timer_ptr) }
39+
}
40+
}
41+
42+
impl<'a, T> Drop for PinMutHrTimerHandle<'a, T>
43+
where
44+
T: HasHrTimer<T>,
45+
{
46+
fn drop(&mut self) {
47+
self.cancel();
48+
}
49+
}
50+
51+
// SAFETY: We capture the lifetime of `Self` when we create a
52+
// `PinMutHrTimerHandle`, so `Self` will outlive the handle.
53+
unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a mut T>
54+
where
55+
T: Send + Sync,
56+
T: HasHrTimer<T>,
57+
T: HrTimerCallback<Pointer<'a> = Self>,
58+
Pin<&'a mut T>: RawHrTimerCallback<CallbackTarget<'a> = Self>,
59+
{
60+
type TimerHandle = PinMutHrTimerHandle<'a, T>;
61+
62+
unsafe fn start(self, expires: Ktime) -> Self::TimerHandle {
63+
// Cast to pointer
64+
let self_ptr: *const T = <Self as core::ops::Deref>::deref(&self);
65+
66+
// SAFETY:
67+
// - As we derive `self_ptr` from a reference above, it must point to a
68+
// valid `T`.
69+
// - We keep `self` alive by wrapping it in a handle below.
70+
unsafe { T::start(self_ptr, expires) };
71+
72+
PinMutHrTimerHandle { inner: self }
73+
}
74+
}
75+
76+
impl<'a, T> RawHrTimerCallback for Pin<&'a mut T>
77+
where
78+
T: HasHrTimer<T>,
79+
T: HrTimerCallback<Pointer<'a> = Self>,
80+
{
81+
type CallbackTarget<'b> = Self;
82+
83+
unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
84+
// `HrTimer` is `repr(C)`
85+
let timer_ptr = ptr as *mut HrTimer<T>;
86+
87+
// SAFETY: By the safety requirement of this function, `timer_ptr`
88+
// points to a `HrTimer<T>` contained in an `T`.
89+
let receiver_ptr = unsafe { T::timer_container_of(timer_ptr) };
90+
91+
// SAFETY: By the safety requirement of this function, `timer_ptr`
92+
// points to a `HrTimer<T>` contained in an `T`.
93+
let receiver_ref = unsafe { &mut *receiver_ptr };
94+
95+
// SAFETY: `receiver_ref` only exists as pinned, so it is safe to pin it
96+
// here.
97+
let receiver_pin = unsafe { Pin::new_unchecked(receiver_ref) };
98+
99+
T::run(receiver_pin).into_c()
100+
}
101+
}

0 commit comments

Comments
 (0)