Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hongyu Xie" <xiehongyu1@kylinos.cn>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH RESEND v2 3/3] usb: core: Close residual anchor read-to-lock window with RCU
Date: Fri, 27 Nov 2026 09:17:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-236958@kernel.org> (raw)
In-Reply-To: <179577008559.23588.16952362934102548601@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: core: unconditional synchronize_rcu() in anchor wait and
  drain helpers
--

commit 196ab1faf05f61727494005dc79df8a5473ad6cb
Author: Hongyu Xie <xiehongyu1@kylinos.cn>

usb: core: Close residual anchor read-to-lock window with RCU

This commit protects lockless urb->anchor reads with RCU and adds a
synchronize_rcu() grace-period wait to the return paths of
usb_wait_anchor_empty_timeout(), usb_kill_anchored_urbs(),
usb_poison_anchored_urbs(), and usb_scuttle_anchored_urbs().

> diff --git a/drivers/usb/core/urb.c b/drivers/usb/core/urb.c
> index 174dc42c95c14..d7528bd45d11e 100644
> --- a/drivers/usb/core/urb.c
> +++ b/drivers/usb/core/urb.c

[ ... ]

> @@ -903,11 +1000,47 @@ 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);
> +
> +		if (__usb_anchor_check_wakeup(anchor)) {
> +			spin_unlock_irq(&anchor->lock);
> +			/*
> +			 * One grace period before declaring the anchor
> +			 * unused and returning: a thread that has read
> +			 * urb->anchor but has not yet acquired anchor->lock
> +			 * must not touch the anchor after the caller frees
> +			 * it.  Nothing in this path may touch the anchor
> +			 * after this point.
> +			 */
> +			synchronize_rcu();
> +			return;
> +		}

[ ... ]

> @@ -1016,29 +1181,59 @@ void usb_anchor_resume_wakeups(struct usb_anchor *anchor)
>  int usb_wait_anchor_empty_timeout(struct usb_anchor *anchor,
>  				  unsigned int timeout)
>  {
> +	unsigned long deadline = jiffies + msecs_to_jiffies(timeout);
> +	long ret;

[ ... ]

> -	return wait_event_timeout(anchor->wait,
> -				  usb_anchor_check_wakeup(anchor),
> -				  msecs_to_jiffies(timeout));
> +	ret = wait_event_timeout(anchor->wait,
> +				 usb_anchor_check_wakeup(anchor),
> +				 max_t(long, (long)(deadline - jiffies), 0));
> +	if (!ret)
> +		return 0;
> +
> +	/* Same lifetime exit as the teardown drains. */
> +	synchronize_rcu();
> +	return 1;
>  }

[Severity: Medium]
Does waiting for an unconditional synchronize_rcu() on every successful
usb_wait_anchor_empty_timeout() and anchor drain call regress runtime I/O
paths that wait on persistent anchors without freeing them?

For example, zd1211rw calls usb_wait_anchor_empty_timeout() in
zd_usb_iowrite16v_async_end() at the end of every register write batch:

drivers/net/wireless/zydas/zd1211rw/zd_usb.c:zd_usb_iowrite16v_async_end() {
    ...
	if (timeout)
		timeout = usb_wait_anchor_empty_timeout(&usb->submitted_cmds,
							timeout);
    ...
}

A single Wi-Fi channel switch in zd_chip_set_channel() issues 15 to 20
sequential register write batches under chip->mutex, turning
sub-millisecond USB transfers into 15 to 20 sequential RCU grace-period
stalls per channel hop during scanning.  Drivers that stop multiple
persistent anchors in a loop or sequence (such as hiface_pcm_stream_stop()
waiting on 8 anchors on every ALSA stream stop/prepare, or
usbtmc_generic_read() and btusb_stop_traffic() draining already-empty
anchors) also pay sequential grace periods even when no URBs were
anchored.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179577006613.23588.1120308700542393912@kylinos.cn?part=3

  reply	other threads:[~2026-11-27  9:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  9:01 [PATCH RESEND v2 0/3] usb: core: fix three use-after-free windows in the USB anchor protocol Hongyu Xie
2026-11-27  9:01 ` [PATCH RESEND v2 1/3] usb: core: Fix use-after-free in anchor wakeup protocol Hongyu Xie
2026-11-27  9:01 ` [PATCH RESEND v2 2/3] usb: core: Close anchor capture-to-increment race in URB giveback Hongyu Xie
2026-11-27  9:01 ` [PATCH RESEND v2 3/3] usb: core: Close residual anchor read-to-lock window with RCU Hongyu Xie
2026-11-27  9:17   ` sashiko-bot [this message]
2026-11-27  9:17 ` [PATCH RESEND v2 0/3] usb: core: fix three use-after-free windows in the USB anchor protocol sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-236958@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xiehongyu1@kylinos.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox