serial: imx: Fix potential deadlock on sport->port.lock
Subsystems (3)
PATCH RESEND 1/1 serial: imx: Fix potential deadlock on sport->port.lock
As &sport->port.lock is acquired under irq context along the following call chain from imx_uart_rtsint(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock.
<deadlock #1>
imx_uart_dma_rx_callback()
--> spin_lock(&sport->port.lock)
<interrupt>
--> imx_uart_rtsint()
--> spin_lock(&sport->port.lock)
This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.
To prevent the potential deadlock, the patch uses spin_lock_irqsave() on the &sport->port.lock inside imx_uart_dma_rx_callback() to prevent the possible deadlock scenario.
--- drivers/tty/serial/imx.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/tty/serial/imx.c +5 -4
@@ -1165,13 +1165,14 @@ static void imx_uart_dma_rx_callback(void *data)unsigned int w_bytes =0 ;unsigned int r_bytes;unsigned int bd_size; +unsigned long flags; status =dmaengine_tx_status (chan, sport->rx_cookie, &state);if (status ==DMA_ERROR ) { -spin_lock (&sport->port.lock); +spin_lock_irqsave (&sport->port.lock, flags);imx_uart_clear_rx_errors (sport); -spin_unlock (&sport->port.lock); +spin_unlock_irqrestore (&sport->port.lock, flags);return ; }
@@ -1200,9 +1201,9 @@ static void imx_uart_dma_rx_callback(void *data) r_bytes = rx_ring->head - rx_ring->tail;/* If we received something, check for 0xff flood */ -spin_lock (&sport->port.lock); +spin_lock_irqsave (&sport->port.lock, flags);imx_uart_check_flood (sport,imx_uart_readl (sport,USR2 )); -spin_unlock (&sport->port.lock); +spin_unlock_irqrestore (&sport->port.lock, flags);if (!(sport->port.ignore_status_mask &URXD_DUMMY_READ )) {
Signature · 1 line
2.17.1
[Cc += Vinod Koul, [email protected]]
Hello,
Quoted 7 lines · Chengfeng Ye
As
&sport->port.lockis acquired under irq context along the following call chain fromimx_uart_rtsint(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock.<deadlock #1>
Quoted 8 lines
imx_uart_dma_rx_callback()
-->spin_lock(&sport->port.lock)
<interrupt>
-->imx_uart_rtsint()
-->spin_lock(&sport->port.lock)This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.
Ah, I understood before that you really experienced that deadlock (or a lockdep splat). I didn't test anything, but I think the imx_uart_dma_rx_callback() is called indirectly by sdma_update_channel_loop() which is called in irq context. I don't know if this is the case for all dma drivers?!
@Vinod: Maybe you can chime in here: Is a dma callback always called in irq context?
If yes, this patch isn't needed. Otherwise it might be a good idea to not use the special knowledge and switch to spin_lock_irqsave() as suggested.
Quoted 5 lines
To prevent the potential deadlock, the patch uses
spin_lock_irqsave()on the&sport->port.lockinsideimx_uart_dma_rx_callback()to prevent the possible deadlock scenario.Signed-off-by Chengfeng Ye <[email protected]>
If we agree this patch is a good idea, we can add:
Thanks
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Vinod Koul
maintainerQuoted 21 lines · Uwe Kleine-König
[Cc += Vinod Koul, [email protected]]
Hello,
Quoted 7 lines · Chengfeng Ye
As
&sport->port.lockis acquired under irq context along the following call chain fromimx_uart_rtsint(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock.<deadlock #1>
Quoted 8 lines
imx_uart_dma_rx_callback()
-->spin_lock(&sport->port.lock)
<interrupt>
-->imx_uart_rtsint()
-->spin_lock(&sport->port.lock)This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.
Quoted 8 lines
Ah, I understood before that you really experienced that deadlock (or a lockdep splat). I didn't test anything, but I think the
imx_uart_dma_rx_callback()is called indirectly bysdma_update_channel_loop()which is called in irq context. I don't know if this is the case for all dma drivers?!@Vinod: Maybe you can chime in here: Is a dma callback always called in irq context?
Not in callback but a tasklet context. The DMA irq handler is supposed to use a tasklet for invoking the callback
Quoted 20 lines
If yes, this patch isn't needed. Otherwise it might be a good idea to not use the special knowledge and switch to
spin_lock_irqsave()as suggested.Quoted 5 lines
To prevent the potential deadlock, the patch uses
spin_lock_irqsave()on the&sport->port.lockinsideimx_uart_dma_rx_callback()to prevent the possible deadlock scenario.Signed-off-by Chengfeng Ye <[email protected]>If we agree this patch is a good idea, we can add:
Fixes 496a4471b7c3 ("serial: imx: work-around for hardware RX flood")Thanks
UweSignature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Signature · 1 line
~Vinod
Uwe Kleine-König
Acked-byHello Vinod,
thanks for your quick answer!
Quoted 28 lines · Vinod Koul
Quoted 27 lines · Uwe Kleine-König
[Cc += Vinod Koul, [email protected]]
Hello,
Quoted 7 lines · Chengfeng Ye
As
&sport->port.lockis acquired under irq context along the following call chain fromimx_uart_rtsint(), other acquisition of the same lock inside process context or softirq context should disable irq avoid double lock.<deadlock #1>
Quoted 8 lines
imx_uart_dma_rx_callback()
-->spin_lock(&sport->port.lock)
<interrupt>
-->imx_uart_rtsint()
-->spin_lock(&sport->port.lock)This flaw was found by an experimental static analysis tool I am developing for irq-related deadlock.
Ah, I understood before that you really experienced that deadlock (or a lockdep splat). I didn't test anything, but I think the
imx_uart_dma_rx_callback()is called indirectly bysdma_update_channel_loop()which is called in irq context. I don't know if this is the case for all dma drivers?!
Quoted 5 lines
Quoted 2 lines
@Vinod: Maybe you can chime in here: Is a dma callback always called in irq context?
Not in callback but a tasklet context. The DMA irq handler is supposed to use a tasklet for invoking the callback
So drivers/dma/imx-sdma.c is bogous as it calls
-> sdma_int_handler() -> sdma_update_channel_loop() -> dmaengine_desc_get_callback_invoke()
resulting in imx_uart_dma_rx_callback() (and others) being called in irq context, right?
In that case:
(for the imx-UART patch that stops assuming imx_uart_dma_rx_callback() is called with irqs off).
Best regards
Uwe
Signature · 2 lines
Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |