From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.1 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060102; b=S61DyqXg98wtf4fQmPJwP8HYNF7STDlXN1vXh4NY+QcbIrRpirkv/i3zf0krtej0EscZ psfdp7S4nmhLroNK/TC82HoUqjOI3ZdbgqGSGo5A4iL9p2STv6i3GG3lZZikmRUrugepu ++ie0RQu6K4yQupbK5hhXAEcFXau2TqDf6xFtV2j/e9DH4lFcGgDhAlGvOyA/qn8J+AHY ASu1zHUkwRzA9zkARgXz0RTZsQ6fWgDUssGn+xoQxl3zvSGFPx+lBGaJfm3FqK6CHUs0V OdCEhvCCHugrl58X3LIixixIpaU6wd+oGi9GOBB0pvGNVbTTc2US7gIu7TyljvXiJgA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060102; 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=q6xsCXh3Edo3yHP608x4QQK5aJkHu1gEW/cnYWfUH0Y=; b=1luCShaMgp+NUUBPdXNEUQv+1lPWkmRefIiUtw3X2mN46JSl7p3SygCO8YSxMv70/C7f qwwhFlT8fYAxDHFdFSMbyh7rW1xzo+6+FXRjnfF6iHnN+widkEKB6dc8+Ivay5a2A4PxV 562elquSBJYsfPIS/FJ3s5b2cwYydOwFHCpxKovYgbMYmRwCxlkSGob1UFk3xVxVSIS8A nu2UyP3YevkdO4Qv+7m2oMVRA37r9FU8+NrDC4nH9ySmvDoLCn6Jl5g7mrzSU3j84Ujfv lDdEiXVbKjY4EVO+/FYcudGG15A/qsRcOtRuUtnyzMBESBhH6xM18F+kKPGOIS3CoCw== 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=AwD2eHFy; arc=none smtp.remote-ip=91.218.175.1 Received: from mta0.migadu.com (out-1.mta0.migadu.com [91.218.175.1]) (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 21B04606A680 for ; Mon, 30 Nov 2026 17:35:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 21B04606A680 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=AwD2eHFy X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=Xf6FwqTBTwzDWq80cX8utrMecRH7Qvzoi6uT3XIvuKQ=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796060100; v=1; x=1796664900; b=AwD2eHFycuVFTfDMDiyd46jBvIh+YTUOnXroXcvsXPznnQKtfp7tqZmZRAlWmSD+WLqGs+Jq R7o6cZm1ZmI6yPRiQqtOv99SkEz2KEjgxoFyOLUXQeT21QtPBtU5QveVsYAMFVsW6ppsSRRMCkJ kW6XODZ39GDnE544Iq/hKuHA= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 5b1c4136f0b55a03; Mon, 30 Nov 2026 17:34:59 +0000 X-Mizu-Trace-ID: 5b1c4136f0b55a03 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, Qingye Zhao , stable@vger.kernel.org Subject: [PATCH v2 01/11] cgroup: don't lose a task in css_task_iter when a migration fails Date: Mon, 30 Nov 2026 09:34:15 -0800 Message-ID: <20261130173425.4117363-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev> References: <20261130173425.4117363-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 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