kobil_write() stops the interrupt-in URB, submits a block in chunks and waits for every transfer to complete, so the write path sleeps. The completion it waits on is embedded in the port private data, and the tty layer may call the write method from atomic context (for example, line disciplines such as N_GSM call tty->ops->write() under a spinlock). Move the block transmission to a work item. The write path now only copies data into the buffer and schedules the work, while the work item stops the interrupt-in URB, sends the chunks at the existing pace, handles transfer errors and restarts reading. Cancel the work in close() and port_remove() so the private data cannot be freed while the work item is still using it, and restart the interrupt-in URB only when the whole block was sent. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") 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> --- drivers/usb/serial/kobil_sct.c | 190 +++++++++++++++++++++------------ 1 file changed, 120 insertions(+), 70 deletions(-) diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c index aa4170ba3676..0b52ae7fa165 100644 --- a/drivers/usb/serial/kobil_sct.c +++ b/drivers/usb/serial/kobil_sct.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,7 @@ static int kobil_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear); static void kobil_read_int_callback(struct urb *urb); static void kobil_write_int_callback(struct urb *urb); +static void kobil_write_work(struct work_struct *work); static void kobil_set_termios(struct tty_struct *tty, struct usb_serial_port *port, const struct ktermios *old); @@ -108,6 +110,9 @@ struct kobil_private { struct completion write_done; spinlock_t lock; /* protects the transmit buffer state */ bool sending; + bool closed; + struct work_struct write_work; + struct usb_serial_port *port; }; static int kobil_ctrl_send(struct usb_serial_port *port, u8 req, u16 val) @@ -140,6 +145,9 @@ static int kobil_port_probe(struct usb_serial_port *port) priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct); init_completion(&priv->write_done); spin_lock_init(&priv->lock); + priv->closed = false; + priv->port = port; + INIT_WORK(&priv->write_work, kobil_write_work); switch (priv->device_type) { case KOBIL_ADAPTER_B_PRODUCT_ID: @@ -166,6 +174,7 @@ static void kobil_port_remove(struct usb_serial_port *port) struct kobil_private *priv; priv = usb_get_serial_port_data(port); + cancel_work_sync(&priv->write_work); kfree(priv); } @@ -183,11 +192,19 @@ static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port) struct device *dev = &port->dev; struct kobil_private *priv; unsigned char *transfer_buffer; + unsigned long flags; int transfer_buffer_length = 8; int result; priv = usb_get_serial_port_data(port); + spin_lock_irqsave(&priv->lock, flags); + priv->filled = 0; + priv->cur_pos = 0; + priv->sending = false; + priv->closed = false; + spin_unlock_irqrestore(&priv->lock, flags); + /* allocate memory for transfer buffer */ transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL); if (!transfer_buffer) @@ -237,9 +254,27 @@ static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port) static void kobil_close(struct usb_serial_port *port) { + struct kobil_private *priv = usb_get_serial_port_data(port); + unsigned long flags; + /* FIXME: Add rts/dtr methods */ + spin_lock_irqsave(&priv->lock, flags); + priv->closed = true; + spin_unlock_irqrestore(&priv->lock, flags); + + usb_kill_urb(port->interrupt_out_urb); + usb_kill_urb(port->interrupt_in_urb); + + /* The work item may have resubmitted a URB before it saw closed. */ + cancel_work_sync(&priv->write_work); usb_kill_urb(port->interrupt_out_urb); usb_kill_urb(port->interrupt_in_urb); + + spin_lock_irqsave(&priv->lock, flags); + priv->filled = 0; + priv->cur_pos = 0; + priv->sending = false; + spin_unlock_irqrestore(&priv->lock, flags); } @@ -281,7 +316,6 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port, { struct kobil_private *priv; unsigned long flags; - int length, todo, result; int offset; bool complete; @@ -315,91 +349,107 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port, (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) || ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))); - if (complete) + if (complete && !priv->closed) { priv->sending = true; + schedule_work(&priv->write_work); + } spin_unlock_irqrestore(&priv->lock, flags); usb_serial_debug_data(&port->dev, __func__, count, priv->buf + offset); - if (complete) { - /* stop reading (except TWIN and KAAN SIM) */ - if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || - priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) { - usb_kill_urb(port->interrupt_in_urb); + return count; +} + +static void kobil_write_work(struct work_struct *work) +{ + struct kobil_private *priv = container_of(work, struct kobil_private, + write_work); + struct usb_serial_port *port = priv->port; + unsigned long flags; + int length, todo, result; + bool closed; + + /* stop reading (except TWIN and KAAN SIM) */ + if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || + priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) { + usb_kill_urb(port->interrupt_in_urb); + } + + spin_lock_irqsave(&priv->lock, flags); + todo = priv->filled - priv->cur_pos; + spin_unlock_irqrestore(&priv->lock, flags); + + result = 0; + while (todo > 0) { + if (READ_ONCE(priv->closed)) { + result = -ENODEV; + break; } - todo = priv->filled - priv->cur_pos; - result = 0; - - while (todo > 0) { - /* max 8 byte in one urb (endpoint size) */ - length = min(todo, port->interrupt_out_size); - /* copy data to transfer buffer */ - memcpy(port->interrupt_out_buffer, - priv->buf + priv->cur_pos, length); - port->interrupt_out_urb->transfer_buffer_length = length; - - priv->cur_pos = priv->cur_pos + length; - reinit_completion(&priv->write_done); - result = usb_submit_urb(port->interrupt_out_urb, - GFP_ATOMIC); - if (result) { - dev_err(&port->dev, - "%s - failed to send write URB: %d\n", - __func__, result); - break; - } - - /* Do not reuse the URB before the transfer has completed. */ - if (!wait_for_completion_timeout(&priv->write_done, - msecs_to_jiffies(KOBIL_TIMEOUT))) { - usb_kill_urb(port->interrupt_out_urb); - result = -ETIMEDOUT; - break; - } - - /* - * The port may have been disconnected while waiting, in - * which case the private data may already have been freed. - */ - if (port->serial->disconnected) - return count; - - result = port->interrupt_out_urb->status; - if (result) { - dev_dbg(&port->dev, - "%s - write URB status: %d\n", - __func__, result); - break; - } - - todo = priv->filled - priv->cur_pos; - - if (todo > 0) - msleep(24); + /* max 8 byte in one urb (endpoint size) */ + length = min(todo, port->interrupt_out_size); + /* copy data to transfer buffer */ + memcpy(port->interrupt_out_buffer, + priv->buf + priv->cur_pos, length); + port->interrupt_out_urb->transfer_buffer_length = length; + + priv->cur_pos = priv->cur_pos + length; + reinit_completion(&priv->write_done); + result = usb_submit_urb(port->interrupt_out_urb, GFP_KERNEL); + if (result) { + dev_err(&port->dev, + "%s - failed to send write URB: %d\n", + __func__, result); + break; } - spin_lock_irqsave(&priv->lock, flags); - priv->filled = 0; - priv->cur_pos = 0; - priv->sending = false; - spin_unlock_irqrestore(&priv->lock, flags); + /* Do not reuse the URB before the transfer has completed. */ + if (!wait_for_completion_timeout(&priv->write_done, + msecs_to_jiffies(KOBIL_TIMEOUT))) { + usb_kill_urb(port->interrupt_out_urb); + result = -ETIMEDOUT; + break; + } - tty_port_tty_wakeup(&port->port); + if (port->serial->disconnected) { + result = -ENODEV; + break; + } - /* start reading (except TWIN and KAAN SIM) */ - if (!result && - (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || - priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID)) { - result = usb_submit_urb(port->interrupt_in_urb, - GFP_ATOMIC); - dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result); + result = port->interrupt_out_urb->status; + if (result) { + dev_dbg(&port->dev, + "%s - write URB status: %d\n", + __func__, result); + break; } + + todo = priv->filled - priv->cur_pos; + + if (todo > 0) + msleep(24); } - return count; + + spin_lock_irqsave(&priv->lock, flags); + priv->filled = 0; + priv->cur_pos = 0; + priv->sending = false; + closed = priv->closed; + spin_unlock_irqrestore(&priv->lock, flags); + + /* start reading (except TWIN and KAAN SIM) */ + if (!result && !closed && + (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || + priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID)) { + result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); + dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result); + } + + tty_port_tty_wakeup(&port->port); } + static unsigned int kobil_write_room(struct tty_struct *tty) { struct usb_serial_port *port = tty->driver_data; -- 2.34.1