Skip to contentSkip to conversation

samples/damon: handle damon_{start,stop}() failures

RFC PATCH In review SeongJae Park 2 participants 9 messages Jun 10 – Jun 13

Subsystems (3)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

v4 cover samples/damon: handle damon_{start,stop}() failures

All DAMON sample modules are not correctly handling failures from damon_start(). Among those, mtier also has an additional problem for handling of damon_stop() failures. wsse and prcl also have a problem in their damon_call() failure handling. As a result, memory leaks, next DAMON operation disruptions, and use-after-free can happen. Fix those.

Note that only the damon_start() failure caused issues can reliably be reproduced. Reproducing those issues require the admin permission, though.

Changes from RFC v3
- RFC v3: https://lore.kernel.org/[email protected]
- Add damon_Call() failure handling fixes for wsse and prcl. Changes from RFC v2
- RFC v2: https://lore.kernel.org/[email protected]
- Add damon_start() failure handling fixes for wsse and prcl. Changes from RFC v1
- RFC v1: https://lore.kernel.org/[email protected]
- Add damon_stop() failure handling fix to the series.

SeongJae Park (6):
samples/damon/wsse: handle damon_start() failure samples/damon/prcl: handle damon_start() failure samples/damon/mtier: handle damon_start() failure samples/damon/mtier: handle damon_stop() failure samples/damon/wsse: stop and free damon ctx when damon_call() fails samples/damon/prcl: stop and free damon ctx when damon_call() fails

 samples/damon/mtier.c | 14 ++++++++++++--
 samples/damon/prcl.c  | 11 +++++++++--
 samples/damon/wsse.c  | 11 +++++++++--
 3 files changed, 30 insertions(+), 6 deletions(-)

base-commit: 1fe919b2e7b6455d0b976d75dcbe44324361a83b

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

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

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/[email protected]

Fixes b757c6cfc696 ("samples/damon/wsse: start and stop DAMON as the user requests")
Cc <[email protected]> # 6.14.x
---
 samples/damon/wsse.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
samples/damon/wsse.c +3 -1
index 799ad44439434..bbd9392ab5b36 100644
@@ -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);
 }
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

RFC PATCH v4 2/6 samples/damon/prcl: handle damon_start() failure

In reply to SeongJae Park

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/[email protected]

Fixes 2aca254620a8 ("samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation")
Cc <[email protected]> # 6.14.x
---
 samples/damon/prcl.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
samples/damon/prcl.c +3 -1
index b7c50f2656ce7..0db2598946911 100644
@@ -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);
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

RFC PATCH v4 3/6 samples/damon/mtier: handle damon_start() failure

In reply to SeongJae Park

damon_sample_mtier_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 could leak the memory for DAMON context. Also, if damon_start() fails for only the second context, the first context will indefinitely run, and avoid starting other DAMON contexts since it is running in the exclusive mode. Stop possibly started DAMON context and free the contexts 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 Y | sudo tee /sys/module/damon_sample_mtier/parameters/enabled
    $ sudo cat /proc/allocinfo | grep damon_new_ctx

Because the first command is running another DAMON instance, the second command fails the damon_start() call because the new DAMON instance cannot exclusively run. And without this fix, by repeating the second and the third 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/[email protected]

Fixes 82a08bde3cf7 ("samples/damon: implement a DAMON module for memory tiering")
Cc <[email protected]> # 6.16.x
---
 samples/damon/mtier.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
samples/damon/mtier.c +10 -1
index eb1143de8df17..66b591f2180fa 100644
@@ -174,6 +174,7 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
 static int damon_sample_mtier_start(void)
 {
 	struct damon_ctx *ctx;
+	int err;
 
 	ctx = damon_sample_mtier_build_ctx(true);
 	if (!ctx)
@@ -185,7 +186,15 @@ static int damon_sample_mtier_start(void)
 		return -ENOMEM;
 	}
 	ctxs[1] = ctx;
-	return damon_start(ctxs, 2, true);
+	err = damon_start(ctxs, 2, true);
+	if (!err)
+		return 0;
+
+	if (damon_is_running(ctxs[0]))
+		damon_stop(ctxs, 1);
+	damon_destroy_ctx(ctxs[0]);
+	damon_destroy_ctx(ctxs[1]);
+	return err;
 }
 
 static void damon_sample_mtier_stop(void)
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

RFC PATCH v4 4/6 samples/damon/mtier: handle damon_stop() failure

In reply to SeongJae Park

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.

[1] https://lore.kernel.org/[email protected]

Fixes 82a08bde3cf7 ("samples/damon: implement a DAMON module for memory tiering")
Cc <[email protected]> # 6.16.x
---
 samples/damon/mtier.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
