* [PATCH] USB: serial: f81534: fix write urb reuse while still in flight
@ 2026-11-30 9:00 YuanZhe Liu
2026-11-30 9:20 ` sashiko-bot
2026-11-30 10:19 ` Greg Kroah-Hartman
0 siblings, 2 replies; 3+ messages in thread
From: YuanZhe Liu @ 2026-11-30 9:00 UTC (permalink / raw)
To: Johan Hovold
Cc: Greg Kroah-Hartman, linux-usb, linux-kernel, stable, Bin Yu,
Cheng Wen
The driver uses the device's TX_EMPTY notification as the only gate for
reusing the single bulk-out urb. That notification only tells that the
device transmitter has drained; it does not guarantee that the host has
retired the previous transfer, which is completed asynchronously.
When a TX_EMPTY block is received while the tty layer is submitting the
next transfer, the block is parsed in the bulk-in completion handler and
f81534_submit_writer() submits the same urb again:
URB 00000000f03a9f3b submitted while active
WARNING: drivers/usb/core/urb.c:379 at usb_submit_urb
...
f81534_submit_writer
f81534_process_read_urb
...
f81534_submit_writer
f81534_write
usb_submit_urb() rejects the urb with -EBUSY and the transfer buffer is
rewritten while the controller may still be using it.
Track whether the write urb is in flight and only reuse it after the
completion handler has run. A TX_EMPTY notification received while a
transfer is pending is kept and the completion handler submits the next
transfer as soon as the urb is available.
Fixes: 0c9bd6004d25 ("USB: serial: add Fintek F81532/534 driver")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
drivers/usb/serial/f81534.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/drivers/usb/serial/f81534.c b/drivers/usb/serial/f81534.c
index 8fc60c4d6166..13bd29d5075b 100644
--- a/drivers/usb/serial/f81534.c
+++ b/drivers/usb/serial/f81534.c
@@ -159,6 +159,7 @@ struct f81534_port_private {
struct work_struct lsr_work;
struct usb_serial_port *port;
unsigned long tx_empty;
+ unsigned long write_urb_active;
spinlock_t msr_lock;
u32 baud_base;
u8 shadow_mcr;
@@ -517,12 +518,22 @@ static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
return 0;
+ /*
+ * The TX_EMPTY token only tells that the device drained its
+ * transmitter; the urb may still be owned by the HCD.
+ */
+ if (test_and_set_bit(0, &port_priv->write_urb_active)) {
+ set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
+ return 0;
+ }
+
urb = port->write_urbs[0];
f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
urb->transfer_buffer_length = F81534_WRITE_BUFFER_SIZE;
result = usb_submit_urb(urb, mem_flags);
if (result) {
+ clear_bit(0, &port_priv->write_urb_active);
set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
result);
@@ -1270,6 +1281,9 @@ static void f81534_process_read_urb(struct urb *urb)
static void f81534_write_usb_callback(struct urb *urb)
{
struct usb_serial_port *port = urb->context;
+ struct f81534_port_private *port_priv = usb_get_serial_port_data(port);
+
+ clear_bit(0, &port_priv->write_urb_active);
switch (urb->status) {
case 0:
@@ -1287,8 +1301,10 @@ static void f81534_write_usb_callback(struct urb *urb)
default:
dev_dbg(&port->dev, "%s - nonzero urb status: %d\n",
__func__, urb->status);
- break;
+ return;
}
+
+ f81534_submit_writer(port, GFP_ATOMIC);
}
static void f81534_lsr_worker(struct work_struct *work)
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] USB: serial: f81534: fix write urb reuse while still in flight
2026-11-30 9:00 [PATCH] USB: serial: f81534: fix write urb reuse while still in flight YuanZhe Liu
@ 2026-11-30 9:20 ` sashiko-bot
2026-11-30 10:19 ` Greg Kroah-Hartman
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-30 9:20 UTC (permalink / raw)
To: YuanZhe Liu; +Cc: linux-usb
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] USB: serial: f81534: race on tx_empty in
f81534_submit_writer() stalls TX
--
commit e5c172675a1d26c17159432b9b68ed3d9a834936
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
USB: serial: f81534: fix write urb reuse while still in flight
This commit tracks whether the bulk-out write URB is active so that it is
only reused after its completion handler runs.
> diff --git a/drivers/usb/serial/f81534.c b/drivers/usb/serial/f81534.c
> index 8fc60c4d61660..13bd29d5075b5 100644
> --- a/drivers/usb/serial/f81534.c
> +++ b/drivers/usb/serial/f81534.c
[ ... ]
> @@ -517,12 +518,22 @@ static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
> if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
> return 0;
>
> + /*
> + * The TX_EMPTY token only tells that the device drained its
> + * transmitter; the urb may still be owned by the HCD.
> + */
> + if (test_and_set_bit(0, &port_priv->write_urb_active)) {
> + set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
> + return 0;
> + }
[Severity: Medium]
Can clearing and restoring F81534_TX_EMPTY_BIT around the write_urb_active
check race with f81534_write_usb_callback() and stall TX?
f81534_submit_writer() drops port->lock before testing tx_empty and
write_urb_active. If a caller finds write_urb_active set while the
in-flight URB completes on another CPU, both paths can exit without
submitting the next transfer:
CPU0 CPU1
f81534_submit_writer()
test_and_clear_bit(..., &tx_empty)
test_and_set_bit(..., &write_urb_active)
/* returns 1, already active */
f81534_write_usb_callback()
clear_bit(..., &write_urb_active)
f81534_submit_writer()
test_and_clear_bit(..., &tx_empty)
/* sees 0, returns 0 */
set_bit(..., &tx_empty)
return 0; <-- data stranded in write_fifo
With no URB in flight and no further TX_EMPTY notification coming from the
device, queued data remains stuck in port->write_fifo.
Conversely, if f81534_write() -> f81534_submit_writer() clears
F81534_TX_EMPTY_BIT and is interrupted before test_and_set_bit() by a
bulk-in completion receiving F81534_TOKEN_TX_EMPTY, the interrupt handler
sets F81534_TX_EMPTY_BIT, claims write_urb_active, and submits the URB.
When the interrupted context resumes, it sees write_urb_active set and
calls set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty) while the new
transfer is active, allowing f81534_write_usb_callback() to submit the
following URB before the device transmitter has drained.
Checking and updating tx_empty and write_urb_active under port->lock would
avoid both races.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130090037.24537-1-25031212351@stu.xidian.edu.cn?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] USB: serial: f81534: fix write urb reuse while still in flight
2026-11-30 9:00 [PATCH] USB: serial: f81534: fix write urb reuse while still in flight YuanZhe Liu
2026-11-30 9:20 ` sashiko-bot
@ 2026-11-30 10:19 ` Greg Kroah-Hartman
1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2026-11-30 10:19 UTC (permalink / raw)
To: YuanZhe Liu
Cc: Johan Hovold, linux-usb, linux-kernel, stable, Bin Yu, Cheng Wen
On Mon, Nov 30, 2026 at 05:00:37PM +0800, YuanZhe Liu wrote:
> The driver uses the device's TX_EMPTY notification as the only gate for
> reusing the single bulk-out urb. That notification only tells that the
> device transmitter has drained; it does not guarantee that the host has
> retired the previous transfer, which is completed asynchronously.
>
> When a TX_EMPTY block is received while the tty layer is submitting the
> next transfer, the block is parsed in the bulk-in completion handler and
> f81534_submit_writer() submits the same urb again:
>
> URB 00000000f03a9f3b submitted while active
> WARNING: drivers/usb/core/urb.c:379 at usb_submit_urb
> ...
> f81534_submit_writer
> f81534_process_read_urb
> ...
> f81534_submit_writer
> f81534_write
>
> usb_submit_urb() rejects the urb with -EBUSY and the transfer buffer is
> rewritten while the controller may still be using it.
>
> Track whether the write urb is in flight and only reuse it after the
> completion handler has run. A TX_EMPTY notification received while a
> transfer is pending is kept and the completion handler submits the next
> transfer as soon as the urb is available.
>
> Fixes: 0c9bd6004d25 ("USB: serial: add Fintek F81532/534 driver")
> Cc: stable@vger.kernel.org
> Co-developed-by: Bin Yu <byu@xidian.edu.cn>
> Signed-off-by: Bin Yu <byu@xidian.edu.cn>
> Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
> Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
> ---
> drivers/usb/serial/f81534.c | 18 +++++++++++++++++-
> 1 file changed, 17 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/usb/serial/f81534.c b/drivers/usb/serial/f81534.c
> index 8fc60c4d6166..13bd29d5075b 100644
> --- a/drivers/usb/serial/f81534.c
> +++ b/drivers/usb/serial/f81534.c
> @@ -159,6 +159,7 @@ struct f81534_port_private {
> struct work_struct lsr_work;
> struct usb_serial_port *port;
> unsigned long tx_empty;
> + unsigned long write_urb_active;
Why are you using an unsigned long for a boolean flag?
LLMs are dumb auto-word-generators, please use your judgement when
reviewing their output, this shows it was not reviewed :(
And LLMs _love_ adding new boolean flags, be very sure that this is the
only solution for this "problem" before proposing it again. Odds are,
based on what we have seen for kernel-output from these tools, it's
almost always not the proper solution (i.e. they can deduce state easy
from this type of change, when the real solution is a logic change
instead.)
good luck!
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-30 10:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 9:00 [PATCH] USB: serial: f81534: fix write urb reuse while still in flight YuanZhe Liu
2026-11-30 9:20 ` sashiko-bot
2026-11-30 10:19 ` Greg Kroah-Hartman
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox