Skip to contentSkip to conversation

mm/damon/reclaim: add 'available' memory as an optional watermarks metric

RFC PATCH In review Liew Rui Yan 3 participants 7 messages Jun 26 – Jun 28

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

RFC PATCH 1/1 mm/damon/reclaim: add 'available' memory as an optional watermarks metric

Problem
=======

Currently, DAMON's watermarks metric is calculated based on the proportion of the system's free memory.

However, on devices like Android, the system typically maintains a very low amount of free memory, even when the available memory is abundant. This causes DAMON_RECLAIM to prematurely or aggressive trigger memory reclamation, thereby increasing the risk of page refaults.

Solution
========

Introduce a new sysfs parameter 'memrate_type' to DAMON_RECLAIM. Users can switch DAMON's watermark metric evaluation to be based on available memory by writing 'available' to this parameter and commit the change.

---
 include/linux/damon.h    |  9 ++++---
 mm/damon/core.c          |  6 +++++
 mm/damon/reclaim.c       | 56 ++++++++++++++++++++++++++++++++++++++++
 mm/damon/sysfs-schemes.c |  4 +++
 4 files changed, 72 insertions(+), 3 deletions(-)
include/linux/damon.h +6 -3
index 6f7edb3590ef..0006e3a25317 100644
@@ -314,13 +314,16 @@ struct damos_quota {
 /**
  * enum damos_wmark_metric - Represents the watermark metric.
  *
- * @DAMOS_WMARK_NONE:		Ignore the watermarks of the given scheme.
- * @DAMOS_WMARK_FREE_MEM_RATE:	Free memory rate of the system in [0,1000].
- * @NR_DAMOS_WMARK_METRICS:	Total number of DAMOS watermark metrics
+ * @DAMOS_WMARK_NONE:		    Ignore the watermarks of the given scheme.
+ * @DAMOS_WMARK_FREE_MEM_RATE:	    Free memory rate of the system in [0,1000].
+ * @DAMOS_WMARK_AVAILABLE_MEM_RATE: Available memory rate of the system in
+ *				    [0,1000].
+ * @NR_DAMOS_WMARK_METRICS:	    Total number of DAMOS watermark metrics
  */
 enum damos_wmark_metric {
 	DAMOS_WMARK_NONE,
 	DAMOS_WMARK_FREE_MEM_RATE,
+	DAMOS_WMARK_AVAILABLE_MEM_RATE,
 	NR_DAMOS_WMARK_METRICS,
 };
 
mm/damon/core.c +6 -0
index 7e4b9affc5b0..f12d06081d70 100644
@@ -3300,11 +3300,17 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 static int damos_get_wmark_metric_value(enum damos_wmark_metric metric,
 					unsigned long *metric_value)
 {
+	long available_pages;
+
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
 		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
 		return 0;
+	case DAMOS_WMARK_AVAILABLE_MEM_RATE:
+		available_pages = si_mem_available();
+		*metric_value = available_pages * 1000 / totalram_pages();
+		return 0;
 	default:
 		break;
 	}
mm/damon/reclaim.c +56 -0

74 lines not on this page · raw message

mm/damon/sysfs-schemes.c +4 -0
index 329cfd0bbe9f..5d6636433bd6 100644
@@ -1056,6 +1056,10 @@ damos_sysfs_wmark_metric_names[] = {
 		.metric = DAMOS_WMARK_FREE_MEM_RATE,
 		.name = "free_mem_rate",
 	},
+	{
+		.metric = DAMOS_WMARK_AVAILABLE_MEM_RATE,
+		.name = "available_mem_rate",
+	},
 };
 
 static ssize_t metric_show(struct kobject *kobj, struct kobj_attribute *attr,
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Integer overflow in `DAMOS_WMARK_AVAILABLE_MEM_RATE` metric calculation on 32-bit systems with…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Integer overflow in DAMOS_WMARK_AVAILABLE_MEM_RATE metric calculation on 32-bit systems with large RAM. --

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

mm/damon/reclaim: add 'available' memory as an optional watermarks metric

This commit introduces a new 'memrate_type' sysfs parameter to DAMON_RECLAIM. It allows users to switch the watermark metric evaluation from free memory to available memory, which is particularly useful on devices like Android that maintain low free memory.

Quoted 4 lines
mm/damon/core.c +0 -0
index 7e4b9affc5b06..f12d06081d70f 100644

[ ... ]

Quoted 4 lines
mm/damon/core.c +0 -0
@@ -3300,11 +3300,17 @@ static bool kdamond_need_stop(struct damon_ctx *ctx)
 static int damos_get_wmark_metric_value(enum damos_wmark_metric metric,
 					unsigned long *metric_value)
 {

mm/damon/core.c static bool kdamond_need_stop(struct damon_ctx *ctx) · line 3312

Quoted 10 lines
mm/damon/core.c +5 -0
+	long available_pages;
+
 	switch (metric) {
 	case DAMOS_WMARK_FREE_MEM_RATE:
 		*metric_value = global_zone_page_state(NR_FREE_PAGES) * 1000 /
 		       totalram_pages();
 		return 0;
+	case DAMOS_WMARK_AVAILABLE_MEM_RATE:
+		available_pages = si_mem_available();
+		*metric_value = available_pages * 1000 / totalram_pages();

[Severity: Low]
Can this calculation overflow on 32-bit systems with a large amount of memory?

Since available_pages is a long type, on 32-bit architectures it is a 32-bit integer. If the system's available memory exceeds roughly 17.1 GB (about 4,294,967 pages), multiplying available_pages by 1000 before the division would overflow the maximum 32-bit value.

This truncation could result in an artificially low watermark metric rate.

Would it be safer to cast available_pages to a 64-bit type or use mult_frac() to prevent the potential overflow, even if such large memory sizes are uncommon on 32-bit targets?

Quoted 4 lines
mm/damon/core.c +1 -0
+		return 0;
 	default:
 		break;
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Liew Rui Yan

Hi Liew,

Thank you for this patch!

Quoted 11 lines · On Fri, 26 Jun 2026 16:10:38 +0800 Liew Rui Yan <[email protected]>…

Problem
=======

Currently, DAMON's watermarks metric is calculated based on the proportion of the system's free memory.

However, on devices like Android, the system typically maintains a very low amount of free memory, even when the available memory is abundant. This causes DAMON_RECLAIM to prematurely or aggressive trigger memory reclamation, thereby increasing the risk of page refaults.

Quoted 6 lines

Solution
========

Introduce a new sysfs parameter 'memrate_type' to DAMON_RECLAIM. Users can switch DAMON's watermark metric evaluation to be based on available memory by writing 'available' to this parameter and commit the change.

How about using DAMOS quota with its aim-based auto-tuning, instead?

I developed DAMOS watermarks and quota as safe guards of DAMOS. In more detail, DAMOS quota is for restricting only resource usage of DAMOS. Meanwhile, watermarks is for restricting resource usage of both DAMON and DAMOS. That is, when the watermarks condition is met, not only DAMOS but also DAMON is completely paused. Also, watermarks feature was considered smarter than quota, because it is a kind of auto-tuning.

However, it turned out completely turning DAMON off is not a very good idea, because it drops monitoring results when the watermarks condition is met and therefore completely stops DAMON and DAMOS. It drops the region shapes and 'age' information. Especially, 'age' of some regions commonly cumulated up to hours or even days. Dropping that was a problem in production use cases.

Meanwhile, we introduced aim-oriented DAMOS quota auto-tuning (a.k.a DAMOS quota goal). It made DAMOS quota smarter and easier to extend than watermarks. Nowadays, users can also pause and resume [1] DAMON/DAMOS without losing the monitoring information when they want.

So, I personally don't encourage people to use watermarks. Of course, I may missing some problems in alternatives. Hence I'm asking the question to you.

How about using DAMOS quota with its aim-based auto-tuning, instead? If you cannot, could you share more details?

I'll hold reviewing the code until this high level discussion is resolved.

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

Thanks,
SJ

[...]

#3 lore raw

Hi SeongJae,

Quoted 9 lines · On Fri, 26 Jun 2026 07:18:18 -0700 SeongJae Park <[email protected]> wrote:

How about using DAMOS quota with its aim-based auto-tuning, instead?

I developed DAMOS watermarks and quota as safe guards of DAMOS. In more detail, DAMOS quota is for restricting only resource usage of DAMOS. Meanwhile, watermarks is for restricting resource usage of both DAMON and DAMOS. That is, when the watermarks condition is met, not only DAMOS but also DAMON is completely paused. Also, watermarks feature was considered smarter than quota, because it is a kind of auto-tuning.

Quoted 5 lines

However, it turned out completely turning DAMON off is not a very good idea, because it drops monitoring results when the watermarks condition is met and therefore completely stops DAMON and DAMOS. It drops the region shapes and 'age' information. Especially, 'age' of some regions commonly cumulated up to hours or even days. Dropping that was a problem in production use cases.

I looked at the code and noticed that kdamond_wait_activation() doesn't seem to free the regions, the data remains in memory. By "drops", do you mean the age information becomes stale/unreliable during the inactive period rather than being literally discarded?

Quoted 9 lines

Meanwhile, we introduced aim-oriented DAMOS quota auto-tuning (a.k.a DAMOS quota goal). It made DAMOS quota smarter and easier to extend than watermarks. Nowadays, users can also pause and resume [1] DAMON/DAMOS without losing the monitoring information when they want.

So, I personally don't encourage people to use watermarks. Of course, I may missing some problems in alternatives. Hence I'm asking the question to you.

Quoted 6 lines

How about using DAMOS quota with its aim-based auto-tuning, instead? If you cannot, could you share more details?

I'll hold reviewing the code until this high level discussion is resolved.

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

Thank you for the detailed explanation. I now understand that watermarks pause the entire DAMON monitoring loop rather than just gating DAMOS actions. I agree this makes watermarks unsuitable for the Android use case I described.

I will drop this patch. I think controlling DAMOS pause/rusume separately might be better suited for execution in user space (such as controlling‘quota_ms’ and ‘quota_sz’).

By the way, I sent you an email about three weeks ago (Fri, 5 Jun 2026 20:28:00 +0800) introducing a userspace auto-tuner program I wrote for DAMON_RECLAIM's 'min_age', and asking for your thoughts. I am not sure if it reached you, could you please check? I would really appreciate your feedback on that as well.

Best regards,
Rui Yan

maintainer
#4 lore raw
Quoted 13 lines · On Sun, 28 Jun 2026 05:36:50 +0800 Liew Rui Yan <[email protected]>…

Hi SeongJae,

Quoted 9 lines · On Fri, 26 Jun 2026 07:18:18 -0700 SeongJae Park <[email protected]> wrote:

How about using DAMOS quota with its aim-based auto-tuning, instead?

I developed DAMOS watermarks and quota as safe guards of DAMOS. In more detail, DAMOS quota is for restricting only resource usage of DAMOS. Meanwhile, watermarks is for restricting resource usage of both DAMON and DAMOS. That is, when the watermarks condition is met, not only DAMOS but also DAMON is completely paused. Also, watermarks feature was considered smarter than quota, because it is a kind of auto-tuning.

Quoted 8 lines
Quoted 5 lines

However, it turned out completely turning DAMON off is not a very good idea, because it drops monitoring results when the watermarks condition is met and therefore completely stops DAMON and DAMOS. It drops the region shapes and 'age' information. Especially, 'age' of some regions commonly cumulated up to hours or even days. Dropping that was a problem in production use cases.

I looked at the code and noticed that kdamond_wait_activation() doesn't seem to free the regions, the data remains in memory.

Ah, you are correct.

Quoted 3 lines

By "drops",
do you mean the age information becomes stale/unreliable during the inactive period rather than being literally discarded?

I was just wrongly recall the detail ;) But, yes, your representation is correct and much better.

Quoted 17 lines
Quoted 10 lines

Meanwhile, we introduced aim-oriented DAMOS quota auto-tuning (a.k.a DAMOS quota goal). It made DAMOS quota smarter and easier to extend than watermarks. Nowadays, users can also pause and resume [1] DAMON/DAMOS without losing the monitoring information when they want.

So, I personally don't encourage people to use watermarks. Of course, I may missing some problems in alternatives. Hence I'm asking the question to you.

Quoted 6 lines

How about using DAMOS quota with its aim-based auto-tuning, instead? If you cannot, could you share more details?

I'll hold reviewing the code until this high level discussion is resolved.

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

Quoted 8 lines

Thank you for the detailed explanation. I now understand that watermarks pause the entire DAMON monitoring loop rather than just gating DAMOS actions. I agree this makes watermarks unsuitable for the Android use case I described.

I will drop this patch. I think controlling DAMOS pause/rusume separately might be better suited for execution in user space (such as controlling‘quota_ms’ and ‘quota_sz’).

For explicit DAMOS-only pause/resume, you may also be interested in max_nr_snapshots [1] feature.

Quoted 6 lines

By the way, I sent you an email about three weeks ago (Fri, 5 Jun 2026 20:28:00 +0800) introducing a userspace auto-tuner program I wrote for DAMON_RECLAIM's 'min_age', and asking for your thoughts. I am not sure if it reached you, could you please check? I would really appreciate your feedback on that as well.

I don't recall that mail. Did you send that to [email protected] or other public mailing lists? If so, could you share the message id or lore.kernel.org link of the mail? If not, could you please resend?

[1] https://lkml.kernel.org/r/[email protected]

Thanks,
SJ

[...]

#5 lore raw
Quoted 6 lines · On Sat, 27 Jun 2026 15:26:43 -0700 SJ Park <[email protected]> wrote:
Quoted 3 lines

I will drop this patch. I think controlling DAMOS pause/rusume separately might be better suited for execution in user space (such as controlling‘quota_ms’ and ‘quota_sz’).

For explicit DAMOS-only pause/resume, you may also be interested in max_nr_snapshots [1] feature.

Thank you for the pointer to the 'max_nr_snapshots' feature!

Quoted 10 lines
Quoted 6 lines

By the way, I sent you an email about three weeks ago (Fri, 5 Jun 2026 20:28:00 +0800) introducing a userspace auto-tuner program I wrote for DAMON_RECLAIM's 'min_age', and asking for your thoughts. I am not sure if it reached you, could you please check? I would really appreciate your feedback on that as well.

I don't recall that mail. Did you send that to [email protected] or other public mailing lists? If so, could you share the message id or lore.kernel.org link of the mail? If not, could you please resend?

I hadn't previously sent this to [email protected] or any public mailing list.

Since I have completely rewritten it (the email) and it focuses on a standalone userspace program/daemon, I have submitted it as a new independent RFC thread [1] to avoid biasing this patch's topic.

Quoted 1 line

[1] https://lkml.kernel.org/r/[email protected]

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

Best regards,
Rui Yan

maintainer
#6 lore raw
Quoted 10 lines · On Sun, 28 Jun 2026 16:55:38 +0800 Liew Rui Yan <[email protected]>…
Quoted 6 lines · On Sat, 27 Jun 2026 15:26:43 -0700 SJ Park <[email protected]> wrote:
Quoted 3 lines

I will drop this patch. I think controlling DAMOS pause/rusume separately might be better suited for execution in user space (such as controlling‘quota_ms’ and ‘quota_sz’).

For explicit DAMOS-only pause/resume, you may also be interested in max_nr_snapshots [1] feature.

Thank you for the pointer to the 'max_nr_snapshots' feature!

Feel free to let me know if you have any question for the feature.

Quoted 12 lines
Quoted 11 lines
Quoted 7 lines

By the way, I sent you an email about three weeks ago (Fri, 5 Jun 2026 20:28:00 +0800) introducing a userspace auto-tuner program I wrote for DAMON_RECLAIM's 'min_age', and asking for your thoughts. I am not sure if it reached you, could you please check? I would really appreciate your feedback on that as well.

I don't recall that mail. Did you send that to [email protected] or other public mailing lists? If so, could you share the message id or lore.kernel.org link of the mail? If not, could you please resend?

Quoted 6 lines

I hadn't previously sent this to [email protected] or any public mailing list.

Since I have completely rewritten it (the email) and it focuses on a standalone userspace program/daemon, I have submitted it as a new independent RFC thread [1] to avoid biasing this patch's topic.

Makes perfect sense. I will review.

Quoted 4 lines
Quoted 2 lines

[1] https://lkml.kernel.org/r/[email protected]

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

Thanks,
SJ

[...]