[PATCH] tty: serial: meson: fix UART driver lifetime

Greg Kroah-Hartman gregkh at linuxfoundation.org
Wed Sep 23 03:15:38 PDT 2026


On Sat, Sep 12, 2026 at 11:41:48PM -0400, Myeonghun Pak wrote:
> 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")
> Cc: stable at vger.kernel.org
> Assisted-by: OpenAI:GPT-5.6
> Co-developed-by: Ijae Kim <ae878000 at gmail.com>
> Signed-off-by: Ijae Kim <ae878000 at gmail.com>
> Signed-off-by: Myeonghun Pak <mhun512 at gmail.com>
> ---
>  drivers/tty/serial/meson_uart.c | 52 +++++++++++++++++++++++++--------
>  1 file changed, 40 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/tty/serial/meson_uart.c b/drivers/tty/serial/meson_uart.c
> index a6cb2a535..4bd9d9826 100644
> --- a/drivers/tty/serial/meson_uart.c
> +++ b/drivers/tty/serial/meson_uart.c
> @@ -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)

As you require a lock to be held here, document it properly so the
compiler will catch it when you get it wrong.

> +{
> +	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);

guard?

thanks,

greg k-h



More information about the linux-arm-kernel mailing list