cgroups v2 and resource control

Advanced · 57 min

Control groups put processes in a tree and cap CPU, memory, I/O and process counts per branch. How v2 works and how systemd's slices drive it.

Before thisUnits, targets and dependenciesResource limits with ulimitThe OOM killer

NDC Conferences · 57 min

cgroups v2 and resource control

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by NDC ConferencesReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A cgroup is a group of processes in one tree mounted at /sys/fs/cgroup. Children of a process start in its cgroup, so a service cannot escape by forking.
  • Controllers such as cpu, memory, io and pids enforce limits. A controller is switched on for a cgroup's children by writing it to cgroup.subtree_control.
  • In v2, processes live only in leaf cgroups when controllers are enabled below: the no-internal-process rule.
  • systemd owns the tree: every service gets a cgroup under system.slice, user sessions go under user.slice, and slices are units you can configure.
  • Use unit settings, not raw files: MemoryMax=, MemoryHigh=, CPUWeight=, CPUQuota=, IOWeight=, TasksMax=. `systemctl set-property` changes them live.
  • memory.high throttles and reclaims. memory.max is the hard wall where the cgroup's OOM killer fires. The kernel docs call memory.high the main mechanism.
  • `systemd-cgls` shows the tree, `systemd-cgtop` shows usage, `systemd-run -p MemoryMax=...` runs a one-off command inside a limit.
  • systemd 258 removed cgroup v1 support entirely. On a current system the unified v2 hierarchy is the only one.

Limits that a fork cannot escape

The ulimit lesson showed per-process limits. They have a hole: a process that forks gets a fresh set of counters, so ten workers at 1 GB each use 10 GB. And nothing per-process can say “this service, all of it, gets at most a quarter of the CPU”.

Control groups close that hole. A cgroup is a named group of processes. Every child a process creates starts in the same cgroup, so a service and everything it spawns are accounted together. Controllers then apply limits to the group as a whole. Think of a shared office budget: no matter how many people a team hires, the team’s spending comes out of one account.

Containers are built on this, alongside the namespaces of the next lesson. So is every systemd service.

One tree

cgroup v2 is a single hierarchy, a filesystem mounted at /sys/fs/cgroup. Each directory is a cgroup. The kernel’s Control Group v2 documentation describes the core files:

  • cgroup.procs: the PIDs in this cgroup. Writing a PID here moves the process.
  • cgroup.controllers: the controllers available to this cgroup.
  • cgroup.subtree_control: the controllers enabled for its children. No controller is enabled by default. Writing +cpu +memory turns them on below.

The common controllers are cpu, memory, io, pids and cpuset. Each adds its own files to the child cgroups: memory.max, cpu.weight, pids.max and so on.

One rule surprises people. The kernel docs call it the no internal process constraint: a non-root cgroup can hand out resources to its children only if it has no processes of its own. Processes live in leaves. This removes the old v1 ambiguity of a parent’s own processes competing with its children.

v1 had a separate tree per controller, which made combined limits awkward. systemd’s release notes for version 258 say support for cgroup v1 “has been removed”, and v2 is always mounted at boot. Older guides showing /sys/fs/cgroup/memory/ and memory.limit_in_bytes describe v1.

The controllers that matter

From the kernel documentation:

File Default What it does
cpu.weight 100 Share of CPU under contention, 1 to 10000, relative to siblings
cpu.max max 100000 Hard cap: at most $MAX microseconds per $PERIOD
memory.high max Throttle: over it, the group is slowed and reclaimed hard. Never invokes the OOM killer
memory.max max Hard limit: if usage cannot be reclaimed below it, the OOM killer runs inside the cgroup
io.weight, io.max Relative and absolute disk bandwidth
pids.max max Hard limit on processes and threads, a fork-bomb stop

Weight versus max is the key distinction. A weight costs nothing when the machine is idle. It only decides who wins under contention. A max applies even on an idle machine. For memory, the kernel docs say plainly that memory.high “is the main mechanism to control memory usage”, with memory.max as the backstop, because throttling gives you a slow service and a pressure signal instead of a dead one. Memory charged to a cgroup includes page cache it causes, so a service that reads a lot of files can approach its limit through cache, which the kernel reclaims before resorting to an OOM kill.

