Skip to contentSkip to conversation

mm/damon: allow DAMOS auto-tuned for per-memcg per-node memory usage

RFC PATCH Superseded SeongJae Park 1 participant 14 messages Jun 19, 2025 – Oct 9, 2025

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover mm/damon: allow DAMOS auto-tuned for per-memcg per-node memory usage

This patch series introduce two new DAMOS quota auto-tuning target metrics for per-cgroup per-node memory usage. Those are assumed to be useful for various cgroup-aware case including memory tiering on cgroup-based multi-tenant systems.

Background
----------

DAMOS' aim-oriented aggressiveness auto-tuning feature is a highly recommended way for modern DAMOS use cases. Using it, users can specify what system status they want to achive with what access-aware system operations. For example, reclaim cold memory aiming 0.5 % of memory pressure (proactive reclaim), or migrate hot and cold memory between NUMA nodes having different speed (memory tiering). Then DAMOS automatically adjusts the aggressiveness of the system operation (e.g., increase/decrease reclaim target coldness threshold) based on current status of the system.

The use case is hence limited by the supported system status metrics for specifying the target system status. Two new system metrics for per-node memory usage ratio, namely DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP, were recently added to extend the use cases for access-aware NUMA nodes management, such as memory tiering.

Limitation
~~~~~~~~~~

The per-node memory usage based auto-tuning can be applied only system-wide. For cgroups-based multi tenant systems, it could harm the fairness. For example, even if the cgroups have their own memory.low values for protecting their memory, it could result in a cgroup having more hot pages using more portion of fast tier NUMA node. Depending on the definition and promise of the fairness, this could be suboptimum.

DAMOS supports cgroup-aware system operations via DAMOS filter. But quota auto-tuning system is not aware of cgroups.

New DAMOS Quota Tuning Metrics for Per-Cgroup Per-NUMA Memory Usage
-------------------------------------------------------------------

To overcome the limitation, this patch series introduces two new DAMOS quota auto-tuning goal metrics, namely DAMOS_QUOTA_NODE_MEMCG_{USED,FREE}_BP. Those can be thought of as a variant of DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP that extended for cgroups.

The two metrics specifies per-cgroup, per-node amount of used and unused memory in ratio to the total memory of the node. For example, let's assume a system has two NUMA nodes of size 100 GiB and 50 GiB. And two cgroups are using 40 and 60 GiB of node 0, 20 and 10 GiB of node 1, respectively.

                     node-0    node-1
    Total memory     100 GiB   50 GiB
    Cgroup A usage   40 GiB    20 GiB
    Cgroup B usage   60 GiB    10 GiB

Then, DAMOS_QUOTA_NODE_MEMCG_USED_BP for the cgroups for the first node are, 40 GiB / 100 GiB = 4000 bp and 60 GiB / 100 GiB = 6000 bp, respectively. Those for the second node are, 20 GiB / 50 GiB = 4000 bp and 10 GiB / 50 GiB = 2000 bp, respectively.

DAMOS_QUOTA_NODE_MEMCG_FREE_BP for the four cases are, 60 GiB /100 GiB = 6000 bp, 40 GiB / 100 GiB = 4000 bp, 30 GiB / 50 GiB = 6000 bp, and 40 GiB / 50 GiB = 8000 bp, respectively.

DAMOS_QUOTA_NODE_MEMCG_USED_BP for cgroup A node-0: 4000 bp DAMOS_QUOTA_NODE_MEMCG_USED_BP for cgroup B node-0: 6000 bp DAMOS_QUOTA_NODE_MEMCG_USED_BP for cgroup A node-1: 4000 bp DAMOS_QUOTA_NODE_MEMCG_USED_BP for cgroup B node-1: 2000 bp

DAMOS_QUOTA_NODE_MEMCG_FREE_BP for cgroup A node-0: 6000 bp DAMOS_QUOTA_NODE_MEMCG_FREE_BP for cgroup B node-0: 4000 bp DAMOS_QUOTA_NODE_MEMCG_FREE_BP for cgroup A node-1: 6000 bp DAMOS_QUOTA_NODE_MEMCG_FREE_BP for cgroup B node-1: 8000 bp

Using these, users can specify how much [un]used amount of memory for per-cgroup and per-node DAMOS should make as a result of the auto-tuning.

Example Usecase: Cgroup-aware Memory Tiering
--------------------------------------------

Let's suppose a system having two NUMA nodes. The first node (node 0) is CPU-attached and fast. The second node (node 1) is CPU-unattached and slow. It runs two cgroups, hoping to use 30 percent and 70 percent of system memory, respectively. Then, we can implement memory tiering for the system using DAMOS to let the cgroups use 30 percent and 70 percent of each nodes preferring more of node 0, like below, using DAMON user-space tool.

    # ./damo start \
    	\ # kdamond for node 1 (slow)
        --numa_node 1 --monitoring_intervals_goal 4% 3 5ms 10s \
	    \ # promotion scheme for cgroup a
            --damos_action migrate_hot 0 --damos_access_rate 5% max \
            --damos_apply_interval 1s \
	    --damos_filter allow memcg /workloads/a \
            --damos_filter allow young \
            --damos_quota_interval 1s --damos_quota_space 200MB \
            --damos_quota_goal node_memcg_used_bp 29.7% 0 /workloads/a \
	    \
	    \ # promotion scheme for cgroup b
            --damos_action migrate_hot 0 --damos_access_rate 5% max \
            --damos_apply_interval 1s \
	    --damos_filter allow memcg /workloads/b \
            --damos_filter allow young \
            --damos_quota_interval 1s --damos_quota_space 200MB \
            --damos_quota_goal node_memcg_used_bp 69.7% 0 workloads/b \
	    \
    	\ # kdamond for node 0 (fast)
        --numa_node 0 --monitoring_intervals_goal 4% 3 5ms 10s \
            \ # demotion scheme for cgroup a
            --damos_action migrate_cold 1 --damos_access_rate 0% 0% \
            --damos_apply_interval 1s \
	    --damos_filter allow memcg /workloads/a \
            --damos_filter reject young \
            --damos_quota_interval 1s --damos_quota_space 200MB \
            --damos_quota_goal node_memcg_free_bp 70.5% 0 \
	    \
            \ # demotion scheme for cgroup b
            --damos_action migrate_cold 1 --damos_access_rate 0% 0% \
            --damos_apply_interval 1s \
	    --damos_filter allow memcg /workloads/a \
            --damos_filter reject young \
            --damos_quota_interval 1s --damos_quota_space 200MB \
            --damos_quota_goal node_memcg_free_bp 30.5% 0 \
	    \
            --damos_nr_quota_goals 1 1 1 1 --damos_nr_filters 1 1 1 1 \
        --nr_targets 1 1 --nr_schemes 2 2 --nr_ctxs 1 1

With the command, the user-space tool will ask DAMON to spawn two kernel threads, monitoring access to node 1 (slow) and node 0 (fast), respectively. It installs two DAMOS schemes on each threads. Let's call them "promotion scheme for cgroup a", "promotion scheme for cgroup b", "demotion scheme for cgroup a", "demotion scheme for cgroup b", in the order. The promotion schemes are installed on the DAMON thread for node 1 (slow), and demotion schemes are installed on the DAMON thrad for node 0 (fast).

Cgroup-aware Hot Pages Migration (Promotion)
--------------------------------------------

Promotion schemes will find memory regions on node 1 (slow), that any access was detected. The schemes will then migrate the found memory to node 0 (fast), hottest pages first.

For accurate and effective migration, these schemes use two page level filters. First, the migration will be filtered for only cgroup A and cgroup B. That is, promotion scheme for cgroup B will not do the migration if the page is for cgroup A. Secondly, the schemes will filter out pages that having unset page table's Accessed bit for those. The per-page Accessed bit check will also unset the bit for next check.

For controlled amount of system resource consumption and aiming on the target memory usage, the schemes use quotas setup. The migration is limited to be done only up to 200 MiB per second, to limit the system resource usage. And DAMOS_QUOTA_NODE_MEMCG_USED_BP target is set for 29.7% and 69.7% of node 0 (fast), respectively. The target value is lower than the high level goal (30% and 70% system memory), to give a headroom on node 0 (fast). DAMOS will adjust the speed of the pages migration based on the target and current memory usage. For example, if cgroup A is utilizing only 10% of node 0, DAMOS will try to migrate more of cgroup A hot pages from node 1, up to 200 MiB per second. If cgroup A utilizes more than 29.7% of node 0 memory, the cgroup A hot pages migration from node 1 to node 0 will be slowered and eventually stopped.

Cgroup-aware Cold Pages Migration (Demotion)
--------------------------------------------

Demotion schemes are similar to promotion schemes, but differ in filtering setup and quota tuning setup. Those filters out pages having page table Accessed bit set. And set 70.5% and 30.5% of node 0 memory free rate for the cgroup A and B, respectively. Hence, if promotion schemes or something made cgroup A and/or B uses more than 29.5% and 69.5% of node 0, demotion schemes will start migrating cold pages of appropriate cgroups in node 0 to node 1, under the 200 MiB per second speed cap, while adjusting the speed based on how much more than wanted memory is being used.

Future Works for Dropping RFC tag
---------------------------------

This is an RFC. Only build test is done. More testing should be essential for dropping the RFC tag. Patches sequence and commit messages might also be changed.

Also, this patch series is based on the tip of DAMON development tree (damon/next), which contains a few patches that not yet merged in mm-new or even not yet posted. Proper rebase should be made for non-RFC version.

SeongJae Park (11):
mm/damon: document damos_quota_goal->nid use case mm/damon: add a new DAMOS quota goal metric for cgroup on node memory
usage
mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP mm/damon/sysfs-schemes: implement path file under quota goal directory mm/damon/sysfs-schemes: connect quota goal path file to DAMON core Docs/mm/damon/design: document DAMOS_QUOTA_NODE_MEMCG_USED_BP Docs/admin-guide/mm/damon/usage: document DAMOS quota goal path file mm/damon: add DAMOS_QUOTA_NODE_MEMCG_FREE_BP quota tuning goal metric mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_FREE_BP mm/damon/sysfs-schemes: support DAMOS_QUOTA_NODE_MEMCG_FREE_BP Docs/mm/damon/design: document DAMOS_QUOTA_NODE_MEMCG_FREE_BP

 Documentation/admin-guide/mm/damon/usage.rst |  8 +--
 Documentation/mm/damon/design.rst            | 18 ++++--
 include/linux/damon.h                        | 16 +++++-
 mm/damon/core.c                              | 37 +++++++++++++
 mm/damon/sysfs-schemes.c                     | 58 +++++++++++++++++++-
 5 files changed, 124 insertions(+), 13 deletions(-)

base-commit: b7471357d2a80aeeb77f49a4e8fcaf037d5c3aee

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

RFC PATCH 1/11 mm/damon: document damos_quota_goal->nid use case

damos_quota_goal kerneldoc comment is not explaining @metric use case. Add it.

---
 include/linux/damon.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/damon.h +3 -0
index 492be8319a25..39b835dd3c4d 100644
@@ -206,6 +206,9 @@ enum damos_quota_goal_metric {
  * If @metric is DAMOS_QUOTA_USER_INPUT, @current_value should be manually
  * entered by the user, probably inside the kdamond callbacks.  Otherwise,
  * DAMON sets @current_value with self-measured value of @metric.
+ *
+ * If @metric is DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP, @nid represents the node
+ * id of the target node to account the used/free memory.
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
Signature · 1 line
2.39.5
author maintainer
#2 lore raw

RFC PATCH 2/11 mm/damon: add a new DAMOS quota goal metric for cgroup on node memory usage

In reply to SeongJae Park

Define a new DAMOS quota auto-tuning target metrics for per-cgroup per-node memory usage. Also extend damos_quota_goal struct to have a field for specifying the cgroup of the interest.

Note that this commit is only for DAMON kernel API extension. Real behavior of the new metric will be implemented by a following commit.

---
 include/linux/damon.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
include/linux/damon.h +10 -1
index 39b835dd3c4d..43aef959d357 100644
@@ -175,6 +175,7 @@ enum damos_action {
  * @DAMOS_QUOTA_SOME_MEM_PSI_US:	System level some memory PSI in us.
  * @DAMOS_QUOTA_NODE_MEM_USED_BP:	MemUsed ratio of a node.
  * @DAMOS_QUOTA_NODE_MEM_FREE_BP:	MemFree ratio of a node.
+ * @DAMOS_QUOTA_NODE_MEMCG_USED_BP:	MemUsed ratio of a node for a cgroup.
  * @DAMOS_QUOTA_ACTIVE_MEM_BP:		Active to inactive memory ratio.
  * @NR_DAMOS_QUOTA_GOAL_METRICS:	Number of DAMOS quota goal metrics.
  *
@@ -185,6 +186,7 @@ enum damos_quota_goal_metric {
 	DAMOS_QUOTA_SOME_MEM_PSI_US,
 	DAMOS_QUOTA_NODE_MEM_USED_BP,
 	DAMOS_QUOTA_NODE_MEM_FREE_BP,
+	DAMOS_QUOTA_NODE_MEMCG_USED_BP,
 	DAMOS_QUOTA_ACTIVE_MEM_BP,
 	NR_DAMOS_QUOTA_GOAL_METRICS,
 };
@@ -196,6 +198,7 @@ enum damos_quota_goal_metric {
  * @current_value:	Current value of @metric.
  * @last_psi_total:	Last measured total PSI
  * @nid:		Node id.
+ * @memcg:		Memcg id.
  * @list:		List head for siblings.
  *
  * Data structure for getting the current score of the quota tuning goal.  The
@@ -209,6 +212,9 @@ enum damos_quota_goal_metric {
  *
  * If @metric is DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP, @nid represents the node
  * id of the target node to account the used/free memory.
+ *
+ * If @metric is DAMOS_QUOTA_NODE_MEMCG_USED_BP, @nid and @memcg_id represents
+ * the node id and the cgroup to account the used memory for.
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
@@ -217,7 +223,10 @@ struct damos_quota_goal {
 	/* metric-dependent fields */
 	union {
 		u64 last_psi_total;
-		int nid;
+		struct {
+			int nid;
+			unsigned short memcg_id;
+		};
 	};
 	struct list_head list;
 };
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

RFC PATCH 3/11 mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP

In reply to SeongJae Park

Implement the behavior of DAMOS_QUOTA_NODE_MEMCG_USED_BP. It uses sum of active/inactive anon/file pages of a given cgroup on a given NUMA node as the value of the metric.

---
 mm/damon/core.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
mm/damon/core.c +32 -0
index d675dd932a23..1481b43f2710 100644
@@ -10,6 +10,7 @@
 #include <linux/damon.h>
 #include <linux/delay.h>
 #include <linux/kthread.h>
+#include <linux/memcontrol.h>
 #include <linux/mm.h>
 #include <linux/psi.h>
 #include <linux/slab.h>
@@ -2021,12 +2022,40 @@ static __kernel_ulong_t damos_get_node_mem_bp(
 		numerator = i.freeram;
 	return numerator * 10000 / i.totalram;
 }
+
+static unsigned long damos_get_node_memcg_used_bp(
+		struct damos_quota_goal *goal)
+{
+	struct mem_cgroup *memcg;
+	struct lruvec *lruvec;
+	unsigned long used_pages;
+	struct sysinfo i;
+
+	rcu_read_lock();
+	memcg = mem_cgroup_from_id(goal->memcg_id);
+	mem_cgroup_flush_stats(memcg);
+	lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(goal->nid));
+	used_pages = lruvec_page_state(lruvec, NR_ACTIVE_ANON);
+	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_ANON);
+	used_pages += lruvec_page_state(lruvec, NR_ACTIVE_FILE);
+	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_FILE);
+	rcu_read_unlock();
+
+	si_meminfo_node(&i, goal->nid);
+	return used_pages * PAGE_SIZE * 10000 / i.totalram;
+}
 #else
 static __kernel_ulong_t damos_get_node_mem_bp(
 		struct damos_quota_goal *goal)
 {
 	return 0;
 }
+
+static unsigned long damos_get_node_memcg_used_bp(
+		struct damos_quota_goal *goal)
+{
+	return 0;
+}
 #endif
 
 /*
@@ -2062,6 +2091,9 @@ static void damos_set_quota_goal_current_value(struct damos_quota_goal *goal)
 	case DAMOS_QUOTA_NODE_MEM_FREE_BP:
 		goal->current_value = damos_get_node_mem_bp(goal);
 		break;
+	case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
+		goal->current_value = damos_get_node_memcg_used_bp(goal);
+		break;
 	case DAMOS_QUOTA_ACTIVE_MEM_BP:
 		goal->current_value = damos_get_active_inactive_mem_bp();
 		break;
Signature · 1 line
2.39.5
author maintainer
#6 lore raw

RFC PATCH 4/11 mm/damon/sysfs-schemes: implement path file under quota goal directory

In reply to SeongJae Park

Add a DAMOS sysfs file for specifying the cgroup of the interest for DAMOS_QUOTA_NODE_MEMCG_USED_BP.

---
 mm/damon/sysfs-schemes.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)
mm/damon/sysfs-schemes.c +36 -2
index 4805761d9127..4a340baa72a4 100644
@@ -999,6 +999,7 @@ struct damos_sysfs_quota_goal {
 	unsigned long target_value;
 	unsigned long current_value;
 	int nid;
+	char *path;
 };
 
 static struct damos_sysfs_quota_goal *damos_sysfs_quota_goal_alloc(void)
@@ -1132,10 +1133,39 @@ static ssize_t nid_store(struct kobject *kobj,
 	return err ? err : count;
 }
 
+static ssize_t path_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, "%s\n", goal->path ? goal->path : "");
+}
+
+static ssize_t path_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);
+	char *path = kmalloc_array(size_add(count, 1), sizeof(*path),
+				   GFP_KERNEL);
+
+	if (!path)
+		return -ENOMEM;
+
+	strscpy(path, buf, count + 1);
+	kfree(goal->path);
+	goal->path = path;
+	return count;
+}
+
 static void damos_sysfs_quota_goal_release(struct kobject *kobj)
 {
-	/* or, notify this release to the feed callback */
-	kfree(container_of(kobj, struct damos_sysfs_quota_goal, kobj));
+	struct damos_sysfs_quota_goal *goal = container_of(kobj,
+			struct damos_sysfs_quota_goal, kobj);
+
+	kfree(goal->path);
+	kfree(goal);
 }
 
 static struct kobj_attribute damos_sysfs_quota_goal_target_metric_attr =
@@ -1150,11 +1180,15 @@ static struct kobj_attribute damos_sysfs_quota_goal_current_value_attr =
 static struct kobj_attribute damos_sysfs_quota_goal_nid_attr =
 		__ATTR_RW_MODE(nid, 0600);
 
+static struct kobj_attribute damos_sysfs_quota_goal_path_attr =
+		__ATTR_RW_MODE(path, 0600);
+
 static struct attribute *damos_sysfs_quota_goal_attrs[] = {
 	&damos_sysfs_quota_goal_target_metric_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,
+	&damos_sysfs_quota_goal_path_attr.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(damos_sysfs_quota_goal);
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

RFC PATCH 5/11 mm/damon/sysfs-schemes: connect quota goal path file to DAMON core

In reply to SeongJae Park

Read and commit the user-specified cgroup information for DAMOS_QUOTA_NODE_MEMCG_USED_BP to DAMON core.

---
 mm/damon/sysfs-schemes.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
mm/damon/sysfs-schemes.c +14 -1
index 4a340baa72a4..89e5e40bdf63 100644
@@ -1030,6 +1030,10 @@ static struct damos_sysfs_qgoal_metric_name damos_sysfs_qgoal_metric_names[] =
 		.metric = DAMOS_QUOTA_NODE_MEM_FREE_BP,
 		.name = "node_mem_free_bp",
 	},
+	{
+		.metric = DAMOS_QUOTA_NODE_MEMCG_USED_BP,
+		.name = "node_memcg_used_bp",
+	},
 	{
 		.metric = DAMOS_QUOTA_ACTIVE_MEM_BP,
 		.name = "active_mem_bp",
@@ -2317,7 +2321,7 @@ static int damos_sysfs_add_quota_score(
 		struct damos_quota *quota)
 {
 	struct damos_quota_goal *goal;
-	int i;
+	int i, err;
 
 	for (i = 0; i < sysfs_goals->nr; i++) {
 		struct damos_sysfs_quota_goal *sysfs_goal =
@@ -2338,6 +2342,15 @@ static int damos_sysfs_add_quota_score(
 		case DAMOS_QUOTA_NODE_MEM_FREE_BP:
 			goal->nid = sysfs_goal->nid;
 			break;
+		case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
+			err = damon_sysfs_memcg_path_to_id(
+					sysfs_goal->path, &goal->memcg_id);
+			if (err) {
+				damos_destroy_quota_goal(goal);
+				return err;
+			}
+			goal->nid = sysfs_goal->nid;
+			break;
 		default:
 			break;
 		}
Signature · 1 line
2.39.5
author maintainer
#8 lore raw

RFC PATCH 6/11 Docs/mm/damon/design: document DAMOS_QUOTA_NODE_MEMCG_USED_BP

In reply to SeongJae Park
---
 Documentation/mm/damon/design.rst | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
Documentation/mm/damon/design.rst +12 -5
index 2290ebefc648..eb6d3b7d0643 100644
@@ -553,9 +553,9 @@ aggressiveness (the quota) of the corresponding scheme.  For example, if DAMOS
 is under achieving the goal, DAMOS automatically increases the quota.  If DAMOS
 is over achieving the goal, it decreases the quota.
 
-The goal can be specified with four parameters, namely ``target_metric``,
-``target_value``, ``current_value`` and ``nid``.  The auto-tuning mechanism
-tries to make ``current_value`` of ``target_metric`` be same to
+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``.
 
 - ``user_input``: User-provided value.  Users could use any metric that they
@@ -570,9 +570,16 @@ tries to make ``current_value`` of ``target_metric`` be same to
   set by users at the initial time.  In other words, DAMOS does self-feedback.
 - ``node_mem_used_bp``: Specific NUMA node's used memory ratio in bp (1/10,000).
 - ``node_mem_free_bp``: Specific NUMA node's free memory ratio in bp (1/10,000).
+- ``node_memcg_used_bp``: Specific cgroup's node used memory ratio for a
+  specific NUMA node, in bp (1/10,000).
 
-``nid`` is optionally required for only ``node_mem_used_bp`` and
-``node_mem_free_bp`` to point the specific NUMA node.
+``nid`` is optionally required for only ``node_mem_used_bp``,
+``node_mem_free_bp`` and ``node_memcg_used_bp`` to point the specific NUMA
+node.
+
+``path`` is optionally required for only ``node_memcg_used_bp`` to point the
+path to the cgroup.  The value should be the path of the memory cgroup from the
+cgroups mount point.
 
 To know how user-space can set the tuning goal metric, the target value, and/or
 the current value via :ref:`DAMON sysfs interface <sysfs_interface>`, refer to
Signature · 1 line
2.39.5
author maintainer
#9 lore raw

RFC PATCH 7/11 Docs/admin-guide/mm/damon/usage: document DAMOS quota goal path file

In reply to SeongJae Park
---
 Documentation/admin-guide/mm/damon/usage.rst | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
Documentation/admin-guide/mm/damon/usage.rst +4 -4
index d960aba72b82..2f603977fa88 100644
@@ -81,7 +81,7 @@ comma (",").
     │ │ │ │ │ │ │ :ref:`quotas <sysfs_quotas>`/ms,bytes,reset_interval_ms,effective_bytes
     │ │ │ │ │ │ │ │ weights/sz_permil,nr_accesses_permil,age_permil
     │ │ │ │ │ │ │ │ :ref:`goals <sysfs_schemes_quota_goals>`/nr_goals
-    │ │ │ │ │ │ │ │ │ 0/target_metric,target_value,current_value,nid
+    │ │ │ │ │ │ │ │ │ 0/target_metric,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,target_idx,min,max
@@ -390,9 +390,9 @@ 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 four files, namely ``target_metric``,
-``target_value``, ``current_value`` and ``nid``.  Users can set and get the
-four parameters for the quota auto-tuning goals that specified on the
+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.  Note that users should further write
 ``commit_schemes_quota_goals`` to the ``state`` file of the :ref:`kdamond
Signature · 1 line
2.39.5
author maintainer
#10 lore raw

RFC PATCH 8/11 mm/damon: add DAMOS_QUOTA_NODE_MEMCG_FREE_BP quota tuning goal metric

In reply to SeongJae Park

Add a variant of DAMOS_QUOTA_NODE_MEMCG_USED_BP, for free memory portion. Note that this commit only extends the DAMON API. The real behavior will be implemented by a following commit.

---
 include/linux/damon.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
include/linux/damon.h +4 -2
index 43aef959d357..ba02c95e7d85 100644
@@ -176,6 +176,7 @@ enum damos_action {
  * @DAMOS_QUOTA_NODE_MEM_USED_BP:	MemUsed ratio of a node.
  * @DAMOS_QUOTA_NODE_MEM_FREE_BP:	MemFree ratio of a node.
  * @DAMOS_QUOTA_NODE_MEMCG_USED_BP:	MemUsed ratio of a node for a cgroup.
+ * @DAMOS_QUOTA_NODE_MEMCG_FREE_BP:	MemFree ratio of a node for a cgroup.
  * @DAMOS_QUOTA_ACTIVE_MEM_BP:		Active to inactive memory ratio.
  * @NR_DAMOS_QUOTA_GOAL_METRICS:	Number of DAMOS quota goal metrics.
  *
@@ -187,6 +188,7 @@ enum damos_quota_goal_metric {
 	DAMOS_QUOTA_NODE_MEM_USED_BP,
 	DAMOS_QUOTA_NODE_MEM_FREE_BP,
 	DAMOS_QUOTA_NODE_MEMCG_USED_BP,
+	DAMOS_QUOTA_NODE_MEMCG_FREE_BP,
 	DAMOS_QUOTA_ACTIVE_MEM_BP,
 	NR_DAMOS_QUOTA_GOAL_METRICS,
 };
@@ -213,8 +215,8 @@ enum damos_quota_goal_metric {
  * If @metric is DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP, @nid represents the node
  * id of the target node to account the used/free memory.
  *
- * If @metric is DAMOS_QUOTA_NODE_MEMCG_USED_BP, @nid and @memcg_id represents
- * the node id and the cgroup to account the used memory for.
+ * If @metric is DAMOS_QUOTA_NODE_MEMCG_{USED,FREE}_BP, @nid and @memcg_id
+ * represents the node id and the cgroup to account the used memory for.
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
Signature · 1 line
2.39.5
author maintainer
#11 lore raw

RFC PATCH 9/11 mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_FREE_BP

In reply to SeongJae Park

Implement the core part of DAMOS_QUOTA_NODE_MEMCG_FREE_BP to get the real value. The value is implemented as the entire memory of the given NUMA node, except the given cgroup's portion. So strictly speaking, it is not free memory but memory that not used by the cgroup.

---
 mm/damon/core.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
mm/damon/core.c +7 -2
index 1481b43f2710..ee3d6d4b3c9b 100644
@@ -2028,7 +2028,7 @@ static unsigned long damos_get_node_memcg_used_bp(
 {
 	struct mem_cgroup *memcg;
 	struct lruvec *lruvec;
-	unsigned long used_pages;
+	unsigned long used_pages, numerator;
 	struct sysinfo i;
 
 	rcu_read_lock();
@@ -2042,7 +2042,11 @@ static unsigned long damos_get_node_memcg_used_bp(
 	rcu_read_unlock();
 
 	si_meminfo_node(&i, goal->nid);
-	return used_pages * PAGE_SIZE * 10000 / i.totalram;
+	if (goal->metric == DAMOS_QUOTA_NODE_MEMCG_USED_BP)
+		numerator = used_pages * PAGE_SIZE;
+	else	/* DAMOS_QUOTA_NODE_MEMCG_FREE_BP */
+		numerator = i.totalram - used_pages * PAGE_SIZE;
+	return numerator * 10000 / i.totalram;
 }
 #else
 static __kernel_ulong_t damos_get_node_mem_bp(
@@ -2092,6 +2096,7 @@ static void damos_set_quota_goal_current_value(struct damos_quota_goal *goal)
 		goal->current_value = damos_get_node_mem_bp(goal);
 		break;
 	case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
+	case DAMOS_QUOTA_NODE_MEMCG_FREE_BP:
 		goal->current_value = damos_get_node_memcg_used_bp(goal);
 		break;
 	case DAMOS_QUOTA_ACTIVE_MEM_BP:
Signature · 1 line
2.39.5
author maintainer
#12 lore raw

RFC PATCH 10/11 mm/damon/sysfs-schemes: support DAMOS_QUOTA_NODE_MEMCG_FREE_BP

In reply to SeongJae Park

Let DAMON sysfs to support DAMOS_QUOTA_NODE_MEMCG_FREE_BP.

---
 mm/damon/sysfs-schemes.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/damon/sysfs-schemes.c +5 -0
index 89e5e40bdf63..819e16151752 100644
@@ -1034,6 +1034,10 @@ static struct damos_sysfs_qgoal_metric_name damos_sysfs_qgoal_metric_names[] =
 		.metric = DAMOS_QUOTA_NODE_MEMCG_USED_BP,
 		.name = "node_memcg_used_bp",
 	},
+	{
+		.metric = DAMOS_QUOTA_NODE_MEMCG_FREE_BP,
+		.name = "node_memcg_free_bp",
+	},
 	{
 		.metric = DAMOS_QUOTA_ACTIVE_MEM_BP,
 		.name = "active_mem_bp",
@@ -2343,6 +2347,7 @@ static int damos_sysfs_add_quota_score(
 			goal->nid = sysfs_goal->nid;
 			break;
 		case DAMOS_QUOTA_NODE_MEMCG_USED_BP:
+		case DAMOS_QUOTA_NODE_MEMCG_FREE_BP:
 			err = damon_sysfs_memcg_path_to_id(
 					sysfs_goal->path, &goal->memcg_id);
 			if (err) {
Signature · 1 line
2.39.5
author maintainer
#13 lore raw

RFC PATCH 11/11 Docs/mm/damon/design: document DAMOS_QUOTA_NODE_MEMCG_FREE_BP

In reply to SeongJae Park
---
 Documentation/mm/damon/design.rst | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
Documentation/mm/damon/design.rst +6 -5
index eb6d3b7d0643..6e1f772006d9 100644
@@ -571,15 +571,16 @@ mechanism tries to make ``current_value`` of ``target_metric`` be same to
 - ``node_mem_used_bp``: Specific NUMA node's used memory ratio in bp (1/10,000).
 - ``node_mem_free_bp``: Specific NUMA node's free memory ratio in bp (1/10,000).
 - ``node_memcg_used_bp``: Specific cgroup's node used memory ratio for a
+- ``node_memcg_free_bp``: Specific cgroup's node unused memory ratio for a
   specific NUMA node, in bp (1/10,000).
 
 ``nid`` is optionally required for only ``node_mem_used_bp``,
-``node_mem_free_bp`` and ``node_memcg_used_bp`` to point the specific NUMA
-node.
+``node_mem_free_bp``, ``node_memcg_used_bp`` and ``node_memcg_free_bp`` to
+point the specific NUMA node.
 
-``path`` is optionally required for only ``node_memcg_used_bp`` to point the
-path to the cgroup.  The value should be the path of the memory cgroup from the
-cgroups mount point.
+``path`` is optionally required for only ``node_memcg_used_bp`` and
+``node_memcg_free_bp`` to point the path to the cgroup.  The value should be
+the path of the memory cgroup from the cgroups mount point.
 
 To know how user-space can set the tuning goal metric, the target value, and/or
 the current value via :ref:`DAMON sysfs interface <sysfs_interface>`, refer to
Signature · 1 line
2.39.5
author maintainer
#5 lore raw

Re: [RFC PATCH 03/11] mm/damon/core: implement DAMOS_QUOTA_NODE_MEMCG_USED_BP

In reply to SeongJae Park
Quoted 13 lines · On Thu, 19 Jun 2025 15:00:15 -0700 SeongJae Park <[email protected]> wrote:

Implement the behavior of DAMOS_QUOTA_NODE_MEMCG_USED_BP. It uses sum of active/inactive anon/file pages of a given cgroup on a given NUMA node as the value of the metric.

---
 mm/damon/core.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
mm/damon/core.c +0 -0
index d675dd932a23..1481b43f2710 100644

[...]

Quoted 7 lines
mm/damon/core.c +7 -0
+static unsigned long damos_get_node_memcg_used_bp(
+		struct damos_quota_goal *goal)
+{
+	struct mem_cgroup *memcg;
+	struct lruvec *lruvec;
+	unsigned long used_pages;
+	struct sysinfo i;

mm/damon/core.c static __kernel_ulong_t damos_get_node_mem_bp( · line 2042

Quoted 10 lines
mm/damon/core.c +10 -0
+
+	rcu_read_lock();
+	memcg = mem_cgroup_from_id(goal->memcg_id);
+	mem_cgroup_flush_stats(memcg);
+	lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(goal->nid));
+	used_pages = lruvec_page_state(lruvec, NR_ACTIVE_ANON);
+	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_ANON);
+	used_pages += lruvec_page_state(lruvec, NR_ACTIVE_FILE);
+	used_pages += lruvec_page_state(lruvec, NR_INACTIVE_FILE);
+	rcu_read_unlock();

mem_cgroup_flush_stats() calls css_rstat_flush() which may block. But the above diff is putting it inside an RCU read-side critical section. Actually only mem_cgroup_from_id() needs to be called inside an RCU read-side critical section. I will revise the next version to call rcu_read_unlock() right after mem_cgroup_from_id().

Also, mem_cgroup_from_id() could return NULL if the memcg is removeed while DAMON is running, but this diff is not checking the NULL return case. I will add the handling in the next version.

mm/damon/core.c static __kernel_ulong_t damos_get_node_mem_bp( · line 2045

Quoted 3 lines
mm/damon/core.c +3 -0
+
+	si_meminfo_node(&i, goal->nid);
+	return used_pages * PAGE_SIZE * 10000 / i.totalram;

i.totalram is in number of pages unit, but the above code is assuming it is in bytes unit. This makes the result significantly wrong. I will fix this in the next version.

Thanks,
SJ

[...]

author maintainer
#3 lore raw

Re: [RFC PATCH 02/11] mm/damon: add a new DAMOS quota goal metric for cgroup on node memory usage

In reply to SeongJae Park
Quoted 33 lines · On Thu, 19 Jun 2025 15:00:14 -0700 SeongJae Park <[email protected]> wrote:

Define a new DAMOS quota auto-tuning target metrics for per-cgroup per-node memory usage. Also extend damos_quota_goal struct to have a field for specifying the cgroup of the interest.

Note that this commit is only for DAMON kernel API extension. Real behavior of the new metric will be implemented by a following commit.

---
 include/linux/damon.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
include/linux/damon.h +2 -0
index 39b835dd3c4d..43aef959d357 100644
@@ -175,6 +175,7 @@ enum damos_action {
  * @DAMOS_QUOTA_SOME_MEM_PSI_US:	System level some memory PSI in us.
  * @DAMOS_QUOTA_NODE_MEM_USED_BP:	MemUsed ratio of a node.
  * @DAMOS_QUOTA_NODE_MEM_FREE_BP:	MemFree ratio of a node.
+ * @DAMOS_QUOTA_NODE_MEMCG_USED_BP:	MemUsed ratio of a node for a cgroup.
  * @DAMOS_QUOTA_ACTIVE_MEM_BP:		Active to inactive memory ratio.
  * @NR_DAMOS_QUOTA_GOAL_METRICS:	Number of DAMOS quota goal metrics.
  *
@@ -185,6 +186,7 @@ enum damos_quota_goal_metric {
 	DAMOS_QUOTA_SOME_MEM_PSI_US,
 	DAMOS_QUOTA_NODE_MEM_USED_BP,
 	DAMOS_QUOTA_NODE_MEM_FREE_BP,
+	DAMOS_QUOTA_NODE_MEMCG_USED_BP,
 	DAMOS_QUOTA_ACTIVE_MEM_BP,
 	NR_DAMOS_QUOTA_GOAL_METRICS,
 };

include/linux/damon.h enum damos_quota_goal_metric { · line 201

Quoted 4 lines
include/linux/damon.h +1 -0
@@ -196,6 +198,7 @@ enum damos_quota_goal_metric {
  * @current_value:	Current value of @metric.
  * @last_psi_total:	Last measured total PSI
  * @nid:		Node id.
+ * @memcg:		Memcg id.

The name of the field is not memcg but memcg_id. I will fix this in the next version.

Quoted 28 lines
include/linux/damon.h +7 -1
  * @list:		List head for siblings.
  *
  * Data structure for getting the current score of the quota tuning goal.  The
@@ -209,6 +212,9 @@ enum damos_quota_goal_metric {
  *
  * If @metric is DAMOS_QUOTA_NODE_MEM_{USED,FREE}_BP, @nid represents the node
  * id of the target node to account the used/free memory.
+ *
+ * If @metric is DAMOS_QUOTA_NODE_MEMCG_USED_BP, @nid and @memcg_id represents
+ * the node id and the cgroup to account the used memory for.
  */
 struct damos_quota_goal {
 	enum damos_quota_goal_metric metric;
@@ -217,7 +223,10 @@ struct damos_quota_goal {
 	/* metric-dependent fields */
 	union {
 		u64 last_psi_total;
-		int nid;
+		struct {
+			int nid;
+			unsigned short memcg_id;
+		};
 	};
 	struct list_head list;
 };
Signature · 1 line
2.39.5

Thanks,
SJ