[PATCH 2/3] serial: max310x: unwind only the registered ports when probe fails
From: Tapio Reijonen
Date: Fri Nov 20 2026 - 02:34:08 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Tapio Reijonen <tapio.reijonen@xxxxxxxxxxx>
---
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