Re: [PATCH v3] usb: core: hcd: flush giveback work on stop

From: Alan Stern

Date: Sun Oct 25 2026 - 11:17:33 EST


On Sun, Oct 25, 2026 at 10:54:57AM +0300, IgorpetinDev wrote:
> Regarding cancel_work_sync() vs. flush_work() and usb_remove_hcd() vs.
> usb_stop_hcd(): these changes were specifically suggested by Alan Stern
> during the review of v1 and v2, which is why v3 was shaped this way and
> subsequently received his reviewed-by tag.
>
> I'll defer to Alan and Greg on how they wish to proceed and which
> approach they prefer to take.

I don't much care whether the new calls go in usb_remove_hcd() or
usb_stop_hcd(). I suppose usb_remove_hcd() would make more sense, since
no URBs can be sent to the HCD until it is added.

As for cancel_work_sync() vs. flush_work(), it doesn't really matter.
The situation we're in is that there are no more entries remaining in
the workqueue and no more will be added, but the final one may still be
executing. In this setting, cancel_work_sync() and flush_work() do the
same thing. However, the name "flush_work" does a better job of
expressing what we want to accomplish: wait for the final work item to
finish, not try to cancel it.

> > > @@ -2787,6 +2789,13 @@ static void usb_stop_hcd(struct usb_hcd *hcd)
> > > /* In case the HCD restarted the timer, stop it again. */
> > > clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
> > > timer_delete_sync(&hcd->rh_timer);
> > > +
> > > + /*
> > > + * Even though all URBs have been given back,
> >
> > but does anyone actually believe this here?
> >
> > Hundreds of drivers written over three decades, often
> > without linux-usb review. I don't trust them at all.

If it's not true, there's likely to be trouble when the completion
handler is called. We could forcibly remove any remaining URBs from the
lists before flushing the work item and just leak them (with a warning
of some sort -- ideally, one that prints out the names of the completion
handler routines for the leaked URBs, if at all possible).

Alan Stern