Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.248 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060099; b=H1ywYctTjsy2t3XBeG+f5sPlGE4kJAK/HGIbuW8funiCmIr5aQnR+kt37PnYzl1vnEjZ AnPKXCciiQ8zUr095CIEjD6T6xk7NYwjwhoMeoX6wadGzSOb2Ns2bA9CaI+6NYAEOMl0B iX/1qP+w8/Wsew28WYdz/Rw4skZzG5DvmFM3ZbBWVq2ocWLhFKRulDvkqNLEVTaYKE9Kc HSjGeH7KtLtyoYr/J4OSlboXm+9VgNwXRj5pgB30VhShm+H+Y91cJXZShX2WT+r+lU1NN ee+h5Xee4rQFFoKuG7XjioTgZpbE+AMuaVPu5Ortj3g4uF6IIESPvrgO/5mqPO9F9FQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060099; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=lI0QGTSr+RgUMbKpcZjL6LDIC4cS/RxZWPWbqa/IPlg=; b=FuLIunE8ZOxAujWxkKTFTjIVlajzuwdTsoTRDTDtXgnchexBzVkXO5ymFF3AMwfma5Tu PWTd6h9tbY5cDEIx9OPP1ILYjGz5/Y7qq7fLYnFduMCR8AVzbY3MlX5ti2USGHFlPOnWt TgiqEIjAkR+xratMHTMl4iNUqT2yT1h52eI6ldkYe9C8YUprWPYB1E3BbkvcQ5uD3cG1H VAY+lJ4boLBffjVzcX8bJeNhqbz5+L5aqGOabwfkO1qEm4py9vFdqhCVtsfT+ebyANFHM JxDqg6qcjmCi148zHnJQRBDu5MPT+/ZQyrCUFOCvVUhp5H3KDI7QTUXXJcMSCQfnLqQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=JpSadZvd; arc=none smtp.remote-ip=91.218.175.248 Received: from mta0.migadu.com (out-248.mta0.migadu.com [91.218.175.248]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45F81606A31C for ; Mon, 30 Nov 2026 17:34:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45F81606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev 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=JpSadZvd X-Envelope-To: cgroups@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=QBrwHqF1TrFr4hydea0SF4Inw38h7cYRXtom+4neeks=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796060097; v=1; x=1796664897; b=JpSadZvdbtxqgEGTIjwL7WBk3pzbq2YPxfFmu3b3jS6WXo/fE6YTfIM+XmFdT4fD51BijW3r +S09l4aEA2a8TysjH5930P3qCE1b3fHmm9qI6bOH4TPuZBx3ZBiiNVg3UKEyXwUpr1IrZDeHG09 S8jPQc+3f7Z1FmsWJ4C57WAo= X-Envelope-To: cgroups@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 8a19095307bb73e1; Mon, 30 Nov 2026 17:34:57 +0000 X-Mizu-Trace-ID: 8a19095307bb73e1 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 v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Date: Mon, 30 Nov 2026 09:34:14 -0800 Message-ID: <20261130173425.4117363-1-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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-5 fix bugs found while auditing these paths and in the review of v1. They don't depend on the rest and can be taken first. Patches 1-4 are tagged for stable; patch 4 needs patch 3. 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. 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. 4. rmdir of a cgroup whose last task is still exiting breaks the ancestors' frozen counts, so they can stop reporting frozen=1. 5. cgroup_leave_frozen() tests the freeze trap bit without siglock. Patches 6 and 7 add selftests for bugs 1 and 3, and patch 8 a selftest for cgroup.kill in threaded cgroups, which must keep working. Patch 9 splits the kernfs active reference handling out of cgroup_kn_lock_live(). Patch 10 moves cgroup.kill off cgroup_mutex and patch 11 does the same for cgroup.freeze. Both walks rely on patch 1. Without cgroup_mutex, cgroup.kill (patch 10) 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. It is killed the way the walk would kill it: a pid namespace init is spared only if the cgroup.kill writer is inside its pid namespace. 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. A cgroup must be empty to switch, and cgroup.kill returns right away for an empty tree. Three behaviours change, and patch 10 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 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 11) 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 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-4: 1, 3 and 4 need no kernel changes, 2 needs a sleep added by a debug patch. Bug 5 was found by reading the code. The selftests in patches 6 and 7 fail without patches 1 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 and pid namespace inits that move. 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 v1, all from Tejun's review: - 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 6). - Patch 10: 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 10: 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 10: document the CLONE_INTO_CGROUP case. - Patch 11: 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 4: the stable tag names patch 3. - Rebased on cgroup/for-next 7da6eb0e0177. v1: https://lore.kernel.org/r/20261129183524.1052436-1-shakeel.butt@linux.dev Shakeel Butt (11): cgroup: don't lose a task in css_task_iter when a migration fails 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 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 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 | 10 +- include/linux/cgroup-defs.h | 24 +- include/linux/cgroup.h | 4 +- include/linux/sched/task.h | 1 + kernel/cgroup/cgroup.c | 425 ++++++++++++++---- kernel/cgroup/freezer.c | 232 ++++++---- tools/testing/selftests/cgroup/test_core.c | 154 +++++++ tools/testing/selftests/cgroup/test_freezer.c | 105 +++++ tools/testing/selftests/cgroup/test_kill.c | 68 +++ 9 files changed, 857 insertions(+), 166 deletions(-) base-commit: 7da6eb0e0177edff74d288f94511fa55155d8477 -- 2.53.1-Meta