[PATCH v2 1/4] serial: max310x: do not advance the TX fifo past a flush
From: Tapio Reijonen
Date: Tue Nov 17 2026 - 01:14:23 EST
max310x_handle_tx() runs from a work item. It peeks at the transmit
fifo with kfifo_out_linear_ptr(), writes the bytes to the chip over
SPI, which sleeps, and advances the fifo afterwards with
uart_xmit_advance(), all without the port lock. uart_flush_buffer()
resets the fifo under the port lock. When the reset lands between the
peek and the advance, the advance adds the chunk length to a read
index that is already zero, the index ends up ahead of the write
index, and kfifo_len() wraps to a value near 4G.
>From then on the fifo never reads as empty: tcdrain() sleeps forever
in tty_wait_until_sent(), and while the port stays open the work item
keeps sending stale bytes from the buffer. Observed with a tcdrain()
in flight at 300 baud when the port was hung up by an unbind, which
flushes twice: the first flush left 498 bytes, the second found
4294967217, the draining task read 4294967276 and never woke again.
Before the kfifo conversion the same race left the circular buffer
with up to 4 KiB of stale data to resend, after which it recovered.
Copy the bytes into a per-port buffer under the port lock, send them
from there, and take the lock again for the advance. A flush_buffer()
callback records a reset of the fifo, and the advance is skipped when
one happened during the transfer. kfifo_reset() is documented to
require that no other thread accesses the fifo, so the consumer has to
hold the same lock as the flush.
The bytes stay in the fifo until the chip has them. tx_empty() reads
only the chip FIFO level, so taking them out before the transfer would
make tcdrain() and close() see an idle transmitter with up to one FIFO
load still to be sent.
The lock is not held over the SPI transfer. regmap hands that to the
controller straight from the buffer, which is why the buffer is per
port and not on the stack. The copy is limited to the buffer size: its
length is derived from port->fifosize, which TIOCSSERIAL can change,
and from a FIFO level read from the chip.
Fixes: 1788cf6a91d9 ("tty: serial: switch from circ_buf to kfifo")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>
---
drivers/tty/serial/max310x.c | 46 ++++++++++++++++++++++++++++++++++++--------
1 file changed, 38 insertions(+), 8 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index a0ed70185555eed745d905b36c8b8f162468dd6f..e6a740295b96653281a92f8d94999f06c2c74685 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -317,11 +317,13 @@ struct max310x_one {
bool sw_rts_during_tx;
bool cancel_tx_delay_tmr;
bool tx_teardown; /* envelope being torn down */
+ bool tx_flushed; /* fifo reset under a transfer */
bool rs485_pending; /* rs_work not yet applied */
bool tx_break; /* break_ctl() owns the transceiver */
enum max310x_tx_state tx_state;
u8 rx_buf[MAX310X_FIFO_SIZE];
+ u8 tx_buf[MAX310X_FIFO_SIZE];
};
#define to_max310x_port(_port) \
container_of(_port, struct max310x_one, port)
@@ -911,6 +913,7 @@ static void max310x_delayed_stop_tx(struct uart_port *port)
static void max310x_handle_tx(struct uart_port *port)
{
+ struct max310x_one *one = to_max310x_port(port);
struct tty_port *tport = &port->state->port;
if (unlikely(port->x_char)) {
@@ -931,23 +934,41 @@ static void max310x_handle_tx(struct uart_port *port)
return;
}
- /*
- * It's a circ buffer -- wrap around.
- * We could do that in one SPI transaction, but meh.
- */
while (!kfifo_is_empty(&tport->xmit_fifo)) {
unsigned int txlen, to_send;
- unsigned char *tail;
+ unsigned long flags;
/* Limit to space available in TX FIFO */
txlen = max310x_port_read(port, MAX310X_TXFIFOLVL_REG);
txlen = port->fifosize - txlen;
+ txlen = min(txlen, sizeof(one->tx_buf));
if (!txlen)
break;
- to_send = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, txlen);
- max310x_batch_write(port, tail, to_send);
- uart_xmit_advance(port, to_send);
+ /*
+ * Copy the bytes under the port lock, but leave them in the
+ * fifo until the chip has them: an empty fifo with an empty
+ * chip FIFO reads as an idle transmitter.
+ */
+ uart_port_lock_irqsave(port, &flags);
+ to_send = kfifo_out_peek(&tport->xmit_fifo, one->tx_buf, txlen);
+ one->tx_flushed = false;
+ uart_port_unlock_irqrestore(port, flags);
+ if (!to_send)
+ break;
+
+ max310x_batch_write(port, one->tx_buf, to_send);
+
+ /*
+ * uart_flush_buffer() reset the fifo during the transfer:
+ * advancing now would move the read index past that reset.
+ */
+ uart_port_lock_irqsave(port, &flags);
+ if (one->tx_flushed)
+ port->icount.tx += to_send;
+ else
+ uart_xmit_advance(port, to_send);
+ uart_port_unlock_irqrestore(port, flags);
}
if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
@@ -1094,6 +1115,14 @@ static irqreturn_t max310x_ist(int irq, void *dev_id)
return IRQ_RETVAL(handled);
}
+/* called with port.lock taken and irqs off */
+static void max310x_flush_buffer(struct uart_port *port)
+{
+ struct max310x_one *one = to_max310x_port(port);
+
+ one->tx_flushed = true;
+}
+
static void max310x_tx_proc(struct work_struct *ws)
{
struct max310x_one *one = container_of(ws, struct max310x_one, tx_work);
@@ -1732,6 +1761,7 @@ static const struct uart_ops max310x_ops = {
.break_ctl = max310x_break_ctl,
.startup = max310x_startup,
.shutdown = max310x_shutdown,
+ .flush_buffer = max310x_flush_buffer,
.set_termios = max310x_set_termios,
.type = max310x_type,
.request_port = max310x_request_port,
--
2.47.3