[PATCH v3 2/3] USB: serial: kobil_sct: serialize concurrent writes

From: YuanZhe Liu

Date: Mon Nov 30 2026 - 07:06:11 EST


kobil_write() and kobil_write_room() access the shared transmit buffer
without locking. The tty layer can call the write method concurrently,
for example tty_send_xchar() from an ioctl holds only
tty->atomic_write_lock while echo processing from the receive path
holds only the n_tty output lock.

Two callers can therefore both enter the chunking loop, overwrite the
interrupt-out buffer and resubmit the same URB while it is still active.

Serialize access to the transmit buffer, make a second writer return
zero (and report no room) while a block is being sent, and wake up the
tty layer once it has been sent.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
drivers/usb/serial/kobil_sct.c | 49 +++++++++++++++++++++++++++++-----
1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/drivers/usb/serial/kobil_sct.c b/drivers/usb/serial/kobil_sct.c
index f1c3b53c264f..aa4170ba3676 100644
--- a/drivers/usb/serial/kobil_sct.c
+++ b/drivers/usb/serial/kobil_sct.c
@@ -106,6 +106,8 @@ struct kobil_private {
int cur_pos; /* index of the next char to send in buf */
__u16 device_type;
struct completion write_done;
+ spinlock_t lock; /* protects the transmit buffer state */
+ bool sending;
};

static int kobil_ctrl_send(struct usb_serial_port *port, u8 req, u16 val)
@@ -137,6 +139,7 @@ static int kobil_port_probe(struct usb_serial_port *port)
priv->cur_pos = 0;
priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
init_completion(&priv->write_done);
+ spin_lock_init(&priv->lock);

switch (priv->device_type) {
case KOBIL_ADAPTER_B_PRODUCT_ID:
@@ -277,7 +280,10 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
const unsigned char *buf, int count)
{
struct kobil_private *priv;
+ unsigned long flags;
int length, todo, result;
+ int offset;
+ bool complete;

if (count == 0) {
dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
@@ -286,20 +292,36 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,

priv = usb_get_serial_port_data(port);

+ spin_lock_irqsave(&priv->lock, flags);
+ if (priv->sending) {
+ spin_unlock_irqrestore(&priv->lock, flags);
+ return 0;
+ }
+
if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
+ spin_unlock_irqrestore(&priv->lock, flags);
dev_dbg(&port->dev, "%s - Error: write request bigger than buffer size\n", __func__);
return -ENOMEM;
}

/* Copy data to buffer */
- memcpy(priv->buf + priv->filled, buf, count);
- usb_serial_debug_data(&port->dev, __func__, count, priv->buf + priv->filled);
- priv->filled = priv->filled + count;
+ offset = priv->filled;
+ memcpy(priv->buf + offset, buf, count);
+ priv->filled = offset + count;

/* only send complete block. TWIN, KAAN SIM and adapter K
use the same protocol. */
- if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (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)))) {
+ complete = ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) &&
+ (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)
+ priv->sending = true;
+ 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) {
@@ -357,8 +379,13 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
msleep(24);
}

+ spin_lock_irqsave(&priv->lock, flags);
priv->filled = 0;
priv->cur_pos = 0;
+ priv->sending = false;
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ tty_port_tty_wakeup(&port->port);

/* start reading (except TWIN and KAAN SIM) */
if (!result &&
@@ -375,8 +402,16 @@ static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,

static unsigned int kobil_write_room(struct tty_struct *tty)
{
- /* FIXME */
- return 8;
+ struct usb_serial_port *port = tty->driver_data;
+ struct kobil_private *priv = usb_get_serial_port_data(port);
+ unsigned long flags;
+ unsigned int room;
+
+ spin_lock_irqsave(&priv->lock, flags);
+ room = priv->sending ? 0 : 8;
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ return room;
}


--
2.34.1