Skip to content

Commit

Permalink
Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
Browse files Browse the repository at this point in the history
[ Upstream commit 8bc69f8 ]

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: c2e5df6 ("vmbus: add per-channel sysfs info")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
Reviewed-by: Juan Vazquez <juvazq@linux.microsoft.com>
Link: https://lore.kernel.org/r/20220203173008.43480-1-linmq006@gmail.com
Signed-off-by: Wei Liu <wei.liu@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Yuuoniy authored and gregkh committed Feb 23, 2022
1 parent a176d55 commit 91d8866
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion drivers/hv/vmbus_drv.c
Expand Up @@ -1944,8 +1944,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);

Expand All @@ -1954,6 +1956,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;
}
Expand Down

0 comments on commit 91d8866

Please sign in to comment.