Skip to contentSkip to conversation

block: add put_device() call when device_add() fails in device_add_disk()

PATCH In review Alfredo Cruz 2 participants 2 messages Feb 21, 2024 – Feb 22, 2024 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: add put_device() call when device_add() fails in device_add_disk()

When device_add() fails, put_device() should called for cleanup. Added this call before the jump to out_free_ext_minor to not interfere with device_del() on error paths where deivce_add() succeeded

---
 block/genhd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
block/genhd.c +3 -1
index d74fb5b4ae68..5a231fb075bd 100644
@@ -456,8 +456,10 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 	if (!(disk->flags & GENHD_FL_HIDDEN))
 		ddev->devt = MKDEV(disk->major, disk->first_minor);
 	ret = device_add(ddev);
-	if (ret)
+	if (ret) {
+		put_device(ddev);
 		goto out_free_ext_minor;
+	}
 
 	ret = disk_alloc_events(disk);
 	if (ret)
Signature · 1 line
2.43.2
#1 lore raw

Hi,

Quoted 14 lines · Alfredo Cruz

When device_add() fails, put_device() should called for cleanup. Added this call before the jump to out_free_ext_minor to not interfere with device_del() on error paths where deivce_add() succeeded

---
  block/genhd.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
block/genhd.c +0 -0
index d74fb5b4ae68..5a231fb075bd 100644

block/genhd.c int __must_check device_add_disk(struct device *parent, struct gendisk *disk, · line 462

Quoted 8 lines
block/genhd.c +3 -1
@@ -456,8 +456,10 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
  	if (!(disk->flags & GENHD_FL_HIDDEN))
  		ddev->devt = MKDEV(disk->major, disk->first_minor);
  	ret = device_add(ddev);
-	if (ret)
+	if (ret) {
+		put_device(ddev);
  		goto out_free_ext_minor;
+	}

This is wrong, gendisk is allocated by caller, and caller is responsible to free the gendisk, as you can see in many callers.

Thanks,
Kuai

Quoted 4 lines
block/genhd.c +0 -0
  	ret = disk_alloc_events(disk);
  	if (ret)