samples/damon/mtier.c +2 -1
index 66b591f2180fa..faaaaa12e6206 100644
@@ -199,7 +199,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]);
 }
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

RFC PATCH v4 5/6 samples/damon/wsse: stop and free damon ctx when damon_call() fails

In reply to SeongJae Park

damon_sample_wsse_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_wsse_start() caller assumes it would clean up the object, though. When the user requests to start DAMON again, damon_sample_wsse_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.

[1] https://lore.kernel.org/[email protected]

Fixes cc9c1b8c205b ("samples/damon/wsse: use damon_call() repeat mode instead of damon_callback")
Cc <[email protected]> # 6.17.x
---
 samples/damon/wsse.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
samples/damon/wsse.c +6 -1
index bbd9392ab5b36..ff5e8a890f448 100644
@@ -92,7 +92,12 @@ static int damon_sample_wsse_start(void)
 		return err;
 	}
 	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_wsse_stop(void)
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

RFC PATCH v4 6/6 samples/damon/prcl: stop and free damon ctx when damon_call() fails

In reply to SeongJae Park

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.

[1] https://lore.kernel.org/[email protected]

Fixes a6c33f1054e3 ("samples/damon/prcl: use damon_call() repeat mode instead of damon_callback")
Cc <[email protected]> # 6.17.x
---
 samples/damon/prcl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
samples/damon/prcl.c +6 -1
index 0db2598946911..edeae145c4a8a 100644
@@ -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)
Signature · 1 line
2.47.3
author maintainer
#7 lore raw
In reply to SeongJae Park
Quoted 9 lines · On Wed, 10 Jun 2026 06:55:38 -0700 SeongJae Park <[email protected]> wrote:

All DAMON sample modules are not correctly handling failures from damon_start(). Among those, mtier also has an additional problem for handling of damon_stop() failures. wsse and prcl also have a problem in their damon_call() failure handling. As a result, memory leaks, next DAMON operation disruptions, and use-after-free can happen. Fix those.

Note that only the damon_start() failure caused issues can reliably be reproduced. Reproducing those issues require the admin permission, though.

Finally this series passed the Sashiko review [1]. I initially aimed to drop RFC of this patch series as soon as passing Sashiko review. But, the size of this series has been 3x compared to the initial one, because Sashiko was finding more pre-existing issues for every review round, and I added fixes for those in this series.

Due to the size and short remaining time until the next merge window, I'm now hesitatng.i The last three fixes might not deserve to rush, because they are relatively difficult to intentionally be reproduced. The first three fixes might be better to be merged as soon as possible, since the issues can reliably and repetitively triggered. But, still those are sample modules, and the bug was introduced with 6.14, which was released in Mar 2025.

So I think it is better to give Andrew and Linus more time for the merge window. I will hold dropping RFC tag of this series until it seems Andrew finished the next merge window works. Let me know if any of you have different opinions, though!

[1] https://sashiko.dev/#/patchset/20260610135546.64943-1-sj%40kernel.org

Thanks,
SJ

[...]

Reviewed-by on the v4 series
#8 lore raw
In reply to SeongJae Park
Quoted 28 lines · SeongJae Park

All DAMON sample modules are not correctly handling failures from damon_start(). Among those, mtier also has an additional problem for handling of damon_stop() failures. wsse and prcl also have a problem in their damon_call() failure handling. As a result, memory leaks, next DAMON operation disruptions, and use-after-free can happen. Fix those.

Note that only the damon_start() failure caused issues can reliably be reproduced. Reproducing those issues require the admin permission, though.

Changes from RFC v3
- RFC v3: https://lore.kernel.org/[email protected]
- Add damon_Call() failure handling fixes for wsse and prcl. Changes from RFC v2
- RFC v2: https://lore.kernel.org/[email protected]
- Add damon_start() failure handling fixes for wsse and prcl. Changes from RFC v1
- RFC v1: https://lore.kernel.org/[email protected]
- Add damon_stop() failure handling fix to the series.

SeongJae Park (6):
samples/damon/wsse: handle damon_start() failure samples/damon/prcl: handle damon_start() failure samples/damon/mtier: handle damon_start() failure samples/damon/mtier: handle damon_stop() failure samples/damon/wsse: stop and free damon ctx when damon_call() fails samples/damon/prcl: stop and free damon ctx when damon_call() fails

Quoted 4 lines
 samples/damon/mtier.c | 14 ++++++++++++--
 samples/damon/prcl.c  | 11 +++++++++--
 samples/damon/wsse.c  | 11 +++++++++--
 3 files changed, 30 insertions(+), 6 deletions(-)

Looks good,

Thanks,
Zenghui