Skip to contentSkip to conversation

block: fix error unwinding in device_add_disk

PATCH Applied Christoph Hellwig 2 participants 2 messages Dec 21, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix error unwinding in device_add_disk

One device_add is called disk->ev will be freed by disk_release, so we should free it twice. Fix this by allocating disk->ev after device_add so that the extra local unwinding can be removed entirely.

Based on an earlier patch from Tetsuo Handa.

Fixes 83cbce9574462c6b ("block: add error handling for device_add_disk / add_disk")
---
 block/genhd.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
block/genhd.c +6 -7
index 3c139a1b6f049..603db5d6f10c0 100644
@@ -442,10 +442,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 		disk->first_minor = ret;
 	}
 
-	ret = disk_alloc_events(disk);
-	if (ret)
-		goto out_free_ext_minor;
-
 	/* delay uevents, until we scanned partition table */
 	dev_set_uevent_suppress(ddev, 1);
 
@@ -456,7 +452,12 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 		ddev->devt = MKDEV(disk->major, disk->first_minor);
 	ret = device_add(ddev);
 	if (ret)
-		goto out_disk_release_events;
+		goto out_free_ext_minor;
+
+	ret = disk_alloc_events(disk);
+	if (ret)
+		goto out_device_del;
+
 	if (!sysfs_deprecated) {
 		ret = sysfs_create_link(block_depr, &ddev->kobj,
 					kobject_name(&ddev->kobj));
@@ -538,8 +539,6 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 		sysfs_remove_link(block_depr, dev_name(ddev));
 out_device_del:
 	device_del(ddev);
-out_disk_release_events:
-	disk_release_events(disk);
 out_free_ext_minor:
 	if (disk->major == BLOCK_EXT_MAJOR)
 		blk_free_ext_minor(disk->first_minor);
Signature · 1 line
2.30.2
maintainer Applied
#1 lore raw
Quoted 8 lines · Christoph Hellwig

One device_add is called disk->ev will be freed by disk_release, so we should free it twice. Fix this by allocating disk->ev after device_add so that the extra local unwinding can be removed entirely.

Based on an earlier patch from Tetsuo Handa.

[...]

Applied, thanks!

[1/1] block: fix error unwinding in device_add_disk

      commit: 99d8690aae4b2f0d1d90075de355ac087f820a66

Best regards,

Signature · 1 line
Jens Axboe