Skip to contentSkip to conversation

serial: 8250: 8250_omap: Fix race b/w dma completion and RX timeout

PATCH Superseded Vignesh R 2 participants 3 messages Jun 17, 2017 – Jun 19, 2017 TTY layer and serial drivers

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Vignesh R

author
#0 lore raw

PATCH 1/1 serial: 8250: 8250_omap: Fix race b/w dma completion and RX timeout

DMA RX completion handler for UART is called from a tasklet and hence may be delayed depending on the system load. In meanwhile, there may be RX timeout interrupt which can get serviced first before DMA RX completion handler is executed for the completed transfer. omap_8250_rx_dma_flush() which is called on RX timeout interrupt makes sure that the DMA RX buffer is pushed and then the FIFO is drained and also queues a new DMA request. But, when DMA RX completion handler executes, it will erroneously flush the currently queued DMA transfer which sometimes results in data corruption and double queueing of DMA RX requests.

Fix this by checking whether RX completion is for the currently queued transfer or not. And also hold port lock when in DMA completion to avoid race wrt RX timeout handler preempting it.

---

Tested on AM335x, AM437x, DRA74x EVM with lockdep enabled.

 drivers/tty/serial/8250/8250_omap.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_omap.c +19 -2
index d81bac98d190..f418808a2062 100644
@@ -786,8 +786,25 @@ static void __dma_rx_do_complete(struct uart_8250_port *p)
 
 static void __dma_rx_complete(void *param)
 {
-	__dma_rx_do_complete(param);
-	omap_8250_rx_dma(param);
+	struct uart_8250_port *p = param;
+	struct uart_8250_dma *dma = p->dma;
+	unsigned long flags;
+
+	spin_lock_irqsave(&p->port.lock, flags);
+
+	/*
+	 * If the completion is for the current cookie then handle it,
+	 * else a previous RX timeout flush would have already pushed
+	 * data from DMA buffers, so exit.
+	 */
+	if (dma->rx_cookie != dma->rxchan->completed_cookie) {
+		spin_unlock_irqrestore(&p->port.lock, flags);
+		return;
+	}
+	__dma_rx_do_complete(p);
+	omap_8250_rx_dma(p);
+
+	spin_unlock_irqrestore(&p->port.lock, flags);
 }
 
 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)
Signature · 1 line
2.13.0
#1 lore raw
Quoted 32 lines · Vignesh R

DMA RX completion handler for UART is called from a tasklet and hence may be delayed depending on the system load. In meanwhile, there may be
RX timeout interrupt which can get serviced first before DMA RX completion handler is executed for the completed transfer. omap_8250_rx_dma_flush() which is called on RX timeout interrupt makes sure that the DMA RX buffer is pushed and then the FIFO is drained and also queues a new DMA request. But, when DMA RX completion handler executes, it will erroneously flush the currently queued DMA transfer which sometimes results in data corruption and double queueing of DMA RX
requests.

Fix this by checking whether RX completion is for the currently queued transfer or not. And also hold port lock when in DMA completion to avoid
race wrt RX timeout handler preempting it.

 static void __dma_rx_complete(void *param)
 {

drivers/tty/serial/8250/8250_omap.c +8 -2
-	__dma_rx_do_complete(param);
-	omap_8250_rx_dma(param);
+	struct uart_8250_port *p = param;
+	struct uart_8250_dma *dma = p->dma;
+	unsigned long flags;
+
+	spin_lock_irqsave(&p->port.lock, flags);
+
+	/*
+	 * If the completion is for the current cookie then handle

it,

drivers/tty/serial/8250/8250_omap.c static void __dma_rx_do_complete(struct uart_8250_port *p)

Quoted 5 lines
drivers/tty/serial/8250/8250_omap.c +4 -0
+	 * else a previous RX timeout flush would have already pushed
+	 * data from DMA buffers, so exit.
+	 */
 
+	if (dma->rx_cookie != dma->rxchan->completed_cookie) {

Wouldn't be better to call DMAEngine API for that? dmaengine_tx_status() I suppose

Quoted 10 lines
drivers/tty/serial/8250/8250_omap.c +7 -0
+		spin_unlock_irqrestore(&p->port.lock, flags);
+		return;
+	}
+	__dma_rx_do_complete(p);
+	omap_8250_rx_dma(p);
+
+	spin_unlock_irqrestore(&p->port.lock, flags);

 }

 static void omap_8250_rx_dma_flush(struct uart_8250_port *p)

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author
#2 lore raw
Quoted 37 lines · Andy Shevchenko
Quoted 35 lines · Vignesh R

DMA RX completion handler for UART is called from a tasklet and hence may be delayed depending on the system load. In meanwhile, there may be
RX timeout interrupt which can get serviced first before DMA RX completion handler is executed for the completed transfer. omap_8250_rx_dma_flush() which is called on RX timeout interrupt makes sure that the DMA RX buffer is pushed and then the FIFO is drained and also queues a new DMA request. But, when DMA RX completion handler executes, it will erroneously flush the currently queued DMA transfer which sometimes results in data corruption and double queueing of DMA RX
requests.

Fix this by checking whether RX completion is for the currently queued transfer or not. And also hold port lock when in DMA completion to avoid
race wrt RX timeout handler preempting it.

static void __dma_rx_complete(void *param)
{

drivers/tty/serial/8250/8250_omap.c +8 -2
-	__dma_rx_do_complete(param);
-	omap_8250_rx_dma(param);
+	struct uart_8250_port *p = param;
+	struct uart_8250_dma *dma = p->dma;
+	unsigned long flags;
+
+	spin_lock_irqsave(&p->port.lock, flags);
+
+	/*
+	 * If the completion is for the current cookie then handle

it,

drivers/tty/serial/8250/8250_omap.c +3 -0
+	 * else a previous RX timeout flush would have already pushed
+	 * data from DMA buffers, so exit.
+	 */
Quoted 4 lines

drivers/tty/serial/8250/8250_omap.c static void __dma_rx_do_complete(struct uart_8250_port *p) · line 800

Quoted 1 line
drivers/tty/serial/8250/8250_omap.c +1 -0
+	if (dma->rx_cookie != dma->rxchan->completed_cookie) {

Wouldn't be better to call DMAEngine API for that? dmaengine_tx_status() I suppose

Yeah, will update the patch. Thanks!

Signature · 2 lines
Regards
Vignesh