aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/cgroup.c
diff options
context:
space:
mode:
authorTejun Heo <tj@kernel.org>2014-03-19 17:43:21 -0400
committerTejun Heo <tj@kernel.org>2014-03-19 17:43:21 -0400
commit1b9aba49eab5e85b0d3de8ba630cda6d68546297 (patch)
tree189d5d24c13399c346e50e1d3be40dbc59ec98e4 /kernel/cgroup.c
parent8cbbf2c972c4444cad36f61cd571714c39b8cf04 (diff)
cgroup: fix cgroup_taskset walking order
cgroup_taskset is used to track and iterate target tasks while migrating a task or process and should guarantee that the first task iterated is the task group leader if a process is being migrated. b3dc094e9390 ("cgroup: use css_set->mg_tasks to track target tasks during migration") replaced flex array cgroup_taskset->tc_array with css_set->mg_tasks list to remove process size limit and dynamic allocation during migration; unfortunately, it incorrectly used list operations which don't preserve order breaking the guarantee that cgroup_taskset_first() returns the leader for a process target. Fix it by using order preserving list operations. Note that as multiple src_csets may map to a single dst_cset, the iteration order may change across cgroup_task_migrate(); however, the leader is still guaranteed to be the first entry. The switch to list_splice_tail_init() at the end of cgroup_migrate() isn't strictly necessary. Let's still do it for consistency. Signed-off-by: Tejun Heo <tj@kernel.org>
Diffstat (limited to 'kernel/cgroup.c')
-rw-r--r--kernel/cgroup.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 37b6d534b0ca..98a8045e2149 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1761,7 +1761,14 @@ static void cgroup_task_migrate(struct cgroup *old_cgrp,
1761 1761
1762 get_css_set(new_cset); 1762 get_css_set(new_cset);
1763 rcu_assign_pointer(tsk->cgroups, new_cset); 1763 rcu_assign_pointer(tsk->cgroups, new_cset);
1764 list_move(&tsk->cg_list, &new_cset->mg_tasks); 1764
1765 /*
1766 * Use move_tail so that cgroup_taskset_first() still returns the
1767 * leader after migration. This works because cgroup_migrate()
1768 * ensures that the dst_cset of the leader is the first on the
1769 * tset's dst_csets list.
1770 */
1771 list_move_tail(&tsk->cg_list, &new_cset->mg_tasks);
1765 1772
1766 /* 1773 /*
1767 * We just gained a reference on old_cset by taking it from the 1774 * We just gained a reference on old_cset by taking it from the
@@ -1936,9 +1943,16 @@ static int cgroup_migrate(struct cgroup *cgrp, struct task_struct *leader,
1936 if (!cset->mg_src_cgrp) 1943 if (!cset->mg_src_cgrp)
1937 goto next; 1944 goto next;
1938 1945
1939 list_move(&task->cg_list, &cset->mg_tasks); 1946 /*
1940 list_move(&cset->mg_node, &tset.src_csets); 1947 * cgroup_taskset_first() must always return the leader.
1941 list_move(&cset->mg_dst_cset->mg_node, &tset.dst_csets); 1948 * Take care to avoid disturbing the ordering.
1949 */
1950 list_move_tail(&task->cg_list, &cset->mg_tasks);
1951 if (list_empty(&cset->mg_node))
1952 list_add_tail(&cset->mg_node, &tset.src_csets);
1953 if (list_empty(&cset->mg_dst_cset->mg_node))
1954 list_move_tail(&cset->mg_dst_cset->mg_node,
1955 &tset.dst_csets);
1942 next: 1956 next:
1943 if (!threadgroup) 1957 if (!threadgroup)
1944 break; 1958 break;
@@ -1999,7 +2013,7 @@ out_release_tset:
1999 down_write(&css_set_rwsem); 2013 down_write(&css_set_rwsem);
2000 list_splice_init(&tset.dst_csets, &tset.src_csets); 2014 list_splice_init(&tset.dst_csets, &tset.src_csets);
2001 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) { 2015 list_for_each_entry_safe(cset, tmp_cset, &tset.src_csets, mg_node) {
2002 list_splice_init(&cset->mg_tasks, &cset->tasks); 2016 list_splice_tail_init(&cset->mg_tasks, &cset->tasks);
2003 list_del_init(&cset->mg_node); 2017 list_del_init(&cset->mg_node);
2004 } 2018 }
2005 up_write(&css_set_rwsem); 2019 up_write(&css_set_rwsem);