[PATCH v2 0/2] usb: gadget: u_serial: flush_buffer, and keep the port for its tty
From: Mehmet Fide
Date: Mon Oct 19 2026 - 05:06:24 EST
From: Mehmet Fide <mehmet.fide@xxxxxxxxxxxxxxxxxx>
On a gadget whose host side does not read the port, the last close of
/dev/ttyGS* waits GS_CLOSE_TIMEOUT (15 s) for the write buffer to drain,
and tcflush(TCOFLUSH) cannot avoid that because u_serial has no
flush_buffer operation. v1 added it.
Sashiko pointed out that the hangup gserial_disconnect() schedules can
then call flush_buffer after gserial_free_line() has freed the port.
That window is older than the new callback: the tty core already reads
the freed port there, KASAN reports it in check_tty_count() with a
delay added to gs_close(). Patch 1 makes the port live as long as its
tty, with a tty_port reference taken in install and dropped in cleanup
as cdc-acm does, and carries Fixes and Cc stable. Patch 2 is v1.
Changes in v2:
- new patch 1, the port lifetime fix
- patch 2 unchanged
v1: https://lore.kernel.org/linux-usb/20261015162732.3170938-1-mehmet.fide@xxxxxxxxx/
Tested on a Verdin AM62 with KASAN and lockdep, 6.12.113 plus both
patches: with a 3 s delay in gs_close() the hangup after
gserial_free_line() no longer reports a use-after-free and the port is
freed after the tty; four function teardowns, two with the port open,
free four ports without leaks or warnings; close after tcflush returns
at once with the host not reading and the data written after the flush
arrives; 47850 flushes against a concurrent writer; NCM torn down
under iperf traffic. The same two patches run in our i.MX8MP and VF50
product images on CI rigs, gadget replug included. On usb-next both
patches build with W=1 and sparse without new warnings, each on its own.
Mehmet Fide (2):
usb: gadget: u_serial: keep the port until its tty is released
usb: gadget: u_serial: implement flush_buffer
drivers/usb/gadget/function/u_serial.c | 59 +++++++++++++++++++++++++-
1 file changed, 57 insertions(+), 2 deletions(-)
base-commit: 04ac7b445880b0878c83e6e89d961455cbea0427
--
2.55.0