summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSJ Park <sj@kernel.org>2026-06-28 14:54:45 -0700
committerAndrew Morton <akpm@linux-foundation.org>2026-07-28 21:12:06 -0700
commita73fa45d3f0f42c446ae55c5799e3d5ef044cd5d (patch)
treee0b6a0f53fab8e71c4ba292de7f2d8c947017d26
parenta2c6fa6c23ad87c61e1379b05dc05cf5fed4bf8d (diff)
downloadlinux-next-a73fa45d3f0f42c446ae55c5799e3d5ef044cd5d.tar.gz
linux-next-a73fa45d3f0f42c446ae55c5799e3d5ef044cd5d.zip
samples/damon/prcl: stop and free damon ctx when damon_call() fails
damon_sample_prcl_start() calls damon_call() right after damon_start() is succeeded. The kdamond that has started by the damon_start() could be terminated by itself before or in the middle of the damon_call() execution. There could be multiple reasons for such a stop including monitoring target process termination and kdamond_fn() internal memory allocation failures. In the case, damon_call() will fail and return an error without cleaning up the DAMON context object. The damon_sample_prcl_start() caller assumes it would clean up the object, though. When the user requests to start DAMON again, damon_sample_prcl_start() is called again, allocates a new DAMON context object and overwrites the pointer for the previous object. As a result, the previous context object is leaked. Safely stop the kdamond and deallocate the context object when the failure is returned. Note that the kdamond should be stopped first, because damon_call() failure means not complete termination of the kdamond but only the fact that the termination process has started. The user impact shouldn't be that significant because the race is not easy to happen, and only up to one DAMON context object can be leaked per race. The issue was discovered [1] by Sashiko. Link: https://lore.kernel.org/20260628215447.96166-7-sj@kernel.org Link: https://lore.kernel.org/20260610035214.4850-1-sj@kernel.org [1] Fixes: a6c33f1054e3 ("samples/damon/prcl: use damon_call() repeat mode instead of damon_callback") Signed-off-by: SJ Park <sj@kernel.org> Reviewed-by: Zenghui Yu <zenghui.yu@linux.dev> Cc: <stable@vger.kernel.org> # 6.17.x Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
-rw-r--r--samples/damon/prcl.c7
1 files changed, 6 insertions, 1 deletions
diff --git a/samples/damon/prcl.c b/samples/damon/prcl.c
index 0db259894691..edeae145c4a8 100644
--- a/samples/damon/prcl.c
+++ b/samples/damon/prcl.c
@@ -112,7 +112,12 @@ static int damon_sample_prcl_start(void)
}
repeat_call_control.data = ctx;
- return damon_call(ctx, &repeat_call_control);
+ err = damon_call(ctx, &repeat_call_control);
+ if (err) {
+ damon_stop(&ctx, 1);
+ damon_destroy_ctx(ctx);
+ }
+ return err;
}
static void damon_sample_prcl_stop(void)