Skip to contentSkip to conversation

xhci fixes for usb-linus

PATCH New Mathias Nyman 2 participants 12 messages Nov 27

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover xhci fixes for usb-linus

Hi Greg

A set of xhci fixes for usb-linus, many are related to cases where a usb device is reset as part of re-enumeration (retry), or recovery from failed resume. These are fixes like the sideband use-after-free patches and xhci ep0 context mangling fix.

Some generic fixes like avoiding dev_warn floods, and reporting current USB3 supported speeds correctly on a set of xHCI hosts.

Thanks
Mathias

Alex Kononov (1):
usb: xhci: Demote isoch buffer overrun message to debug level

Chien Kun Niu (1):
xhci: Give back URB under lock in xhci_urb_dequeue()

Jon Hunter (1):
usb: xhci: tegra: Fix missing semi-colon

Mathias Nyman (6):
xhci: check for halted host before accessing virt devices in
xhci_check_args()
xhci: Fix reporting incorrect supported speeds in roothub BOS
descriptor
xhci: sideband: avoid use-after-free of stale virt device pointer
xhci: sideband: check virt device under xhci lock when stopping
endpoints
xhci: fix use-after-free for errors mid TD on empty halted rings
xhci: fix NULL pointer dereference of rhub_port in xhci_urb_enqueue()

Michal Pecio (1):
usb: xhci: Fix ep0 maxpacket update on enabled slots

 drivers/usb/host/xhci-hub.c       |  15 ++-
 drivers/usb/host/xhci-ring.c      |   7 +-
 drivers/usb/host/xhci-sideband.c  | 151 ++++++++++++++++++++++--------
 drivers/usb/host/xhci-tegra.c     |   2 +-
 drivers/usb/host/xhci.c           |  87 +++++------------
 drivers/usb/host/xhci.h           |   2 -
 include/linux/usb/xhci-sideband.h |   4 +-
 7 files changed, 155 insertions(+), 113 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/10 usb: xhci: Fix ep0 maxpacket update on enabled slots

From: Michal Pecio <[email protected]>

A slot in Enabled state has uninitialized output contexts; it doesn't need and cannot accept Evaluate Context command. Attempting the command only results in a (harmless) warning, observed when core uses "old scheme" on a full-speed device:

WARN: invalid context state for evaluate context command.

This problem doesn't seem to have existed before ep0 update was moved from urb_enqueue() to endpoint_reset(). USB core doesn't use URBs before enable_device() or address_device(), but it may call endpoint_reset() on ep0.

A more serious bug has been identified by Sashiko review bot. Transaction Error of Address Device may cause the slot to be reallocated and left in Enabled state, but the input context is (needlessly) regenerated before actually being needed by the next Address Device. xhci_check_ep0_maxpacket() corrupts this context by copying uninitialized ep0 output context.

This matters when core resets a device and doesn't reallocate udev after address_device() errors. Some HCs reject the next Address Device with Parameter Error, some break. ASMedia HCs write a Setup Stage TRB to the event ring and stop working:

ERROR unknown event type 2

  INFO: task usbreset:4071 blocked for more than 122 seconds.

This exact case is a relatively recent regression, but even if xhci_setup_device() didn't regenerate the input context on Transaction Error, next iteration could regenerate it and keep it there when command timeout leaves the slot Enabled.

We can kill both bugs with one stone by checking slot state. If it's Enabled, do nothing and let next xhci_setup_device() configure ep0 later. It will actually ignore the packet size requested by core, but its defaults match core expectations at the time of address_device() and enable_device() calls.

Fixes e34900f46cd6 ("xhci: Reconfigure endpoint 0 max packet size only during endpoint reset")
Fixes af8e119f52e9 ("xhci: Fix Panther point NULL pointer deref at full-speed re-enumeration")
---
 drivers/usb/host/xhci.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/usb/host/xhci.c +5 -0
index fc1b0a6b0429..6e0729a07e5f 100644
@@ -1554,6 +1554,11 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
 	int hw_max_packet_size;
 	int ret = 0;
 
