[PATCH v2 2/2] mm/damon/reclaim: reset thread status parameters upon kdamond termination

Liew Rui Yan
2026-04-13T18:52:54Z

Problem
========
When kdamond terminates unexpectedly, 'enabled' remains 'Y' and
'kdamond_pid' remains stale. This prevents user from restarting DAMON
because both writing 'Y' and 'N' to 'enabled' will fail.

"Unexpected termination" here means the kdamond exits without any user
request (e.g., not by writing 'N' to 'enabled').

User Impact
===========
Once kdamond terminates this way, it cannot be restarted via sysfs
because:

1. DAMON_RECLAIM is built into the kernel, so it cannot be unloaded and
reloaded at runtime.
2. Writing 'N' to 'enabled' fails because kdamond no longer exists;
Writing 'Y' does nothing, as 'enabled' is already Y.

As a result, the only way to restore DAMON functionality is a full
system reboot.

Solution
========
damon_commit_ctx() sets 'maybe_corrupted=true' at the beginning and only
sets it to false upon successful completion. When 'maybe_corrupted'
remains true, kdamond will terminate eventually.

Therefore:
1. In damon_reclaim_turn(): Add fallback logic to reset parameters when
damon_stop() fails but kdamond is not running.
2. In damon_reclaim_apply_parameters(): Reset parameters when
damon_commit_ctx() fails, as kdamond will terminate due to
maybe_corrupted mechanism.

Signed-off-by: Liew Rui Yan <aethernet65535@gmail.com>
---
mm/damon/reclaim.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c
index 86da14778658..e3e148fd80f9 100644
--- a/mm/damon/reclaim.c
+++ b/mm/damon/reclaim.c
@@ -250,6 +250,10 @@ static int damon_reclaim_apply_parameters(void)
if (err)
goto out;
err = damon_commit_ctx(ctx, param_ctx);
+ if (err) {
+ enabled = false;
+ kdamond_pid = -1;
+ }
out:
damon_destroy_ctx(param_ctx);
return err;
@@ -290,8 +294,13 @@ static int damon_reclaim_turn(bool on)

if (!on) {
err = damon_stop(&ctx, 1);
- if (!err)
- kdamond_pid = -1;
+ if (err) {
+ if (!damon_is_running(ctx))
+ err = 0;
+ else
+ return err;
+ }
+ kdamond_pid = -1;
return err;
}

--
2.53.0