[PATCH 4/5] tty: serial: msm: fix TX kfifo underflow on flush
Samarth Narang
samarth.colleges at gmail.com
Sat Oct 31 05:39:10 PDT 2026
uart_flush_buffer() resets the transmit kfifo under the port lock before
calling the driver's flush_buffer() hook. Since msm_serial does not
implement this hook, an outstanding TX DMA transfer may complete after
the flush and call uart_xmit_advance() using the flushed transfer's byte
count. The existing check for a stopped transfer relies on tx_sg, which
the flush does not clear, so it does not catch this case. The kfifo tail
then advances past its head, causing kfifo_len() to wrap and the driver
to transmit stale buffer contents. The same issue was fixed in 8250 by
commit 9e512eaaf8f4 ("serial: 8250: Fix fifo underflow on flush").
Since the TX buffer is mapped separately for each transfer and the flush
hook runs under the port lock, the driver cannot synchronously terminate
DMA and safely unmap the buffer from that context. Instead, leave the
outstanding transfer running and mark it as flushed, allowing the
completion callback to unmap the buffer without advancing the kfifo.
Clear the flag when preparing the next transfer. This avoids the kfifo
underflow, although the data already queued for DMA before the flush may
still be transmitted.
Fixes: 3a878c430fd6 ("tty: serial: msm: Add TX DMA support")
Cc: stable at vger.kernel.org
Assisted-by: LLM
Signed-off-by: Samarth Narang <samarth.colleges at gmail.com>
---
drivers/tty/serial/msm_serial.c | 19 ++++++++++++++++++-
1 file changed, 18 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/serial/msm_serial.c b/drivers/tty/serial/msm_serial.c
index bfa44b01c3e9..a9eca971e9ff 100644
--- a/drivers/tty/serial/msm_serial.c
+++ b/drivers/tty/serial/msm_serial.c
@@ -185,6 +185,7 @@ struct msm_port {
bool break_detected;
struct msm_dma tx_dma;
struct msm_dma rx_dma;
+ bool tx_dma_flushed;
};
static inline struct msm_port *to_msm_port(struct uart_port *up)
@@ -428,6 +429,19 @@ static void msm_stop_tx(struct uart_port *port)
msm_write(port, msm_port->imr, MSM_UART_IMR);
}
+static void msm_flush_buffer(struct uart_port *port)
+{
+ struct msm_port *msm_port = to_msm_port(port);
+
+ /*
+ * The kfifo has been reset. Let the running transfer finish, so that
+ * its buffer is unmapped only once it has stopped, but do not advance
+ * the kfifo for it.
+ */
+ if (sg_dma_len(&msm_port->tx_dma.tx_sg))
+ msm_port->tx_dma_flushed = true;
+}
+
static void msm_start_tx(struct uart_port *port)
{
struct msm_port *msm_port = to_msm_port(port);
@@ -479,7 +493,8 @@ static void msm_complete_tx_dma(void *args)
}
count = sg_dma_len(&dma->tx_sg) - state.residue;
- uart_xmit_advance(port, count);
+ if (!msm_port->tx_dma_flushed)
+ uart_xmit_advance(port, count);
sg_init_table(&dma->tx_sg, 1);
/* Restore "Tx FIFO below watermark" interrupt */
@@ -503,6 +518,7 @@ static int msm_handle_tx_dma(struct msm_port *msm_port, unsigned int count)
int ret;
u32 val;
+ msm_port->tx_dma_flushed = false;
sg_init_table(&dma->tx_sg, 1);
kfifo_dma_out_prepare(&tport->xmit_fifo, &dma->tx_sg, 1, count);
@@ -1549,6 +1565,7 @@ static const struct uart_ops msm_uart_pops = {
.set_mctrl = msm_set_mctrl,
.get_mctrl = msm_get_mctrl,
.stop_tx = msm_stop_tx,
+ .flush_buffer = msm_flush_buffer,
.start_tx = msm_start_tx,
.stop_rx = msm_stop_rx,
.enable_ms = msm_enable_ms,
--
2.34.1
More information about the linux-arm-kernel
mailing list