[PATCH v4 2/3] usb: core: Close anchor capture-to-increment race in URB giveback

From: Hongyu Xie

Date: Wed Dec 02 2026 - 19:48:59 EST


__usb_hcd_giveback_urb() reads urb->anchor at entry but only calls
usb_anchor_suspend_wakeups() after DMA unmapping and usbmon hooks.
If another thread unanchors the URB during that window, the anchor
can become empty with suspend_wakeups == 0, allowing a waiter to
return and free the anchor before the giveback increments
suspend_wakeups and later calls usb_anchor_resume_wakeups().

Add usb_unanchor_urb_and_suspend_wakeups() to re-check urb->anchor,
increment suspend_wakeups, and unanchor the URB under a single
anchor->lock critical section. If a concurrent unanchor won the race,
return NULL so the giveback does not touch the anchor.

Fixes: 6ec4147e7bdb ("usb-anchor: Delay usb_wait_anchor_empty_timeout wake up till completion is done")
Cc: stable@xxxxxxxxxxxxxxx
Cc: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
Cc: Oliver Neukum <oliver@xxxxxxxxxx>
Cc: Hans de Goede <hansg@xxxxxxxxxx>
Signed-off-by: Hongyu Xie <xiehongyu1@xxxxxxxxxx>
---
drivers/usb/core/hcd.c | 5 ++---
drivers/usb/core/urb.c | 25 +++++++++++++++++++++++++
drivers/usb/core/usb.h | 1 +
3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/core/hcd.c b/drivers/usb/core/hcd.c
index 8e8a5f59b319..0d7ee6b625bc 100644
--- a/drivers/usb/core/hcd.c
+++ b/drivers/usb/core/hcd.c
@@ -1630,7 +1630,7 @@ int usb_hcd_unlink_urb (struct urb *urb, int status)
static void __usb_hcd_giveback_urb(struct urb *urb)
{
struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
- struct usb_anchor *anchor = urb->anchor;
+ struct usb_anchor *anchor;
int status = urb->unlinked;

urb->hcpriv = NULL;
@@ -1641,8 +1641,7 @@ static void __usb_hcd_giveback_urb(struct urb *urb)

unmap_urb_for_dma(hcd, urb);
usbmon_urb_complete(&hcd->self, urb, status);
- usb_anchor_suspend_wakeups(anchor);
- usb_unanchor_urb(urb);
+ anchor = usb_unanchor_urb_and_suspend_wakeups(urb);
if (likely(status == 0))
usb_led_activity(USB_LED_EVENT_HOST);

diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index 5ecde5dc613e..d1d7ed6e9950 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -15,6 +15,8 @@
#include <linux/usb/hcd.h>
#include <linux/scatterlist.h>

+#include "usb.h"
+
#define to_urb(d) container_of(d, struct urb, kref)


@@ -197,6 +199,29 @@ void usb_unanchor_urb(struct urb *urb)
}
EXPORT_SYMBOL_GPL(usb_unanchor_urb);

+/*
+ * Atomically unanchor @urb and increment anchor->suspend_wakeups.
+ * Returns the anchor to pass to usb_anchor_resume_wakeups(), or NULL
+ * if @urb was already unanchored concurrently.
+ */
+struct usb_anchor *usb_unanchor_urb_and_suspend_wakeups(struct urb *urb)
+{
+ struct usb_anchor *anchor;
+
+ anchor = urb->anchor;
+ if (!anchor)
+ return NULL;
+
+ guard(spinlock_irqsave)(&anchor->lock);
+ if (likely(anchor == urb->anchor)) {
+ atomic_inc(&anchor->suspend_wakeups);
+ __usb_unanchor_urb(urb, anchor);
+ return anchor;
+ }
+
+ return NULL;
+}
+
/*-------------------------------------------------------------------*/

static const int pipetypes[4] = {
diff --git a/drivers/usb/core/usb.h b/drivers/usb/core/usb.h
index a9b37aeb515b..362bfda497cc 100644
--- a/drivers/usb/core/usb.h
+++ b/drivers/usb/core/usb.h
@@ -20,6 +20,7 @@ extern int usb_create_ep_devs(struct device *parent,
struct usb_host_endpoint *endpoint,
struct usb_device *udev);
extern void usb_remove_ep_devs(struct usb_host_endpoint *endpoint);
+extern struct usb_anchor *usb_unanchor_urb_and_suspend_wakeups(struct urb *urb);

extern void usb_enable_endpoint(struct usb_device *dev,
struct usb_host_endpoint *ep, bool reset_toggle);
--
2.32.0