[v2] block: fix memory leak for elevator on add_disk failure

Message ID 20221022021615.2756171-1-yukuai1@huaweicloud.com
State New
Headers
Series [v2] block: fix memory leak for elevator on add_disk failure |

Commit Message

Yu Kuai Oct. 22, 2022, 2:16 a.m. UTC
  From: Yu Kuai <yukuai3@huawei.com>

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>
---
Changes in v2:
 - fix wrong fix tag
 - add review tag

 block/genhd.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
  

Comments

Jason Yan Oct. 22, 2022, 8:51 a.m. UTC | #1
On 2022/10/22 10:16, Yu Kuai wrote:
> From: Yu Kuai<yukuai3@huawei.com>
> 
> 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>
> ---
> Changes in v2:
>   - fix wrong fix tag
>   - add review tag
> 
>   block/genhd.c | 12 ++++++++----
>   1 file changed, 8 insertions(+), 4 deletions(-)

Reviewed-by: Jason Yan <yanaijie@huawei.com>
  
Jens Axboe Oct. 22, 2022, 9:14 p.m. UTC | #2
On Sat, 22 Oct 2022 10:16:15 +0800, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
> 
> The default elevator is allocated in the beginning of device_add_disk(),
> however, it's not freed in the following error path.
> 
> 

Applied, thanks!

[1/1] block: fix memory leak for elevator on add_disk failure
      commit: 02341a08c9dec5a88527981b0bdf0fb6f7499cbf

Best regards,
  

Patch

diff --git a/block/genhd.c b/block/genhd.c
index b3d04e79e854..fea319c8f99e 100644
--- a/block/genhd.c
+++ b/block/genhd.c
@@ -437,9 +437,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",
@@ -447,14 +448,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;
 	}
@@ -571,6 +572,9 @@  int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
 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);