Skip to contentSkip to conversation

Drivers: hv: vmbus: fix two issues

PATCH Applied Yang Yingliang 3 participants 6 messages Nov 19, 2022 – Nov 21, 2022

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

v3 cover Drivers: hv: vmbus: fix two issues

This patchset fixes a double free and a memory leak problems.

v2 -> v3:
Update commit message and comment suggested by Michael.

v1 -> v2:
Add a comment before vmbus_device_register().

Yang Yingliang (2):
Drivers: hv: vmbus: fix double free in the error path of
vmbus_add_channel_work()
Drivers: hv: vmbus: fix possible memory leak in
vmbus_device_register()

 drivers/hv/channel_mgmt.c | 5 ++++-
 drivers/hv/vmbus_drv.c    | 1 +
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/2 Drivers: hv: vmbus: fix double free in the error path of vmbus_add_channel_work()

In the error path of vmbus_device_register(), device_unregister() is called, which calls vmbus_device_release(). The latter frees the struct hv_device that was passed in to vmbus_device_register(). So remove the kfree() in vmbus_add_channel_work() to avoid a double free.

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
---
 drivers/hv/channel_mgmt.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hv/channel_mgmt.c +4 -1
index 5b120402d405..0ebd73d571bc 100644
@@ -533,13 +533,16 @@ static void vmbus_add_channel_work(struct work_struct *work)
 	 * Add the new device to the bus. This will kick off device-driver
 	 * binding which eventually invokes the device driver's AddDevice()
 	 * method.
+	 * If vmbus_device_register() fails, the 'device_obj' is freed in
+	 * vmbus_device_release() as called by device_unregister() in the
+	 * error path of vmbus_device_register(). In the outside error
+	 * path, there's no need to free it.
 	 */
 	ret = vmbus_device_register(newchannel->device_obj);
 
 	if (ret != 0) {
 		pr_err("unable to add child device object (relid %d)\n",
 			newchannel->offermsg.child_relid);
-		kfree(newchannel->device_obj);
 		goto err_deq_chan;
 	}
 
Signature · 1 line
2.25.1
Reviewed-by on v3 1/2
#2 lore raw

RE: [PATCH v3 1/2] Drivers: hv: vmbus: fix double free in the error path of vmbus_add_channel_work()

From: Yang Yingliang <[email protected]>

Quoted 37 lines

In the error path of vmbus_device_register(), device_unregister() is called, which calls vmbus_device_release(). The latter frees the struct hv_device that was passed in to vmbus_device_register(). So remove the kfree() in vmbus_add_channel_work() to avoid a double free.

Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")
---
 drivers/hv/channel_mgmt.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hv/channel_mgmt.c +4 -2
index 5b120402d405..0ebd73d571bc 100644
@@ -533,13 +533,16 @@ static void vmbus_add_channel_work(struct work_struct *work)
 	 * Add the new device to the bus. This will kick off device-driver
 	 * binding which eventually invokes the device driver's AddDevice()
 	 * method.
+	 * If vmbus_device_register() fails, the 'device_obj' is freed in
+	 * vmbus_device_release() as called by device_unregister() in the
+	 * error path of vmbus_device_register(). In the outside error
+	 * path, there's no need to free it.
 	 */
 	ret = vmbus_device_register(newchannel->device_obj);
 
 	if (ret != 0) {
 		pr_err("unable to add child device object (relid %d)\n",
 			newchannel->offermsg.child_relid);
-		kfree(newchannel->device_obj);
 		goto err_deq_chan;
 	}
 
--
Quoted 1 line

2.25.1

#3 lore raw

PATCH v3 2/2 Drivers: hv: vmbus: fix possible memory leak in vmbus_device_register()

In reply to Yang Yingliang

If device_register() returns error in vmbus_device_register(), the name allocated by dev_set_name() must be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup().

Fixes 09d50ff8a233 ("Staging: hv: make the Hyper-V virtual bus code build")
---
 drivers/hv/vmbus_drv.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hv/vmbus_drv.c +1 -0
index 8b2e413bf19c..e592c481f7ae 100644
@@ -2082,6 +2082,7 @@ int vmbus_device_register(struct hv_device *child_device_obj)
 	ret = device_register(&child_device_obj->device);
 	if (ret) {
 		pr_err("Unable to register child device\n");
+		put_device(&child_device_obj->device);
 		return ret;
 	}
 
Signature · 1 line
2.25.1
Reviewed-by on v3 2/2
#4 lore raw

RE: [PATCH v3 2/2] Drivers: hv: vmbus: fix possible memory leak in vmbus_device_register()

From: Yang Yingliang <[email protected]> Sent: Saturday, November 19, 2022 12:12 AM

Quoted 27 lines

If device_register() returns error in vmbus_device_register(), the name allocated by dev_set_name() must be freed. As comment of device_register() says, it should use put_device() to give up the reference in the error path. So fix this by calling put_device(), then the name can be freed in kobject_cleanup().

Fixes 09d50ff8a233 ("Staging: hv: make the Hyper-V virtual bus code build")
---
 drivers/hv/vmbus_drv.c | 1 +
 1 file changed, 1 insertion(+)
drivers/hv/vmbus_drv.c +1 -1
index 8b2e413bf19c..e592c481f7ae 100644
@@ -2082,6 +2082,7 @@ int vmbus_device_register(struct hv_device
 *child_device_obj)
 	ret = device_register(&child_device_obj->device);
 	if (ret) {
 		pr_err("Unable to register child device\n");
+		put_device(&child_device_obj->device);
 		return ret;
 	}
 
--
Quoted 1 line

2.25.1

maintainer Applied
#5 lore raw
In reply to Yang Yingliang
Quoted 5 lines · Yang Yingliang

This patchset fixes a double free and a memory leak problems.

v2 -> v3:
Update commit message and comment suggested by Michael.

Quoted 8 lines

v1 -> v2:
Add a comment before vmbus_device_register().

Yang Yingliang (2):
Drivers: hv: vmbus: fix double free in the error path of
vmbus_add_channel_work()
Drivers: hv: vmbus: fix possible memory leak in
vmbus_device_register()

I added a new line between the old and new paragraphs in the second commit.

Applied to hyperv-fixes. Thanks.