Re: [PATCH v2 2/2] rust: workqueue: add creation of workqueues

From: Alice Ryhl

Date: Fri Nov 14 2025 - 04:45:00 EST


On Thu, Nov 13, 2025 at 11:52:17AM -0800, Boqun Feng wrote:
> On Thu, Nov 13, 2025 at 10:01:07AM +0000, Alice Ryhl wrote:
> > Creating workqueues is needed by various GPU drivers. Not only does it
> > give you better control over execution, it also allows devices to ensure
> > that all tasks have exited before the device is unbound (or similar) by
> > running the workqueue destructor.
> >
> > A wrapper type Flags is provided for workqueue flags. It allows you to
> > build any valid flag combination, while using a type-level marker for
> > whether WQ_BH is used to prevent invalid flag combinations. The Flags wrapper
> > also forces you to explicitly pick one of percpu, unbound, or bh.
> >
> > Signed-off-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> [...]
> > +/// An owned kernel work queue.
> > +///
> > +/// Dropping a workqueue blocks on all pending work.
> > +///
> > +/// # Invariants
> > +///
> > +/// `queue` points at a valid workqueue that is owned by this `OwnedQueue`.
> > +pub struct OwnedQueue {
> > + queue: NonNull<Queue>,
>
> I hope Owned/Ownable can make it just a Owned<Queue> here ;-) And
> that'll make Owned<Queue> automatically Send + Sync. I think it's not a
> rare ask for `OwnedQueue` to be Send + Sync.

Oh yeah it should be Send/Sync.

And hopefully we get Owned eventually.

> > +}
> > +
> > +#[expect(clippy::manual_c_str_literals)]
> > +impl OwnedQueue {
> > + /// Allocates a new workqueue.
> > + ///
> > + /// The provided name is used verbatim as the workqueue name.
> > + ///
> > + /// # Examples
> > + ///
> > + /// ```
> > + /// use kernel::c_str;
> > + /// use kernel::workqueue::{OwnedQueue, Flags};
> > + ///
> > + /// let wq = OwnedQueue::new(c_str!("my-wq"), Flags::unbound().sysfs(), 0)?;
> > + /// wq.try_spawn(
> > + /// GFP_KERNEL,
> > + /// || pr_warn!("Printing from my-wq"),
> > + /// )?;
> > + /// # Ok::<(), Error>(())
> > + /// ```
> > + #[inline]
> > + pub fn new<const BH: bool>(
> > + name: &CStr,
> > + flags: Flags<BH>,
> > + max_active: usize,
>
> Do we need to support `max_active` as `usize` when the underlying C
> code only support i32?

Negative values don't really make sense. But maybe it's a good idea,
yeah.

Alice