Skip to contentSkip to conversation

cxl: Fix memory leak bug in alloc_mock_res()

PATCH In review Chenyuan Mi 4 participants 5 messages Jul 17, 2023 – Jul 18, 2023 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl: Fix memory leak bug in alloc_mock_res()

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.

---
 tools/testing/cxl/test/cxl.c |   4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)
tools/testing/cxl/test/cxl.c +3 -1
index 0e78d8e19895..250ffd147067 100644
@@ -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,
Signature · 1 line
2.25.1
#1 lore raw
Quoted 6 lines · On Mon, 17 Jul 2023 04:16:09 +0000 Chenyuan Mi <[email protected]> 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.

+CC linux-cxl list

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

Quoted 20 lines
---
 tools/testing/cxl/test/cxl.c |   4 +++-
 1 files changed, 3 insertions(+), 1 deletion(-)
tools/testing/cxl/test/cxl.c +3 -1
index 0e78d8e19895..250ffd147067 100644
@@ -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,
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Chenyuan Mi
Quoted 6 lines · Chenyuan Mi

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.

Quoted 20 lines
---
  tools/testing/cxl/test/cxl.c |   4 +++-
  1 files changed, 3 insertions(+), 1 deletion(-)
tools/testing/cxl/test/cxl.c +3 -1
index 0e78d8e19895..250ffd147067 100644
@@ -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,
#3 lore raw
In reply to Chenyuan Mi
Quoted 4 lines · Chenyuan Mi

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

tools/testing/cxl/test/cxl.c +3 -2
index 0e78d8e19895..ea04995fe42c 100644
@@ -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)
#4 lore raw
Quoted 10 lines · Dan Williams
Quoted 4 lines · Chenyuan Mi

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.

Quoted 25 lines

-- >8 --

tools/testing/cxl/test/cxl.c +3 -2
index 0e78d8e19895..ea04995fe42c 100644
@@ -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)