stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/3] serial: stm32: fix threaded interrupt handling
       [not found] <20210416140557.25177-1-johan@kernel.org>
@ 2021-04-16 14:05 ` Johan Hovold
  2021-04-16 14:19   ` Johan Hovold
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Johan Hovold @ 2021-04-16 14:05 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Jiri Slaby, Maxime Coquelin, Alexandre Torgue, dillon.minfei,
	Erwan Le Ray, linux-serial, linux-kernel, Johan Hovold, stable,
	Alexandre TORGUE, Gerald Baeza

When DMA is enabled the receive handler runs in a threaded handler, but
the primary handler up until very recently neither disabled interrupts
in the device or used IRQF_ONESHOT. This would lead to a deadlock if an
interrupt comes in while the threaded receive handler is running under
the port lock.

Commit ad7676812437 ("serial: stm32: fix a deadlock condition with
wakeup event") claimed to fix an unrelated deadlock, but unfortunately
also disabled interrupts in the threaded handler. While this prevents
the deadlock mentioned in the previous paragraph it also defeats the
purpose of using a threaded handler in the first place.

Fix this by making the interrupt one-shot and not disabling interrupts
in the threaded handler.

Note that (receive) DMA must not be used for a console port as the
threaded handler could be interrupted while holding the port lock,
something which could lead to a deadlock in case an interrupt handler
ends up calling printk.

Fixes: ad7676812437 ("serial: stm32: fix a deadlock condition with wakeup event")
Fixes: 3489187204eb ("serial: stm32: adding dma support")
Cc: stable@vger.kernel.org      # 4.9
Cc: Alexandre TORGUE <alexandre.torgue@st.com>
Cc: Gerald Baeza <gerald.baeza@st.com>
Signed-off-by: Johan Hovold <johan@kernel.org>
---
 drivers/tty/serial/stm32-usart.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c
index 4d277804c63e..3524ed2c0c73 100644
--- a/drivers/tty/serial/stm32-usart.c
+++ b/drivers/tty/serial/stm32-usart.c
@@ -214,14 +214,11 @@ static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
 	struct tty_port *tport = &port->state->port;
 	struct stm32_port *stm32_port = to_stm32_port(port);
 	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
-	unsigned long c, flags;
+	unsigned long c;
 	u32 sr;
 	char flag;
 
-	if (threaded)
-		spin_lock_irqsave(&port->lock, flags);
-	else
-		spin_lock(&port->lock);
+	spin_lock(&port->lock);
 
 	while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res,
 				      threaded)) {
@@ -278,10 +275,7 @@ static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
 		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
 	}
 
-	if (threaded)
-		spin_unlock_irqrestore(&port->lock, flags);
-	else
-		spin_unlock(&port->lock);
+	spin_unlock(&port->lock);
 
 	tty_flip_buffer_push(tport);
 }
@@ -667,7 +661,8 @@ static int stm32_usart_startup(struct uart_port *port)
 
 	ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
 				   stm32_usart_threaded_interrupt,
-				   IRQF_NO_SUSPEND, name, port);
+				   IRQF_ONESHOT | IRQF_NO_SUSPEND,
+				   name, port);
 	if (ret)
 		return ret;
 
@@ -1156,6 +1151,13 @@ static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
 	struct dma_async_tx_descriptor *desc = NULL;
 	int ret;
 
+	/*
+	 * Using DMA and threaded handler for the console could lead to
+	 * deadlocks.
+	 */
+	if (uart_console(port))
+		return -ENODEV;
+
 	/* Request DMA RX channel */
 	stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
 	if (!stm32port->rx_ch) {
-- 
2.26.3


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

* Re: [PATCH 2/3] serial: stm32: fix threaded interrupt handling
  2021-04-16 14:05 ` [PATCH 2/3] serial: stm32: fix threaded interrupt handling Johan Hovold
@ 2021-04-16 14:19   ` Johan Hovold
       [not found]   ` <CAL9mu0KwgOFQfa8ft4rB6+F=KLd1gZLYDvwpAW72zPAFntehVw@mail.gmail.com>
  2021-04-22  9:34   ` Valentin CARON - foss
  2 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2021-04-16 14:19 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Jiri Slaby, Maxime Coquelin, Alexandre Torgue, dillon.minfei,
	Erwan Le Ray, linux-serial, linux-kernel, stable,
	Alexandre TORGUE, Gerald Baeza

On Fri, Apr 16, 2021 at 04:05:56PM +0200, Johan Hovold wrote:
> When DMA is enabled the receive handler runs in a threaded handler, but
> the primary handler up until very recently neither disabled interrupts

Scratch the "up until very recently" bit here since the driver still
doesn't disable interrupt in the device (it just disables all interrupts
in the threaded handler). The rest stands as is.

