mm/memcontrol, page_counter: move stock from mem_cgroup to page_counter
Subsystems (4)
v3 cover mm/memcontrol, page_counter: move stock from mem_cgroup to page_counter
Memcg currently keeps a "stock" of 64 pages per-cpu to cache pre-charged allocations, allowing small allocations to avoid walking the expensive mem_cgroup hierarchy traversal and atomic operations on each charge. This design introduces a fastpath, but there is room for improvement:
1. Currently, each CPU tracks up to 7 (NR_MEMCG_STOCK) mem_cgroups. When more than 7 mem_cgroups are actively charging on a single CPU, a random victim is evicted and its associated stock is drained.
2. Stock management is tightly coupled to struct mem_cgroup, which makes it difficult to add a new page_counter to mem_cgroup and have multiple sources of stock management, which is required when trying to introduce fastpaths to multiple hard limit checks.
This series moves the per-cpu stock down into the page_counter which consolidates stock limit checking and page_counter limit checking into page_counter_try_charge. This eliminates the 7-memcg-per-cpu slot limit, the random evictions (drain & refill), and slot traversal.
In turn, we can add independent stock management for additional page_counters in each memcg, which is used in my tiered memory limits series to add a new page_counter to track toptier usage [1].
The resulting code in memcg is also easier to follow, as the caching becomes transparent from memcg's perspective and managed entirely within page_counter.
There are, however, a few tradeoffs.
First, the bound on how much memory can be overcharged (and remain stale as stock) is raised. Previously, it was fixed to nr_cpus x 7 x 64 pages. Now, it becomes nr_leaf_cgroups x nr_cpus x 64 pages. On large machines with many cgroups, this could be significant. There are three qualifying
points: (1) larger machines should be able to tolerate the additional overhead, (2) the stock should not remain stale as long as the cgroups are actively charging memory, and (3) a process would have to migrate across all CPUs to incur this upper bound on overhead.
Secondly, we introduce some additional memory footprint. The new struct page_counter_stock adds 2 words of extra overhead per-(cpu x memcg).
A small change is that for cgroupv1, reported memsw usage can be lower than reported memory usage, if the memsw page_counter overcharges to its stock whereas the memory page_counter does not.
Finally, to keep the above memory footprint limited, I opted to not embed a work_struct into page_counter_stock, but rather decided to trigger synchronous stock draining, since the drain operation is rarer now, and only happens under memory pressure and on cgroup death.
Performance testing across single-cgroup, as well as 4-cgroup (under the 7 memcg limit) and 32-cgroup scenarios on a 40CPU, 50G memory system shows negligible performance differences. In the tests, I repeatedly fault and release anonymous pages using madvise(MADV_DONTNEED) to stress the charge/uncharge path, across 40 trials of 50 iterations. Metric here is time it took across each iteration (ms).
There are two testing versions below; the only difference is that v3 is based on top of mm-new, and v2 is based on top of mm-stable. The "after" on both sides are similar, but mm-new and mm-stable have different perforamnces.
v3, tested against mm-new
+----------+--------+-------+-----------+ | #cgroups | mm-new | after | delta (%) | +----------+--------+-------+-----------+ | 1 | 357 | 358 | +0.283 | | 4 | 1245 | 1214 | -2.430 | | 32 | 9281 | 8970 | -3.470 | +----------+--------+-------+-----------+
v2, tested against mm-stable
+----------+-----------+-------+-----------+ | #cgroups | mm-stable | after | delta (%) | +----------+-----------+-------+-----------+ | 1 | 352 | 353 | +0.283 | | 4 | 1198 | 1217 | +1.585 | | 32 | 8980 | 9027 | +0.526 | +----------+-----------+-------+-----------+
Further testing on other stress-ng microbenchmarks also agreed with these results.
v2 --> v3:
- Rebased on top of latest mm-new, May 25, 2026, since the previous version could not be applied for Sashiko review.
- Re-ran test numbers
v1 --> v2:
- Dropped stock returning on uncharge to preserve same behavior as memcg stock. This resolves some race conditions present in v1.
- Fixed many race conditions between disabling page_counter_stock and in-flight charges
- Restructured drain_all_stock to iterate over all CPUs first before memcgs, to reduce the number of synchronous CPU work scheduling
- Optimized cgroup v2 further to drain only on the first child and skip the root mem_cgroup
- Dropped RFC
- Wordsmithing cover letter
[1] https://lore.kernel.org/all/[email protected]/
Joshua Hahn (7):
mm/page_counter: introduce per-page_counter stock mm/page_counter: use page_counter_stock in page_counter_try_charge mm/page_counter: introduce stock drain APIs mm/memcontrol: convert memcg to use page_counter_stock mm/memcontrol: optimize memsw stock for cgroup v1 mm/memcontrol: optimize stock usage for cgroup v2 mm/memcontrol: remove unused memcg_stock code
include/linux/page_counter.h | 16 ++ mm/memcontrol.c | 289 +++++++---------------------------- mm/page_counter.c | 140 ++++++++++++++++- 3 files changed, 212 insertions(+), 233 deletions(-)
Signature · 1 line
2.53.0-Meta
PATCH v3 1/7 mm/page_counter: introduce per-page_counter stock
In order to avoid expensive hierarchy walks on every memcg charge and limit check, memcontrol uses per-cpu stocks (memcg_stock_pcp) to cache pre-charged pages and introduce a fast path to try_charge_memcg.
However, there are a few quirks with the current implementation that could be improved upon.
First, each memcg_stock_pcp can only cache the charges of 7 memcgs (defined as NR_MEMCG_STOCK), which means that once a CPU starts handling the charging of more than 7 memcgs, it randomly selects a victim memcg to evict and drain from the cpu, which can cause unnecessarily increased latencies and thrashing as memcgs continually evict each others' stock.
Second, stock is tightly coupled with memcg, which means that all page counters in a memcg share the same resource. This may simplify some of the charging logic, but it prevents new page counters from being added and using a separate stock.
We can address these concerns by pushing the concept of stock down to the page_counter level, which addresses the random eviction problem by getting rid of the 7 slot limit, and makes enabling separate stock caches for other page_counters simpler.
Introduce a generic per-cpu stock directly in struct page_counter. Stock can optionally be enabled per-page_counter, limiting the overhead increase for page_counters who do not benefit greatly from caching charges.
This patch introduces the page_counter_stock struct and its enable/disable/free functions, but does not use these yet.
--- include/linux/page_counter.h | 13 ++++++++ mm/page_counter.c | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+)
include/linux/page_counter.h +13 -0
@@ -5,8 +5,15 @@#include <linux/atomic.h> #include <linux/cache.h> #include <linux/limits.h> +#include <linux/local_lock.h> +#include <linux/percpu.h> #include <asm/page.h> +struct page_counter_stock { +local_trylock_t lock; +unsigned long nr_pages; +}; +struct page_counter {/* * Make sure 'usage' does not share cacheline with any other field in
@@ -41,6 +48,8 @@ struct page_counter {unsigned long high;unsigned long max;struct page_counter *parent; +struct page_counter_stock __percpu *stock; +unsigned int batch; }____cacheline_internodealigned_in_smp ;#if BITS_PER_LONG ==32
@@ -99,6 +108,10 @@ static inline void page_counter_reset_watermark(struct page_counter *counter) counter->watermark = usage; } +int page_counter_enable_stock (struct page_counter *counter,unsigned int batch); +void page_counter_disable_stock (struct page_counter *counter); +void page_counter_free_stock (struct page_counter *counter); +#if IS_ENABLED (CONFIG_MEMCG ) ||IS_ENABLED (CONFIG_CGROUP_DMEM ) void page_counter_calculate_protection(struct page_counter *root, struct page_counter *counter,
mm/page_counter.c +64 -0
76 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Re: [PATCH 1/7 v3] mm/page_counter: introduce per-page_counter stock
Quoted 135 lines · On Mon, 25 May 2026 12:04:48 -0700 Joshua Hahn <[email protected]> wrote:
In order to avoid expensive hierarchy walks on every memcg charge and limit check, memcontrol uses per-cpu stocks (
memcg_stock_pcp) to cache pre-charged pages and introduce a fast path totry_charge_memcg.However, there are a few quirks with the current implementation that could be improved upon.
First, each
memcg_stock_pcpcan only cache the charges of 7 memcgs (defined asNR_MEMCG_STOCK), which means that once a CPU starts handling the charging of more than 7 memcgs, it randomly selects a victim memcg to evict and drain from the cpu, which can cause unnecessarily increased latencies and thrashing as memcgs continually evict each others' stock.Second, stock is tightly coupled with memcg, which means that all page counters in a memcg share the same resource. This may simplify some of the charging logic, but it prevents new page counters from being added and using a separate stock.
We can address these concerns by pushing the concept of stock down to the
page_counterlevel, which addresses the random eviction problem by getting rid of the 7 slot limit, and makes enabling separate stock caches for otherpage_counterssimpler.Introduce a generic per-cpu stock directly in struct
page_counter. Stock can optionally be enabled per-page_counter, limiting the overhead increase forpage_counterswho do not benefit greatly from caching charges.This patch introduces the
page_counter_stockstruct and its enable/disable/free functions, but does not use these yet.Suggested-by Johannes Weiner <[email protected]>Signed-off-by Joshua Hahn <[email protected]>--- include/linux/page_counter.h | 13 ++++++++ mm/page_counter.c | 64 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+)include/linux/page_counter.h +13
-0@@ -5,8 +5,15 @@#include <linux/atomic.h> #include <linux/cache.h> #include <linux/limits.h> +#include <linux/local_lock.h> +#include <linux/percpu.h> #include <asm/page.h> +struct page_counter_stock { +local_trylock_t lock; +unsigned long nr_pages; +}; +struct page_counter {/* * Make sure 'usage' does not share cacheline with any other field in @@ -41,6 +48,8 @@ struct page_counter {unsigned long high;unsigned long max;struct page_counter *parent; +struct page_counter_stock __percpu *stock; +unsigned int batch; }____cacheline_internodealigned_in_smp ;#if BITS_PER_LONG ==32 @@ -99,6 +108,10 @@ static inline void page_counter_reset_watermark(struct page_counter *counter) counter->watermark = usage; } +int page_counter_enable_stock (struct page_counter *counter,unsigned int batch); +void page_counter_disable_stock (struct page_counter *counter); +void page_counter_free_stock (struct page_counter *counter); +#if IS_ENABLED (CONFIG_MEMCG ) ||IS_ENABLED (CONFIG_CGROUP_DMEM ) void page_counter_calculate_protection(struct page_counter *root, struct page_counter *counter,mm/page_counter.c +42
-0@@ -8,6 +8,7 @@#include <linux/page_counter.h> #include <linux/atomic.h> #include <linux/kernel.h> +#include <linux/percpu.h> #include <linux/string.h> #include <linux/sched.h> #include <linux/bug.h> @@ -289,6 +290,69 @@ int page_counter_memparse(const char *buf, const char *max,return 0 ; } +int page_counter_enable_stock (struct page_counter *counter,unsigned int batch) +{ +struct page_counter_stock __percpu *stock; +int cpu; + + stock = alloc_percpu(struct page_counter_stock); +if (!stock) +return -ENOMEM ; + +for_each_possible_cpu (cpu) { +struct page_counter_stock *s =per_cpu_ptr (stock, cpu); + +local_trylock_init (&s->lock); + } + counter->stock = stock; + counter->batch = batch; + +return 0 ; +} + +static void page_counter_drain_stock_nolock (struct page_counter *counter) +{ +unsigned long stock_to_drain =0 ; +int cpu; + +for_each_possible_cpu (cpu) { +struct page_counter_stock *stock; + + stock =per_cpu_ptr (counter->stock, cpu); + stock_to_drain += stock->nr_pages; + stock->nr_pages =0 ; + } + +if (stock_to_drain) +page_counter_uncharge (counter, stock_to_drain); +} + +void page_counter_disable_stock (struct page_counter *counter) +{ +if (!counter->stock) +return ;
mm/page_counter.c int page_counter_memparse(const char *buf, const char *max, · line 343
Quoted 10 lines
mm/page_counter.c +10
-0+ +/* This prevents future charges from trying to deposit pages */ +WRITE_ONCE (counter->batch,0 ); + +/* +* Charges can still be in-flight at this time. Instead of locking here, +* do the majority of the drains here without locking to free up pages +* now. Any remaining stock will be drained in page_counter_free_stock. +*/ +page_counter_drain_stock_nolock (counter);
Sashiko raised a concern here.
Allowing racy charges is OK, but the problem is that writing stock->nr_pages = 0 with no lock here can race with the reading of that value, and lead to double-uncharging when racing with concurrent charges.
I think that this can be fixed by not draining in disable_stock and reordering the callsite:
Before:drain_all_stock(memcg);page_counter_disable_stock(&memcg->memory);
After:page_counter_disable_stock(&memcg->memory);drain_all_stock(memcg);
This way, the WRITE_ONCE(counter->batch, 0); should prevent any future charges from trying to land, before we drain stock.
Despite not allowing any more racy charges, we still need to keep the drain in free_stock since drain_all_stock() uses a mutex_trylock and can fail; if that happens we need to still drain the stock at a later time, when we can guarantee that there will be no more contention.
Thanks, Sashiko! I'll keep my eyes posted on the rest of the series as it goes through the review pipeline.
Joshua
Quoted 12 lines
mm/page_counter.c +12
-0+} + +void page_counter_free_stock (struct page_counter *counter) +{ +if (!counter->stock) +return ; + +page_counter_drain_stock_nolock (counter); +free_percpu (counter->stock); + counter->stock =NULL ; +} +
PATCH v3 2/7 mm/page_counter: use page_counter_stock in page_counter_try_charge
In reply to Joshua HahnMake page_counter_try_charge() stock-aware. We preserve the same semantics as the existing stock handling logic in try_charge_memcg:
1. Limit-check against the stock. If there is enough, charge to the stock (non-hierarchical) and return immediately.
2. Greedily attempt to fulfill the charge request and fill the stock up at the same time via a hierarchical charge.
3. If we fail with this charge, retry again (once) with the exact number of pages requested.
4. If we succeed with the greedy attempt, then try to add those extra pages to the stock. If that fails (trylock), then uncharge those surplus pages hierarchically.
As of this patch, the page_counter_stock is unused, as it has not been enabled on any memcg yet. No functional changes intended.
--- mm/page_counter.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
mm/page_counter.c +39 -3
@@ -121,9 +121,25 @@ bool page_counter_try_charge(struct page_counter *counter, struct page_counter **fail) {struct page_counter *c; +unsigned long charge = nr_pages; +unsigned long batch =READ_ONCE (counter->batch);bool protection =track_protection (counter);bool track_failcnt = counter->track_failcnt; +if (counter->stock &&local_trylock (&counter->stock->lock)) { +struct page_counter_stock *stock =this_cpu_ptr (counter->stock); + +if (stock->nr_pages >= charge) { + stock->nr_pages -= charge; +local_unlock (&counter->stock->lock); +return true; + } +local_unlock (&counter->stock->lock); + } + + charge = max_t(unsigned long, batch, nr_pages); + +retry:for (c = counter; c; c = c->parent) {long new;/*
@@ -140,9 +156,9 @@ bool page_counter_try_charge(struct page_counter *counter,* we either see the new limit or the setter sees the * counter has changed and retries. */ - new =atomic_long_add_return (nr_pages, &c->usage); + new =atomic_long_add_return (charge, &c->usage);if (new > c->max) { -atomic_long_sub (nr_pages, &c->usage); +atomic_long_sub (charge, &c->usage);/* * This is racy, but we can live with some * inaccuracy in the failcnt which is only used
@@ -163,11 +179,31 @@ bool page_counter_try_charge(struct page_counter *counter,WRITE_ONCE (c->watermark, new); } } + +/* charge > nr_pages implies this page_counter has stock enabled */ +if (charge > nr_pages) { +if (local_trylock (&counter->stock->lock)) { +struct page_counter_stock *stock; + + stock =this_cpu_ptr (counter->stock); + stock->nr_pages += charge - nr_pages; +local_unlock (&counter->stock->lock); + }else { +page_counter_uncharge (counter, charge - nr_pages); + } + } +return true; failed:for (c = counter; c != *fail; c = c->parent) -page_counter_cancel (c, nr_pages); +page_counter_cancel (c, charge); + +if (charge > nr_pages) { +/* Retry without trying to grab extra pages to refill stock */ + charge = nr_pages; + goto retry; + }return false; }
Signature · 1 line
2.53.0-Meta
PATCH v3 3/7 mm/page_counter: introduce stock drain APIs
In reply to Joshua HahnIntroduce page_counter variants to replace memcg stock draining functions.
page_counter_drain_stock_local() drains the stock of the local CPU, taking a local stock lock to serialize against concurrent charges.
page_counter_drain_stock_cpu() does the same, but without taking a local lock. This is possible because it will only be called from the CPU hotplug path, where the CPU is dead and there cannot be any more charges.
--- include/linux/page_counter.h | 3 +++ mm/page_counter.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+)
include/linux/page_counter.h +3 -0
@@ -111,6 +111,9 @@ static inline void page_counter_reset_watermark(struct page_counter *counter)int page_counter_enable_stock (struct page_counter *counter,unsigned int batch);void page_counter_disable_stock (struct page_counter *counter);void page_counter_free_stock (struct page_counter *counter); +void page_counter_drain_stock_local (struct page_counter *counter); +void page_counter_drain_stock_cpu (struct page_counter *counter, +unsigned int cpu);#if IS_ENABLED (CONFIG_MEMCG ) ||IS_ENABLED (CONFIG_CGROUP_DMEM ) void page_counter_calculate_protection(struct page_counter *root,
mm/page_counter.c +34 -0
@@ -389,6 +389,40 @@ void page_counter_free_stock(struct page_counter *counter) counter->stock =NULL ; } +void page_counter_drain_stock_local (struct page_counter *counter) +{ +struct page_counter_stock *stock; +unsigned long nr_pages; + +if (!counter->stock) +return ; + +local_lock (&counter->stock->lock); + stock =this_cpu_ptr (counter->stock); + nr_pages = stock->nr_pages; + stock->nr_pages =0 ; +local_unlock (&counter->stock->lock); + +if (nr_pages) +page_counter_uncharge (counter, nr_pages); +} + +void page_counter_drain_stock_cpu (struct page_counter *counter, +unsigned int cpu) +{ +struct page_counter_stock *stock; +unsigned long nr_pages; + +if (!counter->stock) +return ; + + stock =per_cpu_ptr (counter->stock, cpu); + nr_pages = stock->nr_pages; +if (nr_pages) { + stock->nr_pages =0 ; +page_counter_uncharge (counter, nr_pages); + } +}#if IS_ENABLED (CONFIG_MEMCG ) ||IS_ENABLED (CONFIG_CGROUP_DMEM )/*
Signature · 1 line
2.53.0-Meta
PATCH v3 4/7 mm/memcontrol: convert memcg to use page_counter_stock
In reply to Joshua HahnNow with all of the memcg_stock handling logic replicated in page_counter_stock, switch memcg to use the page_counter_stock.
There are a few details that have changed:
First, the old special-casing for the !allow_spinning check to avoid refilling and flushing of the old stock is removed. This special casing was important previously, because refilling the stock could do a lot of extra work by evicting one of 7 random victim memcgs in the percpu memcg_stock slots. In the new per-counter design, refilling stock just adds pages to the counter's own local cache without affecting other memcgs, so the original reason for the special case no longer applies.
Also, we can now fail during page_counter_enable_stock(), if there is not enough memory to allocate a percpu page_counter_stock. This failure is rare and nonfatal; the system can continue to operate, with the page counter working without stock and falling back to walking the hierarchy.
Finally, drain_all_stock is restructured to iterate CPUs in the outer loop (rather than memcgs) to be able to schedule draining all memcgs via a single work_on_cpu call. It reduces the number of synchronous per-CPU work calls from O(memcgs * CPUs) to just O(CPUs).
Note that obj_stock remains untouched by these changes.
--- mm/memcontrol.c | 78 +++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 44 deletions(-)
mm/memcontrol.c +34 -44
@@ -2260,6 +2260,17 @@ static void schedule_drain_work(int cpu, struct work_struct *work)queue_work_on (cpu, memcg_wq, work); } +static long drain_stock_on_cpu (void *arg) +{ +struct mem_cgroup *root_memcg = arg; +struct mem_cgroup *memcg; + +for_each_mem_cgroup_tree (memcg, root_memcg) +page_counter_drain_stock_local (&memcg->memory); + +return 0 ; +} +/* * Drains all per-CPU charge caches for given root_memcg resp. subtree * of the hierarchy under it.
@@ -2271,28 +2282,16 @@ void drain_all_stock(struct mem_cgroup *root_memcg)/* If someone's already draining, avoid adding running more workers. */ if (!mutex_trylock (&percpu_charge_mutex))return ; -/* -* Notify other cpus that system-wide "drain" is running -* We do not care about races with the cpu hotplug because cpu down -* as well as workers from this path always operate on the local -* per-cpu data. CPU up doesn't touch memcg_stock at all. -*/ + +for_each_online_cpu (cpu) +work_on_cpu (cpu, drain_stock_on_cpu, root_memcg); + +/* Drain obj_stock on all online CPUs */ migrate_disable (); curcpu =smp_processor_id (); for_each_online_cpu(cpu) { - struct memcg_stock_pcp *memcg_st = &per_cpu(memcg_stock, cpu); struct obj_stock_pcp *obj_st = &per_cpu(obj_stock, cpu); - if (!test_bit(FLUSHING_CACHED_CHARGE, &memcg_st->flags) && - is_memcg_drain_needed(memcg_st, root_memcg) && - !test_and_set_bit(FLUSHING_CACHED_CHARGE, - &memcg_st->flags)) { - if (cpu == curcpu) - drain_local_memcg_stock(&memcg_st->work); - else - schedule_drain_work(cpu, &memcg_st->work); - } - if (!test_bit(FLUSHING_CACHED_CHARGE, &obj_st->flags) && obj_stock_flush_required(obj_st, root_memcg) && !test_and_set_bit(FLUSHING_CACHED_CHARGE,
@@ -2309,9 +2308,13 @@ void drain_all_stock(struct mem_cgroup *root_memcg)static int memcg_hotplug_cpu_dead (unsigned int cpu) { +struct mem_cgroup *memcg; +/* no need for the local lock */ drain_obj_stock (&per_cpu (obj_stock, cpu)); -drain_stock_fully (&per_cpu (memcg_stock, cpu)); + +for_each_mem_cgroup (memcg) +page_counter_drain_stock_cpu (&memcg->memory, cpu);return 0 ; }
@@ -2586,7 +2589,6 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)static int try_charge_memcg (struct mem_cgroup *memcg,gfp_t gfp_mask,unsigned int nr_pages) { -unsigned int batch =max (MEMCG_CHARGE_BATCH , nr_pages);int nr_retries =MAX_RECLAIM_RETRIES ;struct mem_cgroup *mem_over_limit;struct page_counter *counter;
@@ -2599,31 +2601,19 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,bool allow_spinning =gfpflags_allow_spinning (gfp_mask); retry: -if (consume_stock (memcg, nr_pages)) -return 0 ; - -if (!allow_spinning) -/* Avoid the refill and flush of the older stock */ - 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)) +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, batch); +page_counter_uncharge (&memcg->memsw, nr_pages); mem_over_limit =mem_cgroup_from_counter (counter, memory); }else { mem_over_limit =mem_cgroup_from_counter (counter, memsw); reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP ; } -if (batch > nr_pages) { - batch = nr_pages; - goto retry; - } -/* * Prevent unbounded recursion when reclaim operations need to * allocate memory. This might exceed the limits temporarily,
@@ -2720,9 +2710,6 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,return 0 ; done_restock: -if (batch > nr_pages) -refill_stock (memcg, batch - nr_pages); -/* * If the hierarchy is above the normal consumption range, schedule * reclaim on returning to userland. We can perform reclaim here
@@ -2759,7 +2746,7 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,* and distribute reclaim work and delay penalties * based on how much each task is actually allocating. */ - current->memcg_nr_pages_over_high += batch; + current->memcg_nr_pages_over_high += nr_pages;set_notify_resume (current);break ; }
@@ -3064,7 +3051,7 @@ static void obj_cgroup_uncharge_pages(struct obj_cgroup *objcg,account_kmem_nmi_safe (memcg, -nr_pages);memcg1_account_kmem (memcg, -nr_pages);if (!mem_cgroup_is_root (memcg)) -refill_stock (memcg, nr_pages); +memcg_uncharge (memcg, nr_pages);css_put (&memcg->css); }
@@ -4096,6 +4083,8 @@ static void __mem_cgroup_free(struct mem_cgroup *memcg)static void mem_cgroup_free (struct mem_cgroup *memcg) { +page_counter_free_stock (&memcg->memory); +page_counter_free_stock (&memcg->memsw);lru_gen_exit_memcg (memcg);memcg_wb_domain_exit (memcg);__mem_cgroup_free (memcg);
@@ -4268,6 +4257,9 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)refcount_set (&memcg->id.ref,1 );css_get (css); +/* failure is nonfatal, charges fall back to direct hierarchy */ +page_counter_enable_stock (&memcg->memory,MEMCG_CHARGE_BATCH ); +/* * Ensure mem_cgroup_from_private_id() works once we're fully online. *
@@ -4330,6 +4322,7 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)lru_gen_offline_memcg (memcg);drain_all_stock (memcg); +page_counter_disable_stock (&memcg->memory);mem_cgroup_private_id_put (memcg,1 ); }
@@ -5524,7 +5517,7 @@ void mem_cgroup_sk_uncharge(const struct sock *sk, unsigned int nr_pages)mod_memcg_state (memcg,MEMCG_SOCK , -nr_pages); -refill_stock (memcg, nr_pages); +page_counter_uncharge (&memcg->memory, nr_pages); }void mem_cgroup_flush_workqueue (void )
@@ -5577,12 +5570,9 @@ int __init mem_cgroup_init(void) memcg_wq =alloc_workqueue ("memcg" ,WQ_PERCPU ,0 );WARN_ON (!memcg_wq); -for_each_possible_cpu (cpu) { -INIT_WORK (&per_cpu_ptr (&memcg_stock, cpu)->work, - drain_local_memcg_stock); +for_each_possible_cpu (cpu)INIT_WORK (&per_cpu_ptr (&obj_stock, cpu)->work, drain_local_obj_stock); - } memcg_size = struct_size_t(struct mem_cgroup, nodeinfo, nr_node_ids); memcg_cachep = kmem_cache_create("mem_cgroup", memcg_size, 0,
Signature · 1 line
2.53.0-Meta
PATCH v3 5/7 mm/memcontrol: optimize memsw stock for cgroup v1
In reply to Joshua HahnPreviously, each memcg had its own stock, which was shared by all page counters within it. Specifically in try_charge_memcg, the stock limit check would occur before the memsw and memory page_counters were charged hierarchically. Now that the memcg stock was folded into the page_counter level, and we have replaced try_charge_memcg's stock check against the memory page_counter's stock, this leaves no fast path available for cgroup v1's memsw check.
Introduce a new stock for the memsw page_counter, charged independently from the memory page_counter. This provides better caching on cgroup v1:
The best case scenario is when both the memsw and memory page_counters can use their cached stock charge; this is the old behavior.
The halfway scenario is when either the memsw or memory page_counter is within the stock size, but the other isn't. This requires one hierarchical charge.
The worst case scenario is when both memsw and memory page_counters are over their limit, and must walk two page_counter hierarchies. This is the same as the old behavior.
By introducing an independent stock for memsw, we can avoid the worst case scenario more often and can fail or succeed separately from the memory page counter.
One user-visible change is that reported memsw usage may transiently be lower than memory usage. This happens because each counter independently batches the stock charges, so the visible values can differ by up to the stock batch size (MEMCG_CHARGE_BATCH) pages.
--- mm/memcontrol.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
mm/memcontrol.c +12 -2
@@ -2265,8 +2265,11 @@ static long drain_stock_on_cpu(void *arg)struct mem_cgroup *root_memcg = arg;struct mem_cgroup *memcg; -for_each_mem_cgroup_tree (memcg, root_memcg) +for_each_mem_cgroup_tree (memcg, root_memcg) {page_counter_drain_stock_local (&memcg->memory); +if (do_memsw_account ()) +page_counter_drain_stock_local (&memcg->memsw); + }return 0 ; }
@@ -2313,8 +2316,11 @@ static int memcg_hotplug_cpu_dead(unsigned int cpu)/* no need for the local lock */ drain_obj_stock (&per_cpu (obj_stock, cpu)); -for_each_mem_cgroup (memcg) +for_each_mem_cgroup (memcg) {page_counter_drain_stock_cpu (&memcg->memory, cpu); +if (do_memsw_account ()) +page_counter_drain_stock_cpu (&memcg->memsw, cpu); + }return 0 ; }
@@ -4259,6 +4265,8 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)/* failure is nonfatal, charges fall back to direct hierarchy */ page_counter_enable_stock (&memcg->memory,MEMCG_CHARGE_BATCH ); +if (do_memsw_account ()) +page_counter_enable_stock (&memcg->memsw,MEMCG_CHARGE_BATCH );/* * Ensure mem_cgroup_from_private_id() works once we're fully online.
@@ -4323,6 +4331,8 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)drain_all_stock (memcg);page_counter_disable_stock (&memcg->memory); +if (do_memsw_account ()) +page_counter_disable_stock (&memcg->memsw);mem_cgroup_private_id_put (memcg,1 ); }
Signature · 1 line
2.53.0-Meta
PATCH v3 6/7 mm/memcontrol: optimize stock usage for cgroup v2
In reply to Joshua HahnIn cgroup v2, it is unlikely for memcg charges to happen directly on non-leaf cgroups. There are a few exceptions, such as processes that have yet to be migrated to children, and tasks that are reparented on memcg destruction, that charge to the parent.
Because it is rare for parent cgroups to receive direct charges, stock that remains in them are wasted memory.
Drain parent stocks when the first child is created to return those pages for other memcgs to use.
This optimization is not for cgroup v1, where tasks can be attached to any cgroup in the hierarchy, meaning stock can be consumed & refilled for non-leaf cgroups as well.
--- mm/memcontrol.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
mm/memcontrol.c +15 -0
@@ -4280,6 +4280,21 @@ static int mem_cgroup_css_online(struct cgroup_subsys_state *css)*/ xa_store (&mem_cgroup_private_ids, memcg->id.id, memcg,GFP_KERNEL ); +/* +* It is unlikely for non-leaf memcgs to get direct charges on v2. +* Drain the parent's stock if we are the first child. +*/ +if (cgroup_subsys_on_dfl (memory_cgrp_subsys)) { +struct mem_cgroup *parent =parent_mem_cgroup (memcg); +int cpu; + +if (parent && !mem_cgroup_is_root (parent) && + !css_has_online_children (&parent->css)) { +for_each_online_cpu (cpu) +work_on_cpu (cpu, drain_stock_on_cpu, parent); + } + } +return 0 ; free_objcg:for_each_node (nid) {
Signature · 1 line
2.53.0-Meta
PATCH v3 7/7 mm/memcontrol: remove unused memcg_stock code
In reply to Joshua HahnNow that all memcg_stock logic has been moved to page_counter_stock, we can remove all code related to handling memcg_stock. Note that obj_stock is untouched and is still needed. FLUSHING_CACHED_CHARGE is preserved so that it can be used by obj_stock as well.
--- mm/memcontrol.c | 186 ------------------------------------------------ 1 file changed, 186 deletions(-)
mm/memcontrol.c +0 -186
@@ -1998,25 +1998,7 @@ void mem_cgroup_print_oom_group(struct mem_cgroup *memcg)pr_cont (" are going to be killed due to memory.oom.group set\n" ); } -/* -* The value of NR_MEMCG_STOCK is selected to keep the cached memcgs and their -* nr_pages in a single cacheline. This may change in future. -*/ -#define NR_MEMCG_STOCK 7#define FLUSHING_CACHED_CHARGE 0 -struct memcg_stock_pcp { -local_trylock_t lock; -uint8_t nr_pages[NR_MEMCG_STOCK ]; -struct mem_cgroup *cached[NR_MEMCG_STOCK ]; - -struct work_struct work; -unsigned long flags; -uint8_t drain_idx; -}; - -static DEFINE_PER_CPU_ALIGNED(struct memcg_stock_pcp, memcg_stock) = { - .lock =INIT_LOCAL_TRYLOCK (lock), -};/* * NR_OBJ_STOCK is sized so the entire hot path of obj_stock_pcp
@@ -2056,47 +2038,6 @@ static void drain_obj_stock(struct obj_stock_pcp *stock);static bool obj_stock_flush_required (struct obj_stock_pcp *stock,struct mem_cgroup *root_memcg); -/** -* consume_stock: Try to consume stocked charge on this cpu. -* @memcg: memcg to consume from. -* @nr_pages: how many pages to charge. -* -* Consume the cached charge if enough nr_pages are present otherwise return -* failure. Also return failure for charge request larger than -* MEMCG_CHARGE_BATCH or if the local lock is already taken. -* -* returns true if successful, false otherwise. -*/ -static bool consume_stock (struct mem_cgroup *memcg,unsigned int nr_pages) -{ -struct memcg_stock_pcp *stock; -uint8_t stock_pages; -bool ret = false; -int i; - -if (nr_pages >MEMCG_CHARGE_BATCH || - !local_trylock (&memcg_stock.lock)) -return ret; - - stock =this_cpu_ptr (&memcg_stock); - -for (i =0 ; i <NR_MEMCG_STOCK ; ++i) { -if (memcg !=READ_ONCE (stock->cached[i])) -continue ; - - stock_pages =READ_ONCE (stock->nr_pages[i]); -if (stock_pages >= nr_pages) { -WRITE_ONCE (stock->nr_pages[i], stock_pages - nr_pages); - ret = true; - } -break ; - } - -local_unlock (&memcg_stock.lock); - -return ret; -} -static void memcg_uncharge (struct mem_cgroup *memcg,unsigned int nr_pages) {page_counter_uncharge (&memcg->memory, nr_pages);
@@ -2104,51 +2045,6 @@ static void memcg_uncharge(struct mem_cgroup *memcg, unsigned int nr_pages)page_counter_uncharge (&memcg->memsw, nr_pages); } -/* -* Returns stocks cached in percpu and reset cached information. -*/ -static void drain_stock (struct memcg_stock_pcp *stock,int i) -{ -struct mem_cgroup *old =READ_ONCE (stock->cached[i]); -uint8_t stock_pages; - -if (!old) -return ; - - stock_pages =READ_ONCE (stock->nr_pages[i]); -if (stock_pages) { -memcg_uncharge (old, stock_pages); -WRITE_ONCE (stock->nr_pages[i],0 ); - } - -css_put (&old->css); -WRITE_ONCE (stock->cached[i],NULL ); -} - -static void drain_stock_fully (struct memcg_stock_pcp *stock) -{ -int i; - -for (i =0 ; i <NR_MEMCG_STOCK ; ++i) -drain_stock (stock, i); -} - -static void drain_local_memcg_stock (struct work_struct *dummy) -{ -struct memcg_stock_pcp *stock; - -if (WARN_ONCE (!in_task (),"drain in non-task context" )) -return ; - -local_lock (&memcg_stock.lock); - - stock =this_cpu_ptr (&memcg_stock); -drain_stock_fully (stock); -clear_bit (FLUSHING_CACHED_CHARGE , &stock->flags); - -local_unlock (&memcg_stock.lock); -} -static void drain_local_obj_stock (struct work_struct *dummy) {struct obj_stock_pcp *stock;
@@ -2165,88 +2061,6 @@ static void drain_local_obj_stock(struct work_struct *dummy)local_unlock (&obj_stock.lock); } -static void refill_stock (struct mem_cgroup *memcg,unsigned int nr_pages) -{ -struct memcg_stock_pcp *stock; -struct mem_cgroup *cached; -uint8_t stock_pages; -bool success = false; -int empty_slot =-1 ; -int i; - -/* -* For now limit MEMCG_CHARGE_BATCH to 127 and less. In future if we -* decide to increase it more than 127 then we will need more careful -* handling of nr_pages[] in struct memcg_stock_pcp. -*/ -BUILD_BUG_ON (MEMCG_CHARGE_BATCH >S8_MAX ); - -VM_WARN_ON_ONCE (mem_cgroup_is_root (memcg)); - -if (nr_pages >MEMCG_CHARGE_BATCH || - !local_trylock (&memcg_stock.lock)) { -/* -* In case of larger than batch refill or unlikely failure to -* lock the percpu memcg_stock.lock, uncharge memcg directly. -*/ -memcg_uncharge (memcg, nr_pages); -return ; - } - - stock =this_cpu_ptr (&memcg_stock); -for (i =0 ; i <NR_MEMCG_STOCK ; ++i) { - cached =READ_ONCE (stock->cached[i]); -if (!cached && empty_slot ==-1 ) - empty_slot = i; -if (memcg ==READ_ONCE (stock->cached[i])) { - stock_pages =READ_ONCE (stock->nr_pages[i]) + nr_pages; -WRITE_ONCE (stock->nr_pages[i], stock_pages); -if (stock_pages >MEMCG_CHARGE_BATCH ) -drain_stock (stock, i); - success = true; -break ; - } - } - -if (!success) { - i = empty_slot; -if (i ==-1 ) { - i = stock->drain_idx++; -if (stock->drain_idx ==NR_MEMCG_STOCK ) - stock->drain_idx =0 ; -drain_stock (stock, i); - } -css_get (&memcg->css); -WRITE_ONCE (stock->cached[i], memcg); -WRITE_ONCE (stock->nr_pages[i], nr_pages); - } - -local_unlock (&memcg_stock.lock); -} - -static bool is_memcg_drain_needed (struct memcg_stock_pcp *stock, -struct mem_cgroup *root_memcg) -{ -struct mem_cgroup *memcg; -bool flush = false; -int i; - -rcu_read_lock (); -for (i =0 ; i <NR_MEMCG_STOCK ; ++i) { - memcg =READ_ONCE (stock->cached[i]); -if (!memcg) -continue ; - -if (READ_ONCE (stock->nr_pages[i]) && -mem_cgroup_is_descendant (memcg, root_memcg)) { - flush = true; -break ; - } - } -rcu_read_unlock (); -return flush; -} -static void schedule_drain_work (int cpu,struct work_struct *work) {/*
Signature · 1 line
2.53.0-Meta
Quoted 4 lines · On Mon, 25 May 2026 12:04:47 -0700 Joshua Hahn <[email protected]> wrote:
Memcg currently keeps a "stock" of 64 pages per-cpu to cache pre-charged allocations, allowing small allocations to avoid walking the expensive
mem_cgrouphierarchy traversal and atomic operations on each charge. This design introduces a fastpath, but there is room for improvement:
Hello everyone,
Sashiko has left some great comments on the series, some of which are things I need to address for the next iteration, while others are false positives. There aren't a bunch, so I'll go over all of them below. Note that the same warning brought up in 1/7 is duplicated for the rest of the series; I've addressed my thoughts for that in [1].
Quoted 2 lines
Joshua Hahn (7):
mm/page_counter: introduce per-page_counter stock
Sashiko raised a concern about how zeroing per-cpu stock during the nolock drain could race with in-flight charges that read the value before it gets zeroed, leading to duplicate drains. I think this is solvable by just changing the order in the callsites (disable, then drain). More details can be found in [1].
Quoted 1 line
mm/page_counter: usepage_counter_stockinpage_counter_try_charge
Sashiko raises the same concern as [1].
Quoted 1 line
mm/page_counter: introduce stock drain APIs
Same concern as [1].
Quoted 1 line
mm/memcontrol: convert memcg to use
page_counter_stock
Sashiko raises 4 concerns, of which I think 3 of them are false positives (or not as serious as Sashiko makes it out to be).
(1) Sashiko asks whether the synchronous draining with the percpu_charge_mutex lock held could lead to more time spent holding the lock, which means more callers of drain_all_stock would fail the trylock and just skip draining.
To clarify, even in the original code, two tasks simultaneously calling drain_all_stock would serialize and only one of them would schedule the drain work, so this problem definitely existed before as well. It's just that the window for this race is a bit longer now.
I do think that there is actually a behavior change here (for the better). Previously, drain_all_stock had no guarantees on whether the stock was drained before retrying. Now, if the caller can get the trylock, they have a stronger guarantee that the stock is drained before retrying the drain.
On the note of premature OOMs, each retry loop takes much longer than the draining itself; I would imagine that by the time the next retry loop happens, there's a better chance that the trylock succeeds in the next iteration.
(2) Sashiko also raises another concern about a potential ABBA deadlock with the mmap_lock. I think this concern is not really true, the synchronous work being done (drain_stock_on_cpu) only takes a local lock. Hopefully I'm not missing anything here.
(3) I think Sashiko's concerns about NOHZ / CPU isolation is real. But it shouldn't be too bad, all I need is a cpu_is_isolated() check in the for_each_online_cpu iterator. Again, not draining a CPU is not fatal here, so it shouldn't be too big of a problem to skip some of them.
I also just wanted to note here explicitly that we don't need the migrate_disable() for the memcg stock drain, since we don't differentiate between local drain work & remote drain scheduling (like objcg_stock).
(4) Finally Sashiko asks if we should enable the memcg->memsw stock here. That's included in the very next patch : -) I separated them so that they can be reviewed separately, since they are separate ideas.
Quoted 1 line
mm/memcontrol: optimize memsw stock for cgroup v1
Both concerns here are addressed in the previous section.
Quoted 1 line
mm/memcontrol: optimize stock usage for cgroup v2
Sashiko raises 3 concerns, of which I think all of them are actually OK.
(1) If we drain the parent memcg stock on first child creation, then this would mean that there will be additional synchronous work being done with the cgroup_mutex lock held. I personally think this is fine, since it happens once per parent cgroup, and the draining work is pretty cheap. But I would appreciate it if other reviewers could chime in here.
(2) Sashiko also asks whether we need cpus_read_lock during the iteration. I think it's fine without it; if a CPU happens to go offline during the iteration, then that work will be scheduled on another CPU. That's fine, duplicate draining work on 1 CPU isn't the end of the world (and preferable to taking a cpus_read_lock here). As for the dying CPU, it will drain its own stock during the destruction path anyways, so no stock is lost.
(3) This one is not related to this series, so I'll move on.
Quoted 1 line
mm/memcontrol: remove unused
memcg_stockcode
No comments for this patch.
I think that's all the comments that Sashiko raised for this patch. Most of them had to do with performance tradeoffs, for which I hope that my testing results in the cover letter were able to instill some confidence that a lot of these tradeoffs aren't as bad as they seem. Regardless, I would really appreciate reviewer feedback on whether they think it is acceptable.
There are definitely some real bugs that I want to address, so a v4 will be incoming to address those (in a week or so).
Thank you Sashiko!
Joshua