Re: [BUG] usbip: vhci: Sleeping function called from invalid context in vhci_urb_enqueue on PREEMPT_RT

Alan Stern
2025-08-16T01:51:45Z

On Sat, Aug 16, 2025 at 10:29:34AM +0900, Yunseong Kim wrote:
> While testing a PREEMPT_RT enabled kernel (based on v6.17.0-rc1),
> I encountered a "BUG: sleeping function called from invalid context"
> error originating from the USB/IP VHCI driver.
>
> On PREEMPT_RT configurations, standard spin_lock() calls are replaced by
> rt_spin_lock(). Since rt_spin_lock() may sleep when contended, it must not
> be called from an atomic context (e.g., with interrupts disabled).
>
> The issue occurs within the vhci_urb_enqueue function This function
> explicitly disables local interrupts using local_irq_disable() immediately
> before calling usb_hcd_giveback_urb(), adhering to HCD requirements.

...

> This error reported after this work:
> It occurs after going through the code below:
>
> static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
> {
>
> ...
>
> no_need_unlink:
> spin_unlock_irqrestore(&vhci->lock, flags);
> if (!ret) {
> /* usb_hcd_giveback_urb() should be called with
> * irqs disabled
> */
> local_irq_disable(); // <--- Entering atomic context (IRQs disabled)
> usb_hcd_giveback_urb(hcd, urb, urb->status);
> local_irq_enable();
> }
> return ret;
> }
>
> static void mon_bus_complete(struct mon_bus *mbus, struct urb *urb, int status)
> {
> ...
> spin_lock_irqsave(&mbus->lock, flags);
^
------------------^

> ...
> }
>
> When called with interrupts disabled, usb_hcd_giveback_urb() eventually
> leads to mon_complete() in the USB monitoring, if usbmon is enabled,
> via __usb_hcd_giveback_urb().
>
> mon_complete() attempts to acquire a lock via spin_lock(), observed in the
> trace within the inlined mon_bus_complete.

Look again. mon_bus_complete() calls spin_lock_irqsave(), not
spin_lock().

Is the kernel tree that you are using different from Linus's tree?

Alan Stern