kernel/time/hrtimer/arc.rs
1// SPDX-License-Identifier: GPL-2.0
2
3use super::HasHrTimer;
4use super::HrTimer;
5use super::HrTimerCallback;
6use super::HrTimerCallbackContext;
7use super::HrTimerHandle;
8use super::HrTimerMode;
9use super::HrTimerPointer;
10use super::RawHrTimerCallback;
11use crate::sync::Arc;
12use crate::sync::ArcBorrow;
13
14/// A handle for an `Arc<HasHrTimer<T>>` returned by a call to
15/// [`HrTimerPointer::start`].
16pub struct ArcHrTimerHandle<T>
17where
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.
25unsafe impl<T> HrTimerHandle for ArcHrTimerHandle<T>
26where
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
42impl<T> Drop for ArcHrTimerHandle<T>
43where
44 T: HasHrTimer<T>,
45{
46 fn drop(&mut self) {
47 self.cancel();
48 }
49}
50
51impl<T> HrTimerPointer for Arc<T>
52where
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
74impl<T> RawHrTimerCallback for Arc<T>
75where
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}