Rust for Linux List
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: Danilo Krummrich <dakr@kernel.org>
Cc: tj@kernel.org, jiangshanlai@gmail.com, ojeda@kernel.org,
	boqun@kernel.org,  gary@garyguo.net, bjorn3_gh@protonmail.com,
	lossin@kernel.org,  a.hindborg@kernel.org, tmgross@umich.edu,
	daniel.almeida@collabora.com,  tamird@kernel.org,
	acourbot@nvidia.com, work@onurozkan.dev,  jhubbard@nvidia.com,
	rust-for-linux@vger.kernel.org,  linux-kernel@vger.kernel.org,
	driver-core@lists.linux.dev
Subject: Re: [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items
Date: Wed, 2 Sep 2026 14:49:49 +0000	[thread overview]
Message-ID: <apg3jcchnlwTeiTN@google.com> (raw)
In-Reply-To: <20260807165252.3849875-7-dakr@kernel.org>

On Fri, Aug 07, 2026 at 06:52:49PM +0200, Danilo Krummrich wrote:
> Add ScopedWork<T>, a work item wrapper whose destructor calls
> cancel_work_sync(), allowing T to carry non-'static lifetimes. Ownership
> of the data is not transferred to the workqueue; instead, the
> synchronous cancellation on drop guarantees the work function is not
> running when the data is freed.
> 
> ScopedWork uses the existing Work/HasWork/WorkItem infrastructure with
> NonNull<ScopedWorkRef<T>> as WorkItem::Pointer for the callback path,
> and implements RawWorkItem for &ScopedWork<T> and &ScopedWorkRef<T> for
> the enqueue path (requiring T: Sync for cross-thread shared access
> safety).
> 
> Two enqueue paths are provided:
>   - Queue::enqueue_scoped() (unsafe): the caller must ensure the work
>     item is not forgotten.
>   - ScopedQueue::enqueue() (safe): when the work item's lifetime
>     satisfies the queue's 'scope bound.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>      pub fn enqueue<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
>      where
>          W: RawWorkItem<ID> + Send + 'static,
> [...]
> +    pub unsafe fn enqueue_scoped<W, const ID: u64>(&self, w: W) -> W::EnqueueOutput
> +    where
> +        W: RawWorkItem<ID> + Send,

I'm not convinced that enqueue_scoped() should be the unsafe operation.
Rather, I think that should be the constructor of ScopedWork. If we
promise to not forget it in ::new(), we can make this safe.

> +impl Deref for ScopedQueue<'_> {
> +    type Target = Queue;
> +
> +    #[inline]
> +    fn deref(&self) -> &Queue {
> +        &self.inner
> +    }
> +}

Should be part of previous patch.

> +/// The work function's view of a [`ScopedWork`] item.
> +///
> +/// The work function callback receives `&ScopedWorkRef<T>`, which [`Deref`]s to `&T` and can be
> +/// passed to queue enqueue methods for re-enqueueing from within the work function.
> +#[pin_data]
> +pub struct ScopedWorkRef<T: ScopedWorkItem> {

Structs with 'Ref' in their name sound like they are a
reference/pointer, but this struct has no indirection.

> +impl<T: ScopedWorkItem> WorkItem for ScopedWorkRef<T> {
> +    type Pointer = NonNull<Self>;
> +
> +    #[inline]
> +    fn run(this: NonNull<Self>) {
> +        // SAFETY: `this` points to a valid, pinned `ScopedWorkRef`. `cancel_work_sync()` in
> +        // `ScopedWork`'s `PinnedDrop` prevents use-after-drop.
> +        let work = unsafe { &*this.as_ptr() };
> +
> +        T::run(work);
> +    }
> +}

This is unsound because its a safe function that dereferences a raw
pointer.

I think you can avoid all of this logic by just not implementing
WorkItem. You can skip all of that and just implement RawWorkItem for
&ScopedWorkRef<T> and remove all the other implementations. You do not
need the WorkItem impl.

Alice

  parent reply	other threads:[~2026-09-02 14:49 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07 16:52 [PATCH v2 0/6] workqueue: OwnedQueue, ScopedQueue and ScopedWork Danilo Krummrich
2026-08-07 16:52 ` [PATCH v2 1/6] rust: workqueue: replace deprecated system_wq with system_{percpu,dfl}_wq Danilo Krummrich
2026-08-27 17:12   ` Daniel Almeida
2026-08-07 16:52 ` [PATCH v2 2/6] rust: workqueue: restrict delayed work to global wqs Danilo Krummrich
2026-08-27 17:13   ` Daniel Almeida
2026-08-07 16:52 ` [PATCH v2 3/6] rust: workqueue: create workqueue subdirectory Danilo Krummrich
2026-08-27 17:16   ` Daniel Almeida
2026-08-07 16:52 ` [PATCH v2 4/6] rust: workqueue: add creation of workqueues Danilo Krummrich
2026-08-07 22:39   ` Danilo Krummrich
2026-08-27 19:25     ` Daniel Almeida
2026-08-27 19:25   ` Daniel Almeida
2026-08-07 16:52 ` [PATCH v2 5/6] rust: workqueue: add ScopedQueue for lifetime bound items Danilo Krummrich
2026-08-27 21:39   ` Daniel Almeida
2026-09-02 14:40   ` Alice Ryhl
2026-08-07 16:52 ` [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items Danilo Krummrich
2026-08-07 18:35   ` Danilo Krummrich
2026-08-27 23:07   ` Daniel Almeida
2026-09-02 14:49   ` Alice Ryhl [this message]
2026-09-02 14:52   ` Gary Guo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=apg3jcchnlwTeiTN@google.com \
    --to=aliceryhl@google.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=jhubbard@nvidia.com \
    --cc=jiangshanlai@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tj@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox