Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tapio Reijonen" <tapio.reijonen@vaisala.com>
Cc: linux-serial@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed
Date: Sun, 06 Dec 2026 09:19:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-252307@kernel.org> (raw)
In-Reply-To: <20261206-serial-rs485-mode-gpio-v2-1-b9ea0dc2af3a@vaisala.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] serial: core: unlocked uart_forget_rs485_gpios() before
  refcount drain
- [Low] serial: core: missing Fixes trailers for RS485 GPIO commits
- [Low] serial: core: redundant uart_forget_rs485_gpios() call on error
  path
--

commit 31b880969cda7f2c09673a8a1805582d0de68b47
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>

> serial: core: Forget the RS485 GPIO descriptors when a port is removed
>
> The RS485 termination and RX-during-TX GPIO descriptors are acquired
> with devm on behalf of the port's device in uart_get_rs485_mode() and
> stored in struct uart_port, but nothing ever clears them. A uart_port
> can outlive the device it was registered with: the 8250 core keeps its
> ports in a static array, re-adds an unregistered entry as a placeholder
> and hands it to the next device that claims the slot. If that device
> has no RS485 support or no firmware node, uart_get_rs485_mode() returns
> before acquiring anything and the stale descriptors stay in place,
> pointing at lines that devres has already released. Enabling RS485 on
> such a port then drives GPIO lines the port no longer owns. The same
> happens when uart_get_rs485_mode() itself fails after the first
> descriptor was acquired.
>
> Forget the descriptors when a port is removed, on entry to
> uart_get_rs485_mode() and on its error paths, so a port only ever
> carries descriptors acquired for its current device.
>
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>

[Severity: Low]
This fixes stale devm-managed RS485 GPIO descriptors across port removal
and re-probe.  Should this include Fixes: tags for the commits that
introduced them?

  Fixes: d58a2df3d887 ("serial: 8250: Support rs485 bus termination GPIO")
  Fixes: 163f080eb717 ("serial: core: Add option to output RS485 RX_DURING_TX state via GPIO")

> diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
> index c0deec0e6af41..9516f50a72139 100644
> --- a/drivers/tty/serial/serial_core.c
> +++ b/drivers/tty/serial/serial_core.c

[ ... ]

> @@ -3265,6 +3276,7 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
>  	 */
>  	uport->type = PORT_UNKNOWN;
>  	uport->port_dev = NULL;
> +	uart_forget_rs485_gpios(uport);
>  
>  	guard(mutex)(&port->mutex);
>  	WARN_ON(atomic_dec_return(&state->refcount) < 0);

[Severity: Medium]
Can clearing the RS485 GPIO pointers before taking port->mutex and
draining state->refcount race with concurrent ioctls on /dev/console?

When the removed port is the active console, tty_port_tty_vhangup()
keeps console_fops and skips uart_hangup(), leaving TTY_IO_ERROR unset.
Before port->mutex is re-acquired and state->refcount is drained, a
TIOCSRS485 ioctl on /dev/console can still enter uart_set_rs485_config():

CPU0                                   CPU1
serial_core_remove_one_port()
  tty_port_tty_vhangup()
    /* /dev/console keeps console_fops */
  unlock(&port->mutex)
                                       uart_ioctl(TIOCSRS485)
                                         lock(&port->mutex)
                                         uart_set_rs485_config()
  uart_forget_rs485_gpios(uport)
    uport->rs485_term_gpio = NULL;
                                           gpiod_set_value_cansleep(
                                             port->rs485_term_gpio, ...)
                                             <-- data race
  lock(&port->mutex)
  wait_event(... !refcount)
  state->uart_port = NULL

This causes a data race on the RS485 GPIO pointers and can leave the
GPIOs in an inconsistent state if uart_set_rs485_config() rolls back on
error.  Moving uart_forget_rs485_gpios(uport) inside the mutex guard
after wait_event() and state->uart_port = NULL closes the race.

> @@ -3579,6 +3591,8 @@ int uart_get_rs485_mode(struct uart_port *port)
>  	u32 rs485_delay[2];
>  	int ret;
>  
> +	uart_forget_rs485_gpios(port);
> +
>  	if (!(port->rs485_supported.flags & SER_RS485_ENABLED))
>  		return 0;
>  
> @@ -3628,8 +3642,11 @@ int uart_get_rs485_mode(struct uart_port *port)
>  	 * Works fine for short cables and users may enable for longer cables.
>  	 */
>  	desc = devm_gpiod_get_optional(dev, "rs485-term", GPIOD_OUT_LOW);
> -	if (IS_ERR(desc))
> -		return dev_err_probe(dev, PTR_ERR(desc), "Cannot get rs485-term-gpios\n");
> +	if (IS_ERR(desc)) {
> +		uart_forget_rs485_gpios(port);
> +		return dev_err_probe(dev, PTR_ERR(desc),
> +				     "Cannot get rs485-term-gpios\n");
> +	}

[Severity: Low]
Is this uart_forget_rs485_gpios() call redundant?  The function already
cleared both GPIO pointers on entry and has not assigned either one yet.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206-serial-rs485-mode-gpio-v2-0-b9ea0dc2af3a@vaisala.com?part=1

  reply	other threads:[~2026-12-06  9:19 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  8:50 [PATCH RESEND v2 0/3] serial: core: add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 1/3] serial: core: Forget the RS485 GPIO descriptors when a port is removed Tapio Reijonen
2026-12-06  9:19   ` sashiko-bot [this message]
2026-12-06  8:50 ` [PATCH RESEND v2 2/3] dt-bindings: serial: rs485: Add transceiver interface select GPIO Tapio Reijonen
2026-12-06  8:50 ` [PATCH RESEND v2 3/3] serial: core: Drive the " Tapio Reijonen
2026-12-06  9:15   ` sashiko-bot

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=sashiko-outbox-252307@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tapio.reijonen@vaisala.com \
    /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