Skip to contentSkip to conversation

mm/damon/core: remove call_control in inactive contexts

PATCH Superseded SeongJae Park 2 participants 10 messages Dec 28, 2025 – Jan 1

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

PATCH 1/1 mm/damon/core: remove call_control in inactive contexts

If damon_call() is executed against a DAMON context that is not running, the function returns error while keeping the damon_call_control object linked to the context's call_controls list. Let's suppose the object is deallocated after the damon_call(), and yet another damon_call() is executed against the same context. The function tries to add the new damon_call_control object to the call_controls list, which still has the pointer to the previous damon_call_control object, which is deallocated. As a result, use-after-free happens.

This can actually be triggered using the DAMON sysfs interface. It is not easily exploitable since it requires the sysfs write permission and making a definitely weird file writes, though. Please refer to the report for more details about the issue reproduction steps.

Fix the issue by making damon_call() to cleanup the damon_call_control object before returning the error.

Fixes 42b7491af14c ("mm/damon/core: introduce damon_call()")
Cc <[email protected]> # 6.14.x
---
 mm/damon/core.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
mm/damon/core.c +30 -1
index 2d3e8006db50..65482a0ce20b 100644
@@ -1442,6 +1442,35 @@ bool damon_is_running(struct damon_ctx *ctx)
 	return running;
 }
 
+/*
+ * damon_call_handle_inactive_ctx() - handle DAMON call request that added to
+ *				      an inactive context.
+ * @ctx:	The inactive DAMON context.
+ * @control:	Control variable of the call request.
+ *
+ * This function is called in a case that @control is added to @ctx but @ctx is
+ * not running (inactive).  See if @ctx handled @control or not, and cleanup
+ * @control if it was not handled.
+ *
+ * Returns 0 if @control was handled by @ctx, negative error code otherwise.
+ */
+static int damon_call_handle_inactive_ctx(
+		struct damon_ctx *ctx, struct damon_call_control *control)
+{
+	struct damon_call_control *c;
+
+	mutex_lock(&ctx->call_controls_lock);
+	list_for_each_entry(c, &ctx->call_controls, list) {
+		if (c == control) {
+			list_del(&control->list);
+			mutex_unlock(&ctx->call_controls_lock);
+			return -EINVAL;
+		}
+	}
+	mutex_unlock(&ctx->call_controls_lock);
+	return 0;
+}
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:	DAMON context to call the function for.
@@ -1472,7 +1501,7 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)
 	list_add_tail(&control->list, &ctx->call_controls);
 	mutex_unlock(&ctx->call_controls_lock);
 	if (!damon_is_running(ctx))
-		return -EINVAL;
+		return damon_call_handle_inactive_ctx(ctx, control);
 	if (control->repeat)
 		return 0;
 	wait_for_completion(&control->completion);

base-commit: a100272b3541cb00a5e29fdc16e428234ebfddd1

Signature · 1 line
2.47.3
author maintainer
#1 lore raw
Quoted 67 lines · On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

If damon_call() is executed against a DAMON context that is not running, the function returns error while keeping the damon_call_control object linked to the context's call_controls list. Let's suppose the object is deallocated after the damon_call(), and yet another damon_call() is executed against the same context. The function tries to add the new damon_call_control object to the call_controls list, which still has the pointer to the previous damon_call_control object, which is deallocated. As a result, use-after-free happens.

This can actually be triggered using the DAMON sysfs interface. It is not easily exploitable since it requires the sysfs write permission and making a definitely weird file writes, though. Please refer to the report for more details about the issue reproduction steps.

Fix the issue by making damon_call() to cleanup the damon_call_control object before returning the error.

Fixes 42b7491af14c ("mm/damon/core: introduce damon_call()")
Cc <[email protected]> # 6.14.x
---
 mm/damon/core.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
mm/damon/core.c +29 -0
index 2d3e8006db50..65482a0ce20b 100644
@@ -1442,6 +1442,35 @@ bool damon_is_running(struct damon_ctx *ctx)
 	return running;
 }
 
+/*
+ * damon_call_handle_inactive_ctx() - handle DAMON call request that added to
+ *				      an inactive context.
+ * @ctx:	The inactive DAMON context.
+ * @control:	Control variable of the call request.
+ *
+ * This function is called in a case that @control is added to @ctx but @ctx is
+ * not running (inactive).  See if @ctx handled @control or not, and cleanup
+ * @control if it was not handled.
+ *
+ * Returns 0 if @control was handled by @ctx, negative error code otherwise.
+ */
+static int damon_call_handle_inactive_ctx(
+		struct damon_ctx *ctx, struct damon_call_control *control)
+{
+	struct damon_call_control *c;
+
+	mutex_lock(&ctx->call_controls_lock);
+	list_for_each_entry(c, &ctx->call_controls, list) {
+		if (c == control) {
+			list_del(&control->list);
+			mutex_unlock(&ctx->call_controls_lock);
+			return -EINVAL;
+		}
+	}
+	mutex_unlock(&ctx->call_controls_lock);
+	return 0;
+}
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:	DAMON context to call the function for.

mm/damon/core.c int damon_call(struct damon_ctx *ctx, struct damon_call_control *control) · line 1507

Quoted 8 lines
mm/damon/core.c +1 -1
@@ -1472,7 +1501,7 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)
 	list_add_tail(&control->list, &ctx->call_controls);
 	mutex_unlock(&ctx->call_controls_lock);
 	if (!damon_is_running(ctx))
-		return -EINVAL;
+		return damon_call_handle_inactive_ctx(ctx, control);
 	if (control->repeat)
 		return 0;
 	wait_for_completion(&control->completion);

TL; DR: This patch introduces another UAF bug under a race condition. I will send a new version of the fix that solves the another issue. Andrew, could you please remove this from mm tree for now?

kdamond_fn() resets ->kdamond, which is read by damon_is_running(), and then make the final kdamond_call() for cancelling any remaining damon_call() requests. Hence, if the above damon_is_running() was invoked between the ->kdamond reset and the final kdamond_call() invocation, damon_call_handle_inactive_ctx() and the final kdamond_call() could concurrently run.

kdamond_call() safely get a pointer to a damon_call_control object in ctx->call_controls, and then access it without a lock. Only after that, it removes the object from the list while holding the lock. The intermediate lock-less access is safe because kdamond_call() is the only code that removes items from ctx->call_controls. But this patch makes it no more safe, because this patch is introducing another ctx->call_controls item removing code, namely damon_call_handle_inactive_ctx().

To see this in details, let's suppose kdamond_call() got the pointer, and released the call_controls_lock. After that, damon_call_handle_inactive_ctx() shows the object is still in the ctx->call_controls, and removes it from the list. The damon_call() caller further deallocates the object. Then, continued execution of kdamond_call() accesses the already deallocated object.

I will send a new version of this fix soon.

Thanks,
SJ

[...]

author maintainer
#2 lore raw
Quoted 16 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:
Quoted 14 lines · On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

If damon_call() is executed against a DAMON context that is not running, the function returns error while keeping the damon_call_control object linked to the context's call_controls list. Let's suppose the object is deallocated after the damon_call(), and yet another damon_call() is executed against the same context. The function tries to add the new damon_call_control object to the call_controls list, which still has the pointer to the previous damon_call_control object, which is deallocated. As a result, use-after-free happens.

This can actually be triggered using the DAMON sysfs interface. It is not easily exploitable since it requires the sysfs write permission and making a definitely weird file writes, though. Please refer to the report for more details about the issue reproduction steps.

Quoted 6 lines
Quoted 6 lines

Fix the issue by making damon_call() to cleanup the damon_call_control object before returning the error.

Fixes 42b7491af14c ("mm/damon/core: introduce damon_call()")

The above Fixes: tag is wrong. At the moment of the commit, only single damon_call() request was allowed. Hence only a pointer instead of the linked list was used. In the problematic scenario, repeated damon_call() would simply return -EBUSY.

The correct Fixes: should be,

Fixes 004ded6bee11 ("mm/damon: accept parallel damon_call() requests")
Cc <[email protected]> # 6.17.x
Quoted 75 lines
Quoted 47 lines
---
 mm/damon/core.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
mm/damon/core.c +29 -0
index 2d3e8006db50..65482a0ce20b 100644
@@ -1442,6 +1442,35 @@ bool damon_is_running(struct damon_ctx *ctx)
 	return running;
 }
 
