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 879BB60A2618 for ; Fri, 4 Dec 2026 18:41:11 +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=wRPXnmuS Received: by kanga.kvack.org (Postfix) id 6DC0F6B05B4; Fri, 4 Dec 2026 13:41:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 68C9B6B05B5; Fri, 4 Dec 2026 13:41:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57CBB6B05B6; Fri, 4 Dec 2026 13:41:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 231006B05B4 for ; Fri, 4 Dec 2026 13:41:04 -0500 (EST) Received: from smtpin18.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9D7351C3CA9 for ; Fri, 4 Dec 2026 18:41:03 +0000 (UTC) X-FDA: 85507167606.18.C00149F Received: from mta0.migadu.com (out-10.mta0.migadu.com [91.218.175.10]) by imf18.hostedemail.com (Postfix) with ESMTP id 8A70A1C0004 for ; Fri, 4 Dec 2026 18:41:01 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wRPXnmuS; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.10 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=1796409661; b=kvEBDci7bFOMl85tnnbuVAsZdGPCViaFou5pBEgjSW1mQE1+pFg06Ls5TSHSjByZ8ib9fR f4A+4wsn6VkA34hOBwsBeD1JtJN3KMP5s1EukXFgUCgAhOtBTu0kdQq4nhaWzdACf8srn2 xnhEhoqr8dXgJ7x99tBCwBWa+Pjrgng= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796409661; 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:in-reply-to:references:references:dkim-signature; bh=T4UihkWZaDcs+U906egwtP3WYwuzK697wuTSbAu8lvk=; b=N0CxNHoswM23Qt0mGoV+ljpgaFOZAkU4Vt4AlVs4KySQFY1znQiUGjGJDDHlIzy/Npg8ze U6U7YRxEss9l920pPR7EXBR2QUkwExbZYUarIWDxYQMXSH8R/dtIyNyrZQU8kwG/WPzUr+ 48r32FigBTQzKb3ol+dav3joLy6N4II= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wRPXnmuS; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf18.hostedemail.com: domain of shakeel.butt@linux.dev designates 91.218.175.10 as permitted sender) smtp.mailfrom=shakeel.butt@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=LxH173pq4dXpbXBbiY5zy9sXzRH2WBngH7lOBvQ/NJM=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796409660; v=1; x=1797014460; b=wRPXnmuSv+AL1CEig7FnpY+Y4MBhDIse/jzuLFpdAPukr2IiPYzMFD9ng9NEn8tQAqhIbICq ZYUIPA3SuiBCadB/sy3S7pqG1ZCkLhViQjm71IsMHh8Hn9rttXePwkZTOk4h6uABXk8TDQ2iehE CzCsKlSt4OuKuA2SCRKcapkc= X-Envelope-To: linux-mm@kvack.org Received: by smtp.migadu.com with ESMTPS id 2c4c30dd6063a274; Fri, 04 Dec 2026 18:40:59 +0000 X-Mizu-Trace-ID: 2c4c30dd6063a274 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, Sashiko , Qingye Zhao , stable@vger.kernel.org Subject: [PATCH v3 01/12] cgroup: don't lose a task in css_task_iter when a migration fails Date: Fri, 4 Dec 2026 10:40:30 -0800 Message-ID: <20261204184041.1489976-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261204184041.1489976-1-shakeel.butt@linux.dev> References: <20261204184041.1489976-1-shakeel.butt@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam02 X-Rspam-User: X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: y8rtn99hpmbhd45wt9ahkzir719a6zck X-Rspamd-Queue-Id: 8A70A1C0004 X-HE-Tag: 1796409661-181308 X-HE-Meta: U2FsdGVkX18Ey87DMpAp9a3+32ZSEFub2DFCnxP0bDnYHZ5V4Euhjwgv/jm+tJlPoPl9i+DKj1w++7Nh2EIDXtOf5UDq77gpekXr1vOLmACLg8leXLHjO/2Xg/yyjlGDDFtBLCmvx2xoQXNPii2zP86hxk77air9MDx0/MVDHNoEeBprHTxMPxFtoyLjr8n3Z53rhU20aWZnRS9+ED+QkkNYKnk5CN56Xq05/noYNOem0NN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P5/aJI8WrMWMhzXjtMM7r+MP9Idgh2KbtKo56g12sSRtMfDpSbhLIQT43n2Vma+uyqCQr0w+wcQaHFdw9vwCev9QJrbL+3uXCYFu7aVafUooIMjJlAg7MP7z0siHHC3Hyng94D8d9IYU2ZHnMtcXx5Z07Hr7gYczDpica2D1p9wnr0AwmEDBX8KWEIhCZpbgluAcBc4rwAl+ItjsJycIEhl59mTW1NdhtInn3UtAi4W4JY8O2q3V8lg 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 A cgroup.procs reader can miss a process that never left the cgroup. With processes 148, 150 and 151 in src: read one byte of src/cgroup.procs echo 151 > empty/cgroup.procs fails with ENOSPC read the rest "148 150", 151 is missing Since commit 5ee01f1a7343 ("cgroup: fix race between task migration and iteration"), cgroup_migrate_add_task() skips an iterator past a task before it moves the task to mg_tasks. If the task was the last one on ->tasks, the iterator stops at the head of ->tasks and goes to mg_tasks next. If the migration then fails, out_release_tset splices mg_tasks back onto the tail of ->tasks. The iterator finds mg_tasks empty and moves on to the next css_set, so it never returns the task. Any task walk that doesn't hold cgroup_mutex can miss a task this way. cgroup.procs reads are the easiest case, because they keep the iterator between reads. A ->can_attach() callback can fail the migration, for example cpuset's for a cgroup without CPUs. Point such an iterator at the first task that is moved back. The iterator can then return a task twice, which a migration to a css_set it hasn't walked yet can already cause. The selftest added later in this series fails without this patch. Fixes: 5ee01f1a7343 ("cgroup: fix race between task migration and iteration") Reported-by: Tejun Heo Closes: https://lore.kernel.org/r/a6553d7921f3f47b7b890bfd232de644@kernel.org Reported-by: Sashiko Closes: https://lore.kernel.org/r/sashiko-outbox-241838@kernel.org Cc: Qingye Zhao Cc: stable@vger.kernel.org Signed-off-by: Shakeel Butt --- kernel/cgroup/cgroup.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index f6df19241772..a5a7395dc5b8 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -865,6 +865,35 @@ static void css_set_skip_task_iters(struct css_set *cset, css_task_iter_skip(it, task); } +/** + * css_set_splice_mg_tasks - move the tasks on a css_set's mg_tasks to its tasks + * @cset: css_set of interest + * + * The tasks go to the tail of @cset->tasks. An iterator that is done with + * @cset->tasks and is about to move on to @cset->mg_tasks would miss them. + * This happens when cgroup_migrate_add_task() skipped the iterator past the + * last task on @cset->tasks and the migration then failed. Point such an + * iterator at the first task that is moved. It may return tasks that it + * returned before, as it may when a task migrates to a css_set it hasn't + * walked yet. + */ +static void css_set_splice_mg_tasks(struct css_set *cset) +{ + struct css_task_iter *it; + + lockdep_assert_held(&css_set_lock); + + if (list_empty(&cset->mg_tasks)) + return; + + /* only a skipped iterator stops at the head, see css_task_iter_skip() */ + list_for_each_entry(it, &cset->task_iters, iters_node) + if (it->task_pos == &cset->tasks) + it->task_pos = cset->mg_tasks.next; + + list_splice_tail_init(&cset->mg_tasks, &cset->tasks); +} + /** * css_set_move_task - move a task from one css_set to another * @task: task being moved @@ -2851,7 +2880,7 @@ static int cgroup_migrate_execute(struct cgroup_mgctx *mgctx) spin_lock_irq(&css_set_lock); list_splice_init(&tset->dst_csets, &tset->src_csets); list_for_each_entry_safe(cset, tmp_cset, &tset->src_csets, mg_node) { - list_splice_tail_init(&cset->mg_tasks, &cset->tasks); + css_set_splice_mg_tasks(cset); list_del_init(&cset->mg_node); } spin_unlock_irq(&css_set_lock); -- 2.53.1-Meta