Skip to contentSkip to conversation

mm/damon: repost non-hotfix reviewed patches in damon/next tree

PATCH Applied SeongJae Park 3 participants 14 messages Apr 26 – Apr 28

Subsystems (5)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

v2 cover mm/damon: repost non-hotfix reviewed patches in damon/next tree

Re-posting non-hotfix DAMON patches that reviewed by DAMON maintainer but not yet merged into mm.git.

The first patch from Liew Rui Yan add a minor performance optimization using ilog2() instead of inefficient manual implementation of the functionality.

The second patch from Cheng-Han Wu fixes a minor typo:
s/parametrs/parameters/.

The third patch from Liew Rui Yan make commit_inputs operation of DAMON_RECLAIM and DAMON_LRU_SORT synchronous to improve the user experience.

The fourth patch from Asier Gutierrez adds a new DAMOS action, DAMOS_COLLAPSE for deterministic DAMOS-based access-aware THP system.

Changes from v1
- v1: https://lore.kernel.org/[email protected]
- Add DAMOS_COLLAPSE patch.
- Rebase to latest mm-new.

Asier Gutierrez (1):
mm/damon: support MADV_COLLAPSE via DAMOS_COLLAPSE scheme action

Cheng-Han Wu (1):
Docs/admin-guide/mm/damon: fix 'parametrs' typo

Liew Rui Yan (2):
mm/damon/ops-common: optimize damon_hot_score() using ilog2() mm/damon: add synchronous commit for commit_inputs

 .../admin-guide/mm/damon/lru_sort.rst         |  2 +-
 .../admin-guide/mm/damon/reclaim.rst          |  2 +-
 Documentation/mm/damon/design.rst             |  4 ++
 include/linux/damon.h                         |  2 +
 mm/damon/lru_sort.c                           | 46 ++++++++++++++++---
 mm/damon/ops-common.c                         |  9 ++--
 mm/damon/reclaim.c                            | 46 ++++++++++++++++---
 mm/damon/sysfs-schemes.c                      |  4 ++
 mm/damon/vaddr.c                              |  3 ++
 tools/testing/selftests/damon/sysfs.py        | 11 +++--
 10 files changed, 105 insertions(+), 24 deletions(-)

base-commit: 1e24f85d7a997307185d5345c1711f485570a123

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH v2 1/4 mm/damon/ops-common: optimize damon_hot_score() using ilog2()

From: Liew Rui Yan <[email protected]>

The current implementation of damon_hot_score() uses a manual for-loop to calculate the value of 'age_in_log'. This can be efficiently replaced by ilog2(), which is semantically more appropriate for calculating the logarithmic value of age.

In a simulated-kernel-module performance test with 10,000,000 iterations, this optimization showed a significant reduction in latency (average latency reduced from ~12ns to ~1ns).

Test results from the simulated-kernel-module:
- ilog2:
DAMON Perf Test: Starting 10000000 iterations
=============================================
Total Iterations : 10000000
Average Latency : 1 ns
P95 Latency : 41 ns
P99 Latency : 41 ns
---------------------------------------------
Range (ns) | Count | Percent
---------------------------------------------
0-19 | 0 | 0%
20-39 | 2625000 | 26%
40-59 | 7374000 | 73%
60-79 | 0 | 0%
80-99 | 0 | 0%
100+ | 1000 | 0%
=============================================

- for-loop:

    DAMON Perf Test: Starting 10000000 iterations
    =============================================
     Total Iterations : 10000000
     Average Latency  : 12 ns
     P95 Latency      : 51 ns
     P99 Latency      : 60 ns
    ---------------------------------------------
     Range (ns)      | Count        | Percent
    ---------------------------------------------
     0-19            | 0            |      0%
     20-39           | 0            |      0%
     40-59           | 9862000      |     98%
     60-79           | 135000       |      1%
     80-99           | 1000         |      0%
     100+            | 2000         |      0%
    =============================================

Full raw benchmark results can be found at [1].

[1] https://github.com/aethernet65535/damon-hot-score-fls-optimize/tree/master/result-raw

