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 Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen 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