1 // SPDX-License-Identifier: GPL-2.0 2 3 use super::{ 4 HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, Ktime, 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 TimerHandle = PinMutHrTimerHandle<'a, T>; 56 start(mut self, expires: Ktime) -> Self::TimerHandle57 unsafe fn start(mut self, expires: Ktime) -> Self::TimerHandle { 58 // SAFETY: 59 // - We promise not to move out of `self`. We only pass `self` 60 // back to the caller as a `Pin<&mut self>`. 61 // - The return value of `get_unchecked_mut` is guaranteed not to be null. 62 let self_ptr = unsafe { NonNull::new_unchecked(self.as_mut().get_unchecked_mut()) }; 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.as_ptr(), expires) }; 69 70 PinMutHrTimerHandle { 71 inner: self_ptr, 72 _p: PhantomData, 73 } 74 } 75 } 76 77 impl<'a, T> RawHrTimerCallback for Pin<&'a mut 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 as *mut 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 // `PinMutHrTimerHandle` associated with this timer is guaranteed to 97 // be alive until this method returns. That handle borrows the `T` 98 // behind `receiver_ptr` mutably thus guaranteeing the validity of 99 // the reference created below. 100 let receiver_ref = unsafe { &mut *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