---
Changes from v2
(https://lore.kernel.org/[email protected])
- Rebase to latest mm-new.
Changes from v1 (actually it was RFC)
(https://lore.kernel.org/[email protected])
- Replace fls() with ilog2() per SeongJae Park's suggestion for better semantic clarity.
- Move performance benchmark results into the commit message and add comparison between for-loop and ilog2.

 mm/damon/ops-common.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/damon/ops-common.c +6 -3
index 8c6d613425c13..3a0ddc3ac7196 100644
@@ -117,9 +117,12 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
 		damon_max_nr_accesses(&c->attrs);
 
 	age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;
-	for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
-			age_in_log++, age_in_sec >>= 1)
-		;
+	if (age_in_sec)
+		age_in_log = min_t(int, ilog2(age_in_sec) + 1,
+				DAMON_MAX_AGE_IN_LOG);
+	else
+		age_in_log = 0;
+
 
 	/* If frequency is 0, higher age means it's colder */
 	if (freq_subscore == 0)
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH v2 1/4] mm/damon/ops-common: optimize damon_hot_score() using ilog2()

Sashiko AI review found 3 potential issue(s): - [High] Arithmetic overflow on 32-bit architectures during `age_in_sec` calculation. - [Low] The logic using `ilog2()` with a zero-check branch can be…

Sashiko AI review found 3 potential issue(s):
- [High] Arithmetic overflow on 32-bit architectures during age_in_sec calculation.
- [Low] The logic using ilog2() with a zero-check branch can be simplified by directly using fls().
- [Low] Unnecessary double blank line introduced.
--

commit c096ebdf3dd6d7a6e783e2ac65dec1740282929f
Author: Liew Rui Yan <[email protected]>

mm/damon/ops-common: optimize damon_hot_score() using ilog2()

This commit replaces the manual for-loop used to calculate age_in_log in damon_hot_score() with the ilog2() function. This optimization aims to significantly reduce the latency of the hotness score calculation.

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 119

Quoted 8 lines
mm/damon/ops-common.c +0 -0
index 8c6d613425c1..3a0ddc3ac719 100644
@@ -117,9 +117,12 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
 		damon_max_nr_accesses(&c->attrs);
 
 	age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;

This isn't a bug in your patch, but does the multiplication of (unsigned long)r->age * c->attrs.aggr_interval overflow on 32-bit architectures?

Since both variables evaluate as 32-bit types on 32-bit architectures, if aggr_interval is 1,000,000 (1 second), the multiplication will wrap around when r->age exceeds 4294. This wrap-around could result in a very small age_in_sec value for extremely cold regions, causing them to be falsely evaluated as young and hot.

Should this calculation be cast to a 64-bit integer prior to multiplication, perhaps using div_u64()?

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 124

Quoted 8 lines
mm/damon/ops-common.c +5 -3
-	for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
-			age_in_log++, age_in_sec >>= 1)
-		;
+	if (age_in_sec)
+		age_in_log = min_t(int, ilog2(age_in_sec) + 1,
+				DAMON_MAX_AGE_IN_LOG);
+	else
+		age_in_log = 0;

Instead of using ilog2() with a zero-check branch, could this be simplified by directly using fls()?

The fls() function safely returns 0 when the value is 0 and correctly calculates the 1-based log2 value for positive numbers, allowing this branch to be removed entirely while preserving the exact mathematical result.

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 127

Quoted 3 lines
mm/damon/ops-common.c +1 -0
+
 
 	/* If frequency is 0, higher age means it's colder */

This isn't a bug, but an extra blank line appears to be introduced here, creating two consecutive blank lines.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCH v2 1/4] mm/damon/ops-common: optimize damon_hot_score() using ilog2()

Quoted 28 lines · On Sun, 26 Apr 2026 23:28:10 +0000 [email protected] wrote:

Sashiko AI review found 3 potential issue(s):
- [High] Arithmetic overflow on 32-bit architectures during age_in_sec calculation.
- [Low] The logic using ilog2() with a zero-check branch can be simplified by directly using fls().
- [Low] Unnecessary double blank line introduced.
--

commit c096ebdf3dd6d7a6e783e2ac65dec1740282929f
Author: Liew Rui Yan <[email protected]>

mm/damon/ops-common: optimize damon_hot_score() using ilog2()

This commit replaces the manual for-loop used to calculate age_in_log in damon_hot_score() with the ilog2() function. This optimization aims to significantly reduce the latency of the hotness score calculation.

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 119

Quoted 8 lines
mm/damon/ops-common.c +0 -0
index 8c6d613425c1..3a0ddc3ac719 100644
@@ -117,9 +117,12 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
 		damon_max_nr_accesses(&c->attrs);
 
 	age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;

This isn't a bug in your patch, but does the multiplication of (unsigned long)r->age * c->attrs.aggr_interval overflow on 32-bit architectures?

Quoted 8 lines

