Skip to contentSkip to conversation

cgroup: fix race between task migration and iteration

PATCH Applied zhaoqingye 3 participants 3 messages Feb 11 – Feb 12 cgroup

lore
Share & tools
lore

zhaoqingye

author
#0 lore raw

PATCH 1/1 cgroup: fix race between task migration and iteration

When a task is migrated out of a css_set, cgroup_migrate_add_task() first moves it from cset->tasks to cset->mg_tasks via:

    list_move_tail(&task->cg_list, &cset->mg_tasks);

If a css_task_iter currently has it->task_pos pointing to this task, css_set_move_task() calls css_task_iter_skip() to keep the iterator valid. However, since the task has already been moved to ->mg_tasks, the iterator is advanced relative to the mg_tasks list instead of the original tasks list. As a result, remaining tasks on cset->tasks, as well as tasks queued on cset->mg_tasks, can be skipped by iteration.

Fix this by calling css_set_skip_task_iters() before unlinking task->cg_list from cset->tasks. This advances all active iterators to the next task on cset->tasks, so iteration continues correctly even when a task is concurrently being migrated.

This race is hard to hit in practice without instrumentation, but it can be reproduced by artificially slowing down cgroup_procs_show(). For example, on an Android device a temporary /sys/kernel/cgroup/cgroup_test knob can be added to inject a delay into cgroup_procs_show(), and then:

1) Spawn three long-running tasks (PIDs 101, 102, 103).
2) Create a test cgroup and move the tasks into it.
3) Enable a large delay via /sys/kernel/cgroup/cgroup_test.
4) In one shell, read cgroup.procs from the test cgroup.
5) Within the delay window, in another shell migrate PID 102 by writing it to a different cgroup.procs file.

Under this setup, cgroup.procs can intermittently show only PID 101 while skipping PID 103. Once the migration completes, reading the file again shows all tasks as expected.

Note that this change does not allow removing the existing css_set_skip_task_iters() call in css_set_move_task(). The new call in cgroup_migrate_add_task() only handles iterators that are racing with migration while the task is still on cset->tasks. Iterators may also start after the task has been moved to cset->mg_tasks. If we dropped css_set_skip_task_iters() from css_set_move_task(), such iterators could keep task_pos pointing to a migrating task, causing css_task_iter_advance() to malfunction on the destination css_set, up to and including crashes or infinite loops.

The race window between migration and iteration is very small, and css_task_iter is not on a hot path. In the worst case, when an iterator is positioned on the first thread of the migrating process, cgroup_migrate_add_task() may have to skip multiple tasks via css_set_skip_task_iters(). However, this only happens when migration and iteration actually race, so the performance impact is negligible compared to the correctness fix provided here.

---
 kernel/cgroup/cgroup.c | 1 +
 1 file changed, 1 insertion(+)
kernel/cgroup/cgroup.c +1 -0
index 5f0d33b04910..a34d46c50194 100644
@@ -2608,6 +2608,7 @@ static void cgroup_migrate_add_task(struct task_struct *task,
 
 	mgctx->tset.nr_tasks++;
 
+	css_set_skip_task_iters(cset, task);
 	list_move_tail(&task->cg_list, &cset->mg_tasks);
 	if (list_empty(&cset->mg_node))
 		list_add_tail(&cset->mg_node,
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Hi Qingye.

On Wed, Feb 11, 2026 at 09:24:04AM +0000, zhaoqingye <[email protected]> wrote:
...

Quoted 3 lines

Under this setup, cgroup.procs can intermittently show only PID 101 while skipping PID 103. Once the migration completes, reading the file again shows all tasks as expected.

Yup, such a skip is buggy -- at places when task is removed from task->cg_list's list, the iterators should be skipped.

Quoted 2 lines

Note that this change does not allow removing the existing css_set_skip_task_iters() call in css_set_move_task().

Sure, css_set_move_task() isn't called together with cgroup_migrate_add_task() under one css_set_lock.

Quoted 5 lines

The race window between migration and iteration is very small, and css_task_iter is not on a hot path. In the worst case, when an iterator is positioned on the first thread of the migrating process, cgroup_migrate_add_task() may have to skip multiple tasks via css_set_skip_task_iters().

Only when it->task_pos == &task->cg_list (in css_task_iter_skip()).

Quoted 3 lines

However, this only happens when migration and iteration actually race, so the performance impact is negligible compared to the correctness fix provided here.

Of course, correctness > performance in these discrete cases.

This is a good catch, well described and correction is OK.

maintainer Applied
#2 lore raw
In reply to zhaoqingye

Applied to cgroup/for-7.0-fixes with the following Fixes tag added.

Fixes b636fd38dc40 ("cgroup: Implement css_task_iter_skip()")

Thanks.

Signature · 1 line
tejun