From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.191 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796409662; b=k3gH+rcpwdEy2FvNhhcq3UhPsfJC5EtI66G0pNHTi8pa7HhGpavslH3eXSeYLSJD1HVC DP6mn4oGgYQ9cuwsnsLGp1WZhRV9jjpfouqnCTw8KDnzW2GaA3Gba0fI+5IyTzQl+cz/9 4JnXiBPa9fw5pFyoTyB/tZcrXP2/jD7B3IA5CLQ124rbn393be/WTBpyh3KLCymp1/jpb 0eIkluZ/H+RojCYaYAxb4+Lb+K0RpndUy+bqt0VPr2Z1xWFOr9OqfY2Q+7EafmWytNcTV ZutL1Jj9kf0Q2c7ntsePKf63HLJMqB/lvRqiQfWxnHhL0eOnXl+UlimLuIpKLhEemBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409662; 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:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=T4UihkWZaDcs+U906egwtP3WYwuzK697wuTSbAu8lvk=; b=3aHIjJF5ltHWyei+5wyNDamAb+i3sI4/GWbRPqVkq/UqwTHJekteATQNBkr8mhWVQXog RLn2W674cpuFn1vmQGwx/IFGHSHdOqmr3XdjgKNudu+7UCgvPr2DVPH0+uApyAOnkuqIX rftyZl2jPjNnKBoaeXdL2+w63nRLnJ+oe2eHNiOLoNjMFmF6oztIdgwaP0GImg+sWJmRt kZzGpk6Mqhh1k0elUYdwkpCQJ5bqWxWTDBxlQ13hCqHmiu9FyqEBM5QC65kXUthaK9oWD Fvb29M4UcALpzxr2wXW9rQUNPwzhyGzKdN3by9qqnQjeYSeXnLTHyHmqTF8745DCm9A== 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=wRPXnmuS; arc=none smtp.remote-ip=95.215.58.191 Received: from mta1.migadu.com (out-191.mta1.migadu.com [95.215.58.191]) (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 29DCE606A693 for ; Fri, 4 Dec 2026 18:41:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29DCE606A693 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=wRPXnmuS X-Envelope-To: stable@vger.kernel.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: stable@vger.kernel.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-Mailing-List: stable@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 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