Problem ======= kdamond does not actively modify the module's (DAMON_LRU_SORT/DAMON_RECLAIM) 'enabled' and 'kdamond_pid' after exiting. After an unexpected termination, user will still see 'enabled' as 'Y' and 'kdamond_pid' as a value other than -1. Furthermore, user cannot restart the unexpectedly terminated kdamond by executing 'echo Y/N > enabled' again. Solution ======== Introduce a 'thread_status' structure to link the internal kdamond state with module parameters ('enabled' and 'kdamond_pid'). Specifically: 1. Extend 'struct damon_ctx' to include pointers to the module's parameters. 2. Initialize these pointers in damon_lru_sort_apply_parameters() and damon_reclaim_apply_parameters() to point the respective module variables. 3. Implement damon_update_thread_status() to reset 'enabled' to false and 'kdamond_pid' to -1 when the kdamond thread finishes. Signed-off-by: Liew Rui Yan --- include/linux/damon.h | 7 +++++++ mm/damon/core.c | 19 ++++++++++++++++++- mm/damon/lru_sort.c | 5 +++-- mm/damon/reclaim.c | 5 +++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index d9a3babbafc1..66cdac1eea93 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -763,6 +763,11 @@ struct damon_attrs { unsigned long aggr_samples; }; +struct damon_thread_status { + int *kdamond_pid; + bool *enabled; +}; + /** * struct damon_ctx - Represents a context for each monitoring. This is the * main interface that allows users to set the attributes and get the results @@ -841,6 +846,8 @@ struct damon_ctx { struct list_head adaptive_targets; struct list_head schemes; + + struct damon_thread_status thread_status; }; static inline struct damon_region *damon_next_region(struct damon_region *r) diff --git a/mm/damon/core.c b/mm/damon/core.c index db6c67e52d2b..6c71203beec5 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1353,6 +1353,9 @@ int damon_commit_ctx(struct damon_ctx *dst, struct damon_ctx *src) dst->addr_unit = src->addr_unit; dst->min_region_sz = src->min_region_sz; + dst->thread_status.kdamond_pid = src->thread_status.kdamond_pid; + dst->thread_status.enabled = src->thread_status.enabled; + dst->maybe_corrupted = false; return 0; } @@ -2941,6 +2944,14 @@ static void kdamond_init_ctx(struct damon_ctx *ctx) } } +static void damon_update_thread_status(struct damon_ctx *ctx) +{ + if (ctx->thread_status.kdamond_pid) + *ctx->thread_status.kdamond_pid = -1; + if (ctx->thread_status.enabled) + *ctx->thread_status.enabled = false; +} + /* * The monitoring daemon that runs as a kernel thread */ @@ -3065,17 +3076,23 @@ static int kdamond_fn(void *data) kdamond_call(ctx, true); damos_walk_cancel(ctx); - pr_debug("kdamond (%d) finishes\n", current->pid); mutex_lock(&ctx->kdamond_lock); ctx->kdamond = NULL; mutex_unlock(&ctx->kdamond_lock); + if (ctx->thread_status.enabled && *ctx->thread_status.enabled) + pr_debug("kdamond (%d) crashed\n", current->pid); + else + pr_debug("kdamond (%d) finishes\n", current->pid); + mutex_lock(&damon_lock); nr_running_ctxs--; if (!nr_running_ctxs && running_exclusive_ctxs) running_exclusive_ctxs = false; mutex_unlock(&damon_lock); + damon_update_thread_status(ctx); + return 0; } diff --git a/mm/damon/lru_sort.c b/mm/damon/lru_sort.c index 554559d72976..7196010ee9b4 100644 --- a/mm/damon/lru_sort.c +++ b/mm/damon/lru_sort.c @@ -294,6 +294,9 @@ static int damon_lru_sort_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz = max(DAMON_MIN_REGION_SZ / addr_unit, 1); + param_ctx->thread_status.kdamond_pid = &kdamond_pid; + param_ctx->thread_status.enabled = &enabled; + if (!damon_lru_sort_mon_attrs.sample_interval) { err = -EINVAL; goto out; @@ -388,8 +391,6 @@ static int damon_lru_sort_turn(bool on) if (!on) { err = damon_stop(&ctx, 1); - if (!err) - kdamond_pid = -1; return err; } diff --git a/mm/damon/reclaim.c b/mm/damon/reclaim.c index 86da14778658..61592be438d3 100644 --- a/mm/damon/reclaim.c +++ b/mm/damon/reclaim.c @@ -204,6 +204,9 @@ static int damon_reclaim_apply_parameters(void) param_ctx->addr_unit = addr_unit; param_ctx->min_region_sz = max(DAMON_MIN_REGION_SZ / addr_unit, 1); + param_ctx->thread_status.kdamond_pid = &kdamond_pid; + param_ctx->thread_status.enabled = &enabled; + if (!damon_reclaim_mon_attrs.aggr_interval) { err = -EINVAL; goto out; @@ -290,8 +293,6 @@ static int damon_reclaim_turn(bool on) if (!on) { err = damon_stop(&ctx, 1); - if (!err) - kdamond_pid = -1; return err; } -- 2.53.0