[RFC PATCH 7/9] cgroup: attach container's tasks to proper cgroup

Gao feng gaofeng at cn.fujitsu.com
Mon Dec 17 06:43:33 UTC 2012


If cgroup is mounted in container,move container's
tasks to the top cgroup of container.

If cgroup is unmounted in container,move container's
tasks back to the host's cgroup.

Signed-off-by: Gao feng <gaofeng at cn.fujitsu.com>
---
 kernel/cgroup.c |   48 ++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index ac61027..e077660 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1110,6 +1110,7 @@ static int __rebind_subsystems_ns(struct cgroupfs_root *root,
 	struct cgroup *parent = NULL;
 	struct cgroupfs_root *top_root = NULL;
 	unsigned long bit;
+	struct tgid_iter iter;
 	int i;
 
 	removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
@@ -1126,6 +1127,25 @@ static int __rebind_subsystems_ns(struct cgroupfs_root *root,
 		BUG_ON(parent == NULL);
 	}
 
+	/*
+	 * Attach container's tasks to host's cgroup,since this
+	 * top cgroup may be destroyed or changed. If task attaching
+	 * failed, return error immediately.
+	 */
+	if (cgrp->parent) {
+		int error;
+		iter.task = NULL;
+		iter.tgid = 1;
+		for (iter = next_tgid(root->pid_ns, iter); iter.task;
+		     iter.tgid += 1, iter = next_tgid(root->pid_ns, iter)) {
+			error = cgroup_attach_task(cgrp->parent, iter.task);
+			if (error && error != -ESRCH) {
+				put_task_struct(iter.task);
+				return error;
+			}
+		}
+	}
+
 	/* Process each subsystem */
 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
 		struct cgroup_subsys *ss = subsys[i];
@@ -1158,15 +1178,31 @@ static int __rebind_subsystems_ns(struct cgroupfs_root *root,
 		}
 	}
 
-	root->top_root = top_root;
-	cgrp->parent = parent;
-
 	/* Link to new top_root or unlink when umounting */
-	if (top_root)
+	if (top_root) {
+		root->top_root = top_root;
+		cgrp->parent = parent;
+		/*
+		 * Attach container's tasks to the new top cgroup.
+		 * We don't care whether some tasks are not attached
+		 * to this cgroup successfully, it totally does no harm.
+		 */
+		iter.task = NULL;
+		iter.tgid = 1;
+		for (iter = next_tgid(root->pid_ns, iter); iter.task;
+		     iter.tgid += 1, iter = next_tgid(root->pid_ns, iter)) {
+			cgroup_attach_task(cgrp, iter.task);
+		}
+		atomic_inc(&top_root->sb->s_active);
+		top_root->number_of_cgroups++;
 		list_move_tail(&cgrp->allcg_node, &top_root->allcg_list);
-	else
+	} else {
+		root->top_root->number_of_cgroups--;
+		atomic_dec(&root->top_root->sb->s_active);
+		root->top_root = NULL;
+		cgrp->parent = NULL;
 		list_del_init(&cgrp->allcg_node);
-
+	}
 	return 0;
 out:
 	for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
-- 
1.7.7.6



More information about the Containers mailing list