Skip to contentSkip to conversation

mm/damon: add synchronous commit for commit_inputs

RFC PATCH In review Liew Rui Yan 2 participants 6 messages Mar 27 – Mar 28

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

RFC PATCH v6 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-v5:
- Removed unnecessary assignment 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 | 42 +++++++++++++++++++++++++++++++++++-------
 mm/damon/reclaim.c  | 42 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 70 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +35 -7
index 554559d72976..453033f3cb3e 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,47 @@ 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,
+		.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 -EINVAL;
+
+	err = damon_call(ctx, &control);
+
+	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 +402,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 +35 -7

67 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw

Re: (sashiko review) [RFC PATCH v6] 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 405545126e8bb145d7fb1fbb4f36016bc1f6c42c
Author: Liew Rui Yan <[email protected]>
Date:   Fri Mar 27 06:25:58 2026 +0000

mm/damon: add synchronous commit for commit_inputs

This commit modifies the commit_inputs_store() callback to use damon_call() for synchronously committing parameters in the kdamond thread's context. This change aims to ensure validation errors are returned immediately to userspace.

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

[ ... ]

Quoted 19 lines
mm/damon/lru_sort.c +13 -1
@@ -349,18 +348,47 @@ 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,
+		.repeat = false,
+	};

[ ... ]

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

Quoted 4 lines
mm/damon/lru_sort.c +3 -0
+	err = damon_call(ctx, &control);
+
+	return control.return_code;
 }

If damon_call() fails, for example returning -EINVAL when kdamond is not running, err is set but the error code is never returned or checked.

Will this cause the sysfs write to incorrectly succeed by returning the default control.return_code of 0?

Should it return err if err is non-zero, or control.return_code otherwise?

Quoted 4 lines
mm/damon/reclaim.c +0 -0
index 86da14778658..03e3300ce607 100644

[ ... ]

Quoted 9 lines
mm/damon/reclaim.c +8 -0
+static int damon_reclaim_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_reclaim_commit_inputs_fn,
+		.repeat = false,
+	};

[ ... ]

mm/damon/reclaim.c

Quoted 4 lines
mm/damon/reclaim.c +3 -0
+	err = damon_call(ctx, &control);
+
+	return control.return_code;
 }

Does this function have the same issue where the return value of damon_call() is ignored?

# 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
#2 lore raw

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

Quoted 54 lines

Forwarding Sashiko review for doing discussions via mails.

# review url: https://sashiko.dev/#/patchset/[email protected]
# start of sashiko.dev inline review
commit 405545126e8bb145d7fb1fbb4f36016bc1f6c42c
Author: Liew Rui Yan <[email protected]>
Date:   Fri Mar 27 06:25:58 2026 +0000

mm/damon: add synchronous commit for commit_inputs

This commit modifies the commit_inputs_store() callback to use damon_call() for synchronously committing parameters in the kdamond thread's context. This change aims to ensure validation errors are returned immediately to userspace.

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

[ ... ]

Quoted 19 lines
mm/damon/lru_sort.c +13 -1
@@ -349,18 +348,47 @@ 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,
+		.repeat = false,
+	};

[ ... ]

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

Quoted 4 lines
mm/damon/lru_sort.c +3 -0
+	err = damon_call(ctx, &control);
+
+	return control.return_code;
 }

If damon_call() fails, for example returning -EINVAL when kdamond is not running, err is set but the error code is never returned or checked.

Quoted 4 lines

Will this cause the sysfs write to incorrectly succeed by returning the default control.return_code of 0?

Should it return err if err is non-zero, or control.return_code otherwise?

Thank you for reminder. I will add
'return err ? err : control.return_code' back in next patch.

Quoted 19 lines
Quoted 4 lines
mm/damon/reclaim.c +0 -0
index 86da14778658..03e3300ce607 100644

[ ... ]

Quoted 9 lines
mm/damon/reclaim.c +8 -0
+static int damon_reclaim_commit_inputs_store(const char *val,
+		const struct kernel_param *kp)
+{
+	bool commit_inputs_request;
 	int err;
+	struct damon_call_control control = {
+		.fn = damon_reclaim_commit_inputs_fn,
+		.repeat = false,
+	};

[ ... ]

Quoted 7 lines

mm/damon/reclaim.c

Quoted 4 lines
mm/damon/reclaim.c +3 -0
+	err = damon_call(ctx, &control);
+
+	return control.return_code;
 }

Does this function have the same issue where the return value of damon_call() is ignored?

Yes, it have the same issue. Thank you for reminder. :>

Best regards,
Rui Yan

#3 lore raw

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

Quoted 11 lines

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

Quoted 8 lines
mm/damon/lru_sort.c +7 -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,
+		.repeat = false,

Undeclared fields are initialized to zero. I don't think we need to explicitly set repeat here.

Agreed. This change can makes the code more clean.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	return control.return_code;

Shouldn't we return err if it is not zero?

Correct, I've noticed that as well [1]. But thank you for reminding me anyway.

Quoted 4 lines

mm/damon/reclaim.c

Quoted 2 lines
mm/damon/reclaim.c +0 -0

Same comments can be applied to changes for this file.

I will apply these changes in the next version. I will remove the RFC tag because I believe the high-level design in now solid, and the low-level details have been mostly addressed. :>

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

Best regards,
Rui Yan

maintainer
#4 lore raw
In reply to Liew Rui Yan

Hello Liew,

Quoted 85 lines · On Fri, 27 Mar 2026 14:25:58 +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-v5:
- Removed unnecessary assignment 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 | 42 +++++++++++++++++++++++++++++++++++-------
 mm/damon/reclaim.c  | 42 +++++++++++++++++++++++++++++++++++-------
 2 files changed, 70 insertions(+), 14 deletions(-)
mm/damon/lru_sort.c +2 -2
index 554559d72976..453033f3cb3e 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,47 @@ 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,
+		.repeat = false,

Undeclared fields are initialized to zero. I don't think we need to explicitly set repeat here.

Quoted 13 lines
mm/damon/lru_sort.c +6 -4
+	};
 
-	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;

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

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

Shouldn't we return err if it is not zero?

[...]

mm/damon/reclaim.c

Quoted 2 lines
mm/damon/reclaim.c +0 -0

Same comments can be applied to changes for this file.

Thanks,
SJ

[...]

#5 lore raw
Quoted 11 lines

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

Quoted 8 lines
mm/damon/lru_sort.c +7 -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,
+		.repeat = false,

Undeclared fields are initialized to zero. I don't think we need to explicitly set repeat here.

Agreed. This change can makes the code more clean.

Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	return control.return_code;

Shouldn't we return err if it is not zero?

Correct, I've noticed that as well [1]. But thank you for reminding me anyway.

Quoted 4 lines

mm/damon/reclaim.c

Quoted 2 lines
mm/damon/reclaim.c +0 -0

Same comments can be applied to changes for this file.

I will apply these changes in the next version. I will remove the RFC tag because I believe the high-level design in now solid, and the low-level details have been mostly addressed. :>

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

Best regards,
Rui Yan