]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - kernel/pid.c
pid: replace pid bitmap implementation with IDR API
[mirror_ubuntu-eoan-kernel.git] / kernel / pid.c
CommitLineData
1da177e4
LT
1/*
2 * Generic pidhash and scalable, time-bounded PID allocator
3 *
6d49e352
NYC
4 * (C) 2002-2003 Nadia Yvette Chambers, IBM
5 * (C) 2004 Nadia Yvette Chambers, Oracle
1da177e4
LT
6 * (C) 2002-2004 Ingo Molnar, Red Hat
7 *
8 * pid-structures are backing objects for tasks sharing a given ID to chain
9 * against. There is very little to them aside from hashing them and
10 * parking tasks using given ID's on a list.
11 *
12 * The hash is always changed with the tasklist_lock write-acquired,
13 * and the hash is only accessed with the tasklist_lock at least
14 * read-acquired, so there's no additional SMP locking needed here.
15 *
16 * We have a list of bitmap pages, which bitmaps represent the PID space.
17 * Allocating and freeing PIDs is completely lockless. The worst-case
18 * allocation scenario when all but one out of 1 million PIDs possible are
19 * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
20 * bytes. The typical fastpath is a single successful setbit. Freeing is O(1).
30e49c26
PE
21 *
22 * Pid namespaces:
23 * (C) 2007 Pavel Emelyanov <xemul@openvz.org>, OpenVZ, SWsoft Inc.
24 * (C) 2007 Sukadev Bhattiprolu <sukadev@us.ibm.com>, IBM
25 * Many thanks to Oleg Nesterov for comments and help
26 *
1da177e4
LT
27 */
28
29#include <linux/mm.h>
9984de1a 30#include <linux/export.h>
1da177e4
LT
31#include <linux/slab.h>
32#include <linux/init.h>
82524746 33#include <linux/rculist.h>
1da177e4
LT
34#include <linux/bootmem.h>
35#include <linux/hash.h>
61a58c6c 36#include <linux/pid_namespace.h>
820e45db 37#include <linux/init_task.h>
3eb07c8c 38#include <linux/syscalls.h>
0bb80f24 39#include <linux/proc_ns.h>
0a01f2cc 40#include <linux/proc_fs.h>
29930025 41#include <linux/sched/task.h>
95846ecf 42#include <linux/idr.h>
1da177e4 43
8ef047aa
PE
44#define pid_hashfn(nr, ns) \
45 hash_long((unsigned long)nr + (unsigned long)ns, pidhash_shift)
92476d7f 46static struct hlist_head *pid_hash;
2c85f51d 47static unsigned int pidhash_shift = 4;
820e45db 48struct pid init_struct_pid = INIT_STRUCT_PID;
1da177e4
LT
49
50int pid_max = PID_MAX_DEFAULT;
1da177e4
LT
51
52#define RESERVED_PIDS 300
53
54int pid_max_min = RESERVED_PIDS + 1;
55int pid_max_max = PID_MAX_LIMIT;
56
1da177e4
LT
57
58/*
59 * PID-map pages start out as NULL, they get allocated upon
60 * first use and are never deallocated. This way a low pid_max
61 * value does not cause lots of bitmaps to be allocated, but
62 * the scheme scales to up to 4 million PIDs, runtime.
63 */
61a58c6c 64struct pid_namespace init_pid_ns = {
1e24edca 65 .kref = KREF_INIT(2),
95846ecf 66 .idr = IDR_INIT,
8f75af44 67 .nr_hashed = PIDNS_HASH_ADDING,
faacbfd3
PE
68 .level = 0,
69 .child_reaper = &init_task,
49f4d8b9 70 .user_ns = &init_user_ns,
435d5f4b 71 .ns.inum = PROC_PID_INIT_INO,
33c42940
AV
72#ifdef CONFIG_PID_NS
73 .ns.ops = &pidns_operations,
74#endif
3fbc9648 75};
198fe21b 76EXPORT_SYMBOL_GPL(init_pid_ns);
1da177e4 77
92476d7f
EB
78/*
79 * Note: disable interrupts while the pidmap_lock is held as an
80 * interrupt might come in and do read_lock(&tasklist_lock).
81 *
82 * If we don't disable interrupts there is a nasty deadlock between
83 * detach_pid()->free_pid() and another cpu that does
84 * spin_lock(&pidmap_lock) followed by an interrupt routine that does
85 * read_lock(&tasklist_lock);
86 *
87 * After we clean up the tasklist_lock and know there are no
88 * irq handlers that take it we can leave the interrupts enabled.
89 * For now it is easier to be safe than to prove it can't happen.
90 */
3fbc9648 91
1da177e4
LT
92static __cacheline_aligned_in_smp DEFINE_SPINLOCK(pidmap_lock);
93
7ad5b3a5 94void put_pid(struct pid *pid)
92476d7f 95{
baf8f0f8
PE
96 struct pid_namespace *ns;
97
92476d7f
EB
98 if (!pid)
99 return;
baf8f0f8 100
8ef047aa 101 ns = pid->numbers[pid->level].ns;
92476d7f 102 if ((atomic_read(&pid->count) == 1) ||
8ef047aa 103 atomic_dec_and_test(&pid->count)) {
baf8f0f8 104 kmem_cache_free(ns->pid_cachep, pid);
b461cc03 105 put_pid_ns(ns);
8ef047aa 106 }
92476d7f 107}
bbf73147 108EXPORT_SYMBOL_GPL(put_pid);
92476d7f
EB
109
110static void delayed_put_pid(struct rcu_head *rhp)
111{
112 struct pid *pid = container_of(rhp, struct pid, rcu);
113 put_pid(pid);
114}
115
7ad5b3a5 116void free_pid(struct pid *pid)
92476d7f
EB
117{
118 /* We can be called with write_lock_irq(&tasklist_lock) held */
8ef047aa 119 int i;
92476d7f
EB
120 unsigned long flags;
121
122 spin_lock_irqsave(&pidmap_lock, flags);
0a01f2cc
EB
123 for (i = 0; i <= pid->level; i++) {
124 struct upid *upid = pid->numbers + i;
af4b8a83 125 struct pid_namespace *ns = upid->ns;
0a01f2cc 126 hlist_del_rcu(&upid->pid_chain);
95846ecf 127 switch (--ns->nr_hashed) {
a6064885 128 case 2:
af4b8a83
EB
129 case 1:
130 /* When all that is left in the pid namespace
131 * is the reaper wake up the reaper. The reaper
132 * may be sleeping in zap_pid_ns_processes().
133 */
134 wake_up_process(ns->child_reaper);
135 break;
314a8ad0
ON
136 case PIDNS_HASH_ADDING:
137 /* Handle a fork failure of the first process */
138 WARN_ON(ns->child_reaper);
139 ns->nr_hashed = 0;
140 /* fall through */
af4b8a83 141 case 0:
af4b8a83
EB
142 schedule_work(&ns->proc_work);
143 break;
5e1182de 144 }
95846ecf
GS
145
146 idr_remove(&ns->idr, upid->nr);
0a01f2cc 147 }
92476d7f
EB
148 spin_unlock_irqrestore(&pidmap_lock, flags);
149
92476d7f
EB
150 call_rcu(&pid->rcu, delayed_put_pid);
151}
152
8ef047aa 153struct pid *alloc_pid(struct pid_namespace *ns)
92476d7f
EB
154{
155 struct pid *pid;
156 enum pid_type type;
8ef047aa
PE
157 int i, nr;
158 struct pid_namespace *tmp;
198fe21b 159 struct upid *upid;
35f71bc0 160 int retval = -ENOMEM;
92476d7f 161
baf8f0f8 162 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
92476d7f 163 if (!pid)
35f71bc0 164 return ERR_PTR(retval);
92476d7f 165
8ef047aa 166 tmp = ns;
0a01f2cc 167 pid->level = ns->level;
95846ecf 168
8ef047aa 169 for (i = ns->level; i >= 0; i--) {
95846ecf
GS
170 int pid_min = 1;
171
172 idr_preload(GFP_KERNEL);
173 spin_lock_irq(&pidmap_lock);
174
175 /*
176 * init really needs pid 1, but after reaching the maximum
177 * wrap back to RESERVED_PIDS
178 */
179 if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
180 pid_min = RESERVED_PIDS;
181
182 /*
183 * Store a null pointer so find_pid_ns does not find
184 * a partially initialized PID (see below).
185 */
186 nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
187 pid_max, GFP_ATOMIC);
188 spin_unlock_irq(&pidmap_lock);
189 idr_preload_end();
190
287980e4 191 if (nr < 0) {
35f71bc0 192 retval = nr;
8ef047aa 193 goto out_free;
35f71bc0 194 }
92476d7f 195
8ef047aa
PE
196 pid->numbers[i].nr = nr;
197 pid->numbers[i].ns = tmp;
198 tmp = tmp->parent;
199 }
200
0a01f2cc 201 if (unlikely(is_child_reaper(pid))) {
8896c23d
KT
202 if (pid_ns_prepare_proc(ns)) {
203 disable_pid_allocation(ns);
0a01f2cc 204 goto out_free;
8896c23d 205 }
0a01f2cc
EB
206 }
207
b461cc03 208 get_pid_ns(ns);
92476d7f 209 atomic_set(&pid->count, 1);
92476d7f
EB
210 for (type = 0; type < PIDTYPE_MAX; ++type)
211 INIT_HLIST_HEAD(&pid->tasks[type]);
212
417e3152 213 upid = pid->numbers + ns->level;
92476d7f 214 spin_lock_irq(&pidmap_lock);
c876ad76 215 if (!(ns->nr_hashed & PIDNS_HASH_ADDING))
5e1182de 216 goto out_unlock;
0a01f2cc 217 for ( ; upid >= pid->numbers; --upid) {
198fe21b
PE
218 hlist_add_head_rcu(&upid->pid_chain,
219 &pid_hash[pid_hashfn(upid->nr, upid->ns)]);
95846ecf
GS
220 /* Make the PID visible to find_pid_ns. */
221 idr_replace(&upid->ns->idr, pid, upid->nr);
0a01f2cc
EB
222 upid->ns->nr_hashed++;
223 }
92476d7f
EB
224 spin_unlock_irq(&pidmap_lock);
225
92476d7f
EB
226 return pid;
227
5e1182de 228out_unlock:
6e666884 229 spin_unlock_irq(&pidmap_lock);
24c037eb
ON
230 put_pid_ns(ns);
231
92476d7f 232out_free:
95846ecf 233 spin_lock_irq(&pidmap_lock);
b7127aa4 234 while (++i <= ns->level)
95846ecf
GS
235 idr_remove(&ns->idr, (pid->numbers + i)->nr);
236
237 spin_unlock_irq(&pidmap_lock);
8ef047aa 238
baf8f0f8 239 kmem_cache_free(ns->pid_cachep, pid);
35f71bc0 240 return ERR_PTR(retval);
92476d7f
EB
241}
242
c876ad76
EB
243void disable_pid_allocation(struct pid_namespace *ns)
244{
245 spin_lock_irq(&pidmap_lock);
246 ns->nr_hashed &= ~PIDNS_HASH_ADDING;
247 spin_unlock_irq(&pidmap_lock);
248}
249
7ad5b3a5 250struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
1da177e4 251{
198fe21b
PE
252 struct upid *pnr;
253
b67bfe0d 254 hlist_for_each_entry_rcu(pnr,
198fe21b
PE
255 &pid_hash[pid_hashfn(nr, ns)], pid_chain)
256 if (pnr->nr == nr && pnr->ns == ns)
257 return container_of(pnr, struct pid,
258 numbers[ns->level]);
1da177e4 259
1da177e4
LT
260 return NULL;
261}
198fe21b 262EXPORT_SYMBOL_GPL(find_pid_ns);
1da177e4 263
8990571e
PE
264struct pid *find_vpid(int nr)
265{
17cf22c3 266 return find_pid_ns(nr, task_active_pid_ns(current));
8990571e
PE
267}
268EXPORT_SYMBOL_GPL(find_vpid);
269
e713d0da
SB
270/*
271 * attach_pid() must be called with the tasklist_lock write-held.
272 */
81907739 273void attach_pid(struct task_struct *task, enum pid_type type)
1da177e4 274{
81907739
ON
275 struct pid_link *link = &task->pids[type];
276 hlist_add_head_rcu(&link->node, &link->pid->tasks[type]);
1da177e4
LT
277}
278
24336eae
ON
279static void __change_pid(struct task_struct *task, enum pid_type type,
280 struct pid *new)
1da177e4 281{
92476d7f
EB
282 struct pid_link *link;
283 struct pid *pid;
284 int tmp;
1da177e4 285
92476d7f
EB
286 link = &task->pids[type];
287 pid = link->pid;
1da177e4 288
92476d7f 289 hlist_del_rcu(&link->node);
24336eae 290 link->pid = new;
1da177e4 291
92476d7f
EB
292 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
293 if (!hlist_empty(&pid->tasks[tmp]))
294 return;
1da177e4 295
92476d7f 296 free_pid(pid);
1da177e4
LT
297}
298
24336eae
ON
299void detach_pid(struct task_struct *task, enum pid_type type)
300{
301 __change_pid(task, type, NULL);
302}
303
304void change_pid(struct task_struct *task, enum pid_type type,
305 struct pid *pid)
306{
307 __change_pid(task, type, pid);
81907739 308 attach_pid(task, type);
24336eae
ON
309}
310
c18258c6 311/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
7ad5b3a5 312void transfer_pid(struct task_struct *old, struct task_struct *new,
c18258c6
EB
313 enum pid_type type)
314{
315 new->pids[type].pid = old->pids[type].pid;
316 hlist_replace_rcu(&old->pids[type].node, &new->pids[type].node);
c18258c6
EB
317}
318
7ad5b3a5 319struct task_struct *pid_task(struct pid *pid, enum pid_type type)
1da177e4 320{
92476d7f
EB
321 struct task_struct *result = NULL;
322 if (pid) {
323 struct hlist_node *first;
67bdbffd 324 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
db1466b3 325 lockdep_tasklist_lock_is_held());
92476d7f
EB
326 if (first)
327 result = hlist_entry(first, struct task_struct, pids[(type)].node);
328 }
329 return result;
330}
eccba068 331EXPORT_SYMBOL(pid_task);
1da177e4 332
92476d7f 333/*
9728e5d6 334 * Must be called under rcu_read_lock().
92476d7f 335 */
17f98dcf 336struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
92476d7f 337{
f78f5b90
PM
338 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
339 "find_task_by_pid_ns() needs rcu_read_lock() protection");
17f98dcf 340 return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
92476d7f 341}
1da177e4 342
228ebcbe
PE
343struct task_struct *find_task_by_vpid(pid_t vnr)
344{
17cf22c3 345 return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
228ebcbe 346}
228ebcbe 347
1a657f78
ON
348struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
349{
350 struct pid *pid;
351 rcu_read_lock();
2ae448ef
ON
352 if (type != PIDTYPE_PID)
353 task = task->group_leader;
81b1a832 354 pid = get_pid(rcu_dereference(task->pids[type].pid));
1a657f78
ON
355 rcu_read_unlock();
356 return pid;
357}
77c100c8 358EXPORT_SYMBOL_GPL(get_task_pid);
1a657f78 359
7ad5b3a5 360struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
92476d7f
EB
361{
362 struct task_struct *result;
363 rcu_read_lock();
364 result = pid_task(pid, type);
365 if (result)
366 get_task_struct(result);
367 rcu_read_unlock();
368 return result;
1da177e4 369}
77c100c8 370EXPORT_SYMBOL_GPL(get_pid_task);
1da177e4 371
92476d7f 372struct pid *find_get_pid(pid_t nr)
1da177e4
LT
373{
374 struct pid *pid;
375
92476d7f 376 rcu_read_lock();
198fe21b 377 pid = get_pid(find_vpid(nr));
92476d7f 378 rcu_read_unlock();
1da177e4 379
92476d7f 380 return pid;
1da177e4 381}
339caf2a 382EXPORT_SYMBOL_GPL(find_get_pid);
1da177e4 383
7af57294
PE
384pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
385{
386 struct upid *upid;
387 pid_t nr = 0;
388
389 if (pid && ns->level <= pid->level) {
390 upid = &pid->numbers[ns->level];
391 if (upid->ns == ns)
392 nr = upid->nr;
393 }
394 return nr;
395}
4f82f457 396EXPORT_SYMBOL_GPL(pid_nr_ns);
7af57294 397
44c4e1b2
EB
398pid_t pid_vnr(struct pid *pid)
399{
17cf22c3 400 return pid_nr_ns(pid, task_active_pid_ns(current));
44c4e1b2
EB
401}
402EXPORT_SYMBOL_GPL(pid_vnr);
403
52ee2dfd
ON
404pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
405 struct pid_namespace *ns)
2f2a3a46 406{
52ee2dfd
ON
407 pid_t nr = 0;
408
409 rcu_read_lock();
410 if (!ns)
17cf22c3 411 ns = task_active_pid_ns(current);
52ee2dfd 412 if (likely(pid_alive(task))) {
dd1c1f2f
ON
413 if (type != PIDTYPE_PID) {
414 if (type == __PIDTYPE_TGID)
415 type = PIDTYPE_PID;
52ee2dfd 416 task = task->group_leader;
dd1c1f2f 417 }
81b1a832 418 nr = pid_nr_ns(rcu_dereference(task->pids[type].pid), ns);
52ee2dfd
ON
419 }
420 rcu_read_unlock();
421
422 return nr;
2f2a3a46 423}
52ee2dfd 424EXPORT_SYMBOL(__task_pid_nr_ns);
2f2a3a46 425
61bce0f1
EB
426struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
427{
428 return ns_of_pid(task_pid(tsk));
429}
430EXPORT_SYMBOL_GPL(task_active_pid_ns);
431
0804ef4b 432/*
025dfdaf 433 * Used by proc to find the first pid that is greater than or equal to nr.
0804ef4b 434 *
e49859e7 435 * If there is a pid at nr this function is exactly the same as find_pid_ns.
0804ef4b 436 */
198fe21b 437struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
0804ef4b 438{
95846ecf 439 return idr_get_next(&ns->idr, &nr);
0804ef4b
EB
440}
441
1da177e4
LT
442/*
443 * The pid hash table is scaled according to the amount of memory in the
444 * machine. From a minimum of 16 slots up to 4096 slots at one gigabyte or
445 * more.
446 */
447void __init pidhash_init(void)
448{
2c85f51d 449 pid_hash = alloc_large_system_hash("PID", sizeof(*pid_hash), 0, 18,
3d375d78 450 HASH_EARLY | HASH_SMALL | HASH_ZERO,
31fe62b9
TB
451 &pidhash_shift, NULL,
452 0, 4096);
1da177e4
LT
453}
454
95846ecf 455void __init pid_idr_init(void)
1da177e4 456{
840d6fe7 457 /* Verify no one has done anything silly: */
c876ad76
EB
458 BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_HASH_ADDING);
459
72680a19
HB
460 /* bump default and minimum pid_max based on number of cpus */
461 pid_max = min(pid_max_max, max_t(int, pid_max,
462 PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
463 pid_max_min = max_t(int, pid_max_min,
464 PIDS_PER_CPU_MIN * num_possible_cpus());
465 pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
466
95846ecf 467 idr_init(&init_pid_ns.idr);
92476d7f 468
74bd59bb 469 init_pid_ns.pid_cachep = KMEM_CACHE(pid,
5d097056 470 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
1da177e4 471}