Author: Harshal Patil

These are my notes for kubernetes/kubernetes issue 144131, with every test behind them.

Linux 6.7 added a cgroup v2 mount option, memory_hugetlb_accounting, that makes the memory controller count the hugepages a cgroup uses on top of its ordinary memory. The option is opt-in, but systemd 259 turns it on for every machine. The kubelet still sets a pod's memory limit as if hugepages were counted separately, so pods that use both get killed or hang. Raising the limit makes them run but changes what the limit means, so the plan is to turn the option off on the node now and add a kernel switch for pods later.

The problem

A pod gets two jars

Ordinary memory comes in tiny pages. Hugepages are big blocks the node sets aside in advance. A pod asks for each one separately: here memory: 96Mi and hugepages-2Mi: 64Mi.

Ordinary memory lid 96 80 used Hugepages lid 64 64 used
The old world. Each jar has its own lid and its own count. The memory lid is memory.max; the hugepage lid is hugetlb.2MB.max. This pod fits in both.

The new rule: hugepages fill both jars

With the cgroup mount option memory_hugetlb_accounting, every hugepage the pod touches is counted in the memory jar too. The pod asked for 96 of ordinary memory. Now only 32 fits.

Ordinary memory 32 left lid 96 also counted here Hugepages lid 64 64 used
The same 64 of hugepages now takes room under the memory lid. The lid has not moved.
  • Who turned it on: the kernel has offered the option since 6.7. systemd 259 switches it on at boot. Ubuntu 26.04 ships both.
  • Only touched pages count. The kernel documentation says the pool belongs to nobody until a page is used. Reserving hugepages without using them adds nothing to the memory jar.

The pod overflows a jar it sized correctly

Three pods, all with the same two limits, all inside both of them as Kubernetes defines them. The memory lid is still 96.

lid 96 hugepages 32 memory 80 16 over
32 of hugepages, then 80 of memoryOOMKilled
lid 96 hugepages 64 memory 80 48 over
64 of hugepages, then 80 of memoryOOMKilled
lid 96 memory 80 hugepages 16 stuck here
80 of memory, then 32 of hugepagesHangs at 100% CPU

The order decides the failure. An ordinary page that does not fit calls the OOM killer. A hugepage that does not fit does not, and on this kernel the program retries forever.

Why the third pod hangs instead of dying

This part is a kernel bug, separate from the kubelet. It is present from kernel 6.14 (commit 991135774c0e) through 7.3.

Program touches a hugepage Jar is full, the charge is refused Kernel says out of memory, kills nobody Program gets an error (SIGBUS) and can stop try again forever, 100% CPU kernel 7.4 and later
Measured: the pod stayed Running, spinning, with no SIGBUS and no OOM kill. Upstream commit 956aeff185e7 replaces the loop with an error; it is first in 7.4-rc1.

The node's numbers are fine. The pod's are not.

A touched hugepage is counted in every box around the process, from the inside out. Only the two inner boxes have a lid that is too small.

Node Allocatable memory already leaves out the hugepage pool. right
All pods together kubepods lid is the whole node here, pool included. right
Pod memory.max = 96, should hold 96 + 64. 64 too small
Container memory.max = 96, should hold 96 + 64. 64 too small
allocatable 11,313 hugepage pool 512 eviction reserve 100
The test node, 11,925 in total. The kubelet already takes the pool out before it tells the scheduler how much ordinary memory there is.

So taking hugepages out of allocatable a second time would not move either small lid. The pods above would still die.

Why not just raise the limit

First idea: raise the lid by the hugepage amount

The kubelet could set the memory lid to memory plus hugepages when it sees the mount option. kubectl would still show 96, because that is what the user asked for. A prototype does this: the three pods above run, and a pod under MemoryQoS is no longer throttled.

  • Raised the same way: memory.high, memory.min and memory.low, which the kubelet writes when MemoryQoS is configured.
  • Unchanged: the hugepage jar keeps its lid of 64. With the mount option off, the same kubelet writes the old values.
  • In-place resize: the lid follows the new memory limit plus 64.
  • Size: about 165 lines of kubelet code in 11 files, plus tests. No API or CRI change.

It has two costs. The first can be repaired. The second cannot, and it is why this is not the plan.

Cost one: the usage meter reads higher (repairable)

With the option on, the kubelet's usage number for a pod (working set) includes its hugepages. Eviction ranking, kubectl top and VPA all read that number. From kernel 6.13 the kernel reports the hugepage part on its own line (hugetlb in memory.stat), so the meter can subtract it. Kernels 6.7 to 6.12 count the hugepages without reporting them separately, so this repair does not work there.

