* [PATCH 1/3] serial: max310x: fix use-after-free on remove
2026-11-20 7:33 [PATCH 0/3] serial: max310x: teardown ordering and CTS locking fixes Tapio Reijonen
@ 2026-11-20 7:33 ` Tapio Reijonen
2026-11-20 7:45 ` sashiko-bot
2026-11-20 7:33 ` [PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails Tapio Reijonen
2026-11-20 7:33 ` [PATCH 3/3] serial: max310x: hold the port lock around the CTS change Tapio Reijonen
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-20 7:33 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Alexander Shiyan, Andy Shevchenko
Cc: linux-serial, linux-kernel, Hugo Villeneuve, stable,
Tapio Reijonen
max310x_remove() cancels a port's timer and work items first and
unregisters the port afterwards. Until uart_remove_one_port() has shut
the port down, an open port keeps queuing its tx_work from the
interrupt handler and from start_tx(), and the RS485 code re-arms the
delay timer and rts_work from those, so a work item or the timer can
still run after the cancels, on a port that is being torn down, and
fire after the port memory has been freed.
The unregister itself queues work too: the hangup it triggers clears
DTR and RTS through set_mctrl(), which schedules md_work, so a cancel
done before uart_remove_one_port() cannot cover it. Tested with the
port memory zeroed on free, md_work ran after the unbind on the freed
port and oopsed in max310x_md_proc().
The interrupt handler is a source of its own. The IRQ enable bits
only gate the pin, the status bits stay latched regardless, so on the
single-port parts, where the handler reads the port status on every
event of the shared line, a status bit left behind by shutdown() can
still queue tx_work after the cancels. With the IRQ managed by devres
it was released only after remove() had returned.
Similar to commit 36672c8d7d14 ("serial: amba-pl011: cancel RS485
hrtimers after freeing IRQ"), stop the sources before the cancels:
unregister the ports, so that the shutdown() this triggers stops the
TX path, then free the IRQ, then cancel the timers and work items. The
IRQ goes second because the handler loops while a port has status or
RX data pending and free_irq() waits for it: freed with the ports
still open under traffic, the unbind blocked until the traffic paused.
Keep the two per-port steps in helpers; the probe error path needs
them too, with no IRQ handling since the request is probe's last step.
Fixes: 10d8b34a4217 ("serial: max310x: Driver rework")
Cc: stable@vger.kernel.org
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 47 +++++++++++++++++++++++++++++++++-----------
1 file changed, 36 insertions(+), 11 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index a0ed70185555eed745d905b36c8b8f162468dd6f..f5b32303353208d9f85ae0b04d38a9abdd398d24 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1856,6 +1856,28 @@ static const struct serial_rs485 max310x_rs485_supported = {
.delay_rts_after_send = 1,
};
+/*
+ * Unregister the port before cancelling its work: until the port is
+ * gone, an open port can queue the work and arm the timer again, and
+ * the hangup queues md_work itself. The caller frees the IRQ in
+ * between where there is one. A port that was never registered has
+ * nothing queued.
+ */
+static void max310x_port_unregister(struct max310x_one *one)
+{
+ if (test_and_clear_bit(one->port.line, max310x_lines))
+ uart_remove_one_port(&max310x_uart, &one->port);
+}
+
+static void max310x_port_cancel_work(struct max310x_one *one)
+{
+ hrtimer_cancel(&one->tx_delay_tmr);
+ cancel_work_sync(&one->rts_work);
+ cancel_work_sync(&one->tx_work);
+ cancel_work_sync(&one->md_work);
+ cancel_work_sync(&one->rs_work);
+}
+
static int max310x_probe(struct device *dev, const struct max310x_devtype *devtype,
const struct max310x_if_cfg *if_cfg,
struct regmap *regmaps[], int irq)
@@ -2068,9 +2090,9 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
max310x_power(&s->p[i].port, 0);
}
- /* Setup interrupt */
- ret = devm_request_threaded_irq(dev, irq, NULL, max310x_ist,
- IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s);
+ /* Setup interrupt, freed by max310x_remove() */
+ ret = request_threaded_irq(irq, NULL, max310x_ist,
+ IRQF_ONESHOT | IRQF_SHARED, dev_name(dev), s);
if (!ret)
return 0;
@@ -2093,16 +2115,19 @@ static void max310x_remove(struct device *dev)
struct max310x_port *s = dev_get_drvdata(dev);
int i;
- for (i = 0; i < s->devtype->nr; i++) {
- hrtimer_cancel(&s->p[i].tx_delay_tmr);
- cancel_work_sync(&s->p[i].rts_work);
- cancel_work_sync(&s->p[i].tx_work);
- cancel_work_sync(&s->p[i].md_work);
- cancel_work_sync(&s->p[i].rs_work);
+ /*
+ * The handler loops while a port has status or RX data pending and
+ * free_irq() waits for it: unregister first, so that the shutdown()
+ * this triggers masks the interrupts and stops the transmitter.
+ */
+ for (i = 0; i < s->devtype->nr; i++)
+ max310x_port_unregister(&s->p[i]);
- if (test_and_clear_bit(s->p[i].port.line, max310x_lines))
- uart_remove_one_port(&max310x_uart, &s->p[i].port);
+ /* The handler queues tx_work from latched status regardless of IRQEN */
+ free_irq(s->p[0].port.irq, s);
+ for (i = 0; i < s->devtype->nr; i++) {
+ max310x_port_cancel_work(&s->p[i]);
max310x_power(&s->p[i].port, 0);
}
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 1/3] serial: max310x: fix use-after-free on remove
2026-11-20 7:33 ` [PATCH 1/3] serial: max310x: fix use-after-free on remove Tapio Reijonen
@ 2026-11-20 7:45 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-20 7:45 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
> max310x_remove() cancels a port's timer and work items first and
> unregisters the port afterwards. Until uart_remove_one_port() has shut
> the port down, an open port keeps queuing its tx_work from the
> interrupt handler and from start_tx(), and the RS485 code re-arms the
> delay timer and rts_work from those, so a work item or the timer can
> still run after the cancels, on a port that is being torn down, and
> fire after the port memory has been freed.
>
> The unregister itself queues work too: the hangup it triggers clears
> DTR and RTS through set_mctrl(), which schedules md_work, so a cancel
> done before uart_remove_one_port() cannot cover it. Tested with the
> port memory zeroed on free, md_work ran after the unbind on the freed
> port and oopsed in max310x_md_proc().
>
> The interrupt handler is a source of its own. The IRQ enable bits
> [ ... ]
> them too, with no IRQ handling since the request is probe's last step.
>
> Fixes: 10d8b34a4217 ("serial: max310x: Driver rework")
> Cc: stable@vger.kernel.org
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-max310x-teardown-fixes-v1-0-eb52c8b612df@vaisala.com?part=1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails
2026-11-20 7:33 [PATCH 0/3] serial: max310x: teardown ordering and CTS locking fixes Tapio Reijonen
2026-11-20 7:33 ` [PATCH 1/3] serial: max310x: fix use-after-free on remove Tapio Reijonen
@ 2026-11-20 7:33 ` Tapio Reijonen
2026-11-20 7:39 ` sashiko-bot
2026-11-20 7:33 ` [PATCH 3/3] serial: max310x: hold the port lock around the CTS change Tapio Reijonen
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-20 7:33 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Alexander Shiyan, Andy Shevchenko
Cc: linux-serial, linux-kernel, Hugo Villeneuve, stable,
Tapio Reijonen
uart_add_one_port() queues work on the port it registers: set_mctrl()
schedules md_work or, with RS485 enabled at boot, the RS485
configuration it applies schedules rs_work instead. When the IRQ
request or the registration of a later port then fails, the error path
unregisters the ports but leaves that work queued, and it runs after
probe has returned and the port memory has been freed.
The same loop walks every port, and a port the register loop never
reached still has line 0 from the allocation. If another chip holds
line 0, the loop clears that chip's bit in max310x_lines.
uart_remove_one_port() returns early for a port that was never
registered, so nothing is unregistered, but line 0 is now marked free
while the other chip still uses it: the next probe is given line 0 and
fails in uart_add_one_port() with -EINVAL, and when the other chip is
removed its port 0 is skipped and stays registered on freed memory.
Reachable with two chips when the second one defers on a GPIO, for
example.
Unwind from the failure index down, with the removal helpers, so that
only the ports that were registered are unregistered and have their
work items cancelled, and take the exits that come before any
registration straight to the clock release.
Fixes: 78adccac1ff9 ("serial: max310x: Assign port line automatically")
Fixes: 5d888f1c32e2 ("serial: max310x: simplify probe() and remove() error handling")
Cc: stable@vger.kernel.org
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index f5b32303353208d9f85ae0b04d38a9abdd398d24..179f5f4cc7b2f994c1dfd1b498878f4f7d6a3299 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1960,7 +1960,7 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
MAX310X_PORT_STARTUP_TIMEOUT_US);
if (ret) {
ret = dev_err_probe(dev, -EAGAIN, "port reset failed\n");
- goto out_uart;
+ goto out_clk;
}
regmap_write(regmaps[i], MAX310X_MODE1_REG, devtype->mode1);
@@ -1968,7 +1968,7 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
ret = max310x_set_ref_clk(dev, s, freq, &uartclk, xtal);
if (ret < 0)
- goto out_uart;
+ goto out_clk;
dev_dbg(dev, "Reference clock set to %i Hz\n", uartclk);
@@ -2029,7 +2029,7 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
s->gpio.can_sleep = 1;
ret = devm_gpiochip_add_data(dev, &s->gpio, s);
if (ret)
- goto out_uart;
+ goto out_clk;
#endif
for (i = 0; i < devtype->nr; i++) {
@@ -2099,9 +2099,9 @@ static int max310x_probe(struct device *dev, const struct max310x_devtype *devty
dev_err(dev, "Unable to request IRQ %i\n", irq);
out_uart:
- for (i = 0; i < devtype->nr; i++) {
- if (test_and_clear_bit(s->p[i].port.line, max310x_lines))
- uart_remove_one_port(&max310x_uart, &s->p[i].port);
+ while (i--) {
+ max310x_port_unregister(&s->p[i]);
+ max310x_port_cancel_work(&s->p[i]);
}
out_clk:
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails
2026-11-20 7:33 ` [PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails Tapio Reijonen
@ 2026-11-20 7:39 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-20 7:39 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
> uart_add_one_port() queues work on the port it registers: set_mctrl()
> schedules md_work or, with RS485 enabled at boot, the RS485
> configuration it applies schedules rs_work instead. When the IRQ
> request or the registration of a later port then fails, the error path
> unregisters the ports but leaves that work queued, and it runs after
> probe has returned and the port memory has been freed.
>
> The same loop walks every port, and a port the register loop never
> reached still has line 0 from the allocation. If another chip holds
> line 0, the loop clears that chip's bit in max310x_lines.
> uart_remove_one_port() returns early for a port that was never
> registered, so nothing is unregistered, but line 0 is now marked free
> while the other chip still uses it: the next probe is given line 0 and
> fails in uart_add_one_port() with -EINVAL, and when the other chip is
> removed its port 0 is skipped and stays registered on freed memory.
> Reachable with two chips when the second one defers on a GPIO, for
> example.
>
> Unwind from the failure index down, with the removal helpers, so that
> only the ports that were registered are unregistered and have their
> work items cancelled, and take the exits that come before any
> registration straight to the clock release.
>
> Fixes: 78adccac1ff9 ("serial: max310x: Assign port line automatically")
> Fixes: 5d888f1c32e2 ("serial: max310x: simplify probe() and remove() error handling")
> Cc: stable@vger.kernel.org
> Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-max310x-teardown-fixes-v1-0-eb52c8b612df@vaisala.com?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 3/3] serial: max310x: hold the port lock around the CTS change
2026-11-20 7:33 [PATCH 0/3] serial: max310x: teardown ordering and CTS locking fixes Tapio Reijonen
2026-11-20 7:33 ` [PATCH 1/3] serial: max310x: fix use-after-free on remove Tapio Reijonen
2026-11-20 7:33 ` [PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails Tapio Reijonen
@ 2026-11-20 7:33 ` Tapio Reijonen
2026-11-20 7:41 ` sashiko-bot
2 siblings, 1 reply; 7+ messages in thread
From: Tapio Reijonen @ 2026-11-20 7:33 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby, Alexander Shiyan, Andy Shevchenko
Cc: linux-serial, linux-kernel, Hugo Villeneuve, Tapio Reijonen
uart_handle_cts_change() has to be called with the port lock held: it
updates the CTS counter and, in software flow control mode, hw_stopped
and the transmitter through start_tx() and stop_tx(), all under the
lock. The interrupt thread calls it without the lock, which lockdep
reports on the first CTS edge.
Take the lock around the call, after the register read, since the read
is a bus transfer that sleeps and must not happen under the spinlock.
Use uart_port_lock_irqsave() for it:
- irqsave, because the delay timer takes the same lock from hardirq
context while this code runs in a thread with interrupts on;
- the serial core accessor rather than spin_lock_irqsave(), because
that is how drivers take the port lock, and start_tx(), which
uart_handle_cts_change() may call, drops and retakes it with
uart_port_unlock() and uart_port_lock().
Fixes: f65444187a66 ("serial: New serial driver MAX310X")
Signed-off-by: Tapio Reijonen <tapio.reijonen@vaisala.com>
---
drivers/tty/serial/max310x.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
index 179f5f4cc7b2f994c1dfd1b498878f4f7d6a3299..b2d887316957c2ca46e83efab08a537fc584b407 100644
--- a/drivers/tty/serial/max310x.c
+++ b/drivers/tty/serial/max310x.c
@@ -1043,7 +1043,8 @@ static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno)
if (ists & MAX310X_IRQ_CTS_BIT) {
lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
- uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT);
+ scoped_guard(uart_port_lock_irqsave, port)
+ uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT);
}
if (rxlen)
max310x_handle_rx(port, rxlen);
--
2.47.3
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH 3/3] serial: max310x: hold the port lock around the CTS change
2026-11-20 7:33 ` [PATCH 3/3] serial: max310x: hold the port lock around the CTS change Tapio Reijonen
@ 2026-11-20 7:41 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-20 7:41 UTC (permalink / raw)
To: Tapio Reijonen; +Cc: linux-serial
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] serial: max310x: lockless access to xmit_fifo in
max310x_handle_tx() [1]
--
commit b6f9f6d1c6352edbe4d9493e77cb10be6d1f66a0
Author: Tapio Reijonen <tapio.reijonen@vaisala.com>
serial: max310x: hold the port lock around the CTS change
This commit acquires the UART port lock around uart_handle_cts_change() in
max310x_port_irq().
> diff --git a/drivers/tty/serial/max310x.c b/drivers/tty/serial/max310x.c
> index 179f5f4cc7b2f..b2d887316957c 100644
> --- a/drivers/tty/serial/max310x.c
> +++ b/drivers/tty/serial/max310x.c
> @@ -1043,7 +1043,8 @@ static irqreturn_t max310x_port_irq(struct max310x_port *s, int portno)
>
> if (ists & MAX310X_IRQ_CTS_BIT) {
> lsr = max310x_port_read(port, MAX310X_LSR_IRQSTS_REG);
> - uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT);
> + scoped_guard(uart_port_lock_irqsave, port)
> + uart_handle_cts_change(port, lsr & MAX310X_LSR_CTS_BIT);
> }
> if (rxlen)
> max310x_handle_rx(port, rxlen);
[Severity: High]
This problem wasn't introduced by this patch, but does max310x_handle_tx()
also race on tport->xmit_fifo and port->icount.tx without holding port->lock
[1]?
drivers/tty/serial/max310x.c:max310x_handle_tx() {
...
while (!kfifo_is_empty(&tport->xmit_fifo)) {
...
to_send = kfifo_out_linear_ptr(&tport->xmit_fifo, &tail, txlen);
max310x_batch_write(port, tail, to_send);
uart_xmit_advance(port, to_send);
}
if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
uart_write_wakeup(port);
}
Callers such as uart_write() modify tport->xmit_fifo under port->lock, while
the tx_work worker drains it and calls uart_xmit_advance() and
uart_write_wakeup() locklessly, which can corrupt the transmit FIFO state
and send garbage or drop characters.
[1] https://sashiko.dev/#/bug/linux-10a0ea86-42c1-439b-aa6f-385dd9c88f23
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-max310x-teardown-fixes-v1-0-eb52c8b612df@vaisala.com?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread