Skip to contentSkip to conversation

Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj

PATCH Superseded Miaoqian Lin 3 participants 6 messages Jan 26, 2022 – Feb 7, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj

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 memory leak by calling kobject_put().

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
---
 drivers/hv/vmbus_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +3 -1
index 17bf55fe3169..9e055697783b 100644
@@ -2028,8 +2028,10 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	kobj->kset = dev->channels_kset;
 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
 				   "%u", relid);
-	if (ret)
+	if (ret) {
+		kobject_put(kobj);
 		return ret;
+	}
 
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 19 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 memory leak by calling kobject_put().

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
---
 drivers/hv/vmbus_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +0 -0
index 17bf55fe3169..9e055697783b 100644

drivers/hv/vmbus_drv.c int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel) · line 2036

Quoted 10 lines
drivers/hv/vmbus_drv.c +3 -1
@@ -2028,8 +2028,10 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	kobj->kset = dev->channels_kset;
 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
 				   "%u", relid);
-	if (ret)
+	if (ret) {
+		kobject_put(kobj);
 		return ret;
+	}
 
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);

If sysfs_create_group() fails same cleanup I think is required.

Later kobject_uevent() may fail according to doc, but there is no error handling, maybe a good moment to consider adding it and do same cleanup.

Quoted 3 lines
Signature · 2 lines
-- 
2.17.1
maintainer
#2 lore raw
Quoted 29 lines · Juan Vazquez
Quoted 28 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 memory leak by calling kobject_put().

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
---
 drivers/hv/vmbus_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +3 -1
index 17bf55fe3169..9e055697783b 100644
@@ -2028,8 +2028,10 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	kobj->kset = dev->channels_kset;
 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
 				   "%u", relid);
-	if (ret)
+	if (ret) {
+		kobject_put(kobj);
 		return ret;
+	}
 
Quoted 5 lines

drivers/hv/vmbus_drv.c int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel) · line 2036

Quoted 1 line
drivers/hv/vmbus_drv.c +0 -0
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);

If sysfs_create_group() fails same cleanup I think is required.

Later kobject_uevent() may fail according to doc, but there is no error handling, maybe a good moment to consider adding it and do same cleanup.

Miaoqian, are you going to post a new version to address Juan's comment here?

Thanks,
Wei.

Quoted 3 lines
Quoted 3 lines
Signature · 2 lines
-- 
2.17.1
#3 lore raw

PATCH v2 1/1 Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj

In reply to Juan Vazquez

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 memory leak by calling kobject_put().

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")

---
Changes in v2:
- Add cleanup when sysfs_create_group() fails

kobject_uevent() is used for notifying userspace by sending an uevent, I don't think we need to do error handling for it.

---
 drivers/hv/vmbus_drv.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +4 -1
index 17bf55fe3169..34a4fd21bdf5 100644
@@ -2028,8 +2028,10 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	kobj->kset = dev->channels_kset;
 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
 				   "%u", relid);
-	if (ret)
+	if (ret) {
+		kobject_put(kobj);
 		return ret;
+	}
 
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
 
@@ -2038,6 +2040,7 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 		 * The calling functions' error handling paths will cleanup the
 		 * empty channel directory.
 		 */
+		kobject_put(kobj);
 		dev_err(device, "Unable to set up channel sysfs files\n");
 		return ret;
 	}
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#4 lore raw
Quoted 8 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 memory leak by calling kobject_put().

Quoted 8 lines
Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")

---
Changes in v2:
- Add cleanup when sysfs_create_group() fails

kobject_uevent() is used for notifying userspace by sending an uevent, I don't think we need to do error handling for it.

Thanks for the patch. It looks good to me.

Quoted 30 lines
---
 drivers/hv/vmbus_drv.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hv/vmbus_drv.c +4 -1
index 17bf55fe3169..34a4fd21bdf5 100644
@@ -2028,8 +2028,10 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 	kobj->kset = dev->channels_kset;
 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
 				   "%u", relid);
-	if (ret)
+	if (ret) {
+		kobject_put(kobj);
 		return ret;
+	}
 
 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
 
@@ -2038,6 +2040,7 @@ int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
 		 * The calling functions' error handling paths will cleanup the
 		 * empty channel directory.
 		 */
+		kobject_put(kobj);
 		dev_err(device, "Unable to set up channel sysfs files\n");
 		return ret;
 	}
Signature · 1 line
2.25.1
maintainer Applied
#5 lore raw
Quoted 15 lines · Juan Vazquez
Quoted 14 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 memory leak by calling kobject_put().

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")

---
Changes in v2:
- Add cleanup when sysfs_create_group() fails

Quoted 6 lines
Quoted 2 lines

kobject_uevent() is used for notifying userspace by sending an uevent, I don't think we need to do error handling for it.

Thanks for the patch. It looks good to me.

Applied to hyperv-fixes. Thanks.