Option off
Option on, today
Option on, cAdvisor patched
right edge = the pod's memory request, 96
Same pod each time: 10 of ordinary memory and 64 of hugepages. The meter reads 10, then 74, then 10 again with the patch. Measured for the container, the pod and all pods together. The node's number never included hugepages.
  • One place by default. The number comes from cAdvisor for containerd and CRI-O alike, so one cAdvisor change covers the default setup.
  • Total usage stays as the kernel reports it (74 here), so it still matches the raised lid.
  • The available number still counts hugepages. The kubelet computes it as lid minus working set and uses it to evict pods before all pods together reach their lid. With the meter fix alone, a growing pod was OOM-killed and no eviction happened. With available = lid − working set − hugepages, it was evicted at the same point as today.
  • Why it matters: without it, a pod that fills its 64 of hugepages and uses more than 32 of ordinary memory reads above its 96 request, and eviction can treat it as over its request.

Tested with a patched copy of cAdvisor inside the kubelet. This repair is only needed if the lid is raised: with the option off, or with the kernel switch below, the counter already reads 10.

Cost two: the memory limit stops being a limit

The kernel keeps one count per jar and cannot tell the two kinds apart. With the lid at 160, a pod that leaves its hugepages untouched can use that room for ordinary memory, and nothing stops it at 96.

lid 96 memory 96 54 refused
What users have today: a pod that asks for 150 of memoryOOM-killed at 96
asked 96 lid 160 memory 150 54 extra
Lid raised: the same podNot stopped
asked 96 lid 160 memory 150 stuck here
Lid raised: the same pod then touches its hugepagesHangs
  • The pod breaks later instead of now. Its own hugepages no longer fit, although the node still holds them for it. On kernel 7.0 it hung; the kernel documentation says SIGBUS.
  • The node can run short. The scheduler placed the pod for 96 of ordinary memory. Each such pod can take up to 64 more, while its hugepages stay set aside. This follows from the arithmetic and was not measured.
  • It is a change users can see. A release that behaves differently from the last one for the same pod is what upstream rejects.

Getting the old behaviour back

Short term, works today: remount cgroup v2 at boot

Nothing in Kubernetes can restore the old behaviour on kernels that are already shipped. The node can. With the systemd that is installed today, a small boot unit mounts cgroup v2 again without the option, before the container runtime starts.

systemd (in the initrd) mounts: option on Boot unit remounts without the option Container runtime and kubelet start Pods get two separate jars again option on option off short window at early boot
The bar shows the state of the option over the boot. The drawing is not to scale; the window lasts until the unit runs.
[Unit]
Description=Mount cgroup v2 without memory_hugetlb_accounting
DefaultDependencies=no
After=systemd-remount-fs.service
Before=sysinit.target containerd.service crio.service kubelet.service

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/mount -o remount,nsdelegate,memory_recursiveprot cgroup2 /sys/fs/cgroup

[Install]
WantedBy=sysinit.target
  • Tested with nothing modified: Ubuntu kernel 7.0.0-43, systemd 259.5, containerd 2.2.2 and a kubelet built from the unmodified source. The option was off after the reboot, and all five hugepage test pods behaved as before the option existed.
  • Control: with the unit disabled, the same node booted with the option on, killed two of the pods and hung a third.
  • It stays off. systemctl daemon-reexec, service restarts and services with cgroup protections did not bring the option back.

Weak points

  • It must repeat every other option. A remount replaces the whole list, so the unit names nsdelegate and memory_recursiveprot itself. If a later systemd adds an option, the unit silently drops it.
  • Ordering. It only works if it runs before the container runtime, and nothing enforces that on a differently arranged system.
  • Stock systemd can turn it back on. systemd-nspawn started without its own cgroup namespace mounts cgroup v2 with the option, and the kernel applies that to the whole machine. Measured on stock systemd: after such a container ran, the option was on again. A systemd with the change below no longer does this.
  • It is a workaround. It undoes a default instead of choosing a setting.

Short term, cleaner: a switch in systemd

systemd turns the option on whenever the kernel supports it and offers no way to say no. The change makes the option opt-in again: systemd does not set it unless systemd.memory_hugetlb_accounting=yes is on the kernel command line, so on a default boot nothing has to undo it.

Default boot, no switch needed systemd mounts without the option Container runtime and kubelet start Pods get two separate jars again option off from the first mount not mounted yet
There is no window, no second mount and nothing to configure. The switch that turns the option on is read from the kernel command line because that is the one place both the initrd's systemd and the main one can see.

With the option off at boot, an unmodified kubelet ran all five hugepage test pods as before the option existed, and the pod holding 64 of hugepages read a usage of 10. That run was on Ubuntu 26.04 (kernel 7.0.0-43, containerd 2.2.2) with an earlier form of the change on Ubuntu's systemd 259.9, where a switch turned the option off. The resulting mount state is the same.

