linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device
@ 2022-01-10  7:31 Miaoqian Lin
  2022-01-20 17:56 ` Mathieu Poirier
  0 siblings, 1 reply; 5+ messages in thread
From: Miaoqian Lin @ 2022-01-10  7:31 UTC (permalink / raw)
  Cc: linmq006, Mathieu Poirier, Suzuki K Poulose, Mike Leach, Leo Yan,
	Alexander Shishkin, coresight, linux-arm-kernel, linux-kernel

device_register() calls device_initialize(),
according to doc of device_initialize:

    Use put_device() to give up your reference instead of freeing
    * @dev directly once you have called this function.

To prevent potential memleak, use put_device() instead call kfree
directly.

Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
 drivers/hwtracing/coresight/coresight-syscfg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
index 43054568430f..007fa1c761a7 100644
--- a/drivers/hwtracing/coresight/coresight-syscfg.c
+++ b/drivers/hwtracing/coresight/coresight-syscfg.c
@@ -764,7 +764,7 @@ struct device *cscfg_device(void)
 /* Must have a release function or the kernel will complain on module unload */
 static void cscfg_dev_release(struct device *dev)
 {
-	kfree(cscfg_mgr);
+	put_device(dev);
 	cscfg_mgr = NULL;
 }
 
-- 
2.17.1


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

* Re: [PATCH] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device
  2022-01-10  7:31 [PATCH] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device Miaoqian Lin
@ 2022-01-20 17:56 ` Mathieu Poirier
  2022-01-20 18:28   ` Mathieu Poirier
  0 siblings, 1 reply; 5+ messages in thread
From: Mathieu Poirier @ 2022-01-20 17:56 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: Suzuki K Poulose, Mike Leach, Leo Yan, Alexander Shishkin,
	coresight, linux-arm-kernel, linux-kernel

Good morning,

On Mon, Jan 10, 2022 at 07:31:00AM +0000, Miaoqian Lin wrote:
> device_register() calls device_initialize(),
> according to doc of device_initialize:
> 
>     Use put_device() to give up your reference instead of freeing
>     * @dev directly once you have called this function.

That is _if_ device_initialize() is called manually.  In this instance
@dev is registered with device_register() and unregistered with
device_unregister().  The latter conforms to the comment you pointed out and
calls put_device() as expected.

Thanks,
Mathieu

> 
> To prevent potential memleak, use put_device() instead call kfree
> directly.
> 
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
>  drivers/hwtracing/coresight/coresight-syscfg.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
> index 43054568430f..007fa1c761a7 100644
> --- a/drivers/hwtracing/coresight/coresight-syscfg.c
> +++ b/drivers/hwtracing/coresight/coresight-syscfg.c
> @@ -764,7 +764,7 @@ struct device *cscfg_device(void)
>  /* Must have a release function or the kernel will complain on module unload */
>  static void cscfg_dev_release(struct device *dev)
>  {
> -	kfree(cscfg_mgr);
> +	put_device(dev);
>  	cscfg_mgr = NULL;
>  }
>  
> -- 
> 2.17.1
> 

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

* Re: [PATCH] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device
  2022-01-20 17:56 ` Mathieu Poirier
@ 2022-01-20 18:28   ` Mathieu Poirier
  2022-01-24 12:41     ` [PATCH v2] " Miaoqian Lin
  0 siblings, 1 reply; 5+ messages in thread
From: Mathieu Poirier @ 2022-01-20 18:28 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: Suzuki K Poulose, Mike Leach, Leo Yan, Alexander Shishkin,
	coresight, linux-arm-kernel, linux-kernel

On Thu, Jan 20, 2022 at 10:56:42AM -0700, Mathieu Poirier wrote:
> Good morning,
> 
> On Mon, Jan 10, 2022 at 07:31:00AM +0000, Miaoqian Lin wrote:
> > device_register() calls device_initialize(),
> > according to doc of device_initialize:
> > 
> >     Use put_device() to give up your reference instead of freeing
> >     * @dev directly once you have called this function.
> 
> That is _if_ device_initialize() is called manually.  In this instance
> @dev is registered with device_register() and unregistered with
> device_unregister().  The latter conforms to the comment you pointed out and
> calls put_device() as expected.

I originally misunderstood the context - you are referring to the failure path in
cscfg_create_device().  You are correct about needing to call put_device() but
your solution will not work when the module is unloaded properly and
device_unregister() is called by way of cscfg_clear_device().  In that case
device_unregister() is already calling put_device().

Here simply calling put_device() instead of cscfg_dev_release() in the error
path should do just fine.  That will call cscfg_dev_release() and the memory
allocated for cscfg_mgr will be release.

> 
> Thanks,
> Mathieu
> 
> > 
> > To prevent potential memleak, use put_device() instead call kfree
> > directly.
> > 
> > Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> > ---
> >  drivers/hwtracing/coresight/coresight-syscfg.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
> > index 43054568430f..007fa1c761a7 100644
> > --- a/drivers/hwtracing/coresight/coresight-syscfg.c
> > +++ b/drivers/hwtracing/coresight/coresight-syscfg.c
> > @@ -764,7 +764,7 @@ struct device *cscfg_device(void)
> >  /* Must have a release function or the kernel will complain on module unload */
> >  static void cscfg_dev_release(struct device *dev)
> >  {
> > -	kfree(cscfg_mgr);
> > +	put_device(dev);
> >  	cscfg_mgr = NULL;
> >  }
> >  
> > -- 
> > 2.17.1
> > 

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

* [PATCH v2] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device
  2022-01-20 18:28   ` Mathieu Poirier
@ 2022-01-24 12:41     ` Miaoqian Lin
  2022-02-01 17:13       ` Mathieu Poirier
  0 siblings, 1 reply; 5+ messages in thread
From: Miaoqian Lin @ 2022-01-24 12:41 UTC (permalink / raw)
  To: mathieu.poirier
  Cc: alexander.shishkin, coresight, leo.yan, linmq006,
	linux-arm-kernel, linux-kernel, mike.leach, suzuki.poulose

device_register() calls device_initialize(),
according to doc of device_initialize:

    Use put_device() to give up your reference instead of freeing
    * @dev directly once you have called this function.

To prevent potential memleak, use put_device() for error handling.

Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
---
Changes in v2:
- simply call put_device() instead of cscfg_dev_release() in the error
path of cscfg_create_device.
---
 drivers/hwtracing/coresight/coresight-syscfg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
index 43054568430f..c30989e0675f 100644
--- a/drivers/hwtracing/coresight/coresight-syscfg.c
+++ b/drivers/hwtracing/coresight/coresight-syscfg.c
@@ -791,7 +791,7 @@ static int cscfg_create_device(void)
 
 	err = device_register(dev);
 	if (err)
-		cscfg_dev_release(dev);
+		put_device(dev);
 
 create_dev_exit_unlock:
 	mutex_unlock(&cscfg_mutex);
-- 
2.17.1


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

* Re: [PATCH v2] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device
  2022-01-24 12:41     ` [PATCH v2] " Miaoqian Lin
@ 2022-02-01 17:13       ` Mathieu Poirier
  0 siblings, 0 replies; 5+ messages in thread
From: Mathieu Poirier @ 2022-02-01 17:13 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: alexander.shishkin, coresight, leo.yan, linux-arm-kernel,
	linux-kernel, mike.leach, suzuki.poulose

On Mon, Jan 24, 2022 at 12:41:21PM +0000, Miaoqian Lin wrote:
> device_register() calls device_initialize(),
> according to doc of device_initialize:
> 
>     Use put_device() to give up your reference instead of freeing
>     * @dev directly once you have called this function.
> 
> To prevent potential memleak, use put_device() for error handling.
> 
> Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
> ---
> Changes in v2:
> - simply call put_device() instead of cscfg_dev_release() in the error
> path of cscfg_create_device.
> ---
>  drivers/hwtracing/coresight/coresight-syscfg.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/hwtracing/coresight/coresight-syscfg.c b/drivers/hwtracing/coresight/coresight-syscfg.c
> index 43054568430f..c30989e0675f 100644
> --- a/drivers/hwtracing/coresight/coresight-syscfg.c
> +++ b/drivers/hwtracing/coresight/coresight-syscfg.c
> @@ -791,7 +791,7 @@ static int cscfg_create_device(void)
>  
>  	err = device_register(dev);
>  	if (err)
> -		cscfg_dev_release(dev);
> +		put_device(dev);

Applied.

Thanks,
Mathieu

>  
>  create_dev_exit_unlock:
>  	mutex_unlock(&cscfg_mutex);
> -- 
> 2.17.1
> 

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

end of thread, other threads:[~2022-02-01 17:13 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-10  7:31 [PATCH] coresight: syscfg: Fix memleak on registration failure in cscfg_create_device Miaoqian Lin
2022-01-20 17:56 ` Mathieu Poirier
2022-01-20 18:28   ` Mathieu Poirier
2022-01-24 12:41     ` [PATCH v2] " Miaoqian Lin
2022-02-01 17:13       ` Mathieu Poirier

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).