* [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol
@ 2026-12-03 0:48 Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Hongyu Xie @ 2026-12-03 0:48 UTC (permalink / raw)
To: Greg KH, Alan Stern
Cc: Oliver Neukum, oliver, Hans de Goede, linux-usb, linux-kernel
This series closes three use-after-free windows in the USB anchor
teardown and URB giveback protocol:
1. Serialize usb_anchor_check_wakeup() and usb_anchor_resume_wakeups()
under anchor->lock so a waiter cannot observe the anchor unused and
free it while a giveback still touches anchor->wait.
2. Atomically re-check urb->anchor, increment anchor->suspend_wakeups,
and unanchor the URB under anchor->lock during URB giveback via
usb_unanchor_urb_and_suspend_wakeups().
3. Protect lockless urb->anchor readers with contention-gated RCU and
replace busy-waiting in kill/poison/scuttle drains with wait_event().
Changes since v3:
- Use guard(spinlock_irqsave), scoped_guard(), and guard(rcu)() in
drivers/usb/core/urb.c.
- Annotate __usb_anchor_check_wakeup(), __usb_anchor_prepare_rcu_gp(),
and __usb_unanchor_urb() with __must_hold(&anchor->lock) and
lockdep_assert_held(&anchor->lock).
- Shorten comments and rewrite all commit messages concisely per review.
v3: https://lore.kernel.org/r/179578612505.44635.14553263875690325600@kylinos.cn
v3 review: https://lore.kernel.org/all/2026112752-comic-stream-65a3@gregkh/
Hongyu Xie (3):
usb: core: Fix use-after-free in anchor wakeup protocol
usb: core: Close anchor capture-to-increment race in URB giveback
usb: core: Close residual anchor read-to-lock window with RCU
Documentation/driver-api/usb/anchors.rst | 14 +-
drivers/usb/core/hcd.c | 5 +-
drivers/usb/core/urb.c | 305 +++++++++++++++++++----
drivers/usb/core/usb.h | 1 +
include/linux/usb.h | 8 +-
5 files changed, 276 insertions(+), 57 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.32.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol
2026-12-03 0:48 [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol Hongyu Xie
@ 2026-12-03 0:48 ` Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 2/3] usb: core: Close anchor capture-to-increment race in URB giveback Hongyu Xie
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Hongyu Xie @ 2026-12-03 0:48 UTC (permalink / raw)
To: Greg KH, Alan Stern
Cc: Oliver Neukum, oliver, Hans de Goede, linux-usb, linux-kernel
Drivers embed struct usb_anchor in private structures and free them
once usb_wait_anchor_empty_timeout() returns non-zero (for example,
xpad_disconnect() frees xpad->irq_out_anchor).
Two races allow a waiter to return and free the anchor while an URB
giveback still touches anchor->wait:
1. usb_anchor_resume_wakeups() decrements suspend_wakeups and calls
wake_up(&anchor->wait) without holding anchor->lock. A waiter can
observe suspend_wakeups == 0 and free the anchor before wake_up()
runs.
2. usb_wait_anchor_empty_timeout() evaluates usb_anchor_check_wakeup()
without holding anchor->lock, so it can observe an empty urb_list
while __usb_unanchor_urb() has not yet finished wake_up() and
released anchor->lock.
Take anchor->lock via guard(spinlock_irqsave) in
usb_anchor_check_wakeup() and usb_anchor_resume_wakeups(). Keep
__usb_anchor_check_wakeup() for callers that already hold anchor->lock,
and annotate both it and __usb_unanchor_urb() with __must_hold() and
lockdep_assert_held().
syzbot reported this race repeatedly:
BUG: KASAN: use-after-free in register_lock_class+0xe6a/0x1120 kernel/locking/lockdep.c:1336
[...]
__wake_up_common_lock+0xb4/0x130 kernel/sched/wait.c:136
usb_anchor_resume_wakeups drivers/usb/core/urb.c:958 [inline]
usb_anchor_resume_wakeups+0xbe/0xe0 drivers/usb/core/urb.c:951
__usb_hcd_giveback_urb+0x2df/0x5c0 drivers/usb/core/hcd.c:1674
Freed by task 3889:
[...]
kfree+0x173/0x390 mm/slab.c:3786
xpad_disconnect+0x1cb/0x530 drivers/input/joystick/xpad.c:1905
Fixes: 6ec4147e7bdb ("usb-anchor: Delay usb_wait_anchor_empty_timeout wake up till completion is done")
Fixes: 51a2f077c44e ("USB: introduce usb_anchor")
Reported-by: syzbot+7f5e98b052c766f47f68@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/r/00000000000090170a0614431f47@google.com
Reported-by: syzbot+59d3714b44d79d02f944@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/r/00000000000038435e05e9f94cd4@google.com
Cc: stable@vger.kernel.org
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oliver@neukum.org>
Cc: Hans de Goede <hansg@kernel.org>
Signed-off-by: Hongyu Xie <xiehongyu1@kylinos.cn>
---
drivers/usb/core/urb.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index ae0d61b5081f..5ecde5dc613e 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -6,6 +6,7 @@
#include <linux/module.h>
#include <linux/string.h>
#include <linux/bitops.h>
+#include <linux/cleanup.h>
#include <linux/slab.h>
#include <linux/log2.h>
#include <linux/kmsan.h>
@@ -139,19 +140,30 @@ void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor)
}
EXPORT_SYMBOL_GPL(usb_anchor_urb);
-static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
+static int __usb_anchor_check_wakeup(struct usb_anchor *anchor)
+ __must_hold(&anchor->lock)
{
+ lockdep_assert_held(&anchor->lock);
+
return atomic_read(&anchor->suspend_wakeups) == 0 &&
list_empty(&anchor->urb_list);
}
-/* Callers must hold anchor->lock */
+static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
+{
+ guard(spinlock_irqsave)(&anchor->lock);
+ return __usb_anchor_check_wakeup(anchor);
+}
+
static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
+ __must_hold(&anchor->lock)
{
+ lockdep_assert_held(&anchor->lock);
+
urb->anchor = NULL;
list_del(&urb->anchor_list);
usb_put_urb(urb);
- if (usb_anchor_check_wakeup(anchor))
+ if (__usb_anchor_check_wakeup(anchor))
wake_up(&anchor->wait);
}
@@ -835,7 +847,7 @@ void usb_kill_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irq(&anchor->lock);
cpu_relax();
@@ -874,7 +886,7 @@ void usb_poison_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irq(&anchor->lock);
cpu_relax();
@@ -930,8 +942,9 @@ void usb_anchor_resume_wakeups(struct usb_anchor *anchor)
if (!anchor)
return;
+ guard(spinlock_irqsave)(&anchor->lock);
atomic_dec(&anchor->suspend_wakeups);
- if (usb_anchor_check_wakeup(anchor))
+ if (__usb_anchor_check_wakeup(anchor))
wake_up(&anchor->wait);
}
EXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups);
@@ -1005,7 +1018,7 @@ void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
struct urb, anchor_list);
__usb_unanchor_urb(victim, anchor);
}
- surely_empty = usb_anchor_check_wakeup(anchor);
+ surely_empty = __usb_anchor_check_wakeup(anchor);
spin_unlock_irqrestore(&anchor->lock, flags);
cpu_relax();
--
2.32.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 2/3] usb: core: Close anchor capture-to-increment race in URB giveback
2026-12-03 0:48 [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
@ 2026-12-03 0:48 ` Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 3/3] usb: core: Close residual anchor read-to-lock window with RCU Hongyu Xie
2026-12-03 1:04 ` [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol sashiko-bot
3 siblings, 0 replies; 5+ messages in thread
From: Hongyu Xie @ 2026-12-03 0:48 UTC (permalink / raw)
To: Greg KH, Alan Stern
Cc: Oliver Neukum, oliver, Hans de Goede, linux-usb, linux-kernel
__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@vger.kernel.org
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oliver@neukum.org>
Cc: Hans de Goede <hansg@kernel.org>
Signed-off-by: Hongyu Xie <xiehongyu1@kylinos.cn>
---
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v4 3/3] usb: core: Close residual anchor read-to-lock window with RCU
2026-12-03 0:48 [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 2/3] usb: core: Close anchor capture-to-increment race in URB giveback Hongyu Xie
@ 2026-12-03 0:48 ` Hongyu Xie
2026-12-03 1:04 ` [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol sashiko-bot
3 siblings, 0 replies; 5+ messages in thread
From: Hongyu Xie @ 2026-12-03 0:48 UTC (permalink / raw)
To: Greg KH, Alan Stern
Cc: Oliver Neukum, oliver, Hans de Goede, linux-usb, linux-kernel
usb_unanchor_urb() and usb_unanchor_urb_and_suspend_wakeups() read
urb->anchor before acquiring anchor->lock. A concurrent unanchor of
the last URB can empty the anchor and let a waiter return and free it
before spin_lock_irqsave(&anchor->lock) runs.
A reference count on the anchor cannot protect the first dereference
of the freshly read pointer, while an unconditional synchronize_rcu()
on every wait or drain would stall uncontended runtime I/O paths.
Use contention-gated RCU protection for urb->anchor:
- Lockless readers (usb_unanchor_urb() and
usb_unanchor_urb_and_suspend_wakeups()) take an RCU read lock,
increment urb->anchor_readers, and order it with
smp_mb__after_atomic() before rcu_dereference(urb->anchor), with an
early rcu_access_pointer() exit when unanchored. usb_anchor_urb()
publishes urb->anchor with rcu_assign_pointer().
- Under anchor->lock, __usb_unanchor_urb() clears urb->anchor, orders
the clear against urb->anchor_readers, and only when another reader
is still in flight records get_state_synchronize_rcu() in
anchor->rcu_gp_seq and sets anchor->need_rcu_gp.
- usb_wait_anchor_empty_timeout(), usb_kill_anchored_urbs(),
usb_poison_anchored_urbs(), and usb_scuttle_anchored_urbs() check
anchor->need_rcu_gp under anchor->lock when the anchor becomes
unused, clear it if poll_state_synchronize_rcu() has already
elapsed, and otherwise call cond_synchronize_rcu() after unlocking
without touching the anchor again.
Replace the cpu_relax() busy-wait in kill/poison/scuttle with
wait_event() on anchor->wait (woken by usb_anchor_urb() when
waitqueue_active() is true so late-anchored URBs are still drained).
Busy-waiting prevents !PREEMPT_RCU CPUs from reporting quiescent
states and can stall grace periods. Add might_sleep() to all three
drain functions; all in-tree callers already sleep.
Uncontended waits pay no grace period and preserve remaining-jiffies
returns in usb_wait_anchor_empty_timeout(). A contended wait may
overrun its timeout by up to one grace period and clamps a non-zero
return to at least 1. Update Documentation/driver-api/usb/anchors.rst
to match.
Fixes: 51a2f077c44e ("USB: introduce usb_anchor")
Cc: stable@vger.kernel.org
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: Oliver Neukum <oliver@neukum.org>
Cc: Hans de Goede <hansg@kernel.org>
Signed-off-by: Hongyu Xie <xiehongyu1@kylinos.cn>
---
Documentation/driver-api/usb/anchors.rst | 14 +-
drivers/usb/core/urb.c | 271 ++++++++++++++++++-----
include/linux/usb.h | 8 +-
3 files changed, 237 insertions(+), 56 deletions(-)
diff --git a/Documentation/driver-api/usb/anchors.rst b/Documentation/driver-api/usb/anchors.rst
index 5a93d171e76c..e6aa7e7ebc3f 100644
--- a/Documentation/driver-api/usb/anchors.rst
+++ b/Documentation/driver-api/usb/anchors.rst
@@ -22,8 +22,12 @@ initialise the data structure.
Deallocation
============
-Once it has no more URBs associated with it, the anchor can be
-freed with normal memory management operations.
+Once it has no more URBs associated with it, and one of
+usb_wait_anchor_empty_timeout(), usb_kill_anchored_urbs(),
+usb_poison_anchored_urbs(), or usb_scuttle_anchored_urbs() has returned
+successfully, the anchor can be freed with normal memory management
+operations, provided no new URBs are anchored and no other teardown
+call on it is still in flight.
Association and disassociation of URBs with anchors
===================================================
@@ -48,14 +52,16 @@ This way no data can be reordered.
:c:func:`usb_scuttle_anchored_urbs`
-----------------------------------
-All URBs of an anchor are unanchored en masse.
+All URBs of an anchor are unanchored en masse. This function may sleep.
:c:func:`usb_wait_anchor_empty_timeout`
---------------------------------------
This function waits for all URBs associated with an anchor to finish
or a timeout, whichever comes first. Its return value will tell you
-whether the timeout was reached.
+whether the timeout was reached (0 on timeout, remaining jiffies or 1
+otherwise). If a concurrent unanchor raced on the anchor, a successful
+wait may sleep for one RCU grace period and overrun the timeout.
:c:func:`usb_anchor_empty`
--------------------------
diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
index d1d7ed6e9950..f4800f7f3f41 100644
--- a/drivers/usb/core/urb.c
+++ b/drivers/usb/core/urb.c
@@ -10,6 +10,7 @@
#include <linux/slab.h>
#include <linux/log2.h>
#include <linux/kmsan.h>
+#include <linux/rcupdate.h>
#include <linux/usb.h>
#include <linux/wait.h>
#include <linux/usb/hcd.h>
@@ -133,11 +134,18 @@ void usb_anchor_urb(struct urb *urb, struct usb_anchor *anchor)
spin_lock_irqsave(&anchor->lock, flags);
usb_get_urb(urb);
list_add_tail(&urb->anchor_list, &anchor->urb_list);
- urb->anchor = anchor;
+ rcu_assign_pointer(urb->anchor, anchor);
if (unlikely(anchor->poisoned))
atomic_inc(&urb->reject);
+ /*
+ * Wake any sleeping teardown drain for this late URB; anchor->lock
+ * orders list_add_tail() with waitqueue_active() against the waiter.
+ */
+ if (waitqueue_active(&anchor->wait))
+ wake_up(&anchor->wait);
+
spin_unlock_irqrestore(&anchor->lock, flags);
}
EXPORT_SYMBOL_GPL(usb_anchor_urb);
@@ -151,18 +159,87 @@ static int __usb_anchor_check_wakeup(struct usb_anchor *anchor)
list_empty(&anchor->urb_list);
}
-static int usb_anchor_check_wakeup(struct usb_anchor *anchor)
+static bool __usb_anchor_prepare_rcu_gp(struct usb_anchor *anchor,
+ unsigned long *gp_seq)
+ __must_hold(&anchor->lock)
+{
+ lockdep_assert_held(&anchor->lock);
+
+ if (likely(!anchor->need_rcu_gp))
+ return false;
+
+ if (poll_state_synchronize_rcu(anchor->rcu_gp_seq)) {
+ anchor->need_rcu_gp = 0;
+ return false;
+ }
+
+ *gp_seq = anchor->rcu_gp_seq;
+ return true;
+}
+
+static int usb_anchor_check_wakeup(struct usb_anchor *anchor,
+ bool *need_gp, unsigned long *gp_seq)
+{
+ int wakeup;
+
+ guard(spinlock_irqsave)(&anchor->lock);
+ wakeup = __usb_anchor_check_wakeup(anchor);
+ if (wakeup)
+ *need_gp = __usb_anchor_prepare_rcu_gp(anchor, gp_seq);
+
+ return wakeup;
+}
+
+/*
+ * Wait condition for teardown drains: return true when the anchor is unused
+ * (*unused = true, with any pending RCU cookie captured) or when a late URB
+ * arrived (*unused = false, so the caller restarts the drain).
+ */
+static bool usb_anchor_teardown_progress(struct usb_anchor *anchor,
+ bool *unused, bool *need_gp,
+ unsigned long *gp_seq)
{
+ if (!list_empty_careful(&anchor->urb_list)) {
+ *unused = false;
+ return true;
+ }
+
guard(spinlock_irqsave)(&anchor->lock);
- return __usb_anchor_check_wakeup(anchor);
+ if (!list_empty(&anchor->urb_list)) {
+ *unused = false;
+ return true;
+ }
+
+ *unused = __usb_anchor_check_wakeup(anchor);
+ if (*unused)
+ *need_gp = __usb_anchor_prepare_rcu_gp(anchor, gp_seq);
+
+ return *unused;
}
-static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
+static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor,
+ bool from_reader)
__must_hold(&anchor->lock)
{
+ int remaining;
+
lockdep_assert_held(&anchor->lock);
- urb->anchor = NULL;
+ rcu_assign_pointer(urb->anchor, NULL);
+ if (from_reader) {
+ /* Pairs with smp_mb__after_atomic() in lockless readers. */
+ remaining = atomic_dec_return(&urb->anchor_readers);
+ } else {
+ /* Pairs with smp_mb__after_atomic() in lockless readers. */
+ smp_mb();
+ remaining = atomic_read(&urb->anchor_readers);
+ }
+
+ if (unlikely(remaining > 0)) {
+ anchor->rcu_gp_seq = get_state_synchronize_rcu();
+ anchor->need_rcu_gp = 1;
+ }
+
list_del(&urb->anchor_list);
usb_put_urb(urb);
if (__usb_anchor_check_wakeup(anchor))
@@ -177,25 +254,31 @@ static void __usb_unanchor_urb(struct urb *urb, struct usb_anchor *anchor)
*/
void usb_unanchor_urb(struct urb *urb)
{
- unsigned long flags;
struct usb_anchor *anchor;
- if (!urb)
+ if (!urb || !rcu_access_pointer(urb->anchor))
return;
- anchor = urb->anchor;
- if (!anchor)
+ guard(rcu)();
+ atomic_inc(&urb->anchor_readers);
+ /* Pairs with __usb_unanchor_urb() */
+ smp_mb__after_atomic();
+ anchor = rcu_dereference(urb->anchor);
+ if (!anchor) {
+ atomic_dec(&urb->anchor_readers);
return;
+ }
- spin_lock_irqsave(&anchor->lock, flags);
+ guard(spinlock_irqsave)(&anchor->lock);
/*
* At this point, we could be competing with another thread which
* has the same intention. To protect the urb from being unanchored
* twice, only the winner of the race gets the job.
*/
- if (likely(anchor == urb->anchor))
- __usb_unanchor_urb(urb, anchor);
- spin_unlock_irqrestore(&anchor->lock, flags);
+ if (likely(anchor == rcu_dereference(urb->anchor)))
+ __usb_unanchor_urb(urb, anchor, true);
+ else
+ atomic_dec(&urb->anchor_readers);
}
EXPORT_SYMBOL_GPL(usb_unanchor_urb);
@@ -208,17 +291,27 @@ struct usb_anchor *usb_unanchor_urb_and_suspend_wakeups(struct urb *urb)
{
struct usb_anchor *anchor;
- anchor = urb->anchor;
- if (!anchor)
+ if (!rcu_access_pointer(urb->anchor))
+ return NULL;
+
+ guard(rcu)();
+ atomic_inc(&urb->anchor_readers);
+ /* Pairs with __usb_unanchor_urb() */
+ smp_mb__after_atomic();
+ anchor = rcu_dereference(urb->anchor);
+ if (!anchor) {
+ atomic_dec(&urb->anchor_readers);
return NULL;
+ }
guard(spinlock_irqsave)(&anchor->lock);
- if (likely(anchor == urb->anchor)) {
+ if (likely(anchor == rcu_dereference(urb->anchor))) {
atomic_inc(&anchor->suspend_wakeups);
- __usb_unanchor_urb(urb, anchor);
+ __usb_unanchor_urb(urb, anchor, true);
return anchor;
}
+ atomic_dec(&urb->anchor_readers);
return NULL;
}
@@ -851,15 +944,23 @@ EXPORT_SYMBOL_GPL(usb_block_urb);
* with guarantee that no completer callbacks will take place from the
* anchor after this function returns.
*
+ * This function may sleep. Once it returns, the anchor may be freed if no
+ * new URBs are anchored and no other teardown call on it is in flight.
+ * It must not be called from a completion callback of an URB on @anchor.
+ *
* This routine should not be called by a driver after its disconnect
* method has returned.
*/
void usb_kill_anchored_urbs(struct usb_anchor *anchor)
{
+ unsigned long gp_seq = 0;
+ bool need_gp = false;
struct urb *victim;
- int surely_empty;
+ bool unused;
- do {
+ might_sleep();
+
+ for (;;) {
spin_lock_irq(&anchor->lock);
while (!list_empty(&anchor->urb_list)) {
victim = list_entry(anchor->urb_list.prev,
@@ -872,11 +973,22 @@ void usb_kill_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = __usb_anchor_check_wakeup(anchor);
+ unused = __usb_anchor_check_wakeup(anchor);
+ if (unused)
+ need_gp = __usb_anchor_prepare_rcu_gp(anchor, &gp_seq);
spin_unlock_irq(&anchor->lock);
- cpu_relax();
- } while (!surely_empty);
+
+ if (!unused)
+ wait_event(anchor->wait,
+ usb_anchor_teardown_progress(anchor, &unused,
+ &need_gp, &gp_seq));
+ if (unused) {
+ if (unlikely(need_gp))
+ cond_synchronize_rcu(gp_seq);
+ return;
+ }
+ }
}
EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs);
@@ -889,15 +1001,22 @@ EXPORT_SYMBOL_GPL(usb_kill_anchored_urbs);
* from the back of the queue. Newly added URBs will also be
* poisoned
*
+ * This function may sleep. Once it returns, the anchor may be freed under
+ * the same conditions as usb_kill_anchored_urbs().
+ *
* This routine should not be called by a driver after its disconnect
* method has returned.
*/
void usb_poison_anchored_urbs(struct usb_anchor *anchor)
{
+ unsigned long gp_seq = 0;
+ bool need_gp = false;
struct urb *victim;
- int surely_empty;
+ bool unused;
+
+ might_sleep();
- do {
+ for (;;) {
spin_lock_irq(&anchor->lock);
anchor->poisoned = 1;
while (!list_empty(&anchor->urb_list)) {
@@ -911,11 +1030,22 @@ void usb_poison_anchored_urbs(struct usb_anchor *anchor)
usb_put_urb(victim);
spin_lock_irq(&anchor->lock);
}
- surely_empty = __usb_anchor_check_wakeup(anchor);
+ unused = __usb_anchor_check_wakeup(anchor);
+ if (unused)
+ need_gp = __usb_anchor_prepare_rcu_gp(anchor, &gp_seq);
spin_unlock_irq(&anchor->lock);
- cpu_relax();
- } while (!surely_empty);
+
+ if (!unused)
+ wait_event(anchor->wait,
+ usb_anchor_teardown_progress(anchor, &unused,
+ &need_gp, &gp_seq));
+ if (unused) {
+ if (unlikely(need_gp))
+ cond_synchronize_rcu(gp_seq);
+ return;
+ }
+ }
}
EXPORT_SYMBOL_GPL(usb_poison_anchored_urbs);
@@ -945,8 +1075,8 @@ EXPORT_SYMBOL_GPL(usb_unpoison_anchored_urbs);
* @anchor: the anchor you want to suspend wakeups on
*
* Call this to stop the last urb being unanchored from waking up any
- * usb_wait_anchor_empty_timeout waiters. This is used in the hcd urb give-
- * back path to delay waking up until after the completion handler has run.
+ * usb_wait_anchor_empty_timeout waiters. The hcd urb give-back path uses
+ * usb_unanchor_urb_and_suspend_wakeups() instead.
*/
void usb_anchor_suspend_wakeups(struct usb_anchor *anchor)
{
@@ -975,21 +1105,41 @@ void usb_anchor_resume_wakeups(struct usb_anchor *anchor)
EXPORT_SYMBOL_GPL(usb_anchor_resume_wakeups);
/**
- * usb_wait_anchor_empty_timeout - wait for an anchor to be unused
+ * usb_wait_anchor_empty_timeout - wait for an anchor to become unused
* @anchor: the anchor you want to become unused
* @timeout: how long you are willing to wait in milliseconds
*
- * Call this is you want to be sure all an anchor's
- * URBs have finished
+ * Call this if you want to be sure all an anchor's URBs have finished.
+ * A non-zero return allows freeing the anchor if no new URBs are anchored
+ * and no other teardown call on it is in flight. If a concurrent unanchor
+ * raced on @anchor, a successful wait may sleep for one RCU grace period
+ * and overrun @timeout.
*
- * Return: Non-zero if the anchor became unused. Zero on timeout.
+ * Return: Non-zero (remaining jiffies, or 1 if none remain) if the
+ * anchor became unused. Zero on timeout.
*/
int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor,
unsigned int timeout)
{
- return wait_event_timeout(anchor->wait,
- usb_anchor_check_wakeup(anchor),
- msecs_to_jiffies(timeout));
+ unsigned long expire = msecs_to_jiffies(timeout);
+ unsigned long deadline = jiffies + expire;
+ unsigned long gp_seq = 0;
+ bool need_gp = false;
+ long ret;
+
+ ret = wait_event_timeout(anchor->wait,
+ usb_anchor_check_wakeup(anchor, &need_gp,
+ &gp_seq),
+ expire);
+ if (!ret)
+ return 0;
+
+ if (unlikely(need_gp)) {
+ cond_synchronize_rcu(gp_seq);
+ ret = max_t(long, (long)(deadline - jiffies), 1);
+ }
+
+ return min_t(long, ret, INT_MAX);
}
EXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout);
@@ -998,7 +1148,8 @@ EXPORT_SYMBOL_GPL(usb_wait_anchor_empty_timeout);
* @anchor: the anchor whose urb you want
*
* This will take the oldest urb from an anchor, unanchor and return it with
- * a reference held.
+ * a reference held. A %NULL return does not guarantee that in-flight
+ * givebacks have finished with @anchor.
*
* Return: The oldest urb from @anchor, or %NULL if @anchor has no
* urbs associated with it.
@@ -1013,7 +1164,7 @@ struct urb *usb_get_from_anchor(struct usb_anchor *anchor)
victim = list_entry(anchor->urb_list.next, struct urb,
anchor_list);
usb_get_urb(victim);
- __usb_unanchor_urb(victim, anchor);
+ __usb_unanchor_urb(victim, anchor, false);
} else {
victim = NULL;
}
@@ -1029,25 +1180,43 @@ EXPORT_SYMBOL_GPL(usb_get_from_anchor);
* @anchor: the anchor whose urbs you want to unanchor
*
* use this to get rid of all an anchor's urbs
+ *
+ * This function may sleep. Once it returns, the anchor may be freed under
+ * the same conditions as usb_kill_anchored_urbs().
*/
void usb_scuttle_anchored_urbs(struct usb_anchor *anchor)
{
+ unsigned long gp_seq = 0;
+ bool need_gp = false;
struct urb *victim;
- unsigned long flags;
- int surely_empty;
+ bool unused;
- do {
- spin_lock_irqsave(&anchor->lock, flags);
- while (!list_empty(&anchor->urb_list)) {
- victim = list_entry(anchor->urb_list.prev,
- struct urb, anchor_list);
- __usb_unanchor_urb(victim, anchor);
+ might_sleep();
+
+ for (;;) {
+ scoped_guard(spinlock_irqsave, &anchor->lock) {
+ while (!list_empty(&anchor->urb_list)) {
+ victim = list_entry(anchor->urb_list.prev,
+ struct urb, anchor_list);
+ __usb_unanchor_urb(victim, anchor, false);
+ }
+
+ unused = __usb_anchor_check_wakeup(anchor);
+ if (unused)
+ need_gp = __usb_anchor_prepare_rcu_gp(anchor,
+ &gp_seq);
}
- surely_empty = __usb_anchor_check_wakeup(anchor);
- spin_unlock_irqrestore(&anchor->lock, flags);
- cpu_relax();
- } while (!surely_empty);
+ if (!unused)
+ wait_event(anchor->wait,
+ usb_anchor_teardown_progress(anchor, &unused,
+ &need_gp, &gp_seq));
+ if (unused) {
+ if (unlikely(need_gp))
+ cond_synchronize_rcu(gp_seq);
+ return;
+ }
+ }
}
EXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs);
@@ -1056,6 +1225,8 @@ EXPORT_SYMBOL_GPL(usb_scuttle_anchored_urbs);
* usb_anchor_empty - is an anchor empty
* @anchor: the anchor you want to query
*
+ * This check is unsynchronized and gives no lifetime guarantee.
+ *
* Return: 1 if the anchor has no urbs associated with it.
*/
int usb_anchor_empty(struct usb_anchor *anchor)
diff --git a/include/linux/usb.h b/include/linux/usb.h
index 825f03b29067..8500e44d592a 100644
--- a/include/linux/usb.h
+++ b/include/linux/usb.h
@@ -1431,6 +1431,8 @@ struct usb_anchor {
spinlock_t lock;
atomic_t suspend_wakeups;
unsigned int poisoned:1;
+ unsigned int need_rcu_gp:1;
+ unsigned long rcu_gp_seq;
};
static inline void init_usb_anchor(struct usb_anchor *anchor)
@@ -1447,7 +1449,8 @@ typedef void (*usb_complete_t)(struct urb *);
* struct urb - USB Request Block
* @urb_list: For use by current owner of the URB.
* @anchor_list: membership in the list of an anchor
- * @anchor: to anchor URBs to a common mooring
+ * @anchor: to anchor URBs to a common mooring (RCU-protected for
+ * lockless unanchor)
* @ep: Points to the endpoint's data structure. Will eventually
* replace @pipe.
* @pipe: Holds endpoint number, direction, type, and more.
@@ -1642,12 +1645,13 @@ struct urb {
void *hcpriv; /* private data for host controller */
atomic_t use_count; /* concurrent submissions counter */
atomic_t reject; /* submissions will fail */
+ atomic_t anchor_readers; /* lockless urb->anchor readers */
/* public: documented fields in the urb that can be used by drivers */
struct list_head urb_list; /* list head for use by the urb's
* current owner */
struct list_head anchor_list; /* the URB may be anchored */
- struct usb_anchor *anchor;
+ struct usb_anchor __rcu *anchor;
struct usb_device *dev; /* (in) pointer to associated device */
struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */
unsigned int pipe; /* (in) pipe information */
--
2.32.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol
2026-12-03 0:48 [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol Hongyu Xie
` (2 preceding siblings ...)
2026-12-03 0:48 ` [PATCH v4 3/3] usb: core: Close residual anchor read-to-lock window with RCU Hongyu Xie
@ 2026-12-03 1:04 ` sashiko-bot
3 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-03 1:04 UTC (permalink / raw)
To: Hongyu Xie; +Cc: linux-usb
Sashiko has reviewed this 3-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179625891568.5518.7782041808979544421@kylinos.cn
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-03 1:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 0:48 [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 2/3] usb: core: Close anchor capture-to-increment race in URB giveback Hongyu Xie
2026-12-03 0:48 ` [PATCH v4 3/3] usb: core: Close residual anchor read-to-lock window with RCU Hongyu Xie
2026-12-03 1:04 ` [PATCH v4 0/3] usb: core: Fix three use-after-free windows in the USB anchor protocol sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox