Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Fabricio Akio" <fabricioakio@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 2/2] usb: gadget: u_serial: Free the console request only once it is back
Date: Mon, 23 Nov 2026 02:02:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-228539@kernel.org> (raw)
In-Reply-To: <20261122-u-serial-console-dequeue-v2-2-820f355827e6@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] usb: gadget: u_serial: race queuing orphaned request in
  gs_console_work()
--

commit 2ecaecd16aac6588c9af880f3745192c34097106
Author: Fabricio Akio <fabricioakio@gmail.com>

usb: gadget: u_serial: Free the console request only once it is back

This commit tracks the console USB request state with cons->busy and a
kref on struct gs_console so that requests in flight during a disconnect
are orphaned and freed upon completion or queue failure rather than
immediately after usb_ep_dequeue().

> diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
> index 1f0110ea7f6a5..fd511327c0831 100644
> --- a/drivers/usb/gadget/function/u_serial.c
> +++ b/drivers/usb/gadget/function/u_serial.c

[ ... ]

> @@ -1049,21 +1087,24 @@ static void __gs_console_push(struct gs_console *cons)
>  	}
>  
>  	req->length = size;
> +	cons->busy = true;
>  
>  	spin_unlock_irq(&cons->lock);
> -	if (usb_ep_queue(ep, req, GFP_ATOMIC))
> -		req->length = 0;
> -	spin_lock_irq(&cons->lock);
> -}
>  
> -static void gs_console_work(struct work_struct *work)
> -{
> -	struct gs_console *cons = container_of(work, struct gs_console, work);
> +	if (!usb_ep_queue(ep, req, GFP_ATOMIC))
> +		return;
>  
>  	spin_lock_irq(&cons->lock);
> +	orphaned = req != cons->req;
> +	if (!orphaned)
> +		cons->busy = false;
> +	spin_unlock_irq(&cons->lock);
>  
> -	__gs_console_push(cons);
> +	if (orphaned)
> +		gs_console_free_orphaned(cons, ep, req);
> +	return;
>  
> +unlock:
>  	spin_unlock_irq(&cons->lock);
>  }

[Severity: Medium]
Can gs_console_work() queue a request that was already orphaned if a
disconnect and reconnect happen after cons->lock is dropped?

During set_alt (for example in gser_set_alt()), gserial_disconnect() is
followed immediately by gserial_connect(), which disables and re-enables
the same IN endpoint:

CPU0 (kworker)                    CPU1 (gser_set_alt)
gs_console_work()
  req = cons->req; /* req1 */
  cons->busy = true;
  spin_unlock_irq(&cons->lock);
                                  gserial_disconnect()
                                    gs_console_disconnect()
                                      cons->req = NULL;
                                      cons->busy = false;
                                      kref_get(&cons->kref);
                                    usb_ep_disable(ep)
                                  gserial_connect()
                                    usb_ep_enable(ep)
                                    gs_console_connect()
                                      cons->req = req2;
  usb_ep_queue(ep, req1, ...)
    /* succeeds on re-enabled ep */

Because ep is re-enabled before usb_ep_queue() runs, req1 is queued
alongside the newly allocated req2 instead of failing and being freed.
Both requests can then be queued concurrently on the IN endpoint, and if
gs_console_exit() runs while req1 is still pending in the controller, it
only checks and dequeues cons->req (req2), leaving the orphaned req1
queued and pinning cons->kref.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261122-u-serial-console-dequeue-v2-0-820f355827e6@gmail.com?part=2

  reply	other threads:[~2026-11-23  2:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  1:48 [PATCH v2 0/2] usb: gadget: u_serial: Fix the console request's lifetime Fabricio Akio via B4 Relay
2026-11-23  1:48 ` [PATCH v2 1/2] usb: gadget: u_serial: Publish the console only under port_lock Fabricio Akio via B4 Relay
2026-11-23  1:48 ` [PATCH v2 2/2] usb: gadget: u_serial: Free the console request only once it is back Fabricio Akio via B4 Relay
2026-11-23  2:02   ` sashiko-bot [this message]
2026-11-23  2:02 ` [PATCH v2 0/2] usb: gadget: u_serial: Fix the console request's lifetime 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-228539@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fabricioakio@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox