mm: memcontrol: let a cgroup opt out of HugeTLB accounting
Subsystems (6)
RFC PATCH 1/1 mm: memcontrol: let a cgroup opt out of HugeTLB accounting
Since commit 8cba9576df60 ("hugetlb: memcg: account hugetlb-backed memory in memory controller"), cgroup v2 can be mounted with memory_hugetlb_accounting, which charges HugeTLB folios to the memory controller. The option is global: it applies to every cgroup on the machine, or to none.
That is a problem for hierarchies that give a workload a HugeTLB budget of its own. Kubernetes is one [7]. A pod requests huge pages as a resource that is separate from memory and comes from a preallocated pool. The kubelet turns the request into hugetlb.<size>.max, and sets memory.max from the memory limit of the pod alone. With the option on, a pod with memory.max=96M and hugetlb.2MB.max=64M that uses 64M of huge pages and 80M of other memory exceeds memory.max: it is OOM-killed, or gets SIGBUS when it faults a huge page. Raising memory.max by the HugeTLB limit does not restore the old behaviour either, because the pod can then use 160M of non-HugeTLB memory while the scheduler placed it for 96M.
The accounting was made opt-in because such setups exist [1][2]. But the choice is made once per machine, by whoever mounts cgroup v2, and systemd mounts it with the option since v259 [3]. A workload manager that runs under that systemd cannot keep the separate accounting for the part of the hierarchy that it owns. That a mixed machine would need a more localized control was raised when the option was added, and deferred until it came up [4][5][6].
Add memory.hugetlb_accounting, a file on non-root cgroups that defaults to
1. Writing 0 keeps HugeTLB folios used by the cgroup and its descendants out of their memory accounting. The folios do not become unaccounted: they are charged to the parent of the cgroup, or to the parent of the topmost ancestor that opted out. Charging the parent, instead of skipping the charge, has two effects:
- The cgroups above keep the complete picture that the mount option gives them, up to the root, whose memory.stat still counts the folios.
- A cgroup cannot use the file to escape a limit of an ancestor, so writing it needs no privilege beyond the permissions of the file.
The file is not delegatable. Like memory.max, it is a setting that a parent makes for its child; a delegatee could otherwise move the HugeTLB usage of its own cgroup out of the limit that the delegator put on it.
Folios keep the charge that they got when they were allocated. A change of the file only affects folios allocated afterwards, in the same way as a remount with or without the mount option.
Extend the hugetlb memcg selftest to check, in a three-level hierarchy, that the usage is charged to the parent of the topmost cgroup that opted out, that a descendant cannot opt back in, and that the root counts the pages in every case.
---
Hi,
When memory_hugetlb_accounting was added, Michal asked what would happen on a machine that runs a mixed bag of containers, some of them not ready for the new accounting [4]. Johannes and Nhat said that more localized control could be added on top of the global flag if it ever came up [5][6]. I think it has now, in Kubernetes [7], and this is my attempt at that control. I would like to hear whether it is the right shape before I take it any further.
Some background on how we got here. I work on Kubernetes. Huge pages are a separate resource for us: they come from a preallocated pool, a pod asks for them next to its memory, and we set memory.max from the memory limit alone and hugetlb.<size>.max from the hugepage limit. That is the kind of setup the option was made opt-in for [1][2], and we never opted in. But systemd v259 started to pass memory_hugetlb_accounting whenever the kernel supports it, with no way to leave it out [3], and Ubuntu 26.04 ships that version. So our users got the new accounting with a distro upgrade.
For example, for a pod that asks for 96M of memory and 64M of huge pages, Kubernetes sets memory.max=96M and hugetlb.2MB.max=64M. With the option on, that pod is OOM-killed when it uses its 64M of huge pages and then 80M of anonymous memory, and in the other order it gets SIGBUS on the huge pages. Both run fine with the option off. I wrote up this and the other cases, with the tests behind them, in [8].
I first looked for a way to deal with this in Kubernetes, and tried a few things with the same workloads.
The obvious one is to set memory.max to the memory limit plus the hugepage limit. The pod above then runs, but its memory limit no longer limits memory: with no huge pages in use it ran with 152M of anonymous memory under a 96M limit. That changes what the limit means for every existing pod.
Adjusting memory.max as the HugeTLB usage changes does not work either. Even with a 50ms poll, anonymous memory allocated right after the huge pages still hits the old limit. This is the polling scheme that Nhat's cover letter for the mount option describes as "cumbersome and buggy" [9].
Raising the limit and having a userspace agent enforce the real one works for pods that stay within their memory, but one that does not can stay over its limit for a whole poll interval.
Remounting cgroup v2 without the option works, and it is what we will document for now. But it is machine wide, and it takes the accounting away from everything else on the machine too.
So I could not find anything in userspace that gives a pod back a limit on just its non-HugeTLB memory, and that is what existing pods are sized for.
With this patch Kubernetes would write 0 to the cgroup that holds all pods, and to the cgroups of host services that it puts a memory limit on. The rest of the machine keeps the accounting that the mount option asked for, and the root still counts every huge page.
The patch, the tests and this text were developed with an AI coding assistant (Claude). I have reviewed them and ran the tests.
[1] https://lore.kernel.org/all/CAPTztWY8eDSa1qKx35hTm5ef+e13SDnRHDrevc-1V1v7-pEP3w@mail.gmail.com/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://github.com/systemd/systemd/pull/39486
[4] https://lore.kernel.org/all/ZRrI90KcRBwVZn%[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/CAKEwX=OwrMNMAvve9a965mBJm8as+njtXE993VcnKXdccd5GEw@mail.gmail.com/
[7] https://github.com/kubernetes/kubernetes/issues/144131#issuecomment-7104174643
[8] https://harche.github.io/2026/11/23/notes-on-kubernetes-issue-144131-hugepages.html
[9] https://lore.kernel.org/all/[email protected]/
Documentation/admin-guide/cgroup-v2.rst | 44 ++++ include/linux/memcontrol.h | 9 + mm/memcontrol.c | 78 ++++++- .../selftests/cgroup/test_hugetlb_memcg.c | 210 +++++++++++++++++- 4 files changed, 335 insertions(+), 6 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +44 -0
@@ -245,6 +245,8 @@ cgroup v2 currently supports the following mount options. * HugeTLB pages utilized while this option is not selected will not be tracked by the memory controller (even if cgroup v2 is remounted later on). + * A cgroup can be exempted, together with its descendants, by + writing "0" to its memory.hugetlb_accounting file. pids_localevents The option restores v1-like behavior of pids.events:max, that is only
@@ -1478,6 +1480,45 @@ The following nested keys are defined. current memory usage for subsequent reads through the same file descriptor. + memory.hugetlb_accounting + A read-write single value file which exists on non-root + cgroups. The default value is "1". + + Determines whether HugeTLB memory used by the cgroup and its + descendants is charged to them. The file only has an effect + if cgroup v2 is mounted with the memory_hugetlb_accounting + option. + + If set to "0", HugeTLB memory used by the cgroup or by any of + its descendants is charged to the parent of the cgroup + instead. If several cgroups on the path to the root have the + file set to "0", it is the parent of the topmost one. The + cgroups above therefore keep accounting for the memory, and + the file cannot be used to escape their limits. A descendant + cannot turn the accounting back on for itself. + + This is meant for hierarchies where a workload manager gives + its workloads a HugeTLB budget of their own, with the HugeTLB + controller, next to a memory.max that covers only their other + memory. Without the exemption such a workload reaches its + memory.max as soon as it uses its huge pages. + + HugeTLB folios that are allocated while a cgroup is exempt do + not show up in its memory.current, nor in the "hugetlb" entry + of its memory.stat, but in those of the cgroup that is charged + for them. A HugeTLB fault in an exempt cgroup still results in + SIGBUS if the cgroup that is charged, or one above it, is at + its memory.max and reclaim fails. The failure is counted in the + memory.events of the cgroup whose limit was hit and of its + ancestors, not in those of the exempt cgroup. + + Like the limits of a cgroup, the file is meant to be written + by the parent, and is not delegatable. + + HugeTLB folios keep the accounting that they got when they + were allocated. A change of the file only affects folios that + are allocated afterwards. + memory.oom.group A read-write single value file which exists on non-root cgroups. The default value is "0".
@@ -1815,6 +1856,9 @@ The following nested keys are defined. Amount of memory used by hugetlb pages. This metric only shows up if hugetlb usage is accounted for in memory.current (i.e. cgroup is mounted with the memory_hugetlb_accounting option). + HugeTLB memory of cgroups that are exempt through + memory.hugetlb_accounting is reported in the cgroup that + is charged for it. memory.numa_stat A read-only nested-keyed file which exists on non-root cgroups.
include/linux/memcontrol.h +9 -0
@@ -342,6 +342,15 @@ struct mem_cgroup {*/ bool oom_group; +#ifdef CONFIG_HUGETLB_PAGE +/* +* Charge HugeTLB folios used by this memcg and its descendants to +* them? If not, the folios are charged to the parent of this memcg. +* Only relevant with the memory_hugetlb_accounting mount option. +*/ +bool hugetlb_accounting; +#endif +#ifdef CONFIG_ZSWAP /* * Prevent pages from this memcg from being written back from zswap to
mm/memcontrol.c +76 -2
125 lines not on this page · raw message
tools/testing/selftests/cgroup/test_hugetlb_memcg.c +206 -4
255 lines not on this page · raw message
base-commit: 15f114dac9a7bde1796f0e54e22726fd31cfa8a5
Signature · 1 line
2.54.0 (Apple Git-157)