Skip to content

Commit 6cca13d

Browse files
matnymangregkh
authored andcommitted
usb: hub: Fix locking issues with address0_mutex
Fix the circular lock dependency and unbalanced unlock of addess0_mutex introduced when fixing an address0_mutex enumeration retry race in commit ae6dc22d2d1 ("usb: hub: Fix usb enumeration issue due to address0 race") Make sure locking order between port_dev->status_lock and address0_mutex is correct, and that address0_mutex is not unlocked in hub_port_connect "done:" codepath which may be reached without locking address0_mutex Fixes: 6ae6dc2 ("usb: hub: Fix usb enumeration issue due to address0 race") Cc: <[email protected]> Reported-by: Marek Szyprowski <[email protected]> Tested-by: Hans de Goede <[email protected]> Tested-by: Marek Szyprowski <[email protected]> Acked-by: Hans de Goede <[email protected]> Signed-off-by: Mathias Nyman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent d4d2e53 commit 6cca13d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

drivers/usb/core/hub.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5188,6 +5188,7 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
51885188
struct usb_port *port_dev = hub->ports[port1 - 1];
51895189
struct usb_device *udev = port_dev->child;
51905190
static int unreliable_port = -1;
5191+
bool retry_locked;
51915192

51925193
/* Disconnect any existing devices under this port */
51935194
if (udev) {
@@ -5244,17 +5245,19 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
52445245

52455246
status = 0;
52465247

5247-
mutex_lock(hcd->address0_mutex);
5248-
52495248
for (i = 0; i < PORT_INIT_TRIES; i++) {
5250-
5249+
usb_lock_port(port_dev);
5250+
mutex_lock(hcd->address0_mutex);
5251+
retry_locked = true;
52515252
/* reallocate for each attempt, since references
52525253
* to the previous one can escape in various ways
52535254
*/
52545255
udev = usb_alloc_dev(hdev, hdev->bus, port1);
52555256
if (!udev) {
52565257
dev_err(&port_dev->dev,
52575258
"couldn't allocate usb_device\n");
5259+
mutex_unlock(hcd->address0_mutex);
5260+
usb_unlock_port(port_dev);
52585261
goto done;
52595262
}
52605263

@@ -5276,13 +5279,13 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
52765279
}
52775280

52785281
/* reset (non-USB 3.0 devices) and get descriptor */
5279-
usb_lock_port(port_dev);
52805282
status = hub_port_init(hub, udev, port1, i);
5281-
usb_unlock_port(port_dev);
52825283
if (status < 0)
52835284
goto loop;
52845285

52855286
mutex_unlock(hcd->address0_mutex);
5287+
usb_unlock_port(port_dev);
5288+
retry_locked = false;
52865289

52875290
if (udev->quirks & USB_QUIRK_DELAY_INIT)
52885291
msleep(2000);
@@ -5372,11 +5375,14 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
53725375

53735376
loop_disable:
53745377
hub_port_disable(hub, port1, 1);
5375-
mutex_lock(hcd->address0_mutex);
53765378
loop:
53775379
usb_ep0_reinit(udev);
53785380
release_devnum(udev);
53795381
hub_free_dev(udev);
5382+
if (retry_locked) {
5383+
mutex_unlock(hcd->address0_mutex);
5384+
usb_unlock_port(port_dev);
5385+
}
53805386
usb_put_dev(udev);
53815387
if ((status == -ENOTCONN) || (status == -ENOTSUPP))
53825388
break;
@@ -5399,8 +5405,6 @@ static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,
53995405
}
54005406

54015407
done:
5402-
mutex_unlock(hcd->address0_mutex);
5403-
54045408
hub_port_disable(hub, port1, 1);
54055409
if (hcd->driver->relinquish_port && !hub->hdev->parent) {
54065410
if (status != -ENOTCONN && status != -ENODEV)

0 commit comments

Comments
 (0)