damon_stat_start() allocates and starts damon_stat_context before registering the repeated damon_call() callback. If damon_call() fails, the function currently returns the error while leaving the context allocated and stored in the global pointer. The retry-time cleanup added for this path only runs if users try to enable DAMON_STAT again. If no retry happens, the failed start leaves the context allocated indefinitely. Roll back the failed start by stopping the kdamond before destroying the context and clearing the global pointer. damon_stop() waits for a live kdamond via kthread_stop_put(); if the worker has already completed teardown, there is no kdamond left to wait on and the context can be destroyed. Fixes: 405f61996d9d ("mm/damon/stat: use damon_call() repeat mode instead of damon_callback") Signed-off-by: Yuho Choi --- mm/damon/stat.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mm/damon/stat.c b/mm/damon/stat.c index 3951b762cbdd..7f222b5b7193 100644 --- a/mm/damon/stat.c +++ b/mm/damon/stat.c @@ -266,7 +266,14 @@ static int damon_stat_start(void) damon_stat_last_refresh_jiffies = jiffies; call_control.data = damon_stat_context; - return damon_call(damon_stat_context, &call_control); + err = damon_call(damon_stat_context, &call_control); + if (err) { + damon_stop(&damon_stat_context, 1); + damon_destroy_ctx(damon_stat_context); + damon_stat_context = NULL; + return err; + } + return 0; } static void damon_stat_stop(void) -- 2.43.0