Skip to contentSkip to conversation

floppy: unregister platform device on add_disk failure

PATCH In review Guangshuo Li 2 participants 2 messages Jul 8 – Jul 24

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

PATCH 1/1 floppy: unregister platform device on add_disk failure

The change referenced by the Fixes tag reverted a previous fix because that fix added incorrect cleanup for the platform_device_register() failure path.

However, reverting the whole change also removed the cleanup for a later failure path. If platform_device_register() succeeds but device_add_disk() fails, the current floppy platform device has already been registered and registered[drive] has been set.

The common out_remove_drives path uses while (drive--), so it starts from the previous drive and does not unregister the current one. As a result, the platform device registered for the failing drive is leaked.

Unregister the current platform device directly on the device_add_disk() failure path, where platform_device_register() is known to have succeeded. Leave the platform_device_register() failure path unchanged.

Fixes 895a9b37917d ("Revert "floppy: fix reference leak on platform_device_register() failure"")
---
 drivers/block/floppy.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/block/floppy.c +4 -1
index f04397b8e381..7a34af534460 100644
@@ -4729,8 +4729,11 @@ static int __init do_floppy_init(void)
 
 		err = device_add_disk(&floppy_device[drive].dev,
 				      disks[drive][0], NULL);
-		if (err)
+		if (err) {
+			platform_device_unregister(&floppy_device[drive]);
+			registered[drive] = false;
 			goto out_remove_drives;
+		}
 	}
 
 	return 0;
Signature · 1 line
2.43.0
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hello,

Thank you for the patch.

Quoted 13 lines · Guangshuo Li

The change referenced by the Fixes tag reverted a previous fix because that fix added incorrect cleanup for the platform_device_register() failure path.

However, reverting the whole change also removed the cleanup for a later failure path. If platform_device_register() succeeds but device_add_disk() fails, the current floppy platform device has already been registered and registered[drive] has been set.

The common out_remove_drives path uses while (drive--), so it starts from the previous drive and does not unregister the current one. As a result, the platform device registered for the failing drive is leaked.

Quoted 6 lines

Unregister the current platform device directly on the device_add_disk() failure path, where platform_device_register() is known to have succeeded. Leave the platform_device_register() failure path unchanged.

Fixes 895a9b37917d ("Revert "floppy: fix reference leak on platform_device_register() failure"")

I would also add

Fixes 47d34aa2d211 ("floppy: add error handling support for add_disk()")
Quoted 11 lines
---
 drivers/block/floppy.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/block/floppy.c +0 -0
index f04397b8e381..7a34af534460 100644
@@ -4729,8 +4729,11 @@ static int __init do_floppy_init(void)
 
 		err = device_add_disk(&floppy_device[drive].dev,

drivers/block/floppy.c static int __init do_floppy_init(void) · line 4739

Quoted 10 lines
drivers/block/floppy.c +4 -1
 				      disks[drive][0], NULL);
-		if (err)
+		if (err) {
+			platform_device_unregister(&floppy_device[drive]);
+			registered[drive] = false;
 			goto out_remove_drives;
+		}
 	}
 
 	return 0;

Thanks,
Denis