Linux USB
 help / color / mirror / Atom feed
* [PATCH v2] USB: serial: f81534: fix write urb reuse while still in flight
@ 2026-11-30 10:39 YuanZhe Liu
  2026-11-30 10:48 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-11-30 10:39 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.

Take the write urb from port->write_urbs_free under port->lock before
consuming the notification, and leave the notification pending when the
urb is still in flight.  The completion handler releases the urb and
submits the pending transfer.

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>
---
v2: Use the existing write_urbs_free bitmap instead of a driver-private
    flag, and decide under port->lock whether to consume the TX_EMPTY
    notification.
---
 drivers/usb/serial/f81534.c | 40 +++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/serial/f81534.c b/drivers/usb/serial/f81534.c
index 8fc60c4d6166..60ff4c9eff81 100644
--- a/drivers/usb/serial/f81534.c
+++ b/drivers/usb/serial/f81534.c
@@ -506,16 +506,25 @@ static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
 	/* Check is any data in write_fifo */
 	spin_lock_irqsave(&port->lock, flags);
 
-	if (kfifo_is_empty(&port->write_fifo)) {
-		spin_unlock_irqrestore(&port->lock, flags);
-		return 0;
-	}
+	if (kfifo_is_empty(&port->write_fifo))
+		goto unlock;
 
-	spin_unlock_irqrestore(&port->lock, flags);
+	/*
+	 * The TX_EMPTY notification only tells that the device drained its
+	 * transmitter; the write urb may still be owned by the HCD.  Keep the
+	 * notification pending in that case and let the completion handler
+	 * submit the transfer.
+	 */
+	if (!test_and_clear_bit(0, &port->write_urbs_free))
+		goto unlock;
 
 	/* Check H/W is TXEMPTY */
-	if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty))
-		return 0;
+	if (!test_and_clear_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty)) {
+		set_bit(0, &port->write_urbs_free);
+		goto unlock;
+	}
+
+	spin_unlock_irqrestore(&port->lock, flags);
 
 	urb = port->write_urbs[0];
 	f81534_prepare_write_buffer(port, port->bulk_out_buffers[0]);
@@ -523,7 +532,11 @@ static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
 
 	result = usb_submit_urb(urb, mem_flags);
 	if (result) {
+		spin_lock_irqsave(&port->lock, flags);
+		set_bit(0, &port->write_urbs_free);
 		set_bit(F81534_TX_EMPTY_BIT, &port_priv->tx_empty);
+		spin_unlock_irqrestore(&port->lock, flags);
+
 		dev_err(&port->dev, "%s: submit failed: %d\n", __func__,
 				result);
 		return result;
@@ -531,6 +544,10 @@ static int f81534_submit_writer(struct usb_serial_port *port, gfp_t mem_flags)
 
 	usb_serial_port_softint(port);
 	return 0;
+
+unlock:
+	spin_unlock_irqrestore(&port->lock, flags);
+	return 0;
 }
 
 static u32 f81534_calc_baud_divisor(u32 baudrate, u32 clockrate)
@@ -1270,6 +1287,11 @@ 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;
+	unsigned long flags;
+
+	spin_lock_irqsave(&port->lock, flags);
+	set_bit(0, &port->write_urbs_free);
+	spin_unlock_irqrestore(&port->lock, flags);
 
 	switch (urb->status) {
 	case 0:
@@ -1287,8 +1309,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] 2+ messages in thread

* Re: [PATCH v2] USB: serial: f81534: fix write urb reuse while still in flight
  2026-11-30 10:39 [PATCH v2] USB: serial: f81534: fix write urb reuse while still in flight YuanZhe Liu
@ 2026-11-30 10:48 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 10:48 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-usb

> 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
>   ...
> [ ... ]
> 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>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130103950.22937-1-25031212351@stu.xidian.edu.cn?part=1

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

end of thread, other threads:[~2026-11-30 10:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 10:39 [PATCH v2] USB: serial: f81534: fix write urb reuse while still in flight YuanZhe Liu
2026-11-30 10:48 ` sashiko-bot

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