Skip to contentSkip to conversation

block: fix memory leak in disk_register_independent_access_ranges

PATCH Applied Miaoqian Lin 3 participants 3 messages Jan 20, 2022 – Jan 21, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: fix memory leak in disk_register_independent_access_ranges

kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add()

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix this issue by adding kobject_put().
Callback function blk_ia_ranges_sysfs_release() in kobject_put() can handle the pointer "iars" properly.

Fixes a2247f19ee1c ("block: Add independent access ranges support")
---
 block/blk-ia-ranges.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-ia-ranges.c +1 -1
index b925f3db3ab7..18c68d8b9138 100644
@@ -144,7 +144,7 @@ int disk_register_independent_access_ranges(struct gendisk *disk,
 				   &q->kobj, "%s", "independent_access_ranges");
 	if (ret) {
 		q->ia_ranges = NULL;
-		kfree(iars);
+		kobject_put(&iars->kobj);
 		return ret;
 	}
 
Signature · 1 line
2.17.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Miaoqian Lin

kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add()

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix this issue by adding kobject_put().
Callback function blk_ia_ranges_sysfs_release() in kobject_put() can handle the pointer "iars" properly.

Fixes a2247f19ee1c ("block: Add independent access ranges support")
---
 block/blk-ia-ranges.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block/blk-ia-ranges.c +0 -0
index b925f3db3ab7..18c68d8b9138 100644

block/blk-ia-ranges.c int disk_register_independent_access_ranges(struct gendisk *disk, · line 150

Quoted 8 lines
block/blk-ia-ranges.c +1 -1
@@ -144,7 +144,7 @@ int disk_register_independent_access_ranges(struct gendisk *disk,
 				   &q->kobj, "%s", "independent_access_ranges");
 	if (ret) {
 		q->ia_ranges = NULL;
-		kfree(iars);
+		kobject_put(&iars->kobj);
 		return ret;
 	}
 

Looks good.

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Applied
#2 lore raw
In reply to Miaoqian Lin
Quoted 11 lines · Miaoqian Lin

kobject_init_and_add() takes reference even when it fails. According to the doc of kobject_init_and_add()

If this function returns an error, kobject_put() must be called to properly clean up the memory associated with the object.

Fix this issue by adding kobject_put().
Callback function blk_ia_ranges_sysfs_release() in kobject_put() can handle the pointer "iars" properly.

[...]

Applied, thanks!

[1/1] block: fix memory leak in disk_register_independent_access_ranges

      commit: c321e650a45c4228cce1107680ac74e06f014906

Best regards,

Signature · 1 line
Jens Axboe