[PATCH] eeprom: digsy_mtc: fix reference leak on failed device registration

Guangshuo Li posted 1 patch 3 days, 21 hours ago
drivers/misc/eeprom/digsy_mtc_eeprom.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] eeprom: digsy_mtc: fix reference leak on failed device registration
Posted by Guangshuo Li 3 days, 21 hours ago
When platform_device_register() fails in digsy_mtc_eeprom_devices_init(),
the embedded struct device in digsy_mtc_eeprom has already been
initialized by device_initialize(), but the failure path only removes
the software node and does not drop the device reference for the current
platform device:

  digsy_mtc_eeprom_devices_init()
    -> platform_device_register(&digsy_mtc_eeprom)
       -> device_initialize(&digsy_mtc_eeprom.dev)
       -> setup_pdev_dma_masks(&digsy_mtc_eeprom)
       -> platform_device_add(&digsy_mtc_eeprom)

This leads to a reference leak when platform_device_register() fails.
Fix this by calling platform_device_put() after removing the software
node.

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

Fixes: c8ed97d8c3984 ("eeprom: digsy_mtc: Convert to use GPIO descriptors")
Cc: stable@vger.kernel.org
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
---
 drivers/misc/eeprom/digsy_mtc_eeprom.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/eeprom/digsy_mtc_eeprom.c b/drivers/misc/eeprom/digsy_mtc_eeprom.c
index ee58f7ce5bfa..54fcbdc0412a 100644
--- a/drivers/misc/eeprom/digsy_mtc_eeprom.c
+++ b/drivers/misc/eeprom/digsy_mtc_eeprom.c
@@ -89,8 +89,10 @@ static int __init digsy_mtc_eeprom_devices_init(void)
 		return ret;
 
 	ret = platform_device_register(&digsy_mtc_eeprom);
-	if (ret)
+	if (ret) {
 		device_remove_software_node(&digsy_mtc_eeprom.dev);
+		platform_device_put(&digsy_mtc_eeprom);
+	}
 
 	return ret;
 }
-- 
2.43.0
Re: [PATCH] eeprom: digsy_mtc: fix reference leak on failed device registration
Posted by Andy Shevchenko 3 days, 6 hours ago
On Thu, Apr 16, 2026 at 12:52:02AM +0800, Guangshuo Li wrote:
> When platform_device_register() fails in digsy_mtc_eeprom_devices_init(),
> the embedded struct device in digsy_mtc_eeprom has already been
> initialized by device_initialize(), but the failure path only removes
> the software node and does not drop the device reference for the current
> platform device:
> 
>   digsy_mtc_eeprom_devices_init()
>     -> platform_device_register(&digsy_mtc_eeprom)
>        -> device_initialize(&digsy_mtc_eeprom.dev)
>        -> setup_pdev_dma_masks(&digsy_mtc_eeprom)
>        -> platform_device_add(&digsy_mtc_eeprom)
> 
> This leads to a reference leak when platform_device_register() fails.
> Fix this by calling platform_device_put() after removing the software
> node.
> 
> The issue was identified by a static analysis tool I developed and
> confirmed by manual review.

Thanks for catching this up!
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko