ALSA: pcm: Fix missing check of the new non-cached buffer type
[linux-2.6-block.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>
f57e515a 40#include <linux/refcount.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 46struct pid init_struct_pid = {
f57e515a 47 .count = REFCOUNT_INIT(1),
e1e871af
DH
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;
f57e515a 111 if (refcount_dec_and_test(&pid->count)) {
baf8f0f8 112 kmem_cache_free(ns->pid_cachep, pid);
b461cc03 113 put_pid_ns(ns);
8ef047aa 114 }
92476d7f 115}
bbf73147 116EXPORT_SYMBOL_GPL(put_pid);
92476d7f
EB
117
118static void delayed_put_pid(struct rcu_head *rhp)
119{
120 struct pid *pid = container_of(rhp, struct pid, rcu);
121 put_pid(pid);
122}
123
7ad5b3a5 124void free_pid(struct pid *pid)
92476d7f
EB
125{
126 /* We can be called with write_lock_irq(&tasklist_lock) held */
8ef047aa 127 int i;
92476d7f
EB
128 unsigned long flags;
129
130 spin_lock_irqsave(&pidmap_lock, flags);
0a01f2cc
EB
131 for (i = 0; i <= pid->level; i++) {
132 struct upid *upid = pid->numbers + i;
af4b8a83 133 struct pid_namespace *ns = upid->ns;
e8cfbc24 134 switch (--ns->pid_allocated) {
a6064885 135 case 2:
af4b8a83
EB
136 case 1:
137 /* When all that is left in the pid namespace
138 * is the reaper wake up the reaper. The reaper
139 * may be sleeping in zap_pid_ns_processes().
140 */
141 wake_up_process(ns->child_reaper);
142 break;
e8cfbc24 143 case PIDNS_ADDING:
314a8ad0
ON
144 /* Handle a fork failure of the first process */
145 WARN_ON(ns->child_reaper);
e8cfbc24 146 ns->pid_allocated = 0;
314a8ad0 147 /* fall through */
af4b8a83 148 case 0:
af4b8a83
EB
149 schedule_work(&ns->proc_work);
150 break;
5e1182de 151 }
95846ecf
GS
152
153 idr_remove(&ns->idr, upid->nr);
0a01f2cc 154 }
92476d7f
EB
155 spin_unlock_irqrestore(&pidmap_lock, flags);
156
92476d7f
EB
157 call_rcu(&pid->rcu, delayed_put_pid);
158}
159
8ef047aa 160struct pid *alloc_pid(struct pid_namespace *ns)
92476d7f
EB
161{
162 struct pid *pid;
163 enum pid_type type;
8ef047aa
PE
164 int i, nr;
165 struct pid_namespace *tmp;
198fe21b 166 struct upid *upid;
35f71bc0 167 int retval = -ENOMEM;
92476d7f 168
baf8f0f8 169 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL);
92476d7f 170 if (!pid)
35f71bc0 171 return ERR_PTR(retval);
92476d7f 172
8ef047aa 173 tmp = ns;
0a01f2cc 174 pid->level = ns->level;
95846ecf 175
8ef047aa 176 for (i = ns->level; i >= 0; i--) {
95846ecf
GS
177 int pid_min = 1;
178
179 idr_preload(GFP_KERNEL);
180 spin_lock_irq(&pidmap_lock);
181
182 /*
183 * init really needs pid 1, but after reaching the maximum
184 * wrap back to RESERVED_PIDS
185 */
186 if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS)
187 pid_min = RESERVED_PIDS;
188
189 /*
190 * Store a null pointer so find_pid_ns does not find
191 * a partially initialized PID (see below).
192 */
193 nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min,
194 pid_max, GFP_ATOMIC);
195 spin_unlock_irq(&pidmap_lock);
196 idr_preload_end();
197
287980e4 198 if (nr < 0) {
f83606f5 199 retval = (nr == -ENOSPC) ? -EAGAIN : nr;
8ef047aa 200 goto out_free;
35f71bc0 201 }
92476d7f 202
8ef047aa
PE
203 pid->numbers[i].nr = nr;
204 pid->numbers[i].ns = tmp;
205 tmp = tmp->parent;
206 }
207
0a01f2cc 208 if (unlikely(is_child_reaper(pid))) {
c0ee5549 209 if (pid_ns_prepare_proc(ns))
0a01f2cc
EB
210 goto out_free;
211 }
212
b461cc03 213 get_pid_ns(ns);
f57e515a 214 refcount_set(&pid->count, 1);
92476d7f
EB
215 for (type = 0; type < PIDTYPE_MAX; ++type)
216 INIT_HLIST_HEAD(&pid->tasks[type]);
217
b53b0b9d
JFG
218 init_waitqueue_head(&pid->wait_pidfd);
219
417e3152 220 upid = pid->numbers + ns->level;
92476d7f 221 spin_lock_irq(&pidmap_lock);
e8cfbc24 222 if (!(ns->pid_allocated & PIDNS_ADDING))
5e1182de 223 goto out_unlock;
0a01f2cc 224 for ( ; upid >= pid->numbers; --upid) {
95846ecf
GS
225 /* Make the PID visible to find_pid_ns. */
226 idr_replace(&upid->ns->idr, pid, upid->nr);
e8cfbc24 227 upid->ns->pid_allocated++;
0a01f2cc 228 }
92476d7f
EB
229 spin_unlock_irq(&pidmap_lock);
230
92476d7f
EB
231 return pid;
232
5e1182de 233out_unlock:
6e666884 234 spin_unlock_irq(&pidmap_lock);
24c037eb
ON
235 put_pid_ns(ns);
236
92476d7f 237out_free:
95846ecf 238 spin_lock_irq(&pidmap_lock);
1a80dade
MW
239 while (++i <= ns->level) {
240 upid = pid->numbers + i;
241 idr_remove(&upid->ns->idr, upid->nr);
242 }
95846ecf 243
c0ee5549
EB
244 /* On failure to allocate the first pid, reset the state */
245 if (ns->pid_allocated == PIDNS_ADDING)
246 idr_set_cursor(&ns->idr, 0);
247
95846ecf 248 spin_unlock_irq(&pidmap_lock);
8ef047aa 249
baf8f0f8 250 kmem_cache_free(ns->pid_cachep, pid);
35f71bc0 251 return ERR_PTR(retval);
92476d7f
EB
252}
253
c876ad76
EB
254void disable_pid_allocation(struct pid_namespace *ns)
255{
256 spin_lock_irq(&pidmap_lock);
e8cfbc24 257 ns->pid_allocated &= ~PIDNS_ADDING;
c876ad76
EB
258 spin_unlock_irq(&pidmap_lock);
259}
260
7ad5b3a5 261struct pid *find_pid_ns(int nr, struct pid_namespace *ns)
1da177e4 262{
e8cfbc24 263 return idr_find(&ns->idr, nr);
1da177e4 264}
198fe21b 265EXPORT_SYMBOL_GPL(find_pid_ns);
1da177e4 266
8990571e
PE
267struct pid *find_vpid(int nr)
268{
17cf22c3 269 return find_pid_ns(nr, task_active_pid_ns(current));
8990571e
PE
270}
271EXPORT_SYMBOL_GPL(find_vpid);
272
2c470475
EB
273static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type)
274{
275 return (type == PIDTYPE_PID) ?
276 &task->thread_pid :
2c470475
EB
277 &task->signal->pids[type];
278}
279
e713d0da
SB
280/*
281 * attach_pid() must be called with the tasklist_lock write-held.
282 */
81907739 283void attach_pid(struct task_struct *task, enum pid_type type)
1da177e4 284{
2c470475
EB
285 struct pid *pid = *task_pid_ptr(task, type);
286 hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]);
1da177e4
LT
287}
288
24336eae
ON
289static void __change_pid(struct task_struct *task, enum pid_type type,
290 struct pid *new)
1da177e4 291{
2c470475 292 struct pid **pid_ptr = task_pid_ptr(task, type);
92476d7f
EB
293 struct pid *pid;
294 int tmp;
1da177e4 295
2c470475 296 pid = *pid_ptr;
1da177e4 297
2c470475
EB
298 hlist_del_rcu(&task->pid_links[type]);
299 *pid_ptr = new;
1da177e4 300
92476d7f
EB
301 for (tmp = PIDTYPE_MAX; --tmp >= 0; )
302 if (!hlist_empty(&pid->tasks[tmp]))
303 return;
1da177e4 304
92476d7f 305 free_pid(pid);
1da177e4
LT
306}
307
24336eae
ON
308void detach_pid(struct task_struct *task, enum pid_type type)
309{
310 __change_pid(task, type, NULL);
311}
312
313void change_pid(struct task_struct *task, enum pid_type type,
314 struct pid *pid)
315{
316 __change_pid(task, type, pid);
81907739 317 attach_pid(task, type);
24336eae
ON
318}
319
c18258c6 320/* transfer_pid is an optimization of attach_pid(new), detach_pid(old) */
7ad5b3a5 321void transfer_pid(struct task_struct *old, struct task_struct *new,
c18258c6
EB
322 enum pid_type type)
323{
2c470475
EB
324 if (type == PIDTYPE_PID)
325 new->thread_pid = old->thread_pid;
326 hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]);
c18258c6
EB
327}
328
7ad5b3a5 329struct task_struct *pid_task(struct pid *pid, enum pid_type type)
1da177e4 330{
92476d7f
EB
331 struct task_struct *result = NULL;
332 if (pid) {
333 struct hlist_node *first;
67bdbffd 334 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]),
db1466b3 335 lockdep_tasklist_lock_is_held());
92476d7f 336 if (first)
2c470475 337 result = hlist_entry(first, struct task_struct, pid_links[(type)]);
92476d7f
EB
338 }
339 return result;
340}
eccba068 341EXPORT_SYMBOL(pid_task);
1da177e4 342
92476d7f 343/*
9728e5d6 344 * Must be called under rcu_read_lock().
92476d7f 345 */
17f98dcf 346struct task_struct *find_task_by_pid_ns(pid_t nr, struct pid_namespace *ns)
92476d7f 347{
f78f5b90
PM
348 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
349 "find_task_by_pid_ns() needs rcu_read_lock() protection");
17f98dcf 350 return pid_task(find_pid_ns(nr, ns), PIDTYPE_PID);
92476d7f 351}
1da177e4 352
228ebcbe
PE
353struct task_struct *find_task_by_vpid(pid_t vnr)
354{
17cf22c3 355 return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
228ebcbe 356}
228ebcbe 357
2ee08260
MR
358struct task_struct *find_get_task_by_vpid(pid_t nr)
359{
360 struct task_struct *task;
361
362 rcu_read_lock();
363 task = find_task_by_vpid(nr);
364 if (task)
365 get_task_struct(task);
366 rcu_read_unlock();
367
368 return task;
369}
370
1a657f78
ON
371struct pid *get_task_pid(struct task_struct *task, enum pid_type type)
372{
373 struct pid *pid;
374 rcu_read_lock();
2c470475 375 pid = get_pid(rcu_dereference(*task_pid_ptr(task, type)));
1a657f78
ON
376 rcu_read_unlock();
377 return pid;
378}
77c100c8 379EXPORT_SYMBOL_GPL(get_task_pid);
1a657f78 380
7ad5b3a5 381struct task_struct *get_pid_task(struct pid *pid, enum pid_type type)
92476d7f
EB
382{
383 struct task_struct *result;
384 rcu_read_lock();
385 result = pid_task(pid, type);
386 if (result)
387 get_task_struct(result);
388 rcu_read_unlock();
389 return result;
1da177e4 390}
77c100c8 391EXPORT_SYMBOL_GPL(get_pid_task);
1da177e4 392
92476d7f 393struct pid *find_get_pid(pid_t nr)
1da177e4
LT
394{
395 struct pid *pid;
396
92476d7f 397 rcu_read_lock();
198fe21b 398 pid = get_pid(find_vpid(nr));
92476d7f 399 rcu_read_unlock();
1da177e4 400
92476d7f 401 return pid;
1da177e4 402}
339caf2a 403EXPORT_SYMBOL_GPL(find_get_pid);
1da177e4 404
7af57294
PE
405pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns)
406{
407 struct upid *upid;
408 pid_t nr = 0;
409
410 if (pid && ns->level <= pid->level) {
411 upid = &pid->numbers[ns->level];
412 if (upid->ns == ns)
413 nr = upid->nr;
414 }
415 return nr;
416}
4f82f457 417EXPORT_SYMBOL_GPL(pid_nr_ns);
7af57294 418
44c4e1b2
EB
419pid_t pid_vnr(struct pid *pid)
420{
17cf22c3 421 return pid_nr_ns(pid, task_active_pid_ns(current));
44c4e1b2
EB
422}
423EXPORT_SYMBOL_GPL(pid_vnr);
424
52ee2dfd
ON
425pid_t __task_pid_nr_ns(struct task_struct *task, enum pid_type type,
426 struct pid_namespace *ns)
2f2a3a46 427{
52ee2dfd
ON
428 pid_t nr = 0;
429
430 rcu_read_lock();
431 if (!ns)
17cf22c3 432 ns = task_active_pid_ns(current);
2c470475
EB
433 if (likely(pid_alive(task)))
434 nr = pid_nr_ns(rcu_dereference(*task_pid_ptr(task, type)), ns);
52ee2dfd
ON
435 rcu_read_unlock();
436
437 return nr;
2f2a3a46 438}
52ee2dfd 439EXPORT_SYMBOL(__task_pid_nr_ns);
2f2a3a46 440
61bce0f1
EB
441struct pid_namespace *task_active_pid_ns(struct task_struct *tsk)
442{
443 return ns_of_pid(task_pid(tsk));
444}
445EXPORT_SYMBOL_GPL(task_active_pid_ns);
446
0804ef4b 447/*
025dfdaf 448 * Used by proc to find the first pid that is greater than or equal to nr.
0804ef4b 449 *
e49859e7 450 * If there is a pid at nr this function is exactly the same as find_pid_ns.
0804ef4b 451 */
198fe21b 452struct pid *find_ge_pid(int nr, struct pid_namespace *ns)
0804ef4b 453{
95846ecf 454 return idr_get_next(&ns->idr, &nr);
0804ef4b
EB
455}
456
32fcb426
CB
457/**
458 * pidfd_create() - Create a new pid file descriptor.
459 *
460 * @pid: struct pid that the pidfd will reference
461 *
462 * This creates a new pid file descriptor with the O_CLOEXEC flag set.
463 *
464 * Note, that this function can only be called after the fd table has
465 * been unshared to avoid leaking the pidfd to the new process.
466 *
467 * Return: On success, a cloexec pidfd is returned.
468 * On error, a negative errno number will be returned.
469 */
470static int pidfd_create(struct pid *pid)
471{
472 int fd;
473
474 fd = anon_inode_getfd("[pidfd]", &pidfd_fops, get_pid(pid),
475 O_RDWR | O_CLOEXEC);
476 if (fd < 0)
477 put_pid(pid);
478
479 return fd;
480}
481
482/**
483 * pidfd_open() - Open new pid file descriptor.
484 *
485 * @pid: pid for which to retrieve a pidfd
486 * @flags: flags to pass
487 *
488 * This creates a new pid file descriptor with the O_CLOEXEC flag set for
489 * the process identified by @pid. Currently, the process identified by
490 * @pid must be a thread-group leader. This restriction currently exists
491 * for all aspects of pidfds including pidfd creation (CLONE_PIDFD cannot
492 * be used with CLONE_THREAD) and pidfd polling (only supports thread group
493 * leaders).
494 *
495 * Return: On success, a cloexec pidfd is returned.
496 * On error, a negative errno number will be returned.
497 */
498SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags)
499{
500 int fd, ret;
501 struct pid *p;
502
503 if (flags)
504 return -EINVAL;
505
506 if (pid <= 0)
507 return -EINVAL;
508
509 p = find_get_pid(pid);
510 if (!p)
511 return -ESRCH;
512
513 ret = 0;
514 rcu_read_lock();
515 if (!pid_task(p, PIDTYPE_TGID))
516 ret = -EINVAL;
517 rcu_read_unlock();
518
519 fd = ret ?: pidfd_create(p);
520 put_pid(p);
521 return fd;
522}
523
95846ecf 524void __init pid_idr_init(void)
1da177e4 525{
840d6fe7 526 /* Verify no one has done anything silly: */
e8cfbc24 527 BUILD_BUG_ON(PID_MAX_LIMIT >= PIDNS_ADDING);
c876ad76 528
72680a19
HB
529 /* bump default and minimum pid_max based on number of cpus */
530 pid_max = min(pid_max_max, max_t(int, pid_max,
531 PIDS_PER_CPU_DEFAULT * num_possible_cpus()));
532 pid_max_min = max_t(int, pid_max_min,
533 PIDS_PER_CPU_MIN * num_possible_cpus());
534 pr_info("pid_max: default: %u minimum: %u\n", pid_max, pid_max_min);
535
95846ecf 536 idr_init(&init_pid_ns.idr);
92476d7f 537
74bd59bb 538 init_pid_ns.pid_cachep = KMEM_CACHE(pid,
5d097056 539 SLAB_HWCACHE_ALIGN | SLAB_PANIC | SLAB_ACCOUNT);
1da177e4 540}