[PATCH 2/6] samples/damon/prcl: handle damon_start() failure
From: SJ Park
Date: Sun Jun 28 2026 - 17:55:50 EST
damon_sample_prcl_start() callers assume it will clean up resources when
it fails. And the function does the cleanup for context buildup
failures. However, it is not doing the cleanup for damon_start()
failure. As a result, when damon_start() fails, it leaks the memory for
DAMON context. Free the context in case of the failure to fix the
issues.
Note that the issue can reliably be reproduced because the module calls
damon_start() in the exclusive mode. For example,
$ sudo damo start
$ echo $$ | sudo tee /sys/module/damon_sample_prcl/parameters/target_pid
$ echo Y | sudo tee /sys/module/damon_sample_prcl/parameters/enabled
$ sudo cat /proc/allocinfo | grep damon_new_ctx
Because the first command is running another DAMON instance, the third
command fails the damon_start() call because the new DAMON instance
cannot exclusively run. And without this fix, by repeating the third
and the fourth commands above, we can show the memory consumption is
only increasing due to the leaks. It requires the sudo permission
though.
The issue was discovered [1] by Sashiko.
[1] https://lore.kernel.org/20260609145814.70163-1-sj@xxxxxxxxxx
Fixes: 2aca254620a8 ("samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation")
Cc: <stable@xxxxxxxxxxxxxxx> # 6.14.x
Reviewed-by: Zenghui Yu <zenghui.yu@xxxxxxxxx>
Signed-off-by: SJ Park <sj@xxxxxxxxxx>
---
samples/damon/prcl.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/samples/damon/prcl.c b/samples/damon/prcl.c
index b7c50f2656ce7..0db2598946911 100644
--- a/samples/damon/prcl.c
+++ b/samples/damon/prcl.c
@@ -106,8 +106,10 @@ static int damon_sample_prcl_start(void)
damon_set_schemes(ctx, &scheme, 1);
err = damon_start(&ctx, 1, true);
- if (err)
+ if (err) {
+ damon_destroy_ctx(ctx);
return err;
+ }
repeat_call_control.data = ctx;
return damon_call(ctx, &repeat_call_control);
--
2.47.3