+/*
+ * damon_call_handle_inactive_ctx() - handle DAMON call request that added to
+ *				      an inactive context.
+ * @ctx:	The inactive DAMON context.
+ * @control:	Control variable of the call request.
+ *
+ * This function is called in a case that @control is added to @ctx but @ctx is
+ * not running (inactive).  See if @ctx handled @control or not, and cleanup
+ * @control if it was not handled.
+ *
+ * Returns 0 if @control was handled by @ctx, negative error code otherwise.
+ */
+static int damon_call_handle_inactive_ctx(
+		struct damon_ctx *ctx, struct damon_call_control *control)
+{
+	struct damon_call_control *c;
+
+	mutex_lock(&ctx->call_controls_lock);
+	list_for_each_entry(c, &ctx->call_controls, list) {
+		if (c == control) {
+			list_del(&control->list);
+			mutex_unlock(&ctx->call_controls_lock);
+			return -EINVAL;
+		}
+	}
+	mutex_unlock(&ctx->call_controls_lock);
+	return 0;
+}
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:	DAMON context to call the function for.

mm/damon/core.c int damon_call(struct damon_ctx *ctx, struct damon_call_control *control) · line 1507

Quoted 8 lines
mm/damon/core.c +1 -1
@@ -1472,7 +1501,7 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)
 	list_add_tail(&control->list, &ctx->call_controls);
 	mutex_unlock(&ctx->call_controls_lock);
 	if (!damon_is_running(ctx))
-		return -EINVAL;
+		return damon_call_handle_inactive_ctx(ctx, control);
 	if (control->repeat)
 		return 0;
 	wait_for_completion(&control->completion);

TL; DR: This patch introduces another UAF bug under a race condition. I will send a new version of the fix that solves the another issue. Andrew, could you please remove this from mm tree for now?

kdamond_fn() resets ->kdamond, which is read by damon_is_running(), and then make the final kdamond_call() for cancelling any remaining damon_call() requests. Hence, if the above damon_is_running() was invoked between the ->kdamond reset and the final kdamond_call() invocation, damon_call_handle_inactive_ctx() and the final kdamond_call() could concurrently run.

kdamond_call() safely get a pointer to a damon_call_control object in ctx->call_controls, and then access it without a lock. Only after that, it removes the object from the list while holding the lock. The intermediate lock-less access is safe because kdamond_call() is the only code that removes items from ctx->call_controls. But this patch makes it no more safe, because this patch is introducing another ctx->call_controls item removing code, namely damon_call_handle_inactive_ctx().

Quoted 7 lines

To see this in details, let's suppose kdamond_call() got the pointer, and released the call_controls_lock. After that, damon_call_handle_inactive_ctx() shows the object is still in the ctx->call_controls, and removes it from the list. The damon_call() caller further deallocates the object. Then, continued execution of kdamond_call() accesses the already deallocated object.

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

Thanks,
SJ

author maintainer
#3 lore raw
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

Thanks,
SJ

[...]

author maintainer
#4 lore raw
Quoted 28 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

Quoted 4 lines

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

I just posted the v2 [1] with the third idea.

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

Thanks,
SJ

[...]

#5 lore raw
Quoted 31 lines · SeongJae Park
Quoted 28 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

Quoted 8 lines
Quoted 4 lines

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

I just posted the v2 [1] with the third idea.

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

I generally agree with what you've said so far. However, it's inefficient to continue executing damon_call_handle_inactive_ctx() while kdamond is "off". There's no need to add a new damon_call_handle_inactive_ctx() function. As shown below, it's better to call list_add only when kdamond is "on" (true), and then use the existing code to end with kdamond_call(ctx, true) when kdamond is "off."

+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
/**
* damon_call() - Invoke a given function on DAMON worker thread (kdamond).
* @ctx: DAMON context to call the function for.
@@ -1496,14 +1475,17 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)

        control->canceled = false;
        INIT_LIST_HEAD(&control->list);

- if (damon_is_running(ctx)) {
- mutex_lock(&ctx->call_controls_lock);
+ mutex_lock(&ctx->call_controls_lock);

+       if (ctx->kdamond) {
                list_add_tail(&control->list, &ctx->call_controls);
-               mutex_unlock(&ctx->call_controls_lock);
        } else {
-               /* return damon_call_handle_inactive_ctx(ctx, control); */
+               mutex_unlock(&ctx->call_controls_lock);
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
        }
