xref: /linux/rust/kernel/time/hrtimer/pin.rs (revision 0074281bb6316108e0cff094bd4db78ab3eee236)
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::HrTimerMode;
8 use super::RawHrTimerCallback;
9 use super::UnsafeHrTimerPointer;
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 {
cancel(&mut self) -> bool27     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 {
drop(&mut self)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 TimerMode = <T as HasHrTimer<T>>::TimerMode;
58     type TimerHandle = PinHrTimerHandle<'a, T>;
59 
start( self, expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires, ) -> Self::TimerHandle60     unsafe fn start(
61         self,
62         expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires,
63     ) -> Self::TimerHandle {
64         // Cast to pointer
65         let self_ptr: *const T = self.get_ref();
66 
67         // SAFETY:
68         //  - As we derive `self_ptr` from a reference above, it must point to a
69         //    valid `T`.
70         //  - We keep `self` alive by wrapping it in a handle below.
71         unsafe { T::start(self_ptr, expires) };
72 
73         PinHrTimerHandle { inner: self }
74     }
75 }
76 
77 impl<'a, T> RawHrTimerCallback for Pin<&'a T>
78 where
79     T: HasHrTimer<T>,
80     T: HrTimerCallback<Pointer<'a> = Self>,
81 {
82     type CallbackTarget<'b> = Self;
83 
run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart84     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
85         // `HrTimer` is `repr(C)`
86         let timer_ptr = ptr.cast::<HrTimer<T>>();
87 
88         // SAFETY: By the safety requirement of this function, `timer_ptr`
89         // points to a `HrTimer<T>` contained in an `T`.
90         let receiver_ptr = unsafe { T::timer_container_of(timer_ptr) };
91 
92         // SAFETY:
93         //  - By the safety requirement of this function, `timer_ptr`
94         //    points to a `HrTimer<T>` contained in an `T`.
95         //  - As per the safety requirements of the trait `HrTimerHandle`, the
96         //    `PinHrTimerHandle` associated with this timer is guaranteed to
97         //    be alive until this method returns. That handle borrows the `T`
98         //    behind `receiver_ptr`, thus guaranteeing the validity of
99         //    the reference created below.
100         let receiver_ref = unsafe { &*receiver_ptr };
101 
102         // SAFETY: `receiver_ref` only exists as pinned, so it is safe to pin it
103         // here.
104         let receiver_pin = unsafe { Pin::new_unchecked(receiver_ref) };
105 
106         T::run(receiver_pin).into_c()
107     }
108 }
109