Skip to contentSkip to conversation

mm/damon: introduce damos quota goal target metric complement flag

PATCH Applied SJ Park 3 participants 22 messages Sep 29

Subsystems (5)
lore
Share & tools
lore

SJ Park

author maintainer
#0 lore raw

cover mm/damon: introduce damos quota goal target metric complement flag

Aim-oriented DAMOS quota auto-tuning assumes the aggressiveness of the scheme (quota) and the quota goal metric are directly proportional. Depending on the scheme setup and usage, keeping the relationship can be challenging.

For example, let's suppose a memory tiering approach for higher upper tier utilization. One common idea for that (TPP) is utilizing two schemes, one for promotion and the other one for demotion. The promotion scheme migrates hot pages from lower tier to upper tier, aiming for high utilization of the upper tier. The demotion scheme migrates cold pages from upper tier to lower tier, aiming for head room free memory of the upper tier. Both schemes and their goals are in direct proportion. However, for this kind of use case, we need to implement two different goal metrics (per-node memory utilization and free memory ratio) while essentially the free memory ratio is just a complemented value of the utilization.

To avoid adding too many new metrics, we are adding metrics that turn out to be really needed for each found use case. For example, some_mem_psi_us, node_eligible_mem_bp and hugepage_mem_bp don't have their complemented value version. But it is not really difficult to expect use cases that their complemented version can be useful. For example, hugepage_mem_bp use case may need a way to reduce the hugepage ratio. That would require a complemented version of hugepage_mem_bp. Adding a new metric for each of such use cases could make the number of metrics unnecessarily high, and discourage flexible usages of DAMOS.

Add a new flag, quota goal complement, to allow flexible tuning goal setup without unnecessarily increasing the number of metrics. The flag specifies whether to use the complemented value of the given goal metric for the tuning. For example, if the complement flag is set, the upper tier memory utilization ratio metric works the same as the free memory ratio metric for the tier.

Patches Sequence
================

Patch 1 introduces the quota goal complement flag API and implements its behavior. Patch 2 updates damos_new_quota_goal() to receive the flag. Patch 3 update DAMON sysfs interface to support the complement flag. Patches 4 and 5 extend existing kunit and kselftest to cover the new flag. Patches 6-8 update design, usage and ABI documents for the new feature.

Note: Below is additional information that is not intended to be added to the final commit message.

Test
====

I tested this feature using the latest version of DAMON user-space tool, damo [1], as below.

On an idle system having 10 GiB single node memory (node id 0), fill up 80% of memory with page cache.

    $ dd if=/dev/zero of=foo bs=1M count=$((1024*8))

Run DAMOS with pageout action for 80% of free memory, but using node_mem_used_bp metric like below.

    $ sudo ./damo start --damos_action pageout \
            --damos_quota_space 100M --damos_quota_interval 1s \
            --damos_quota_fail_charge_ratio 1 4096 \
            --damos_quota_goal_tuner temporal \
            --damos_quota_goal node_mem_used_bp 20% 0

Because the system has more than 20% node 0 memory utilization, DAMOS does nothing, as expected.

Run DAMOS with pageout action for 80% of free memory, using node_mem_used_bp metrics with complement flag like below.

    $ sudo ./damo start --damos_action pageout \
            --damos_quota_space 100M --damos_quota_interval 1s \
            --damos_quota_fail_charge_ratio 1 4096 \
            --damos_quota_goal_tuner temporal \
            --damos_quota_goal node_mem_used_bp 80% complement 0

Because the complement flag is given, it is effectively the same as node_mem_freepbp metrics with a target of 80%. DAMOS reclaims the page cache ~100 MiB per second, until the free memory ratio (complemented memory utilization ratio) reaches the goal, 80%.

Changes from RFC v5
- RFC v5: https://lore.kernel.org/[email protected]
- Rebase to latest mm-new.
- Drop RFC tag.
Changes from RFC v4
- RFC v4: https://lore.kernel.org/[email protected]
- Fix a typo in test code (s/complementfalse/complement/).
- Extend sysfs.sh DAMON selftest for complement flag sysfs file. Changes from RFC v3
- RFC v3: https://lore.kernel.org/[email protected]
- Rebase to the latest mm-new.
- Add tests done to the cover letter.
Changes from RFC v2
- RFC v2: https://lore.kernel.org/[email protected]
- Avoid quota goal value underflow for some_mem_psi.
- Handle complement flag for psi goal with unset last_psi_total.
- Pass complement argument to damos_new_quota_goal() from core and core-kunit.
- Explicitly document no-op complement behavior for user_input. Changes from RFC
- RFC: https://lore.kernel.org/[email protected]
- Avoid quota goal value underflow.
- Fix usage doc for number of files in each goal directory.
- Rebase to the latest mm-new.

[1] https://github.com/damonitor/damo

SJ Park (8):
mm/damon/core: introduce damos_quota_goal->complement mm/damon/core: add complement argument to damos_new_quota_goal() mm/damon/sysfs-schemes: support quota goal complement flag mm/damon/tests/core-kunit: test quota_goal->complement commit selftests/damon/sysfs.sh: test quota goal complement flag file Docs/mm/damon/design: document damos quota goal complement flag Docs/admin-guide/mm/damon/usage: update for quota goal complement file Docs/ABI/damon: update for quota goal metric complement sysfs file

 .../ABI/testing/sysfs-kernel-mm-damon         |  6 ++++
 Documentation/admin-guide/mm/damon/usage.rst  | 15 +++++----
 Documentation/mm/damon/design.rst             | 15 ++++++---
 include/linux/damon.h                         |  4 ++-
 mm/damon/core.c                               | 33 ++++++++++++++++---
 mm/damon/lru_sort.c                           |  5 +--
 mm/damon/reclaim.c                            |  5 +--
 mm/damon/sysfs-schemes.c                      | 30 +++++++++++++++++
 mm/damon/tests/core-kunit.h                   |  5 ++-
 samples/damon/mtier.c                         |  2 +-
 tools/testing/selftests/damon/sysfs.sh        |  3 ++
 11 files changed, 101 insertions(+), 22 deletions(-)

