Skip to contentSkip to conversation

ssb: fix reference leaks on failed flash device registration

PATCH In review Guangshuo Li 1 participant 2 messages Apr 15 – Apr 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ssb: fix reference leaks on failed flash device registration

When platform_device_register() fails in ssb_devices_register(), the embedded struct device in ssb_pflash_dev or ssb_sflash_dev has already been initialized by device_initialize(), but the failure paths only report the error and do not drop the device reference for the current platform device:

ssb_devices_register()

    -> platform_device_register(&ssb_pflash_dev)
       -> device_initialize(&ssb_pflash_dev.dev)
       -> setup_pdev_dma_masks(&ssb_pflash_dev)
       -> platform_device_add(&ssb_pflash_dev)

ssb_devices_register()

    -> platform_device_register(&ssb_sflash_dev)
       -> device_initialize(&ssb_sflash_dev.dev)
       -> setup_pdev_dma_masks(&ssb_sflash_dev)
       -> platform_device_add(&ssb_sflash_dev)

This leads to reference leaks when platform_device_register() fails. Fix this by calling platform_device_put() after reporting the error.

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

Fixes c7a4a9e3880cc ("ssb: register platform device for parallel flash")
Fixes 7b5d6043de312 ("ssb: register serial flash as platform device")
---
 drivers/ssb/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/ssb/main.c +6 -2
index b2d339eb57d5..5cdf612a8516 100644
@@ -535,16 +535,20 @@ static int ssb_devices_register(struct ssb_bus *bus)
 #ifdef CONFIG_SSB_DRIVER_MIPS
 	if (bus->mipscore.pflash.present) {
 		err = platform_device_register(&ssb_pflash_dev);
-		if (err)
+		if (err) {
 			pr_err("Error registering parallel flash\n");
+			platform_device_put(&ssb_pflash_dev);
+		}
 	}
 #endif
 
 #ifdef CONFIG_SSB_SFLASH
 	if (bus->mipscore.sflash.present) {
 		err = platform_device_register(&ssb_sflash_dev);
-		if (err)
+		if (err) {
 			pr_err("Error registering serial flash\n");
+			platform_device_put(&ssb_sflash_dev);
+		}
 	}
 #endif
 
Signature · 1 line
2.43.0
#1 lore raw

Hi,

Please disregard this patch.

Quoted 61 lines · Guangshuo Li

When platform_device_register() fails in ssb_devices_register(), the embedded struct device in ssb_pflash_dev or ssb_sflash_dev has already been initialized by device_initialize(), but the failure paths only report the error and do not drop the device reference for the current platform device:

ssb_devices_register()

    -> platform_device_register(&ssb_pflash_dev)
       -> device_initialize(&ssb_pflash_dev.dev)
       -> setup_pdev_dma_masks(&ssb_pflash_dev)
       -> platform_device_add(&ssb_pflash_dev)

ssb_devices_register()

    -> platform_device_register(&ssb_sflash_dev)
       -> device_initialize(&ssb_sflash_dev.dev)
       -> setup_pdev_dma_masks(&ssb_sflash_dev)
       -> platform_device_add(&ssb_sflash_dev)

This leads to reference leaks when platform_device_register() fails. Fix this by calling platform_device_put() after reporting the error.

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

Fixes c7a4a9e3880cc ("ssb: register platform device for parallel flash")
Fixes 7b5d6043de312 ("ssb: register serial flash as platform device")
---
 drivers/ssb/main.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/ssb/main.c +6 -3
index b2d339eb57d5..5cdf612a8516 100644
@@ -535,16 +535,20 @@ static int ssb_devices_register(struct ssb_bus *bus)
 #ifdef CONFIG_SSB_DRIVER_MIPS
        if (bus->mipscore.pflash.present) {
                err = platform_device_register(&ssb_pflash_dev);
-               if (err)
+               if (err) {
                        pr_err("Error registering parallel flash\n");
+                       platform_device_put(&ssb_pflash_dev);
+               }
        }
 #endif
 
 #ifdef CONFIG_SSB_SFLASH
        if (bus->mipscore.sflash.present) {
                err = platform_device_register(&ssb_sflash_dev);
-               if (err)
+               if (err) {
                        pr_err("Error registering serial flash\n");
+                       platform_device_put(&ssb_sflash_dev);
+               }
        }
 #endif
 
--
Quoted 2 lines

2.43.0

After re-checking it, ssb_pflash_dev and ssb_sflash_dev are global platform_device objects and they do not provide dev.release callbacks. Therefore calling platform_device_put() on the platform_device_register() failure paths 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