linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
@ 2022-01-26  5:52 Miaoqian Lin
  2022-01-28 21:56 ` Juan Vazquez
  0 siblings, 1 reply; 6+ messages in thread
From: Miaoqian Lin @ 2022-01-26  5:52 UTC (permalink / raw)
  To: K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	Dexuan Cui, Greg Kroah-Hartman, linux-hyperv, linux-kernel
  Cc: linmq006

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")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 drivers/hv/vmbus_drv.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index 17bf55fe3169..9e055697783b 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -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);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
  2022-01-26  5:52 [PATCH] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj Miaoqian Lin
@ 2022-01-28 21:56 ` Juan Vazquez
  2022-02-03 12:44   ` Wei Liu
  2022-02-03 17:30   ` [PATCH v2] " Miaoqian Lin
  0 siblings, 2 replies; 6+ messages in thread
From: Juan Vazquez @ 2022-01-28 21:56 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	Dexuan Cui, Greg Kroah-Hartman, linux-hyperv, linux-kernel

On Wed, Jan 26, 2022 at 05:52:46AM +0000, Miaoqian Lin wrote:
> 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")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  drivers/hv/vmbus_drv.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> index 17bf55fe3169..9e055697783b 100644
> --- a/drivers/hv/vmbus_drv.c
> +++ b/drivers/hv/vmbus_drv.c
> @@ -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.
>  
> -- 
> 2.17.1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
  2022-01-28 21:56 ` Juan Vazquez
@ 2022-02-03 12:44   ` Wei Liu
  2022-02-03 17:30   ` [PATCH v2] " Miaoqian Lin
  1 sibling, 0 replies; 6+ messages in thread
From: Wei Liu @ 2022-02-03 12:44 UTC (permalink / raw)
  To: Juan Vazquez
  Cc: Miaoqian Lin, K. Y. Srinivasan, Haiyang Zhang, Stephen Hemminger,
	Wei Liu, Dexuan Cui, Greg Kroah-Hartman, linux-hyperv,
	linux-kernel

On Fri, Jan 28, 2022 at 01:56:04PM -0800, Juan Vazquez wrote:
> On Wed, Jan 26, 2022 at 05:52:46AM +0000, Miaoqian Lin wrote:
> > 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")
> > Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> > ---
> >  drivers/hv/vmbus_drv.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> > index 17bf55fe3169..9e055697783b 100644
> > --- a/drivers/hv/vmbus_drv.c
> > +++ b/drivers/hv/vmbus_drv.c
> > @@ -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.

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

Thanks,
Wei.

> >  
> > -- 
> > 2.17.1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
  2022-01-28 21:56 ` Juan Vazquez
  2022-02-03 12:44   ` Wei Liu
@ 2022-02-03 17:30   ` Miaoqian Lin
  2022-02-06 14:55     ` Juan Vazquez
  1 sibling, 1 reply; 6+ messages in thread
From: Miaoqian Lin @ 2022-02-03 17:30 UTC (permalink / raw)
  To: juvazq
  Cc: decui, gregkh, haiyangz, kys, linmq006, linux-hyperv,
	linux-kernel, sthemmin, wei.liu

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")
Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
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(-)

diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
index 17bf55fe3169..34a4fd21bdf5 100644
--- a/drivers/hv/vmbus_drv.c
+++ b/drivers/hv/vmbus_drv.c
@@ -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;
 	}
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
  2022-02-03 17:30   ` [PATCH v2] " Miaoqian Lin
@ 2022-02-06 14:55     ` Juan Vazquez
  2022-02-07 17:54       ` Wei Liu
  0 siblings, 1 reply; 6+ messages in thread
From: Juan Vazquez @ 2022-02-06 14:55 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: decui, gregkh, haiyangz, kys, linux-hyperv, linux-kernel,
	sthemmin, wei.liu

On Fri, Feb 04, 2022 at 01:30:08AM +0800, Miaoqian Lin wrote:
> 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")
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
> 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.

Reviewed-by: Juan Vazquez <juvazq@linux.microsoft.com>

> ---
>  drivers/hv/vmbus_drv.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/hv/vmbus_drv.c b/drivers/hv/vmbus_drv.c
> index 17bf55fe3169..34a4fd21bdf5 100644
> --- a/drivers/hv/vmbus_drv.c
> +++ b/drivers/hv/vmbus_drv.c
> @@ -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;
>  	}
> -- 
> 2.25.1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj
  2022-02-06 14:55     ` Juan Vazquez
@ 2022-02-07 17:54       ` Wei Liu
  0 siblings, 0 replies; 6+ messages in thread
From: Wei Liu @ 2022-02-07 17:54 UTC (permalink / raw)
  To: Juan Vazquez
  Cc: Miaoqian Lin, decui, gregkh, haiyangz, kys, linux-hyperv,
	linux-kernel, sthemmin, wei.liu

On Sun, Feb 06, 2022 at 06:55:56AM -0800, Juan Vazquez wrote:
> On Fri, Feb 04, 2022 at 01:30:08AM +0800, Miaoqian Lin wrote:
> > 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")
> > Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> > ---
> > 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.
> 
> Reviewed-by: Juan Vazquez <juvazq@linux.microsoft.com>

Applied to hyperv-fixes. Thanks.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-02-07 18:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-26  5:52 [PATCH] Drivers: hv: vmbus: Fix memory leak in vmbus_add_channel_kobj Miaoqian Lin
2022-01-28 21:56 ` Juan Vazquez
2022-02-03 12:44   ` Wei Liu
2022-02-03 17:30   ` [PATCH v2] " Miaoqian Lin
2022-02-06 14:55     ` Juan Vazquez
2022-02-07 17:54       ` Wei Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).