Skip to contentSkip to conversation

mm/memcontrol: Make memory cgroup limits tier-aware

RFC PATCH In review Joshua Hahn 3 participants 17 messages Apr 23 – Jul 23

Subsystems (7)
lore
Share & tools
lore

Joshua Hahn

author reviewer
#0 lore raw

v2 cover mm/memcontrol: Make memory cgroup limits tier-aware

INTRODUCTION
============
Memory cgroups provide an interface that allow multiple works on a host to co-exist via weak and strong memory isolation guarantees. This works, because for the most part, all memory has equal utility. Isolating a cgroup’s memory footprint restricts how much it can hurt other workloads competing for memory, or protects it from other cgroups looking for more memory.

However, on systems with tiered memory (e.g. CXL), memory utility is no longer homogeneous; toptier and lowtier memory provide different performance characteristics and have different scarcity, meaning memory footprint no longer serves as an accurate representation of a cgroup’s consumption of the system’s limited resources. As an extreme example, a cgroup with 10G of toptier (e.g. DRAM) memory and a cgroup with 10G of lowtier (e.g. CXL) memory both appear to be consuming the same amount of system resources from memcg’s perspective, despite the performance asymmetry between the two workloads.

Therefore on tiered systems, memory isolation cannot currently happen, as workloads that are well-behaved within their memcg limits may still hurt the performance of other well-behaving workloads by hogging more than its “fair share” of toptier memory.

Introduce tier-aware memcg limits, which establish independent toptier limits that scale with the memory limits and the ratio of toptier:total memory available on the system.

INTERFACE
=========
This series introduces only one adjustable knob to userspace; a new cgroup mount option “memory_tiered_limits” which toggles whether the cgroup mount will scale toptier limits. It also introduces 4 new read-only sysfs entries per-cgroup:
memory.toptier_{min, low, high, max}.

The new toptier memory limits are scaled according to the amount of toptier memory and total memory available on the system as such:

memory.toptier_high = (toptier_mem / total_mem) * memory.high

For instance, on a host with 100GB memory, with 75G toptier and 25G CXL, the “toptier ratio” would be 75 / 100 = 0.75. A cgroup with the following memcg limits {min: 8G, low: 12G, high: 20G, max: 24G} might see toptier limits scaled at {min: 6G, low: 9G, high: 15G, max: 18G}.

USE CASES
=========
There are workloads that benefit from tiered memory limits, and those that do not. Explicitly, hosts containing multiple workloads with the goal of maximizing host-level throughput may see a regression because fairness is not free; it comes at the cost of underutilized toptier memory, overhead to manage memory migrations, and host-level memory hotness inversion.

On the other hand, fairness can prove to be a valuable resource for a number of configurations, especially with workloads that want to raise the lower bound on performance, rather than optimize for raw throughput:
- VM hosting services that must provide the maximal performance guarantee (i.e. supremum) for any workload present on a host.
- Database workloads that want to minimize the maximum latency (i.e. infimum) for queries hosted on the host.
- Hosts running memory-isolated sharded workloads that blocks progress until the last shard terminates.
- Any workload that wants to minimize variance, as a means to gather measurable gains in performance over time.

TESTING
=======
To demonstrate the fairness and minimum performance guarantee increases, I performed some performance tests across three data access patterns. All tests were done on a 1T host with 750G DRAM and 250G CXL, spawning 4 220G workloads {memory.high == memory.max == 220G}. 3 of those workloads are “memory hogs”, who get to run on the host and pre-allocate all of their memory. The last workload is the “victim”, who only gets to run once the other 3 workloads have already allocated their memory. Once the victim allocates its memory as well, we begin measuring read times for the following setups:
1. random memory access in the 220G anon region
2. hot / cold memory access, where the hot region (100G) gets 90% of the reads, and the cold region (120G) gets 10% of the reads

First, let’s look at what the results look like with NUMAB=2:

Per-cgroup throughput (Mops/s):

Cgroup     Baseline   Tier-Aware
------     --------   ----------
hog          21.457       17.733
hog          22.773       16.329
hog          22.630       16.549
victim       12.315       16.950

DRAM / CXL distribution (GB):

Cgroup          Baseline              Tier-Aware
------          --------              ----------
hog      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
hog      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
hog      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
victim   69.3 DRAM  / 150.7 CXL  186.7 DRAM / 33.3 CXL

Experiment 2 (hot / cold access)

Per-cgroup throughput (Mops/s):

Cgroup     Baseline   Tier-Aware
------     --------   ----------
wl0          24.280       17.815
wl1          23.929       15.019
wl2          23.645       15.605
wl3          11.624       15.998

DRAM / CXL distribution (GB):

Cgroup          Baseline               Tier-Aware
------          --------               ----------
wl0      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
wl1      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
wl2      220.0 DRAM / 0.0 CXL    181.6 DRAM / 38.4 CXL
wl3      70.4 DRAM  / 149.6 CXL  186.7 DRAM / 33.3 CXL

With NUMAB=0, the pattern remains the same, but overall, throughput seems increased, and variance seems decreased.
I believe there is a negative interaction here between NUMA balancing’s host-level hotness tracking, and the tier-aware memcg limit’s push to make memcg-aware migration decisions (see open questions below).

The results above demonstrate the desired effect of fairly distributing CXL usage across the workloads regardless of when they were launched, and minimizing performance variance.

OPEN QUESTIONS (for mailing list & for LSFMMBPF)
================================================
1. Should memory.toptier_max be enforced? And if so, what should it look like? In my testing, I have found that enforcing memory.toptier_max in the same way as memory.max leads to significant throttling, as each allocation above the toptier limit causes a loop of allocate on toptier --> scan toptier LRU for victim --> demote victim page --> allocate on toptier...
Thus, in my test above, I ran with the last patch (memory.toptier_max enforcement) disabled. Are there use-cases for enforcing memory.toptier_max? For this RFC, I’ve included it for review, but I feel that it makes sense to drop toptier enforcement.
2. This version of the code does its best to generalize the memcg stock system as much as possible, but still only makes a distinction between toptier / lowtier. Does it make sense to support 3+ tiers? Are there currently real systems / hardware out there that desires to enforce fairness at that scale?
2-1. Should swap be considered its own tier?
3. Should users be able to tune anything? Currently, the only choice is for users to enable the limits or not. Options for userspace tuning include:
setting cgroup-wide toptier limits; system-wide toptier:lowtier ratios; cgroup-level toptier:lowtier ratios.
4. Tiered memcg limits interfere with existing promotion mechanisms like NUMA balancing (NUMAB2), that promote memory on a systemwide basis, ignoring process and memcg contexts. What kinds of promotion mechanisms should be used to work in memcg-aware contexts?

DEPENDENCIES
============
This work is built upon my recent RFC [1] to move stocks from the memcg level to the page_counter level, to make the toptier charging path cheaper. In addition, this patch is limited to working on LRU folios; kmem memory and memory that is otherwise not charged on an lruvec-basis (i.e. has both physical node & memcg information; aka enum memcg_stat_item) is not accounted for. There are landed & ongoing efforts to introduce per-lruvec accounting for these as well:
- vmalloc (from Johannes): mm-stable [2]
- zswap / zswapped / zswap_incompressible [3]
- percpu: in progress [4]

CHANGELOG V1 --> V2
===================
- The toptier:total ratio calculation has been simplified to ignore cpusets and now exist as a system-wide ratio. This came from the realization that having cgroups that opt-in and opt-out of CXL co-existing on the system leads to a question on how the limits should be enforced, and whether such a configuration is even desirable.
- The simplification above means struct page_counter can be per-memcg, not mem_cgroup_per_node.
- Independent memcg stock management for toptier
- Included min / max enforcement (for max, see questions above)
- Exported toptier limits as read-only sysfs files
- Turned the build config into a mount option, as suggested by Michal Hocko

Thank you for reading this long cover letter. Have a great day everyone!

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
[4] https://lore.kernel.org/all/[email protected]/

Joshua Hahn (9):
cgroup: Introduce memory_tiered_limits cgroup mount option mm/memory-tiers: Introduce toptier utility functions mm/memcontrol: Refactor page_counter charging in try_charge_memcg mm/memcontrol: charge/uncharge toptier memory to mem_cgroup mm/memcontrol: Set toptier limits proportional to memory limits mm/vmscan, memcontrol: Add nodemask to try_to_free_mem_cgroup_pages mm/memcontrol: Make memory.low and memory.min tier-aware mm/memcontrol: Make memory.high tier-aware mm/memcontrol: Make memory.max tier-aware

 include/linux/cgroup-defs.h  |   5 +
 include/linux/memcontrol.h   |  35 ++++
 include/linux/memory-tiers.h |  17 ++
 include/linux/swap.h         |   3 +-
 kernel/cgroup/cgroup.c       |  12 ++
 mm/memcontrol-v1.c           |   6 +-
 mm/memcontrol.c              | 306 +++++++++++++++++++++++++++++++++++++----
 mm/memory-tiers.c            |  46 +++++-
 mm/vmscan.c                  |  11 +-
 9 files changed, 402 insertions(+), 39 deletions(-)
