Skip to contentSkip to conversation

serial: mcf: add devicetree support

PATCH In review Greg Ungerer 6 participants 22 messages Oct 13 – Oct 27

Subsystems (4)
lore
Share & tools
lore

Greg Ungerer

author maintainer
#0 lore raw

cover serial: mcf: add devicetree support

This patch series is designed to add devicetree support to the ColdFire serial driver (mcf.c). The m68k/ColdFire arch support is not devicetree capable itself yet, but this is an important first step.

The first patch is a cleanup, the second is about normalizing the existing platform device setup. The third changes the driver to always use dynamic memory allocation for devices, the forth and fifth patches add the devicetree support itself.

---
 b/Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml |   43 ++
 b/arch/m68k/coldfire/device.c                                |  179 +++++++----
 b/arch/m68k/include/asm/mcfuart.h                            |   10 
 b/drivers/tty/serial/mcf.c                                   |    4 
 drivers/tty/serial/mcf.c                                     |   93 +++--
 5 files changed, 235 insertions(+), 94 deletions(-)
author maintainer
#1 lore raw

PATCH 1/5 serial: mcf: update author email address

The copyright and author email address of [email protected] is no longer valid. Change to a current valid email address, [email protected].

---
 drivers/tty/serial/mcf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/mcf.c +2 -2
index 93e7dda4d39a..2b862dfe39ab 100644
@@ -4,7 +4,7 @@
 /*
  *	mcf.c -- Freescale ColdFire UART driver
  *
- *	(C) Copyright 2003-2007, Greg Ungerer <[email protected]>
+ *	(C) Copyright 2003-2007, Greg Ungerer <[email protected]>
  */
 
 /****************************************************************************/
@@ -654,7 +654,7 @@ static void __exit mcf_exit(void)
 module_init(mcf_init);
 module_exit(mcf_exit);
 
-MODULE_AUTHOR("Greg Ungerer <[email protected]>");
+MODULE_AUTHOR("Greg Ungerer <[email protected]>");
 MODULE_DESCRIPTION("Freescale ColdFire UART driver");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("platform:mcfuart");
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/5] serial: mcf: update author email address

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

The copyright and author email address of [email protected] is no longer valid. Change to a current valid email address, [email protected].

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/5 serial: mcf: convert to use proper platform resources

In reply to Greg Ungerer

The ColdFire SoC device setup code supplies a non-standard array of hardware address information (memory address and IRQ) for initializing the UARTS - which it does as a single blob. The mcf serial probe function processes the whole set in one go from a single platform entry.

Convert this setup to use the proper resource structures, with one for each UART present on a platform. Modify the probe function to process a single platform device per call, and use the proper platform resource API to get the memory and IRQ addresses. This results in the proper entries now being present in /proc/iomem.

---
 arch/m68k/coldfire/device.c     | 179 +++++++++++++++++++++++---------
 arch/m68k/include/asm/mcfuart.h |  10 --
 drivers/tty/serial/mcf.c        |  45 ++++----
 3 files changed, 154 insertions(+), 80 deletions(-)
arch/m68k/coldfire/device.c +130 -49
index 9a0258acd998..e32bc3322c97 100644
@@ -18,8 +18,8 @@
 #include <asm/traps.h>
 #include <asm/coldfire.h>
 #include <asm/mcfsim.h>
-#include <asm/mcfuart.h>
 #include <asm/mcfqspi.h>
+#include <linux/platform_device.h>
 #include <linux/platform_data/edma.h>
 #include <linux/platform_data/dma-mcf-edma.h>
 #include <linux/platform_data/mmc-esdhc-mcf.h>
@@ -27,71 +27,125 @@
 /*
  *	All current ColdFire parts contain from 2, 3, 4 or 10 UARTS.
  */
-static struct mcf_platform_uart mcf_uart_platform_data[] = {
-	{
-		.mapbase	= MCFUART_BASE0,
-		.irq		= MCF_IRQ_UART0,
-	},
-	{
-		.mapbase	= MCFUART_BASE1,
-		.irq		= MCF_IRQ_UART1,
-	},
+static struct resource mcf_uart0_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE0, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART0),
+};
+static struct platform_device mcf_uart0 = {
+	.name			= "mcfuart",
+	.id			= 0,
+	.num_resources		= ARRAY_SIZE(mcf_uart0_resources),
+	.resource		= mcf_uart0_resources,
+};
+
+#ifdef MCFUART_BASE1
+static struct resource mcf_uart1_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE1, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART1),
+};
+static struct platform_device mcf_uart1 = {
+	.name			= "mcfuart",
+	.id			= 1,
+	.num_resources		= ARRAY_SIZE(mcf_uart1_resources),
+	.resource		= mcf_uart1_resources,
+};
+#endif
 #ifdef MCFUART_BASE2
-	{
-		.mapbase	= MCFUART_BASE2,
-		.irq		= MCF_IRQ_UART2,
-	},
+static struct resource mcf_uart2_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE2, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART2),
+};
+static struct platform_device mcf_uart2 = {
+	.name			= "mcfuart",
+	.id			= 2,
+	.num_resources		= ARRAY_SIZE(mcf_uart2_resources),
+	.resource		= mcf_uart2_resources,
+};
 #endif
 #ifdef MCFUART_BASE3
-	{
-		.mapbase	= MCFUART_BASE3,
-		.irq		= MCF_IRQ_UART3,
-	},
+static struct resource mcf_uart3_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE3, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART3),
+};
+static struct platform_device mcf_uart3 = {
+	.name			= "mcfuart",
+	.id			= 3,
+	.num_resources		= ARRAY_SIZE(mcf_uart3_resources),
+	.resource		= mcf_uart3_resources,
+};
 #endif
 #ifdef MCFUART_BASE4
-	{
-		.mapbase	= MCFUART_BASE4,
-		.irq		= MCF_IRQ_UART4,
-	},
+static struct resource mcf_uart4_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE4, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART4),
+};
+static struct platform_device mcf_uart4 = {
+	.name			= "mcfuart",
+	.id			= 4,
+	.num_resources		= ARRAY_SIZE(mcf_uart4_resources),
+	.resource		= mcf_uart4_resources,
+};
 #endif
 #ifdef MCFUART_BASE5
-	{
-		.mapbase	= MCFUART_BASE5,
-		.irq		= MCF_IRQ_UART5,
-	},
+static struct resource mcf_uart5_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE5, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART5),
+};
+static struct platform_device mcf_uart5 = {
+	.name			= "mcfuart",
+	.id			= 5,
+	.num_resources		= ARRAY_SIZE(mcf_uart5_resources),
+	.resource		= mcf_uart5_resources,
+};
 #endif
 #ifdef MCFUART_BASE6
-	{
-		.mapbase	= MCFUART_BASE6,
-		.irq		= MCF_IRQ_UART6,
-	},
+static struct resource mcf_uart6_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE6, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART6),
+};
+static struct platform_device mcf_uart6 = {
+	.name			= "mcfuart",
+	.id			= 6,
+	.num_resources		= ARRAY_SIZE(mcf_uart6_resources),
+	.resource		= mcf_uart6_resources,
+};
 #endif
 #ifdef MCFUART_BASE7
-	{
-		.mapbase	= MCFUART_BASE7,
-		.irq		= MCF_IRQ_UART7,
-	},
+static struct resource mcf_uart7_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE7, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART7),
+};
+static struct platform_device mcf_uart7 = {
+	.name			= "mcfuart",
+	.id			= 7,
+	.num_resources		= ARRAY_SIZE(mcf_uart7_resources),
+	.resource		= mcf_uart7_resources,
+};
 #endif
 #ifdef MCFUART_BASE8
-	{
-		.mapbase	= MCFUART_BASE8,
-		.irq		= MCF_IRQ_UART8,
-	},
+static struct resource mcf_uart8_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE8, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART8),
+};
+static struct platform_device mcf_uart8 = {
+	.name			= "mcfuart",
+	.id			= 8,
+	.num_resources		= ARRAY_SIZE(mcf_uart8_resources),
+	.resource		= mcf_uart8_resources,
+};
 #endif
 #ifdef MCFUART_BASE9
-	{
-		.mapbase	= MCFUART_BASE9,
-		.irq		= MCF_IRQ_UART9,
-	},
-#endif
-	{ },
+static struct resource mcf_uart9_resources[] = {
+	DEFINE_RES_MEM(MCFUART_BASE9, 0x80),
+	DEFINE_RES_IRQ(MCF_IRQ_UART9),
 };
-
-static struct platform_device mcf_uart = {
+static struct platform_device mcf_uart9 = {
 	.name			= "mcfuart",
-	.id			= 0,
-	.dev.platform_data	= mcf_uart_platform_data,
+	.id			= 9,
+	.num_resources		= ARRAY_SIZE(mcf_uart9_resources),
+	.resource		= mcf_uart9_resources,
 };
+#endif
 
 #ifdef MCFFEC_BASE0
 
@@ -623,7 +677,34 @@ static struct platform_device mcf_flexcan0 = {
 #endif /* MCFFLEXCAN_SIZE */
 
 static struct platform_device *mcf_devices[] __initdata = {
-	&mcf_uart,
+	&mcf_uart0,
+#ifdef MCFUART_BASE1
+	&mcf_uart1,
+#endif
+#ifdef MCFUART_BASE2
+	&mcf_uart2,
+#endif
+#ifdef MCFUART_BASE3
+	&mcf_uart3,
+#endif
+#ifdef MCFUART_BASE4
+	&mcf_uart4,
+#endif
+#ifdef MCFUART_BASE5
+	&mcf_uart5,
+#endif
+#ifdef MCFUART_BASE6
+	&mcf_uart6,
+#endif
+#ifdef MCFUART_BASE7
+	&mcf_uart7,
+#endif
+#ifdef MCFUART_BASE8
+	&mcf_uart8,
+#endif
+#ifdef MCFUART_BASE9
+	&mcf_uart9,
+#endif
 #ifdef MCFFEC_BASE0
 	&mcf_fec0,
 #endif
arch/m68k/include/asm/mcfuart.h +0 -10
index a1f35352f328..c80b022c1ebb 100644
@@ -13,16 +13,6 @@
 #define	mcfuart_h
 /****************************************************************************/
 
-#include <linux/serial_core.h>
-#include <linux/platform_device.h>
-
-struct mcf_platform_uart {
-	unsigned long	mapbase;	/* Physical address base */
-	void __iomem	*membase;	/* Virtual address if mapped */
-	unsigned int	irq;		/* Interrupt vector */
-	unsigned int	uartclk;	/* UART clock rate */
-};
-
 /*
  *	Define the ColdFire UART register set addresses.
  */
drivers/tty/serial/mcf.c +24 -21

55 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH 2/5] serial: mcf: convert to use proper platform resources

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] tty: mcf: unconditional removal of all UART ports in mcf_remove() - [High] tty: mcf: ERR_PTR…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] tty: mcf: unconditional removal of all UART ports in mcf_remove()
- [High] tty: mcf: ERR_PTR stored in membase bypasses uninitialized check
- [Medium] tty: mcf: tautological error check on platform_get_irq()
--

commit c5cb7916e270b19e0758d11e193eb23c71763eca
Author: Greg Ungerer <[email protected]>

serial: mcf: convert to use proper platform resources

The ColdFire SoC device setup code supplies a non-standard array of hardware address information (memory address and IRQ) for initializing the UARTS - which it does as a single blob. The mcf serial probe function processes the whole set in one go from a single platform entry.

[ ... ]

Quoted 4 lines
drivers/tty/serial/mcf.c +0 -0
index 2b862dfe39ab3..3651910dff99e 100644

[ ... ]

Quoted 25 lines
drivers/tty/serial/mcf.c +4 -14
@@ -570,31 +570,34 @@ static struct uart_driver mcf_driver = {
 
 static int mcf_probe(struct platform_device *pdev)
 {
-	struct mcf_platform_uart *platp = dev_get_platdata(&pdev->dev);
 	struct uart_port *port;
-	int i;
+	struct resource *res;
 
-	for (i = 0; ((i < MCF_MAXPORTS) && (platp[i].mapbase)); i++) {
-		port = &mcf_ports[i].port;
+	if (pdev->id >= MCF_MAXPORTS)
+		return -ENODEV;
+	port = &mcf_ports[pdev->id].port;
 
-		port->line = i;
-		port->type = PORT_MCF;
-		port->mapbase = platp[i].mapbase;
-		port->membase = (platp[i].membase) ? platp[i].membase :
-			(unsigned char __iomem *) platp[i].mapbase;
-		port->dev = &pdev->dev;
-		port->iotype = SERIAL_IO_MEM;
-		port->irq = platp[i].irq;
-		port->uartclk = MCF_BUSCLK;
-		port->ops = &mcf_uart_ops;

drivers/tty/serial/mcf.c static struct uart_driver mcf_driver = { · line 582

Quoted 10 lines
drivers/tty/serial/mcf.c +3 -7
-		port->flags = UPF_BOOT_AUTOCONF;
-		port->rs485_config = mcf_config_rs485;
-		port->rs485_supported = mcf_rs485_supported;
-		port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MCF_CONSOLE);
-
-		uart_add_one_port(&mcf_driver, port);
-	}
+	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+	if (IS_ERR(port->membase))
+		return PTR_ERR(port->membase);

[Severity: High]
Since port is a pointer to the global mcf_ports array, assigning an ERR_PTR to port->membase leaves the error pointer in the global state if devm_platform_get_and_ioremap_resource() fails.

Later, when the early console is initialized, mcf_console_setup() checks if membase is valid using:

    if (port->membase == 0)
        return -ENODEV;

Since an ERR_PTR is not zero, this check will pass, which could cause a kernel panic when mcf_console_write() dereferences the ERR_PTR during a printk.

Could we store the mapped address in a local variable and only assign it to port->membase on success?

drivers/tty/serial/mcf.c static struct uart_driver mcf_driver = { · line 587

Quoted 5 lines
drivers/tty/serial/mcf.c +5 -0
+	port->mapbase = res->start;
+
+	port->irq = platform_get_irq(pdev, 0);
+	if (port->irq < 0)
+		return port->irq;

[Severity: Medium]
In struct uart_port, irq is defined as an unsigned int. Does the check (port->irq < 0) undergo unsigned comparison and become tautologically false?

If platform_get_irq() fails and returns a negative error code like -ENXIO, it seems this error would be swallowed, and the port would be registered with a bogus, very large IRQ number.

Quoted 4 lines
drivers/tty/serial/mcf.c +4 -0
+	port->line = pdev->id;
+	port->type = PORT_MCF;
+	port->dev = &pdev->dev;
+	port->iotype = SERIAL_IO_MEM;

drivers/tty/serial/mcf.c static struct uart_driver mcf_driver = { · line 602

Quoted 10 lines
drivers/tty/serial/mcf.c +8 -0
+	port->uartclk = MCF_BUSCLK;
+	port->ops = &mcf_uart_ops;
+	port->flags = UPF_BOOT_AUTOCONF;
+	port->rs485_config = mcf_config_rs485;
+	port->rs485_supported = mcf_rs485_supported;
+	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MCF_CONSOLE);
+
+	uart_add_one_port(&mcf_driver, port);
 	return 0;
 }

[Severity: High]
With the conversion of mcf_probe() to process a single platform device per call instead of a single blob, does mcf_remove() also need to be updated?

Looking at mcf_remove() in drivers/tty/serial/mcf.c:

static void mcf_remove(struct platform_device *pdev)

{
	struct uart_port *port;
	int i;

	for (i = 0; (i < MCF_MAXPORTS); i++) {
		port = &mcf_ports[i].port;
		if (port)
			uart_remove_one_port(&mcf_driver, port);
	}
}

It appears this loop still unconditionally unregisters all 10 ports, regardless of which pdev is being removed. If a single device is unbound via sysfs, will this disrupt other devices or lead to list corruption by calling uart_remove_one_port() on uninitialized structs or multiple times on the same port?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH 3/5 serial: mcf: dynamically allocate memory

In reply to Greg Ungerer

Modify the ColdFire serial driver to dynamically allocate the local uart port structures as required at init time. Currently it uses a fixed size array - a little wasteful of memory on most parts with only a couple of UARTs.

---
 drivers/tty/serial/mcf.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)
drivers/tty/serial/mcf.c +24 -12
index 3651910dff99..7cbe43cbe3a0 100644
@@ -55,6 +55,14 @@ struct mcf_uart {
 	unsigned char		imr;		/* Local IMR mirror */
 };
 
+/*
+ * Define maximum number of UARTs this driver will support.
+ * This is set to maximum number on any known ColdFire SoC.
+ */
+#define MCF_MAXPORTS            10
+
+static struct mcf_uart *mcf_ports[MCF_MAXPORTS];
+
 /****************************************************************************/
 
 static unsigned int mcf_tx_empty(struct uart_port *port)
@@ -462,19 +470,19 @@ static const struct uart_ops mcf_uart_ops = {
 	.verify_port	= mcf_verify_port,
 };
 
-static struct mcf_uart mcf_ports[10];
-
-#define	MCF_MAXPORTS	ARRAY_SIZE(mcf_ports)
-
 /****************************************************************************/
 #if defined(CONFIG_SERIAL_MCF_CONSOLE)
 /****************************************************************************/
 
 static void mcf_console_putc(struct console *co, const char c)
 {
-	struct uart_port *port = &(mcf_ports + co->index)->port;
+	struct uart_port *port;
 	int i;
 
+	if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
+		co->index = 0;
+	port = &mcf_ports[co->index]->port;
+
 	for (i = 0; (i < 0x10000); i++) {
 		if (readb(port->membase + MCFUART_USR) & MCFUART_USR_TXREADY)
 			break;
@@ -509,9 +517,9 @@ static int __init mcf_console_setup(struct console *co, char *options)
 
 	if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
 		co->index = 0;
-	port = &mcf_ports[co->index].port;
-	if (port->membase == 0)
+	if (mcf_ports[co->index] == NULL)
 		return -ENODEV;
+	port = &mcf_ports[co->index]->port;
 
 	if (options)
 		uart_parse_options(options, &baud, &parity, &bits, &flow);
@@ -572,10 +580,16 @@ static int mcf_probe(struct platform_device *pdev)
 {
 	struct uart_port *port;
 	struct resource *res;
+	struct mcf_uart *mp;
 
 	if (pdev->id >= MCF_MAXPORTS)
 		return -ENODEV;
-	port = &mcf_ports[pdev->id].port;
+
+	mp = devm_kzalloc(&pdev->dev, sizeof(*mp), GFP_KERNEL);
+	if (mp == NULL)
+		return -ENOMEM;
+	mcf_ports[pdev->id] = mp;
+	port = &mp->port;
 
 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
 	if (IS_ERR(port->membase))
@@ -605,13 +619,11 @@ static int mcf_probe(struct platform_device *pdev)
 
 static void mcf_remove(struct platform_device *pdev)
 {
-	struct uart_port *port;
 	int i;
 
 	for (i = 0; (i < MCF_MAXPORTS); i++) {
-		port = &mcf_ports[i].port;
-		if (port)
-			uart_remove_one_port(&mcf_driver, port);
+		if (mcf_ports[i])
+			uart_remove_one_port(&mcf_driver, &mcf_ports[i]->port);
 	}
 }
 
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 3/5] serial: mcf: dynamically allocate memory

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] tty: mcf: Use-After-Free and double-unregister due to dangling pointers and global array loop…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] tty: mcf: Use-After-Free and double-unregister due to dangling pointers and global array loop
- [High] tty: mcf: Premature console publication causes NULL pointer dereference
- [High] tty: serial: mcf: Use-After-Free on probe error and driver removal
--

commit cc5afe1f9a1d1092ba4a245994a51d452fb2379c
Author: Greg Ungerer <[email protected]>

serial: mcf: dynamically allocate memory

Modify the ColdFire serial driver to dynamically allocate the local uart port structures as required at init time. Currently it uses a fixed size array - a little wasteful of memory on most parts with only a couple of UARTs.

Quoted 4 lines
drivers/tty/serial/mcf.c +0 -0
index 3651910dff99e..7cbe43cbe3a0e 100644

[ ... ]

drivers/tty/serial/mcf.c static int __init mcf_console_setup(struct console *co, char *options) · line 522

Quoted 9 lines
drivers/tty/serial/mcf.c +2 -2
@@ -509,9 +517,9 @@ static int __init mcf_console_setup(struct console *co, char *options)
 
 	if ((co->index < 0) || (co->index >= MCF_MAXPORTS))
 		co->index = 0;
-	port = &mcf_ports[co->index].port;
-	if (port->membase == 0)
+	if (mcf_ports[co->index] == NULL)
 		return -ENODEV;
+	port = &mcf_ports[co->index]->port;

[Severity: High]
Could this change lead to a NULL pointer dereference if console setup runs concurrently with device probe?

In mcf_probe(), the new port structure is allocated and assigned to the global mcf_ports[] array before port->membase is initialized via ioremap. Since mcf_console_setup() no longer checks if port->membase is zero, it can successfully complete before the mapping is done. If a printk occurs in this window, mcf_console_putc() will dereference the NULL membase when attempting to read the UART status.

[ ... ]

Quoted 8 lines
drivers/tty/serial/mcf.c +1 -0
@@ -572,10 +580,16 @@ static int mcf_probe(struct platform_device *pdev)
 {
 	struct uart_port *port;
 	struct resource *res;
+	struct mcf_uart *mp;
 
 	if (pdev->id >= MCF_MAXPORTS)
 		return -ENODEV;

drivers/tty/serial/mcf.c static int mcf_probe(struct platform_device *pdev) · line 595

Quoted 10 lines
drivers/tty/serial/mcf.c +6 -1
-	port = &mcf_ports[pdev->id].port;
+
+	mp = devm_kzalloc(&pdev->dev, sizeof(*mp), GFP_KERNEL);
+	if (mp == NULL)
+		return -ENOMEM;
+	mcf_ports[pdev->id] = mp;
+	port = &mp->port;
 
 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
 	if (IS_ERR(port->membase))

[Severity: High]
Does this leak a dangling pointer into the global mcf_ports array if probe fails?

The devm_kzalloc() pointer is assigned to mcf_ports[pdev->id] before devm_platform_get_and_ioremap_resource() or platform_get_irq() are called. If either of those functions fail, mcf_probe() returns an error, and the devres framework will free the memory. However, mcf_ports[pdev->id] is never cleared, leaving a dangling pointer that can be dereferenced later.

[ ... ]

Quoted 5 lines
drivers/tty/serial/mcf.c +0 -1
@@ -605,13 +619,11 @@ static int mcf_probe(struct platform_device *pdev)
 
 static void mcf_remove(struct platform_device *pdev)
 {
-	struct uart_port *port;

drivers/tty/serial/mcf.c static int mcf_probe(struct platform_device *pdev) · line 628

Quoted 10 lines
drivers/tty/serial/mcf.c +2 -3
 	int i;
 
 	for (i = 0; (i < MCF_MAXPORTS); i++) {
-		port = &mcf_ports[i].port;
-		if (port)
-			uart_remove_one_port(&mcf_driver, port);
+		if (mcf_ports[i])
+			uart_remove_one_port(&mcf_driver, &mcf_ports[i]->port);
 	}
 }

[Severity: High]
Can this loop cause a use-after-free and improperly unregister all ports when a single device is removed?

During mcf_remove(), the code iterates through the entire mcf_ports array up to MCF_MAXPORTS and calls uart_remove_one_port() on every non-NULL entry, rather than just the port associated with the pdev being removed.

Additionally, it does not clear mcf_ports[i] back to NULL. When mcf_remove() finishes, devres will free the memory associated with the removed pdev. If another UART device is subsequently unbound, mcf_remove() will run again, iterate over the array, and call uart_remove_one_port() using the dangling pointer from the previously freed device.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#7 lore raw

PATCH 4/5 dt-bindings: serial: create binding for mcf-uart

In reply to Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

---
 .../bindings/serial/fsl,mcf-uart.yaml         | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +43 -0
new file mode 100644
index 000000000000..d1dba518f67a
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/fsl-mcf-uart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Freescale ColdFire UART
+
+maintainers:
+  - Greg Ungerer <[email protected]>
+
+allof:
+  - $ref: serial.yaml#
+
+properties:
+  compatible:
+    const: fsl,mcf-uart
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    uart0: serial@fc060000 {
+        compatible = "fsl,mcf-uart";
+        reg = <0xfc060000 0x80>;
+        interrupts = <90>;
+        clocks = <&coreclk>;
+    };
Signature · 1 line
2.55.0
#8 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] dt-bindings: $id path mismatch breaks schema cross-referencing - [Medium] dt-bindings: schema…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] dt-bindings: $id path mismatch breaks schema cross-referencing
- [Medium] dt-bindings: schema keyword typo allof prevents base serial schema evaluation --

commit c5bd98e903d69d54479e4a7058d0ad59884cfb54
Author: Greg Ungerer <[email protected]>

dt-bindings: serial: create binding for mcf-uart

This commit creates documentation for a new binding to support the Freescale ColdFire UART hardware block. This hardware block is found on M68K derived Freescale ColdFire family of SoC devices.

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 4

Quoted 10 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +4 -0
new file mode 100644
index 0000000000000..d1dba518f67af
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/fsl-mcf-uart.yaml#

[Severity: Medium]
Will this schema mismatch cause a regression by breaking cross referencing?

In Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml, the file is named with a comma, but the id is defined with a hyphen.

The device tree bindings subsystem guidelines require that the path in the id field exactly match the file path relative to Documentation/devicetree/bindings/. The dt_binding_check tooling will encounter this mismatch and silently skip or fail validation because the paths do not correspond.

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 13

Quoted 9 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +9 -0
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Freescale ColdFire UART
+
+maintainers:
+  - Greg Ungerer <[email protected]>
+
+allof:
+  - $ref: serial.yaml#

[Severity: Medium]
Could this typo cause a regression where the base serial schema is not evaluated?

JSON Schema keywords are case-sensitive. The binding uses allof instead of allOf, meaning the reference directive is silently ignored.

When running dt_binding_check on device trees utilizing this compatible string, it will not enforce necessary properties or allow standard UART properties because it fails to inherit the base serial schema.

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

In reply to Greg Ungerer
Quoted 5 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

You never tested this, so limited review.

Quoted 20 lines
---
 .../bindings/serial/fsl,mcf-uart.yaml         | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +7 -0
new file mode 100644
index 000000000000..d1dba518f67a
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/fsl-mcf-uart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Freescale ColdFire UART

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 17

Quoted 10 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +10 -0
+
+maintainers:
+  - Greg Ungerer <[email protected]>
+
+allof:
+  - $ref: serial.yaml#
+
+properties:
+  compatible:
+    const: fsl,mcf-uart

What is the SoC name? MCF? Compatibles must be specific (see writing bindings doc).

Quoted 11 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +11 -0
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+required:

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 38

Quoted 10 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +10 -0
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    uart0: serial@fc060000 {

Drop unused label

Best regards,
Krzysztof

author maintainer
#10 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Quoted 8 lines · Krzysztof Kozlowski
Quoted 5 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

Quoted 24 lines
Quoted 24 lines
---
 .../bindings/serial/fsl,mcf-uart.yaml         | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +12 -0
new file mode 100644
index 000000000000..d1dba518f67a
@@ -0,0 +1,43 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/serial/fsl-mcf-uart.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Freescale ColdFire UART
+
+maintainers:
+  - Greg Ungerer <[email protected]>
+
+allof:
Quoted 8 lines

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 17

Quoted 5 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +5 -0
+  - $ref: serial.yaml#
+
+properties:
+  compatible:
+    const: fsl,mcf-uart

What is the SoC name? MCF? Compatibles must be specific (see writing bindings doc).

Yes. MCF = "Motorola ColdFire". It is the SoC prefix used for naming of all ColdFire parts (which is dozens of variants):

MCF5206
MCF5208
MCF5307
MCF54415
etc

Even when Motorola became Freescale they stuck with MCF on all ColdFire parts.

Quoted 15 lines
Quoted 15 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +15 -0
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
Quoted 8 lines

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 38

Quoted 6 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +6 -0
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    uart0: serial@fc060000 {

Drop unused label

Ok, will drop.

Thanks for the review.

Regards
Greg

#11 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Quoted 7 lines · Greg Ungerer
Quoted 5 lines · Krzysztof Kozlowski
Quoted 4 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

You cannot run a binding. It's impossible, otherwise show me how do you execute that code.

But as proven already on the lists, no it WAS NOT tested. You have clear a warning.

Otherwise you claim that tests shown errors and then you decided to still send it with errors, so then th enext question: why do you send code which your tests tell it is wrong?

Also, this misses to cc actual maintainers, so I don't have it even in my mailbox except Patchwork.

Best regards,
Krzysztof

author maintainer
#12 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Quoted 8 lines · Krzysztof Kozlowski
Quoted 7 lines · Greg Ungerer
Quoted 5 lines · Krzysztof Kozlowski
Quoted 4 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

You cannot run a binding. It's impossible, otherwise show me how do you execute that code.

Of course you cannot run a binding. I took your statement to mean that this series as a whole was not tested. Which is wrong.

Quoted 6 lines

But as proven already on the lists, no it WAS NOT tested. You have clear a warning.

Otherwise you claim that tests shown errors and then you decided to still send it with errors, so then th enext question: why do you send code which your tests tell it is wrong?

Sorry, I have no idea what you are talking about.

This series of 5 patches was tested before sending, on multiple targets that use this code (obviously all m68k/ColdFire platforms).

Just in case I missed something I re-ran testing with a fresh 7.3-rc7 code base. All 5 patches applied cleanly, with a clean compile. My testing showed no new problems in normal operation. Granted sashkio did find a couple of error case checking issues that you just do not normally encounter in practice (but I will of course fix those for v2).

So, no, I didn't just send it knowing that it was failing any tests.

Regards
Greg

#13 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Hi Greg,

Quoted 10 lines · Greg Ungerer
Quoted 6 lines · Krzysztof Kozlowski

But as proven already on the lists, no it WAS NOT tested. You have clear a warning.

Otherwise you claim that tests shown errors and then you decided to still send it with errors, so then th enext question: why do you send code which your tests tell it is wrong?

Sorry, I have no idea what you are talking about.

Quoted 8 lines

This series of 5 patches was tested before sending, on multiple targets that use this code (obviously all m68k/ColdFire platforms).

Just in case I missed something I re-ran testing with a fresh 7.3-rc7 code base. All 5 patches applied cleanly, with a clean compile. My testing showed no new problems in normal operation. Granted sashkio did find a couple of error case checking issues that you just do not normally encounter in practice (but I will of course fix those for v2).

make dt_binding_check dtbs_check

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
author maintainer
#14 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Hi Geert,

Quoted 16 lines · Geert Uytterhoeven

Hi Greg,

Quoted 13 lines · Greg Ungerer
Quoted 6 lines · Krzysztof Kozlowski

But as proven already on the lists, no it WAS NOT tested. You have clear a warning.

Otherwise you claim that tests shown errors and then you decided to still send it with errors, so then th enext question: why do you send code which your tests tell it is wrong?

Sorry, I have no idea what you are talking about.

This series of 5 patches was tested before sending, on multiple targets that use this code (obviously all m68k/ColdFire platforms).

Quoted 7 lines
Quoted 5 lines

Just in case I missed something I re-ran testing with a fresh 7.3-rc7 code base. All 5 patches applied cleanly, with a clean compile. My testing showed no new problems in normal operation. Granted sashkio did find a couple of error case checking issues that you just do not normally encounter in practice (but I will of course fix those for v2).

make dt_binding_check dtbs_check

I wasn't aware of that. Thanks for pointing it out! Makes more sense now.

I can see that running it throws up the same 2 errors that sashiko found, so easily fixed for v2.

Regards
Greg

#15 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

In reply to Greg Ungerer

Hi Greg,

Quoted 27 lines · Greg Ungerer
Quoted 8 lines · Krzysztof Kozlowski
Quoted 5 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

Quoted 11 lines

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 17

Quoted 8 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +3 -0
 
+properties:
+  compatible:
+    const: fsl,mcf-uart

What is the SoC name? MCF? Compatibles must be specific (see writing bindings doc).

Yes. MCF = "Motorola ColdFire". It is the SoC prefix used for naming of all ColdFire parts (which is dozens of variants):

Quoted 7 lines
   MCF5206
   MCF5208
   MCF5307
   MCF54415
   etc

Even when Motorola became Freescale they stuck with MCF on all ColdFire parts.

I guess you want:

items:
- enum:
- fsl,mcf5206
- fsl,mcf5208
- fsl,mcf5307
[...]
- const: fsl,mcf-uart

As no new (possibly incompatible) Coldfire parts will appear, a generic fallback looks reasonable to me.

An "mcf" vs. "motorola" vendor prefix might still be a discussion point ;-)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#16 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Quoted 10 lines · Geert Uytterhoeven

I guess you want:

items:
- enum:
- fsl,mcf5206
- fsl,mcf5208
- fsl,mcf5307
[...]
- const: fsl,mcf-uart

Quoted 2 lines

As no new (possibly incompatible) Coldfire parts will appear, a generic fallback looks reasonable to me.

Yes, although still preference is to use one device as a fallback.

Quoted 2 lines

An "mcf" vs. "motorola" vendor prefix might still be a discussion point ;-)

Best regards,
Krzysztof

author maintainer
#17 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

In reply to Geert Uytterhoeven

Hi Geert,

Quoted 46 lines · Geert Uytterhoeven
Quoted 27 lines · Greg Ungerer
Quoted 8 lines · Krzysztof Kozlowski
Quoted 5 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

Quoted 11 lines

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 17

Quoted 8 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +3 -0
 
+properties:
+  compatible:
+    const: fsl,mcf-uart

What is the SoC name? MCF? Compatibles must be specific (see writing bindings doc).

Yes. MCF = "Motorola ColdFire". It is the SoC prefix used for naming of all ColdFire parts (which is dozens of variants):

Quoted 7 lines
   MCF5206
   MCF5208
   MCF5307
   MCF54415
   etc

Even when Motorola became Freescale they stuck with MCF on all ColdFire parts.

I guess you want:

items:
- enum:
- fsl,mcf5206
- fsl,mcf5208
- fsl,mcf5307
[...]
- const: fsl,mcf-uart

Quoted 2 lines

As no new (possibly incompatible) Coldfire parts will appear, a generic fallback looks reasonable to me.

It will be a long list of variants - thanks Freescale. Quite a few of the SoC parts have sub-parts with different single digits, for example:

    mcf54410, mcf54415, mcf54416, mcf54417, mcf54118

At a quick count I can see about 42 variants. I don't mind adding the full list if that is preferred.

Quoted 1 line

An "mcf" vs. "motorola" vendor prefix might still be a discussion point ;-)

At least they didn't rename the chip part numbers when they renamed the company ;-)

Regards
Greg

maintainer
#18 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

Quoted 49 lines · Greg Ungerer

Hi Geert,

Quoted 46 lines · Geert Uytterhoeven
Quoted 27 lines · Greg Ungerer
Quoted 8 lines · Krzysztof Kozlowski
Quoted 5 lines · Greg Ungerer

Create documentation for a new binding to support the Freescale ColdFire UART hardware block. This is found on pretty much all variants of the M68K derived Freescale ColdFire family of System-on-Chip devices.

It is not compatible with any other known UARTs.

You never tested this, so limited review.

It is tested. It is working and running on multiple ColdFire platforms.

Quoted 11 lines

Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml line 17

Quoted 8 lines
Documentation/devicetree/bindings/serial/fsl,mcf-uart.yaml new file +3 -0
 
+properties:
+  compatible:
+    const: fsl,mcf-uart

What is the SoC name? MCF? Compatibles must be specific (see writing bindings doc).

Yes. MCF = "Motorola ColdFire". It is the SoC prefix used for naming of all ColdFire parts (which is dozens of variants):

Quoted 7 lines
   MCF5206
   MCF5208
   MCF5307
   MCF54415
   etc

Even when Motorola became Freescale they stuck with MCF on all ColdFire parts.

I guess you want:

items:
- enum:
- fsl,mcf5206
- fsl,mcf5208
- fsl,mcf5307
[...]
- const: fsl,mcf-uart

Quoted 7 lines
Quoted 2 lines

As no new (possibly incompatible) Coldfire parts will appear, a generic fallback looks reasonable to me.

It will be a long list of variants - thanks Freescale. Quite a few of the SoC parts have sub-parts with different single digits, for example:

    mcf54410, mcf54415, mcf54416, mcf54417, mcf54118

I imagine all of those are just package or marketing differences. Freescale did not have the money to tapeout 5 different parts. Just using one compatible for all of them is fine.

Quoted 2 lines

At a quick count I can see about 42 variants. I don't mind adding the full list if that is preferred.

Just add what you care about or know people use. I imagine that's a much smaller subset.

Probably not many users today of the 5272 I worked on which was designed for ISDN modems.

Rob

#19 lore raw

Re: [PATCH 4/5] dt-bindings: serial: create binding for mcf-uart

In reply to Geert Uytterhoeven
Quoted 3 lines · Geert Uytterhoeven

An "mcf" vs. "motorola" vendor prefix might still be a discussion point ;-)

Motorola Solutions is still trading under that name. Sure, that entity is unrelated to the Coldfire silicon presently marketed NXP, but I would not presume that Motorola Solutions has no interest in this namespace.

author maintainer
#20 lore raw

PATCH 5/5 serial: mcf: add devicetree support

In reply to Greg Ungerer

Add basic support for probing the ColdFire UARTs via a devicetree.

---
 drivers/tty/serial/mcf.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/tty/serial/mcf.c +12 -0
index 7cbe43cbe3a0..01b3ab6e7dba 100644
@@ -21,6 +21,7 @@
 #include <linux/io.h>
 #include <linux/uaccess.h>
 #include <linux/platform_device.h>
+#include <linux/of.h>
 #include <asm/coldfire.h>
 #include <asm/mcfsim.h>
 #include <asm/mcfuart.h>
@@ -62,6 +63,7 @@ struct mcf_uart {
 #define MCF_MAXPORTS            10
 
 static struct mcf_uart *mcf_ports[MCF_MAXPORTS];
+static int mcf_numports;
 
 /****************************************************************************/
 
@@ -582,6 +584,8 @@ static int mcf_probe(struct platform_device *pdev)
 	struct resource *res;
 	struct mcf_uart *mp;
 
+	if (pdev->id == -1)
+		pdev->id = mcf_numports;
 	if (pdev->id >= MCF_MAXPORTS)
 		return -ENODEV;
 
@@ -612,6 +616,8 @@ static int mcf_probe(struct platform_device *pdev)
 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MCF_CONSOLE);
 
 	uart_add_one_port(&mcf_driver, port);
+
+	mcf_numports++;
 	return 0;
 }
 
@@ -629,11 +635,17 @@ static void mcf_remove(struct platform_device *pdev)
 
 /****************************************************************************/
 
+static const struct of_device_id mcf_uart_dt_ids[] = {
+	{ .compatible = "fsl,mcf-uart", },
+	{ }
+};
+
 static struct platform_driver mcf_platform_driver = {
 	.probe		= mcf_probe,
 	.remove		= mcf_remove,
 	.driver		= {
 		.name	= "mcfuart",
+		.of_match_table = mcf_uart_dt_ids,
 	},
 };
 
Signature · 1 line
2.55.0
#21 lore raw
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] tty: serial: mcf: ID collision due to persistent pdev->id mutation on probe deferral - [High] tty…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] tty: serial: mcf: ID collision due to persistent pdev->id mutation on probe deferral
- [High] tty: serial: mcf: dangling pointer in global mcf_ports array on probe error
- [High] tty: serial: mcf: mcf_remove unregisters all ports system-wide instead of unbound device
- [High] serial: mcf: broken port numbering and API violations in mcf_probe
- [Medium] tty: serial: mcf: missing MODULE_DEVICE_TABLE prevents module autoloading
- [Medium] tty: serial: mcf: missing error check for uart_add_one_port
--

commit 61f92ee3964b5b13fd6350da88b718b7350512cd
Author: Greg Ungerer <[email protected]>

serial: mcf: add devicetree support

Add basic support for probing the ColdFire UARTs via a devicetree.

drivers/tty/serial/mcf.c static int mcf_probe(struct platform_device *pdev) · line 588

Quoted 10 lines
drivers/tty/serial/mcf.c +2 -0
index 7cbe43cbe3a0e..01b3ab6e7dba9 100644
@@ -582,6 +584,8 @@ static int mcf_probe(struct platform_device *pdev)
 	struct resource *res;
 	struct mcf_uart *mp;
 
+	if (pdev->id == -1)
+		pdev->id = mcf_numports;

[Severity: High]
Does modifying pdev->id persistently break deferred probe or unbind/rebind cycles?

If the device is unbound and rebound, or if probe defers, it will retain the old ID on the next probe attempt. Since mcf_numports is incremented on success, this can lead to ID exhaustion over multiple unbind/rebind cycles.

Additionally, should of_alias_get_id(pdev->dev.of_node, "serial") be used here to respect standard device tree aliases instead of an auto-incrementing counter?

[Severity: High]
Does this code leave a dangling pointer in the global mcf_ports array if probe fails later?

If a subsequent call (like devm_platform_get_and_ioremap_resource() or platform_get_irq()) fails, the devres subsystem will automatically free 'mp' allocated via devm_kzalloc(), but mcf_ports[pdev->id] is never cleared.

Quoted 3 lines
drivers/tty/serial/mcf.c +0 -0
 	if (pdev->id >= MCF_MAXPORTS)
 		return -ENODEV;
 

[ ... ]

drivers/tty/serial/mcf.c static int mcf_probe(struct platform_device *pdev) · line 618

Quoted 4 lines
drivers/tty/serial/mcf.c +0 -0
@@ -612,6 +616,8 @@ static int mcf_probe(struct platform_device *pdev)
 	port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_MCF_CONSOLE);
 
 	uart_add_one_port(&mcf_driver, port);

[Severity: Medium]
Is it safe to ignore the return value of uart_add_one_port() here?

If registration fails (e.g., out of memory or line conflicts), mcf_numports is still incremented and probe falsely reports success, leaving a partially initialized state.

Quoted 5 lines
drivers/tty/serial/mcf.c +2 -0
+
+	mcf_numports++;
 	return 0;
 }
 

[ ... ]

drivers/tty/serial/mcf.c static void mcf_remove(struct platform_device *pdev) · line 637

Quoted 4 lines
drivers/tty/serial/mcf.c +0 -0
@@ -629,11 +635,17 @@ static void mcf_remove(struct platform_device *pdev)
 
 /****************************************************************************/
 

[Severity: High]
Will mcf_remove() still unregister all ports system-wide when a single device is unbound?

It appears that mcf_remove() unconditionally iterates over all MCF_MAXPORTS and removes every active port, rather than only removing the specific port associated with the unbound device.

drivers/tty/serial/mcf.c static void mcf_remove(struct platform_device *pdev) · line 642

Quoted 5 lines
drivers/tty/serial/mcf.c +5 -0
+static const struct of_device_id mcf_uart_dt_ids[] = {
+	{ .compatible = "fsl,mcf-uart", },
+	{ }
+};
+

[Severity: Medium]
Does the absence of MODULE_DEVICE_TABLE(of, mcf_uart_dt_ids) prevent module autoloading when the hardware is present in the device tree?

Quoted 8 lines
drivers/tty/serial/mcf.c +1 -0
 static struct platform_driver mcf_platform_driver = {
 	.probe		= mcf_probe,
 	.remove		= mcf_remove,
 	.driver		= {
 		.name	= "mcfuart",
+		.of_match_table = mcf_uart_dt_ids,
 	},
 };
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5