[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <D8A2O5QBXCRI.18W3W0EDI3UXZ@proton.me>
Date: Fri, 07 Mar 2025 13:36:00 +0000
From: Benno Lossin <benno.lossin@...ton.me>
To: Andreas Hindborg <a.hindborg@...nel.org>
Cc: 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>, 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>, Markus Elfring <Markus.Elfring@....de>, rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v10 03/13] rust: hrtimer: implement `HrTimerPointer` for `Arc`
On Fri Mar 7, 2025 at 2:27 PM CET, Andreas Hindborg wrote:
> "Benno Lossin" <benno.lossin@...ton.me> writes:
>
>> On Fri Mar 7, 2025 at 11:11 AM CET, Andreas Hindborg wrote:
>>> +impl<T> HrTimerPointer for Arc<T>
>>> +where
>>> + T: 'static,
>>> + T: Send + Sync,
>>> + T: HasHrTimer<T>,
>>> + T: for<'a> HrTimerCallback<Pointer<'a> = Self>,
>>> + Arc<T>: for<'a> RawHrTimerCallback<CallbackTarget<'a> = ArcBorrow<'a, T>>,
>>
>> I don't understand why you need this bound here.
>
> This impl is applicable only when `Arc<T> has an implementation of
> `RawTimerCallback` where CallbackTarget<'a> = ArcBorrow<'a, T>. I don't
> want the impl to be available if that is not the case.
The impl below has less strict other bounds than this one, so this bound
doesn't change anything.
> It's just an additional check.
To me it's just additional noise.
>>> +{
>>> + type TimerHandle = ArcHrTimerHandle<T>;
>>> +
>>> + fn start(self, expires: Ktime) -> ArcHrTimerHandle<T> {
>>> + // SAFETY:
>>> + // - We keep `self` alive by wrapping it in a handle below.
>>> + // - Since we generate the pointer passed to `start` from a valid
>>> + // reference, it is a valid pointer.
>>> + unsafe { T::start(Arc::as_ptr(&self), expires) };
>>> + ArcHrTimerHandle { inner: self }
>>> + }
>>> +}
>>> +
>>> +impl<T> RawHrTimerCallback for Arc<T>
>>> +where
>>> + T: 'static,
>>> + T: HasHrTimer<T>,
>>> + T: for<'a> HrTimerCallback<Pointer<'a> = Self>,
>>> +{
>>> + type CallbackTarget<'a> = ArcBorrow<'a, T>;
>>> +
>>> + unsafe extern "C" fn run(ptr: *mut bindings::hrtimer) -> bindings::hrtimer_restart {
>>> + // `HrTimer` is `repr(C)`
>>> + let timer_ptr = ptr.cast::<super::HrTimer<T>>();
>>> +
>>> + // SAFETY: By C API contract `ptr` is the pointer we passed when
>>> + // queuing the timer, so it is a `HrTimer<T>` embedded in a `T`.
>>> + let data_ptr = unsafe { T::timer_container_of(timer_ptr) };
>>> +
>>> + // SAFETY: `data_ptr` points to the `T` that was used to queue the
>>> + // timer. This `T` is contained in an `Arc`.
>>
>> You're not justifying all safety requirements of `ArcBorrow::from_raw`.
>
> How is this:
>
> // SAFETY:
> // - `data_ptr` is derived form the pointer to the `T` that was used to
> // queue the timer.
> // - The `ArcTimerHandle` associated with this timer is guaranteed to
> // be alive for the duration of the lifetime of `receiver`, so the
There is no `receiver` in this context?
Is the reason for the handle staying alive that when it is dropped, it
calls `cancel` and that waits until the callback finishes? If so, did
you write that down somewhere here?
> // refcount of the underlying `Arc` is guaranteed to be nonzero for
> // the duration.
> // - We own one refcount in the `ArcTimerHandle` associted with this
> // timer, so it is not possible to get a `UniqueArc` to this
> // allocation from other `Arc` clones.
Otherwise this sounds good.
---
Cheers,
Benno
Powered by blists - more mailing lists