Skip to content

Commit f88ba78

Browse files
Sarah Sharpgregkh
authored andcommitted
USB: xhci: Respect critical sections.
Narrow down time spent holding the xHCI spinlock so that it's only used to protect the xHCI rings, not as mutual exclusion. Stop allocating memory while holding the spinlock and calling xhci_alloc_virt_device() and xhci_endpoint_init(). The USB core should have locking in it to prevent device state to be manipulated by more than one kernel thread. E.g. you can't free a device while you're in the middle of setting a new configuration. So removing the locks from the sections where xhci_alloc_dev() and xhci_reset_bandwidth() touch xHCI's representation of the device should be OK. Signed-off-by: Sarah Sharp <sarah.a.sharp@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent a4d8830 commit f88ba78

3 files changed

Lines changed: 37 additions & 35 deletions

File tree

‎drivers/usb/host/xhci-hcd.c‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,14 @@ int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
687687
* different endpoint descriptor in usb_host_endpoint.
688688
* A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
689689
* not allowed.
690+
*
691+
* The USB core will not allow URBs to be queued to an endpoint that is being
692+
* disabled, so there's no need for mutual exclusion to protect
693+
* the xhci->devs[slot_id] structure.
690694
*/
691695
int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
692696
struct usb_host_endpoint *ep)
693697
{
694-
unsigned long flags;
695698
struct xhci_hcd *xhci;
696699
struct xhci_device_control *in_ctx;
697700
unsigned int last_ctx;
@@ -714,11 +717,9 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
714717
return 0;
715718
}
716719

717-
spin_lock_irqsave(&xhci->lock, flags);
718720
if (!xhci->devs || !xhci->devs[udev->slot_id]) {
719721
xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
720722
__func__);
721-
spin_unlock_irqrestore(&xhci->lock, flags);
722723
return -EINVAL;
723724
}
724725

@@ -732,7 +733,6 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
732733
in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
733734
xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
734735
__func__, ep);
735-
spin_unlock_irqrestore(&xhci->lock, flags);
736736
return 0;
737737
}
738738

@@ -752,8 +752,6 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
752752

753753
xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
754754

755-
spin_unlock_irqrestore(&xhci->lock, flags);
756-
757755
xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
758756
(unsigned int) ep->desc.bEndpointAddress,
759757
udev->slot_id,
@@ -771,11 +769,14 @@ int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
771769
* different endpoint descriptor in usb_host_endpoint.
772770
* A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
773771
* not allowed.
772+
*
773+
* The USB core will not allow URBs to be queued to an endpoint until the
774+
* configuration or alt setting is installed in the device, so there's no need
775+
* for mutual exclusion to protect the xhci->devs[slot_id] structure.
774776
*/
775777
int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
776778
struct usb_host_endpoint *ep)
777779
{
778-
unsigned long flags;
779780
struct xhci_hcd *xhci;
780781
struct xhci_device_control *in_ctx;
781782
unsigned int ep_index;
@@ -802,11 +803,9 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
802803
return 0;
803804
}
804805

805-
spin_lock_irqsave(&xhci->lock, flags);
806806
if (!xhci->devs || !xhci->devs[udev->slot_id]) {
807807
xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
808808
__func__);
809-
spin_unlock_irqrestore(&xhci->lock, flags);
810809
return -EINVAL;
811810
}
812811

@@ -819,14 +818,18 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
819818
if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
820819
xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
821820
__func__, ep);
822-
spin_unlock_irqrestore(&xhci->lock, flags);
823821
return 0;
824822
}
825823

826-
if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], udev, ep) < 0) {
824+
/*
825+
* Configuration and alternate setting changes must be done in
826+
* process context, not interrupt context (or so documenation
827+
* for usb_set_interface() and usb_set_configuration() claim).
828+
*/
829+
if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
830+
udev, ep, GFP_KERNEL) < 0) {
827831
dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
828832
__func__, ep->desc.bEndpointAddress);
829-
spin_unlock_irqrestore(&xhci->lock, flags);
830833
return -ENOMEM;
831834
}
832835

