Skip to content

Commit 236742d

Browse files
bblackhamgregkh
authored andcommitted
staging: usbip: avoid deadlock in vhci_device_unlink_cleanup()
Almost all of usbip assumes that the_controller->lock is acquired before vdev->priv_lock. The exception is in vhci_device_unlink_cleanup(), where locks are acquired in the reverse order. This leads to occasional deadlocks. Fixing this is a bit fiddly, as the_controller->lock can't be held when calling usb_hcd_unlink_urb_from_ep() in the middle of the list traversal. As I can't rule out concurrent callers to this function (perhaps it is safe?), the code here becomes slightly uglier - when locks are dropped in the middle so the list may have emptied itself (not even list_for_each_entry_safe is safe here). Signed-off-by: Bernard Blackham <b-linuxgit@largestprime.net> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 73295fe commit 236742d

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

‎drivers/staging/usbip/vhci_hcd.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
749749
{
750750
struct vhci_unlink *unlink, *tmp;
751751

752+
spin_lock(&the_controller->lock);
752753
spin_lock(&vdev->priv_lock);
753754

754755
list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
@@ -757,9 +758,12 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
757758
kfree(unlink);
758759
}
759760

760-
list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
761+
while (!list_empty(&vdev->unlink_rx)) {
761762
struct urb *urb;
762763

764+
unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
765+
list);
766+
763767
/* give back URB of unanswered unlink request */
764768
pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
765769

@@ -774,18 +778,24 @@ static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
774778

775779
urb->status = -ENODEV;
776780

777-
spin_lock(&the_controller->lock);
778781
usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
782+
783+
list_del(&unlink->list);
784+
785+
spin_unlock(&vdev->priv_lock);
779786
spin_unlock(&the_controller->lock);
780787

781788
usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
782789
urb->status);
783790

784-
list_del(&unlink->list);
791+
spin_lock(&the_controller->lock);
792+
spin_lock(&vdev->priv_lock);
793+
785794
kfree(unlink);
786795
}
787796

788797
spin_unlock(&vdev->priv_lock);
798+
spin_unlock(&the_controller->lock);
789799
}
790800

791801
/*

0 commit comments

Comments
 (0)