The change

  • Written against upstream systemd (the development branch for v263; pull request 45018): about 190 lines in src/shared/mount-setup.c, a man page entry in kernel-command-line(7), a NEWS entry, new checks in the existing integration test TEST-08-INITRD and a new case in TEST-13-NSPAWN.
  • The default goes back to off. Without the switch systemd mounts cgroup v2 as it did before v259, without the option. systemd.memory_hugetlb_accounting=yes turns it on.
  • One name for the whole boot. The initrd's systemd and the main one read the same switch, because cgroup v2 is mounted once, by whichever runs first.
  • An older initrd does not defeat it. If cgroup v2 was already mounted differently from what is wanted, for example with the option on by a v259 to v262 systemd in the initrd, the main systemd remounts it at boot and keeps the other mount options. Measured with a deliberately old initrd: off after a default boot, on after a boot with =yes.
  • Only at boot. systemctl daemon-reexec does not remount, so a change made by hand on a running system stays, and a system updated from v259 to v262 changes at its next boot.
  • Containers do not override the host. A cgroup v2 mount made from the host's cgroup namespace sets the options for the whole machine. systemd-nspawn without a cgroup namespace, and a systemd that boots inside a container sharing the host's cgroup namespace, both keep what the host uses and ignore their own command line.

Tests

  • Values of the switch: yes, 1 and a switch without a value turn the option on. No switch, no, an invalid value (with a warning in the log) and the rd. form all leave it off. One boot each, on Ubuntu 26.04 arm64 with upstream systemd plus the change.
  • systemd's own checks pass on Fedora 44 x86_64: the build with warnings as errors, clang-tidy on the changed file, TEST-08-INITRD, TEST-13-NSPAWN, TEST-01-BASIC and TEST-19-CGROUP. Each new check fails when the code it covers is disabled. On Ubuntu 26.04 arm64 the build, clang-tidy, TEST-08-INITRD and the new TEST-13-NSPAWN case passed on the change as it was before the container part was added.
  • systemd-nspawn keeps whatever state the machine has. Measured both ways: on a default boot the option stayed off after containers ran, with and without their own cgroup namespace, also when the container tool saw a command line with =yes. On a boot with =yes it stayed on. A real systemd booted in a container that shares the host's cgroup namespace and has no cgroup mount left the host's setting alone too, with the host's option off and on.

Weak points

  • It needs systemd to accept it and distributions to ship that version. It reverses a default systemd chose in v259, which is a bigger ask than an added switch. It is proposed as pull request 45018.
  • Nodes only get it with a systemd update. Until then the remount unit above is the way.
  • It is node-wide. System services on that node go back to the old accounting too.

Long term: a kernel switch for the pods' cgroup

The mount option is one switch for the whole machine. A per-cgroup file, memory.hugetlb_accounting, lets one part of the cgroup tree keep the old rule, and the kubelet sets it on the cgroups it owns.

Why the kernel should take it

  • Reviewers expected this breakage; that is why the option is opt-in.
    • Frank van der Linden (26 Sep 2023): workloads "will break badly because they'll hit their limits immediately - imagine a container that uses 1G hugetlb pages to back something large (a database, a VM), and 'plain' memory for control processes."
    • Michal Hocko (27 Sep 2023): "I will also Nack involving hugetlb pages being accounted by default. This would break any setups which mix normal and hugetlb memory with memcg limits applied."
    • Johannes Weiner (27 Sep 2023): "I think we're all on the same page this needs to be opt-in."
  • A per-cgroup control was raised and deferred, not rejected.
    • Michal Hocko (2 Oct 2023): "I am especially worried about setups where a mixed bag of workloads (containers) is executed. While some of them will be ready for the new accounting mode many will leave in their own world without ever being modified. How do we deal with that situation?"
    • Johannes Weiner (2 Oct 2023): "It's possible to add more localized control on top of the global flag should this come up. [...] Let's cross this bridge not when but if we have to."
    • Nhat Pham, the author (2 Oct 2023): "we can always revisit it if/when it proves to be an issue and add appropriate memcg-specific control for this feature as a follow-up."
  • It has now come up. systemd 259 turns the option on for every machine (systemd pull request 39486), and Kubernetes is the mixed case: pods with their own memory limit next to a hugepage limit. The tests on this page are the evidence.
  • The authors' own use is untouched. Their case is one total budget per container with hugepages allocated on demand. The switch defaults to 1, so nothing changes unless a cgroup owner sets it.

Review threads: v1, v2, v3, v4. Merged as kernel commit 8cba9576df60.

