2022-07-14 06:43:28

by Jianglei Nie

[permalink] [raw]
Subject: [PATCH] mm/damon/reclaim: fix potential memory leak in damon_reclaim_init()

damon_reclaim_init() allocates a memory chunk for ctx with
damon_new_ctx(). When damon_select_ops() fails, ctx is not released, which
will lead to a memory leak.

We should release the ctx with damon_destroy_ctx() when damon_select_ops()
fails to fix the memory leak.

Signed-off-by: Jianglei Nie <[email protected]>
---
mm/damon/reclaim.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 4b07c29effe9..0b3c7396cb90 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -441,8 +441,10 @@ static int __init damon_reclaim_init(void)
if (!ctx)
return -ENOMEM;

- if (damon_select_ops(ctx, DAMON_OPS_PADDR))
+ if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
+ damon_destroy_ctx(ctx);
return -EINVAL;
+ }

ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check;
ctx->callback.after_aggregation = damon_reclaim_after_aggregation;
--
2.25.1


2022-07-14 16:48:32

by SeongJae Park

[permalink] [raw]
Subject: Re: [PATCH] mm/damon/reclaim: fix potential memory leak in damon_reclaim_init()

Hello Jianglei,

On Thu, 14 Jul 2022 14:37:46 +0800 Jianglei Nie <[email protected]> wrote:

> damon_reclaim_init() allocates a memory chunk for ctx with
> damon_new_ctx(). When damon_select_ops() fails, ctx is not released, which
> will lead to a memory leak.
>
> We should release the ctx with damon_destroy_ctx() when damon_select_ops()
> fails to fix the memory leak.

Thank you for this patch!

I think below tags would be better to be added.

Fixes: 4d69c3457821 ("mm/damon/reclaim: use damon_select_ops() instead of damon_{v,p}a_set_operations()")
Cc: <[email protected]> # 5.18.x

>
> Signed-off-by: Jianglei Nie <[email protected]>

Reviewed-by: SeongJae Park <[email protected]>


Thanks,
SJ

> ---
> mm/damon/reclaim.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
> index 4b07c29effe9..0b3c7396cb90 100644
> --- a/mm/damon/reclaim.c
> +++ b/mm/damon/reclaim.c
> @@ -441,8 +441,10 @@ static int __init damon_reclaim_init(void)
> if (!ctx)
> return -ENOMEM;
>
> - if (damon_select_ops(ctx, DAMON_OPS_PADDR))
> + if (damon_select_ops(ctx, DAMON_OPS_PADDR)) {
> + damon_destroy_ctx(ctx);
> return -EINVAL;
> + }
>
> ctx->callback.after_wmarks_check = damon_reclaim_after_wmarks_check;
> ctx->callback.after_aggregation = damon_reclaim_after_aggregation;
> --
> 2.25.1

2022-07-14 18:04:55

by SeongJae Park

[permalink] [raw]
Subject: Re: [PATCH] mm/damon/reclaim: fix potential memory leak in damon_reclaim_init()

Hello Jianglei,

On Thu, 14 Jul 2022 16:44:27 +0000 SeongJae Park <[email protected]> wrote:

> Hello Jianglei,
>
> On Thu, 14 Jul 2022 14:37:46 +0800 Jianglei Nie <[email protected]> wrote:
>
> > damon_reclaim_init() allocates a memory chunk for ctx with
> > damon_new_ctx(). When damon_select_ops() fails, ctx is not released, which
> > will lead to a memory leak.

I realized this issue is also in DAMON_LRU_SORT, so posted a patch:
https://lore.kernel.org/damon/[email protected]/

I mistakenly forgot CC-ing you, so letting you know here.


Thanks,
SJ