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

From: Alice Ryhl

Date: Mon Nov 30 2026 - 12:05:09 EST


On Fri, Nov 27, 2026 at 5:12 PM Tejun Heo <tj@xxxxxxxxxx> wrote:
>
> Hello, Alice.
>
> On Thu, Nov 26, 2026 at 12:43:07PM +0000, Alice Ryhl wrote:
> > Of course, there may be other ways to enforce the non-reentrance
> > conditions hold, which could let me get rid of the lifetime. But the
> > lifetime is there to handle the cleanup before destroy_workqueue() is
> > called case, so a percpu refcount does not come into play.
>
> I didn't explain myself well. I'm not saying that the current scheme
> doesn't work. My concern is how easy the interface is to use, and I wonder
> whether taking workqueue lifetime out of the compiler-managed lifetime
> rules could make it simpler. Something like the following:
>
> - Every Rust work item and delayed work item is bound to one workqueue.
>
> - A Rust workqueue has a percpu refcnt and each work item bound to it
> holds a reference.
>
> - Destroying the workqueue waits for the refcnt to reach zero. A leaked
> work item then stalls workqueue destruction, which seems like an okay
> failure mode.
>
> Work items bound this way never move between workqueues, and neither
> delayed work items nor ScopedWork would need the workqueue to be 'static
> or carry its lifetime. This doesn't solve data or device lifetimes but it
> decouples them from the workqueue's, and device accesses can use workqueue
> destruction as the draining point.
>
> We can add whatever is needed on the workqueue side to make draining work
> well, e.g. an operation which expires a workqueue so that work items
> queued on it afterwards are dropped without running, self-freeing ones
> included.
>
> Workqueue looks the way it does because it's C and leaves all lifetime
> management to its users. Carrying the same model over to Rust and then
> wrapping it for each use case seems like a missed opportunity. I don't
> know Rust well, so I could just be blowing hot air.

I guess we could always just store the address of the workqueue it's
tied to, and require a reference to it on enqueue(), with an equality
check. That way, it's not a problem if the workqueue gets freed before
the work item, but we still enforce no workqueue migration.

Alice