+       mutex_unlock(&ctx->call_controls_lock);
+
        if (control->repeat)
                return 0;
        wait_for_completion(&control->completion);

Thanks,
JaeJoon

Quoted 6 lines

Thanks,
SJ

[...]

author maintainer
#6 lore raw
Quoted 37 lines · On Wed, 31 Dec 2025 14:27:54 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 36 lines · SeongJae Park
Quoted 32 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

Quoted 8 lines
Quoted 3 lines

I just posted the v2 [1] with the third idea.

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

I generally agree with what you've said so far. However, it's inefficient to continue executing damon_call_handle_inactive_ctx() while kdamond is "off". There's no need to add a new damon_call_handle_inactive_ctx() function.

As I mentioned before on other threads with you, we care not only efficiency but also maintainability of the code. The inefficiency you are saying about happens only in corner cases because damon_call() is not usually called while kdamond is off. So the gain of making this efficient is not that big.

Meanwhile, to avoid this, as I mentioned on the previous reply to the first and the second idea of the fix, we need to add locking rule, which makes the code bit difficult to maintain.

I therefore think the v2 is a good tradeoff.

Quoted 10 lines

As shown below, it's better to call list_add only when kdamond is "on" (true), and then use the existing code to end with kdamond_call(ctx, true) when kdamond is "off."

mm/damon/core.c +2 -0
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.

mm/damon/core.c int damon_call(struct damon_ctx *ctx, struct · line 1480

Quoted 8 lines
mm/damon/core.c +2 -2
@@ -1496,14 +1475,17 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
-       if (damon_is_running(ctx)) {
-               mutex_lock(&ctx->call_controls_lock);
+       mutex_lock(&ctx->call_controls_lock);
+       if (ctx->kdamond) {

This is wrong. You shouldn't access ctx->kdamond without holding ctx->kdamond_lock. Please read the comment about kdamond_lock field on damon.h file.

Quoted 13 lines
(quoted diff) +5 -2
                list_add_tail(&control->list, &ctx->call_controls);
-               mutex_unlock(&ctx->call_controls_lock);
        } else {
-               /* return damon_call_handle_inactive_ctx(ctx, control); */
+               mutex_unlock(&ctx->call_controls_lock);
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
        }
+       mutex_unlock(&ctx->call_controls_lock);
+
        if (control->repeat)
                return 0;
Quoted 1 line
        wait_for_completion(&control->completion);

Thanks,
SJ

[...]

#7 lore raw
Quoted 49 lines · SeongJae Park
Quoted 37 lines · On Wed, 31 Dec 2025 14:27:54 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 36 lines · SeongJae Park
Quoted 32 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

Quoted 8 lines
Quoted 3 lines

I just posted the v2 [1] with the third idea.

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

I generally agree with what you've said so far. However, it's inefficient to continue executing damon_call_handle_inactive_ctx() while kdamond is "off". There's no need to add a new damon_call_handle_inactive_ctx() function.

Quoted 4 lines

As I mentioned before on other threads with you, we care not only efficiency but also maintainability of the code. The inefficiency you are saying about happens only in corner cases because damon_call() is not usually called while kdamond is off. So the gain of making this efficient is not that big.

The overhead isn't that high, but I think it's better to keep things simple.
I think it's better to use list_add_tail() when kdamond is "on".

Quoted 4 lines

Meanwhile, to avoid this, as I mentioned on the previous reply to the first and the second idea of the fix, we need to add locking rule, which makes the code bit difficult to maintain.

I think it's better to solve it with the existing kdamond_call(ctx, cancel=true) rather than adding the damon_call_handle_inactive_ctx().

Quoted 17 lines

I therefore think the v2 is a good tradeoff.

Quoted 14 lines

As shown below, it's better to call list_add only when kdamond is "on" (true), and then use the existing code to end with kdamond_call(ctx, true) when kdamond is "off."

(quoted diff) +2 -0
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.
@@ -1496,14 +1475,17 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
Quoted 8 lines

Review int damon_call(struct damon_ctx *ctx, struct · line 1480

Quoted 4 lines
(quoted diff) +2 -2
-       if (damon_is_running(ctx)) {
-               mutex_lock(&ctx->call_controls_lock);
+       mutex_lock(&ctx->call_controls_lock);
+       if (ctx->kdamond) {

This is wrong. You shouldn't access ctx->kdamond without holding ctx->kdamond_lock. Please read the comment about kdamond_lock field on damon.h file.

That's right, I misjudged.
I've reorganized the code below.

Quoted 16 lines
Quoted 14 lines
(quoted diff) +5 -2
                list_add_tail(&control->list, &ctx->call_controls);
-               mutex_unlock(&ctx->call_controls_lock);
        } else {
-               /* return damon_call_handle_inactive_ctx(ctx, control); */
+               mutex_unlock(&ctx->call_controls_lock);
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
        }
+       mutex_unlock(&ctx->call_controls_lock);
+
        if (control->repeat)
                return 0;
Quoted 1 line
        wait_for_completion(&control->completion);

+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
/**
* damon_call() - Invoke a given function on DAMON worker thread (kdamond).
* @ctx: DAMON context to call the function for.
@@ -1457,11 +1459,15 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control)

        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
-       mutex_lock(&ctx->call_controls_lock);
-       list_add_tail(&control->list, &ctx->call_controls);
-       mutex_unlock(&ctx->call_controls_lock);
-       if (!damon_is_running(ctx))
+       if (damon_is_running(ctx)) {
+               mutex_lock(&ctx->call_controls_lock);
+               list_add_tail(&control->list, &ctx->call_controls);
+               mutex_unlock(&ctx->call_controls_lock);
+       } else {
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
+       }
        if (control->repeat)
                return 0;
        wait_for_completion(&control->completion);

Thanks,
JaeJoon

Quoted 5 lines

Thanks,
SJ

[...]

author maintainer
#8 lore raw
Quoted 60 lines · On Thu, 1 Jan 2026 09:55:56 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 49 lines · SeongJae Park
Quoted 37 lines · On Wed, 31 Dec 2025 14:27:54 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 36 lines · SeongJae Park
Quoted 32 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

Quoted 8 lines
Quoted 3 lines

I just posted the v2 [1] with the third idea.

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

I generally agree with what you've said so far. However, it's inefficient to continue executing damon_call_handle_inactive_ctx() while kdamond is "off". There's no need to add a new damon_call_handle_inactive_ctx() function.

Quoted 4 lines

As I mentioned before on other threads with you, we care not only efficiency but also maintainability of the code. The inefficiency you are saying about happens only in corner cases because damon_call() is not usually called while kdamond is off. So the gain of making this efficient is not that big.

The overhead isn't that high, but I think it's better to keep things simple.
I think it's better to use list_add_tail() when kdamond is "on".

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile, to avoid this, as I mentioned on the previous reply to the first and the second idea of the fix, we need to add locking rule, which makes the code bit difficult to maintain.

I think it's better to solve it with the existing kdamond_call(ctx, cancel=true) rather than adding the damon_call_handle_inactive_ctx().

On my previous reply, I was saying I think your suggested approach is not making it simple but only more complicated in the next paragraph. And I was again further explaining why I think so. More specifically, the added locking rule, that I previously explained with the third fix idea.

Your above reply is saying it is "better", without any reason. It even not addressing my concern that I explained more than once. As a result, I again fail at finding why you keep repeating the argument.

So, let me again ask you. Please explain.

Quoted 27 lines
Quoted 18 lines

I therefore think the v2 is a good tradeoff.

Quoted 14 lines

As shown below, it's better to call list_add only when kdamond is "on" (true), and then use the existing code to end with kdamond_call(ctx, true) when kdamond is "off."

(quoted diff) +2 -0
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.
@@ -1496,14 +1475,17 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
Quoted 8 lines

Review int damon_call(struct damon_ctx *ctx, struct · line 1480

Quoted 4 lines
(quoted diff) +2 -2
-       if (damon_is_running(ctx)) {
-               mutex_lock(&ctx->call_controls_lock);
+       mutex_lock(&ctx->call_controls_lock);
+       if (ctx->kdamond) {

This is wrong. You shouldn't access ctx->kdamond without holding ctx->kdamond_lock. Please read the comment about kdamond_lock field on damon.h file.

Quoted 2 lines

That's right, I misjudged.
I've reorganized the code below.

Your reply is not explaining how the reorganized code is solving it. Let me ask you again. Please explain.

Quoted 43 lines
Quoted 17 lines
Quoted 16 lines
(quoted diff) +5 -2
                list_add_tail(&control->list, &ctx->call_controls);
-               mutex_unlock(&ctx->call_controls_lock);
        } else {
-               /* return damon_call_handle_inactive_ctx(ctx, control); */
+               mutex_unlock(&ctx->call_controls_lock);
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
        }
+       mutex_unlock(&ctx->call_controls_lock);
+
        if (control->repeat)
                return 0;
        wait_for_completion(&control->completion);
(quoted diff) +10 -4
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.
@@ -1457,11 +1459,15 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
-       mutex_lock(&ctx->call_controls_lock);
-       list_add_tail(&control->list, &ctx->call_controls);
-       mutex_unlock(&ctx->call_controls_lock);
-       if (!damon_is_running(ctx))
+       if (damon_is_running(ctx)) {
+               mutex_lock(&ctx->call_controls_lock);
+               list_add_tail(&control->list, &ctx->call_controls);
+               mutex_unlock(&ctx->call_controls_lock);
+       } else {
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
+       }
        if (control->repeat)
                return 0;
Quoted 1 line
        wait_for_completion(&control->completion);

I think my previous concern that raised to your two previous approach [1,2] is again samely applied to the above diff. And now you are suggesting a similar approach here. Please explain and answer all questions before you move on, or explain why your new apprach is different from your previous one and how it addresses raised concerns..

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

Thanks,
SJ

[...]

#9 lore raw
Quoted 75 lines · SeongJae Park
Quoted 60 lines · On Thu, 1 Jan 2026 09:55:56 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 49 lines · SeongJae Park
Quoted 37 lines · On Wed, 31 Dec 2025 14:27:54 +0900 JaeJoon Jung <[email protected]> wrote:
Quoted 36 lines · SeongJae Park
Quoted 32 lines · On Mon, 29 Dec 2025 19:45:14 -0800 SeongJae Park <[email protected]> wrote:
Quoted 4 lines · On Mon, 29 Dec 2025 18:41:28 -0800 SeongJae Park <[email protected]> wrote:
Quoted 2 lines · On Mon, 29 Dec 2025 17:45:30 -0800 SeongJae Park <[email protected]> wrote:

On Sun, 28 Dec 2025 10:31:01 -0800 SeongJae Park <[email protected]> wrote:

[...]

Quoted 8 lines
Quoted 1 line

I will send a new version of this fix soon.

So far, I got two fixup ideas.

The first one is keeping the current code as is, and additionally modifying kdamond_call() to protect all call_control object accesses under ctx->call_controls_lock protection.

Quoted 5 lines

The second one is reverting this patch, and doing the DAMON running status check before adding the damon_call_control object, but releasing the kdamond_lock after the object insertion is done.

I'm in favor of the second one at the moment, as it seems more simple.

I don't really like both approaches because those implicitly add locking rules. If the first approach is taken, damon_call() callers should aware they should not register callback functions that can hold call_controls_lock. If the second approach is taken, we should avoid holding kdamond_lock while holding damon_call_control lock. The second implicit rule seems easier to keep to me, but I want to avoid that if possible.

The third idea I just got is, keeping this patch as is, and moving the final kdamond_call() invocation to be made _before_ the ctx->kdamond reset. That removes the race condition between the final kdamond_call() and damon_call_handle_inactive_ctx(), without introducing new locking rules.

Quoted 8 lines
Quoted 3 lines

I just posted the v2 [1] with the third idea.

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

I generally agree with what you've said so far. However, it's inefficient to continue executing damon_call_handle_inactive_ctx() while kdamond is "off". There's no need to add a new damon_call_handle_inactive_ctx() function.

Quoted 4 lines

As I mentioned before on other threads with you, we care not only efficiency but also maintainability of the code. The inefficiency you are saying about happens only in corner cases because damon_call() is not usually called while kdamond is off. So the gain of making this efficient is not that big.

The overhead isn't that high, but I think it's better to keep things simple.
I think it's better to use list_add_tail() when kdamond is "on".

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Meanwhile, to avoid this, as I mentioned on the previous reply to the first and the second idea of the fix, we need to add locking rule, which makes the code bit difficult to maintain.

I think it's better to solve it with the existing kdamond_call(ctx, cancel=true) rather than adding the damon_call_handle_inactive_ctx().

On my previous reply, I was saying I think your suggested approach is not making it simple but only more complicated in the next paragraph. And I was again further explaining why I think so. More specifically, the added locking rule, that I previously explained with the third fix idea.

Quoted 5 lines

Your above reply is saying it is "better", without any reason. It even not addressing my concern that I explained more than once. As a result, I again fail at finding why you keep repeating the argument.

So, let me again ask you. Please explain.

I've already answered your question.
Please understand my lack of expressiveness.

Quoted 28 lines
Quoted 28 lines
Quoted 27 lines

I therefore think the v2 is a good tradeoff.

Quoted 10 lines

As shown below, it's better to call list_add only when kdamond is "on" (true), and then use the existing code to end with kdamond_call(ctx, true) when kdamond is "off."

(quoted diff) +2 -0
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.

Review int damon_call(struct damon_ctx *ctx, struct · line 1480

Quoted 8 lines
(quoted diff) +2 -2
@@ -1496,14 +1475,17 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
-       if (damon_is_running(ctx)) {
-               mutex_lock(&ctx->call_controls_lock);
+       mutex_lock(&ctx->call_controls_lock);
+       if (ctx->kdamond) {

This is wrong. You shouldn't access ctx->kdamond without holding ctx->kdamond_lock. Please read the comment about kdamond_lock field on damon.h file.

Quoted 5 lines
Quoted 2 lines

That's right, I misjudged.
I've reorganized the code below.

Your reply is not explaining how the reorganized code is solving it. Let me ask you again. Please explain.

I will explain this in the code I have summarized below.

Quoted 43 lines
Quoted 43 lines
Quoted 18 lines
Quoted 17 lines
(quoted diff) +5 -2
                list_add_tail(&control->list, &ctx->call_controls);
-               mutex_unlock(&ctx->call_controls_lock);
        } else {
-               /* return damon_call_handle_inactive_ctx(ctx, control); */
+               mutex_unlock(&ctx->call_controls_lock);
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
        }
+       mutex_unlock(&ctx->call_controls_lock);
+
        if (control->repeat)
                return 0;
        wait_for_completion(&control->completion);
(quoted diff) +10 -4
+static void kdamond_call(struct damon_ctx *ctx, bool cancel);
+
 /**
  * damon_call() - Invoke a given function on DAMON worker thread (kdamond).
  * @ctx:       DAMON context to call the function for.
@@ -1457,11 +1459,15 @@ int damon_call(struct damon_ctx *ctx, struct
 damon_call_control *control)
        control->canceled = false;
        INIT_LIST_HEAD(&control->list);
 
-       mutex_lock(&ctx->call_controls_lock);
-       list_add_tail(&control->list, &ctx->call_controls);
-       mutex_unlock(&ctx->call_controls_lock);
-       if (!damon_is_running(ctx))
+       if (damon_is_running(ctx)) {
+               mutex_lock(&ctx->call_controls_lock);
+               list_add_tail(&control->list, &ctx->call_controls);
+               mutex_unlock(&ctx->call_controls_lock);
+       } else {
+               if (!list_empty_careful(&ctx->call_controls))
+                       kdamond_call(ctx, true);
                return -EINVAL;
+       }
        if (control->repeat)
Quoted 8 lines
Quoted 2 lines
(quoted diff) +0 -0
                return 0;
        wait_for_completion(&control->completion);

I think my previous concern that raised to your two previous approach [1,2] is again samely applied to the above diff. And now you are suggesting a similar approach here. Please explain and answer all questions before you move on, or explain why your new apprach is different from your previous one and how it addresses raised concerns..

The code above applies mutex and the locking method is the same. The difference is that control->list does list_add_tail() in the "on" state in damon_is_running(ctx).
Another difference is that when damon_is_running(ctx) is false("off"), the existing call_controls list is cleared with kdamond_call(ctx, cancel=true).

Regarding spin_lock,
I'm sure you're familiar with spin_lock, It can apply like this:

spin_lock(&ctx->call_controls_lock); list_add_tail(&control->list, &ctx->call_controls); spin_unlock(&ctx->call_controls_lock);

I have expressed my opinion sufficiently,
and I believe you will make a better judgment than I.

Thanks,
JaeJoon

Quoted 10 lines

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

Thanks,
SJ

[...]