kernel/time/hrtimer/pin.rs
1// SPDX-License-Identifier: GPL-2.0
2
3use super::HasHrTimer;
4use super::HrTimer;
5use super::HrTimerCallback;
6use super::HrTimerHandle;
7use super::RawHrTimerCallback;
8use super::UnsafeHrTimerPointer;
9use crate::time::Ktime;
10use core::pin::Pin;
11
12/// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer might be
13/// running.
14pub struct PinHrTimerHandle<'a, T>
15where
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.
23unsafe impl<'a, T> HrTimerHandle for PinHrTimerHandle<'a, T>
24where
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
40impl<'a, T> Drop for PinHrTimerHandle<'a, T>
41where
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.
51unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a T>
52where
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
73impl<'a, T> RawHrTimerCallback for Pin<&'a T>
74where
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}