]> git.itanic.dy.fi Git - linux-stable/commitdiff
serial: 8250: Fix serial8250_tx_empty() race with DMA Tx
authorIlpo Järvinen <ilpo.jarvinen@linux.intel.com>
Thu, 11 May 2023 12:32:44 +0000 (15:32 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 May 2023 09:11:51 +0000 (11:11 +0200)
There's a potential race before THRE/TEMT deasserts when DMA Tx is
starting up (or the next batch of continuous Tx is being submitted).
This can lead to misdetecting Tx empty condition.

It is entirely normal for THRE/TEMT to be set for some time after the
DMA Tx had been setup in serial8250_tx_dma(). As Tx side is definitely
not empty at that point, it seems incorrect for serial8250_tx_empty()
claim Tx is empty.

Fix the race by also checking in serial8250_tx_empty() whether there's
DMA Tx active.

Note: This fix only addresses in-kernel race mainly to make using
TCSADRAIN/FLUSH robust. Userspace can still cause other races but they
seem userspace concurrency control problems.

Fixes: 9ee4b83e51f74 ("serial: 8250: Add support for dmaengine")
Cc: stable@vger.kernel.org
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20230317113318.31327-3-ilpo.jarvinen@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
(cherry picked from commit 146a37e05d620cef4ad430e5d1c9c077fe6fa76f)
Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/tty/serial/8250/8250.h
drivers/tty/serial/8250/8250_port.c

index b2bdc35f74955fe134606ad9103811d227de8abf..6e8a54eb98427053c648ea932a0c7e4658065206 100644 (file)
@@ -221,6 +221,13 @@ extern int serial8250_rx_dma(struct uart_8250_port *);
 extern void serial8250_rx_dma_flush(struct uart_8250_port *);
 extern int serial8250_request_dma(struct uart_8250_port *);
 extern void serial8250_release_dma(struct uart_8250_port *);
+
+static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
+{
+       struct uart_8250_dma *dma = p->dma;
+
+       return dma && dma->tx_running;
+}
 #else
 static inline int serial8250_tx_dma(struct uart_8250_port *p)
 {
@@ -236,6 +243,11 @@ static inline int serial8250_request_dma(struct uart_8250_port *p)
        return -1;
 }
 static inline void serial8250_release_dma(struct uart_8250_port *p) { }
+
+static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
+{
+       return false;
+}
 #endif
 
 static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
index cdc1b2b0f4bc6a6a65a015a43ac2cb49df27bf8a..78af258111e580305d81febca852e32fd1f1d257 100644 (file)
@@ -1968,19 +1968,25 @@ static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
 static unsigned int serial8250_tx_empty(struct uart_port *port)
 {
        struct uart_8250_port *up = up_to_u8250p(port);
+       unsigned int result = 0;
        unsigned long flags;
        unsigned int lsr;
 
        serial8250_rpm_get(up);
 
        spin_lock_irqsave(&port->lock, flags);
-       lsr = serial_port_in(port, UART_LSR);
-       up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
+       if (!serial8250_tx_dma_running(up)) {
+               lsr = serial_port_in(port, UART_LSR);
+               up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
+
+               if ((lsr & BOTH_EMPTY) == BOTH_EMPTY)
+                       result = TIOCSER_TEMT;
+       }
        spin_unlock_irqrestore(&port->lock, flags);
 
        serial8250_rpm_put(up);
 
-       return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
+       return result;
 }
 
 unsigned int serial8250_do_get_mctrl(struct uart_port *port)