Introduction
Control groups, commonly known as cgroups, are a Linux kernel feature that enables fine-grained resource management for processes. They first appeared in 2007 with Linux kernel 2.6.24. That initial version, cgroups v1, provided a mechanism to allocate resources like CPU, memory, and I/O among process groups. But its complexity and inconsistencies led to a redesign. The result was cgroups v2, merged into the Linux kernel 4.5 in 2016. This article delves into the features of cgroups v2, its impact on modern Linux systems, and its role in technologies like Docker and Kubernetes.
Evolution from cgroups v1
cgroups v1 allowed multiple hierarchies. Each one managed different resources through controllers such as cpu, memory, and blkio. This flexibility was powerful, but it added complexity. Controllers worked independently, so their behavior was inconsistent and hard to manage. For example, a process could belong to different cgroups for different resources. This made resource allocation difficult in containerized environments.
cgroups v2 fixes these problems. It introduces a unified hierarchy, where all controllers sit under a single tree structure. Management becomes simpler. A process belongs to only one cgroup at a time, for all resources. Tejun Heo led the redesign. Its goal was to streamline resource management and improve compatibility with modern workloads, especially containerization.
Key Features of cgroups v2
cgroups v2 improves on its predecessor. It is more robust and easier to use.
Unified Hierarchy
cgroups v1 allowed multiple hierarchies. cgroups v2 does not. It enforces a single, unified hierarchy. All controllers—such as cpu, memory, io, and pids—sit under one tree. Resource allocation is therefore consistent. There is no confusion from managing processes across many hierarchies. Tools get simpler too. systemd integrates tightly with cgroups v2 for service management.
Thread Granularity
cgroups v2 tells processes and threads apart. Threaded controllers allow control at the thread level. This helps multi-threaded applications. A process can allocate resources in fine detail across its threads. For example, specific threads in a threaded subtree can get different CPU or memory limits.
Enhanced Resource Controllers
cgroups v2 refines its resource controllers. They are more precise and more flexible:
- Memory Controller: The memory controller now includes the
memory.highlimit. It throttles memory use without triggering the Out-Of-Memory (OOM) killer. This is a gentler way to manage memory. The controller also supports zswap usage limits and improved swap handling. This softens the impact of memory pressure on workloads. - CPU Controller: The cpu controller supports
cpu.maxfor bandwidth limits. It also supportscpu.weightfor proportional CPU time among cgroups. This gives precise control over CPU allocation. It matters for workloads like Kubernetes pods. - IO Controller: The io controller replaces blkio. It offers advanced I/O management. This includes weight-based scheduling and I/O latency targets. Performance improves in containerized environments.
- Pressure Stall Information (PSI): PSI is a new interface in cgroups v2. It shows resource pressure, such as memory or CPU stalls. Administrators use it to monitor and optimize system performance.
No Internal Processes in Non-Leaf Nodes
In cgroups v2, only leaf nodes can hold processes. Resource limits are applied consistently at the lowest level of the hierarchy. cgroups v1 was different. There, processes could sit in non-leaf nodes. That led to unpredictable resource distribution.
Namespace Integration
cgroups v2 works with Linux namespaces. Containers get better isolation. The nsdelegate mount option treats cgroup namespaces as delegation boundaries. Unprivileged users can then manage sub-hierarchies securely. This is crucial for rootless containers.
Simplified Delegation
Delegation in cgroups v2 is more secure and more straightforward. Users can manage sub-hierarchies without touching parent resources. Access to control files like cgroup.procs and cgroup.subtree_control is restricted. This supports use cases like user-level container management.
Impact of cgroups v2
cgroups v2 has changed how administrators manage Linux systems. It also shapes containerization and orchestration platforms.
Improved Containerization
Modern container runtimes such as Docker and containerd are built on cgroups v2. Its unified hierarchy and enhanced controllers give containers consistent resource allocation. This prevents the "noisy neighbor" problem, where one container uses up too many resources. Kubernetes, a leading orchestration platform, uses cgroups v2 to manage pod and container resources. Some of its features, like MemoryQoS, depend solely on v2 primitives. Since Kubernetes 1.25, cgroups v2 support has been generally available. This reflects the maturity of the design.
Adoption in Linux Distributions
Major Linux distributions have made cgroups v2 the default. Fedora 31 did so in October 2019, and Ubuntu 22.04 followed. The change comes from simpler management and better performance. Fedora, for example, sets systemd.unified_cgroup_hierarchy=1 in its boot configuration to enable cgroups v2. Kubernetes can also detect it on compatible kernels (version 5.8 or later) and adapt automatically.
Enhanced Security
cgroups v2 improves security in two ways. It restricts resource access and supports rootless containers. Device access control uses eBPF programs. These allow fine-grained permissions and shrink the attack surface for containerized applications. The OOM killer is also cgroup-aware, a change introduced in Linux 4.19. Under memory pressure, it can kill a whole cgroup as one unit. This preserves the integrity of the workload.
Systemd Integration
Systemd is the default init system in most modern Linux distributions. It relies heavily on cgroups v2 to manage services, slices, and scopes. Systemd mounts the cgroups v2 filesystem at /sys/fs/cgroup on its own. It then organizes processes into a unified hierarchy. This makes resource management easier for administrators. They can set CPU, memory, or I/O limits for services with systemctl or unit files.
Performance Optimization
The PSI interface and refined controllers make performance tuning easier. Administrators can watch memory pressure to decide if a workload needs more resources. They can also spot bottlenecks in I/O-heavy applications. The memory.high mechanism manages memory dynamically. It avoids abrupt process termination, which keeps the system more stable.
Challenges and Limitations
While cgroups v2 offers significant improvements, it is not without challenges. Not all cgroups v1 controllers are
fully supported in v2; for example, the cpuacct controller is integrated into the CPU controller, and some niche
controllers lack equivalents. This requires legacy applications to adapt to the new API. Additionally, transitioning to
cgroups v2 may involve modifying kernel boot parameters (e.g., systemd.unified_cgroup_hierarchy=1) and ensuring
compatibility with container runtimes and Linux distributions.
Direct access to the cgroups filesystem may also pose challenges for applications that relied on v1’s structure.
Administrators must update scripts or tools to use v2’s unified hierarchy and new interfaces, such as
cgroup.controllers and cgroup.subtree_control.
Practical Example
To illustrate cgroups v2 usage, consider limiting a process to 2GB of memory using systemd. On a cgroups v2-enabled system, you can create a systemd slice:
# Create a slice with memory limits
sudo systemctl set-property user.slice MemoryMax=2G
# Run a process under the slice
systemd-run --slice=user.slice --scope matlab
This restricts MATLAB to 2GB of memory, preventing it from consuming excessive system resources. Such configurations are common in environments running resource-intensive applications.
Final Notes
cgroups v2 gives Linux a unified hierarchy, stronger controllers, and tight integration with Docker, Kubernetes, and systemd. Major distributions and container ecosystems have adopted it because it makes resource allocation efficient, secure, and scalable. Moving over from cgroups v1 takes some adjustment, but the payoff is simpler management, better performance, and stronger security. For details, see the Linux kernel documentation, the kernel admin guide, the Kubernetes cgroups v2 announcement, and the Red Hat resource management guide.