[RFC PATCH v4 1/6] samples/damon/wsse: handle damon_start() failure

From: SeongJae Park

Date: Wed Jun 10 2026 - 10:06:22 EST


damon_sample_wsse_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_wsse/parameters/target_pid
$ echo Y | sudo tee /sys/module/damon_sample_wsse/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: b757c6cfc696 ("samples/damon/wsse: start and stop DAMON as the user requests")
Cc: <stable@xxxxxxxxxxxxxxx> # 6.14.x
Signed-off-by: SeongJae Park <sj@xxxxxxxxxx>
---
samples/damon/wsse.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/samples/damon/wsse.c b/samples/damon/wsse.c
index 799ad44439434..bbd9392ab5b36 100644
--- a/samples/damon/wsse.c
+++ b/samples/damon/wsse.c
@@ -87,8 +87,10 @@ static int damon_sample_wsse_start(void)
target->pid = target_pidp;

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