From: Shakeel Butt <shakeel.butt@linux.dev>
To: Tejun Heo <tj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Michal Koutny <mkoutny@suse.com>,
Michal Hocko <mhocko@kernel.org>,
Roman Gushchin <roman.gushchin@linux.dev>,
Christian Brauner <brauner@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
Suren Baghdasaryan <surenb@google.com>,
"T . J . Mercier" <tjmercier@google.com>,
Usama Arif <usama.arif@linux.dev>,
Meta kernel team <kernel-team@meta.com>,
cgroups@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Qingye Zhao <zhaoqingye@honor.com>,
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 [thread overview]
Message-ID: <20261130173425.4117363-2-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261130173425.4117363-1-shakeel.butt@linux.dev>
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 <tj@kernel.org>
Closes: https://lore.kernel.org/r/a6553d7921f3f47b7b890bfd232de644@kernel.org
Cc: Qingye Zhao <zhaoqingye@honor.com>
Cc: stable@vger.kernel.org
Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
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
next prev parent reply other threads:[~2026-11-30 17:35 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 17:34 [PATCH v2 00/11] cgroup: don't hold cgroup_mutex for cgroup.kill and cgroup.freeze Shakeel Butt
2026-11-30 17:34 ` Shakeel Butt [this message]
2026-11-30 17:34 ` [PATCH v2 02/11] cgroup: kill a child forked while cgroup.kill runs Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 03/11] cgroup/freezer: check own tasks and descendants for the frozen state Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 04/11] cgroup/freezer: don't update the frozen state of removed cgroups Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 05/11] cgroup/freezer: test the freeze trap bit under siglock Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 06/11] selftests/cgroup: test cgroup.procs reads across a failed migration Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 07/11] selftests/cgroup: test the frozen state of a cgroup with tasks and children Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 08/11] selftests/cgroup: test cgroup.kill in a threaded cgroup Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 09/11] cgroup: factor out cgroup_kn_break_active() and cgroup_kn_unbreak_active() Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 10/11] cgroup: don't hold cgroup_mutex for cgroup.kill Shakeel Butt
2026-11-30 17:34 ` [PATCH v2 11/11] cgroup/freezer: don't hold cgroup_mutex for cgroup.freeze Shakeel Butt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130173425.4117363-2-shakeel.butt@linux.dev \
--to=shakeel.butt@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=bigeasy@linutronix.de \
--cc=brauner@kernel.org \
--cc=cgroups@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=mkoutny@suse.com \
--cc=oleg@redhat.com \
--cc=roman.gushchin@linux.dev \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=tj@kernel.org \
--cc=tjmercier@google.com \
--cc=usama.arif@linux.dev \
--cc=zhaoqingye@honor.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®