Skip to content

Commit 0a01f2c

Browse files
committed
pidns: Make the pidns proc mount/umount logic obvious.
Track the number of pids in the proc hash table. When the number of pids goes to 0 schedule work to unmount the kernel mount of proc. Move the mount of proc into alloc_pid when we allocate the pid for init. Remove the surprising calls of pid_ns_release proc in fork and proc_flush_task. Those code paths really shouldn't know about proc namespace implementation details and people have demonstrated several times that finding and understanding those code paths is difficult and non-obvious. Because of the call path detach pid is alwasy called with the rtnl_lock held free_pid is not allowed to sleep, so the work to unmounting proc is moved to a work queue. This has the side benefit of not blocking the entire world waiting for the unnecessary rcu_barrier in deactivate_locked_super. In the process of making the code clear and obvious this fixes a bug reported by Gao feng <gaofeng@cn.fujitsu.com> where we would leak a mount of proc during clone(CLONE_NEWPID|CLONE_NEWNET) if copy_pid_ns succeeded and copy_net_ns failed. Acked-by: "Serge E. Hallyn" <serge@hallyn.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent 17cf22c commit 0a01f2c

6 files changed

Lines changed: 26 additions & 22 deletions

File tree

fs/proc/base.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,10 +2590,6 @@ void proc_flush_task(struct task_struct *task)
25902590
proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
25912591
tgid->numbers[i].nr);
25922592
}
2593-
2594-
upid = &pid->numbers[pid->level];
2595-
if (upid->nr == 1)
2596-
pid_ns_release_proc(upid->ns);
25972593
}
25982594

25992595
static struct dentry *proc_pid_instantiate(struct inode *dir,

fs/proc/root.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ void __init proc_root_init(void)
155155
err = register_filesystem(&proc_fs_type);
156156
if (err)
157157
return;
158-
err = pid_ns_prepare_proc(&init_pid_ns);
159-
if (err) {
160-
unregister_filesystem(&proc_fs_type);
161-
return;
162-
}
163158

164159
proc_self_init();
165160
proc_symlink("mounts", NULL, "self/mounts");

include/linux/pid_namespace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct pid_namespace {
2121
struct kref kref;
2222
struct pidmap pidmap[PIDMAP_ENTRIES];
2323
int last_pid;
24+
int nr_hashed;
2425
struct task_struct *child_reaper;
2526
struct kmem_cache *pid_cachep;
2627
unsigned int level;
@@ -32,6 +33,7 @@ struct pid_namespace {
3233
struct bsd_acct_struct *bacct;
3334
#endif
3435
struct user_namespace *user_ns;
36+
struct work_struct proc_work;
3537
kgid_t pid_gid;
3638
int hide_pid;
3739
int reboot; /* group exit code if this pidns was rebooted */

kernel/fork.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,8 +1476,6 @@ static struct task_struct *copy_process(unsigned long clone_flags,
14761476
if (p->io_context)
14771477
exit_io_context(p);
14781478
bad_fork_cleanup_namespaces:
1479-
if (unlikely(clone_flags & CLONE_NEWPID))
1480-
pid_ns_release_proc(p->nsproxy->pid_ns);
14811479
exit_task_namespaces(p);
14821480
bad_fork_cleanup_mm:
14831481
if (p->mm)

kernel/pid.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/pid_namespace.h>
3737
#include <linux/init_task.h>
3838
#include <linux/syscalls.h>
39+
#include <linux/proc_fs.h>
3940

4041
#define pid_hashfn(nr, ns) \
4142
hash_long((unsigned long)nr + (unsigned long)ns, pidhash_shift)
@@ -270,8 +271,12 @@ void free_pid(struct pid *pid)
270271
unsigned long flags;
271272

272273
spin_lock_irqsave(&pidmap_lock, flags);
273-
for (i = 0; i <= pid->level; i++)
274-
hlist_del_rcu(&pid->numbers[i].pid_chain);
274+
for (i = 0; i <= pid->level; i++) {
275+
struct upid *upid = pid->numbers + i;
276+
hlist_del_rcu(&upid->pid_chain);
277+
if (--upid->ns->nr_hashed == 0)
278+
schedule_work(&upid->ns->proc_work);
279+
}
275280
spin_unlock_irqrestore(&pidmap_lock, flags);
276281

277282
for (i = 0; i <= pid->level; i++)
@@ -293,6 +298,7 @@ struct pid *alloc_pid(struct pid_namespace *ns)
293298
goto out;
294299

295300
tmp = ns;
301+
pid->level = ns->level;
296302
for (i = ns->level; i >= 0; i--) {
297303
nr = alloc_pidmap(tmp);
298304
if (nr < 0)
@@ -303,17 +309,23 @@ struct pid *alloc_pid(struct pid_namespace *ns)
303309
tmp = tmp->parent;
304310
}
305311

312+
if (unlikely(is_child_reaper(pid))) {
313+
if (pid_ns_prepare_proc(ns))
314+
goto out_free;
315+
}
316+
306317
get_pid_ns(ns);
307-
pid->level = ns->level;
308318
atomic_set(&pid->count, 1);
309319
for (type = 0; type < PIDTYPE_MAX; ++type)
310320
INIT_HLIST_HEAD(&pid->tasks[type]);
311321

312322
upid = pid->numbers + ns->level;
313323
spin_lock_irq(&pidmap_lock);
314-
for ( ; upid >= pid->numbers; --upid)
324+
for ( ; upid >= pid->numbers; --upid) {
315325
hlist_add_head_rcu(&upid->pid_chain,
316326
&pid_hash[pid_hashfn(upid->nr, upid->ns)]);
327+
upid->ns->nr_hashed++;
328+
}
317329
spin_unlock_irq(&pidmap_lock);
318330

319331
out:
@@ -570,6 +582,7 @@ void __init pidmap_init(void)
570582
/* Reserve PID 0. We never call free_pidmap(0) */
571583
set_bit(0, init_pid_ns.pidmap[0].page);
572584
atomic_dec(&init_pid_ns.pidmap[0].nr_free);
585+
init_pid_ns.nr_hashed = 1;
573586

574587
init_pid_ns.pid_cachep = KMEM_CACHE(pid,
575588
SLAB_HWCACHE_ALIGN | SLAB_PANIC);

kernel/pid_namespace.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ static struct kmem_cache *create_pid_cachep(int nr_ids)
7272
return NULL;
7373
}
7474

75+
static void proc_cleanup_work(struct work_struct *work)
76+
{
77+
struct pid_namespace *ns = container_of(work, struct pid_namespace, proc_work);
78+
pid_ns_release_proc(ns);
79+
}
80+
7581
/* MAX_PID_NS_LEVEL is needed for limiting size of 'struct pid' */
7682
#define MAX_PID_NS_LEVEL 32
7783

@@ -105,22 +111,16 @@ static struct pid_namespace *create_pid_namespace(struct user_namespace *user_ns
105111
ns->level = level;
106112
ns->parent = get_pid_ns(parent_pid_ns);
107113
ns->user_ns = get_user_ns(user_ns);
114+
INIT_WORK(&ns->proc_work, proc_cleanup_work);
108115

109116
set_bit(0, ns->pidmap[0].page);
110117
atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
111118

112119
for (i = 1; i < PIDMAP_ENTRIES; i++)
113120
atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
114121

115-
err = pid_ns_prepare_proc(ns);
116-
if (err)
117-
goto out_put_parent_pid_ns;
118-
119122
return ns;
120123

121-
out_put_parent_pid_ns:
122-
put_pid_ns(parent_pid_ns);
123-
put_user_ns(user_ns);
124124
out_free_map:
125125
kfree(ns->pidmap[0].page);
126126
out_free:

0 commit comments

Comments
 (0)