Signature · 1 line
2.52.0
author reviewer
#1 lore raw

RFC PATCH v2 1/9 cgroup: Introduce memory_tiered_limits cgroup mount option

Introduce a cgroup mount option memory_tiered_limits to enable tier-proportional scaling of the memory cgroup controller limits memory.{min, low, high, max}.

The mount option currently does not have any effect. Later commits will scale memcg limits proportional to the system's toptier:total capacity ratio.

---
 include/linux/cgroup-defs.h |  5 +++++
 include/linux/memcontrol.h  | 14 ++++++++++++++
 kernel/cgroup/cgroup.c      | 12 ++++++++++++
 3 files changed, 31 insertions(+)
include/linux/cgroup-defs.h +5 -0
index bb92f5c169ca2..0b6861f4faece 100644
@@ -128,6 +128,11 @@ enum {
 	 * Enable legacy local pids.events.
 	 */
 	CGRP_ROOT_PIDS_LOCAL_EVENTS = (1 << 20),
+
+	/*
+	 * Enable tier-proportional scaling of limits for the memory controller.
+	 */
+	CGRP_ROOT_MEMORY_TIERED_LIMITS = (1 << 21),
 };
 
 /* cftype->flags */
include/linux/memcontrol.h +14 -0
index dc3fa687759b4..be45641e890e4 100644
@@ -533,6 +533,15 @@ static inline bool mem_cgroup_disabled(void)
 	return !cgroup_subsys_enabled(memory_cgrp_subsys);
 }
 
+static inline bool mem_cgroup_tiered_limits(void)
+{
+#ifdef CONFIG_NUMA
+	return cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_TIERED_LIMITS;
+#else
+	return false;
+#endif
+}
+
 static inline void mem_cgroup_protection(struct mem_cgroup *root,
 					 struct mem_cgroup *memcg,
 					 unsigned long *min,
@@ -1084,6 +1093,11 @@ static inline bool mem_cgroup_disabled(void)
 	return true;
 }
 
+static inline bool mem_cgroup_tiered_limits(void)
+{
+	return false;
+}
+
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
 				      enum memcg_memory_event event)
 {
kernel/cgroup/cgroup.c +12 -0
index babf7b4560488..6a34d0e179dc5 100644
@@ -1989,6 +1989,7 @@ enum cgroup2_param {
 	Opt_memory_recursiveprot,
 	Opt_memory_hugetlb_accounting,
 	Opt_pids_localevents,
+	Opt_memory_tiered_limits,
 	nr__cgroup2_params
 };
 
@@ -1999,6 +2000,7 @@ static const struct fs_parameter_spec cgroup2_fs_parameters[] = {
 	fsparam_flag("memory_recursiveprot",	Opt_memory_recursiveprot),
 	fsparam_flag("memory_hugetlb_accounting", Opt_memory_hugetlb_accounting),
 	fsparam_flag("pids_localevents",	Opt_pids_localevents),
+	fsparam_flag("memory_tiered_limits",	Opt_memory_tiered_limits),
 	{}
 };
 
@@ -2031,6 +2033,9 @@ static int cgroup2_parse_param(struct fs_context *fc, struct fs_parameter *param
 	case Opt_pids_localevents:
 		ctx->flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS;
 		return 0;
+	case Opt_memory_tiered_limits:
+		ctx->flags |= CGRP_ROOT_MEMORY_TIERED_LIMITS;
+		return 0;
 	}
 	return -EINVAL;
 }
@@ -2072,6 +2077,11 @@ static void apply_cgroup_root_flags(unsigned int root_flags)
 			cgrp_dfl_root.flags |= CGRP_ROOT_PIDS_LOCAL_EVENTS;
 		else
 			cgrp_dfl_root.flags &= ~CGRP_ROOT_PIDS_LOCAL_EVENTS;
+
+		if (root_flags & CGRP_ROOT_MEMORY_TIERED_LIMITS)
+			cgrp_dfl_root.flags |= CGRP_ROOT_MEMORY_TIERED_LIMITS;
+		else
+			cgrp_dfl_root.flags &= ~CGRP_ROOT_MEMORY_TIERED_LIMITS;
 	}
 }
 
@@ -2089,6 +2099,8 @@ static int cgroup_show_options(struct seq_file *seq, struct kernfs_root *kf_root
 		seq_puts(seq, ",memory_hugetlb_accounting");
 	if (cgrp_dfl_root.flags & CGRP_ROOT_PIDS_LOCAL_EVENTS)
 		seq_puts(seq, ",pids_localevents");
+	if (cgrp_dfl_root.flags & CGRP_ROOT_MEMORY_TIERED_LIMITS)
+		seq_puts(seq, ",memory_tiered_limits");
 	return 0;
 }
 
Signature · 1 line
2.52.0
author reviewer
#2 lore raw

RFC PATCH v2 2/9 mm/memory-tiers: Introduce toptier utility functions

In reply to Joshua Hahn

This patch introduces two toptier-related utility functions, get_toptier_nodemask() and mt_scale_by_toptier().

Tier aware limits will introduce new memcg thresholds on toptier nodes for systems with multiple memory tiers. To simplify the calculation for these new thresholds, introduce a function mt_scale_by_toptier to scale memory limits by the ratio of toptier capacity and total capacity available on the system.

For single-node / single-tier systems, the scaling operation will be a no-op since capacity updates are hooked into establish_demotion_targets.

Note that the ratio is static for the entire system. Explicitly, it does not take cgroups' cpuset.mems into consideration, meaning even cgroups limited to toptier nodes only will still get a scaled down toptier limit.

This is to ensure that all cgroups are limited to their fair share of toptier memory, regardless of what nodes they are restricted to. This also has the added benefit of preventing accidental /unintentional overcommitting of toptier memory, since every cgroup shares the same toptier ratio.

get_toptier_nodemask() extends the existing node_is_toptier check to return a nodemask of all N_MEMORY nodes living on toptier. For !CONFIG_NUMA_MIGRATION or !CONFIG_NUMA systems, it will just return all N_MEMORY nodes.

---
 include/linux/memory-tiers.h | 17 ++++++++++++++++
 mm/memory-tiers.c            | 38 ++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
include/linux/memory-tiers.h +17 -0
index 7999c58629eeb..f21525c50a5ff 100644
@@ -52,10 +52,12 @@ int mt_perf_to_adistance(struct access_coordinate *perf, int *adist);
 struct memory_dev_type *mt_find_alloc_memory_type(int adist,
 						  struct list_head *memory_types);
 void mt_put_memory_types(struct list_head *memory_types);
+unsigned long mt_scale_by_toptier(unsigned long val);
 #ifdef CONFIG_NUMA_MIGRATION
 int next_demotion_node(int node, const nodemask_t *allowed_mask);
 void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets);
 bool node_is_toptier(int node);
+void get_toptier_nodemask(nodemask_t *mask);
 #else
 static inline int next_demotion_node(int node, const nodemask_t *allowed_mask)
 {
@@ -71,6 +73,11 @@ static inline bool node_is_toptier(int node)
 {
 	return true;
 }
+
+static inline void get_toptier_nodemask(nodemask_t *mask)
+{
+	*mask = node_states[N_MEMORY];
+}
 #endif
 
 #else
@@ -116,6 +123,11 @@ static inline bool node_is_toptier(int node)
 	return true;
 }
 
+static inline void get_toptier_nodemask(nodemask_t *mask)
+{
+	*mask = node_states[N_MEMORY];
+}
+
 static inline int register_mt_adistance_algorithm(struct notifier_block *nb)
 {
 	return 0;
@@ -151,5 +163,10 @@ static inline struct memory_dev_type *mt_find_alloc_memory_type(int adist,
 static inline void mt_put_memory_types(struct list_head *memory_types)
 {
 }
+
+static inline unsigned long mt_scale_by_toptier(unsigned long val)
+{
+	return val;
+}
 #endif	/* CONFIG_NUMA */
 #endif  /* _LINUX_MEMORY_TIERS_H */
mm/memory-tiers.c +38 -0
index 54851d8a195b0..acc02679e312d 100644
@@ -46,6 +46,8 @@ static struct node_memory_type_map node_memory_types[MAX_NUMNODES];
 struct memory_dev_type *default_dram_type;
 nodemask_t default_dram_nodes __initdata = NODE_MASK_NONE;
 
+static unsigned long toptier_capacity;
+
 static const struct bus_type memory_tier_subsys = {
 	.name = "memory_tiering",
 	.dev_name = "memory_tier",
@@ -299,6 +301,17 @@ bool node_is_toptier(int node)
 	return toptier;
 }
 
+void get_toptier_nodemask(nodemask_t *mask)
+{
+	int node;
+
+	nodes_clear(*mask);
+	for_each_node_state(node, N_MEMORY) {
+		if (node_is_toptier(node))
+			node_set(node, *mask);
+	}
+}
+
 void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets)
 {
 	struct memory_tier *memtier;
@@ -428,6 +441,7 @@ static void establish_demotion_targets(void)
 	struct demotion_nodes *nd;
 	int target = NUMA_NO_NODE, node;
 	int distance, best_distance;
+	int i;
 	nodemask_t tier_nodes, lower_tier;
 
 	lockdep_assert_held_once(&memory_tier_lock);
@@ -496,6 +510,19 @@ static void establish_demotion_targets(void)
 			break;
 		}
 	}
+
+	toptier_capacity = 0;
+	for_each_node_state(node, N_MEMORY) {
+		if (!node_is_toptier(node))
+			continue;
+
+		for (i = 0; i < MAX_NR_ZONES; i++) {
+			struct zone *z = &NODE_DATA(node)->node_zones[i];
+
+			toptier_capacity += zone_managed_pages(z);
+		}
+	}
+
 	/*
 	 * Now build the lower_tier mask for each node collecting node mask from
 	 * all memory tier below it. This allows us to fallback demotion page
@@ -878,6 +905,16 @@ int mt_calc_adistance(int node, int *adist)
 }
 EXPORT_SYMBOL_GPL(mt_calc_adistance);
 
+unsigned long mt_scale_by_toptier(unsigned long val)
+{
+	unsigned long total_capacity = totalram_pages();
+
+	if (!total_capacity)
+		return 0;
+
+	return mult_frac(val, toptier_capacity, total_capacity);
+}
+
 static int __meminit memtier_hotplug_callback(struct notifier_block *self,
 					      unsigned long action, void *_arg)
 {
@@ -932,6 +969,7 @@ static int __init memory_tier_init(void)
 		  node_states[N_CPU]);
 
 	hotplug_node_notifier(memtier_hotplug_callback, MEMTIER_HOTPLUG_PRI);
+	toptier_capacity = totalram_pages();
 	return 0;
 }
 subsys_initcall(memory_tier_init);
Signature · 1 line
2.52.0
author reviewer
#3 lore raw

RFC PATCH v2 3/9 mm/memcontrol: Refactor page_counter charging in try_charge_memcg

In reply to Joshua Hahn

In preparation for adding charging and uncharging of a new page_counter toptier to try_charge_memcg, refactor the code so that it is easier to roll back partial charges when any one of the three page_counters fail to charge.

No functional changes intended.

---
 mm/memcontrol.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
mm/memcontrol.c +12 -8
index 7de23ecd7cef6..8f7bedb55dbb1 100644
@@ -2385,18 +2385,22 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 
 retry:
 	reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
-	if (!do_memsw_account() ||
-	    page_counter_try_charge(&memcg->memsw, nr_pages, &counter)) {
-		if (page_counter_try_charge(&memcg->memory, nr_pages, &counter))
-			goto done_restock;
-		if (do_memsw_account())
-			page_counter_uncharge(&memcg->memsw, nr_pages);
-		mem_over_limit = mem_cgroup_from_counter(counter, memory);
-	} else {
+
+	if (do_memsw_account() &&
+	    !page_counter_try_charge(&memcg->memsw, nr_pages, &counter)) {
 		mem_over_limit = mem_cgroup_from_counter(counter, memsw);
 		reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP;
+		goto reclaim;
 	}
 
+	if (page_counter_try_charge(&memcg->memory, nr_pages, &counter))
+		goto done_restock;
+
+	if (do_memsw_account())
+		page_counter_uncharge(&memcg->memsw, nr_pages);
+	mem_over_limit = mem_cgroup_from_counter(counter, memory);
+
+reclaim:
 	/*
 	 * Prevent unbounded recursion when reclaim operations need to
 	 * allocate memory. This might exceed the limits temporarily,
Signature · 1 line
2.52.0
author reviewer
#4 lore raw

RFC PATCH v2 4/9 mm/memcontrol: charge/uncharge toptier memory to mem_cgroup

In reply to Joshua Hahn

Memory cgroup limits currently offer a way to isolate memory as a resource, but treats the cost/value of all memory to be equal, regardless of whether it is present in a toptier node or not.

To better capture the asymmetric utility of toptier memory from "lowtier" memory, account toptier memory usage in parallel to existing memory accounting mechanisms. To do this, introduce a new page_counter "toptier" to mem_cgroup.

From a simplified perspective, we can achieve this by checking the physical location of folios when the memory page_counter is updated, and decide whether to also account to toptier. Add a new "toptier" parameter to try_charge_memcg(), which callers must determine.

However, as of this RFC, this simplified model only works on LRU folios (callers of try_charge_memcg() from charge_memcg()). The other two sites, obj_cgroup_charge_pages() and mem_cgroup_sk_charge(), will be addressed in future patches that transition enum memcg_stat_item to a per-lruvec counter (enum memcg_stat_item).

Enforcement mechanisms are not present at this point. Failing the toptier limit check leads to nothing, but the charges are accumulated.

---
 include/linux/memcontrol.h |  1 +
 mm/memcontrol.c            | 63 ++++++++++++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 6 deletions(-)
include/linux/memcontrol.h +1 -0
index be45641e890e4..0cdb6cd1955dc 100644
@@ -206,6 +206,7 @@ struct mem_cgroup {
 
 	/* Accounted resources */
 	struct page_counter memory;		/* Both v1 & v2 */
+	struct page_counter toptier;		/* v2 only */
 
 	union {
 		struct page_counter swap;	/* v2 only */
mm/memcontrol.c +57 -6

198 lines not on this page · raw message

Signature · 1 line
2.52.0
author reviewer
#5 lore raw

RFC PATCH v2 5/9 mm/memcontrol: Set toptier limits proportional to memory limits

In reply to Joshua Hahn

Compute proportional toptier limits based on memory limits when users write to memory limit sysfs files, or when memory hotplug causes the toptier capacity / total capacity ratio to be shifted.

Also introduce new read-only cgroup files memory.toptier_{min,low,high,max} to expose the derived toptier limits.

---
 include/linux/memcontrol.h | 12 +++++
 mm/memcontrol.c            | 93 ++++++++++++++++++++++++++++++++++++++
 mm/memory-tiers.c          |  8 +++-
 3 files changed, 111 insertions(+), 2 deletions(-)
include/linux/memcontrol.h +12 -0
index 0cdb6cd1955dc..6bcb866440075 100644
@@ -543,6 +543,14 @@ static inline bool mem_cgroup_tiered_limits(void)
 #endif
 }
 
+#ifdef CONFIG_NUMA
+void update_memcg_toptier_limits(void);
+#else
+static inline void update_memcg_toptier_limits(void)
+{
+}
+#endif
+
 static inline void mem_cgroup_protection(struct mem_cgroup *root,
 					 struct mem_cgroup *memcg,
 					 unsigned long *min,
@@ -1099,6 +1107,10 @@ static inline bool mem_cgroup_tiered_limits(void)
 	return false;
 }
 
+static inline void update_memcg_toptier_limits(void)
+{
+}
+
 static inline void memcg_memory_event(struct mem_cgroup *memcg,
 				      enum memcg_memory_event event)
 {
mm/memcontrol.c +93 -0

171 lines not on this page · raw message

mm/memory-tiers.c +6 -2
index acc02679e312d..ddcc11e3919da 100644
@@ -924,15 +924,19 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
 	switch (action) {
 	case NODE_REMOVED_LAST_MEMORY:
 		mutex_lock(&memory_tier_lock);
-		if (clear_node_memory_tier(nn->nid))
+		if (clear_node_memory_tier(nn->nid)) {
 			establish_demotion_targets();
+			update_memcg_toptier_limits();
+		}
 		mutex_unlock(&memory_tier_lock);
 		break;
 	case NODE_ADDED_FIRST_MEMORY:
 		mutex_lock(&memory_tier_lock);
 		memtier = set_node_memory_tier(nn->nid);
-		if (!IS_ERR(memtier))
+		if (!IS_ERR(memtier)) {
 			establish_demotion_targets();
+			update_memcg_toptier_limits();
+		}
 		mutex_unlock(&memory_tier_lock);
 		break;
 	}
Signature · 1 line
2.52.0
author reviewer
#6 lore raw

RFC PATCH v2 6/9 mm/vmscan, memcontrol: Add nodemask to try_to_free_mem_cgroup_pages

In reply to Joshua Hahn

Add a new nodemask parameter to try_to_free_mem_cgroup_pages to allow selective reclaim on certain nodes. This new function signature can be used in future patches to selectively perform reclaim on toptier and place downward pressure when toptier limits are breached but memcg-wide limits are not yet breached.

All callers pass NULL to the new nodemask, so there are no functional changes with this patch.

---
 include/linux/swap.h |  3 ++-
 mm/memcontrol-v1.c   |  6 ++++--
 mm/memcontrol.c      | 11 +++++++----
 mm/vmscan.c          | 11 ++++++-----
 4 files changed, 19 insertions(+), 12 deletions(-)
include/linux/swap.h +2 -1
index 1930f81e6be4d..493dd99f3165a 100644
@@ -367,7 +367,8 @@ extern unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 						  unsigned long nr_pages,
 						  gfp_t gfp_mask,
 						  unsigned int reclaim_options,
-						  int *swappiness);
+						  int *swappiness,
+						  nodemask_t *allowed);
 extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem,
 						gfp_t gfp_mask, bool noswap,
 						pg_data_t *pgdat,
