block: fix memory leak for elevator on add_disk failure
authorYu Kuai <yukuai3@huawei.com>
Sat, 22 Oct 2022 02:16:15 +0000 (10:16 +0800)
committerJens Axboe <axboe@kernel.dk>
Sat, 22 Oct 2022 21:14:38 +0000 (15:14 -0600)
The default elevator is allocated in the beginning of device_add_disk(),
however, it's not freed in the following error path.

Fixes: 50e34d78815e ("block: disable the elevator int del_gendisk")
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Jason Yan <yanaijie@huawei.com>
Link: https://lore.kernel.org/r/20221022021615.2756171-1-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
block/genhd.c

index 17b33c6..fee90eb 100644 (file)
@@ -410,9 +410,10 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
         * Otherwise just allocate the device numbers for both the whole device
         * and all partitions from the extended dev_t space.
         */
+       ret = -EINVAL;
        if (disk->major) {
                if (WARN_ON(!disk->minors))
-                       return -EINVAL;
+                       goto out_exit_elevator;
 
                if (disk->minors > DISK_MAX_PARTS) {
                        pr_err("block: can't allocate more than %d partitions\n",
@@ -420,14 +421,14 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
                        disk->minors = DISK_MAX_PARTS;
                }
                if (disk->first_minor + disk->minors > MINORMASK + 1)
-                       return -EINVAL;
+                       goto out_exit_elevator;
        } else {
                if (WARN_ON(disk->minors))
-                       return -EINVAL;
+                       goto out_exit_elevator;
 
                ret = blk_alloc_ext_minor();
                if (ret < 0)
-                       return ret;
+                       goto out_exit_elevator;
                disk->major = BLOCK_EXT_MAJOR;
                disk->first_minor = ret;
        }
@@ -540,6 +541,9 @@ out_device_del:
 out_free_ext_minor:
        if (disk->major == BLOCK_EXT_MAJOR)
                blk_free_ext_minor(disk->first_minor);
+out_exit_elevator:
+       if (disk->queue->elevator)
+               elevator_exit(disk->queue);
        return ret;
 }
 EXPORT_SYMBOL(device_add_disk);