base-commit: af86bf5500fcc53d591e1000e6e08dec1227fb06

Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/8 mm/damon/core: introduce damos_quota_goal->complement

Introduce damos_quota_goal->complement for specifying whether to use a complemented value of the given goal target metric. Add the field to the data structure and implement essential core support. Handle the flag in the quota goal commit and current quota goal metric value retrieval.

---
 include/linux/damon.h |  2 ++
 mm/damon/core.c       | 26 +++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
include/linux/damon.h +2 -0
index 6a29dc2ac8db..42234839ce29 100644
@@ -169,6 +169,7 @@ enum damos_quota_goal_metric {
 /**
  * struct damos_quota_goal - DAMOS scheme quota auto-tuning goal.
  * @metric:		Metric to be used for representing the goal.
+ * @complement:		Use the complement of the metric.
  * @target_value:	Target value of @metric to achieve with the tuning.
  * @current_value:	Current value of @metric.
  * @nid:		Node id.
@@ -191,6 +192,7 @@ enum damos_quota_goal_metric {
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
+	bool complement;
 	unsigned long target_value;
 	unsigned long current_value;
 	/* metric-dependent fields */
mm/damon/core.c +25 -1
index 5ecbea5d71e1..1ab5154511e6 100644
@@ -1224,6 +1224,7 @@ static int damos_commit_quota_goal(
 	if (!src->target_value)
 		return  -EINVAL;
 	dst->metric = src->metric;
+	dst->complement = src->complement;
 	dst->target_value = src->target_value;
 	if (dst->metric == DAMOS_QUOTA_USER_INPUT)
 		dst->current_value = src->current_value;
@@ -2960,10 +2961,18 @@ static void damos_set_psi_current_val(u64 now_psi_total,
 		struct damos_quota_goal *goal, struct damos *s)
 {
 	u64 last_psi_total = goal->last_psi_total;
+	unsigned long val;
 
 	goal->last_psi_total = now_psi_total;
 	if (last_psi_total != U64_MAX) {
-		goal->current_value = now_psi_total - last_psi_total;
+		val = now_psi_total - last_psi_total;
+		if (goal->complement) {
+			if (val < s->quota.reset_interval * 1000)
+				val = s->quota.reset_interval * 1000 - val;
+			else
+				val = 0;
+		}
+		goal->current_value = val;
 		return;
 	}
 	/* uninitialized last_psi_total; make no effect this round */
@@ -3255,6 +3264,21 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
 	default:
 		break;
 	}
+	if (!goal->complement)
+		return;
+
+	/* updte current_value to complemented value */
+
+	/* for user_input, users set complemented value on their own */
+	if (goal->metric == DAMOS_QUOTA_USER_INPUT)
+		return;
+	/* damos_set_psi_current_val() handles complement flag itself */
+	if (goal->metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
+		return;
+	if (goal->current_value < 10000)
+		goal->current_value = 10000 - goal->current_value;
+	else
+		goal->current_value = 0;
 }
 
 /* Return the highest score since it makes schemes least aggressive */
Signature · 1 line
2.47.3
#2 lore raw

Re: [PATCH 1/8] mm/damon/core: introduce damos_quota_goal->complement

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] damon: integer overflow in psi complement calculation on 32-bit --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] damon: integer overflow in psi complement calculation on 32-bit
--

commit 194cde60eb24e43a85afddadebba1bd09c82f1e0
Author: SJ Park <[email protected]>

mm/damon/core: introduce damos_quota_goal->complement

This commit introduces the complement flag to the damos_quota_goal structure to specify whether to use a complemented value of the goal target metric. It also implements the core logic to handle this flag during quota goal commit and metric retrieval.

Quoted 3 lines
mm/damon/core.c +0 -0

[ ... ]