mm/memcontrol-v1.c +4 -2
index 433bba9dfe715..03df1cc71842c 100644
@@ -1500,7 +1500,8 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
 		}
 
 		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
-				memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) {
+				memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP,
+				NULL, NULL)) {
 			ret = -EBUSY;
 			break;
 		}
@@ -1532,7 +1533,8 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
 			return -EINTR;
 
 		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
-						  MEMCG_RECLAIM_MAY_SWAP, NULL))
+						  MEMCG_RECLAIM_MAY_SWAP,
+						  NULL, NULL))
 			nr_retries--;
 	}
 
mm/memcontrol.c +7 -4
index 3acb06388405c..3fb1ee1d18603 100644
@@ -2123,7 +2123,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 		nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
 							gfp_mask,
 							MEMCG_RECLAIM_MAY_SWAP,
-							NULL);
+							NULL, NULL);
 		psi_memstall_leave(&pflags);
 	} while ((memcg = parent_mem_cgroup(memcg)) &&
 		 !mem_cgroup_is_root(memcg));
@@ -2432,7 +2432,8 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
-						    gfp_mask, reclaim_options, NULL);
+						    gfp_mask, reclaim_options,
+						    NULL, NULL);
 	psi_memstall_leave(&pflags);
 
 	if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
@@ -4591,7 +4592,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
 		}
 
 		reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
-					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL);
+					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
+					NULL, NULL);
 
 		if (!reclaimed && !nr_retries--)
 			break;
@@ -4651,7 +4653,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 
 		if (nr_reclaims) {
 			if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
-					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL))
+					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
+					NULL, NULL))
 				nr_reclaims--;
 			continue;
 		}
mm/vmscan.c +6 -5
index 5a8c8fcccbfc9..615aa0c899dad 100644
@@ -6807,7 +6807,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 					   unsigned long nr_pages,
 					   gfp_t gfp_mask,
 					   unsigned int reclaim_options,
-					   int *swappiness)
+					   int *swappiness, nodemask_t *allowed)
 {
 	unsigned long nr_reclaimed;
 	unsigned int noreclaim_flag;
@@ -6823,6 +6823,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 		.may_unmap = 1,
 		.may_swap = !!(reclaim_options & MEMCG_RECLAIM_MAY_SWAP),
 		.proactive = !!(reclaim_options & MEMCG_RECLAIM_PROACTIVE),
+		.nodemask = allowed,
 	};
 	/*
 	 * Traverse the ZONELIST_FALLBACK zonelist of the current node to put
@@ -6848,7 +6849,7 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 					   unsigned long nr_pages,
 					   gfp_t gfp_mask,
 					   unsigned int reclaim_options,
-					   int *swappiness)
+					   int *swappiness, nodemask_t *allowed)
 {
 	return 0;
 }
@@ -7964,9 +7965,9 @@ int user_proactive_reclaim(char *buf,
 			reclaim_options = MEMCG_RECLAIM_MAY_SWAP |
 					  MEMCG_RECLAIM_PROACTIVE;
 			reclaimed = try_to_free_mem_cgroup_pages(memcg,
-						 batch_size, gfp_mask,
-						 reclaim_options,
-						 swappiness == -1 ? NULL : &swappiness);
+					batch_size, gfp_mask, reclaim_options,
+					swappiness == -1 ? NULL : &swappiness,
+					NULL);
 		} else {
 			struct scan_control sc = {
 				.gfp_mask = current_gfp_context(gfp_mask),
Signature · 1 line
2.52.0
author reviewer
#7 lore raw

RFC PATCH v2 7/9 mm/memcontrol: Make memory.low and memory.min tier-aware

In reply to Joshua Hahn

On machines serving multiple workloads whose memory is isolated via the memory cgroup controller, it is currently impossible to enforce a fair distribution of toptier memory among the workloads, as the only enforceable limits have to do with total memory footprint, but not where that memory resides.

This makes ensuring a consistent and baseline performance difficult, as each workload's performance is heavily impacted by workload-external factors such as which other workloads are co-located in the same host, and the order at which different workloads are started.

Extend the existing memory.{low, min} protection to be tier-aware in order to enforce proportional best-effort and guaranteed memory protection of toptier memory.

---
 include/linux/memcontrol.h | 8 ++++++++
 mm/memcontrol.c            | 3 +++
 2 files changed, 11 insertions(+)
include/linux/memcontrol.h +8 -0
index 6bcb866440075..2222b390ebf10 100644
@@ -624,6 +624,10 @@ static inline bool mem_cgroup_below_low(struct mem_cgroup *target,
 	if (mem_cgroup_unprotected(target, memcg))
 		return false;
 
+	if (mem_cgroup_tiered_limits() && READ_ONCE(memcg->toptier.elow) >=
+					  page_counter_read(&memcg->toptier))
+		return true;
+
 	return READ_ONCE(memcg->memory.elow) >=
 		page_counter_read(&memcg->memory);
 }
@@ -634,6 +638,10 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,
 	if (mem_cgroup_unprotected(target, memcg))
 		return false;
 
+	if (mem_cgroup_tiered_limits() && READ_ONCE(memcg->toptier.emin) >=
+					  page_counter_read(&memcg->toptier))
+		return true;
+
 	return READ_ONCE(memcg->memory.emin) >=
 		page_counter_read(&memcg->memory);
 }
mm/memcontrol.c +3 -0
index 3fb1ee1d18603..b115ff40e268d 100644
@@ -4933,6 +4933,9 @@ void mem_cgroup_calculate_protection(struct mem_cgroup *root,
 		root = root_mem_cgroup;
 
 	page_counter_calculate_protection(&root->memory, &memcg->memory, recursive_protection);
+	if (mem_cgroup_tiered_limits())
+		page_counter_calculate_protection(&root->toptier,
+				&memcg->toptier, recursive_protection);
 }
 
 static int charge_memcg(struct folio *folio, struct mem_cgroup *memcg,
Signature · 1 line
2.52.0
author reviewer
#8 lore raw

RFC PATCH v2 8/9 mm/memcontrol: Make memory.high tier-aware

In reply to Joshua Hahn

On machines serving multiple workloads whose memory is isolated via the memory cgroup controller, it is currently impossible to enforce a fair distribution of toptier memory among the workloads, as the limits only enforce total memory footprint, but not where that memory resides.

This makes ensuring consistent baseline performance difficult, as each workload's performance is heavily impacted by workload-external factors such as which other workloads are co-located in the same host, and the order in which the workloads are started.

Extend the existing memory.high protection to be tier-aware.

Depending on the combination of limit breaches, selectively reclaim on toptier nodes: when memory.high is breached, perform reclaim on all nodes. When memory.high is safe but toptier.high is breached, perform targeted reclaim on toptier nodes only.

Also, throttle allocations when toptier is breached as well, making sure not to double-penalize when both toptier and memory limits are met.

---
 mm/memcontrol.c | 82 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 10 deletions(-)
mm/memcontrol.c +72 -10
index b115ff40e268d..e5f39830d250d 100644
@@ -2112,10 +2112,25 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 
 	do {
 		unsigned long pflags;
+		nodemask_t toptier_nodes;
+		nodemask_t *reclaim_targets = NULL;
 
 		if (page_counter_read(&memcg->memory) <=
-		    READ_ONCE(memcg->memory.high))
-			continue;
+		    READ_ONCE(memcg->memory.high)) {
+			if (!mem_cgroup_tiered_limits())
+				continue;
+
+			/*
+			 * Even if the memcg is under the memory limit, toptier
+			 * may have breached the toptier limit. Engage
+			 * targeted reclaim on toptier nodes if so.
+			 */
+			if (page_counter_read(&memcg->toptier) <=
+			    READ_ONCE(memcg->toptier.high))
+				continue;
+			get_toptier_nodemask(&toptier_nodes);
+			reclaim_targets = &toptier_nodes;
+		}
 
 		memcg_memory_event(memcg, MEMCG_HIGH);
 
@@ -2123,7 +2138,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 		nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
 							gfp_mask,
 							MEMCG_RECLAIM_MAY_SWAP,
-							NULL, NULL);
+							NULL, reclaim_targets);
 		psi_memstall_leave(&pflags);
 	} while ((memcg = parent_mem_cgroup(memcg)) &&
 		 !mem_cgroup_is_root(memcg));
