Linux USB
 help / color / mirror / Atom feed
* [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds
@ 2026-12-06  0:18 Mateusz Sikora
  2026-12-06  0:18 ` [PATCH 1/2] USB: serial: allow drivers to use more read urbs Mateusz Sikora
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Mateusz Sikora @ 2026-12-06  0:18 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, Mateusz Sikora

The CH340 has 32-byte bulk endpoints and with the two read urbs used by
the generic implementation it loses characters at 921600 baud as soon as
completion of the read urbs is delayed by more than about 0.7 ms. Two
endpoint-sized urbs, 64 bytes, are far less than what other drivers keep
queued: 1 KiB or more for cdc-acm, 1 KiB for ftdi_sio and 512 bytes for
cp210x and pl2303.

Willy's earlier fix, which used 512-byte buffers, had to be reverted as
these devices do not send zero-length packets [1]. This series instead
lets drivers use more read urbs and makes ch341 use 32 endpoint-sized
ones, 1 KiB in all, each still completing with every packet. WCH's own
out-of-tree driver [2] also reads in endpoint-sized urbs, four of them.

I found this on a Raspberry Pi 4 (6.18.50) recording ESP32 boards at
921600 baud, and reproduced it on an x86 laptop (Ubuntu's 7.0 kernel),
both with the in-tree xhci-hcd, by creating and removing Docker networks
and containers in a loop; on x86 the largest gaps correspond to about
7 ms of data. Lines lost by a CH340 receiving 24-byte lines, per run
(about 220 seconds on the Pi, with two CH340 devices in each run and two
runs with 32 urbs, and 180 seconds on x86):

                    stock      16 urbs    32 urbs
  RPi 4, 921600     392, 398   -          0, 0 / 0, 0
  x86, 921600       436, 335   13, 6      0, 0
  x86, 1500000      703, 681   237, 279   0, 0, 0

With continuous iperf3 traffic through the bridge instead, x86 at
1500000 baud lost 74 to 127 lines per run with the stock driver and
nothing in three runs with 32 urbs. 1 KiB lasts about 7 ms at that rate,
so there is little margin left there. No read urb completed with -EPIPE
in these runs, so this is unrelated to the stalls reported in [3].

Bursts of 32 to 128 bytes followed by a pause were held back for 200 to
800 ms with 512-byte buffers, while this series delivered every burst as
quickly as the stock driver.

Hardware flow control is no option on these boards either: the CH340 is
soldered on, and its RTS output drives the ESP32's reset through the
auto-reset circuit, so with CRTSCTS set on a LOLIN32 the board was held
in reset.

The series is built on top of usb-next with W=1 for x86_64 and i386
without warnings, and the tests ran with the same changes applied to the
6.18 and 7.0 kernels. I can share the test firmware and scripts if that
helps.

I used an AI assistant (Claude) to help track the problem down and to
draft the patches and descriptions. I've reviewed and tested them
myself.

[1] https://lore.kernel.org/all/20210824121926.19311-1-johan@kernel.org/
[2] https://github.com/WCHSoftGroup/ch341ser_linux
[3] https://lore.kernel.org/all/CA+ycdsQ-k3ZnN9t9x4AeVRPgmT2eFWkOzHVChbaPY0ogJEcO6A@mail.gmail.com/

Mateusz

Mateusz Sikora (2):
  USB: serial: allow drivers to use more read urbs
  USB: serial: ch341: use more read urbs

 drivers/usb/serial/ch341.c      |  1 +
 drivers/usb/serial/usb-serial.c | 13 ++++++++++++-
 include/linux/usb/serial.h      | 10 ++++++++--
 3 files changed, 21 insertions(+), 3 deletions(-)

-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] USB: serial: allow drivers to use more read urbs
  2026-12-06  0:18 [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds Mateusz Sikora
@ 2026-12-06  0:18 ` Mateusz Sikora
  2026-12-06  0:18 ` [PATCH 2/2] USB: serial: ch341: " Mateusz Sikora
  2026-12-06  0:32 ` [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: Mateusz Sikora @ 2026-12-06  0:18 UTC (permalink / raw)
  To: Johan Hovold; +Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, Mateusz Sikora

The generic read implementation uses two bulk-in urbs per port. For
devices with small bulk endpoints this means that only two packets can
be in flight, which at higher line speeds leaves little margin for
completion latency on the host before the device runs out of buffer
space and starts dropping characters.

Add a num_read_urbs field to struct usb_serial_driver so that drivers
can request more read urbs (up to 32, the number of bits in the
read_urbs_free bitmap on 32-bit architectures). The default remains
two.

The remaining loops over the read urbs already handle unused (NULL)
entries.

Note that this grows struct usb_serial_port by 480 bytes on 64-bit.

This patch and its description were drafted with an AI assistant
(Claude) and reviewed and tested by me.

Signed-off-by: Mateusz Sikora <ms1qaz@gmail.com>
---
 drivers/usb/serial/usb-serial.c | 13 ++++++++++++-
 include/linux/usb/serial.h      | 10 ++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/serial/usb-serial.c b/drivers/usb/serial/usb-serial.c
index 4a19621f0..6de1823e1 100644
--- a/drivers/usb/serial/usb-serial.c
+++ b/drivers/usb/serial/usb-serial.c
@@ -889,19 +889,25 @@ static void find_endpoints(struct usb_serial *serial,
 	}
 }
 
+/* There is a bit for each read urb in read_urbs_free. */
+static_assert(USB_SERIAL_MAX_READ_URBS <= BITS_PER_LONG);
+
 static int setup_port_bulk_in(struct usb_serial_port *port,
 					struct usb_endpoint_descriptor *epd)
 {
 	struct usb_serial_driver *type = port->serial->type;
 	struct usb_device *udev = port->serial->dev;
 	int buffer_size;
+	int num_urbs;
 	int i;
 
 	buffer_size = max_t(int, type->bulk_in_size, usb_endpoint_maxp(epd));
 	port->bulk_in_size = buffer_size;
 	port->bulk_in_endpointAddress = epd->bEndpointAddress;
 
-	for (i = 0; i < ARRAY_SIZE(port->read_urbs); ++i) {
+	num_urbs = type->num_read_urbs ?: USB_SERIAL_DEFAULT_READ_URBS;
+
+	for (i = 0; i < num_urbs; ++i) {
 		set_bit(i, &port->read_urbs_free);
 		port->read_urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
 		if (!port->read_urbs[i])
@@ -1476,6 +1482,11 @@ static int usb_serial_register(struct usb_serial_driver *driver)
 				driver->description);
 		return -EINVAL;
 	}
+	if (driver->num_read_urbs > USB_SERIAL_MAX_READ_URBS) {
+		WARN(1, "Serial driver %s requests too many read urbs\n",
+				driver->description);
+		return -EINVAL;
+	}
 
 	/* Prevent individual ports from being unbound. */
 	driver->driver.suppress_bind_attrs = true;
diff --git a/include/linux/usb/serial.h b/include/linux/usb/serial.h
index 5866f19d3..7df729a44 100644
--- a/include/linux/usb/serial.h
+++ b/include/linux/usb/serial.h
@@ -18,6 +18,10 @@
 /* The maximum number of ports one device can grab at once */
 #define MAX_NUM_PORTS		16
 
+/* The default and maximum number of bulk-in urbs per port */
+#define USB_SERIAL_DEFAULT_READ_URBS	2
+#define USB_SERIAL_MAX_READ_URBS	32
+
 /* USB serial flags */
 #define USB_SERIAL_WRITE_BUSY	0
 #define USB_SERIAL_THROTTLED	1
@@ -85,8 +89,8 @@ struct usb_serial_port {
 	struct urb		*read_urb;
 	__u8			bulk_in_endpointAddress;
 
-	unsigned char		*bulk_in_buffers[2];
-	struct urb		*read_urbs[2];
+	unsigned char		*bulk_in_buffers[USB_SERIAL_MAX_READ_URBS];
+	struct urb		*read_urbs[USB_SERIAL_MAX_READ_URBS];
 	unsigned long		read_urbs_free;
 
 	unsigned char		*bulk_out_buffer;
@@ -194,6 +198,7 @@ struct usb_serial_endpoints {
  * @num_bulk_out: minimum number of bulk-out endpoints
  * @num_interrupt_in: minimum number of interrupt-in endpoints
  * @num_interrupt_out: minimum number of interrupt-out endpoints
+ * @num_read_urbs: number of bulk-in urbs to allocate per port (0 = 2)
  * @bulk_in_size: minimum number of bytes to allocate for bulk-in buffer
  *	(0 = end-point size)
  * @bulk_out_size: bytes to allocate for bulk-out buffer (0 = end-point size)
@@ -246,6 +251,7 @@ struct usb_serial_driver {
 	unsigned char		num_bulk_out;
 	unsigned char		num_interrupt_in;
 	unsigned char		num_interrupt_out;
+	unsigned char		num_read_urbs;
 
 	size_t			bulk_in_size;
 	size_t			bulk_out_size;
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] USB: serial: ch341: use more read urbs
  2026-12-06  0:18 [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds Mateusz Sikora
  2026-12-06  0:18 ` [PATCH 1/2] USB: serial: allow drivers to use more read urbs Mateusz Sikora
@ 2026-12-06  0:18 ` Mateusz Sikora
  2026-12-06  0:32   ` sashiko-bot
  2026-12-06  0:32 ` [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds sashiko-bot
  2 siblings, 1 reply; 5+ messages in thread
From: Mateusz Sikora @ 2026-12-06  0:18 UTC (permalink / raw)
  To: Johan Hovold
  Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, Mateusz Sikora,
	Willy Tarreau

The CH340 has 32-byte bulk endpoints so with two read urbs only 64
bytes can be in flight, which is less than 0.7 ms worth of data at
921600 baud. Characters are lost whenever the completion of a read urb
is delayed for longer than that.

This was first observed on a Raspberry Pi 4 where heavy network traffic
through a Docker bridge delayed the softirq that gives back completed
bulk-in urbs, on the CPU handling the xHCI interrupt, by up to 1.1 ms.
With six devices receiving at 921600 baud while Docker networks and
containers were created and removed in a loop for 150 seconds, the two
CH340 devices lost 392 and 398 lines of output in about 220 seconds of
recording, while a CH343 (cdc-acm) and three USB-Serial/JTAG devices on
the same hub lost nothing.

It can also be reproduced on x86 (Intel Sunrise Point xHCI) with the
same kind of load during 135 seconds of a 180 second recording. A CH340
receiving 24-byte lines then lost 335 to 436 lines per run at 921600
baud, and 681 to 703 lines at 1500000 baud. The largest gaps there
correspond to about 7 ms of data.

Using 512-byte buffers instead also avoids the loss but had to be
reverted as these devices do not send a zero-length packet when a
transfer is a multiple of the endpoint size, which could leave received
data stuck until more data arrived. See commit df7b16d1c00e ("Revert
"USB: serial: ch341: fix character loss at high transfer rates"").

Use 32 endpoint-sized read urbs instead, which gives 1 KiB of buffering
(about 11 ms at 921600 baud and 7 ms at 1500000 baud) without waiting
for short packets. This is the same amount as cdc-acm, which uses 16
urbs of twice the endpoint size.

With bursts of 32 to 128 bytes followed by a pause, 512-byte buffers
held the data back for 200 to 800 ms, until a burst ended with a short
packet, while endpoint-sized urbs delivered every burst as quickly as
the stock driver.

With this, nothing was lost in the setups above, in two runs on the
Raspberry Pi and in five runs on x86 (two at 921600 baud and three at
1500000 baud), nor in three x86 runs with continuous traffic through the
bridge at 1500000 baud. With 16 urbs the x86 setup still lost 6 to 13
lines at 921600 baud and 237 to 279 lines at 1500000 baud. Note that at
1500000 baud the buffering is about the same as the largest gaps seen,
so there is little margin left at that rate.

This patch and its description were drafted with an AI assistant
(Claude) and reviewed and tested by me.

Cc: Willy Tarreau <w@1wt.eu>
Signed-off-by: Mateusz Sikora <ms1qaz@gmail.com>
---
 drivers/usb/serial/ch341.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
index 569f4aede..c9ac114b8 100644
--- a/drivers/usb/serial/ch341.c
+++ b/drivers/usb/serial/ch341.c
@@ -868,6 +868,7 @@ static struct usb_serial_driver ch341_device = {
 	},
 	.id_table          = id_table,
 	.num_ports         = 1,
+	.num_read_urbs     = 32,
 	.open              = ch341_open,
 	.dtr_rts	   = ch341_dtr_rts,
 	.carrier_raised	   = ch341_carrier_raised,
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] USB: serial: ch341: use more read urbs
  2026-12-06  0:18 ` [PATCH 2/2] USB: serial: ch341: " Mateusz Sikora
@ 2026-12-06  0:32   ` sashiko-bot
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-06  0:32 UTC (permalink / raw)
  To: Mateusz Sikora; +Cc: linux-usb

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] USB: serial: ch341: 32 read URBs serialize HCD unlinks on
  close and suspend
--

commit 013e0416b1d1c563b2c9b985e82a5ab90d4fa11e
Author: Mateusz Sikora <ms1qaz@gmail.com>

USB: serial: ch341: use more read urbs

This commit increases the number of bulk-in read URBs used by the ch341
driver from the default of 2 to 32.

> diff --git a/drivers/usb/serial/ch341.c b/drivers/usb/serial/ch341.c
> index 569f4aede215d..c9ac114b82e36 100644
> --- a/drivers/usb/serial/ch341.c
> +++ b/drivers/usb/serial/ch341.c
> @@ -868,6 +868,7 @@ static struct usb_serial_driver ch341_device = {
>  	},
>  	.id_table          = id_table,
>  	.num_ports         = 1,
> +	.num_read_urbs     = 32,
>  	.open              = ch341_open,
>  	.dtr_rts	   = ch341_dtr_rts,
>  	.carrier_raised	   = ch341_carrier_raised,

[Severity: Medium]
Does queuing 32 read URBs introduce a close and suspend latency regression?

On ch341_close(), usb_serial_generic_close() loops over port->read_urbs[]
in forward order calling usb_kill_urb(), and usb_serial_port_poison_urbs()
does the same with usb_poison_urb() on suspend.  Each call unlinks one URB
and sleeps until it completes, leaving the rest active on the endpoint.

On EHCI, each active bulk QH unlink waits on a 2 ms timer and relinks the
QH while other URBs remain queued, stalling close or suspend for at least
64 ms per open port (up from ~4 ms).  OHCI and UHCI stall for 32-64 ms,
and xHCI runs 32 Stop Endpoint and Set TR Dequeue Pointer cycles.

Blocking all read URBs with usb_block_urb(), issuing asynchronous
usb_unlink_urb() across them, and then waiting for completion in
usb-serial core would let the HCD unlink the queue in a single cycle.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206001844.14858-1-ms1qaz@gmail.com?part=2

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds
  2026-12-06  0:18 [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds Mateusz Sikora
  2026-12-06  0:18 ` [PATCH 1/2] USB: serial: allow drivers to use more read urbs Mateusz Sikora
  2026-12-06  0:18 ` [PATCH 2/2] USB: serial: ch341: " Mateusz Sikora
@ 2026-12-06  0:32 ` sashiko-bot
  2 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-06  0:32 UTC (permalink / raw)
  To: Mateusz Sikora; +Cc: linux-usb

Sashiko has finished reviewing this 2-patch series:

- Patch 1: no issues found.
- Patch 2 ("USB: serial: ch341: use more read urbs"): 1 potential issue
  (1 Medium)

A detailed review has been sent as a separate reply to patch 2.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206001844.14858-1-ms1qaz@gmail.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-06  0:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  0:18 [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds Mateusz Sikora
2026-12-06  0:18 ` [PATCH 1/2] USB: serial: allow drivers to use more read urbs Mateusz Sikora
2026-12-06  0:18 ` [PATCH 2/2] USB: serial: ch341: " Mateusz Sikora
2026-12-06  0:32   ` sashiko-bot
2026-12-06  0:32 ` [PATCH 0/2] USB: serial: ch341: fix character loss at high line speeds sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox