tty: serial: meson: fix UART driver lifetime
PATCH 1/1 tty: serial: meson: fix UART driver lifetime
Moving Amlogic UART registration into probe left the first driver registration behind if uart_add_one_port() failed. Adding the separate ttyS driver later also exposed a global last-port test: removing the last port of one driver kept it registered whenever the other had a port.
Track whether probe registered the driver and undo it when port addition fails. On remove, retain the current driver only if a remaining port maps to that driver. Serialize the shared lifecycle decisions against parallel probe and remove operations.
This issue was identified during our ongoing static-analysis research while reviewing kernel code.
--- drivers/tty/serial/meson_uart.c | 52 +++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 12 deletions(-)
drivers/tty/serial/meson_uart.c +40 -12
@@ -12,6 +12,7 @@#include <linux/io.h> #include <linux/iopoll.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/kernel.h> #include <linux/of.h> #include <linux/platform_device.h>
@@ -80,6 +81,7 @@ static struct uart_driver meson_uart_driver_ttyAML;static struct uart_driver meson_uart_driver_ttyS;static struct uart_port *meson_ports[AML_UART_PORT_NUM ]; +static DEFINE_MUTEX (meson_uart_mutex);struct meson_uart_data {struct uart_driver *uart_driver;
@@ -692,6 +694,16 @@ static struct uart_driver *meson_uart_current(const struct meson_uart_data *pd) pd->uart_driver : &meson_uart_driver_ttyAML; } +static bool meson_uart_has_ports (struct uart_driver *uart_driver) +{ +for (int id =0 ; id <AML_UART_PORT_NUM ; id++) +if (meson_ports[id] && +meson_uart_current (meson_ports[id]->private_data) == uart_driver) +return true; + +return false; +} +static int meson_uart_probe (struct platform_device *pdev) {const struct meson_uart_data *priv_data;
@@ -702,6 +714,7 @@ static int meson_uart_probe(struct platform_device *pdev)int ret =0 ;int irq;bool has_rtscts; +bool registered = false;if (pdev->dev.of_node) pdev->id =of_alias_get_id (pdev->dev.of_node,"serial" );
@@ -731,11 +744,6 @@ static int meson_uart_probe(struct platform_device *pdev)of_property_read_u32 (pdev->dev.of_node,"fifo-size" , &fifosize); has_rtscts =of_property_read_bool (pdev->dev.of_node,"uart-has-rtscts" ); -if (meson_ports[pdev->id]) { -return dev_err_probe (&pdev->dev, -EBUSY , -"port %d already allocated\n" , pdev->id); - } - port =devm_kzalloc (&pdev->dev,sizeof (struct uart_port ),GFP_KERNEL );if (!port)return -ENOMEM ;
@@ -748,11 +756,22 @@ static int meson_uart_probe(struct platform_device *pdev) uart_driver =meson_uart_current (priv_data); +mutex_lock (&meson_uart_mutex); + +if (meson_ports[pdev->id]) { + ret =dev_err_probe (&pdev->dev, -EBUSY , +"port %d already allocated\n" , pdev->id); + goto out_unlock; + } +if (!uart_driver->state) { ret =uart_register_driver (uart_driver); -if (ret) -return dev_err_probe (&pdev->dev, ret, -"can't register uart driver\n" ); +if (ret) { + ret =dev_err_probe (&pdev->dev, ret, +"can't register uart driver\n" ); + goto out_unlock; + } + registered = true; } port->iotype =UPIO_MEM ;
@@ -781,9 +800,14 @@ static int meson_uart_probe(struct platform_device *pdev) } ret =uart_add_one_port (uart_driver, port); -if (ret) +if (ret) { meson_ports[pdev->id] =NULL ; +if (registered) +uart_unregister_driver (uart_driver); + } +out_unlock: +mutex_unlock (&meson_uart_mutex);return ret; }
@@ -794,15 +818,19 @@ static void meson_uart_remove(struct platform_device *pdev) port =platform_get_drvdata (pdev); uart_driver =meson_uart_current (port->private_data); + +mutex_lock (&meson_uart_mutex);uart_remove_one_port (uart_driver, port); meson_ports[pdev->id] =NULL ; -for (int id =0 ; id <AML_UART_PORT_NUM ; id++) -if (meson_ports[id]) -return ; +if (meson_uart_has_ports (uart_driver)) + goto out_unlock;/* No more available uart ports, unregister uart driver */ uart_unregister_driver (uart_driver); + +out_unlock: +mutex_unlock (&meson_uart_mutex); }static struct meson_uart_data meson_g12a_uart_data = {
Signature · 1 line
2.47.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Failure to map device memory during port configuration causes a null pointer dereference during UART startup. --
commit f142190e115d4a0b9969d2dd8bd4d9461c01f15a
Author: Myeonghun Pak <[email protected]>
tty: serial: meson: fix UART driver lifetime
Moving Amlogic UART registration into probe left the first driver registration behind if uart_add_one_port() failed. Track whether probe registered the driver and undo it when port addition fails. Serialize the shared lifecycle decisions against parallel probe and remove operations.
Quoted 4 lines
drivers/tty/serial/meson_uart.c +0
-0
[ ... ]
drivers/tty/serial/meson_uart.c static int meson_uart_probe(struct platform_device *pdev) · line 802
Quoted 4 lines
drivers/tty/serial/meson_uart.c +0
-0@@ -781,9 +800,14 @@ static int meson_uart_probe(struct platform_device *pdev) } ret =uart_add_one_port (uart_driver, port);
[Severity: High]
This isn't a bug introduced by this patch, but I noticed a pre-existing issue where failure to map device memory during port configuration causes a null pointer dereference during UART startup.
If devm_request_mem_region() or devm_ioremap() fails during meson_uart_request_port() (which is called via the .config_port callback during uart_add_one_port), the failure is silently ignored:
drivers/tty/serial/meson_uart.c:meson_uart_config_port() {
if (flags & UART_CONFIG_TYPE) {
port->type = PORT_MESON;
meson_uart_request_port(port);
}
}Because port->type is unconditionally set to PORT_MESON, the serial core believes the port is successfully configured. When user space subsequently opens the corresponding tty device, meson_uart_startup() is invoked and executes:
drivers/tty/serial/meson_uart.c:meson_uart_startup() {
uart_port_lock_irqsave(port, &flags);
val = readl(port->membase + AML_UART_CONTROL);
...
}Since port->membase is NULL, this readl() causes an immediate kernel panic on device open.
Could meson_uart_config_port() be updated to only set port->type if meson_uart_request_port() succeeds, preventing the serial core from using an unconfigured port?
Quoted 11 lines
drivers/tty/serial/meson_uart.c +6
-1-if (ret) +if (ret) { meson_ports[pdev->id] =NULL ; +if (registered) +uart_unregister_driver (uart_driver); + } +out_unlock: +mutex_unlock (&meson_uart_mutex);return ret; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg Kroah-Hartman
maintainerQuoted 45 lines · Myeonghun Pak
Moving Amlogic UART registration into probe left the first driver registration behind if
uart_add_one_port()failed. Adding the separate ttyS driver later also exposed a global last-port test: removing the last port of one driver kept it registered whenever the other had a port.Track whether probe registered the driver and undo it when port addition fails. On remove, retain the current driver only if a remaining port maps to that driver. Serialize the shared lifecycle decisions against parallel probe and remove operations.
This issue was identified during our ongoing static-analysis research while reviewing kernel code.
Fixes bcb5645f99ef ("tty: serial: meson: redesign the module to platform_driver")Fixes e71aab9d6132 ("tty: serial: meson: apply ttyS devname instead of ttyAML for new SoCs")Assisted-by OpenAI:GPT-5.6Co-developed-by Ijae Kim <[email protected]>Signed-off-by Ijae Kim <[email protected]>Signed-off-by Myeonghun Pak <[email protected]>--- drivers/tty/serial/meson_uart.c | 52 +++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 12 deletions(-)drivers/tty/serial/meson_uart.c +2
-0@@ -12,6 +12,7 @@#include <linux/io.h> #include <linux/iopoll.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/kernel.h> #include <linux/of.h> #include <linux/platform_device.h> @@ -80,6 +81,7 @@ static struct uart_driver meson_uart_driver_ttyAML;static struct uart_driver meson_uart_driver_ttyS;static struct uart_port *meson_ports[AML_UART_PORT_NUM ]; +static DEFINE_MUTEX (meson_uart_mutex);struct meson_uart_data {struct uart_driver *uart_driver;
drivers/tty/serial/meson_uart.c static struct uart_driver *meson_uart_current(const struct meson_uart_data *pd) · line 697
Quoted 4 lines
drivers/tty/serial/meson_uart.c +1
-0@@ -692,6 +694,16 @@ static struct uart_driver *meson_uart_current(const struct meson_uart_data *pd) pd->uart_driver : &meson_uart_driver_ttyAML; } +static bool meson_uart_has_ports (struct uart_driver *uart_driver)
As you require a lock to be held here, document it properly so the compiler will catch it when you get it wrong.
Quoted 33 lines
drivers/tty/serial/meson_uart.c +10
-5+{ +for (int id =0 ; id <AML_UART_PORT_NUM ; id++) +if (meson_ports[id] && +meson_uart_current (meson_ports[id]->private_data) == uart_driver) +return true; + +return false; +} +static int meson_uart_probe (struct platform_device *pdev) {const struct meson_uart_data *priv_data;@@ -702,6 +714,7 @@ static int meson_uart_probe(struct platform_device *pdev)int ret =0 ;int irq;bool has_rtscts; +bool registered = false;if (pdev->dev.of_node) pdev->id =of_alias_get_id (pdev->dev.of_node,"serial" );@@ -731,11 +744,6 @@ static int meson_uart_probe(struct platform_device *pdev)of_property_read_u32 (pdev->dev.of_node,"fifo-size" , &fifosize); has_rtscts =of_property_read_bool (pdev->dev.of_node,"uart-has-rtscts" ); -if (meson_ports[pdev->id]) { -return dev_err_probe (&pdev->dev, -EBUSY , -"port %d already allocated\n" , pdev->id); - } - port =devm_kzalloc (&pdev->dev,sizeof (struct uart_port ),GFP_KERNEL );if (!port)return -ENOMEM ;
drivers/tty/serial/meson_uart.c static int meson_uart_probe(struct platform_device *pdev) · line 759
Quoted 4 lines
drivers/tty/serial/meson_uart.c +1
-0@@ -748,11 +756,22 @@ static int meson_uart_probe(struct platform_device *pdev) uart_driver =meson_uart_current (priv_data); +mutex_lock (&meson_uart_mutex);
guard?
thanks,
greg k-h