[PATCH] serial: 8250_exar_st16c554: fix reference leak on failed device registration

Guangshuo Li posted 1 patch 2 months ago
drivers/tty/serial/8250/8250_exar_st16c554.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
[PATCH] serial: 8250_exar_st16c554: fix reference leak on failed device registration
Posted by Guangshuo Li 2 months ago
When platform_device_register() fails in exar_init(), the embedded
struct device in exar_device has already been initialized by
device_initialize(), but the failure path returns the error without
dropping the device reference for the current platform device:

  exar_init()
    -> platform_device_register(&exar_device)
       -> device_initialize(&exar_device.dev)
       -> setup_pdev_dma_masks(&exar_device)
       -> platform_device_add(&exar_device)

This leads to a reference leak when platform_device_register() fails.
Fix this by calling platform_device_put() before returning the error.

The issue was identified by a static analysis tool I developed and
confirmed by manual review.

Fixes: e0980dafa329d ("[PATCH] Exar quad port serial")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/tty/serial/8250/8250_exar_st16c554.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/serial/8250/8250_exar_st16c554.c b/drivers/tty/serial/8250/8250_exar_st16c554.c
index 933811ebfaac..b2c37f0c52aa 100644
--- a/drivers/tty/serial/8250/8250_exar_st16c554.c
+++ b/drivers/tty/serial/8250/8250_exar_st16c554.c
@@ -30,7 +30,13 @@ static struct platform_device exar_device = {
 
 static int __init exar_init(void)
 {
-	return platform_device_register(&exar_device);
+	int ret;
+
+	ret = platform_device_register(&exar_device);
+	if (ret)
+		platform_device_put(&exar_device);
+
+	return ret;
 }
 
 module_init(exar_init);
-- 
2.43.0
Re: [PATCH] serial: 8250_exar_st16c554: fix reference leak on failed device registration
Posted by Guangshuo Li 1 month, 3 weeks ago
Hi,

Please disregard this patch.

On Thu, 16 Apr 2026 at 02:40, Guangshuo Li <lgs201920130244@gmail.com> wrote:
>
> When platform_device_register() fails in exar_init(), the embedded
> struct device in exar_device has already been initialized by
> device_initialize(), but the failure path returns the error without
> dropping the device reference for the current platform device:
>
>   exar_init()
>     -> platform_device_register(&exar_device)
>        -> device_initialize(&exar_device.dev)
>        -> setup_pdev_dma_masks(&exar_device)
>        -> platform_device_add(&exar_device)
>
> This leads to a reference leak when platform_device_register() fails.
> Fix this by calling platform_device_put() before returning the error.
>
> The issue was identified by a static analysis tool I developed and
> confirmed by manual review.
>
> Fixes: e0980dafa329d ("[PATCH] Exar quad port serial")
> Cc: stable@vger.kernel.org
> Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
> ---
>  drivers/tty/serial/8250/8250_exar_st16c554.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/tty/serial/8250/8250_exar_st16c554.c b/drivers/tty/serial/8250/8250_exar_st16c554.c
> index 933811ebfaac..b2c37f0c52aa 100644
> --- a/drivers/tty/serial/8250/8250_exar_st16c554.c
> +++ b/drivers/tty/serial/8250/8250_exar_st16c554.c
> @@ -30,7 +30,13 @@ static struct platform_device exar_device = {
>
>  static int __init exar_init(void)
>  {
> -       return platform_device_register(&exar_device);
> +       int ret;
> +
> +       ret = platform_device_register(&exar_device);
> +       if (ret)
> +               platform_device_put(&exar_device);
> +
> +       return ret;
>  }
>
>  module_init(exar_init);
> --
> 2.43.0
>

After re-checking it, exar_device is a static platform_device and it does
not provide a dev.release callback. Therefore calling
platform_device_put() on the platform_device_register() failure path is
not appropriate here and can trigger the missing release callback
warning.

This falls into the same static platform_device pattern pointed out in
the other reviews, so I will drop this patch.

Sorry for the noise.

Best regards,
Guangshuo Li