mm: page_counter: move hierarchical protection out of struct page_counter
Subsystems (6)
v2 cover mm: page_counter: move hierarchical protection out of struct page_counter
Hierarchical memory protection (memory.min / memory.low) is built on struct page_counter today: every counter carries the full protection state - emin/elow, the protected-usage trackers (min_usage, children_min_usage, low_usage, children_low_usage), the configured min/low values and a protection_support flag - although only the memory page counter (and dmem pools) ever participates in protection. swap/memsw, kmem, tcpmem and hugetlb counters ship this state around unused.
This series moves that state into a dedicated struct page_counter_protection, instantiated only for the counters that actually support protection, which shrinks struct page_counter by one cache line.
Patch 1 adds struct page_counter_protection and links it to struct page_counter through a ->prot pointer (NULL when protection is not supported). page_counter_init() loses its protection_support argument, and the new page_counter_init_protection() attaches the context. Protection stays enabled only on the cgroup v2 hierarchy, matching the previous page_counter_init(..., memcg_on_dfl) behaviour, and the root memcg keeps it unconditionally.
Patch 2 migrates the read/write side of protection onto the new
structure: propagate_protected_usage(), page_counter_set_min()/low() and page_counter_calculate_protection() now operate on the protection context, and the memcg and dmem accessors (mem_cgroup_protection, mem_cgroup_below_min/low, the dmem below_min/low helpers and the dmem eviction check) read emin/elow/children_*_usage from it.
Patch 3 deletes the now-unused fields from struct page_counter. On 64-bit the structure drops from three cache lines to two, one cache line saved per counter. For reference, pahole shows the layout before and after (x86_64, 64-byte cache lines):
before: after:
0 usage 0 usage
8 failcnt 8 failcnt
64 emin 64 watermark
72 min_usage 72 local_watermark
80 children_min_usage 80 track_failcnt
88 elow 88 high
96 low_usage 96 max
104 children_low_usage 104 parent 112 watermark 112 prot 120 local_watermark 128 protection_support size 128, 2 cachelines, 129 track_failcnt 11 members 136 min 144 low (the protection fields moved 152 high into struct 160 max page_counter_protection, 168 parent 72 bytes, allocated only
where protection is used)
size 192, 3 cachelines, 19 members
The four embedded page counters of struct mem_cgroup all shrink by 64 bytes, which translates to 128 bytes saved per cgroup once the one embedded page_counter_protection is accounted for (2176 -> 2048 bytes with CONFIG_MEMCG_V1=y, verified with pahole).
No functional change is intended: protection semantics and the cgroup v1/v2 behaviour are preserved.
---
Changes in v2:
- dmem: fix up prot.parent in get_cg_pool_locked() too, so bottom-up
created pools keep hierarchical protection.
- Drop the orphaned _pad2_ padding and its stale comment from struct page_counter.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
linuszeng (3):
mm: page_counter: add page_counter_protection struct and init API
mm: page_counter: track protection state in page_counter_protection
mm: page_counter: drop protection fields from struct page_counter
include/linux/memcontrol.h | 15 ++++++--- include/linux/page_counter.h | 73 +++++++++++++++++++++++++++++++------------- kernel/cgroup/dmem.c | 21 +++++++------ mm/hugetlb_cgroup.c | 4 +-- mm/memcontrol.c | 29 ++++++++++-------- mm/page_counter.c | 61 +++++++++++++++++++++++------------- 6 files changed, 133 insertions(+), 70 deletions(-)
---
base-commit: d118502628f8b673be9023db8bdf878f64a7ed45
change-id: 20260909-descriptive-name-e382a3f978dd
Best regards,
Signature · 1 line
linuszeng <[email protected]>
PATCH v2 1/3 mm: page_counter: add page_counter_protection struct and init API
This commit extracts the hierarchical protection state (memory.min and memory.low) from struct page_counter into a new page_counter_protection structure. It introduces page_counter_init_protection() to attach this context, saving space for counters that don't support protection.
The dmem pool allocator now points its counter at the embedded protection context, and the pool fix-up path in get_cg_pool_locked() links the new prot->parent the same way it links cnt.parent, so pools created bottom-up do not lose hierarchical protection.
No functional change.
--- include/linux/memcontrol.h | 7 ++++++ include/linux/page_counter.h | 59 +++++++++++++++++++++++++++++++++++++++----- kernel/cgroup/dmem.c | 9 ++++--- mm/hugetlb_cgroup.c | 4 +-- mm/memcontrol.c | 21 ++++++++++------ mm/page_counter.c | 2 +- 6 files changed, 82 insertions(+), 20 deletions(-)
include/linux/memcontrol.h +7 -0
@@ -195,6 +195,13 @@ struct mem_cgroup {/* Accounted resources */ struct page_counter memory;/* Both v1 & v2 */ +/* +* Hierarchical memory.min/memory.low protection tracking for the +* memory page counter. swap/memsw, kmem and tcpmem counters do not +* support protection and have no such context. +*/ +struct page_counter_protection memory_prot; +union {struct page_counter swap;/* v2 only */ struct page_counter memsw;/* v1 only */
include/linux/page_counter.h +53 -6
83 lines not on this page · raw message
kernel/cgroup/dmem.c +6 -3
@@ -88,6 +88,7 @@ struct dmem_cgroup_pool_state {struct rcu_head rcu;struct page_counter cnt; +struct page_counter_protection prot;struct dmem_cgroup_pool_state *parent;refcount_t ref;
@@ -426,8 +427,9 @@ alloc_pool_single(struct dmemcg_state *dmemcs, struct dmem_cgroup_region *regionif (parent) ppool =find_cg_pool_locked (parent, region); -page_counter_init (&pool->cnt, - ppool ? &ppool->cnt :NULL , true); +page_counter_init (&pool->cnt, ppool ? &ppool->cnt :NULL ); +page_counter_init_protection (&pool->cnt, &pool->prot, + ppool ? &ppool->prot :NULL );reset_all_resource_limits (pool);refcount_set (&pool->ref,1 );kref_get (®ion->ref);
@@ -480,8 +482,9 @@ get_cg_pool_locked(struct dmemcg_state *dmemcs, struct dmem_cgroup_region *regio/* ppool was created if it didn't exist by above loop. */ ppool =find_cg_pool_locked (pp, region); -/* Fix up parent links, mark as inited. */ +/* Fix up parent links (counter and protection), mark as inited. */ pool->cnt.parent = &ppool->cnt; + pool->prot.parent = &ppool->prot;if (ppool && !pool->parent) { pool->parent = ppool;dmemcg_pool_get (ppool);
mm/hugetlb_cgroup.c +2 -2
@@ -108,8 +108,8 @@ static void hugetlb_cgroup_init(struct hugetlb_cgroup *h_cgroup, fault =hugetlb_cgroup_counter_from_cgroup (h_cgroup, idx); rsvd =hugetlb_cgroup_counter_from_cgroup_rsvd (h_cgroup, idx); -page_counter_init (fault, fault_parent, false); -page_counter_init (rsvd, rsvd_parent, false); +page_counter_init (fault, fault_parent); +page_counter_init (rsvd, rsvd_parent);if (!cgroup_subsys_on_dfl (hugetlb_cgrp_subsys)) { fault->track_failcnt = true;
mm/memcontrol.c +13 -8
@@ -4267,25 +4267,30 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)#endif page_counter_set_high (&memcg->swap,PAGE_COUNTER_MAX );if (parent) { -page_counter_init (&memcg->memory, &parent->memory, memcg_on_dfl); -page_counter_init (&memcg->swap, &parent->swap, false); +page_counter_init (&memcg->memory, &parent->memory); +if (memcg_on_dfl) +page_counter_init_protection (&memcg->memory, &memcg->memory_prot, + &parent->memory_prot); +page_counter_init (&memcg->swap, &parent->swap);#ifdef CONFIG_MEMCG_V1 WRITE_ONCE (memcg->swappiness,mem_cgroup_swappiness (parent)); memcg->memory.track_failcnt = !memcg_on_dfl; memcg->memsw.track_failcnt = !memcg_on_dfl;WRITE_ONCE (memcg->oom_kill_disable,READ_ONCE (parent->oom_kill_disable)); -page_counter_init (&memcg->kmem, &parent->kmem, false); -page_counter_init (&memcg->tcpmem, &parent->tcpmem, false); +page_counter_init (&memcg->kmem, &parent->kmem); +page_counter_init (&memcg->tcpmem, &parent->tcpmem); memcg->tcpmem.track_failcnt = !memcg_on_dfl;#endif }else {init_memcg_stats ();init_memcg_events (); -page_counter_init (&memcg->memory,NULL , true); -page_counter_init (&memcg->swap,NULL , false); +page_counter_init (&memcg->memory,NULL ); +page_counter_init_protection (&memcg->memory, &memcg->memory_prot, +NULL ); +page_counter_init (&memcg->swap,NULL );#ifdef CONFIG_MEMCG_V1 -page_counter_init (&memcg->kmem,NULL , false); -page_counter_init (&memcg->tcpmem,NULL , false); +page_counter_init (&memcg->kmem,NULL ); +page_counter_init (&memcg->tcpmem,NULL );#endif root_mem_cgroup = memcg;return &memcg->css;
mm/page_counter.c +1 -1
@@ -15,7 +15,7 @@static bool track_protection (struct page_counter *c) { -return c->protection_support; +return c->prot !=NULL ; } static void propagate_protected_usage(struct page_counter *c,
Signature · 1 line
2.43.7
PATCH v2 2/3 mm: page_counter: track protection state in page_counter_protection
In reply to linuszengMove the read/write side of hierarchical protection from struct page_counter to struct page_counter_protection: propagate_protected_usage() updates the protection context of the parent, page_counter_set_min()/low() and page_counter_calculate_protection() operate on it, and memcg and dmem accessors (including dmem_cgroup_below_min()/below_low()) read min/low/emin/elow and children_*_usage from it.
struct page_counter keeps its now-unused protection fields for now; they are removed in a follow-up commit.
No functional change.
--- include/linux/memcontrol.h | 8 +++---- kernel/cgroup/dmem.c | 12 +++++----- mm/memcontrol.c | 8 +++---- mm/page_counter.c | 59 +++++++++++++++++++++++++++++----------------- 4 files changed, 52 insertions(+), 35 deletions(-)
include/linux/memcontrol.h +4 -4
@@ -591,8 +591,8 @@ static inline void mem_cgroup_protection(struct mem_cgroup *root,if (root == memcg)return ; - *min =READ_ONCE (memcg->memory.emin); - *low =READ_ONCE (memcg->memory.elow); + *min =READ_ONCE (memcg->memory_prot.emin); + *low =READ_ONCE (memcg->memory_prot.elow); } void mem_cgroup_calculate_protection(struct mem_cgroup *root,
@@ -616,7 +616,7 @@ static inline bool mem_cgroup_below_low(struct mem_cgroup *target,if (mem_cgroup_unprotected (target, memcg))return false; -return READ_ONCE (memcg->memory.elow) >= +return READ_ONCE (memcg->memory_prot.elow) >=page_counter_read (&memcg->memory); }
@@ -626,7 +626,7 @@ static inline bool mem_cgroup_below_min(struct mem_cgroup *target,if (mem_cgroup_unprotected (target, memcg))return false; -return READ_ONCE (memcg->memory.emin) >= +return READ_ONCE (memcg->memory_prot.emin) >=page_counter_read (&memcg->memory); }
kernel/cgroup/dmem.c +6 -6
@@ -212,12 +212,12 @@ set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val, bool nonblock)static u64 get_resource_low (struct dmem_cgroup_pool_state *pool) { -return pool ?READ_ONCE (pool->cnt.low) :0 ; +return pool ?READ_ONCE (pool->cnt.prot->low) :0 ; }static u64 get_resource_min (struct dmem_cgroup_pool_state *pool) { -return pool ?READ_ONCE (pool->cnt.min) :0 ; +return pool ?READ_ONCE (pool->cnt.prot->min) :0 ; }static u64 get_resource_max (struct dmem_cgroup_pool_state *pool)
@@ -388,13 +388,13 @@ bool dmem_cgroup_state_evict_valuable(struct dmem_cgroup_pool_state *limit_pool,dmem_cgroup_calculate_protection (limit_pool, test_pool); used =page_counter_read (ctest); - min =READ_ONCE (ctest->emin); + min =READ_ONCE (ctest->prot->emin);if (used <= min)return false;if (!ignore_low) { - low =READ_ONCE (ctest->elow); + low =READ_ONCE (ctest->prot->elow);if (used > low)return true;
@@ -787,7 +787,7 @@ bool dmem_cgroup_below_min(struct dmem_cgroup_pool_state *root,* here. */ dmem_cgroup_calculate_protection (root, test); -return page_counter_read (&test->cnt) <=READ_ONCE (test->cnt.emin); +return page_counter_read (&test->cnt) <=READ_ONCE (test->cnt.prot->emin); }EXPORT_SYMBOL_GPL (dmem_cgroup_below_min);
@@ -818,7 +818,7 @@ bool dmem_cgroup_below_low(struct dmem_cgroup_pool_state *root,* here. */ dmem_cgroup_calculate_protection (root, test); -return page_counter_read (&test->cnt) <=READ_ONCE (test->cnt.elow); +return page_counter_read (&test->cnt) <=READ_ONCE (test->cnt.prot->elow); }EXPORT_SYMBOL_GPL (dmem_cgroup_below_low);
mm/memcontrol.c +4 -4
@@ -4823,7 +4823,7 @@ static ssize_t memory_peak_write(struct kernfs_open_file *of, char *buf,static int memory_min_show (struct seq_file *m,void *v) {return seq_puts_memcg_tunable (m, -READ_ONCE (mem_cgroup_from_seq (m)->memory.min)); +READ_ONCE (mem_cgroup_from_seq (m)->memory_prot.min)); } static ssize_t memory_min_write(struct kernfs_open_file *of,
@@ -4846,7 +4846,7 @@ static ssize_t memory_min_write(struct kernfs_open_file *of,static int memory_low_show (struct seq_file *m,void *v) {return seq_puts_memcg_tunable (m, -READ_ONCE (mem_cgroup_from_seq (m)->memory.low)); +READ_ONCE (mem_cgroup_from_seq (m)->memory_prot.low)); } static ssize_t memory_low_write(struct kernfs_open_file *of,
@@ -6271,6 +6271,6 @@ void mem_cgroup_show_protected_memory(struct mem_cgroup *memcg) memcg = root_mem_cgroup;pr_warn ("Memory cgroup min protection %lukB -- low protection %lukB" , -K (atomic_long_read (&memcg->memory.children_min_usage)), -K (atomic_long_read (&memcg->memory.children_low_usage))); +K (atomic_long_read (&memcg->memory_prot.children_min_usage)), +K (atomic_long_read (&memcg->memory_prot.children_low_usage))); }
mm/page_counter.c +38 -21
112 lines not on this page · raw message
Signature · 1 line
2.43.7
[email protected]
AI reviewRe: [PATCH v2 2/3] mm: page_counter: track protection state in page_counter_protection
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent dmem cgroup eviction can observe a partially initialized pool and trigger a NULL pointer dereference. --
commit 505dda1717b6dcbcc1903e6c7cc5e90077d70bb6
Author: linuszeng <[email protected]>
Subject: mm: page_counter: track protection state in page_counter_protection
This patch moves the read/write side of hierarchical protection from struct page_counter to struct page_counter_protection. It updates propagate_protected_usage and calculate_protection, along with accessors to operate on the new context structure. The patch intends to introduce no functional changes.
Quoted 4 lines
mm/page_counter.c +0
-0
[ ... ]
mm/page_counter.c void page_counter_calculate_protection(struct page_counter *root, · line 487
Quoted 12 lines
mm/page_counter.c +3
-2@@ -463,23 +479,24 @@ void page_counter_calculate_protection(struct page_counter *root,return ;if (parent == root) { - counter->emin =READ_ONCE (counter->min); - counter->elow =READ_ONCE (counter->low); + prot->emin =READ_ONCE (prot->min); + prot->elow =READ_ONCE (prot->low);return ; } + parent_prot = parent->prot;
[Severity: High]
This is a pre-existing issue, but could this code encounter a null pointer dereference if concurrent dmem cgroup eviction observes a partially initialized pool?
Looking at alloc_pool_single in kernel/cgroup/dmem.c, a new pool is published to the RCU-protected pools list before its parent link is fully established:
alloc_pool_single()
...
list_add_tail_rcu(&pool->css_node, &dmemcs->pools);
...If a concurrent thread running dmem_cgroup_state_evict_valuable or dmem_cgroup_below_min iterates over the cgroup descendants via css_for_each_descendant_pre, it can find this partially initialized pool and pass it to page_counter_calculate_protection.
Since counter->parent is null and does not match the root argument, the early return above is skipped, leading to a dereference of parent->prot here when parent is null.
Does the eviction path need to synchronize with the completion of pool initialization, or should page_counter_calculate_protection check if parent is null before dereferencing it?
Quoted 5 lines
mm/page_counter.c +0
-3parent_usage =page_counter_read (parent); - WRITE_ONCE(counter->emin, effective_protection(usage, parent_usage, - READ_ONCE(counter->min), - READ_ONCE(parent->emin),
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/3 mm: page_counter: drop protection fields from struct page_counter
In reply to linuszengThe protection state now lives in struct page_counter_protection, so remove the emin/min_usage/children_min_usage, elow/low_usage/ children_low_usage, min, low and protection_support fields from struct page_counter.
Also drop the now-orphaned _pad2_ padding and its comment: with the protection fields gone it no longer separates the read-mostly fields from anything, and the structure's cacheline alignment already pads the tail out.
swap/memsw, kmem, tcpmem and hugetlb counters no longer carry this unused state: on 64-bit the structure shrinks from three cache lines to two, saving one cache line.
--- include/linux/page_counter.h | 16 ---------------- 1 file changed, 16 deletions(-)
include/linux/page_counter.h +0 -16
@@ -43,27 +43,11 @@ struct page_counter {CACHELINE_PADDING (_pad1_); -/* effective memory.min and memory.min usage tracking */ -unsigned long emin; -atomic_long_t min_usage; -atomic_long_t children_min_usage; - -/* effective memory.low and memory.low usage tracking */ -unsigned long elow; -atomic_long_t low_usage; -atomic_long_t children_low_usage; -unsigned long watermark;/* Latest cg2 reset watermark */ unsigned long local_watermark; -/* Keep all the read most fields in a separete cacheline. */ -CACHELINE_PADDING (_pad2_); - -bool protection_support;bool track_failcnt; -unsigned long min; -unsigned long low;unsigned long high;unsigned long max;struct page_counter *parent;
Signature · 1 line
2.43.7
Michal Koutný
maintainerHi.
Quoted 2 lines · On Wed, Sep 09, 2026 at 05:44:18PM +0800, linuszeng via B4 Relay…
No functional change is intended: protection semantics and the cgroup v1/v2 behaviour are preserved.
It is not clear from the description what is the intention then :-)
Do you have any measurements that the reduced cache footprint changes performance for setups without protection?
And what is the positive impact on protected scenarios where the counters are in (possibly) different cacheline and one indirection further?
Thanks,
Michal
Quoted 8 lines · Michal Koutný
Hi.
Quoted 2 lines · On Wed, Sep 09, 2026 at 05:44:18PM +0800, linuszeng via B4 Relay…
No functional change is intended: protection semantics and the cgroup v1/v2 behaviour are preserved.
It is not clear from the description what is the intention then :-)
You are right, and that is my fault for posting these three patches without the context they came from.
They implement what Johannes asked for when I last tried to make a combined memory+swap limit available on the default hierarchy [1]:
My suggestion is to factor out from struct page_counter all the stuff that is not necessary for all users, and then have separate counters for swap and memsw.
The protection stuff is long overdue for this. It makes up nearly half of the struct's members, but is only used by the memory counter. Even before your patches this is unnecessary bloat in the swap/memsw, kmem and tcpmem counters.
Fix that and having separate counters is a non-issue.
and, in the same thread, about the cost of a second counter [2]:
It seems like a good opportunity to refactor struct page_counter.
So the intention is not the cache footprint on its own.
The combined memory+swap counter is the one v1 exposes as memsw.limit_in_bytes. On the default hierarchy it does not exist:
struct mem_cgroup keeps swap and memsw in a union, because v1 only ever charges memsw and v2 only ever charges swap, so the two never needed to be live at the same time. Making the combined limit available on v2 means charging both on both hierarchies, which means giving them separate page counters.
That is where struct page_counter comes in. Adding a counter costs a cgroup one more of them, and at 192 bytes each that is 192 bytes per cgroup for a feature most of them will not use. Trimming the counter to 128 bytes first frees 128 bytes per cgroup, which is very nearly what the new counter then costs, so the combined limit becomes close to free in struct mem_cgroup rather than something every cgroup pays for. This is the "good opportunity to refactor struct page_counter" from [2], and it is why the preparation is a prerequisite rather than a cleanup I happened to do on the side.
The follow-up is written and tested; I should have posted it together with these patches instead of sending the preparation on its own, and I will do that now (details at the end).
Quoted 3 lines
Do you have any measurements that the reduced cache footprint changes performance for setups without protection?
No. So far I have only used pahole to look at the cache line footprint of struct mem_cgroup and struct page_counter (pahole, x86_64, 64-byte cache lines, CONFIG_MEMCG_V1=y)::
struct page_counter 192 -> 128 bytes (3 -> 2 cache lines)
struct page_counter_protection - -> 72 bytes
struct mem_cgroup 2176 -> 2048 bytes
The four embedded counters lose 64 bytes each and the one protection context takes 72 back, which nets out to 128 bytes per cgroup. The counters that never participate in protection - swap/memsw, kmem, tcpmem and hugetlb - are also down from three cache lines to two.
The reason I need these patches is the prerequisite
above, and those 128 bytes are exactly what the follow-up needs to afford splitting the swap and memsw counters.
Quoted 3 lines
And what is the positive impact on protected scenarios where the counters are in (possibly) different cacheline and one indirection further?
There is none, and in the form I posted it was worse than before. Your reading of the layout was correct.
propagate_protected_usage() runs once per level on every charge and uncharge, and touches min, low, min_usage, low_usage and the parent's children_{min,low}_usage. Counting the cache lines each level touches:
before this series page_counter 3 + parent 1 = 4, no indirection v2 as posted page_counter 2 + prot 2 + parent prot 1 = 5 with the fix below page_counter 2 + prot 1 + parent prot 1 = 4
In v2 the new structure kept the field order of the old one, which put min at offset 56 and low at 64. The two values the propagation path reads together ended up on either side of a cache line boundary, while emin and elow - which are only recomputed by page_counter_calculate_protection() during reclaim - occupied the first line. That is how the count got to 5.
The seven fields the charge path touches are 56 bytes and do fit in one line, so I have reordered the structure to parent, min, low, min_usage, low_usage, children_min_usage, children_low_usage, then emin and elow last. Both embedders already place the context on a cache line boundary
- offset 384 in struct mem_cgroup, 192 in the dmem pool state - so no alignment attribute is needed and the structure stays 72 bytes. That brings the per-level line count back to what it was before the series.
The dependent load of ->prot stays; it cannot be removed while the state lives outside the counter. The pointer shares a line with ->parent and ->local_watermark, which the same loop reads anyway, so it costs an address dependency rather than an extra miss.
To summarise honestly: this series is size-neutral for a cgroup and, after the reorder, layout-neutral for protected charging. It earns its place only as groundwork for the combined limit.
So rather than reposting these three patches on their own, I am going to send them as the first half of
[PATCH 0/6] mm/memcontrol: implement the memsw limit on cgroup v2
which is not posted yet; it will follow shortly after this reply, with the reordered protection context folded into patch 1. The second half builds directly on them: it splits the swap and memsw page counters out of their union - which is what needs struct page_counter to have stopped carrying the protection state - maintains the combined counter on both hierarchies, and adds memory.memsw.current and memory.memsw.max to the default hierarchy.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
Thanks for looking at this.
Quoted 3 lines
Thanks,
Michal