Skip to content

Commit 1fe837e

Browse files
Mike Bradleypopcornmix
authored andcommitted
dwc_otg: Call usb_hcd_unlink_urb_from_ep with lock held in completion handler
usb_hcd_unlink_urb_from_ep must be called with the HCD lock held. Calling it asynchronously in the tasklet was not safe (regression in c4564d4). This change unlinks it from the endpoint prior to queueing it for handling in the tasklet, and also adds a check to ensure the urb is OK to be unlinked before doing so. NULL pointer dereference kernel oopses had been observed in usb_hcd_giveback_urb when a USB device was unplugged/replugged during data transfer. This effect was reproduced using automated USB port power control, hundreds of replug events were performed during active transfers to confirm that the problem was eliminated.
1 parent 647a260 commit 1fe837e

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎drivers/usb/host/dwc_otg/dwc_otg_hcd.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ static void completion_tasklet_func(void *ptr)
704704
urb_tq_entry_t *item;
705705
dwc_irqflags_t flags;
706706

707+
/* This could just be spin_lock_irq */
707708
DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
708709
while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
709710
item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
@@ -713,7 +714,6 @@ static void completion_tasklet_func(void *ptr)
713714
DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
714715
DWC_FREE(item);
715716

716-
usb_hcd_unlink_urb_from_ep(hcd->priv, urb);
717717
usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
718718

719719
DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);

‎drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,15 @@ static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
265265

266266
/**
267267
* Sets the final status of an URB and returns it to the device driver. Any
268-
* required cleanup of the URB is performed.
268+
* required cleanup of the URB is performed. The HCD lock should be held on
269+
* entry.
269270
*/
270271
static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
271272
dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
272273
{
273274
struct urb *urb = (struct urb *)urb_handle;
274275
urb_tq_entry_t *new_entry;
276+
int rc = 0;
275277
if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
276278
DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
277279
__func__, urb, usb_pipedevice(urb->pipe),
@@ -363,9 +365,17 @@ static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
363365
#endif
364366
} else {
365367
new_entry->urb = urb;
366-
DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
367-
urb_tq_entries);
368-
DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
368+
#if USB_URB_EP_LINKING
369+
rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
370+
if(0 == rc) {
371+
usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
372+
}
373+
#endif
374+
if(0 == rc) {
375+
DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
376+
urb_tq_entries);
377+
DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
378+
}
369379
}
370380
return 0;
371381
}

0 commit comments

Comments
 (0)