usb: gadget: u_serial: Fix the console request's lifetime
Subsystems (2)
v2 cover usb: gadget: u_serial: Fix the console request's lifetime
v2 is a rework rather than a revision of v1. The console does not follow the gadget API's rule that a queued request belongs to the controller until its completion runs: it reads req->length while the request is queued, frees the request right after an asynchronous usb_ep_dequeue(), and can free it while __gs_console_push() is still queueing it. Patch 2 frees the request only once it is back, which also ends the "request ... was not queued" message that v1 was about. It relies on patch 1: the configfs console attribute sets and clears port->console outside port_lock, which connect and disconnect use to find the console.
---
Changes in v2:
- Rework, per Sashiko's review of v1 [1]: instead of skipping the
dequeue of an idle request, free the request only once it is back.
v1 kept the use-after-free, and could still dequeue a request after a
failed queue.
- New patch 1, for a race the rework depends on.
- Link to v1: https://patch.msgid.link/[email protected]
Tested on a Motorola Moto X4 (XT1900-6, SDM630, dwc3) with linux-next's u_serial.c plus this series, built in the sdm660-mainline 7.2.3 tree with KASAN and the console on ttyGS0; linux-next itself was only build-tested. The control was the same file without the series.
- A host bus reset during console output: a slab-use-after-free in dwc3_gadget_giveback() at each of 20 resets without the series, none with it.
- The console turned off through configfs with its request queued and the host not reading: the request is cancelled and freed by its completion, with no KASAN report, over 5 rounds. Without the series, KASAN caught dwc3 giving it back after it was freed, in each round.
- The configfs write racing a host reset, with a delay added inside gs_console_init() and gs_console_exit(): one connect per console, and no request left on a released console. Without patch 1, a second connect or a request on the released console in 6 of 10 rounds.
- With the console registered but not selected by console=: no "was not queued" over three suspends.
[1] https://sashiko.dev/#/patchset/[email protected]
---
Fabricio Akio (2):
usb: gadget: u_serial: Publish the console only under port_lock
usb: gadget: u_serial: Free the console request only once it is back
drivers/usb/gadget/function/u_serial.c | 134 +++++++++++++++++++++++++-------- 1 file changed, 103 insertions(+), 31 deletions(-)
---
base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726
change-id: 20260928-u-serial-console-dequeue-f9790a8e87c6
Best regards,
Signature · 1 line
Fabricio Akio <[email protected]>
PATCH v2 1/2 usb: gadget: u_serial: Publish the console only under port_lock
gserial_connect() and gserial_disconnect() find the console through port->console under port_lock, but gs_console_init() sets it before taking the lock, and gs_console_exit() clears it after dropping it. A host connect while the console is being turned on through configfs can then connect it twice, leaking the first request. While it is being turned off, a connect can attach a request to the console being freed and schedule its work after cancel_work_sync().
Set and clear port->console under port_lock, together with connecting and disconnecting the console.
--- drivers/usb/gadget/function/u_serial.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_serial.c +2 -2
@@ -1173,10 +1173,10 @@ static int gs_console_init(struct gs_port *port)return err; } - port->console = cons;register_console (&cons->console);spin_lock_irq (&port->port_lock); + port->console = cons;if (port->port_usb)gs_console_connect (port);spin_unlock_irq (&port->port_lock);
@@ -1196,12 +1196,12 @@ static void gs_console_exit(struct gs_port *port)spin_lock_irq (&port->port_lock);if (cons->req)gs_console_disconnect (port); + port->console =NULL ;spin_unlock_irq (&port->port_lock);cancel_work_sync (&cons->work);kfifo_free (&cons->buf);kfree (cons); - port->console =NULL ; }ssize_t gserial_set_console (unsigned char port_num,const char *page,size_t count)
Signature · 1 line
2.56.1
PATCH v2 2/2 usb: gadget: u_serial: Free the console request only once it is back
In reply to Fabricio Akiogs_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. On disconnect, the console gives the request up and frees it only if idle. A busy request, now orphaned, is freed when it comes back: by its completion, or by gs_console_work() when usb_ep_queue() fails. Until then it holds a reference on the console, so that the console outlives it after gs_console_exit(). __gs_console_push() is folded into gs_console_work(), its only caller, so that a failed queue takes cons->lock only for as long as it needs.
The disconnect no longer dequeues: in gserial_disconnect(), usb_ep_disable() follows and gives the request back. gs_console_exit(), where nothing follows, drops the unsent output and dequeues a request still queued itself.
--- drivers/usb/gadget/function/u_serial.c | 130 +++++++++++++++++++++++++-------- 1 file changed, 101 insertions(+), 29 deletions(-)
drivers/usb/gadget/function/u_serial.c +101 -29
@@ -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"
@@ -111,8 +112,10 @@ struct gs_console {struct work_struct work;spinlock_t lock;struct usb_request *req; +bool busy;struct kfifo buf;size_t missed; +struct kref kref; };/*
@@ -998,46 +1001,81 @@ 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); +} + +/* +* Free a request that gs_console_disconnect() orphaned while it was busy, +* and drop the reference it took for it. Called without cons->lock. +*/ +static void gs_console_free_orphaned (struct gs_console *cons, +struct usb_ep *ep,struct usb_request *req) +{ +gs_free_req (ep, req); +kref_put (&cons->kref, gs_console_release); +} +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); + orphaned = req != cons->req; +if (!orphaned) { + cons->busy = false; +if (push) +schedule_work (&cons->work); + } +spin_unlock (&cons->lock); + +if (orphaned) +gs_console_free_orphaned (cons, ep, req); } -static void __gs_console_push (struct gs_console *cons) +static void gs_console_work (struct work_struct *work) { -struct usb_request *req = cons->req; + struct gs_console *cons = container_of(work, struct gs_console, work); +struct usb_request *req;struct usb_ep *ep; +bool orphaned;size_t size; -if (!req) -return ;/* disconnected */ +spin_lock_irq (&cons->lock); -if (req->length) -return ;/* busy */ + req = cons->req; +if (!req) + goto unlock;/* disconnected */ + +if (cons->busy) + goto unlock;/* busy */ ep = cons->console.data; size =kfifo_out (&cons->buf, req->buf, ep->maxpacket);if (!size) -return ; + goto unlock;if (cons->missed && ep->maxpacket >=64 ) {char buf[64 ];
@@ -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); }
@@ -1080,7 +1121,7 @@ static void gs_console_write(struct console *co,if (n < count) cons->missed += count - n; -if (cons->req && !cons->req->length) +if (cons->req && !cons->busy)schedule_work (&cons->work);spin_unlock_irqrestore (&cons->lock, flags);
@@ -1107,7 +1148,6 @@ static int gs_console_connect(struct gs_port *port)return -ENOMEM ; req->complete = gs_console_complete_out; req->context = cons; - req->length =0 ;spin_lock (&cons->lock); cons->req = req;
@@ -1126,6 +1166,7 @@ static void gs_console_disconnect(struct gs_port *port)struct gs_console *cons = port->console;struct usb_request *req;struct usb_ep *ep; +bool busy;if (!cons)return ;
@@ -1134,15 +1175,22 @@ static void gs_console_disconnect(struct gs_port *port) req = cons->req; ep = cons->console.data; + busy = cons->busy; cons->req =NULL ; + cons->busy = false; +/* +* A busy request is queued, or being queued by gs_console_work(). The +* console gives it up here, and the orphaned request is freed when it +* comes back: by its completion, or by gs_console_work() if +* usb_ep_queue() fails. It holds a reference on the console until then. +*/ +if (busy) +kref_get (&cons->kref);spin_unlock (&cons->lock); -if (!req) -return ; - -usb_ep_dequeue (ep, req); -gs_free_req (ep, req); +if (req && !busy) +gs_free_req (ep, req); }static int gs_console_init (struct gs_port *port)
@@ -1165,6 +1213,7 @@ static int gs_console_init(struct gs_port *port)INIT_WORK (&cons->work, gs_console_work);spin_lock_init (&cons->lock); +kref_init (&cons->kref); err =kfifo_alloc (&cons->buf,GS_CONSOLE_BUF_SIZE ,GFP_KERNEL );if (err) {
@@ -1187,21 +1236,44 @@ static int gs_console_init(struct gs_port *port)static void gs_console_exit (struct gs_port *port) {struct gs_console *cons = port->console; +struct usb_request *req; +struct usb_ep *ep;if (!cons)return ;unregister_console (&cons->console); +/* +* Drop the unsent output and wait for a push in progress, so that the +* request is busy below only if it is queued. +*/ +spin_lock_irq (&cons->lock); +kfifo_reset (&cons->buf); +spin_unlock_irq (&cons->lock); +cancel_work_sync (&cons->work); +spin_lock_irq (&port->port_lock); + +/* +* Nothing gives a queued request back here, so cancel it. Holding +* port_lock keeps a disconnect from orphaning it, so it cannot be +* freed under usb_ep_dequeue(). +*/ +spin_lock (&cons->lock); + req = cons->busy ? cons->req :NULL ; + ep = cons->console.data; +spin_unlock (&cons->lock); +if (req) +usb_ep_dequeue (ep, req); +if (cons->req)gs_console_disconnect (port); port->console =NULL ;spin_unlock_irq (&port->port_lock);cancel_work_sync (&cons->work); -kfifo_free (&cons->buf); -kfree (cons); +kref_put (&cons->kref, gs_console_release); }ssize_t gserial_set_console (unsigned char port_num,const char *page,size_t count)
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v2 2/2] usb: gadget: u_serial: Free the console request only once it is back
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() --
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 <[email protected]>
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().
Quoted 4 lines
drivers/usb/gadget/function/u_serial.c +0
-0
[ ... ]
Quoted 22 lines
drivers/usb/gadget/function/u_serial.c +6
-7@@ -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;
drivers/usb/gadget/function/u_serial.c static void __gs_console_push(struct gs_console *cons) · line 1109
Quoted 10 lines
drivers/usb/gadget/function/u_serial.c +5
-1+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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewSashiko has finished reviewing this 2-patch series:
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("usb: gadget: u_serial: Free the console request only..."): 1 potential issue (1 Medium)
A detailed review has been sent as a separate reply to patch 2.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Re: [PATCH v2 1/2] usb: gadget: u_serial: Publish the console only under port_lock
In reply to Fabricio AkioQuoted 25 lines · On Sun, Nov 22, 2026 at 10:48:34PM -0300, Fabricio Akio via B4 Relay wrote:
From: Fabricio Akio <[email protected]>
gserial_connect()andgserial_disconnect()find the console throughport->consoleunderport_lock, butgs_console_init()sets it before taking the lock, andgs_console_exit()clears it after dropping it. A host connect while the console is being turned on through configfs can then connect it twice, leaking the first request. While it is being turned off, a connect can attach a request to the console being freed and schedule its work aftercancel_work_sync().Set and clear
port->consoleunderport_lock, together with connecting and disconnecting the console.Fixes d7cb8fb7aa7d ("usb: gadget: u_serial: allow more console gadget ports")Signed-off-by Fabricio Akio <[email protected]>--- drivers/usb/gadget/function/u_serial.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/usb/gadget/function/u_serial.c +0
-0
drivers/usb/gadget/function/u_serial.c static int gs_console_init(struct gs_port *port) · line 1176
Quoted 5 lines
drivers/usb/gadget/function/u_serial.c +0
-1@@ -1173,10 +1173,10 @@ static int gs_console_init(struct gs_port *port)return err; } - port->console = cons;register_console (&cons->console);
Note that there is if (port->console) return just at the start of this function. If there is indeed a race here, that should be taken into account.
Signature · 1 line
Michał Mirosław
Re: [PATCH v2 1/2] usb: gadget: u_serial: Publish the console only under port_lock
Quoted 3 lines · Michał Mirosław
Note that there is
if (port->console) returnjust at the start of this function. If there is indeed a race here, that should be taken into account.
gserial_set_console() and gserial_free_line() hold ports[port_num].lock across gs_console_init() and gs_console_exit(). The initial call from gserial_alloc_line() during configfs function creation runs before the function's console attribute is exposed. That makes the early check safe.
The race is later in gs_console_init(). After it sets port->console and before it takes port_lock, gserial_connect() can take port_lock, set port->port_usb and attach a request to the console. Init then takes port_lock, sees port->port_usb set and attaches a second request, overwriting the first. Moving the assignment under port_lock closes that window.