+	/* Only slots initialized by successful Address Device command need updating */
+	if (GET_SLOT_STATE(le32_to_cpu(xhci_get_slot_ctx(xhci, vdev->out_ctx)->dev_state))
+	    == SLOT_STATE_ENABLED)
+		return 0;
+
 	ep_ctx = xhci_get_ep_ctx(xhci, vdev->out_ctx, 0);
 	hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
 	max_packet_size = usb_endpoint_maxp(&vdev->udev->ep0.desc);
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/10 xhci: check for halted host before accessing virt devices in xhci_check_args()

In reply to Mathias Nyman

xhci_resume() may halt and reset xHC. It then frees all virt devices without holding xhci->lock, before the host is reinitialized and started again.

xhci_check_args() reads xhci->devs[udev->slot_id], and only checks for a halted host after it has dereferenced the virt device.

If an URB is submitted while the virt devices are freed, this can lead to a NULL pointer dereference, or a use-after-free of a virt device whose rings and contexts were already freed.

This is possible during runtime resume. Userspace can allow a device opened via usbfs to runtime suspend with USBDEVFS_ALLOW_SUSPEND, and still submit URBs to it.

Check for a halted host first in xhci_check_args(). XHCI_STATE_HALTED is set under xhci->lock by xhci_halt() before the virt devices are freed, and cleared only when xhci_start() starts the host again.

Fixes 203a86613fb3 ("xhci: Avoid NULL pointer deref when host dies.")
---
 drivers/usb/host/xhci.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/usb/host/xhci.c +15 -3
index 6e0729a07e5f..b3df40f22d55 100644
@@ -1514,6 +1514,14 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
 	}
 
 	xhci = hcd_to_xhci(hcd);
+
+	/*
+	 * Check halted before touching virt devices. Resume halts the host and
+	 * frees all virt devices without xhci->lock if it needs to be reset.
+	 */
+	if (xhci->xhc_state & XHCI_STATE_HALTED)
+		return -ENODEV;
+
 	if (check_virt_dev) {
 		if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
 			xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
@@ -1529,9 +1537,6 @@ static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
 		}
 	}
 
-	if (xhci->xhc_state & XHCI_STATE_HALTED)
-		return -ENODEV;
-
 	return 1;
 }
 
@@ -4132,7 +4137,14 @@ static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
 	if (ret <= 0 && ret != -ENODEV)
 		return;
 
+	/* Halted host returns -ENODEV before virt device is checked */
+	if (!udev->slot_id || !xhci->devs[udev->slot_id])
+		return;
+
 	virt_dev = xhci->devs[udev->slot_id];
+	if (virt_dev->udev != udev)
+		return;
+
 	slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
 	trace_xhci_free_dev(slot_ctx);
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/10 xhci: Give back URB under lock in xhci_urb_dequeue()

In reply to Mathias Nyman

From: Chien Kun Niu <[email protected]>

Before commit 36dc01657b49 ("usb: host: xhci: Support running urb giveback in tasklet context") set HCD_BH, usb_hcd_giveback_urb() ran urb->complete() synchronously, so xHCI had to drop xhci->lock before giving back an URB. That commit updated xhci_giveback_urb_in_irq() to keep xhci->lock held across usb_hcd_giveback_urb(), covering normal completions, xhci_process_cancelled_tds(), and xhci_hc_died(), while the open-coded err_giveback path in xhci_urb_dequeue() was missed.

Dropping xhci->lock between usb_hcd_unlink_urb_from_ep() and usb_hcd_giveback_urb() breaks per-endpoint giveback serialization: a concurrent xhci_urb_dequeue() or xhci_hc_died() can acquire xhci->lock and queue a later URB onto bh->head ahead of this one, completing URBs out of order. When spinlocks disable interrupts, unlocking first also causes a redundant IRQ restore/save bounce into bh->lock, and without an IRQ-safe bh->lock it exposed task-context unlinks to a same-CPU deadlock.

Move usb_hcd_giveback_urb() before spin_unlock_irqrestore(&xhci->lock, flags) in err_giveback to match xhci_giveback_urb_in_irq().

Fixes 36dc01657b49 ("usb: host: xhci: Support running urb giveback in tasklet context")
---
 drivers/usb/host/xhci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci.c +1 -1
index b3df40f22d55..d55c8b7058ce 100644
@@ -1891,8 +1891,8 @@ static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
 	if (urb_priv)
 		xhci_urb_free_priv(urb_priv);
 	usb_hcd_unlink_urb_from_ep(hcd, urb);
-	spin_unlock_irqrestore(&xhci->lock, flags);
 	usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
+	spin_unlock_irqrestore(&xhci->lock, flags);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/10 xhci: Fix reporting incorrect supported speeds in roothub BOS descriptor

In reply to Mathias Nyman

xhci driver can show incorrect supported speeds for roothub ports if the xHC controller lists several port protocol capabilities with the same revision but with different supported speeds.

AMD xHCI 1022:149c lists several port protocol capabilities with the same USB 3.1 revision. Some of these capabilities report that a set of ports only support 5Gbps, while other capabilities report their ports support both 5Gbps and 10Gbps.

xhci_create_usb3x_bos_desc() builds the root hub SSP capability from one port capability only: the first one with the highest revision, ending up with a roothub BOS SSP capability descriptor claiming that roothub only supports 5Gbps speeds.

Fix this by picking the port protocol capability with highest revision and largest unique protocol speed ID count. Prefer a capability with a protocol speed ID count of 0, as that means its ports support all default speeds of that USB revision.

This won't guarantee that the supported speeds of all protocol capabilities are listed in the BOS descriptor, but it's an improvement over the current situation.

---
 drivers/usb/host/xhci-hub.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
drivers/usb/host/xhci-hub.c +11 -4
index 55430b673f1e..d47d1e736a37 100644
@@ -59,13 +59,20 @@ static int xhci_create_usb3x_bos_desc(struct xhci_hcd *xhci, char *buf,
 
 	/* Create the descriptor for port with the highest revision */
 	for (i = 0; i < xhci->num_port_caps; i++) {
-		u8 major = xhci->port_caps[i].maj_rev;
-		u8 minor = xhci->port_caps[i].min_rev;
-		u16 rev = (major << 8) | minor;
+		struct xhci_port_cap *cap = &xhci->port_caps[i];
+		u16 rev = (cap->maj_rev << 8) | cap->min_rev;
 
 		if (i == 0 || bcdUSB < rev) {
 			bcdUSB = rev;
-			port_cap = &xhci->port_caps[i];
+			port_cap = cap;
+		} else if (bcdUSB == rev && port_cap->psi_count != 0) {
+			/*
+			 * Same revision: prefer psi_count 0 (all default speeds),
+			 * otherwise the highest psi_uid_count.
+			 */
+			if (cap->psi_count == 0 ||
+			    cap->psi_uid_count > port_cap->psi_uid_count)
+				port_cap = cap;
 		}
 	}
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/10 xhci: sideband: avoid use-after-free of stale virt device pointer

In reply to Mathias Nyman

The sideband structure caches vdev, an xhci_virt_device pointer that the xhci driver may free and reallocate while the sideband is still registered, leaving the sideband with a dangling sb->vdev pointer.

This can happen during resume, either when the xhci driver detects xHC power loss and resets the xHC, freeing all virt devices without informing class drivers, or during reset-resume when usb core re-enumerates the device and xhci frees and allocates a new virt device.

Store the usb_device pointer (udev) instead. It stays valid across reset-resume and xHC reset.
Look up the current virt device and ensure it still points to this sideband before using and dereferencing it in sideband API calls.

Make sure xhci_sideband_unregister() works properly when called with an invalid vdev. Don't dereference it, or ask the xHC to stop non-existing endpoints, but make sure the sideband is properly freed.
Fail other relevant sideband API calls that would access an invalid vdev.

This solution isn't airtight. It only validates vdev when entering sideband API functions. It does not serialize the sideband API functions against the code in the xhci driver that reallocates vdev, so vdev may still be freed in the middle of a sideband call. It's still a big improvement and a first step in resolving this.

The following patch starts working on the serialization.

Fixes de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")
---
 drivers/usb/host/xhci-sideband.c  | 46 +++++++++++++++++++++----------
 include/linux/usb/xhci-sideband.h |  4 +--
 2 files changed, 34 insertions(+), 16 deletions(-)
drivers/usb/host/xhci-sideband.c +32 -14
index a5deeee4d5dc..e4dd298b80f1 100644
@@ -75,6 +75,22 @@ xhci_ring_to_sgtable(struct xhci_sideband *sb, struct xhci_ring *ring)
 	return NULL;
 }
 
