[PATCH v9 0/7] mm/page_counter: move stock from mem_cgroup to page_counter
From: Joshua Hahn
Date: Fri Nov 13 2026 - 17:06:19 EST
v8 --> v9
=========
- Rebase onto mm-new as of 11/13/2026
- Fix draining issues raised by Sashiko for 7/7. Drop the lazy drain of
disabled stock, instead relying on slot ruse, drain_all_stock, or
CPU hotplug, just like any other stale stock.
INTRODUCTION
============
Memcg keeps a per-CPU stock of precharged pages so that small and
frequent allocations do not walk the page_counter hierarchy every time.
Today, the stock implementation lives in 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 series moves the stock into the page_counter layer.
page_counter_try_charge() consumes the stock, charges a batch to the
hierarchy on a miss, and refills the stock with the surplus. Memcg no
longer manages the stock in try_charge_memcg.
So the stock keeps the existing layout, which is still a per-CPU bank
of 7 slots, each holding a count of precharged pages that it hands out.
It maintains exclusion using local locks on each CPU, and for remote
drains we schedule on the memcg workqueue.
This change gives us the following benefits:
- Separate charge caching (page_counter) from charge policy (memcg).
- Makes it simple to keep stock out of interior cgroups on cgroup v2.
- Prepares for future work for stock improvement, which can now be
isolated from memcg changes.
With this change, make sure to preserve:
- All locking behavior is unchanged from before.
- Non-leaf cgroups do not hold stock. Specifically, their stock is
drained when they become a parent, and remaining stock (isolated CPUs,
concurrent refills, etc.) gets drained just like any stale stock.
- Exact charging for non-spinning callers.
- Stocks only exist for the memory and memsw page_counters as before.
Other page_counters (swap, v1 kmem, v1 tcpmem, hugetlb_cgroup, dmem)
don't stock as before.
EFFECT ON CGROUP V2 USERS
=========================
Currently when a cgroup becomes a parent, it doesn't drain the stock.
Rather, it just prevents new charges from accumulating by nature of
parent cgroups not usually having processes. We can do a little better.
When a cgroup becomes a parent, schedule drains right away, in addition
to making sure parents don't accumulate new charges.
Note that it was possible for interior cgroups to use stock before
(reparented objcgs, swapins, etc). We remove those to not strand stock
charge in interior cgroups.
Everything else is preserved. We preserve all draining, refilling,
and (un)charging behavior, including the uncharge path's refills
and direct uncharges.
EFFECT ON CGROUP V1 USERS
=========================
The memory and memsw counters now have separate stocks, which can hit,
evict, and drain independently. Their raw values can therefore drift
apart by the stocked amounts, even though every charge is applied to
both.
To preserve the user-visible property that
memory.usage_in_bytes <= memory.memsw.usage_in_bytes, the memory
value reports the smaller of the two values. Both raw values are already
upper bounds on real usage, so this stays within the fuzz that
usage_in_bytes always had (and this is also already documented, see
section 5.5 usage_in_bytes in Documentation/admin-guide/cgroup-v1/
memory.rst).
TESTING
=======
This series should not have big changes to performance, so I ran some
benchmarks to verify that. I did testing on a 16-CPU, 32G machine VM.
I ran repeated anonymous memory faults and MADV_DONTNEED to measure the
CPU time spent per page. So + number is worse. I ran each experiment
5 times, with 4 workers.
+-----------+-----------+--------------+-------+
| | base (ns) | patched (ns) | delta |
+-----------+-----------+--------------+-------+
| cgroup v1 | 890.6 | 901.8 | +1.2% |
| cgroup v2 | 893.5 | 902.2 | +1.0% |
+-----------+-----------+--------------+-------+
Joshua Hahn (7):
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: check PF_MEMALLOC after trying the limits
mm/memcontrol: move memory stock to page counters
mm/memcontrol: add stock to the memsw page counter
mm/memcontrol: restrict memory stock to leaf cgroups on v2
include/linux/page_counter.h | 35 ++-
kernel/cgroup/dmem.c | 2 +-
mm/hugetlb_cgroup.c | 2 +-
mm/memcontrol-v1.c | 17 +-
mm/memcontrol.c | 435 +++++++++++++----------------------
mm/page_counter.c | 227 +++++++++++++++++-
6 files changed, 429 insertions(+), 289 deletions(-)
--
2.53.1-Meta