Skip to content

Commit e885dcd

Browse files
Serge E. Hallyntorvalds
authored andcommitted
cgroup_clone: use pid of newly created task for new cgroup
cgroup_clone creates a new cgroup with the pid of the task. This works correctly for unshare, but for clone cgroup_clone is called from copy_namespaces inside copy_process, which happens before the new pid is created. As a result, the new cgroup was created with current's pid. This patch: 1. Moves the call inside copy_process to after the new pid is created 2. Passes the struct pid into ns_cgroup_clone (as it is not yet attached to the task) 3. Passes a name from ns_cgroup_clone() into cgroup_clone() so as to keep cgroup_clone() itself simpler 4. Uses pid_vnr() to get the process id value, so that the pid used to name the new cgroup is always the pid as it would be known to the task which did the cloning or unsharing. I think that is the most intuitive thing to do. This way, task t1 does clone(CLONE_NEWPID) to get t2, which does clone(CLONE_NEWPID) to get t3, then the cgroup for t3 will be named for the pid by which t2 knows t3. (Thanks to Dan Smith for finding the main bug) Changelog: June 11: Incorporate Paul Menage's feedback: don't pass NULL to ns_cgroup_clone from unshare, and reduce patch size by using 'nodename' in cgroup_clone. June 10: Original version [akpm@linux-foundation.org: build fix] [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Serge Hallyn <serge@us.ibm.com> Acked-by: Paul Menage <menage@google.com> Tested-by: Dan Smith <danms@us.ibm.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: KAMEZAWA Hiroyuki <kamezawa.hiroyu@jp.fujitsu.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 856c13a commit e885dcd

6 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎include/linux/cgroup.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ static inline struct cgroup* task_cgroup(struct task_struct *task,
364364
return task_subsys_state(task, subsys_id)->cgroup;
365365
}
366366

367-
int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss);
367+
int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *ss,
368+
char *nodename);
368369

369370
/* A cgroup_iter should be treated as an opaque object */
370371
struct cgroup_iter {

‎include/linux/nsproxy.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ static inline void get_nsproxy(struct nsproxy *ns)
8282
}
8383

8484
#ifdef CONFIG_CGROUP_NS
85-
int ns_cgroup_clone(struct task_struct *tsk);
85+
int ns_cgroup_clone(struct task_struct *tsk, struct pid *pid);
8686
#else
87-
static inline int ns_cgroup_clone(struct task_struct *tsk) { return 0; }
87+
static inline int ns_cgroup_clone(struct task_struct *tsk, struct pid *pid)
88+
{
89+
return 0;
90+
}
8891
#endif
8992

9093
#endif

‎kernel/cgroup.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,16 +2848,17 @@ void cgroup_exit(struct task_struct *tsk, int run_callbacks)
28482848
* cgroup_clone - clone the cgroup the given subsystem is attached to
28492849
* @tsk: the task to be moved
28502850
* @subsys: the given subsystem
2851+
* @nodename: the name for the new cgroup
28512852
*
28522853
* Duplicate the current cgroup in the hierarchy that the given
28532854
* subsystem is attached to, and move this task into the new
28542855
* child.
28552856
*/
2856-
int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
2857+
int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys,
2858+
char *nodename)
28572859
{
28582860
struct dentry *dentry;
28592861
int ret = 0;
2860-
char nodename[MAX_CGROUP_TYPE_NAMELEN];
28612862
struct cgroup *parent, *child;
28622863
struct inode *inode;
28632864
struct css_set *cg;
@@ -2882,8 +2883,6 @@ int cgroup_clone(struct task_struct *tsk, struct cgroup_subsys *subsys)
28822883
cg = tsk->cgroups;
28832884
parent = task_cgroup(tsk, subsys->subsys_id);
28842885

2885-
snprintf(nodename, MAX_CGROUP_TYPE_NAMELEN, "%d", tsk->pid);
2886-
28872886
/* Pin the hierarchy */
28882887
atomic_inc(&parent->root->sb->s_active);
28892888

‎kernel/fork.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,12 @@ static struct task_struct *copy_process(unsigned long clone_flags,
11071107
if (clone_flags & CLONE_THREAD)
11081108
p->tgid = current->tgid;
11091109

1110+
if (current->nsproxy != p->nsproxy) {
1111+
retval = ns_cgroup_clone(p, pid);
1112+
if (retval)
1113+
goto bad_fork_free_pid;
1114+
}
1115+
11101116
p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
11111117
/*
11121118
* Clear TID on mm_release()?

‎kernel/ns_cgroup.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/module.h>
88
#include <linux/cgroup.h>
99
#include <linux/fs.h>
10+
#include <linux/proc_fs.h>
1011
#include <linux/slab.h>
1112
#include <linux/nsproxy.h>
1213

@@ -24,9 +25,12 @@ static inline struct ns_cgroup *cgroup_to_ns(
2425
struct ns_cgroup, css);
2526
}
2627

27-
int ns_cgroup_clone(struct task_struct *task)
28+
int ns_cgroup_clone(struct task_struct *task, struct pid *pid)
2829
{
29-
return cgroup_clone(task, &ns_subsys);
30+
char name[PROC_NUMBUF];
31+
32+
snprintf(name, PROC_NUMBUF, "%d", pid_vnr(pid));
33+
return cgroup_clone(task, &ns_subsys, name);
3034
}
3135

3236
/*

‎kernel/nsproxy.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,6 @@ int copy_namespaces(unsigned long flags, struct task_struct *tsk)
157157
goto out;
158158
}
159159

160-
err = ns_cgroup_clone(tsk);
161-
if (err) {
162-
put_nsproxy(new_ns);
163-
goto out;
164-
}
165-
166160
tsk->nsproxy = new_ns;
167161

168162
out:
@@ -209,7 +203,7 @@ int unshare_nsproxy_namespaces(unsigned long unshare_flags,
209203
goto out;
210204
}
211205

212-
err = ns_cgroup_clone(current);
206+
err = ns_cgroup_clone(current, task_pid(current));
213207
if (err)
214208
put_nsproxy(*new_nsp);
215209

0 commit comments

Comments
 (0)