Skip to contentSkip to conversation

ACPI / APEI: release resources if gen_pool_add fails

PATCH In review luanshi 4 participants 6 messages Jul 15, 2019 – Aug 26, 2019

Subsystems (2)
lore
Share & tools
lore

luanshi

author
#0 lore raw

PATCH v4 1/1 ACPI / APEI: release resources if gen_pool_add fails

To avoid memory leaks, destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init().

---
 drivers/acpi/apei/ghes.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/acpi/apei/ghes.c +14 -3
index a66e00f..6d42ae1 100644
@@ -153,10 +153,11 @@ static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
 int ghes_estatus_pool_init(int num_ghes)
 {
 	unsigned long addr, len;
+	int rc = 0;
 
 	ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
 	if (!ghes_estatus_pool)
-		return -ENOMEM;
+		goto nomem;
 
 	len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
 	len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
@@ -164,7 +165,7 @@ int ghes_estatus_pool_init(int num_ghes)
 	ghes_estatus_pool_size_request = PAGE_ALIGN(len);
 	addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
 	if (!addr)
-		return -ENOMEM;
+		goto pool_destroy;
 
 	/*
 	 * New allocation must be visible in all pgd before it can be found by
@@ -172,7 +173,17 @@ int ghes_estatus_pool_init(int num_ghes)
 	 */
 	vmalloc_sync_all();
 
-	return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	if (rc)
+		goto pool_destroy;
+	return 0;
+
+pool_destroy:
+	gen_pool_destroy(ghes_estatus_pool);
+	if (addr)
+		vfree((void *)addr);
+nomem:
+	return -ENOMEM;
 }
 
 static int map_gen_v2(struct ghes *ghes)
Signature · 1 line
1.8.3.1
Reviewed-by on v4 1/1
#1 lore raw

Hi Liguang,

Quoted 5 lines · luanshi

To avoid memory leaks, destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init().

---

Playing spot-the-difference with [v3], you've moved an empty line.

Please include a change log in this space below the '---' tearoff. This helps reviewers know what you changed between versions, and git knows not to add stuff in here to the log.

This is still:

Thanks,

James

[v3]
https://lore.kernel.org/linux-acpi/[email protected]/

Quoted 3 lines
 drivers/acpi/apei/ghes.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

[...]

author
#2 lore raw

Hi James,

Quoted 8 lines · James Morse

Hi Liguang,

Quoted 5 lines · luanshi

To avoid memory leaks, destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init().

---

Quoted 7 lines

Playing spot-the-difference with [v3], you've moved an empty line.

Please include a change log in this space below the '---' tearoff. This helps reviewers know what you changed between versions, and git knows not to add stuff in here to the log.

This is still:

Thanks for your review. Because your reviewed-by was given ealier, and the code is reconstructed,

so I remove the reviewed-by. I will send v5 by your suggestions.

Thanks,

Liguang

Quoted 14 lines

Thanks,

James

[v3]
https://lore.kernel.org/linux-acpi/[email protected]/

Quoted 3 lines
  drivers/acpi/apei/ghes.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)

[...]

#3 lore raw

Hi Liguang,

Quoted 12 lines · 乱石
Quoted 11 lines · James Morse
Quoted 5 lines · luanshi

To avoid memory leaks, destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init().

---

Playing spot-the-difference with [v3], you've moved an empty line.

Please include a change log in this space below the '---' tearoff. This helps reviewers know what you changed between versions, and git knows not to add stuff in here to the log.

Quoted 7 lines
Quoted 2 lines

This is still:

Thanks for your review. Because your reviewed-by was given ealier, and the code is reconstructed,

so I remove the reviewed-by. I will send v5 by your suggestions.

There is no need to post a new version just to pick up the tags.

Having a versioned changelog on your future work makes it easier for reviewers to know what changed between versions.

Thanks,

James

#4 lore raw

PATCH 1/1 ACPI / APEI: release resources if gen_pool_add fails

In reply to luanshi

Hi,

here's v4.1 with the labels properly balanced.

Thx.

---
From: Liguang Zhang <[email protected]>
Date: Mon, 15 Jul 2019 14:58:44 +0800
Subject: [PATCH] ACPI / APEI: Release resources if gen_pool_add() fails

Destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init() in order to avoid memory leaks.

[ bp: do the labels properly and with descriptive names and massage. ]

---
 drivers/acpi/apei/ghes.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/acpi/apei/ghes.c +15 -2
index a66e00fe31fe..66205ec54555 100644
@@ -153,6 +153,7 @@ static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
 int ghes_estatus_pool_init(int num_ghes)
 {
 	unsigned long addr, len;
+	int rc;
 
 	ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
 	if (!ghes_estatus_pool)
@@ -164,7 +165,7 @@ int ghes_estatus_pool_init(int num_ghes)
 	ghes_estatus_pool_size_request = PAGE_ALIGN(len);
 	addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
 	if (!addr)
-		return -ENOMEM;
+		goto err_pool_alloc;
 
 	/*
 	 * New allocation must be visible in all pgd before it can be found by
@@ -172,7 +173,19 @@ int ghes_estatus_pool_init(int num_ghes)
 	 */
 	vmalloc_sync_all();
 
-	return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	if (rc)
+		goto err_pool_add;
+
+	return 0;
+
+err_pool_add:
+	vfree((void *)addr);
+
+err_pool_alloc:
+	gen_pool_destroy(ghes_estatus_pool);
+
+	return -ENOMEM;
 }
 
 static int map_gen_v2(struct ghes *ghes)
Signature · 8 lines
2.21.0


-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.
#5 lore raw
Quoted 3 lines · CEST Borislav Petkov

Hi,

here's v4.1 with the labels properly balanced.

Applied, thanks!

Quoted 65 lines

---
From: Liguang Zhang <[email protected]>
Date: Mon, 15 Jul 2019 14:58:44 +0800
Subject: [PATCH] ACPI / APEI: Release resources if gen_pool_add() fails

Destroy ghes_estatus_pool and release memory allocated via vmalloc() on errors in ghes_estatus_pool_init() in order to avoid memory leaks.

[ bp: do the labels properly and with descriptive names and massage. ]

---
 drivers/acpi/apei/ghes.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/acpi/apei/ghes.c +15 -2
index a66e00fe31fe..66205ec54555 100644
@@ -153,6 +153,7 @@ static void ghes_unmap(void __iomem *vaddr, enum fixed_addresses fixmap_idx)
 int ghes_estatus_pool_init(int num_ghes)
 {
 	unsigned long addr, len;
+	int rc;
 
 	ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
 	if (!ghes_estatus_pool)
@@ -164,7 +165,7 @@ int ghes_estatus_pool_init(int num_ghes)
 	ghes_estatus_pool_size_request = PAGE_ALIGN(len);
 	addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
 	if (!addr)
-		return -ENOMEM;
+		goto err_pool_alloc;
 
 	/*
 	 * New allocation must be visible in all pgd before it can be found by
@@ -172,7 +173,19 @@ int ghes_estatus_pool_init(int num_ghes)
 	 */
 	vmalloc_sync_all();
 
-	return gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
+	if (rc)
+		goto err_pool_add;
+
+	return 0;
+
+err_pool_add:
+	vfree((void *)addr);
+
+err_pool_alloc:
+	gen_pool_destroy(ghes_estatus_pool);
+
+	return -ENOMEM;
 }
 
 static int map_gen_v2(struct ghes *ghes)