Lines Matching +full:left +full:- +full:most

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic pidhash and scalable, time-bounded PID allocator
5 * (C) 2002-2003 Nadia Yvette Chambers, IBM
7 * (C) 2002-2004 Ingo Molnar, Red Hat
9 * pid-structures are backing objects for tasks sharing a given ID to chain
13 * The hash is always changed with the tasklist_lock write-acquired,
15 * read-acquired, so there's no additional SMP locking needed here.
18 * Allocating and freeing PIDs is completely lockless. The worst-case
20 * allocated already: the scanning of 32 list entries and at most PAGE_SIZE
70 * PID-map pages start out as NULL, they get allocated upon
94 * detach_pid()->free_pid() and another cpu that does
112 ns = pid->numbers[pid->level].ns; in put_pid()
113 if (refcount_dec_and_test(&pid->count)) { in put_pid()
114 kmem_cache_free(ns->pid_cachep, pid); in put_pid()
133 for (i = 0; i <= pid->level; i++) { in free_pid()
134 struct upid *upid = pid->numbers + i; in free_pid()
135 struct pid_namespace *ns = upid->ns; in free_pid()
136 switch (--ns->pid_allocated) { in free_pid()
139 /* When all that is left in the pid namespace in free_pid()
143 wake_up_process(ns->child_reaper); in free_pid()
147 WARN_ON(ns->child_reaper); in free_pid()
148 ns->pid_allocated = 0; in free_pid()
152 idr_remove(&ns->idr, upid->nr); in free_pid()
156 call_rcu(&pid->rcu, delayed_put_pid); in free_pid()
167 int retval = -ENOMEM; in alloc_pid()
171 * the most nested currently active PID namespace it tells alloc_pid() in alloc_pid()
172 * which PID to set for a process in that most nested PID namespace in alloc_pid()
175 * never be greater than the current ns->level + 1. in alloc_pid()
177 if (set_tid_size > ns->level + 1) in alloc_pid()
178 return ERR_PTR(-EINVAL); in alloc_pid()
180 pid = kmem_cache_alloc(ns->pid_cachep, GFP_KERNEL); in alloc_pid()
185 pid->level = ns->level; in alloc_pid()
187 for (i = ns->level; i >= 0; i--) { in alloc_pid()
191 tid = set_tid[ns->level - i]; in alloc_pid()
193 retval = -EINVAL; in alloc_pid()
200 if (tid != 1 && !tmp->child_reaper) in alloc_pid()
202 retval = -EPERM; in alloc_pid()
203 if (!checkpoint_restore_ns_capable(tmp->user_ns)) in alloc_pid()
205 set_tid_size--; in alloc_pid()
212 nr = idr_alloc(&tmp->idr, NULL, tid, in alloc_pid()
218 if (nr == -ENOSPC) in alloc_pid()
219 nr = -EEXIST; in alloc_pid()
226 if (idr_get_cursor(&tmp->idr) > RESERVED_PIDS) in alloc_pid()
233 nr = idr_alloc_cyclic(&tmp->idr, NULL, pid_min, in alloc_pid()
240 retval = (nr == -ENOSPC) ? -EAGAIN : nr; in alloc_pid()
244 pid->numbers[i].nr = nr; in alloc_pid()
245 pid->numbers[i].ns = tmp; in alloc_pid()
246 tmp = tmp->parent; in alloc_pid()
250 * ENOMEM is not the most obvious choice especially for the case in alloc_pid()
257 retval = -ENOMEM; in alloc_pid()
260 refcount_set(&pid->count, 1); in alloc_pid()
261 spin_lock_init(&pid->lock); in alloc_pid()
263 INIT_HLIST_HEAD(&pid->tasks[type]); in alloc_pid()
265 init_waitqueue_head(&pid->wait_pidfd); in alloc_pid()
266 INIT_HLIST_HEAD(&pid->inodes); in alloc_pid()
268 upid = pid->numbers + ns->level; in alloc_pid()
270 if (!(ns->pid_allocated & PIDNS_ADDING)) in alloc_pid()
272 for ( ; upid >= pid->numbers; --upid) { in alloc_pid()
274 idr_replace(&upid->ns->idr, pid, upid->nr); in alloc_pid()
275 upid->ns->pid_allocated++; in alloc_pid()
287 while (++i <= ns->level) { in alloc_pid()
288 upid = pid->numbers + i; in alloc_pid()
289 idr_remove(&upid->ns->idr, upid->nr); in alloc_pid()
293 if (ns->pid_allocated == PIDNS_ADDING) in alloc_pid()
294 idr_set_cursor(&ns->idr, 0); in alloc_pid()
298 kmem_cache_free(ns->pid_cachep, pid); in alloc_pid()
305 ns->pid_allocated &= ~PIDNS_ADDING; in disable_pid_allocation()
311 return idr_find(&ns->idr, nr); in find_pid_ns()
324 &task->thread_pid : in task_pid_ptr()
325 &task->signal->pids[type]; in task_pid_ptr()
329 * attach_pid() must be called with the tasklist_lock write-held.
334 hlist_add_head_rcu(&task->pid_links[type], &pid->tasks[type]); in attach_pid()
346 hlist_del_rcu(&task->pid_links[type]); in __change_pid()
349 for (tmp = PIDTYPE_MAX; --tmp >= 0; ) in __change_pid()
368 void exchange_tids(struct task_struct *left, struct task_struct *right) in exchange_tids() argument
370 struct pid *pid1 = left->thread_pid; in exchange_tids()
371 struct pid *pid2 = right->thread_pid; in exchange_tids()
372 struct hlist_head *head1 = &pid1->tasks[PIDTYPE_PID]; in exchange_tids()
373 struct hlist_head *head2 = &pid2->tasks[PIDTYPE_PID]; in exchange_tids()
379 rcu_assign_pointer(left->thread_pid, pid2); in exchange_tids()
380 rcu_assign_pointer(right->thread_pid, pid1); in exchange_tids()
383 WRITE_ONCE(left->pid, pid_nr(pid2)); in exchange_tids()
384 WRITE_ONCE(right->pid, pid_nr(pid1)); in exchange_tids()
392 new->thread_pid = old->thread_pid; in transfer_pid()
393 hlist_replace_rcu(&old->pid_links[type], &new->pid_links[type]); in transfer_pid()
401 first = rcu_dereference_check(hlist_first_rcu(&pid->tasks[type]), in pid_task()
477 if (pid && ns->level <= pid->level) { in pid_nr_ns()
478 upid = &pid->numbers[ns->level]; in pid_nr_ns()
479 if (upid->ns == ns) in pid_nr_ns()
480 nr = upid->nr; in pid_nr_ns()
520 return idr_get_next(&ns->idr, &nr); in find_ge_pid()
530 return ERR_PTR(-EBADF); in pidfd_get_pid()
535 *flags = f.file->f_flags; in pidfd_get_pid()
543 * pidfd_create() - Create a new pid file descriptor.
569 * pidfd_open() - Open new pid file descriptor.
576 * @pid must be a thread-group leader. This restriction currently exists
590 return -EINVAL; in SYSCALL_DEFINE2()
593 return -EINVAL; in SYSCALL_DEFINE2()
597 return -ESRCH; in SYSCALL_DEFINE2()
602 fd = -EINVAL; in SYSCALL_DEFINE2()
631 ret = mutex_lock_killable(&task->signal->exec_update_mutex); in __pidfd_fget()
638 file = ERR_PTR(-EPERM); in __pidfd_fget()
640 mutex_unlock(&task->signal->exec_update_mutex); in __pidfd_fget()
642 return file ?: ERR_PTR(-EBADF); in __pidfd_fget()
653 return -ESRCH; in pidfd_getfd()
667 * sys_pidfd_getfd() - Get a file descriptor from another process
689 /* flags is currently unused - make sure it's unset */ in SYSCALL_DEFINE3()
691 return -EINVAL; in SYSCALL_DEFINE3()
695 return -EBADF; in SYSCALL_DEFINE3()