Each cgroup also has cpu.pressure, memory.pressure and io.pressure files, pressure-stall information showing how much time its tasks spent waiting for that resource. systemd-oomd from the OOM lesson reads these.

systemd drives the tree

On a systemd machine you rarely write these files by hand, because systemd owns the hierarchy. systemd.special(7) describes the layout:

  • -.slice: the root.
  • system.slice: every system service, each in its own cgroup, such as system.slice/nginx.service.
  • user.slice: user sessions, one user-1000.slice per user.
  • machine.slice: VMs and containers registered with systemd-machined.

A slice is a unit type, the .slice from the units lesson. The Debian 13 systemd package I unpacked on this Mac ships user.slice, machine.slice and capsule.slice as plain unit files. user.slice is four lines:

[Unit]
Description=User and Session Slice
Documentation=man:systemd.special(7)
Before=slices.target

Resource settings go on any service, slice or scope, per systemd.resource-control(5):

# systemctl edit myapp.service
[Service]
MemoryHigh=1.5G
MemoryMax=2G
CPUWeight=200
CPUQuota=150%
TasksMax=512

CPUQuota=150% means at most one and a half CPUs’ worth, which systemd writes to cpu.max. To put several services under one budget, create a slice, set limits on it, and give each service Slice=myteam.slice.

Commands

From the systemd 262 man pages. I did not run them, because there is no Linux machine here:

systemd-cgls                                  # the whole tree with processes
systemd-cgls -u nginx.service                 # one unit's subtree
systemd-cgtop                                 # live usage per cgroup, like top
systemctl set-property myapp.service MemoryMax=2G   # change live, persisted
systemctl set-property --runtime myapp.service CPUQuota=50%   # live, until reboot
systemd-run -p MemoryMax=1G -p CPUQuota=20% --wait ./backup.sh
systemd-run --scope -p MemoryMax=500M make -j8   # run in the foreground inside a scope
cat /proc/self/cgroup                         # which cgroup am I in?

systemd-run creates a transient unit, a service by default or a scope with --scope, and the -p options are the same unit properties. It is the quickest way to cap one heavy command, such as a build or a reindex, without touching anything permanent. The man page’s own example is systemd-run -p IOWeight=10 updatedb.

About the video

The embed is all of “An introduction to control groups (cgroups) version 2” by Michael Kerrisk at NDC TechTown 2021, 57 minutes. Kerrisk maintained the Linux man-pages project and wrote The Linux Programming Interface. The description says the talk deliberately ignores v1 and explains cgroups from a v2 perspective: how they work, the hierarchy, controllers. That matches the kernel half of this lesson. It is a conference talk without chapters, so I judged it from its description. Its limit is that it shows the raw filesystem, not systemd’s slices and unit settings, which is how you will usually meet cgroups, so the lesson adds those. It predates systemd dropping v1 in version 258. English was judged from the speaker and English auto-captions. Chris Down’s FOSDEM talk in deeper is the view from a kernel cgroup and memory developer.

What was run: unpacking Debian 13’s systemd package and reading its slice units. Everything else is from the kernel 7.4-rc4 cgroup v2 documentation, the systemd 262 man pages and systemd’s NEWS file, checked 2026-11-28.

Where this fits

cgroups decide how much a group of processes may use. Namespaces decide what it can see: namespaces.

Check yourself

  1. 01Why is a per-service memory limit better done with MemoryMax= than with LimitAS= or ulimit?

    rlimits are per process and escape by forking, and LimitAS caps address space, not memory. MemoryMax= limits the whole cgroup, counting every process the service starts, including page cache it uses.

  2. 02Two services share a CPU. One has CPUWeight=100, the other CPUWeight=300. Both are busy. How is CPU split?

    About 25% and 75%. Weights are relative and only matter under contention. With one idle, the other can use all of it.

  3. 03How do you run a one-off backup script with at most 1 GB of memory and 20% of one CPU, without writing a unit?

    `systemd-run -p MemoryMax=1G -p CPUQuota=20% --wait /usr/local/bin/backup.sh`. It runs as a transient service in its own cgroup.

  4. 04What does writing `+memory` to /sys/fs/cgroup/app/cgroup.subtree_control do?

    Enables the memory controller for the children of app, so each child cgroup gets memory.* files and can be limited.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark