xref: /linux/rust/kernel/time/hrtimer/tbox.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::prelude::*;
12 use core::ptr::NonNull;
13 
14 /// A handle for a [`Box<HasHrTimer<T>>`] returned by a call to
15 /// [`HrTimerPointer::start`].
16 ///
17 /// # Invariants
18 ///
19 /// - `self.inner` comes from a `Box::into_raw` call.
20 pub struct BoxHrTimerHandle<T, A>
21 where
22     T: HasHrTimer<T>,
23     A: crate::alloc::Allocator,
24 {
25     pub(crate) inner: NonNull<T>,
26     _p: core::marker::PhantomData<A>,
27 }
28 
29 // SAFETY: We implement drop below, and we cancel the timer in the drop
30 // implementation.
31 unsafe impl<T, A> HrTimerHandle for BoxHrTimerHandle<T, A>
32 where
33     T: HasHrTimer<T>,
34     A: crate::alloc::Allocator,
35 {
36     fn cancel(&mut self) -> bool {
37         // SAFETY: As we obtained `self.inner` from a valid reference when we
38         // created `self`, it must point to a valid `T`.
39         let timer_ptr = unsafe { <T as HasHrTimer<T>>::raw_get_timer(self.inner.as_ptr()) };
40 
41         // SAFETY: As `timer_ptr` points into `T` and `T` is valid, `timer_ptr`
42         // must point to a valid `HrTimer` instance.
43         unsafe { HrTimer::<T>::raw_cancel(timer_ptr) }
44     }
45 }
46 
47 impl<T, A> Drop for BoxHrTimerHandle<T, A>
48 where
49     T: HasHrTimer<T>,
50     A: crate::alloc::Allocator,
51 {
52     fn drop(&mut self) {
53         self.cancel();
54         // SAFETY: By type invariant, `self.inner` came from a `Box::into_raw`
55         // call.
56         drop(unsafe { Box::<T, A>::from_raw(self.inner.as_ptr()) })
57     }
58 }
59 
60 impl<T, A> HrTimerPointer for Pin<Box<T, A>>
61 where
62     T: 'static,
63     T: Send + Sync,
64     T: HasHrTimer<T>,
65     T: for<'a> HrTimerCallback<Pointer<'a> = Pin<Box<T, A>>>,
66     A: crate::alloc::Allocator,
67 {
68     type TimerMode = <T as HasHrTimer<T>>::TimerMode;
69     type TimerHandle = BoxHrTimerHandle<T, A>;
70 
71     fn start(
72         self,
73         expires: <<T as HasHrTimer<T>>::TimerMode as HrTimerMode>::Expires,
74     ) -> Self::TimerHandle {
75         // SAFETY:
76         //  - We will not move out of this box during timer callback (we pass an
77         //    immutable reference to the callback).
78         //  - `Box::into_raw` is guaranteed to return a valid pointer.
79         let inner =
80             unsafe { NonNull::new_unchecked(Box::into_raw(Pin::into_inner_unchecked(self))) };
81 
82         // SAFETY:
83         //  - We keep `self` alive by wrapping it in a handle below.
84         //  - Since we generate the pointer passed to `start` from a valid
85         //    reference, it is a valid pointer.
86         unsafe { T::start(inner.as_ptr(), expires) };
87 
88         // INVARIANT: `inner` came from `Box::into_raw` above.
89         BoxHrTimerHandle {
90             inner,
91             _p: core::marker::PhantomData,
92         }
93     }
94 }
95 
96 impl<T, A> RawHrTimerCallback for Pin<Box<T, A>>
97 where
98     T: 'static,
99     T: HasHrTimer<T>,
100     T: for<'a> HrTimerCallback<Pointer<'a> = Pin<Box<T, A>>>,
101     A: crate::alloc::Allocator,
102 {
103     type CallbackTarget<'a> = Pin<&'a mut T>;
104 
105     unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
106         // `HrTimer` is `repr(C)`
107         let timer_ptr = ptr.cast::<super::HrTimer<T>>();
108 
109         // SAFETY: By C API contract `ptr` is the pointer we passed when
110         // queuing the timer, so it is a `HrTimer<T>` embedded in a `T`.
111         let data_ptr = unsafe { T::timer_container_of(timer_ptr) };
112 
113         // SAFETY:
114         //  - As per the safety requirements of the trait `HrTimerHandle`, the
115         //   `BoxHrTimerHandle` associated with this timer is guaranteed to
116         //   be alive until this method returns. That handle owns the `T`
117         //   behind `data_ptr` thus guaranteeing the validity of
118         //   the reference created below.
119         // - As `data_ptr` comes from a `Pin<Box<T>>`, only pinned references to
120         //   `data_ptr` exist.
121         let data_mut_ref = unsafe { Pin::new_unchecked(&mut *data_ptr) };
122 
123         // SAFETY:
124         // - By C API contract `timer_ptr` is the pointer that we passed when queuing the timer, so
125         //   it is a valid pointer to a `HrTimer<T>` embedded in a `T`.
126         // - We are within `RawHrTimerCallback::run`
127         let context = unsafe { HrTimerCallbackContext::from_raw(timer_ptr) };
128 
129         T::run(data_mut_ref, context).into_c()
130     }
131 }
132