Skip to contentSkip to conversation

mm/damon: add synchronous commit for commit_inputs

PATCH In review Liew Rui Yan 2 participants 4 messages Mar 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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 v1:
- Restore the KERNEL_PARAM_OPS_FL_NOARG flag to keep the compatibility.
- Link to V1: https://lore.kernel.org/[email protected]

Changes from RFC-v6:
- Removed unnecessary assignment (repeat) in commit_inputs_store().
- Change the return value; if an error occurs, return the error code.
- Removed the RFC tag.
- Link to RFC-v6: https://lore.kernel.org/[email protected]

Changes from RFC-v5:
- Removed unnecessary assignment (data) in commit_inputs_store().
- Return -EINVAL instead of -EBUSY when 'commit_inputs' is triggered
while kdamond is not running.
- Link to RFC-v5: 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 | 46 ++++++++++++++++++++++++++++++++++++++-------
 mm/damon/reclaim.c  | 46 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 78 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +39 -7
index 554559d72976..641af42cc2d1 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,51 @@ 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,
+	};
+
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		err = kstrtobool(val, &commit_inputs_request);
+		if (err)
+			return err;
+	}
 
-	if (!commit_inputs)
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_lru_sort_apply_parameters();
-	commit_inputs = false;
-	return err;
+	/*
+	 * Skip damon_call() if ctx is not initialized to avoid
+	 * NULL pointer dereference.
+	 */
+	if (!ctx)
+		return -EINVAL;
+
+	err = damon_call(ctx, &control);
+
+	return err ? err : control.return_code;
 }
 
+static const struct kernel_param_ops commit_inputs_param_ops = {
+	.flags = KERNEL_PARAM_OPS_FL_NOARG,
+	.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 +406,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 +39 -7

71 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw

Forwarding Sashiko review for doing discussions via mails.

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

mm/damon: add synchronous commit for commit_inputs

This commit modifies the commit_inputs module parameter callback to synchronously apply parameters using damon_call() instead of relying on asynchronous validation in the kdamond thread.

mm/damon/lru_sort.c +0 -0

[ ... ]

Quoted 1 line
(quoted diff) +0 -0
@@ -349,18 +348,51 @@ static int damon_lru_sort_apply_parameters(void)

[ ... ]

Quoted 20 lines
(quoted diff) +14 -3
+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,
+	};
 
-	if (!commit_inputs)
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		err = kstrtobool(val, &commit_inputs_request);
+		if (err)
+			return err;
+	}
 
-	err = damon_lru_sort_apply_parameters();
-	commit_inputs = false;

Review

Quoted 10 lines
(quoted diff) +7 -1
-	return err;
+	if (!commit_inputs_request)
 		return 0;
 
+	/*
+	* Skip damon_call() if ctx is not initialized to avoid
+	* NULL pointer dereference.
+	*/
+	if (!ctx)
+		return -EINVAL;

Does this break module loading if commit_inputs=Y is provided as a boot parameter or via a modprobe configuration?

During module initialization, parse_args() executes module parameter callbacks before the module's init function runs.

Since ctx is initialized in the module's init function, won't ctx be NULL when this callback fires, causing an -EINVAL return that aborts the entire module load?

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+	err = damon_call(ctx, &control);

Could this synchronous call break backward compatibility for userspace scripts?

Previously, writing Y to commit_inputs while the monitor was disabled (enabled=N) would silently succeed and apply the parameters on the next start.

If the kdamond thread is not running, won't damon_call() detect !damon_is_running(ctx) and return -EINVAL, causing writes to fail when the monitor is off?

Since this is a module parameter callback, the global param_lock mutex is held during its execution. Does calling damon_call() here cause system-wide latency spikes?

damon_call() blocks on wait_for_completion() waiting for the kdamond thread, which only checks its call list once per sample interval.

Will this stall all other tasks attempting to read or write built-in module parameters for up to the duration of the DAMON sampling interval?

Is there a race condition here with the kdamond thread termination that could result in a permanent deadlock?

When kdamond_fn() exits, it flushes pending controls and then sets ctx->kdamond to NULL.

If a concurrent sysfs write triggers damon_call() after the controls are flushed but before ctx->kdamond is cleared, damon_is_running(ctx) will return true.

Will damon_call() block indefinitely on wait_for_completion() because the thread is exiting and will never signal it, leaving the sysfs writer permanently stuck in D state while holding the system-wide param_lock?

Quoted 3 lines
(quoted diff) +2 -0
+
+	return err ? err : control.return_code;
 }

[ ... ]

mm/damon/reclaim.c +0 -0

[ ... ]

Quoted 3 lines
(quoted diff) +3 -0
+static int damon_reclaim_commit_inputs_store(const char *val,
+					    const struct kernel_param *kp)
+{

[ ... ]

Review

Quoted 11 lines
(quoted diff) +10 -0
+	/*
+	* Skip damon_call() if ctx is not initialized to avoid
+	* NULL pointer dereference.
+	*/
+	if (!ctx)
+		return -EINVAL;
+
+	err = damon_call(ctx, &control);
+
+	return err ? err : control.return_code;
 }

Are these same regressions present here in damon_reclaim_commit_inputs_store() as well?

# 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
maintainer
#2 lore raw
Quoted 1 line · On Sun, 29 Mar 2026 17:56:02 +0800 Liew Rui Yan <[email protected]>…

Forwarding Sashiko review for doing discussions via mails.

To my understanding, all the comments are same to those for v1, which we concluded to be no real issues. Please let me know if I'm missing something.

Thanks,
SJ

[...]

maintainer Reviewed-by on v2 1/1
#3 lore raw
In reply to Liew Rui Yan
Quoted 19 lines · On Sun, 29 Mar 2026 15:54:15 +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().

Quoted 5 lines

This change is motivated from another discussion [1].

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

Note that mm.git is nearly closed for non-hotfixes until 7.1-rc1 release. I will add this to my tree now, and resubmit for mm.git merge after 7.1-rc1 release.

Thanks,
SJ

[...]