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