From: Patrice CHOTARD <patrice.chotard@foss.st.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Jiri Slaby <jirislaby@kernel.org>,
Dan Carpenter <dan.carpenter@linaro.org>,
Linus Walleij <linus.walleij@linaro.org>
Cc: <linux-kernel@vger.kernel.org>, <linux-serial@vger.kernel.org>,
Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Subject: Re: [PATCH v3] serial: st-asc: don't get/put GPIOs in atomic context
Date: Wed, 21 Feb 2024 10:53:12 +0100 [thread overview]
Message-ID: <b381cb72-943d-473f-b2f2-eb288132aaac@foss.st.com> (raw)
In-Reply-To: <20240220113410.16613-1-brgl@bgdev.pl>
On 2/20/24 12:34, 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>
> ---
> v2 -> v3:
> - we only need to change the GPIO configuration for RTS in certain situations
> so use a separate variable for storing that information; if we don't then we
> may end up putting the descriptor when setting a different option
> - I dropped Linus tag as the code change significantly
>
> v1 -> v2:
> - initialize the 'manual_rts' variable to false as we don't always get to
> the place where it's set
>
> drivers/tty/serial/st-asc.c | 40 ++++++++++++++++++++++---------------
> 1 file changed, 24 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/tty/serial/st-asc.c b/drivers/tty/serial/st-asc.c
> index bbb5595d7e24..a23e59551848 100644
> --- a/drivers/tty/serial/st-asc.c
> +++ b/drivers/tty/serial/st-asc.c
> @@ -465,6 +465,7 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
> const struct ktermios *old)
> {
> struct asc_port *ascport = to_asc_port(port);
> + bool manual_rts, toggle_rts = false;
> struct gpio_desc *gpiod;
> unsigned int baud;
> u32 ctrl_val;
> @@ -518,25 +519,13 @@ static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
>
> /* 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]);
> + toggle_rts = true;
> + 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 = toggle_rts = true;
> }
>
> if ((baud < 19200) && !ascport->force_m1) {
> @@ -595,6 +584,25 @@ 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 (toggle_rts) {
> + 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)
Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>
Thanks
Patrice
prev parent reply other threads:[~2024-02-21 9:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-20 11:34 Bartosz Golaszewski
2024-02-21 9:53 ` Patrice CHOTARD [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b381cb72-943d-473f-b2f2-eb288132aaac@foss.st.com \
--to=patrice.chotard@foss.st.com \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=dan.carpenter@linaro.org \
--cc=gregkh@linuxfoundation.org \
--cc=jirislaby@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®