Skip to content

usb: gadget: u_serial: flush_buffer, and keep the port for its tty

Mehmet Fide linux-usb lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest2 patches 1 replyIn review

Changes in v2 from the cover letter

- new patch 1, the port lifetime fix
- patch 2 unchanged

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2usb: gadget: u_serial: keep the port until its tty is releaseddrivers/usb/gadget/function/u_serial.c+43 -21 reply
2/2usb: gadget: u_serial: implement flush_bufferdrivers/usb/gadget/function/u_serial.c+14 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/2] usb: gadget: u_serial: flush_buffer, and keep the port for its tty

From: Mehmet Fide <[email protected]>

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/[email protected]/

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

Signature · 1 line
2.55.0