mm/damon/core.c static void damos_set_psi_current_val(u64 now_psi_total, · line 2971

Quoted 13 lines
mm/damon/core.c +5 -1
@@ -2960,10 +2961,18 @@ static void damos_set_psi_current_val(u64 now_psi_total,
 		struct damos_quota_goal *goal, struct damos *s)
 {
 	u64 last_psi_total = goal->last_psi_total;
+	unsigned long val;
 
 	goal->last_psi_total = now_psi_total;
 	if (last_psi_total != U64_MAX) {
-		goal->current_value = now_psi_total - last_psi_total;
+		val = now_psi_total - last_psi_total;
+		if (goal->complement) {
+			if (val < s->quota.reset_interval * 1000)
+				val = s->quota.reset_interval * 1000 - val;

[Severity: Low]
Does this calculation overflow on 32-bit systems?

On 32-bit architectures, s->quota.reset_interval is a 32-bit unsigned long. Multiplying it by the integer literal 1000 will overflow before the comparison and subtraction if the user configures a reset_interval_ms greater than 4,294,967 (around 71.5 minutes).

While such a high interval is atypical for DAMON's continuous monitoring, it is technically reachable via sysfs since the sysfs store function does not bound the value beyond the maximum unsigned long value.

Quoted 6 lines
mm/damon/core.c +4 -0
+			else
+				val = 0;
+		}
+		goal->current_value = val;
 		return;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCH 1/8] mm/damon/core: introduce damos_quota_goal->complement

Quoted 35 lines · On Tue, 29 Sep 2026 08:12:39 +0000 [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] damon: integer overflow in psi complement calculation on 32-bit
--

commit 194cde60eb24e43a85afddadebba1bd09c82f1e0
Author: SJ Park <[email protected]>

mm/damon/core: introduce damos_quota_goal->complement

This commit introduces the complement flag to the damos_quota_goal structure to specify whether to use a complemented value of the goal target metric. It also implements the core logic to handle this flag during quota goal commit and metric retrieval.

Quoted 3 lines
mm/damon/core.c +0 -0

[ ... ]

mm/damon/core.c static void damos_set_psi_current_val(u64 now_psi_total, · line 2971

Quoted 13 lines
mm/damon/core.c +5 -1
@@ -2960,10 +2961,18 @@ static void damos_set_psi_current_val(u64 now_psi_total,
 		struct damos_quota_goal *goal, struct damos *s)
 {
 	u64 last_psi_total = goal->last_psi_total;
+	unsigned long val;
 
 	goal->last_psi_total = now_psi_total;
 	if (last_psi_total != U64_MAX) {
-		goal->current_value = now_psi_total - last_psi_total;
+		val = now_psi_total - last_psi_total;
+		if (goal->complement) {
+			if (val < s->quota.reset_interval * 1000)
+				val = s->quota.reset_interval * 1000 - val;

[Severity: Low]
Does this calculation overflow on 32-bit systems?

Quoted 8 lines

On 32-bit architectures, s->quota.reset_interval is a 32-bit unsigned long. Multiplying it by the integer literal 1000 will overflow before the comparison and subtraction if the user configures a reset_interval_ms greater than 4,294,967 (around 71.5 minutes).

While such a high interval is atypical for DAMON's continuous monitoring, it is technically reachable via sysfs since the sysfs store function does not bound the value beyond the maximum unsigned long value.

Yes, it is atypical and extreme setup. Sane users ain't use such setup without testing. I think it wouldn't be too late to revisit this after a report from a user is arrived. I will also consider making a better handling of the corner case. But not a blocker of this series.

Quoted 11 lines
Quoted 7 lines
mm/damon/core.c +4 -0
+			else
+				val = 0;
+		}
+		goal->current_value = val;
 		return;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

Thanks,
SJ

#4 lore raw

Re: [PATCH 1/8] mm/damon/core: introduce damos_quota_goal->complement

In reply to SJ Park

Hi SJ,

Quoted 65 lines · SJ Park

Introduce damos_quota_goal->complement for specifying whether to use a complemented value of the given goal target metric. Add the field to the data structure and implement essential core support. Handle the flag in the quota goal commit and current quota goal metric value retrieval.

---
 include/linux/damon.h |  2 ++
 mm/damon/core.c       | 26 +++++++++++++++++++++++++-
 2 files changed, 27 insertions(+), 1 deletion(-)
include/linux/damon.h +2 -0
index 6a29dc2ac8db..42234839ce29 100644
@@ -169,6 +169,7 @@ enum damos_quota_goal_metric {
 /**
  * struct damos_quota_goal - DAMOS scheme quota auto-tuning goal.
  * @metric:		Metric to be used for representing the goal.
+ * @complement:		Use the complement of the metric.
  * @target_value:	Target value of @metric to achieve with the tuning.
  * @current_value:	Current value of @metric.
  * @nid:		Node id.
@@ -191,6 +192,7 @@ enum damos_quota_goal_metric {
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
+	bool complement;
 	unsigned long target_value;
 	unsigned long current_value;
 	/* metric-dependent fields */
mm/damon/core.c +10 -1
index 5ecbea5d71e1..1ab5154511e6 100644
@@ -1224,6 +1224,7 @@ static int damos_commit_quota_goal(
 	if (!src->target_value)
 		return  -EINVAL;
 	dst->metric = src->metric;
+	dst->complement = src->complement;
 	dst->target_value = src->target_value;
 	if (dst->metric == DAMOS_QUOTA_USER_INPUT)
 		dst->current_value = src->current_value;
@@ -2960,10 +2961,18 @@ static void damos_set_psi_current_val(u64 now_psi_total,
 		struct damos_quota_goal *goal, struct damos *s)
 {
 	u64 last_psi_total = goal->last_psi_total;
+	unsigned long val;
 
 	goal->last_psi_total = now_psi_total;
 	if (last_psi_total != U64_MAX) {
-		goal->current_value = now_psi_total - last_psi_total;
+		val = now_psi_total - last_psi_total;
+		if (goal->complement) {
+			if (val < s->quota.reset_interval * 1000)
+				val = s->quota.reset_interval * 1000 - val;
+			else
+				val = 0;
+		}
+		goal->current_value = val;
 		return;
 	}
 	/* uninitialized last_psi_total; make no effect this round */

mm/damon/core.c static void damos_set_quota_goal_current_value(struct damon_ctx *c, · line 3270

Quoted 7 lines
mm/damon/core.c +4 -0
@@ -3255,6 +3264,21 @@ static void damos_set_quota_goal_current_value(struct damon_ctx *c,
 	default:
 		break;
 	}
+	if (!goal->complement)
+		return;
+
+	/* updte current_value to complemented value */

There is a typo there. It should be "update". Also, what is the purpose of this comment? It doesn't explain anything, which may not be obvious.> +

Quoted 13 lines
mm/damon/core.c +10 -0
+	/* for user_input, users set complemented value on their own */
+	if (goal->metric == DAMOS_QUOTA_USER_INPUT)
+		return;
+	/* damos_set_psi_current_val() handles complement flag itself */
+	if (goal->metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
+		return;
+	if (goal->current_value < 10000)
+		goal->current_value = 10000 - goal->current_value;
+	else
+		goal->current_value = 0;
 }
 
 /* Return the highest score since it makes schemes least aggressive */
Signature · 2 lines
Asier Gutierrez
Huawei
author maintainer
#5 lore raw

Re: [PATCH 1/8] mm/damon/core: introduce damos_quota_goal->complement

Quoted 3 lines · On Tue, 29 Sep 2026 13:24:20 +0300 Gutierrez Asier…

Hi SJ,

On 9/29/2026 11:01 AM, SJ Park wrote:

[...]

Quoted 2 lines

mm/damon/core.c static void damos_set_quota_goal_current_value(struct damon_ctx *c, · line 3270

Quoted 1 line
mm/damon/core.c +1 -0
+	/* updte current_value to complemented value */

There is a typo there. It should be "update".

Good catch, thank you. Andrew, could you please fix it up when you take this? Let me know if you prefer reposting.

Quoted 3 lines

Also, what is the
purpose of this comment? It doesn't explain anything, which may not be obvious.> +

The purpose is explaining the code below does what the comment says.

Thanks,
SJ

[...]

author maintainer
#6 lore raw

PATCH 2/8 mm/damon/core: add complement argument to damos_new_quota_goal()

In reply to SJ Park

damos_quota_goal->complement needs to be manually set by each API callers. It is easy to make mistakes. Extend the quota goal constructor, damos_new_quota_goal() to receive and set the complement flag value. Also update all callers to use the new signature.

---
 include/linux/damon.h       | 2 +-
 mm/damon/core.c             | 7 ++++---
 mm/damon/lru_sort.c         | 5 +++--
 mm/damon/reclaim.c          | 5 +++--
 mm/damon/sysfs-schemes.c    | 2 +-
 mm/damon/tests/core-kunit.h | 3 ++-
 samples/damon/mtier.c       | 2 +-
 7 files changed, 15 insertions(+), 11 deletions(-)
include/linux/damon.h +1 -1
index 42234839ce29..63050eb2206a 100644
@@ -1090,7 +1090,7 @@ bool damos_filter_for_ops(enum damos_filter_type type);
 void damos_destroy_filter(struct damos_filter *f);
 
 struct damos_quota_goal *damos_new_quota_goal(
-		enum damos_quota_goal_metric metric,
+		enum damos_quota_goal_metric metric, bool complement,
 		unsigned long target_value);
 void damos_add_quota_goal(struct damos_quota *q, struct damos_quota_goal *g);
 void damos_destroy_quota_goal(struct damos_quota_goal *goal);
mm/damon/core.c +4 -3
index 1ab5154511e6..86fee9ca03a4 100644
@@ -691,7 +691,7 @@ void damos_destroy_filter(struct damos_filter *f)
 }
 
 struct damos_quota_goal *damos_new_quota_goal(
-		enum damos_quota_goal_metric metric,
+		enum damos_quota_goal_metric metric, bool complement,
 		unsigned long target_value)
 {
 	struct damos_quota_goal *goal;
@@ -700,6 +700,7 @@ struct damos_quota_goal *damos_new_quota_goal(
 	if (!goal)
 		return NULL;
 	goal->metric = metric;
+	goal->complement = complement;
 	goal->target_value = target_value;
 	if (metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
 		goal->last_psi_total = U64_MAX;
@@ -1262,8 +1263,8 @@ int damos_commit_quota_goals(struct damos_quota *dst, struct damos_quota *src)
 	damos_for_each_quota_goal_safe(src_goal, next, src) {
 		if (j++ < i)
 			continue;
-		new_goal = damos_new_quota_goal(
-				src_goal->metric, src_goal->target_value);
+		new_goal = damos_new_quota_goal(src_goal->metric,
+				src_goal->complement, src_goal->target_value);
 		if (!new_goal)
 			return -ENOMEM;
 		err = damos_commit_quota_goal(new_goal, src_goal);
mm/damon/lru_sort.c +3 -2
index 273efa3c913e..64e086985eb5 100644
@@ -233,12 +233,13 @@ static int damon_lru_sort_add_quota_goals(struct damos *hot_scheme,
 
 	if (!active_mem_bp)
 		return 0;
-	goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, active_mem_bp);
+	goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, false,
+			active_mem_bp);
 	if (!goal)
 		return -ENOMEM;
 	damos_add_quota_goal(&hot_scheme->quota, goal);
 	/* aim 0.2 % goal conflict, to keep little ping pong */
-	goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP,
+	goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP, false,
 			10000 - active_mem_bp + 2);
 	if (!goal)
 		return -ENOMEM;
mm/damon/reclaim.c +3 -2
index 42a2c9cb1343..014b0779ea6d 100644
@@ -233,7 +233,7 @@ static int damon_reclaim_apply_parameters(void)
 	damon_set_schemes(param_ctx, &scheme, 1);
 
 	if (quota_mem_pressure_us) {
-		goal = damos_new_quota_goal(DAMOS_QUOTA_SOME_MEM_PSI_US,
+		goal = damos_new_quota_goal(DAMOS_QUOTA_SOME_MEM_PSI_US, false,
 				quota_mem_pressure_us);
 		if (!goal)
 			goto out;
@@ -241,7 +241,8 @@ static int damon_reclaim_apply_parameters(void)
 	}
 
 	if (quota_autotune_feedback) {
-		goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, 10000);
+		goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, false,
+				10000);
 		if (!goal)
 			goto out;
 		goal->current_value = quota_autotune_feedback;
mm/damon/sysfs-schemes.c +1 -1
index bfb6f0bc3f21..06af417bc9a2 100644
@@ -2869,7 +2869,7 @@ static int damos_sysfs_add_quota_score(
 		if (!sysfs_goal->target_value)
 			continue;
 
-		goal = damos_new_quota_goal(sysfs_goal->metric,
+		goal = damos_new_quota_goal(sysfs_goal->metric, false,
 				sysfs_goal->target_value);
 		if (!goal)
 			return -ENOMEM;
mm/damon/tests/core-kunit.h +2 -1
index 2111faa58153..5a924edb171f 100644
@@ -1014,6 +1014,7 @@ static void damos_test_commit_quota_goals_for(struct kunit *test,
 		 * Make it kfree()-able.
 		 */
 		goal = damos_new_quota_goal(dst_goals[i].metric,
+				dst_goals[i].complement,
 				dst_goals[i].target_value);
 		if (!goal)
 			goto out;
@@ -2411,7 +2412,7 @@ static void damos_test_esz_goal_temporal(struct kunit *test)
 	}
 	damon_add_scheme(ctx, s);
 
-	goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, 10000);
+	goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, false, 10000);
 	if (!goal) {
 		damon_destroy_ctx(ctx);
 		kunit_skip(test, "quota goal alloc fail");
samples/damon/mtier.c +1 -1
index 27dc88bdf7a0..a2e311082cd4 100644
@@ -163,7 +163,7 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
 	damon_set_schemes(ctx, &scheme, 1);
 	quota_goal = damos_new_quota_goal(
 			promote ? DAMOS_QUOTA_NODE_MEM_USED_BP :
-			DAMOS_QUOTA_NODE_MEM_FREE_BP,
+			DAMOS_QUOTA_NODE_MEM_FREE_BP, false,
 			promote ? node0_mem_used_bp : node0_mem_free_bp);
 	if (!quota_goal)
 		goto free_out;
Signature · 1 line
2.47.3
#7 lore raw

Re: [PATCH 2/8] mm/damon/core: add complement argument to damos_new_quota_goal()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

damos_quota_goal->complement needs to be manually set by each API callers. It is easy to make mistakes. Extend the quota goal constructor, damos_new_quota_goal() to receive and set the complement flag value. Also update all callers to use the new signature.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Reviewed-by on v1 2/8
#8 lore raw

Re: [PATCH 2/8] mm/damon/core: add complement argument to damos_new_quota_goal()

In reply to SJ Park
Quoted 143 lines · SJ Park

damos_quota_goal->complement needs to be manually set by each API callers. It is easy to make mistakes. Extend the quota goal constructor, damos_new_quota_goal() to receive and set the complement flag value. Also update all callers to use the new signature.

---
 include/linux/damon.h       | 2 +-
 mm/damon/core.c             | 7 ++++---
 mm/damon/lru_sort.c         | 5 +++--
 mm/damon/reclaim.c          | 5 +++--
 mm/damon/sysfs-schemes.c    | 2 +-
 mm/damon/tests/core-kunit.h | 3 ++-
 samples/damon/mtier.c       | 2 +-
 7 files changed, 15 insertions(+), 11 deletions(-)
include/linux/damon.h +1 -1
index 42234839ce29..63050eb2206a 100644
@@ -1090,7 +1090,7 @@ bool damos_filter_for_ops(enum damos_filter_type type);
 void damos_destroy_filter(struct damos_filter *f);
 
 struct damos_quota_goal *damos_new_quota_goal(
-		enum damos_quota_goal_metric metric,
+		enum damos_quota_goal_metric metric, bool complement,
 		unsigned long target_value);
 void damos_add_quota_goal(struct damos_quota *q, struct damos_quota_goal *g);
 void damos_destroy_quota_goal(struct damos_quota_goal *goal);
mm/damon/core.c +4 -3
index 1ab5154511e6..86fee9ca03a4 100644
@@ -691,7 +691,7 @@ void damos_destroy_filter(struct damos_filter *f)
 }
 
 struct damos_quota_goal *damos_new_quota_goal(
-		enum damos_quota_goal_metric metric,
+		enum damos_quota_goal_metric metric, bool complement,
 		unsigned long target_value)
 {
 	struct damos_quota_goal *goal;
@@ -700,6 +700,7 @@ struct damos_quota_goal *damos_new_quota_goal(
 	if (!goal)
 		return NULL;
 	goal->metric = metric;
+	goal->complement = complement;
 	goal->target_value = target_value;
 	if (metric == DAMOS_QUOTA_SOME_MEM_PSI_US)
 		goal->last_psi_total = U64_MAX;
@@ -1262,8 +1263,8 @@ int damos_commit_quota_goals(struct damos_quota *dst, struct damos_quota *src)
 	damos_for_each_quota_goal_safe(src_goal, next, src) {
 		if (j++ < i)
 			continue;
-		new_goal = damos_new_quota_goal(
-				src_goal->metric, src_goal->target_value);
+		new_goal = damos_new_quota_goal(src_goal->metric,
+				src_goal->complement, src_goal->target_value);
 		if (!new_goal)
 			return -ENOMEM;
 		err = damos_commit_quota_goal(new_goal, src_goal);
mm/damon/lru_sort.c +3 -2
index 273efa3c913e..64e086985eb5 100644
@@ -233,12 +233,13 @@ static int damon_lru_sort_add_quota_goals(struct damos *hot_scheme,
 
 	if (!active_mem_bp)
 		return 0;
-	goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, active_mem_bp);
+	goal = damos_new_quota_goal(DAMOS_QUOTA_ACTIVE_MEM_BP, false,
+			active_mem_bp);
 	if (!goal)
 		return -ENOMEM;
 	damos_add_quota_goal(&hot_scheme->quota, goal);
 	/* aim 0.2 % goal conflict, to keep little ping pong */
-	goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP,
+	goal = damos_new_quota_goal(DAMOS_QUOTA_INACTIVE_MEM_BP, false,
 			10000 - active_mem_bp + 2);
 	if (!goal)
 		return -ENOMEM;
mm/damon/reclaim.c +3 -2
index 42a2c9cb1343..014b0779ea6d 100644
@@ -233,7 +233,7 @@ static int damon_reclaim_apply_parameters(void)
 	damon_set_schemes(param_ctx, &scheme, 1);
 
 	if (quota_mem_pressure_us) {
-		goal = damos_new_quota_goal(DAMOS_QUOTA_SOME_MEM_PSI_US,
+		goal = damos_new_quota_goal(DAMOS_QUOTA_SOME_MEM_PSI_US, false,
 				quota_mem_pressure_us);
 		if (!goal)
 			goto out;
@@ -241,7 +241,8 @@ static int damon_reclaim_apply_parameters(void)
 	}
 
 	if (quota_autotune_feedback) {
-		goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, 10000);
+		goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, false,
+				10000);
 		if (!goal)
 			goto out;
 		goal->current_value = quota_autotune_feedback;
mm/damon/sysfs-schemes.c +1 -1
index bfb6f0bc3f21..06af417bc9a2 100644
@@ -2869,7 +2869,7 @@ static int damos_sysfs_add_quota_score(
 		if (!sysfs_goal->target_value)
 			continue;
 
-		goal = damos_new_quota_goal(sysfs_goal->metric,
+		goal = damos_new_quota_goal(sysfs_goal->metric, false,
 				sysfs_goal->target_value);
 		if (!goal)
 			return -ENOMEM;
mm/damon/tests/core-kunit.h +2 -1
index 2111faa58153..5a924edb171f 100644
@@ -1014,6 +1014,7 @@ static void damos_test_commit_quota_goals_for(struct kunit *test,
 		 * Make it kfree()-able.
 		 */
 		goal = damos_new_quota_goal(dst_goals[i].metric,
+				dst_goals[i].complement,
 				dst_goals[i].target_value);
 		if (!goal)
 			goto out;
@@ -2411,7 +2412,7 @@ static void damos_test_esz_goal_temporal(struct kunit *test)
 	}
 	damon_add_scheme(ctx, s);
 
-	goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, 10000);
+	goal = damos_new_quota_goal(DAMOS_QUOTA_USER_INPUT, false, 10000);
 	if (!goal) {
 		damon_destroy_ctx(ctx);
 		kunit_skip(test, "quota goal alloc fail");
samples/damon/mtier.c +0 -0
index 27dc88bdf7a0..a2e311082cd4 100644

samples/damon/mtier.c static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote) · line 169

Quoted 8 lines
samples/damon/mtier.c +1 -1
@@ -163,7 +163,7 @@ static struct damon_ctx *damon_sample_mtier_build_ctx(bool promote)
 	damon_set_schemes(ctx, &scheme, 1);
 	quota_goal = damos_new_quota_goal(
 			promote ? DAMOS_QUOTA_NODE_MEM_USED_BP :
-			DAMOS_QUOTA_NODE_MEM_FREE_BP,
+			DAMOS_QUOTA_NODE_MEM_FREE_BP, false,
 			promote ? node0_mem_used_bp : node0_mem_free_bp);
 	if (!quota_goal)
 		goto free_out;

LGTM

Signature · 2 lines
Asier Gutierrez
Huawei
author maintainer
#9 lore raw

PATCH 3/8 mm/damon/sysfs-schemes: support quota goal complement flag

In reply to SJ Park

Add a new sysfs file, complement, under the quota goal directory. It works for setting and getting the quota goal metric complement flag value.

---
 mm/damon/sysfs-schemes.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)
mm/damon/sysfs-schemes.c +31 -1
index 06af417bc9a2..8f083611741f 100644
@@ -1220,6 +1220,7 @@ static const struct kobj_type damon_sysfs_watermarks_ktype = {
 struct damos_sysfs_quota_goal {
 	struct kobject kobj;
 	enum damos_quota_goal_metric metric;
+	bool complement;
 	unsigned long target_value;
 	unsigned long current_value;
 	int nid;
@@ -1316,6 +1317,30 @@ static ssize_t target_metric_store(struct kobject *kobj,
 	return -EINVAL;
 }
 
+static ssize_t complement_show(struct kobject *kobj,
+		struct kobj_attribute *attr, char *buf)
+{
+	struct damos_sysfs_quota_goal *goal = container_of(kobj,
+			struct damos_sysfs_quota_goal, kobj);
+
+	return sysfs_emit(buf, "%c\n", goal->complement ? 'Y' : 'N');
+}
+
+static ssize_t complement_store(struct kobject *kobj,
+		struct kobj_attribute *attr, const char *buf, size_t count)
+{
+	struct damos_sysfs_quota_goal *goal = container_of(kobj,
+			struct damos_sysfs_quota_goal, kobj);
+	bool complement;
+	int err = kstrtobool(buf, &complement);
+
+	if (err)
+		return err;
+
+	goal->complement = complement;
+	return count;
+}
+
 static ssize_t target_value_show(struct kobject *kobj,
 		struct kobj_attribute *attr, char *buf)
 {
@@ -1424,6 +1449,9 @@ static void damos_sysfs_quota_goal_release(struct kobject *kobj)
 static struct kobj_attribute damos_sysfs_quota_goal_target_metric_attr =
 		__ATTR_RW_MODE(target_metric, 0600);
 
+static struct kobj_attribute damos_sysfs_quota_goal_complement_attr =
+		__ATTR_RW_MODE(complement, 0600);
+
 static struct kobj_attribute damos_sysfs_quota_goal_target_value_attr =
 		__ATTR_RW_MODE(target_value, 0600);
 
@@ -1438,6 +1466,7 @@ static struct kobj_attribute damos_sysfs_quota_goal_path_attr =
 
 static struct attribute *damos_sysfs_quota_goal_attrs[] = {
 	&damos_sysfs_quota_goal_target_metric_attr.attr,
+	&damos_sysfs_quota_goal_complement_attr.attr,
 	&damos_sysfs_quota_goal_target_value_attr.attr,
 	&damos_sysfs_quota_goal_current_value_attr.attr,
 	&damos_sysfs_quota_goal_nid_attr.attr,
@@ -2869,7 +2898,8 @@ static int damos_sysfs_add_quota_score(
 		if (!sysfs_goal->target_value)
 			continue;
 
-		goal = damos_new_quota_goal(sysfs_goal->metric, false,
+		goal = damos_new_quota_goal(sysfs_goal->metric,
+				sysfs_goal->complement,
 				sysfs_goal->target_value);
 		if (!goal)
 			return -ENOMEM;
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 3/8] mm/damon/sysfs-schemes: support quota goal complement flag

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Add a new sysfs file, complement, under the quota goal directory. It works for setting and getting the quota goal metric complement flag value.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH 4/8 mm/damon/tests/core-kunit: test quota_goal->complement commit

In reply to SJ Park

Extend existing DAMOS quota goal commit unit test to test the complement flag. Set the source complement flag and confirm the destination is updated to the given input.

---
 mm/damon/tests/core-kunit.h | 2 ++
 1 file changed, 2 insertions(+)
mm/damon/tests/core-kunit.h +2 -0
index 5a924edb171f..ef146ca2ae8a 100644
@@ -861,6 +861,7 @@ static void damos_test_commit_quota_goal_for(struct kunit *test,
 	damos_commit_quota_goal(dst, src);
 
 	KUNIT_EXPECT_EQ(test, dst->metric, src->metric);
+	KUNIT_EXPECT_EQ(test, dst->complement, src->complement);
 	KUNIT_EXPECT_EQ(test, dst->target_value, src->target_value);
 	if (src->metric == DAMOS_QUOTA_USER_INPUT)
 		KUNIT_EXPECT_EQ(test, dst->current_value, src->current_value);
@@ -904,6 +905,7 @@ static void damos_test_commit_quota_goal(struct kunit *test)
 	damos_test_commit_quota_goal_for(test, &dst,
 			&(struct damos_quota_goal){
 			.metric = DAMOS_QUOTA_USER_INPUT,
+			.complement = true,
 			.target_value = 789,
 			.current_value = 12});
 	damos_test_commit_quota_goal_for(test, &dst,
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH 4/8] mm/damon/tests/core-kunit: test quota_goal->complement commit

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Extend existing DAMOS quota goal commit unit test to test the complement flag. Set the source complement flag and confirm the destination is updated to the given input.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH 5/8 selftests/damon/sysfs.sh: test quota goal complement flag file

In reply to SJ Park

Test the existence and the valid input acceptance of the newly added quota goal complement flag sysfs file.

---
 tools/testing/selftests/damon/sysfs.sh | 3 +++
 1 file changed, 3 insertions(+)
tools/testing/selftests/damon/sysfs.sh +3 -0
index b66593c9ac47..1cc7ab2d7e22 100755
@@ -212,6 +212,9 @@ test_goal()
 	ensure_write_succ "$fpath" "node_eligible_mem_bp" "valid input"
 	ensure_write_succ "$fpath" "hugepage_mem_bp" "valid input"
 	ensure_write_fail "$fpath" "foo" "invalid input"
+	ensure_file "$goal_dir/complement" "exist" "600"
+	ensure_write_succ "$goal_dir/complement" "Y" "valid input"
+	ensure_write_succ "$goal_dir/complement" "N" "valid input"
 	ensure_file "$goal_dir/nid" "exist" "600"
 	ensure_file "$goal_dir/path" "exist" "600"
 }
Signature · 1 line
2.47.3
#14 lore raw

Re: [PATCH 5/8] selftests/damon/sysfs.sh: test quota goal complement flag file

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Test the existence and the valid input acceptance of the newly added quota goal complement flag sysfs file.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH 6/8 Docs/mm/damon/design: document damos quota goal complement flag

In reply to SJ Park

Update DAMON design document for the newly added damos quota goal metric value complement flag.

---
 Documentation/mm/damon/design.rst | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
Documentation/mm/damon/design.rst +11 -4
index e82390e77a70..5cd651b1b7aa 100644
@@ -702,10 +702,10 @@ There are two such tuning algorithms that users can select as they need.
   that the zero quota is a valid quota, and therefore ``qt_exceeds`` :ref:`stat
   <damon_design_damos_stat>` will keep increasing in this case.
 
-The goal can be specified with five parameters, namely ``target_metric``,
-``target_value``, ``current_value``, ``nid`` and ``path``.  The auto-tuning
-mechanism tries to make ``current_value`` of ``target_metric`` be same to
-``target_value``.
+The goal can be specified with six parameters, namely ``target_metric``,
+``complement``, ``target_value``, ``current_value``, ``nid`` and ``path``.  The
+auto-tuning mechanism tries to make ``current_value`` of ``complement``-ed
+``target_metric`` be same to ``target_value``.
 
 - ``user_input``: User-provided value.  Users could use any metric that they
   has interest in for the value.  Use space main workload's latency or
@@ -733,6 +733,13 @@ mechanism tries to make ``current_value`` of ``target_metric`` be same to
 - ``hugepage_mem_bp``: Total huge page to total used memory ratio in bp
   (1/10,000).
 
+``complement`` is a boolean parameter that determines whether to use
+complemented value of the target metric.  For example, if ``complement`` is set
+and target metric is ``active_mem_bp``, it is effectively same to
+``inactive_mem_bp``.  ``complement`` is no-op when the target metric type is
+``user_input``.  For the metric type, the user should be able to emit
+complemented metric values on their own.
+
 ``nid`` is optionally required for ``node_mem_used_bp``, ``node_mem_free_bp``,
 ``node_memcg_used_bp``, ``node_memcg_free_bp`` and ``node_eligible_mem_bp`` to
 point the specific NUMA node.
Signature · 1 line
2.47.3
#16 lore raw

Re: [PATCH 6/8] Docs/mm/damon/design: document damos quota goal complement flag

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Update DAMON design document for the newly added damos quota goal metric value complement flag.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH 7/8 Docs/admin-guide/mm/damon/usage: update for quota goal complement file

In reply to SJ Park

Update DAMON usage document for the newly added quota goal metric complement sysfs file.

---
 Documentation/admin-guide/mm/damon/usage.rst | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
Documentation/admin-guide/mm/damon/usage.rst +8 -7
index ba4725544856..3d43954c7046 100644
@@ -98,7 +98,8 @@ comma (",").
     │ │ │ │ │ │ │     fail_charge_num,fail_charge_denom
     │ │ │ │ │ │ │ │ weights/sz_permil,nr_accesses_permil,age_permil
     │ │ │ │ │ │ │ │ :ref:`goals <sysfs_schemes_quota_goals>`/nr_goals
-    │ │ │ │ │ │ │ │ │ 0/target_metric,target_value,current_value,nid,path
+    │ │ │ │ │ │ │ │ │ 0/target_metric,complement,target_value,
+    │ │ │ │ │ │ │ │ │  current_value,nid,path
     │ │ │ │ │ │ │ :ref:`watermarks <sysfs_watermarks>`/metric,interval_us,high,mid,low
     │ │ │ │ │ │ │ :ref:`{core_,ops_,}filters <sysfs_filters>`/nr_filters
     │ │ │ │ │ │ │ │ 0/type,matching,allow,memcg_path,addr_start,addr_end,damon_target_idx,min,max
@@ -492,12 +493,12 @@ number (``N``) to the file creates the number of child directories named ``0``
 to ``N-1``.  Each directory represents each goal and current achievement.
 Among the multiple feedback, the best one is used.
 
-Each goal directory contains five files, namely ``target_metric``,
-``target_value``, ``current_value``, ``nid``, and ``path``.  Users can set and
-get the five parameters for the quota auto-tuning goals that specified on the
-:ref:`design doc <damon_design_damos_quotas_auto_tuning>` by writing to and
-reading from each of the files.  Because the kernel does not update
-``current_value``, reading it only makes sense when ``target_metric`` is
+Each goal directory contains six files, namely ``target_metric``,
+``complement``, ``target_value``, ``current_value``, ``nid``, and ``path``.
+Users can set and get the six parameters for the quota auto-tuning goals that
+specified on the :ref:`design doc <damon_design_damos_quotas_auto_tuning>` by
+writing to and reading from each of the files.  Because the kernel does not
+update ``current_value``, reading it only makes sense when ``target_metric`` is
 ``user_input``.  Note that users should further write
 ``commit_schemes_quota_goals`` to the ``state`` file of the :ref:`kdamond
 directory <sysfs_kdamond>` to pass the feedback to DAMON.
Signature · 1 line
2.47.3
#18 lore raw

Re: [PATCH 7/8] Docs/admin-guide/mm/damon/usage: update for quota goal complement file

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Update DAMON usage document for the newly added quota goal metric complement sysfs file.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH 8/8 Docs/ABI/damon: update for quota goal metric complement sysfs file

In reply to SJ Park

Update DAMON ABI document for the newly added damos quota goal metric complement sysfs file.

---
 Documentation/ABI/testing/sysfs-kernel-mm-damon | 6 ++++++
 1 file changed, 6 insertions(+)
Documentation/ABI/testing/sysfs-kernel-mm-damon +6 -0
index 55df688ea596..4acb2015c2f4 100644
@@ -375,6 +375,12 @@ Contact:	SJ Park <[email protected]>
 Description:	Writing to and reading from this file sets and gets the quota
 		auto-tuning goal metric.
 
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/schemes/<S>/quotas/goals/<G>/complement
+Date:		Sep 2026
+Contact:	SJ Park <[email protected]>
+Description:	Writing to and reading from this file sets and gets whether to
+		use the complement value of the quota auto-tuning goal metric.
+
 What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/schemes/<S>/quotas/goals/<G>/target_value
 Date:		Nov 2023
 Contact:	SJ Park <[email protected]>
Signature · 1 line
2.47.3
#20 lore raw

Re: [PATCH 8/8] Docs/ABI/damon: update for quota goal metric complement sysfs file

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 4 lines

Update DAMON ABI document for the newly added damos quota goal metric complement sysfs file.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author maintainer
#21 lore raw
In reply to SJ Park

On Tue, 29 Sep 2026 01:01:03 -0700 SJ Park <[email protected]> wrote:
[...]

Quoted 6 lines

Add a new flag, quota goal complement, to allow flexible tuning goal setup without unnecessarily increasing the number of metrics. The flag specifies whether to use the complemented value of the given goal metric for the tuning. For example, if the complement flag is set, the upper tier memory utilization ratio metric works the same as the free memory ratio metric for the tier.

Sashiko found no blocker for this series. Sashiko sends its findings to damon@ mailing list [1], and I reply those if anything needs to be clarified. Please refer to those for details.

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

Thanks,
SJ

[...]