From: Joshua Hahn <joshua.hahnjy@gmail.com>
To: Johannes Weiner <hannes@cmpxchg.org>, Gregory Price <gourry@gourry.net>
Cc: Alistair Popple <apopple@nvidia.com>,
Andrew Morton <akpm@linux-foundation.org>,
Axel Rasmussen <axelrasmussen@google.com>,
Barry Song <baohua@kernel.org>, Ben Segall <bsegall@google.com>,
Brendan Jackman <jackmanb@google.com>,
Byungchul Park <byungchul@sk.com>,
David Hildenbrand <david@kernel.org>,
David Rientjes <rientjes@google.com>,
Dietmar Eggemann <dietmar.eggemann@arm.com>,
"Harry Yoo (Oracle)" <harry@kernel.org>,
Ingo Molnar <mingo@redhat.com>,
Juri Lelli <juri.lelli@redhat.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Kairui Song <kasong@tencent.com>,
"Liam R. Howlett" <liam@infradead.org>,
Lorenzo Stoakes <ljs@kernel.org>,
Matthew Brost <matthew.brost@intel.com>,
Mel Gorman <mgorman@suse.de>, Michal Hocko <mhocko@kernel.org>,
Michal Hocko <mhocko@suse.com>, Mike Rapoport <rppt@kernel.org>,
Muchun Song <muchun.song@linux.dev>,
Peter Zijlstra <peterz@infradead.org>,
Qi Zheng <qi.zheng@linux.dev>, Rakie Kim <rakie.kim@sk.com>,
Roman Gushchin <roman.gushchin@linux.dev>,
Shakeel Butt <shakeel.butt@linux.dev>,
Steven Rostedt <rostedt@goodmis.org>,
Suren Baghdasaryan <surenb@google.com>,
"T.J. Mercier" <tjmercier@google.com>,
Valentin Schneider <vschneid@redhat.com>,
Vincent Guittot <vincent.guittot@linaro.org>,
Vlastimil Babka <vbabka@kernel.org>, Wei Xu <weixugc@google.com>,
Ying Huang <ying.huang@linux.alibaba.com>,
Yosry Ahmed <yosry@kernel.org>, Yuanchu Xie <yuanchu@google.com>,
Zi Yan <ziy@nvidia.com>,
cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, kernel-team@meta.com
Subject: [RFC PATCH v3 05/14] mm/memcontrol: Set tier limits proportional to memory limits
Date: Fri, 7 Aug 2026 13:20:48 -0700 [thread overview]
Message-ID: <20260807202059.2620949-6-joshua.hahnjy@gmail.com> (raw)
In-Reply-To: <20260807202059.2620949-1-joshua.hahnjy@gmail.com>
Compute proportional per-tier limits based on memory limits when
users write to memory limit sysfs files, or when memory hotplug causes
tier proportions to be shifted.
No-op unless the system has tiered memcg limits enabled.
Signed-off-by: Joshua Hahn <joshua.hahnjy@gmail.com>
---
include/linux/memcontrol.h | 10 +++++++
include/linux/memory-tiers.h | 6 ++++
mm/memcontrol.c | 54 ++++++++++++++++++++++++++++++++++++
mm/memory-tiers.c | 13 +++++++++
4 files changed, 83 insertions(+)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index bb5bde87ac85a..f7a92b66330ec 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -537,11 +537,17 @@ static inline bool mem_cgroup_tiered_limits(void)
{
return static_branch_unlikely(&memcg_tiered_limits_key);
}
+
+void establish_memcg_tier_limits(void);
#else
static inline bool mem_cgroup_tiered_limits(void)
{
return false;
}
+
+static inline void establish_memcg_tier_limits(void)
+{
+}
#endif
static inline void mem_cgroup_protection(struct mem_cgroup *root,
@@ -1102,6 +1108,10 @@ static inline bool mem_cgroup_tiered_limits(void)
return false;
}
+static inline void establish_memcg_tier_limits(void)
+{
+}
+
static inline void memcg_memory_event(struct mem_cgroup *memcg,
enum memcg_memory_event event)
{
diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h
index 0e49645cdd1a9..04b396f60b457 100644
--- a/include/linux/memory-tiers.h
+++ b/include/linux/memory-tiers.h
@@ -55,6 +55,7 @@ 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);
const nodemask_t *mt_tier_nodes(int slot);
+unsigned long mt_scale_by_tier(unsigned long val, int slot);
#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);
@@ -169,5 +170,10 @@ static inline const nodemask_t *mt_tier_nodes(int slot)
{
return NULL;
}
+
+static inline unsigned long mt_scale_by_tier(unsigned long val, int slot)
+{
+ return val;
+}
#endif /* CONFIG_NUMA */
#endif /* _LINUX_MEMORY_TIERS_H */
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index d096010366515..defd04acfb3fd 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4421,6 +4421,35 @@ static void mem_cgroup_css_free(struct cgroup_subsys_state *css)
mem_cgroup_free(memcg);
}
+static inline unsigned long page_counter_max_or_scale(unsigned long val,
+ int slot)
+{
+ return val == PAGE_COUNTER_MAX ? PAGE_COUNTER_MAX :
+ mt_scale_by_tier(val, slot);
+}
+
+static void memcg_scale_tier_limits(struct mem_cgroup *memcg)
+{
+ unsigned long min = READ_ONCE(memcg->memory.min);
+ unsigned long low = READ_ONCE(memcg->memory.low);
+ unsigned long high = READ_ONCE(memcg->memory.high);
+ unsigned long max = READ_ONCE(memcg->memory.max);
+ int nr_tier_slots = mt_nr_tier_slots();
+
+ for (int slot = 0; slot < nr_tier_slots; slot++) {
+ unsigned long new_min = page_counter_max_or_scale(min, slot);
+ unsigned long new_low = page_counter_max_or_scale(low, slot);
+ unsigned long new_high = page_counter_max_or_scale(high, slot);
+ unsigned long new_max = page_counter_max_or_scale(max, slot);
+ struct page_counter *tier = &memcg->tier[slot];
+
+ page_counter_set_min(tier, new_min);
+ page_counter_set_low(tier, new_low);
+ page_counter_set_high(tier, new_high);
+ xchg(&tier->max, new_max);
+ }
+}
+
/**
* mem_cgroup_css_reset - reset the states of a mem_cgroup
* @css: the target css
@@ -4454,6 +4483,8 @@ static void mem_cgroup_css_reset(struct cgroup_subsys_state *css)
page_counter_set_high(&memcg->memory, PAGE_COUNTER_MAX);
memcg1_soft_limit_reset(memcg);
page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX);
+ if (mem_cgroup_tiered_limits())
+ memcg_scale_tier_limits(memcg);
memcg_wb_domain_size_changed(memcg);
}
@@ -4797,6 +4828,21 @@ static ssize_t memory_peak_write(struct kernfs_open_file *of, char *buf,
&memcg->memory_peaks);
}
+#ifdef CONFIG_NUMA
+void establish_memcg_tier_limits(void)
+{
+ struct mem_cgroup *memcg;
+
+ if (!mem_cgroup_tiered_limits())
+ return;
+
+ for_each_mem_cgroup_tree(memcg, NULL) {
+ if (memcg != root_mem_cgroup)
+ memcg_scale_tier_limits(memcg);
+ }
+}
+#endif
+
#undef OFP_PEAK_UNSET
static int memory_min_show(struct seq_file *m, void *v)
@@ -4818,6 +4864,8 @@ static ssize_t memory_min_write(struct kernfs_open_file *of,
return err;
page_counter_set_min(&memcg->memory, min);
+ if (mem_cgroup_tiered_limits())
+ memcg_scale_tier_limits(memcg);
return nbytes;
}
@@ -4841,6 +4889,8 @@ static ssize_t memory_low_write(struct kernfs_open_file *of,
return err;
page_counter_set_low(&memcg->memory, low);
+ if (mem_cgroup_tiered_limits())
+ memcg_scale_tier_limits(memcg);
return nbytes;
}
@@ -4866,6 +4916,8 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
return err;
page_counter_set_high(&memcg->memory, high);
+ if (mem_cgroup_tiered_limits())
+ memcg_scale_tier_limits(memcg);
if (of->file->f_flags & O_NONBLOCK)
goto out;
@@ -4925,6 +4977,8 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
return err;
xchg(&memcg->memory.max, max);
+ if (mem_cgroup_tiered_limits())
+ memcg_scale_tier_limits(memcg);
if (of->file->f_flags & O_NONBLOCK)
goto out;
diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c
index bd5c78cd26ec4..e2c99f51c36d1 100644
--- a/mm/memory-tiers.c
+++ b/mm/memory-tiers.c
@@ -810,6 +810,7 @@ static int __init memory_tier_late_init(void)
establish_demotion_targets();
establish_tier_slots();
+ establish_memcg_tier_limits();
put_online_mems();
return 0;
@@ -967,6 +968,16 @@ const nodemask_t *mt_tier_nodes(int slot)
return &tier_nodemasks[slot];
}
+unsigned long mt_scale_by_tier(unsigned long val, int slot)
+{
+ unsigned long total_capacity = totalram_pages();
+
+ if (slot < 0 || !total_capacity)
+ return 0;
+
+ return mult_frac(val, READ_ONCE(tier_capacity[slot]), total_capacity);
+}
+
static int __meminit memtier_hotplug_callback(struct notifier_block *self,
unsigned long action, void *_arg)
{
@@ -979,6 +990,7 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
if (clear_node_memory_tier(nn->nid)) {
establish_demotion_targets();
establish_tier_slots();
+ establish_memcg_tier_limits();
}
mutex_unlock(&memory_tier_lock);
break;
@@ -988,6 +1000,7 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self,
if (!IS_ERR(memtier)) {
establish_demotion_targets();
establish_tier_slots();
+ establish_memcg_tier_limits();
}
mutex_unlock(&memory_tier_lock);
break;
--
2.53.0-Meta
next prev parent reply other threads:[~2026-08-07 20:21 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-07 20:20 [RFC PATCH v3 00/14] Introduce tiered memcg limits Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 01/14] mm/memcontrol: Introduce cgroup.memory=tiered_limits boot parameter Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 02/14] mm/memcontrol: Refactor page_counter charging in try_charge_memcg Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 03/14] mm/memory-tiers: Introduce a mapping from nid to tier_slot Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 04/14] mm/memcontrol: Allocate per-tier page_counters Joshua Hahn
2026-08-07 20:20 ` Joshua Hahn [this message]
2026-08-07 20:20 ` [RFC PATCH v3 06/14] mm/vmscan, memcontrol: Add nodemask to try_to_free_mem_cgroup_pages Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 07/14] mm/memcontrol: Charge/uncharge tiered memory to mem_cgroup Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 08/14] mm/memcontrol: Make memory.low and memory.min tier-aware Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 09/14] mm/memcontrol: Make memory.high tier-aware Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 10/14] mm/memcontrol: Make memory.max tier-aware Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 11/14] mm/memcontrol, migrate: Transfer tier charge on migration Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 12/14] mm/memcontrol: Kick async reclaim on migration and folio replacement Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 13/14] mm/memcontrol, sched/numa: Gate NUMA promotions into memcg tiers Joshua Hahn
2026-08-07 20:20 ` [RFC PATCH v3 14/14] mm/page_alloc: steer allocations away from exhausted memory tiers Joshua Hahn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260807202059.2620949-6-joshua.hahnjy@gmail.com \
--to=joshua.hahnjy@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=axelrasmussen@google.com \
--cc=baohua@kernel.org \
--cc=bsegall@google.com \
--cc=byungchul@sk.com \
--cc=cgroups@vger.kernel.org \
--cc=david@kernel.org \
--cc=dietmar.eggemann@arm.com \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=harry@kernel.org \
--cc=jackmanb@google.com \
--cc=juri.lelli@redhat.com \
--cc=kasong@tencent.com \
--cc=kernel-team@meta.com \
--cc=kprateek.nayak@amd.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mgorman@suse.de \
--cc=mhocko@kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=muchun.song@linux.dev \
--cc=peterz@infradead.org \
--cc=qi.zheng@linux.dev \
--cc=rakie.kim@sk.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=surenb@google.com \
--cc=tjmercier@google.com \
--cc=vbabka@kernel.org \
--cc=vincent.guittot@linaro.org \
--cc=vschneid@redhat.com \
--cc=weixugc@google.com \
--cc=ying.huang@linux.alibaba.com \
--cc=yosry@kernel.org \
--cc=yuanchu@google.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox