Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
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: Re: [PATCH v3 00/12] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze
Date: Fri, 4 Dec 2026 10:54:15 -0800	[thread overview]
Message-ID: <axML9VzRlxntlPq2@linux.dev> (raw)
In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev>

Sorry, I forgot to mention in the cover letter, so writing it now. If we think
this series is too late for this cycle then we can delay the last three patches
and pickup the first 9 patches which are fixes and corresponding tests.

On Fri, Dec 04, 2026 at 10:40:29AM -0800, Shakeel Butt wrote:
> 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-6 fix bugs found while auditing these paths and in the
> reviews of v1. They don't depend on the rest and can be taken first.
> Patches 1-5 are tagged for stable; patch 5 needs patch 4.
> 
>  1. A failed migration can make a task walk miss a task that never left
>     the cgroup, for example a cgroup.procs reader.
>  2. A child forked while cgroup.kill walks its parent's cgroup can
>     survive the kill.
>  3. cgroup.kill of a cgroup below a threaded cgroup can kill processes
>     that live outside of it.
>  4. 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.
>  5. rmdir of a cgroup whose last task is still exiting breaks the
>     ancestors' frozen counts, so they can stop reporting frozen=1.
>  6. cgroup_leave_frozen() tests the freeze trap bit without siglock.
> 
> Patches 7 and 8 add selftests for bugs 1 and 4. Patch 9 tests cgroup.kill
> in threaded cgroups, which must keep working, and below them (bug 3).
> Patch 10 splits the kernfs active reference handling out of
> cgroup_kn_lock_live(). Patch 11 moves cgroup.kill off cgroup_mutex and
> patch 12 does the same for cgroup.freeze. Both walks rely on patch 1.
> 
> Without cgroup_mutex, cgroup.kill (patch 11) keeps its guarantees this
> way:
> 
>  - Fork: a child forked while a walk runs is killed, as in patch 2, 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.
>  - pid namespaces: a task killed by a move or a fork is killed the way
>    the walk would kill it. The walk sends SIGKILL from the writer, so a
>    pid namespace init is spared only if the cgroup.kill writer is inside
>    its pid namespace.
>  - Threaded cgroups: a cgroup can't switch to threaded while it, or a
>    cgroup below it, is being killed. Otherwise a thread from outside
>    could join the tree, and killing that thread would kill its whole
>    process. A cgroup must be empty to switch, and cgroup.kill returns
>    right away for an empty tree. cgroup.kill refuses threaded cgroups and
>    the cgroups below them, as patch 3 does with cgroup_mutex.
> 
> Three behaviours change, and patch 11 documents them:
> 
>  - 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.
>  - A process in the tree that creates a child outside it with
>    CLONE_INTO_CGROUP gets a child that is killed right away. Before, the
>    fork waited until the kill was done, and then failed.
>  - Writing "threaded" to cgroup.type fails with EBUSY while the cgroup,
>    or a cgroup below it, 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 12) gets a mutex of its own for writers. New
> cgroups inherit the freeze state under css_set_lock. The walk sets the
> new state of every cgroup in the tree before it walks the tasks, and
> then walks only the cgroups whose state it changed and that have tasks.
> 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-5: 1, 3, 4 and 5 need no kernel changes, 2
>    needs a sleep added by a debug patch. Bug 6 was found by reading the
>    code. The new selftests in patches 7, 8 and 9 fail without patches 1,
>    4 and 3;
>  - Tejun's reproducers from the review of v1 pass;
>  - race tests that widen each new window with sleeps from the same debug
>    patch, including migrations that fail, pid namespace inits that move
>    or create threads in cgroups the walk has passed, an ancestor that
>    switches to threaded, and cgroups created below a subtree the freeze
>    walk skipped. Each passes, and fails with its protection turned off;
>  - a random stress test (freeze, kill, migrations that succeed and fail,
>    fork, clone3, pid namespace inits, 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.
> 
> Changes since v2, from the reviews by Sashiko and Andrew Morton, and
> Andrew's testing of v2 on a 64-CPU guest:
>  - New patch 3 (Sashiko, Andrew): cgroup.kill below a threaded cgroup
>    could kill processes that live outside of it. Mainline has this too,
>    so it is a separate fix for stable, with a selftest in patch 9.
>  - Patch 11 (Sashiko, Andrew): a switch to threaded also fails with
>    EBUSY while a cgroup below it is being killed.
>  - Patch 11 (Sashiko): the fork path decides whether its SIGKILL is
>    forced the way the walk does, by the cgroup.kill writer.
>  - Patch 12 (Andrew): the task pass skips branches without tasks, and
>    the state pass updates their frozen state. With 1,024 empty children,
>    a freeze or thaw write takes a median of 183 us, against 416 us in v2
>    and 273 us before the series.
>  - Patch 12 (Sashiko): the task pass walks only the cgroups whose state
>    the state pass changed.
>  - Patch 8 (Sashiko, Andrew): check cg_name() results before using them.
>  - Patch 1: Sashiko reported the failed migration bug on v1 as well.
>  - Rebased on cgroup/for-next 7e16ab5a117a.
> 
> Changes since v1, from Tejun's review (patch numbers as in v3):
>  - New patch 1: a failed migration could make the kill and freeze walks
>    skip a task. The bug is in css_task_iter and mainline has it since
>    5ee01f1a7343, so it is a separate fix for stable, with a selftest
>    (patch 7).
>  - Patch 11: a task that leaves a tree being killed is killed the way
>    the walk would kill it, so the cgroup.kill writer's pid namespace
>    decides whether a pid namespace init dies.
>  - Patch 11: cgroup.kill returns 0 right away for an empty tree, and
>    CGRP_THREADED is set only when a switch to threaded succeeds, so a
>    switch that fails can't make cgroup.kill fail with EOPNOTSUPP.
>  - Patch 11: document the CLONE_INTO_CGROUP case.
>  - Patch 12: set the state of the whole tree before walking the tasks,
>    so a frozen task moved ahead of the walk doesn't run again.
>  - Patch 5: the stable tag names patch 4.
> 
> v2: https://lore.kernel.org/r/20261130173425.4117363-1-shakeel.butt@linux.dev
> v1: https://lore.kernel.org/r/20261129183524.1052436-1-shakeel.butt@linux.dev
> 
> Shakeel Butt (12):
>   cgroup: don't lose a task in css_task_iter when a migration fails
>   cgroup: kill a child forked while cgroup.kill runs
>   cgroup: refuse cgroup.kill below a threaded cgroup
>   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 cgroup.procs reads across a failed migration
>   selftests/cgroup: test the frozen state of a cgroup with tasks and
>     children
>   selftests/cgroup: test cgroup.kill in and below 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       |  17 +-
>  include/linux/cgroup-defs.h                   |  30 +-
>  include/linux/cgroup.h                        |   4 +-
>  include/linux/sched/task.h                    |   1 +
>  kernel/cgroup/cgroup.c                        | 500 +++++++++++++++---
>  kernel/cgroup/freezer.c                       | 249 ++++++---
>  tools/testing/selftests/cgroup/test_core.c    | 154 ++++++
>  tools/testing/selftests/cgroup/test_freezer.c | 108 ++++
>  tools/testing/selftests/cgroup/test_kill.c    | 127 +++++
>  9 files changed, 1023 insertions(+), 167 deletions(-)
> 
> 
> base-commit: 7e16ab5a117a40032257a6bdd8e47e9cdacf4dca
> -- 
> 2.53.1-Meta
> 


      parent reply	other threads:[~2026-12-04 18:54 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 18:40 [PATCH v3 00/12] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 01/12] cgroup: don't lose a task in css_task_iter when a migration fails Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 02/12] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 03/12] cgroup: refuse cgroup.kill below a threaded cgroup Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 04/12] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 05/12] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 06/12] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 07/12] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 08/12] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 09/12] selftests/cgroup: test cgroup.kill in and below a threaded cgroup Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 10/12] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 11/12] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-12-04 18:40 ` [PATCH v3 12/12] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
2026-12-04 18:54 ` Shakeel Butt [this message]

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=axML9VzRlxntlPq2@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