All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
@ 2016-12-02 10:28 Nikita Yushchenko
  2016-12-02 18:05 ` Stefan Agner
  0 siblings, 1 reply; 8+ messages in thread
From: Nikita Yushchenko @ 2016-12-02 10:28 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Bhuvanchandra DV, Stefan Agner,
	Wei Yongjun, Aaron Brice, Nicolae Rosia, linux-serial
  Cc: Chris Healy, linux-kernel, Nikita Yushchenko

Problem found via lockdep:

- lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
  holding sport->port.lock

- sport->lpuart_timer routine is lpuart_timer_func() that calls
  lpuart_copy_rx_to_tty() that acquires same lock.

To fix, move Rx DMA stopping out of lock, as it already is in other places
in the same file.

While at it, also make Rx DMA start/stop code to look the same is in
other places in the same file.

Signed-off-by: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
---
 drivers/tty/serial/fsl_lpuart.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
index a1c6519837a4..81100c40f33b 100644
--- a/drivers/tty/serial/fsl_lpuart.c
+++ b/drivers/tty/serial/fsl_lpuart.c
@@ -1407,6 +1407,18 @@ lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
 	/* ask the core to calculate the divisor */
 	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
 
+	/*
+	 * Need to update the Ring buffer length according to the selected
+	 * baud rate and restart Rx DMA path.
+	 *
+	 * Since timer function acqures sport->port.lock, need to stop before
+	 * acquring same lock because otherwise del_timer_sync() can deadlock.
+	 */
+	if (sport->lpuart_dma_rx_use) {
+		del_timer_sync(&sport->lpuart_timer);
+		lpuart_dma_rx_free(&sport->port);
+	}
+
 	spin_lock_irqsave(&sport->port.lock, flags);
 
 	sport->port.read_status_mask = 0;
@@ -1456,17 +1468,8 @@ lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
 	/* restore control register */
 	writeb(old_cr2, sport->port.membase + UARTCR2);
 
-	/*
-	 * If new baud rate is set, we will also need to update the Ring buffer
-	 * length according to the selected baud rate and restart Rx DMA path.
-	 */
-	if (old) {
-		if (sport->lpuart_dma_rx_use) {
-			del_timer_sync(&sport->lpuart_timer);
-			lpuart_dma_rx_free(&sport->port);
-		}
-
-		if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
+	if (sport->lpuart_dma_rx_use) {
+		if (!lpuart_start_rx_dma(sport)) {
 			sport->lpuart_dma_rx_use = true;
 			rx_dma_timer_init(sport);
 		} else {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
  2016-12-02 10:28 [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock Nikita Yushchenko
@ 2016-12-02 18:05 ` Stefan Agner
  2016-12-02 21:28   ` Nikita Yushchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Stefan Agner @ 2016-12-02 18:05 UTC (permalink / raw)
  To: Nikita Yushchenko
  Cc: Greg Kroah-Hartman, Jiri Slaby, Bhuvanchandra DV, Wei Yongjun,
	Aaron Brice, Nicolae Rosia, linux-serial, Chris Healy,
	linux-kernel

On 2016-12-02 02:28, Nikita Yushchenko wrote:
> Problem found via lockdep:
> 
> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>   holding sport->port.lock
> 
> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>   lpuart_copy_rx_to_tty() that acquires same lock.
> 
> To fix, move Rx DMA stopping out of lock, as it already is in other places
> in the same file.
> 
> While at it, also make Rx DMA start/stop code to look the same is in
> other places in the same file.

Yeah I saw that too, never really came around to look closer into it.

Thanks for looking into it.

You removed the check whether there was an old configuration, I think
the idea of that was that we only resize DMA if it was configured
differently before... Not sure how startup/set_termios calls are
ordered. I guess in practice that shouldn't really make a difference
since lpuart_dma_rx_use can't be true until after startup?

One nit below.

--
Stefan

> 
> Signed-off-by: Nikita Yushchenko <nikita.yoush@cogentembedded.com>
> ---
>  drivers/tty/serial/fsl_lpuart.c | 25 ++++++++++++++-----------
>  1 file changed, 14 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/tty/serial/fsl_lpuart.c b/drivers/tty/serial/fsl_lpuart.c
> index a1c6519837a4..81100c40f33b 100644
> --- a/drivers/tty/serial/fsl_lpuart.c
> +++ b/drivers/tty/serial/fsl_lpuart.c
> @@ -1407,6 +1407,18 @@ lpuart_set_termios(struct uart_port *port,
> struct ktermios *termios,
>  	/* ask the core to calculate the divisor */
>  	baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
>  
> +	/*
> +	 * Need to update the Ring buffer length according to the selected
> +	 * baud rate and restart Rx DMA path.
> +	 *
> +	 * Since timer function acqures sport->port.lock, need to stop before
> +	 * acquring same lock because otherwise del_timer_sync() can deadlock.
> +	 */
> +	if (sport->lpuart_dma_rx_use) {
> +		del_timer_sync(&sport->lpuart_timer);
> +		lpuart_dma_rx_free(&sport->port);
> +	}
> +
>  	spin_lock_irqsave(&sport->port.lock, flags);
>  
>  	sport->port.read_status_mask = 0;
> @@ -1456,17 +1468,8 @@ lpuart_set_termios(struct uart_port *port,
> struct ktermios *termios,
>  	/* restore control register */
>  	writeb(old_cr2, sport->port.membase + UARTCR2);
>  
> -	/*
> -	 * If new baud rate is set, we will also need to update the Ring buffer
> -	 * length according to the selected baud rate and restart Rx DMA path.
> -	 */
> -	if (old) {
> -		if (sport->lpuart_dma_rx_use) {
> -			del_timer_sync(&sport->lpuart_timer);
> -			lpuart_dma_rx_free(&sport->port);
> -		}
> -
> -		if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
> +	if (sport->lpuart_dma_rx_use) {
> +		if (!lpuart_start_rx_dma(sport)) {
>  			sport->lpuart_dma_rx_use = true;

No need to set to true here, it is guaranteed to be true at this point.

>  			rx_dma_timer_init(sport);
>  		} else {

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
  2016-12-02 18:05 ` Stefan Agner
@ 2016-12-02 21:28   ` Nikita Yushchenko
  2016-12-03  7:06       ` Bhuvanchandra DV
  0 siblings, 1 reply; 8+ messages in thread
From: Nikita Yushchenko @ 2016-12-02 21:28 UTC (permalink / raw)
  To: Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Bhuvanchandra DV, Wei Yongjun,
	Aaron Brice, Nicolae Rosia, linux-serial, Chris Healy,
	linux-kernel

>> Problem found via lockdep:
>>
>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>   holding sport->port.lock
>>
>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>   lpuart_copy_rx_to_tty() that acquires same lock.
>>
>> To fix, move Rx DMA stopping out of lock, as it already is in other places
>> in the same file.
>>
>> While at it, also make Rx DMA start/stop code to look the same is in
>> other places in the same file.
> 
> Yeah I saw that too, never really came around to look closer into it.
> 
> Thanks for looking into it.
> 
> You removed the check whether there was an old configuration, I think
> the idea of that was that we only resize DMA if it was configured
> differently before...

Per my code reading, checking for sport->lpuart_dma_rx_use should be
enough, this flag will be set only if DMA was previously enabled,

>> +	if (sport->lpuart_dma_rx_use) {
>> +		if (!lpuart_start_rx_dma(sport)) {
>>  			sport->lpuart_dma_rx_use = true;
> 
> No need to set to true here, it is guaranteed to be true at this point.

I've seen this...  However elsewhere in this file (namely in
lpuart_resume(), in very similar situation, code is exactly the same,
i.e. it sets sport->lpuart_dma_rx_use in both clauses. I thought it
could be for a reason (i.e. for readability).

Nikita

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
  2016-12-02 21:28   ` Nikita Yushchenko
@ 2016-12-03  7:06       ` Bhuvanchandra DV
  0 siblings, 0 replies; 8+ messages in thread
From: Bhuvanchandra DV @ 2016-12-03  7:06 UTC (permalink / raw)
  To: Nikita Yushchenko, Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Wei Yongjun, Aaron Brice,
	Nicolae Rosia, linux-serial, Chris Healy, linux-kernel

On 12/03/2016 02:58 AM, Nikita Yushchenko wrote:

>>> Problem found via lockdep:
>>>
>>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>>    holding sport->port.lock
>>>
>>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>>    lpuart_copy_rx_to_tty() that acquires same lock.
>>>
>>> To fix, move Rx DMA stopping out of lock, as it already is in other places
>>> in the same file.
>>>
>>> While at it, also make Rx DMA start/stop code to look the same is in
>>> other places in the same file.
>> Yeah I saw that too, never really came around to look closer into it.
>>
>> Thanks for looking into it.
>>
>> You removed the check whether there was an old configuration, I think
>> the idea of that was that we only resize DMA if it was configured
>> differently before...
> Per my code reading, checking for sport->lpuart_dma_rx_use should be
> enough, this flag will be set only if DMA was previously enabled,

The check is to make sure the reconfiguration of DMA is done only when
the baudrate is altered.

--
Bhuvan

>
>>> +	if (sport->lpuart_dma_rx_use) {
>>> +		if (!lpuart_start_rx_dma(sport)) {
>>>   			sport->lpuart_dma_rx_use = true;
>> No need to set to true here, it is guaranteed to be true at this point.
> I've seen this...  However elsewhere in this file (namely in
> lpuart_resume(), in very similar situation, code is exactly the same,
> i.e. it sets sport->lpuart_dma_rx_use in both clauses. I thought it
> could be for a reason (i.e. for readability).
>
> Nikita

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
@ 2016-12-03  7:06       ` Bhuvanchandra DV
  0 siblings, 0 replies; 8+ messages in thread
From: Bhuvanchandra DV @ 2016-12-03  7:06 UTC (permalink / raw)
  To: Nikita Yushchenko, Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Wei Yongjun, Aaron Brice,
	Nicolae Rosia, linux-serial, Chris Healy, linux-kernel

On 12/03/2016 02:58 AM, Nikita Yushchenko wrote:

>>> Problem found via lockdep:
>>>
>>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>>    holding sport->port.lock
>>>
>>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>>    lpuart_copy_rx_to_tty() that acquires same lock.
>>>
>>> To fix, move Rx DMA stopping out of lock, as it already is in other places
>>> in the same file.
>>>
>>> While at it, also make Rx DMA start/stop code to look the same is in
>>> other places in the same file.
>> Yeah I saw that too, never really came around to look closer into it.
>>
>> Thanks for looking into it.
>>
>> You removed the check whether there was an old configuration, I think
>> the idea of that was that we only resize DMA if it was configured
>> differently before...
> Per my code reading, checking for sport->lpuart_dma_rx_use should be
> enough, this flag will be set only if DMA was previously enabled,

The check is to make sure the reconfiguration of DMA is done only when
the baudrate is altered.

--
Bhuvan

>
>>> +	if (sport->lpuart_dma_rx_use) {
>>> +		if (!lpuart_start_rx_dma(sport)) {
>>>   			sport->lpuart_dma_rx_use = true;
>> No need to set to true here, it is guaranteed to be true at this point.
> I've seen this...  However elsewhere in this file (namely in
> lpuart_resume(), in very similar situation, code is exactly the same,
> i.e. it sets sport->lpuart_dma_rx_use in both clauses. I thought it
> could be for a reason (i.e. for readability).
>
> Nikita

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
  2016-12-03  7:06       ` Bhuvanchandra DV
  (?)
@ 2016-12-03  8:55       ` Nikita Yushchenko
  2016-12-03 10:06           ` Bhuvanchandra DV
  -1 siblings, 1 reply; 8+ messages in thread
From: Nikita Yushchenko @ 2016-12-03  8:55 UTC (permalink / raw)
  To: Bhuvanchandra DV, Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Wei Yongjun, Aaron Brice,
	Nicolae Rosia, linux-serial, Chris Healy, linux-kernel



03.12.2016 10:06, Bhuvanchandra DV пишет:
> On 12/03/2016 02:58 AM, Nikita Yushchenko wrote:
> 
>>>> Problem found via lockdep:
>>>>
>>>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>>>    holding sport->port.lock
>>>>
>>>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>>>    lpuart_copy_rx_to_tty() that acquires same lock.
>>>>
>>>> To fix, move Rx DMA stopping out of lock, as it already is in other
>>>> places
>>>> in the same file.
>>>>
>>>> While at it, also make Rx DMA start/stop code to look the same is in
>>>> other places in the same file.
>>> Yeah I saw that too, never really came around to look closer into it.
>>>
>>> Thanks for looking into it.
>>>
>>> You removed the check whether there was an old configuration, I think
>>> the idea of that was that we only resize DMA if it was configured
>>> differently before...
>> Per my code reading, checking for sport->lpuart_dma_rx_use should be
>> enough, this flag will be set only if DMA was previously enabled,
> 
> The check is to make sure the reconfiguration of DMA is done only when
> the baudrate is altered.

Then, ok to use

if (old && sport->lpuart_dma_rx_use) {...}

in both places?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
  2016-12-03  8:55       ` Nikita Yushchenko
@ 2016-12-03 10:06           ` Bhuvanchandra DV
  0 siblings, 0 replies; 8+ messages in thread
From: Bhuvanchandra DV @ 2016-12-03 10:06 UTC (permalink / raw)
  To: Nikita Yushchenko, Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Wei Yongjun, Aaron Brice,
	Nicolae Rosia, linux-serial, Chris Healy, linux-kernel

On 12/03/2016 02:25 PM, Nikita Yushchenko wrote:

>
> 03.12.2016 10:06, Bhuvanchandra DV пишет:
>> On 12/03/2016 02:58 AM, Nikita Yushchenko wrote:
>>
>>>>> Problem found via lockdep:
>>>>>
>>>>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>>>>     holding sport->port.lock
>>>>>
>>>>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>>>>     lpuart_copy_rx_to_tty() that acquires same lock.
>>>>>
>>>>> To fix, move Rx DMA stopping out of lock, as it already is in other
>>>>> places
>>>>> in the same file.
>>>>>
>>>>> While at it, also make Rx DMA start/stop code to look the same is in
>>>>> other places in the same file.
>>>> Yeah I saw that too, never really came around to look closer into it.
>>>>
>>>> Thanks for looking into it.
>>>>
>>>> You removed the check whether there was an old configuration, I think
>>>> the idea of that was that we only resize DMA if it was configured
>>>> differently before...
>>> Per my code reading, checking for sport->lpuart_dma_rx_use should be
>>> enough, this flag will be set only if DMA was previously enabled,
>> The check is to make sure the reconfiguration of DMA is done only when
>> the baudrate is altered.
> Then, ok to use
>
> if (old && sport->lpuart_dma_rx_use) {...}
>
> in both places?

Looks ok to me.

--
Bhuvan

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock
@ 2016-12-03 10:06           ` Bhuvanchandra DV
  0 siblings, 0 replies; 8+ messages in thread
From: Bhuvanchandra DV @ 2016-12-03 10:06 UTC (permalink / raw)
  To: Nikita Yushchenko, Stefan Agner
  Cc: Greg Kroah-Hartman, Jiri Slaby, Wei Yongjun, Aaron Brice,
	Nicolae Rosia, linux-serial, Chris Healy, linux-kernel

On 12/03/2016 02:25 PM, Nikita Yushchenko wrote:

>
> 03.12.2016 10:06, Bhuvanchandra DV пишет:
>> On 12/03/2016 02:58 AM, Nikita Yushchenko wrote:
>>
>>>>> Problem found via lockdep:
>>>>>
>>>>> - lpuart_set_termios() calls del_timer_sync(&sport->lpuart_timer) while
>>>>>     holding sport->port.lock
>>>>>
>>>>> - sport->lpuart_timer routine is lpuart_timer_func() that calls
>>>>>     lpuart_copy_rx_to_tty() that acquires same lock.
>>>>>
>>>>> To fix, move Rx DMA stopping out of lock, as it already is in other
>>>>> places
>>>>> in the same file.
>>>>>
>>>>> While at it, also make Rx DMA start/stop code to look the same is in
>>>>> other places in the same file.
>>>> Yeah I saw that too, never really came around to look closer into it.
>>>>
>>>> Thanks for looking into it.
>>>>
>>>> You removed the check whether there was an old configuration, I think
>>>> the idea of that was that we only resize DMA if it was configured
>>>> differently before...
>>> Per my code reading, checking for sport->lpuart_dma_rx_use should be
>>> enough, this flag will be set only if DMA was previously enabled,
>> The check is to make sure the reconfiguration of DMA is done only when
>> the baudrate is altered.
> Then, ok to use
>
> if (old && sport->lpuart_dma_rx_use) {...}
>
> in both places?

Looks ok to me.

--
Bhuvan

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-12-03 22:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-02 10:28 [PATCH] tty: serial: fsl_lpuart: fix del_timer_sync() vs timer routine deadlock Nikita Yushchenko
2016-12-02 18:05 ` Stefan Agner
2016-12-02 21:28   ` Nikita Yushchenko
2016-12-03  7:06     ` Bhuvanchandra DV
2016-12-03  7:06       ` Bhuvanchandra DV
2016-12-03  8:55       ` Nikita Yushchenko
2016-12-03 10:06         ` Bhuvanchandra DV
2016-12-03 10:06           ` Bhuvanchandra DV

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.