Commit | Line | Data |
---|---|---|
74bd59bb PE |
1 | /* |
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> | |
15 | ||
16 | #define BITS_PER_PAGE (PAGE_SIZE*8) | |
17 | ||
18 | struct pid_cache { | |
19 | int nr_ids; | |
20 | char name[16]; | |
21 | struct kmem_cache *cachep; | |
22 | struct list_head list; | |
23 | }; | |
24 | ||
25 | static LIST_HEAD(pid_caches_lh); | |
26 | static DEFINE_MUTEX(pid_caches_mutex); | |
27 | static struct kmem_cache *pid_ns_cachep; | |
28 | ||
29 | /* | |
30 | * creates the kmem cache to allocate pids from. | |
31 | * @nr_ids: the number of numerical ids this pid will have to carry | |
32 | */ | |
33 | ||
34 | static struct kmem_cache *create_pid_cachep(int nr_ids) | |
35 | { | |
36 | struct pid_cache *pcache; | |
37 | struct kmem_cache *cachep; | |
38 | ||
39 | mutex_lock(&pid_caches_mutex); | |
40 | list_for_each_entry(pcache, &pid_caches_lh, list) | |
41 | if (pcache->nr_ids == nr_ids) | |
42 | goto out; | |
43 | ||
44 | pcache = kmalloc(sizeof(struct pid_cache), GFP_KERNEL); | |
45 | if (pcache == NULL) | |
46 | goto err_alloc; | |
47 | ||
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); | |
52 | if (cachep == NULL) | |
53 | goto err_cachep; | |
54 | ||
55 | pcache->nr_ids = nr_ids; | |
56 | pcache->cachep = cachep; | |
57 | list_add(&pcache->list, &pid_caches_lh); | |
58 | out: | |
59 | mutex_unlock(&pid_caches_mutex); | |
60 | return pcache->cachep; | |
61 | ||
62 | err_cachep: | |
63 | kfree(pcache); | |
64 | err_alloc: | |
65 | mutex_unlock(&pid_caches_mutex); | |
66 | return NULL; | |
67 | } | |
68 | ||
caafa432 | 69 | static struct pid_namespace *create_pid_namespace(unsigned int level) |
74bd59bb PE |
70 | { |
71 | struct pid_namespace *ns; | |
72 | int i; | |
73 | ||
84406c15 | 74 | ns = kmem_cache_zalloc(pid_ns_cachep, GFP_KERNEL); |
74bd59bb PE |
75 | if (ns == NULL) |
76 | goto out; | |
77 | ||
78 | ns->pidmap[0].page = kzalloc(PAGE_SIZE, GFP_KERNEL); | |
79 | if (!ns->pidmap[0].page) | |
80 | goto out_free; | |
81 | ||
82 | ns->pid_cachep = create_pid_cachep(level + 1); | |
83 | if (ns->pid_cachep == NULL) | |
84 | goto out_free_map; | |
85 | ||
86 | kref_init(&ns->kref); | |
74bd59bb PE |
87 | ns->level = level; |
88 | ||
89 | set_bit(0, ns->pidmap[0].page); | |
90 | atomic_set(&ns->pidmap[0].nr_free, BITS_PER_PAGE - 1); | |
91 | ||
84406c15 | 92 | for (i = 1; i < PIDMAP_ENTRIES; i++) |
74bd59bb | 93 | atomic_set(&ns->pidmap[i].nr_free, BITS_PER_PAGE); |
74bd59bb PE |
94 | |
95 | return ns; | |
96 | ||
97 | out_free_map: | |
98 | kfree(ns->pidmap[0].page); | |
99 | out_free: | |
100 | kmem_cache_free(pid_ns_cachep, ns); | |
101 | out: | |
102 | return ERR_PTR(-ENOMEM); | |
103 | } | |
104 | ||
105 | static void destroy_pid_namespace(struct pid_namespace *ns) | |
106 | { | |
107 | int i; | |
108 | ||
109 | for (i = 0; i < PIDMAP_ENTRIES; i++) | |
110 | kfree(ns->pidmap[i].page); | |
111 | kmem_cache_free(pid_ns_cachep, ns); | |
112 | } | |
113 | ||
114 | struct pid_namespace *copy_pid_ns(unsigned long flags, struct pid_namespace *old_ns) | |
115 | { | |
116 | struct pid_namespace *new_ns; | |
117 | ||
118 | BUG_ON(!old_ns); | |
119 | new_ns = get_pid_ns(old_ns); | |
120 | if (!(flags & CLONE_NEWPID)) | |
121 | goto out; | |
122 | ||
123 | new_ns = ERR_PTR(-EINVAL); | |
124 | if (flags & CLONE_THREAD) | |
125 | goto out_put; | |
126 | ||
127 | new_ns = create_pid_namespace(old_ns->level + 1); | |
128 | if (!IS_ERR(new_ns)) | |
129 | new_ns->parent = get_pid_ns(old_ns); | |
130 | ||
131 | out_put: | |
132 | put_pid_ns(old_ns); | |
133 | out: | |
134 | return new_ns; | |
135 | } | |
136 | ||
137 | void free_pid_ns(struct kref *kref) | |
138 | { | |
139 | struct pid_namespace *ns, *parent; | |
140 | ||
141 | ns = container_of(kref, struct pid_namespace, kref); | |
142 | ||
143 | parent = ns->parent; | |
144 | destroy_pid_namespace(ns); | |
145 | ||
146 | if (parent != NULL) | |
147 | put_pid_ns(parent); | |
148 | } | |
149 | ||
150 | void zap_pid_ns_processes(struct pid_namespace *pid_ns) | |
151 | { | |
152 | int nr; | |
153 | int rc; | |
154 | ||
155 | /* | |
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 | |
158 | * to exit. | |
159 | * | |
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. | |
166 | * | |
167 | */ | |
168 | read_lock(&tasklist_lock); | |
169 | nr = next_pidmap(pid_ns, 1); | |
170 | while (nr > 0) { | |
171 | kill_proc_info(SIGKILL, SEND_SIG_PRIV, nr); | |
172 | nr = next_pidmap(pid_ns, nr); | |
173 | } | |
174 | read_unlock(&tasklist_lock); | |
175 | ||
176 | do { | |
177 | clear_thread_flag(TIF_SIGPENDING); | |
178 | rc = sys_wait4(-1, NULL, __WALL, NULL); | |
179 | } while (rc != -ECHILD); | |
180 | ||
181 | ||
182 | /* Child reaper for the pid namespace is going away */ | |
183 | pid_ns->child_reaper = NULL; | |
184 | return; | |
185 | } | |
186 | ||
187 | static __init int pid_namespaces_init(void) | |
188 | { | |
189 | pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | __initcall(pid_namespaces_init); |