Drivers: hv: vmbus: fix two issues
Subsystems (2)
v2 cover Drivers: hv: vmbus: fix two issues
This patchset fixes a double free and a memory leak problems.
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 | 4 +++- drivers/hv/vmbus_drv.c | 1 + 2 files changed, 4 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
PATCH v2 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, hv_device has already been freed in vmbus_device_release(), remove the kfree() in vmbus_add_channel_work() to avoid double free.
--- drivers/hv/channel_mgmt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/hv/channel_mgmt.c +3 -1
@@ -533,13 +533,15 @@ 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' will be freed +* in vmbus_device_release() in vmbus_device_register(). In the outside +* error path, it'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
RE: [PATCH v2 1/2] Drivers: hv: vmbus: fix double free in the error path of vmbus_add_channel_work()
From: Yang Yingliang <[email protected]> Sent: Thursday, November 10, 2022 5:49 PM
Quoted 4 lines
In the error path of
vmbus_device_register(),device_unregister()is called,hv_devicehas already been freed invmbus_device_release(), remove thekfree()invmbus_add_channel_work()to avoid double free.
Let me suggest some clearer wording in the commit message:
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.
Quoted 12 lines
Fixes c2e5df616e1a ("vmbus: add per-channel sysfs info")Signed-off-by Yang Yingliang <[email protected]>--- drivers/hv/channel_mgmt.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/hv/channel_mgmt.c +0
-0
drivers/hv/channel_mgmt.c static void vmbus_add_channel_work(struct work_struct · line 539
Quoted 7 lines
drivers/hv/channel_mgmt.c +3
-0@@ -533,13 +533,15 @@ 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' will be freed + * in vmbus_device_release() in vmbus_device_register(). In the outside + * error path, it's no need to free it.
Let me suggest clarifying the comment as well:
* 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.
Quoted 12 lines
drivers/hv/channel_mgmt.c +0
-2*/ 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; } --2.25.1
PATCH v2 2/2 Drivers: hv: vmbus: fix possible memory leak in vmbus_device_register()
In reply to Yang YingliangIf device_register() returns error in vmbus_device_register(), the name allocated by dev_set_name() need 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().
--- drivers/hv/vmbus_drv.c | 1 + 1 file changed, 1 insertion(+)
drivers/hv/vmbus_drv.c +1 -0
@@ -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
RE: [PATCH v2 2/2] Drivers: hv: vmbus: fix possible memory leak in vmbus_device_register()
From: Yang Yingliang <[email protected]> Sent: Thursday, November 10, 2022 5:49 PM
Quoted 3 lines
If
device_register()returns error invmbus_device_register(), the name allocated bydev_set_name()need be freed. As comment
s/need be freed/must be freed/
Quoted 25 lines
of
device_register()says, it should useput_device()to give up the reference in the error path. So fix this by callingput_device(), then the name can be freed inkobject_cleanup().Fixes 09d50ff8a233 ("Staging: hv: make the Hyper-V virtual bus code build")Signed-off-by Yang Yingliang <[email protected]>--- drivers/hv/vmbus_drv.c | 1 + 1 file changed, 1 insertion(+)drivers/hv/vmbus_drv.c +1
-1@@ -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; } --2.25.1