xref: /openbmc/linux/kernel/exit.c (revision ee1cd5048959de496cd005c50b137212a5b62062)
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>
289f3acc31SAl Viro #include <linux/fdtable.h>
2980d26af8SMandeep Singh Baines #include <linux/freezer.h>
301da177e4SLinus Torvalds #include <linux/binfmts.h>
31ab516013SSerge E. Hallyn #include <linux/nsproxy.h>
3284d73786SSukadev Bhattiprolu #include <linux/pid_namespace.h>
331da177e4SLinus Torvalds #include <linux/ptrace.h>
341da177e4SLinus Torvalds #include <linux/profile.h>
351da177e4SLinus Torvalds #include <linux/mount.h>
361da177e4SLinus Torvalds #include <linux/proc_fs.h>
3749d769d5SEric W. Biederman #include <linux/kthread.h>
381da177e4SLinus Torvalds #include <linux/mempolicy.h>
39c757249aSShailabh Nagar #include <linux/taskstats_kern.h>
40ca74e92bSShailabh Nagar #include <linux/delayacct.h>
41b4f48b63SPaul Menage #include <linux/cgroup.h>
421da177e4SLinus Torvalds #include <linux/syscalls.h>
437ed20e1aSJesper Juhl #include <linux/signal.h>
446a14c5c9SOleg Nesterov #include <linux/posix-timers.h>
459f46080cSMatt Helsley #include <linux/cn_proc.h>
46de5097c2SIngo Molnar #include <linux/mutex.h>
470771dfefSIngo Molnar #include <linux/futex.h>
48b92ce558SJens Axboe #include <linux/pipe_fs_i.h>
49fa84cb93SAl Viro #include <linux/audit.h> /* for audit_free() */
5083cc5ed3SAdrian Bunk #include <linux/resource.h>
516eaeeabaSEric Dumazet #include <linux/task_io_accounting_ops.h>
52355f841aSEric W. Biederman #include <linux/blkdev.h>
53355f841aSEric W. Biederman #include <linux/task_work.h>
545ad4e53bSAl Viro #include <linux/fs_struct.h>
55d84f4f99SDavid Howells #include <linux/init_task.h>
56cdd6c482SIngo Molnar #include <linux/perf_event.h>
57ad8d75ffSSteven Rostedt #include <trace/events/sched.h>
5824f1e32cSFrederic Weisbecker #include <linux/hw_breakpoint.h>
593d5992d2SYing Han #include <linux/oom.h>
6054848d73SWu Fengguang #include <linux/writeback.h>
6140401530SAl Viro #include <linux/shm.h>
625c9a8750SDmitry Vyukov #include <linux/kcov.h>
6350b5e49cSAlexander Potapenko #include <linux/kmsan.h>
6453d3eaa3SNicolas Pitre #include <linux/random.h>
658f95c90cSDavidlohr Bueso #include <linux/rcuwait.h>
667e95a225SAl Viro #include <linux/compat.h>
67b1b6b5a3SPavel Begunkov #include <linux/io_uring.h>
68670721c7SThomas Gleixner #include <linux/kprobes.h>
6954ecbe6fSMasami Hiramatsu #include <linux/rethook.h>
709db89b41SKees Cook #include <linux/sysfs.h>
71fd593511SBeau Belgrave #include <linux/user_events.h>
721da177e4SLinus Torvalds 
737c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
741da177e4SLinus Torvalds #include <asm/unistd.h>
751da177e4SLinus Torvalds #include <asm/mmu_context.h>
761da177e4SLinus Torvalds 
77d4ccd54dSJann Horn /*
78d4ccd54dSJann Horn  * The default value should be high enough to not crash a system that randomly
79d4ccd54dSJann Horn  * crashes its kernel from time to time, but low enough to at least not permit
80d4ccd54dSJann Horn  * overflowing 32-bit refcounts or the ldsem writer count.
81d4ccd54dSJann Horn  */
82d4ccd54dSJann Horn static unsigned int oops_limit = 10000;
83d4ccd54dSJann Horn 
84d4ccd54dSJann Horn #ifdef CONFIG_SYSCTL
85d4ccd54dSJann Horn static struct ctl_table kern_exit_table[] = {
86d4ccd54dSJann Horn 	{
87d4ccd54dSJann Horn 		.procname       = "oops_limit",
88d4ccd54dSJann Horn 		.data           = &oops_limit,
89d4ccd54dSJann Horn 		.maxlen         = sizeof(oops_limit),
90d4ccd54dSJann Horn 		.mode           = 0644,
91d4ccd54dSJann Horn 		.proc_handler   = proc_douintvec,
92d4ccd54dSJann Horn 	},
93d4ccd54dSJann Horn 	{ }
94d4ccd54dSJann Horn };
95d4ccd54dSJann Horn 
kernel_exit_sysctls_init(void)96d4ccd54dSJann Horn static __init int kernel_exit_sysctls_init(void)
97d4ccd54dSJann Horn {
98d4ccd54dSJann Horn 	register_sysctl_init("kernel", kern_exit_table);
99d4ccd54dSJann Horn 	return 0;
100d4ccd54dSJann Horn }
101d4ccd54dSJann Horn late_initcall(kernel_exit_sysctls_init);
102d4ccd54dSJann Horn #endif
103d4ccd54dSJann Horn 
1049db89b41SKees Cook static atomic_t oops_count = ATOMIC_INIT(0);
1059db89b41SKees Cook 
1069db89b41SKees Cook #ifdef CONFIG_SYSFS
oops_count_show(struct kobject * kobj,struct kobj_attribute * attr,char * page)1079db89b41SKees Cook static ssize_t oops_count_show(struct kobject *kobj, struct kobj_attribute *attr,
1089db89b41SKees Cook 			       char *page)
1099db89b41SKees Cook {
1109db89b41SKees Cook 	return sysfs_emit(page, "%d\n", atomic_read(&oops_count));
1119db89b41SKees Cook }
1129db89b41SKees Cook 
1139db89b41SKees Cook static struct kobj_attribute oops_count_attr = __ATTR_RO(oops_count);
1149db89b41SKees Cook 
kernel_exit_sysfs_init(void)1159db89b41SKees Cook static __init int kernel_exit_sysfs_init(void)
1169db89b41SKees Cook {
1179db89b41SKees Cook 	sysfs_add_file_to_group(kernel_kobj, &oops_count_attr.attr, NULL);
1189db89b41SKees Cook 	return 0;
1199db89b41SKees Cook }
1209db89b41SKees Cook late_initcall(kernel_exit_sysfs_init);
1219db89b41SKees Cook #endif
1229db89b41SKees Cook 
__unhash_process(struct task_struct * p,bool group_dead)123d40e48e0SOleg Nesterov static void __unhash_process(struct task_struct *p, bool group_dead)
1241da177e4SLinus Torvalds {
1251da177e4SLinus Torvalds 	nr_threads--;
12650d75f8dSOleg Nesterov 	detach_pid(p, PIDTYPE_PID);
127d40e48e0SOleg Nesterov 	if (group_dead) {
1286883f81aSEric W. Biederman 		detach_pid(p, PIDTYPE_TGID);
1291da177e4SLinus Torvalds 		detach_pid(p, PIDTYPE_PGID);
1301da177e4SLinus Torvalds 		detach_pid(p, PIDTYPE_SID);
131c97d9893SOleg Nesterov 
1325e85d4abSEric W. Biederman 		list_del_rcu(&p->tasks);
1339cd80bbbSOleg Nesterov 		list_del_init(&p->sibling);
134909ea964SChristoph Lameter 		__this_cpu_dec(process_counts);
1356347e900SEric W. Biederman 	}
13647e65328SOleg Nesterov 	list_del_rcu(&p->thread_group);
1370c740d0aSOleg Nesterov 	list_del_rcu(&p->thread_node);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1406a14c5c9SOleg Nesterov /*
1416a14c5c9SOleg Nesterov  * This function expects the tasklist_lock write-locked.
1426a14c5c9SOleg Nesterov  */
__exit_signal(struct task_struct * tsk)1436a14c5c9SOleg Nesterov static void __exit_signal(struct task_struct *tsk)
1446a14c5c9SOleg Nesterov {
1456a14c5c9SOleg Nesterov 	struct signal_struct *sig = tsk->signal;
146d40e48e0SOleg Nesterov 	bool group_dead = thread_group_leader(tsk);
1476a14c5c9SOleg Nesterov 	struct sighand_struct *sighand;
1483f649ab7SKees Cook 	struct tty_struct *tty;
1495613fda9SFrederic Weisbecker 	u64 utime, stime;
1506a14c5c9SOleg Nesterov 
151d11c563dSPaul E. McKenney 	sighand = rcu_dereference_check(tsk->sighand,
152db1466b3SPaul E. McKenney 					lockdep_tasklist_lock_is_held());
1536a14c5c9SOleg Nesterov 	spin_lock(&sighand->siglock);
1546a14c5c9SOleg Nesterov 
155baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
1566a14c5c9SOleg Nesterov 	posix_cpu_timers_exit(tsk);
157b95e31c0SEric W. Biederman 	if (group_dead)
1586a14c5c9SOleg Nesterov 		posix_cpu_timers_exit_group(tsk);
159baa73d9eSNicolas Pitre #endif
160e0a70217SOleg Nesterov 
161baa73d9eSNicolas Pitre 	if (group_dead) {
162baa73d9eSNicolas Pitre 		tty = sig->tty;
163baa73d9eSNicolas Pitre 		sig->tty = NULL;
164baa73d9eSNicolas Pitre 	} else {
165e0a70217SOleg Nesterov 		/*
1666a14c5c9SOleg Nesterov 		 * If there is any task waiting for the group exit
1676a14c5c9SOleg Nesterov 		 * then notify it:
1686a14c5c9SOleg Nesterov 		 */
169d344193aSOleg Nesterov 		if (sig->notify_count > 0 && !--sig->notify_count)
17060700e38SEric W. Biederman 			wake_up_process(sig->group_exec_task);
1716db840faSOleg Nesterov 
1726a14c5c9SOleg Nesterov 		if (tsk == sig->curr_target)
1736a14c5c9SOleg Nesterov 			sig->curr_target = next_thread(tsk);
17490ed9cbeSRik van Riel 	}
17590ed9cbeSRik van Riel 
17653d3eaa3SNicolas Pitre 	add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
17753d3eaa3SNicolas Pitre 			      sizeof(unsigned long long));
17853d3eaa3SNicolas Pitre 
1796a14c5c9SOleg Nesterov 	/*
18026e75b5cSOleg Nesterov 	 * Accumulate here the counters for all threads as they die. We could
18126e75b5cSOleg Nesterov 	 * skip the group leader because it is the last user of signal_struct,
18226e75b5cSOleg Nesterov 	 * but we want to avoid the race with thread_group_cputime() which can
18326e75b5cSOleg Nesterov 	 * see the empty ->thread_head list.
1846a14c5c9SOleg Nesterov 	 */
1856fac4829SFrederic Weisbecker 	task_cputime(tsk, &utime, &stime);
186e78c3496SRik van Riel 	write_seqlock(&sig->stats_lock);
1876fac4829SFrederic Weisbecker 	sig->utime += utime;
1886fac4829SFrederic Weisbecker 	sig->stime += stime;
1896fac4829SFrederic Weisbecker 	sig->gtime += task_gtime(tsk);
1906a14c5c9SOleg Nesterov 	sig->min_flt += tsk->min_flt;
1916a14c5c9SOleg Nesterov 	sig->maj_flt += tsk->maj_flt;
1926a14c5c9SOleg Nesterov 	sig->nvcsw += tsk->nvcsw;
1936a14c5c9SOleg Nesterov 	sig->nivcsw += tsk->nivcsw;
1946eaeeabaSEric Dumazet 	sig->inblock += task_io_get_inblock(tsk);
1956eaeeabaSEric Dumazet 	sig->oublock += task_io_get_oublock(tsk);
1965995477aSAndrea Righi 	task_io_accounting_add(&sig->ioac, &tsk->ioac);
19732bd671dSPeter Zijlstra 	sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
198b3ac022cSOleg Nesterov 	sig->nr_threads--;
199d40e48e0SOleg Nesterov 	__unhash_process(tsk, group_dead);
200e78c3496SRik van Riel 	write_sequnlock(&sig->stats_lock);
2015876700cSOleg Nesterov 
202da7978b0SOleg Nesterov 	/*
203da7978b0SOleg Nesterov 	 * Do this under ->siglock, we can race with another thread
204da7978b0SOleg Nesterov 	 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
205da7978b0SOleg Nesterov 	 */
206da7978b0SOleg Nesterov 	flush_sigqueue(&tsk->pending);
207a7e5328aSOleg Nesterov 	tsk->sighand = NULL;
2086a14c5c9SOleg Nesterov 	spin_unlock(&sighand->siglock);
2096a14c5c9SOleg Nesterov 
210a7e5328aSOleg Nesterov 	__cleanup_sighand(sighand);
2116a14c5c9SOleg Nesterov 	clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
212d40e48e0SOleg Nesterov 	if (group_dead) {
2136a14c5c9SOleg Nesterov 		flush_sigqueue(&sig->shared_pending);
2144ada856fSOleg Nesterov 		tty_kref_put(tty);
2156a14c5c9SOleg Nesterov 	}
2166a14c5c9SOleg Nesterov }
2176a14c5c9SOleg Nesterov 
delayed_put_task_struct(struct rcu_head * rhp)2188c7904a0SEric W. Biederman static void delayed_put_task_struct(struct rcu_head *rhp)
2198c7904a0SEric W. Biederman {
2200a16b607SMathieu Desnoyers 	struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
2210a16b607SMathieu Desnoyers 
222670721c7SThomas Gleixner 	kprobe_flush_task(tsk);
22354ecbe6fSMasami Hiramatsu 	rethook_flush_task(tsk);
2244e231c79SPeter Zijlstra 	perf_event_delayed_put(tsk);
2250a16b607SMathieu Desnoyers 	trace_sched_process_free(tsk);
2260a16b607SMathieu Desnoyers 	put_task_struct(tsk);
2278c7904a0SEric W. Biederman }
2288c7904a0SEric W. Biederman 
put_task_struct_rcu_user(struct task_struct * task)2293fbd7ee2SEric W. Biederman void put_task_struct_rcu_user(struct task_struct *task)
2303fbd7ee2SEric W. Biederman {
2313fbd7ee2SEric W. Biederman 	if (refcount_dec_and_test(&task->rcu_users))
2323fbd7ee2SEric W. Biederman 		call_rcu(&task->rcu, delayed_put_task_struct);
2333fbd7ee2SEric W. Biederman }
234f470021aSRoland McGrath 
release_thread(struct task_struct * dead_task)2352be9880dSKefeng Wang void __weak release_thread(struct task_struct *dead_task)
2362be9880dSKefeng Wang {
2372be9880dSKefeng Wang }
2382be9880dSKefeng Wang 
release_task(struct task_struct * p)2391da177e4SLinus Torvalds void release_task(struct task_struct *p)
2401da177e4SLinus Torvalds {
24136c8b586SIngo Molnar 	struct task_struct *leader;
2427bc3e6e5SEric W. Biederman 	struct pid *thread_pid;
2431da177e4SLinus Torvalds 	int zap_leader;
2441da177e4SLinus Torvalds repeat:
245c69e8d9cSDavid Howells 	/* don't need to get the RCU readlock here - the process is dead and
246d11c563dSPaul E. McKenney 	 * can't be modifying its own credentials. But shut RCU-lockdep up */
247d11c563dSPaul E. McKenney 	rcu_read_lock();
24821d1c5e3SAlexey Gladkov 	dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
249d11c563dSPaul E. McKenney 	rcu_read_unlock();
250c69e8d9cSDavid Howells 
2516b115bf5STejun Heo 	cgroup_release(p);
2520203026bSIngo Molnar 
2531da177e4SLinus Torvalds 	write_lock_irq(&tasklist_lock);
254a288eeccSTejun Heo 	ptrace_release_task(p);
2557bc3e6e5SEric W. Biederman 	thread_pid = get_pid(p->thread_pid);
2561da177e4SLinus Torvalds 	__exit_signal(p);
25735f5cad8SOleg Nesterov 
2581da177e4SLinus Torvalds 	/*
2591da177e4SLinus Torvalds 	 * If we are the last non-leader member of the thread
2601da177e4SLinus Torvalds 	 * group, and the leader is zombie, then notify the
2611da177e4SLinus Torvalds 	 * group leader's parent process. (if it wants notification.)
2621da177e4SLinus Torvalds 	 */
2631da177e4SLinus Torvalds 	zap_leader = 0;
2641da177e4SLinus Torvalds 	leader = p->group_leader;
265a0be55deSIonut Alexa 	if (leader != p && thread_group_empty(leader)
266a0be55deSIonut Alexa 			&& leader->exit_state == EXIT_ZOMBIE) {
2671da177e4SLinus Torvalds 		/*
2681da177e4SLinus Torvalds 		 * If we were the last child thread and the leader has
2691da177e4SLinus Torvalds 		 * exited already, and the leader's parent ignores SIGCHLD,
2701da177e4SLinus Torvalds 		 * then we are the one who should release the leader.
2711da177e4SLinus Torvalds 		 */
27286773473SOleg Nesterov 		zap_leader = do_notify_parent(leader, leader->exit_signal);
273dae33574SRoland McGrath 		if (zap_leader)
274dae33574SRoland McGrath 			leader->exit_state = EXIT_DEAD;
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
2783a15fb6eSChristian Brauner 	seccomp_filter_release(p);
2797bc3e6e5SEric W. Biederman 	proc_flush_pid(thread_pid);
2806ade99ecSEric W. Biederman 	put_pid(thread_pid);
2811da177e4SLinus Torvalds 	release_thread(p);
2823fbd7ee2SEric W. Biederman 	put_task_struct_rcu_user(p);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	p = leader;
2851da177e4SLinus Torvalds 	if (unlikely(zap_leader))
2861da177e4SLinus Torvalds 		goto repeat;
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds 
rcuwait_wake_up(struct rcuwait * w)2899d9a6ebfSDavidlohr Bueso int rcuwait_wake_up(struct rcuwait *w)
2908f95c90cSDavidlohr Bueso {
2919d9a6ebfSDavidlohr Bueso 	int ret = 0;
2928f95c90cSDavidlohr Bueso 	struct task_struct *task;
2938f95c90cSDavidlohr Bueso 
2948f95c90cSDavidlohr Bueso 	rcu_read_lock();
2958f95c90cSDavidlohr Bueso 
2968f95c90cSDavidlohr Bueso 	/*
2978f95c90cSDavidlohr Bueso 	 * Order condition vs @task, such that everything prior to the load
2988f95c90cSDavidlohr Bueso 	 * of @task is visible. This is the condition as to why the user called
299c9d64a1bSDavidlohr Bueso 	 * rcuwait_wake() in the first place. Pairs with set_current_state()
3008f95c90cSDavidlohr Bueso 	 * barrier (A) in rcuwait_wait_event().
3018f95c90cSDavidlohr Bueso 	 *
3028f95c90cSDavidlohr Bueso 	 *    WAIT                WAKE
3038f95c90cSDavidlohr Bueso 	 *    [S] tsk = current	  [S] cond = true
3048f95c90cSDavidlohr Bueso 	 *        MB (A)	      MB (B)
3058f95c90cSDavidlohr Bueso 	 *    [L] cond		  [L] tsk
3068f95c90cSDavidlohr Bueso 	 */
3076dc080eeSPrateek Sood 	smp_mb(); /* (B) */
3088f95c90cSDavidlohr Bueso 
3098f95c90cSDavidlohr Bueso 	task = rcu_dereference(w->task);
3108f95c90cSDavidlohr Bueso 	if (task)
3119d9a6ebfSDavidlohr Bueso 		ret = wake_up_process(task);
3128f95c90cSDavidlohr Bueso 	rcu_read_unlock();
3139d9a6ebfSDavidlohr Bueso 
3149d9a6ebfSDavidlohr Bueso 	return ret;
3158f95c90cSDavidlohr Bueso }
316ac8dec42SDavidlohr Bueso EXPORT_SYMBOL_GPL(rcuwait_wake_up);
3178f95c90cSDavidlohr Bueso 
318150593bfSOleg Nesterov /*
3191da177e4SLinus Torvalds  * Determine if a process group is "orphaned", according to the POSIX
3201da177e4SLinus Torvalds  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
3211da177e4SLinus Torvalds  * by terminal-generated stop signals.  Newly orphaned process groups are
3221da177e4SLinus Torvalds  * to receive a SIGHUP and a SIGCONT.
3231da177e4SLinus Torvalds  *
3241da177e4SLinus Torvalds  * "I ask you, have you ever known what it is to be an orphan?"
3251da177e4SLinus Torvalds  */
will_become_orphaned_pgrp(struct pid * pgrp,struct task_struct * ignored_task)326a0be55deSIonut Alexa static int will_become_orphaned_pgrp(struct pid *pgrp,
327a0be55deSIonut Alexa 					struct task_struct *ignored_task)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct task_struct *p;
3301da177e4SLinus Torvalds 
3310475ac08SEric W. Biederman 	do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
33205e83df6SOleg Nesterov 		if ((p == ignored_task) ||
33305e83df6SOleg Nesterov 		    (p->exit_state && thread_group_empty(p)) ||
33405e83df6SOleg Nesterov 		    is_global_init(p->real_parent))
3351da177e4SLinus Torvalds 			continue;
33605e83df6SOleg Nesterov 
3370475ac08SEric W. Biederman 		if (task_pgrp(p->real_parent) != pgrp &&
33805e83df6SOleg Nesterov 		    task_session(p->real_parent) == task_session(p))
33905e83df6SOleg Nesterov 			return 0;
3400475ac08SEric W. Biederman 	} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
34105e83df6SOleg Nesterov 
34205e83df6SOleg Nesterov 	return 1;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
is_current_pgrp_orphaned(void)3453e7cd6c4SEric W. Biederman int is_current_pgrp_orphaned(void)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	int retval;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
3503e7cd6c4SEric W. Biederman 	retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
3511da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	return retval;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
has_stopped_jobs(struct pid * pgrp)356961c4675SOleg Nesterov static bool has_stopped_jobs(struct pid *pgrp)
3571da177e4SLinus Torvalds {
3581da177e4SLinus Torvalds 	struct task_struct *p;
3591da177e4SLinus Torvalds 
3600475ac08SEric W. Biederman 	do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
361961c4675SOleg Nesterov 		if (p->signal->flags & SIGNAL_STOP_STOPPED)
362961c4675SOleg Nesterov 			return true;
3630475ac08SEric W. Biederman 	} while_each_pid_task(pgrp, PIDTYPE_PGID, p);
364961c4675SOleg Nesterov 
365961c4675SOleg Nesterov 	return false;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
368f49ee505SOleg Nesterov /*
369f49ee505SOleg Nesterov  * Check to see if any process groups have become orphaned as
370f49ee505SOleg Nesterov  * a result of our exiting, and if they have any stopped jobs,
371f49ee505SOleg Nesterov  * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
372f49ee505SOleg Nesterov  */
373f49ee505SOleg Nesterov static void
kill_orphaned_pgrp(struct task_struct * tsk,struct task_struct * parent)374f49ee505SOleg Nesterov kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
375f49ee505SOleg Nesterov {
376f49ee505SOleg Nesterov 	struct pid *pgrp = task_pgrp(tsk);
377f49ee505SOleg Nesterov 	struct task_struct *ignored_task = tsk;
378f49ee505SOleg Nesterov 
379f49ee505SOleg Nesterov 	if (!parent)
380f49ee505SOleg Nesterov 		/* exit: our father is in a different pgrp than
381f49ee505SOleg Nesterov 		 * we are and we were the only connection outside.
382f49ee505SOleg Nesterov 		 */
383f49ee505SOleg Nesterov 		parent = tsk->real_parent;
384f49ee505SOleg Nesterov 	else
385f49ee505SOleg Nesterov 		/* reparent: our child is in a different pgrp than
386f49ee505SOleg Nesterov 		 * we are, and it was the only connection outside.
387f49ee505SOleg Nesterov 		 */
388f49ee505SOleg Nesterov 		ignored_task = NULL;
389f49ee505SOleg Nesterov 
390f49ee505SOleg Nesterov 	if (task_pgrp(parent) != pgrp &&
391f49ee505SOleg Nesterov 	    task_session(parent) == task_session(tsk) &&
392f49ee505SOleg Nesterov 	    will_become_orphaned_pgrp(pgrp, ignored_task) &&
393f49ee505SOleg Nesterov 	    has_stopped_jobs(pgrp)) {
394f49ee505SOleg Nesterov 		__kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
395f49ee505SOleg Nesterov 		__kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
396f49ee505SOleg Nesterov 	}
397f49ee505SOleg Nesterov }
398f49ee505SOleg Nesterov 
coredump_task_exit(struct task_struct * tsk)39992307383SEric W. Biederman static void coredump_task_exit(struct task_struct *tsk)
400d67e03e3SEric W. Biederman {
401d67e03e3SEric W. Biederman 	struct core_state *core_state;
402d67e03e3SEric W. Biederman 
403d67e03e3SEric W. Biederman 	/*
404d67e03e3SEric W. Biederman 	 * Serialize with any possible pending coredump.
4050258b5fdSEric W. Biederman 	 * We must hold siglock around checking core_state
40692307383SEric W. Biederman 	 * and setting PF_POSTCOREDUMP.  The core-inducing thread
407d67e03e3SEric W. Biederman 	 * will increment ->nr_threads for each thread in the
40892307383SEric W. Biederman 	 * group without PF_POSTCOREDUMP set.
409d67e03e3SEric W. Biederman 	 */
4100258b5fdSEric W. Biederman 	spin_lock_irq(&tsk->sighand->siglock);
41192307383SEric W. Biederman 	tsk->flags |= PF_POSTCOREDUMP;
4120258b5fdSEric W. Biederman 	core_state = tsk->signal->core_state;
4130258b5fdSEric W. Biederman 	spin_unlock_irq(&tsk->sighand->siglock);
414f9010dbdSMike Christie 
415f9010dbdSMike Christie 	/* The vhost_worker does not particpate in coredumps */
416f9010dbdSMike Christie 	if (core_state &&
417f9010dbdSMike Christie 	    ((tsk->flags & (PF_IO_WORKER | PF_USER_WORKER)) != PF_USER_WORKER)) {
418d67e03e3SEric W. Biederman 		struct core_thread self;
419d67e03e3SEric W. Biederman 
420d67e03e3SEric W. Biederman 		self.task = current;
421d67e03e3SEric W. Biederman 		if (self.task->flags & PF_SIGNALED)
422d67e03e3SEric W. Biederman 			self.next = xchg(&core_state->dumper.next, &self);
423d67e03e3SEric W. Biederman 		else
424d67e03e3SEric W. Biederman 			self.task = NULL;
425d67e03e3SEric W. Biederman 		/*
426d67e03e3SEric W. Biederman 		 * Implies mb(), the result of xchg() must be visible
427d67e03e3SEric W. Biederman 		 * to core_state->dumper.
428d67e03e3SEric W. Biederman 		 */
429d67e03e3SEric W. Biederman 		if (atomic_dec_and_test(&core_state->nr_threads))
430d67e03e3SEric W. Biederman 			complete(&core_state->startup);
431d67e03e3SEric W. Biederman 
432d67e03e3SEric W. Biederman 		for (;;) {
433f5d39b02SPeter Zijlstra 			set_current_state(TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
434d67e03e3SEric W. Biederman 			if (!self.task) /* see coredump_finish() */
435d67e03e3SEric W. Biederman 				break;
436f5d39b02SPeter Zijlstra 			schedule();
437d67e03e3SEric W. Biederman 		}
438d67e03e3SEric W. Biederman 		__set_current_state(TASK_RUNNING);
439d67e03e3SEric W. Biederman 	}
440d67e03e3SEric W. Biederman }
441d67e03e3SEric W. Biederman 
442f98bafa0SOleg Nesterov #ifdef CONFIG_MEMCG
443cf475ad2SBalbir Singh /*
444733eda7aSKAMEZAWA Hiroyuki  * A task is exiting.   If it owned this mm, find a new owner for the mm.
445cf475ad2SBalbir Singh  */
mm_update_next_owner(struct mm_struct * mm)446cf475ad2SBalbir Singh void mm_update_next_owner(struct mm_struct *mm)
447cf475ad2SBalbir Singh {
448cf475ad2SBalbir Singh 	struct task_struct *c, *g, *p = current;
449cf475ad2SBalbir Singh 
450cf475ad2SBalbir Singh retry:
451733eda7aSKAMEZAWA Hiroyuki 	/*
452733eda7aSKAMEZAWA Hiroyuki 	 * If the exiting or execing task is not the owner, it's
453733eda7aSKAMEZAWA Hiroyuki 	 * someone else's problem.
454733eda7aSKAMEZAWA Hiroyuki 	 */
455733eda7aSKAMEZAWA Hiroyuki 	if (mm->owner != p)
456cf475ad2SBalbir Singh 		return;
457733eda7aSKAMEZAWA Hiroyuki 	/*
458733eda7aSKAMEZAWA Hiroyuki 	 * The current owner is exiting/execing and there are no other
459733eda7aSKAMEZAWA Hiroyuki 	 * candidates.  Do not leave the mm pointing to a possibly
460733eda7aSKAMEZAWA Hiroyuki 	 * freed task structure.
461733eda7aSKAMEZAWA Hiroyuki 	 */
462733eda7aSKAMEZAWA Hiroyuki 	if (atomic_read(&mm->mm_users) <= 1) {
463987717e5SAndrea Arcangeli 		WRITE_ONCE(mm->owner, NULL);
464733eda7aSKAMEZAWA Hiroyuki 		return;
465733eda7aSKAMEZAWA Hiroyuki 	}
466cf475ad2SBalbir Singh 
467cf475ad2SBalbir Singh 	read_lock(&tasklist_lock);
468cf475ad2SBalbir Singh 	/*
469cf475ad2SBalbir Singh 	 * Search in the children
470cf475ad2SBalbir Singh 	 */
471cf475ad2SBalbir Singh 	list_for_each_entry(c, &p->children, sibling) {
472cf475ad2SBalbir Singh 		if (c->mm == mm)
473cf475ad2SBalbir Singh 			goto assign_new_owner;
474cf475ad2SBalbir Singh 	}
475cf475ad2SBalbir Singh 
476cf475ad2SBalbir Singh 	/*
477cf475ad2SBalbir Singh 	 * Search in the siblings
478cf475ad2SBalbir Singh 	 */
479dea33cfdSOleg Nesterov 	list_for_each_entry(c, &p->real_parent->children, sibling) {
480cf475ad2SBalbir Singh 		if (c->mm == mm)
481cf475ad2SBalbir Singh 			goto assign_new_owner;
482cf475ad2SBalbir Singh 	}
483cf475ad2SBalbir Singh 
484cf475ad2SBalbir Singh 	/*
485f87fb599SOleg Nesterov 	 * Search through everything else, we should not get here often.
486cf475ad2SBalbir Singh 	 */
48739af1765SOleg Nesterov 	for_each_process(g) {
488*79ad410cSJinliang Zheng 		if (atomic_read(&mm->mm_users) <= 1)
489*79ad410cSJinliang Zheng 			break;
49039af1765SOleg Nesterov 		if (g->flags & PF_KTHREAD)
49139af1765SOleg Nesterov 			continue;
49239af1765SOleg Nesterov 		for_each_thread(g, c) {
49339af1765SOleg Nesterov 			if (c->mm == mm)
494cf475ad2SBalbir Singh 				goto assign_new_owner;
49539af1765SOleg Nesterov 			if (c->mm)
49639af1765SOleg Nesterov 				break;
49739af1765SOleg Nesterov 		}
498f87fb599SOleg Nesterov 	}
499cf475ad2SBalbir Singh 	read_unlock(&tasklist_lock);
50031a78f23SBalbir Singh 	/*
50131a78f23SBalbir Singh 	 * We found no owner yet mm_users > 1: this implies that we are
50231a78f23SBalbir Singh 	 * most likely racing with swapoff (try_to_unuse()) or /proc or
503e5991371SHugh Dickins 	 * ptrace or page migration (get_task_mm()).  Mark owner as NULL.
50431a78f23SBalbir Singh 	 */
505987717e5SAndrea Arcangeli 	WRITE_ONCE(mm->owner, NULL);
506cf475ad2SBalbir Singh 	return;
507cf475ad2SBalbir Singh 
508cf475ad2SBalbir Singh assign_new_owner:
509cf475ad2SBalbir Singh 	BUG_ON(c == p);
510cf475ad2SBalbir Singh 	get_task_struct(c);
511cf475ad2SBalbir Singh 	/*
512cf475ad2SBalbir Singh 	 * The task_lock protects c->mm from changing.
513cf475ad2SBalbir Singh 	 * We always want mm->owner->mm == mm
514cf475ad2SBalbir Singh 	 */
515cf475ad2SBalbir Singh 	task_lock(c);
516e5991371SHugh Dickins 	/*
517e5991371SHugh Dickins 	 * Delay read_unlock() till we have the task_lock()
518e5991371SHugh Dickins 	 * to ensure that c does not slip away underneath us
519e5991371SHugh Dickins 	 */
520e5991371SHugh Dickins 	read_unlock(&tasklist_lock);
521cf475ad2SBalbir Singh 	if (c->mm != mm) {
522cf475ad2SBalbir Singh 		task_unlock(c);
523cf475ad2SBalbir Singh 		put_task_struct(c);
524cf475ad2SBalbir Singh 		goto retry;
525cf475ad2SBalbir Singh 	}
526987717e5SAndrea Arcangeli 	WRITE_ONCE(mm->owner, c);
527bd74fdaeSYu Zhao 	lru_gen_migrate_mm(mm);
528cf475ad2SBalbir Singh 	task_unlock(c);
529cf475ad2SBalbir Singh 	put_task_struct(c);
530cf475ad2SBalbir Singh }
531f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */
532cf475ad2SBalbir Singh 
5331da177e4SLinus Torvalds /*
5341da177e4SLinus Torvalds  * Turn us into a lazy TLB process if we
5351da177e4SLinus Torvalds  * aren't already..
5361da177e4SLinus Torvalds  */
exit_mm(void)5370039962aSDavidlohr Bueso static void exit_mm(void)
5381da177e4SLinus Torvalds {
5390039962aSDavidlohr Bueso 	struct mm_struct *mm = current->mm;
5401da177e4SLinus Torvalds 
5414610ba7aSThomas Gleixner 	exit_mm_release(current, mm);
5421da177e4SLinus Torvalds 	if (!mm)
5431da177e4SLinus Torvalds 		return;
5444fe7efdbSKonstantin Khlebnikov 	sync_mm_rss(mm);
545d8ed45c5SMichel Lespinasse 	mmap_read_lock(mm);
546aa464ba9SNicholas Piggin 	mmgrab_lazy_tlb(mm);
5470039962aSDavidlohr Bueso 	BUG_ON(mm != current->active_mm);
5481da177e4SLinus Torvalds 	/* more a memory barrier than a real lock */
5490039962aSDavidlohr Bueso 	task_lock(current);
5505bc78502SMathieu Desnoyers 	/*
5515bc78502SMathieu Desnoyers 	 * When a thread stops operating on an address space, the loop
5525bc78502SMathieu Desnoyers 	 * in membarrier_private_expedited() may not observe that
5535bc78502SMathieu Desnoyers 	 * tsk->mm, and the loop in membarrier_global_expedited() may
5545bc78502SMathieu Desnoyers 	 * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
5555bc78502SMathieu Desnoyers 	 * rq->membarrier_state, so those would not issue an IPI.
5565bc78502SMathieu Desnoyers 	 * Membarrier requires a memory barrier after accessing
5575bc78502SMathieu Desnoyers 	 * user-space memory, before clearing tsk->mm or the
5585bc78502SMathieu Desnoyers 	 * rq->membarrier_state.
5595bc78502SMathieu Desnoyers 	 */
5605bc78502SMathieu Desnoyers 	smp_mb__after_spinlock();
5615bc78502SMathieu Desnoyers 	local_irq_disable();
5620039962aSDavidlohr Bueso 	current->mm = NULL;
5635bc78502SMathieu Desnoyers 	membarrier_update_current_mm(NULL);
5641da177e4SLinus Torvalds 	enter_lazy_tlb(mm, current);
5655bc78502SMathieu Desnoyers 	local_irq_enable();
5660039962aSDavidlohr Bueso 	task_unlock(current);
5675bc78502SMathieu Desnoyers 	mmap_read_unlock(mm);
568cf475ad2SBalbir Singh 	mm_update_next_owner(mm);
5691da177e4SLinus Torvalds 	mmput(mm);
570c32b3cbeSMichal Hocko 	if (test_thread_flag(TIF_MEMDIE))
57138531201STetsuo Handa 		exit_oom_victim();
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
find_alive_thread(struct task_struct * p)574c9dc05bfSOleg Nesterov static struct task_struct *find_alive_thread(struct task_struct *p)
575c9dc05bfSOleg Nesterov {
576c9dc05bfSOleg Nesterov 	struct task_struct *t;
577c9dc05bfSOleg Nesterov 
578c9dc05bfSOleg Nesterov 	for_each_thread(p, t) {
579c9dc05bfSOleg Nesterov 		if (!(t->flags & PF_EXITING))
580c9dc05bfSOleg Nesterov 			return t;
581c9dc05bfSOleg Nesterov 	}
582c9dc05bfSOleg Nesterov 	return NULL;
583c9dc05bfSOleg Nesterov }
584c9dc05bfSOleg Nesterov 
find_child_reaper(struct task_struct * father,struct list_head * dead)5858fb335e0SAndrei Vagin static struct task_struct *find_child_reaper(struct task_struct *father,
5868fb335e0SAndrei Vagin 						struct list_head *dead)
5871109909cSOleg Nesterov 	__releases(&tasklist_lock)
5881109909cSOleg Nesterov 	__acquires(&tasklist_lock)
5891109909cSOleg Nesterov {
5901109909cSOleg Nesterov 	struct pid_namespace *pid_ns = task_active_pid_ns(father);
5911109909cSOleg Nesterov 	struct task_struct *reaper = pid_ns->child_reaper;
5928fb335e0SAndrei Vagin 	struct task_struct *p, *n;
5931109909cSOleg Nesterov 
5941109909cSOleg Nesterov 	if (likely(reaper != father))
5951109909cSOleg Nesterov 		return reaper;
5961109909cSOleg Nesterov 
597c9dc05bfSOleg Nesterov 	reaper = find_alive_thread(father);
598c9dc05bfSOleg Nesterov 	if (reaper) {
5991109909cSOleg Nesterov 		pid_ns->child_reaper = reaper;
6001109909cSOleg Nesterov 		return reaper;
6011109909cSOleg Nesterov 	}
6021109909cSOleg Nesterov 
6031109909cSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
6048fb335e0SAndrei Vagin 
6058fb335e0SAndrei Vagin 	list_for_each_entry_safe(p, n, dead, ptrace_entry) {
6068fb335e0SAndrei Vagin 		list_del_init(&p->ptrace_entry);
6078fb335e0SAndrei Vagin 		release_task(p);
6088fb335e0SAndrei Vagin 	}
6098fb335e0SAndrei Vagin 
6101109909cSOleg Nesterov 	zap_pid_ns_processes(pid_ns);
6111109909cSOleg Nesterov 	write_lock_irq(&tasklist_lock);
6121109909cSOleg Nesterov 
6131109909cSOleg Nesterov 	return father;
6141109909cSOleg Nesterov }
6151109909cSOleg Nesterov 
6161da177e4SLinus Torvalds /*
617ebec18a6SLennart Poettering  * When we die, we re-parent all our children, and try to:
618ebec18a6SLennart Poettering  * 1. give them to another thread in our thread group, if such a member exists
619ebec18a6SLennart Poettering  * 2. give it to the first ancestor process which prctl'd itself as a
620ebec18a6SLennart Poettering  *    child_subreaper for its children (like a service manager)
621ebec18a6SLennart Poettering  * 3. give it to the init process (PID 1) in our pid namespace
6221da177e4SLinus Torvalds  */
find_new_reaper(struct task_struct * father,struct task_struct * child_reaper)6231109909cSOleg Nesterov static struct task_struct *find_new_reaper(struct task_struct *father,
6241109909cSOleg Nesterov 					   struct task_struct *child_reaper)
625950bbabbSOleg Nesterov {
626c9dc05bfSOleg Nesterov 	struct task_struct *thread, *reaper;
627950bbabbSOleg Nesterov 
628c9dc05bfSOleg Nesterov 	thread = find_alive_thread(father);
629c9dc05bfSOleg Nesterov 	if (thread)
630950bbabbSOleg Nesterov 		return thread;
631950bbabbSOleg Nesterov 
6327d24e2dfSOleg Nesterov 	if (father->signal->has_child_subreaper) {
633c6c70f44SOleg Nesterov 		unsigned int ns_level = task_pid(father)->level;
634ebec18a6SLennart Poettering 		/*
635175aed3fSOleg Nesterov 		 * Find the first ->is_child_subreaper ancestor in our pid_ns.
636c6c70f44SOleg Nesterov 		 * We can't check reaper != child_reaper to ensure we do not
637c6c70f44SOleg Nesterov 		 * cross the namespaces, the exiting parent could be injected
638c6c70f44SOleg Nesterov 		 * by setns() + fork().
639c6c70f44SOleg Nesterov 		 * We check pid->level, this is slightly more efficient than
640c6c70f44SOleg Nesterov 		 * task_active_pid_ns(reaper) != task_active_pid_ns(father).
641ebec18a6SLennart Poettering 		 */
642c6c70f44SOleg Nesterov 		for (reaper = father->real_parent;
643c6c70f44SOleg Nesterov 		     task_pid(reaper)->level == ns_level;
644ebec18a6SLennart Poettering 		     reaper = reaper->real_parent) {
645175aed3fSOleg Nesterov 			if (reaper == &init_task)
646ebec18a6SLennart Poettering 				break;
647ebec18a6SLennart Poettering 			if (!reaper->signal->is_child_subreaper)
648ebec18a6SLennart Poettering 				continue;
649c9dc05bfSOleg Nesterov 			thread = find_alive_thread(reaper);
650c9dc05bfSOleg Nesterov 			if (thread)
6518a1296aeSOleg Nesterov 				return thread;
6523750ef97SOleg Nesterov 		}
653ebec18a6SLennart Poettering 	}
654950bbabbSOleg Nesterov 
6551109909cSOleg Nesterov 	return child_reaper;
656950bbabbSOleg Nesterov }
657950bbabbSOleg Nesterov 
6585dfc80beSOleg Nesterov /*
6595dfc80beSOleg Nesterov * Any that need to be release_task'd are put on the @dead list.
6605dfc80beSOleg Nesterov  */
reparent_leader(struct task_struct * father,struct task_struct * p,struct list_head * dead)6619cd80bbbSOleg Nesterov static void reparent_leader(struct task_struct *father, struct task_struct *p,
6625dfc80beSOleg Nesterov 				struct list_head *dead)
6635dfc80beSOleg Nesterov {
6642831096eSOleg Nesterov 	if (unlikely(p->exit_state == EXIT_DEAD))
6655dfc80beSOleg Nesterov 		return;
6665dfc80beSOleg Nesterov 
667abd50b39SOleg Nesterov 	/* We don't want people slaying init. */
6685dfc80beSOleg Nesterov 	p->exit_signal = SIGCHLD;
6695dfc80beSOleg Nesterov 
6705dfc80beSOleg Nesterov 	/* If it has exited notify the new parent about this child's death. */
671d21142ecSTejun Heo 	if (!p->ptrace &&
6725dfc80beSOleg Nesterov 	    p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
67386773473SOleg Nesterov 		if (do_notify_parent(p, p->exit_signal)) {
6745dfc80beSOleg Nesterov 			p->exit_state = EXIT_DEAD;
675dc2fd4b0SOleg Nesterov 			list_add(&p->ptrace_entry, dead);
6765dfc80beSOleg Nesterov 		}
6775dfc80beSOleg Nesterov 	}
6785dfc80beSOleg Nesterov 
6795dfc80beSOleg Nesterov 	kill_orphaned_pgrp(p, father);
6805dfc80beSOleg Nesterov }
6815dfc80beSOleg Nesterov 
682482a3767SOleg Nesterov /*
683482a3767SOleg Nesterov  * This does two things:
684482a3767SOleg Nesterov  *
685482a3767SOleg Nesterov  * A.  Make init inherit all the child processes
686482a3767SOleg Nesterov  * B.  Check to see if any process groups have become orphaned
687482a3767SOleg Nesterov  *	as a result of our exiting, and if they have any stopped
688482a3767SOleg Nesterov  *	jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
689482a3767SOleg Nesterov  */
forget_original_parent(struct task_struct * father,struct list_head * dead)690482a3767SOleg Nesterov static void forget_original_parent(struct task_struct *father,
691482a3767SOleg Nesterov 					struct list_head *dead)
6921da177e4SLinus Torvalds {
693482a3767SOleg Nesterov 	struct task_struct *p, *t, *reaper;
694762a24beSOleg Nesterov 
6957c8bd232SOleg Nesterov 	if (unlikely(!list_empty(&father->ptraced)))
696482a3767SOleg Nesterov 		exit_ptrace(father, dead);
697f470021aSRoland McGrath 
6987c8bd232SOleg Nesterov 	/* Can drop and reacquire tasklist_lock */
6998fb335e0SAndrei Vagin 	reaper = find_child_reaper(father, dead);
700ad9e206aSOleg Nesterov 	if (list_empty(&father->children))
701482a3767SOleg Nesterov 		return;
7021109909cSOleg Nesterov 
7031109909cSOleg Nesterov 	reaper = find_new_reaper(father, reaper);
7042831096eSOleg Nesterov 	list_for_each_entry(p, &father->children, sibling) {
70557a05918SOleg Nesterov 		for_each_thread(p, t) {
70622a34c6fSMadhuparna Bhowmik 			RCU_INIT_POINTER(t->real_parent, reaper);
70722a34c6fSMadhuparna Bhowmik 			BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
70857a05918SOleg Nesterov 			if (likely(!t->ptrace))
7099cd80bbbSOleg Nesterov 				t->parent = t->real_parent;
7109cd80bbbSOleg Nesterov 			if (t->pdeath_signal)
7119cd80bbbSOleg Nesterov 				group_send_sig_info(t->pdeath_signal,
71201024980SEric W. Biederman 						    SEND_SIG_NOINFO, t,
71301024980SEric W. Biederman 						    PIDTYPE_TGID);
71457a05918SOleg Nesterov 		}
7152831096eSOleg Nesterov 		/*
7162831096eSOleg Nesterov 		 * If this is a threaded reparent there is no need to
7172831096eSOleg Nesterov 		 * notify anyone anything has happened.
7182831096eSOleg Nesterov 		 */
7192831096eSOleg Nesterov 		if (!same_thread_group(reaper, father))
720482a3767SOleg Nesterov 			reparent_leader(father, p, dead);
7211da177e4SLinus Torvalds 	}
7222831096eSOleg Nesterov 	list_splice_tail_init(&father->children, &reaper->children);
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds /*
7261da177e4SLinus Torvalds  * Send signals to all our closest relatives so that they know
7271da177e4SLinus Torvalds  * to properly mourn us..
7281da177e4SLinus Torvalds  */
exit_notify(struct task_struct * tsk,int group_dead)729821c7de7SOleg Nesterov static void exit_notify(struct task_struct *tsk, int group_dead)
7301da177e4SLinus Torvalds {
73153c8f9f1SOleg Nesterov 	bool autoreap;
732482a3767SOleg Nesterov 	struct task_struct *p, *n;
733482a3767SOleg Nesterov 	LIST_HEAD(dead);
7341da177e4SLinus Torvalds 
735762a24beSOleg Nesterov 	write_lock_irq(&tasklist_lock);
736482a3767SOleg Nesterov 	forget_original_parent(tsk, &dead);
737482a3767SOleg Nesterov 
738821c7de7SOleg Nesterov 	if (group_dead)
739821c7de7SOleg Nesterov 		kill_orphaned_pgrp(tsk->group_leader, NULL);
7401da177e4SLinus Torvalds 
741b191d649SSuren Baghdasaryan 	tsk->exit_state = EXIT_ZOMBIE;
74245cdf5ccSOleg Nesterov 	if (unlikely(tsk->ptrace)) {
74345cdf5ccSOleg Nesterov 		int sig = thread_group_leader(tsk) &&
74445cdf5ccSOleg Nesterov 				thread_group_empty(tsk) &&
74545cdf5ccSOleg Nesterov 				!ptrace_reparented(tsk) ?
74645cdf5ccSOleg Nesterov 			tsk->exit_signal : SIGCHLD;
74745cdf5ccSOleg Nesterov 		autoreap = do_notify_parent(tsk, sig);
74845cdf5ccSOleg Nesterov 	} else if (thread_group_leader(tsk)) {
74945cdf5ccSOleg Nesterov 		autoreap = thread_group_empty(tsk) &&
75045cdf5ccSOleg Nesterov 			do_notify_parent(tsk, tsk->exit_signal);
75145cdf5ccSOleg Nesterov 	} else {
75245cdf5ccSOleg Nesterov 		autoreap = true;
75345cdf5ccSOleg Nesterov 	}
7541da177e4SLinus Torvalds 
75530b692d3SChristian Brauner 	if (autoreap) {
75630b692d3SChristian Brauner 		tsk->exit_state = EXIT_DEAD;
7576c66e7dbSOleg Nesterov 		list_add(&tsk->ptrace_entry, &dead);
75830b692d3SChristian Brauner 	}
7591da177e4SLinus Torvalds 
7609c339168SOleg Nesterov 	/* mt-exec, de_thread() is waiting for group leader */
7619c339168SOleg Nesterov 	if (unlikely(tsk->signal->notify_count < 0))
76260700e38SEric W. Biederman 		wake_up_process(tsk->signal->group_exec_task);
7631da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
7641da177e4SLinus Torvalds 
765482a3767SOleg Nesterov 	list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
766482a3767SOleg Nesterov 		list_del_init(&p->ptrace_entry);
767482a3767SOleg Nesterov 		release_task(p);
768482a3767SOleg Nesterov 	}
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
771e18eecb8SJeff Dike #ifdef CONFIG_DEBUG_STACK_USAGE
check_stack_usage(void)772e18eecb8SJeff Dike static void check_stack_usage(void)
773e18eecb8SJeff Dike {
774e18eecb8SJeff Dike 	static DEFINE_SPINLOCK(low_water_lock);
775e18eecb8SJeff Dike 	static int lowest_to_date = THREAD_SIZE;
776e18eecb8SJeff Dike 	unsigned long free;
777e18eecb8SJeff Dike 
7787c9f8861SEric Sandeen 	free = stack_not_used(current);
779e18eecb8SJeff Dike 
780e18eecb8SJeff Dike 	if (free >= lowest_to_date)
781e18eecb8SJeff Dike 		return;
782e18eecb8SJeff Dike 
783e18eecb8SJeff Dike 	spin_lock(&low_water_lock);
784e18eecb8SJeff Dike 	if (free < lowest_to_date) {
785627393d4SAnton Blanchard 		pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
786168eeccbSTim Bird 			current->comm, task_pid_nr(current), free);
787e18eecb8SJeff Dike 		lowest_to_date = free;
788e18eecb8SJeff Dike 	}
789e18eecb8SJeff Dike 	spin_unlock(&low_water_lock);
790e18eecb8SJeff Dike }
791e18eecb8SJeff Dike #else
check_stack_usage(void)792e18eecb8SJeff Dike static inline void check_stack_usage(void) {}
793e18eecb8SJeff Dike #endif
794e18eecb8SJeff Dike 
synchronize_group_exit(struct task_struct * tsk,long code)795d80f7d7bSEric W. Biederman static void synchronize_group_exit(struct task_struct *tsk, long code)
796d80f7d7bSEric W. Biederman {
797d80f7d7bSEric W. Biederman 	struct sighand_struct *sighand = tsk->sighand;
798d80f7d7bSEric W. Biederman 	struct signal_struct *signal = tsk->signal;
799d80f7d7bSEric W. Biederman 
800d80f7d7bSEric W. Biederman 	spin_lock_irq(&sighand->siglock);
801d80f7d7bSEric W. Biederman 	signal->quick_threads--;
802d80f7d7bSEric W. Biederman 	if ((signal->quick_threads == 0) &&
803d80f7d7bSEric W. Biederman 	    !(signal->flags & SIGNAL_GROUP_EXIT)) {
804d80f7d7bSEric W. Biederman 		signal->flags = SIGNAL_GROUP_EXIT;
805d80f7d7bSEric W. Biederman 		signal->group_exit_code = code;
806d80f7d7bSEric W. Biederman 		signal->group_stop_count = 0;
807d80f7d7bSEric W. Biederman 	}
808d80f7d7bSEric W. Biederman 	spin_unlock_irq(&sighand->siglock);
809d80f7d7bSEric W. Biederman }
810d80f7d7bSEric W. Biederman 
do_exit(long code)8119af6528eSPeter Zijlstra void __noreturn do_exit(long code)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct task_struct *tsk = current;
8141da177e4SLinus Torvalds 	int group_dead;
8151da177e4SLinus Torvalds 
816001c28e5SNicholas Piggin 	WARN_ON(irqs_disabled());
817001c28e5SNicholas Piggin 
818d80f7d7bSEric W. Biederman 	synchronize_group_exit(tsk, code);
819d80f7d7bSEric W. Biederman 
820b1f866b0SChristoph Hellwig 	WARN_ON(tsk->plug);
82122e2c507SJens Axboe 
822586b58caSJann Horn 	kcov_task_exit(tsk);
82350b5e49cSAlexander Potapenko 	kmsan_task_exit(tsk);
824586b58caSJann Horn 
82592307383SEric W. Biederman 	coredump_task_exit(tsk);
826a288eeccSTejun Heo 	ptrace_event(PTRACE_EVENT_EXIT, code);
827fd593511SBeau Belgrave 	user_events_exit(tsk);
8281da177e4SLinus Torvalds 
829f552a27aSHao Xu 	io_uring_files_cancel();
830d12619b5SOleg Nesterov 	exit_signals(tsk);  /* sets PF_EXITING */
8311da177e4SLinus Torvalds 
83248d212a2SLinus Torvalds 	/* sync mm's RSS info before statistics gathering */
83348d212a2SLinus Torvalds 	if (tsk->mm)
83448d212a2SLinus Torvalds 		sync_mm_rss(tsk->mm);
83551229b49SRik van Riel 	acct_update_integrals(tsk);
8361da177e4SLinus Torvalds 	group_dead = atomic_dec_and_test(&tsk->signal->live);
837c3068951SAndrew Morton 	if (group_dead) {
83843cf75d9Schenqiwu 		/*
83943cf75d9Schenqiwu 		 * If the last thread of global init has exited, panic
84043cf75d9Schenqiwu 		 * immediately to get a useable coredump.
84143cf75d9Schenqiwu 		 */
84243cf75d9Schenqiwu 		if (unlikely(is_global_init(tsk)))
84343cf75d9Schenqiwu 			panic("Attempted to kill init! exitcode=0x%08x\n",
84443cf75d9Schenqiwu 				tsk->signal->group_exit_code ?: (int)code);
84543cf75d9Schenqiwu 
846baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
8472ff678b8SThomas Gleixner 		hrtimer_cancel(&tsk->signal->real_timer);
848d5b36a4dSOleg Nesterov 		exit_itimers(tsk);
849baa73d9eSNicolas Pitre #endif
8501f10206cSJiri Pirko 		if (tsk->mm)
8511f10206cSJiri Pirko 			setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
852c3068951SAndrew Morton 	}
853f6ec29a4SKaiGai Kohei 	acct_collect(code, group_dead);
854522ed776SMiloslav Trmac 	if (group_dead)
855522ed776SMiloslav Trmac 		tty_audit_exit();
856fa84cb93SAl Viro 	audit_free(tsk);
857115085eaSOleg Nesterov 
85848d212a2SLinus Torvalds 	tsk->exit_code = code;
859115085eaSOleg Nesterov 	taskstats_exit(tsk, group_dead);
860c757249aSShailabh Nagar 
8610039962aSDavidlohr Bueso 	exit_mm();
8621da177e4SLinus Torvalds 
8630e464814SKaiGai Kohei 	if (group_dead)
864f6ec29a4SKaiGai Kohei 		acct_process();
8650a16b607SMathieu Desnoyers 	trace_sched_process_exit(tsk);
8660a16b607SMathieu Desnoyers 
8671da177e4SLinus Torvalds 	exit_sem(tsk);
868b34a6b1dSVasiliy Kulikov 	exit_shm(tsk);
8691ec7f1ddSAl Viro 	exit_files(tsk);
8701ec7f1ddSAl Viro 	exit_fs(tsk);
871c39df5faSOleg Nesterov 	if (group_dead)
872c39df5faSOleg Nesterov 		disassociate_ctty(1);
8738aac6270SOleg Nesterov 	exit_task_namespaces(tsk);
874ed3e694dSAl Viro 	exit_task_work(tsk);
875e6464694SJiri Slaby 	exit_thread(tsk);
8760b3fcf17SStephane Eranian 
8770b3fcf17SStephane Eranian 	/*
8780b3fcf17SStephane Eranian 	 * Flush inherited counters to the parent - before the parent
8790b3fcf17SStephane Eranian 	 * gets woken up by child-exit notifications.
8800b3fcf17SStephane Eranian 	 *
8810b3fcf17SStephane Eranian 	 * because of cgroup mode, must be called before cgroup_exit()
8820b3fcf17SStephane Eranian 	 */
8830b3fcf17SStephane Eranian 	perf_event_exit_task(tsk);
8840b3fcf17SStephane Eranian 
8858e5bfa8cSOleg Nesterov 	sched_autogroup_exit_task(tsk);
8861ec41830SLi Zefan 	cgroup_exit(tsk);
8871da177e4SLinus Torvalds 
88833b2fb30SIngo Molnar 	/*
88924f1e32cSFrederic Weisbecker 	 * FIXME: do that only when needed, using sched_exit tracepoint
89024f1e32cSFrederic Weisbecker 	 */
8917c8df286SOleg Nesterov 	flush_ptrace_hw_breakpoint(tsk);
89233b2fb30SIngo Molnar 
893ccdd29ffSPaul E. McKenney 	exit_tasks_rcu_start();
894821c7de7SOleg Nesterov 	exit_notify(tsk, group_dead);
895ef982393SGuillaume Morin 	proc_exit_connector(tsk);
896c11600e4SDavid Rientjes 	mpol_put_task_policy(tsk);
89742b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
898c87e2837SIngo Molnar 	if (unlikely(current->pi_state_cache))
899c87e2837SIngo Molnar 		kfree(current->pi_state_cache);
90042b2dd0aSAlexey Dobriyan #endif
901c87e2837SIngo Molnar 	/*
9029a11b49aSIngo Molnar 	 * Make sure we are holding no locks:
903de5097c2SIngo Molnar 	 */
9041b1d2fb4SColin Cross 	debug_check_no_locks_held();
9051da177e4SLinus Torvalds 
906afc847b7SAl Viro 	if (tsk->io_context)
907b69f2292SLouis Rilling 		exit_io_context(tsk);
908afc847b7SAl Viro 
909b92ce558SJens Axboe 	if (tsk->splice_pipe)
9104b8a8f1eSAl Viro 		free_pipe_info(tsk->splice_pipe);
911b92ce558SJens Axboe 
9125640f768SEric Dumazet 	if (tsk->task_frag.page)
9135640f768SEric Dumazet 		put_page(tsk->task_frag.page);
9145640f768SEric Dumazet 
9151a03d3f1SSebastian Andrzej Siewior 	exit_task_stack_account(tsk);
916e0e81739SDavid Howells 
9174bcb8232SOleg Nesterov 	check_stack_usage();
9187407251aSCoywolf Qi Hunt 	preempt_disable();
91954848d73SWu Fengguang 	if (tsk->nr_dirtied)
92054848d73SWu Fengguang 		__this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
921f41d911fSPaul E. McKenney 	exit_rcu();
922ccdd29ffSPaul E. McKenney 	exit_tasks_rcu_finish();
923b5740f4bSYasunori Goto 
924b09be676SByungchul Park 	lockdep_free_task(tsk);
9259af6528eSPeter Zijlstra 	do_task_dead();
9261da177e4SLinus Torvalds }
927012914daSRuss Anderson 
make_task_dead(int signr)9280e25498fSEric W. Biederman void __noreturn make_task_dead(int signr)
9290e25498fSEric W. Biederman {
9300e25498fSEric W. Biederman 	/*
9310e25498fSEric W. Biederman 	 * Take the task off the cpu after something catastrophic has
9320e25498fSEric W. Biederman 	 * happened.
93305ea0424SEric W. Biederman 	 *
93405ea0424SEric W. Biederman 	 * We can get here from a kernel oops, sometimes with preemption off.
93505ea0424SEric W. Biederman 	 * Start by checking for critical errors.
93605ea0424SEric W. Biederman 	 * Then fix up important state like USER_DS and preemption.
93705ea0424SEric W. Biederman 	 * Then do everything else.
9380e25498fSEric W. Biederman 	 */
93905ea0424SEric W. Biederman 	struct task_struct *tsk = current;
9407535b832SKees Cook 	unsigned int limit;
94105ea0424SEric W. Biederman 
94205ea0424SEric W. Biederman 	if (unlikely(in_interrupt()))
94305ea0424SEric W. Biederman 		panic("Aiee, killing interrupt handler!");
94405ea0424SEric W. Biederman 	if (unlikely(!tsk->pid))
94505ea0424SEric W. Biederman 		panic("Attempted to kill the idle task!");
94605ea0424SEric W. Biederman 
947001c28e5SNicholas Piggin 	if (unlikely(irqs_disabled())) {
948001c28e5SNicholas Piggin 		pr_info("note: %s[%d] exited with irqs disabled\n",
949001c28e5SNicholas Piggin 			current->comm, task_pid_nr(current));
950001c28e5SNicholas Piggin 		local_irq_enable();
951001c28e5SNicholas Piggin 	}
95205ea0424SEric W. Biederman 	if (unlikely(in_atomic())) {
95305ea0424SEric W. Biederman 		pr_info("note: %s[%d] exited with preempt_count %d\n",
95405ea0424SEric W. Biederman 			current->comm, task_pid_nr(current),
95505ea0424SEric W. Biederman 			preempt_count());
95605ea0424SEric W. Biederman 		preempt_count_set(PREEMPT_ENABLED);
95705ea0424SEric W. Biederman 	}
95805ea0424SEric W. Biederman 
95905ea0424SEric W. Biederman 	/*
960d4ccd54dSJann Horn 	 * Every time the system oopses, if the oops happens while a reference
961d4ccd54dSJann Horn 	 * to an object was held, the reference leaks.
962d4ccd54dSJann Horn 	 * If the oops doesn't also leak memory, repeated oopsing can cause
963d4ccd54dSJann Horn 	 * reference counters to wrap around (if they're not using refcount_t).
964d4ccd54dSJann Horn 	 * This means that repeated oopsing can make unexploitable-looking bugs
965d4ccd54dSJann Horn 	 * exploitable through repeated oopsing.
966d4ccd54dSJann Horn 	 * To make sure this can't happen, place an upper bound on how often the
967d4ccd54dSJann Horn 	 * kernel may oops without panic().
968d4ccd54dSJann Horn 	 */
9697535b832SKees Cook 	limit = READ_ONCE(oops_limit);
9707535b832SKees Cook 	if (atomic_inc_return(&oops_count) >= limit && limit)
9717535b832SKees Cook 		panic("Oopsed too often (kernel.oops_limit is %d)", limit);
972d4ccd54dSJann Horn 
973d4ccd54dSJann Horn 	/*
97405ea0424SEric W. Biederman 	 * We're taking recursive faults here in make_task_dead. Safest is to just
97505ea0424SEric W. Biederman 	 * leave this task alone and wait for reboot.
97605ea0424SEric W. Biederman 	 */
97705ea0424SEric W. Biederman 	if (unlikely(tsk->flags & PF_EXITING)) {
97805ea0424SEric W. Biederman 		pr_alert("Fixing recursive fault but reboot is needed!\n");
97905ea0424SEric W. Biederman 		futex_exit_recursive(tsk);
980912616f1SEric W. Biederman 		tsk->exit_state = EXIT_DEAD;
981912616f1SEric W. Biederman 		refcount_inc(&tsk->rcu_users);
9827f80a2fdSEric W. Biederman 		do_task_dead();
98305ea0424SEric W. Biederman 	}
98405ea0424SEric W. Biederman 
9850e25498fSEric W. Biederman 	do_exit(signr);
9860e25498fSEric W. Biederman }
9870e25498fSEric W. Biederman 
SYSCALL_DEFINE1(exit,int,error_code)988754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit, int, error_code)
9891da177e4SLinus Torvalds {
9901da177e4SLinus Torvalds 	do_exit((error_code&0xff)<<8);
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds /*
9941da177e4SLinus Torvalds  * Take down every thread in the group.  This is called by fatal signals
9951da177e4SLinus Torvalds  * as well as by sys_exit_group (below).
9961da177e4SLinus Torvalds  */
997eae654f1SPeter Zijlstra void __noreturn
do_group_exit(int exit_code)9981da177e4SLinus Torvalds do_group_exit(int exit_code)
9991da177e4SLinus Torvalds {
1000bfc4b089SOleg Nesterov 	struct signal_struct *sig = current->signal;
1001bfc4b089SOleg Nesterov 
100249697335SEric W. Biederman 	if (sig->flags & SIGNAL_GROUP_EXIT)
1003bfc4b089SOleg Nesterov 		exit_code = sig->group_exit_code;
100449697335SEric W. Biederman 	else if (sig->group_exec_task)
100549697335SEric W. Biederman 		exit_code = 0;
1006cbe9dac3SEric W. Biederman 	else {
10071da177e4SLinus Torvalds 		struct sighand_struct *const sighand = current->sighand;
1008a0be55deSIonut Alexa 
10091da177e4SLinus Torvalds 		spin_lock_irq(&sighand->siglock);
101049697335SEric W. Biederman 		if (sig->flags & SIGNAL_GROUP_EXIT)
10111da177e4SLinus Torvalds 			/* Another thread got here before we took the lock.  */
10121da177e4SLinus Torvalds 			exit_code = sig->group_exit_code;
101349697335SEric W. Biederman 		else if (sig->group_exec_task)
101449697335SEric W. Biederman 			exit_code = 0;
10151da177e4SLinus Torvalds 		else {
10161da177e4SLinus Torvalds 			sig->group_exit_code = exit_code;
1017ed5d2cacSOleg Nesterov 			sig->flags = SIGNAL_GROUP_EXIT;
10181da177e4SLinus Torvalds 			zap_other_threads(current);
10191da177e4SLinus Torvalds 		}
10201da177e4SLinus Torvalds 		spin_unlock_irq(&sighand->siglock);
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	do_exit(exit_code);
10241da177e4SLinus Torvalds 	/* NOTREACHED */
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds /*
10281da177e4SLinus Torvalds  * this kills every thread in the thread group. Note that any externally
10291da177e4SLinus Torvalds  * wait4()-ing process will get the correct exit code - even if this
10301da177e4SLinus Torvalds  * thread is not the thread group leader.
10311da177e4SLinus Torvalds  */
SYSCALL_DEFINE1(exit_group,int,error_code)1032754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit_group, int, error_code)
10331da177e4SLinus Torvalds {
10341da177e4SLinus Torvalds 	do_group_exit((error_code & 0xff) << 8);
10352ed7c03eSHeiko Carstens 	/* NOTREACHED */
10362ed7c03eSHeiko Carstens 	return 0;
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
103967d7dddeSAl Viro struct waitid_info {
104067d7dddeSAl Viro 	pid_t pid;
104167d7dddeSAl Viro 	uid_t uid;
104267d7dddeSAl Viro 	int status;
104367d7dddeSAl Viro 	int cause;
104467d7dddeSAl Viro };
104567d7dddeSAl Viro 
10469e8ae01dSOleg Nesterov struct wait_opts {
10479e8ae01dSOleg Nesterov 	enum pid_type		wo_type;
10489e8ae01dSOleg Nesterov 	int			wo_flags;
1049e1eb1ebcSRichard Kennedy 	struct pid		*wo_pid;
10509e8ae01dSOleg Nesterov 
105167d7dddeSAl Viro 	struct waitid_info	*wo_info;
1052359566faSAl Viro 	int			wo_stat;
1053ce72a16fSAl Viro 	struct rusage		*wo_rusage;
10549e8ae01dSOleg Nesterov 
1055ac6424b9SIngo Molnar 	wait_queue_entry_t		child_wait;
10569e8ae01dSOleg Nesterov 	int			notask_error;
10579e8ae01dSOleg Nesterov };
10589e8ae01dSOleg Nesterov 
eligible_pid(struct wait_opts * wo,struct task_struct * p)1059989264f4SOleg Nesterov static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
10601da177e4SLinus Torvalds {
10615c01ba49SOleg Nesterov 	return	wo->wo_type == PIDTYPE_MAX ||
10625c01ba49SOleg Nesterov 		task_pid_type(p, wo->wo_type) == wo->wo_pid;
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds 
1065bf959931SOleg Nesterov static int
eligible_child(struct wait_opts * wo,bool ptrace,struct task_struct * p)1066bf959931SOleg Nesterov eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
10675c01ba49SOleg Nesterov {
10685c01ba49SOleg Nesterov 	if (!eligible_pid(wo, p))
10695c01ba49SOleg Nesterov 		return 0;
1070bf959931SOleg Nesterov 
1071bf959931SOleg Nesterov 	/*
1072bf959931SOleg Nesterov 	 * Wait for all children (clone and not) if __WALL is set or
1073bf959931SOleg Nesterov 	 * if it is traced by us.
1074bf959931SOleg Nesterov 	 */
1075bf959931SOleg Nesterov 	if (ptrace || (wo->wo_flags & __WALL))
1076bf959931SOleg Nesterov 		return 1;
1077bf959931SOleg Nesterov 
1078bf959931SOleg Nesterov 	/*
1079bf959931SOleg Nesterov 	 * Otherwise, wait for clone children *only* if __WCLONE is set;
1080bf959931SOleg Nesterov 	 * otherwise, wait for non-clone children *only*.
1081bf959931SOleg Nesterov 	 *
1082bf959931SOleg Nesterov 	 * Note: a "clone" child here is one that reports to its parent
1083bf959931SOleg Nesterov 	 * using a signal other than SIGCHLD, or a non-leader thread which
1084bf959931SOleg Nesterov 	 * we can only see if it is traced by us.
1085bf959931SOleg Nesterov 	 */
1086bf959931SOleg Nesterov 	if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
10871da177e4SLinus Torvalds 		return 0;
10881da177e4SLinus Torvalds 
108914dd0b81SRoland McGrath 	return 1;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds /*
10931da177e4SLinus Torvalds  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
10941da177e4SLinus Torvalds  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
10951da177e4SLinus Torvalds  * the lock and this task is uninteresting.  If we return nonzero, we have
10961da177e4SLinus Torvalds  * released the lock and the system call should return.
10971da177e4SLinus Torvalds  */
wait_task_zombie(struct wait_opts * wo,struct task_struct * p)10989e8ae01dSOleg Nesterov static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
10991da177e4SLinus Torvalds {
110067d7dddeSAl Viro 	int state, status;
11016c5f3e7bSPavel Emelyanov 	pid_t pid = task_pid_vnr(p);
110243e13cc1SOleg Nesterov 	uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
110367d7dddeSAl Viro 	struct waitid_info *infop;
11041da177e4SLinus Torvalds 
11059e8ae01dSOleg Nesterov 	if (!likely(wo->wo_flags & WEXITED))
110698abed02SRoland McGrath 		return 0;
110798abed02SRoland McGrath 
11089e8ae01dSOleg Nesterov 	if (unlikely(wo->wo_flags & WNOWAIT)) {
1109907c311fSEric W. Biederman 		status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1110907c311fSEric W. Biederman 			? p->signal->group_exit_code : p->exit_code;
11111da177e4SLinus Torvalds 		get_task_struct(p);
11121da177e4SLinus Torvalds 		read_unlock(&tasklist_lock);
11131029a2b5SPeter Zijlstra 		sched_annotate_sleep();
1114e61a2502SAl Viro 		if (wo->wo_rusage)
1115e61a2502SAl Viro 			getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1116bb380ec3SAl Viro 		put_task_struct(p);
111776d9871eSAl Viro 		goto out_info;
11181da177e4SLinus Torvalds 	}
1119befca967SOleg Nesterov 	/*
1120abd50b39SOleg Nesterov 	 * Move the task's state to DEAD/TRACE, only one thread can do this.
1121abd50b39SOleg Nesterov 	 */
1122f6507f83SOleg Nesterov 	state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1123f6507f83SOleg Nesterov 		EXIT_TRACE : EXIT_DEAD;
1124abd50b39SOleg Nesterov 	if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1125abd50b39SOleg Nesterov 		return 0;
1126986094dfSOleg Nesterov 	/*
1127986094dfSOleg Nesterov 	 * We own this thread, nobody else can reap it.
1128986094dfSOleg Nesterov 	 */
1129986094dfSOleg Nesterov 	read_unlock(&tasklist_lock);
1130986094dfSOleg Nesterov 	sched_annotate_sleep();
1131f6507f83SOleg Nesterov 
1132abd50b39SOleg Nesterov 	/*
1133f6507f83SOleg Nesterov 	 * Check thread_group_leader() to exclude the traced sub-threads.
1134befca967SOleg Nesterov 	 */
1135f6507f83SOleg Nesterov 	if (state == EXIT_DEAD && thread_group_leader(p)) {
1136f953ccd0SOleg Nesterov 		struct signal_struct *sig = p->signal;
1137f953ccd0SOleg Nesterov 		struct signal_struct *psig = current->signal;
11381f10206cSJiri Pirko 		unsigned long maxrss;
11395613fda9SFrederic Weisbecker 		u64 tgutime, tgstime;
11403795e161SJesper Juhl 
11411da177e4SLinus Torvalds 		/*
11421da177e4SLinus Torvalds 		 * The resource counters for the group leader are in its
11431da177e4SLinus Torvalds 		 * own task_struct.  Those for dead threads in the group
11441da177e4SLinus Torvalds 		 * are in its signal_struct, as are those for the child
11451da177e4SLinus Torvalds 		 * processes it has previously reaped.  All these
11461da177e4SLinus Torvalds 		 * accumulate in the parent's signal_struct c* fields.
11471da177e4SLinus Torvalds 		 *
11481da177e4SLinus Torvalds 		 * We don't bother to take a lock here to protect these
1149f953ccd0SOleg Nesterov 		 * p->signal fields because the whole thread group is dead
1150f953ccd0SOleg Nesterov 		 * and nobody can change them.
1151f953ccd0SOleg Nesterov 		 *
1152dcca3475SIngo Molnar 		 * psig->stats_lock also protects us from our sub-threads
1153f953ccd0SOleg Nesterov 		 * which can reap other children at the same time. Until
1154f953ccd0SOleg Nesterov 		 * we change k_getrusage()-like users to rely on this lock
1155f953ccd0SOleg Nesterov 		 * we have to take ->siglock as well.
11560cf55e1eSHidetoshi Seto 		 *
1157a0be55deSIonut Alexa 		 * We use thread_group_cputime_adjusted() to get times for
1158a0be55deSIonut Alexa 		 * the thread group, which consolidates times for all threads
1159a0be55deSIonut Alexa 		 * in the group including the group leader.
11601da177e4SLinus Torvalds 		 */
1161e80d0a1aSFrederic Weisbecker 		thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1162f953ccd0SOleg Nesterov 		spin_lock_irq(&current->sighand->siglock);
1163e78c3496SRik van Riel 		write_seqlock(&psig->stats_lock);
116464861634SMartin Schwidefsky 		psig->cutime += tgutime + sig->cutime;
116564861634SMartin Schwidefsky 		psig->cstime += tgstime + sig->cstime;
11666fac4829SFrederic Weisbecker 		psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
11673795e161SJesper Juhl 		psig->cmin_flt +=
11683795e161SJesper Juhl 			p->min_flt + sig->min_flt + sig->cmin_flt;
11693795e161SJesper Juhl 		psig->cmaj_flt +=
11703795e161SJesper Juhl 			p->maj_flt + sig->maj_flt + sig->cmaj_flt;
11713795e161SJesper Juhl 		psig->cnvcsw +=
11723795e161SJesper Juhl 			p->nvcsw + sig->nvcsw + sig->cnvcsw;
11733795e161SJesper Juhl 		psig->cnivcsw +=
11743795e161SJesper Juhl 			p->nivcsw + sig->nivcsw + sig->cnivcsw;
11756eaeeabaSEric Dumazet 		psig->cinblock +=
11766eaeeabaSEric Dumazet 			task_io_get_inblock(p) +
11776eaeeabaSEric Dumazet 			sig->inblock + sig->cinblock;
11786eaeeabaSEric Dumazet 		psig->coublock +=
11796eaeeabaSEric Dumazet 			task_io_get_oublock(p) +
11806eaeeabaSEric Dumazet 			sig->oublock + sig->coublock;
11811f10206cSJiri Pirko 		maxrss = max(sig->maxrss, sig->cmaxrss);
11821f10206cSJiri Pirko 		if (psig->cmaxrss < maxrss)
11831f10206cSJiri Pirko 			psig->cmaxrss = maxrss;
11845995477aSAndrea Righi 		task_io_accounting_add(&psig->ioac, &p->ioac);
11855995477aSAndrea Righi 		task_io_accounting_add(&psig->ioac, &sig->ioac);
1186e78c3496SRik van Riel 		write_sequnlock(&psig->stats_lock);
1187f953ccd0SOleg Nesterov 		spin_unlock_irq(&current->sighand->siglock);
11881da177e4SLinus Torvalds 	}
11891da177e4SLinus Torvalds 
1190ce72a16fSAl Viro 	if (wo->wo_rusage)
1191ce72a16fSAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
11921da177e4SLinus Torvalds 	status = (p->signal->flags & SIGNAL_GROUP_EXIT)
11931da177e4SLinus Torvalds 		? p->signal->group_exit_code : p->exit_code;
1194359566faSAl Viro 	wo->wo_stat = status;
11952f4e6e2aSOleg Nesterov 
1196b4360690SOleg Nesterov 	if (state == EXIT_TRACE) {
11971da177e4SLinus Torvalds 		write_lock_irq(&tasklist_lock);
11982f4e6e2aSOleg Nesterov 		/* We dropped tasklist, ptracer could die and untrace */
11992f4e6e2aSOleg Nesterov 		ptrace_unlink(p);
1200b4360690SOleg Nesterov 
1201b4360690SOleg Nesterov 		/* If parent wants a zombie, don't release it now */
1202abd50b39SOleg Nesterov 		state = EXIT_ZOMBIE;
1203b4360690SOleg Nesterov 		if (do_notify_parent(p, p->exit_signal))
1204b4360690SOleg Nesterov 			state = EXIT_DEAD;
1205abd50b39SOleg Nesterov 		p->exit_state = state;
12061da177e4SLinus Torvalds 		write_unlock_irq(&tasklist_lock);
12071da177e4SLinus Torvalds 	}
1208abd50b39SOleg Nesterov 	if (state == EXIT_DEAD)
12091da177e4SLinus Torvalds 		release_task(p);
12102f4e6e2aSOleg Nesterov 
121176d9871eSAl Viro out_info:
121276d9871eSAl Viro 	infop = wo->wo_info;
121376d9871eSAl Viro 	if (infop) {
121476d9871eSAl Viro 		if ((status & 0x7f) == 0) {
121576d9871eSAl Viro 			infop->cause = CLD_EXITED;
121676d9871eSAl Viro 			infop->status = status >> 8;
121776d9871eSAl Viro 		} else {
121876d9871eSAl Viro 			infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
121976d9871eSAl Viro 			infop->status = status & 0x7f;
122076d9871eSAl Viro 		}
122176d9871eSAl Viro 		infop->pid = pid;
122276d9871eSAl Viro 		infop->uid = uid;
122376d9871eSAl Viro 	}
122476d9871eSAl Viro 
122567d7dddeSAl Viro 	return pid;
12261da177e4SLinus Torvalds }
12271da177e4SLinus Torvalds 
task_stopped_code(struct task_struct * p,bool ptrace)122890bc8d8bSOleg Nesterov static int *task_stopped_code(struct task_struct *p, bool ptrace)
122990bc8d8bSOleg Nesterov {
123090bc8d8bSOleg Nesterov 	if (ptrace) {
1231570ac933SOleg Nesterov 		if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
123290bc8d8bSOleg Nesterov 			return &p->exit_code;
123390bc8d8bSOleg Nesterov 	} else {
123490bc8d8bSOleg Nesterov 		if (p->signal->flags & SIGNAL_STOP_STOPPED)
123590bc8d8bSOleg Nesterov 			return &p->signal->group_exit_code;
123690bc8d8bSOleg Nesterov 	}
123790bc8d8bSOleg Nesterov 	return NULL;
123890bc8d8bSOleg Nesterov }
123990bc8d8bSOleg Nesterov 
124019e27463STejun Heo /**
124119e27463STejun Heo  * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
124219e27463STejun Heo  * @wo: wait options
124319e27463STejun Heo  * @ptrace: is the wait for ptrace
124419e27463STejun Heo  * @p: task to wait for
124519e27463STejun Heo  *
124619e27463STejun Heo  * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
124719e27463STejun Heo  *
124819e27463STejun Heo  * CONTEXT:
124919e27463STejun Heo  * read_lock(&tasklist_lock), which is released if return value is
125019e27463STejun Heo  * non-zero.  Also, grabs and releases @p->sighand->siglock.
125119e27463STejun Heo  *
125219e27463STejun Heo  * RETURNS:
125319e27463STejun Heo  * 0 if wait condition didn't exist and search for other wait conditions
125419e27463STejun Heo  * should continue.  Non-zero return, -errno on failure and @p's pid on
125519e27463STejun Heo  * success, implies that tasklist_lock is released and wait condition
125619e27463STejun Heo  * search should terminate.
12571da177e4SLinus Torvalds  */
wait_task_stopped(struct wait_opts * wo,int ptrace,struct task_struct * p)12589e8ae01dSOleg Nesterov static int wait_task_stopped(struct wait_opts *wo,
12599e8ae01dSOleg Nesterov 				int ptrace, struct task_struct *p)
12601da177e4SLinus Torvalds {
126167d7dddeSAl Viro 	struct waitid_info *infop;
126267d7dddeSAl Viro 	int exit_code, *p_code, why;
1263ee7c82daSOleg Nesterov 	uid_t uid = 0; /* unneeded, required by compiler */
1264c8950783SOleg Nesterov 	pid_t pid;
12651da177e4SLinus Torvalds 
126647918025SOleg Nesterov 	/*
126747918025SOleg Nesterov 	 * Traditionally we see ptrace'd stopped tasks regardless of options.
126847918025SOleg Nesterov 	 */
12699e8ae01dSOleg Nesterov 	if (!ptrace && !(wo->wo_flags & WUNTRACED))
127098abed02SRoland McGrath 		return 0;
127198abed02SRoland McGrath 
127219e27463STejun Heo 	if (!task_stopped_code(p, ptrace))
127319e27463STejun Heo 		return 0;
127419e27463STejun Heo 
1275ee7c82daSOleg Nesterov 	exit_code = 0;
1276ee7c82daSOleg Nesterov 	spin_lock_irq(&p->sighand->siglock);
1277ee7c82daSOleg Nesterov 
127890bc8d8bSOleg Nesterov 	p_code = task_stopped_code(p, ptrace);
127990bc8d8bSOleg Nesterov 	if (unlikely(!p_code))
1280ee7c82daSOleg Nesterov 		goto unlock_sig;
1281ee7c82daSOleg Nesterov 
128290bc8d8bSOleg Nesterov 	exit_code = *p_code;
1283ee7c82daSOleg Nesterov 	if (!exit_code)
1284ee7c82daSOleg Nesterov 		goto unlock_sig;
1285ee7c82daSOleg Nesterov 
12869e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WNOWAIT))
128790bc8d8bSOleg Nesterov 		*p_code = 0;
1288ee7c82daSOleg Nesterov 
12898ca937a6SSasha Levin 	uid = from_kuid_munged(current_user_ns(), task_uid(p));
1290ee7c82daSOleg Nesterov unlock_sig:
1291ee7c82daSOleg Nesterov 	spin_unlock_irq(&p->sighand->siglock);
1292ee7c82daSOleg Nesterov 	if (!exit_code)
12931da177e4SLinus Torvalds 		return 0;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	/*
12961da177e4SLinus Torvalds 	 * Now we are pretty sure this task is interesting.
12971da177e4SLinus Torvalds 	 * Make sure it doesn't get reaped out from under us while we
12981da177e4SLinus Torvalds 	 * give up the lock and then examine it below.  We don't want to
12991da177e4SLinus Torvalds 	 * keep holding onto the tasklist_lock while we call getrusage and
13001da177e4SLinus Torvalds 	 * possibly take page faults for user memory.
13011da177e4SLinus Torvalds 	 */
13021da177e4SLinus Torvalds 	get_task_struct(p);
13036c5f3e7bSPavel Emelyanov 	pid = task_pid_vnr(p);
1304f470021aSRoland McGrath 	why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
13051da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
13061029a2b5SPeter Zijlstra 	sched_annotate_sleep();
1307e61a2502SAl Viro 	if (wo->wo_rusage)
1308e61a2502SAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
13091da177e4SLinus Torvalds 	put_task_struct(p);
13101da177e4SLinus Torvalds 
1311bb380ec3SAl Viro 	if (likely(!(wo->wo_flags & WNOWAIT)))
1312359566faSAl Viro 		wo->wo_stat = (exit_code << 8) | 0x7f;
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds 	infop = wo->wo_info;
131567d7dddeSAl Viro 	if (infop) {
131667d7dddeSAl Viro 		infop->cause = why;
131767d7dddeSAl Viro 		infop->status = exit_code;
131867d7dddeSAl Viro 		infop->pid = pid;
131967d7dddeSAl Viro 		infop->uid = uid;
132067d7dddeSAl Viro 	}
132167d7dddeSAl Viro 	return pid;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds /*
13251da177e4SLinus Torvalds  * Handle do_wait work for one task in a live, non-stopped state.
13261da177e4SLinus Torvalds  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
13271da177e4SLinus Torvalds  * the lock and this task is uninteresting.  If we return nonzero, we have
13281da177e4SLinus Torvalds  * released the lock and the system call should return.
13291da177e4SLinus Torvalds  */
wait_task_continued(struct wait_opts * wo,struct task_struct * p)13309e8ae01dSOleg Nesterov static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
13311da177e4SLinus Torvalds {
1332bb380ec3SAl Viro 	struct waitid_info *infop;
13331da177e4SLinus Torvalds 	pid_t pid;
13341da177e4SLinus Torvalds 	uid_t uid;
13351da177e4SLinus Torvalds 
13369e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WCONTINUED))
133798abed02SRoland McGrath 		return 0;
133898abed02SRoland McGrath 
13391da177e4SLinus Torvalds 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
13401da177e4SLinus Torvalds 		return 0;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	spin_lock_irq(&p->sighand->siglock);
13431da177e4SLinus Torvalds 	/* Re-check with the lock held.  */
13441da177e4SLinus Torvalds 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
13451da177e4SLinus Torvalds 		spin_unlock_irq(&p->sighand->siglock);
13461da177e4SLinus Torvalds 		return 0;
13471da177e4SLinus Torvalds 	}
13489e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WNOWAIT))
13491da177e4SLinus Torvalds 		p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
13508ca937a6SSasha Levin 	uid = from_kuid_munged(current_user_ns(), task_uid(p));
13511da177e4SLinus Torvalds 	spin_unlock_irq(&p->sighand->siglock);
13521da177e4SLinus Torvalds 
13536c5f3e7bSPavel Emelyanov 	pid = task_pid_vnr(p);
13541da177e4SLinus Torvalds 	get_task_struct(p);
13551da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
13561029a2b5SPeter Zijlstra 	sched_annotate_sleep();
1357ce72a16fSAl Viro 	if (wo->wo_rusage)
1358ce72a16fSAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
13591da177e4SLinus Torvalds 	put_task_struct(p);
13601da177e4SLinus Torvalds 
1361bb380ec3SAl Viro 	infop = wo->wo_info;
1362bb380ec3SAl Viro 	if (!infop) {
1363bb380ec3SAl Viro 		wo->wo_stat = 0xffff;
1364bb380ec3SAl Viro 	} else {
1365bb380ec3SAl Viro 		infop->cause = CLD_CONTINUED;
1366bb380ec3SAl Viro 		infop->pid = pid;
1367bb380ec3SAl Viro 		infop->uid = uid;
1368bb380ec3SAl Viro 		infop->status = SIGCONT;
1369bb380ec3SAl Viro 	}
1370bb380ec3SAl Viro 	return pid;
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
137398abed02SRoland McGrath /*
137498abed02SRoland McGrath  * Consider @p for a wait by @parent.
137598abed02SRoland McGrath  *
13769e8ae01dSOleg Nesterov  * -ECHILD should be in ->notask_error before the first call.
137798abed02SRoland McGrath  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
137898abed02SRoland McGrath  * Returns zero if the search for a child should continue;
13799e8ae01dSOleg Nesterov  * then ->notask_error is 0 if @p is an eligible child,
13803a2f5a59SStephen Smalley  * or still -ECHILD.
138198abed02SRoland McGrath  */
wait_consider_task(struct wait_opts * wo,int ptrace,struct task_struct * p)1382b6e763f0SOleg Nesterov static int wait_consider_task(struct wait_opts *wo, int ptrace,
1383b6e763f0SOleg Nesterov 				struct task_struct *p)
138498abed02SRoland McGrath {
13853245d6acSOleg Nesterov 	/*
13863245d6acSOleg Nesterov 	 * We can race with wait_task_zombie() from another thread.
13873245d6acSOleg Nesterov 	 * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
13883245d6acSOleg Nesterov 	 * can't confuse the checks below.
13893245d6acSOleg Nesterov 	 */
13906aa7de05SMark Rutland 	int exit_state = READ_ONCE(p->exit_state);
1391b3ab0316SOleg Nesterov 	int ret;
1392b3ab0316SOleg Nesterov 
13933245d6acSOleg Nesterov 	if (unlikely(exit_state == EXIT_DEAD))
1394b3ab0316SOleg Nesterov 		return 0;
1395b3ab0316SOleg Nesterov 
1396bf959931SOleg Nesterov 	ret = eligible_child(wo, ptrace, p);
139714dd0b81SRoland McGrath 	if (!ret)
139898abed02SRoland McGrath 		return ret;
139998abed02SRoland McGrath 
14003245d6acSOleg Nesterov 	if (unlikely(exit_state == EXIT_TRACE)) {
140150b8d257SOleg Nesterov 		/*
1402abd50b39SOleg Nesterov 		 * ptrace == 0 means we are the natural parent. In this case
1403abd50b39SOleg Nesterov 		 * we should clear notask_error, debugger will notify us.
140450b8d257SOleg Nesterov 		 */
1405abd50b39SOleg Nesterov 		if (likely(!ptrace))
140650b8d257SOleg Nesterov 			wo->notask_error = 0;
1407823b018eSTejun Heo 		return 0;
140850b8d257SOleg Nesterov 	}
1409823b018eSTejun Heo 
1410377d75daSOleg Nesterov 	if (likely(!ptrace) && unlikely(p->ptrace)) {
1411377d75daSOleg Nesterov 		/*
1412377d75daSOleg Nesterov 		 * If it is traced by its real parent's group, just pretend
1413377d75daSOleg Nesterov 		 * the caller is ptrace_do_wait() and reap this child if it
1414377d75daSOleg Nesterov 		 * is zombie.
1415377d75daSOleg Nesterov 		 *
1416377d75daSOleg Nesterov 		 * This also hides group stop state from real parent; otherwise
1417377d75daSOleg Nesterov 		 * a single stop can be reported twice as group and ptrace stop.
1418377d75daSOleg Nesterov 		 * If a ptracer wants to distinguish these two events for its
1419377d75daSOleg Nesterov 		 * own children it should create a separate process which takes
1420377d75daSOleg Nesterov 		 * the role of real parent.
1421377d75daSOleg Nesterov 		 */
1422377d75daSOleg Nesterov 		if (!ptrace_reparented(p))
1423377d75daSOleg Nesterov 			ptrace = 1;
1424377d75daSOleg Nesterov 	}
1425377d75daSOleg Nesterov 
142645cb24a1STejun Heo 	/* slay zombie? */
14273245d6acSOleg Nesterov 	if (exit_state == EXIT_ZOMBIE) {
14287c733eb3SOleg Nesterov 		/* we don't reap group leaders with subthreads */
14297c733eb3SOleg Nesterov 		if (!delay_group_leader(p)) {
1430f470021aSRoland McGrath 			/*
143145cb24a1STejun Heo 			 * A zombie ptracee is only visible to its ptracer.
14327c733eb3SOleg Nesterov 			 * Notification and reaping will be cascaded to the
14337c733eb3SOleg Nesterov 			 * real parent when the ptracer detaches.
1434f470021aSRoland McGrath 			 */
14357c733eb3SOleg Nesterov 			if (unlikely(ptrace) || likely(!p->ptrace))
14369e8ae01dSOleg Nesterov 				return wait_task_zombie(wo, p);
14377c733eb3SOleg Nesterov 		}
143898abed02SRoland McGrath 
143998abed02SRoland McGrath 		/*
14409b84cca2STejun Heo 		 * Allow access to stopped/continued state via zombie by
14419b84cca2STejun Heo 		 * falling through.  Clearing of notask_error is complex.
14429b84cca2STejun Heo 		 *
14439b84cca2STejun Heo 		 * When !@ptrace:
14449b84cca2STejun Heo 		 *
14459b84cca2STejun Heo 		 * If WEXITED is set, notask_error should naturally be
14469b84cca2STejun Heo 		 * cleared.  If not, subset of WSTOPPED|WCONTINUED is set,
14479b84cca2STejun Heo 		 * so, if there are live subthreads, there are events to
14489b84cca2STejun Heo 		 * wait for.  If all subthreads are dead, it's still safe
14499b84cca2STejun Heo 		 * to clear - this function will be called again in finite
14509b84cca2STejun Heo 		 * amount time once all the subthreads are released and
14519b84cca2STejun Heo 		 * will then return without clearing.
14529b84cca2STejun Heo 		 *
14539b84cca2STejun Heo 		 * When @ptrace:
14549b84cca2STejun Heo 		 *
14559b84cca2STejun Heo 		 * Stopped state is per-task and thus can't change once the
14569b84cca2STejun Heo 		 * target task dies.  Only continued and exited can happen.
14579b84cca2STejun Heo 		 * Clear notask_error if WCONTINUED | WEXITED.
14589b84cca2STejun Heo 		 */
14599b84cca2STejun Heo 		if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
14609b84cca2STejun Heo 			wo->notask_error = 0;
14619b84cca2STejun Heo 	} else {
14629b84cca2STejun Heo 		/*
14639b84cca2STejun Heo 		 * @p is alive and it's gonna stop, continue or exit, so
14649b84cca2STejun Heo 		 * there always is something to wait for.
146598abed02SRoland McGrath 		 */
14669e8ae01dSOleg Nesterov 		wo->notask_error = 0;
14679b84cca2STejun Heo 	}
146898abed02SRoland McGrath 
146945cb24a1STejun Heo 	/*
147045cb24a1STejun Heo 	 * Wait for stopped.  Depending on @ptrace, different stopped state
147145cb24a1STejun Heo 	 * is used and the two don't interact with each other.
147245cb24a1STejun Heo 	 */
147319e27463STejun Heo 	ret = wait_task_stopped(wo, ptrace, p);
147419e27463STejun Heo 	if (ret)
147519e27463STejun Heo 		return ret;
147698abed02SRoland McGrath 
147745cb24a1STejun Heo 	/*
147845cb24a1STejun Heo 	 * Wait for continued.  There's only one continued state and the
147945cb24a1STejun Heo 	 * ptracer can consume it which can confuse the real parent.  Don't
148045cb24a1STejun Heo 	 * use WCONTINUED from ptracer.  You don't need or want it.
148145cb24a1STejun Heo 	 */
14829e8ae01dSOleg Nesterov 	return wait_task_continued(wo, p);
148398abed02SRoland McGrath }
148498abed02SRoland McGrath 
148598abed02SRoland McGrath /*
148698abed02SRoland McGrath  * Do the work of do_wait() for one thread in the group, @tsk.
148798abed02SRoland McGrath  *
14889e8ae01dSOleg Nesterov  * -ECHILD should be in ->notask_error before the first call.
148998abed02SRoland McGrath  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
149098abed02SRoland McGrath  * Returns zero if the search for a child should continue; then
14919e8ae01dSOleg Nesterov  * ->notask_error is 0 if there were any eligible children,
14923a2f5a59SStephen Smalley  * or still -ECHILD.
149398abed02SRoland McGrath  */
do_wait_thread(struct wait_opts * wo,struct task_struct * tsk)14949e8ae01dSOleg Nesterov static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
149598abed02SRoland McGrath {
149698abed02SRoland McGrath 	struct task_struct *p;
149798abed02SRoland McGrath 
149898abed02SRoland McGrath 	list_for_each_entry(p, &tsk->children, sibling) {
1499b6e763f0SOleg Nesterov 		int ret = wait_consider_task(wo, 0, p);
1500a0be55deSIonut Alexa 
150198abed02SRoland McGrath 		if (ret)
150298abed02SRoland McGrath 			return ret;
150398abed02SRoland McGrath 	}
150498abed02SRoland McGrath 
150598abed02SRoland McGrath 	return 0;
150698abed02SRoland McGrath }
150798abed02SRoland McGrath 
ptrace_do_wait(struct wait_opts * wo,struct task_struct * tsk)15089e8ae01dSOleg Nesterov static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
150998abed02SRoland McGrath {
151098abed02SRoland McGrath 	struct task_struct *p;
151198abed02SRoland McGrath 
1512f470021aSRoland McGrath 	list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1513b6e763f0SOleg Nesterov 		int ret = wait_consider_task(wo, 1, p);
1514a0be55deSIonut Alexa 
1515f470021aSRoland McGrath 		if (ret)
151698abed02SRoland McGrath 			return ret;
151798abed02SRoland McGrath 	}
151898abed02SRoland McGrath 
151998abed02SRoland McGrath 	return 0;
152098abed02SRoland McGrath }
152198abed02SRoland McGrath 
child_wait_callback(wait_queue_entry_t * wait,unsigned mode,int sync,void * key)1522ac6424b9SIngo Molnar static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
15230b7570e7SOleg Nesterov 				int sync, void *key)
15240b7570e7SOleg Nesterov {
15250b7570e7SOleg Nesterov 	struct wait_opts *wo = container_of(wait, struct wait_opts,
15260b7570e7SOleg Nesterov 						child_wait);
15270b7570e7SOleg Nesterov 	struct task_struct *p = key;
15280b7570e7SOleg Nesterov 
15295c01ba49SOleg Nesterov 	if (!eligible_pid(wo, p))
15300b7570e7SOleg Nesterov 		return 0;
15310b7570e7SOleg Nesterov 
1532b4fe5182SOleg Nesterov 	if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
1533b4fe5182SOleg Nesterov 		return 0;
1534b4fe5182SOleg Nesterov 
15350b7570e7SOleg Nesterov 	return default_wake_function(wait, mode, sync, key);
15360b7570e7SOleg Nesterov }
15370b7570e7SOleg Nesterov 
__wake_up_parent(struct task_struct * p,struct task_struct * parent)1538a7f0765eSOleg Nesterov void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1539a7f0765eSOleg Nesterov {
15400b7570e7SOleg Nesterov 	__wake_up_sync_key(&parent->signal->wait_chldexit,
1541ce4dd442SDavid Howells 			   TASK_INTERRUPTIBLE, p);
1542a7f0765eSOleg Nesterov }
1543a7f0765eSOleg Nesterov 
is_effectively_child(struct wait_opts * wo,bool ptrace,struct task_struct * target)15445449162aSJim Newsome static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
15455449162aSJim Newsome 				 struct task_struct *target)
15465449162aSJim Newsome {
15475449162aSJim Newsome 	struct task_struct *parent =
15485449162aSJim Newsome 		!ptrace ? target->real_parent : target->parent;
15495449162aSJim Newsome 
15505449162aSJim Newsome 	return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
15515449162aSJim Newsome 				     same_thread_group(current, parent));
15525449162aSJim Newsome }
15535449162aSJim Newsome 
15545449162aSJim Newsome /*
15555449162aSJim Newsome  * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
15565449162aSJim Newsome  * and tracee lists to find the target task.
15575449162aSJim Newsome  */
do_wait_pid(struct wait_opts * wo)15585449162aSJim Newsome static int do_wait_pid(struct wait_opts *wo)
15595449162aSJim Newsome {
15605449162aSJim Newsome 	bool ptrace;
15615449162aSJim Newsome 	struct task_struct *target;
15625449162aSJim Newsome 	int retval;
15635449162aSJim Newsome 
15645449162aSJim Newsome 	ptrace = false;
15655449162aSJim Newsome 	target = pid_task(wo->wo_pid, PIDTYPE_TGID);
15665449162aSJim Newsome 	if (target && is_effectively_child(wo, ptrace, target)) {
15675449162aSJim Newsome 		retval = wait_consider_task(wo, ptrace, target);
15685449162aSJim Newsome 		if (retval)
15695449162aSJim Newsome 			return retval;
15705449162aSJim Newsome 	}
15715449162aSJim Newsome 
15725449162aSJim Newsome 	ptrace = true;
15735449162aSJim Newsome 	target = pid_task(wo->wo_pid, PIDTYPE_PID);
15745449162aSJim Newsome 	if (target && target->ptrace &&
15755449162aSJim Newsome 	    is_effectively_child(wo, ptrace, target)) {
15765449162aSJim Newsome 		retval = wait_consider_task(wo, ptrace, target);
15775449162aSJim Newsome 		if (retval)
15785449162aSJim Newsome 			return retval;
15795449162aSJim Newsome 	}
15805449162aSJim Newsome 
15815449162aSJim Newsome 	return 0;
15825449162aSJim Newsome }
15835449162aSJim Newsome 
do_wait(struct wait_opts * wo)15849e8ae01dSOleg Nesterov static long do_wait(struct wait_opts *wo)
15851da177e4SLinus Torvalds {
158698abed02SRoland McGrath 	int retval;
15871da177e4SLinus Torvalds 
15889e8ae01dSOleg Nesterov 	trace_sched_process_wait(wo->wo_pid);
15890a16b607SMathieu Desnoyers 
15900b7570e7SOleg Nesterov 	init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
15910b7570e7SOleg Nesterov 	wo->child_wait.private = current;
15920b7570e7SOleg Nesterov 	add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
15931da177e4SLinus Torvalds repeat:
159498abed02SRoland McGrath 	/*
15953da56d16SFrans Klaver 	 * If there is nothing that can match our criteria, just get out.
15969e8ae01dSOleg Nesterov 	 * We will clear ->notask_error to zero if we see any child that
15979e8ae01dSOleg Nesterov 	 * might later match our criteria, even if we are not able to reap
15989e8ae01dSOleg Nesterov 	 * it yet.
159998abed02SRoland McGrath 	 */
160064a16cafSOleg Nesterov 	wo->notask_error = -ECHILD;
16019e8ae01dSOleg Nesterov 	if ((wo->wo_type < PIDTYPE_MAX) &&
16021722c14aSChristian Brauner 	   (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
160364a16cafSOleg Nesterov 		goto notask;
1604161550d7SEric W. Biederman 
1605f95d39d1SOleg Nesterov 	set_current_state(TASK_INTERRUPTIBLE);
16061da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
16075449162aSJim Newsome 
16085449162aSJim Newsome 	if (wo->wo_type == PIDTYPE_PID) {
16095449162aSJim Newsome 		retval = do_wait_pid(wo);
16105449162aSJim Newsome 		if (retval)
16115449162aSJim Newsome 			goto end;
16125449162aSJim Newsome 	} else {
16135449162aSJim Newsome 		struct task_struct *tsk = current;
16145449162aSJim Newsome 
16151da177e4SLinus Torvalds 		do {
161664a16cafSOleg Nesterov 			retval = do_wait_thread(wo, tsk);
161764a16cafSOleg Nesterov 			if (retval)
16189cbab810SOleg Nesterov 				goto end;
161964a16cafSOleg Nesterov 
162064a16cafSOleg Nesterov 			retval = ptrace_do_wait(wo, tsk);
162164a16cafSOleg Nesterov 			if (retval)
162264a16cafSOleg Nesterov 				goto end;
162398abed02SRoland McGrath 
16249e8ae01dSOleg Nesterov 			if (wo->wo_flags & __WNOTHREAD)
16251da177e4SLinus Torvalds 				break;
1626a3f6dfb7SOleg Nesterov 		} while_each_thread(current, tsk);
16275449162aSJim Newsome 	}
16281da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1629f2cc3eb1SOleg Nesterov 
163064a16cafSOleg Nesterov notask:
16319e8ae01dSOleg Nesterov 	retval = wo->notask_error;
16329e8ae01dSOleg Nesterov 	if (!retval && !(wo->wo_flags & WNOHANG)) {
16331da177e4SLinus Torvalds 		retval = -ERESTARTSYS;
163498abed02SRoland McGrath 		if (!signal_pending(current)) {
16351da177e4SLinus Torvalds 			schedule();
16361da177e4SLinus Torvalds 			goto repeat;
16371da177e4SLinus Torvalds 		}
163898abed02SRoland McGrath 	}
16391da177e4SLinus Torvalds end:
1640f95d39d1SOleg Nesterov 	__set_current_state(TASK_RUNNING);
16410b7570e7SOleg Nesterov 	remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
16421da177e4SLinus Torvalds 	return retval;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
kernel_waitid(int which,pid_t upid,struct waitid_info * infop,int options,struct rusage * ru)164567d7dddeSAl Viro static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
1646ce72a16fSAl Viro 			  int options, struct rusage *ru)
16471da177e4SLinus Torvalds {
16489e8ae01dSOleg Nesterov 	struct wait_opts wo;
1649161550d7SEric W. Biederman 	struct pid *pid = NULL;
1650161550d7SEric W. Biederman 	enum pid_type type;
16511da177e4SLinus Torvalds 	long ret;
1652ba7d25f3SChristian Brauner 	unsigned int f_flags = 0;
16531da177e4SLinus Torvalds 
165491c4e8eaSOleg Nesterov 	if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
165591c4e8eaSOleg Nesterov 			__WNOTHREAD|__WCLONE|__WALL))
16561da177e4SLinus Torvalds 		return -EINVAL;
16571da177e4SLinus Torvalds 	if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
16581da177e4SLinus Torvalds 		return -EINVAL;
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds 	switch (which) {
16611da177e4SLinus Torvalds 	case P_ALL:
1662161550d7SEric W. Biederman 		type = PIDTYPE_MAX;
16631da177e4SLinus Torvalds 		break;
16641da177e4SLinus Torvalds 	case P_PID:
1665161550d7SEric W. Biederman 		type = PIDTYPE_PID;
1666161550d7SEric W. Biederman 		if (upid <= 0)
16671da177e4SLinus Torvalds 			return -EINVAL;
16683695eae5SChristian Brauner 
16693695eae5SChristian Brauner 		pid = find_get_pid(upid);
16701da177e4SLinus Torvalds 		break;
16711da177e4SLinus Torvalds 	case P_PGID:
1672161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
1673821cc7b0SEric W. Biederman 		if (upid < 0)
16741da177e4SLinus Torvalds 			return -EINVAL;
16753695eae5SChristian Brauner 
1676821cc7b0SEric W. Biederman 		if (upid)
16773695eae5SChristian Brauner 			pid = find_get_pid(upid);
1678821cc7b0SEric W. Biederman 		else
1679821cc7b0SEric W. Biederman 			pid = get_task_pid(current, PIDTYPE_PGID);
16803695eae5SChristian Brauner 		break;
16813695eae5SChristian Brauner 	case P_PIDFD:
16823695eae5SChristian Brauner 		type = PIDTYPE_PID;
16833695eae5SChristian Brauner 		if (upid < 0)
16843695eae5SChristian Brauner 			return -EINVAL;
16853695eae5SChristian Brauner 
1686ba7d25f3SChristian Brauner 		pid = pidfd_get_pid(upid, &f_flags);
16873695eae5SChristian Brauner 		if (IS_ERR(pid))
16883695eae5SChristian Brauner 			return PTR_ERR(pid);
1689ba7d25f3SChristian Brauner 
16901da177e4SLinus Torvalds 		break;
16911da177e4SLinus Torvalds 	default:
16921da177e4SLinus Torvalds 		return -EINVAL;
16931da177e4SLinus Torvalds 	}
16941da177e4SLinus Torvalds 
16959e8ae01dSOleg Nesterov 	wo.wo_type	= type;
16969e8ae01dSOleg Nesterov 	wo.wo_pid	= pid;
16979e8ae01dSOleg Nesterov 	wo.wo_flags	= options;
16989e8ae01dSOleg Nesterov 	wo.wo_info	= infop;
16999e8ae01dSOleg Nesterov 	wo.wo_rusage	= ru;
1700ba7d25f3SChristian Brauner 	if (f_flags & O_NONBLOCK)
1701ba7d25f3SChristian Brauner 		wo.wo_flags |= WNOHANG;
1702ba7d25f3SChristian Brauner 
17039e8ae01dSOleg Nesterov 	ret = do_wait(&wo);
1704ba7d25f3SChristian Brauner 	if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK))
1705ba7d25f3SChristian Brauner 		ret = -EAGAIN;
1706dfe16dfaSVitaly Mayatskikh 
1707161550d7SEric W. Biederman 	put_pid(pid);
17081da177e4SLinus Torvalds 	return ret;
17091da177e4SLinus Torvalds }
17101da177e4SLinus Torvalds 
SYSCALL_DEFINE5(waitid,int,which,pid_t,upid,struct siginfo __user *,infop,int,options,struct rusage __user *,ru)1711ce72a16fSAl Viro SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1712ce72a16fSAl Viro 		infop, int, options, struct rusage __user *, ru)
1713ce72a16fSAl Viro {
1714ce72a16fSAl Viro 	struct rusage r;
171567d7dddeSAl Viro 	struct waitid_info info = {.status = 0};
171667d7dddeSAl Viro 	long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
1717634a8160SAl Viro 	int signo = 0;
17186c85501fSAl Viro 
1719634a8160SAl Viro 	if (err > 0) {
1720634a8160SAl Viro 		signo = SIGCHLD;
1721634a8160SAl Viro 		err = 0;
1722ce72a16fSAl Viro 		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1723ce72a16fSAl Viro 			return -EFAULT;
1724ce72a16fSAl Viro 	}
172567d7dddeSAl Viro 	if (!infop)
172667d7dddeSAl Viro 		return err;
172767d7dddeSAl Viro 
172841cd7805SChristophe Leroy 	if (!user_write_access_begin(infop, sizeof(*infop)))
17291c9fec47SKees Cook 		return -EFAULT;
173096ca579aSKees Cook 
1731634a8160SAl Viro 	unsafe_put_user(signo, &infop->si_signo, Efault);
17324c48abe9SAl Viro 	unsafe_put_user(0, &infop->si_errno, Efault);
1733cc731525SEric W. Biederman 	unsafe_put_user(info.cause, &infop->si_code, Efault);
17344c48abe9SAl Viro 	unsafe_put_user(info.pid, &infop->si_pid, Efault);
17354c48abe9SAl Viro 	unsafe_put_user(info.uid, &infop->si_uid, Efault);
17364c48abe9SAl Viro 	unsafe_put_user(info.status, &infop->si_status, Efault);
173741cd7805SChristophe Leroy 	user_write_access_end();
1738ce72a16fSAl Viro 	return err;
17394c48abe9SAl Viro Efault:
174041cd7805SChristophe Leroy 	user_write_access_end();
17414c48abe9SAl Viro 	return -EFAULT;
1742ce72a16fSAl Viro }
1743ce72a16fSAl Viro 
kernel_wait4(pid_t upid,int __user * stat_addr,int options,struct rusage * ru)174492ebce5aSAl Viro long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
174592ebce5aSAl Viro 		  struct rusage *ru)
17461da177e4SLinus Torvalds {
17479e8ae01dSOleg Nesterov 	struct wait_opts wo;
1748161550d7SEric W. Biederman 	struct pid *pid = NULL;
1749161550d7SEric W. Biederman 	enum pid_type type;
17501da177e4SLinus Torvalds 	long ret;
17511da177e4SLinus Torvalds 
17521da177e4SLinus Torvalds 	if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
17531da177e4SLinus Torvalds 			__WNOTHREAD|__WCLONE|__WALL))
17541da177e4SLinus Torvalds 		return -EINVAL;
1755161550d7SEric W. Biederman 
1756dd83c161Szhongjiang 	/* -INT_MIN is not defined */
1757dd83c161Szhongjiang 	if (upid == INT_MIN)
1758dd83c161Szhongjiang 		return -ESRCH;
1759dd83c161Szhongjiang 
1760161550d7SEric W. Biederman 	if (upid == -1)
1761161550d7SEric W. Biederman 		type = PIDTYPE_MAX;
1762161550d7SEric W. Biederman 	else if (upid < 0) {
1763161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
1764161550d7SEric W. Biederman 		pid = find_get_pid(-upid);
1765161550d7SEric W. Biederman 	} else if (upid == 0) {
1766161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
17672ae448efSOleg Nesterov 		pid = get_task_pid(current, PIDTYPE_PGID);
1768161550d7SEric W. Biederman 	} else /* upid > 0 */ {
1769161550d7SEric W. Biederman 		type = PIDTYPE_PID;
1770161550d7SEric W. Biederman 		pid = find_get_pid(upid);
1771161550d7SEric W. Biederman 	}
1772161550d7SEric W. Biederman 
17739e8ae01dSOleg Nesterov 	wo.wo_type	= type;
17749e8ae01dSOleg Nesterov 	wo.wo_pid	= pid;
17759e8ae01dSOleg Nesterov 	wo.wo_flags	= options | WEXITED;
17769e8ae01dSOleg Nesterov 	wo.wo_info	= NULL;
1777359566faSAl Viro 	wo.wo_stat	= 0;
17789e8ae01dSOleg Nesterov 	wo.wo_rusage	= ru;
17799e8ae01dSOleg Nesterov 	ret = do_wait(&wo);
1780161550d7SEric W. Biederman 	put_pid(pid);
1781359566faSAl Viro 	if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
1782359566faSAl Viro 		ret = -EFAULT;
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds 	return ret;
17851da177e4SLinus Torvalds }
17861da177e4SLinus Torvalds 
kernel_wait(pid_t pid,int * stat)17878043fc14SChristoph Hellwig int kernel_wait(pid_t pid, int *stat)
17888043fc14SChristoph Hellwig {
17898043fc14SChristoph Hellwig 	struct wait_opts wo = {
17908043fc14SChristoph Hellwig 		.wo_type	= PIDTYPE_PID,
17918043fc14SChristoph Hellwig 		.wo_pid		= find_get_pid(pid),
17928043fc14SChristoph Hellwig 		.wo_flags	= WEXITED,
17938043fc14SChristoph Hellwig 	};
17948043fc14SChristoph Hellwig 	int ret;
17958043fc14SChristoph Hellwig 
17968043fc14SChristoph Hellwig 	ret = do_wait(&wo);
17978043fc14SChristoph Hellwig 	if (ret > 0 && wo.wo_stat)
17988043fc14SChristoph Hellwig 		*stat = wo.wo_stat;
17998043fc14SChristoph Hellwig 	put_pid(wo.wo_pid);
18008043fc14SChristoph Hellwig 	return ret;
18018043fc14SChristoph Hellwig }
18028043fc14SChristoph Hellwig 
SYSCALL_DEFINE4(wait4,pid_t,upid,int __user *,stat_addr,int,options,struct rusage __user *,ru)1803ce72a16fSAl Viro SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1804ce72a16fSAl Viro 		int, options, struct rusage __user *, ru)
1805ce72a16fSAl Viro {
1806ce72a16fSAl Viro 	struct rusage r;
1807ce72a16fSAl Viro 	long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
1808ce72a16fSAl Viro 
1809ce72a16fSAl Viro 	if (err > 0) {
1810ce72a16fSAl Viro 		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1811ce72a16fSAl Viro 			return -EFAULT;
1812ce72a16fSAl Viro 	}
1813ce72a16fSAl Viro 	return err;
1814ce72a16fSAl Viro }
1815ce72a16fSAl Viro 
18161da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_WAITPID
18171da177e4SLinus Torvalds 
18181da177e4SLinus Torvalds /*
18191da177e4SLinus Torvalds  * sys_waitpid() remains for compatibility. waitpid() should be
18201da177e4SLinus Torvalds  * implemented by calling sys_wait4() from libc.a.
18211da177e4SLinus Torvalds  */
SYSCALL_DEFINE3(waitpid,pid_t,pid,int __user *,stat_addr,int,options)182217da2bd9SHeiko Carstens SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
18231da177e4SLinus Torvalds {
1824d300b610SDominik Brodowski 	return kernel_wait4(pid, stat_addr, options, NULL);
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds #endif
18287e95a225SAl Viro 
18297e95a225SAl 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)18307e95a225SAl Viro COMPAT_SYSCALL_DEFINE4(wait4,
18317e95a225SAl Viro 	compat_pid_t, pid,
18327e95a225SAl Viro 	compat_uint_t __user *, stat_addr,
18337e95a225SAl Viro 	int, options,
18347e95a225SAl Viro 	struct compat_rusage __user *, ru)
18357e95a225SAl Viro {
18367e95a225SAl Viro 	struct rusage r;
1837ce72a16fSAl Viro 	long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
1838ce72a16fSAl Viro 	if (err > 0) {
1839ce72a16fSAl Viro 		if (ru && put_compat_rusage(&r, ru))
18407e95a225SAl Viro 			return -EFAULT;
18417e95a225SAl Viro 	}
1842ce72a16fSAl Viro 	return err;
18437e95a225SAl Viro }
18447e95a225SAl Viro 
COMPAT_SYSCALL_DEFINE5(waitid,int,which,compat_pid_t,pid,struct compat_siginfo __user *,infop,int,options,struct compat_rusage __user *,uru)18457e95a225SAl Viro COMPAT_SYSCALL_DEFINE5(waitid,
18467e95a225SAl Viro 		int, which, compat_pid_t, pid,
18477e95a225SAl Viro 		struct compat_siginfo __user *, infop, int, options,
18487e95a225SAl Viro 		struct compat_rusage __user *, uru)
18497e95a225SAl Viro {
18507e95a225SAl Viro 	struct rusage ru;
185167d7dddeSAl Viro 	struct waitid_info info = {.status = 0};
185267d7dddeSAl Viro 	long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
1853634a8160SAl Viro 	int signo = 0;
1854634a8160SAl Viro 	if (err > 0) {
1855634a8160SAl Viro 		signo = SIGCHLD;
1856634a8160SAl Viro 		err = 0;
18576c85501fSAl Viro 		if (uru) {
185867d7dddeSAl Viro 			/* kernel_waitid() overwrites everything in ru */
18597e95a225SAl Viro 			if (COMPAT_USE_64BIT_TIME)
186067d7dddeSAl Viro 				err = copy_to_user(uru, &ru, sizeof(ru));
18617e95a225SAl Viro 			else
186267d7dddeSAl Viro 				err = put_compat_rusage(&ru, uru);
186367d7dddeSAl Viro 			if (err)
18647e95a225SAl Viro 				return -EFAULT;
18657e95a225SAl Viro 		}
18666c85501fSAl Viro 	}
18677e95a225SAl Viro 
18684c48abe9SAl Viro 	if (!infop)
186967d7dddeSAl Viro 		return err;
18704c48abe9SAl Viro 
187141cd7805SChristophe Leroy 	if (!user_write_access_begin(infop, sizeof(*infop)))
18721c9fec47SKees Cook 		return -EFAULT;
187396ca579aSKees Cook 
1874634a8160SAl Viro 	unsafe_put_user(signo, &infop->si_signo, Efault);
18754c48abe9SAl Viro 	unsafe_put_user(0, &infop->si_errno, Efault);
1876cc731525SEric W. Biederman 	unsafe_put_user(info.cause, &infop->si_code, Efault);
18774c48abe9SAl Viro 	unsafe_put_user(info.pid, &infop->si_pid, Efault);
18784c48abe9SAl Viro 	unsafe_put_user(info.uid, &infop->si_uid, Efault);
18794c48abe9SAl Viro 	unsafe_put_user(info.status, &infop->si_status, Efault);
188041cd7805SChristophe Leroy 	user_write_access_end();
18814c48abe9SAl Viro 	return err;
18824c48abe9SAl Viro Efault:
188341cd7805SChristophe Leroy 	user_write_access_end();
18844c48abe9SAl Viro 	return -EFAULT;
18857e95a225SAl Viro }
18867e95a225SAl Viro #endif
18877c2c11b2SSudip Mukherjee 
188838fd525aSEric W. Biederman /**
188938fd525aSEric W. Biederman  * thread_group_exited - check that a thread group has exited
189038fd525aSEric W. Biederman  * @pid: tgid of thread group to be checked.
189138fd525aSEric W. Biederman  *
189238fd525aSEric W. Biederman  * Test if the thread group represented by tgid has exited (all
189338fd525aSEric W. Biederman  * threads are zombies, dead or completely gone).
189438fd525aSEric W. Biederman  *
189538fd525aSEric W. Biederman  * Return: true if the thread group has exited. false otherwise.
189638fd525aSEric W. Biederman  */
thread_group_exited(struct pid * pid)189738fd525aSEric W. Biederman bool thread_group_exited(struct pid *pid)
189838fd525aSEric W. Biederman {
189938fd525aSEric W. Biederman 	struct task_struct *task;
190038fd525aSEric W. Biederman 	bool exited;
190138fd525aSEric W. Biederman 
190238fd525aSEric W. Biederman 	rcu_read_lock();
190338fd525aSEric W. Biederman 	task = pid_task(pid, PIDTYPE_PID);
190438fd525aSEric W. Biederman 	exited = !task ||
190538fd525aSEric W. Biederman 		(READ_ONCE(task->exit_state) && thread_group_empty(task));
190638fd525aSEric W. Biederman 	rcu_read_unlock();
190738fd525aSEric W. Biederman 
190838fd525aSEric W. Biederman 	return exited;
190938fd525aSEric W. Biederman }
191038fd525aSEric W. Biederman EXPORT_SYMBOL(thread_group_exited);
191138fd525aSEric W. Biederman 
1912c27cd083SMark Rutland /*
1913c27cd083SMark Rutland  * This needs to be __function_aligned as GCC implicitly makes any
1914c27cd083SMark Rutland  * implementation of abort() cold and drops alignment specified by
1915c27cd083SMark Rutland  * -falign-functions=N.
1916c27cd083SMark Rutland  *
1917c27cd083SMark Rutland  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
1918c27cd083SMark Rutland  */
abort(void)1919c27cd083SMark Rutland __weak __function_aligned void abort(void)
19207c2c11b2SSudip Mukherjee {
19217c2c11b2SSudip Mukherjee 	BUG();
19227c2c11b2SSudip Mukherjee 
19237c2c11b2SSudip Mukherjee 	/* if that doesn't kill us, halt */
19247c2c11b2SSudip Mukherjee 	panic("Oops failed to kill thread");
19257c2c11b2SSudip Mukherjee }
1926dc8635b7SAndrew Morton EXPORT_SYMBOL(abort);
1927