Skip to content

Commit 2eb0337

Browse files
Weitao Wanggregkh
authored andcommitted
usb: xhci: Fix slot_id resource race conflict
xHC controller may immediately reuse a slot_id after it's disabled, giving it to a new enumerating device before the xhci driver freed all resources related to the disabled device. In such a scenario, device-A with slot_id equal to 1 is disconnecting while device-B is enumerating, device-B will fail to enumerate in the follow sequence. 1.[device-A] send disable slot command 2.[device-B] send enable slot command 3.[device-A] disable slot command completed and wakeup waiting thread 4.[device-B] enable slot command completed with slot_id equal to 1 and wakeup waiting thread 5.[device-B] driver checks that slot_id is still in use (by device-A) in xhci_alloc_virt_device, and fail to enumerate due to this conflict 6.[device-A] xhci->devs[slot_id] set to NULL in xhci_free_virt_device To fix driver's slot_id resources conflict, clear xhci->devs[slot_id] and xhci->dcbba->dev_context_ptrs[slot_id] pointers in the interrupt context when disable slot command completes successfully. Simultaneously, adjust function xhci_free_virt_device to accurately handle device release. [minor smatch warning and commit message fix -Mathias] Cc: [email protected] Fixes: 7faac19 ("xhci: avoid race between disable slot command and host runtime suspend") Signed-off-by: Weitao Wang <[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 309b634 commit 2eb0337

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

drivers/usb/host/xhci-hub.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,7 @@ static int xhci_enter_test_mode(struct xhci_hcd *xhci,
704704
if (!xhci->devs[i])
705705
continue;
706706

707-
retval = xhci_disable_slot(xhci, i);
708-
xhci_free_virt_device(xhci, i);
707+
retval = xhci_disable_and_free_slot(xhci, i);
709708
if (retval)
710709
xhci_err(xhci, "Failed to disable slot %d, %d. Enter test mode anyway\n",
711710
i, retval);

drivers/usb/host/xhci-mem.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -865,21 +865,20 @@ int xhci_alloc_tt_info(struct xhci_hcd *xhci,
865865
* will be manipulated by the configure endpoint, allocate device, or update
866866
* hub functions while this function is removing the TT entries from the list.
867867
*/
868-
void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
868+
void xhci_free_virt_device(struct xhci_hcd *xhci, struct xhci_virt_device *dev,
869+
int slot_id)
869870
{
870-
struct xhci_virt_device *dev;
871871
int i;
872872
int old_active_eps = 0;
873873

874874
/* Slot ID 0 is reserved */
875-
if (slot_id == 0 || !xhci->devs[slot_id])
875+
if (slot_id == 0 || !dev)
876876
return;
877877

878-
dev = xhci->devs[slot_id];
879-
880-
xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
881-
if (!dev)
882-
return;
878+
/* If device ctx array still points to _this_ device, clear it */
879+
if (dev->out_ctx &&
880+
xhci->dcbaa->dev_context_ptrs[slot_id] == cpu_to_le64(dev->out_ctx->dma))
881+
xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
883882

884883
trace_xhci_free_virt_device(dev);
885884

@@ -920,8 +919,9 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
920919
dev->udev->slot_id = 0;
921920
if (dev->rhub_port && dev->rhub_port->slot_id == slot_id)
922921
dev->rhub_port->slot_id = 0;
923-
kfree(xhci->devs[slot_id]);
924-
xhci->devs[slot_id] = NULL;
922+
if (xhci->devs[slot_id] == dev)
923+
xhci->devs[slot_id] = NULL;
924+
kfree(dev);
925925
}
926926

927927
/*
@@ -962,7 +962,7 @@ static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_i
962962
out:
963963
/* we are now at a leaf device */
964964
xhci_debugfs_remove_slot(xhci, slot_id);
965-
xhci_free_virt_device(xhci, slot_id);
965+
xhci_free_virt_device(xhci, vdev, slot_id);
966966
}
967967

968968
int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,

drivers/usb/host/xhci-ring.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,8 @@ static void xhci_handle_cmd_enable_slot(int slot_id, struct xhci_command *comman
15921592
command->slot_id = 0;
15931593
}
15941594

1595-
static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
1595+
static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id,
1596+
u32 cmd_comp_code)
15961597
{
15971598
struct xhci_virt_device *virt_dev;
15981599
struct xhci_slot_ctx *slot_ctx;
@@ -1607,6 +1608,10 @@ static void xhci_handle_cmd_disable_slot(struct xhci_hcd *xhci, int slot_id)
16071608
if (xhci->quirks & XHCI_EP_LIMIT_QUIRK)
16081609
/* Delete default control endpoint resources */
16091610
xhci_free_device_endpoint_resources(xhci, virt_dev, true);
1611+
if (cmd_comp_code == COMP_SUCCESS) {
1612+
xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
1613+
xhci->devs[slot_id] = NULL;
1614+
}
16101615
}
16111616

16121617
static void xhci_handle_cmd_config_ep(struct xhci_hcd *xhci, int slot_id)
@@ -1856,7 +1861,7 @@ static void handle_cmd_completion(struct xhci_hcd *xhci,
18561861
xhci_handle_cmd_enable_slot(slot_id, cmd, cmd_comp_code);
18571862
break;
18581863
case TRB_DISABLE_SLOT:
1859-
xhci_handle_cmd_disable_slot(xhci, slot_id);
1864+
xhci_handle_cmd_disable_slot(xhci, slot_id, cmd_comp_code);
18601865
break;
18611866
case TRB_CONFIG_EP:
18621867
if (!cmd->completion)

drivers/usb/host/xhci.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3932,8 +3932,7 @@ static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
39323932
* Obtaining a new device slot to inform the xHCI host that
39333933
* the USB device has been reset.
39343934
*/
3935-
ret = xhci_disable_slot(xhci, udev->slot_id);
3936-
xhci_free_virt_device(xhci, udev->slot_id);
3935+
ret = xhci_disable_and_free_slot(xhci, udev->slot_id);
39373936
if (!ret) {
39383937
ret = xhci_alloc_dev(hcd, udev);
39393938
if (ret == 1)
@@ -4090,7 +4089,7 @@ static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
40904089
xhci_disable_slot(xhci, udev->slot_id);
40914090

40924091
spin_lock_irqsave(&xhci->lock, flags);
4093-
xhci_free_virt_device(xhci, udev->slot_id);
4092+
xhci_free_virt_device(xhci, virt_dev, udev->slot_id);
40944093
spin_unlock_irqrestore(&xhci->lock, flags);
40954094

40964095
}
@@ -4139,6 +4138,16 @@ int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
41394138
return 0;
41404139
}
41414140

4141+
int xhci_disable_and_free_slot(struct xhci_hcd *xhci, u32 slot_id)
4142+
{
4143+
struct xhci_virt_device *vdev = xhci->devs[slot_id];
4144+
int ret;
4145+
4146+
ret = xhci_disable_slot(xhci, slot_id);
4147+
xhci_free_virt_device(xhci, vdev, slot_id);
4148+
return ret;
4149+
}
4150+
41424151
/*
41434152
* Checks if we have enough host controller resources for the default control
41444153
* endpoint.
@@ -4245,8 +4254,7 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
42454254
return 1;
42464255

42474256
disable_slot:
4248-
xhci_disable_slot(xhci, udev->slot_id);
4249-
xhci_free_virt_device(xhci, udev->slot_id);
4257+
xhci_disable_and_free_slot(xhci, udev->slot_id);
42504258

42514259
return 0;
42524260
}
@@ -4382,8 +4390,7 @@ static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
43824390
dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
43834391

43844392
mutex_unlock(&xhci->mutex);
4385-
ret = xhci_disable_slot(xhci, udev->slot_id);
4386-
xhci_free_virt_device(xhci, udev->slot_id);
4393+
ret = xhci_disable_and_free_slot(xhci, udev->slot_id);
43874394
if (!ret) {
43884395
if (xhci_alloc_dev(hcd, udev) == 1)
43894396
xhci_setup_addressable_virt_dev(xhci, udev);

drivers/usb/host/xhci.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,7 @@ void xhci_dbg_trace(struct xhci_hcd *xhci, void (*trace)(struct va_format *),
17911791
/* xHCI memory management */
17921792
void xhci_mem_cleanup(struct xhci_hcd *xhci);
17931793
int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags);
1794-
void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id);
1794+
void xhci_free_virt_device(struct xhci_hcd *xhci, struct xhci_virt_device *dev, int slot_id);
17951795
int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id, struct usb_device *udev, gfp_t flags);
17961796
int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev);
17971797
void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
@@ -1888,6 +1888,7 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev);
18881888
int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
18891889
struct usb_tt *tt, gfp_t mem_flags);
18901890
int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id);
1891+
int xhci_disable_and_free_slot(struct xhci_hcd *xhci, u32 slot_id);
18911892
int xhci_ext_cap_init(struct xhci_hcd *xhci);
18921893

18931894
int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup);

0 commit comments

Comments
 (0)