Skip to content

mm/page_counter: move stock from mem_cgroup to page_counter

Joshua Hahn cgroups linux-mm lkml 11 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v9 Latest7 patches 0 repliesNew
  2. v87 patches 5 repliesSuperseded
  3. v75 patches 10 repliesSuperseded
Earlier versions · 8
VersionPostedPatchesRepliesStatus
v6 posting 2/25 patches1 replySuperseded
v6 posting 1/25 patches3 repliesSuperseded
v57 patches11 repliesSuperseded
v45 patches7 repliesSuperseded
v3 posting 2/26 patches2 repliesSuperseded
v3 posting 1/27 patches2 repliesSuperseded
v27 patches2 repliesSuperseded
v1 RFC8 patches3 repliesSuperseded

Changes in v9 from the cover letter

=========
- 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.

Patches in v9 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/7mm/memcontrol: flatten try_charge_memcg control flowmm/memcontrol.c+11 -80 replies
2/7mm/page_counter: introduce per-CPU stockinclude/linux/page_counter.h mm/page_counter.c+173 -00 replies
3/7mm/page_counter: make page_counter_try_charge() stock-awareinclude/linux/page_counter.h kernel/cgroup/dmem.c mm/hugetlb_cgroup.c +3 more+86 -180 replies
4/7mm/memcontrol: check PF_MEMALLOC after trying the limitsmm/memcontrol.c+18 -190 replies
5/7mm/memcontrol: move memory stock to page countersmm/memcontrol-v1.c mm/memcontrol.c+84 -2520 replies
6/7mm/memcontrol: add stock to the memsw page countermm/memcontrol.c+48 -160 replies
7/7mm/memcontrol: restrict memory stock to leaf cgroups on v2mm/memcontrol.c+33 -00 replies

Reviews trailers per patch

1 of 7 patches has a Reviewed-by or Acked-by.

PatchHarry Yoo (Meta)Shakeel ButtBalbir Singh
1/7R-b A-b A-b
2/7
3/7
4/7
5/7
6/7
7/7

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes. Scroll the table sideways for all 3 reviewers.

Read the v9 thread

Cover letter [PATCH v9 0/7] mm/page_counter: move stock from mem_cgroup to page_counter

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(-)
Signature · 1 line
2.53.1-Meta