Skip to contentSkip to conversation

mm/damon: add synchronous commit for commit_inputs

RFC Superseded Liew Rui Yan 2 participants 10 messages Mar 25 – Mar 27

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

RFC v5 1/1 mm/damon: add synchronous commit for commit_inputs

Problem
=======
Writing invalid parameters to sysfs followed by 'commit_inputs=Y' fails silently (no error returned to shell), because the validation happens asynchronously in the kdamond.

Solution
========
To fix this, the commit_inputs_store() callback now uses damon_call() to synchronously commit parameters in the kdamond thread's safe context. This ensures that validation errors are returned immediately to userspace, following the pattern used by DAMON_SYSFS.

Changes
=======
1. Added commit_inputs_store() and commit_inputs_fn() to commit synchronously.
2. Removed handle_commit_inputs().

This change is motivated from another discussion [1].

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

---
Changes from RFC-v4:
- Rename the 'yes' variable in commit_inputs_store() to the more understandable 'commit_inputs_request'.
- Return -EBUSY instead of -EINVAL when 'commit_inputs' is triggered
while kdamond is not running.
- Link to RFC-v4: https://lore.kernel.org/[email protected]

Changes from RFC-v3:
- Added checks for 'ctx' and 'damon_is_running()' to prevent NULL
pointer dereference during early boot. (Found by Sashiko.dev)
- Removed handle_commit_inputs() and its associated polling logic as
they have become dead code after moving to the synchronous damon_call() approach.
- Ensure the 'commit_inputs' is properly updated.
Link to RFC-v3: https://lore.kernel.org/[email protected]

Changes from RFC-v2:
- Removed damon_validate_attrs(), now using damon_commit_ctx() for synchronous validation in the kdamond context.
- Following DAMON_SYSFS pattern for synchronous commit via damon_call().
- Link to RFC-v2: https://lore.kernel.org/[email protected]

Changes from RFC-v1:
- Remove question from commit message area.
- Added synchronous validation for DAMON_RECLAIM.
- Rename damon_valid_attrs() -> damon_validate_attrs().
- Exported a new function damon_validate_attrs() and declared it in damon.h.
- Link to RFC-v1: https://lore.kernel.org/[email protected]

 mm/damon/lru_sort.c | 54 +++++++++++++++++++++++++++++++++++++++------
 mm/damon/reclaim.c  | 54 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 94 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +47 -7
index 554559d72976..a2410f648b51 100644
@@ -39,7 +39,6 @@ static bool enabled __read_mostly;
  * the re-reading, DAMON_LRU_SORT will be disabled.
  */
 static bool commit_inputs __read_mostly;
-module_param(commit_inputs, bool, 0600);
 
 /*
  * Desired active to [in]active memory ratio in bp (1/10,000).
@@ -349,18 +348,59 @@ static int damon_lru_sort_apply_parameters(void)
 	return err;
 }
 
-static int damon_lru_sort_handle_commit_inputs(void)
+static int damon_lru_sort_commit_inputs_fn(void *arg)
 {
+	return damon_lru_sort_apply_parameters();
+}
+
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;
+
+		return err;
+	}
+
+	return control.return_code;
 }
 
+static const struct kernel_param_ops commit_inputs_param_ops = {
+	.set = damon_lru_sort_commit_inputs_store,
+	.get = param_get_bool,
+};
+
+module_param_cb(commit_inputs, &commit_inputs_param_ops, &commit_inputs, 0600);
+
 static int damon_lru_sort_damon_call_fn(void *arg)
 {
 	struct damon_ctx *c = arg;
@@ -374,7 +414,7 @@ static int damon_lru_sort_damon_call_fn(void *arg)
 			damon_lru_sort_cold_stat = s->stat;
 	}
 
-	return damon_lru_sort_handle_commit_inputs();
+	return 0;
 }
 
 static struct damon_call_control call_control = {
mm/damon/reclaim.c +47 -7

79 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer
#1 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Forwarding Sashiko review for doing discussions via mails.

# review url: https://sashiko.dev/#/patchset/[email protected]
# start of sashiko.dev inline review
commit 60ccea4154b0c58741fae2323454a5a9496b67fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

Writing invalid parameters to sysfs followed by commit_inputs=Y previously failed silently. This patch uses damon_call() to synchronously commit parameters in the kdamond thread's safe context, returning validation errors immediately to userspace.

Quoted 4 lines
mm/damon/lru_sort.c +0 -0
index 554559d72976..a2410f648b51 100644

[ ... ]

Quoted 20 lines
mm/damon/lru_sort.c +14 -2
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 381

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Can this sequence lead to a system-wide deadlock on kernel_param_lock?

Because damon_lru_sort_commit_inputs_store() is a sysfs .set callback, it executes with the global kernel_param_lock held.

If kdamond_fn terminates (e.g., due to an invalid parameter like addr_unit=3 causing a non-power-of-2 min_region_sz), it processes its exit sequence by calling kdamond_call(ctx, true) to flush pending requests. After flushing, it eventually sets ctx->kdamond = NULL at the very end of the thread.

If a user writes to commit_inputs exactly in the window after kdamond_call() finishes but before ctx->kdamond = NULL is set:

Thread 1 (kdamond_fn):

    kdamond_call(ctx, true);

Thread 2 (sysfs write):

    damon_call(ctx, &control);
        if (damon_is_running(ctx))
            wait_for_completion(&control.completion);

Thread 1 (kdamond_fn):

    ctx->kdamond = NULL;

Thread 2 will sleep indefinitely in wait_for_completion(). Because Thread 2 holds the global kernel_param_lock, this permanently freezes all module parameter sysfs interfaces system-wide.

Quoted 19 lines
mm/damon/lru_sort.c +13 -0
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;
+
+		return err;
+	}
+
+	return control.return_code;
 }
mm/damon/reclaim.c +0 -0
index 86da14778658..4cbd67e08d56 100644

[ ... ]

mm/damon/reclaim.c

Quoted 8 lines
mm/damon/reclaim.c +8 -0
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Does this code share the same kernel_param_lock deadlock potential as damon_lru_sort_commit_inputs_store() described above?

# end of sashiko.dev inline review
# review url: https://sashiko.dev/#/patchset/[email protected]
#
# hkml [1] generated a draft of this mail.  It can be regenerated
# using below command:
#
#     hkml patch sashiko_dev --for_forwarding \
#             [email protected]
#
# [1] https://github.com/sjp38/hackermail

Sent using hkml (https://github.com/sjp38/hackermail)

#2 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Hi SeongJae,

Quoted 14 lines

Forwarding Sashiko review for doing discussions via mails.

# review url: https://sashiko.dev/#/patchset/[email protected]
# start of sashiko.dev inline review
commit 60ccea4154b0c58741fae2323454a5a9496b67fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

Writing invalid parameters to sysfs followed by commit_inputs=Y previously failed silently. This patch uses damon_call() to synchronously commit parameters in the kdamond thread's safe context, returning validation errors immediately to userspace.

Quoted 6 lines

mm/damon/lru_sort.c

Quoted 4 lines
mm/damon/lru_sort.c +0 -0
index 554559d72976..a2410f648b51 100644

Thank you for forwarding the review from Sashiko.dev!

Quoted 52 lines
Quoted 20 lines
mm/damon/lru_sort.c +14 -2
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 381

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Can this sequence lead to a system-wide deadlock on kernel_param_lock?

Because damon_lru_sort_commit_inputs_store() is a sysfs .set callback, it executes with the global kernel_param_lock held.

If kdamond_fn terminates (e.g., due to an invalid parameter like addr_unit=3 causing a non-power-of-2 min_region_sz), it processes its exit sequence by calling kdamond_call(ctx, true) to flush pending requests. After flushing, it eventually sets ctx->kdamond = NULL at the very end of the thread.

If a user writes to commit_inputs exactly in the window after kdamond_call() finishes but before ctx->kdamond = NULL is set:

Thread 1 (kdamond_fn):

    kdamond_call(ctx, true);

Thread 2 (sysfs write):

    damon_call(ctx, &control);
        if (damon_is_running(ctx))
            wait_for_completion(&control.completion);
Quoted 6 lines

Thread 1 (kdamond_fn):

    ctx->kdamond = NULL;

Thread 2 will sleep indefinitely in wait_for_completion(). Because Thread 2 holds the global kernel_param_lock, this permanently freezes all module parameter sysfs interfaces system-wide.

I have verified this behavior with the following test case:

    # cd /sys/module/damon_lru_sort/parameters/
    # echo Y > enabled
    # ps aux | grep "[k]damond"
    root   70  0.0  0.0  0  0 ?  I  12:16  0:00 [kdamond.0]
    # echo 3 > addr_unit
    # echo Y > commit_inputs
    bash: echo: write error: Invalid argument
    # ps aux | grep "[k]damond"

... kdamond has exited unexpectedly

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

Quoted 22 lines
Quoted 19 lines
mm/damon/lru_sort.c +13 -0
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;
+
+		return err;
+	}
+
+	return control.return_code;
 }
mm/damon/reclaim.c +0 -0
index 86da14778658..4cbd67e08d56 100644

[ ... ]

Quoted 11 lines

mm/damon/reclaim.c

Quoted 8 lines
mm/damon/reclaim.c +8 -0
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Does this code share the same kernel_param_lock deadlock potential as damon_lru_sort_commit_inputs_store() described above?

As we discussed in the RFC-v4 thread [1], this is a false positive. Since 'enabled=N' and 'commit_inputs=Y' are both serialized by the global 'kernel_param_lock', and kthreads cannot be forcibly terminated by userspace signals, the completion signal in damon_call() will always be reached in a controlled manner.

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

Best regards,
Rui Yan

maintainer
#3 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Hi Liew,

Quoted 16 lines · On Wed, 25 Mar 2026 15:17:09 +0800 Liew Rui Yan <[email protected]>…

Hi SeongJae,

Quoted 14 lines

Forwarding Sashiko review for doing discussions via mails.

# review url: https://sashiko.dev/#/patchset/[email protected]
# start of sashiko.dev inline review
commit 60ccea4154b0c58741fae2323454a5a9496b67fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

Writing invalid parameters to sysfs followed by commit_inputs=Y previously failed silently. This patch uses damon_call() to synchronously commit parameters in the kdamond thread's safe context, returning validation errors immediately to userspace.

Quoted 8 lines
Quoted 6 lines

mm/damon/lru_sort.c

Quoted 4 lines
mm/damon/lru_sort.c +0 -0
index 554559d72976..a2410f648b51 100644

Thank you for forwarding the review from Sashiko.dev!

Thank you for reviewing the review.

Quoted 60 lines
Quoted 53 lines
Quoted 21 lines
mm/damon/lru_sort.c +14 -2
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 381

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Can this sequence lead to a system-wide deadlock on kernel_param_lock?

Because damon_lru_sort_commit_inputs_store() is a sysfs .set callback, it executes with the global kernel_param_lock held.

If kdamond_fn terminates (e.g., due to an invalid parameter like addr_unit=3 causing a non-power-of-2 min_region_sz), it processes its exit sequence by calling kdamond_call(ctx, true) to flush pending requests. After flushing, it eventually sets ctx->kdamond = NULL at the very end of the thread.

If a user writes to commit_inputs exactly in the window after kdamond_call() finishes but before ctx->kdamond = NULL is set:

Thread 1 (kdamond_fn):

    kdamond_call(ctx, true);

Thread 2 (sysfs write):

    damon_call(ctx, &control);
        if (damon_is_running(ctx))
            wait_for_completion(&control.completion);
Quoted 6 lines

Thread 1 (kdamond_fn):

    ctx->kdamond = NULL;

Thread 2 will sleep indefinitely in wait_for_completion(). Because Thread 2 holds the global kernel_param_lock, this permanently freezes all module parameter sysfs interfaces system-wide.

Quoted 6 lines

I have verified this behavior with the following test case:

    # cd /sys/module/damon_lru_sort/parameters/
    # echo Y > enabled
    # ps aux | grep "[k]damond"
    root   70  0.0  0.0  0  0 ?  I  12:16  0:00 [kdamond.0]

FYI, you can read kdamond_pid parameter.

Quoted 9 lines
    # echo 3 > addr_unit
    # echo Y > commit_inputs
    bash: echo: write error: Invalid argument
    # ps aux | grep "[k]damond"

... kdamond has exited unexpectedly

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

That's because kdamond_fn() has recently changed to exit the loop if damon_commit_ctx() fails in the loop for any reason. Refer to commit 26f775a054c3 ("mm/damon/core: avoid use of half-online-committed context") for more details.

Adding the validation in addr_unit_store() would work, but it could be a kind of whack-a-mole game. I'd prefer doing the validation of all inputs in one place, just before the last damon_commit_ctx() call. DAMON_SYSFS is doing so, by making a test context and try committing user input to the test context before doing that to the running context. How about doing so here, too?

Quoted 35 lines
Quoted 23 lines
Quoted 20 lines
mm/damon/lru_sort.c +13 -0
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;
+
+		return err;
+	}
+
+	return control.return_code;
 }
mm/damon/reclaim.c +0 -0
index 86da14778658..4cbd67e08d56 100644

[ ... ]

Quoted 11 lines

mm/damon/reclaim.c

Quoted 8 lines
mm/damon/reclaim.c +8 -0
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Does this code share the same kernel_param_lock deadlock potential as damon_lru_sort_commit_inputs_store() described above?

Quoted 5 lines

As we discussed in the RFC-v4 thread [1], this is a false positive. Since 'enabled=N' and 'commit_inputs=Y' are both serialized by the global 'kernel_param_lock', and kthreads cannot be forcibly terminated by userspace signals, the completion signal in damon_call() will always be reached in a controlled manner.

TL; DR: I think the deadlock can happen. But that's a separate issue.

Let's suppose the user writes 'Y' to commit_inputs, and damon_commit_ctx() fails due to an allocation failure. The write to commit_inputs will complete, and kdmond_fn() will start its exit sequence.

The user can write 'Y' to commit_inputs again, when kdamond_fn() finished the last kdamond_call() with 'cancel=True', but before it unset the ->kdamond field.

Then, the deadlock that sashiko pointed out could happen.

The deadlock is due to the incomplete design of damon_call() cleanup. And I think the same deadlock is already available with DAMON_SYSFS. I will work on fixing this.

So the issue already exists. But let's ensure the fix is merged before this patch, since this patch adds another exploitable path that can consequence in whole param_lock deadlock.

Quoted 2 lines

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

Thanks,
SJ

[...]

#4 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Hi SeongJae,

Quoted 115 lines · SeongJae Park

Hi Liew,

Quoted 16 lines · On Wed, 25 Mar 2026 15:17:09 +0800 Liew Rui Yan <[email protected]>…

Hi SeongJae,

Quoted 14 lines

Forwarding Sashiko review for doing discussions via mails.

# review url: https://sashiko.dev/#/patchset/[email protected]
# start of sashiko.dev inline review
commit 60ccea4154b0c58741fae2323454a5a9496b67fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

Writing invalid parameters to sysfs followed by commit_inputs=Y previously failed silently. This patch uses damon_call() to synchronously commit parameters in the kdamond thread's safe context, returning validation errors immediately to userspace.

Quoted 8 lines
Quoted 6 lines

mm/damon/lru_sort.c

Quoted 4 lines
mm/damon/lru_sort.c +0 -0
index 554559d72976..a2410f648b51 100644

Thank you for forwarding the review from Sashiko.dev!

Thank you for reviewing the review.

Quoted 60 lines
Quoted 53 lines
Quoted 21 lines
mm/damon/lru_sort.c +14 -2
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 381

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Can this sequence lead to a system-wide deadlock on kernel_param_lock?

Because damon_lru_sort_commit_inputs_store() is a sysfs .set callback, it executes with the global kernel_param_lock held.

If kdamond_fn terminates (e.g., due to an invalid parameter like addr_unit=3 causing a non-power-of-2 min_region_sz), it processes its exit sequence by calling kdamond_call(ctx, true) to flush pending requests. After flushing, it eventually sets ctx->kdamond = NULL at the very end of the thread.

If a user writes to commit_inputs exactly in the window after kdamond_call() finishes but before ctx->kdamond = NULL is set:

Thread 1 (kdamond_fn):

    kdamond_call(ctx, true);

Thread 2 (sysfs write):

    damon_call(ctx, &control);
        if (damon_is_running(ctx))
            wait_for_completion(&control.completion);
Quoted 6 lines

Thread 1 (kdamond_fn):

    ctx->kdamond = NULL;

Thread 2 will sleep indefinitely in wait_for_completion(). Because Thread 2 holds the global kernel_param_lock, this permanently freezes all module parameter sysfs interfaces system-wide.

Quoted 6 lines

I have verified this behavior with the following test case:

    # cd /sys/module/damon_lru_sort/parameters/
    # echo Y > enabled
    # ps aux | grep "[k]damond"
    root   70  0.0  0.0  0  0 ?  I  12:16  0:00 [kdamond.0]

FYI, you can read kdamond_pid parameter.

Quoted 9 lines
    # echo 3 > addr_unit
    # echo Y > commit_inputs
    bash: echo: write error: Invalid argument
    # ps aux | grep "[k]damond"

... kdamond has exited unexpectedly

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

That's because kdamond_fn() has recently changed to exit the loop if damon_commit_ctx() fails in the loop for any reason. Refer to commit 26f775a054c3 ("mm/damon/core: avoid use of half-online-committed context") for more details.

Quoted 5 lines

Adding the validation in addr_unit_store() would work, but it could be a kind of whack-a-mole game. I'd prefer doing the validation of all inputs in one place, just before the last damon_commit_ctx() call. DAMON_SYSFS is doing so, by making a test context and try committing user input to the test context before doing that to the running context. How about doing so here, too?

Thank you for the suggestion. I have now added the check '!src->addr_unit || ! is_power_of_2(src->addr_unit)' to damon_commit_ctx().

Quoted 51 lines
Quoted 35 lines
Quoted 23 lines
Quoted 20 lines
mm/damon/lru_sort.c +13 -0
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;
+
+		return err;
+	}
+
+	return control.return_code;
 }
mm/damon/reclaim.c +0 -0
index 86da14778658..4cbd67e08d56 100644

[ ... ]

Quoted 11 lines

mm/damon/reclaim.c

Quoted 8 lines
mm/damon/reclaim.c +8 -0
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);

Does this code share the same kernel_param_lock deadlock potential as damon_lru_sort_commit_inputs_store() described above?

Quoted 5 lines

As we discussed in the RFC-v4 thread [1], this is a false positive. Since 'enabled=N' and 'commit_inputs=Y' are both serialized by the global 'kernel_param_lock', and kthreads cannot be forcibly terminated by userspace signals, the completion signal in damon_call() will always be reached in a controlled manner.

TL; DR: I think the deadlock can happen. But that's a separate issue.

Let's suppose the user writes 'Y' to commit_inputs, and damon_commit_ctx() fails due to an allocation failure. The write to commit_inputs will complete, and kdmond_fn() will start its exit sequence.

The user can write 'Y' to commit_inputs again, when kdamond_fn() finished the last kdamond_call() with 'cancel=True', but before it unset the ->kdamond field.

Quoted 5 lines

Then, the deadlock that sashiko pointed out could happen.

The deadlock is due to the incomplete design of damon_call() cleanup. And I think the same deadlock is already available with DAMON_SYSFS. I will work on fixing this.

I really appreciate your dedication to DAMON.

Quoted 3 lines

So the issue already exists. But let's ensure the fix is merged before this patch, since this patch adds another exploitable path that can consequence in whole param_lock deadlock.

Should I wait for your fix to be merged into damon/next before I post the next version?

Best regards,
Rui Yan

maintainer
#5 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Quoted 7 lines · On Thu, 26 Mar 2026 14:15:54 +0800 Liew Rui Yan <[email protected]>…

Hi SeongJae,

Quoted 3 lines · SeongJae Park

Hi Liew,

On Wed, 25 Mar 2026 15:17:09 +0800 Liew Rui Yan <[email protected]> wrote:

[...]

Quoted 14 lines
Quoted 8 lines
Quoted 2 lines

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

That's because kdamond_fn() has recently changed to exit the loop if damon_commit_ctx() fails in the loop for any reason. Refer to commit 26f775a054c3 ("mm/damon/core: avoid use of half-online-committed context") for more details.

Quoted 5 lines

Adding the validation in addr_unit_store() would work, but it could be a kind of whack-a-mole game. I'd prefer doing the validation of all inputs in one place, just before the last damon_commit_ctx() call. DAMON_SYSFS is doing so, by making a test context and try committing user input to the test context before doing that to the running context. How about doing so here, too?

Quoted 3 lines

Thank you for the suggestion. I have now added the check '!src->addr_unit || ! is_power_of_2(src->addr_unit)' to damon_commit_ctx().

No, what I'm suggesting is not adding more checks, but reusing the existing checks in damon_commit_ctx().

And, now I think bit differently. Given the purpose of this patch is making commit_inputs synchronous, I think making more complete validation check is somewhat that can be done separately. So my suggestion now is just keeping this part just as is.

[...]

Quoted 6 lines
Quoted 3 lines

So the issue already exists. But let's ensure the fix is merged before this patch, since this patch adds another exploitable path that can consequence in whole param_lock deadlock.

Should I wait for your fix to be merged into damon/next before I post the next version?

As long as you agree about the mainline merge order, I think you don't need to wait for my fix. Please feel free to post next version.

FYI, the latest version of my fix is available [1] on the mailing list.

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

Thanks,
SJ

[...]

#6 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Hi SeongJae,

Quoted 15 lines · On Thu, 26 Mar 2026 22:08:22 -0700 SeongJae Park <[email protected]> wrote:

[...]

Quoted 14 lines
Quoted 13 lines
Quoted 2 lines

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

That's because kdamond_fn() has recently changed to exit the loop if damon_commit_ctx() fails in the loop for any reason. Refer to commit 26f775a054c3 ("mm/damon/core: avoid use of half-online-committed context") for more details.

Adding the validation in addr_unit_store() would work, but it could be a kind of whack-a-mole game. I'd prefer doing the validation of all inputs in one place, just before the last damon_commit_ctx() call. DAMON_SYSFS is doing so, by making a test context and try committing user input to the test context before doing that to the running context. How about doing so here, too?

Quoted 6 lines
Quoted 3 lines

Thank you for the suggestion. I have now added the check '!src->addr_unit || ! is_power_of_2(src->addr_unit)' to damon_commit_ctx().

No, what I'm suggesting is not adding more checks, but reusing the existing checks in damon_commit_ctx().

I just realized my previous reply might have caused a small misunderstanding. My intention was not to _add_ a new check, but to _move_ the validation to damon_commit_ctx(). So that patch will only modify damon_commit_ctx().

Quoted 5 lines

And, now I think bit differently. Given the purpose of this patch is making commit_inputs synchronous, I think making more complete validation check is somewhat that can be done separately. So my suggestion now is just keeping this part just as is.

Understood. I will keep this series focused on making 'commit_inputs' synchronous, and prepare the validation for 'addr_unit' as a separate patch later. Thanks for helping me scope this properly.

Quoted 8 lines

[...]

Quoted 6 lines
Quoted 3 lines

So the issue already exists. But let's ensure the fix is merged before this patch, since this patch adds another exploitable path that can consequence in whole param_lock deadlock.

Should I wait for your fix to be merged into damon/next before I post the next version?

Quoted 6 lines

As long as you agree about the mainline merge order, I think you don't need to wait for my fix. Please feel free to post next version.

FYI, the latest version of my fix is available [1] on the mailing list.

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

Thank you for your confirmation, and your fix. :>

Best regards,
Rui Yan

[...]

maintainer
#7 lore raw

Re: (sashiko review) [RFC v5] mm/damon: add synchronous commit for commit_inputs

Quoted 23 lines · On Fri, 27 Mar 2026 13:50:51 +0800 Liew Rui Yan <[email protected]>…

Hi SeongJae,

Quoted 19 lines · On Thu, 26 Mar 2026 22:08:22 -0700 SeongJae Park <[email protected]> wrote:

[...]

Quoted 17 lines
Quoted 8 lines
Quoted 2 lines

I will add a patch in v6 to validate 'addr_unit' in addr_unit_store() to reject non-power-of-2 inputs immediately.

That's because kdamond_fn() has recently changed to exit the loop if damon_commit_ctx() fails in the loop for any reason. Refer to commit 26f775a054c3 ("mm/damon/core: avoid use of half-online-committed context") for more details.

Quoted 5 lines

Adding the validation in addr_unit_store() would work, but it could be a kind of whack-a-mole game. I'd prefer doing the validation of all inputs in one place, just before the last damon_commit_ctx() call. DAMON_SYSFS is doing so, by making a test context and try committing user input to the test context before doing that to the running context. How about doing so here, too?

Thank you for the suggestion. I have now added the check '!src->addr_unit || ! is_power_of_2(src->addr_unit)' to damon_commit_ctx().

Quoted 7 lines
Quoted 2 lines

No, what I'm suggesting is not adding more checks, but reusing the existing checks in damon_commit_ctx().

I just realized my previous reply might have caused a small misunderstanding. My intention was not to _add_ a new check, but to _move_ the validation to damon_commit_ctx(). So that patch will only modify damon_commit_ctx().

I don't really understand what you mean. I found your patch [1], and still not clear. Let's discuss on the new patch.

Quoted 10 lines
Quoted 6 lines

And, now I think bit differently. Given the purpose of this patch is making commit_inputs synchronous, I think making more complete validation check is somewhat that can be done separately. So my suggestion now is just keeping this part just as is.

Understood. I will keep this series focused on making 'commit_inputs' synchronous, and prepare the validation for 'addr_unit' as a separate patch later. Thanks for helping me scope this properly.

Yes, let's discuss on the patch.

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

Quoted 9 lines
Quoted 9 lines

[...]

Quoted 6 lines
Quoted 3 lines

So the issue already exists. But let's ensure the fix is merged before this patch, since this patch adds another exploitable path that can consequence in whole param_lock deadlock.

Should I wait for your fix to be merged into damon/next before I post the next version?

Quoted 8 lines
Quoted 6 lines

As long as you agree about the mainline merge order, I think you don't need to wait for my fix. Please feel free to post next version.

FYI, the latest version of my fix is available [1] on the mailing list.

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

Thank you for your confirmation, and your fix. :>

You're welcome :)

Thanks,
SJ

[...]

maintainer
#8 lore raw
In reply to Liew Rui Yan
Quoted 79 lines · On Wed, 25 Mar 2026 09:39:39 +0800 Liew Rui Yan <[email protected]>…

Problem
=======
Writing invalid parameters to sysfs followed by 'commit_inputs=Y' fails silently (no error returned to shell), because the validation happens asynchronously in the kdamond.

Solution
========
To fix this, the commit_inputs_store() callback now uses damon_call() to synchronously commit parameters in the kdamond thread's safe context. This ensures that validation errors are returned immediately to userspace, following the pattern used by DAMON_SYSFS.

Changes
=======
1. Added commit_inputs_store() and commit_inputs_fn() to commit synchronously.
2. Removed handle_commit_inputs().

This change is motivated from another discussion [1].

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

---
Changes from RFC-v4:
- Rename the 'yes' variable in commit_inputs_store() to the more understandable 'commit_inputs_request'.
- Return -EBUSY instead of -EINVAL when 'commit_inputs' is triggered
while kdamond is not running.
- Link to RFC-v4: https://lore.kernel.org/[email protected]

Changes from RFC-v3:
- Added checks for 'ctx' and 'damon_is_running()' to prevent NULL
pointer dereference during early boot. (Found by Sashiko.dev)
- Removed handle_commit_inputs() and its associated polling logic as
they have become dead code after moving to the synchronous damon_call() approach.
- Ensure the 'commit_inputs' is properly updated.
Link to RFC-v3: https://lore.kernel.org/[email protected]

Changes from RFC-v2:
- Removed damon_validate_attrs(), now using damon_commit_ctx() for synchronous validation in the kdamond context.
- Following DAMON_SYSFS pattern for synchronous commit via damon_call().
- Link to RFC-v2: https://lore.kernel.org/[email protected]

Changes from RFC-v1:
- Remove question from commit message area.
- Added synchronous validation for DAMON_RECLAIM.
- Rename damon_valid_attrs() -> damon_validate_attrs().
- Exported a new function damon_validate_attrs() and declared it in damon.h.
- Link to RFC-v1: https://lore.kernel.org/[email protected]

 mm/damon/lru_sort.c | 54 +++++++++++++++++++++++++++++++++++++++------
 mm/damon/reclaim.c  | 54 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 94 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +2 -2
index 554559d72976..a2410f648b51 100644
@@ -39,7 +39,6 @@ static bool enabled __read_mostly;
  * the re-reading, DAMON_LRU_SORT will be disabled.
  */
 static bool commit_inputs __read_mostly;
-module_param(commit_inputs, bool, 0600);
 
 /*
  * Desired active to [in]active memory ratio in bp (1/10,000).
@@ -349,18 +348,59 @@ static int damon_lru_sort_apply_parameters(void)
 	return err;
 }
 
-static int damon_lru_sort_handle_commit_inputs(void)
+static int damon_lru_sort_commit_inputs_fn(void *arg)
 {
+	return damon_lru_sort_apply_parameters();

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 363

Quoted 10 lines
mm/damon/lru_sort.c +9 -0
+}
+
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,

You don't really use the data in the callback function. Why don't you just unset it?

Quoted 20 lines
mm/damon/lru_sort.c +13 -4
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 389

Quoted 10 lines
mm/damon/lru_sort.c +10 -0
+
+	err = damon_call(ctx, &control);
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;

In my opinion, EINVAL is a better return value. It was failed not because something is busy but just because DAMON is not running. Committing something to DAMON while DAMON is not running seems invalid operation to me.

mm/damon/reclaim.c

Quoted 30 lines
mm/damon/lru_sort.c +13 -1
+
+		return err;
+	}
+
+	return control.return_code;
 }
 
+static const struct kernel_param_ops commit_inputs_param_ops = {
+	.set = damon_lru_sort_commit_inputs_store,
+	.get = param_get_bool,
+};
+
+module_param_cb(commit_inputs, &commit_inputs_param_ops, &commit_inputs, 0600);
+
 static int damon_lru_sort_damon_call_fn(void *arg)
 {
 	struct damon_ctx *c = arg;
@@ -374,7 +414,7 @@ static int damon_lru_sort_damon_call_fn(void *arg)
 			damon_lru_sort_cold_stat = s->stat;
 	}
 
-	return damon_lru_sort_handle_commit_inputs();
+	return 0;
 }
 
 static struct damon_call_control call_control = {
mm/damon/reclaim.c +0 -0
index 86da14778658..4cbd67e08d56 100644

Comments for lru_sort.c apply here, too.

Thanks,
SJ

[...]

#9 lore raw
Quoted 86 lines · On Wed, 25 Mar 2026 07:29:20 -0700 SeongJae Park <[email protected]> wrote:
Quoted 84 lines · On Wed, 25 Mar 2026 09:39:39 +0800 Liew Rui Yan <[email protected]>…

Problem
=======
Writing invalid parameters to sysfs followed by 'commit_inputs=Y' fails silently (no error returned to shell), because the validation happens asynchronously in the kdamond.

Solution
========
To fix this, the commit_inputs_store() callback now uses damon_call() to synchronously commit parameters in the kdamond thread's safe context. This ensures that validation errors are returned immediately to userspace, following the pattern used by DAMON_SYSFS.

Changes
=======
1. Added commit_inputs_store() and commit_inputs_fn() to commit synchronously.
2. Removed handle_commit_inputs().

This change is motivated from another discussion [1].

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

---
Changes from RFC-v4:
- Rename the 'yes' variable in commit_inputs_store() to the more understandable 'commit_inputs_request'.
- Return -EBUSY instead of -EINVAL when 'commit_inputs' is triggered
while kdamond is not running.
- Link to RFC-v4: https://lore.kernel.org/[email protected]

Changes from RFC-v3:
- Added checks for 'ctx' and 'damon_is_running()' to prevent NULL
pointer dereference during early boot. (Found by Sashiko.dev)
- Removed handle_commit_inputs() and its associated polling logic as
they have become dead code after moving to the synchronous damon_call() approach.
- Ensure the 'commit_inputs' is properly updated.
Link to RFC-v3: https://lore.kernel.org/[email protected]

Changes from RFC-v2:
- Removed damon_validate_attrs(), now using damon_commit_ctx() for synchronous validation in the kdamond context.
- Following DAMON_SYSFS pattern for synchronous commit via damon_call().
- Link to RFC-v2: https://lore.kernel.org/[email protected]

Changes from RFC-v1:
- Remove question from commit message area.
- Added synchronous validation for DAMON_RECLAIM.
- Rename damon_valid_attrs() -> damon_validate_attrs().
- Exported a new function damon_validate_attrs() and declared it in damon.h.
- Link to RFC-v1: https://lore.kernel.org/[email protected]

 mm/damon/lru_sort.c | 54 +++++++++++++++++++++++++++++++++++++++------
 mm/damon/reclaim.c  | 54 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 94 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +7 -2
index 554559d72976..a2410f648b51 100644
@@ -39,7 +39,6 @@ static bool enabled __read_mostly;
  * the re-reading, DAMON_LRU_SORT will be disabled.
  */
 static bool commit_inputs __read_mostly;
