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