public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
@ 2023-07-17  4:16 Chenyuan Mi
  2023-07-17 10:50 ` Jonathan Cameron
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Chenyuan Mi @ 2023-07-17  4:16 UTC (permalink / raw)
  To: dan.j.williams
  Cc: Jonathan.Cameron, dave.jiang, rrichter, alison.schofield,
	linux-kernel

When gen_pool_alloc_algo() fails, the error handling path
forgets to free 'res'. It would cause a memory leak bug.

Fix it by add kfree() in error handling path.

Signed-off-by: Chenyuan Mi <michenyuan@huawei.com>
---
 tools/testing/cxl/test/cxl.c |   4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
index 0e78d8e19895..250ffd147067 100644
--- a/tools/testing/cxl/test/cxl.c
+++ b/tools/testing/cxl/test/cxl.c
@@ -405,8 +405,10 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 	INIT_LIST_HEAD(&res->list);
 	phys = gen_pool_alloc_algo(cxl_mock_pool, size,
 				   gen_pool_first_fit_align, &data);
-	if (!phys)
+	if (!phys) {
+		kfree(res);
 		return NULL;
+	}
 
 	res->range = (struct range) {
 		.start = phys,
-- 
2.25.1


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

* Re: [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
  2023-07-17  4:16 [PATCH] cxl: Fix memory leak bug in alloc_mock_res() Chenyuan Mi
@ 2023-07-17 10:50 ` Jonathan Cameron
  2023-07-17 16:19 ` Dave Jiang
  2023-07-17 23:49 ` Dan Williams
  2 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2023-07-17 10:50 UTC (permalink / raw)
  To: Chenyuan Mi
  Cc: dan.j.williams, dave.jiang, rrichter, alison.schofield,
	linux-kernel, linux-cxl

On Mon, 17 Jul 2023 04:16:09 +0000
Chenyuan Mi <michenyuan@huawei.com> wrote:

> When gen_pool_alloc_algo() fails, the error handling path
> forgets to free 'res'. It would cause a memory leak bug.
> 
> Fix it by add kfree() in error handling path.
> 
> Signed-off-by: Chenyuan Mi <michenyuan@huawei.com>

+CC linux-cxl list

Please make sure to include that for any future CXL patches.

> ---
>  tools/testing/cxl/test/cxl.c |   4 +++-
>  1 files changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
> index 0e78d8e19895..250ffd147067 100644
> --- a/tools/testing/cxl/test/cxl.c
> +++ b/tools/testing/cxl/test/cxl.c
> @@ -405,8 +405,10 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
>  	INIT_LIST_HEAD(&res->list);
>  	phys = gen_pool_alloc_algo(cxl_mock_pool, size,
>  				   gen_pool_first_fit_align, &data);
> -	if (!phys)
> +	if (!phys) {
> +		kfree(res);
>  		return NULL;
> +	}
>  
>  	res->range = (struct range) {
>  		.start = phys,


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

* Re: [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
  2023-07-17  4:16 [PATCH] cxl: Fix memory leak bug in alloc_mock_res() Chenyuan Mi
  2023-07-17 10:50 ` Jonathan Cameron
@ 2023-07-17 16:19 ` Dave Jiang
  2023-07-17 23:49 ` Dan Williams
  2 siblings, 0 replies; 5+ messages in thread
From: Dave Jiang @ 2023-07-17 16:19 UTC (permalink / raw)
  To: Chenyuan Mi, dan.j.williams
  Cc: Jonathan.Cameron, rrichter, alison.schofield, linux-kernel



On 7/16/23 21:16, Chenyuan Mi wrote:
> When gen_pool_alloc_algo() fails, the error handling path
> forgets to free 'res'. It would cause a memory leak bug.
> 
> Fix it by add kfree() in error handling path.
> 
> Signed-off-by: Chenyuan Mi <michenyuan@huawei.com>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>

> ---
>   tools/testing/cxl/test/cxl.c |   4 +++-
>   1 files changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
> index 0e78d8e19895..250ffd147067 100644
> --- a/tools/testing/cxl/test/cxl.c
> +++ b/tools/testing/cxl/test/cxl.c
> @@ -405,8 +405,10 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
>   	INIT_LIST_HEAD(&res->list);
>   	phys = gen_pool_alloc_algo(cxl_mock_pool, size,
>   				   gen_pool_first_fit_align, &data);
> -	if (!phys)
> +	if (!phys) {
> +		kfree(res);
>   		return NULL;
> +	}
>   
>   	res->range = (struct range) {
>   		.start = phys,

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

* RE: [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
  2023-07-17  4:16 [PATCH] cxl: Fix memory leak bug in alloc_mock_res() Chenyuan Mi
  2023-07-17 10:50 ` Jonathan Cameron
  2023-07-17 16:19 ` Dave Jiang
@ 2023-07-17 23:49 ` Dan Williams
  2023-07-18  2:53   ` Dan Williams
  2 siblings, 1 reply; 5+ messages in thread
From: Dan Williams @ 2023-07-17 23:49 UTC (permalink / raw)
  To: Chenyuan Mi, dan.j.williams
  Cc: Jonathan.Cameron, dave.jiang, rrichter, alison.schofield,
	linux-kernel, linux-cxl

Chenyuan Mi wrote:
> When gen_pool_alloc_algo() fails, the error handling path
> forgets to free 'res'. It would cause a memory leak bug.
> 
> Fix it by add kfree() in error handling path.

Going forward I want to set the policy that any error path resource leaks be
fixed by converting to using __free() and associated helpers.

So in this case it would be:

-- >8 --
diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
index 0e78d8e19895..ea04995fe42c 100644
--- a/tools/testing/cxl/test/cxl.c
+++ b/tools/testing/cxl/test/cxl.c
@@ -396,7 +396,8 @@ static void depopulate_all_mock_resources(void)
 
 static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 {
-	struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
+	struct cxl_mock_res *res __free(kfree) =
+		kzalloc(sizeof(*res), GFP_KERNEL);
 	struct genpool_data_align data = {
 		.align = align,
 	};
@@ -416,7 +417,7 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
 	list_add(&res->list, &mock_res);
 	mutex_unlock(&mock_res_lock);
 
-	return res;
+	return_ptr(res);
 }
 
 static int populate_cedt(void)

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

* RE: [PATCH] cxl: Fix memory leak bug in alloc_mock_res()
  2023-07-17 23:49 ` Dan Williams
@ 2023-07-18  2:53   ` Dan Williams
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Williams @ 2023-07-18  2:53 UTC (permalink / raw)
  To: Dan Williams, Chenyuan Mi
  Cc: Jonathan.Cameron, dave.jiang, rrichter, alison.schofield,
	linux-kernel, linux-cxl

Dan Williams wrote:
> Chenyuan Mi wrote:
> > When gen_pool_alloc_algo() fails, the error handling path
> > forgets to free 'res'. It would cause a memory leak bug.
> > 
> > Fix it by add kfree() in error handling path.
> 
> Going forward I want to set the policy that any error path resource leaks be
> fixed by converting to using __free() and associated helpers.
> 
> So in this case it would be:

...and to be clear I do think the fix should be applied in a backportable
fashion and then followed up with a conversion for the next merge window.

> 
> -- >8 --
> diff --git a/tools/testing/cxl/test/cxl.c b/tools/testing/cxl/test/cxl.c
> index 0e78d8e19895..ea04995fe42c 100644
> --- a/tools/testing/cxl/test/cxl.c
> +++ b/tools/testing/cxl/test/cxl.c
> @@ -396,7 +396,8 @@ static void depopulate_all_mock_resources(void)
>  
>  static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
>  {
> -	struct cxl_mock_res *res = kzalloc(sizeof(*res), GFP_KERNEL);
> +	struct cxl_mock_res *res __free(kfree) =
> +		kzalloc(sizeof(*res), GFP_KERNEL);
>  	struct genpool_data_align data = {
>  		.align = align,
>  	};
> @@ -416,7 +417,7 @@ static struct cxl_mock_res *alloc_mock_res(resource_size_t size, int align)
>  	list_add(&res->list, &mock_res);
>  	mutex_unlock(&mock_res_lock);
>  
> -	return res;
> +	return_ptr(res);
>  }
>  
>  static int populate_cedt(void)



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

end of thread, other threads:[~2023-07-18  2:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-17  4:16 [PATCH] cxl: Fix memory leak bug in alloc_mock_res() Chenyuan Mi
2023-07-17 10:50 ` Jonathan Cameron
2023-07-17 16:19 ` Dave Jiang
2023-07-17 23:49 ` Dan Williams
2023-07-18  2:53   ` Dan Williams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox