Full name: Control group (Linux kernel).
Definition
A cgroup (control group) is a set of running programs, called processes, that the Linux kernel treats as one unit for counting and limiting resource use. In version 2, the current design, cgroups form one tree, each process belongs to exactly one cgroup, and a new process starts in its creator’s cgroup. Controllers attached to the tree measure and limit resources such as processor time, memory and the number of processes. A cgroup groups processes; it does not restrict what they may access.
The idea
A measurement harness starts a launcher program and watches its process identifier (PID), the number the kernel gives each process. The launcher starts two workers and exits after a second. The harness sees the PID disappear, records the run as finished and starts the next run, while the two workers still use the processor and the device under test. Watching one process cannot follow the work, because programs start other programs, and those can outlive their creator, be adopted by another parent or start a new session.
A cgroup fixes this with a label that the kernel attaches to every process and copies to every process that process creates. Losing a parent or starting a new session leaves the label unchanged; only an explicit move, which needs a write permission that a job can be denied, changes it. The kernel can then tell, for the whole group at once, whether any labeled process still lives and how much the group uses. A household register is a fair picture: every child born at an address is registered there and stays registered after leaving home, until someone with authority files a transfer. The picture stops being accurate in one respect: work that the household asks an outside firm to do is registered at the firm. A cgroup never contains work that another service does on its members’ behalf.
How it works
The kernel shows the cgroup tree as a directory tree in a special file system, usually mounted at /sys/fs/cgroup. Creating a directory creates a cgroup, and the files inside each directory are its interface.
Membership. The file
cgroup.procslists the PIDs of the member processes. Writing a PID into it moves that process, with all its threads, into the cgroup. Only that process moves; children it created earlier stay where they are.Inheritance. A new process is born into the cgroup that its creator belongs to at that moment. This rule makes every descendant a member: a process placed in a cgroup before it starts any other process has all its later descendants in the same cgroup.
Liveness signal. The file
cgroup.eventscontains a fieldpopulated, which is 1 while the cgroup or any cgroup below it contains a live process and 0 otherwise; a program can be notified when the field changes instead of polling it. A process that has ended but whose exit status §13.24 its parent has not yet collected, called a zombie, no longer counts as live.Controllers. A controller distributes one kind of resource along the tree. The
cpucontroller offerscpu.max, a limit on processor time per period, andcpu.weight, a relative share. Thememorycontroller offersmemory.max, a limit on memory use. Thepidscontroller offerspids.max, a limit on the number of processes and threads, beyond which creating a process fails with the errorEAGAIN. A cgroup enables controllers for its children by writing their names into its filecgroup.subtree_control. Limits are hierarchical: nothing below a cgroup can use more than that cgroup is allowed.Ending. Writing
1intocgroup.killkills every process in the cgroup and below it withSIGKILL, a signal that a process cannot catch or ignore; the kernel also catches processes being created at that moment. A cgroup with no live process and no child cgroup can be deleted by removing its directory, after which nothing can join it.
Who may move a process decides who can leave.
A process with full administrator rights (root) can write any PID into any
cgroup.procsfile, so it can move itself or any other process out of a job’s cgroup.Delegation hands a subtree to a less privileged user: an administrator gives that user write access to a cgroup directory and to its files
cgroup.procs,cgroup.threadsandcgroup.subtree_control. The user can then create child cgroups, move its own processes among them and divide the subtree’s resources. A writer without root rights can move a process only if it may write both the destination’scgroup.procsand that of the common ancestor, the nearest cgroup containing both source and destination. For a move into or out of the subtree, the common ancestor lies above the point of delegation, where the user has no write access, so the user can move processes only within the subtree. The user also cannot write the limit files of the delegated cgroup itself, so it cannot raise the limits that the parent set.Outside work. A process can ask another program to work for it: a system service, a container engine, a job scheduler, a remote login. The processes that do this work are created by that program and belong to its cgroup.
The rules above are those of version 2. In version 1, each controller can have its own tree, and a process belongs to one cgroup in each tree. The file /proc/1201/cgroup shows the cgroups of process 1201; a line that starts with 0:: gives its version 2 cgroup.
Worked example
A harness creates the cgroup run42, a directory under /sys/fs/cgroup. It starts a child process, which writes its own PID, 1200, into run42/cgroup.procs and only then runs the launcher program; this order guarantees that nothing the launcher starts is created outside the cgroup. The launcher creates workers 1201 and 1202, and worker 1201 creates helper 1203: 4 live members.
At 1 s the launcher, 1200, exits. The cgroup has live members and
populatedis 1. A check of PID 1200 alone would report the job finished.At 2 s worker 1202, which runs as root, writes its own PID into the
cgroup.procsfile of another cgroup,other. The job’s cgroup now has members, 1201 and 1203.At 3 s worker 1201 asks a system service to start a long computation. The service creates process 2000 in its own cgroup, never in
run42.At 5 s processes 1201 and 1203 exit. The cgroup has members,
populatedbecomes 0, and the directory can be removed.
At 5 s the job’s cgroup reports empty while 2 processes, 1202 and 2000, still do the job’s work. The cgroup reported its members correctly; the work had left it by two routes. If the job runs as an ordinary user inside a delegated subtree, the move at 2 s fails with a permission error, because the common ancestor of run42 and other lies above the point of delegation. The request at 3 s is not a cgroup matter: the job must be denied the service, or the service’s work must be tracked separately.
Stated precisely
Let be the set of live processes whose cgroup at time is or a cgroup below . Version 2 of the kernel interface guarantees three rules.
Inheritance: a process created by a member of cgroup starts in .
Explicit moves only: a process changes cgroup only when some process writes its PID into a
cgroup.procsfile. A writer without root rights succeeds only if it may write the destination’scgroup.procsand that of the common ancestor of source and destination.Population: the field
populatedof is 1 at time if and only if is not empty.
Let be the set of processes doing a job’s work at time . The cgroup contains the job, meaning at every time , if three conditions hold: the job’s first process joined before it created any process; no process able to move members out of does so, which is ensured when the job runs without root rights inside a delegated subtree; and the job hands no work to processes outside . If the three conditions hold, populated equal to 0 at time means that is empty.
Limits and common mistakes
On its own a cgroup is not a sandbox §2.30, an environment that confines what code can do. Its controllers limit how much the members use, not what they may read, write or contact. Linux can attach access checks to a cgroup, but those are separate mechanisms that must be set up on purpose.
An empty cgroup is a statement about processes on one machine. A command a member sent to a device or another machine, and work done by other services, can continue after populated reaches 0.
A limit is a cap, not a guarantee: cpu.max and memory.max bound what the group may use and promise nothing about what it will get.
Memory stays charged to the cgroup in which it was allocated, even after the process that allocated it moves to another cgroup.
The field populated describes one moment. Until the directory is removed, any process allowed to write its cgroup.procs can move a new process in.
Versions 1 and 2 follow different rules, and so does the threaded mode of version 2, in which the threads of one process can sit in different cgroups. Check which one applies before relying on any rule above.
Questions answered
Which descendants are members of the group and which can escape or be delegated?
Every process created by a member of the group is itself a member, at any depth, and stays a member after its creator exits, after it is adopted by another parent and after it starts a new session: in Linux cgroup (control group) version 2, a new process starts in its creator’s cgroup, and only an explicit move changes that. Processes that a member created before it joined the group are not members, so the first process must join before it starts the job.
A process escapes when some process writes its process identifier (PID) into the cgroup.procs file of another cgroup. A process with full administrator rights (root) can do that freely; a writer without root rights also needs write access to the cgroup.procs of the nearest cgroup containing both source and destination. Work also escapes, without any move, when a member asks another program, such as a system service, to do it.
A subtree is delegated by giving a less privileged user write access to its directory and to its files cgroup.procs, cgroup.threads and cgroup.subtree_control. The user can create child cgroups, move its processes among them and divide the subtree’s resources, but cannot move processes into or out of the subtree or raise the limits set above it. If a job runs as an ordinary user in a delegated subtree and its first process joins before it starts, every process of the job stays in the group; work it hands to other programs must still be tracked separately.
Related entries: systemd service versus scope (§13.21), Resource cap versus resource reservation (§13.22), Process closure (§13.23), Sandbox (§2.30). Further reading: [45].