xref: /openbmc/linux/kernel/exit.c (revision 207f135d)
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);
414*f9010dbdSMike Christie 
415*f9010dbdSMike Christie 	/* The vhost_worker does not particpate in coredumps */
416*f9010dbdSMike Christie 	if (core_state &&
417*f9010dbdSMike 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) {
48839af1765SOleg Nesterov 		if (g->flags & PF_KTHREAD)
48939af1765SOleg Nesterov 			continue;
49039af1765SOleg Nesterov 		for_each_thread(g, c) {
49139af1765SOleg Nesterov 			if (c->mm == mm)
492cf475ad2SBalbir Singh 				goto assign_new_owner;
49339af1765SOleg Nesterov 			if (c->mm)
49439af1765SOleg Nesterov 				break;
49539af1765SOleg Nesterov 		}
496f87fb599SOleg Nesterov 	}
497cf475ad2SBalbir Singh 	read_unlock(&tasklist_lock);
49831a78f23SBalbir Singh 	/*
49931a78f23SBalbir Singh 	 * We found no owner yet mm_users > 1: this implies that we are
50031a78f23SBalbir Singh 	 * most likely racing with swapoff (try_to_unuse()) or /proc or
501e5991371SHugh Dickins 	 * ptrace or page migration (get_task_mm()).  Mark owner as NULL.
50231a78f23SBalbir Singh 	 */
503987717e5SAndrea Arcangeli 	WRITE_ONCE(mm->owner, NULL);
504cf475ad2SBalbir Singh 	return;
505cf475ad2SBalbir Singh 
506cf475ad2SBalbir Singh assign_new_owner:
507cf475ad2SBalbir Singh 	BUG_ON(c == p);
508cf475ad2SBalbir Singh 	get_task_struct(c);
509cf475ad2SBalbir Singh 	/*
510cf475ad2SBalbir Singh 	 * The task_lock protects c->mm from changing.
511cf475ad2SBalbir Singh 	 * We always want mm->owner->mm == mm
512cf475ad2SBalbir Singh 	 */
513cf475ad2SBalbir Singh 	task_lock(c);
514e5991371SHugh Dickins 	/*
515e5991371SHugh Dickins 	 * Delay read_unlock() till we have the task_lock()
516e5991371SHugh Dickins 	 * to ensure that c does not slip away underneath us
517e5991371SHugh Dickins 	 */
518e5991371SHugh Dickins 	read_unlock(&tasklist_lock);
519cf475ad2SBalbir Singh 	if (c->mm != mm) {
520cf475ad2SBalbir Singh 		task_unlock(c);
521cf475ad2SBalbir Singh 		put_task_struct(c);
522cf475ad2SBalbir Singh 		goto retry;
523cf475ad2SBalbir Singh 	}
524987717e5SAndrea Arcangeli 	WRITE_ONCE(mm->owner, c);
525bd74fdaeSYu Zhao 	lru_gen_migrate_mm(mm);
526cf475ad2SBalbir Singh 	task_unlock(c);
527cf475ad2SBalbir Singh 	put_task_struct(c);
528cf475ad2SBalbir Singh }
529f98bafa0SOleg Nesterov #endif /* CONFIG_MEMCG */
530cf475ad2SBalbir Singh 
5311da177e4SLinus Torvalds /*
5321da177e4SLinus Torvalds  * Turn us into a lazy TLB process if we
5331da177e4SLinus Torvalds  * aren't already..
5341da177e4SLinus Torvalds  */
exit_mm(void)5350039962aSDavidlohr Bueso static void exit_mm(void)
5361da177e4SLinus Torvalds {
5370039962aSDavidlohr Bueso 	struct mm_struct *mm = current->mm;
5381da177e4SLinus Torvalds 
5394610ba7aSThomas Gleixner 	exit_mm_release(current, mm);
5401da177e4SLinus Torvalds 	if (!mm)
5411da177e4SLinus Torvalds 		return;
5424fe7efdbSKonstantin Khlebnikov 	sync_mm_rss(mm);
543d8ed45c5SMichel Lespinasse 	mmap_read_lock(mm);
544aa464ba9SNicholas Piggin 	mmgrab_lazy_tlb(mm);
5450039962aSDavidlohr Bueso 	BUG_ON(mm != current->active_mm);
5461da177e4SLinus Torvalds 	/* more a memory barrier than a real lock */
5470039962aSDavidlohr Bueso 	task_lock(current);
5485bc78502SMathieu Desnoyers 	/*
5495bc78502SMathieu Desnoyers 	 * When a thread stops operating on an address space, the loop
5505bc78502SMathieu Desnoyers 	 * in membarrier_private_expedited() may not observe that
5515bc78502SMathieu Desnoyers 	 * tsk->mm, and the loop in membarrier_global_expedited() may
5525bc78502SMathieu Desnoyers 	 * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
5535bc78502SMathieu Desnoyers 	 * rq->membarrier_state, so those would not issue an IPI.
5545bc78502SMathieu Desnoyers 	 * Membarrier requires a memory barrier after accessing
5555bc78502SMathieu Desnoyers 	 * user-space memory, before clearing tsk->mm or the
5565bc78502SMathieu Desnoyers 	 * rq->membarrier_state.
5575bc78502SMathieu Desnoyers 	 */
5585bc78502SMathieu Desnoyers 	smp_mb__after_spinlock();
5595bc78502SMathieu Desnoyers 	local_irq_disable();
5600039962aSDavidlohr Bueso 	current->mm = NULL;
5615bc78502SMathieu Desnoyers 	membarrier_update_current_mm(NULL);
5621da177e4SLinus Torvalds 	enter_lazy_tlb(mm, current);
5635bc78502SMathieu Desnoyers 	local_irq_enable();
5640039962aSDavidlohr Bueso 	task_unlock(current);
5655bc78502SMathieu Desnoyers 	mmap_read_unlock(mm);
566cf475ad2SBalbir Singh 	mm_update_next_owner(mm);
5671da177e4SLinus Torvalds 	mmput(mm);
568c32b3cbeSMichal Hocko 	if (test_thread_flag(TIF_MEMDIE))
56938531201STetsuo Handa 		exit_oom_victim();
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds 
find_alive_thread(struct task_struct * p)572c9dc05bfSOleg Nesterov static struct task_struct *find_alive_thread(struct task_struct *p)
573c9dc05bfSOleg Nesterov {
574c9dc05bfSOleg Nesterov 	struct task_struct *t;
575c9dc05bfSOleg Nesterov 
576c9dc05bfSOleg Nesterov 	for_each_thread(p, t) {
577c9dc05bfSOleg Nesterov 		if (!(t->flags & PF_EXITING))
578c9dc05bfSOleg Nesterov 			return t;
579c9dc05bfSOleg Nesterov 	}
580c9dc05bfSOleg Nesterov 	return NULL;
581c9dc05bfSOleg Nesterov }
582c9dc05bfSOleg Nesterov 
find_child_reaper(struct task_struct * father,struct list_head * dead)5838fb335e0SAndrei Vagin static struct task_struct *find_child_reaper(struct task_struct *father,
5848fb335e0SAndrei Vagin 						struct list_head *dead)
5851109909cSOleg Nesterov 	__releases(&tasklist_lock)
5861109909cSOleg Nesterov 	__acquires(&tasklist_lock)
5871109909cSOleg Nesterov {
5881109909cSOleg Nesterov 	struct pid_namespace *pid_ns = task_active_pid_ns(father);
5891109909cSOleg Nesterov 	struct task_struct *reaper = pid_ns->child_reaper;
5908fb335e0SAndrei Vagin 	struct task_struct *p, *n;
5911109909cSOleg Nesterov 
5921109909cSOleg Nesterov 	if (likely(reaper != father))
5931109909cSOleg Nesterov 		return reaper;
5941109909cSOleg Nesterov 
595c9dc05bfSOleg Nesterov 	reaper = find_alive_thread(father);
596c9dc05bfSOleg Nesterov 	if (reaper) {
5971109909cSOleg Nesterov 		pid_ns->child_reaper = reaper;
5981109909cSOleg Nesterov 		return reaper;
5991109909cSOleg Nesterov 	}
6001109909cSOleg Nesterov 
6011109909cSOleg Nesterov 	write_unlock_irq(&tasklist_lock);
6028fb335e0SAndrei Vagin 
6038fb335e0SAndrei Vagin 	list_for_each_entry_safe(p, n, dead, ptrace_entry) {
6048fb335e0SAndrei Vagin 		list_del_init(&p->ptrace_entry);
6058fb335e0SAndrei Vagin 		release_task(p);
6068fb335e0SAndrei Vagin 	}
6078fb335e0SAndrei Vagin 
6081109909cSOleg Nesterov 	zap_pid_ns_processes(pid_ns);
6091109909cSOleg Nesterov 	write_lock_irq(&tasklist_lock);
6101109909cSOleg Nesterov 
6111109909cSOleg Nesterov 	return father;
6121109909cSOleg Nesterov }
6131109909cSOleg Nesterov 
6141da177e4SLinus Torvalds /*
615ebec18a6SLennart Poettering  * When we die, we re-parent all our children, and try to:
616ebec18a6SLennart Poettering  * 1. give them to another thread in our thread group, if such a member exists
617ebec18a6SLennart Poettering  * 2. give it to the first ancestor process which prctl'd itself as a
618ebec18a6SLennart Poettering  *    child_subreaper for its children (like a service manager)
619ebec18a6SLennart Poettering  * 3. give it to the init process (PID 1) in our pid namespace
6201da177e4SLinus Torvalds  */
find_new_reaper(struct task_struct * father,struct task_struct * child_reaper)6211109909cSOleg Nesterov static struct task_struct *find_new_reaper(struct task_struct *father,
6221109909cSOleg Nesterov 					   struct task_struct *child_reaper)
623950bbabbSOleg Nesterov {
624c9dc05bfSOleg Nesterov 	struct task_struct *thread, *reaper;
625950bbabbSOleg Nesterov 
626c9dc05bfSOleg Nesterov 	thread = find_alive_thread(father);
627c9dc05bfSOleg Nesterov 	if (thread)
628950bbabbSOleg Nesterov 		return thread;
629950bbabbSOleg Nesterov 
6307d24e2dfSOleg Nesterov 	if (father->signal->has_child_subreaper) {
631c6c70f44SOleg Nesterov 		unsigned int ns_level = task_pid(father)->level;
632ebec18a6SLennart Poettering 		/*
633175aed3fSOleg Nesterov 		 * Find the first ->is_child_subreaper ancestor in our pid_ns.
634c6c70f44SOleg Nesterov 		 * We can't check reaper != child_reaper to ensure we do not
635c6c70f44SOleg Nesterov 		 * cross the namespaces, the exiting parent could be injected
636c6c70f44SOleg Nesterov 		 * by setns() + fork().
637c6c70f44SOleg Nesterov 		 * We check pid->level, this is slightly more efficient than
638c6c70f44SOleg Nesterov 		 * task_active_pid_ns(reaper) != task_active_pid_ns(father).
639ebec18a6SLennart Poettering 		 */
640c6c70f44SOleg Nesterov 		for (reaper = father->real_parent;
641c6c70f44SOleg Nesterov 		     task_pid(reaper)->level == ns_level;
642ebec18a6SLennart Poettering 		     reaper = reaper->real_parent) {
643175aed3fSOleg Nesterov 			if (reaper == &init_task)
644ebec18a6SLennart Poettering 				break;
645ebec18a6SLennart Poettering 			if (!reaper->signal->is_child_subreaper)
646ebec18a6SLennart Poettering 				continue;
647c9dc05bfSOleg Nesterov 			thread = find_alive_thread(reaper);
648c9dc05bfSOleg Nesterov 			if (thread)
6498a1296aeSOleg Nesterov 				return thread;
6503750ef97SOleg Nesterov 		}
651ebec18a6SLennart Poettering 	}
652950bbabbSOleg Nesterov 
6531109909cSOleg Nesterov 	return child_reaper;
654950bbabbSOleg Nesterov }
655950bbabbSOleg Nesterov 
6565dfc80beSOleg Nesterov /*
6575dfc80beSOleg Nesterov * Any that need to be release_task'd are put on the @dead list.
6585dfc80beSOleg Nesterov  */
reparent_leader(struct task_struct * father,struct task_struct * p,struct list_head * dead)6599cd80bbbSOleg Nesterov static void reparent_leader(struct task_struct *father, struct task_struct *p,
6605dfc80beSOleg Nesterov 				struct list_head *dead)
6615dfc80beSOleg Nesterov {
6622831096eSOleg Nesterov 	if (unlikely(p->exit_state == EXIT_DEAD))
6635dfc80beSOleg Nesterov 		return;
6645dfc80beSOleg Nesterov 
665abd50b39SOleg Nesterov 	/* We don't want people slaying init. */
6665dfc80beSOleg Nesterov 	p->exit_signal = SIGCHLD;
6675dfc80beSOleg Nesterov 
6685dfc80beSOleg Nesterov 	/* If it has exited notify the new parent about this child's death. */
669d21142ecSTejun Heo 	if (!p->ptrace &&
6705dfc80beSOleg Nesterov 	    p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
67186773473SOleg Nesterov 		if (do_notify_parent(p, p->exit_signal)) {
6725dfc80beSOleg Nesterov 			p->exit_state = EXIT_DEAD;
673dc2fd4b0SOleg Nesterov 			list_add(&p->ptrace_entry, dead);
6745dfc80beSOleg Nesterov 		}
6755dfc80beSOleg Nesterov 	}
6765dfc80beSOleg Nesterov 
6775dfc80beSOleg Nesterov 	kill_orphaned_pgrp(p, father);
6785dfc80beSOleg Nesterov }
6795dfc80beSOleg Nesterov 
680482a3767SOleg Nesterov /*
681482a3767SOleg Nesterov  * This does two things:
682482a3767SOleg Nesterov  *
683482a3767SOleg Nesterov  * A.  Make init inherit all the child processes
684482a3767SOleg Nesterov  * B.  Check to see if any process groups have become orphaned
685482a3767SOleg Nesterov  *	as a result of our exiting, and if they have any stopped
686482a3767SOleg Nesterov  *	jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
687482a3767SOleg Nesterov  */
forget_original_parent(struct task_struct * father,struct list_head * dead)688482a3767SOleg Nesterov static void forget_original_parent(struct task_struct *father,
689482a3767SOleg Nesterov 					struct list_head *dead)
6901da177e4SLinus Torvalds {
691482a3767SOleg Nesterov 	struct task_struct *p, *t, *reaper;
692762a24beSOleg Nesterov 
6937c8bd232SOleg Nesterov 	if (unlikely(!list_empty(&father->ptraced)))
694482a3767SOleg Nesterov 		exit_ptrace(father, dead);
695f470021aSRoland McGrath 
6967c8bd232SOleg Nesterov 	/* Can drop and reacquire tasklist_lock */
6978fb335e0SAndrei Vagin 	reaper = find_child_reaper(father, dead);
698ad9e206aSOleg Nesterov 	if (list_empty(&father->children))
699482a3767SOleg Nesterov 		return;
7001109909cSOleg Nesterov 
7011109909cSOleg Nesterov 	reaper = find_new_reaper(father, reaper);
7022831096eSOleg Nesterov 	list_for_each_entry(p, &father->children, sibling) {
70357a05918SOleg Nesterov 		for_each_thread(p, t) {
70422a34c6fSMadhuparna Bhowmik 			RCU_INIT_POINTER(t->real_parent, reaper);
70522a34c6fSMadhuparna Bhowmik 			BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
70657a05918SOleg Nesterov 			if (likely(!t->ptrace))
7079cd80bbbSOleg Nesterov 				t->parent = t->real_parent;
7089cd80bbbSOleg Nesterov 			if (t->pdeath_signal)
7099cd80bbbSOleg Nesterov 				group_send_sig_info(t->pdeath_signal,
71001024980SEric W. Biederman 						    SEND_SIG_NOINFO, t,
71101024980SEric W. Biederman 						    PIDTYPE_TGID);
71257a05918SOleg Nesterov 		}
7132831096eSOleg Nesterov 		/*
7142831096eSOleg Nesterov 		 * If this is a threaded reparent there is no need to
7152831096eSOleg Nesterov 		 * notify anyone anything has happened.
7162831096eSOleg Nesterov 		 */
7172831096eSOleg Nesterov 		if (!same_thread_group(reaper, father))
718482a3767SOleg Nesterov 			reparent_leader(father, p, dead);
7191da177e4SLinus Torvalds 	}
7202831096eSOleg Nesterov 	list_splice_tail_init(&father->children, &reaper->children);
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /*
7241da177e4SLinus Torvalds  * Send signals to all our closest relatives so that they know
7251da177e4SLinus Torvalds  * to properly mourn us..
7261da177e4SLinus Torvalds  */
exit_notify(struct task_struct * tsk,int group_dead)727821c7de7SOleg Nesterov static void exit_notify(struct task_struct *tsk, int group_dead)
7281da177e4SLinus Torvalds {
72953c8f9f1SOleg Nesterov 	bool autoreap;
730482a3767SOleg Nesterov 	struct task_struct *p, *n;
731482a3767SOleg Nesterov 	LIST_HEAD(dead);
7321da177e4SLinus Torvalds 
733762a24beSOleg Nesterov 	write_lock_irq(&tasklist_lock);
734482a3767SOleg Nesterov 	forget_original_parent(tsk, &dead);
735482a3767SOleg Nesterov 
736821c7de7SOleg Nesterov 	if (group_dead)
737821c7de7SOleg Nesterov 		kill_orphaned_pgrp(tsk->group_leader, NULL);
7381da177e4SLinus Torvalds 
739b191d649SSuren Baghdasaryan 	tsk->exit_state = EXIT_ZOMBIE;
74045cdf5ccSOleg Nesterov 	if (unlikely(tsk->ptrace)) {
74145cdf5ccSOleg Nesterov 		int sig = thread_group_leader(tsk) &&
74245cdf5ccSOleg Nesterov 				thread_group_empty(tsk) &&
74345cdf5ccSOleg Nesterov 				!ptrace_reparented(tsk) ?
74445cdf5ccSOleg Nesterov 			tsk->exit_signal : SIGCHLD;
74545cdf5ccSOleg Nesterov 		autoreap = do_notify_parent(tsk, sig);
74645cdf5ccSOleg Nesterov 	} else if (thread_group_leader(tsk)) {
74745cdf5ccSOleg Nesterov 		autoreap = thread_group_empty(tsk) &&
74845cdf5ccSOleg Nesterov 			do_notify_parent(tsk, tsk->exit_signal);
74945cdf5ccSOleg Nesterov 	} else {
75045cdf5ccSOleg Nesterov 		autoreap = true;
75145cdf5ccSOleg Nesterov 	}
7521da177e4SLinus Torvalds 
75330b692d3SChristian Brauner 	if (autoreap) {
75430b692d3SChristian Brauner 		tsk->exit_state = EXIT_DEAD;
7556c66e7dbSOleg Nesterov 		list_add(&tsk->ptrace_entry, &dead);
75630b692d3SChristian Brauner 	}
7571da177e4SLinus Torvalds 
7589c339168SOleg Nesterov 	/* mt-exec, de_thread() is waiting for group leader */
7599c339168SOleg Nesterov 	if (unlikely(tsk->signal->notify_count < 0))
76060700e38SEric W. Biederman 		wake_up_process(tsk->signal->group_exec_task);
7611da177e4SLinus Torvalds 	write_unlock_irq(&tasklist_lock);
7621da177e4SLinus Torvalds 
763482a3767SOleg Nesterov 	list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
764482a3767SOleg Nesterov 		list_del_init(&p->ptrace_entry);
765482a3767SOleg Nesterov 		release_task(p);
766482a3767SOleg Nesterov 	}
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
769e18eecb8SJeff Dike #ifdef CONFIG_DEBUG_STACK_USAGE
check_stack_usage(void)770e18eecb8SJeff Dike static void check_stack_usage(void)
771e18eecb8SJeff Dike {
772e18eecb8SJeff Dike 	static DEFINE_SPINLOCK(low_water_lock);
773e18eecb8SJeff Dike 	static int lowest_to_date = THREAD_SIZE;
774e18eecb8SJeff Dike 	unsigned long free;
775e18eecb8SJeff Dike 
7767c9f8861SEric Sandeen 	free = stack_not_used(current);
777e18eecb8SJeff Dike 
778e18eecb8SJeff Dike 	if (free >= lowest_to_date)
779e18eecb8SJeff Dike 		return;
780e18eecb8SJeff Dike 
781e18eecb8SJeff Dike 	spin_lock(&low_water_lock);
782e18eecb8SJeff Dike 	if (free < lowest_to_date) {
783627393d4SAnton Blanchard 		pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
784168eeccbSTim Bird 			current->comm, task_pid_nr(current), free);
785e18eecb8SJeff Dike 		lowest_to_date = free;
786e18eecb8SJeff Dike 	}
787e18eecb8SJeff Dike 	spin_unlock(&low_water_lock);
788e18eecb8SJeff Dike }
789e18eecb8SJeff Dike #else
check_stack_usage(void)790e18eecb8SJeff Dike static inline void check_stack_usage(void) {}
791e18eecb8SJeff Dike #endif
792e18eecb8SJeff Dike 
synchronize_group_exit(struct task_struct * tsk,long code)793d80f7d7bSEric W. Biederman static void synchronize_group_exit(struct task_struct *tsk, long code)
794d80f7d7bSEric W. Biederman {
795d80f7d7bSEric W. Biederman 	struct sighand_struct *sighand = tsk->sighand;
796d80f7d7bSEric W. Biederman 	struct signal_struct *signal = tsk->signal;
797d80f7d7bSEric W. Biederman 
798d80f7d7bSEric W. Biederman 	spin_lock_irq(&sighand->siglock);
799d80f7d7bSEric W. Biederman 	signal->quick_threads--;
800d80f7d7bSEric W. Biederman 	if ((signal->quick_threads == 0) &&
801d80f7d7bSEric W. Biederman 	    !(signal->flags & SIGNAL_GROUP_EXIT)) {
802d80f7d7bSEric W. Biederman 		signal->flags = SIGNAL_GROUP_EXIT;
803d80f7d7bSEric W. Biederman 		signal->group_exit_code = code;
804d80f7d7bSEric W. Biederman 		signal->group_stop_count = 0;
805d80f7d7bSEric W. Biederman 	}
806d80f7d7bSEric W. Biederman 	spin_unlock_irq(&sighand->siglock);
807d80f7d7bSEric W. Biederman }
808d80f7d7bSEric W. Biederman 
do_exit(long code)8099af6528eSPeter Zijlstra void __noreturn do_exit(long code)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct task_struct *tsk = current;
8121da177e4SLinus Torvalds 	int group_dead;
8131da177e4SLinus Torvalds 
814001c28e5SNicholas Piggin 	WARN_ON(irqs_disabled());
815001c28e5SNicholas Piggin 
816d80f7d7bSEric W. Biederman 	synchronize_group_exit(tsk, code);
817d80f7d7bSEric W. Biederman 
818b1f866b0SChristoph Hellwig 	WARN_ON(tsk->plug);
81922e2c507SJens Axboe 
820586b58caSJann Horn 	kcov_task_exit(tsk);
82150b5e49cSAlexander Potapenko 	kmsan_task_exit(tsk);
822586b58caSJann Horn 
82392307383SEric W. Biederman 	coredump_task_exit(tsk);
824a288eeccSTejun Heo 	ptrace_event(PTRACE_EVENT_EXIT, code);
825fd593511SBeau Belgrave 	user_events_exit(tsk);
8261da177e4SLinus Torvalds 
827f552a27aSHao Xu 	io_uring_files_cancel();
828d12619b5SOleg Nesterov 	exit_signals(tsk);  /* sets PF_EXITING */
8291da177e4SLinus Torvalds 
83048d212a2SLinus Torvalds 	/* sync mm's RSS info before statistics gathering */
83148d212a2SLinus Torvalds 	if (tsk->mm)
83248d212a2SLinus Torvalds 		sync_mm_rss(tsk->mm);
83351229b49SRik van Riel 	acct_update_integrals(tsk);
8341da177e4SLinus Torvalds 	group_dead = atomic_dec_and_test(&tsk->signal->live);
835c3068951SAndrew Morton 	if (group_dead) {
83643cf75d9Schenqiwu 		/*
83743cf75d9Schenqiwu 		 * If the last thread of global init has exited, panic
83843cf75d9Schenqiwu 		 * immediately to get a useable coredump.
83943cf75d9Schenqiwu 		 */
84043cf75d9Schenqiwu 		if (unlikely(is_global_init(tsk)))
84143cf75d9Schenqiwu 			panic("Attempted to kill init! exitcode=0x%08x\n",
84243cf75d9Schenqiwu 				tsk->signal->group_exit_code ?: (int)code);
84343cf75d9Schenqiwu 
844baa73d9eSNicolas Pitre #ifdef CONFIG_POSIX_TIMERS
8452ff678b8SThomas Gleixner 		hrtimer_cancel(&tsk->signal->real_timer);
846d5b36a4dSOleg Nesterov 		exit_itimers(tsk);
847baa73d9eSNicolas Pitre #endif
8481f10206cSJiri Pirko 		if (tsk->mm)
8491f10206cSJiri Pirko 			setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
850c3068951SAndrew Morton 	}
851f6ec29a4SKaiGai Kohei 	acct_collect(code, group_dead);
852522ed776SMiloslav Trmac 	if (group_dead)
853522ed776SMiloslav Trmac 		tty_audit_exit();
854fa84cb93SAl Viro 	audit_free(tsk);
855115085eaSOleg Nesterov 
85648d212a2SLinus Torvalds 	tsk->exit_code = code;
857115085eaSOleg Nesterov 	taskstats_exit(tsk, group_dead);
858c757249aSShailabh Nagar 
8590039962aSDavidlohr Bueso 	exit_mm();
8601da177e4SLinus Torvalds 
8610e464814SKaiGai Kohei 	if (group_dead)
862f6ec29a4SKaiGai Kohei 		acct_process();
8630a16b607SMathieu Desnoyers 	trace_sched_process_exit(tsk);
8640a16b607SMathieu Desnoyers 
8651da177e4SLinus Torvalds 	exit_sem(tsk);
866b34a6b1dSVasiliy Kulikov 	exit_shm(tsk);
8671ec7f1ddSAl Viro 	exit_files(tsk);
8681ec7f1ddSAl Viro 	exit_fs(tsk);
869c39df5faSOleg Nesterov 	if (group_dead)
870c39df5faSOleg Nesterov 		disassociate_ctty(1);
8718aac6270SOleg Nesterov 	exit_task_namespaces(tsk);
872ed3e694dSAl Viro 	exit_task_work(tsk);
873e6464694SJiri Slaby 	exit_thread(tsk);
8740b3fcf17SStephane Eranian 
8750b3fcf17SStephane Eranian 	/*
8760b3fcf17SStephane Eranian 	 * Flush inherited counters to the parent - before the parent
8770b3fcf17SStephane Eranian 	 * gets woken up by child-exit notifications.
8780b3fcf17SStephane Eranian 	 *
8790b3fcf17SStephane Eranian 	 * because of cgroup mode, must be called before cgroup_exit()
8800b3fcf17SStephane Eranian 	 */
8810b3fcf17SStephane Eranian 	perf_event_exit_task(tsk);
8820b3fcf17SStephane Eranian 
8838e5bfa8cSOleg Nesterov 	sched_autogroup_exit_task(tsk);
8841ec41830SLi Zefan 	cgroup_exit(tsk);
8851da177e4SLinus Torvalds 
88633b2fb30SIngo Molnar 	/*
88724f1e32cSFrederic Weisbecker 	 * FIXME: do that only when needed, using sched_exit tracepoint
88824f1e32cSFrederic Weisbecker 	 */
8897c8df286SOleg Nesterov 	flush_ptrace_hw_breakpoint(tsk);
89033b2fb30SIngo Molnar 
891ccdd29ffSPaul E. McKenney 	exit_tasks_rcu_start();
892821c7de7SOleg Nesterov 	exit_notify(tsk, group_dead);
893ef982393SGuillaume Morin 	proc_exit_connector(tsk);
894c11600e4SDavid Rientjes 	mpol_put_task_policy(tsk);
89542b2dd0aSAlexey Dobriyan #ifdef CONFIG_FUTEX
896c87e2837SIngo Molnar 	if (unlikely(current->pi_state_cache))
897c87e2837SIngo Molnar 		kfree(current->pi_state_cache);
89842b2dd0aSAlexey Dobriyan #endif
899c87e2837SIngo Molnar 	/*
9009a11b49aSIngo Molnar 	 * Make sure we are holding no locks:
901de5097c2SIngo Molnar 	 */
9021b1d2fb4SColin Cross 	debug_check_no_locks_held();
9031da177e4SLinus Torvalds 
904afc847b7SAl Viro 	if (tsk->io_context)
905b69f2292SLouis Rilling 		exit_io_context(tsk);
906afc847b7SAl Viro 
907b92ce558SJens Axboe 	if (tsk->splice_pipe)
9084b8a8f1eSAl Viro 		free_pipe_info(tsk->splice_pipe);
909b92ce558SJens Axboe 
9105640f768SEric Dumazet 	if (tsk->task_frag.page)
9115640f768SEric Dumazet 		put_page(tsk->task_frag.page);
9125640f768SEric Dumazet 
9131a03d3f1SSebastian Andrzej Siewior 	exit_task_stack_account(tsk);
914e0e81739SDavid Howells 
9154bcb8232SOleg Nesterov 	check_stack_usage();
9167407251aSCoywolf Qi Hunt 	preempt_disable();
91754848d73SWu Fengguang 	if (tsk->nr_dirtied)
91854848d73SWu Fengguang 		__this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
919f41d911fSPaul E. McKenney 	exit_rcu();
920ccdd29ffSPaul E. McKenney 	exit_tasks_rcu_finish();
921b5740f4bSYasunori Goto 
922b09be676SByungchul Park 	lockdep_free_task(tsk);
9239af6528eSPeter Zijlstra 	do_task_dead();
9241da177e4SLinus Torvalds }
925012914daSRuss Anderson 
make_task_dead(int signr)9260e25498fSEric W. Biederman void __noreturn make_task_dead(int signr)
9270e25498fSEric W. Biederman {
9280e25498fSEric W. Biederman 	/*
9290e25498fSEric W. Biederman 	 * Take the task off the cpu after something catastrophic has
9300e25498fSEric W. Biederman 	 * happened.
93105ea0424SEric W. Biederman 	 *
93205ea0424SEric W. Biederman 	 * We can get here from a kernel oops, sometimes with preemption off.
93305ea0424SEric W. Biederman 	 * Start by checking for critical errors.
93405ea0424SEric W. Biederman 	 * Then fix up important state like USER_DS and preemption.
93505ea0424SEric W. Biederman 	 * Then do everything else.
9360e25498fSEric W. Biederman 	 */
93705ea0424SEric W. Biederman 	struct task_struct *tsk = current;
9387535b832SKees Cook 	unsigned int limit;
93905ea0424SEric W. Biederman 
94005ea0424SEric W. Biederman 	if (unlikely(in_interrupt()))
94105ea0424SEric W. Biederman 		panic("Aiee, killing interrupt handler!");
94205ea0424SEric W. Biederman 	if (unlikely(!tsk->pid))
94305ea0424SEric W. Biederman 		panic("Attempted to kill the idle task!");
94405ea0424SEric W. Biederman 
945001c28e5SNicholas Piggin 	if (unlikely(irqs_disabled())) {
946001c28e5SNicholas Piggin 		pr_info("note: %s[%d] exited with irqs disabled\n",
947001c28e5SNicholas Piggin 			current->comm, task_pid_nr(current));
948001c28e5SNicholas Piggin 		local_irq_enable();
949001c28e5SNicholas Piggin 	}
95005ea0424SEric W. Biederman 	if (unlikely(in_atomic())) {
95105ea0424SEric W. Biederman 		pr_info("note: %s[%d] exited with preempt_count %d\n",
95205ea0424SEric W. Biederman 			current->comm, task_pid_nr(current),
95305ea0424SEric W. Biederman 			preempt_count());
95405ea0424SEric W. Biederman 		preempt_count_set(PREEMPT_ENABLED);
95505ea0424SEric W. Biederman 	}
95605ea0424SEric W. Biederman 
95705ea0424SEric W. Biederman 	/*
958d4ccd54dSJann Horn 	 * Every time the system oopses, if the oops happens while a reference
959d4ccd54dSJann Horn 	 * to an object was held, the reference leaks.
960d4ccd54dSJann Horn 	 * If the oops doesn't also leak memory, repeated oopsing can cause
961d4ccd54dSJann Horn 	 * reference counters to wrap around (if they're not using refcount_t).
962d4ccd54dSJann Horn 	 * This means that repeated oopsing can make unexploitable-looking bugs
963d4ccd54dSJann Horn 	 * exploitable through repeated oopsing.
964d4ccd54dSJann Horn 	 * To make sure this can't happen, place an upper bound on how often the
965d4ccd54dSJann Horn 	 * kernel may oops without panic().
966d4ccd54dSJann Horn 	 */
9677535b832SKees Cook 	limit = READ_ONCE(oops_limit);
9687535b832SKees Cook 	if (atomic_inc_return(&oops_count) >= limit && limit)
9697535b832SKees Cook 		panic("Oopsed too often (kernel.oops_limit is %d)", limit);
970d4ccd54dSJann Horn 
971d4ccd54dSJann Horn 	/*
97205ea0424SEric W. Biederman 	 * We're taking recursive faults here in make_task_dead. Safest is to just
97305ea0424SEric W. Biederman 	 * leave this task alone and wait for reboot.
97405ea0424SEric W. Biederman 	 */
97505ea0424SEric W. Biederman 	if (unlikely(tsk->flags & PF_EXITING)) {
97605ea0424SEric W. Biederman 		pr_alert("Fixing recursive fault but reboot is needed!\n");
97705ea0424SEric W. Biederman 		futex_exit_recursive(tsk);
978912616f1SEric W. Biederman 		tsk->exit_state = EXIT_DEAD;
979912616f1SEric W. Biederman 		refcount_inc(&tsk->rcu_users);
9807f80a2fdSEric W. Biederman 		do_task_dead();
98105ea0424SEric W. Biederman 	}
98205ea0424SEric W. Biederman 
9830e25498fSEric W. Biederman 	do_exit(signr);
9840e25498fSEric W. Biederman }
9850e25498fSEric W. Biederman 
SYSCALL_DEFINE1(exit,int,error_code)986754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit, int, error_code)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	do_exit((error_code&0xff)<<8);
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds /*
9921da177e4SLinus Torvalds  * Take down every thread in the group.  This is called by fatal signals
9931da177e4SLinus Torvalds  * as well as by sys_exit_group (below).
9941da177e4SLinus Torvalds  */
995eae654f1SPeter Zijlstra void __noreturn
do_group_exit(int exit_code)9961da177e4SLinus Torvalds do_group_exit(int exit_code)
9971da177e4SLinus Torvalds {
998bfc4b089SOleg Nesterov 	struct signal_struct *sig = current->signal;
999bfc4b089SOleg Nesterov 
100049697335SEric W. Biederman 	if (sig->flags & SIGNAL_GROUP_EXIT)
1001bfc4b089SOleg Nesterov 		exit_code = sig->group_exit_code;
100249697335SEric W. Biederman 	else if (sig->group_exec_task)
100349697335SEric W. Biederman 		exit_code = 0;
1004cbe9dac3SEric W. Biederman 	else {
10051da177e4SLinus Torvalds 		struct sighand_struct *const sighand = current->sighand;
1006a0be55deSIonut Alexa 
10071da177e4SLinus Torvalds 		spin_lock_irq(&sighand->siglock);
100849697335SEric W. Biederman 		if (sig->flags & SIGNAL_GROUP_EXIT)
10091da177e4SLinus Torvalds 			/* Another thread got here before we took the lock.  */
10101da177e4SLinus Torvalds 			exit_code = sig->group_exit_code;
101149697335SEric W. Biederman 		else if (sig->group_exec_task)
101249697335SEric W. Biederman 			exit_code = 0;
10131da177e4SLinus Torvalds 		else {
10141da177e4SLinus Torvalds 			sig->group_exit_code = exit_code;
1015ed5d2cacSOleg Nesterov 			sig->flags = SIGNAL_GROUP_EXIT;
10161da177e4SLinus Torvalds 			zap_other_threads(current);
10171da177e4SLinus Torvalds 		}
10181da177e4SLinus Torvalds 		spin_unlock_irq(&sighand->siglock);
10191da177e4SLinus Torvalds 	}
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	do_exit(exit_code);
10221da177e4SLinus Torvalds 	/* NOTREACHED */
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds  * this kills every thread in the thread group. Note that any externally
10271da177e4SLinus Torvalds  * wait4()-ing process will get the correct exit code - even if this
10281da177e4SLinus Torvalds  * thread is not the thread group leader.
10291da177e4SLinus Torvalds  */
SYSCALL_DEFINE1(exit_group,int,error_code)1030754fe8d2SHeiko Carstens SYSCALL_DEFINE1(exit_group, int, error_code)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	do_group_exit((error_code & 0xff) << 8);
10332ed7c03eSHeiko Carstens 	/* NOTREACHED */
10342ed7c03eSHeiko Carstens 	return 0;
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
103767d7dddeSAl Viro struct waitid_info {
103867d7dddeSAl Viro 	pid_t pid;
103967d7dddeSAl Viro 	uid_t uid;
104067d7dddeSAl Viro 	int status;
104167d7dddeSAl Viro 	int cause;
104267d7dddeSAl Viro };
104367d7dddeSAl Viro 
10449e8ae01dSOleg Nesterov struct wait_opts {
10459e8ae01dSOleg Nesterov 	enum pid_type		wo_type;
10469e8ae01dSOleg Nesterov 	int			wo_flags;
1047e1eb1ebcSRichard Kennedy 	struct pid		*wo_pid;
10489e8ae01dSOleg Nesterov 
104967d7dddeSAl Viro 	struct waitid_info	*wo_info;
1050359566faSAl Viro 	int			wo_stat;
1051ce72a16fSAl Viro 	struct rusage		*wo_rusage;
10529e8ae01dSOleg Nesterov 
1053ac6424b9SIngo Molnar 	wait_queue_entry_t		child_wait;
10549e8ae01dSOleg Nesterov 	int			notask_error;
10559e8ae01dSOleg Nesterov };
10569e8ae01dSOleg Nesterov 
eligible_pid(struct wait_opts * wo,struct task_struct * p)1057989264f4SOleg Nesterov static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
10581da177e4SLinus Torvalds {
10595c01ba49SOleg Nesterov 	return	wo->wo_type == PIDTYPE_MAX ||
10605c01ba49SOleg Nesterov 		task_pid_type(p, wo->wo_type) == wo->wo_pid;
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
1063bf959931SOleg Nesterov static int
eligible_child(struct wait_opts * wo,bool ptrace,struct task_struct * p)1064bf959931SOleg Nesterov eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
10655c01ba49SOleg Nesterov {
10665c01ba49SOleg Nesterov 	if (!eligible_pid(wo, p))
10675c01ba49SOleg Nesterov 		return 0;
1068bf959931SOleg Nesterov 
1069bf959931SOleg Nesterov 	/*
1070bf959931SOleg Nesterov 	 * Wait for all children (clone and not) if __WALL is set or
1071bf959931SOleg Nesterov 	 * if it is traced by us.
1072bf959931SOleg Nesterov 	 */
1073bf959931SOleg Nesterov 	if (ptrace || (wo->wo_flags & __WALL))
1074bf959931SOleg Nesterov 		return 1;
1075bf959931SOleg Nesterov 
1076bf959931SOleg Nesterov 	/*
1077bf959931SOleg Nesterov 	 * Otherwise, wait for clone children *only* if __WCLONE is set;
1078bf959931SOleg Nesterov 	 * otherwise, wait for non-clone children *only*.
1079bf959931SOleg Nesterov 	 *
1080bf959931SOleg Nesterov 	 * Note: a "clone" child here is one that reports to its parent
1081bf959931SOleg Nesterov 	 * using a signal other than SIGCHLD, or a non-leader thread which
1082bf959931SOleg Nesterov 	 * we can only see if it is traced by us.
1083bf959931SOleg Nesterov 	 */
1084bf959931SOleg Nesterov 	if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
10851da177e4SLinus Torvalds 		return 0;
10861da177e4SLinus Torvalds 
108714dd0b81SRoland McGrath 	return 1;
10881da177e4SLinus Torvalds }
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds /*
10911da177e4SLinus Torvalds  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
10921da177e4SLinus Torvalds  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
10931da177e4SLinus Torvalds  * the lock and this task is uninteresting.  If we return nonzero, we have
10941da177e4SLinus Torvalds  * released the lock and the system call should return.
10951da177e4SLinus Torvalds  */
wait_task_zombie(struct wait_opts * wo,struct task_struct * p)10969e8ae01dSOleg Nesterov static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
10971da177e4SLinus Torvalds {
109867d7dddeSAl Viro 	int state, status;
10996c5f3e7bSPavel Emelyanov 	pid_t pid = task_pid_vnr(p);
110043e13cc1SOleg Nesterov 	uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
110167d7dddeSAl Viro 	struct waitid_info *infop;
11021da177e4SLinus Torvalds 
11039e8ae01dSOleg Nesterov 	if (!likely(wo->wo_flags & WEXITED))
110498abed02SRoland McGrath 		return 0;
110598abed02SRoland McGrath 
11069e8ae01dSOleg Nesterov 	if (unlikely(wo->wo_flags & WNOWAIT)) {
1107907c311fSEric W. Biederman 		status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1108907c311fSEric W. Biederman 			? p->signal->group_exit_code : p->exit_code;
11091da177e4SLinus Torvalds 		get_task_struct(p);
11101da177e4SLinus Torvalds 		read_unlock(&tasklist_lock);
11111029a2b5SPeter Zijlstra 		sched_annotate_sleep();
1112e61a2502SAl Viro 		if (wo->wo_rusage)
1113e61a2502SAl Viro 			getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1114bb380ec3SAl Viro 		put_task_struct(p);
111576d9871eSAl Viro 		goto out_info;
11161da177e4SLinus Torvalds 	}
1117befca967SOleg Nesterov 	/*
1118abd50b39SOleg Nesterov 	 * Move the task's state to DEAD/TRACE, only one thread can do this.
1119abd50b39SOleg Nesterov 	 */
1120f6507f83SOleg Nesterov 	state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1121f6507f83SOleg Nesterov 		EXIT_TRACE : EXIT_DEAD;
1122abd50b39SOleg Nesterov 	if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1123abd50b39SOleg Nesterov 		return 0;
1124986094dfSOleg Nesterov 	/*
1125986094dfSOleg Nesterov 	 * We own this thread, nobody else can reap it.
1126986094dfSOleg Nesterov 	 */
1127986094dfSOleg Nesterov 	read_unlock(&tasklist_lock);
1128986094dfSOleg Nesterov 	sched_annotate_sleep();
1129f6507f83SOleg Nesterov 
1130abd50b39SOleg Nesterov 	/*
1131f6507f83SOleg Nesterov 	 * Check thread_group_leader() to exclude the traced sub-threads.
1132befca967SOleg Nesterov 	 */
1133f6507f83SOleg Nesterov 	if (state == EXIT_DEAD && thread_group_leader(p)) {
1134f953ccd0SOleg Nesterov 		struct signal_struct *sig = p->signal;
1135f953ccd0SOleg Nesterov 		struct signal_struct *psig = current->signal;
11361f10206cSJiri Pirko 		unsigned long maxrss;
11375613fda9SFrederic Weisbecker 		u64 tgutime, tgstime;
11383795e161SJesper Juhl 
11391da177e4SLinus Torvalds 		/*
11401da177e4SLinus Torvalds 		 * The resource counters for the group leader are in its
11411da177e4SLinus Torvalds 		 * own task_struct.  Those for dead threads in the group
11421da177e4SLinus Torvalds 		 * are in its signal_struct, as are those for the child
11431da177e4SLinus Torvalds 		 * processes it has previously reaped.  All these
11441da177e4SLinus Torvalds 		 * accumulate in the parent's signal_struct c* fields.
11451da177e4SLinus Torvalds 		 *
11461da177e4SLinus Torvalds 		 * We don't bother to take a lock here to protect these
1147f953ccd0SOleg Nesterov 		 * p->signal fields because the whole thread group is dead
1148f953ccd0SOleg Nesterov 		 * and nobody can change them.
1149f953ccd0SOleg Nesterov 		 *
1150dcca3475SIngo Molnar 		 * psig->stats_lock also protects us from our sub-threads
1151f953ccd0SOleg Nesterov 		 * which can reap other children at the same time. Until
1152f953ccd0SOleg Nesterov 		 * we change k_getrusage()-like users to rely on this lock
1153f953ccd0SOleg Nesterov 		 * we have to take ->siglock as well.
11540cf55e1eSHidetoshi Seto 		 *
1155a0be55deSIonut Alexa 		 * We use thread_group_cputime_adjusted() to get times for
1156a0be55deSIonut Alexa 		 * the thread group, which consolidates times for all threads
1157a0be55deSIonut Alexa 		 * in the group including the group leader.
11581da177e4SLinus Torvalds 		 */
1159e80d0a1aSFrederic Weisbecker 		thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1160f953ccd0SOleg Nesterov 		spin_lock_irq(&current->sighand->siglock);
1161e78c3496SRik van Riel 		write_seqlock(&psig->stats_lock);
116264861634SMartin Schwidefsky 		psig->cutime += tgutime + sig->cutime;
116364861634SMartin Schwidefsky 		psig->cstime += tgstime + sig->cstime;
11646fac4829SFrederic Weisbecker 		psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
11653795e161SJesper Juhl 		psig->cmin_flt +=
11663795e161SJesper Juhl 			p->min_flt + sig->min_flt + sig->cmin_flt;
11673795e161SJesper Juhl 		psig->cmaj_flt +=
11683795e161SJesper Juhl 			p->maj_flt + sig->maj_flt + sig->cmaj_flt;
11693795e161SJesper Juhl 		psig->cnvcsw +=
11703795e161SJesper Juhl 			p->nvcsw + sig->nvcsw + sig->cnvcsw;
11713795e161SJesper Juhl 		psig->cnivcsw +=
11723795e161SJesper Juhl 			p->nivcsw + sig->nivcsw + sig->cnivcsw;
11736eaeeabaSEric Dumazet 		psig->cinblock +=
11746eaeeabaSEric Dumazet 			task_io_get_inblock(p) +
11756eaeeabaSEric Dumazet 			sig->inblock + sig->cinblock;
11766eaeeabaSEric Dumazet 		psig->coublock +=
11776eaeeabaSEric Dumazet 			task_io_get_oublock(p) +
11786eaeeabaSEric Dumazet 			sig->oublock + sig->coublock;
11791f10206cSJiri Pirko 		maxrss = max(sig->maxrss, sig->cmaxrss);
11801f10206cSJiri Pirko 		if (psig->cmaxrss < maxrss)
11811f10206cSJiri Pirko 			psig->cmaxrss = maxrss;
11825995477aSAndrea Righi 		task_io_accounting_add(&psig->ioac, &p->ioac);
11835995477aSAndrea Righi 		task_io_accounting_add(&psig->ioac, &sig->ioac);
1184e78c3496SRik van Riel 		write_sequnlock(&psig->stats_lock);
1185f953ccd0SOleg Nesterov 		spin_unlock_irq(&current->sighand->siglock);
11861da177e4SLinus Torvalds 	}
11871da177e4SLinus Torvalds 
1188ce72a16fSAl Viro 	if (wo->wo_rusage)
1189ce72a16fSAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
11901da177e4SLinus Torvalds 	status = (p->signal->flags & SIGNAL_GROUP_EXIT)
11911da177e4SLinus Torvalds 		? p->signal->group_exit_code : p->exit_code;
1192359566faSAl Viro 	wo->wo_stat = status;
11932f4e6e2aSOleg Nesterov 
1194b4360690SOleg Nesterov 	if (state == EXIT_TRACE) {
11951da177e4SLinus Torvalds 		write_lock_irq(&tasklist_lock);
11962f4e6e2aSOleg Nesterov 		/* We dropped tasklist, ptracer could die and untrace */
11972f4e6e2aSOleg Nesterov 		ptrace_unlink(p);
1198b4360690SOleg Nesterov 
1199b4360690SOleg Nesterov 		/* If parent wants a zombie, don't release it now */
1200abd50b39SOleg Nesterov 		state = EXIT_ZOMBIE;
1201b4360690SOleg Nesterov 		if (do_notify_parent(p, p->exit_signal))
1202b4360690SOleg Nesterov 			state = EXIT_DEAD;
1203abd50b39SOleg Nesterov 		p->exit_state = state;
12041da177e4SLinus Torvalds 		write_unlock_irq(&tasklist_lock);
12051da177e4SLinus Torvalds 	}
1206abd50b39SOleg Nesterov 	if (state == EXIT_DEAD)
12071da177e4SLinus Torvalds 		release_task(p);
12082f4e6e2aSOleg Nesterov 
120976d9871eSAl Viro out_info:
121076d9871eSAl Viro 	infop = wo->wo_info;
121176d9871eSAl Viro 	if (infop) {
121276d9871eSAl Viro 		if ((status & 0x7f) == 0) {
121376d9871eSAl Viro 			infop->cause = CLD_EXITED;
121476d9871eSAl Viro 			infop->status = status >> 8;
121576d9871eSAl Viro 		} else {
121676d9871eSAl Viro 			infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
121776d9871eSAl Viro 			infop->status = status & 0x7f;
121876d9871eSAl Viro 		}
121976d9871eSAl Viro 		infop->pid = pid;
122076d9871eSAl Viro 		infop->uid = uid;
122176d9871eSAl Viro 	}
122276d9871eSAl Viro 
122367d7dddeSAl Viro 	return pid;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
task_stopped_code(struct task_struct * p,bool ptrace)122690bc8d8bSOleg Nesterov static int *task_stopped_code(struct task_struct *p, bool ptrace)
122790bc8d8bSOleg Nesterov {
122890bc8d8bSOleg Nesterov 	if (ptrace) {
1229570ac933SOleg Nesterov 		if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
123090bc8d8bSOleg Nesterov 			return &p->exit_code;
123190bc8d8bSOleg Nesterov 	} else {
123290bc8d8bSOleg Nesterov 		if (p->signal->flags & SIGNAL_STOP_STOPPED)
123390bc8d8bSOleg Nesterov 			return &p->signal->group_exit_code;
123490bc8d8bSOleg Nesterov 	}
123590bc8d8bSOleg Nesterov 	return NULL;
123690bc8d8bSOleg Nesterov }
123790bc8d8bSOleg Nesterov 
123819e27463STejun Heo /**
123919e27463STejun Heo  * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
124019e27463STejun Heo  * @wo: wait options
124119e27463STejun Heo  * @ptrace: is the wait for ptrace
124219e27463STejun Heo  * @p: task to wait for
124319e27463STejun Heo  *
124419e27463STejun Heo  * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
124519e27463STejun Heo  *
124619e27463STejun Heo  * CONTEXT:
124719e27463STejun Heo  * read_lock(&tasklist_lock), which is released if return value is
124819e27463STejun Heo  * non-zero.  Also, grabs and releases @p->sighand->siglock.
124919e27463STejun Heo  *
125019e27463STejun Heo  * RETURNS:
125119e27463STejun Heo  * 0 if wait condition didn't exist and search for other wait conditions
125219e27463STejun Heo  * should continue.  Non-zero return, -errno on failure and @p's pid on
125319e27463STejun Heo  * success, implies that tasklist_lock is released and wait condition
125419e27463STejun Heo  * search should terminate.
12551da177e4SLinus Torvalds  */
wait_task_stopped(struct wait_opts * wo,int ptrace,struct task_struct * p)12569e8ae01dSOleg Nesterov static int wait_task_stopped(struct wait_opts *wo,
12579e8ae01dSOleg Nesterov 				int ptrace, struct task_struct *p)
12581da177e4SLinus Torvalds {
125967d7dddeSAl Viro 	struct waitid_info *infop;
126067d7dddeSAl Viro 	int exit_code, *p_code, why;
1261ee7c82daSOleg Nesterov 	uid_t uid = 0; /* unneeded, required by compiler */
1262c8950783SOleg Nesterov 	pid_t pid;
12631da177e4SLinus Torvalds 
126447918025SOleg Nesterov 	/*
126547918025SOleg Nesterov 	 * Traditionally we see ptrace'd stopped tasks regardless of options.
126647918025SOleg Nesterov 	 */
12679e8ae01dSOleg Nesterov 	if (!ptrace && !(wo->wo_flags & WUNTRACED))
126898abed02SRoland McGrath 		return 0;
126998abed02SRoland McGrath 
127019e27463STejun Heo 	if (!task_stopped_code(p, ptrace))
127119e27463STejun Heo 		return 0;
127219e27463STejun Heo 
1273ee7c82daSOleg Nesterov 	exit_code = 0;
1274ee7c82daSOleg Nesterov 	spin_lock_irq(&p->sighand->siglock);
1275ee7c82daSOleg Nesterov 
127690bc8d8bSOleg Nesterov 	p_code = task_stopped_code(p, ptrace);
127790bc8d8bSOleg Nesterov 	if (unlikely(!p_code))
1278ee7c82daSOleg Nesterov 		goto unlock_sig;
1279ee7c82daSOleg Nesterov 
128090bc8d8bSOleg Nesterov 	exit_code = *p_code;
1281ee7c82daSOleg Nesterov 	if (!exit_code)
1282ee7c82daSOleg Nesterov 		goto unlock_sig;
1283ee7c82daSOleg Nesterov 
12849e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WNOWAIT))
128590bc8d8bSOleg Nesterov 		*p_code = 0;
1286ee7c82daSOleg Nesterov 
12878ca937a6SSasha Levin 	uid = from_kuid_munged(current_user_ns(), task_uid(p));
1288ee7c82daSOleg Nesterov unlock_sig:
1289ee7c82daSOleg Nesterov 	spin_unlock_irq(&p->sighand->siglock);
1290ee7c82daSOleg Nesterov 	if (!exit_code)
12911da177e4SLinus Torvalds 		return 0;
12921da177e4SLinus Torvalds 
12931da177e4SLinus Torvalds 	/*
12941da177e4SLinus Torvalds 	 * Now we are pretty sure this task is interesting.
12951da177e4SLinus Torvalds 	 * Make sure it doesn't get reaped out from under us while we
12961da177e4SLinus Torvalds 	 * give up the lock and then examine it below.  We don't want to
12971da177e4SLinus Torvalds 	 * keep holding onto the tasklist_lock while we call getrusage and
12981da177e4SLinus Torvalds 	 * possibly take page faults for user memory.
12991da177e4SLinus Torvalds 	 */
13001da177e4SLinus Torvalds 	get_task_struct(p);
13016c5f3e7bSPavel Emelyanov 	pid = task_pid_vnr(p);
1302f470021aSRoland McGrath 	why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
13031da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
13041029a2b5SPeter Zijlstra 	sched_annotate_sleep();
1305e61a2502SAl Viro 	if (wo->wo_rusage)
1306e61a2502SAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
13071da177e4SLinus Torvalds 	put_task_struct(p);
13081da177e4SLinus Torvalds 
1309bb380ec3SAl Viro 	if (likely(!(wo->wo_flags & WNOWAIT)))
1310359566faSAl Viro 		wo->wo_stat = (exit_code << 8) | 0x7f;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 	infop = wo->wo_info;
131367d7dddeSAl Viro 	if (infop) {
131467d7dddeSAl Viro 		infop->cause = why;
131567d7dddeSAl Viro 		infop->status = exit_code;
131667d7dddeSAl Viro 		infop->pid = pid;
131767d7dddeSAl Viro 		infop->uid = uid;
131867d7dddeSAl Viro 	}
131967d7dddeSAl Viro 	return pid;
13201da177e4SLinus Torvalds }
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds /*
13231da177e4SLinus Torvalds  * Handle do_wait work for one task in a live, non-stopped state.
13241da177e4SLinus Torvalds  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
13251da177e4SLinus Torvalds  * the lock and this task is uninteresting.  If we return nonzero, we have
13261da177e4SLinus Torvalds  * released the lock and the system call should return.
13271da177e4SLinus Torvalds  */
wait_task_continued(struct wait_opts * wo,struct task_struct * p)13289e8ae01dSOleg Nesterov static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
13291da177e4SLinus Torvalds {
1330bb380ec3SAl Viro 	struct waitid_info *infop;
13311da177e4SLinus Torvalds 	pid_t pid;
13321da177e4SLinus Torvalds 	uid_t uid;
13331da177e4SLinus Torvalds 
13349e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WCONTINUED))
133598abed02SRoland McGrath 		return 0;
133698abed02SRoland McGrath 
13371da177e4SLinus Torvalds 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
13381da177e4SLinus Torvalds 		return 0;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	spin_lock_irq(&p->sighand->siglock);
13411da177e4SLinus Torvalds 	/* Re-check with the lock held.  */
13421da177e4SLinus Torvalds 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
13431da177e4SLinus Torvalds 		spin_unlock_irq(&p->sighand->siglock);
13441da177e4SLinus Torvalds 		return 0;
13451da177e4SLinus Torvalds 	}
13469e8ae01dSOleg Nesterov 	if (!unlikely(wo->wo_flags & WNOWAIT))
13471da177e4SLinus Torvalds 		p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
13488ca937a6SSasha Levin 	uid = from_kuid_munged(current_user_ns(), task_uid(p));
13491da177e4SLinus Torvalds 	spin_unlock_irq(&p->sighand->siglock);
13501da177e4SLinus Torvalds 
13516c5f3e7bSPavel Emelyanov 	pid = task_pid_vnr(p);
13521da177e4SLinus Torvalds 	get_task_struct(p);
13531da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
13541029a2b5SPeter Zijlstra 	sched_annotate_sleep();
1355ce72a16fSAl Viro 	if (wo->wo_rusage)
1356ce72a16fSAl Viro 		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
13571da177e4SLinus Torvalds 	put_task_struct(p);
13581da177e4SLinus Torvalds 
1359bb380ec3SAl Viro 	infop = wo->wo_info;
1360bb380ec3SAl Viro 	if (!infop) {
1361bb380ec3SAl Viro 		wo->wo_stat = 0xffff;
1362bb380ec3SAl Viro 	} else {
1363bb380ec3SAl Viro 		infop->cause = CLD_CONTINUED;
1364bb380ec3SAl Viro 		infop->pid = pid;
1365bb380ec3SAl Viro 		infop->uid = uid;
1366bb380ec3SAl Viro 		infop->status = SIGCONT;
1367bb380ec3SAl Viro 	}
1368bb380ec3SAl Viro 	return pid;
13691da177e4SLinus Torvalds }
13701da177e4SLinus Torvalds 
137198abed02SRoland McGrath /*
137298abed02SRoland McGrath  * Consider @p for a wait by @parent.
137398abed02SRoland McGrath  *
13749e8ae01dSOleg Nesterov  * -ECHILD should be in ->notask_error before the first call.
137598abed02SRoland McGrath  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
137698abed02SRoland McGrath  * Returns zero if the search for a child should continue;
13779e8ae01dSOleg Nesterov  * then ->notask_error is 0 if @p is an eligible child,
13783a2f5a59SStephen Smalley  * or still -ECHILD.
137998abed02SRoland McGrath  */
wait_consider_task(struct wait_opts * wo,int ptrace,struct task_struct * p)1380b6e763f0SOleg Nesterov static int wait_consider_task(struct wait_opts *wo, int ptrace,
1381b6e763f0SOleg Nesterov 				struct task_struct *p)
138298abed02SRoland McGrath {
13833245d6acSOleg Nesterov 	/*
13843245d6acSOleg Nesterov 	 * We can race with wait_task_zombie() from another thread.
13853245d6acSOleg Nesterov 	 * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
13863245d6acSOleg Nesterov 	 * can't confuse the checks below.
13873245d6acSOleg Nesterov 	 */
13886aa7de05SMark Rutland 	int exit_state = READ_ONCE(p->exit_state);
1389b3ab0316SOleg Nesterov 	int ret;
1390b3ab0316SOleg Nesterov 
13913245d6acSOleg Nesterov 	if (unlikely(exit_state == EXIT_DEAD))
1392b3ab0316SOleg Nesterov 		return 0;
1393b3ab0316SOleg Nesterov 
1394bf959931SOleg Nesterov 	ret = eligible_child(wo, ptrace, p);
139514dd0b81SRoland McGrath 	if (!ret)
139698abed02SRoland McGrath 		return ret;
139798abed02SRoland McGrath 
13983245d6acSOleg Nesterov 	if (unlikely(exit_state == EXIT_TRACE)) {
139950b8d257SOleg Nesterov 		/*
1400abd50b39SOleg Nesterov 		 * ptrace == 0 means we are the natural parent. In this case
1401abd50b39SOleg Nesterov 		 * we should clear notask_error, debugger will notify us.
140250b8d257SOleg Nesterov 		 */
1403abd50b39SOleg Nesterov 		if (likely(!ptrace))
140450b8d257SOleg Nesterov 			wo->notask_error = 0;
1405823b018eSTejun Heo 		return 0;
140650b8d257SOleg Nesterov 	}
1407823b018eSTejun Heo 
1408377d75daSOleg Nesterov 	if (likely(!ptrace) && unlikely(p->ptrace)) {
1409377d75daSOleg Nesterov 		/*
1410377d75daSOleg Nesterov 		 * If it is traced by its real parent's group, just pretend
1411377d75daSOleg Nesterov 		 * the caller is ptrace_do_wait() and reap this child if it
1412377d75daSOleg Nesterov 		 * is zombie.
1413377d75daSOleg Nesterov 		 *
1414377d75daSOleg Nesterov 		 * This also hides group stop state from real parent; otherwise
1415377d75daSOleg Nesterov 		 * a single stop can be reported twice as group and ptrace stop.
1416377d75daSOleg Nesterov 		 * If a ptracer wants to distinguish these two events for its
1417377d75daSOleg Nesterov 		 * own children it should create a separate process which takes
1418377d75daSOleg Nesterov 		 * the role of real parent.
1419377d75daSOleg Nesterov 		 */
1420377d75daSOleg Nesterov 		if (!ptrace_reparented(p))
1421377d75daSOleg Nesterov 			ptrace = 1;
1422377d75daSOleg Nesterov 	}
1423377d75daSOleg Nesterov 
142445cb24a1STejun Heo 	/* slay zombie? */
14253245d6acSOleg Nesterov 	if (exit_state == EXIT_ZOMBIE) {
14267c733eb3SOleg Nesterov 		/* we don't reap group leaders with subthreads */
14277c733eb3SOleg Nesterov 		if (!delay_group_leader(p)) {
1428f470021aSRoland McGrath 			/*
142945cb24a1STejun Heo 			 * A zombie ptracee is only visible to its ptracer.
14307c733eb3SOleg Nesterov 			 * Notification and reaping will be cascaded to the
14317c733eb3SOleg Nesterov 			 * real parent when the ptracer detaches.
1432f470021aSRoland McGrath 			 */
14337c733eb3SOleg Nesterov 			if (unlikely(ptrace) || likely(!p->ptrace))
14349e8ae01dSOleg Nesterov 				return wait_task_zombie(wo, p);
14357c733eb3SOleg Nesterov 		}
143698abed02SRoland McGrath 
143798abed02SRoland McGrath 		/*
14389b84cca2STejun Heo 		 * Allow access to stopped/continued state via zombie by
14399b84cca2STejun Heo 		 * falling through.  Clearing of notask_error is complex.
14409b84cca2STejun Heo 		 *
14419b84cca2STejun Heo 		 * When !@ptrace:
14429b84cca2STejun Heo 		 *
14439b84cca2STejun Heo 		 * If WEXITED is set, notask_error should naturally be
14449b84cca2STejun Heo 		 * cleared.  If not, subset of WSTOPPED|WCONTINUED is set,
14459b84cca2STejun Heo 		 * so, if there are live subthreads, there are events to
14469b84cca2STejun Heo 		 * wait for.  If all subthreads are dead, it's still safe
14479b84cca2STejun Heo 		 * to clear - this function will be called again in finite
14489b84cca2STejun Heo 		 * amount time once all the subthreads are released and
14499b84cca2STejun Heo 		 * will then return without clearing.
14509b84cca2STejun Heo 		 *
14519b84cca2STejun Heo 		 * When @ptrace:
14529b84cca2STejun Heo 		 *
14539b84cca2STejun Heo 		 * Stopped state is per-task and thus can't change once the
14549b84cca2STejun Heo 		 * target task dies.  Only continued and exited can happen.
14559b84cca2STejun Heo 		 * Clear notask_error if WCONTINUED | WEXITED.
14569b84cca2STejun Heo 		 */
14579b84cca2STejun Heo 		if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
14589b84cca2STejun Heo 			wo->notask_error = 0;
14599b84cca2STejun Heo 	} else {
14609b84cca2STejun Heo 		/*
14619b84cca2STejun Heo 		 * @p is alive and it's gonna stop, continue or exit, so
14629b84cca2STejun Heo 		 * there always is something to wait for.
146398abed02SRoland McGrath 		 */
14649e8ae01dSOleg Nesterov 		wo->notask_error = 0;
14659b84cca2STejun Heo 	}
146698abed02SRoland McGrath 
146745cb24a1STejun Heo 	/*
146845cb24a1STejun Heo 	 * Wait for stopped.  Depending on @ptrace, different stopped state
146945cb24a1STejun Heo 	 * is used and the two don't interact with each other.
147045cb24a1STejun Heo 	 */
147119e27463STejun Heo 	ret = wait_task_stopped(wo, ptrace, p);
147219e27463STejun Heo 	if (ret)
147319e27463STejun Heo 		return ret;
147498abed02SRoland McGrath 
147545cb24a1STejun Heo 	/*
147645cb24a1STejun Heo 	 * Wait for continued.  There's only one continued state and the
147745cb24a1STejun Heo 	 * ptracer can consume it which can confuse the real parent.  Don't
147845cb24a1STejun Heo 	 * use WCONTINUED from ptracer.  You don't need or want it.
147945cb24a1STejun Heo 	 */
14809e8ae01dSOleg Nesterov 	return wait_task_continued(wo, p);
148198abed02SRoland McGrath }
148298abed02SRoland McGrath 
148398abed02SRoland McGrath /*
148498abed02SRoland McGrath  * Do the work of do_wait() for one thread in the group, @tsk.
148598abed02SRoland McGrath  *
14869e8ae01dSOleg Nesterov  * -ECHILD should be in ->notask_error before the first call.
148798abed02SRoland McGrath  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
148898abed02SRoland McGrath  * Returns zero if the search for a child should continue; then
14899e8ae01dSOleg Nesterov  * ->notask_error is 0 if there were any eligible children,
14903a2f5a59SStephen Smalley  * or still -ECHILD.
149198abed02SRoland McGrath  */
do_wait_thread(struct wait_opts * wo,struct task_struct * tsk)14929e8ae01dSOleg Nesterov static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
149398abed02SRoland McGrath {
149498abed02SRoland McGrath 	struct task_struct *p;
149598abed02SRoland McGrath 
149698abed02SRoland McGrath 	list_for_each_entry(p, &tsk->children, sibling) {
1497b6e763f0SOleg Nesterov 		int ret = wait_consider_task(wo, 0, p);
1498a0be55deSIonut Alexa 
149998abed02SRoland McGrath 		if (ret)
150098abed02SRoland McGrath 			return ret;
150198abed02SRoland McGrath 	}
150298abed02SRoland McGrath 
150398abed02SRoland McGrath 	return 0;
150498abed02SRoland McGrath }
150598abed02SRoland McGrath 
ptrace_do_wait(struct wait_opts * wo,struct task_struct * tsk)15069e8ae01dSOleg Nesterov static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
150798abed02SRoland McGrath {
150898abed02SRoland McGrath 	struct task_struct *p;
150998abed02SRoland McGrath 
1510f470021aSRoland McGrath 	list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1511b6e763f0SOleg Nesterov 		int ret = wait_consider_task(wo, 1, p);
1512a0be55deSIonut Alexa 
1513f470021aSRoland McGrath 		if (ret)
151498abed02SRoland McGrath 			return ret;
151598abed02SRoland McGrath 	}
151698abed02SRoland McGrath 
151798abed02SRoland McGrath 	return 0;
151898abed02SRoland McGrath }
151998abed02SRoland McGrath 
child_wait_callback(wait_queue_entry_t * wait,unsigned mode,int sync,void * key)1520ac6424b9SIngo Molnar static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
15210b7570e7SOleg Nesterov 				int sync, void *key)
15220b7570e7SOleg Nesterov {
15230b7570e7SOleg Nesterov 	struct wait_opts *wo = container_of(wait, struct wait_opts,
15240b7570e7SOleg Nesterov 						child_wait);
15250b7570e7SOleg Nesterov 	struct task_struct *p = key;
15260b7570e7SOleg Nesterov 
15275c01ba49SOleg Nesterov 	if (!eligible_pid(wo, p))
15280b7570e7SOleg Nesterov 		return 0;
15290b7570e7SOleg Nesterov 
1530b4fe5182SOleg Nesterov 	if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
1531b4fe5182SOleg Nesterov 		return 0;
1532b4fe5182SOleg Nesterov 
15330b7570e7SOleg Nesterov 	return default_wake_function(wait, mode, sync, key);
15340b7570e7SOleg Nesterov }
15350b7570e7SOleg Nesterov 
__wake_up_parent(struct task_struct * p,struct task_struct * parent)1536a7f0765eSOleg Nesterov void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1537a7f0765eSOleg Nesterov {
15380b7570e7SOleg Nesterov 	__wake_up_sync_key(&parent->signal->wait_chldexit,
1539ce4dd442SDavid Howells 			   TASK_INTERRUPTIBLE, p);
1540a7f0765eSOleg Nesterov }
1541a7f0765eSOleg Nesterov 
is_effectively_child(struct wait_opts * wo,bool ptrace,struct task_struct * target)15425449162aSJim Newsome static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
15435449162aSJim Newsome 				 struct task_struct *target)
15445449162aSJim Newsome {
15455449162aSJim Newsome 	struct task_struct *parent =
15465449162aSJim Newsome 		!ptrace ? target->real_parent : target->parent;
15475449162aSJim Newsome 
15485449162aSJim Newsome 	return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
15495449162aSJim Newsome 				     same_thread_group(current, parent));
15505449162aSJim Newsome }
15515449162aSJim Newsome 
15525449162aSJim Newsome /*
15535449162aSJim Newsome  * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
15545449162aSJim Newsome  * and tracee lists to find the target task.
15555449162aSJim Newsome  */
do_wait_pid(struct wait_opts * wo)15565449162aSJim Newsome static int do_wait_pid(struct wait_opts *wo)
15575449162aSJim Newsome {
15585449162aSJim Newsome 	bool ptrace;
15595449162aSJim Newsome 	struct task_struct *target;
15605449162aSJim Newsome 	int retval;
15615449162aSJim Newsome 
15625449162aSJim Newsome 	ptrace = false;
15635449162aSJim Newsome 	target = pid_task(wo->wo_pid, PIDTYPE_TGID);
15645449162aSJim Newsome 	if (target && is_effectively_child(wo, ptrace, target)) {
15655449162aSJim Newsome 		retval = wait_consider_task(wo, ptrace, target);
15665449162aSJim Newsome 		if (retval)
15675449162aSJim Newsome 			return retval;
15685449162aSJim Newsome 	}
15695449162aSJim Newsome 
15705449162aSJim Newsome 	ptrace = true;
15715449162aSJim Newsome 	target = pid_task(wo->wo_pid, PIDTYPE_PID);
15725449162aSJim Newsome 	if (target && target->ptrace &&
15735449162aSJim Newsome 	    is_effectively_child(wo, ptrace, target)) {
15745449162aSJim Newsome 		retval = wait_consider_task(wo, ptrace, target);
15755449162aSJim Newsome 		if (retval)
15765449162aSJim Newsome 			return retval;
15775449162aSJim Newsome 	}
15785449162aSJim Newsome 
15795449162aSJim Newsome 	return 0;
15805449162aSJim Newsome }
15815449162aSJim Newsome 
do_wait(struct wait_opts * wo)15829e8ae01dSOleg Nesterov static long do_wait(struct wait_opts *wo)
15831da177e4SLinus Torvalds {
158498abed02SRoland McGrath 	int retval;
15851da177e4SLinus Torvalds 
15869e8ae01dSOleg Nesterov 	trace_sched_process_wait(wo->wo_pid);
15870a16b607SMathieu Desnoyers 
15880b7570e7SOleg Nesterov 	init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
15890b7570e7SOleg Nesterov 	wo->child_wait.private = current;
15900b7570e7SOleg Nesterov 	add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
15911da177e4SLinus Torvalds repeat:
159298abed02SRoland McGrath 	/*
15933da56d16SFrans Klaver 	 * If there is nothing that can match our criteria, just get out.
15949e8ae01dSOleg Nesterov 	 * We will clear ->notask_error to zero if we see any child that
15959e8ae01dSOleg Nesterov 	 * might later match our criteria, even if we are not able to reap
15969e8ae01dSOleg Nesterov 	 * it yet.
159798abed02SRoland McGrath 	 */
159864a16cafSOleg Nesterov 	wo->notask_error = -ECHILD;
15999e8ae01dSOleg Nesterov 	if ((wo->wo_type < PIDTYPE_MAX) &&
16001722c14aSChristian Brauner 	   (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
160164a16cafSOleg Nesterov 		goto notask;
1602161550d7SEric W. Biederman 
1603f95d39d1SOleg Nesterov 	set_current_state(TASK_INTERRUPTIBLE);
16041da177e4SLinus Torvalds 	read_lock(&tasklist_lock);
16055449162aSJim Newsome 
16065449162aSJim Newsome 	if (wo->wo_type == PIDTYPE_PID) {
16075449162aSJim Newsome 		retval = do_wait_pid(wo);
16085449162aSJim Newsome 		if (retval)
16095449162aSJim Newsome 			goto end;
16105449162aSJim Newsome 	} else {
16115449162aSJim Newsome 		struct task_struct *tsk = current;
16125449162aSJim Newsome 
16131da177e4SLinus Torvalds 		do {
161464a16cafSOleg Nesterov 			retval = do_wait_thread(wo, tsk);
161564a16cafSOleg Nesterov 			if (retval)
16169cbab810SOleg Nesterov 				goto end;
161764a16cafSOleg Nesterov 
161864a16cafSOleg Nesterov 			retval = ptrace_do_wait(wo, tsk);
161964a16cafSOleg Nesterov 			if (retval)
162064a16cafSOleg Nesterov 				goto end;
162198abed02SRoland McGrath 
16229e8ae01dSOleg Nesterov 			if (wo->wo_flags & __WNOTHREAD)
16231da177e4SLinus Torvalds 				break;
1624a3f6dfb7SOleg Nesterov 		} while_each_thread(current, tsk);
16255449162aSJim Newsome 	}
16261da177e4SLinus Torvalds 	read_unlock(&tasklist_lock);
1627f2cc3eb1SOleg Nesterov 
162864a16cafSOleg Nesterov notask:
16299e8ae01dSOleg Nesterov 	retval = wo->notask_error;
16309e8ae01dSOleg Nesterov 	if (!retval && !(wo->wo_flags & WNOHANG)) {
16311da177e4SLinus Torvalds 		retval = -ERESTARTSYS;
163298abed02SRoland McGrath 		if (!signal_pending(current)) {
16331da177e4SLinus Torvalds 			schedule();
16341da177e4SLinus Torvalds 			goto repeat;
16351da177e4SLinus Torvalds 		}
163698abed02SRoland McGrath 	}
16371da177e4SLinus Torvalds end:
1638f95d39d1SOleg Nesterov 	__set_current_state(TASK_RUNNING);
16390b7570e7SOleg Nesterov 	remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
16401da177e4SLinus Torvalds 	return retval;
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
kernel_waitid(int which,pid_t upid,struct waitid_info * infop,int options,struct rusage * ru)164367d7dddeSAl Viro static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
1644ce72a16fSAl Viro 			  int options, struct rusage *ru)
16451da177e4SLinus Torvalds {
16469e8ae01dSOleg Nesterov 	struct wait_opts wo;
1647161550d7SEric W. Biederman 	struct pid *pid = NULL;
1648161550d7SEric W. Biederman 	enum pid_type type;
16491da177e4SLinus Torvalds 	long ret;
1650ba7d25f3SChristian Brauner 	unsigned int f_flags = 0;
16511da177e4SLinus Torvalds 
165291c4e8eaSOleg Nesterov 	if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
165391c4e8eaSOleg Nesterov 			__WNOTHREAD|__WCLONE|__WALL))
16541da177e4SLinus Torvalds 		return -EINVAL;
16551da177e4SLinus Torvalds 	if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
16561da177e4SLinus Torvalds 		return -EINVAL;
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 	switch (which) {
16591da177e4SLinus Torvalds 	case P_ALL:
1660161550d7SEric W. Biederman 		type = PIDTYPE_MAX;
16611da177e4SLinus Torvalds 		break;
16621da177e4SLinus Torvalds 	case P_PID:
1663161550d7SEric W. Biederman 		type = PIDTYPE_PID;
1664161550d7SEric W. Biederman 		if (upid <= 0)
16651da177e4SLinus Torvalds 			return -EINVAL;
16663695eae5SChristian Brauner 
16673695eae5SChristian Brauner 		pid = find_get_pid(upid);
16681da177e4SLinus Torvalds 		break;
16691da177e4SLinus Torvalds 	case P_PGID:
1670161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
1671821cc7b0SEric W. Biederman 		if (upid < 0)
16721da177e4SLinus Torvalds 			return -EINVAL;
16733695eae5SChristian Brauner 
1674821cc7b0SEric W. Biederman 		if (upid)
16753695eae5SChristian Brauner 			pid = find_get_pid(upid);
1676821cc7b0SEric W. Biederman 		else
1677821cc7b0SEric W. Biederman 			pid = get_task_pid(current, PIDTYPE_PGID);
16783695eae5SChristian Brauner 		break;
16793695eae5SChristian Brauner 	case P_PIDFD:
16803695eae5SChristian Brauner 		type = PIDTYPE_PID;
16813695eae5SChristian Brauner 		if (upid < 0)
16823695eae5SChristian Brauner 			return -EINVAL;
16833695eae5SChristian Brauner 
1684ba7d25f3SChristian Brauner 		pid = pidfd_get_pid(upid, &f_flags);
16853695eae5SChristian Brauner 		if (IS_ERR(pid))
16863695eae5SChristian Brauner 			return PTR_ERR(pid);
1687ba7d25f3SChristian Brauner 
16881da177e4SLinus Torvalds 		break;
16891da177e4SLinus Torvalds 	default:
16901da177e4SLinus Torvalds 		return -EINVAL;
16911da177e4SLinus Torvalds 	}
16921da177e4SLinus Torvalds 
16939e8ae01dSOleg Nesterov 	wo.wo_type	= type;
16949e8ae01dSOleg Nesterov 	wo.wo_pid	= pid;
16959e8ae01dSOleg Nesterov 	wo.wo_flags	= options;
16969e8ae01dSOleg Nesterov 	wo.wo_info	= infop;
16979e8ae01dSOleg Nesterov 	wo.wo_rusage	= ru;
1698ba7d25f3SChristian Brauner 	if (f_flags & O_NONBLOCK)
1699ba7d25f3SChristian Brauner 		wo.wo_flags |= WNOHANG;
1700ba7d25f3SChristian Brauner 
17019e8ae01dSOleg Nesterov 	ret = do_wait(&wo);
1702ba7d25f3SChristian Brauner 	if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK))
1703ba7d25f3SChristian Brauner 		ret = -EAGAIN;
1704dfe16dfaSVitaly Mayatskikh 
1705161550d7SEric W. Biederman 	put_pid(pid);
17061da177e4SLinus Torvalds 	return ret;
17071da177e4SLinus Torvalds }
17081da177e4SLinus Torvalds 
SYSCALL_DEFINE5(waitid,int,which,pid_t,upid,struct siginfo __user *,infop,int,options,struct rusage __user *,ru)1709ce72a16fSAl Viro SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1710ce72a16fSAl Viro 		infop, int, options, struct rusage __user *, ru)
1711ce72a16fSAl Viro {
1712ce72a16fSAl Viro 	struct rusage r;
171367d7dddeSAl Viro 	struct waitid_info info = {.status = 0};
171467d7dddeSAl Viro 	long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
1715634a8160SAl Viro 	int signo = 0;
17166c85501fSAl Viro 
1717634a8160SAl Viro 	if (err > 0) {
1718634a8160SAl Viro 		signo = SIGCHLD;
1719634a8160SAl Viro 		err = 0;
1720ce72a16fSAl Viro 		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1721ce72a16fSAl Viro 			return -EFAULT;
1722ce72a16fSAl Viro 	}
172367d7dddeSAl Viro 	if (!infop)
172467d7dddeSAl Viro 		return err;
172567d7dddeSAl Viro 
172641cd7805SChristophe Leroy 	if (!user_write_access_begin(infop, sizeof(*infop)))
17271c9fec47SKees Cook 		return -EFAULT;
172896ca579aSKees Cook 
1729634a8160SAl Viro 	unsafe_put_user(signo, &infop->si_signo, Efault);
17304c48abe9SAl Viro 	unsafe_put_user(0, &infop->si_errno, Efault);
1731cc731525SEric W. Biederman 	unsafe_put_user(info.cause, &infop->si_code, Efault);
17324c48abe9SAl Viro 	unsafe_put_user(info.pid, &infop->si_pid, Efault);
17334c48abe9SAl Viro 	unsafe_put_user(info.uid, &infop->si_uid, Efault);
17344c48abe9SAl Viro 	unsafe_put_user(info.status, &infop->si_status, Efault);
173541cd7805SChristophe Leroy 	user_write_access_end();
1736ce72a16fSAl Viro 	return err;
17374c48abe9SAl Viro Efault:
173841cd7805SChristophe Leroy 	user_write_access_end();
17394c48abe9SAl Viro 	return -EFAULT;
1740ce72a16fSAl Viro }
1741ce72a16fSAl Viro 
kernel_wait4(pid_t upid,int __user * stat_addr,int options,struct rusage * ru)174292ebce5aSAl Viro long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
174392ebce5aSAl Viro 		  struct rusage *ru)
17441da177e4SLinus Torvalds {
17459e8ae01dSOleg Nesterov 	struct wait_opts wo;
1746161550d7SEric W. Biederman 	struct pid *pid = NULL;
1747161550d7SEric W. Biederman 	enum pid_type type;
17481da177e4SLinus Torvalds 	long ret;
17491da177e4SLinus Torvalds 
17501da177e4SLinus Torvalds 	if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
17511da177e4SLinus Torvalds 			__WNOTHREAD|__WCLONE|__WALL))
17521da177e4SLinus Torvalds 		return -EINVAL;
1753161550d7SEric W. Biederman 
1754dd83c161Szhongjiang 	/* -INT_MIN is not defined */
1755dd83c161Szhongjiang 	if (upid == INT_MIN)
1756dd83c161Szhongjiang 		return -ESRCH;
1757dd83c161Szhongjiang 
1758161550d7SEric W. Biederman 	if (upid == -1)
1759161550d7SEric W. Biederman 		type = PIDTYPE_MAX;
1760161550d7SEric W. Biederman 	else if (upid < 0) {
1761161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
1762161550d7SEric W. Biederman 		pid = find_get_pid(-upid);
1763161550d7SEric W. Biederman 	} else if (upid == 0) {
1764161550d7SEric W. Biederman 		type = PIDTYPE_PGID;
17652ae448efSOleg Nesterov 		pid = get_task_pid(current, PIDTYPE_PGID);
1766161550d7SEric W. Biederman 	} else /* upid > 0 */ {
1767161550d7SEric W. Biederman 		type = PIDTYPE_PID;
1768161550d7SEric W. Biederman 		pid = find_get_pid(upid);
1769161550d7SEric W. Biederman 	}
1770161550d7SEric W. Biederman 
17719e8ae01dSOleg Nesterov 	wo.wo_type	= type;
17729e8ae01dSOleg Nesterov 	wo.wo_pid	= pid;
17739e8ae01dSOleg Nesterov 	wo.wo_flags	= options | WEXITED;
17749e8ae01dSOleg Nesterov 	wo.wo_info	= NULL;
1775359566faSAl Viro 	wo.wo_stat	= 0;
17769e8ae01dSOleg Nesterov 	wo.wo_rusage	= ru;
17779e8ae01dSOleg Nesterov 	ret = do_wait(&wo);
1778161550d7SEric W. Biederman 	put_pid(pid);
1779359566faSAl Viro 	if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
1780359566faSAl Viro 		ret = -EFAULT;
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds 	return ret;
17831da177e4SLinus Torvalds }
17841da177e4SLinus Torvalds 
kernel_wait(pid_t pid,int * stat)17858043fc14SChristoph Hellwig int kernel_wait(pid_t pid, int *stat)
17868043fc14SChristoph Hellwig {
17878043fc14SChristoph Hellwig 	struct wait_opts wo = {
17888043fc14SChristoph Hellwig 		.wo_type	= PIDTYPE_PID,
17898043fc14SChristoph Hellwig 		.wo_pid		= find_get_pid(pid),
17908043fc14SChristoph Hellwig 		.wo_flags	= WEXITED,
17918043fc14SChristoph Hellwig 	};
17928043fc14SChristoph Hellwig 	int ret;
17938043fc14SChristoph Hellwig 
17948043fc14SChristoph Hellwig 	ret = do_wait(&wo);
17958043fc14SChristoph Hellwig 	if (ret > 0 && wo.wo_stat)
17968043fc14SChristoph Hellwig 		*stat = wo.wo_stat;
17978043fc14SChristoph Hellwig 	put_pid(wo.wo_pid);
17988043fc14SChristoph Hellwig 	return ret;
17998043fc14SChristoph Hellwig }
18008043fc14SChristoph Hellwig 
SYSCALL_DEFINE4(wait4,pid_t,upid,int __user *,stat_addr,int,options,struct rusage __user *,ru)1801ce72a16fSAl Viro SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1802ce72a16fSAl Viro 		int, options, struct rusage __user *, ru)
1803ce72a16fSAl Viro {
1804ce72a16fSAl Viro 	struct rusage r;
1805ce72a16fSAl Viro 	long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
1806ce72a16fSAl Viro 
1807ce72a16fSAl Viro 	if (err > 0) {
1808ce72a16fSAl Viro 		if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1809ce72a16fSAl Viro 			return -EFAULT;
1810ce72a16fSAl Viro 	}
1811ce72a16fSAl Viro 	return err;
1812ce72a16fSAl Viro }
1813ce72a16fSAl Viro 
18141da177e4SLinus Torvalds #ifdef __ARCH_WANT_SYS_WAITPID
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds /*
18171da177e4SLinus Torvalds  * sys_waitpid() remains for compatibility. waitpid() should be
18181da177e4SLinus Torvalds  * implemented by calling sys_wait4() from libc.a.
18191da177e4SLinus Torvalds  */
SYSCALL_DEFINE3(waitpid,pid_t,pid,int __user *,stat_addr,int,options)182017da2bd9SHeiko Carstens SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
18211da177e4SLinus Torvalds {
1822d300b610SDominik Brodowski 	return kernel_wait4(pid, stat_addr, options, NULL);
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
18251da177e4SLinus Torvalds #endif
18267e95a225SAl Viro 
18277e95a225SAl 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)18287e95a225SAl Viro COMPAT_SYSCALL_DEFINE4(wait4,
18297e95a225SAl Viro 	compat_pid_t, pid,
18307e95a225SAl Viro 	compat_uint_t __user *, stat_addr,
18317e95a225SAl Viro 	int, options,
18327e95a225SAl Viro 	struct compat_rusage __user *, ru)
18337e95a225SAl Viro {
18347e95a225SAl Viro 	struct rusage r;
1835ce72a16fSAl Viro 	long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
1836ce72a16fSAl Viro 	if (err > 0) {
1837ce72a16fSAl Viro 		if (ru && put_compat_rusage(&r, ru))
18387e95a225SAl Viro 			return -EFAULT;
18397e95a225SAl Viro 	}
1840ce72a16fSAl Viro 	return err;
18417e95a225SAl Viro }
18427e95a225SAl Viro 
COMPAT_SYSCALL_DEFINE5(waitid,int,which,compat_pid_t,pid,struct compat_siginfo __user *,infop,int,options,struct compat_rusage __user *,uru)18437e95a225SAl Viro COMPAT_SYSCALL_DEFINE5(waitid,
18447e95a225SAl Viro 		int, which, compat_pid_t, pid,
18457e95a225SAl Viro 		struct compat_siginfo __user *, infop, int, options,
18467e95a225SAl Viro 		struct compat_rusage __user *, uru)
18477e95a225SAl Viro {
18487e95a225SAl Viro 	struct rusage ru;
184967d7dddeSAl Viro 	struct waitid_info info = {.status = 0};
185067d7dddeSAl Viro 	long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
1851634a8160SAl Viro 	int signo = 0;
1852634a8160SAl Viro 	if (err > 0) {
1853634a8160SAl Viro 		signo = SIGCHLD;
1854634a8160SAl Viro 		err = 0;
18556c85501fSAl Viro 		if (uru) {
185667d7dddeSAl Viro 			/* kernel_waitid() overwrites everything in ru */
18577e95a225SAl Viro 			if (COMPAT_USE_64BIT_TIME)
185867d7dddeSAl Viro 				err = copy_to_user(uru, &ru, sizeof(ru));
18597e95a225SAl Viro 			else
186067d7dddeSAl Viro 				err = put_compat_rusage(&ru, uru);
186167d7dddeSAl Viro 			if (err)
18627e95a225SAl Viro 				return -EFAULT;
18637e95a225SAl Viro 		}
18646c85501fSAl Viro 	}
18657e95a225SAl Viro 
18664c48abe9SAl Viro 	if (!infop)
186767d7dddeSAl Viro 		return err;
18684c48abe9SAl Viro 
186941cd7805SChristophe Leroy 	if (!user_write_access_begin(infop, sizeof(*infop)))
18701c9fec47SKees Cook 		return -EFAULT;
187196ca579aSKees Cook 
1872634a8160SAl Viro 	unsafe_put_user(signo, &infop->si_signo, Efault);
18734c48abe9SAl Viro 	unsafe_put_user(0, &infop->si_errno, Efault);
1874cc731525SEric W. Biederman 	unsafe_put_user(info.cause, &infop->si_code, Efault);
18754c48abe9SAl Viro 	unsafe_put_user(info.pid, &infop->si_pid, Efault);
18764c48abe9SAl Viro 	unsafe_put_user(info.uid, &infop->si_uid, Efault);
18774c48abe9SAl Viro 	unsafe_put_user(info.status, &infop->si_status, Efault);
187841cd7805SChristophe Leroy 	user_write_access_end();
18794c48abe9SAl Viro 	return err;
18804c48abe9SAl Viro Efault:
188141cd7805SChristophe Leroy 	user_write_access_end();
18824c48abe9SAl Viro 	return -EFAULT;
18837e95a225SAl Viro }
18847e95a225SAl Viro #endif
18857c2c11b2SSudip Mukherjee 
188638fd525aSEric W. Biederman /**
188738fd525aSEric W. Biederman  * thread_group_exited - check that a thread group has exited
188838fd525aSEric W. Biederman  * @pid: tgid of thread group to be checked.
188938fd525aSEric W. Biederman  *
189038fd525aSEric W. Biederman  * Test if the thread group represented by tgid has exited (all
189138fd525aSEric W. Biederman  * threads are zombies, dead or completely gone).
189238fd525aSEric W. Biederman  *
189338fd525aSEric W. Biederman  * Return: true if the thread group has exited. false otherwise.
189438fd525aSEric W. Biederman  */
thread_group_exited(struct pid * pid)189538fd525aSEric W. Biederman bool thread_group_exited(struct pid *pid)
189638fd525aSEric W. Biederman {
189738fd525aSEric W. Biederman 	struct task_struct *task;
189838fd525aSEric W. Biederman 	bool exited;
189938fd525aSEric W. Biederman 
190038fd525aSEric W. Biederman 	rcu_read_lock();
190138fd525aSEric W. Biederman 	task = pid_task(pid, PIDTYPE_PID);
190238fd525aSEric W. Biederman 	exited = !task ||
190338fd525aSEric W. Biederman 		(READ_ONCE(task->exit_state) && thread_group_empty(task));
190438fd525aSEric W. Biederman 	rcu_read_unlock();
190538fd525aSEric W. Biederman 
190638fd525aSEric W. Biederman 	return exited;
190738fd525aSEric W. Biederman }
190838fd525aSEric W. Biederman EXPORT_SYMBOL(thread_group_exited);
190938fd525aSEric W. Biederman 
1910c27cd083SMark Rutland /*
1911c27cd083SMark Rutland  * This needs to be __function_aligned as GCC implicitly makes any
1912c27cd083SMark Rutland  * implementation of abort() cold and drops alignment specified by
1913c27cd083SMark Rutland  * -falign-functions=N.
1914c27cd083SMark Rutland  *
1915c27cd083SMark Rutland  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
1916c27cd083SMark Rutland  */
abort(void)1917c27cd083SMark Rutland __weak __function_aligned void abort(void)
19187c2c11b2SSudip Mukherjee {
19197c2c11b2SSudip Mukherjee 	BUG();
19207c2c11b2SSudip Mukherjee 
19217c2c11b2SSudip Mukherjee 	/* if that doesn't kill us, halt */
19227c2c11b2SSudip Mukherjee 	panic("Oops failed to kill thread");
19237c2c11b2SSudip Mukherjee }
1924dc8635b7SAndrew Morton EXPORT_SYMBOL(abort);
1925