From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796354691; b=qOwCJXznSno/NKk0NWPHFzTkTftIb5Dymyt1qzl6+L6QItI+BqsllLeE+nXDfE+tqm3B vwwMdEgHFOtR1ld2ojCsZapj7UpfTiM9lWNk43L3CdjJVhx319imukM3dV4j6bcoMNFKZ ALZ18W5PvdEyQTxHDErIyiz6Q1tS0hVidlaomxxgkP9yhSQb8L0CzXj9JlvoMX/h9ppwi HNw/uJDcGRaQdwTPWL0RZlb896scTk/h8t+k93CPe43O3ooZpPgIBl3msJNWXnUJTR9kC 7CK3JjvL7aZc9PATTGH/zlwHoGkYO78yamns+mddQsVdsck8qCqK0jicgfmtIicxtiw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796354691; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=bCOwgh1fcspKYRFyBFDxocliBuPD5shcns6B++TK2ao=; b=RbOYcb5vvRFMGU0oxFAqBpfaSEa5rFiwMUgEtI/qmcvM+GIK9UcnxQYuSdhApkMVEbV0 OTTwOpjvL0lc54wBz9ZQb/vY7kbnDMgMdIq/5oMw9sUbKoQ26iuTJEA1VkGFSI30r7xGg G35EZFrldMkoOtTnDkX1I77A4k1GXpALYIxyb0XGpa4wg2yK66zRgbZHZVy0yOPFXafJW 1mQzBoLsnIEh7M+lfkKfMAwmFJQVyuTPzd3F0a24bSiFcYo+bs2axCrDfRr9SAw129kdd hOEIQCm5ObTEP+7T1zkvhH/NnMMcTpsUtyoZI8O2GOJc1Hx3U9ybLR3kaW2Af/LitCw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1NxV7LIE; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6500B606A322; Fri, 4 Dec 2026 03:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=1NxV7LIE Received: by smtp.kernel.org (Postfix) with ESMTPSA id C652E1F00897; Fri, 4 Dec 2026 03:24:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796354691; bh=bCOwgh1fcspKYRFyBFDxocliBuPD5shcns6B++TK2ao=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=1NxV7LIEMA9r9TQ2I3JVZj1UqtX8Sgueh9c1nCnNcVPMdqeNzqqGlD6g+wED6UdPz jOWzCPbWOJFTLpXw6WvAxnPHkIwLA1a5jxGW4J41rtDjUJwJCzPFepNLVEZUnu//0r UO7pAEieroC88CfHDlSz1g2/Juf225TMFHo9qQJ0= Date: Thu, 3 Dec 2026 19:36:34 +0100 From: Greg Kroah-Hartman To: fabricioakio@gmail.com Cc: Neill Kapron , Krystian Kaniewski , Brian Ellis , Ai Chao , =?utf-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Prashanth K , 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 Message-ID: <2026120341-trifle-eardrum-2a1e@gregkh> References: <20261125-u-serial-console-dequeue-v3-0-b47f6e401dc1@gmail.com> <20261125-u-serial-console-dequeue-v3-2-b47f6e401dc1@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > > 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 > --- > 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 > #include > #include > +#include > #include > > #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