Skip to content

Commit a6968ce

Browse files
author
Andreas Hindborg
committed
rust: hrtimer: add UnsafeHrTimerPointer
Add a trait to allow unsafely queuing stack allocated timers. Acked-by: Frederic Weisbecker <frederic@kernel.org> Acked-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Lyude Paul <lyude@redhat.com> Link: https://lore.kernel.org/r/20250309-hrtimer-v3-v6-12-rc2-v12-5-73586e2bd5f1@kernel.org Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent 94e05a6 commit a6968ce

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
@@ -188,6 +188,37 @@ pub trait HrTimerPointer: Sync + Sized {
188188
fn start(self, expires: Ktime) -> Self::TimerHandle;
189189
}
190190

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

0 commit comments

Comments
 (0)