-module_param(commit_inputs, bool, 0600);
 
 /*
  * Desired active to [in]active memory ratio in bp (1/10,000).
@@ -349,18 +348,59 @@ static int damon_lru_sort_apply_parameters(void)
 	return err;
 }
 
-static int damon_lru_sort_handle_commit_inputs(void)
+static int damon_lru_sort_commit_inputs_fn(void *arg)
 {
+	return damon_lru_sort_apply_parameters();
+}
+
+static int damon_lru_sort_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
Quoted 8 lines

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 363

Quoted 5 lines
mm/damon/lru_sort.c +4 -0
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_lru_sort_commit_inputs_fn,
+		.data = ctx,

You don't really use the data in the callback function. Why don't you just unset it?

You are right, thank you for pointing this out. I will remove the unnecessary assignment in the next version.

Quoted 26 lines
Quoted 26 lines
mm/damon/lru_sort.c +19 -4
+		.repeat = false,
+	};
 
-	if (!commit_inputs)
+	err = kstrtobool(val, &commit_inputs_request);
+	if (err)
+		return err;
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() during early boot or when kdamond is
+	 * not running to avoid NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EBUSY;
+
+	err = damon_call(ctx, &control);
+	if (err) {
+		/*
+		 * If kdamond is not running, damon_call() returns -EINVAL.
+		 * Remap it to -EBUSY to better reflect that the operation
Quoted 8 lines

mm/damon/lru_sort.c static int damon_lru_sort_apply_parameters(void) · line 389

Quoted 4 lines
mm/damon/lru_sort.c +4 -0
+		 * failed due to the current state of the system.
+		 */
+		if (err == -EINVAL && !damon_is_running(ctx))
+			return -EBUSY;

In my opinion, EINVAL is a better return value. It was failed not because something is busy but just because DAMON is not running. Committing something to DAMON while DAMON is not running seems invalid operation to me.

That makes sense. I will revert this to return -EINVAL when DAMON is not running.

Best regards,
Rui Yan