linux-serial.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] serial: st-asc: don't get/put GPIOs in atomic context
@ 2024-02-14  9:24 Bartosz Golaszewski
  2024-02-14 16:16 ` Linus Walleij
  2024-02-18 17:59 ` Greg Kroah-Hartman
  0 siblings, 2 replies; 3+ messages in thread
From: Bartosz Golaszewski @ 2024-02-14  9:24 UTC (permalink / raw)
  To: Patrice Chotard, Greg Kroah-Hartman, Jiri Slaby, Dan Carpenter,
	Linus Walleij
  Cc: linux-kernel, linux-serial, Bartosz Golaszewski

From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Since commit 1f2bcb8c8ccd ("gpio: protect the descriptor label with
SRCU") gpiod_set_consumer_name() calls synchronize_srcu() which led to
a "sleeping in atomic context" smatch warning.

This function (along with gpiod_get/put() and all other GPIO APIs apart
from gpiod_get/set_value() and gpiod_direction_input/output()) should
have never been called with a spinlock taken. We're only fixing this now
as GPIOLIB has been rebuilt to use SRCU for access serialization which
uncovered this problem.

Move the calls to gpiod_get/put() outside the spinlock critical section.

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/linux-gpio/deee1438-efc1-47c4-8d80-0ab2cf01d60a@moroto.mountain/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/tty/serial/st-asc.c | 40 ++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c
index bbb5595d7e24..52a20277df98 100644
--- a/drivers/tty/serial/st-asc.c
+++ b/drivers/tty/serial/st-asc.c
@@ -467,6 +467,7 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
 	struct asc_port *ascport = to_asc_port(port);
 	struct gpio_desc *gpiod;
 	unsigned int baud;
+	bool manual_rts;
 	u32 ctrl_val;
 	tcflag_t cflag;
 	unsigned long flags;
@@ -517,26 +518,12 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
 		ctrl_val |= ASC_CTL_CTSENABLE;
 
 		/* If flow-control selected, stop handling RTS manually */
-		if (ascport->rts) {
-			devm_gpiod_put(port->dev, ascport->rts);
-			ascport->rts = NULL;
-
-			pinctrl_select_state(ascport->pinctrl,
-					     ascport->states[DEFAULT]);
-		}
+		if (ascport->rts)
+			manual_rts = false;
 	} else {
 		/* If flow-control disabled, it's safe to handle RTS manually */
-		if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
-			pinctrl_select_state(ascport->pinctrl,
-					     ascport->states[NO_HW_FLOWCTRL]);
-
-			gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
-			if (!IS_ERR(gpiod)) {
-				gpiod_set_consumer_name(gpiod,
-						port->dev->of_node->name);
-				ascport->rts = gpiod;
-			}
-		}
+		if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL])
+			manual_rts = true;
 	}
 
 	if ((baud < 19200) && !ascport->force_m1) {
@@ -595,6 +582,23 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
 	asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
 
 	uart_port_unlock_irqrestore(port, flags);
+
+	if (manual_rts) {
+		pinctrl_select_state(ascport->pinctrl,
+				     ascport->states[NO_HW_FLOWCTRL]);
+
+		gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
+		if (!IS_ERR(gpiod)) {
+			gpiod_set_consumer_name(gpiod,
+						port->dev->of_node->name);
+			ascport->rts = gpiod;
+		} else {
+			devm_gpiod_put(port->dev, ascport->rts);
+			ascport->rts = NULL;
+			pinctrl_select_state(ascport->pinctrl,
+					     ascport->states[DEFAULT]);
+		}
+	}
 }
 
 static const char *asc_type(struct uart_port *port)
-- 
2.40.1


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

* Re: [PATCH] serial: st-asc: don't get/put GPIOs in atomic context
  2024-02-14  9:24 [PATCH] serial: st-asc: don't get/put GPIOs in atomic context Bartosz Golaszewski
@ 2024-02-14 16:16 ` Linus Walleij
  2024-02-18 17:59 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 3+ messages in thread
From: Linus Walleij @ 2024-02-14 16:16 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Patrice Chotard, Greg Kroah-Hartman, Jiri Slaby, Dan Carpenter,
	linux-kernel, linux-serial, Bartosz Golaszewski

On Wed, Feb 14, 2024 at 10:24 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> Since commit 1f2bcb8c8ccd ("gpio: protect the descriptor label with
> SRCU") gpiod_set_consumer_name() calls synchronize_srcu() which led to
> a "sleeping in atomic context" smatch warning.
>
> This function (along with gpiod_get/put() and all other GPIO APIs apart
> from gpiod_get/set_value() and gpiod_direction_input/output()) should
> have never been called with a spinlock taken. We're only fixing this now
> as GPIOLIB has been rebuilt to use SRCU for access serialization which
> uncovered this problem.
>
> Move the calls to gpiod_get/put() outside the spinlock critical section.
>
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/linux-gpio/deee1438-efc1-47c4-8d80-0ab2cf01d60a@moroto.mountain/
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Good find!
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

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

* Re: [PATCH] serial: st-asc: don't get/put GPIOs in atomic context
  2024-02-14  9:24 [PATCH] serial: st-asc: don't get/put GPIOs in atomic context Bartosz Golaszewski
  2024-02-14 16:16 ` Linus Walleij
@ 2024-02-18 17:59 ` Greg Kroah-Hartman
  1 sibling, 0 replies; 3+ messages in thread
From: Greg Kroah-Hartman @ 2024-02-18 17:59 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Patrice Chotard, Jiri Slaby, Dan Carpenter, Linus Walleij,
	linux-kernel, linux-serial, Bartosz Golaszewski

On Wed, Feb 14, 2024 at 10:24:38AM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Since commit 1f2bcb8c8ccd ("gpio: protect the descriptor label with
> SRCU") gpiod_set_consumer_name() calls synchronize_srcu() which led to
> a "sleeping in atomic context" smatch warning.
> 
> This function (along with gpiod_get/put() and all other GPIO APIs apart
> from gpiod_get/set_value() and gpiod_direction_input/output()) should
> have never been called with a spinlock taken. We're only fixing this now
> as GPIOLIB has been rebuilt to use SRCU for access serialization which
> uncovered this problem.
> 
> Move the calls to gpiod_get/put() outside the spinlock critical section.
> 
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/linux-gpio/deee1438-efc1-47c4-8d80-0ab2cf01d60a@moroto.mountain/
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
>  drivers/tty/serial/st-asc.c | 40 ++++++++++++++++++++-----------------
>  1 file changed, 22 insertions(+), 18 deletions(-)
> 
> diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c
> index bbb5595d7e24..52a20277df98 100644
> --- a/drivers/tty/serial/st-asc.c
> +++ b/drivers/tty/serial/st-asc.c
> @@ -467,6 +467,7 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
>  	struct asc_port *ascport = to_asc_port(port);
>  	struct gpio_desc *gpiod;
>  	unsigned int baud;
> +	bool manual_rts;
>  	u32 ctrl_val;
>  	tcflag_t cflag;
>  	unsigned long flags;
> @@ -517,26 +518,12 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
>  		ctrl_val |= ASC_CTL_CTSENABLE;
>  
>  		/* If flow-control selected, stop handling RTS manually */
> -		if (ascport->rts) {
> -			devm_gpiod_put(port->dev, ascport->rts);
> -			ascport->rts = NULL;
> -
> -			pinctrl_select_state(ascport->pinctrl,
> -					     ascport->states[DEFAULT]);
> -		}
> +		if (ascport->rts)
> +			manual_rts = false;
>  	} else {
>  		/* If flow-control disabled, it's safe to handle RTS manually */
> -		if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
> -			pinctrl_select_state(ascport->pinctrl,
> -					     ascport->states[NO_HW_FLOWCTRL]);
> -
> -			gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
> -			if (!IS_ERR(gpiod)) {
> -				gpiod_set_consumer_name(gpiod,
> -						port->dev->of_node->name);
> -				ascport->rts = gpiod;
> -			}
> -		}
> +		if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL])
> +			manual_rts = true;
>  	}
>  
>  	if ((baud < 19200) && !ascport->force_m1) {
> @@ -595,6 +582,23 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
>  	asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
>  
>  	uart_port_unlock_irqrestore(port, flags);
> +
> +	if (manual_rts) {
> +		pinctrl_select_state(ascport->pinctrl,
> +				     ascport->states[NO_HW_FLOWCTRL]);
> +
> +		gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
> +		if (!IS_ERR(gpiod)) {
> +			gpiod_set_consumer_name(gpiod,
> +						port->dev->of_node->name);
> +			ascport->rts = gpiod;
> +		} else {
> +			devm_gpiod_put(port->dev, ascport->rts);
> +			ascport->rts = NULL;
> +			pinctrl_select_state(ascport->pinctrl,
> +					     ascport->states[DEFAULT]);
> +		}
> +	}
>  }

The 0-day bot rightly points out that manual_rts could be uninitialized
by this change, so I'm dropping it from my tree.  Please fix up and
resend.

thanks,

greg k-h

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

end of thread, other threads:[~2024-02-18 17:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-14  9:24 [PATCH] serial: st-asc: don't get/put GPIOs in atomic context Bartosz Golaszewski
2024-02-14 16:16 ` Linus Walleij
2024-02-18 17:59 ` Greg Kroah-Hartman

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).