freenode
Kernel & Low-Level

Linux cgroup kill and freeze stop blocking on the cgroup lock

Meta saw writers stuck for up to an hour behind mkdir and rmdir; the series also fixes fork and freezer races found while the paths were audited.

Linux is set to stop holding the global cgroup lock while cgroup.kill and cgroup.freeze walk every task in a subtree, after Meta recorded those writes stalling for minutes, and in one case more than an hour, on production machines.

Shakeel Butt wrote that cgroup.kill is how userspace OOM killers, service managers, and container runtimes stop a workload, often when the system is already under pressure. Waiting on cgroup_mutex made the kill as slow as the slowest other cgroup operation on the machine. Over ten days, Meta logged hung-task reports of writers blocked for more than 120 seconds in the kill path. The writers were systemd, the userspace OOM killers oomd and systemd-oomd, the buck2 build system, and Meta's container agent. Where the reports named the lock owner, it was a mkdir or rmdir itself waiting to add or remove cgroup files, or a mkdir reclaiming memory while it allocated memory-controller state. A four-week sampler of uninterruptible tasks saw kill writers waiting on the same lock for up to 61 minutes.

The kill does not need that lock. It needs every task that is in the subtree when the kill starts to receive SIGKILL, including tasks that fork or move while the walk runs. The series keeps that guarantee without cgroup_mutex, and takes freeze off the same lock. Afterward, these writes do not wait for other cgroup operations (a freeze write only waits for other freeze writes), and other cgroup operations do not wait for a kill or freeze walk.

Auditing the paths turned up bugs that can land first, three of them marked for stable. A child forked while cgroup.kill walks its parent's cgroup can survive the kill: an earlier fix missed a window between stepping the iterator and sending SIGKILL, and preemption can stretch that window. Butt reproduced it in a virtual machine; the child survived in five of five runs, and in none of five with the fix. A cgroup that has both its own tasks and child cgroups can report frozen while part of the subtree still runs, and can stay frozen after a thaw. Removing a cgroup whose last task is still exiting can leave ancestors' frozen counts one too high, so a freezing ancestor reports frozen too early or never. A fourth, unreproduced, bug is a few instructions wide: a task leaving the frozen state can return to user space while it is still counted as frozen if it observes a freeze trap bit without the signal lock.

The kill and freeze rewrites replace the global lock with narrower rules: pin the cgroup so removal does not wait on the write, walk under RCU, and kill tasks that leave a subtree while a kill is in progress. Tasks that move in may or may not be killed. Freeze writers serialize only with each other.