[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <87zfhx2afi.fsf@kernel.org>
Date: Fri, 07 Mar 2025 15:16:17 +0100
From: Andreas Hindborg <a.hindborg@...nel.org>
To: "Benno Lossin" <benno.lossin@...ton.me>
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`
"Benno Lossin" <benno.lossin@...ton.me> writes:
> 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.
I'll drop it then.
>
>>>> +{
>>>> + 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?
It's the value returned from the call, same line.
>
> 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?
Yes, it is in the safety requirement of the `HrTimerHandle` trait.
Should I add that? It becomes quite a story.
>
>> // 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.
Cool.
Best regards,
Andreas Hindborg
Powered by blists - more mailing lists