Linux Serial subsystem development
 help / color / mirror / Atom feed
* [PATCH v2] serial: mvebu-uart: do not leave a stale port behind when probe fails
@ 2026-12-03 11:11 Arina Kharlamova via B4 Relay
  2026-12-03 11:27 ` sashiko-bot
  2026-12-03 11:39 ` Miquel Raynal
  0 siblings, 2 replies; 3+ messages in thread
From: Arina Kharlamova via B4 Relay @ 2026-12-03 11:11 UTC (permalink / raw)
  To: Pali Rohár, Greg Kroah-Hartman, Jiri Slaby, Gregory CLEMENT,
	Wilson Ding
  Cc: Miquel Raynal, linux-kernel, linux-serial, Arina Kharlamova

From: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>

mvebu_uart_probe() fills the static mvebu_uart_ports[] entry with the
devm mapping and the devm private data before it gets the clock and
the interrupts, and keeps them if uart_add_one_port() fails. On any of
these failures devres unmaps the registers and frees the private data,
but the static port keeps membase, mapbase and private_data.

mvebu_uart_console_setup() only checks membase and mapbase. When
another port registers the console first, for example with
console=ttyMV1 and ttyMV1 still waiting for its clock, the setup
callback uses the stale port, accesses the unmapped registers and
dereferences the freed private data.

Keep the mapping in a local variable, test is_ext through mvuart
instead of the port, and store membase, mapbase and private_data in
the port only after the last step that can fail before
uart_add_one_port(). Clear them again if uart_add_one_port() fails.

Fixes: 30530791a7a0 ("serial: mvebu-uart: initial support for Armada-3700 serial port")
Signed-off-by: Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>
---
The static port array keeps pointers into devm memory after a failed
or deferred probe. This patch moves the assignment of membase, mapbase
and private_data after the clock and the interrupts and clears them
when uart_add_one_port() fails. It is independent of my earlier clock
patch for this driver; both add the same ret variable, so the one
applied second needs git am -3.

I noticed this while working on "serial: mvebu-uart: disable the clock
when probe fails":
https://lore.kernel.org/r/20261129-s2p-v08-v1-1-342b7170e692@mbzuai.ac.ae
The Sashiko review pointed at it. With the help of an AI coding
assistant (Claude); I reviewed the analysis and the fix.
Compile-tested only, not tested on hardware.
---
Changes in v2:
- Set private_data after the clock and the interrupts too, and clear
  membase, mapbase and private_data when uart_add_one_port() fails
  (Sashiko review).
- Fixes: point at the commit that added the uart_add_one_port() path.
- Drop the Reviewed-by from Miquel Raynal, the patch changed.
- Link to v1: https://patch.msgid.link/20261201-v08b-v1-1-add40eb9ffee@mbzuai.ac.ae
---
 drivers/tty/serial/mvebu-uart.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/drivers/tty/serial/mvebu-uart.c b/drivers/tty/serial/mvebu-uart.c
index 8a61517d81f2..5ea513555998 100644
--- a/drivers/tty/serial/mvebu-uart.c
+++ b/drivers/tty/serial/mvebu-uart.c
@@ -868,10 +868,11 @@ static int mvebu_uart_probe(struct platform_device *pdev)
 {
 	const struct of_device_id *match = of_match_device(mvebu_uart_of_match,
 							   &pdev->dev);
+	void __iomem *base;
 	struct uart_port *port;
 	struct mvebu_uart *mvuart;
 	struct resource *reg;
-	int id, irq;
+	int id, irq, ret;
 
 	/* Assume that all UART ports have a DT alias or none has */
 	id = of_alias_get_id(pdev->dev.of_node, "serial");
@@ -908,10 +909,9 @@ static int mvebu_uart_probe(struct platform_device *pdev)
 	port->irq        = 0;
 	port->irqflags   = 0;
 
-	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &reg);
-	if (IS_ERR(port->membase))
-		return PTR_ERR(port->membase);
-	port->mapbase    = reg->start;
+	base = devm_platform_get_and_ioremap_resource(pdev, 0, &reg);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
 
 	mvuart = devm_kzalloc_obj(&pdev->dev, struct mvebu_uart);
 	if (!mvuart)
@@ -921,7 +921,6 @@ static int mvebu_uart_probe(struct platform_device *pdev)
 	mvuart->data = (struct mvebu_uart_driver_data *)match->data;
 	mvuart->port = port;
 
-	port->private_data = mvuart;
 	platform_set_drvdata(pdev, mvuart);
 
 	/* Get fixed clock frequency */
@@ -930,7 +929,7 @@ static int mvebu_uart_probe(struct platform_device *pdev)
 		if (PTR_ERR(mvuart->clk) == -EPROBE_DEFER)
 			return PTR_ERR(mvuart->clk);
 
-		if (IS_EXTENDED(port)) {
+		if (mvuart->data->is_ext) {
 			dev_err(&pdev->dev, "unable to get UART clock\n");
 			return PTR_ERR(mvuart->clk);
 		}
@@ -966,12 +965,23 @@ static int mvebu_uart_probe(struct platform_device *pdev)
 		mvuart->irq[UART_TX_IRQ] = irq;
 	}
 
+	port->private_data = mvuart;
+	port->membase = base;
+	port->mapbase = reg->start;
+
 	/* UART Soft Reset*/
 	writel(CTRL_SOFT_RST, port->membase + UART_CTRL(port));
 	udelay(1);
 	writel(0, port->membase + UART_CTRL(port));
 
-	return uart_add_one_port(&mvebu_uart_driver, port);
+	ret = uart_add_one_port(&mvebu_uart_driver, port);
+	if (ret) {
+		port->mapbase = 0;
+		port->membase = NULL;
+		port->private_data = NULL;
+	}
+
+	return ret;
 }
 
 static struct mvebu_uart_driver_data uart_std_driver_data = {

---
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
change-id: 20261201-v08b-0fdf4a54e942

Best regards,
--  
Arina Kharlamova <arina.kharlamova@mbzuai.ac.ae>



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

end of thread, other threads:[~2026-12-03 11:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 11:11 [PATCH v2] serial: mvebu-uart: do not leave a stale port behind when probe fails Arina Kharlamova via B4 Relay
2026-12-03 11:27 ` sashiko-bot
2026-12-03 11:39 ` Miquel Raynal

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