blob: 17b232869a04efce1ac4d43f9c30a66869c470ee [file] [log] [blame]
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -08001/*
2 * Pid namespaces
3 *
4 * Authors:
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
8 *
9 */
10
11#include <linux/pid.h>
12#include <linux/pid_namespace.h>
13#include <linux/syscalls.h>
14#include <linux/err.h>
Pavel Emelyanov0b6b0302008-07-25 01:48:47 -070015#include <linux/acct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Eric W. Biederman4308eeb2011-03-23 16:43:13 -070017#include <linux/proc_fs.h>
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080018
19#define BITS_PER_PAGE (PAGE_SIZE*8)
20
21struct pid_cache {
22 int nr_ids;
23 char name[16];
24 struct kmem_cache *cachep;
25 struct list_head list;
26};
27
28static LIST_HEAD(pid_caches_lh);
29static DEFINE_MUTEX(pid_caches_mutex);
30static struct kmem_cache *pid_ns_cachep;
31
32/*
33 * creates the kmem cache to allocate pids from.
34 * @nr_ids: the number of numerical ids this pid will have to carry
35 */
36
37static struct kmem_cache *create_pid_cachep(int nr_ids)
38{
39 struct pid_cache *pcache;
40 struct kmem_cache *cachep;
41
42 mutex_lock(&pid_caches_mutex);
43 list_for_each_entry(pcache, &pid_caches_lh, list)
44 if (pcache->nr_ids == nr_ids)
45 goto out;
46
47 pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL);
48 if (pcache == NULL)
49 goto err_alloc;
50
51 snprintf(pcache->name, sizeof(pcache->name), "pid_%d", nr_ids);
52 cachep = kmem_cache_create(pcache->name,
53 sizeof(struct pid) + (nr_ids - 1) * sizeof(struct upid),
54 0, SLAB_HWCACHE_ALIGN, NULL);
55 if (cachep == NULL)
56 goto err_cachep;
57
58 pcache->nr_ids = nr_ids;
59 pcache->cachep = cachep;
60 list_add(&pcache->list, &pid_caches_lh);
61out:
62 mutex_unlock(&pid_caches_mutex);
63 return pcache->cachep;
64
65err_cachep:
66 kfree(pcache);
67err_alloc:
68 mutex_unlock(&pid_caches_mutex);
69 return NULL;
70}
71
Alexey Dobriyaned469a62009-06-17 16:27:52 -070072static struct pid_namespace *create_pid_namespace(struct pid_namespace *parent_pid_ns)
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080073{
74 struct pid_namespace *ns;
Alexey Dobriyaned469a62009-06-17 16:27:52 -070075 unsigned int level = parent_pid_ns->level + 1;
Eric W. Biederman4308eeb2011-03-23 16:43:13 -070076 int i, err = -ENOMEM;
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080077
Pavel Emelyanov84406c12008-07-25 01:48:42 -070078 ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080079 if (ns == NULL)
80 goto out;
81
82 ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL);
83 if (!ns->pidmap[0].page)
84 goto out_free;
85
86 ns->pid_cachep = create_pid_cachep(level + 1);
87 if (ns->pid_cachep == NULL)
88 goto out_free_map;
89
90 kref_init(&ns->kref);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080091 ns->level = level;
Alexey Dobriyaned469a62009-06-17 16:27:52 -070092 ns->parent = get_pid_ns(parent_pid_ns);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080093
94 set_bit(0, ns->pidmap[0].page);
95 atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1);
96
Pavel Emelyanov84406c12008-07-25 01:48:42 -070097 for (i = 1; i < PIDMAP_ENTRIES; i++)
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080098 atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -080099
Eric W. Biederman4308eeb2011-03-23 16:43:13 -0700100 err = pid_ns_prepare_proc(ns);
101 if (err)
102 goto out_put_parent_pid_ns;
103
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800104 return ns;
105
Eric W. Biederman4308eeb2011-03-23 16:43:13 -0700106out_put_parent_pid_ns:
107 put_pid_ns(parent_pid_ns);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800108out_free_map:
109 kfree(ns->pidmap[0].page);
110out_free:
111 kmem_cache_free(pid_ns_cachep, ns);
112out:
Eric W. Biederman4308eeb2011-03-23 16:43:13 -0700113 return ERR_PTR(err);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800114}
115
116static void destroy_pid_namespace(struct pid_namespace *ns)
117{
118 int i;
119
120 for (i = 0; i < PIDMAP_ENTRIES; i++)
121 kfree(ns->pidmap[i].page);
122 kmem_cache_free(pid_ns_cachep, ns);
123}
124
125struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns)
126{
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800127 if (!(flags & CLONE_NEWPID))
Alexey Dobriyandca4a972009-06-17 16:27:53 -0700128 return get_pid_ns(old_ns);
Sukadev Bhattiprolue5a47382009-09-23 15:57:22 -0700129 if (flags & (CLONE_THREAD|CLONE_PARENT))
Alexey Dobriyandca4a972009-06-17 16:27:53 -0700130 return ERR_PTR(-EINVAL);
131 return create_pid_namespace(old_ns);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800132}
133
134void free_pid_ns(struct kref *kref)
135{
136 struct pid_namespace *ns, *parent;
137
138 ns = container_of(kref, struct pid_namespace, kref);
139
140 parent = ns->parent;
141 destroy_pid_namespace(ns);
142
143 if (parent != NULL)
144 put_pid_ns(parent);
145}
146
147void zap_pid_ns_processes(struct pid_namespace *pid_ns)
148{
149 int nr;
150 int rc;
Sukadev Bhattiprolue4da0262009-04-02 16:58:06 -0700151 struct task_struct *task;
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800152
153 /*
154 * The last thread in the cgroup-init thread group is terminating.
155 * Find remaining pid_ts in the namespace, signal and wait for them
156 * to exit.
157 *
158 * Note: This signals each threads in the namespace - even those that
159 * belong to the same thread group, To avoid this, we would have
160 * to walk the entire tasklist looking a processes in this
161 * namespace, but that could be unnecessarily expensive if the
162 * pid namespace has just a few processes. Or we need to
163 * maintain a tasklist for each pid namespace.
164 *
165 */
166 read_lock(&tasklist_lock);
167 nr = next_pidmap(pid_ns, 1);
168 while (nr > 0) {
Sukadev Bhattiprolue4da0262009-04-02 16:58:06 -0700169 rcu_read_lock();
170
Sukadev Bhattiprolue4da0262009-04-02 16:58:06 -0700171 task = pid_task(find_vpid(nr), PIDTYPE_PID);
Oleg Nesterova02d6fd2012-03-23 15:02:46 -0700172 if (task && !__fatal_signal_pending(task))
173 send_sig_info(SIGKILL, SEND_SIG_FORCED, task);
Sukadev Bhattiprolue4da0262009-04-02 16:58:06 -0700174
175 rcu_read_unlock();
176
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800177 nr = next_pidmap(pid_ns, nr);
178 }
179 read_unlock(&tasklist_lock);
180
181 do {
182 clear_thread_flag(TIF_SIGPENDING);
183 rc = sys_wait4(-1, NULL, __WALL, NULL);
184 } while (rc != -ECHILD);
185
Pavel Emelyanov0b6b0302008-07-25 01:48:47 -0700186 acct_exit_ns(pid_ns);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800187 return;
188}
189
Pavel Emelyanovb8f566b2012-01-12 17:20:27 -0800190static int pid_ns_ctl_handler(struct ctl_table *table, int write,
191 void __user *buffer, size_t *lenp, loff_t *ppos)
192{
193 struct ctl_table tmp = *table;
194
195 if (write && !capable(CAP_SYS_ADMIN))
196 return -EPERM;
197
198 /*
199 * Writing directly to ns' last_pid field is OK, since this field
200 * is volatile in a living namespace anyway and a code writing to
201 * it should synchronize its usage with external means.
202 */
203
204 tmp.data = &current->nsproxy->pid_ns->last_pid;
205 return proc_dointvec(&tmp, write, buffer, lenp, ppos);
206}
207
208static struct ctl_table pid_ns_ctl_table[] = {
209 {
210 .procname = "ns_last_pid",
211 .maxlen = sizeof(int),
212 .mode = 0666, /* permissions are checked in the handler */
213 .proc_handler = pid_ns_ctl_handler,
214 },
215 { }
216};
217
218static struct ctl_path kern_path[] = { { .procname = "kernel", }, { } };
219
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800220static __init int pid_namespaces_init(void)
221{
222 pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
Pavel Emelyanovb8f566b2012-01-12 17:20:27 -0800223 register_sysctl_paths(kern_path, pid_ns_ctl_table);
Pavel Emelyanov74bd59b2008-02-08 04:18:24 -0800224 return 0;
225}
226
227__initcall(pid_namespaces_init);