From: Shakeel Butt <shakeel.butt@linux.dev>
To: Tejun Heo <tj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Koutny <mkoutny@suse.com>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Christian Brauner <brauner@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Suren Baghdasaryan <surenb@google.com>,
"T . J . Mercier" <tjmercier@google.com>,
Usama Arif <usama.arif@linux.dev>,
Meta kernel team <kernel-team@meta.com>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org
Subject: [PATCH 0/9] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze
Date: Sun, 29 Nov 2026 10:35:15 -0800 [thread overview]
Message-ID: <20261129183524.1052436-1-shakeel.butt@linux.dev> (raw)
cgroup.kill and cgroup.freeze take cgroup_mutex and hold it while they
walk every task of the subtree. cgroup.kill is how userspace OOM
killers, service managers and container runtimes stop a workload, often
when the system is already under pressure. Waiting for cgroup_mutex
makes the kill as slow as the slowest cgroup operation on the machine.
In the Meta fleet:
- Over the last ten days we recorded several hung-task reports of
tasks blocked for more than 120 seconds in cgroup_kill_write(),
waiting for cgroup_mutex. The writers were systemd, two userspace OOM
killers (oomd and systemd-oomd), buck2 (a build system) and our
container agent.
- In the reports that named the mutex owner, the owner was a
mkdir or rmdir that was itself waiting for kernfs_rwsem to add or
remove cgroup files. Two more were a mkdir reclaiming memory while it
allocated memcg state.
- Over four weeks, a D-state sampler saw cgroup.kill writers waiting
for cgroup_mutex for up to 61 minutes.
There is work to shorten the kernfs_rwsem waits, but cgroup.kill doesn't
need cgroup_mutex in the first place. It needs every task that is in the
subtree when the kill starts to get SIGKILL, including tasks that fork
or move while the kill runs. This series keeps that guarantee without
cgroup_mutex, and moves cgroup.freeze off cgroup_mutex as well. After
it, these writes don't wait for other cgroup operations (a freeze write
only waits for other freeze writes), and other cgroup operations don't
wait for a kill or freeze walk.
Patches 1-4 fix bugs found while auditing these paths. They don't depend
on the rest and can be taken first. Patches 1-3 are tagged for stable;
patch 3 needs patch 2.
1. A child forked while cgroup.kill walks its parent's cgroup can
survive the kill.
2. A cgroup with its own tasks and child cgroups reports frozen=1 while
part of its subtree still runs, and can stay frozen=1 after a thaw.
3. rmdir of a cgroup whose last task is still exiting breaks the
ancestors' frozen counts, so they can stop reporting frozen=1.
4. cgroup_leave_frozen() tests the freeze trap bit without siglock.
Patch 5 adds a selftest for bug 2, and patch 6 a selftest for
cgroup.kill in threaded cgroups, which must keep working. Patch 7 splits
the kernfs active reference handling out of cgroup_kn_lock_live().
Patch 8 moves cgroup.kill off cgroup_mutex and patch 9 does the same for
cgroup.freeze.
Without cgroup_mutex, cgroup.kill (patch 8) keeps its guarantees this
way:
- Fork: a child forked while a walk runs is killed, as in patch 1, with
counters so that several walks can overlap.
- Migration: a task that leaves a cgroup in a subtree being killed is
killed when the move commits, so moves out of the tree and inside it
can't escape. A task that moves in from outside may or may not be
killed; it arrived after the kill started.
- CLONE_INTO_CGROUP: a child placed outside the subtree by a parent
inside it is killed.
- Threaded cgroups: a cgroup can't switch to threaded while it is being
killed. Otherwise a thread from outside could join it, and killing
that thread would kill its whole process.
Two behaviours change, and patch 8 documents both:
- A process moved out of a tree while the tree is being killed now gets
SIGKILL during the move. Before, the move waited until the kill was
done.
- Writing "threaded" to cgroup.type fails with EBUSY while the cgroup
is being killed. Before, it waited until the kill was done. The
cgroup has to be empty to switch, so this needs a kill and a switch
that run at the same time.
cgroup.freeze (patch 9) gets a mutex of its own for writers. New cgroups
inherit the freeze state under css_set_lock, and each task is set to the
state of the cgroup it is in at that moment.
Testing, in an 8 CPU VM with lockdep:
- reproducers for bugs 1-3: 2 and 3 need no kernel changes, 1 needs a
sleep added by a debug patch. Bug 4 was found by reading the code.
The selftest in patch 5 fails without patch 2;
- race tests that widen each new window with sleeps from the same debug
patch. Each passes, and fails with its protection turned off;
- a random stress test (freeze, kill, migration, fork, clone3, mkdir
and rmdir), followed by checks that the tree freezes and empties;
- the test_freezer, test_kill and test_core selftests;
- KCSAN in strict mode, with no reports in the changed code.
Shakeel Butt (9):
cgroup: kill a child forked while cgroup.kill runs
cgroup/freezer: check own tasks and descendants for the frozen state
cgroup/freezer: don't update the frozen state of removed cgroups
cgroup/freezer: test the freeze trap bit under siglock
selftests/cgroup: test the frozen state of a cgroup with tasks and
children
selftests/cgroup: test cgroup.kill in a threaded cgroup
cgroup: factor out cgroup_kn_break_active() and
cgroup_kn_unbreak_active()
cgroup: don't hold cgroup_mutex for cgroup.kill
cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze
Documentation/admin-guide/cgroup-v2.rst | 9 +-
include/linux/cgroup-defs.h | 24 +-
include/linux/cgroup.h | 4 +-
include/linux/sched/task.h | 1 +
kernel/cgroup/cgroup.c | 347 ++++++++++++++----
kernel/cgroup/freezer.c | 208 +++++++----
tools/testing/selftests/cgroup/test_freezer.c | 105 ++++++
tools/testing/selftests/cgroup/test_kill.c | 68 ++++
8 files changed, 605 insertions(+), 161 deletions(-)
base-commit: 14dbe5fa6933de380a1e75dc5ad83c22eef70a0b
--
2.53.1-Meta
next reply other threads:[~2026-11-29 18:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 18:35 Shakeel Butt [this message]
2026-11-29 18:35 ` [PATCH 1/9] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-29 18:35 ` [PATCH 2/9] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-29 18:35 ` [PATCH 3/9] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-29 18:35 ` [PATCH 4/9] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-29 18:35 ` [PATCH 5/9] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-11-29 18:35 ` [PATCH 6/9] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-29 18:35 ` [PATCH 7/9] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-29 18:35 ` [PATCH 8/9] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-11-29 18:35 ` [PATCH 9/9] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129183524.1052436-1-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=brauner@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=tjmercier@google.com \
--cc=usama.arif@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®