Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] serial: 8250: release the transmit PM reference on shutdown
@ 2026-12-04  9:57 James Hilliard
  2026-12-04 10:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: James Hilliard @ 2026-12-04  9:57 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Jiri Slaby, Raul E Rangel, Ilpo Järvinen,
	Andy Shevchenko, Heikki Krogerus, Tony Lindgren,
	Sebastian Andrzej Siewior
  Cc: linux-kernel, linux-serial, James Hilliard

Transmit holds a runtime PM reference until the transmitter-empty path
clears THRI. A hangup can shut down the port while that reference is still
held. Shutdown clears the interrupt enables and stops DMA, leaving no TX
interrupt to release rpm_tx_active. A suppressed DMA completion cannot
release it either.

Drop the transmit reference after shutting down TX in both the generic
and OMAP shutdown paths. Their separate shutdown PM references keep the
UART accessible throughout cleanup. The exchange in the helper also
handles shutdown after the normal TX path already released its reference.

Fixes: d74d5d1b7288 ("tty: serial: 8250_core: add run time pm")
Reported-by: Raul E Rangel <rrangel@chromium.org>
Closes: https://lore.kernel.org/r/20261022152019.1621716-1-rrangel@chromium.org
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v2:
- Rebase on current tty-next for resubmission.
- Link to v1: https://patch.msgid.link/20261022-submit-serial-8250-tx-pm-shutdown-v1-v1-1-0e894afa3bb8@gmail.com
---
 drivers/tty/serial/8250/8250.h      | 1 +
 drivers/tty/serial/8250/8250_omap.c | 1 +
 drivers/tty/serial/8250/8250_port.c | 5 ++++-
 3 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250.h b/drivers/tty/serial/8250/8250.h
index 21ed9c41b751..4a7d26e2dbf0 100644
--- a/drivers/tty/serial/8250/8250.h
+++ b/drivers/tty/serial/8250/8250.h
@@ -190,6 +190,7 @@ void serial8250_wait_for_xmitr(struct uart_8250_port *up, int bits);
 
 void serial8250_rpm_get(struct uart_8250_port *p);
 void serial8250_rpm_put(struct uart_8250_port *p);
+void serial8250_rpm_put_tx(struct uart_8250_port *p);
 DEFINE_GUARD(serial8250_rpm, struct uart_8250_port *,
 	     serial8250_rpm_get(_T), serial8250_rpm_put(_T));
 
diff --git a/drivers/tty/serial/8250/8250_omap.c b/drivers/tty/serial/8250/8250_omap.c
index ed0b8f125332..bdef7c4b6b20 100644
--- a/drivers/tty/serial/8250/8250_omap.c
+++ b/drivers/tty/serial/8250/8250_omap.c
@@ -795,6 +795,7 @@ static void omap_8250_shutdown(struct uart_port *port)
 	if (up->lcr & UART_LCR_SBC)
 		serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
 	serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
+	serial8250_rpm_put_tx(up);
 }
 
 static void omap_8250_throttle(struct uart_port *port)
diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
index 36b2b0a2c05c..d8a5ca46d9a9 100644
--- a/drivers/tty/serial/8250/8250_port.c
+++ b/drivers/tty/serial/8250/8250_port.c
@@ -672,7 +672,7 @@ static void serial8250_rpm_get_tx(struct uart_8250_port *p)
 	pm_runtime_get_sync(p->port.dev);
 }
 
-static void serial8250_rpm_put_tx(struct uart_8250_port *p)
+void serial8250_rpm_put_tx(struct uart_8250_port *p)
 {
 	unsigned char rpm_active;
 
@@ -684,6 +684,7 @@ static void serial8250_rpm_put_tx(struct uart_8250_port *p)
 		return;
 	pm_runtime_put_autosuspend(p->port.dev);
 }
+EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);
 
 /*
  * IER sleep support.  UARTs which have EFRs need the "extended
@@ -2486,6 +2487,8 @@ void serial8250_do_shutdown(struct uart_port *port)
 	 */
 	synchronize_irq(port->irq);
 
+	/* TX interrupts can no longer release the transmit reference. */
+	serial8250_rpm_put_tx(up);
 	serial8250_rpm_put(up);
 
 	up->ops->release_irq(up);

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
base-tree: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty.git tty-next
change-id: 20261022-submit-serial-8250-tx-pm-shutdown-v1-db9091dde8f7

Best regards,
--  
James Hilliard <james.hilliard1@gmail.com>


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

* Re: [PATCH v2] serial: 8250: release the transmit PM reference on shutdown
  2026-12-04  9:57 [PATCH v2] serial: 8250: release the transmit PM reference on shutdown James Hilliard
@ 2026-12-04 10:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 10:23 UTC (permalink / raw)
  To: James Hilliard; +Cc: linux-serial

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] serial: 8250: shutdown drops TX PM ref before cancelling em485
  start_tx_timer
- [High] serial: 8250: shutdown drops TX PM ref without cancelling em485
  start_tx_timer
- [Medium] serial: 8250: unlocked serial8250_rpm_put_tx() races with TX
  PM get
--

commit 4f158a86b3f5586570a06a8414aa89964b94419b
Author: James Hilliard <james.hilliard1@gmail.com>

serial: 8250: release the transmit PM reference on shutdown

This commit exports serial8250_rpm_put_tx() and calls it during both
omap_8250_shutdown() and serial8250_do_shutdown() to drop any held transmit
runtime PM reference when a port is shut down.