@@ -847,7 +850,6 @@ int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
847850
in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
848851
}
849852
new_slot_info = in_ctx->slot.dev_info;
850-
spin_unlock_irqrestore(&xhci->lock, flags);
851853

852854
xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
853855
(unsigned int) ep->desc.bEndpointAddress,
@@ -883,6 +885,16 @@ static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
883885
}
884886
}
885887

888+
/* Called after one or more calls to xhci_add_endpoint() or
889+
* xhci_drop_endpoint(). If this call fails, the USB core is expected
890+
* to call xhci_reset_bandwidth().
891+
*
892+
* Since we are in the middle of changing either configuration or
893+
* installing a new alt setting, the USB core won't allow URBs to be
894+
* enqueued for any endpoint on the old config or interface. Nothing
895+
* else should be touching the xhci->devs[slot_id] structure, so we
896+
* don't need to take the xhci->lock for manipulating that.
897+
*/
886898
int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
887899
{
888900
int i;
@@ -897,11 +909,9 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
897909
return ret;
898910
xhci = hcd_to_xhci(hcd);
899911

900-
spin_lock_irqsave(&xhci->lock, flags);
901912
if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
902913
xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
903914
__func__);
904-
spin_unlock_irqrestore(&xhci->lock, flags);
905915
return -EINVAL;
906916
}
907917
xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
@@ -916,11 +926,12 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
916926
xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
917927
LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
918928

929+
spin_lock_irqsave(&xhci->lock, flags);
919930
ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx_dma,
920931
udev->slot_id);
921932
if (ret < 0) {
922-
xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
923933
spin_unlock_irqrestore(&xhci->lock, flags);
934+
xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
924935
return -ENOMEM;
925936
}
926937
xhci_ring_cmd_db(xhci);
@@ -937,7 +948,6 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
937948
return -ETIME;
938949
}
939950

940-
spin_lock_irqsave(&xhci->lock, flags);
941951
switch (virt_dev->cmd_status) {
942952
case COMP_ENOMEM:
943953
dev_warn(&udev->dev, "Not enough host controller resources "
@@ -968,7 +978,6 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
968978
}
969979
if (ret) {
970980
/* Callee should call reset_bandwidth() */
971-
spin_unlock_irqrestore(&xhci->lock, flags);
972981
return ret;
973982
}
974983

@@ -986,14 +995,11 @@ int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
986995
}
987996
}
988997

989-
spin_unlock_irqrestore(&xhci->lock, flags);
990-
991998
return ret;
992999
}
9931000

9941001
void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
9951002
{
996-
unsigned long flags;
9971003
struct xhci_hcd *xhci;
9981004
struct xhci_virt_device *virt_dev;
9991005
int i, ret;
@@ -1003,11 +1009,9 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
10031009
return;
10041010
xhci = hcd_to_xhci(hcd);
10051011

1006-
spin_lock_irqsave(&xhci->lock, flags);
10071012
if (!xhci->devs || !xhci->devs[udev->slot_id]) {
10081013
xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
10091014
__func__);
1010-
spin_unlock_irqrestore(&xhci->lock, flags);
10111015
return;
10121016
}
10131017
xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
@@ -1020,7 +1024,6 @@ void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
10201024
}
10211025
}
10221026
xhci_zero_in_ctx(virt_dev);
1023-
spin_unlock_irqrestore(&xhci->lock, flags);
10241027
}
10251028

10261029
/*
@@ -1046,7 +1049,7 @@ void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
10461049
spin_unlock_irqrestore(&xhci->lock, flags);
10471050
/*
10481051
* Event command completion handler will free any data structures
1049-
* associated with the slot
1052+
* associated with the slot. XXX Can free sleep?
10501053
*/
10511054
}
10521055

@@ -1081,15 +1084,15 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
10811084
return 0;
10821085
}
10831086

