Skip to content

Commit 95ba6ba

Browse files
Andreas Hindborgfbq
authored andcommitted
rust: hrtimer: add UnsafeHrTimerPointer
Add a trait to allow unsafely queuing stack allocated timers. Acked-by: Frederic Weisbecker <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Signed-off-by: Andreas Hindborg <[email protected]> Reviewed-by: Lyude Paul <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 15f00c3 commit 95ba6ba

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

rust/kernel/time/hrtimer.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,37 @@ pub trait HrTimerPointer: Sync + Sized {
181181
fn start(self, expires: Ktime) -> Self::TimerHandle;
182182
}
183183

184+
/// Unsafe version of [`HrTimerPointer`] for situations where leaking the
185+
/// [`HrTimerHandle`] returned by `start` would be unsound. This is the case for
186+
/// stack allocated timers.
187+
///
188+
/// Typical implementers are pinned references such as [`Pin<&T>`].
189+
///
190+
/// # Safety
191+
///
192+
/// Implementers of this trait must ensure that instances of types implementing
193+
/// [`UnsafeHrTimerPointer`] outlives any associated [`HrTimerPointer::TimerHandle`]
194+
/// instances.
195+
pub unsafe trait UnsafeHrTimerPointer: Sync + Sized {
196+
/// A handle representing a running timer.
197+
///
198+
/// # Safety
199+
///
200+
/// If the timer is running, or if the timer callback is executing when the
201+
/// handle is dropped, the drop method of [`Self::TimerHandle`] must not return
202+
/// until the timer is stopped and the callback has completed.
203+
type TimerHandle: HrTimerHandle;
204+
205+
/// Start the timer after `expires` time units. If the timer was already
206+
/// running, it is restarted at the new expiry time.
207+
///
208+
/// # Safety
209+
///
210+
/// Caller promises keep the timer structure alive until the timer is dead.
211+
/// Caller can ensure this by not leaking the returned [`Self::TimerHandle`].
212+
unsafe fn start(self, expires: Ktime) -> Self::TimerHandle;
213+
}
214+
184215
/// Implemented by [`HrTimerPointer`] implementers to give the C timer callback a
185216
/// function to call.
186217
// This is split from `HrTimerPointer` to make it easier to specify trait bounds.

0 commit comments

Comments
 (0)