@@ -2224,6 +2239,23 @@ static u64 mem_find_max_overage(struct mem_cgroup *memcg)
 	return max_overage;
 }
 
+static u64 toptier_find_max_overage(struct mem_cgroup *memcg)
+{
+	u64 overage, max_overage = 0;
+
+	if (!mem_cgroup_tiered_limits())
+		return 0;
+
+	do {
+		overage = calculate_overage(page_counter_read(&memcg->toptier),
+					    READ_ONCE(memcg->toptier.high));
+		max_overage = max(overage, max_overage);
+	} while ((memcg = parent_mem_cgroup(memcg)) &&
+		  !mem_cgroup_is_root(memcg));
+
+	return max_overage;
+}
+
 static u64 swap_find_max_overage(struct mem_cgroup *memcg)
 {
 	u64 overage, max_overage = 0;
@@ -2326,6 +2358,14 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
 	penalty_jiffies = calculate_high_delay(memcg, nr_pages,
 					       mem_find_max_overage(memcg));
 
+	/*
+	 * Don't double-penalize for toptier high overage if memory.high
+	 * overage penalization has already been accounted for.
+	 */
+	if (!penalty_jiffies)
+		penalty_jiffies += calculate_high_delay(memcg, nr_pages,
+					toptier_find_max_overage(memcg));
+
 	penalty_jiffies += calculate_high_delay(memcg, nr_pages,
 						swap_find_max_overage(memcg));
 
@@ -2522,22 +2562,26 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	 */
 	do {
 		bool mem_high, swap_high;
+		bool toptier_high = false;
 
 		mem_high = page_counter_read(&memcg->memory) >
 			READ_ONCE(memcg->memory.high);
 		swap_high = page_counter_read(&memcg->swap) >
 			READ_ONCE(memcg->swap.high);
+		toptier_high = mem_cgroup_tiered_limits() &&
+			       page_counter_read(&memcg->toptier) >
+			       READ_ONCE(memcg->toptier.high);
 
 		/* Don't bother a random interrupted task */
 		if (!in_task()) {
-			if (mem_high) {
+			if (mem_high || toptier_high) {
 				schedule_work(&memcg->high_work);
 				break;
 			}
 			continue;
 		}
 
-		if (mem_high || swap_high) {
+		if (mem_high || swap_high || toptier_high) {
 			/*
 			 * The allocating tasks in this cgroup will need to do
 			 * reclaim or be throttled to prevent further growth
@@ -4577,10 +4621,28 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
 
 	for (;;) {
 		unsigned long nr_pages = page_counter_read(&memcg->memory);
-		unsigned long reclaimed;
+		unsigned long reclaimed, charge;
+		nodemask_t toptier_nodes;
+		nodemask_t *reclaim_targets = NULL;
 
-		if (nr_pages <= high)
-			break;
+		if (nr_pages <= high) {
+			unsigned long toptier_nr_pages, toptier_high;
+
+			if (!mem_cgroup_tiered_limits())
+				break;
+
+			toptier_nr_pages = page_counter_read(&memcg->toptier);
+			toptier_high = READ_ONCE(memcg->toptier.high);
+
+			if (toptier_nr_pages <= toptier_high)
+				break;
+
+			get_toptier_nodemask(&toptier_nodes);
+			reclaim_targets = &toptier_nodes;
+			charge = toptier_nr_pages - toptier_high;
+		} else {
+			charge = nr_pages - high;
+		}
 
 		if (signal_pending(current))
 			break;
@@ -4591,9 +4653,9 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
 			continue;
 		}
 
-		reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
+		reclaimed = try_to_free_mem_cgroup_pages(memcg, charge,
 					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
-					NULL, NULL);
+					NULL, reclaim_targets);
 
 		if (!reclaimed && !nr_retries--)
 			break;
Signature · 1 line
2.52.0
author reviewer
#9 lore raw

RFC PATCH v2 9/9 mm/memcontrol: Make memory.max tier-aware

In reply to Joshua Hahn

On machines serving multiple workloads whose memory is isolated via the memory cgroup controller, it is currently impossible to enforce a fair distribution of toptier memory among the workloads, as the limits only enforce total memory footprint, but not where that memory resides.

This makes ensuring consistent baseline performance difficult, as each workload's performance is heavily impacted by workload-external factors such as which other workloads are co-located in the same host, and the order in which the workloads are started.

Extend the existing memory.max protection to be tier-aware.

Depending on the combination of limit breaches, selectively reclaim on toptier nodes: when memory.max is breached, perform reclaim on all nodes. When memory.max is safe but toptier.max is breached, perform targeted reclaim on toptier nodes only.

---
 mm/memcontrol.c | 56 ++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 44 insertions(+), 12 deletions(-)
mm/memcontrol.c +44 -12
index e5f39830d250d..d8d67ada993ff 100644
@@ -1518,6 +1518,15 @@ static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
 	if (count < limit)
 		margin = limit - count;
 
+	if (mem_cgroup_tiered_limits()) {
+		count = page_counter_read(&memcg->toptier);
+		limit = READ_ONCE(memcg->toptier.max);
+		if (count < limit)
+			margin = min(margin, limit - count);
+		else
+			margin = 0;
+	}
+
 	if (do_memsw_account()) {
 		count = page_counter_read(&memcg->memsw);
 		limit = READ_ONCE(memcg->memsw.max);
@@ -2424,11 +2433,12 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	bool raised_max_event = false;
 	unsigned long pflags;
 	bool allow_spinning = gfpflags_allow_spinning(gfp_mask);
-	bool toptier_charged;
+	nodemask_t toptier_nodes;
+	nodemask_t *reclaim_nodes;
 
 retry:
 	reclaim_options = MEMCG_RECLAIM_MAY_SWAP;
-	toptier_charged = false;
+	reclaim_nodes = NULL;
 
 	if (do_memsw_account() &&
 	    !page_counter_try_charge(&memcg->memsw, nr_pages, &counter)) {
@@ -2438,13 +2448,20 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	}
 
 	if (toptier &&
-	    page_counter_try_charge(&memcg->toptier, nr_pages, &counter))
-		toptier_charged = true;
+	    !page_counter_try_charge(&memcg->toptier, nr_pages, &counter)) {
+		get_toptier_nodemask(&toptier_nodes);
+		reclaim_nodes = &toptier_nodes;
+		mem_over_limit = mem_cgroup_from_counter(counter, toptier);
+
+		if (do_memsw_account())
+			page_counter_uncharge(&memcg->memsw, nr_pages);
+		goto reclaim;
+	}
 
 	if (page_counter_try_charge(&memcg->memory, nr_pages, &counter))
 		goto done_restock;
 
-	if (toptier_charged)
+	if (toptier)
 		page_counter_uncharge(&memcg->toptier, nr_pages);
 	if (do_memsw_account())
 		page_counter_uncharge(&memcg->memsw, nr_pages);
@@ -2473,7 +2490,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
 						    gfp_mask, reclaim_options,
-						    NULL, NULL);
+						    NULL, reclaim_nodes);
 	psi_memstall_leave(&pflags);
 
 	if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
@@ -4683,7 +4700,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
 	unsigned int nr_reclaims = MAX_RECLAIM_RETRIES;
 	bool drained = false;
-	unsigned long max;
+	unsigned long max, toptier_max = PAGE_COUNTER_MAX;
+	nodemask_t toptier_nodes;
 	int err;
 
 	buf = strstrip(buf);
@@ -4692,16 +4710,30 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 		return err;
 
 	xchg(&memcg->memory.max, max);
-	if (mem_cgroup_tiered_limits())
-		xchg(&memcg->toptier.max, page_counter_max_or_scale(max));
+	if (mem_cgroup_tiered_limits()) {
+		toptier_max = page_counter_max_or_scale(max);
+		xchg(&memcg->toptier.max, toptier_max);
+		get_toptier_nodemask(&toptier_nodes);
+	}
 
 	if (of->file->f_flags & O_NONBLOCK)
 		goto out;
 
 	for (;;) {
 		unsigned long nr_pages = page_counter_read(&memcg->memory);
+		unsigned long nr_toptier = page_counter_read(&memcg->toptier);
+		unsigned long to_reclaim = 0;
+		nodemask_t *reclaim_nodes = NULL;
+
+		if (nr_pages > max) {
+			to_reclaim = nr_pages - max;
+		} else if (mem_cgroup_tiered_limits() &&
+				nr_toptier > toptier_max) {
+			to_reclaim = nr_toptier - toptier_max;
+			reclaim_nodes = &toptier_nodes;
+		}
 
-		if (nr_pages <= max)
+		if (!to_reclaim)
 			break;
 
 		if (signal_pending(current))
@@ -4714,9 +4746,9 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 		}
 
 		if (nr_reclaims) {
-			if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
+			if (!try_to_free_mem_cgroup_pages(memcg, to_reclaim,
 					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP,
-					NULL, NULL))
+					NULL, reclaim_nodes))
 				nr_reclaims--;
 			continue;
 		}
Signature · 1 line
2.52.0
#10 lore raw
In reply to Joshua Hahn
Quoted 36 lines · Joshua Hahn

INTRODUCTION
============
Memory cgroups provide an interface that allow multiple works on a host to co-exist via weak and strong memory isolation guarantees. This works, because for the most part, all memory has equal utility. Isolating a cgroup’s memory footprint restricts how much it can hurt other workloads competing for memory, or protects it from other cgroups looking for more memory.

However, on systems with tiered memory (e.g. CXL), memory utility is no longer homogeneous; toptier and lowtier memory provide different performance characteristics and have different scarcity, meaning memory footprint no longer serves as an accurate representation of a cgroup’s consumption of the system’s limited resources. As an extreme example, a cgroup with 10G of toptier (e.g. DRAM) memory and a cgroup with 10G of lowtier (e.g. CXL) memory both appear to be consuming the same amount of system resources from memcg’s perspective, despite the performance asymmetry between the two workloads.

Therefore on tiered systems, memory isolation cannot currently happen, as workloads that are well-behaved within their memcg limits may still hurt the performance of other well-behaving workloads by hogging more than its “fair share” of toptier memory.

Introduce tier-aware memcg limits, which establish independent toptier limits that scale with the memory limits and the ratio of toptier:total memory available on the system.

INTERFACE
=========
This series introduces only one adjustable knob to userspace; a new cgroup mount option “memory_tiered_limits” which toggles whether the cgroup mount will scale toptier limits. It also introduces 4 new read-only sysfs entries per-cgroup:
memory.toptier_{min, low, high, max}.

The new toptier memory limits are scaled according to the amount of toptier memory and total memory available on the system as such:

Quoted 6 lines

memory.toptier_high = (toptier_mem / total_mem) * memory.high

For instance, on a host with 100GB memory, with 75G toptier and 25G CXL, the “toptier ratio” would be 75 / 100 = 0.75. A cgroup with the following memcg limits {min: 8G, low: 12G, high: 20G, max: 24G} might see toptier limits scaled at {min: 6G, low: 9G, high: 15G, max: 18G}.

Assume you have a bigger hierarchy (HBP, DRAM, CXL), or assume you have multiple NUMA nodes with a hierarchy each.

Your proposal doesn't really seem to be very versatile, or am I wrong?

Signature · 3 lines
Cheers,

David
author reviewer
#11 lore raw
Quoted 38 lines · David Hildenbrand (Arm)
Quoted 36 lines · Joshua Hahn

INTRODUCTION
============
Memory cgroups provide an interface that allow multiple works on a host to co-exist via weak and strong memory isolation guarantees. This works, because for the most part, all memory has equal utility. Isolating a cgroup’s memory footprint restricts how much it can hurt other workloads competing for memory, or protects it from other cgroups looking for more memory.

However, on systems with tiered memory (e.g. CXL), memory utility is no longer homogeneous; toptier and lowtier memory provide different performance characteristics and have different scarcity, meaning memory footprint no longer serves as an accurate representation of a cgroup’s consumption of the system’s limited resources. As an extreme example, a cgroup with 10G of toptier (e.g. DRAM) memory and a cgroup with 10G of lowtier (e.g. CXL) memory both appear to be consuming the same amount of system resources from memcg’s perspective, despite the performance asymmetry between the two workloads.

Therefore on tiered systems, memory isolation cannot currently happen, as workloads that are well-behaved within their memcg limits may still hurt the performance of other well-behaving workloads by hogging more than its “fair share” of toptier memory.

Introduce tier-aware memcg limits, which establish independent toptier limits that scale with the memory limits and the ratio of toptier:total memory available on the system.

INTERFACE
=========
This series introduces only one adjustable knob to userspace; a new cgroup mount option “memory_tiered_limits” which toggles whether the cgroup mount will scale toptier limits. It also introduces 4 new read-only sysfs entries per-cgroup:
memory.toptier_{min, low, high, max}.

The new toptier memory limits are scaled according to the amount of toptier memory and total memory available on the system as such:

Quoted 6 lines
Quoted 6 lines

memory.toptier_high = (toptier_mem / total_mem) * memory.high

For instance, on a host with 100GB memory, with 75G toptier and 25G CXL, the “toptier ratio” would be 75 / 100 = 0.75. A cgroup with the following memcg limits {min: 8G, low: 12G, high: 20G, max: 24G} might see toptier limits scaled at {min: 6G, low: 9G, high: 15G, max: 18G}.

Hi David!!

It was great seeing you at LSFMMBPF. I didn't get a chance to have a conversation with you at Zagreb but hopefully I will be less shy and say hello next conference : -)

Quoted 4 lines

Assume you have a bigger hierarchy (HBP, DRAM, CXL), or assume you have multiple NUMA nodes with a hierarchy each.

Your proposal doesn't really seem to be very versatile, or am I wrong?

Let me address these comments separately!

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

Next for the 3+ tier case, I think this is a lot more scalable than it seems at first. This series depends on another RFC that I sent out [1], which pushes the concept of "stock" from memcg to page_counter, which means that it is more scalable to just add more page counters to each memcg. This means that each tier would just need another page_counter to track its memory usage, and we trigger selective reclaim on the tier that is being targeted via the scan control nodemask we introduce in this series.

At my talk in LSFMMBPF, Usama noted that the user-visible API should probably remain the same, no matter what. The way I have currently established the memcg files aren't really scalable, so Usama suggested turning the "memory.toptier_XXX" sysfs files to "memory.tiered_XXX", which would include a newline-separated / space-separated list of per-tier limits. Something like:

$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280
...

So we have a way to make both the user-facing side stable, and the internals also more scalable.

With that said, I've opted to leave the internals to 2 tiers for now -- I think it is not too late to add the generalization series when we start seeing 3+ tier systems out there in the wild. My goal was to introduce tieredness, and we can work towards generalization in a future work.

On that note, it seems like in general mm is aware of 3+ tiers, but most of the existing work revolves around distinguishing between toptier/everything else. I got this impression from reading mm/memory-tiers.c -- but please feel free to correct me if you feel like I have the wrong idea here : -)

So perhaps the generalization work would benefit from first introducing more general tier awareness (not just toptier vs. rest) in memory-tiers.c.

What do you think? Does this approach of introducing toptier restriction for now, and then generalizing in future work make sense to you?

Thanks again for your interest. Have a great day!
Joshua

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

#12 lore raw
Quoted 47 lines · Joshua Hahn
Quoted 38 lines · David Hildenbrand (Arm)
Quoted 36 lines · Joshua Hahn

INTRODUCTION
============
Memory cgroups provide an interface that allow multiple works on a host to co-exist via weak and strong memory isolation guarantees. This works, because for the most part, all memory has equal utility. Isolating a cgroup’s memory footprint restricts how much it can hurt other workloads competing for memory, or protects it from other cgroups looking for more memory.

However, on systems with tiered memory (e.g. CXL), memory utility is no longer homogeneous; toptier and lowtier memory provide different performance characteristics and have different scarcity, meaning memory footprint no longer serves as an accurate representation of a cgroup’s consumption of the system’s limited resources. As an extreme example, a cgroup with 10G of toptier (e.g. DRAM) memory and a cgroup with 10G of lowtier (e.g. CXL) memory both appear to be consuming the same amount of system resources from memcg’s perspective, despite the performance asymmetry between the two workloads.

Therefore on tiered systems, memory isolation cannot currently happen, as workloads that are well-behaved within their memcg limits may still hurt the performance of other well-behaving workloads by hogging more than its “fair share” of toptier memory.

Introduce tier-aware memcg limits, which establish independent toptier limits that scale with the memory limits and the ratio of toptier:total memory available on the system.

INTERFACE
=========
This series introduces only one adjustable knob to userspace; a new cgroup mount option “memory_tiered_limits” which toggles whether the cgroup mount will scale toptier limits. It also introduces 4 new read-only sysfs entries per-cgroup:
memory.toptier_{min, low, high, max}.

The new toptier memory limits are scaled according to the amount of toptier memory and total memory available on the system as such:

Quoted 6 lines
Quoted 6 lines

memory.toptier_high = (toptier_mem / total_mem) * memory.high

For instance, on a host with 100GB memory, with 75G toptier and 25G CXL, the “toptier ratio” would be 75 / 100 = 0.75. A cgroup with the following memcg limits {min: 8G, low: 12G, high: 20G, max: 24G} might see toptier limits scaled at {min: 6G, low: 9G, high: 15G, max: 18G}.

Quoted 5 lines

Hi David!!

It was great seeing you at LSFMMBPF. I didn't get a chance to have a conversation with you at Zagreb but hopefully I will be less shy and say hello next conference : -)

Sorry for the late reply :) Yes, hopefully talk to you in person next time!

Quoted 8 lines
Quoted 5 lines

Assume you have a bigger hierarchy (HBP, DRAM, CXL), or assume you have multiple NUMA nodes with a hierarchy each.

Your proposal doesn't really seem to be very versatile, or am I wrong?

Let me address these comments separately!

Quoted 7 lines

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

I am not really sure about the "toptier" notion, in particular if your memory hierarchy is not static.

Imagine that you have only DRAM in your system. Then you hotplug some slow CXL memory. The you hotplug some HBM CXL memory.

The meanting of "toptier" changes, which is rather confusing to me.

Quoted 15 lines

Next for the 3+ tier case, I think this is a lot more scalable than it seems at first. This series depends on another RFC that I sent out [1], which pushes the concept of "stock" from memcg to page_counter, which means that it is more scalable to just add more page counters to each memcg. This means that each tier would just need another page_counter to track its memory usage, and we trigger selective reclaim on the tier that is being targeted via the scan control nodemask we introduce in this series.

At my talk in LSFMMBPF, Usama noted that the user-visible API should probably remain the same, no matter what. The way I have currently established the memcg files aren't really scalable, so Usama suggested turning the "memory.toptier_XXX" sysfs files to "memory.tiered_XXX", which would include a newline-separated / space-separated list of per-tier limits. Something like:

Quoted 4 lines
$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280
...

memcgs are currently not NUMA aware, so this could be one possible direction.

But is "tier" the right abstraction? I really don't know.

Are tier numbers really what we want? I don't know.

We'd really need some feedback from memcg folks.

Quoted 9 lines

So we have a way to make both the user-facing side stable, and the internals also more scalable.

With that said, I've opted to leave the internals to 2 tiers for now -- I think it is not too late to add the generalization series when we start seeing 3+ tier systems out there in the wild. My goal was to introduce tieredness, and we can work towards generalization in a future work.

Quoted 4 lines

On that note, it seems like in general mm is aware of 3+ tiers, but most of the existing work revolves around distinguishing between toptier/everything else. I got this impression from reading mm/memory-tiers.c -- but please feel free to correct me if you feel like I have the wrong idea here : -)

I assume you're right, but it's something different if we end up providing a proper memcg API towards the users.

Quoted 6 lines

So perhaps the generalization work would benefit from first introducing more general tier awareness (not just toptier vs. rest) in memory-tiers.c.

What do you think? Does this approach of introducing toptier restriction for now, and then generalizing in future work make sense to you?

I'd suspect that we'd want to support multiple tiers from day one -- especially get the interfaces right.

Signature · 3 lines
Cheers,

David
author reviewer
#13 lore raw
Quoted 1 line · On Thu, 16 Jul 2026 15:11:30 +0200 "David Hildenbrand (Arm)"…

Sorry for the late reply :) Yes, hopefully talk to you in person next time!

Hello David, no worries! Thank you for taking the time to consider this series again.

Quoted 15 lines
Quoted 7 lines
Quoted 4 lines

Assume you have a bigger hierarchy (HBP, DRAM, CXL), or assume you have multiple NUMA nodes with a hierarchy each.

Your proposal doesn't really seem to be very versatile, or am I wrong?

Let me address these comments separately!

Quoted 7 lines

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

Quoted 7 lines

I am not really sure about the "toptier" notion, in particular if your memory hierarchy is not static.

Imagine that you have only DRAM in your system. Then you hotplug some slow CXL memory. The you hotplug some HBM CXL memory.

The meanting of "toptier" changes, which is rather confusing to me.

While I do agree with the idea that tiers in general are not stable, I think that for the most part the "toptier" node is generally stable through hotplugging nodes. In your example above, the DRAM will always be the toptier node, right? In any case, I think if we are to support 3+ tiers I have to change the semantics to distinguish between higher and lower tiers instead to be generic anyways, so I will change the wording to reflect this!

Quoted 14 lines
Quoted 14 lines

Next for the 3+ tier case, I think this is a lot more scalable than it seems at first. This series depends on another RFC that I sent out [1], which pushes the concept of "stock" from memcg to page_counter, which means that it is more scalable to just add more page counters to each memcg. This means that each tier would just need another page_counter to track its memory usage, and we trigger selective reclaim on the tier that is being targeted via the scan control nodemask we introduce in this series.

At my talk in LSFMMBPF, Usama noted that the user-visible API should probably remain the same, no matter what. The way I have currently established the memcg files aren't really scalable, so Usama suggested turning the "memory.toptier_XXX" sysfs files to "memory.tiered_XXX", which would include a newline-separated / space-separated list of per-tier limits. Something like:

Quoted 8 lines
Quoted 4 lines
$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280
...

memcgs are currently not NUMA aware, so this could be one possible direction.

But is "tier" the right abstraction? I really don't know.

From my Pov after working on this for a while, I think tiering is the grouping that makes the most sense. It is my understanding that memory present in different nodes, but belonging to the same memcg are fungible; memory from one node isn't more or less scarce than the other node.

However, if the system has multiple tiers then the memory is no longer fungible, and memcgs will be competing for the same scarce resource. So I think it makes sense to limit how much a memcg can make.

Quoted 1 line

Are tier numbers really what we want? I don't know.

If this doesn't make sense, I can change this to just tiers 0-3 (MAX_NR_TIERS is currently hardcoded to 4 across all architectures).

Quoted 1 line

We'd really need some feedback from memcg folks.

That would really be awesome : -)

Quoted 16 lines
Quoted 8 lines

So we have a way to make both the user-facing side stable, and the internals also more scalable.

With that said, I've opted to leave the internals to 2 tiers for now -- I think it is not too late to add the generalization series when we start seeing 3+ tier systems out there in the wild. My goal was to introduce tieredness, and we can work towards generalization in a future work.

Quoted 4 lines

On that note, it seems like in general mm is aware of 3+ tiers, but most of the existing work revolves around distinguishing between toptier/everything else. I got this impression from reading mm/memory-tiers.c -- but please feel free to correct me if you feel like I have the wrong idea here : -)

I assume you're right, but it's something different if we end up providing a proper memcg API towards the users.

Quoted 8 lines
Quoted 5 lines

So perhaps the generalization work would benefit from first introducing more general tier awareness (not just toptier vs. rest) in memory-tiers.c.

What do you think? Does this approach of introducing toptier restriction for now, and then generalizing in future work make sense to you?

I'd suspect that we'd want to support multiple tiers from day one -- especially get the interfaces right.

So I think the interface that I have at the moment (in the v3) is stable across any number of memory tiers. My idea was to land something with a stable API and then as future work support 3+ tiers. But if it makes more sense to land the scalable tiered memcg work as the first iteration then I can work on that too.

I've been working on v3 for a while, I will send it up sometime soon so that I can start the discussion on this again. I really want to discuss with memcg maintianers and migration maintainers to get some confirmation on whether the series's direction looks good and whether they are happy with my vision for the series.

And then in v4, I will add the scalable tiers.

How does that sound? : -) Again, thank you for coming back to this. It means a lot!
Joshua

maintainer
#14 lore raw
In reply to David Hildenbrand (Arm)
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 8 lines

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

Quoted 8 lines

I am not really sure about the "toptier" notion, in particular if your memory hierarchy is not static.

Imagine that you have only DRAM in your system. Then you hotplug some slow CXL memory. The you hotplug some HBM CXL memory.

The meanting of "toptier" changes, which is rather confusing to me.

I think this is a naming problem.

Maybe we should start calling this "initiator tier" or something. Maybe tier is even the wrong notion here, but it's the best we have.

But I agree "toptier" naming doesn't generalize to complex systems.

Quoted 10 lines
Quoted 5 lines
$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280
...

memcgs are currently not NUMA aware, so this could be one possible direction.

But is "tier" the right abstraction? I really don't know.

Really I think we're trying to classify 3 general pools of memory

- Precious: HBM
- Preferred: DRAM (initiator preferred targets)
- Non-Preferred: CXL (fallback and demotion targets)

HBM nodes are already weird in terms of fallback ordering, but ignoring that for a moment.

Non-preferred "tiers" are slightly different than preferred "tiers".

Under normal operation (assume no mempolicy or cgroup weirdness):

1) Direct reclaim NEVER targets non-preferred tiers (kswapd only)
2) Promotion NEVER targets non-preferred tiers (promotion -> toptier)
3) Under total system pressure (direct reclaim on top tier), we swap directly from the preferred tier out to swap

So non-preferred tiers don't have the same reclaim behavior associated with them. In fact i'm pretty sure we could summarize non-preferred tiers as being managed hyper-lazily in the current scheme.

(if there's space, have at it - otherwise you have to wait for kswapd).

In my head, I think what we mostly want is:
- Precious - controlled entry, Private node?
- Preferred - semi-precious, fairness desired
- Non-preferred - not precious, lazy managed, no fairness

Just some food for thought. I've been poking at making non-preferred tiers less lazily managed, and I'm just finding this results to massive regressions.

~Gregory

author reviewer
#15 lore raw
Quoted 10 lines · On Thu, 16 Jul 2026 12:55:55 -0400 Gregory Price <[email protected]> wrote:
Quoted 9 lines · David Hildenbrand (Arm)
Quoted 8 lines

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

Quoted 7 lines
Quoted 7 lines

I am not really sure about the "toptier" notion, in particular if your memory hierarchy is not static.

Imagine that you have only DRAM in your system. Then you hotplug some slow CXL memory. The you hotplug some HBM CXL memory.

The meanting of "toptier" changes, which is rather confusing to me.

Hello Gregory, hello David,

So circling back here a little bit (sorry that it took a while to get back)

Quoted 4 lines

I think this is a naming problem.

Maybe we should start calling this "initiator tier" or something. Maybe tier is even the wrong notion here, but it's the best we have.

I think the confusion is that "toptier-ness" doesn't give the full picture for whether a node is a promotion candidate, a promotion candidate node needs to both be toptier (adistance < toptier_adistance) AND also have CPUs. So a HBM node that is hotplugged can definitely be toptier in that it has a smaller adistance than the toptier_adistance, and will probably even have a smaller adistance than the DRAM node, but if it doesn't have CPUs, it won't be a promotion candidate.

It gets a bit more confusing because this distinction is not a tier-wide distinction; you can have two HBM nodes hotplugged where only one of them is CPUless -- then we cannot make any statement on whether the tier is a proper promotion candidate, only that one of the nodes within that tier is a candidate.

Quoted 1 line

But I agree "toptier" naming doesn't generalize to complex systems.

The merits of calling a node a "toptier" node is therefore a little dubious I think, it's just one part of whether a node can be promoted to.

Anyways, I think this is outside the scope of this series. I think that for the scope of what I am working on I will try to move away from referring to things as "toptier" nodes but rather call them "promotion candidate" nodes. I hope that distinction will make this series easier to understand and read.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280

One change here is that it is not really possible to have 0..N-1 indexed tiers, since those are not stable. For instance, hotplugging a HBM node (as David pointed out) would replace what tier_0 is. However, one thing that remains stable is the abstract distance (adistance) the tiers are associated with, so I'll preserve the ABI that I have right now, so we can have something like

tier_42 20971520
tier_128 31457280

And if HBM does come up as the "highest tier" with the lowest abstract distance we will just have

tier 16 1048576
tier_42 20971520
tier_128 31457280

And we will not have any problems with tier_0 defining DRAM and then later switching to define HBM. It is certainly less interpretable but I think this is the only way to have a stable set of tiers, since adistance is pre-calculated for all possible nodes at lateinit time.

Quoted 28 lines
Quoted 3 lines

memcgs are currently not NUMA aware, so this could be one possible direction.

But is "tier" the right abstraction? I really don't know.

Really I think we're trying to classify 3 general pools of memory

- Precious: HBM
- Preferred: DRAM (initiator preferred targets)
- Non-Preferred: CXL (fallback and demotion targets)

HBM nodes are already weird in terms of fallback ordering, but ignoring that for a moment.

Non-preferred "tiers" are slightly different than preferred "tiers".

Under normal operation (assume no mempolicy or cgroup weirdness):

1) Direct reclaim NEVER targets non-preferred tiers (kswapd only)
2) Promotion NEVER targets non-preferred tiers (promotion -> toptier)
3) Under total system pressure (direct reclaim on top tier), we swap directly from the preferred tier out to swap

So non-preferred tiers don't have the same reclaim behavior associated with them. In fact i'm pretty sure we could summarize non-preferred tiers as being managed hyper-lazily in the current scheme.

(if there's space, have at it - otherwise you have to wait for kswapd).

Quoted 8 lines

In my head, I think what we mostly want is:
- Precious - controlled entry, Private node?
- Preferred - semi-precious, fairness desired
- Non-preferred - not precious, lazy managed, no fairness

Just some food for thought. I've been poking at making non-preferred tiers less lazily managed, and I'm just finding this results to massive regressions.

Thanks for your insights Gregory. All of what I am saying is just in aggressive agreement with what you and David have pointed out.

I am working to make the next version generic across tiers. Thank you both! I hope you all have a great weekend, Joshua

author reviewer
#16 lore raw
In reply to David Hildenbrand (Arm)

Sorry, re-sending because linux-mm didn't get added to the reply when I sent it out.

Quoted 1 line

Sorry for the late reply :) Yes, hopefully talk to you in person next time!

Hello David, no worries! Thank you for taking the time to consider this series again.

Quoted 15 lines
Quoted 7 lines
Quoted 4 lines

Assume you have a bigger hierarchy (HBP, DRAM, CXL), or assume you have multiple NUMA nodes with a hierarchy each.

Your proposal doesn't really seem to be very versatile, or am I wrong?

Let me address these comments separately!

Quoted 7 lines

First, for the multi-numa-per-tier case, I think this is already pretty well handled by my series. Once we realize that a memcg is consuming too much memory from a tier, we trigger reclaim from that memcg via try_to_free_mem_cgroup_pages,
which as far as I can tell already handles the multi-numa per memcg case. Other than restricting the scan_control's nodemask to target the nodes from that tier, I don't think there's anything else to be done.

Quoted 7 lines

I am not really sure about the "toptier" notion, in particular if your memory hierarchy is not static.

Imagine that you have only DRAM in your system. Then you hotplug some slow CXL memory. The you hotplug some HBM CXL memory.

The meanting of "toptier" changes, which is rather confusing to me.

While I do agree with the idea that tiers in general are not stable, I think that for the most part the "toptier" node is generally stable through hotplugging nodes. In your example above, the DRAM will always be the toptier node, right? In any case, I think if we are to support 3+ tiers I have to change the semantics to distinguish between higher and lower tiers instead to be generic anyways, so I will change the wording to reflect this!

Quoted 14 lines
Quoted 14 lines

Next for the 3+ tier case, I think this is a lot more scalable than it seems at first. This series depends on another RFC that I sent out [1], which pushes the concept of "stock" from memcg to page_counter, which means that it is more scalable to just add more page counters to each memcg. This means that each tier would just need another page_counter to track its memory usage, and we trigger selective reclaim on the tier that is being targeted via the scan control nodemask we introduce in this series.

At my talk in LSFMMBPF, Usama noted that the user-visible API should probably remain the same, no matter what. The way I have currently established the memcg files aren't really scalable, so Usama suggested turning the "memory.toptier_XXX" sysfs files to "memory.tiered_XXX", which would include a newline-separated / space-separated list of per-tier limits. Something like:

Quoted 8 lines
Quoted 4 lines
$ cat memory.tiered_max
tier_0 20971520
tier_1 31457280
...

memcgs are currently not NUMA aware, so this could be one possible direction.

But is "tier" the right abstraction? I really don't know.

From my Pov after working on this for a while, I think tiering is the grouping that makes the most sense. It is my understanding that memory present in different nodes, but belonging to the same memcg are fungible; memory from one node isn't more or less scarce than the other node.

However, if the system has multiple tiers then the memory is no longer fungible, and memcgs will be competing for the same scarce resource. So I think it makes sense to limit how much a memcg can make.

Quoted 1 line

Are tier numbers really what we want? I don't know.

If this doesn't make sense, I can change this to just tiers 0-3 (MAX_NR_TIERS is currently hardcoded to 4 across all architectures).

Quoted 1 line

We'd really need some feedback from memcg folks.

That would really be awesome : -)

Quoted 16 lines
Quoted 8 lines

So we have a way to make both the user-facing side stable, and the internals also more scalable.

With that said, I've opted to leave the internals to 2 tiers for now -- I think it is not too late to add the generalization series when we start seeing 3+ tier systems out there in the wild. My goal was to introduce tieredness, and we can work towards generalization in a future work.

Quoted 4 lines

On that note, it seems like in general mm is aware of 3+ tiers, but most of the existing work revolves around distinguishing between toptier/everything else. I got this impression from reading mm/memory-tiers.c -- but please feel free to correct me if you feel like I have the wrong idea here : -)

I assume you're right, but it's something different if we end up providing a proper memcg API towards the users.

Quoted 8 lines
Quoted 5 lines

So perhaps the generalization work would benefit from first introducing more general tier awareness (not just toptier vs. rest) in memory-tiers.c.

What do you think? Does this approach of introducing toptier restriction for now, and then generalizing in future work make sense to you?

I'd suspect that we'd want to support multiple tiers from day one -- especially get the interfaces right.

So I think the interface that I have at the moment (in the v3) is stable across any number of memory tiers. My idea was to land something with a stable API and then as future work support 3+ tiers. But if it makes more sense to land the scalable tiered memcg work as the first iteration then I can work on that too.

I've been working on v3 for a while, I will send it up sometime soon so that I can start the discussion on this again. I really want to discuss with memcg maintianers and migration maintainers to get some confirmation on whether the series's direction looks good and whether they are happy with my vision for the series.

And then in v4, I will add the scalable tiers.

How does that sound? : -) Again, thank you for coming back to this. It means a lot!
Joshua