mm/page_counter: move stock from mem_cgroup to page_counter
Subsystems (6)
v6 cover mm/page_counter: move stock from mem_cgroup to page_counter
v5 --> v6
=========
Following feedback that v5 combined the (1) stock abstraction move from memcg to page_counter and (2) changing the allocation / draining behavior, v6 limits itself to only the first goal. It retains the existing seven-slot per-CPU design and drain policy.
INTRODUCTION
============
Memcg keeps a per-CPU stock of precharged pages so that small, frequent allocations do not walk the page_counter hierarchy every time. Today, the stock implementation is within memcontrol code, even though the operation it caches is a page_counter charge. This makes it difficult to add new page_counters to a memcg and preserve the fast path behavior.
This matters for future work like my tiered memcg limits series [1] which introduces multiple new page_counters to memcg. Without making stock a page_counter-level property, it means that every memcg charge now goes through multiple page_counter hierarchy walks, instead of being able to cache these charges.
To make future page_counters scalable and performant, move stock from mem_cgroup to page_counter so that each page_counter can opt into its own per-CPU cache of pre-charged pages.
We get an added benefit of simplifying try_charge_memcg code, which now has all the stock management handled transparently within the page_counter layer.
EFFECT ON MEMCG V2 USERS
========================
This series has no functional changes intended for memcg v2 users. We preserve all draining, refilling, and (un)charging behavior, including the uncharge path's refills / direct uncharges.
EFFECT ON MEMCG V1 USERS
========================
For memcg v1 users, the decoupling of the memsw and memory stock means that each of them now manage their own independent stocks and can lead to a different size of precharged cache for each.
Cgroup v1 has an invariant that memory.memsw.usage_in_bytes is larger than or equal to memory.usage_in_bytes, because memsw is a superset of memory. With separate stocking, this could have been broken in scenarios where the memory stock is bigger than the memsw stock, leading to memory usage appearing to be inflated and greater than memsw usage, even though the real usage preserves the invariant.
To prevent this, report the larger value of memory and memsw usage_in_bytes for memsw reporting, so that the invariant isn't broken. This is a bounded stock-related overestimate and does not affect limit enforcement.
Based on latest mm-new as of 9/16/26:892f5b3b07e5b "mm/swap, PM: hibernate: atomically replace hibernation pin"
[1] https://lore.kernel.org/all/[email protected]/
Joshua Hahn (5):
mm/memcontrol: flatten try_charge_memcg control flow mm/page_counter: introduce per-CPU stock mm/page_counter: make page_counter_try_charge() stock-aware mm/memcontrol: move memory stock to page counters mm/memcontrol: add stock to the memsw page counter
include/linux/page_counter.h | 35 +++- kernel/cgroup/dmem.c | 2 +- mm/hugetlb_cgroup.c | 2 +- mm/memcontrol-v1.c | 12 +- mm/memcontrol.c | 328 ++++++++++------------------------- mm/page_counter.c | 209 ++++++++++++++++++++-- 6 files changed, 334 insertions(+), 254 deletions(-)
Signature · 1 line
2.53.0-Meta
PATCH v6 1/5 mm/memcontrol: flatten try_charge_memcg control flow
Refactor try_charge_memcg by flattening the nested memsw/memory page_counter operations to separate the logic between the two.
When page_counter_try_charge is made stock-aware, this flattening makes the control flow easier to follow since each page counter now has its own success/failure paths.
No functional changes intended.
--- mm/memcontrol.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-)
mm/memcontrol.c +11 -8
@@ -2707,18 +2707,21 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, batch = nr_pages; reclaim_options =MEMCG_RECLAIM_MAY_SWAP ; -if (!do_memsw_account () || -page_counter_try_charge (&memcg->memsw, batch, &counter)) { -if (page_counter_try_charge (&memcg->memory, batch, &counter)) - goto done_restock; -if (do_memsw_account ()) -page_counter_uncharge (&memcg->memsw, batch); - mem_over_limit =mem_cgroup_from_counter (counter, memory); - }else { +if (do_memsw_account () && + !page_counter_try_charge (&memcg->memsw, batch, &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, batch, &counter)) + goto done_restock; + +if (do_memsw_account ()) +page_counter_uncharge (&memcg->memsw, batch); + mem_over_limit =mem_cgroup_from_counter (counter, memory); + +reclaim:if (batch > nr_pages) { batch = nr_pages; goto retry;
Signature · 1 line
2.53.0-Meta
PATCH v6 2/5 mm/page_counter: introduce per-CPU stock
In reply to Joshua HahnIntroduce a copy of the seven-slot per-CPU stock representation from memcg in the page_counter layer. struct page_counter_stock_pcp preserves everything from struct memcg_stock_pcp, but adds a new backpointer to the base of the percpu stock, since there will be multiple percpu stock base pointers later in the series (one for memory, one for memsw).
struct page_counter also gets a pointer to the base of the percpu stock, as well as a struct cgroup_subsys_state pointer to pin its owning CSS as long as the cached counter remains reachable.
Let's also copy over the drain, refill, and "flush required" functions, preserving all behaviors from the memcg equivalent. In this patch they are not connected.
No functional changes intended.
--- include/linux/page_counter.h | 31 +++++++++ mm/page_counter.c | 127 +++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+)
include/linux/page_counter.h +31 -0
@@ -5,8 +5,30 @@#include <linux/atomic.h> #include <linux/cache.h> #include <linux/limits.h> +#include <linux/percpu.h> +#include <linux/local_lock.h> +#include <linux/workqueue_types.h> #include <asm/page.h> +/* +* The value of NR_PAGE_COUNTER_STOCK is selected to keep the cached counters +* and their nr_pages in a single cacheline. This may change in the future. +*/ +#define NR_PAGE_COUNTER_STOCK 7 +#define PAGE_COUNTER_STOCK_BATCH 64UL +struct cgroup_subsys_state ; +struct page_counter ; +struct page_counter_stock_pcp { +local_trylock_t lock; +u8 nr_pages[NR_PAGE_COUNTER_STOCK ]; +struct page_counter *cached[NR_PAGE_COUNTER_STOCK ]; + +struct page_counter_stock_pcp __percpu *base; +struct work_struct work; +unsigned long flags; +u8 drain_idx; +}; +struct page_counter {/* * Make sure 'usage' does not share cacheline with any other field in
@@ -41,6 +63,8 @@ struct page_counter {unsigned long high;unsigned long max;struct page_counter *parent; +struct page_counter_stock_pcp __percpu *stock; +struct cgroup_subsys_state *stock_css; }____cacheline_internodealigned_in_smp ;#if BITS_PER_LONG ==32
@@ -61,6 +85,8 @@ static inline void page_counter_init(struct page_counter *counter, counter->parent = parent; counter->protection_support = protection_support; counter->track_failcnt = false; + counter->stock =NULL ; + counter->stock_css =NULL ; }static inline unsigned long page_counter_read (struct page_counter *counter)
@@ -74,6 +100,11 @@ void page_counter_charge(struct page_counter *counter, unsigned long nr_pages);bool page_counter_try_charge (struct page_counter *counter,unsigned long nr_pages,struct page_counter **fail); +void page_counter_refill_stock (struct page_counter *counter, +unsigned long nr_pages); +void page_counter_drain_stock_fully (struct page_counter_stock_pcp *stock); +bool page_counter_stock_flush_required (struct page_counter_stock_pcp *stock, +struct cgroup_subsys_state *root_css);void page_counter_uncharge (struct page_counter *counter,unsigned long nr_pages);void page_counter_set_min (struct page_counter *counter,unsigned long nr_pages);void page_counter_set_low (struct page_counter *counter,unsigned long nr_pages);
mm/page_counter.c +127 -0
145 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v6 3/5 mm/page_counter: make page_counter_try_charge() stock-aware
In reply to Joshua HahnMake page_counter_try_charge() consume stock transparently to callers while preserving the same semantics as try_charge_memcg's greedy charge attempt, refill, and !allow_spinning special case.
page_counter_try_charge gets two new parameters, may_batch and nr_charged.
may_batch is intended to preserve the behavior of !allow_spinning scenarios in try_charge_memcg, where the goal is to charge as quickly as possible, without evicting other stock slots or making a greedy charge to refill the stock.
nr_charged is used to reflect the size of the successful hierarchy charge, preserving the existing batch-based memory.high accounting for current->memcg_nr_pages_over_high.
As of this patch, no page_counter has stock yet, so there are no functional changes intended.
--- include/linux/page_counter.h | 4 +- kernel/cgroup/dmem.c | 2 +- mm/hugetlb_cgroup.c | 2 +- mm/memcontrol-v1.c | 3 +- mm/memcontrol.c | 8 ++-- mm/page_counter.c | 82 +++++++++++++++++++++++++++++++----- 6 files changed, 83 insertions(+), 18 deletions(-)
include/linux/page_counter.h +2 -2
@@ -98,8 +98,8 @@ long page_counter_margin(struct page_counter *counter);void page_counter_cancel (struct page_counter *counter,unsigned long nr_pages);void page_counter_charge (struct page_counter *counter,unsigned long nr_pages);bool page_counter_try_charge (struct page_counter *counter, -unsigned long nr_pages, -struct page_counter **fail); +unsigned long nr_pages,struct page_counter **fail, +bool may_batch,unsigned long *nr_charged);void page_counter_refill_stock (struct page_counter *counter,unsigned long nr_pages);void page_counter_drain_stock_fully (struct page_counter_stock_pcp *stock);
kernel/cgroup/dmem.c +1 -1
@@ -736,7 +736,7 @@ int dmem_cgroup_try_charge(struct dmem_cgroup_region *region, u64 size, goto err; } -if (!page_counter_try_charge (&pool->cnt, size, &fail)) { +if (!page_counter_try_charge (&pool->cnt, size, &fail, false,NULL )) {if (ret_limit_pool) { *ret_limit_pool = container_of(fail, struct dmem_cgroup_pool_state, cnt);css_get (&(*ret_limit_pool)->cs->css);
mm/hugetlb_cgroup.c +1 -1
@@ -274,7 +274,7 @@ static int __hugetlb_cgroup_charge_cgroup(int idx, unsigned long nr_pages,if (!page_counter_try_charge (__hugetlb_cgroup_counter_from_cgroup (h_cg, idx, rsvd), - nr_pages, &counter)) { + nr_pages, &counter, false,NULL )) { ret = -ENOMEM ;hugetlb_event (h_cg, idx,HUGETLB_MAX );css_put (&h_cg->css);
mm/memcontrol-v1.c +2 -1
@@ -2194,7 +2194,8 @@ bool memcg1_charge_skmem(struct mem_cgroup *memcg, unsigned int nr_pages, {struct page_counter *fail; -if (page_counter_try_charge (&memcg->tcpmem, nr_pages, &fail)) { +if (page_counter_try_charge (&memcg->tcpmem, nr_pages, &fail, + false,NULL )) { memcg->tcpmem_pressure =0 ;return true; }
mm/memcontrol.c +5 -3
@@ -2708,13 +2708,14 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, reclaim_options =MEMCG_RECLAIM_MAY_SWAP ;if (do_memsw_account () && - !page_counter_try_charge (&memcg->memsw, batch, &counter)) { + !page_counter_try_charge (&memcg->memsw, batch, &counter, false, +NULL )) { mem_over_limit =mem_cgroup_from_counter (counter, memsw); reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP ; goto reclaim; } -if (page_counter_try_charge (&memcg->memory, batch, &counter)) +if (page_counter_try_charge (&memcg->memory, batch, &counter, false,NULL )) goto done_restock;if (do_memsw_account ())
@@ -5965,7 +5966,8 @@ int __mem_cgroup_try_charge_swap(struct folio *folio)rcu_read_unlock ();if (!mem_cgroup_is_root (memcg) && - !page_counter_try_charge (&memcg->swap, nr_pages, &counter)) { + !page_counter_try_charge (&memcg->swap, nr_pages, &counter, false, +NULL )) {memcg_memory_event (memcg,MEMCG_SWAP_MAX );memcg_memory_event (memcg,MEMCG_SWAP_FAIL );mem_cgroup_private_id_put (memcg, nr_pages);
mm/page_counter.c +72 -10
115 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v6 4/5 mm/memcontrol: move memory stock to page counters
In reply to Joshua HahnTransition memcg to use the page_counter_stock for the memory page_counter instead of relying on a memcg-wide stock.
One aspect that remains non-transparent to memcg is the uncharge path. This is intentional, as the caller is responsible for managing the batching. Cacheable releases refill the stock, while already-batched frees (i.e. uncharge_gather), rollbacks, and accounting transfers uncharge the hierarchy directly. The refill helper itself already falls back to a raw uncharge when the stock cannot accept the pages anyways.
Because the memory and memsw counters no longer share one stock, their raw values can temporarily diverge. Preserve the legacy user-visible memory <= memory+swap invariant by reporting the larger raw value for memory.memsw.usage_in_bytes. This masks the temporary inversions caused by the decoupling of the single memcg stock.
Note that this remains a bounded stock-related overestimate, consistent with the existing fuzzy usage reporting for usage.
With this transition, remove all memcg code that is no longer used. After all of this, there should be no functional change for cgroup v2 users. All v2 behaviors from memcg are preserved, just moved from memcg to page_counter code, so that future work can introduce additional page_counters without removing the fast path. Explicitly, the preserved behaviors are:
- 7-slot stock
- drain policy works locally and remotely through the memcg_wq
- check whether a stock requires flushing before taking action
- exact charging for non-spinning callers
- report hierarchy growth for memory.high overage accounting
As of this patch, this leaves memsw un-stocked and always taking the slow path (raw hierarchy charge). The next patch will make memsw stocked, which will close the fast path gap for legacy cgroup users.
--- mm/memcontrol-v1.c | 9 +- mm/memcontrol.c | 279 ++++++++------------------------------------- 2 files changed, 56 insertions(+), 232 deletions(-)
mm/memcontrol-v1.c +6 -3
@@ -122,10 +122,13 @@ static unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)if (swap) val += total_swap_pages -get_nr_swap_pages (); }else { -if (!swap) +if (!swap) { val =page_counter_read (&memcg->memory); -else - val =page_counter_read (&memcg->memsw); + }else { +/* Preserve the user-visible memory <= memsw invariant. */ + val =max (page_counter_read (&memcg->memory), +page_counter_read (&memcg->memsw)); + } }return val; }
mm/memcontrol.c +50 -229
417 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
PATCH v6 5/5 mm/memcontrol: add stock to the memsw page counter
In reply to Joshua HahnAttach legacy memsw page counters to their own seven-slot per-CPU stock. Charge, refill, drain, and hotplug handling now operate on the memory and memsw banks independently.
Factor the common drain scheduling into schedule_stock_drain() now that both stocks use it.
Keep the existing direct memsw rollback when the memory charge fails, ensuring that failed allocations do not replenish the newly attached memsw stock.
The separate banks can hit, contend, evict, and drain independently, so their raw counters can temporarily drift by their cached amounts. The previous patch preserves the user-visible cgroup-v1 invariant by reporting memory.memsw.usage_in_bytes as the larger raw value.
--- mm/memcontrol.c | 62 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 16 deletions(-)
mm/memcontrol.c +46 -16
@@ -2056,6 +2056,13 @@ static DEFINE_PER_CPU_ALIGNED(struct page_counter_stock_pcp, memory_stock) = { .base = &memory_stock, }; +#ifdef CONFIG_MEMCG_V1 +static DEFINE_PER_CPU_ALIGNED(struct page_counter_stock_pcp, memsw_stock) = { + .lock =INIT_LOCAL_TRYLOCK (lock), + .base = &memsw_stock, +}; +#endif +/* * NR_OBJ_STOCK is sized so the entire hot path of obj_stock_pcp * (lock, accounting metadata, nr_bytes[] and cached[]) fits within a
@@ -2160,12 +2167,30 @@ static bool schedule_drain_work(int cpu, struct work_struct *work)return true; } +static void schedule_stock_drain (struct page_counter_stock_pcp __percpu *stock, +struct cgroup_subsys_state *root_css, +int cpu,int curcpu) +{ +struct page_counter_stock_pcp *pcp_stock =per_cpu_ptr (stock, cpu); + +if (test_bit (FLUSHING_CACHED_CHARGE , &pcp_stock->flags) || + !page_counter_stock_flush_required (pcp_stock, root_css) || +test_and_set_bit (FLUSHING_CACHED_CHARGE , &pcp_stock->flags)) +return ; + +if (cpu == curcpu) +drain_local_stock (&pcp_stock->work); +else if (!schedule_drain_work (cpu, &pcp_stock->work)) +clear_bit (FLUSHING_CACHED_CHARGE , &pcp_stock->flags); +} +/* * Drains all per-CPU charge caches for given root_memcg resp. subtree * of the hierarchy under it. */ void drain_all_stock (struct mem_cgroup *root_memcg) { +struct cgroup_subsys_state *root_css = &root_memcg->css;int cpu, curcpu;/* If someone's already draining, avoid adding running more workers. */
@@ -2180,21 +2205,13 @@ void drain_all_stock(struct mem_cgroup *root_memcg)migrate_disable (); curcpu =smp_processor_id ();for_each_online_cpu (cpu) { -struct page_counter_stock_pcp *memory_st = -per_cpu_ptr (&memory_stock, cpu);struct obj_stock_pcp *obj_st = &per_cpu (obj_stock, cpu); -if (!test_bit (FLUSHING_CACHED_CHARGE , &memory_st->flags) && -page_counter_stock_flush_required (memory_st, - &root_memcg->css) && - !test_and_set_bit (FLUSHING_CACHED_CHARGE , - &memory_st->flags)) { -if (cpu == curcpu) -drain_local_stock (&memory_st->work); -else if (!schedule_drain_work (cpu, &memory_st->work)) -clear_bit (FLUSHING_CACHED_CHARGE , - &memory_st->flags); - } +schedule_stock_drain (&memory_stock, root_css, cpu, curcpu); +#ifdef CONFIG_MEMCG_V1 +if (do_memsw_account ()) +schedule_stock_drain (&memsw_stock, root_css, cpu, curcpu); +#endif if (!test_bit(FLUSHING_CACHED_CHARGE, &obj_st->flags) && obj_stock_flush_required(obj_st, root_memcg) &&
@@ -2221,6 +2238,11 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu) stock =per_cpu_ptr (&memory_stock, cpu);page_counter_drain_stock_fully (stock);clear_bit (FLUSHING_CACHED_CHARGE , &stock->flags); +#ifdef CONFIG_MEMCG_V1 + stock =per_cpu_ptr (&memsw_stock, cpu); +page_counter_drain_stock_fully (stock); +clear_bit (FLUSHING_CACHED_CHARGE , &stock->flags); +#endif /* * A drain work queued before the CPU went away is executed by an
@@ -2524,8 +2546,8 @@ 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, false, -NULL )) { + !page_counter_try_charge (&memcg->memsw, nr_pages, &counter, + may_batch,NULL )) { mem_over_limit =mem_cgroup_from_counter (counter, memsw); reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP ; goto reclaim;
@@ -3004,7 +3026,7 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,if (!mem_cgroup_is_root (memcg)) {page_counter_refill_stock (&memcg->memory, nr_pages);if (do_memsw_account ()) -page_counter_uncharge (&memcg->memsw, nr_pages); +page_counter_refill_stock (&memcg->memsw, nr_pages); }css_put (&memcg->css);
@@ -4108,6 +4130,10 @@ mem_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) memcg->memory.stock_css = &memcg->css;page_counter_init (&memcg->swap, &parent->swap, false);#ifdef CONFIG_MEMCG_V1 +if (!memcg_on_dfl) { + memcg->memsw.stock = &memsw_stock; + memcg->memsw.stock_css = &memcg->css; + }WRITE_ONCE (memcg->swappiness,mem_cgroup_swappiness (parent)); memcg->memory.track_failcnt = !memcg_on_dfl; memcg->memsw.track_failcnt = !memcg_on_dfl;
@@ -5735,6 +5761,10 @@ int __init mem_cgroup_init(void)for_each_possible_cpu (cpu) {INIT_WORK (&per_cpu_ptr (&memory_stock, cpu)->work, drain_local_stock); +#ifdef CONFIG_MEMCG_V1 +INIT_WORK (&per_cpu_ptr (&memsw_stock, cpu)->work, + drain_local_stock); +#endif INIT_WORK (&per_cpu_ptr (&obj_stock, cpu)->work, drain_local_obj_stock); }
Signature · 1 line
2.53.0-Meta
Quoted 22 lines · On Wed, 16 Sep 2026 14:05:46 -0700 Joshua Hahn <[email protected]> wrote:
v5 --> v6
=========
Following feedback that v5 combined the (1) stock abstraction move from memcg topage_counterand (2) changing the allocation / draining behavior, v6 limits itself to only the first goal. It retains the existing seven-slot per-CPU design and drain policy.INTRODUCTION
============
Memcg keeps a per-CPU stock of precharged pages so that small, frequent allocations do not walk thepage_counterhierarchy every time. Today, the stock implementation is within memcontrol code, even though the operation it caches is apage_countercharge. This makes it difficult to add newpage_countersto a memcg and preserve the fast path behavior.This matters for future work like my tiered memcg limits series [1] which introduces multiple new
page_countersto memcg. Without making stock a page_counter-level property, it means that every memcg charge now goes through multiplepage_counterhierarchy walks, instead of being able to cache these charges.
Quoted 7 lines
To make future
page_countersscalable and performant, move stock frommem_cgrouptopage_counterso that eachpage_countercan opt into its own per-CPU cache of pre-charged pages.We get an added benefit of simplifying
try_charge_memcgcode, which now has all the stock management handled transparently within thepage_counterlayer.
Sashiko raised one bug for the series:
@@ -192,11 +245,20 @@ bool page_counter_try_charge(struct page_counter *counter,
WRITE_ONCE(c->watermark, new);
}
}
+ if (charge > nr_pages)
+ page_counter_refill_stock(counter, charge - nr_pages);
+ if (nr_charged)
+ *nr_charged = charge;
return true;
failed:
And asked: Does this unconditionally report the batched size to the caller even if the excess was rejected by the stock and uncharged from the hierarchy?
---
This is true, but this is already the behavior for vanilla memcg. In this series I'm hoping to preserve all existing semantics without changing behaviors, so I can fix this problem in a separate issue.
Specifically, in vanilla try_charge_memcg:
done_restock:
if (batch > nr_pages) refill_stock(memcg, batch - nr_pages);
...
current->memcg_nr_pages_over_high += batch;
So I've just preserved the exact semantics that we used to have before.
The problem isn't that big anyways though, it's a transient inflation in memcg_over_high and will be wiped on the next high handling run, and there is no effect on accounting or permanent inflations.
So I think this issue is pre-existing and a minor transient inflation for memcg_over_high at best. If this looks problematic I can write an orthogonal fix separately.
Thanks anyways, Sashiko!
Joshua
Michal Koutný
maintainerHi.
Quoted 7 lines · Joshua Hahn
This is true, but this is already the behavior for vanilla memcg. In this series I'm hoping to preserve all existing semantics without changing behaviors, so I can fix this problem in a separate issue.
Specifically, in vanilla
try_charge_memcg:
Quoted 8 lines
done_restock:if (batch > nr_pages) refill_stock(memcg, batch - nr_pages);...
current->memcg_nr_pages_over_high += batch;So I've just preserved the exact semantics that we used to have before.
Kudos to you for the conservative approach.
Quoted 4 lines
The problem isn't that big anyways though, it's a transient inflation in
memcg_over_highand will be wiped on the next high handling run, and there is no effect on accounting or permanent inflations.
It reminds me [1] where stockage imprecision could even trigger OOM but it was reportedly only visible in LTP.
HTH,
Michal
Quoted 14 lines · On Fri, 18 Sep 2026 09:58:17 +0200 Michal Koutný <[email protected]> wrote:
Hi.
Quoted 11 lines · Joshua Hahn
This is true, but this is already the behavior for vanilla memcg. In this series I'm hoping to preserve all existing semantics without changing behaviors, so I can fix this problem in a separate issue.
Specifically, in vanilla
try_charge_memcg:
done_restock:if (batch > nr_pages) refill_stock(memcg, batch - nr_pages);
Quoted 6 lines
Quoted 4 lines
...
current->memcg_nr_pages_over_high += batch;So I've just preserved the exact semantics that we used to have before.
Kudos to you for the conservative approach.
Hi Michal, thanks for your kind words : -)
Quoted 7 lines
Quoted 4 lines
The problem isn't that big anyways though, it's a transient inflation in
memcg_over_highand will be wiped on the next high handling run, and there is no effect on accounting or permanent inflations.It reminds me [1] where stockage imprecision could even trigger OOM but it was reportedly only visible in LTP.
So I was thinking about this part quite a bit in the older versions of this series where I changed the draining from being async to sync.
If I understand the issue you saw in LTP correctly, it's not one of actual memory OOMs but that there was stock that the memcg could use, just not in the current CPU that was trying to fulfill the charge, and also the draining for the remote CPUs did not happen in the time that it took to run through 16 retry attempts.
IOW seems to me we are synchronously waiting for asynchronous drains.
To me this did sound a little bit unfortunate since there really is memory that the memcg could have used, just cached in the "wrong" place (and for some reason that CPU is too busy to do the drain). One of the versions that I worked on previously (v4, [2]) used an atomic for the stock so that we just always do a synchronous drain via cmpxchg and we wouldn't have the problem above.
Obviously there are pros and cons. Synchronous means the reclaim path has the ability to reclaim more memory NOW and prevent futile reclaim retries, but also it makes each flushing operation more expensive as the cmpxchg operation is more expensive than acquiring a local trylock, even if it succeeds on the first try.
In my opinion just doing the synchronous reclamation makes a bit more sense to me since it gives a stronger guarantee that we are going to get the charges we need to fulfill the charge attempt NOW rather than spinning until some remote CPU gets scheduled to perform a local flush.
But this is just personal taste, I haven't really seen too much evidence that we are wasting too much time on the reclaims. So maybe asynchronous drains are OK here. I can do some more digging in our production data to see if we're ever just waiting for remote drains to finish.
Anyways, I still intend on separating out the two efforts so that this one is just a simple code move (and make stock more scalable) for cgroup v2 users (and I hope not-so-big impact for cgroup v1 users) and I can work on removing the 7-slot limit and improving the draining as a follow-up work.
Quoted 2 lines
HTH,
Michal
Thanks for your thoughts Michal. Have a great day! Joshua