> in the device or used IRQF_ONESHOT. This would lead to a deadlock if an
> interrupt comes in while the threaded receive handler is running under
> the port lock.
> 
> Commit ad7676812437 ("serial: stm32: fix a deadlock condition with
> wakeup event") claimed to fix an unrelated deadlock, but unfortunately
> also disabled interrupts in the threaded handler. While this prevents
> the deadlock mentioned in the previous paragraph it also defeats the
> purpose of using a threaded handler in the first place.
> 
> Fix this by making the interrupt one-shot and not disabling interrupts
> in the threaded handler.
> 
> Note that (receive) DMA must not be used for a console port as the
> threaded handler could be interrupted while holding the port lock,
> something which could lead to a deadlock in case an interrupt handler
> ends up calling printk.

Johan

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

* Re: [PATCH 2/3] serial: stm32: fix threaded interrupt handling
       [not found]   ` <CAL9mu0KwgOFQfa8ft4rB6+F=KLd1gZLYDvwpAW72zPAFntehVw@mail.gmail.com>
@ 2021-04-16 14:46     ` Johan Hovold
  0 siblings, 0 replies; 4+ messages in thread
From: Johan Hovold @ 2021-04-16 14:46 UTC (permalink / raw)
  To: dillon min
  Cc: Alexandre TORGUE, Alexandre Torgue, Erwan Le Ray, Gerald Baeza,
	Greg Kroah-Hartman, Jiri Slaby, Maxime Coquelin, linux-kernel,
	linux-serial, stable

On Fri, Apr 16, 2021 at 10:35:25PM +0800, dillon min wrote:
> Hi Johan
> 
> Thanks for share your patch.
> 
> Johan Hovold <johan@kernel.org>于2021年4月16日 周五22:11写道:
> 
> > When DMA is enabled the receive handler runs in a threaded handler, but
> > the primary handler up until very recently neither disabled interrupts
> > in the device or used IRQF_ONESHOT. This would lead to a deadlock if an
> > interrupt comes in while the threaded receive handler is running under
> > the port lock.
> >
> Greg told me there was a patch fixed this case. In case hard irq &
> threaded_fn both offered. The local_irq_save() will be executed before call
> driver’s threaded handler.
> 
> Post the original mail from Greg
> 
> Please see 81e2073c175b ("genirq: Disable interrupts for force threaded
> handlers") for when threaded irq handlers have irqs disabled, isn't that
> the case you are trying to "protect" from here?
> 
> Why is the "threaded" flag used at all?  The driver should not care.
> 
> Also see 9baedb7baeda ("serial: imx: drop workaround for forced irq
> threading") in linux-next for an example of how this was fixed up in a
> serial driver.

Neither of these commits are (directly) related to the problem this
patch addresses (they are about force-threaded handlers, this is about a
normal threaded handler which run with interrupts enabled).

Johan

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

* Re: [PATCH 2/3] serial: stm32: fix threaded interrupt handling
  2021-04-16 14:05 ` [PATCH 2/3] serial: stm32: fix threaded interrupt handling Johan Hovold
  2021-04-16 14:19   ` Johan Hovold
       [not found]   ` <CAL9mu0KwgOFQfa8ft4rB6+F=KLd1gZLYDvwpAW72zPAFntehVw@mail.gmail.com>
@ 2021-04-22  9:34   ` Valentin CARON - foss
  2 siblings, 0 replies; 4+ messages in thread
From: Valentin CARON - foss @ 2021-04-22  9:34 UTC (permalink / raw)
  To: Johan Hovold, Greg Kroah-Hartman
  Cc: Jiri Slaby, Maxime Coquelin, Alexandre TORGUE - foss,
	dillon.minfei, Erwan LE-RAY - foss, linux-serial, linux-kernel,
	stable, Alexandre TORGUE, Gerald BAEZA

Hi, Johan

On 4/16/21 4:05 PM, Johan Hovold wrote:
> When DMA is enabled the receive handler runs in a threaded handler, but
> the primary handler up until very recently neither disabled interrupts
> in the device or used IRQF_ONESHOT. This would lead to a deadlock if an
> interrupt comes in while the threaded receive handler is running under
> the port lock.
>
> Commit ad7676812437 ("serial: stm32: fix a deadlock condition with
> wakeup event") claimed to fix an unrelated deadlock, but unfortunately
> also disabled interrupts in the threaded handler. While this prevents
> the deadlock mentioned in the previous paragraph it also defeats the
> purpose of using a threaded handler in the first place.
>
> Fix this by making the interrupt one-shot and not disabling interrupts
> in the threaded handler.
>
> Note that (receive) DMA must not be used for a console port as the
> threaded handler could be interrupted while holding the port lock,
> something which could lead to a deadlock in case an interrupt handler
> ends up calling printk.
>
> Fixes: ad7676812437 ("serial: stm32: fix a deadlock condition with wakeup event")
> Fixes: 3489187204eb ("serial: stm32: adding dma support")
> Cc: stable@vger.kernel.org      # 4.9
> Cc: Alexandre TORGUE <alexandre.torgue@st.com>
> Cc: Gerald Baeza <gerald.baeza@st.com>
> Signed-off-by: Johan Hovold <johan@kernel.org>

Reviewed-by: Valentin Caron<valentin.caron@foss.st.com>

> ---
>   drivers/tty/serial/stm32-usart.c | 22 ++++++++++++----------
>   1 file changed, 12 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c
> index 4d277804c63e..3524ed2c0c73 100644
> --- a/drivers/tty/serial/stm32-usart.c
> +++ b/drivers/tty/serial/stm32-usart.c
> @@ -214,14 +214,11 @@ static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
>   	struct tty_port *tport = &port->state->port;
>   	struct stm32_port *stm32_port = to_stm32_port(port);
>   	const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
> -	unsigned long c, flags;
> +	unsigned long c;
>   	u32 sr;
>   	char flag;
>   
> -	if (threaded)
> -		spin_lock_irqsave(&port->lock, flags);
> -	else
> -		spin_lock(&port->lock);
> +	spin_lock(&port->lock);
>   
>   	while (stm32_usart_pending_rx(port, &sr, &stm32_port->last_res,
>   				      threaded)) {
> @@ -278,10 +275,7 @@ static void stm32_usart_receive_chars(struct uart_port *port, bool threaded)
>   		uart_insert_char(port, sr, USART_SR_ORE, c, flag);
>   	}
>   
> -	if (threaded)
> -		spin_unlock_irqrestore(&port->lock, flags);
> -	else
> -		spin_unlock(&port->lock);
> +	spin_unlock(&port->lock);
>   
>   	tty_flip_buffer_push(tport);
>   }
> @@ -667,7 +661,8 @@ static int stm32_usart_startup(struct uart_port *port)
>   
>   	ret = request_threaded_irq(port->irq, stm32_usart_interrupt,
>   				   stm32_usart_threaded_interrupt,
> -				   IRQF_NO_SUSPEND, name, port);
> +				   IRQF_ONESHOT | IRQF_NO_SUSPEND,
> +				   name, port);
>   	if (ret)
>   		return ret;
>   
> @@ -1156,6 +1151,13 @@ static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
>   	struct dma_async_tx_descriptor *desc = NULL;
>   	int ret;
>   
> +	/*
> +	 * Using DMA and threaded handler for the console could lead to
> +	 * deadlocks.
> +	 */
> +	if (uart_console(port))
> +		return -ENODEV;
> +
>   	/* Request DMA RX channel */
>   	stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
>   	if (!stm32port->rx_ch) {

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

end of thread, other threads:[~2021-04-22  9:35 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20210416140557.25177-1-johan@kernel.org>
2021-04-16 14:05 ` [PATCH 2/3] serial: stm32: fix threaded interrupt handling Johan Hovold
2021-04-16 14:19   ` Johan Hovold
     [not found]   ` <CAL9mu0KwgOFQfa8ft4rB6+F=KLd1gZLYDvwpAW72zPAFntehVw@mail.gmail.com>
2021-04-16 14:46     ` Johan Hovold
2021-04-22  9:34   ` Valentin CARON - foss

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).