1084-
spin_lock_irqsave(&xhci->lock, flags);
10851087
if (!xhci->slot_id) {
10861088
xhci_err(xhci, "Error while assigning device slot ID\n");
1087-
spin_unlock_irqrestore(&xhci->lock, flags);
10881089
return 0;
10891090
}
1091+
/* xhci_alloc_virt_device() does not touch rings; no need to lock */
10901092
if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
10911093
/* Disable slot, if we can do it without mem alloc */
10921094
xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
1095+
spin_lock_irqsave(&xhci->lock, flags);
10931096
if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
10941097
xhci_ring_cmd_db(xhci);
10951098
spin_unlock_irqrestore(&xhci->lock, flags);
@@ -1098,7 +1101,6 @@ int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
10981101
udev->slot_id = xhci->slot_id;
10991102
/* Is this a LS or FS device under a HS hub? */
11001103
/* Hub or peripherial? */
1101-
spin_unlock_irqrestore(&xhci->lock, flags);
11021104
return 1;
11031105
}
11041106

@@ -1125,14 +1127,14 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
11251127
return -EINVAL;
11261128
}
11271129

1128-
spin_lock_irqsave(&xhci->lock, flags);
11291130
virt_dev = xhci->devs[udev->slot_id];
11301131

11311132
/* If this is a Set Address to an unconfigured device, setup ep 0 */
11321133
if (!udev->config)
11331134
xhci_setup_addressable_virt_dev(xhci, udev);
11341135
/* Otherwise, assume the core has the device configured how it wants */
11351136

1137+
spin_lock_irqsave(&xhci->lock, flags);
11361138
ret = xhci_queue_address_device(xhci, virt_dev->in_ctx_dma,
11371139
udev->slot_id);
11381140
if (ret) {
@@ -1157,7 +1159,6 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
11571159
return -ETIME;
11581160
}
11591161

1160-
spin_lock_irqsave(&xhci->lock, flags);
11611162
switch (virt_dev->cmd_status) {
11621163
case COMP_CTX_STATE:
11631164
case COMP_EBADSLT:
@@ -1179,7 +1180,6 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
11791180
break;
11801181
}
11811182
if (ret) {
1182-
spin_unlock_irqrestore(&xhci->lock, flags);
11831183
return ret;
11841184
}
11851185
temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
@@ -1211,7 +1211,6 @@ int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
12111211
/* Mirror flags in the output context for future ep enable/disable */
12121212
virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
12131213
virt_dev->out_ctx->drop_flags = 0;
1214-
spin_unlock_irqrestore(&xhci->lock, flags);
12151214

12161215
xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
12171216
/* XXX Meh, not sure if anyone else but choose_address uses this. */

‎drivers/usb/host/xhci-mem.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
460460
int xhci_endpoint_init(struct xhci_hcd *xhci,
461461
struct xhci_virt_device *virt_dev,
462462
struct usb_device *udev,
463-
struct usb_host_endpoint *ep)
463+
struct usb_host_endpoint *ep,
464+
gfp_t mem_flags)
464465
{
465466
unsigned int ep_index;
466467
struct xhci_ep_ctx *ep_ctx;
@@ -472,7 +473,7 @@ int xhci_endpoint_init(struct xhci_hcd *xhci,
472473
ep_ctx = &virt_dev->in_ctx->ep[ep_index];
473474

474475
/* Set up the endpoint ring */
475-
virt_dev->new_ep_rings[ep_index] = xhci_ring_alloc(xhci, 1, true, GFP_KERNEL);
476+
virt_dev->new_ep_rings[ep_index] = xhci_ring_alloc(xhci, 1, true, mem_flags);
476477
if (!virt_dev->new_ep_rings[ep_index])
477478
return -ENOMEM;
478479
ep_ring = virt_dev->new_ep_rings[ep_index];

‎drivers/usb/host/xhci.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,9 @@ int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *ud
11011101
unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc);
11021102
unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc);
11031103
void xhci_endpoint_zero(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_host_endpoint *ep);
1104-
int xhci_endpoint_init(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev, struct usb_device *udev, struct usb_host_endpoint *ep);
1104+
int xhci_endpoint_init(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev,
1105+
struct usb_device *udev, struct usb_host_endpoint *ep,
1106+
gfp_t mem_flags);
11051107
void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring);
11061108

11071109
#ifdef CONFIG_PCI

0 commit comments

Comments
 (0)