cgroups: add task along with its threads in one shot inside cgroup
PATCH 1/1 cgroups: add task along with its threads in one shot inside cgroup
From: Gowrishankar M <[email protected]>
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.
--- kernel/cgroup.c | 17 ++++++++++------- 1 files changed, 10 insertions(+), 7 deletions(-)
kernel/cgroup.c +10 -7
@@ -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; }
Signature · 1 line
1.5.5.1
Quoted 7 lines · Gowrishankar M
From: Gowrishankar M <[email protected]>
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.
It was proposed to add 'procs' control file to do so long ago, but there is a big issue, that there is no way to do it atomically, i.e you may success to move some threads but fail for others.
You can get some information here:
https://lists.linux-foundation.org/pipermail/containers/2008-July/011747.html
Quoted 3 lines
In below patch, when a thread group leader is scanned, belonging threads are also moved along with it, while populating cgroup tasks.