From: yahia ahmed 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 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