5 * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
6 * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
7 * Many thanks to Oleg Nesterov for comments and help
11 #include <linux/pid.h>
12 #include <linux/pid_namespace.h>
13 #include <linux/syscalls.h>
14 #include <linux/err.h>
16 #define BITS_PER_PAGE (PAGE_SIZE*8)
21 struct kmem_cache
*cachep
;
22 struct list_head list
;
25 static LIST_HEAD(pid_caches_lh
);
26 static DEFINE_MUTEX(pid_caches_mutex
);
27 static struct kmem_cache
*pid_ns_cachep
;
30 * creates the kmem cache to allocate pids from.
31 * @nr_ids: the number of numerical ids this pid will have to carry
34 static struct kmem_cache
*create_pid_cachep(int nr_ids
)
36 struct pid_cache
*pcache
;
37 struct kmem_cache
*cachep
;
39 mutex_lock(&pid_caches_mutex
);
40 list_for_each_entry(pcache
, &pid_caches_lh
, list
)
41 if (pcache
->nr_ids
== nr_ids
)
44 pcache
= kmalloc(sizeof(struct pid_cache
), GFP_KERNEL
);
48 snprintf(pcache
->name
, sizeof(pcache
->name
), "pid_%d", nr_ids
);
49 cachep
= kmem_cache_create(pcache
->name
,
50 sizeof(struct pid
) + (nr_ids
- 1) * sizeof(struct upid
),
51 0, SLAB_HWCACHE_ALIGN
, NULL
);
55 pcache
->nr_ids
= nr_ids
;
56 pcache
->cachep
= cachep
;
57 list_add(&pcache
->list
, &pid_caches_lh
);
59 mutex_unlock(&pid_caches_mutex
);
60 return pcache
->cachep
;
65 mutex_unlock(&pid_caches_mutex
);
69 static struct pid_namespace
*create_pid_namespace(int level
)
71 struct pid_namespace
*ns
;
74 ns
= kmem_cache_alloc(pid_ns_cachep
, GFP_KERNEL
);
78 ns
->pidmap
[0].page
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
79 if (!ns
->pidmap
[0].page
)
82 ns
->pid_cachep
= create_pid_cachep(level
+ 1);
83 if (ns
->pid_cachep
== NULL
)
88 ns
->child_reaper
= NULL
;
91 set_bit(0, ns
->pidmap
[0].page
);
92 atomic_set(&ns
->pidmap
[0].nr_free
, BITS_PER_PAGE
- 1);
94 for (i
= 1; i
< PIDMAP_ENTRIES
; i
++) {
95 ns
->pidmap
[i
].page
= 0;
96 atomic_set(&ns
->pidmap
[i
].nr_free
, BITS_PER_PAGE
);
102 kfree(ns
->pidmap
[0].page
);
104 kmem_cache_free(pid_ns_cachep
, ns
);
106 return ERR_PTR(-ENOMEM
);
109 static void destroy_pid_namespace(struct pid_namespace
*ns
)
113 for (i
= 0; i
< PIDMAP_ENTRIES
; i
++)
114 kfree(ns
->pidmap
[i
].page
);
115 kmem_cache_free(pid_ns_cachep
, ns
);
118 struct pid_namespace
*copy_pid_ns(unsigned long flags
, struct pid_namespace
*old_ns
)
120 struct pid_namespace
*new_ns
;
123 new_ns
= get_pid_ns(old_ns
);
124 if (!(flags
& CLONE_NEWPID
))
127 new_ns
= ERR_PTR(-EINVAL
);
128 if (flags
& CLONE_THREAD
)
131 new_ns
= create_pid_namespace(old_ns
->level
+ 1);
133 new_ns
->parent
= get_pid_ns(old_ns
);
141 void free_pid_ns(struct kref
*kref
)
143 struct pid_namespace
*ns
, *parent
;
145 ns
= container_of(kref
, struct pid_namespace
, kref
);
148 destroy_pid_namespace(ns
);
154 void zap_pid_ns_processes(struct pid_namespace
*pid_ns
)
160 * The last thread in the cgroup-init thread group is terminating.
161 * Find remaining pid_ts in the namespace, signal and wait for them
164 * Note: This signals each threads in the namespace - even those that
165 * belong to the same thread group, To avoid this, we would have
166 * to walk the entire tasklist looking a processes in this
167 * namespace, but that could be unnecessarily expensive if the
168 * pid namespace has just a few processes. Or we need to
169 * maintain a tasklist for each pid namespace.
172 read_lock(&tasklist_lock
);
173 nr
= next_pidmap(pid_ns
, 1);
175 kill_proc_info(SIGKILL
, SEND_SIG_PRIV
, nr
);
176 nr
= next_pidmap(pid_ns
, nr
);
178 read_unlock(&tasklist_lock
);
181 clear_thread_flag(TIF_SIGPENDING
);
182 rc
= sys_wait4(-1, NULL
, __WALL
, NULL
);
183 } while (rc
!= -ECHILD
);
186 /* Child reaper for the pid namespace is going away */
187 pid_ns
->child_reaper
= NULL
;
191 static __init
int pid_namespaces_init(void)
193 pid_ns_cachep
= KMEM_CACHE(pid_namespace
, SLAB_PANIC
);
197 __initcall(pid_namespaces_init
);