Since both variables evaluate as 32-bit types on 32-bit architectures, if aggr_interval is 1,000,000 (1 second), the multiplication will wrap around when r->age exceeds 4294. This wrap-around could result in a very small age_in_sec value for extremely cold regions, causing them to be falsely evaluated as young and hot.

Should this calculation be cast to a 64-bit integer prior to multiplication, perhaps using div_u64()?

Good point, but as Sashiko also notes, not a blocker of this patch. I will add a todo item for thinking more on this.

Quoted 13 lines

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 124

Quoted 9 lines
mm/damon/ops-common.c +5 -3
-	for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
-			age_in_log++, age_in_sec >>= 1)
-		;
+	if (age_in_sec)
+		age_in_log = min_t(int, ilog2(age_in_sec) + 1,
+				DAMON_MAX_AGE_IN_LOG);
+	else
+		age_in_log = 0;

Instead of using ilog2() with a zero-check branch, could this be simplified by directly using fls()?

Quoted 4 lines

The fls() function safely returns 0 when the value is 0 and correctly calculates the 1-based log2 value for positive numbers, allowing this branch to be removed entirely while preserving the exact mathematical result.

We believe ilog2() is easier to read.

Quoted 7 lines

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 127

Quoted 4 lines
mm/damon/ops-common.c +1 -0
+
 
 	/* If frequency is 0, higher age means it's colder */

This isn't a bug, but an extra blank line appears to be introduced here, creating two consecutive blank lines.

Good cactch. As this is a quite minor issue, I will ask Andrew to fix this.

Quoted 3 lines
Signature · 2 lines
-- 
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

Thanks,
SJ

author maintainer
#4 lore raw

Re: [PATCH v2 1/4] mm/damon/ops-common: optimize damon_hot_score() using ilog2()

In reply to SeongJae Park
Quoted 1 line · On Sun, 26 Apr 2026 16:16:14 -0700 SeongJae Park <[email protected]> wrote:

From: Liew Rui Yan <[email protected]>

[...]

Quoted 8 lines
mm/damon/ops-common.c +0 -2
@@ -117,9 +117,12 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
 		damon_max_nr_accesses(&c->attrs);
 
 	age_in_sec = (unsigned long)r->age * c->attrs.aggr_interval / 1000000;
-	for (age_in_log = 0; age_in_log < DAMON_MAX_AGE_IN_LOG && age_in_sec;
-			age_in_log++, age_in_sec >>= 1)

mm/damon/ops-common.c int damon_hot_score(struct damon_ctx *c, struct damon_region *r, · line 128

Quoted 10 lines
mm/damon/ops-common.c +6 -1
-		;
+	if (age_in_sec)
+		age_in_log = min_t(int, ilog2(age_in_sec) + 1,
+				DAMON_MAX_AGE_IN_LOG);
+	else
+		age_in_log = 0;
+
 
 	/* If frequency is 0, higher age means it's colder */
 	if (freq_subscore == 0)

Sashiko found this patch is introducing two blank lines. Andrew, I saw you already added this to mm-new. Could you please add below attaching fixup? If you prefer reposting this patch, please let me know.

Thanks,
SJ

[...]
=== >8 ===
From 1d97945a7c35463d3757df985cf3545bf9d5ed9c Mon Sep 17 00:00:00 2001
From: SeongJae Park <[email protected]>
Date: Mon, 27 Apr 2026 08:05:09 -0700
Subject: [PATCH] mm/damon/ops-common: (fixup) remove unnecessary blank line

Two blank lines are unnecessary.

The issue was discovered [1] by Sashiko.

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

---
 mm/damon/ops-common.c | 1 -
 1 file changed, 1 deletion(-)
mm/damon/ops-common.c +0 -1
index 3a0ddc3ac7196..b2ce817f33c53 100644
@@ -123,7 +123,6 @@ int damon_hot_score(struct damon_ctx *c, struct damon_region *r,
 	else
 		age_in_log = 0;
 
-
 	/* If frequency is 0, higher age means it's colder */
 	if (freq_subscore == 0)
 		age_in_log *= -1;
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH v2 2/4 Docs/admin-guide/mm/damon: fix 'parametrs' typo

In reply to SeongJae Park

From: Cheng-Han Wu <[email protected]>

Fix the misspelling of "parametrs" as "parameters" in reclaim.rst and lru_sort.rst.

---
Changes from v1
(https://lore.kernel.org/[email protected])
- Rebase to latest mm-new.

 Documentation/admin-guide/mm/damon/lru_sort.rst | 2 +-
 Documentation/admin-guide/mm/damon/reclaim.rst  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Documentation/admin-guide/mm/damon/lru_sort.rst +1 -1
index 14cc6b2db8973..25e2f042a383f 100644
@@ -75,7 +75,7 @@ Make DAMON_LRU_SORT reads the input parameters again, except ``enabled``.
 
 Input parameters that updated while DAMON_LRU_SORT is running are not applied
 by default.  Once this parameter is set as ``Y``, DAMON_LRU_SORT reads values
-of parametrs except ``enabled`` again.  Once the re-reading is done, this
+of parameters except ``enabled`` again.  Once the re-reading is done, this
 parameter is set as ``N``.  If invalid parameters are found while the
 re-reading, DAMON_LRU_SORT will be disabled.
 
Documentation/admin-guide/mm/damon/reclaim.rst +1 -1
index d7a0225b49508..01a34c215b66f 100644
@@ -67,7 +67,7 @@ Make DAMON_RECLAIM reads the input parameters again, except ``enabled``.
 
 Input parameters that updated while DAMON_RECLAIM is running are not applied
 by default.  Once this parameter is set as ``Y``, DAMON_RECLAIM reads values
-of parametrs except ``enabled`` again.  Once the re-reading is done, this
+of parameters except ``enabled`` again.  Once the re-reading is done, this
 parameter is set as ``N``.  If invalid parameters are found while the
 re-reading, DAMON_RECLAIM will be disabled.
 
Signature · 1 line
2.47.3
author maintainer Reviewed-by on v2 2/4
#6 lore raw

Re: [PATCH v2 2/4] Docs/admin-guide/mm/damon: fix 'parametrs' typo

Quoted 6 lines · On Sun, 26 Apr 2026 16:16:15 -0700 SeongJae Park <[email protected]> wrote:

From: Cheng-Han Wu <[email protected]>

Fix the misspelling of "parametrs" as "parameters" in reclaim.rst and lru_sort.rst.

Oops, I forgot adding my Reviewed-by: tag.

Andrew, could you please add the above my Reviewed-by: tag?

Thanks,
SJ

[...]

#7 lore raw

Re: [PATCH v2 2/4] Docs/admin-guide/mm/damon: fix 'parametrs' typo

Quoted 7 lines · SeongJae Park
Quoted 5 lines · On Sun, 26 Apr 2026 16:16:15 -0700 SeongJae Park <[email protected]> wrote:

From: Cheng-Han Wu <[email protected]>

Fix the misspelling of "parametrs" as "parameters" in reclaim.rst and lru_sort.rst.

Quoted 7 lines
Quoted 1 line

Oops, I forgot adding my Reviewed-by: tag.

Andrew, could you please add the above my Reviewed-by: tag?

Since you were sending that patch along in you series, there was (as it must) already your S-o-b tag and that's stronger responsibility than a Reviewed-by, so it's redundant to add it :)

Quoted 4 lines

Thanks,
SJ

[...]

author maintainer
#8 lore raw

Re: [PATCH v2 2/4] Docs/admin-guide/mm/damon: fix 'parametrs' typo

Quoted 12 lines · On Tue, 28 Apr 2026 09:38:02 +0200 "Vlastimil Babka (SUSE)"…
Quoted 11 lines · SeongJae Park
Quoted 6 lines · On Sun, 26 Apr 2026 16:16:15 -0700 SeongJae Park <[email protected]> wrote:

From: Cheng-Han Wu <[email protected]>

Fix the misspelling of "parametrs" as "parameters" in reclaim.rst and lru_sort.rst.

Oops, I forgot adding my Reviewed-by: tag.

Quoted 7 lines
Quoted 3 lines

Andrew, could you please add the above my Reviewed-by: tag?

Since you were sending that patch along in you series, there was (as it must) already your S-o-b tag and that's stronger responsibility than a Reviewed-by, so it's redundant to add it :)

You are right. But, not having R-b tags that I provided on the review stage of patches gone makes me feel odd and bit confusing. Also I made a weird tool for checking DAMON patch review status that doesn't understand this situation well... Maybe I should fix my brain and tool, but if it doesn't bother Andrew, I'd still hope to have the R-b tags on the patches.

Thanks,
SJ

[...]

author maintainer
#9 lore raw

PATCH v2 3/4 mm/damon: add synchronous commit for commit_inputs

In reply to SeongJae Park

From: 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 v2
(https://lore.kernelorg/[email protected])
- Rebase to latest mm-new.
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 8494040b1ee48..7569e471160a0 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).
@@ -340,18 +339,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;
@@ -365,7 +397,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.47.3
#10 lore raw

Re: [PATCH v2 3/4] mm/damon: add synchronous commit for commit_inputs

Sashiko AI review found 1 potential issue(s): - [High] Writing to the `commit_inputs` sysfs parameter before the DAMON worker thread (`kdamond`) is started causes the writing task to hang…

Sashiko AI review found 1 potential issue(s):
- [High] Writing to the commit_inputs sysfs parameter before the DAMON worker thread (kdamond) is started causes the writing task to hang indefinitely in an unkillable D-state. --

commit 43a70e5ab295c21c0b94eddd32d886c9d0eba3fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

This commit changes the commit_inputs_store() callback to use damon_call() to synchronously commit parameters in the kdamond thread's safe context. This ensures validation errors are returned immediately to userspace.

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

[ ... ]

Quoted 32 lines
mm/damon/lru_sort.c +19 -3
@@ -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 (!commit_inputs)
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		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 383

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	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);

If the module has not yet been enabled (e.g., enabled=N) and the kdamond thread is not running, does this call to damon_call() block indefinitely?

Since ctx is allocated during initialization, the ctx pointer is not NULL. damon_call() then queues the control request and calls wait_for_completion().

Without the background thread running to process the queue and signal completion, it seems the sysfs writer task is placed into an unkillable D-state forever.

The damon_call() documentation indicates it should only be called after damon_start() has succeeded, otherwise it could fall into an indefinite wait.

Quoted 3 lines
mm/damon/lru_sort.c +2 -0
+
+	return err ? err : control.return_code;
 }

[ ... ]

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

[ ... ]

Quoted 32 lines
mm/damon/reclaim.c +20 -3
@@ -255,18 +254,51 @@ static int damon_reclaim_apply_parameters(void)
 	return err;
 }
 
-static int damon_reclaim_handle_commit_inputs(void)
+static int damon_reclaim_commit_inputs_fn(void *arg)
 {
+	return damon_reclaim_apply_parameters();
+}
+
+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,
+	};
 
-	if (!commit_inputs)
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		err = kstrtobool(val, &commit_inputs_request);
+		if (err)
+			return err;
+	}
+
+	if (!commit_inputs_request)
 		return 0;
 
-	err = damon_reclaim_apply_parameters();

mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 289

Quoted 10 lines
mm/damon/reclaim.c +8 -2
-	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);

Could this exhibit the same hang as in damon_lru_sort.c? If the worker thread has not been started, does writing to commit_inputs block forever?

Quoted 3 lines
mm/damon/reclaim.c +2 -0
+
+	return err ? err : control.return_code;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#11 lore raw

Re: [PATCH v2 3/4] mm/damon: add synchronous commit for commit_inputs

Quoted 2 lines · On Mon, 27 Apr 2026 00:00:11 +0000 [email protected] wrote:

Sashiko AI review found 1 potential issue(s):
- [High] Writing to the commit_inputs sysfs parameter before the DAMON worker thread (kdamond) is started causes the writing task to hang indefinitely in an unkillable D-state.

I think this is wrong.

Quoted 66 lines

--

commit 43a70e5ab295c21c0b94eddd32d886c9d0eba3fa
Author: Liew Rui Yan <[email protected]>

mm/damon: add synchronous commit for commit_inputs

This commit changes the commit_inputs_store() callback to use damon_call() to synchronously commit parameters in the kdamond thread's safe context. This ensures validation errors are returned immediately to userspace.

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

[ ... ]

Quoted 32 lines
mm/damon/lru_sort.c +19 -3
@@ -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 (!commit_inputs)
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		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 383

Quoted 10 lines
mm/damon/lru_sort.c +8 -2
-	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);

If the module has not yet been enabled (e.g., enabled=N) and the kdamond thread is not running, does this call to damon_call() block indefinitely?

Quoted 6 lines

Since ctx is allocated during initialization, the ctx pointer is not NULL. damon_call() then queues the control request and calls wait_for_completion().

Without the background thread running to process the queue and signal completion, it seems the sysfs writer task is placed into an unkillable D-state forever.

No. ctx is initialized to NULL because it is static. Once it is set, kdamond_fn() is started. If user stops kdamond by writing N to enabled, ctx->call_controls_obsolete is set. If user further writes Y to commit_inputs for invoking damon_call(), it will return -ECANCELED due to ctx->call_controls_obsolete.

Quoted 3 lines

The damon_call() documentation indicates it should only be called after damon_start() has succeeded, otherwise it could fall into an indefinite wait.

