Skip to content

Commit 02341a0

Browse files
YuKuai-huaweiaxboe
authored andcommitted
block: fix memory leak for elevator on add_disk failure
The default elevator is allocated in the beginning of device_add_disk(), however, it's not freed in the following error path. Fixes: 50e34d7 ("block: disable the elevator int del_gendisk") Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Jason Yan <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 2db9621 commit 02341a0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

block/genhd.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -410,24 +410,25 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
410410
* Otherwise just allocate the device numbers for both the whole device
411411
* and all partitions from the extended dev_t space.
412412
*/
413+
ret = -EINVAL;
413414
if (disk->major) {
414415
if (WARN_ON(!disk->minors))
415-
return -EINVAL;
416+
goto out_exit_elevator;
416417

417418
if (disk->minors > DISK_MAX_PARTS) {
418419
pr_err("block: can't allocate more than %d partitions\n",
419420
DISK_MAX_PARTS);
420421
disk->minors = DISK_MAX_PARTS;
421422
}
422423
if (disk->first_minor + disk->minors > MINORMASK + 1)
423-
return -EINVAL;
424+
goto out_exit_elevator;
424425
} else {
425426
if (WARN_ON(disk->minors))
426-
return -EINVAL;
427+
goto out_exit_elevator;
427428

428429
ret = blk_alloc_ext_minor();
429430
if (ret < 0)
430-
return ret;
431+
goto out_exit_elevator;
431432
disk->major = BLOCK_EXT_MAJOR;
432433
disk->first_minor = ret;
433434
}
@@ -540,6 +541,9 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
540541
out_free_ext_minor:
541542
if (disk->major == BLOCK_EXT_MAJOR)
542543
blk_free_ext_minor(disk->first_minor);
544+
out_exit_elevator:
545+
if (disk->queue->elevator)
546+
elevator_exit(disk->queue);
543547
return ret;
544548
}
545549
EXPORT_SYMBOL(device_add_disk);

0 commit comments

Comments
 (0)