Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues
@ 2026-06-19 23:46 yahia
  2026-06-19 23:46 ` [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx yahia
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: yahia @ 2026-06-19 23:46 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia ahmed

From: yahia ahmed <yahia.a.abdrabou@gmail.com>

This patch series addresses a potential memory
leak in the damon_reclain_init() function and a
premature destruction of ctx if there was no error
in the damon_reclaim_parameters() function

yahia (2):
  mm/damon/reclaim: fix destruction of ctx
  mm/damon/reclaim: fix potential memory leak

 mm/damon/reclaim.c | 7 +++++++
 1 file changed, 7 insertions(+)

-- 
2.54.0



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx
  2026-06-19 23:46 [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues yahia
@ 2026-06-19 23:46 ` yahia
  2026-06-19 23:46 ` [PATCH 2/2] mm/damon/reclaim: fix potential memory leak yahia
  2026-06-20  0:26 ` [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues SeongJae Park
  2 siblings, 0 replies; 11+ messages in thread
From: yahia @ 2026-06-19 23:46 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia ahmed

From: yahia ahmed <yahia.a.abdrabou@gmail.com>

Fix the early destruction of ctx if no error was encountered
by returning 0 on success.

Signed-off-by: yahia ahmed <yahia.a.abdrabou@gmail.com>
---
 mm/damon/reclaim.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index ce4499cf4b8b..15bc06ae7505 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -268,6 +268,9 @@ static int damon_reclaim_apply_parameters(void)
 	if (err)
 		goto out;
 	err = damon_commit_ctx(ctx, param_ctx);
+
+	if (!err)
+		return 0;
 out:
 	damon_destroy_ctx(param_ctx);
 	return err;
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/2] mm/damon/reclaim: fix potential memory leak
  2026-06-19 23:46 [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues yahia
  2026-06-19 23:46 ` [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx yahia
@ 2026-06-19 23:46 ` yahia
  2026-06-20  0:26 ` [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues SeongJae Park
  2 siblings, 0 replies; 11+ messages in thread
From: yahia @ 2026-06-19 23:46 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia ahmed

From: yahia ahmed <yahia.a.abdrabou@gmail.com>

Fix a potential memory leak if initialization encounters
an error, ctx won't be destroyed.

Signed-off-by: yahia ahmed <yahia.a.abdrabou@gmail.com>
---
 mm/damon/reclaim.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 15bc06ae7505..dfe52717d92c 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -481,6 +481,10 @@ static int __init damon_reclaim_init(void)
 out:
 	if (err && enabled)
 		enabled = false;
+
+	/* Prevent potenial memory leakage */
+	if (err && ctx)
+		damon_destroy_ctx(ctx);
 	return err;
 }
 
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues
  2026-06-19 23:46 [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues yahia
  2026-06-19 23:46 ` [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx yahia
  2026-06-19 23:46 ` [PATCH 2/2] mm/damon/reclaim: fix potential memory leak yahia
@ 2026-06-20  0:26 ` SeongJae Park
  2026-06-20  0:35   ` [PATCH v2 0/1] fix initialization " yahia
  2 siblings, 1 reply; 11+ messages in thread
From: SeongJae Park @ 2026-06-20  0:26 UTC (permalink / raw)
  To: yahia; +Cc: SeongJae Park, akpm, damon, linux-mm, linux-kernel

On Sat, 20 Jun 2026 02:46:04 +0300 yahia <yahia.a.abdrabou@gmail.com> wrote:

> From: yahia ahmed <yahia.a.abdrabou@gmail.com>
> 
> This patch series addresses a potential memory
> leak in the damon_reclain_init() function and a
> premature destruction of ctx if there was no error
> in the damon_reclaim_parameters() function
> 
> yahia (2):
>   mm/damon/reclaim: fix destruction of ctx
>   mm/damon/reclaim: fix potential memory leak

Looks both patches are unnecessary to me.  Could you please explain the issues
in detail, and if I'm wrong?


Thanks,
SJ

[...]


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 0/1] fix initialization and parameter issues
  2026-06-20  0:26 ` [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues SeongJae Park
@ 2026-06-20  0:35   ` yahia
  2026-06-20  0:35     ` [PATCH v2 1/1] mm/damon/reclaim: fix potential memory leak yahia
  0 siblings, 1 reply; 11+ messages in thread
From: yahia @ 2026-06-20  0:35 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia ahmed

From: yahia ahmed <yahia.a.abdrabou@gmail.com>

Fixed a potential memory leak by removing the
early return to let the function continue with
out and set the ctx variable to NULL to prevent
a use after free, In damon_reclaim_init() function
if an error is returned, the ctx variable is never
freed.
~~~
Changes in v2:
- Remove early return
- Set ctx to NULL to avoid a use after free
- Fix typo

yahia (1):
  mm/damon/reclaim: fix potential memory leak

 mm/damon/reclaim.c | 5 +++++
 1 file changed, 5 insertions(+)

-- 
2.54.0



^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/1] mm/damon/reclaim: fix potential memory leak
  2026-06-20  0:35   ` [PATCH v2 0/1] fix initialization " yahia
@ 2026-06-20  0:35     ` yahia
  2026-06-20  3:29       ` [PATCH v3] " yahia
  0 siblings, 1 reply; 11+ messages in thread
From: yahia @ 2026-06-20  0:35 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia

Signed-off-by: yahia <yahia.a.abdrabou@gmail.com>
---
 mm/damon/reclaim.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index ce4499cf4b8b..6601b08996c6 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -478,6 +478,11 @@ static int __init damon_reclaim_init(void)
 out:
 	if (err && enabled)
 		enabled = false;
+
+	if (err && ctx) {
+		damon_destroy_ctx(ctx);
+		ctx = NULL;
+	}
 	return err;
 }
 
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v3] mm/damon/reclaim: fix potential memory leak
  2026-06-20  0:35     ` [PATCH v2 1/1] mm/damon/reclaim: fix potential memory leak yahia
@ 2026-06-20  3:29       ` yahia
  2026-06-20 16:40         ` SeongJae Park
  0 siblings, 1 reply; 11+ messages in thread
From: yahia @ 2026-06-20  3:29 UTC (permalink / raw)
  To: sj; +Cc: akpm, damon, linux-mm, linux-kernel, yahia ahmed

From: yahia ahmed <yahia.a.abdrabou@gmail.com>

Fix a potential race condition by locking the ctx
then using damon_stop() to eliminate the thread then
destroy ctx.

Signed-off-by: yahia ahmed <yahia.a.abdrabou@gmail.com>
v3:
- Add a mutex lock to prevent possible race condition
- Stop the kdamond thread before destroying ctx
v2:
- Remove early return
- Set ctx to NULL to avoid a use after free
- Fix typo
---
 mm/damon/reclaim.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 6601b08996c6..ec5558b7da41 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -480,6 +480,15 @@ static int __init damon_reclaim_init(void)
 		enabled = false;
 
 	if (err && ctx) {
+		/* Hold a lock and stop the thread to prevent a possible race condition */
+		mutex_lock(&ctx->kdamond_lock);
+
+		if (ctx->kdamond) {
+			mutex_unlock(&ctx->kdamond_lock);
+			damon_stop(ctx);
+		} else {
+			mutex_unlock(&ctx->kdamond_lock);
+		}
 		damon_destroy_ctx(ctx);
 		ctx = NULL;
 	}
-- 
2.54.0



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v3] mm/damon/reclaim: fix potential memory leak
  2026-06-20  3:29       ` [PATCH v3] " yahia
@ 2026-06-20 16:40         ` SeongJae Park
  2026-06-20 16:50           ` [PATCH v1] " yahia
  0 siblings, 1 reply; 11+ messages in thread
From: SeongJae Park @ 2026-06-20 16:40 UTC (permalink / raw)
  To: yahia; +Cc: SeongJae Park, akpm, damon, linux-mm, linux-kernel

Hello Yahia,


Please answer my question [1] to the v1 of this patch, before sending new
version of patches.  I will not review your patch before the discussion on your
v1 is finished.

[1] https://lore.kernel.org/20260620002614.83004-1-sj@kernel.org


Thanks,
SJ

[...]


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v1] mm/damon/reclaim: fix potential memory leak
  2026-06-20 16:40         ` SeongJae Park
@ 2026-06-20 16:50           ` yahia
  2026-06-20 17:09             ` SeongJae Park
  0 siblings, 1 reply; 11+ messages in thread
From: yahia @ 2026-06-20 16:50 UTC (permalink / raw)
  To: sj; +Cc: damon, linux-mm, linux-kernel

Hi SJ,

Regarding you question in v1, This patch
addresses a potential memory leak in the
out path, ctx is never destroyed during exit
thus creating a potential memory leak.

Best regards,
Yahia


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v1] mm/damon/reclaim: fix potential memory leak
  2026-06-20 16:50           ` [PATCH v1] " yahia
@ 2026-06-20 17:09             ` SeongJae Park
  2026-06-20 17:20               ` yahia
  0 siblings, 1 reply; 11+ messages in thread
From: SeongJae Park @ 2026-06-20 17:09 UTC (permalink / raw)
  To: yahia; +Cc: SeongJae Park, damon, linux-mm, linux-kernel

On Sat, 20 Jun 2026 19:50:55 +0300 yahia <yahia.a.abdrabou@gmail.com> wrote:

> Hi SJ,
> 
> Regarding you question in v1, This patch
> addresses a potential memory leak in the
> out path, ctx is never destroyed during exit
> thus creating a potential memory leak.

It is kept in the memory by the design.  It will be destroyed when the user
enables DAMON_RECLAIM later.  So, unfortunately I don't think this patch is
needed.  Please correct me if I'm wrong.

Also, I'd recommend you to thoroughly read and follow the patch submission
guide [1], if you didn't have a chance to read it yet.

[1] https://docs.kernel.org/process/submitting-patches.html


Thanks,
SJ

[...]


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v1] mm/damon/reclaim: fix potential memory leak
  2026-06-20 17:09             ` SeongJae Park
@ 2026-06-20 17:20               ` yahia
  0 siblings, 0 replies; 11+ messages in thread
From: yahia @ 2026-06-20 17:20 UTC (permalink / raw)
  To: sj; +Cc: damon, linux-mm, linux-kernel

Hi SJ,

Thank you for your clarification
on the design intentions.

Best regards,
Yahia


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-06-20 17:20 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-19 23:46 [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues yahia
2026-06-19 23:46 ` [PATCH 1/2] mm/damon/reclaim: fix destruction of ctx yahia
2026-06-19 23:46 ` [PATCH 2/2] mm/damon/reclaim: fix potential memory leak yahia
2026-06-20  0:26 ` [PATCH 0/2] mm/damon/reclaim: fix initalization and parameter issues SeongJae Park
2026-06-20  0:35   ` [PATCH v2 0/1] fix initialization " yahia
2026-06-20  0:35     ` [PATCH v2 1/1] mm/damon/reclaim: fix potential memory leak yahia
2026-06-20  3:29       ` [PATCH v3] " yahia
2026-06-20 16:40         ` SeongJae Park
2026-06-20 16:50           ` [PATCH v1] " yahia
2026-06-20 17:09             ` SeongJae Park
2026-06-20 17:20               ` yahia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox