cgroup: fix to break the while loop in cgroup_attach_task() correctly
Shridhar Rasal [Thu, 24 Oct 2013 03:07:51 +0000 (08:07 +0530)]
Both Anjana and Eunki reported a stall in the while_each_thread loop
in cgroup_attach_task().

It's because, when we attach a single thread to a cgroup, if the cgroup
is exiting or is already in that cgroup, we won't break the loop.

If the task is already in the cgroup, the bug can lead to another thread
being attached to the cgroup unexpectedly:

  # echo 5207 > tasks
  # cat tasks
  # echo 5207 > tasks
  # cat tasks
What's worse, if the task to be attached isn't the leader of the thread
group, we might never exit the loop, hence cpu stall. Thanks for Oleg's

This bug was introduced by commit 081aa458c38ba576bdd4265fc807fa95b48b9e79
("cgroup: consolidate cgroup_attach_task() and cgroup_attach_proc()")

Cc: <> # 3.9+
Reported-by: Eunki Kim <>
Reported-by: Anjana V Kumar <>
Signed-off-by: Anjana V Kumar <>
[ lizf: - fixed the first continue, pointed out by Oleg,
        - rewrote changelog. ]

Bug 1355677

Change-Id: Ifa356d9869e888a759c2be943cffd394a4102a9a
Signed-off-by: Li Zefan <>
Signed-off-by: Shridhar Rasal <>
Signed-off-by: Ajay Nandakumar <>
Reviewed-on: http://git-master/r/303135
Reviewed-by: Bharat Nihalani <>
Tested-by: Bharat Nihalani <>


index 770c43a..6111ef7 100644 (file)
@@ -1995,7 +1995,7 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
                /* @tsk either already exited or can't exit until the end */
                if (tsk->flags & PF_EXITING)
-                       continue;
+                       goto next;
                /* as per above, nr_threads may decrease, but not increase. */
                BUG_ON(i >= group_size);
@@ -2003,7 +2003,7 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
                ent.cgrp = task_cgroup_from_root(tsk, root);
                /* nothing to do if this task is already in the cgroup */
                if (ent.cgrp == cgrp)
-                       continue;
+                       goto next;
                 * saying GFP_ATOMIC has no effect here because we did prealloc
                 * earlier, but it's good form to communicate our expectations.
@@ -2012,6 +2012,7 @@ static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
                BUG_ON(retval != 0);
                if (!threadgroup)
        } while_each_thread(leader, tsk);