Lines Matching full:pid

3  * Generic pidhash and scalable, time-bounded PID allocator
9 * pid-structures are backing objects for tasks sharing a given ID to chain
17 * We have a list of bitmap pages, which bitmaps represent the PID space.
23 * Pid namespaces:
50 struct pid init_struct_pid = {
68 * PID-map pages start out as NULL, they get allocated upon
94 void put_pid(struct pid *pid) in put_pid() argument
98 if (!pid) in put_pid()
101 ns = pid->numbers[pid->level].ns; in put_pid()
102 if (refcount_dec_and_test(&pid->count)) { in put_pid()
103 kmem_cache_free(ns->pid_cachep, pid); in put_pid()
111 struct pid *pid = container_of(rhp, struct pid, rcu); in delayed_put_pid() local
112 put_pid(pid); in delayed_put_pid()
115 void free_pid(struct pid *pid) in free_pid() argument
122 for (i = 0; i <= pid->level; i++) { in free_pid()
123 struct upid *upid = pid->numbers + i; in free_pid()
128 /* When all that is left in the pid namespace in free_pid()
143 pidfs_remove_pid(pid); in free_pid()
146 call_rcu(&pid->rcu, delayed_put_pid); in free_pid()
149 void free_pids(struct pid **pids) in free_pids()
161 struct pid *alloc_pid(struct pid_namespace *ns, pid_t *set_tid, in alloc_pid()
164 struct pid *pid; in alloc_pid() local
173 * the most nested currently active PID namespace it tells alloc_pid() in alloc_pid()
174 * which PID to set for a process in that most nested PID namespace in alloc_pid()
175 * up to set_tid_size PID namespaces. It does not have to set the PID in alloc_pid()
176 * for a process in all nested PID namespaces but set_tid_size must in alloc_pid()
182 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); in alloc_pid()
183 if (!pid) in alloc_pid()
187 pid->level = ns->level; in alloc_pid()
200 * Also fail if a PID != 1 is requested and in alloc_pid()
201 * no PID 1 exists. in alloc_pid()
218 * If ENOSPC is returned it means that the PID is in alloc_pid()
226 * init really needs pid 1, but after reaching the in alloc_pid()
234 * a partially initialized PID (see below). in alloc_pid()
247 pid->numbers[i].nr = nr; in alloc_pid()
248 pid->numbers[i].ns = tmp; in alloc_pid()
254 * where the child subreaper has already exited and the pid in alloc_pid()
257 * documented behavior for pid namespaces. So we can't easily in alloc_pid()
263 refcount_set(&pid->count, 1); in alloc_pid()
264 spin_lock_init(&pid->lock); in alloc_pid()
266 INIT_HLIST_HEAD(&pid->tasks[type]); in alloc_pid()
268 init_waitqueue_head(&pid->wait_pidfd); in alloc_pid()
269 INIT_HLIST_HEAD(&pid->inodes); in alloc_pid()
271 upid = pid->numbers + ns->level; in alloc_pid()
276 pidfs_add_pid(pid); in alloc_pid()
277 for ( ; upid >= pid->numbers; --upid) { in alloc_pid()
278 /* Make the PID visible to find_pid_ns. */ in alloc_pid()
279 idr_replace(&upid->ns->idr, pid, upid->nr); in alloc_pid()
285 return pid; in alloc_pid()
295 upid = pid->numbers + i; in alloc_pid()
299 /* On failure to allocate the first pid, reset the state */ in alloc_pid()
305 kmem_cache_free(ns->pid_cachep, pid); in alloc_pid()
316 struct pid *find_pid_ns(int nr, struct pid_namespace *ns) in find_pid_ns()
322 struct pid *find_vpid(int nr) in find_vpid()
328 static struct pid **task_pid_ptr(struct task_struct *task, enum pid_type type) in task_pid_ptr()
340 struct pid *pid; in attach_pid() local
344 pid = *task_pid_ptr(task, type); in attach_pid()
345 hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); in attach_pid()
348 static void __change_pid(struct pid **pids, struct task_struct *task, in __change_pid()
349 enum pid_type type, struct pid *new) in __change_pid()
351 struct pid **pid_ptr, *pid; in __change_pid() local
357 pid = *pid_ptr; in __change_pid()
363 WARN_ON_ONCE(pid_has_task(pid, PIDTYPE_PID)); in __change_pid()
364 wake_up_all(&pid->wait_pidfd); in __change_pid()
368 if (pid_has_task(pid, tmp)) in __change_pid()
372 pids[type] = pid; in __change_pid()
375 void detach_pid(struct pid **pids, struct task_struct *task, enum pid_type type) in detach_pid()
380 void change_pid(struct pid **pids, struct task_struct *task, enum pid_type type, in change_pid()
381 struct pid *pid) in change_pid() argument
383 __change_pid(pids, task, type, pid); in change_pid()
389 struct pid *pid1 = left->thread_pid; in exchange_tids()
390 struct pid *pid2 = right->thread_pid; in exchange_tids()
399 /* Swap the per task_struct pid */ in exchange_tids()
404 WRITE_ONCE(left->pid, pid_nr(pid2)); in exchange_tids()
405 WRITE_ONCE(right->pid, pid_nr(pid1)); in exchange_tids()
417 struct task_struct *pid_task(struct pid *pid, enum pid_type type) in pid_task() argument
420 if (pid) { in pid_task()
422 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), in pid_task()
459 struct pid *get_task_pid(struct task_struct *task, enum pid_type type) in get_task_pid()
461 struct pid *pid; in get_task_pid() local
463 pid = get_pid(rcu_dereference(*task_pid_ptr(task, type))); in get_task_pid()
465 return pid; in get_task_pid()
469 struct task_struct *get_pid_task(struct pid *pid, enum pid_type type) in get_pid_task() argument
473 result = pid_task(pid, type); in get_pid_task()
481 struct pid *find_get_pid(pid_t nr) in find_get_pid()
483 struct pid *pid; in find_get_pid() local
486 pid = get_pid(find_vpid(nr)); in find_get_pid()
489 return pid; in find_get_pid()
493 pid_t pid_nr_ns(struct pid *pid, struct pid_namespace *ns) in pid_nr_ns() argument
498 if (pid && ns->level <= pid->level) { in pid_nr_ns()
499 upid = &pid->numbers[ns->level]; in pid_nr_ns()
507 pid_t pid_vnr(struct pid *pid) in pid_vnr() argument
509 return pid_nr_ns(pid, task_active_pid_ns(current)); in pid_vnr()
535 * Used by proc to find the first pid that is greater than or equal to nr.
537 * If there is a pid at nr this function is exactly the same as find_pid_ns.
539 struct pid *find_ge_pid(int nr, struct pid_namespace *ns) in find_ge_pid()
545 struct pid *pidfd_get_pid(unsigned int fd, unsigned int *flags) in pidfd_get_pid()
548 struct pid *pid; in pidfd_get_pid() local
553 pid = pidfd_pid(fd_file(f)); in pidfd_get_pid()
554 if (!IS_ERR(pid)) { in pidfd_get_pid()
555 get_pid(pid); in pidfd_get_pid()
558 return pid; in pidfd_get_pid()
576 struct pid *pid; in pidfd_get_task() local
583 pid = get_task_pid(current, type); in pidfd_get_task()
587 pid = get_task_pid(current, type); in pidfd_get_task()
590 pid = pidfd_get_pid(pidfd, &f_flags); in pidfd_get_task()
591 if (IS_ERR(pid)) in pidfd_get_task()
592 return ERR_CAST(pid); in pidfd_get_task()
597 task = get_pid_task(pid, type); in pidfd_get_task()
598 put_pid(pid); in pidfd_get_task()
607 * pidfd_create() - Create a new pid file descriptor.
609 * @pid: struct pid that the pidfd will reference
612 * This creates a new pid file descriptor with the O_CLOEXEC flag set.
622 static int pidfd_create(struct pid *pid, unsigned int flags) in pidfd_create() argument
627 pidfd = pidfd_prepare(pid, flags, &pidfd_file); in pidfd_create()
636 * sys_pidfd_open() - Open new pid file descriptor.
638 * @pid: pid for which to retrieve a pidfd
641 * This creates a new pid file descriptor with the O_CLOEXEC flag set for
642 * the task identified by @pid. Without PIDFD_THREAD flag the target task
648 SYSCALL_DEFINE2(pidfd_open, pid_t, pid, unsigned int, flags) in SYSCALL_DEFINE2() argument
651 struct pid *p; in SYSCALL_DEFINE2()
656 if (pid <= 0) in SYSCALL_DEFINE2()
659 p = find_get_pid(pid); in SYSCALL_DEFINE2()
784 init_pid_ns.pid_cachep = kmem_cache_create("pid", in pid_idr_init()
785 struct_size_t(struct pid, numbers, 1), in pid_idr_init()
786 __alignof__(struct pid), in pid_idr_init()
839 static int pidfd_getfd(struct pid *pid, int fd) in pidfd_getfd() argument
845 task = get_pid_task(pid, PIDTYPE_PID); in pidfd_getfd()
879 struct pid *pid; in SYSCALL_DEFINE3() local
889 pid = pidfd_pid(fd_file(f)); in SYSCALL_DEFINE3()
890 if (IS_ERR(pid)) in SYSCALL_DEFINE3()
891 return PTR_ERR(pid); in SYSCALL_DEFINE3()
893 return pidfd_getfd(pid, fd); in SYSCALL_DEFINE3()