+/* Return the current vdev of sb->udev, or NULL if it was freed or reallocated */
+static struct xhci_virt_device *
+sideband_to_vdev(struct xhci_sideband *sb)
+{
+	struct xhci_virt_device *vdev;
+
+	if (!sb->udev || !sb->udev->slot_id)
+		return NULL;
+
+	vdev = sb->xhci->devs[sb->udev->slot_id];
+	if (vdev && vdev->sideband == sb)
+		return vdev;
+
+	return NULL;
+}
+
 /* Caller must hold sb->mutex */
 static void
 __xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
@@ -146,16 +162,18 @@ int
 xhci_sideband_add_endpoint(struct xhci_sideband *sb,
 			   struct usb_host_endpoint *host_ep)
 {
+	struct xhci_virt_device *vdev;
 	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
 
 	guard(mutex)(&sb->mutex);
 
-	if (!sb->vdev)
+	vdev = sideband_to_vdev(sb);
+	if (!vdev)
 		return -ENODEV;
 
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = &sb->vdev->eps[ep_index];
+	ep = &vdev->eps[ep_index];
 
 	if (ep->ep_state & EP_HAS_STREAMS)
 		return -EINVAL;
@@ -202,9 +220,10 @@ xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) {
+		sb->eps[ep_index] = NULL;
 		return -ENODEV;
-
+	}
 	__xhci_sideband_remove_endpoint(sb, ep);
 
 	return 0;
@@ -221,7 +240,7 @@ xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb)
 		return -EINVAL;
 
 	return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