Yes, and that's excluded here since the module is not calling damon_call() for NULL ctx.

Quoted 45 lines
Quoted 4 lines
mm/damon/lru_sort.c +2 -0
+
+	return err ? err : control.return_code;
 }

[ ... ]

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

[ ... ]

Quoted 31 lines
mm/damon/reclaim.c +20 -2
@@ -255,18 +254,51 @@ static int damon_reclaim_apply_parameters(void)
 	return err;
 }
 
-static int damon_reclaim_handle_commit_inputs(void)
+static int damon_reclaim_commit_inputs_fn(void *arg)
 {
+	return damon_reclaim_apply_parameters();
+}
+
+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,
+	};
 
-	if (!commit_inputs)
+	if (!val) {
+		commit_inputs_request = true;
+	} else {
+		err = kstrtobool(val, &commit_inputs_request);
+		if (err)
+			return err;
+	}
+
+	if (!commit_inputs_request)
 		return 0;
 
Quoted 14 lines

mm/damon/reclaim.c static int damon_reclaim_apply_parameters(void) · line 280

Quoted 11 lines
mm/damon/reclaim.c +8 -3
-	err = damon_reclaim_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);

Could this exhibit the same hang as in damon_lru_sort.c? If the worker thread has not been started, does writing to commit_inputs block forever?

Same to the above reply, no.

Quoted 7 lines
Quoted 4 lines
mm/damon/reclaim.c +2 -0
+
+	return err ? err : control.return_code;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

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

author maintainer
#12 lore raw

PATCH v2 4/4 mm/damon: support MADV_COLLAPSE via DAMOS_COLLAPSE scheme action

In reply to SeongJae Park

From: Asier Gutierrez <[email protected]>

This patch set introces a new action: DAMOS_COLLAPSE.

For DAMOS_HUGEPAGE and DAMOS_NOHUGEPAGE to work, khugepaged should be working, since it relies on hugepage_madvise to add a new slot. This slot should be picked up by khugepaged and eventually collapse (or not, if we are using DAMOS_NOHUGEPAGE) the pages. If THP is not enabled, khugepaged will not be working, and therefore no collapse will happen.

DAMOS_COLLAPSE eventually calls madvise_collapse, which will collapse the address range synchronously. In cases where there is a large VMA (databases, for example), DAMOS_COLLAPSE allows us to collapse only the hot region, and not the entire VMA.

This new action may be required to support autotuning with hugepage as a goal[1].

=========
Benchmarks:
=========

MySQL
=====

Tests were performed in an ARM physical server with MariaDB 10.5 and sysbench. Read only benchmark was perform with gaussian row hitting, which follows a normal distribution.

T n, D h: THP set to never, DAMON action set to hugepage T m, D h: THP set to madvise, DAMON action set to hugepage T n, D c: THP set to never, DAMON action set to collapse

Memory consumption. Lower is better.

+------------------+----------+----------+----------+
|                  | T n, D h | T m, D h | T n, D c |
+------------------+----------+----------+----------+
| Total memory use | 2.13     | 2.20     | 2.20     |
| Huge pages       | 0        | 1.3      | 1.27     |
+------------------+----------+----------+----------+

Performance in TPS (Transactions Per Second). Higher is better.

T n, D h: 18225.58
T m, D h 18252.93
T n, D c: 18270.21

Performance counter

I got the number of L1 D/I TLB accesses and the number a D/I TLB accesses that triggered a page walk. I divided the second by the first to get the percentage of page walkes per TLB access. The lower the better.

+---------------+--------------+--------------+--------------+
|               | T n, D h     | T m, D h     | T n, D c     |
+---------------+--------------+--------------+--------------+
| L1 DTLB       | 127248242753 | 125431020479 | 125327001821 |
| L1 ITLB       | 80332558619  | 79346759071  | 79298139590  |
| DTLB walk     | 75011087     | 52800418     | 55895794     |
| ITLB walk     | 71577076     | 71505137     | 67262140     |
| DTLB % misses | 0.058948623  | 0.042095183  | 0.044599961  |
| ITLB % misses | 0.089100954  | 0.090117275  | 0.084821839  |
+---------------+--------------+--------------+--------------+

Masim
=====

I used masim with the "demo" configuration, but changing the times to 100 seconds for the initial phase and 50 seconds for the rest of the phases.

Memory consumption:

+------------------+----------+----------+----------+
|                  | T n, D h | T m, D h | T n, D c |
+------------------+----------+----------+----------+
| Total memory use | 2.38 GB  | 2.36 GB  | 2.37 GB  |
| Huge pages       | 0        | 190 MB   | 188 MB   |
+------------------+----------+----------+----------+

