[PATCH 2/2] USB: serial: ch341: use more read urbs
From: Mateusz Sikora
Date: Sat Dec 05 2026 - 19:19:04 EST
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@xxxxxx>
Signed-off-by: Mateusz Sikora <ms1qaz@xxxxxxxxx>
---
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)