Re: [PATCH v2 6/6] rust: workqueue: add ScopedWork for non-'static work items

From: Gary Guo

Date: Wed Sep 02 2026 - 11:17:48 EST


On Fri Aug 7, 2026 at 5:52 PM BST, 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@xxxxxxxxxx>
> ---
> rust/kernel/workqueue/mod.rs | 46 +++-
> rust/kernel/workqueue/scoped.rs | 442 +++++++++++++++++++++++++++++---
> 2 files changed, 444 insertions(+), 44 deletions(-)
>
> [snip]
>
> +impl<T: ScopedWorkItem> ScopedWork<T> {
> + /// Creates a pin-initializer for a new scoped work item.
> + ///
> + /// Use [`new_scoped_work!`] to automatically provide the lock class key.
> + #[inline]
> + pub fn new<E>(
> + name: &'static CStr,
> + key: Pin<&'static LockClassKey>,
> + init: impl PinInit<T, E>,
> + ) -> impl PinInit<Self, Error>

This should just propagate error and return `E` (you might need the `? E`
annotation for it).

> + where
> + Error: From<E>,
> + {
> + try_pin_init!(Self {
> + inner <- Opaque::pin_init(try_pin_init!(ScopedWorkRef::<T> {
> + work <- Work::new(name, key),
> + data <- init,
> + })),
> + })
> + }
> +}

Best,
Gary