xref: /linux/rust/kernel/time/hrtimer/arc.rs (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use super::HasHrTimer;
4 use super::HrTimer;
5 use super::HrTimerCallback;
6 use super::HrTimerCallbackContext;
7 use super::HrTimerHandle;
8 use super::HrTimerMode;
9 use super::HrTimerPointer;
10 use super::RawHrTimerCallback;
11 use crate::sync::Arc;
12 use crate::sync::ArcBorrow;
13 
14 /// A handle for an `Arc<HasHrTimer<T>>` returned by a call to
15 /// [`HrTimerPointer::start`].
16 pub struct ArcHrTimerHandle<T>
17 where
18     T: HasHrTimer<T>,
19 {
20     pub(crate) inner: Arc<T>,
21 }
22 
23 // SAFETY: We implement drop below, and we cancel the timer in the drop
24 // implementation.
25 unsafe impl<T> HrTimerHandle for ArcHrTimerHandle<T>
26 where
27     T: HasHrTimer<T>,
28 {
29     fn cancel(&mut self) -> bool {
30         let self_ptr = Arc::as_ptr(&self.inner);
31 
32         // SAFETY: As we obtained `self_ptr` from a valid reference above, it
33         // must point to a valid `T`.
34         let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self_ptr) };
35 
36         // SAFETY: As `timer_ptr` points into `T` and `T` is valid, `timer_ptr`
37         // must point to a valid `HrTimer` instance.
38         unsafe { HrTimer::<T>::raw_cancel(timer_ptr) }
39     }
40 }
41 
42 impl<T> Drop for ArcHrTimerHandle<T>
43 where
44     T: HasHrTimer<T>,
45 {
46     fn drop(&mut self) {
47         self.cancel();
48     }
49 }
50 
51 impl<T> HrTimerPointer for Arc<T>
52 where
53     T: 'static,
54     T: Send + Sync,
55     T: HasHrTimer<T>,
56     T: for<'a> HrTimerCallback<Pointer<'a> = Self>,
57 {
58     type TimerMode = <T as HasHrTimer<T>>::TimerMode;
59     type TimerHandle = ArcHrTimerHandle<T>;
60 
61     fn start(
62         self,
63         expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires,
64     ) -> ArcHrTimerHandle<T> {
65         // SAFETY:
66         //  - We keep `self` alive by wrapping it in a handle below.
67         //  - Since we generate the pointer passed to `start` from a valid
68         //    reference, it is a valid pointer.
69         unsafe { T::start(Arc::as_ptr(&self), expires) };
70         ArcHrTimerHandle { inner: self }
71     }
72 }
73 
74 impl<T> RawHrTimerCallback for Arc<T>
75 where
76     T: 'static,
77     T: HasHrTimer<T>,
78     T: for<'a> HrTimerCallback<Pointer<'a> = Self>,
79 {
80     type CallbackTarget<'a> = ArcBorrow<'a, T>;
81 
82     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
83         // `HrTimer` is `repr(C)`
84         let timer_ptr = ptr.cast::<super::HrTimer<T>>();
85 
86         // SAFETY: By C API contract `ptr` is the pointer we passed when
87         // queuing the timer, so it is a `HrTimer<T>` embedded in a `T`.
88         let data_ptr = unsafe { T::timer_container_of(timer_ptr) };
89 
90         // SAFETY:
91         //  - `data_ptr` is derived form the pointer to the `T` that was used to
92         //    queue the timer.
93         //  - As per the safety requirements of the trait `HrTimerHandle`, the
94         //    `ArcHrTimerHandle` associated with this timer is guaranteed to
95         //    be alive until this method returns. That handle borrows the `T`
96         //    behind `data_ptr` thus guaranteeing the validity of
97         //    the `ArcBorrow` created below.
98         //  - We own one refcount in the `ArcTimerHandle` associated with this
99         //    timer, so it is not possible to get a `UniqueArc` to this
100         //    allocation from other `Arc` clones.
101         let receiver = unsafe { ArcBorrow::from_raw(data_ptr) };
102 
103         // SAFETY:
104         // - By C API contract `timer_ptr` is the pointer that we passed when queuing the timer, so
105         //   it is a valid pointer to a `HrTimer<T>` embedded in a `T`.
106         // - We are within `RawHrTimerCallback::run`
107         let context = unsafe { HrTimerCallbackContext::from_raw(timer_ptr) };
108 
109         T::run(receiver, context).into_c()
110     }
111 }
112