History

cgroups: don't attach task to subsystem if migration failed

If a task has exited to the point it has called cgroup_exit() already, then we can't migrate it to another cgroup anymore. This can happen when we are attaching a task to a new cgroup between the call to ->can_attach_task() on subsystems and the migration that is eventually tried in cgroup_task_migrate(). In this case cgroup_task_migrate() returns -ESRCH and we don't want to attach the task to the subsystems because the attachment to the new cgroup itself failed. Fix this by only calling ->attach_task() on the subsystems if the cgroup migration succeeded.

Ben Blum committed 15 years ago

Commit 77ceab81 parent 33ef6b61 file · +9 −6Browse files

Unified · Split

kernel/cgroup.c+9−6
@@ -2135,14 +2135,17 @@ int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader) · 2134 unmodified lines
21352135 oldcgrp = task_cgroup_from_root(tsk, root);
21362136 if (cgrp == oldcgrp)
21372137 continue;
2138− /* attach each task to each subsystem */
2139− for_each_subsys(root, ss) {
2140− if (ss->attach_task)
2141− ss->attach_task(cgrp, tsk);
2142− }
21432138 /* if the thread is PF_EXITING, it can just get skipped. */
21442139 retval = cgroup_task_migrate(cgrp, oldcgrp, tsk, true);
2145− BUG_ON(retval != 0 && retval != -ESRCH);
2140+ if (retval == 0) {
2141+ /* attach each task to each subsystem */
2142+ for_each_subsys(root, ss) {
2143+ if (ss->attach_task)
2144+ ss->attach_task(cgrp, tsk);
2145+ }
2146+ } else {
2147+ BUG_ON(retval != -ESRCH);
2148+ }
21462149 }
21472150 /* nothing is sensitive to fork() after this point. */
21482151