Performance:

THP never, DAMOS_HUGEPAGE

initial phase:                40,491 accesses/msec, 100001 msecs run
low phase 0:                  39,658 accesses/msec, 50002 msecs run
high phase 0:                 41,678 accesses/msec, 50000 msecs run
low phase 1:                  39,625 accesses/msec, 50003 msecs run
high phase 1:                 41,658 accesses/msec, 50002 msecs run
low phase 2:                  39,642 accesses/msec, 50002 msecs run
high phase 2:                 41,640 accesses/msec, 50001 msecs run

THP madvise, DAMOS_HUGEPAGE

initial phase:                51,977 accesses/msec, 100000 msecs run
low phase 0:                  86,953 accesses/msec, 50000 msecs run
high phase 0:                 94,812 accesses/msec, 50000 msecs run
low phase 1:                 101,017 accesses/msec, 50000 msecs run
high phase 1:                 94,841 accesses/msec, 50000 msecs run
low phase 2:                 100,993 accesses/msec, 50000 msecs run
high phase 2:                 94,791 accesses/msec, 50001 msecs run

THP never, DAMOS_COLLAPSE

initial phase:                93,678 accesses/msec, 100001 msecs run
low phase 0:                 101,475 accesses/msec, 50000 msecs run
high phase 0:                 98,589 accesses/msec, 50000 msecs run
low phase 1:                 101,531 accesses/msec, 50001 msecs run
high phase 1:                 98,506 accesses/msec, 50001 msecs run
low phase 2:                 101,458 accesses/msec, 50001 msecs run
high phase 2:                 98,555 accesses/msec, 50000 msecs run

Memory consumption dynamic (how quickly collapses occur):

It shows in seconds how many huge pages are allocated.

+----+----------+----------+
|    | T m, D h | T n, D c |
+----+----------+----------+
| 5  | 32       | 188      |
| 10 | 48       | 188      |
| 15 | 64       | 188      |
| 20 | 96       | 188      |
| 30 | 112      | 188      |
| 35 | 144      | 188      |
| 40 | 160      | 188      |
| 45 | 190      | 188      |
| 50 | 190      | 188      |
| 55 | 190      | 188      |
| 60 | 190      | 188      |
+----+----------+----------+

=========

- We can see that DAMOS "hugepage" action works only when THP is set to madvise. "collapse" action works even when THP is set to never.
- Performance for "collapse" action is slightly lower than "hugepage" action and THP madvise. This is due to the fact that collapases occur synchronously. With "hugepage" they may occur during page faults.
- Memory consumption is slighly lower for "collapse" than "hugepage" with THP madvise. This is due to the khugepage collapses all VMAs, while "collapse" action only collapses the VMAs in the hot region.
- There is an improvement in TLB utilization when collapse through "hugepage" or "collapse" actions are triggered. The amount of TLB misses is lower.
- "collapse" action is performance synchronously, which means that page collapses happen earlier and more rapidly. This can be useful or not, depending on the scenario.
- "hugepage" action may trigger a VMA split in some scenarios, since it needs to change the flag of the VMA to THP enabled. This may lead to additional overhead.

Collapse action just adds a new option to chose the correct system balance.

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

---
Changes from v2:
- v2: https://lore.kernel.org/[email protected]
- Add Reviewed-by: from SJ
- Wordsmith changelog
- Rebase to latest mm-new
Chagens from v1:
- v1: https://lore.kernel.org/[email protected]/
- Added masim benchmark
- Added performance benchmark for MariaDB
Changes from RFC v2:
- RFC v2: https://lore.kernel.org/[email protected]/
- Fixed a missing comma in the selftest python stript
- Added performance benchmarks
Changes from RFC:
- RFC: https://lore.kernel.org/[email protected]
- Added benchmarks
- Added damos_filter_type documentation for new action to fix kernel-doc

 Documentation/mm/damon/design.rst      |  4 ++++
 include/linux/damon.h                  |  2 ++
 mm/damon/sysfs-schemes.c               |  4 ++++
 mm/damon/vaddr.c                       |  3 +++
 tools/testing/selftests/damon/sysfs.py | 11 ++++++-----
 5 files changed, 19 insertions(+), 5 deletions(-)
Documentation/mm/damon/design.rst +4 -0
index afc7d52bda2f7..365e068a8dbe4 100644
@@ -474,6 +474,10 @@ that supports each action are as below.
    Supported by ``vaddr`` and ``fvaddr`` operations set. When
    TRANSPARENT_HUGEPAGE is disabled, the application of the action will just
    fail.
+ - ``collapse``: Call ``madvise()`` for the region with ``MADV_COLLAPSE``.
+   Supported by ``vaddr`` and ``fvaddr`` operations set. When
+   TRANSPARENT_HUGEPAGE is disabled, the application of the action will just
+   fail.
  - ``lru_prio``: Prioritize the region on its LRU lists.
    Supported by ``paddr`` operations set.
  - ``lru_deprio``: Deprioritize the region on its LRU lists.
include/linux/damon.h +2 -0
index f2cdb7c3f5e6c..ac71b845bb6e8 100644
@@ -121,6 +121,7 @@ struct damon_target {
  * @DAMOS_PAGEOUT:	Reclaim the region.
  * @DAMOS_HUGEPAGE:	Call ``madvise()`` for the region with MADV_HUGEPAGE.
  * @DAMOS_NOHUGEPAGE:	Call ``madvise()`` for the region with MADV_NOHUGEPAGE.
+ * @DAMOS_COLLAPSE:	Call ``madvise()`` for the region with MADV_COLLAPSE.
  * @DAMOS_LRU_PRIO:	Prioritize the region on its LRU lists.
  * @DAMOS_LRU_DEPRIO:	Deprioritize the region on its LRU lists.
  * @DAMOS_MIGRATE_HOT:  Migrate the regions prioritizing warmer regions.
@@ -140,6 +141,7 @@ enum damos_action {
 	DAMOS_PAGEOUT,
 	DAMOS_HUGEPAGE,
 	DAMOS_NOHUGEPAGE,
+	DAMOS_COLLAPSE,
 	DAMOS_LRU_PRIO,
 	DAMOS_LRU_DEPRIO,
 	DAMOS_MIGRATE_HOT,
mm/damon/sysfs-schemes.c +4 -0
index 04746cbb33272..c14a564dd8218 100644
@@ -2061,6 +2061,10 @@ static struct damos_sysfs_action_name damos_sysfs_action_names[] = {
 		.action = DAMOS_NOHUGEPAGE,
 		.name = "nohugepage",
 	},
+	{
+		.action = DAMOS_COLLAPSE,
+		.name = "collapse",
+	},
 	{
 		.action = DAMOS_LRU_PRIO,
 		.name = "lru_prio",
mm/damon/vaddr.c +3 -0
index b069dbc7e3d25..dd5f2d7027ac4 100644
@@ -903,6 +903,9 @@ static unsigned long damon_va_apply_scheme(struct damon_ctx *ctx,
 	case DAMOS_NOHUGEPAGE:
 		madv_action = MADV_NOHUGEPAGE;
 		break;
+	case DAMOS_COLLAPSE:
+		madv_action = MADV_COLLAPSE;
+		break;
 	case DAMOS_MIGRATE_HOT:
 	case DAMOS_MIGRATE_COLD:
 		return damos_va_migrate(t, r, scheme, sz_filter_passed);
tools/testing/selftests/damon/sysfs.py +6 -5
index 3aa5c91548a53..72f53180c6a88 100755
@@ -123,11 +123,12 @@ def assert_scheme_committed(scheme, dump):
             'pageout': 2,
             'hugepage': 3,
             'nohugeapge': 4,
-            'lru_prio': 5,
-            'lru_deprio': 6,
-            'migrate_hot': 7,
-            'migrate_cold': 8,
-            'stat': 9,
+            'collapse': 5,
+            'lru_prio': 6,
+            'lru_deprio': 7,
+            'migrate_hot': 8,
+            'migrate_cold': 9,
+            'stat': 10,
             }
     assert_true(dump['action'] == action_val[scheme.action], 'action', dump)
     assert_true(dump['apply_interval_us'] == scheme. apply_interval_us,
Signature · 1 line
2.47.3
author maintainer Reviewed-by on v2 4/4
#13 lore raw

Re: [PATCH v2 4/4] mm/damon: support MADV_COLLAPSE via DAMOS_COLLAPSE scheme action

Quoted 3 lines · On Sun, 26 Apr 2026 16:16:17 -0700 SeongJae Park <[email protected]> wrote:

From: Asier Gutierrez <[email protected]>

This patch set introces a new action: DAMOS_COLLAPSE.

[...]

Quoted 2 lines

I forgot adding my Reviewed-by: tag here.

Andrew, could you please add the above Reviewed-by: for me?

Thanks,
SJ

[...]