All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jing Lin" <kayano050408@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v9 4/6] usb: gadget: gadgetfs: NAK new setups until control-OUT data is read
Date: Thu, 26 Nov 2026 07:56:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-234979@kernel.org> (raw)
In-Reply-To: <20261126-gadgetfs-ep0-v9-4-81b464c6e641@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] usb: gadget: gadgetfs: ep0_complete() wedges ep0 on aborted
  control-OUT
- [High] usb: gadget: gadgetfs: missing state recheck after
  queue_ep0_req()
--

commit 9c4e5fec69f29dc68b826d0e75ee968e904766c3
Author: Jing Lin <kayano050408@gmail.com>

usb: gadget: gadgetfs: NAK new setups until control-OUT data is read

This commit defers incoming SETUP packets in dev->pending_setup while a
completed control-OUT data stage is waiting to be copied by ep0_read(),
and replays the pending setup once userspace finishes reading the payload.

> diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
> index 4915906d782c3..12ae17f45898c 100644
> --- a/drivers/usb/gadget/legacy/inode.c
> +++ b/drivers/usb/gadget/legacy/inode.c
[ ... ]
> @@ -1021,23 +1025,25 @@ static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
>  {
>  	struct dev_data		*dev = req->context;
>  	unsigned long		flags;
> -	int			free = 1;
>  
>  	/* for control OUT, data must still get to userspace */
>  	spin_lock_irqsave(&dev->lock, flags);
> -	if (!dev->setup_in) {
> +	if (dev->state == STATE_DEV_CONNECTED ||
> +	    dev->state == STATE_DEV_SETUP) {
>  		dev->setup_out_error = (req->status != 0);
> -		if (!dev->setup_out_error)
> -			free = 0;
> +		if (dev->setup_out_error) {
> +			dev->setup_out_len = 0;
> +			clean_req(ep, req);
> +		} else {
> +			dev->setup_out_len = min_t(unsigned int, req->actual,
> +						   sizeof(dev->rbuf));
> +			req->complete = dev_req_complete;
> +		}
>  		dev->setup_out_ready = 1;
>  		ep0_readable(dev);

[Severity: High]
Can ep0_complete() set dev->setup_out_ready = 1 for a control-OUT transfer
that was already superseded, wedging ep0?

CPU0                              CPU1
gadgetfs_setup() [setup 1, OUT]
  gadgetfs_do_setup()
    queue_ep0_req()
    next_event(GADGETFS_SETUP)
                                  gadgetfs_setup() [setup 2]
                                    gadgetfs_do_setup()
                                      ep0_discard_event(GADGETFS_SETUP)
                                      if (dev->ep0_req_queued)
                                        return -EBUSY;
ep0_complete() [setup 1]
  dev->setup_out_ready = 1;  <-- wedges ep0

Before checking dev->ep0_req_queued, gadgetfs_do_setup() discards the
queued GADGETFS_SETUP event and resets STATE_DEV_SETUP to
STATE_DEV_CONNECTED:

drivers/usb/gadget/legacy/inode.c:gadgetfs_do_setup() {
    ...
	ep0_discard_event(dev, GADGETFS_SETUP);

	if (dev->state == STATE_DEV_SETUP) {
		dev->setup_abort = 1;
		dev->state = STATE_DEV_CONNECTED;
		ep0_readable(dev);
	}
    ...
	if (dev->ep0_req_queued)
		return -EBUSY;
    ...
}

When setup 1's data stage then completes with req->status == 0,
ep0_complete() sees dev->state == STATE_DEV_CONNECTED and sets
dev->setup_out_ready = 1.  Because no GADGETFS_SETUP event remains queued,
ep0_read() can never enter STATE_DEV_SETUP to consume the payload and clear
dev->setup_out_ready, so every subsequent gadgetfs_setup() call defers new
SETUP packets into dev->pending_setup forever.

The same wedge happens if a control-OUT transfer supersedes an earlier
STATE_DEV_SETUP transfer (leaving dev->setup_abort = 1) and ep0_read() at
scan: consumes the new GADGETFS_SETUP event: the next ep0_read() hits
ep0_check_abort(), which resets dev->state to STATE_DEV_CONNECTED and
returns -EIDRM without clearing dev->setup_out_ready or replaying
dev->pending_setup.

[ ... ]

> @@ -1725,6 +1789,7 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
>  			dev->setup_wLength = w_length;
>  			dev->setup_out_ready = 0;
>  			dev->setup_out_error = 0;
> +			dev->setup_out_len = 0;
>  
>  			/* read DATA stage for OUT right away */
>  			if (unlikely(!dev->setup_in && w_length)) {
> @@ -1733,7 +1798,7 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
>  				if (value < 0)
>  					break;
>  
> -				value = queue_ep0_req(dev, false);
> +				value = queue_ep0_req(dev, process_ctx);
>  				if (value < 0)
>  					break;
>  
> @@ -1745,7 +1810,6 @@ gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
>  			event = next_event (dev, GADGETFS_SETUP);
>  			event->u.setup = *ctrl;
>  			ep0_readable (dev);

[Severity: High]
Does gadgetfs_do_setup() need to recheck dev->state and
dev->setup_generation after queue_ep0_req() drops and re-acquires
dev->lock?

CPU0 (ep0_read)                   CPU1
ep0_process_pending_setup()
  gadgetfs_do_setup(..., true)
    queue_ep0_req(dev, true)
      spin_unlock_irq(&dev->lock)
      usb_ep_queue()
                                  gadgetfs_disconnect()
                                    next_event(GADGETFS_DISCONNECT)
                                    dev->state = STATE_DEV_UNCONNECTED
                                  ep0_complete()
                                    clean_req(ep, req)
      spin_lock_irq(&dev->lock)
    next_event(GADGETFS_SETUP)  <-- stale event after disconnect

When ep0_process_pending_setup() calls gadgetfs_do_setup() with
process_ctx = true, queue_ep0_req() drops dev->lock and enables interrupts
around usb_ep_queue().  If gadgetfs_disconnect() and ep0_complete() run
during that window, ep0_complete() sees STATE_DEV_UNCONNECTED and does not
set dev->setup_out_ready.  gadgetfs_do_setup() then queues a stale
GADGETFS_SETUP event after GADGETFS_DISCONNECT; when ep0_read() consumes
that event, it enters STATE_DEV_SETUP and the next ep0_read() blocks
indefinitely waiting for dev->setup_out_ready.

Similarly, if queue_ep0_req() fails after a newer SETUP or disconnect has
already arrived while dev->lock was dropped, ep0_process_pending_setup()
will call usb_ep_set_halt(dev->gadget->ep0) on the new state.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261126-gadgetfs-ep0-v9-0-81b464c6e641@gmail.com?part=4

  reply	other threads:[~2026-11-26  7:56 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  6:37 [PATCH v9 0/6] usb: gadget: gadgetfs: fix ep0 request lifetime and control races Jing Lin
2026-11-26  6:37 ` Jing Lin via B4 Relay
2026-11-26  6:37 ` [PATCH v9 1/6] usb: gadget: gadgetfs: fix ep0 request lifetime on unbind Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  6:37 ` [PATCH v9 2/6] usb: gadget: gadgetfs: isolate ep0_write() buffer and reduce setup race Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  6:37 ` [PATCH v9 3/6] usb: gadget: gadgetfs: fix ep0 setup_abort tracking and reporting Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  6:37 ` [PATCH v9 4/6] usb: gadget: gadgetfs: NAK new setups until control-OUT data is read Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  7:56   ` sashiko-bot [this message]
2026-11-26  6:37 ` [PATCH v9 5/6] usb: gadget: gadgetfs: prevent ep0 event counter underflow Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  6:37 ` [PATCH v9 6/6] usb: gadget: gadgetfs: avoid writing through the const setup pointer Jing Lin
2026-11-26  6:37   ` Jing Lin via B4 Relay
2026-11-26  8:02 ` [PATCH v9 0/6] usb: gadget: gadgetfs: fix ep0 request lifetime and control races 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-234979@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kayano050408@gmail.com \
    --cc=linux-usb@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.