The design

  • Kernel: every non-root cgroup gets memory.hugetlb_accounting, default 1 (RFC on the kernel mailing lists). With 0, hugepages used in that cgroup or below are counted in its parent instead. If that parent is the root, no limit applies, and the root's memory.stat still shows them. Because the count moves up and does not disappear, a cgroup cannot use the switch to get around a limit above it. Like memory.max, the file is set by the parent and cannot be delegated.
  • Kubelet: when the file exists, it writes 0 on kubepods and on each reserved cgroup it enforces. Nothing else in the kubelet changes.
Root Hugepages used below a switched-off cgroup are counted in its parent. Here that is the root, which has no lid. counted here
All pods together kubepods: the kubelet writes 0. switch = 0
Pod and container memory.max = 96, ordinary memory only, as before the option existed. hugepages not counted
Reserved cgroups System-reserved and kube-reserved, when the kubelet enforces them (KEP-6252): the kubelet writes 0. switch = 0
Everything else Other system services keep what systemd set up. switch = 1

Tested on mainline 7.4-rc3 with the series and the kubelet change, with the mount option still on: the hugepage pods, the usage numbers, the reserved cgroup, in-place resize, pod-level resources, the DRA claim, the shared hugepage volume and MemoryQoS throttling all behaved as they do with the option off (arm64, containerd).

Other features on the same node

Anything that puts a memory lid next to a hugepage lid, or reads the memory counter, is affected the same way as a pod. Fourteen features were tested with an unmodified kubelet on the unmodified kernel (7.0.0-43).

Hugepages reserved for host services (KEP-6252)

With --system-reserved=memory=96Mi,hugepages-2Mi=64Mi enforced, the kubelet gives the host services' cgroup the same two lids a pod gets. A host service that uses 64 of hugepages and 80 of memory then fails like the pods do.

  • Only with enforcement. It needs system-reserved or kube-reserved in enforceNodeAllocatable, which is not the default.
  • The KEP text is out of date. It says Kubernetes does not enable memory_hugetlb_accounting. systemd now enables it for every node.

In-place resize (KEP-1287)

Before lowering a memory limit, the kubelet checks that the pod's usage is below the new limit. That usage now includes hugepages, so a decrease that is fine for ordinary memory is refused.

Option off
Option on
vertical line = the new limit, 80
A pod using 20 of memory and 64 of hugepages asks to go from a 256 limit to 80. With the option off the usage is 20 and the resize is applied. With it on the usage is 84 and the kubelet answers "cannot decrease memory limits ... below current usage".

All features tested

Each row is the same pod or service run twice with an unmodified kubelet, once with the option on and once with it off. Unless a row says otherwise, the test program touches 64 of hugepages, then 80 of ordinary memory, under a memory limit of 96 and a hugepage limit of 64, and then exits.

FeatureOption onOption off
Reserved hugepages for host services (KEP-6252)Host service in the reserved cgroup is killed by the kernel partway (OOM)Runs every step and exits normally
In-place resize (KEP-1287)Request to lower the limit from 256 to 80 is refused; the limit stays 256Limit is lowered to 80
Pod-level resources (KEP-2837)Pod is killed by the kernel partway (OOM)Runs every step and exits normally
DRA node allocatable resources (KEP-5517)Pod is killed by the kernel partway (OOM). The kubelet set the limits of 96 and 64 from the pod spec plus the claimRuns every step and exits normally
MemoryQoS throttling (KEP-2570)A pod using 180 of memory and 64 of hugepages under a 256 limit is slowed down by the kernel 404 timesNever slowed down
Pressure stall information (KEP-4205)The same pod reports memory stalls, also in the summary APINo stalls
Swap, LimitedSwap (KEP-2400)Pod using 200 of memory and 128 of hugepages under a 256 limit keeps running, with 74 of its memory pushed to swapKeeps running, nothing in swap
Stats from the runtime (KEP-2371, gate on)Working set reads 74 for pod and containerReads 10
Eviction and usage numbers (default stats path)Working set includes hugepages; the all-pods eviction signal drops as they are usedUnchanged
Hugepage emptyDir shared by two containers (KEP-1539)The container that touches the pages first has 64 in its memory counter, the other 00 in both
Memory Manager, static policy (KEP-1769)Pod is killed by the kernel partway (OOM), as without the manager; its assignment is the sameRuns every step and exits normally
QOSReserved (alpha)No failure: 448 of hugepages plus 600 of memory fit under the Burstable lid of 1524. That lid includes the hugepage pool, so the only effect is less room for pods to burstSame
Downward API for hugepages (KEP-2053)Reports 96 and 64Same
Memory-backed volume sizing (KEP-1967)Volume is 96Same

Where to read more

Updated: