summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSJ Park <sj@kernel.org>2026-06-28 14:54:43 -0700
committerAndrew Morton <akpm@linux-foundation.org>2026-07-05 16:23:13 -0700
commite457dc543bb20d2871a731cdbb062ddabf534d52 (patch)
tree29c49d859adba7671838b94d652669cb174e9295
parent6f985b238325d04acb6a4c6ae55e61e44aa42745 (diff)
downloadlinux-next-e457dc543bb20d2871a731cdbb062ddabf534d52.tar.gz
linux-next-e457dc543bb20d2871a731cdbb062ddabf534d52.zip
samples/damon/mtier: handle damon_stop() failure
damon_sample_mtier_stop() assumes its damon_stop() call will always successfully stops the two DAMON contexts. Hence it deallocates the two DAMON contexts after the damon_stop() call. However, if a given context is already stopped, damon_stop() fails and returns an error while letting the DAMON contexts that have not yet stopped keep running. This kind of unexpected early DAMON context stops could happen due to memory allocation failures in kdamond_fn(). Because damon_sample_mtier_stop() just deallocates all DAMON contexts with damon_target and damon_region objects that are linked to the contexts, the execution of the unstopped DAMON context (kdamond) ends up using the memory that freed (use-after-free). Fix the issue by separating the damon_stop() to be invoked per context. Note that DAMON_SYSFS also allows multiple DAMON contexts execution. But, it calls damon_stop() for each context one by one. Hence this issue is only in mtier. For the long term, it would be better to refactor damon_stop() to always ensure stopping all contexts regardless of the failures in the middle. Make this fix in the current way, though, to keep it simple and easy to backport. I will do the refactoring later. The issue was discovered [1] by Sashiko. Link: https://lore.kernel.org/20260628215447.96166-5-sj@kernel.org Link: https://lore.kernel.org/20260609014219.3013-1-sj@kernel.org [1] Fixes: 82a08bde3cf7 ("samples/damon: implement a DAMON module for memory tiering") Signed-off-by: SJ Park <sj@kernel.org> Reviewed-by: Zenghui Yu <zenghui.yu@linux.dev> Cc: <stable@vger.kernel.org> # 6.16.x Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--samples/damon/mtier.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/samples/damon/mtier.c b/samples/damon/mtier.c
index 564212e054de..e567f4edd80e 100644
--- a/samples/damon/mtier.c
+++ b/samples/damon/mtier.c
@@ -202,7 +202,8 @@ static int damon_sample_mtier_start(void)
static void damon_sample_mtier_stop(void)
{
- damon_stop(ctxs, 2);
+ damon_stop(ctxs, 1);
+ damon_stop(&ctxs[1], 1);
damon_destroy_ctx(ctxs[0]);
damon_destroy_ctx(ctxs[1]);
}