Skip to contentSkip to conversation

erofs: fix possible memory leak in z_erofs_gbuf_exit()

PATCH In review Sandeep Dhavale 3 participants 3 messages Jun 24, 2024 – Jun 26, 2024 EROFS file system

lore
Share & tools
lore

Sandeep Dhavale

author reviewer
#0 lore raw

PATCH 1/1 erofs: fix possible memory leak in z_erofs_gbuf_exit()

Because we incorrectly reused of variable i in z_erofs_gbuf_exit() for inner loop, we may exit early from outer loop resulting in memory leak. Fix this by using separate variable for iterating through inner loop.

Fixes f36f3010f676 ("erofs: rename per-CPU buffers to global buffer pool and make it configurable")
---
 fs/erofs/zutil.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/erofs/zutil.c +4 -4
index 036024bce9f7..b80f612867c2 100644
@@ -148,7 +148,7 @@ int __init z_erofs_gbuf_init(void)
 
 void z_erofs_gbuf_exit(void)
 {
-	int i;
+	int i, j;
 
 	for (i = 0; i < z_erofs_gbuf_count + (!!z_erofs_rsvbuf); ++i) {
 		struct z_erofs_gbuf *gbuf = &z_erofs_gbufpool[i];
@@ -161,9 +161,9 @@ void z_erofs_gbuf_exit(void)
 		if (!gbuf->pages)
 			continue;
 
-		for (i = 0; i < gbuf->nrpages; ++i)
-			if (gbuf->pages[i])
-				put_page(gbuf->pages[i]);
+		for (j = 0; j < gbuf->nrpages; ++j)
+			if (gbuf->pages[j])
+				put_page(gbuf->pages[j]);
 		kfree(gbuf->pages);
 		gbuf->pages = NULL;
 	}
Signature · 1 line
2.45.2.741.gdbec12cfda-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Sandeep Dhavale

Because we incorrectly reused of variable i in z_erofs_gbuf_exit() for inner loop, we may exit early from outer loop resulting in memory leak. Fix this by using separate variable for iterating through inner loop.

Fixes f36f3010f676 ("erofs: rename per-CPU buffers to global buffer pool and make it configurable")

Thanks,
Gao Xiang

Quoted 30 lines
---
  fs/erofs/zutil.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
fs/erofs/zutil.c +4 -4
index 036024bce9f7..b80f612867c2 100644
@@ -148,7 +148,7 @@ int __init z_erofs_gbuf_init(void)
 
  void z_erofs_gbuf_exit(void)
  {
-	int i;
+	int i, j;
 
  	for (i = 0; i < z_erofs_gbuf_count + (!!z_erofs_rsvbuf); ++i) {
  		struct z_erofs_gbuf *gbuf = &z_erofs_gbufpool[i];
@@ -161,9 +161,9 @@ void z_erofs_gbuf_exit(void)
  		if (!gbuf->pages)
  			continue;
 
-		for (i = 0; i < gbuf->nrpages; ++i)
-			if (gbuf->pages[i])
-				put_page(gbuf->pages[i]);
+		for (j = 0; j < gbuf->nrpages; ++j)
+			if (gbuf->pages[j])
+				put_page(gbuf->pages[j]);
  		kfree(gbuf->pages);
  		gbuf->pages = NULL;
  	}
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Sandeep Dhavale
Quoted 7 lines · Sandeep Dhavale

Because we incorrectly reused of variable i in z_erofs_gbuf_exit() for inner loop, we may exit early from outer loop resulting in memory leak. Fix this by using separate variable for iterating through inner loop.

Fixes f36f3010f676 ("erofs: rename per-CPU buffers to global buffer pool and make it configurable")

Thanks,