The patch titled Subject: mm/damon/core: make damon_start() waits until kdamond_fn() starts has been added to the -mm mm-hotfixes-unstable branch. Its filename is mm-damon-core-make-damon_start-waits-until-kdamond_fn-starts.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-damon-core-make-damon_start-waits-until-kdamond_fn-starts.patch This patch will later appear in the mm-hotfixes-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: SeongJae Park <sj@xxxxxxxxxx> Subject: mm/damon/core: make damon_start() waits until kdamond_fn() starts Date: Fri, 8 Dec 2023 17:50:18 +0000 The cleanup tasks of kdamond threads including reset of corresponding DAMON context's ->kdamond field and decrease of global nr_running_ctxs counter is supposed to be executed by kdamond_fn(). However, commit 0f91d13366a4 ("mm/damon: simplify stop mechanism") made neither damon_start() nor damon_stop() ensure the corresponding kdamond has started the execution of kdamond_fn(). As a result, the cleanup can be skipped if damon_stop() is called fast enough after the previous damon_start(). Especially the skipped reset of ->kdamond could cause a use-after-free. Fix it by waiting for start of kdamond_fn() execution from damon_start(). Link: https://lkml.kernel.org/r/20231208175018.63880-1-sj@xxxxxxxxxx Fixes: 0f91d13366a4 ("mm/damon: simplify stop mechanism") Signed-off-by: SeongJae Park <sj@xxxxxxxxxx> Reported-by: Jakub Acs <acsjakub@xxxxxxxxx> Cc: Changbin Du <changbin.du@xxxxxxxxx> Cc: Jakub Acs <acsjakub@xxxxxxxxx> Cc: <stable@xxxxxxxxxxxxxxx> # 5.15.x Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- include/linux/damon.h | 2 ++ mm/damon/core.c | 6 ++++++ 2 files changed, 8 insertions(+) --- a/include/linux/damon.h~mm-damon-core-make-damon_start-waits-until-kdamond_fn-starts +++ a/include/linux/damon.h @@ -559,6 +559,8 @@ struct damon_ctx { * update */ unsigned long next_ops_update_sis; + /* for waiting until the execution of the kdamond_fn is started */ + struct completion kdamond_started; /* public: */ struct task_struct *kdamond; --- a/mm/damon/core.c~mm-damon-core-make-damon_start-waits-until-kdamond_fn-starts +++ a/mm/damon/core.c @@ -445,6 +445,8 @@ struct damon_ctx *damon_new_ctx(void) if (!ctx) return NULL; + init_completion(&ctx->kdamond_started); + ctx->attrs.sample_interval = 5 * 1000; ctx->attrs.aggr_interval = 100 * 1000; ctx->attrs.ops_update_interval = 60 * 1000 * 1000; @@ -668,11 +670,14 @@ static int __damon_start(struct damon_ct mutex_lock(&ctx->kdamond_lock); if (!ctx->kdamond) { err = 0; + reinit_completion(&ctx->kdamond_started); ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d", nr_running_ctxs); if (IS_ERR(ctx->kdamond)) { err = PTR_ERR(ctx->kdamond); ctx->kdamond = NULL; + } else { + wait_for_completion(&ctx->kdamond_started); } } mutex_unlock(&ctx->kdamond_lock); @@ -1433,6 +1438,7 @@ static int kdamond_fn(void *data) pr_debug("kdamond (%d) starts\n", current->pid); + complete(&ctx->kdamond_started); kdamond_init_intervals_sis(ctx); if (ctx->ops.init) _ Patches currently in -mm which might be from sj@xxxxxxxxxx are mm-damon-core-make-damon_start-waits-until-kdamond_fn-starts.patch mm-damon-core-test-test-damon_split_region_ats-access-rate-copying.patch mm-damon-core-implement-goal-oriented-feedback-driven-quota-auto-tuning.patch mm-damon-core-implement-goal-oriented-feedback-driven-quota-auto-tuning-fix.patch mm-damon-sysfs-schemes-implement-files-for-scheme-quota-goals-setup.patch mm-damon-sysfs-schemes-commit-damos-quota-goals-user-input-to-damos.patch mm-damon-sysfs-schemes-implement-a-command-for-scheme-quota-goals-only-commit.patch mm-damon-core-test-add-a-unit-test-for-the-feedback-loop-algorithm.patch selftests-damon-test-quota-goals-directory.patch docs-mm-damon-design-document-damos-quota-auto-tuning.patch docs-abi-damon-document-damos-quota-goals.patch docs-admin-guide-mm-damon-usage-document-for-quota-goals.patch