From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: fabricioakio@gmail.com
Cc: "Neill Kapron" <nkapron@google.com>,
"Krystian Kaniewski" <krystianmkaniewski@gmail.com>,
"Brian Ellis" <brianellis@gotenna.com>,
"Ai Chao" <aichao@kylinos.cn>,
"Michał Mirosław" <mirq-linux@rere.qmqm.pl>,
"Prashanth K" <prashanth.k@oss.qualcomm.com>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] usb: gadget: u_serial: Free the console request only once it is back
Date: Thu, 3 Dec 2026 19:36:34 +0100 [thread overview]
Message-ID: <2026120341-trifle-eardrum-2a1e@gregkh> (raw)
In-Reply-To: <20261125-u-serial-console-dequeue-v3-2-b47f6e401dc1@gmail.com>
On Wed, Nov 25, 2026 at 12:05:27PM -0300, Fabricio Akio via B4 Relay wrote:
> From: Fabricio Akio <fabricioakio@gmail.com>
>
> gs_console_disconnect() frees the console request right after
> usb_ep_dequeue(), which is asynchronous: the controller, dwc3 here,
> gives a queued request back from usb_ep_disable() in
> gserial_disconnect(), after it was freed. __gs_console_push() also
> queues the request with cons->lock dropped, so a disconnect in that
> window frees a request still being queued. An idle request is dequeued
> too, which dwc3 reports as "request ... was not queued" at each
> disconnect.
>
> Track whether the request is out in a console flag, cons->busy, under
> cons->lock, instead of in req->length, which the driver may not examine
> while the request is queued. Queue the request under a second lock,
> cons->tx_lock, which the disconnect also takes, so that a disconnect
> waits for a queue in progress; cons->lock stays dropped across
> usb_ep_queue(), since the controller may print from inside it into
> gs_console_write(). The controller may also complete a tty write from
> inside it, and gs_write_complete() takes port_lock, so
> gserial_disconnect() disconnects the console after dropping port_lock,
> holding a reference on it. On disconnect, the console gives the request
> up and frees it only if idle. A busy request, now orphaned, is freed by
> its completion, and holds a reference on the console until then, so
> that the console outlives it after gs_console_exit().
> __gs_console_push() is folded into gs_console_work(), its only caller,
> which holds tx_lock across the push.
>
> The disconnect no longer dequeues: in gserial_disconnect(),
> usb_ep_disable() follows and gives the request back. gs_console_exit()
> drops the unsent output and, with no disconnect under way to give it
> back, dequeues a request still queued itself.
>
> Fixes: fe1ea63ad806 ("usb: gadget: u_serial: reimplement console support")
> Fixes: e5990469943c ("usb: gadget: u_serial: Avoid spinlock recursion in __gs_console_push")
> Cc: stable@vger.kernel.org
> Signed-off-by: Fabricio Akio <fabricioakio@gmail.com>
> ---
> drivers/usb/gadget/function/u_serial.c | 196 ++++++++++++++++++++++++++-------
> 1 file changed, 154 insertions(+), 42 deletions(-)
>
> diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
> index 1f0110ea7f6a..e4698209bb57 100644
> --- a/drivers/usb/gadget/function/u_serial.c
> +++ b/drivers/usb/gadget/function/u_serial.c
> @@ -29,6 +29,7 @@
> #include <linux/kthread.h>
> #include <linux/workqueue.h>
> #include <linux/kfifo.h>
> +#include <linux/kref.h>
> #include <linux/serial.h>
>
> #include "u_serial.h"
> @@ -109,10 +110,20 @@ static DEFINE_SPINLOCK(serial_port_lock);
> struct gs_console {
> struct console console;
> struct work_struct work;
> + /*
> + * Held across usb_ep_queue(), so that gs_console_disconnect() can wait
> + * for a queue in progress. lock is not, since the controller may print
> + * from inside usb_ep_queue() into gs_console_write(). Never taken
> + * under port_lock: the controller may also complete a tty write from
> + * inside usb_ep_queue(), and gs_write_complete() takes port_lock.
> + */
What is held? This lock? Please use normal kernel doc formatting to be
explicit.
> + spinlock_t tx_lock;
> spinlock_t lock;
> struct usb_request *req;
> + bool busy;
Why place a bool here in the middle of these? You now have a hole in
this structure, right?
And you have a lock, a state, and a kref. Are you sure you need them
all? Why can't the reference count work on its own?
> struct kfifo buf;
> size_t missed;
> + struct kref kref;
> };
>
> /*
> @@ -998,46 +1009,73 @@ static struct tty_driver *gs_tty_driver;
>
> #ifdef CONFIG_U_SERIAL_CONSOLE
>
> +static void gs_console_release(struct kref *kref)
> +{
> + struct gs_console *cons = container_of(kref, struct gs_console, kref);
> +
> + kfifo_free(&cons->buf);
> + kfree(cons);
> +}
> +
> static void gs_console_complete_out(struct usb_ep *ep, struct usb_request *req)
> {
> struct gs_console *cons = req->context;
> + bool push = true;
> + bool orphaned;
>
> switch (req->status) {
> default:
> pr_warn("%s: unexpected %s status %d\n",
> __func__, ep->name, req->status);
> - fallthrough;
> + break;
> case 0:
> /* normal completion */
> - spin_lock(&cons->lock);
> - req->length = 0;
> - schedule_work(&cons->work);
> - spin_unlock(&cons->lock);
> break;
> case -ECONNRESET:
> case -ESHUTDOWN:
> /* disconnect */
> pr_vdebug("%s: %s shutdown\n", __func__, ep->name);
> + push = false;
> break;
> }
> +
> + spin_lock(&cons->lock);
guard() please. Everywhere. llms don't know about them, but you do :)
thanks.
greg k-h
prev parent reply other threads:[~2026-12-04 3:24 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 15:05 [PATCH v3 0/2] usb: gadget: u_serial: Fix the console request's lifetime Fabricio Akio via B4 Relay
2026-11-25 15:05 ` [PATCH v3 1/2] usb: gadget: u_serial: Publish the console only under port_lock Fabricio Akio via B4 Relay
2026-11-25 15:05 ` [PATCH v3 2/2] usb: gadget: u_serial: Free the console request only once it is back Fabricio Akio via B4 Relay
2026-12-03 18:36 ` Greg Kroah-Hartman [this message]
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=2026120341-trifle-eardrum-2a1e@gregkh \
--to=gregkh@linuxfoundation.org \
--cc=aichao@kylinos.cn \
--cc=brianellis@gotenna.com \
--cc=fabricioakio@gmail.com \
--cc=krystianmkaniewski@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=mirq-linux@rere.qmqm.pl \
--cc=nkapron@google.com \
--cc=prashanth.k@oss.qualcomm.com \
/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
all inboxes | Powered by JetHome®