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