@@ -251,7 +270,7 @@ xhci_sideband_get_endpoint_buffer(struct xhci_sideband *sb,
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 	ep = sb->eps[ep_index];
 
-	if (!ep || !ep->ring || !ep->sideband || ep->sideband != sb)
+	if (!sideband_to_vdev(sb) || !ep || !ep->ring || ep->sideband != sb)
 		return NULL;
 
 	return xhci_ring_to_sgtable(sb, ep->ring);
@@ -329,7 +348,7 @@ xhci_sideband_create_interrupter(struct xhci_sideband *sb, int num_seg,
 
 	guard(mutex)(&sb->mutex);
 
-	if (!sb->vdev)
+	if (!sideband_to_vdev(sb))
 		return -ENODEV;
 
 	if (sb->ir)
@@ -436,7 +455,7 @@ xhci_sideband_register(struct usb_interface *intf, enum xhci_sideband_type type,
 	}
 
 	sb->xhci = xhci;
-	sb->vdev = vdev;
+	sb->udev = udev;
 	sb->intf = intf;
 	sb->type = type;
 	sb->notify_client = notify_client;
@@ -471,22 +490,21 @@ xhci_sideband_unregister(struct xhci_sideband *sb)
 	xhci = sb->xhci;
 
 	scoped_guard(mutex, &sb->mutex) {
-		vdev = sb->vdev;
-		if (!vdev)
-			return;
+		vdev = sideband_to_vdev(sb);
 
 		for (i = 0; i < EP_CTX_PER_DEV; i++)
-			if (sb->eps[i])
+			if (vdev && sb->eps[i])
 				__xhci_sideband_remove_endpoint(sb, sb->eps[i]);
 
 		__xhci_sideband_remove_interrupter(sb);
 
-		sb->vdev = NULL;
+		sb->udev = NULL;
 	}
 
 	spin_lock_irq(&xhci->lock);
 	sb->xhci = NULL;
-	vdev->sideband = NULL;
+	if (vdev)
+		vdev->sideband = NULL;
 	spin_unlock_irq(&xhci->lock);
 
 	kfree(sb);
include/linux/usb/xhci-sideband.h +2 -2
index 005257085dcb..2d153cb57831 100644
@@ -39,7 +39,7 @@ struct xhci_sideband_event {
 /**
  * struct xhci_sideband - representation of a sideband accessed usb device.
  * @xhci: The xhci host controller the usb device is connected to
- * @vdev: the usb device accessed via sideband
+ * @udev: the usb device accessed via sideband
  * @eps: array of endpoints controlled via sideband
  * @ir: event handling and buffer for sideband accessed device
  * @type: xHCI sideband type
@@ -51,7 +51,7 @@ struct xhci_sideband_event {
  */
 struct xhci_sideband {
 	struct xhci_hcd                 *xhci;
-	struct xhci_virt_device         *vdev;
+	struct usb_device		*udev;
 	struct xhci_virt_ep             *eps[EP_CTX_PER_DEV];
 	struct xhci_interrupter         *ir;
 	enum xhci_sideband_type		type;
Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/10 xhci: sideband: check virt device under xhci lock when stopping endpoints

In reply to Mathias Nyman

Start work to protect sideband API functions against the xhci driver freeing or reallocating the virt device (vdev) in the middle of an API call.

Make the following sideband API calls hold xhci->lock while validating the vdev and its endpoint, and while queueing the stop endpoint command:

xhci_sideband_unregister()
xhci_sideband_stop_endpoint()
xhci_sideband_remove_endpoint()

Locking changes are still needed in several xhci 'core' driver vdev free paths before the above calls are fully serialized against vdev free. Cases involve xHC reset in resume, enumeration retry, and reset-resume.

Move the stop endpoint code from xhci_stop_endpoint_sync() in xhci.c to a new sideband_stop_endpoint_sync() helper in xhci-sideband.c.

xhci_stop_endpoint_sync() has no other users, so remove it.

Also take sb->mutex in xhci_sideband_stop_endpoint(), and keep holding it in xhci_sideband_unregister() until the sideband is detached from the vdev.

Fixes de66754e9f80 ("xhci: sideband: add initial api to register a secondary interrupter entity")
---
 drivers/usb/host/xhci-sideband.c | 121 ++++++++++++++++++++++---------
 drivers/usb/host/xhci.c          |  55 --------------
 drivers/usb/host/xhci.h          |   2 -
 3 files changed, 87 insertions(+), 91 deletions(-)
drivers/usb/host/xhci-sideband.c +87 -34
index e4dd298b80f1..6f7712f95033 100644
@@ -91,20 +91,78 @@ sideband_to_vdev(struct xhci_sideband *sb)
 	return NULL;
 }
 
-/* Caller must hold sb->mutex */
-static void
-__xhci_sideband_remove_endpoint(struct xhci_sideband *sb, struct xhci_virt_ep *ep)
+/*
+ * Synchronous xHCI stop endpoint helper. Issues a stop endpoint command and
+ * waits for the command completion before returning. This does not call
+ * xhci_handle_cmd_stop_ep(), which has additional handling for 'context error'
+ * cases, along with transfer ring cleanup.
+ *
+ * Caller must hold sb->mutex
+ */
+static int
+sideband_stop_endpoint_sync(struct xhci_sideband *sb, unsigned int ep_index,
+			    bool remove)
 {
+	struct xhci_virt_device *vdev;
+	struct xhci_command *cmd;
+	struct xhci_virt_ep *ep;
+	struct xhci_ep_ctx *ctx;
+	unsigned long flags;
+	int ret = -ENODEV;
+
 	lockdep_assert_held(&sb->mutex);
 
-	/*
-	 * Issue a stop endpoint command when an endpoint is removed.
-	 * The stop ep cmd handler will handle the ring cleanup.
-	 */
-	xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
+	cmd = xhci_alloc_command(sb->xhci, true, GFP_KERNEL);
+
+	spin_lock_irqsave(&sb->xhci->lock, flags);
+
+	vdev = sideband_to_vdev(sb);
+	ep = sb->eps[ep_index];
+
+	/* validate that xhci didn't reallocate vdev during reset or resume */
+	if (!vdev || !ep || ep->sideband != sb)
+		goto out_unlock;
+
+	if (remove) {
+		ep->sideband = NULL;
+		/* endpoint is removed, not running is not an error */
+		ret = 0;
+	}
+
+	if (!ep->ring)
+		goto out_unlock;
+
+	/* Stop the endpoint if it's running */
+	ctx = xhci_get_ep_ctx(sb->xhci, vdev->out_ctx, ep_index);
+	if (GET_EP_CTX_STATE(ctx) == EP_STATE_RUNNING) {
+		if (!cmd) {
+			ret = -ENOMEM;
+			goto out_unlock;
+		}
+		ret = xhci_queue_stop_endpoint(sb->xhci, cmd, vdev->slot_id,
+					       ep_index, 0);
+		if (ret)
+			goto out_unlock;
+
+		xhci_ring_cmd_db(sb->xhci);
+		spin_unlock_irqrestore(&sb->xhci->lock, flags);
+		wait_for_completion(cmd->completion);
+
+		if (cmd->status == COMP_COMMAND_ABORTED ||
+		    cmd->status == COMP_COMMAND_RING_STOPPED) {
+			xhci_warn(sb->xhci, "Timeout while waiting for stop endpoint command\n");
+			ret = -ETIME;
+		}
+		xhci_free_command(sb->xhci, cmd);
+		return ret;
+	}
 
-	ep->sideband = NULL;
-	sb->eps[ep->ep_index] = NULL;
+out_unlock:
+	spin_unlock_irqrestore(&sb->xhci->lock, flags);
+	if (cmd)
+		xhci_free_command(sb->xhci, cmd);
+
+	return ret;
 }
 
 /* Caller must hold sb->mutex */
@@ -212,21 +270,17 @@ int
 xhci_sideband_remove_endpoint(struct xhci_sideband *sb,
 			      struct usb_host_endpoint *host_ep)
 {
-	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
+	int ret;
 
 	guard(mutex)(&sb->mutex);
 
 	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = sb->eps[ep_index];
 
-	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb) {
-		sb->eps[ep_index] = NULL;
-		return -ENODEV;
-	}
-	__xhci_sideband_remove_endpoint(sb, ep);
+	ret = sideband_stop_endpoint_sync(sb, ep_index, true);
+	sb->eps[ep_index] = NULL;
 
-	return 0;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_remove_endpoint);
 
@@ -234,16 +288,13 @@ int
 xhci_sideband_stop_endpoint(struct xhci_sideband *sb,
 			    struct usb_host_endpoint *host_ep)
 {
-	struct xhci_virt_ep *ep;
 	unsigned int ep_index;
 
-	ep_index = xhci_get_endpoint_index(&host_ep->desc);
-	ep = sb->eps[ep_index];
+	guard(mutex)(&sb->mutex);
 
-	if (!sideband_to_vdev(sb) || !ep || ep->sideband != sb)
-		return -EINVAL;
+	ep_index = xhci_get_endpoint_index(&host_ep->desc);
 
-	return xhci_stop_endpoint_sync(sb->xhci, ep, 0, GFP_KERNEL);
+	return sideband_stop_endpoint_sync(sb, ep_index, false);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_stop_endpoint);
 
@@ -489,24 +540,26 @@ xhci_sideband_unregister(struct xhci_sideband *sb)
 
 	xhci = sb->xhci;
 
-	scoped_guard(mutex, &sb->mutex) {
-		vdev = sideband_to_vdev(sb);
-
-		for (i = 0; i < EP_CTX_PER_DEV; i++)
-			if (vdev && sb->eps[i])
-				__xhci_sideband_remove_endpoint(sb, sb->eps[i]);
+	mutex_lock(&sb->mutex);
 
-		__xhci_sideband_remove_interrupter(sb);
-
-		sb->udev = NULL;
+	for (i = 0; i < EP_CTX_PER_DEV; i++) {
+		if (sb->eps[i]) {
+			sideband_stop_endpoint_sync(sb, i, true);
+			sb->eps[i] = NULL;
+		}
 	}
+	__xhci_sideband_remove_interrupter(sb);
 
 	spin_lock_irq(&xhci->lock);
-	sb->xhci = NULL;
+	vdev = sideband_to_vdev(sb);
 	if (vdev)
 		vdev->sideband = NULL;
+	sb->udev = NULL;
+	sb->xhci = NULL;
 	spin_unlock_irq(&xhci->lock);
 
+	mutex_unlock(&sb->mutex);
+
 	kfree(sb);
 }
 EXPORT_SYMBOL_GPL(xhci_sideband_unregister);
drivers/usb/host/xhci.c +0 -55

61 lines not on this page · raw message

drivers/usb/host/xhci.h +0 -2
index ec4bfeb4887c..6201b6b24252 100644
@@ -1963,8 +1963,6 @@ void xhci_cleanup_command_queue(struct xhci_hcd *xhci);
 void inc_deq(struct xhci_hcd *xhci, struct xhci_ring *ring);
 unsigned int count_trbs(u64 addr, u64 len);
 unsigned int xhci_num_trbs_free(struct xhci_ring *ring);
-int xhci_stop_endpoint_sync(struct xhci_hcd *xhci, struct xhci_virt_ep *ep,
-			    int suspend, gfp_t gfp_flags);
 void xhci_process_cancelled_tds(struct xhci_hcd *xhci, struct xhci_virt_ep *ep);
 void xhci_update_erst_dequeue(struct xhci_hcd *xhci,
 			      struct xhci_interrupter *ir,
Signature · 1 line
2.43.0
#7 lore raw

PATCH 7/10 usb: xhci: tegra: Fix missing semi-colon

In reply to Mathias Nyman

From: Jon Hunter <[email protected]>

Commit 6d45e9556d4a ("usb: xhci: standardize multi bit-field macros") updated the Tegra XHCI driver replacing the HC_LENGTH() macro with the FIELD_GET() macro, but in doing so dropped the trailing semi-colon. This still compiled because the comma operator joined the assignment with the following polling expression. Replace the comma with a semicolon to make them separate statements.

Fixes 6d45e9556d4a ("usb: xhci: standardize multi bit-field macros")
---
 drivers/usb/host/xhci-tegra.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/host/xhci-tegra.c +1 -1
index 1a14aea58b0b..5fbb09d2a1ae 100644
@@ -995,7 +995,7 @@ static int tegra_xusb_wait_for_falcon(struct tegra_xusb *tegra)
 	u32 value;
 
 	cap_regs = tegra->regs;
-	op_regs = tegra->regs + FIELD_GET(HC_LENGTH, readl(&cap_regs->hc_capbase)),
+	op_regs = tegra->regs + FIELD_GET(HC_LENGTH, readl(&cap_regs->hc_capbase));
 
 	ret = readl_poll_timeout(&op_regs->status, value, !(value & STS_CNR), 1000, 200000);
 
Signature · 1 line
2.43.0
#8 lore raw

PATCH 8/10 usb: xhci: Demote isoch buffer overrun message to debug level

In reply to Mathias Nyman

From: Alex Kononov <[email protected]>

An isochronous Buffer Overrun event means the device sent more data than the TD could hold.
This is triggered by the device, not by a driver bug, and the error is already reported to the class driver as -EOVERFLOW in iso_frame_desc[].status.

Logging it with xhci_warn() can flood the kernel log. A USB S/PDIF adapter (262a:9123) whose S/PDIF input loses its signal produces this event on nearly every microframe, about 7,200 messages per second, keeping systemd-journald busy at 100% of a CPU.

Use xhci_dbg() like the other device-triggerable completion codes.

[mn: remove the "WARN" part of the debug message as well]

---
 drivers/usb/host/xhci-ring.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/usb/host/xhci-ring.c +3 -3
index a1fa188680f5..fca23daf6add 100644
@@ -2815,9 +2815,9 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 			  slot_id, ep_index);
 		break;
 	case COMP_ISOCH_BUFFER_OVERRUN:
-		xhci_warn(xhci,
-			  "WARN: buffer overrun event for slot %u ep %u on endpoint",
-			  slot_id, ep_index);
+		xhci_dbg(xhci,
+			 "buffer overrun event for slot %u ep %u on endpoint",
+			 slot_id, ep_index);
 		break;
 	case COMP_RING_UNDERRUN:
 		/*
Signature · 1 line
2.43.0
#9 lore raw

PATCH 9/10 xhci: fix use-after-free for errors mid TD on empty halted rings

In reply to Mathias Nyman

If a TD with an error mid TD is followed by an event that doesn't point to that TD, handle_tx_event() gives back the TD with xhci_dequeue_td(). If this was the last TD of the URB, the URB is given back and the TD, part of the freed urb_priv, is freed with it.

If the TD list is then empty and the endpoint is halted, handle_tx_event() jumps to check_endpoint_halted and passes the now dangling td pointer to xhci_handle_halted_endpoint(), which adds it to the endpoint's cancelled TD list. This causes a use-after-free and corrupts the cancelled TD list.

Set td to NULL after giving it back. xhci_handle_halted_endpoint() handles a NULL td, and td is reassigned if there are more TDs on the ring.

Fixes f42a36bae064 ("usb: xhci: Fix handling errors mid TD followed by other errors")
---
 drivers/usb/host/xhci-ring.c | 1 +
 1 file changed, 1 insertion(+)
drivers/usb/host/xhci-ring.c +1 -0
index fca23daf6add..7ccd89a4b995 100644
@@ -2891,6 +2891,7 @@ static int handle_tx_event(struct xhci_hcd *xhci,
 	if (td && td->error_mid_td && !trb_in_td(td, ep_trb_dma)) {
 		xhci_dbg(xhci, "Missing TD completion event after mid TD error\n");
 		xhci_dequeue_td(xhci, td, ep_ring, td->status);
+		td = NULL;
 	}
 
 	if (list_empty(&ep_ring->td_list)) {
Signature · 1 line
2.43.0
#10 lore raw

PATCH 10/10 xhci: fix NULL pointer dereference of rhub_port in xhci_urb_enqueue()

In reply to Mathias Nyman

xhci_urb_enqueue() checks vdev->rhub_port->link_inactive before queueing a URB, but rhub_port is only set in xhci_setup_addressable_virt_dev() when the device is enabled or addressed.

If the virt device is freed and reallocated for an existing usb device, the new virt device has no rhub_port until usb core enables or addresses the device again. This happens for example when the xHC is reset during resume, and xhci_discover_or_reset_device() reallocates the virt device during reset-resume.

usb core does not send URBs to the device in this window, but a driver that keeps submitting I/O across system suspend does. This was reported with hid-steelseries-arctis, whose status polling work sends a control request during reset-resume after the xHC failed to restore its state:

 BUG: kernel NULL pointer dereference, address: 0000000000000020
 Workqueue: events steelseries_status_timer_work_handler [hid_steelseries_arctis]
 RIP: 0010:xhci_urb_enqueue+0x1c8/0x350
 Call Trace:
  usb_hcd_submit_urb+0x91/0x190
  usb_start_wait_urb+0x8f/0x1d0
  usb_control_msg+0xdd/0x150
  usbhid_raw_request+0x11e/0x140
  steelseries_arctis_9_request_status+0x63/0xb0 [hid_steelseries_arctis]

Refuse to queue URBs to a device that has no root hub port yet. This also ensures no TDs are queued for a device without rhub_port, which the transfer event and halted endpoint handling code dereference as well.

---
 drivers/usb/host/xhci.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/usb/host/xhci.c +5 -2
index fa6f8b879a18..7f0d34c465f8 100644
@@ -1631,6 +1631,7 @@ static int xhci_check_ep0_maxpacket(struct xhci_hcd *xhci, struct xhci_virt_devi
 static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
 {
 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
+	struct xhci_virt_device *vdev;
 	unsigned long flags;
 	int ret = 0;
 	unsigned int slot_id, ep_index;
@@ -1683,9 +1684,11 @@ static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flag
 		goto free_priv;
 	}
 
-	ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;
+	vdev = xhci->devs[slot_id];
+	ep_state = &vdev->eps[ep_index].ep_state;
 
-	if (xhci->devs[slot_id]->rhub_port->link_inactive ||
+	/* rhub_port is only set once the device is enabled or addressed */
+	if (!vdev->rhub_port || vdev->rhub_port->link_inactive ||
 	    *ep_state & EP_DROP_PENDING) {
 		xhci_dbg(xhci, "Can't queue urb, port disconnect or link inactive\n");
 		ret = -ENODEV;
Signature · 1 line
2.43.0