From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BA4C0606A336 for ; Sun, 29 Nov 2026 18:35:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=Eeyt9YfF Received: by kanga.kvack.org (Postfix) id D24EC6B05D8; Sun, 29 Nov 2026 13:35:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CD5606B05D9; Sun, 29 Nov 2026 13:35:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BEB916B05DA; Sun, 29 Nov 2026 13:35:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 921D96B05D8 for ; Sun, 29 Nov 2026 13:35:47 -0500 (EST) Received: from smtpin24.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4A52A1208BE for ; Sun, 29 Nov 2026 18:35:46 +0000 (UTC) X-FDA: 85489010292.24.3BFEB19 Received: from mta0.migadu.com (out-199.mta0.migadu.com [91.218.175.199]) by imf25.hostedemail.com (Postfix) with ESMTP id 42B55A0003 for ; Sun, 29 Nov 2026 18:35:44 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Eeyt9YfF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.199 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1795977344; b=hdgQsgk2s1HpF+mHS1JchAF9B1l22WqkaxveSrj0uciB/KW208TYEB4nqGzd0PO/IzEbxl urF9mThd0pqmC0tGeNYq8cRW/NFMPPT3tJTbmRWrkuYdZ8LKkBjE1dQvXHtnz1WNc849Xp wRzkjXMBp9t3PKBjF+nhR6FcBs3fdI0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1795977344; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=WdL0J+Ctoty+olyFevv3/aSFlnta5DaAkoupcjtvgmA=; b=RGQlfw2pTiOZAvzW/Z2tfj150/zirai8+cge3BP6JYT1FjfEPfUhQ3Ki4GKkWOFBsdc79Q m7Vj2mZW/ZuIm/cwQH5Hd+iYm/2poFOkLXWWu0lKYvNOnOug+OwmgIYe5cYJTUBNWmLn7l MI/bVUp8fw620sKq7CniRIcgfhCFJ2Q= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Eeyt9YfF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf25.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.199 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=llK4NWkxp4QpkmX6UxNmzC56pC1zEryRHJN1xeF2aZc=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795977342; v=1; x=1796582142; b=Eeyt9YfFdN0kb1rJx9IjO+r+8wSt7LUfxbMcu0ffop1Heq+T90eyuC1taIK/Gg/609r43s9M /d6VItUtQESlXASTBdVl+L9O7suJyxcvQS25T0EzD15z8yhjJlfHi7CPA+LFs6pLPWgc+s3LPNv NszhpO8BuZBHlCtpBZoG7yiQ= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 62e7a7f4c71fdb8b; Sun, 29 Nov 2026 18:35:35 +0000 X-Mizu-Trace-ID: 62e7a7f4c71fdb8b X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Tejun Heo Cc: Andrew Morton , Johannes Weiner , Michal Koutny , Michal Hocko , Roman Gushchin , Christian Brauner , Oleg Nesterov , Sebastian Andrzej Siewior , Suren Baghdasaryan , "T . J . Mercier" , Usama Arif , Meta kernel team , 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 Message-ID: <20261129183524.1052436-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 7h9d89umzj9jedbobf15fbzu88erisni X-Rspamd-Queue-Id: 42B55A0003 X-Rspam-User: X-Rspamd-Server: rspam03 X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1795977344-240904 X-HE-Meta: U2FsdGVkX19UYkL/rscIATOrUk9wmX2dJnfdnKoz0/cq2SQpGrNrNzbRYDTil00oNdLexjoE1t1oD1yb/VELZI2mTyfEWdSH1Hhh2Ry86fl+kI1VF2Hri7GEpVk/OFB1FsM/kerZ9mNJlYRsd+6uzK9elCj5KPVhsRVakcATtihWmxmAkINKd/jPexFjtv1iRpYLNLjJSgz43BkCCp5PL4vFES00aMzUnKfhq0DH0GLQ8bnPYTko0P2Su+TiMOy8D5DsSLmtbIT7tkWgQ0IOLGiku//7ZuIKgwVlTK5OA4kzuPOK6hC5a1wGLf/Ie4PetmlJgycDvbIqnw4R3TDevwcu9f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/A/zNWyBw5HX00irMQuKLMjWq5cvsn+ZA5NMcj8RZ4EveZmNRHX5JUL2aQ= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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