1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/kernel/exit.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/slab.h>
104eb5aaa3SIngo Molnar #include <linux/sched/autogroup.h>
116e84f315SIngo Molnar #include <linux/sched/mm.h>
1203441a34SIngo Molnar #include <linux/sched/stat.h>
1329930025SIngo Molnar #include <linux/sched/task.h>
1468db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
1532ef5517SIngo Molnar #include <linux/sched/cputime.h>
161da177e4SLinus Torvalds #include <linux/interrupt.h>
171da177e4SLinus Torvalds #include <linux/module.h>
18c59ede7bSRandy.Dunlap #include <linux/capability.h>
191da177e4SLinus Torvalds #include <linux/completion.h>
201da177e4SLinus Torvalds #include <linux/personality.h>
211da177e4SLinus Torvalds #include <linux/tty.h>
22da9cbc87SJens Axboe #include <linux/iocontext.h>
231da177e4SLinus Torvalds #include <linux/key.h>
241da177e4SLinus Torvalds #include <linux/cpu.h>
251da177e4SLinus Torvalds #include <linux/acct.h>
268f0ab514SJay Lan #include <linux/tsacct_kern.h>
271da177e4SLinus Torvalds #include <linux/file.h>
2880d26af8SMandeep Singh Baines #include <linux/freezer.h>
291da177e4SLinus Torvalds #include <linux/binfmts.h>
30ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
3184d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
321da177e4SLinus Torvalds #include <linux/ptrace.h>
331da177e4SLinus Torvalds #include <linux/profile.h>
341da177e4SLinus Torvalds #include <linux/mount.h>
351da177e4SLinus Torvalds #include <linux/proc_fs.h>
3649d769d5SEric W. Biederman #include <linux/kthread.h>
371da177e4SLinus Torvalds #include <linux/mempolicy.h>
38c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
39ca74e92bSShailabh Nagar #include <linux/delayacct.h>
40b4f48b63SPaul Menage #include <linux/cgroup.h>
411da177e4SLinus Torvalds #include <linux/syscalls.h>
427ed20e1aSJesper Juhl #include <linux/signal.h>
436a14c5c9SOleg Nesterov #include <linux/posix-timers.h>
449f46080cSMatt Helsley #include <linux/cn_proc.h>
45de5097c2SIngo Molnar #include <linux/mutex.h>
460771dfefSIngo Molnar #include <linux/futex.h>
47b92ce558SJens Axboe #include <linux/pipe_fs_i.h>
48fa84cb93SAl Viro #include <linux/audit.h> /* for audit_free() */
4983cc5ed3SAdrian Bunk #include <linux/resource.h>
506eaeeabaSEric Dumazet #include <linux/task_io_accounting_ops.h>
51355f841aSEric W. Biederman #include <linux/blkdev.h>
52355f841aSEric W. Biederman #include <linux/task_work.h>
535ad4e53bSAl Viro #include <linux/fs_struct.h>
54d84f4f99SDavid Howells #include <linux/init_task.h>
55cdd6c482SIngo Molnar #include <linux/perf_event.h>
56ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
5724f1e32cSFrederic Weisbecker #include <linux/hw_breakpoint.h>
583d5992d2SYing Han #include <linux/oom.h>
5954848d73SWu Fengguang #include <linux/writeback.h>
6040401530SAl Viro #include <linux/shm.h>
615c9a8750SDmitry Vyukov #include <linux/kcov.h>
6250b5e49cSAlexander Potapenko #include <linux/kmsan.h>
6353d3eaa3SNicolas Pitre #include <linux/random.h>
648f95c90cSDavidlohr Bueso #include <linux/rcuwait.h>
657e95a225SAl Viro #include <linux/compat.h>
66b1b6b5a3SPavel Begunkov #include <linux/io_uring.h>
67670721c7SThomas Gleixner #include <linux/kprobes.h>
6854ecbe6fSMasami Hiramatsu #include <linux/rethook.h>
699db89b41SKees Cook #include <linux/sysfs.h>
70fd593511SBeau Belgrave #include <linux/user_events.h>
717c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
7245135229SChristian Brauner #include <linux/pidfs.h>
736dfeff09SMatthew Wilcox (Oracle)
746dfeff09SMatthew Wilcox (Oracle) #include <uapi/linux/wait.h>
756dfeff09SMatthew Wilcox (Oracle)
761da177e4SLinus Torvalds #include <asm/unistd.h>
771da177e4SLinus Torvalds #include <asm/mmu_context.h>
781da177e4SLinus Torvalds
792e521a20SJens Axboe #include "exit.h"
802e521a20SJens Axboe
81d4ccd54dSJann Horn /*
82d4ccd54dSJann Horn * The default value should be high enough to not crash a system that randomly
83d4ccd54dSJann Horn * crashes its kernel from time to time, but low enough to at least not permit
84d4ccd54dSJann Horn * overflowing 32-bit refcounts or the ldsem writer count.
85d4ccd54dSJann Horn */
86d4ccd54dSJann Horn static unsigned int oops_limit = 10000;
87d4ccd54dSJann Horn
88d4ccd54dSJann Horn #ifdef CONFIG_SYSCTL
891751f872SJoel Granados static const struct ctl_table kern_exit_table[] = {
90d4ccd54dSJann Horn {
91d4ccd54dSJann Horn .procname = "oops_limit",
92d4ccd54dSJann Horn .data = &oops_limit,
93d4ccd54dSJann Horn .maxlen = sizeof(oops_limit),
94d4ccd54dSJann Horn .mode = 0644,
95d4ccd54dSJann Horn .proc_handler = proc_douintvec,
96d4ccd54dSJann Horn },
97d4ccd54dSJann Horn };
98d4ccd54dSJann Horn
kernel_exit_sysctls_init(void)99d4ccd54dSJann Horn static __init int kernel_exit_sysctls_init(void)
100d4ccd54dSJann Horn {
101d4ccd54dSJann Horn register_sysctl_init("kernel", kern_exit_table);
102d4ccd54dSJann Horn return 0;
103d4ccd54dSJann Horn }
104d4ccd54dSJann Horn late_initcall(kernel_exit_sysctls_init);
105d4ccd54dSJann Horn #endif
106d4ccd54dSJann Horn
1079db89b41SKees Cook static atomic_t oops_count = ATOMIC_INIT(0);
1089db89b41SKees Cook
1099db89b41SKees Cook #ifdef CONFIG_SYSFS
oops_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1109db89b41SKees Cook static ssize_t oops_count_show(struct kobject *kobj, struct kobj_attribute *attr,
1119db89b41SKees Cook char *page)
1129db89b41SKees Cook {
1139db89b41SKees Cook return sysfs_emit(page, "%d\n", atomic_read(&oops_count));
1149db89b41SKees Cook }
1159db89b41SKees Cook
1169db89b41SKees Cook static struct kobj_attribute oops_count_attr = __ATTR_RO(oops_count);
1179db89b41SKees Cook
kernel_exit_sysfs_init(void)1189db89b41SKees Cook static __init int kernel_exit_sysfs_init(void)
1199db89b41SKees Cook {
1209db89b41SKees Cook sysfs_add_file_to_group(kernel_kobj, &oops_count_attr.attr, NULL);
1219db89b41SKees Cook return 0;
1229db89b41SKees Cook }
1239db89b41SKees Cook late_initcall(kernel_exit_sysfs_init);
1249db89b41SKees Cook #endif
1259db89b41SKees Cook
1267903f907SMateusz Guzik /*
1277903f907SMateusz Guzik * For things release_task() would like to do *after* tasklist_lock is released.
1287903f907SMateusz Guzik */
1297903f907SMateusz Guzik struct release_task_post {
1307903f907SMateusz Guzik struct pid *pids[PIDTYPE_MAX];
1317903f907SMateusz Guzik };
1327903f907SMateusz Guzik
__unhash_process(struct release_task_post * post,struct task_struct * p,bool group_dead)1337903f907SMateusz Guzik static void __unhash_process(struct release_task_post *post, struct task_struct *p,
1347903f907SMateusz Guzik bool group_dead)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds nr_threads--;
1377903f907SMateusz Guzik detach_pid(post->pids, p, PIDTYPE_PID);
138d40e48e0SOleg Nesterov if (group_dead) {
1397903f907SMateusz Guzik detach_pid(post->pids, p, PIDTYPE_TGID);
1407903f907SMateusz Guzik detach_pid(post->pids, p, PIDTYPE_PGID);
1417903f907SMateusz Guzik detach_pid(post->pids, p, PIDTYPE_SID);
142c97d9893SOleg Nesterov
1435e85d4abSEric W. Biederman list_del_rcu(&p->tasks);
1449cd80bbbSOleg Nesterov list_del_init(&p->sibling);
145909ea964SChristoph Lameter __this_cpu_dec(process_counts);
1466347e900SEric W. Biederman }
1470c740d0aSOleg Nesterov list_del_rcu(&p->thread_node);
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds
1506a14c5c9SOleg Nesterov /*
1516a14c5c9SOleg Nesterov * This function expects the tasklist_lock write-locked.
1526a14c5c9SOleg Nesterov */
__exit_signal(struct release_task_post * post,struct task_struct * tsk)1537903f907SMateusz Guzik static void __exit_signal(struct release_task_post *post, struct task_struct *tsk)
1546a14c5c9SOleg Nesterov {
1556a14c5c9SOleg Nesterov struct signal_struct *sig = tsk->signal;
156d40e48e0SOleg Nesterov bool group_dead = thread_group_leader(tsk);
1576a14c5c9SOleg Nesterov struct sighand_struct *sighand;
1583f649ab7SKees Cook struct tty_struct *tty;
1595613fda9SFrederic Weisbecker u64 utime, stime;
1606a14c5c9SOleg Nesterov
161d11c563dSPaul E. McKenney sighand = rcu_dereference_check(tsk->sighand,
162db1466b3SPaul E. McKenney lockdep_tasklist_lock_is_held());
1636a14c5c9SOleg Nesterov spin_lock(&sighand->siglock);
1646a14c5c9SOleg Nesterov
165baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
1666a14c5c9SOleg Nesterov posix_cpu_timers_exit(tsk);
167b95e31c0SEric W. Biederman if (group_dead)
1686a14c5c9SOleg Nesterov posix_cpu_timers_exit_group(tsk);
169baa73d9eSNicolas Pitre #endif
170e0a70217SOleg Nesterov
171baa73d9eSNicolas Pitre if (group_dead) {
172baa73d9eSNicolas Pitre tty = sig->tty;
173baa73d9eSNicolas Pitre sig->tty = NULL;
174baa73d9eSNicolas Pitre } else {
175e0a70217SOleg Nesterov /*
1766a14c5c9SOleg Nesterov * If there is any task waiting for the group exit
1776a14c5c9SOleg Nesterov * then notify it:
1786a14c5c9SOleg Nesterov */
179d344193aSOleg Nesterov if (sig->notify_count > 0 && !--sig->notify_count)
18060700e38SEric W. Biederman wake_up_process(sig->group_exec_task);
1816db840faSOleg Nesterov
1826a14c5c9SOleg Nesterov if (tsk == sig->curr_target)
1836a14c5c9SOleg Nesterov sig->curr_target = next_thread(tsk);
18490ed9cbeSRik van Riel }
18590ed9cbeSRik van Riel
1866a14c5c9SOleg Nesterov /*
18726e75b5cSOleg Nesterov * Accumulate here the counters for all threads as they die. We could
18826e75b5cSOleg Nesterov * skip the group leader because it is the last user of signal_struct,
18926e75b5cSOleg Nesterov * but we want to avoid the race with thread_group_cputime() which can
19026e75b5cSOleg Nesterov * see the empty ->thread_head list.
1916a14c5c9SOleg Nesterov */
1926fac4829SFrederic Weisbecker task_cputime(tsk, &utime, &stime);
193e78c3496SRik van Riel write_seqlock(&sig->stats_lock);
1946fac4829SFrederic Weisbecker sig->utime += utime;
1956fac4829SFrederic Weisbecker sig->stime += stime;
1966fac4829SFrederic Weisbecker sig->gtime += task_gtime(tsk);
1976a14c5c9SOleg Nesterov sig->min_flt += tsk->min_flt;
1986a14c5c9SOleg Nesterov sig->maj_flt += tsk->maj_flt;
1996a14c5c9SOleg Nesterov sig->nvcsw += tsk->nvcsw;
2006a14c5c9SOleg Nesterov sig->nivcsw += tsk->nivcsw;
2016eaeeabaSEric Dumazet sig->inblock += task_io_get_inblock(tsk);
2026eaeeabaSEric Dumazet sig->oublock += task_io_get_oublock(tsk);
2035995477aSAndrea Righi task_io_accounting_add(&sig->ioac, &tsk->ioac);
20432bd671dSPeter Zijlstra sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
205b3ac022cSOleg Nesterov sig->nr_threads--;
2067903f907SMateusz Guzik __unhash_process(post, tsk, group_dead);
207e78c3496SRik van Riel write_sequnlock(&sig->stats_lock);
2085876700cSOleg Nesterov
209a7e5328aSOleg Nesterov tsk->sighand = NULL;
2106a14c5c9SOleg Nesterov spin_unlock(&sighand->siglock);
2116a14c5c9SOleg Nesterov
212a7e5328aSOleg Nesterov __cleanup_sighand(sighand);
213fb3bbcfeSOleg Nesterov if (group_dead)
2144ada856fSOleg Nesterov tty_kref_put(tty);
2156a14c5c9SOleg Nesterov }
2166a14c5c9SOleg Nesterov
delayed_put_task_struct(struct rcu_head * rhp)2178c7904a0SEric W. Biederman static void delayed_put_task_struct(struct rcu_head *rhp)
2188c7904a0SEric W. Biederman {
2190a16b607SMathieu Desnoyers struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
2200a16b607SMathieu Desnoyers
221670721c7SThomas Gleixner kprobe_flush_task(tsk);
22254ecbe6fSMasami Hiramatsu rethook_flush_task(tsk);
2234e231c79SPeter Zijlstra perf_event_delayed_put(tsk);
2240a16b607SMathieu Desnoyers trace_sched_process_free(tsk);
2250a16b607SMathieu Desnoyers put_task_struct(tsk);
2268c7904a0SEric W. Biederman }
2278c7904a0SEric W. Biederman
put_task_struct_rcu_user(struct task_struct * task)2283fbd7ee2SEric W. Biederman void put_task_struct_rcu_user(struct task_struct *task)
2293fbd7ee2SEric W. Biederman {
2303fbd7ee2SEric W. Biederman if (refcount_dec_and_test(&task->rcu_users))
2313fbd7ee2SEric W. Biederman call_rcu(&task->rcu, delayed_put_task_struct);
2323fbd7ee2SEric W. Biederman }
233f470021aSRoland McGrath
release_thread(struct task_struct * dead_task)2342be9880dSKefeng Wang void __weak release_thread(struct task_struct *dead_task)
2352be9880dSKefeng Wang {
2362be9880dSKefeng Wang }
2372be9880dSKefeng Wang
release_task(struct task_struct * p)2381da177e4SLinus Torvalds void release_task(struct task_struct *p)
2391da177e4SLinus Torvalds {
2407903f907SMateusz Guzik struct release_task_post post;
24136c8b586SIngo Molnar struct task_struct *leader;
2427bc3e6e5SEric W. Biederman struct pid *thread_pid;
2431da177e4SLinus Torvalds int zap_leader;
2441da177e4SLinus Torvalds repeat:
2457903f907SMateusz Guzik memset(&post, 0, sizeof(post));
2467903f907SMateusz Guzik
247c69e8d9cSDavid Howells /* don't need to get the RCU readlock here - the process is dead and
248d11c563dSPaul E. McKenney * can't be modifying its own credentials. But shut RCU-lockdep up */
249d11c563dSPaul E. McKenney rcu_read_lock();
25021d1c5e3SAlexey Gladkov dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
251d11c563dSPaul E. McKenney rcu_read_unlock();
252c69e8d9cSDavid Howells
25345135229SChristian Brauner pidfs_exit(p);
2546b115bf5STejun Heo cgroup_release(p);
2550203026bSIngo Molnar
2566731cd97SMateusz Guzik thread_pid = get_pid(p->thread_pid);
2576731cd97SMateusz Guzik
2581da177e4SLinus Torvalds write_lock_irq(&tasklist_lock);
259a288eeccSTejun Heo ptrace_release_task(p);
2607903f907SMateusz Guzik __exit_signal(&post, p);
26135f5cad8SOleg Nesterov
2621da177e4SLinus Torvalds /*
2631da177e4SLinus Torvalds * If we are the last non-leader member of the thread
2641da177e4SLinus Torvalds * group, and the leader is zombie, then notify the
2651da177e4SLinus Torvalds * group leader's parent process. (if it wants notification.)
2661da177e4SLinus Torvalds */
2671da177e4SLinus Torvalds zap_leader = 0;
2681da177e4SLinus Torvalds leader = p->group_leader;
269a0be55deSIonut Alexa if (leader != p && thread_group_empty(leader)
270a0be55deSIonut Alexa && leader->exit_state == EXIT_ZOMBIE) {
271*9133607dSOleg Nesterov /* for pidfs_exit() and do_notify_parent() */
272*9133607dSOleg Nesterov if (leader->signal->flags & SIGNAL_GROUP_EXIT)
273*9133607dSOleg Nesterov leader->exit_code = leader->signal->group_exit_code;
2741da177e4SLinus Torvalds /*
2751da177e4SLinus Torvalds * If we were the last child thread and the leader has
2761da177e4SLinus Torvalds * exited already, and the leader's parent ignores SIGCHLD,
2771da177e4SLinus Torvalds * then we are the one who should release the leader.
2781da177e4SLinus Torvalds */
27986773473SOleg Nesterov zap_leader = do_notify_parent(leader, leader->exit_signal);
280dae33574SRoland McGrath if (zap_leader)
281dae33574SRoland McGrath leader->exit_state = EXIT_DEAD;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds
2841da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock);
2857bc3e6e5SEric W. Biederman proc_flush_pid(thread_pid);
2866ade99ecSEric W. Biederman put_pid(thread_pid);
2871ab27856SMateusz Guzik add_device_randomness(&p->se.sum_exec_runtime,
2881ab27856SMateusz Guzik sizeof(p->se.sum_exec_runtime));
2897903f907SMateusz Guzik free_pids(post.pids);
2901da177e4SLinus Torvalds release_thread(p);
291fb3bbcfeSOleg Nesterov /*
292fb3bbcfeSOleg Nesterov * This task was already removed from the process/thread/pid lists
293fb3bbcfeSOleg Nesterov * and lock_task_sighand(p) can't succeed. Nobody else can touch
294fb3bbcfeSOleg Nesterov * ->pending or, if group dead, signal->shared_pending. We can call
295fb3bbcfeSOleg Nesterov * flush_sigqueue() lockless.
296fb3bbcfeSOleg Nesterov */
297fb3bbcfeSOleg Nesterov flush_sigqueue(&p->pending);
298fb3bbcfeSOleg Nesterov if (thread_group_leader(p))
299fb3bbcfeSOleg Nesterov flush_sigqueue(&p->signal->shared_pending);
300fb3bbcfeSOleg Nesterov
3013fbd7ee2SEric W. Biederman put_task_struct_rcu_user(p);
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds p = leader;
3041da177e4SLinus Torvalds if (unlikely(zap_leader))
3051da177e4SLinus Torvalds goto repeat;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds
rcuwait_wake_up(struct rcuwait * w)3089d9a6ebfSDavidlohr Bueso int rcuwait_wake_up(struct rcuwait *w)
3098f95c90cSDavidlohr Bueso {
3109d9a6ebfSDavidlohr Bueso int ret = 0;
3118f95c90cSDavidlohr Bueso struct task_struct *task;
3128f95c90cSDavidlohr Bueso
3138f95c90cSDavidlohr Bueso rcu_read_lock();
3148f95c90cSDavidlohr Bueso
3158f95c90cSDavidlohr Bueso /*
3168f95c90cSDavidlohr Bueso * Order condition vs @task, such that everything prior to the load
3178f95c90cSDavidlohr Bueso * of @task is visible. This is the condition as to why the user called
318c9d64a1bSDavidlohr Bueso * rcuwait_wake() in the first place. Pairs with set_current_state()
3198f95c90cSDavidlohr Bueso * barrier (A) in rcuwait_wait_event().
3208f95c90cSDavidlohr Bueso *
3218f95c90cSDavidlohr Bueso * WAIT WAKE
3228f95c90cSDavidlohr Bueso * [S] tsk = current [S] cond = true
3238f95c90cSDavidlohr Bueso * MB (A) MB (B)
3248f95c90cSDavidlohr Bueso * [L] cond [L] tsk
3258f95c90cSDavidlohr Bueso */
3266dc080eeSPrateek Sood smp_mb(); /* (B) */
3278f95c90cSDavidlohr Bueso
3288f95c90cSDavidlohr Bueso task = rcu_dereference(w->task);
3298f95c90cSDavidlohr Bueso if (task)
3309d9a6ebfSDavidlohr Bueso ret = wake_up_process(task);
3318f95c90cSDavidlohr Bueso rcu_read_unlock();
3329d9a6ebfSDavidlohr Bueso
3339d9a6ebfSDavidlohr Bueso return ret;
3348f95c90cSDavidlohr Bueso }
335ac8dec42SDavidlohr Bueso EXPORT_SYMBOL_GPL(rcuwait_wake_up);
3368f95c90cSDavidlohr Bueso
337150593bfSOleg Nesterov /*
3381da177e4SLinus Torvalds * Determine if a process group is "orphaned", according to the POSIX
3391da177e4SLinus Torvalds * definition in 2.2.2.52. Orphaned process groups are not to be affected
3401da177e4SLinus Torvalds * by terminal-generated stop signals. Newly orphaned process groups are
3411da177e4SLinus Torvalds * to receive a SIGHUP and a SIGCONT.
3421da177e4SLinus Torvalds *
3431da177e4SLinus Torvalds * "I ask you, have you ever known what it is to be an orphan?"
3441da177e4SLinus Torvalds */
will_become_orphaned_pgrp(struct pid * pgrp,struct task_struct * ignored_task)345a0be55deSIonut Alexa static int will_become_orphaned_pgrp(struct pid *pgrp,
346a0be55deSIonut Alexa struct task_struct *ignored_task)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds struct task_struct *p;
3491da177e4SLinus Torvalds
3500475ac08SEric W. Biederman do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
35105e83df6SOleg Nesterov if ((p == ignored_task) ||
35205e83df6SOleg Nesterov (p->exit_state && thread_group_empty(p)) ||
35305e83df6SOleg Nesterov is_global_init(p->real_parent))
3541da177e4SLinus Torvalds continue;
35505e83df6SOleg Nesterov
3560475ac08SEric W. Biederman if (task_pgrp(p->real_parent) != pgrp &&
35705e83df6SOleg Nesterov task_session(p->real_parent) == task_session(p))
35805e83df6SOleg Nesterov return 0;
3590475ac08SEric W. Biederman } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
36005e83df6SOleg Nesterov
36105e83df6SOleg Nesterov return 1;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds
is_current_pgrp_orphaned(void)3643e7cd6c4SEric W. Biederman int is_current_pgrp_orphaned(void)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds int retval;
3671da177e4SLinus Torvalds
3681da177e4SLinus Torvalds read_lock(&tasklist_lock);
3693e7cd6c4SEric W. Biederman retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
3701da177e4SLinus Torvalds read_unlock(&tasklist_lock);
3711da177e4SLinus Torvalds
3721da177e4SLinus Torvalds return retval;
3731da177e4SLinus Torvalds }
3741da177e4SLinus Torvalds
has_stopped_jobs(struct pid * pgrp)375961c4675SOleg Nesterov static bool has_stopped_jobs(struct pid *pgrp)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds struct task_struct *p;
3781da177e4SLinus Torvalds
3790475ac08SEric W. Biederman do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
380961c4675SOleg Nesterov if (p->signal->flags & SIGNAL_STOP_STOPPED)
381961c4675SOleg Nesterov return true;
3820475ac08SEric W. Biederman } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
383961c4675SOleg Nesterov
384961c4675SOleg Nesterov return false;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds
387f49ee505SOleg Nesterov /*
388f49ee505SOleg Nesterov * Check to see if any process groups have become orphaned as
389f49ee505SOleg Nesterov * a result of our exiting, and if they have any stopped jobs,
390f49ee505SOleg Nesterov * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
391f49ee505SOleg Nesterov */
392f49ee505SOleg Nesterov static void
kill_orphaned_pgrp(struct task_struct * tsk,struct task_struct * parent)393f49ee505SOleg Nesterov kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
394f49ee505SOleg Nesterov {
395f49ee505SOleg Nesterov struct pid *pgrp = task_pgrp(tsk);
396f49ee505SOleg Nesterov struct task_struct *ignored_task = tsk;
397f49ee505SOleg Nesterov
398f49ee505SOleg Nesterov if (!parent)
399f49ee505SOleg Nesterov /* exit: our father is in a different pgrp than
400f49ee505SOleg Nesterov * we are and we were the only connection outside.
401f49ee505SOleg Nesterov */
402f49ee505SOleg Nesterov parent = tsk->real_parent;
403f49ee505SOleg Nesterov else
404f49ee505SOleg Nesterov /* reparent: our child is in a different pgrp than
405f49ee505SOleg Nesterov * we are, and it was the only connection outside.
406f49ee505SOleg Nesterov */
407f49ee505SOleg Nesterov ignored_task = NULL;
408f49ee505SOleg Nesterov
409f49ee505SOleg Nesterov if (task_pgrp(parent) != pgrp &&
410f49ee505SOleg Nesterov task_session(parent) == task_session(tsk) &&
411f49ee505SOleg Nesterov will_become_orphaned_pgrp(pgrp, ignored_task) &&
412f49ee505SOleg Nesterov has_stopped_jobs(pgrp)) {
413f49ee505SOleg Nesterov __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
414f49ee505SOleg Nesterov __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
415f49ee505SOleg Nesterov }
416f49ee505SOleg Nesterov }
417f49ee505SOleg Nesterov
coredump_task_exit(struct task_struct * tsk)41892307383SEric W. Biederman static void coredump_task_exit(struct task_struct *tsk)
419d67e03e3SEric W. Biederman {
420d67e03e3SEric W. Biederman struct core_state *core_state;
421d67e03e3SEric W. Biederman
422d67e03e3SEric W. Biederman /*
423d67e03e3SEric W. Biederman * Serialize with any possible pending coredump.
4240258b5fdSEric W. Biederman * We must hold siglock around checking core_state
42592307383SEric W. Biederman * and setting PF_POSTCOREDUMP. The core-inducing thread
426d67e03e3SEric W. Biederman * will increment ->nr_threads for each thread in the
42792307383SEric W. Biederman * group without PF_POSTCOREDUMP set.
428d67e03e3SEric W. Biederman */
4290258b5fdSEric W. Biederman spin_lock_irq(&tsk->sighand->siglock);
43092307383SEric W. Biederman tsk->flags |= PF_POSTCOREDUMP;
4310258b5fdSEric W. Biederman core_state = tsk->signal->core_state;
4320258b5fdSEric W. Biederman spin_unlock_irq(&tsk->sighand->siglock);
433240a1853SMike Christie if (core_state) {
434d67e03e3SEric W. Biederman struct core_thread self;
435d67e03e3SEric W. Biederman
436d67e03e3SEric W. Biederman self.task = current;
437d67e03e3SEric W. Biederman if (self.task->flags & PF_SIGNALED)
438d67e03e3SEric W. Biederman self.next = xchg(&core_state->dumper.next, &self);
439d67e03e3SEric W. Biederman else
440d67e03e3SEric W. Biederman self.task = NULL;
441d67e03e3SEric W. Biederman /*
442d67e03e3SEric W. Biederman * Implies mb(), the result of xchg() must be visible
443d67e03e3SEric W. Biederman * to core_state->dumper.
444d67e03e3SEric W. Biederman */
445d67e03e3SEric W. Biederman if (atomic_dec_and_test(&core_state->nr_threads))
446d67e03e3SEric W. Biederman complete(&core_state->startup);
447d67e03e3SEric W. Biederman
448d67e03e3SEric W. Biederman for (;;) {
449b8e75312SPaul E. McKenney set_current_state(TASK_IDLE|TASK_FREEZABLE);
450d67e03e3SEric W. Biederman if (!self.task) /* see coredump_finish() */
451d67e03e3SEric W. Biederman break;
452f5d39b02SPeter Zijlstra schedule();
453d67e03e3SEric W. Biederman }
454d67e03e3SEric W. Biederman __set_current_state(TASK_RUNNING);
455d67e03e3SEric W. Biederman }
456d67e03e3SEric W. Biederman }
457d67e03e3SEric W. Biederman
458f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG
4592a22b773SOleg Nesterov /* drops tasklist_lock if succeeds */
__try_to_set_owner(struct task_struct * tsk,struct mm_struct * mm)460d73d0035SOleg Nesterov static bool __try_to_set_owner(struct task_struct *tsk, struct mm_struct *mm)
4612a22b773SOleg Nesterov {
4622a22b773SOleg Nesterov bool ret = false;
4632a22b773SOleg Nesterov
4642a22b773SOleg Nesterov task_lock(tsk);
4652a22b773SOleg Nesterov if (likely(tsk->mm == mm)) {
4662a22b773SOleg Nesterov /* tsk can't pass exit_mm/exec_mmap and exit */
4672a22b773SOleg Nesterov read_unlock(&tasklist_lock);
4682a22b773SOleg Nesterov WRITE_ONCE(mm->owner, tsk);
4692a22b773SOleg Nesterov lru_gen_migrate_mm(mm);
4702a22b773SOleg Nesterov ret = true;
4712a22b773SOleg Nesterov }
4722a22b773SOleg Nesterov task_unlock(tsk);
4732a22b773SOleg Nesterov return ret;
4742a22b773SOleg Nesterov }
4752a22b773SOleg Nesterov
try_to_set_owner(struct task_struct * g,struct mm_struct * mm)476d73d0035SOleg Nesterov static bool try_to_set_owner(struct task_struct *g, struct mm_struct *mm)
477d73d0035SOleg Nesterov {
478d73d0035SOleg Nesterov struct task_struct *t;
479d73d0035SOleg Nesterov
480d73d0035SOleg Nesterov for_each_thread(g, t) {
481d73d0035SOleg Nesterov struct mm_struct *t_mm = READ_ONCE(t->mm);
482d73d0035SOleg Nesterov if (t_mm == mm) {
483d73d0035SOleg Nesterov if (__try_to_set_owner(t, mm))
484d73d0035SOleg Nesterov return true;
485d73d0035SOleg Nesterov } else if (t_mm)
486d73d0035SOleg Nesterov break;
487d73d0035SOleg Nesterov }
488d73d0035SOleg Nesterov
489d73d0035SOleg Nesterov return false;
490d73d0035SOleg Nesterov }
491d73d0035SOleg Nesterov
492cf475ad2SBalbir Singh /*
493733eda7aSKAMEZAWA Hiroyuki * A task is exiting. If it owned this mm, find a new owner for the mm.
494cf475ad2SBalbir Singh */
mm_update_next_owner(struct mm_struct * mm)495cf475ad2SBalbir Singh void mm_update_next_owner(struct mm_struct *mm)
496cf475ad2SBalbir Singh {
497d73d0035SOleg Nesterov struct task_struct *g, *p = current;
498cf475ad2SBalbir Singh
499733eda7aSKAMEZAWA Hiroyuki /*
500733eda7aSKAMEZAWA Hiroyuki * If the exiting or execing task is not the owner, it's
501733eda7aSKAMEZAWA Hiroyuki * someone else's problem.
502733eda7aSKAMEZAWA Hiroyuki */
503733eda7aSKAMEZAWA Hiroyuki if (mm->owner != p)
504cf475ad2SBalbir Singh return;
505733eda7aSKAMEZAWA Hiroyuki /*
506733eda7aSKAMEZAWA Hiroyuki * The current owner is exiting/execing and there are no other
507733eda7aSKAMEZAWA Hiroyuki * candidates. Do not leave the mm pointing to a possibly
508733eda7aSKAMEZAWA Hiroyuki * freed task structure.
509733eda7aSKAMEZAWA Hiroyuki */
510733eda7aSKAMEZAWA Hiroyuki if (atomic_read(&mm->mm_users) <= 1) {
511987717e5SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL);
512733eda7aSKAMEZAWA Hiroyuki return;
513733eda7aSKAMEZAWA Hiroyuki }
514cf475ad2SBalbir Singh
515cf475ad2SBalbir Singh read_lock(&tasklist_lock);
516cf475ad2SBalbir Singh /*
517cf475ad2SBalbir Singh * Search in the children
518cf475ad2SBalbir Singh */
519d73d0035SOleg Nesterov list_for_each_entry(g, &p->children, sibling) {
520d73d0035SOleg Nesterov if (try_to_set_owner(g, mm))
5212a22b773SOleg Nesterov goto ret;
522cf475ad2SBalbir Singh }
523cf475ad2SBalbir Singh /*
524cf475ad2SBalbir Singh * Search in the siblings
525cf475ad2SBalbir Singh */
526d73d0035SOleg Nesterov list_for_each_entry(g, &p->real_parent->children, sibling) {
527d73d0035SOleg Nesterov if (try_to_set_owner(g, mm))
5282a22b773SOleg Nesterov goto ret;
529cf475ad2SBalbir Singh }
530cf475ad2SBalbir Singh /*
531f87fb599SOleg Nesterov * Search through everything else, we should not get here often.
532cf475ad2SBalbir Singh */
53339af1765SOleg Nesterov for_each_process(g) {
534cf3f9a59SJinliang Zheng if (atomic_read(&mm->mm_users) <= 1)
535cf3f9a59SJinliang Zheng break;
53639af1765SOleg Nesterov if (g->flags & PF_KTHREAD)
53739af1765SOleg Nesterov continue;
538d73d0035SOleg Nesterov if (try_to_set_owner(g, mm))
5392a22b773SOleg Nesterov goto ret;
540f87fb599SOleg Nesterov }
541cf475ad2SBalbir Singh read_unlock(&tasklist_lock);
54231a78f23SBalbir Singh /*
54331a78f23SBalbir Singh * We found no owner yet mm_users > 1: this implies that we are
54431a78f23SBalbir Singh * most likely racing with swapoff (try_to_unuse()) or /proc or
545e5991371SHugh Dickins * ptrace or page migration (get_task_mm()). Mark owner as NULL.
54631a78f23SBalbir Singh */
547987717e5SAndrea Arcangeli WRITE_ONCE(mm->owner, NULL);
5482a22b773SOleg Nesterov ret:
549cf475ad2SBalbir Singh return;
550cf475ad2SBalbir Singh
551cf475ad2SBalbir Singh }
552f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */
553cf475ad2SBalbir Singh
5541da177e4SLinus Torvalds /*
5551da177e4SLinus Torvalds * Turn us into a lazy TLB process if we
5561da177e4SLinus Torvalds * aren't already..
5571da177e4SLinus Torvalds */
exit_mm(void)5580039962aSDavidlohr Bueso static void exit_mm(void)
5591da177e4SLinus Torvalds {
5600039962aSDavidlohr Bueso struct mm_struct *mm = current->mm;
5611da177e4SLinus Torvalds
5624610ba7aSThomas Gleixner exit_mm_release(current, mm);
5631da177e4SLinus Torvalds if (!mm)
5641da177e4SLinus Torvalds return;
565d8ed45c5SMichel Lespinasse mmap_read_lock(mm);
566aa464ba9SNicholas Piggin mmgrab_lazy_tlb(mm);
5670039962aSDavidlohr Bueso BUG_ON(mm != current->active_mm);
5681da177e4SLinus Torvalds /* more a memory barrier than a real lock */
5690039962aSDavidlohr Bueso task_lock(current);
5705bc78502SMathieu Desnoyers /*
5715bc78502SMathieu Desnoyers * When a thread stops operating on an address space, the loop
5725bc78502SMathieu Desnoyers * in membarrier_private_expedited() may not observe that
5735bc78502SMathieu Desnoyers * tsk->mm, and the loop in membarrier_global_expedited() may
5745bc78502SMathieu Desnoyers * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
5755bc78502SMathieu Desnoyers * rq->membarrier_state, so those would not issue an IPI.
5765bc78502SMathieu Desnoyers * Membarrier requires a memory barrier after accessing
5775bc78502SMathieu Desnoyers * user-space memory, before clearing tsk->mm or the
5785bc78502SMathieu Desnoyers * rq->membarrier_state.
5795bc78502SMathieu Desnoyers */
5805bc78502SMathieu Desnoyers smp_mb__after_spinlock();
5815bc78502SMathieu Desnoyers local_irq_disable();
5820039962aSDavidlohr Bueso current->mm = NULL;
5835bc78502SMathieu Desnoyers membarrier_update_current_mm(NULL);
5841da177e4SLinus Torvalds enter_lazy_tlb(mm, current);
5855bc78502SMathieu Desnoyers local_irq_enable();
5860039962aSDavidlohr Bueso task_unlock(current);
5875bc78502SMathieu Desnoyers mmap_read_unlock(mm);
588cf475ad2SBalbir Singh mm_update_next_owner(mm);
5891da177e4SLinus Torvalds mmput(mm);
590c32b3cbeSMichal Hocko if (test_thread_flag(TIF_MEMDIE))
59138531201STetsuo Handa exit_oom_victim();
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds
find_alive_thread(struct task_struct * p)594c9dc05bfSOleg Nesterov static struct task_struct *find_alive_thread(struct task_struct *p)
595c9dc05bfSOleg Nesterov {
596c9dc05bfSOleg Nesterov struct task_struct *t;
597c9dc05bfSOleg Nesterov
598c9dc05bfSOleg Nesterov for_each_thread(p, t) {
599c9dc05bfSOleg Nesterov if (!(t->flags & PF_EXITING))
600c9dc05bfSOleg Nesterov return t;
601c9dc05bfSOleg Nesterov }
602c9dc05bfSOleg Nesterov return NULL;
603c9dc05bfSOleg Nesterov }
604c9dc05bfSOleg Nesterov
find_child_reaper(struct task_struct * father,struct list_head * dead)6058fb335e0SAndrei Vagin static struct task_struct *find_child_reaper(struct task_struct *father,
6068fb335e0SAndrei Vagin struct list_head *dead)
6071109909cSOleg Nesterov __releases(&tasklist_lock)
6081109909cSOleg Nesterov __acquires(&tasklist_lock)
6091109909cSOleg Nesterov {
6101109909cSOleg Nesterov struct pid_namespace *pid_ns = task_active_pid_ns(father);
6111109909cSOleg Nesterov struct task_struct *reaper = pid_ns->child_reaper;
6128fb335e0SAndrei Vagin struct task_struct *p, *n;
6131109909cSOleg Nesterov
6141109909cSOleg Nesterov if (likely(reaper != father))
6151109909cSOleg Nesterov return reaper;
6161109909cSOleg Nesterov
617c9dc05bfSOleg Nesterov reaper = find_alive_thread(father);
618c9dc05bfSOleg Nesterov if (reaper) {
6191109909cSOleg Nesterov pid_ns->child_reaper = reaper;
6201109909cSOleg Nesterov return reaper;
6211109909cSOleg Nesterov }
6221109909cSOleg Nesterov
6231109909cSOleg Nesterov write_unlock_irq(&tasklist_lock);
6248fb335e0SAndrei Vagin
6258fb335e0SAndrei Vagin list_for_each_entry_safe(p, n, dead, ptrace_entry) {
6268fb335e0SAndrei Vagin list_del_init(&p->ptrace_entry);
6278fb335e0SAndrei Vagin release_task(p);
6288fb335e0SAndrei Vagin }
6298fb335e0SAndrei Vagin
6301109909cSOleg Nesterov zap_pid_ns_processes(pid_ns);
6311109909cSOleg Nesterov write_lock_irq(&tasklist_lock);
6321109909cSOleg Nesterov
6331109909cSOleg Nesterov return father;
6341109909cSOleg Nesterov }
6351109909cSOleg Nesterov
6361da177e4SLinus Torvalds /*
637ebec18a6SLennart Poettering * When we die, we re-parent all our children, and try to:
638ebec18a6SLennart Poettering * 1. give them to another thread in our thread group, if such a member exists
639ebec18a6SLennart Poettering * 2. give it to the first ancestor process which prctl'd itself as a
640ebec18a6SLennart Poettering * child_subreaper for its children (like a service manager)
641ebec18a6SLennart Poettering * 3. give it to the init process (PID 1) in our pid namespace
6421da177e4SLinus Torvalds */
find_new_reaper(struct task_struct * father,struct task_struct * child_reaper)6431109909cSOleg Nesterov static struct task_struct *find_new_reaper(struct task_struct *father,
6441109909cSOleg Nesterov struct task_struct *child_reaper)
645950bbabbSOleg Nesterov {
646c9dc05bfSOleg Nesterov struct task_struct *thread, *reaper;
647950bbabbSOleg Nesterov
648c9dc05bfSOleg Nesterov thread = find_alive_thread(father);
649c9dc05bfSOleg Nesterov if (thread)
650950bbabbSOleg Nesterov return thread;
651950bbabbSOleg Nesterov
6527d24e2dfSOleg Nesterov if (father->signal->has_child_subreaper) {
653c6c70f44SOleg Nesterov unsigned int ns_level = task_pid(father)->level;
654ebec18a6SLennart Poettering /*
655175aed3fSOleg Nesterov * Find the first ->is_child_subreaper ancestor in our pid_ns.
656c6c70f44SOleg Nesterov * We can't check reaper != child_reaper to ensure we do not
657c6c70f44SOleg Nesterov * cross the namespaces, the exiting parent could be injected
658c6c70f44SOleg Nesterov * by setns() + fork().
659c6c70f44SOleg Nesterov * We check pid->level, this is slightly more efficient than
660c6c70f44SOleg Nesterov * task_active_pid_ns(reaper) != task_active_pid_ns(father).
661ebec18a6SLennart Poettering */
662c6c70f44SOleg Nesterov for (reaper = father->real_parent;
663c6c70f44SOleg Nesterov task_pid(reaper)->level == ns_level;
664ebec18a6SLennart Poettering reaper = reaper->real_parent) {
665175aed3fSOleg Nesterov if (reaper == &init_task)
666ebec18a6SLennart Poettering break;
667ebec18a6SLennart Poettering if (!reaper->signal->is_child_subreaper)
668ebec18a6SLennart Poettering continue;
669c9dc05bfSOleg Nesterov thread = find_alive_thread(reaper);
670c9dc05bfSOleg Nesterov if (thread)
6718a1296aeSOleg Nesterov return thread;
6723750ef97SOleg Nesterov }
673ebec18a6SLennart Poettering }
674950bbabbSOleg Nesterov
6751109909cSOleg Nesterov return child_reaper;
676950bbabbSOleg Nesterov }
677950bbabbSOleg Nesterov
6785dfc80beSOleg Nesterov /*
6795dfc80beSOleg Nesterov * Any that need to be release_task'd are put on the @dead list.
6805dfc80beSOleg Nesterov */
reparent_leader(struct task_struct * father,struct task_struct * p,struct list_head * dead)6819cd80bbbSOleg Nesterov static void reparent_leader(struct task_struct *father, struct task_struct *p,
6825dfc80beSOleg Nesterov struct list_head *dead)
6835dfc80beSOleg Nesterov {
6842831096eSOleg Nesterov if (unlikely(p->exit_state == EXIT_DEAD))
6855dfc80beSOleg Nesterov return;
6865dfc80beSOleg Nesterov
687abd50b39SOleg Nesterov /* We don't want people slaying init. */
6885dfc80beSOleg Nesterov p->exit_signal = SIGCHLD;
6895dfc80beSOleg Nesterov
6905dfc80beSOleg Nesterov /* If it has exited notify the new parent about this child's death. */
691d21142ecSTejun Heo if (!p->ptrace &&
6925dfc80beSOleg Nesterov p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
69386773473SOleg Nesterov if (do_notify_parent(p, p->exit_signal)) {
6945dfc80beSOleg Nesterov p->exit_state = EXIT_DEAD;
695dc2fd4b0SOleg Nesterov list_add(&p->ptrace_entry, dead);
6965dfc80beSOleg Nesterov }
6975dfc80beSOleg Nesterov }
6985dfc80beSOleg Nesterov
6995dfc80beSOleg Nesterov kill_orphaned_pgrp(p, father);
7005dfc80beSOleg Nesterov }
7015dfc80beSOleg Nesterov
702482a3767SOleg Nesterov /*
703482a3767SOleg Nesterov * This does two things:
704482a3767SOleg Nesterov *
705482a3767SOleg Nesterov * A. Make init inherit all the child processes
706482a3767SOleg Nesterov * B. Check to see if any process groups have become orphaned
707482a3767SOleg Nesterov * as a result of our exiting, and if they have any stopped
708482a3767SOleg Nesterov * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
709482a3767SOleg Nesterov */
forget_original_parent(struct task_struct * father,struct list_head * dead)710482a3767SOleg Nesterov static void forget_original_parent(struct task_struct *father,
711482a3767SOleg Nesterov struct list_head *dead)
7121da177e4SLinus Torvalds {
713482a3767SOleg Nesterov struct task_struct *p, *t, *reaper;
714762a24beSOleg Nesterov
7157c8bd232SOleg Nesterov if (unlikely(!list_empty(&father->ptraced)))
716482a3767SOleg Nesterov exit_ptrace(father, dead);
717f470021aSRoland McGrath
7187c8bd232SOleg Nesterov /* Can drop and reacquire tasklist_lock */
7198fb335e0SAndrei Vagin reaper = find_child_reaper(father, dead);
720ad9e206aSOleg Nesterov if (list_empty(&father->children))
721482a3767SOleg Nesterov return;
7221109909cSOleg Nesterov
7231109909cSOleg Nesterov reaper = find_new_reaper(father, reaper);
7242831096eSOleg Nesterov list_for_each_entry(p, &father->children, sibling) {
72557a05918SOleg Nesterov for_each_thread(p, t) {
72622a34c6fSMadhuparna Bhowmik RCU_INIT_POINTER(t->real_parent, reaper);
72722a34c6fSMadhuparna Bhowmik BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
72857a05918SOleg Nesterov if (likely(!t->ptrace))
7299cd80bbbSOleg Nesterov t->parent = t->real_parent;
7309cd80bbbSOleg Nesterov if (t->pdeath_signal)
7319cd80bbbSOleg Nesterov group_send_sig_info(t->pdeath_signal,
73201024980SEric W. Biederman SEND_SIG_NOINFO, t,
73301024980SEric W. Biederman PIDTYPE_TGID);
73457a05918SOleg Nesterov }
7352831096eSOleg Nesterov /*
7362831096eSOleg Nesterov * If this is a threaded reparent there is no need to
7372831096eSOleg Nesterov * notify anyone anything has happened.
7382831096eSOleg Nesterov */
7392831096eSOleg Nesterov if (!same_thread_group(reaper, father))
740482a3767SOleg Nesterov reparent_leader(father, p, dead);
7411da177e4SLinus Torvalds }
7422831096eSOleg Nesterov list_splice_tail_init(&father->children, &reaper->children);
7431da177e4SLinus Torvalds }
7441da177e4SLinus Torvalds
7451da177e4SLinus Torvalds /*
7461da177e4SLinus Torvalds * Send signals to all our closest relatives so that they know
7471da177e4SLinus Torvalds * to properly mourn us..
7481da177e4SLinus Torvalds */
exit_notify(struct task_struct * tsk,int group_dead)749821c7de7SOleg Nesterov static void exit_notify(struct task_struct *tsk, int group_dead)
7501da177e4SLinus Torvalds {
75153c8f9f1SOleg Nesterov bool autoreap;
752482a3767SOleg Nesterov struct task_struct *p, *n;
753482a3767SOleg Nesterov LIST_HEAD(dead);
7541da177e4SLinus Torvalds
755762a24beSOleg Nesterov write_lock_irq(&tasklist_lock);
756482a3767SOleg Nesterov forget_original_parent(tsk, &dead);
757482a3767SOleg Nesterov
758821c7de7SOleg Nesterov if (group_dead)
759821c7de7SOleg Nesterov kill_orphaned_pgrp(tsk->group_leader, NULL);
7601da177e4SLinus Torvalds
761b191d649SSuren Baghdasaryan tsk->exit_state = EXIT_ZOMBIE;
76264bef697SOleg Nesterov
76345cdf5ccSOleg Nesterov if (unlikely(tsk->ptrace)) {
76445cdf5ccSOleg Nesterov int sig = thread_group_leader(tsk) &&
76545cdf5ccSOleg Nesterov thread_group_empty(tsk) &&
76645cdf5ccSOleg Nesterov !ptrace_reparented(tsk) ?
76745cdf5ccSOleg Nesterov tsk->exit_signal : SIGCHLD;
76845cdf5ccSOleg Nesterov autoreap = do_notify_parent(tsk, sig);
76945cdf5ccSOleg Nesterov } else if (thread_group_leader(tsk)) {
77045cdf5ccSOleg Nesterov autoreap = thread_group_empty(tsk) &&
77145cdf5ccSOleg Nesterov do_notify_parent(tsk, tsk->exit_signal);
77245cdf5ccSOleg Nesterov } else {
77345cdf5ccSOleg Nesterov autoreap = true;
7740b7747a5SOleg Nesterov /* untraced sub-thread */
7750b7747a5SOleg Nesterov do_notify_pidfd(tsk);
77645cdf5ccSOleg Nesterov }
7771da177e4SLinus Torvalds
77830b692d3SChristian Brauner if (autoreap) {
77930b692d3SChristian Brauner tsk->exit_state = EXIT_DEAD;
7806c66e7dbSOleg Nesterov list_add(&tsk->ptrace_entry, &dead);
78130b692d3SChristian Brauner }
7821da177e4SLinus Torvalds
7839c339168SOleg Nesterov /* mt-exec, de_thread() is waiting for group leader */
7849c339168SOleg Nesterov if (unlikely(tsk->signal->notify_count < 0))
78560700e38SEric W. Biederman wake_up_process(tsk->signal->group_exec_task);
7861da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock);
7871da177e4SLinus Torvalds
788482a3767SOleg Nesterov list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
789482a3767SOleg Nesterov list_del_init(&p->ptrace_entry);
790482a3767SOleg Nesterov release_task(p);
791482a3767SOleg Nesterov }
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds
794e18eecb8SJeff Dike #ifdef CONFIG_DEBUG_STACK_USAGE
stack_not_used(struct task_struct * p)795fbe76a65SPasha Tatashin unsigned long stack_not_used(struct task_struct *p)
796fbe76a65SPasha Tatashin {
797fbe76a65SPasha Tatashin unsigned long *n = end_of_stack(p);
798fbe76a65SPasha Tatashin
799fbe76a65SPasha Tatashin do { /* Skip over canary */
800fbe76a65SPasha Tatashin # ifdef CONFIG_STACK_GROWSUP
801fbe76a65SPasha Tatashin n--;
802fbe76a65SPasha Tatashin # else
803fbe76a65SPasha Tatashin n++;
804fbe76a65SPasha Tatashin # endif
805fbe76a65SPasha Tatashin } while (!*n);
806fbe76a65SPasha Tatashin
807fbe76a65SPasha Tatashin # ifdef CONFIG_STACK_GROWSUP
808fbe76a65SPasha Tatashin return (unsigned long)end_of_stack(p) - (unsigned long)n;
809fbe76a65SPasha Tatashin # else
810fbe76a65SPasha Tatashin return (unsigned long)n - (unsigned long)end_of_stack(p);
811fbe76a65SPasha Tatashin # endif
812fbe76a65SPasha Tatashin }
813fbe76a65SPasha Tatashin
814c4a6fce8SPasha Tatashin /* Count the maximum pages reached in kernel stacks */
kstack_histogram(unsigned long used_stack)815c4a6fce8SPasha Tatashin static inline void kstack_histogram(unsigned long used_stack)
816c4a6fce8SPasha Tatashin {
817c4a6fce8SPasha Tatashin #ifdef CONFIG_VM_EVENT_COUNTERS
818c4a6fce8SPasha Tatashin if (used_stack <= 1024)
819c4a6fce8SPasha Tatashin count_vm_event(KSTACK_1K);
820c4a6fce8SPasha Tatashin #if THREAD_SIZE > 1024
821c4a6fce8SPasha Tatashin else if (used_stack <= 2048)
822c4a6fce8SPasha Tatashin count_vm_event(KSTACK_2K);
823c4a6fce8SPasha Tatashin #endif
824c4a6fce8SPasha Tatashin #if THREAD_SIZE > 2048
825c4a6fce8SPasha Tatashin else if (used_stack <= 4096)
826c4a6fce8SPasha Tatashin count_vm_event(KSTACK_4K);
827c4a6fce8SPasha Tatashin #endif
828c4a6fce8SPasha Tatashin #if THREAD_SIZE > 4096
829c4a6fce8SPasha Tatashin else if (used_stack <= 8192)
830c4a6fce8SPasha Tatashin count_vm_event(KSTACK_8K);
831c4a6fce8SPasha Tatashin #endif
832c4a6fce8SPasha Tatashin #if THREAD_SIZE > 8192
833c4a6fce8SPasha Tatashin else if (used_stack <= 16384)
834c4a6fce8SPasha Tatashin count_vm_event(KSTACK_16K);
835c4a6fce8SPasha Tatashin #endif
836c4a6fce8SPasha Tatashin #if THREAD_SIZE > 16384
837c4a6fce8SPasha Tatashin else if (used_stack <= 32768)
838c4a6fce8SPasha Tatashin count_vm_event(KSTACK_32K);
839c4a6fce8SPasha Tatashin #endif
840c4a6fce8SPasha Tatashin #if THREAD_SIZE > 32768
841c4a6fce8SPasha Tatashin else if (used_stack <= 65536)
842c4a6fce8SPasha Tatashin count_vm_event(KSTACK_64K);
843c4a6fce8SPasha Tatashin #endif
844c4a6fce8SPasha Tatashin #if THREAD_SIZE > 65536
845c4a6fce8SPasha Tatashin else
846c4a6fce8SPasha Tatashin count_vm_event(KSTACK_REST);
847c4a6fce8SPasha Tatashin #endif
848c4a6fce8SPasha Tatashin #endif /* CONFIG_VM_EVENT_COUNTERS */
849c4a6fce8SPasha Tatashin }
850c4a6fce8SPasha Tatashin
check_stack_usage(void)851e18eecb8SJeff Dike static void check_stack_usage(void)
852e18eecb8SJeff Dike {
853e18eecb8SJeff Dike static DEFINE_SPINLOCK(low_water_lock);
854e18eecb8SJeff Dike static int lowest_to_date = THREAD_SIZE;
855e18eecb8SJeff Dike unsigned long free;
856e18eecb8SJeff Dike
8577c9f8861SEric Sandeen free = stack_not_used(current);
858c4a6fce8SPasha Tatashin kstack_histogram(THREAD_SIZE - free);
859e18eecb8SJeff Dike
860e18eecb8SJeff Dike if (free >= lowest_to_date)
861e18eecb8SJeff Dike return;
862e18eecb8SJeff Dike
863e18eecb8SJeff Dike spin_lock(&low_water_lock);
864e18eecb8SJeff Dike if (free < lowest_to_date) {
865627393d4SAnton Blanchard pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
866168eeccbSTim Bird current->comm, task_pid_nr(current), free);
867e18eecb8SJeff Dike lowest_to_date = free;
868e18eecb8SJeff Dike }
869e18eecb8SJeff Dike spin_unlock(&low_water_lock);
870e18eecb8SJeff Dike }
871e18eecb8SJeff Dike #else
check_stack_usage(void)872e18eecb8SJeff Dike static inline void check_stack_usage(void) {}
873e18eecb8SJeff Dike #endif
874e18eecb8SJeff Dike
synchronize_group_exit(struct task_struct * tsk,long code)875d80f7d7bSEric W. Biederman static void synchronize_group_exit(struct task_struct *tsk, long code)
876d80f7d7bSEric W. Biederman {
877d80f7d7bSEric W. Biederman struct sighand_struct *sighand = tsk->sighand;
878d80f7d7bSEric W. Biederman struct signal_struct *signal = tsk->signal;
879d80f7d7bSEric W. Biederman
880d80f7d7bSEric W. Biederman spin_lock_irq(&sighand->siglock);
881d80f7d7bSEric W. Biederman signal->quick_threads--;
882d80f7d7bSEric W. Biederman if ((signal->quick_threads == 0) &&
883d80f7d7bSEric W. Biederman !(signal->flags & SIGNAL_GROUP_EXIT)) {
884d80f7d7bSEric W. Biederman signal->flags = SIGNAL_GROUP_EXIT;
885d80f7d7bSEric W. Biederman signal->group_exit_code = code;
886d80f7d7bSEric W. Biederman signal->group_stop_count = 0;
887d80f7d7bSEric W. Biederman }
888d80f7d7bSEric W. Biederman spin_unlock_irq(&sighand->siglock);
889d80f7d7bSEric W. Biederman }
890d80f7d7bSEric W. Biederman
do_exit(long code)8919af6528eSPeter Zijlstra void __noreturn do_exit(long code)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds struct task_struct *tsk = current;
8941da177e4SLinus Torvalds int group_dead;
8951da177e4SLinus Torvalds
896001c28e5SNicholas Piggin WARN_ON(irqs_disabled());
897001c28e5SNicholas Piggin
898d80f7d7bSEric W. Biederman synchronize_group_exit(tsk, code);
899d80f7d7bSEric W. Biederman
900b1f866b0SChristoph Hellwig WARN_ON(tsk->plug);
90122e2c507SJens Axboe
902586b58caSJann Horn kcov_task_exit(tsk);
90350b5e49cSAlexander Potapenko kmsan_task_exit(tsk);
904586b58caSJann Horn
90592307383SEric W. Biederman coredump_task_exit(tsk);
906a288eeccSTejun Heo ptrace_event(PTRACE_EVENT_EXIT, code);
907fd593511SBeau Belgrave user_events_exit(tsk);
9081da177e4SLinus Torvalds
909f552a27aSHao Xu io_uring_files_cancel();
910d12619b5SOleg Nesterov exit_signals(tsk); /* sets PF_EXITING */
9111da177e4SLinus Torvalds
912bfafe5efSAndrei Vagin seccomp_filter_release(tsk);
913bfafe5efSAndrei Vagin
91451229b49SRik van Riel acct_update_integrals(tsk);
9151da177e4SLinus Torvalds group_dead = atomic_dec_and_test(&tsk->signal->live);
916c3068951SAndrew Morton if (group_dead) {
91743cf75d9Schenqiwu /*
91843cf75d9Schenqiwu * If the last thread of global init has exited, panic
91943cf75d9Schenqiwu * immediately to get a useable coredump.
92043cf75d9Schenqiwu */
92143cf75d9Schenqiwu if (unlikely(is_global_init(tsk)))
92243cf75d9Schenqiwu panic("Attempted to kill init! exitcode=0x%08x\n",
92343cf75d9Schenqiwu tsk->signal->group_exit_code ?: (int)code);
92443cf75d9Schenqiwu
925baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
9262ff678b8SThomas Gleixner hrtimer_cancel(&tsk->signal->real_timer);
927d5b36a4dSOleg Nesterov exit_itimers(tsk);
928baa73d9eSNicolas Pitre #endif
9291f10206cSJiri Pirko if (tsk->mm)
9301f10206cSJiri Pirko setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
931c3068951SAndrew Morton }
932f6ec29a4SKaiGai Kohei acct_collect(code, group_dead);
933522ed776SMiloslav Trmac if (group_dead)
934522ed776SMiloslav Trmac tty_audit_exit();
935fa84cb93SAl Viro audit_free(tsk);
936115085eaSOleg Nesterov
93748d212a2SLinus Torvalds tsk->exit_code = code;
938115085eaSOleg Nesterov taskstats_exit(tsk, group_dead);
939c757249aSShailabh Nagar
9400039962aSDavidlohr Bueso exit_mm();
9411da177e4SLinus Torvalds
9420e464814SKaiGai Kohei if (group_dead)
943f6ec29a4SKaiGai Kohei acct_process();
9440a16b607SMathieu Desnoyers trace_sched_process_exit(tsk);
9450a16b607SMathieu Desnoyers
9461da177e4SLinus Torvalds exit_sem(tsk);
947b34a6b1dSVasiliy Kulikov exit_shm(tsk);
9481ec7f1ddSAl Viro exit_files(tsk);
9491ec7f1ddSAl Viro exit_fs(tsk);
950c39df5faSOleg Nesterov if (group_dead)
951c39df5faSOleg Nesterov disassociate_ctty(1);
9528aac6270SOleg Nesterov exit_task_namespaces(tsk);
953ed3e694dSAl Viro exit_task_work(tsk);
954e6464694SJiri Slaby exit_thread(tsk);
9550b3fcf17SStephane Eranian
9560b3fcf17SStephane Eranian /*
9570b3fcf17SStephane Eranian * Flush inherited counters to the parent - before the parent
9580b3fcf17SStephane Eranian * gets woken up by child-exit notifications.
9590b3fcf17SStephane Eranian *
9600b3fcf17SStephane Eranian * because of cgroup mode, must be called before cgroup_exit()
9610b3fcf17SStephane Eranian */
9620b3fcf17SStephane Eranian perf_event_exit_task(tsk);
9630b3fcf17SStephane Eranian
9648e5bfa8cSOleg Nesterov sched_autogroup_exit_task(tsk);
9651ec41830SLi Zefan cgroup_exit(tsk);
9661da177e4SLinus Torvalds
96733b2fb30SIngo Molnar /*
96824f1e32cSFrederic Weisbecker * FIXME: do that only when needed, using sched_exit tracepoint
96924f1e32cSFrederic Weisbecker */
9707c8df286SOleg Nesterov flush_ptrace_hw_breakpoint(tsk);
97133b2fb30SIngo Molnar
972ccdd29ffSPaul E. McKenney exit_tasks_rcu_start();
973821c7de7SOleg Nesterov exit_notify(tsk, group_dead);
974ef982393SGuillaume Morin proc_exit_connector(tsk);
975c11600e4SDavid Rientjes mpol_put_task_policy(tsk);
97642b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
977c87e2837SIngo Molnar if (unlikely(current->pi_state_cache))
978c87e2837SIngo Molnar kfree(current->pi_state_cache);
97942b2dd0aSAlexey Dobriyan #endif
980c87e2837SIngo Molnar /*
9819a11b49aSIngo Molnar * Make sure we are holding no locks:
982de5097c2SIngo Molnar */
9831b1d2fb4SColin Cross debug_check_no_locks_held();
9841da177e4SLinus Torvalds
985afc847b7SAl Viro if (tsk->io_context)
986b69f2292SLouis Rilling exit_io_context(tsk);
987afc847b7SAl Viro
988b92ce558SJens Axboe if (tsk->splice_pipe)
9894b8a8f1eSAl Viro free_pipe_info(tsk->splice_pipe);
990b92ce558SJens Axboe
9915640f768SEric Dumazet if (tsk->task_frag.page)
9925640f768SEric Dumazet put_page(tsk->task_frag.page);
9935640f768SEric Dumazet
9941a03d3f1SSebastian Andrzej Siewior exit_task_stack_account(tsk);
995e0e81739SDavid Howells
9964bcb8232SOleg Nesterov check_stack_usage();
9977407251aSCoywolf Qi Hunt preempt_disable();
99854848d73SWu Fengguang if (tsk->nr_dirtied)
99954848d73SWu Fengguang __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
1000f41d911fSPaul E. McKenney exit_rcu();
1001ccdd29ffSPaul E. McKenney exit_tasks_rcu_finish();
1002b5740f4bSYasunori Goto
1003b09be676SByungchul Park lockdep_free_task(tsk);
10049af6528eSPeter Zijlstra do_task_dead();
10051da177e4SLinus Torvalds }
1006012914daSRuss Anderson
make_task_dead(int signr)10070e25498fSEric W. Biederman void __noreturn make_task_dead(int signr)
10080e25498fSEric W. Biederman {
10090e25498fSEric W. Biederman /*
10100e25498fSEric W. Biederman * Take the task off the cpu after something catastrophic has
10110e25498fSEric W. Biederman * happened.
101205ea0424SEric W. Biederman *
101305ea0424SEric W. Biederman * We can get here from a kernel oops, sometimes with preemption off.
101405ea0424SEric W. Biederman * Start by checking for critical errors.
101505ea0424SEric W. Biederman * Then fix up important state like USER_DS and preemption.
101605ea0424SEric W. Biederman * Then do everything else.
10170e25498fSEric W. Biederman */
101805ea0424SEric W. Biederman struct task_struct *tsk = current;
10197535b832SKees Cook unsigned int limit;
102005ea0424SEric W. Biederman
102105ea0424SEric W. Biederman if (unlikely(in_interrupt()))
102205ea0424SEric W. Biederman panic("Aiee, killing interrupt handler!");
102305ea0424SEric W. Biederman if (unlikely(!tsk->pid))
102405ea0424SEric W. Biederman panic("Attempted to kill the idle task!");
102505ea0424SEric W. Biederman
1026001c28e5SNicholas Piggin if (unlikely(irqs_disabled())) {
1027001c28e5SNicholas Piggin pr_info("note: %s[%d] exited with irqs disabled\n",
1028001c28e5SNicholas Piggin current->comm, task_pid_nr(current));
1029001c28e5SNicholas Piggin local_irq_enable();
1030001c28e5SNicholas Piggin }
103105ea0424SEric W. Biederman if (unlikely(in_atomic())) {
103205ea0424SEric W. Biederman pr_info("note: %s[%d] exited with preempt_count %d\n",
103305ea0424SEric W. Biederman current->comm, task_pid_nr(current),
103405ea0424SEric W. Biederman preempt_count());
103505ea0424SEric W. Biederman preempt_count_set(PREEMPT_ENABLED);
103605ea0424SEric W. Biederman }
103705ea0424SEric W. Biederman
103805ea0424SEric W. Biederman /*
1039d4ccd54dSJann Horn * Every time the system oopses, if the oops happens while a reference
1040d4ccd54dSJann Horn * to an object was held, the reference leaks.
1041d4ccd54dSJann Horn * If the oops doesn't also leak memory, repeated oopsing can cause
1042d4ccd54dSJann Horn * reference counters to wrap around (if they're not using refcount_t).
1043d4ccd54dSJann Horn * This means that repeated oopsing can make unexploitable-looking bugs
1044d4ccd54dSJann Horn * exploitable through repeated oopsing.
1045d4ccd54dSJann Horn * To make sure this can't happen, place an upper bound on how often the
1046d4ccd54dSJann Horn * kernel may oops without panic().
1047d4ccd54dSJann Horn */
10487535b832SKees Cook limit = READ_ONCE(oops_limit);
10497535b832SKees Cook if (atomic_inc_return(&oops_count) >= limit && limit)
10507535b832SKees Cook panic("Oopsed too often (kernel.oops_limit is %d)", limit);
1051d4ccd54dSJann Horn
1052d4ccd54dSJann Horn /*
105305ea0424SEric W. Biederman * We're taking recursive faults here in make_task_dead. Safest is to just
105405ea0424SEric W. Biederman * leave this task alone and wait for reboot.
105505ea0424SEric W. Biederman */
105605ea0424SEric W. Biederman if (unlikely(tsk->flags & PF_EXITING)) {
105705ea0424SEric W. Biederman pr_alert("Fixing recursive fault but reboot is needed!\n");
105805ea0424SEric W. Biederman futex_exit_recursive(tsk);
1059912616f1SEric W. Biederman tsk->exit_state = EXIT_DEAD;
1060912616f1SEric W. Biederman refcount_inc(&tsk->rcu_users);
10617f80a2fdSEric W. Biederman do_task_dead();
106205ea0424SEric W. Biederman }
106305ea0424SEric W. Biederman
10640e25498fSEric W. Biederman do_exit(signr);
10650e25498fSEric W. Biederman }
10660e25498fSEric W. Biederman
SYSCALL_DEFINE1(exit,int,error_code)1067754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit, int, error_code)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds do_exit((error_code&0xff)<<8);
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds
10721da177e4SLinus Torvalds /*
10731da177e4SLinus Torvalds * Take down every thread in the group. This is called by fatal signals
10741da177e4SLinus Torvalds * as well as by sys_exit_group (below).
10751da177e4SLinus Torvalds */
1076eae654f1SPeter Zijlstra void __noreturn
do_group_exit(int exit_code)10771da177e4SLinus Torvalds do_group_exit(int exit_code)
10781da177e4SLinus Torvalds {
1079bfc4b089SOleg Nesterov struct signal_struct *sig = current->signal;
1080bfc4b089SOleg Nesterov
108149697335SEric W. Biederman if (sig->flags & SIGNAL_GROUP_EXIT)
1082bfc4b089SOleg Nesterov exit_code = sig->group_exit_code;
108349697335SEric W. Biederman else if (sig->group_exec_task)
108449697335SEric W. Biederman exit_code = 0;
1085cbe9dac3SEric W. Biederman else {
10861da177e4SLinus Torvalds struct sighand_struct *const sighand = current->sighand;
1087a0be55deSIonut Alexa
10881da177e4SLinus Torvalds spin_lock_irq(&sighand->siglock);
108949697335SEric W. Biederman if (sig->flags & SIGNAL_GROUP_EXIT)
10901da177e4SLinus Torvalds /* Another thread got here before we took the lock. */
10911da177e4SLinus Torvalds exit_code = sig->group_exit_code;
109249697335SEric W. Biederman else if (sig->group_exec_task)
109349697335SEric W. Biederman exit_code = 0;
10941da177e4SLinus Torvalds else {
10951da177e4SLinus Torvalds sig->group_exit_code = exit_code;
1096ed5d2cacSOleg Nesterov sig->flags = SIGNAL_GROUP_EXIT;
10971da177e4SLinus Torvalds zap_other_threads(current);
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds spin_unlock_irq(&sighand->siglock);
11001da177e4SLinus Torvalds }
11011da177e4SLinus Torvalds
11021da177e4SLinus Torvalds do_exit(exit_code);
11031da177e4SLinus Torvalds /* NOTREACHED */
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds
11061da177e4SLinus Torvalds /*
11071da177e4SLinus Torvalds * this kills every thread in the thread group. Note that any externally
11081da177e4SLinus Torvalds * wait4()-ing process will get the correct exit code - even if this
11091da177e4SLinus Torvalds * thread is not the thread group leader.
11101da177e4SLinus Torvalds */
SYSCALL_DEFINE1(exit_group,int,error_code)1111754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit_group, int, error_code)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds do_group_exit((error_code & 0xff) << 8);
11142ed7c03eSHeiko Carstens /* NOTREACHED */
11152ed7c03eSHeiko Carstens return 0;
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds
eligible_pid(struct wait_opts * wo,struct task_struct * p)1118989264f4SOleg Nesterov static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
11191da177e4SLinus Torvalds {
11205c01ba49SOleg Nesterov return wo->wo_type == PIDTYPE_MAX ||
11215c01ba49SOleg Nesterov task_pid_type(p, wo->wo_type) == wo->wo_pid;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds
1124bf959931SOleg Nesterov static int
eligible_child(struct wait_opts * wo,bool ptrace,struct task_struct * p)1125bf959931SOleg Nesterov eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
11265c01ba49SOleg Nesterov {
11275c01ba49SOleg Nesterov if (!eligible_pid(wo, p))
11285c01ba49SOleg Nesterov return 0;
1129bf959931SOleg Nesterov
1130bf959931SOleg Nesterov /*
1131bf959931SOleg Nesterov * Wait for all children (clone and not) if __WALL is set or
1132bf959931SOleg Nesterov * if it is traced by us.
1133bf959931SOleg Nesterov */
1134bf959931SOleg Nesterov if (ptrace || (wo->wo_flags & __WALL))
1135bf959931SOleg Nesterov return 1;
1136bf959931SOleg Nesterov
1137bf959931SOleg Nesterov /*
1138bf959931SOleg Nesterov * Otherwise, wait for clone children *only* if __WCLONE is set;
1139bf959931SOleg Nesterov * otherwise, wait for non-clone children *only*.
1140bf959931SOleg Nesterov *
1141bf959931SOleg Nesterov * Note: a "clone" child here is one that reports to its parent
1142bf959931SOleg Nesterov * using a signal other than SIGCHLD, or a non-leader thread which
1143bf959931SOleg Nesterov * we can only see if it is traced by us.
1144bf959931SOleg Nesterov */
1145bf959931SOleg Nesterov if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
11461da177e4SLinus Torvalds return 0;
11471da177e4SLinus Torvalds
114814dd0b81SRoland McGrath return 1;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds
11511da177e4SLinus Torvalds /*
11521da177e4SLinus Torvalds * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
11531da177e4SLinus Torvalds * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
11541da177e4SLinus Torvalds * the lock and this task is uninteresting. If we return nonzero, we have
11551da177e4SLinus Torvalds * released the lock and the system call should return.
11561da177e4SLinus Torvalds */
wait_task_zombie(struct wait_opts * wo,struct task_struct * p)11579e8ae01dSOleg Nesterov static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
11581da177e4SLinus Torvalds {
115967d7dddeSAl Viro int state, status;
11606c5f3e7bSPavel Emelyanov pid_t pid = task_pid_vnr(p);
116143e13cc1SOleg Nesterov uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
116267d7dddeSAl Viro struct waitid_info *infop;
11631da177e4SLinus Torvalds
11649e8ae01dSOleg Nesterov if (!likely(wo->wo_flags & WEXITED))
116598abed02SRoland McGrath return 0;
116698abed02SRoland McGrath
11679e8ae01dSOleg Nesterov if (unlikely(wo->wo_flags & WNOWAIT)) {
1168907c311fSEric W. Biederman status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1169907c311fSEric W. Biederman ? p->signal->group_exit_code : p->exit_code;
11701da177e4SLinus Torvalds get_task_struct(p);
11711da177e4SLinus Torvalds read_unlock(&tasklist_lock);
11721029a2b5SPeter Zijlstra sched_annotate_sleep();
1173e61a2502SAl Viro if (wo->wo_rusage)
1174e61a2502SAl Viro getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1175bb380ec3SAl Viro put_task_struct(p);
117676d9871eSAl Viro goto out_info;
11771da177e4SLinus Torvalds }
1178befca967SOleg Nesterov /*
1179abd50b39SOleg Nesterov * Move the task's state to DEAD/TRACE, only one thread can do this.
1180abd50b39SOleg Nesterov */
1181f6507f83SOleg Nesterov state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1182f6507f83SOleg Nesterov EXIT_TRACE : EXIT_DEAD;
1183abd50b39SOleg Nesterov if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1184abd50b39SOleg Nesterov return 0;
1185986094dfSOleg Nesterov /*
1186986094dfSOleg Nesterov * We own this thread, nobody else can reap it.
1187986094dfSOleg Nesterov */
1188986094dfSOleg Nesterov read_unlock(&tasklist_lock);
1189986094dfSOleg Nesterov sched_annotate_sleep();
1190f6507f83SOleg Nesterov
1191abd50b39SOleg Nesterov /*
1192f6507f83SOleg Nesterov * Check thread_group_leader() to exclude the traced sub-threads.
1193befca967SOleg Nesterov */
1194f6507f83SOleg Nesterov if (state == EXIT_DEAD && thread_group_leader(p)) {
1195f953ccd0SOleg Nesterov struct signal_struct *sig = p->signal;
1196f953ccd0SOleg Nesterov struct signal_struct *psig = current->signal;
11971f10206cSJiri Pirko unsigned long maxrss;
11985613fda9SFrederic Weisbecker u64 tgutime, tgstime;
11993795e161SJesper Juhl
12001da177e4SLinus Torvalds /*
12011da177e4SLinus Torvalds * The resource counters for the group leader are in its
12021da177e4SLinus Torvalds * own task_struct. Those for dead threads in the group
12031da177e4SLinus Torvalds * are in its signal_struct, as are those for the child
12041da177e4SLinus Torvalds * processes it has previously reaped. All these
12051da177e4SLinus Torvalds * accumulate in the parent's signal_struct c* fields.
12061da177e4SLinus Torvalds *
12071da177e4SLinus Torvalds * We don't bother to take a lock here to protect these
1208f953ccd0SOleg Nesterov * p->signal fields because the whole thread group is dead
1209f953ccd0SOleg Nesterov * and nobody can change them.
1210f953ccd0SOleg Nesterov *
1211dcca3475SIngo Molnar * psig->stats_lock also protects us from our sub-threads
1212c1be35a1SOleg Nesterov * which can reap other children at the same time.
12130cf55e1eSHidetoshi Seto *
1214a0be55deSIonut Alexa * We use thread_group_cputime_adjusted() to get times for
1215a0be55deSIonut Alexa * the thread group, which consolidates times for all threads
1216a0be55deSIonut Alexa * in the group including the group leader.
12171da177e4SLinus Torvalds */
1218e80d0a1aSFrederic Weisbecker thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1219c1be35a1SOleg Nesterov write_seqlock_irq(&psig->stats_lock);
122064861634SMartin Schwidefsky psig->cutime += tgutime + sig->cutime;
122164861634SMartin Schwidefsky psig->cstime += tgstime + sig->cstime;
12226fac4829SFrederic Weisbecker psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
12233795e161SJesper Juhl psig->cmin_flt +=
12243795e161SJesper Juhl p->min_flt + sig->min_flt + sig->cmin_flt;
12253795e161SJesper Juhl psig->cmaj_flt +=
12263795e161SJesper Juhl p->maj_flt + sig->maj_flt + sig->cmaj_flt;
12273795e161SJesper Juhl psig->cnvcsw +=
12283795e161SJesper Juhl p->nvcsw + sig->nvcsw + sig->cnvcsw;
12293795e161SJesper Juhl psig->cnivcsw +=
12303795e161SJesper Juhl p->nivcsw + sig->nivcsw + sig->cnivcsw;
12316eaeeabaSEric Dumazet psig->cinblock +=
12326eaeeabaSEric Dumazet task_io_get_inblock(p) +
12336eaeeabaSEric Dumazet sig->inblock + sig->cinblock;
12346eaeeabaSEric Dumazet psig->coublock +=
12356eaeeabaSEric Dumazet task_io_get_oublock(p) +
12366eaeeabaSEric Dumazet sig->oublock + sig->coublock;
12371f10206cSJiri Pirko maxrss = max(sig->maxrss, sig->cmaxrss);
12381f10206cSJiri Pirko if (psig->cmaxrss < maxrss)
12391f10206cSJiri Pirko psig->cmaxrss = maxrss;
12405995477aSAndrea Righi task_io_accounting_add(&psig->ioac, &p->ioac);
12415995477aSAndrea Righi task_io_accounting_add(&psig->ioac, &sig->ioac);
1242c1be35a1SOleg Nesterov write_sequnlock_irq(&psig->stats_lock);
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds
1245ce72a16fSAl Viro if (wo->wo_rusage)
1246ce72a16fSAl Viro getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
12471da177e4SLinus Torvalds status = (p->signal->flags & SIGNAL_GROUP_EXIT)
12481da177e4SLinus Torvalds ? p->signal->group_exit_code : p->exit_code;
1249359566faSAl Viro wo->wo_stat = status;
12502f4e6e2aSOleg Nesterov
1251b4360690SOleg Nesterov if (state == EXIT_TRACE) {
12521da177e4SLinus Torvalds write_lock_irq(&tasklist_lock);
12532f4e6e2aSOleg Nesterov /* We dropped tasklist, ptracer could die and untrace */
12542f4e6e2aSOleg Nesterov ptrace_unlink(p);
1255b4360690SOleg Nesterov
1256b4360690SOleg Nesterov /* If parent wants a zombie, don't release it now */
1257abd50b39SOleg Nesterov state = EXIT_ZOMBIE;
1258b4360690SOleg Nesterov if (do_notify_parent(p, p->exit_signal))
1259b4360690SOleg Nesterov state = EXIT_DEAD;
1260abd50b39SOleg Nesterov p->exit_state = state;
12611da177e4SLinus Torvalds write_unlock_irq(&tasklist_lock);
12621da177e4SLinus Torvalds }
1263abd50b39SOleg Nesterov if (state == EXIT_DEAD)
12641da177e4SLinus Torvalds release_task(p);
12652f4e6e2aSOleg Nesterov
126676d9871eSAl Viro out_info:
126776d9871eSAl Viro infop = wo->wo_info;
126876d9871eSAl Viro if (infop) {
126976d9871eSAl Viro if ((status & 0x7f) == 0) {
127076d9871eSAl Viro infop->cause = CLD_EXITED;
127176d9871eSAl Viro infop->status = status >> 8;
127276d9871eSAl Viro } else {
127376d9871eSAl Viro infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
127476d9871eSAl Viro infop->status = status & 0x7f;
127576d9871eSAl Viro }
127676d9871eSAl Viro infop->pid = pid;
127776d9871eSAl Viro infop->uid = uid;
127876d9871eSAl Viro }
127976d9871eSAl Viro
128067d7dddeSAl Viro return pid;
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds
task_stopped_code(struct task_struct * p,bool ptrace)128390bc8d8bSOleg Nesterov static int *task_stopped_code(struct task_struct *p, bool ptrace)
128490bc8d8bSOleg Nesterov {
128590bc8d8bSOleg Nesterov if (ptrace) {
1286570ac933SOleg Nesterov if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
128790bc8d8bSOleg Nesterov return &p->exit_code;
128890bc8d8bSOleg Nesterov } else {
128990bc8d8bSOleg Nesterov if (p->signal->flags & SIGNAL_STOP_STOPPED)
129090bc8d8bSOleg Nesterov return &p->signal->group_exit_code;
129190bc8d8bSOleg Nesterov }
129290bc8d8bSOleg Nesterov return NULL;
129390bc8d8bSOleg Nesterov }
129490bc8d8bSOleg Nesterov
129519e27463STejun Heo /**
129619e27463STejun Heo * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
129719e27463STejun Heo * @wo: wait options
129819e27463STejun Heo * @ptrace: is the wait for ptrace
129919e27463STejun Heo * @p: task to wait for
130019e27463STejun Heo *
130119e27463STejun Heo * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
130219e27463STejun Heo *
130319e27463STejun Heo * CONTEXT:
130419e27463STejun Heo * read_lock(&tasklist_lock), which is released if return value is
130519e27463STejun Heo * non-zero. Also, grabs and releases @p->sighand->siglock.
130619e27463STejun Heo *
130719e27463STejun Heo * RETURNS:
130819e27463STejun Heo * 0 if wait condition didn't exist and search for other wait conditions
130919e27463STejun Heo * should continue. Non-zero return, -errno on failure and @p's pid on
131019e27463STejun Heo * success, implies that tasklist_lock is released and wait condition
131119e27463STejun Heo * search should terminate.
13121da177e4SLinus Torvalds */
wait_task_stopped(struct wait_opts * wo,int ptrace,struct task_struct * p)13139e8ae01dSOleg Nesterov static int wait_task_stopped(struct wait_opts *wo,
13149e8ae01dSOleg Nesterov int ptrace, struct task_struct *p)
13151da177e4SLinus Torvalds {
131667d7dddeSAl Viro struct waitid_info *infop;
131767d7dddeSAl Viro int exit_code, *p_code, why;
1318ee7c82daSOleg Nesterov uid_t uid = 0; /* unneeded, required by compiler */
1319c8950783SOleg Nesterov pid_t pid;
13201da177e4SLinus Torvalds
132147918025SOleg Nesterov /*
132247918025SOleg Nesterov * Traditionally we see ptrace'd stopped tasks regardless of options.
132347918025SOleg Nesterov */
13249e8ae01dSOleg Nesterov if (!ptrace && !(wo->wo_flags & WUNTRACED))
132598abed02SRoland McGrath return 0;
132698abed02SRoland McGrath
132719e27463STejun Heo if (!task_stopped_code(p, ptrace))
132819e27463STejun Heo return 0;
132919e27463STejun Heo
1330ee7c82daSOleg Nesterov exit_code = 0;
1331ee7c82daSOleg Nesterov spin_lock_irq(&p->sighand->siglock);
1332ee7c82daSOleg Nesterov
133390bc8d8bSOleg Nesterov p_code = task_stopped_code(p, ptrace);
133490bc8d8bSOleg Nesterov if (unlikely(!p_code))
1335ee7c82daSOleg Nesterov goto unlock_sig;
1336ee7c82daSOleg Nesterov
133790bc8d8bSOleg Nesterov exit_code = *p_code;
1338ee7c82daSOleg Nesterov if (!exit_code)
1339ee7c82daSOleg Nesterov goto unlock_sig;
1340ee7c82daSOleg Nesterov
13419e8ae01dSOleg Nesterov if (!unlikely(wo->wo_flags & WNOWAIT))
134290bc8d8bSOleg Nesterov *p_code = 0;
1343ee7c82daSOleg Nesterov
13448ca937a6SSasha Levin uid = from_kuid_munged(current_user_ns(), task_uid(p));
1345ee7c82daSOleg Nesterov unlock_sig:
1346ee7c82daSOleg Nesterov spin_unlock_irq(&p->sighand->siglock);
1347ee7c82daSOleg Nesterov if (!exit_code)
13481da177e4SLinus Torvalds return 0;
13491da177e4SLinus Torvalds
13501da177e4SLinus Torvalds /*
13511da177e4SLinus Torvalds * Now we are pretty sure this task is interesting.
13521da177e4SLinus Torvalds * Make sure it doesn't get reaped out from under us while we
13531da177e4SLinus Torvalds * give up the lock and then examine it below. We don't want to
13541da177e4SLinus Torvalds * keep holding onto the tasklist_lock while we call getrusage and
13551da177e4SLinus Torvalds * possibly take page faults for user memory.
13561da177e4SLinus Torvalds */
13571da177e4SLinus Torvalds get_task_struct(p);
13586c5f3e7bSPavel Emelyanov pid = task_pid_vnr(p);
1359f470021aSRoland McGrath why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
13601da177e4SLinus Torvalds read_unlock(&tasklist_lock);
13611029a2b5SPeter Zijlstra sched_annotate_sleep();
1362e61a2502SAl Viro if (wo->wo_rusage)
1363e61a2502SAl Viro getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
13641da177e4SLinus Torvalds put_task_struct(p);
13651da177e4SLinus Torvalds
1366bb380ec3SAl Viro if (likely(!(wo->wo_flags & WNOWAIT)))
1367359566faSAl Viro wo->wo_stat = (exit_code << 8) | 0x7f;
13681da177e4SLinus Torvalds
13691da177e4SLinus Torvalds infop = wo->wo_info;
137067d7dddeSAl Viro if (infop) {
137167d7dddeSAl Viro infop->cause = why;
137267d7dddeSAl Viro infop->status = exit_code;
137367d7dddeSAl Viro infop->pid = pid;
137467d7dddeSAl Viro infop->uid = uid;
137567d7dddeSAl Viro }
137667d7dddeSAl Viro return pid;
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds
13791da177e4SLinus Torvalds /*
13801da177e4SLinus Torvalds * Handle do_wait work for one task in a live, non-stopped state.
13811da177e4SLinus Torvalds * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
13821da177e4SLinus Torvalds * the lock and this task is uninteresting. If we return nonzero, we have
13831da177e4SLinus Torvalds * released the lock and the system call should return.
13841da177e4SLinus Torvalds */
wait_task_continued(struct wait_opts * wo,struct task_struct * p)13859e8ae01dSOleg Nesterov static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
13861da177e4SLinus Torvalds {
1387bb380ec3SAl Viro struct waitid_info *infop;
13881da177e4SLinus Torvalds pid_t pid;
13891da177e4SLinus Torvalds uid_t uid;
13901da177e4SLinus Torvalds
13919e8ae01dSOleg Nesterov if (!unlikely(wo->wo_flags & WCONTINUED))
139298abed02SRoland McGrath return 0;
139398abed02SRoland McGrath
13941da177e4SLinus Torvalds if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
13951da177e4SLinus Torvalds return 0;
13961da177e4SLinus Torvalds
13971da177e4SLinus Torvalds spin_lock_irq(&p->sighand->siglock);
13981da177e4SLinus Torvalds /* Re-check with the lock held. */
13991da177e4SLinus Torvalds if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
14001da177e4SLinus Torvalds spin_unlock_irq(&p->sighand->siglock);
14011da177e4SLinus Torvalds return 0;
14021da177e4SLinus Torvalds }
14039e8ae01dSOleg Nesterov if (!unlikely(wo->wo_flags & WNOWAIT))
14041da177e4SLinus Torvalds p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
14058ca937a6SSasha Levin uid = from_kuid_munged(current_user_ns(), task_uid(p));
14061da177e4SLinus Torvalds spin_unlock_irq(&p->sighand->siglock);
14071da177e4SLinus Torvalds
14086c5f3e7bSPavel Emelyanov pid = task_pid_vnr(p);
14091da177e4SLinus Torvalds get_task_struct(p);
14101da177e4SLinus Torvalds read_unlock(&tasklist_lock);
14111029a2b5SPeter Zijlstra sched_annotate_sleep();
1412ce72a16fSAl Viro if (wo->wo_rusage)
1413ce72a16fSAl Viro getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
14141da177e4SLinus Torvalds put_task_struct(p);
14151da177e4SLinus Torvalds
1416bb380ec3SAl Viro infop = wo->wo_info;
1417bb380ec3SAl Viro if (!infop) {
1418bb380ec3SAl Viro wo->wo_stat = 0xffff;
1419bb380ec3SAl Viro } else {
1420bb380ec3SAl Viro infop->cause = CLD_CONTINUED;
1421bb380ec3SAl Viro infop->pid = pid;
1422bb380ec3SAl Viro infop->uid = uid;
1423bb380ec3SAl Viro infop->status = SIGCONT;
1424bb380ec3SAl Viro }
1425bb380ec3SAl Viro return pid;
14261da177e4SLinus Torvalds }
14271da177e4SLinus Torvalds
142898abed02SRoland McGrath /*
142998abed02SRoland McGrath * Consider @p for a wait by @parent.
143098abed02SRoland McGrath *
14319e8ae01dSOleg Nesterov * -ECHILD should be in ->notask_error before the first call.
143298abed02SRoland McGrath * Returns nonzero for a final return, when we have unlocked tasklist_lock.
143398abed02SRoland McGrath * Returns zero if the search for a child should continue;
14349e8ae01dSOleg Nesterov * then ->notask_error is 0 if @p is an eligible child,
14353a2f5a59SStephen Smalley * or still -ECHILD.
143698abed02SRoland McGrath */
wait_consider_task(struct wait_opts * wo,int ptrace,struct task_struct * p)1437b6e763f0SOleg Nesterov static int wait_consider_task(struct wait_opts *wo, int ptrace,
1438b6e763f0SOleg Nesterov struct task_struct *p)
143998abed02SRoland McGrath {
14403245d6acSOleg Nesterov /*
14413245d6acSOleg Nesterov * We can race with wait_task_zombie() from another thread.
14423245d6acSOleg Nesterov * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
14433245d6acSOleg Nesterov * can't confuse the checks below.
14443245d6acSOleg Nesterov */
14456aa7de05SMark Rutland int exit_state = READ_ONCE(p->exit_state);
1446b3ab0316SOleg Nesterov int ret;
1447b3ab0316SOleg Nesterov
14483245d6acSOleg Nesterov if (unlikely(exit_state == EXIT_DEAD))
1449b3ab0316SOleg Nesterov return 0;
1450b3ab0316SOleg Nesterov
1451bf959931SOleg Nesterov ret = eligible_child(wo, ptrace, p);
145214dd0b81SRoland McGrath if (!ret)
145398abed02SRoland McGrath return ret;
145498abed02SRoland McGrath
14553245d6acSOleg Nesterov if (unlikely(exit_state == EXIT_TRACE)) {
145650b8d257SOleg Nesterov /*
1457abd50b39SOleg Nesterov * ptrace == 0 means we are the natural parent. In this case
1458abd50b39SOleg Nesterov * we should clear notask_error, debugger will notify us.
145950b8d257SOleg Nesterov */
1460abd50b39SOleg Nesterov if (likely(!ptrace))
146150b8d257SOleg Nesterov wo->notask_error = 0;
1462823b018eSTejun Heo return 0;
146350b8d257SOleg Nesterov }
1464823b018eSTejun Heo
1465377d75daSOleg Nesterov if (likely(!ptrace) && unlikely(p->ptrace)) {
1466377d75daSOleg Nesterov /*
1467377d75daSOleg Nesterov * If it is traced by its real parent's group, just pretend
1468377d75daSOleg Nesterov * the caller is ptrace_do_wait() and reap this child if it
1469377d75daSOleg Nesterov * is zombie.
1470377d75daSOleg Nesterov *
1471377d75daSOleg Nesterov * This also hides group stop state from real parent; otherwise
1472377d75daSOleg Nesterov * a single stop can be reported twice as group and ptrace stop.
1473377d75daSOleg Nesterov * If a ptracer wants to distinguish these two events for its
1474377d75daSOleg Nesterov * own children it should create a separate process which takes
1475377d75daSOleg Nesterov * the role of real parent.
1476377d75daSOleg Nesterov */
1477377d75daSOleg Nesterov if (!ptrace_reparented(p))
1478377d75daSOleg Nesterov ptrace = 1;
1479377d75daSOleg Nesterov }
1480377d75daSOleg Nesterov
148145cb24a1STejun Heo /* slay zombie? */
14823245d6acSOleg Nesterov if (exit_state == EXIT_ZOMBIE) {
14837c733eb3SOleg Nesterov /* we don't reap group leaders with subthreads */
14847c733eb3SOleg Nesterov if (!delay_group_leader(p)) {
1485f470021aSRoland McGrath /*
148645cb24a1STejun Heo * A zombie ptracee is only visible to its ptracer.
14877c733eb3SOleg Nesterov * Notification and reaping will be cascaded to the
14887c733eb3SOleg Nesterov * real parent when the ptracer detaches.
1489f470021aSRoland McGrath */
14907c733eb3SOleg Nesterov if (unlikely(ptrace) || likely(!p->ptrace))
14919e8ae01dSOleg Nesterov return wait_task_zombie(wo, p);
14927c733eb3SOleg Nesterov }
149398abed02SRoland McGrath
149498abed02SRoland McGrath /*
14959b84cca2STejun Heo * Allow access to stopped/continued state via zombie by
14969b84cca2STejun Heo * falling through. Clearing of notask_error is complex.
14979b84cca2STejun Heo *
14989b84cca2STejun Heo * When !@ptrace:
14999b84cca2STejun Heo *
15009b84cca2STejun Heo * If WEXITED is set, notask_error should naturally be
15019b84cca2STejun Heo * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
15029b84cca2STejun Heo * so, if there are live subthreads, there are events to
15039b84cca2STejun Heo * wait for. If all subthreads are dead, it's still safe
15049b84cca2STejun Heo * to clear - this function will be called again in finite
15059b84cca2STejun Heo * amount time once all the subthreads are released and
15069b84cca2STejun Heo * will then return without clearing.
15079b84cca2STejun Heo *
15089b84cca2STejun Heo * When @ptrace:
15099b84cca2STejun Heo *
15109b84cca2STejun Heo * Stopped state is per-task and thus can't change once the
15119b84cca2STejun Heo * target task dies. Only continued and exited can happen.
15129b84cca2STejun Heo * Clear notask_error if WCONTINUED | WEXITED.
15139b84cca2STejun Heo */
15149b84cca2STejun Heo if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
15159b84cca2STejun Heo wo->notask_error = 0;
15169b84cca2STejun Heo } else {
15179b84cca2STejun Heo /*
15189b84cca2STejun Heo * @p is alive and it's gonna stop, continue or exit, so
15199b84cca2STejun Heo * there always is something to wait for.
152098abed02SRoland McGrath */
15219e8ae01dSOleg Nesterov wo->notask_error = 0;
15229b84cca2STejun Heo }
152398abed02SRoland McGrath
152445cb24a1STejun Heo /*
152545cb24a1STejun Heo * Wait for stopped. Depending on @ptrace, different stopped state
152645cb24a1STejun Heo * is used and the two don't interact with each other.
152745cb24a1STejun Heo */
152819e27463STejun Heo ret = wait_task_stopped(wo, ptrace, p);
152919e27463STejun Heo if (ret)
153019e27463STejun Heo return ret;
153198abed02SRoland McGrath
153245cb24a1STejun Heo /*
153345cb24a1STejun Heo * Wait for continued. There's only one continued state and the
153445cb24a1STejun Heo * ptracer can consume it which can confuse the real parent. Don't
153545cb24a1STejun Heo * use WCONTINUED from ptracer. You don't need or want it.
153645cb24a1STejun Heo */
15379e8ae01dSOleg Nesterov return wait_task_continued(wo, p);
153898abed02SRoland McGrath }
153998abed02SRoland McGrath
154098abed02SRoland McGrath /*
154198abed02SRoland McGrath * Do the work of do_wait() for one thread in the group, @tsk.
154298abed02SRoland McGrath *
15439e8ae01dSOleg Nesterov * -ECHILD should be in ->notask_error before the first call.
154498abed02SRoland McGrath * Returns nonzero for a final return, when we have unlocked tasklist_lock.
154598abed02SRoland McGrath * Returns zero if the search for a child should continue; then
15469e8ae01dSOleg Nesterov * ->notask_error is 0 if there were any eligible children,
15473a2f5a59SStephen Smalley * or still -ECHILD.
154898abed02SRoland McGrath */
do_wait_thread(struct wait_opts * wo,struct task_struct * tsk)15499e8ae01dSOleg Nesterov static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
155098abed02SRoland McGrath {
155198abed02SRoland McGrath struct task_struct *p;
155298abed02SRoland McGrath
155398abed02SRoland McGrath list_for_each_entry(p, &tsk->children, sibling) {
1554b6e763f0SOleg Nesterov int ret = wait_consider_task(wo, 0, p);
1555a0be55deSIonut Alexa
155698abed02SRoland McGrath if (ret)
155798abed02SRoland McGrath return ret;
155898abed02SRoland McGrath }
155998abed02SRoland McGrath
156098abed02SRoland McGrath return 0;
156198abed02SRoland McGrath }
156298abed02SRoland McGrath
ptrace_do_wait(struct wait_opts * wo,struct task_struct * tsk)15639e8ae01dSOleg Nesterov static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
156498abed02SRoland McGrath {
156598abed02SRoland McGrath struct task_struct *p;
156698abed02SRoland McGrath
1567f470021aSRoland McGrath list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1568b6e763f0SOleg Nesterov int ret = wait_consider_task(wo, 1, p);
1569a0be55deSIonut Alexa
1570f470021aSRoland McGrath if (ret)
157198abed02SRoland McGrath return ret;
157298abed02SRoland McGrath }
157398abed02SRoland McGrath
157498abed02SRoland McGrath return 0;
157598abed02SRoland McGrath }
157698abed02SRoland McGrath
pid_child_should_wake(struct wait_opts * wo,struct task_struct * p)15772e521a20SJens Axboe bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p)
15789d900d4eSJens Axboe {
15799d900d4eSJens Axboe if (!eligible_pid(wo, p))
15809d900d4eSJens Axboe return false;
15819d900d4eSJens Axboe
15829d900d4eSJens Axboe if ((wo->wo_flags & __WNOTHREAD) && wo->child_wait.private != p->parent)
15839d900d4eSJens Axboe return false;
15849d900d4eSJens Axboe
15859d900d4eSJens Axboe return true;
15869d900d4eSJens Axboe }
15879d900d4eSJens Axboe
child_wait_callback(wait_queue_entry_t * wait,unsigned mode,int sync,void * key)1588ac6424b9SIngo Molnar static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
15890b7570e7SOleg Nesterov int sync, void *key)
15900b7570e7SOleg Nesterov {
15910b7570e7SOleg Nesterov struct wait_opts *wo = container_of(wait, struct wait_opts,
15920b7570e7SOleg Nesterov child_wait);
15930b7570e7SOleg Nesterov struct task_struct *p = key;
15940b7570e7SOleg Nesterov
15959d900d4eSJens Axboe if (pid_child_should_wake(wo, p))
15960b7570e7SOleg Nesterov return default_wake_function(wait, mode, sync, key);
15979d900d4eSJens Axboe
15989d900d4eSJens Axboe return 0;
15990b7570e7SOleg Nesterov }
16000b7570e7SOleg Nesterov
__wake_up_parent(struct task_struct * p,struct task_struct * parent)1601a7f0765eSOleg Nesterov void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1602a7f0765eSOleg Nesterov {
16030b7570e7SOleg Nesterov __wake_up_sync_key(&parent->signal->wait_chldexit,
1604ce4dd442SDavid Howells TASK_INTERRUPTIBLE, p);
1605a7f0765eSOleg Nesterov }
1606a7f0765eSOleg Nesterov
is_effectively_child(struct wait_opts * wo,bool ptrace,struct task_struct * target)16075449162aSJim Newsome static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
16085449162aSJim Newsome struct task_struct *target)
16095449162aSJim Newsome {
16105449162aSJim Newsome struct task_struct *parent =
16115449162aSJim Newsome !ptrace ? target->real_parent : target->parent;
16125449162aSJim Newsome
16135449162aSJim Newsome return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
16145449162aSJim Newsome same_thread_group(current, parent));
16155449162aSJim Newsome }
16165449162aSJim Newsome
16175449162aSJim Newsome /*
16185449162aSJim Newsome * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
16195449162aSJim Newsome * and tracee lists to find the target task.
16205449162aSJim Newsome */
do_wait_pid(struct wait_opts * wo)16215449162aSJim Newsome static int do_wait_pid(struct wait_opts *wo)
16225449162aSJim Newsome {
16235449162aSJim Newsome bool ptrace;
16245449162aSJim Newsome struct task_struct *target;
16255449162aSJim Newsome int retval;
16265449162aSJim Newsome
16275449162aSJim Newsome ptrace = false;
16285449162aSJim Newsome target = pid_task(wo->wo_pid, PIDTYPE_TGID);
16295449162aSJim Newsome if (target && is_effectively_child(wo, ptrace, target)) {
16305449162aSJim Newsome retval = wait_consider_task(wo, ptrace, target);
16315449162aSJim Newsome if (retval)
16325449162aSJim Newsome return retval;
16335449162aSJim Newsome }
16345449162aSJim Newsome
16355449162aSJim Newsome ptrace = true;
16365449162aSJim Newsome target = pid_task(wo->wo_pid, PIDTYPE_PID);
16375449162aSJim Newsome if (target && target->ptrace &&
16385449162aSJim Newsome is_effectively_child(wo, ptrace, target)) {
16395449162aSJim Newsome retval = wait_consider_task(wo, ptrace, target);
16405449162aSJim Newsome if (retval)
16415449162aSJim Newsome return retval;
16425449162aSJim Newsome }
16435449162aSJim Newsome
16445449162aSJim Newsome return 0;
16455449162aSJim Newsome }
16465449162aSJim Newsome
__do_wait(struct wait_opts * wo)16472e521a20SJens Axboe long __do_wait(struct wait_opts *wo)
16481da177e4SLinus Torvalds {
164906a101caSJens Axboe long retval;
16501da177e4SLinus Torvalds
165198abed02SRoland McGrath /*
16523da56d16SFrans Klaver * If there is nothing that can match our criteria, just get out.
16539e8ae01dSOleg Nesterov * We will clear ->notask_error to zero if we see any child that
16549e8ae01dSOleg Nesterov * might later match our criteria, even if we are not able to reap
16559e8ae01dSOleg Nesterov * it yet.
165698abed02SRoland McGrath */
165764a16cafSOleg Nesterov wo->notask_error = -ECHILD;
16589e8ae01dSOleg Nesterov if ((wo->wo_type < PIDTYPE_MAX) &&
16591722c14aSChristian Brauner (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
166064a16cafSOleg Nesterov goto notask;
1661161550d7SEric W. Biederman
16621da177e4SLinus Torvalds read_lock(&tasklist_lock);
16635449162aSJim Newsome
16645449162aSJim Newsome if (wo->wo_type == PIDTYPE_PID) {
16655449162aSJim Newsome retval = do_wait_pid(wo);
16665449162aSJim Newsome if (retval)
166706a101caSJens Axboe return retval;
16685449162aSJim Newsome } else {
16695449162aSJim Newsome struct task_struct *tsk = current;
16705449162aSJim Newsome
16711da177e4SLinus Torvalds do {
167264a16cafSOleg Nesterov retval = do_wait_thread(wo, tsk);
167364a16cafSOleg Nesterov if (retval)
167406a101caSJens Axboe return retval;
167564a16cafSOleg Nesterov
167664a16cafSOleg Nesterov retval = ptrace_do_wait(wo, tsk);
167764a16cafSOleg Nesterov if (retval)
167806a101caSJens Axboe return retval;
167998abed02SRoland McGrath
16809e8ae01dSOleg Nesterov if (wo->wo_flags & __WNOTHREAD)
16811da177e4SLinus Torvalds break;
1682a3f6dfb7SOleg Nesterov } while_each_thread(current, tsk);
16835449162aSJim Newsome }
16841da177e4SLinus Torvalds read_unlock(&tasklist_lock);
1685f2cc3eb1SOleg Nesterov
168664a16cafSOleg Nesterov notask:
16879e8ae01dSOleg Nesterov retval = wo->notask_error;
168806a101caSJens Axboe if (!retval && !(wo->wo_flags & WNOHANG))
168906a101caSJens Axboe return -ERESTARTSYS;
169006a101caSJens Axboe
169106a101caSJens Axboe return retval;
169206a101caSJens Axboe }
169306a101caSJens Axboe
do_wait(struct wait_opts * wo)169406a101caSJens Axboe static long do_wait(struct wait_opts *wo)
169506a101caSJens Axboe {
169606a101caSJens Axboe int retval;
169706a101caSJens Axboe
169806a101caSJens Axboe trace_sched_process_wait(wo->wo_pid);
169906a101caSJens Axboe
170006a101caSJens Axboe init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
170106a101caSJens Axboe wo->child_wait.private = current;
170206a101caSJens Axboe add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait);
170306a101caSJens Axboe
170406a101caSJens Axboe do {
170506a101caSJens Axboe set_current_state(TASK_INTERRUPTIBLE);
170606a101caSJens Axboe retval = __do_wait(wo);
170706a101caSJens Axboe if (retval != -ERESTARTSYS)
170806a101caSJens Axboe break;
170906a101caSJens Axboe if (signal_pending(current))
171006a101caSJens Axboe break;
17111da177e4SLinus Torvalds schedule();
171206a101caSJens Axboe } while (1);
171306a101caSJens Axboe
1714f95d39d1SOleg Nesterov __set_current_state(TASK_RUNNING);
17150b7570e7SOleg Nesterov remove_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait);
17161da177e4SLinus Torvalds return retval;
17171da177e4SLinus Torvalds }
17181da177e4SLinus Torvalds
kernel_waitid_prepare(struct wait_opts * wo,int which,pid_t upid,struct waitid_info * infop,int options,struct rusage * ru)17192e521a20SJens Axboe int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid,
1720eda7e9d4SJens Axboe struct waitid_info *infop, int options,
1721eda7e9d4SJens Axboe struct rusage *ru)
17221da177e4SLinus Torvalds {
1723eda7e9d4SJens Axboe unsigned int f_flags = 0;
1724161550d7SEric W. Biederman struct pid *pid = NULL;
1725161550d7SEric W. Biederman enum pid_type type;
17261da177e4SLinus Torvalds
172791c4e8eaSOleg Nesterov if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
172891c4e8eaSOleg Nesterov __WNOTHREAD|__WCLONE|__WALL))
17291da177e4SLinus Torvalds return -EINVAL;
17301da177e4SLinus Torvalds if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
17311da177e4SLinus Torvalds return -EINVAL;
17321da177e4SLinus Torvalds
17331da177e4SLinus Torvalds switch (which) {
17341da177e4SLinus Torvalds case P_ALL:
1735161550d7SEric W. Biederman type = PIDTYPE_MAX;
17361da177e4SLinus Torvalds break;
17371da177e4SLinus Torvalds case P_PID:
1738161550d7SEric W. Biederman type = PIDTYPE_PID;
1739161550d7SEric W. Biederman if (upid <= 0)
17401da177e4SLinus Torvalds return -EINVAL;
17413695eae5SChristian Brauner
17423695eae5SChristian Brauner pid = find_get_pid(upid);
17431da177e4SLinus Torvalds break;
17441da177e4SLinus Torvalds case P_PGID:
1745161550d7SEric W. Biederman type = PIDTYPE_PGID;
1746821cc7b0SEric W. Biederman if (upid < 0)
17471da177e4SLinus Torvalds return -EINVAL;
17483695eae5SChristian Brauner
1749821cc7b0SEric W. Biederman if (upid)
17503695eae5SChristian Brauner pid = find_get_pid(upid);
1751821cc7b0SEric W. Biederman else
1752821cc7b0SEric W. Biederman pid = get_task_pid(current, PIDTYPE_PGID);
17533695eae5SChristian Brauner break;
17543695eae5SChristian Brauner case P_PIDFD:
17553695eae5SChristian Brauner type = PIDTYPE_PID;
17563695eae5SChristian Brauner if (upid < 0)
17573695eae5SChristian Brauner return -EINVAL;
17583695eae5SChristian Brauner
1759ba7d25f3SChristian Brauner pid = pidfd_get_pid(upid, &f_flags);
17603695eae5SChristian Brauner if (IS_ERR(pid))
17613695eae5SChristian Brauner return PTR_ERR(pid);
1762ba7d25f3SChristian Brauner
17631da177e4SLinus Torvalds break;
17641da177e4SLinus Torvalds default:
17651da177e4SLinus Torvalds return -EINVAL;
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds
1768eda7e9d4SJens Axboe wo->wo_type = type;
1769eda7e9d4SJens Axboe wo->wo_pid = pid;
1770eda7e9d4SJens Axboe wo->wo_flags = options;
1771eda7e9d4SJens Axboe wo->wo_info = infop;
1772eda7e9d4SJens Axboe wo->wo_rusage = ru;
1773ba7d25f3SChristian Brauner if (f_flags & O_NONBLOCK)
1774eda7e9d4SJens Axboe wo->wo_flags |= WNOHANG;
1775eda7e9d4SJens Axboe
1776eda7e9d4SJens Axboe return 0;
1777eda7e9d4SJens Axboe }
1778eda7e9d4SJens Axboe
kernel_waitid(int which,pid_t upid,struct waitid_info * infop,int options,struct rusage * ru)1779eda7e9d4SJens Axboe static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
1780eda7e9d4SJens Axboe int options, struct rusage *ru)
1781eda7e9d4SJens Axboe {
1782eda7e9d4SJens Axboe struct wait_opts wo;
1783eda7e9d4SJens Axboe long ret;
1784eda7e9d4SJens Axboe
1785eda7e9d4SJens Axboe ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru);
1786eda7e9d4SJens Axboe if (ret)
1787eda7e9d4SJens Axboe return ret;
1788ba7d25f3SChristian Brauner
17899e8ae01dSOleg Nesterov ret = do_wait(&wo);
1790eda7e9d4SJens Axboe if (!ret && !(options & WNOHANG) && (wo.wo_flags & WNOHANG))
1791ba7d25f3SChristian Brauner ret = -EAGAIN;
1792dfe16dfaSVitaly Mayatskikh
1793eda7e9d4SJens Axboe put_pid(wo.wo_pid);
17941da177e4SLinus Torvalds return ret;
17951da177e4SLinus Torvalds }
17961da177e4SLinus Torvalds
SYSCALL_DEFINE5(waitid,int,which,pid_t,upid,struct siginfo __user *,infop,int,options,struct rusage __user *,ru)1797ce72a16fSAl Viro SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1798ce72a16fSAl Viro infop, int, options, struct rusage __user *, ru)
1799ce72a16fSAl Viro {
1800ce72a16fSAl Viro struct rusage r;
180167d7dddeSAl Viro struct waitid_info info = {.status = 0};
180267d7dddeSAl Viro long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
1803634a8160SAl Viro int signo = 0;
18046c85501fSAl Viro
1805634a8160SAl Viro if (err > 0) {
1806634a8160SAl Viro signo = SIGCHLD;
1807634a8160SAl Viro err = 0;
1808ce72a16fSAl Viro if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1809ce72a16fSAl Viro return -EFAULT;
1810ce72a16fSAl Viro }
181167d7dddeSAl Viro if (!infop)
181267d7dddeSAl Viro return err;
181367d7dddeSAl Viro
181441cd7805SChristophe Leroy if (!user_write_access_begin(infop, sizeof(*infop)))
18151c9fec47SKees Cook return -EFAULT;
181696ca579aSKees Cook
1817634a8160SAl Viro unsafe_put_user(signo, &infop->si_signo, Efault);
18184c48abe9SAl Viro unsafe_put_user(0, &infop->si_errno, Efault);
1819cc731525SEric W. Biederman unsafe_put_user(info.cause, &infop->si_code, Efault);
18204c48abe9SAl Viro unsafe_put_user(info.pid, &infop->si_pid, Efault);
18214c48abe9SAl Viro unsafe_put_user(info.uid, &infop->si_uid, Efault);
18224c48abe9SAl Viro unsafe_put_user(info.status, &infop->si_status, Efault);
182341cd7805SChristophe Leroy user_write_access_end();
1824ce72a16fSAl Viro return err;
18254c48abe9SAl Viro Efault:
182641cd7805SChristophe Leroy user_write_access_end();
18274c48abe9SAl Viro return -EFAULT;
1828ce72a16fSAl Viro }
1829ce72a16fSAl Viro
kernel_wait4(pid_t upid,int __user * stat_addr,int options,struct rusage * ru)183092ebce5aSAl Viro long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
183192ebce5aSAl Viro struct rusage *ru)
18321da177e4SLinus Torvalds {
18339e8ae01dSOleg Nesterov struct wait_opts wo;
1834161550d7SEric W. Biederman struct pid *pid = NULL;
1835161550d7SEric W. Biederman enum pid_type type;
18361da177e4SLinus Torvalds long ret;
18371da177e4SLinus Torvalds
18381da177e4SLinus Torvalds if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
18391da177e4SLinus Torvalds __WNOTHREAD|__WCLONE|__WALL))
18401da177e4SLinus Torvalds return -EINVAL;
1841161550d7SEric W. Biederman
1842dd83c161Szhongjiang /* -INT_MIN is not defined */
1843dd83c161Szhongjiang if (upid == INT_MIN)
1844dd83c161Szhongjiang return -ESRCH;
1845dd83c161Szhongjiang
1846161550d7SEric W. Biederman if (upid == -1)
1847161550d7SEric W. Biederman type = PIDTYPE_MAX;
1848161550d7SEric W. Biederman else if (upid < 0) {
1849161550d7SEric W. Biederman type = PIDTYPE_PGID;
1850161550d7SEric W. Biederman pid = find_get_pid(-upid);
1851161550d7SEric W. Biederman } else if (upid == 0) {
1852161550d7SEric W. Biederman type = PIDTYPE_PGID;
18532ae448efSOleg Nesterov pid = get_task_pid(current, PIDTYPE_PGID);
1854161550d7SEric W. Biederman } else /* upid > 0 */ {
1855161550d7SEric W. Biederman type = PIDTYPE_PID;
1856161550d7SEric W. Biederman pid = find_get_pid(upid);
1857161550d7SEric W. Biederman }
1858161550d7SEric W. Biederman
18599e8ae01dSOleg Nesterov wo.wo_type = type;
18609e8ae01dSOleg Nesterov wo.wo_pid = pid;
18619e8ae01dSOleg Nesterov wo.wo_flags = options | WEXITED;
18629e8ae01dSOleg Nesterov wo.wo_info = NULL;
1863359566faSAl Viro wo.wo_stat = 0;
18649e8ae01dSOleg Nesterov wo.wo_rusage = ru;
18659e8ae01dSOleg Nesterov ret = do_wait(&wo);
1866161550d7SEric W. Biederman put_pid(pid);
1867359566faSAl Viro if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
1868359566faSAl Viro ret = -EFAULT;
18691da177e4SLinus Torvalds
18701da177e4SLinus Torvalds return ret;
18711da177e4SLinus Torvalds }
18721da177e4SLinus Torvalds
kernel_wait(pid_t pid,int * stat)18738043fc14SChristoph Hellwig int kernel_wait(pid_t pid, int *stat)
18748043fc14SChristoph Hellwig {
18758043fc14SChristoph Hellwig struct wait_opts wo = {
18768043fc14SChristoph Hellwig .wo_type = PIDTYPE_PID,
18778043fc14SChristoph Hellwig .wo_pid = find_get_pid(pid),
18788043fc14SChristoph Hellwig .wo_flags = WEXITED,
18798043fc14SChristoph Hellwig };
18808043fc14SChristoph Hellwig int ret;
18818043fc14SChristoph Hellwig
18828043fc14SChristoph Hellwig ret = do_wait(&wo);
18838043fc14SChristoph Hellwig if (ret > 0 && wo.wo_stat)
18848043fc14SChristoph Hellwig *stat = wo.wo_stat;
18858043fc14SChristoph Hellwig put_pid(wo.wo_pid);
18868043fc14SChristoph Hellwig return ret;
18878043fc14SChristoph Hellwig }
18888043fc14SChristoph Hellwig
SYSCALL_DEFINE4(wait4,pid_t,upid,int __user *,stat_addr,int,options,struct rusage __user *,ru)1889ce72a16fSAl Viro SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1890ce72a16fSAl Viro int, options, struct rusage __user *, ru)
1891ce72a16fSAl Viro {
1892ce72a16fSAl Viro struct rusage r;
1893ce72a16fSAl Viro long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
1894ce72a16fSAl Viro
1895ce72a16fSAl Viro if (err > 0) {
1896ce72a16fSAl Viro if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1897ce72a16fSAl Viro return -EFAULT;
1898ce72a16fSAl Viro }
1899ce72a16fSAl Viro return err;
1900ce72a16fSAl Viro }
1901ce72a16fSAl Viro
19021da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_WAITPID
19031da177e4SLinus Torvalds
19041da177e4SLinus Torvalds /*
19051da177e4SLinus Torvalds * sys_waitpid() remains for compatibility. waitpid() should be
19061da177e4SLinus Torvalds * implemented by calling sys_wait4() from libc.a.
19071da177e4SLinus Torvalds */
SYSCALL_DEFINE3(waitpid,pid_t,pid,int __user *,stat_addr,int,options)190817da2bd9SHeiko Carstens SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
19091da177e4SLinus Torvalds {
1910d300b610SDominik Brodowski return kernel_wait4(pid, stat_addr, options, NULL);
19111da177e4SLinus Torvalds }
19121da177e4SLinus Torvalds
19131da177e4SLinus Torvalds #endif
19147e95a225SAl Viro
19157e95a225SAl Viro #ifdef CONFIG_COMPAT
COMPAT_SYSCALL_DEFINE4(wait4,compat_pid_t,pid,compat_uint_t __user *,stat_addr,int,options,struct compat_rusage __user *,ru)19167e95a225SAl Viro COMPAT_SYSCALL_DEFINE4(wait4,
19177e95a225SAl Viro compat_pid_t, pid,
19187e95a225SAl Viro compat_uint_t __user *, stat_addr,
19197e95a225SAl Viro int, options,
19207e95a225SAl Viro struct compat_rusage __user *, ru)
19217e95a225SAl Viro {
19227e95a225SAl Viro struct rusage r;
1923ce72a16fSAl Viro long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
1924ce72a16fSAl Viro if (err > 0) {
1925ce72a16fSAl Viro if (ru && put_compat_rusage(&r, ru))
19267e95a225SAl Viro return -EFAULT;
19277e95a225SAl Viro }
1928ce72a16fSAl Viro return err;
19297e95a225SAl Viro }
19307e95a225SAl Viro
COMPAT_SYSCALL_DEFINE5(waitid,int,which,compat_pid_t,pid,struct compat_siginfo __user *,infop,int,options,struct compat_rusage __user *,uru)19317e95a225SAl Viro COMPAT_SYSCALL_DEFINE5(waitid,
19327e95a225SAl Viro int, which, compat_pid_t, pid,
19337e95a225SAl Viro struct compat_siginfo __user *, infop, int, options,
19347e95a225SAl Viro struct compat_rusage __user *, uru)
19357e95a225SAl Viro {
19367e95a225SAl Viro struct rusage ru;
193767d7dddeSAl Viro struct waitid_info info = {.status = 0};
193867d7dddeSAl Viro long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
1939634a8160SAl Viro int signo = 0;
1940634a8160SAl Viro if (err > 0) {
1941634a8160SAl Viro signo = SIGCHLD;
1942634a8160SAl Viro err = 0;
19436c85501fSAl Viro if (uru) {
194467d7dddeSAl Viro /* kernel_waitid() overwrites everything in ru */
19457e95a225SAl Viro if (COMPAT_USE_64BIT_TIME)
194667d7dddeSAl Viro err = copy_to_user(uru, &ru, sizeof(ru));
19477e95a225SAl Viro else
194867d7dddeSAl Viro err = put_compat_rusage(&ru, uru);
194967d7dddeSAl Viro if (err)
19507e95a225SAl Viro return -EFAULT;
19517e95a225SAl Viro }
19526c85501fSAl Viro }
19537e95a225SAl Viro
19544c48abe9SAl Viro if (!infop)
195567d7dddeSAl Viro return err;
19564c48abe9SAl Viro
195741cd7805SChristophe Leroy if (!user_write_access_begin(infop, sizeof(*infop)))
19581c9fec47SKees Cook return -EFAULT;
195996ca579aSKees Cook
1960634a8160SAl Viro unsafe_put_user(signo, &infop->si_signo, Efault);
19614c48abe9SAl Viro unsafe_put_user(0, &infop->si_errno, Efault);
1962cc731525SEric W. Biederman unsafe_put_user(info.cause, &infop->si_code, Efault);
19634c48abe9SAl Viro unsafe_put_user(info.pid, &infop->si_pid, Efault);
19644c48abe9SAl Viro unsafe_put_user(info.uid, &infop->si_uid, Efault);
19654c48abe9SAl Viro unsafe_put_user(info.status, &infop->si_status, Efault);
196641cd7805SChristophe Leroy user_write_access_end();
19674c48abe9SAl Viro return err;
19684c48abe9SAl Viro Efault:
196941cd7805SChristophe Leroy user_write_access_end();
19704c48abe9SAl Viro return -EFAULT;
19717e95a225SAl Viro }
19727e95a225SAl Viro #endif
19737c2c11b2SSudip Mukherjee
1974c27cd083SMark Rutland /*
1975c27cd083SMark Rutland * This needs to be __function_aligned as GCC implicitly makes any
1976c27cd083SMark Rutland * implementation of abort() cold and drops alignment specified by
1977c27cd083SMark Rutland * -falign-functions=N.
1978c27cd083SMark Rutland *
1979c27cd083SMark Rutland * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
1980c27cd083SMark Rutland */
abort(void)1981c27cd083SMark Rutland __weak __function_aligned void abort(void)
19827c2c11b2SSudip Mukherjee {
19837c2c11b2SSudip Mukherjee BUG();
19847c2c11b2SSudip Mukherjee
19857c2c11b2SSudip Mukherjee /* if that doesn't kill us, halt */
19867c2c11b2SSudip Mukherjee panic("Oops failed to kill thread");
19877c2c11b2SSudip Mukherjee }
1988dc8635b7SAndrew Morton EXPORT_SYMBOL(abort);
1989