> diff --git a/drivers/tty/serial/8250/8250_omap.c b/drivers/tty/serial/8250/8250_omap.c
> index ed0b8f1253327..bdef7c4b6b202 100644
> --- a/drivers/tty/serial/8250/8250_omap.c
> +++ b/drivers/tty/serial/8250/8250_omap.c
> @@ -795,6 +795,7 @@ static void omap_8250_shutdown(struct uart_port *port)
>  	if (up->lcr & UART_LCR_SBC)
>  		serial_out(up, UART_LCR, up->lcr & ~UART_LCR_SBC);
>  	serial_out(up, UART_FCR, UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
> +	serial8250_rpm_put_tx(up);
>  }
>  
>  static void omap_8250_throttle(struct uart_port *port)

[Severity: High]
Can calling serial8250_rpm_put_tx() here let the UART runtime-suspend while
em485->start_tx_timer is still armed?

When omap8250_rs485_config() falls back to software RS-485 emulation with
delay_rts_before_send > 0, serial8250_start_tx() takes rpm_tx_active and
defers __start_tx() to em485->start_tx_timer.  Neither uart_port_shutdown()
nor omap_8250_shutdown() cancels em485->start_tx_timer, which previously
stayed safe only because rpm_tx_active kept the UART active.

If the port closes or hangs up during that delay window, dropping both
runtime PM references lets omap8250_runtime_suspend() gate clocks before the
timer fires.  serial8250_em485_handle_start_tx() does not take a runtime PM
reference of its own and calls __start_tx(), writing UART_IER on a
runtime-suspended UART, which can cause a synchronous external bus abort or
hang on OMAP/K3 SoCs.  Cancelling pending em485 timers and clearing
em485->active_timer before dropping the runtime PM references would prevent
this.

> diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c
> index 36b2b0a2c05cd..d8a5ca46d9a9b 100644
> --- a/drivers/tty/serial/8250/8250_port.c
> +++ b/drivers/tty/serial/8250/8250_port.c
> @@ -672,7 +672,7 @@ static void serial8250_rpm_get_tx(struct uart_8250_port *p)
>  	pm_runtime_get_sync(p->port.dev);
>  }
>  
> -static void serial8250_rpm_put_tx(struct uart_8250_port *p)
> +void serial8250_rpm_put_tx(struct uart_8250_port *p)
>  {
>  	unsigned char rpm_active;
>  
> @@ -684,6 +684,7 @@ static void serial8250_rpm_put_tx(struct uart_8250_port *p)
>  		return;
>  	pm_runtime_put_autosuspend(p->port.dev);
>  }
> +EXPORT_SYMBOL_GPL(serial8250_rpm_put_tx);

[Severity: Medium]
Does calling serial8250_rpm_put_tx() without port->lock race with
serial8250_rpm_get_tx() and underflow the runtime PM usage count?

Previously all callers of serial8250_rpm_get_tx() and
serial8250_rpm_put_tx() held port->lock, serializing the xchg() on
rpm_tx_active with the paired pm_runtime_*() call.  On an OMAP UART with a
DT cts-gpios line and CRTSCTS software flow control, mctrl_gpio_irq_handle()
remains enabled across shutdown and can still invoke
uart_handle_cts_change() -> serial8250_start_tx() ->
serial8250_rpm_get_tx() under port->lock:

CPU0                              CPU1
omap_8250_shutdown()
  guard(serial8250_rpm)
    pm_runtime_get_sync() (0->1)
                                  mctrl_gpio_irq_handle()
                                    lock(&port->lock)
                                    ... -> serial8250_rpm_get_tx()
                                      xchg(&rpm_tx_active, 1)
  serial8250_rpm_put_tx()
    xchg(&rpm_tx_active, 0) -> 1
    pm_runtime_put_autosuspend()
      (usage_count 1->0)
  guard exit: serial8250_rpm_put()
    pm_runtime_put_autosuspend()
      <-- usage_count underflow!
                                      pm_runtime_get_sync() (0->1)
                                        <-- permanent PM ref leak

When rpm_drop_usage_count() detects the underflow it warns and resets
usage_count back to 0, so CPU1's pm_runtime_get_sync() leaves usage_count
stuck at 1 while rpm_tx_active is already 0, permanently leaking a runtime
PM reference.  Taking port->lock around serial8250_rpm_put_tx() (and
asserting lockdep_assert_held_once(&p->port.lock)) would restore mutual
exclusion with serial8250_rpm_get_tx().

[ ... ]

> @@ -2486,6 +2487,8 @@ void serial8250_do_shutdown(struct uart_port *port)
>  	 */
>  	synchronize_irq(port->irq);
>  
> +	/* TX interrupts can no longer release the transmit reference. */
> +	serial8250_rpm_put_tx(up);
>  	serial8250_rpm_put(up);
>  
>  	up->ops->release_irq(up);

[Severity: High]
Should serial8250_do_shutdown() cancel pending em485 timers before dropping
the transmit runtime PM reference here as well?

When serial8250_start_tx() defers transmission to em485->start_tx_timer,
nothing has been written to the FIFO or DMA yet, so serial8250_tx_empty()
returns TIOCSER_TEMT and a normal close() proceeds straight into
serial8250_do_shutdown() without waiting for the timer.  Dropping both
rpm_tx_active and the shutdown PM reference while em485->start_tx_timer is
still pending allows the UART to runtime-suspend before
serial8250_em485_handle_start_tx() runs and calls __start_tx(), writing
UART_IER on a runtime-suspended port (and potentially entering DMA TX paths
since up->dma is not cleared here).  Cancelling em485->start_tx_timer and
em485->stop_tx_timer before serial8250_rpm_put_tx(up) would close this
window.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-submit-serial-8250-tx-pm-shutdown-v1-v2-1-f32936284ef7@gmail.com?part=1

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

end of thread, other threads:[~2026-12-04 10:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:57 [PATCH v2] serial: 8250: release the transmit PM reference on shutdown James Hilliard
2026-12-04 10:23 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox