* [PATCH] serial: imx: cancel RS485 trigger hrtimers in shutdown and remove
@ 2026-08-19 2:19 Fan Wu
2026-08-19 4:26 ` Jiri Slaby
0 siblings, 1 reply; 3+ messages in thread
From: Fan Wu @ 2026-08-19 2:19 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: Frank Li, Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
linux-serial, linux-kernel, imx, linux-arm-kernel, stable, Fan Wu
The rs485 delay hrtimers trigger_start_tx and trigger_stop_tx are
embedded in the devm allocated struct imx_port, and their callbacks
reach the port through container_of() and touch registers under the
port lock. Nothing cancels them synchronously: the tx paths only
call hrtimer_try_to_cancel(), which does not wait for a running
callback, and the bounded wait in imx_uart_shutdown() can give up,
force tx_state to OFF, and leave a timer armed. After
imx_uart_remove() returns, devm frees the port and a late callback
dereferences freed memory.
Cancel both timers at the end of imx_uart_shutdown(), after the port
lock is dropped and before the clocks are disabled, and again in
imx_uart_remove() before the devm free: serial core does not call the
driver shutdown on every path that reaches remove().
This issue was found by an in-house static analysis tool.
Fixes: bd78ecd6056d ("serial: imx: use hrtimers for rs485 delays")
Cc: stable@vger.kernel.org
Assisted-by: Codex:gpt-5.6
Signed-off-by: Fan Wu <fanwu01@zju.edu.cn>
---
drivers/tty/serial/imx.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c
index 251a50c8aa38..86c99f73c50a 100644
--- a/drivers/tty/serial/imx.c
+++ b/drivers/tty/serial/imx.c
@@ -1707,6 +1707,10 @@ static void imx_uart_shutdown(struct uart_port *port)
uart_port_unlock_irqrestore(&sport->port, flags);
+ /* The rs485 trigger callbacks take the port lock and touch registers. */
+ hrtimer_cancel(&sport->trigger_start_tx);
+ hrtimer_cancel(&sport->trigger_stop_tx);
+
clk_disable_unprepare(sport->clk_per);
clk_disable_unprepare(sport->clk_ipg);
}
@@ -2649,6 +2653,10 @@ static void imx_uart_remove(struct platform_device *pdev)
struct imx_port *sport = platform_get_drvdata(pdev);
uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
+
+ /* Serial core can reach remove() without calling the driver shutdown. */
+ hrtimer_cancel(&sport->trigger_start_tx);
+ hrtimer_cancel(&sport->trigger_stop_tx);
}
static void imx_uart_restore_context(struct imx_port *sport)
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] serial: imx: cancel RS485 trigger hrtimers in shutdown and remove
2026-08-19 2:19 [PATCH] serial: imx: cancel RS485 trigger hrtimers in shutdown and remove Fan Wu
@ 2026-08-19 4:26 ` Jiri Slaby
2026-08-19 6:39 ` Fan Wu
0 siblings, 1 reply; 3+ messages in thread
From: Jiri Slaby @ 2026-08-19 4:26 UTC (permalink / raw)
To: Fan Wu, Greg Kroah-Hartman
Cc: Frank Li, Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
linux-serial, linux-kernel, imx, linux-arm-kernel, stable
On 19. 08. 26, 4:19, Fan Wu wrote:
> The rs485 delay hrtimers trigger_start_tx and trigger_stop_tx are
> embedded in the devm allocated struct imx_port, and their callbacks
> reach the port through container_of() and touch registers under the
> port lock. Nothing cancels them synchronously: the tx paths only
> call hrtimer_try_to_cancel(), which does not wait for a running
> callback, and the bounded wait in imx_uart_shutdown() can give up,
> force tx_state to OFF, and leave a timer armed. After
> imx_uart_remove() returns, devm frees the port and a late callback
> dereferences freed memory.
>
> Cancel both timers at the end of imx_uart_shutdown(), after the port
> lock is dropped and before the clocks are disabled, and again in
> imx_uart_remove() before the devm free:
> serial core does not call the
> driver shutdown on every path that reaches remove().
Could you be more specific on what path it does not?
thanks,
--
js
suse labs
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] serial: imx: cancel RS485 trigger hrtimers in shutdown and remove
2026-08-19 4:26 ` Jiri Slaby
@ 2026-08-19 6:39 ` Fan Wu
0 siblings, 0 replies; 3+ messages in thread
From: Fan Wu @ 2026-08-19 6:39 UTC (permalink / raw)
To: Jiri Slaby
Cc: Fan Wu, Greg Kroah-Hartman, Frank Li, Sascha Hauer,
Pengutronix Kernel Team, Fabio Estevam, linux-serial,
linux-kernel, imx, linux-arm-kernel, stable
> On Aug 19, 2026, at 12:26, Jiri Slaby <jirislaby@kernel.org> wrote:
>
>> serial core does not call the
>> driver shutdown on every path that reaches remove().
>
> Could you be more specific on what path it does not?
>
> thanks,
> --
> js
> suse labs
Hi Jiri
Yes. The relevant path is a console port whose last TTY user closes it
before the device is unbound.
tty_port_shutdown() skips port->ops->shutdown() for console ports. The
last close then clears the active state and dissociates the TTY, so the
later tty_port_tty_vhangup() in the remove path has no TTY to hang up
and cannot invoke the driver shutdown. That is the path the cancel in
imx_uart_remove() covers.
trigger_stop_tx can already be pending at that point: after TXDC,
imx_uart_stop_tx() starts it for delay_rts_after_send, whereas the close
path only waits for TXDC.
I can add this to the changelog if you would prefer the rationale to be
spelled out there.
Thanks,
Fan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-08-19 6:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-19 2:19 [PATCH] serial: imx: cancel RS485 trigger hrtimers in shutdown and remove Fan Wu
2026-08-19 4:26 ` Jiri Slaby
2026-08-19 6:39 ` Fan Wu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox