[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <7c3c758d-658c-4cea-9533-9bbe7b3e285d@proton.me>
Date: Thu, 20 Feb 2025 23:18:07 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Andreas Hindborg <a.hindborg@...nel.org>, Miguel Ojeda <ojeda@...nel.org>, Anna-Maria Behnsen <anna-maria@...utronix.de>, Frederic Weisbecker <frederic@...nel.org>, Thomas Gleixner <tglx@...utronix.de>, Danilo Krummrich <dakr@...nel.org>
Cc: Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>, Gary Guo <gary@...yguo.net>, Björn Roy Baron <bjorn3_gh@...tonmail.com>, Alice Ryhl <aliceryhl@...gle.com>, Trevor Gross <tmgross@...ch.edu>, Lyude Paul <lyude@...hat.com>, Guangbo Cui <2407018371@...com>, Dirk Behme <dirk.behme@...il.com>, Daniel Almeida <daniel.almeida@...labora.com>, Tamir Duberstein <tamird@...il.com>, rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v8 06/14] rust: hrtimer: add `UnsafeHrTimerPointer`
On 18.02.25 14:27, Andreas Hindborg wrote:
> Add a trait to allow unsafely queuing stack allocated timers.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@...nel.org>
Reviewed-by: Benno Lossin <benno.lossin@...ton.me>
> ---
> rust/kernel/time/hrtimer.rs | 31 +++++++++++++++++++++++++++++++
> 1 file changed, 31 insertions(+)
>
> diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs
> index e342193f985eb..196794089f033 100644
> --- a/rust/kernel/time/hrtimer.rs
> +++ b/rust/kernel/time/hrtimer.rs
> @@ -157,6 +157,37 @@ pub trait HrTimerPointer: Sync + Sized {
> fn start(self, expires: Ktime) -> Self::TimerHandle;
> }
>
> +/// Unsafe version of [`HrTimerPointer`] for situations where leaking the
> +/// [`HrTimerHandle`] returned by `start` would be unsound. This is the case for
> +/// stack allocated timers.
> +///
> +/// Typical implementers are pinned references such as [`Pin<&T>`].
> +///
> +/// # Safety
> +///
> +/// Implementers of this trait must ensure that instances of types implementing
> +/// [`UnsafeHrTimerPointer`] outlives any associated [`HrTimerPointer::TimerHandle`]
> +/// instances.
> +pub unsafe trait UnsafeHrTimerPointer: Sync + Sized {
> + /// A handle representing a running timer.
> + ///
> + /// # Safety
> + ///
> + /// If the timer is running, or if the timer callback is executing when the
> + /// handle is dropped, the drop method of [`Self::TimerHandle`] must not return
> + /// until the timer is stopped and the callback has completed.
> + type TimerHandle: HrTimerHandle;
> +
> + /// Start the timer after `expires` time units. If the timer was already
> + /// running, it is restarted at the new expiry time.
> + ///
> + /// # Safety
> + ///
> + /// Caller promises keep the timer structure alive until the timer is dead.
> + /// Caller can ensure this by not leaking the returned [`Self::TimerHandle`].
> + unsafe fn start(self, expires: Ktime) -> Self::TimerHandle;
I have an interesting idea to make this function safe, but I don't want
to block this series.
How about we have the following signature:
fn start(self, expires: Ktime) -> impl PinInit<Self::TimerHandle>;
And in the safety requirements of the trait we ask that `TimerHandle` be
`!Unpin`. Then the TimerHandle cannot be forgotten because it is pinned
and pinned values have the drop guarantee [1].
You can then use `stack_pin_init!` to pin the value directly on the
stack (dropping it by leaving its scope will then cancel the timer &
possibly wait for the timer callback to finish running).
[1]: https://doc.rust-lang.org/std/pin/index.html#drop-guarantee
---
Cheers,
Benno
> +}
> +
> /// Implemented by [`HrTimerPointer`] implementers to give the C timer callback a
> /// function to call.
> // This is split from `HrTimerPointer` to make it easier to specify trait bounds.
>
> --
> 2.47.0
>
>
Powered by blists - more mailing lists