Currently, DAMON does not proactively clean up invalid monitoring targets during its runtime. When some monitored targets exit, DAMON still makes the following unnecessary function calls, --damon_for_each_target-- --damon_for_each_region-- damon_do_apply_schemes damos_apply_scheme damon_va_apply_scheme damos_madvise damon_get_mm and it is only in the damon_get_mm() that it may finally discover that the monitoring target no longer exists. To avoid wasting CPU resources, this patch modifies the kdamond_need_stop() logic to proactively clean up monitoring targets when they are found to be invalid. Signed-off-by: Enze Li --- mm/damon/core.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/damon/core.c b/mm/damon/core.c index f9fc0375890a..eb5612bfd6bf 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -2462,7 +2462,8 @@ static void kdamond_split_regions(struct damon_ctx *ctx) */ static bool kdamond_need_stop(struct damon_ctx *ctx) { - struct damon_target *t; + struct damon_target *t, *next; + bool valid_target_exist = false; if (kthread_should_stop()) return true; @@ -2470,11 +2471,16 @@ static bool kdamond_need_stop(struct damon_ctx *ctx) if (!ctx->ops.target_valid) return false; - damon_for_each_target(t, ctx) { + damon_for_each_target_safe(t, next, ctx) { if (ctx->ops.target_valid(t)) - return false; + valid_target_exist = true; + else + damon_destroy_target(t, ctx); } + if (valid_target_exist) + return false; + return true; } -- 2.52.0