[PATCH v1] erofs: fix possible memory leak in z_erofs_gbuf_exit()

Sandeep Dhavale posted 1 patch 1 year, 5 months ago
fs/erofs/zutil.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH v1] erofs: fix possible memory leak in z_erofs_gbuf_exit()
Posted by Sandeep Dhavale 1 year, 5 months ago
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")

Signed-off-by: Sandeep Dhavale <dhavale@google.com>
---
 fs/erofs/zutil.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/erofs/zutil.c b/fs/erofs/zutil.c
index 036024bce9f7..b80f612867c2 100644
--- a/fs/erofs/zutil.c
+++ b/fs/erofs/zutil.c
@@ -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;
 	}
-- 
2.45.2.741.gdbec12cfda-goog
Re: [PATCH v1] erofs: fix possible memory leak in z_erofs_gbuf_exit()
Posted by Chao Yu 1 year, 5 months ago
On 2024/6/25 6:02, Sandeep Dhavale wrote:
> 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")
> 
> Signed-off-by: Sandeep Dhavale <dhavale@google.com>

Reviewed-by: Chao Yu <chao@kernel.org>

Thanks,
Re: [PATCH v1] erofs: fix possible memory leak in z_erofs_gbuf_exit()
Posted by Gao Xiang 1 year, 5 months ago

On 2024/6/25 06:02, Sandeep Dhavale wrote:
> 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")
> 
> Signed-off-by: Sandeep Dhavale <dhavale@google.com>

Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Thanks,
Gao Xiang

> ---
>   fs/erofs/zutil.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/erofs/zutil.c b/fs/erofs/zutil.c
> index 036024bce9f7..b80f612867c2 100644
> --- a/fs/erofs/zutil.c
> +++ b/fs/erofs/zutil.c
> @@ -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;
>   	}