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(unsigned int level)
71 struct pid_namespace *ns;
74 ns = kmem_cache_zalloc(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)
89 set_bit(0, ns->pidmap[0].page);
90 atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
92 for (i = 1; i < PIDMAP_ENTRIES; i++)
93 atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
98 kfree(ns->pidmap[0].page);
100 kmem_cache_free(pid_ns_cachep, ns);
102 return ERR_PTR(-ENOMEM);
105 static void destroy_pid_namespace(struct pid_namespace *ns)
109 for (i = 0; i < PIDMAP_ENTRIES; i++)
110 kfree(ns->pidmap[i].page);
111 kmem_cache_free(pid_ns_cachep, ns);
114 struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
116 struct pid_namespace *new_ns;
119 new_ns = get_pid_ns(old_ns);
120 if (!(flags & CLONE_NEWPID))
123 new_ns = ERR_PTR(-EINVAL);
124 if (flags & CLONE_THREAD)
127 new_ns = create_pid_namespace(old_ns->level + 1);
129 new_ns->parent = get_pid_ns(old_ns);
137 void free_pid_ns(struct kref *kref)
139 struct pid_namespace *ns, *parent;
141 ns = container_of(kref, struct pid_namespace, kref);
144 destroy_pid_namespace(ns);
150 void zap_pid_ns_processes(struct pid_namespace *pid_ns)
156 * The last thread in the cgroup-init thread group is terminating.
157 * Find remaining pid_ts in the namespace, signal and wait for them
160 * Note: This signals each threads in the namespace - even those that
161 * belong to the same thread group, To avoid this, we would have
162 * to walk the entire tasklist looking a processes in this
163 * namespace, but that could be unnecessarily expensive if the
164 * pid namespace has just a few processes. Or we need to
165 * maintain a tasklist for each pid namespace.
168 read_lock(&tasklist_lock);
169 nr = next_pidmap(pid_ns, 1);
171 kill_proc_info(SIGKILL, SEND_SIG_PRIV, nr);
172 nr = next_pidmap(pid_ns, nr);
174 read_unlock(&tasklist_lock);
177 clear_thread_flag(TIF_SIGPENDING);
178 rc = sys_wait4(-1, NULL, __WALL, NULL);
179 } while (rc != -ECHILD);
182 /* Child reaper for the pid namespace is going away */
183 pid_ns->child_reaper = NULL;
187 static __init int pid_namespaces_init(void)
189 pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
193 __initcall(pid_namespaces_init);