[Devel] [PATCH] cgroups: add task along with its threads in one shot inside cgroup

Gowrishankar M gowrishankar.m at linux.vnet.ibm.com
Wed Dec 17 12:30:26 PST 2008


From: Gowrishankar M <gomuthuk at linux.vnet.ibm.com>

In current model, task by task we populate cgroup.
Incase of multi-threaded task, it becomes difficult
as we should first know about list of threads created
and add them one by one in cgroup.

In below patch, when a thread group leader is scanned,
belonging threads are also moved along with it, while
populating cgroup tasks.

Below steps can help to verify the patch:
(1) mount -t cgroup -o cpuset cgroup /cgroup
(2) /bin/mkdir /cgroup/testgroup and cd /cgroup/testgroup
(3) /bin/echo 0 > cpuset.mems
(4) /bin/echo 0 > cpuset.cpus
(5) /bin/echo "<PID of thread group leader>" > tasks (tip: ps -aeL)
(6) cat tasks and check for thread ids.

Signed-off-by: Gowrishankar M <gowrishankar.m at linux.vnet.ibm.com>
---
 kernel/cgroup.c |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/kernel/cgroup.c b/kernel/cgroup.c
index 35eebd5..de7f0cc 100644
--- a/kernel/cgroup.c
+++ b/kernel/cgroup.c
@@ -1279,6 +1279,7 @@ int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
 static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
 {
 	struct task_struct *tsk;
+	struct task_struct *p;
 	int ret;
 
 	if (pid) {
@@ -1288,21 +1289,23 @@ static int attach_task_by_pid(struct cgroup *cgrp, u64 pid)
 			rcu_read_unlock();
 			return -ESRCH;
 		}
-		get_task_struct(tsk);
 		rcu_read_unlock();
 
 		if ((current->euid) && (current->euid != tsk->uid)
 		    && (current->euid != tsk->suid)) {
-			put_task_struct(tsk);
 			return -EACCES;
 		}
-	} else {
+	} else
 		tsk = current;
-		get_task_struct(tsk);
-	}
 
-	ret = cgroup_attach_task(cgrp, tsk);
-	put_task_struct(tsk);
+	p = tsk;
+	do {
+		get_task_struct(p);
+		ret = cgroup_attach_task(cgrp, p);
+		put_task_struct(p);
+		if (thread_group_empty(p) && !(has_group_leader_pid(p)))
+			break;
+	} while_each_thread(tsk, p);
 	return ret;
 }
 
-- 
1.5.5.1

_______________________________________________
Containers mailing list
Containers at lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/containers




More information about the Devel mailing list