Skip to content
Open Sourceadvanced

What are Linux cgroups?

Updated Nov 30, 20263 min readBased on official documentation
What are Linux cgroups?

In short

Linux cgroups, short for control groups, are a kernel feature that groups processes into a tree and controls how much of the system's resources each group can use. Each process belongs to exactly one cgroup, and limits set on a group apply to everything inside it.

What a cgroup is

A cgroup is a way for the Linux kernel to organize processes in a hierarchy. Think of it as a tree of named buckets. Every process on the system sits in exactly one bucket, and the buckets can hold other buckets. The kernel then uses this tree to share out system resources such as CPU time and memory in a controlled way.

The feature has two parts. The core handles the tree itself and the movement of processes into it. Controllers do the real work. A controller usually distributes one kind of resource along the tree, although a few controllers exist for other purposes.

How it works

All threads of a process share one cgroup by default. A new process starts in the same cgroup as the process that forked it. You can move a process to another cgroup later, and doing so does not change the cgroups of its existing child processes.

Controllers are enabled per cgroup, within certain structural rules. Once a controller is enabled on a cgroup, it affects every process in that cgroup and in all cgroups below it. A nested cgroup can only restrict resources further. A limit set closer to the root cannot be loosened by a child.

The cgroup v2 interface is a filesystem. You mount it with mount -t cgroup2 none $MOUNT_POINT. Creating a directory creates a child cgroup, and removing an empty directory destroys it. Each cgroup exposes a cgroup.procs file. Writing a PID to that file moves the process into the cgroup. Only one process can be moved per write, and writing any thread's PID moves the whole process.

0::/demo

The last line shows the cgroup v2 entry for that process. The format is always 0:: followed by the path.

Why v2 is a single hierarchy

The older v1 layout allowed several separate trees, one per controller group. Version 2 uses a single unified tree. Controllers that are not in use on v2 can be bound to a v1 hierarchy, which lets the two versions coexist. Moving controllers between hierarchies at runtime is discouraged for production systems. The documentation recommends choosing the layout once, early after boot, and sticking with it.

Mount options worth knowing

The v2 mount accepts several options. Some of them change behavior in ways that matter for production. The nsdelegate option treats cgroup namespaces as delegation boundaries. memory_recursiveprot applies memory protection across whole subtrees without explicit setup in each leaf. memory_hugetlb_accounting counts HugeTLB memory against the memory controller, and it is opt-in because it can change existing setups. pids_localevents restores v1-style counting of fork failures. Read the official page before enabling any of them, because each one changes how limits behave.

Common mistakes

A cgroup that still has live processes or child cgroups cannot be removed. Zombie processes are an exception. A cgroup that holds only zombies counts as empty and can be deleted, although zombies themselves cannot be moved.

Moving a controller between hierarchies while the system is running can leave it busy for a while. Other controllers may need to be disabled first because of dependencies between them. Plan the layout before your services start.

Another trap is forgetting that limits are inherited downward. A tight limit high in the tree applies to everything beneath it, even if a child cgroup sets a looser value.

When to use cgroups

Cgroups are the foundation of many container tools, and they are useful any time you need to cap or divide the resources of a group of processes. If you only need to limit one program, a service manager or a container runtime usually sets up the cgroup for you. Learning the raw interface helps when you debug those tools or build your own.

FAQ

Do I need to mount cgroup v2 myself?

Often not. Many init systems and container runtimes mount it during boot. You can mount it manually with mount -t cgroup2 when you are experimenting or building a custom setup.

Can a process belong to more than one cgroup?

In the v2 hierarchy, no. Every process belongs to one and only one cgroup. Threads of the same process share that cgroup by default.

Why can't I remove a cgroup directory?

The cgroup still has child cgroups or live processes. Move or stop those processes first. A cgroup that only has zombie processes can be removed.

Does moving a process change its children?

No. Migrating a process does not affect child processes that already exist. New children are placed in the cgroup their parent belongs to at fork time.

Sources

This page is based on the official documentation below. Read it for full detail.

Linux cgroups — official documentation ↗

Linux cgroups in the news

blozhikDeveloper news from across the web, explained. Every story links to its original sources.