Linux 5.19-rc7
[linux-block.git] / kernel / exit.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/kernel/exit.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
1da177e4
LT
8#include <linux/mm.h>
9#include <linux/slab.h>
4eb5aaa3 10#include <linux/sched/autogroup.h>
6e84f315 11#include <linux/sched/mm.h>
03441a34 12#include <linux/sched/stat.h>
29930025 13#include <linux/sched/task.h>
68db0cf1 14#include <linux/sched/task_stack.h>
32ef5517 15#include <linux/sched/cputime.h>
1da177e4 16#include <linux/interrupt.h>
1da177e4 17#include <linux/module.h>
c59ede7b 18#include <linux/capability.h>
1da177e4
LT
19#include <linux/completion.h>
20#include <linux/personality.h>
21#include <linux/tty.h>
da9cbc87 22#include <linux/iocontext.h>
1da177e4 23#include <linux/key.h>
1da177e4
LT
24#include <linux/cpu.h>
25#include <linux/acct.h>
8f0ab514 26#include <linux/tsacct_kern.h>
1da177e4 27#include <linux/file.h>
9f3acc31 28#include <linux/fdtable.h>
80d26af8 29#include <linux/freezer.h>
1da177e4 30#include <linux/binfmts.h>
ab516013 31#include <linux/nsproxy.h>
84d73786 32#include <linux/pid_namespace.h>
1da177e4
LT
33#include <linux/ptrace.h>
34#include <linux/profile.h>
35#include <linux/mount.h>
36#include <linux/proc_fs.h>
49d769d5 37#include <linux/kthread.h>
1da177e4 38#include <linux/mempolicy.h>
c757249a 39#include <linux/taskstats_kern.h>
ca74e92b 40#include <linux/delayacct.h>
b4f48b63 41#include <linux/cgroup.h>
1da177e4 42#include <linux/syscalls.h>
7ed20e1a 43#include <linux/signal.h>
6a14c5c9 44#include <linux/posix-timers.h>
9f46080c 45#include <linux/cn_proc.h>
de5097c2 46#include <linux/mutex.h>
0771dfef 47#include <linux/futex.h>
b92ce558 48#include <linux/pipe_fs_i.h>
fa84cb93 49#include <linux/audit.h> /* for audit_free() */
83cc5ed3 50#include <linux/resource.h>
6eaeeaba 51#include <linux/task_io_accounting_ops.h>
355f841a
EB
52#include <linux/blkdev.h>
53#include <linux/task_work.h>
5ad4e53b 54#include <linux/fs_struct.h>
d84f4f99 55#include <linux/init_task.h>
cdd6c482 56#include <linux/perf_event.h>
ad8d75ff 57#include <trace/events/sched.h>
24f1e32c 58#include <linux/hw_breakpoint.h>
3d5992d2 59#include <linux/oom.h>
54848d73 60#include <linux/writeback.h>
40401530 61#include <linux/shm.h>
5c9a8750 62#include <linux/kcov.h>
53d3eaa3 63#include <linux/random.h>
8f95c90c 64#include <linux/rcuwait.h>
7e95a225 65#include <linux/compat.h>
b1b6b5a3 66#include <linux/io_uring.h>
670721c7 67#include <linux/kprobes.h>
54ecbe6f 68#include <linux/rethook.h>
1da177e4 69
7c0f6ba6 70#include <linux/uaccess.h>
1da177e4 71#include <asm/unistd.h>
1da177e4
LT
72#include <asm/mmu_context.h>
73
d40e48e0 74static void __unhash_process(struct task_struct *p, bool group_dead)
1da177e4
LT
75{
76 nr_threads--;
50d75f8d 77 detach_pid(p, PIDTYPE_PID);
d40e48e0 78 if (group_dead) {
6883f81a 79 detach_pid(p, PIDTYPE_TGID);
1da177e4
LT
80 detach_pid(p, PIDTYPE_PGID);
81 detach_pid(p, PIDTYPE_SID);
c97d9893 82
5e85d4ab 83 list_del_rcu(&p->tasks);
9cd80bbb 84 list_del_init(&p->sibling);
909ea964 85 __this_cpu_dec(process_counts);
1da177e4 86 }
47e65328 87 list_del_rcu(&p->thread_group);
0c740d0a 88 list_del_rcu(&p->thread_node);
1da177e4
LT
89}
90
6a14c5c9
ON
91/*
92 * This function expects the tasklist_lock write-locked.
93 */
94static void __exit_signal(struct task_struct *tsk)
95{
96 struct signal_struct *sig = tsk->signal;
d40e48e0 97 bool group_dead = thread_group_leader(tsk);
6a14c5c9 98 struct sighand_struct *sighand;
3f649ab7 99 struct tty_struct *tty;
5613fda9 100 u64 utime, stime;
6a14c5c9 101
d11c563d 102 sighand = rcu_dereference_check(tsk->sighand,
db1466b3 103 lockdep_tasklist_lock_is_held());
6a14c5c9
ON
104 spin_lock(&sighand->siglock);
105
baa73d9e 106#ifdef CONFIG_POSIX_TIMERS
6a14c5c9 107 posix_cpu_timers_exit(tsk);
b95e31c0 108 if (group_dead)
6a14c5c9 109 posix_cpu_timers_exit_group(tsk);
baa73d9e 110#endif
e0a70217 111
baa73d9e
NP
112 if (group_dead) {
113 tty = sig->tty;
114 sig->tty = NULL;
115 } else {
6a14c5c9
ON
116 /*
117 * If there is any task waiting for the group exit
118 * then notify it:
119 */
d344193a 120 if (sig->notify_count > 0 && !--sig->notify_count)
60700e38 121 wake_up_process(sig->group_exec_task);
6db840fa 122
6a14c5c9
ON
123 if (tsk == sig->curr_target)
124 sig->curr_target = next_thread(tsk);
6a14c5c9
ON
125 }
126
53d3eaa3
NP
127 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
128 sizeof(unsigned long long));
129
90ed9cbe 130 /*
26e75b5c
ON
131 * Accumulate here the counters for all threads as they die. We could
132 * skip the group leader because it is the last user of signal_struct,
133 * but we want to avoid the race with thread_group_cputime() which can
134 * see the empty ->thread_head list.
90ed9cbe
RR
135 */
136 task_cputime(tsk, &utime, &stime);
e78c3496 137 write_seqlock(&sig->stats_lock);
90ed9cbe
RR
138 sig->utime += utime;
139 sig->stime += stime;
140 sig->gtime += task_gtime(tsk);
141 sig->min_flt += tsk->min_flt;
142 sig->maj_flt += tsk->maj_flt;
143 sig->nvcsw += tsk->nvcsw;
144 sig->nivcsw += tsk->nivcsw;
145 sig->inblock += task_io_get_inblock(tsk);
146 sig->oublock += task_io_get_oublock(tsk);
147 task_io_accounting_add(&sig->ioac, &tsk->ioac);
148 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
b3ac022c 149 sig->nr_threads--;
d40e48e0 150 __unhash_process(tsk, group_dead);
e78c3496 151 write_sequnlock(&sig->stats_lock);
5876700c 152
da7978b0
ON
153 /*
154 * Do this under ->siglock, we can race with another thread
155 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
156 */
157 flush_sigqueue(&tsk->pending);
a7e5328a 158 tsk->sighand = NULL;
6a14c5c9 159 spin_unlock(&sighand->siglock);
6a14c5c9 160
a7e5328a 161 __cleanup_sighand(sighand);
a0be55de 162 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
d40e48e0 163 if (group_dead) {
6a14c5c9 164 flush_sigqueue(&sig->shared_pending);
4ada856f 165 tty_kref_put(tty);
6a14c5c9
ON
166 }
167}
168
8c7904a0
EB
169static void delayed_put_task_struct(struct rcu_head *rhp)
170{
0a16b607
MD
171 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
172
670721c7 173 kprobe_flush_task(tsk);
54ecbe6f 174 rethook_flush_task(tsk);
4e231c79 175 perf_event_delayed_put(tsk);
0a16b607
MD
176 trace_sched_process_free(tsk);
177 put_task_struct(tsk);
8c7904a0
EB
178}
179
3fbd7ee2
EB
180void put_task_struct_rcu_user(struct task_struct *task)
181{
182 if (refcount_dec_and_test(&task->rcu_users))
183 call_rcu(&task->rcu, delayed_put_task_struct);
184}
f470021a 185
a0be55de 186void release_task(struct task_struct *p)
1da177e4 187{
36c8b586 188 struct task_struct *leader;
7bc3e6e5 189 struct pid *thread_pid;
1da177e4 190 int zap_leader;
1f09f974 191repeat:
c69e8d9c 192 /* don't need to get the RCU readlock here - the process is dead and
d11c563d
PM
193 * can't be modifying its own credentials. But shut RCU-lockdep up */
194 rcu_read_lock();
21d1c5e3 195 dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
d11c563d 196 rcu_read_unlock();
c69e8d9c 197
6b115bf5 198 cgroup_release(p);
0203026b 199
1da177e4 200 write_lock_irq(&tasklist_lock);
a288eecc 201 ptrace_release_task(p);
7bc3e6e5 202 thread_pid = get_pid(p->thread_pid);
1da177e4 203 __exit_signal(p);
35f5cad8 204
1da177e4
LT
205 /*
206 * If we are the last non-leader member of the thread
207 * group, and the leader is zombie, then notify the
208 * group leader's parent process. (if it wants notification.)
209 */
210 zap_leader = 0;
211 leader = p->group_leader;
a0be55de
IA
212 if (leader != p && thread_group_empty(leader)
213 && leader->exit_state == EXIT_ZOMBIE) {
1da177e4
LT
214 /*
215 * If we were the last child thread and the leader has
216 * exited already, and the leader's parent ignores SIGCHLD,
217 * then we are the one who should release the leader.
dae33574 218 */
86773473 219 zap_leader = do_notify_parent(leader, leader->exit_signal);
dae33574
RM
220 if (zap_leader)
221 leader->exit_state = EXIT_DEAD;
1da177e4
LT
222 }
223
1da177e4 224 write_unlock_irq(&tasklist_lock);
3a15fb6e 225 seccomp_filter_release(p);
7bc3e6e5 226 proc_flush_pid(thread_pid);
6ade99ec 227 put_pid(thread_pid);
1da177e4 228 release_thread(p);
3fbd7ee2 229 put_task_struct_rcu_user(p);
1da177e4
LT
230
231 p = leader;
232 if (unlikely(zap_leader))
233 goto repeat;
234}
235
9d9a6ebf 236int rcuwait_wake_up(struct rcuwait *w)
8f95c90c 237{
9d9a6ebf 238 int ret = 0;
8f95c90c
DB
239 struct task_struct *task;
240
241 rcu_read_lock();
242
243 /*
244 * Order condition vs @task, such that everything prior to the load
245 * of @task is visible. This is the condition as to why the user called
c9d64a1b 246 * rcuwait_wake() in the first place. Pairs with set_current_state()
8f95c90c
DB
247 * barrier (A) in rcuwait_wait_event().
248 *
249 * WAIT WAKE
250 * [S] tsk = current [S] cond = true
251 * MB (A) MB (B)
252 * [L] cond [L] tsk
253 */
6dc080ee 254 smp_mb(); /* (B) */
8f95c90c 255
8f95c90c
DB
256 task = rcu_dereference(w->task);
257 if (task)
9d9a6ebf 258 ret = wake_up_process(task);
8f95c90c 259 rcu_read_unlock();
9d9a6ebf
DB
260
261 return ret;
8f95c90c 262}
ac8dec42 263EXPORT_SYMBOL_GPL(rcuwait_wake_up);
8f95c90c 264
1da177e4
LT
265/*
266 * Determine if a process group is "orphaned", according to the POSIX
267 * definition in 2.2.2.52. Orphaned process groups are not to be affected
268 * by terminal-generated stop signals. Newly orphaned process groups are
269 * to receive a SIGHUP and a SIGCONT.
270 *
271 * "I ask you, have you ever known what it is to be an orphan?"
272 */
a0be55de
IA
273static int will_become_orphaned_pgrp(struct pid *pgrp,
274 struct task_struct *ignored_task)
1da177e4
LT
275{
276 struct task_struct *p;
1da177e4 277
0475ac08 278 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
05e83df6
ON
279 if ((p == ignored_task) ||
280 (p->exit_state && thread_group_empty(p)) ||
281 is_global_init(p->real_parent))
1da177e4 282 continue;
05e83df6 283
0475ac08 284 if (task_pgrp(p->real_parent) != pgrp &&
05e83df6
ON
285 task_session(p->real_parent) == task_session(p))
286 return 0;
0475ac08 287 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
05e83df6
ON
288
289 return 1;
1da177e4
LT
290}
291
3e7cd6c4 292int is_current_pgrp_orphaned(void)
1da177e4
LT
293{
294 int retval;
295
296 read_lock(&tasklist_lock);
3e7cd6c4 297 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
1da177e4
LT
298 read_unlock(&tasklist_lock);
299
300 return retval;
301}
302
961c4675 303static bool has_stopped_jobs(struct pid *pgrp)
1da177e4 304{
1da177e4
LT
305 struct task_struct *p;
306
0475ac08 307 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
961c4675
ON
308 if (p->signal->flags & SIGNAL_STOP_STOPPED)
309 return true;
0475ac08 310 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
961c4675
ON
311
312 return false;
1da177e4
LT
313}
314
f49ee505
ON
315/*
316 * Check to see if any process groups have become orphaned as
317 * a result of our exiting, and if they have any stopped jobs,
318 * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
319 */
320static void
321kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
322{
323 struct pid *pgrp = task_pgrp(tsk);
324 struct task_struct *ignored_task = tsk;
325
326 if (!parent)
a0be55de
IA
327 /* exit: our father is in a different pgrp than
328 * we are and we were the only connection outside.
329 */
f49ee505
ON
330 parent = tsk->real_parent;
331 else
332 /* reparent: our child is in a different pgrp than
333 * we are, and it was the only connection outside.
334 */
335 ignored_task = NULL;
336
337 if (task_pgrp(parent) != pgrp &&
338 task_session(parent) == task_session(tsk) &&
339 will_become_orphaned_pgrp(pgrp, ignored_task) &&
340 has_stopped_jobs(pgrp)) {
341 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
342 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
343 }
344}
345
92307383 346static void coredump_task_exit(struct task_struct *tsk)
d67e03e3
EB
347{
348 struct core_state *core_state;
349
350 /*
351 * Serialize with any possible pending coredump.
0258b5fd 352 * We must hold siglock around checking core_state
92307383 353 * and setting PF_POSTCOREDUMP. The core-inducing thread
d67e03e3 354 * will increment ->nr_threads for each thread in the
92307383 355 * group without PF_POSTCOREDUMP set.
d67e03e3 356 */
0258b5fd 357 spin_lock_irq(&tsk->sighand->siglock);
92307383 358 tsk->flags |= PF_POSTCOREDUMP;
0258b5fd
EB
359 core_state = tsk->signal->core_state;
360 spin_unlock_irq(&tsk->sighand->siglock);
d67e03e3
EB
361 if (core_state) {
362 struct core_thread self;
363
d67e03e3
EB
364 self.task = current;
365 if (self.task->flags & PF_SIGNALED)
366 self.next = xchg(&core_state->dumper.next, &self);
367 else
368 self.task = NULL;
369 /*
370 * Implies mb(), the result of xchg() must be visible
371 * to core_state->dumper.
372 */
373 if (atomic_dec_and_test(&core_state->nr_threads))
374 complete(&core_state->startup);
375
376 for (;;) {
377 set_current_state(TASK_UNINTERRUPTIBLE);
378 if (!self.task) /* see coredump_finish() */
379 break;
380 freezable_schedule();
381 }
382 __set_current_state(TASK_RUNNING);
d67e03e3
EB
383 }
384}
385
f98bafa0 386#ifdef CONFIG_MEMCG
cf475ad2 387/*
733eda7a 388 * A task is exiting. If it owned this mm, find a new owner for the mm.
cf475ad2 389 */
cf475ad2
BS
390void mm_update_next_owner(struct mm_struct *mm)
391{
392 struct task_struct *c, *g, *p = current;
393
394retry:
733eda7a
KH
395 /*
396 * If the exiting or execing task is not the owner, it's
397 * someone else's problem.
398 */
399 if (mm->owner != p)
cf475ad2 400 return;
733eda7a
KH
401 /*
402 * The current owner is exiting/execing and there are no other
403 * candidates. Do not leave the mm pointing to a possibly
404 * freed task structure.
405 */
406 if (atomic_read(&mm->mm_users) <= 1) {
987717e5 407 WRITE_ONCE(mm->owner, NULL);
733eda7a
KH
408 return;
409 }
cf475ad2
BS
410
411 read_lock(&tasklist_lock);
412 /*
413 * Search in the children
414 */
415 list_for_each_entry(c, &p->children, sibling) {
416 if (c->mm == mm)
417 goto assign_new_owner;
418 }
419
420 /*
421 * Search in the siblings
422 */
dea33cfd 423 list_for_each_entry(c, &p->real_parent->children, sibling) {
cf475ad2
BS
424 if (c->mm == mm)
425 goto assign_new_owner;
426 }
427
428 /*
f87fb599 429 * Search through everything else, we should not get here often.
cf475ad2 430 */
39af1765
ON
431 for_each_process(g) {
432 if (g->flags & PF_KTHREAD)
433 continue;
434 for_each_thread(g, c) {
435 if (c->mm == mm)
436 goto assign_new_owner;
437 if (c->mm)
438 break;
439 }
f87fb599 440 }
cf475ad2 441 read_unlock(&tasklist_lock);
31a78f23
BS
442 /*
443 * We found no owner yet mm_users > 1: this implies that we are
444 * most likely racing with swapoff (try_to_unuse()) or /proc or
e5991371 445 * ptrace or page migration (get_task_mm()). Mark owner as NULL.
31a78f23 446 */
987717e5 447 WRITE_ONCE(mm->owner, NULL);
cf475ad2
BS
448 return;
449
450assign_new_owner:
451 BUG_ON(c == p);
452 get_task_struct(c);
453 /*
454 * The task_lock protects c->mm from changing.
455 * We always want mm->owner->mm == mm
456 */
457 task_lock(c);
e5991371
HD
458 /*
459 * Delay read_unlock() till we have the task_lock()
460 * to ensure that c does not slip away underneath us
461 */
462 read_unlock(&tasklist_lock);
cf475ad2
BS
463 if (c->mm != mm) {
464 task_unlock(c);
465 put_task_struct(c);
466 goto retry;
467 }
987717e5 468 WRITE_ONCE(mm->owner, c);
cf475ad2
BS
469 task_unlock(c);
470 put_task_struct(c);
471}
f98bafa0 472#endif /* CONFIG_MEMCG */
cf475ad2 473
1da177e4
LT
474/*
475 * Turn us into a lazy TLB process if we
476 * aren't already..
477 */
0039962a 478static void exit_mm(void)
1da177e4 479{
0039962a 480 struct mm_struct *mm = current->mm;
1da177e4 481
4610ba7a 482 exit_mm_release(current, mm);
1da177e4
LT
483 if (!mm)
484 return;
4fe7efdb 485 sync_mm_rss(mm);
d8ed45c5 486 mmap_read_lock(mm);
f1f10076 487 mmgrab(mm);
0039962a 488 BUG_ON(mm != current->active_mm);
1da177e4 489 /* more a memory barrier than a real lock */
0039962a 490 task_lock(current);
5bc78502
MD
491 /*
492 * When a thread stops operating on an address space, the loop
493 * in membarrier_private_expedited() may not observe that
494 * tsk->mm, and the loop in membarrier_global_expedited() may
495 * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
496 * rq->membarrier_state, so those would not issue an IPI.
497 * Membarrier requires a memory barrier after accessing
498 * user-space memory, before clearing tsk->mm or the
499 * rq->membarrier_state.
500 */
501 smp_mb__after_spinlock();
502 local_irq_disable();
0039962a 503 current->mm = NULL;
5bc78502 504 membarrier_update_current_mm(NULL);
1da177e4 505 enter_lazy_tlb(mm, current);
5bc78502 506 local_irq_enable();
0039962a 507 task_unlock(current);
5bc78502 508 mmap_read_unlock(mm);
cf475ad2 509 mm_update_next_owner(mm);
1da177e4 510 mmput(mm);
c32b3cbe 511 if (test_thread_flag(TIF_MEMDIE))
38531201 512 exit_oom_victim();
1da177e4
LT
513}
514
c9dc05bf
ON
515static struct task_struct *find_alive_thread(struct task_struct *p)
516{
517 struct task_struct *t;
518
519 for_each_thread(p, t) {
520 if (!(t->flags & PF_EXITING))
521 return t;
522 }
523 return NULL;
524}
525
8fb335e0
AV
526static struct task_struct *find_child_reaper(struct task_struct *father,
527 struct list_head *dead)
1109909c
ON
528 __releases(&tasklist_lock)
529 __acquires(&tasklist_lock)
530{
531 struct pid_namespace *pid_ns = task_active_pid_ns(father);
532 struct task_struct *reaper = pid_ns->child_reaper;
8fb335e0 533 struct task_struct *p, *n;
1109909c
ON
534
535 if (likely(reaper != father))
536 return reaper;
537
c9dc05bf
ON
538 reaper = find_alive_thread(father);
539 if (reaper) {
1109909c
ON
540 pid_ns->child_reaper = reaper;
541 return reaper;
542 }
543
544 write_unlock_irq(&tasklist_lock);
8fb335e0
AV
545
546 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
547 list_del_init(&p->ptrace_entry);
548 release_task(p);
549 }
550
1109909c
ON
551 zap_pid_ns_processes(pid_ns);
552 write_lock_irq(&tasklist_lock);
553
554 return father;
555}
556
1da177e4 557/*
ebec18a6
LP
558 * When we die, we re-parent all our children, and try to:
559 * 1. give them to another thread in our thread group, if such a member exists
560 * 2. give it to the first ancestor process which prctl'd itself as a
561 * child_subreaper for its children (like a service manager)
562 * 3. give it to the init process (PID 1) in our pid namespace
1da177e4 563 */
1109909c
ON
564static struct task_struct *find_new_reaper(struct task_struct *father,
565 struct task_struct *child_reaper)
1da177e4 566{
c9dc05bf 567 struct task_struct *thread, *reaper;
1da177e4 568
c9dc05bf
ON
569 thread = find_alive_thread(father);
570 if (thread)
950bbabb 571 return thread;
1da177e4 572
7d24e2df 573 if (father->signal->has_child_subreaper) {
c6c70f44 574 unsigned int ns_level = task_pid(father)->level;
ebec18a6 575 /*
175aed3f 576 * Find the first ->is_child_subreaper ancestor in our pid_ns.
c6c70f44
ON
577 * We can't check reaper != child_reaper to ensure we do not
578 * cross the namespaces, the exiting parent could be injected
579 * by setns() + fork().
580 * We check pid->level, this is slightly more efficient than
581 * task_active_pid_ns(reaper) != task_active_pid_ns(father).
ebec18a6 582 */
c6c70f44
ON
583 for (reaper = father->real_parent;
584 task_pid(reaper)->level == ns_level;
ebec18a6 585 reaper = reaper->real_parent) {
175aed3f 586 if (reaper == &init_task)
ebec18a6
LP
587 break;
588 if (!reaper->signal->is_child_subreaper)
589 continue;
c9dc05bf
ON
590 thread = find_alive_thread(reaper);
591 if (thread)
592 return thread;
ebec18a6 593 }
1da177e4 594 }
762a24be 595
1109909c 596 return child_reaper;
950bbabb
ON
597}
598
5dfc80be
ON
599/*
600* Any that need to be release_task'd are put on the @dead list.
601 */
9cd80bbb 602static void reparent_leader(struct task_struct *father, struct task_struct *p,
5dfc80be
ON
603 struct list_head *dead)
604{
2831096e 605 if (unlikely(p->exit_state == EXIT_DEAD))
5dfc80be
ON
606 return;
607
abd50b39 608 /* We don't want people slaying init. */
5dfc80be
ON
609 p->exit_signal = SIGCHLD;
610
611 /* If it has exited notify the new parent about this child's death. */
d21142ec 612 if (!p->ptrace &&
5dfc80be 613 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
86773473 614 if (do_notify_parent(p, p->exit_signal)) {
5dfc80be 615 p->exit_state = EXIT_DEAD;
dc2fd4b0 616 list_add(&p->ptrace_entry, dead);
5dfc80be
ON
617 }
618 }
619
620 kill_orphaned_pgrp(p, father);
621}
622
482a3767
ON
623/*
624 * This does two things:
625 *
626 * A. Make init inherit all the child processes
627 * B. Check to see if any process groups have become orphaned
628 * as a result of our exiting, and if they have any stopped
629 * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
630 */
631static void forget_original_parent(struct task_struct *father,
632 struct list_head *dead)
1da177e4 633{
482a3767 634 struct task_struct *p, *t, *reaper;
762a24be 635
7c8bd232 636 if (unlikely(!list_empty(&father->ptraced)))
482a3767 637 exit_ptrace(father, dead);
f470021a 638
7c8bd232 639 /* Can drop and reacquire tasklist_lock */
8fb335e0 640 reaper = find_child_reaper(father, dead);
ad9e206a 641 if (list_empty(&father->children))
482a3767 642 return;
1109909c
ON
643
644 reaper = find_new_reaper(father, reaper);
2831096e 645 list_for_each_entry(p, &father->children, sibling) {
57a05918 646 for_each_thread(p, t) {
22a34c6f
MB
647 RCU_INIT_POINTER(t->real_parent, reaper);
648 BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
57a05918 649 if (likely(!t->ptrace))
9cd80bbb 650 t->parent = t->real_parent;
9cd80bbb
ON
651 if (t->pdeath_signal)
652 group_send_sig_info(t->pdeath_signal,
01024980
EB
653 SEND_SIG_NOINFO, t,
654 PIDTYPE_TGID);
57a05918 655 }
2831096e
ON
656 /*
657 * If this is a threaded reparent there is no need to
658 * notify anyone anything has happened.
659 */
660 if (!same_thread_group(reaper, father))
482a3767 661 reparent_leader(father, p, dead);
1da177e4 662 }
2831096e 663 list_splice_tail_init(&father->children, &reaper->children);
1da177e4
LT
664}
665
666/*
667 * Send signals to all our closest relatives so that they know
668 * to properly mourn us..
669 */
821c7de7 670static void exit_notify(struct task_struct *tsk, int group_dead)
1da177e4 671{
53c8f9f1 672 bool autoreap;
482a3767
ON
673 struct task_struct *p, *n;
674 LIST_HEAD(dead);
1da177e4 675
762a24be 676 write_lock_irq(&tasklist_lock);
482a3767
ON
677 forget_original_parent(tsk, &dead);
678
821c7de7
ON
679 if (group_dead)
680 kill_orphaned_pgrp(tsk->group_leader, NULL);
1da177e4 681
b191d649 682 tsk->exit_state = EXIT_ZOMBIE;
45cdf5cc
ON
683 if (unlikely(tsk->ptrace)) {
684 int sig = thread_group_leader(tsk) &&
685 thread_group_empty(tsk) &&
686 !ptrace_reparented(tsk) ?
687 tsk->exit_signal : SIGCHLD;
688 autoreap = do_notify_parent(tsk, sig);
689 } else if (thread_group_leader(tsk)) {
690 autoreap = thread_group_empty(tsk) &&
691 do_notify_parent(tsk, tsk->exit_signal);
692 } else {
693 autoreap = true;
694 }
1da177e4 695
30b692d3
CB
696 if (autoreap) {
697 tsk->exit_state = EXIT_DEAD;
6c66e7db 698 list_add(&tsk->ptrace_entry, &dead);
30b692d3 699 }
1da177e4 700
9c339168
ON
701 /* mt-exec, de_thread() is waiting for group leader */
702 if (unlikely(tsk->signal->notify_count < 0))
60700e38 703 wake_up_process(tsk->signal->group_exec_task);
1da177e4
LT
704 write_unlock_irq(&tasklist_lock);
705
482a3767
ON
706 list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
707 list_del_init(&p->ptrace_entry);
708 release_task(p);
709 }
1da177e4
LT
710}
711
e18eecb8
JD
712#ifdef CONFIG_DEBUG_STACK_USAGE
713static void check_stack_usage(void)
714{
715 static DEFINE_SPINLOCK(low_water_lock);
716 static int lowest_to_date = THREAD_SIZE;
e18eecb8
JD
717 unsigned long free;
718
7c9f8861 719 free = stack_not_used(current);
e18eecb8
JD
720
721 if (free >= lowest_to_date)
722 return;
723
724 spin_lock(&low_water_lock);
725 if (free < lowest_to_date) {
627393d4 726 pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
a0be55de 727 current->comm, task_pid_nr(current), free);
e18eecb8
JD
728 lowest_to_date = free;
729 }
730 spin_unlock(&low_water_lock);
731}
732#else
733static inline void check_stack_usage(void) {}
734#endif
735
9af6528e 736void __noreturn do_exit(long code)
1da177e4
LT
737{
738 struct task_struct *tsk = current;
739 int group_dead;
740
b1f866b0 741 WARN_ON(tsk->plug);
22e2c507 742
586b58ca
JH
743 kcov_task_exit(tsk);
744
92307383 745 coredump_task_exit(tsk);
a288eecc 746 ptrace_event(PTRACE_EVENT_EXIT, code);
1da177e4 747
e0e81739
DH
748 validate_creds_for_do_exit(tsk);
749
f552a27a 750 io_uring_files_cancel();
d12619b5 751 exit_signals(tsk); /* sets PF_EXITING */
1da177e4 752
48d212a2
LT
753 /* sync mm's RSS info before statistics gathering */
754 if (tsk->mm)
755 sync_mm_rss(tsk->mm);
51229b49 756 acct_update_integrals(tsk);
1da177e4 757 group_dead = atomic_dec_and_test(&tsk->signal->live);
c3068951 758 if (group_dead) {
43cf75d9 759 /*
760 * If the last thread of global init has exited, panic
761 * immediately to get a useable coredump.
762 */
763 if (unlikely(is_global_init(tsk)))
764 panic("Attempted to kill init! exitcode=0x%08x\n",
765 tsk->signal->group_exit_code ?: (int)code);
766
baa73d9e 767#ifdef CONFIG_POSIX_TIMERS
778e9a9c 768 hrtimer_cancel(&tsk->signal->real_timer);
d5b36a4d 769 exit_itimers(tsk);
baa73d9e 770#endif
1f10206c
JP
771 if (tsk->mm)
772 setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
c3068951 773 }
f6ec29a4 774 acct_collect(code, group_dead);
522ed776
MT
775 if (group_dead)
776 tty_audit_exit();
a4ff8dba 777 audit_free(tsk);
115085ea 778
48d212a2 779 tsk->exit_code = code;
115085ea 780 taskstats_exit(tsk, group_dead);
c757249a 781
0039962a 782 exit_mm();
1da177e4 783
0e464814 784 if (group_dead)
f6ec29a4 785 acct_process();
0a16b607
MD
786 trace_sched_process_exit(tsk);
787
1da177e4 788 exit_sem(tsk);
b34a6b1d 789 exit_shm(tsk);
1ec7f1dd
AV
790 exit_files(tsk);
791 exit_fs(tsk);
c39df5fa
ON
792 if (group_dead)
793 disassociate_ctty(1);
8aac6270 794 exit_task_namespaces(tsk);
ed3e694d 795 exit_task_work(tsk);
e6464694 796 exit_thread(tsk);
0b3fcf17
SE
797
798 /*
799 * Flush inherited counters to the parent - before the parent
800 * gets woken up by child-exit notifications.
801 *
802 * because of cgroup mode, must be called before cgroup_exit()
803 */
804 perf_event_exit_task(tsk);
805
8e5bfa8c 806 sched_autogroup_exit_task(tsk);
1ec41830 807 cgroup_exit(tsk);
1da177e4 808
24f1e32c
FW
809 /*
810 * FIXME: do that only when needed, using sched_exit tracepoint
811 */
7c8df286 812 flush_ptrace_hw_breakpoint(tsk);
33b2fb30 813
ccdd29ff 814 exit_tasks_rcu_start();
821c7de7 815 exit_notify(tsk, group_dead);
ef982393 816 proc_exit_connector(tsk);
c11600e4 817 mpol_put_task_policy(tsk);
42b2dd0a 818#ifdef CONFIG_FUTEX
c87e2837
IM
819 if (unlikely(current->pi_state_cache))
820 kfree(current->pi_state_cache);
42b2dd0a 821#endif
de5097c2 822 /*
9a11b49a 823 * Make sure we are holding no locks:
de5097c2 824 */
1b1d2fb4 825 debug_check_no_locks_held();
1da177e4 826
afc847b7 827 if (tsk->io_context)
b69f2292 828 exit_io_context(tsk);
afc847b7 829
b92ce558 830 if (tsk->splice_pipe)
4b8a8f1e 831 free_pipe_info(tsk->splice_pipe);
b92ce558 832
5640f768
ED
833 if (tsk->task_frag.page)
834 put_page(tsk->task_frag.page);
835
e0e81739 836 validate_creds_for_do_exit(tsk);
1a03d3f1 837 exit_task_stack_account(tsk);
e0e81739 838
4bcb8232 839 check_stack_usage();
7407251a 840 preempt_disable();
54848d73
WF
841 if (tsk->nr_dirtied)
842 __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
f41d911f 843 exit_rcu();
ccdd29ff 844 exit_tasks_rcu_finish();
b5740f4b 845
b09be676 846 lockdep_free_task(tsk);
9af6528e 847 do_task_dead();
1da177e4 848}
012914da 849
0e25498f
EB
850void __noreturn make_task_dead(int signr)
851{
852 /*
853 * Take the task off the cpu after something catastrophic has
854 * happened.
05ea0424
EB
855 *
856 * We can get here from a kernel oops, sometimes with preemption off.
857 * Start by checking for critical errors.
858 * Then fix up important state like USER_DS and preemption.
859 * Then do everything else.
0e25498f 860 */
05ea0424
EB
861 struct task_struct *tsk = current;
862
863 if (unlikely(in_interrupt()))
864 panic("Aiee, killing interrupt handler!");
865 if (unlikely(!tsk->pid))
866 panic("Attempted to kill the idle task!");
867
05ea0424
EB
868 if (unlikely(in_atomic())) {
869 pr_info("note: %s[%d] exited with preempt_count %d\n",
870 current->comm, task_pid_nr(current),
871 preempt_count());
872 preempt_count_set(PREEMPT_ENABLED);
873 }
874
875 /*
876 * We're taking recursive faults here in make_task_dead. Safest is to just
877 * leave this task alone and wait for reboot.
878 */
879 if (unlikely(tsk->flags & PF_EXITING)) {
880 pr_alert("Fixing recursive fault but reboot is needed!\n");
881 futex_exit_recursive(tsk);
912616f1
EB
882 tsk->exit_state = EXIT_DEAD;
883 refcount_inc(&tsk->rcu_users);
7f80a2fd 884 do_task_dead();
05ea0424
EB
885 }
886
0e25498f
EB
887 do_exit(signr);
888}
889
754fe8d2 890SYSCALL_DEFINE1(exit, int, error_code)
1da177e4
LT
891{
892 do_exit((error_code&0xff)<<8);
893}
894
1da177e4
LT
895/*
896 * Take down every thread in the group. This is called by fatal signals
897 * as well as by sys_exit_group (below).
898 */
eae654f1 899void __noreturn
1da177e4
LT
900do_group_exit(int exit_code)
901{
bfc4b089
ON
902 struct signal_struct *sig = current->signal;
903
49697335 904 if (sig->flags & SIGNAL_GROUP_EXIT)
bfc4b089 905 exit_code = sig->group_exit_code;
49697335
EB
906 else if (sig->group_exec_task)
907 exit_code = 0;
1da177e4 908 else if (!thread_group_empty(current)) {
1da177e4 909 struct sighand_struct *const sighand = current->sighand;
a0be55de 910
1da177e4 911 spin_lock_irq(&sighand->siglock);
49697335 912 if (sig->flags & SIGNAL_GROUP_EXIT)
1da177e4
LT
913 /* Another thread got here before we took the lock. */
914 exit_code = sig->group_exit_code;
49697335
EB
915 else if (sig->group_exec_task)
916 exit_code = 0;
1da177e4 917 else {
1da177e4 918 sig->group_exit_code = exit_code;
ed5d2cac 919 sig->flags = SIGNAL_GROUP_EXIT;
1da177e4
LT
920 zap_other_threads(current);
921 }
922 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
923 }
924
925 do_exit(exit_code);
926 /* NOTREACHED */
927}
928
929/*
930 * this kills every thread in the thread group. Note that any externally
931 * wait4()-ing process will get the correct exit code - even if this
932 * thread is not the thread group leader.
933 */
754fe8d2 934SYSCALL_DEFINE1(exit_group, int, error_code)
1da177e4
LT
935{
936 do_group_exit((error_code & 0xff) << 8);
2ed7c03e
HC
937 /* NOTREACHED */
938 return 0;
1da177e4
LT
939}
940
67d7ddde
AV
941struct waitid_info {
942 pid_t pid;
943 uid_t uid;
944 int status;
945 int cause;
946};
947
9e8ae01d
ON
948struct wait_opts {
949 enum pid_type wo_type;
9e8ae01d 950 int wo_flags;
e1eb1ebc 951 struct pid *wo_pid;
9e8ae01d 952
67d7ddde 953 struct waitid_info *wo_info;
359566fa 954 int wo_stat;
ce72a16f 955 struct rusage *wo_rusage;
9e8ae01d 956
ac6424b9 957 wait_queue_entry_t child_wait;
9e8ae01d
ON
958 int notask_error;
959};
960
989264f4 961static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
1da177e4 962{
5c01ba49
ON
963 return wo->wo_type == PIDTYPE_MAX ||
964 task_pid_type(p, wo->wo_type) == wo->wo_pid;
965}
1da177e4 966
bf959931
ON
967static int
968eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
5c01ba49
ON
969{
970 if (!eligible_pid(wo, p))
971 return 0;
bf959931
ON
972
973 /*
974 * Wait for all children (clone and not) if __WALL is set or
975 * if it is traced by us.
976 */
977 if (ptrace || (wo->wo_flags & __WALL))
978 return 1;
979
980 /*
981 * Otherwise, wait for clone children *only* if __WCLONE is set;
982 * otherwise, wait for non-clone children *only*.
983 *
984 * Note: a "clone" child here is one that reports to its parent
985 * using a signal other than SIGCHLD, or a non-leader thread which
986 * we can only see if it is traced by us.
987 */
988 if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
1da177e4 989 return 0;
1da177e4 990
14dd0b81 991 return 1;
1da177e4
LT
992}
993
1da177e4
LT
994/*
995 * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
996 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
997 * the lock and this task is uninteresting. If we return nonzero, we have
998 * released the lock and the system call should return.
999 */
9e8ae01d 1000static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1da177e4 1001{
67d7ddde 1002 int state, status;
6c5f3e7b 1003 pid_t pid = task_pid_vnr(p);
43e13cc1 1004 uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
67d7ddde 1005 struct waitid_info *infop;
1da177e4 1006
9e8ae01d 1007 if (!likely(wo->wo_flags & WEXITED))
98abed02
RM
1008 return 0;
1009
9e8ae01d 1010 if (unlikely(wo->wo_flags & WNOWAIT)) {
907c311f
EB
1011 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1012 ? p->signal->group_exit_code : p->exit_code;
1da177e4
LT
1013 get_task_struct(p);
1014 read_unlock(&tasklist_lock);
1029a2b5 1015 sched_annotate_sleep();
e61a2502
AV
1016 if (wo->wo_rusage)
1017 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
bb380ec3 1018 put_task_struct(p);
76d9871e 1019 goto out_info;
1da177e4 1020 }
1da177e4 1021 /*
abd50b39 1022 * Move the task's state to DEAD/TRACE, only one thread can do this.
1da177e4 1023 */
f6507f83
ON
1024 state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1025 EXIT_TRACE : EXIT_DEAD;
abd50b39 1026 if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1da177e4 1027 return 0;
986094df
ON
1028 /*
1029 * We own this thread, nobody else can reap it.
1030 */
1031 read_unlock(&tasklist_lock);
1032 sched_annotate_sleep();
f6507f83 1033
befca967 1034 /*
f6507f83 1035 * Check thread_group_leader() to exclude the traced sub-threads.
befca967 1036 */
f6507f83 1037 if (state == EXIT_DEAD && thread_group_leader(p)) {
f953ccd0
ON
1038 struct signal_struct *sig = p->signal;
1039 struct signal_struct *psig = current->signal;
1f10206c 1040 unsigned long maxrss;
5613fda9 1041 u64 tgutime, tgstime;
3795e161 1042
1da177e4
LT
1043 /*
1044 * The resource counters for the group leader are in its
1045 * own task_struct. Those for dead threads in the group
1046 * are in its signal_struct, as are those for the child
1047 * processes it has previously reaped. All these
1048 * accumulate in the parent's signal_struct c* fields.
1049 *
1050 * We don't bother to take a lock here to protect these
f953ccd0
ON
1051 * p->signal fields because the whole thread group is dead
1052 * and nobody can change them.
1053 *
1054 * psig->stats_lock also protects us from our sub-theads
1055 * which can reap other children at the same time. Until
1056 * we change k_getrusage()-like users to rely on this lock
1057 * we have to take ->siglock as well.
0cf55e1e 1058 *
a0be55de
IA
1059 * We use thread_group_cputime_adjusted() to get times for
1060 * the thread group, which consolidates times for all threads
1061 * in the group including the group leader.
1da177e4 1062 */
e80d0a1a 1063 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
f953ccd0 1064 spin_lock_irq(&current->sighand->siglock);
e78c3496 1065 write_seqlock(&psig->stats_lock);
64861634
MS
1066 psig->cutime += tgutime + sig->cutime;
1067 psig->cstime += tgstime + sig->cstime;
6fac4829 1068 psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
3795e161
JJ
1069 psig->cmin_flt +=
1070 p->min_flt + sig->min_flt + sig->cmin_flt;
1071 psig->cmaj_flt +=
1072 p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1073 psig->cnvcsw +=
1074 p->nvcsw + sig->nvcsw + sig->cnvcsw;
1075 psig->cnivcsw +=
1076 p->nivcsw + sig->nivcsw + sig->cnivcsw;
6eaeeaba
ED
1077 psig->cinblock +=
1078 task_io_get_inblock(p) +
1079 sig->inblock + sig->cinblock;
1080 psig->coublock +=
1081 task_io_get_oublock(p) +
1082 sig->oublock + sig->coublock;
1f10206c
JP
1083 maxrss = max(sig->maxrss, sig->cmaxrss);
1084 if (psig->cmaxrss < maxrss)
1085 psig->cmaxrss = maxrss;
5995477a
AR
1086 task_io_accounting_add(&psig->ioac, &p->ioac);
1087 task_io_accounting_add(&psig->ioac, &sig->ioac);
e78c3496 1088 write_sequnlock(&psig->stats_lock);
f953ccd0 1089 spin_unlock_irq(&current->sighand->siglock);
1da177e4
LT
1090 }
1091
ce72a16f
AV
1092 if (wo->wo_rusage)
1093 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1da177e4
LT
1094 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1095 ? p->signal->group_exit_code : p->exit_code;
359566fa 1096 wo->wo_stat = status;
2f4e6e2a 1097
b4360690 1098 if (state == EXIT_TRACE) {
1da177e4 1099 write_lock_irq(&tasklist_lock);
2f4e6e2a
ON
1100 /* We dropped tasklist, ptracer could die and untrace */
1101 ptrace_unlink(p);
b4360690
ON
1102
1103 /* If parent wants a zombie, don't release it now */
1104 state = EXIT_ZOMBIE;
1105 if (do_notify_parent(p, p->exit_signal))
1106 state = EXIT_DEAD;
abd50b39 1107 p->exit_state = state;
1da177e4
LT
1108 write_unlock_irq(&tasklist_lock);
1109 }
abd50b39 1110 if (state == EXIT_DEAD)
1da177e4 1111 release_task(p);
2f4e6e2a 1112
76d9871e
AV
1113out_info:
1114 infop = wo->wo_info;
1115 if (infop) {
1116 if ((status & 0x7f) == 0) {
1117 infop->cause = CLD_EXITED;
1118 infop->status = status >> 8;
1119 } else {
1120 infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1121 infop->status = status & 0x7f;
1122 }
1123 infop->pid = pid;
1124 infop->uid = uid;
1125 }
1126
67d7ddde 1127 return pid;
1da177e4
LT
1128}
1129
90bc8d8b
ON
1130static int *task_stopped_code(struct task_struct *p, bool ptrace)
1131{
1132 if (ptrace) {
570ac933 1133 if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
90bc8d8b
ON
1134 return &p->exit_code;
1135 } else {
1136 if (p->signal->flags & SIGNAL_STOP_STOPPED)
1137 return &p->signal->group_exit_code;
1138 }
1139 return NULL;
1140}
1141
19e27463
TH
1142/**
1143 * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
1144 * @wo: wait options
1145 * @ptrace: is the wait for ptrace
1146 * @p: task to wait for
1147 *
1148 * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
1149 *
1150 * CONTEXT:
1151 * read_lock(&tasklist_lock), which is released if return value is
1152 * non-zero. Also, grabs and releases @p->sighand->siglock.
1153 *
1154 * RETURNS:
1155 * 0 if wait condition didn't exist and search for other wait conditions
1156 * should continue. Non-zero return, -errno on failure and @p's pid on
1157 * success, implies that tasklist_lock is released and wait condition
1158 * search should terminate.
1da177e4 1159 */
9e8ae01d
ON
1160static int wait_task_stopped(struct wait_opts *wo,
1161 int ptrace, struct task_struct *p)
1da177e4 1162{
67d7ddde
AV
1163 struct waitid_info *infop;
1164 int exit_code, *p_code, why;
ee7c82da 1165 uid_t uid = 0; /* unneeded, required by compiler */
c8950783 1166 pid_t pid;
1da177e4 1167
47918025
ON
1168 /*
1169 * Traditionally we see ptrace'd stopped tasks regardless of options.
1170 */
9e8ae01d 1171 if (!ptrace && !(wo->wo_flags & WUNTRACED))
98abed02
RM
1172 return 0;
1173
19e27463
TH
1174 if (!task_stopped_code(p, ptrace))
1175 return 0;
1176
ee7c82da
ON
1177 exit_code = 0;
1178 spin_lock_irq(&p->sighand->siglock);
1179
90bc8d8b
ON
1180 p_code = task_stopped_code(p, ptrace);
1181 if (unlikely(!p_code))
ee7c82da
ON
1182 goto unlock_sig;
1183
90bc8d8b 1184 exit_code = *p_code;
ee7c82da
ON
1185 if (!exit_code)
1186 goto unlock_sig;
1187
9e8ae01d 1188 if (!unlikely(wo->wo_flags & WNOWAIT))
90bc8d8b 1189 *p_code = 0;
ee7c82da 1190
8ca937a6 1191 uid = from_kuid_munged(current_user_ns(), task_uid(p));
ee7c82da
ON
1192unlock_sig:
1193 spin_unlock_irq(&p->sighand->siglock);
1194 if (!exit_code)
1da177e4
LT
1195 return 0;
1196
1197 /*
1198 * Now we are pretty sure this task is interesting.
1199 * Make sure it doesn't get reaped out from under us while we
1200 * give up the lock and then examine it below. We don't want to
1201 * keep holding onto the tasklist_lock while we call getrusage and
1202 * possibly take page faults for user memory.
1203 */
1204 get_task_struct(p);
6c5f3e7b 1205 pid = task_pid_vnr(p);
f470021a 1206 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1da177e4 1207 read_unlock(&tasklist_lock);
1029a2b5 1208 sched_annotate_sleep();
e61a2502
AV
1209 if (wo->wo_rusage)
1210 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
bb380ec3 1211 put_task_struct(p);
1da177e4 1212
bb380ec3
AV
1213 if (likely(!(wo->wo_flags & WNOWAIT)))
1214 wo->wo_stat = (exit_code << 8) | 0x7f;
1da177e4 1215
9e8ae01d 1216 infop = wo->wo_info;
67d7ddde
AV
1217 if (infop) {
1218 infop->cause = why;
1219 infop->status = exit_code;
1220 infop->pid = pid;
1221 infop->uid = uid;
1222 }
67d7ddde 1223 return pid;
1da177e4
LT
1224}
1225
1226/*
1227 * Handle do_wait work for one task in a live, non-stopped state.
1228 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
1229 * the lock and this task is uninteresting. If we return nonzero, we have
1230 * released the lock and the system call should return.
1231 */
9e8ae01d 1232static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1da177e4 1233{
bb380ec3 1234 struct waitid_info *infop;
1da177e4
LT
1235 pid_t pid;
1236 uid_t uid;
1237
9e8ae01d 1238 if (!unlikely(wo->wo_flags & WCONTINUED))
98abed02
RM
1239 return 0;
1240
1da177e4
LT
1241 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1242 return 0;
1243
1244 spin_lock_irq(&p->sighand->siglock);
1245 /* Re-check with the lock held. */
1246 if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1247 spin_unlock_irq(&p->sighand->siglock);
1248 return 0;
1249 }
9e8ae01d 1250 if (!unlikely(wo->wo_flags & WNOWAIT))
1da177e4 1251 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
8ca937a6 1252 uid = from_kuid_munged(current_user_ns(), task_uid(p));
1da177e4
LT
1253 spin_unlock_irq(&p->sighand->siglock);
1254
6c5f3e7b 1255 pid = task_pid_vnr(p);
1da177e4
LT
1256 get_task_struct(p);
1257 read_unlock(&tasklist_lock);
1029a2b5 1258 sched_annotate_sleep();
e61a2502
AV
1259 if (wo->wo_rusage)
1260 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
bb380ec3 1261 put_task_struct(p);
1da177e4 1262
bb380ec3
AV
1263 infop = wo->wo_info;
1264 if (!infop) {
359566fa 1265 wo->wo_stat = 0xffff;
1da177e4 1266 } else {
bb380ec3
AV
1267 infop->cause = CLD_CONTINUED;
1268 infop->pid = pid;
1269 infop->uid = uid;
1270 infop->status = SIGCONT;
1da177e4 1271 }
bb380ec3 1272 return pid;
1da177e4
LT
1273}
1274
98abed02
RM
1275/*
1276 * Consider @p for a wait by @parent.
1277 *
9e8ae01d 1278 * -ECHILD should be in ->notask_error before the first call.
98abed02
RM
1279 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1280 * Returns zero if the search for a child should continue;
9e8ae01d 1281 * then ->notask_error is 0 if @p is an eligible child,
3a2f5a59 1282 * or still -ECHILD.
98abed02 1283 */
b6e763f0
ON
1284static int wait_consider_task(struct wait_opts *wo, int ptrace,
1285 struct task_struct *p)
98abed02 1286{
3245d6ac
ON
1287 /*
1288 * We can race with wait_task_zombie() from another thread.
1289 * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
1290 * can't confuse the checks below.
1291 */
6aa7de05 1292 int exit_state = READ_ONCE(p->exit_state);
b3ab0316
ON
1293 int ret;
1294
3245d6ac 1295 if (unlikely(exit_state == EXIT_DEAD))
b3ab0316
ON
1296 return 0;
1297
bf959931 1298 ret = eligible_child(wo, ptrace, p);
14dd0b81 1299 if (!ret)
98abed02
RM
1300 return ret;
1301
3245d6ac 1302 if (unlikely(exit_state == EXIT_TRACE)) {
50b8d257 1303 /*
abd50b39
ON
1304 * ptrace == 0 means we are the natural parent. In this case
1305 * we should clear notask_error, debugger will notify us.
50b8d257 1306 */
abd50b39 1307 if (likely(!ptrace))
50b8d257 1308 wo->notask_error = 0;
823b018e 1309 return 0;
50b8d257 1310 }
823b018e 1311
377d75da
ON
1312 if (likely(!ptrace) && unlikely(p->ptrace)) {
1313 /*
1314 * If it is traced by its real parent's group, just pretend
1315 * the caller is ptrace_do_wait() and reap this child if it
1316 * is zombie.
1317 *
1318 * This also hides group stop state from real parent; otherwise
1319 * a single stop can be reported twice as group and ptrace stop.
1320 * If a ptracer wants to distinguish these two events for its
1321 * own children it should create a separate process which takes
1322 * the role of real parent.
1323 */
1324 if (!ptrace_reparented(p))
1325 ptrace = 1;
1326 }
1327
45cb24a1 1328 /* slay zombie? */
3245d6ac 1329 if (exit_state == EXIT_ZOMBIE) {
9b84cca2 1330 /* we don't reap group leaders with subthreads */
7c733eb3
ON
1331 if (!delay_group_leader(p)) {
1332 /*
1333 * A zombie ptracee is only visible to its ptracer.
1334 * Notification and reaping will be cascaded to the
1335 * real parent when the ptracer detaches.
1336 */
1337 if (unlikely(ptrace) || likely(!p->ptrace))
1338 return wait_task_zombie(wo, p);
1339 }
98abed02 1340
f470021a 1341 /*
9b84cca2
TH
1342 * Allow access to stopped/continued state via zombie by
1343 * falling through. Clearing of notask_error is complex.
1344 *
1345 * When !@ptrace:
1346 *
1347 * If WEXITED is set, notask_error should naturally be
1348 * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
1349 * so, if there are live subthreads, there are events to
1350 * wait for. If all subthreads are dead, it's still safe
1351 * to clear - this function will be called again in finite
1352 * amount time once all the subthreads are released and
1353 * will then return without clearing.
1354 *
1355 * When @ptrace:
1356 *
1357 * Stopped state is per-task and thus can't change once the
1358 * target task dies. Only continued and exited can happen.
1359 * Clear notask_error if WCONTINUED | WEXITED.
1360 */
1361 if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
1362 wo->notask_error = 0;
1363 } else {
1364 /*
1365 * @p is alive and it's gonna stop, continue or exit, so
1366 * there always is something to wait for.
f470021a 1367 */
9e8ae01d 1368 wo->notask_error = 0;
f470021a
RM
1369 }
1370
98abed02 1371 /*
45cb24a1
TH
1372 * Wait for stopped. Depending on @ptrace, different stopped state
1373 * is used and the two don't interact with each other.
98abed02 1374 */
19e27463
TH
1375 ret = wait_task_stopped(wo, ptrace, p);
1376 if (ret)
1377 return ret;
98abed02
RM
1378
1379 /*
45cb24a1
TH
1380 * Wait for continued. There's only one continued state and the
1381 * ptracer can consume it which can confuse the real parent. Don't
1382 * use WCONTINUED from ptracer. You don't need or want it.
98abed02 1383 */
9e8ae01d 1384 return wait_task_continued(wo, p);
98abed02
RM
1385}
1386
1387/*
1388 * Do the work of do_wait() for one thread in the group, @tsk.
1389 *
9e8ae01d 1390 * -ECHILD should be in ->notask_error before the first call.
98abed02
RM
1391 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1392 * Returns zero if the search for a child should continue; then
9e8ae01d 1393 * ->notask_error is 0 if there were any eligible children,
3a2f5a59 1394 * or still -ECHILD.
98abed02 1395 */
9e8ae01d 1396static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
98abed02
RM
1397{
1398 struct task_struct *p;
1399
1400 list_for_each_entry(p, &tsk->children, sibling) {
9cd80bbb 1401 int ret = wait_consider_task(wo, 0, p);
a0be55de 1402
9cd80bbb
ON
1403 if (ret)
1404 return ret;
98abed02
RM
1405 }
1406
1407 return 0;
1408}
1409
9e8ae01d 1410static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
98abed02
RM
1411{
1412 struct task_struct *p;
1413
f470021a 1414 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
b6e763f0 1415 int ret = wait_consider_task(wo, 1, p);
a0be55de 1416
f470021a 1417 if (ret)
98abed02 1418 return ret;
98abed02
RM
1419 }
1420
1421 return 0;
1422}
1423
ac6424b9 1424static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
0b7570e7
ON
1425 int sync, void *key)
1426{
1427 struct wait_opts *wo = container_of(wait, struct wait_opts,
1428 child_wait);
1429 struct task_struct *p = key;
1430
5c01ba49 1431 if (!eligible_pid(wo, p))
0b7570e7
ON
1432 return 0;
1433
b4fe5182
ON
1434 if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
1435 return 0;
1436
0b7570e7
ON
1437 return default_wake_function(wait, mode, sync, key);
1438}
1439
a7f0765e
ON
1440void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1441{
0b7570e7 1442 __wake_up_sync_key(&parent->signal->wait_chldexit,
ce4dd442 1443 TASK_INTERRUPTIBLE, p);
a7f0765e
ON
1444}
1445
5449162a
JN
1446static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
1447 struct task_struct *target)
1448{
1449 struct task_struct *parent =
1450 !ptrace ? target->real_parent : target->parent;
1451
1452 return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
1453 same_thread_group(current, parent));
1454}
1455
1456/*
1457 * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
1458 * and tracee lists to find the target task.
1459 */
1460static int do_wait_pid(struct wait_opts *wo)
1461{
1462 bool ptrace;
1463 struct task_struct *target;
1464 int retval;
1465
1466 ptrace = false;
1467 target = pid_task(wo->wo_pid, PIDTYPE_TGID);
1468 if (target && is_effectively_child(wo, ptrace, target)) {
1469 retval = wait_consider_task(wo, ptrace, target);
1470 if (retval)
1471 return retval;
1472 }
1473
1474 ptrace = true;
1475 target = pid_task(wo->wo_pid, PIDTYPE_PID);
1476 if (target && target->ptrace &&
1477 is_effectively_child(wo, ptrace, target)) {
1478 retval = wait_consider_task(wo, ptrace, target);
1479 if (retval)
1480 return retval;
1481 }
1482
1483 return 0;
1484}
1485
9e8ae01d 1486static long do_wait(struct wait_opts *wo)
1da177e4 1487{
98abed02 1488 int retval;
1da177e4 1489
9e8ae01d 1490 trace_sched_process_wait(wo->wo_pid);
0a16b607 1491
0b7570e7
ON
1492 init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
1493 wo->child_wait.private = current;
1494 add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1da177e4 1495repeat:
98abed02 1496 /*
3da56d16 1497 * If there is nothing that can match our criteria, just get out.
9e8ae01d
ON
1498 * We will clear ->notask_error to zero if we see any child that
1499 * might later match our criteria, even if we are not able to reap
1500 * it yet.
98abed02 1501 */
64a16caf 1502 wo->notask_error = -ECHILD;
9e8ae01d 1503 if ((wo->wo_type < PIDTYPE_MAX) &&
1722c14a 1504 (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
64a16caf 1505 goto notask;
161550d7 1506
f95d39d1 1507 set_current_state(TASK_INTERRUPTIBLE);
1da177e4 1508 read_lock(&tasklist_lock);
9e8ae01d 1509
5449162a
JN
1510 if (wo->wo_type == PIDTYPE_PID) {
1511 retval = do_wait_pid(wo);
64a16caf 1512 if (retval)
98abed02 1513 goto end;
5449162a
JN
1514 } else {
1515 struct task_struct *tsk = current;
1516
1517 do {
1518 retval = do_wait_thread(wo, tsk);
1519 if (retval)
1520 goto end;
98abed02 1521
5449162a
JN
1522 retval = ptrace_do_wait(wo, tsk);
1523 if (retval)
1524 goto end;
1525
1526 if (wo->wo_flags & __WNOTHREAD)
1527 break;
1528 } while_each_thread(current, tsk);
1529 }
1da177e4 1530 read_unlock(&tasklist_lock);
f2cc3eb1 1531
64a16caf 1532notask:
9e8ae01d
ON
1533 retval = wo->notask_error;
1534 if (!retval && !(wo->wo_flags & WNOHANG)) {
1da177e4 1535 retval = -ERESTARTSYS;
98abed02
RM
1536 if (!signal_pending(current)) {
1537 schedule();
1538 goto repeat;
1539 }
1da177e4 1540 }
1da177e4 1541end:
f95d39d1 1542 __set_current_state(TASK_RUNNING);
0b7570e7 1543 remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1da177e4
LT
1544 return retval;
1545}
1546
67d7ddde 1547static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
ce72a16f 1548 int options, struct rusage *ru)
1da177e4 1549{
9e8ae01d 1550 struct wait_opts wo;
161550d7
EB
1551 struct pid *pid = NULL;
1552 enum pid_type type;
1da177e4 1553 long ret;
ba7d25f3 1554 unsigned int f_flags = 0;
1da177e4 1555
91c4e8ea
ON
1556 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
1557 __WNOTHREAD|__WCLONE|__WALL))
1da177e4
LT
1558 return -EINVAL;
1559 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1560 return -EINVAL;
1561
1562 switch (which) {
1563 case P_ALL:
161550d7 1564 type = PIDTYPE_MAX;
1da177e4
LT
1565 break;
1566 case P_PID:
161550d7
EB
1567 type = PIDTYPE_PID;
1568 if (upid <= 0)
1da177e4 1569 return -EINVAL;
3695eae5
CB
1570
1571 pid = find_get_pid(upid);
1da177e4
LT
1572 break;
1573 case P_PGID:
161550d7 1574 type = PIDTYPE_PGID;
821cc7b0 1575 if (upid < 0)
1da177e4 1576 return -EINVAL;
3695eae5 1577
821cc7b0
EB
1578 if (upid)
1579 pid = find_get_pid(upid);
1580 else
1581 pid = get_task_pid(current, PIDTYPE_PGID);
3695eae5
CB
1582 break;
1583 case P_PIDFD:
1584 type = PIDTYPE_PID;
1585 if (upid < 0)
1da177e4 1586 return -EINVAL;
3695eae5 1587
ba7d25f3 1588 pid = pidfd_get_pid(upid, &f_flags);
3695eae5
CB
1589 if (IS_ERR(pid))
1590 return PTR_ERR(pid);
ba7d25f3 1591
1da177e4
LT
1592 break;
1593 default:
1594 return -EINVAL;
1595 }
1596
9e8ae01d
ON
1597 wo.wo_type = type;
1598 wo.wo_pid = pid;
1599 wo.wo_flags = options;
1600 wo.wo_info = infop;
9e8ae01d 1601 wo.wo_rusage = ru;
ba7d25f3
CB
1602 if (f_flags & O_NONBLOCK)
1603 wo.wo_flags |= WNOHANG;
1604
9e8ae01d 1605 ret = do_wait(&wo);
ba7d25f3
CB
1606 if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK))
1607 ret = -EAGAIN;
dfe16dfa 1608
161550d7 1609 put_pid(pid);
1da177e4
LT
1610 return ret;
1611}
1612
ce72a16f
AV
1613SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1614 infop, int, options, struct rusage __user *, ru)
1615{
1616 struct rusage r;
67d7ddde
AV
1617 struct waitid_info info = {.status = 0};
1618 long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
634a8160 1619 int signo = 0;
6c85501f 1620
634a8160
AV
1621 if (err > 0) {
1622 signo = SIGCHLD;
1623 err = 0;
ce72a16f
AV
1624 if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1625 return -EFAULT;
1626 }
67d7ddde
AV
1627 if (!infop)
1628 return err;
1629
41cd7805 1630 if (!user_write_access_begin(infop, sizeof(*infop)))
1c9fec47 1631 return -EFAULT;
96ca579a 1632
634a8160 1633 unsafe_put_user(signo, &infop->si_signo, Efault);
4c48abe9 1634 unsafe_put_user(0, &infop->si_errno, Efault);
cc731525 1635 unsafe_put_user(info.cause, &infop->si_code, Efault);
4c48abe9
AV
1636 unsafe_put_user(info.pid, &infop->si_pid, Efault);
1637 unsafe_put_user(info.uid, &infop->si_uid, Efault);
1638 unsafe_put_user(info.status, &infop->si_status, Efault);
41cd7805 1639 user_write_access_end();
ce72a16f 1640 return err;
4c48abe9 1641Efault:
41cd7805 1642 user_write_access_end();
4c48abe9 1643 return -EFAULT;
ce72a16f
AV
1644}
1645
92ebce5a
AV
1646long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
1647 struct rusage *ru)
1da177e4 1648{
9e8ae01d 1649 struct wait_opts wo;
161550d7
EB
1650 struct pid *pid = NULL;
1651 enum pid_type type;
1da177e4
LT
1652 long ret;
1653
1654 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1655 __WNOTHREAD|__WCLONE|__WALL))
1656 return -EINVAL;
161550d7 1657
dd83c161 1658 /* -INT_MIN is not defined */
1659 if (upid == INT_MIN)
1660 return -ESRCH;
1661
161550d7
EB
1662 if (upid == -1)
1663 type = PIDTYPE_MAX;
1664 else if (upid < 0) {
1665 type = PIDTYPE_PGID;
1666 pid = find_get_pid(-upid);
1667 } else if (upid == 0) {
1668 type = PIDTYPE_PGID;
2ae448ef 1669 pid = get_task_pid(current, PIDTYPE_PGID);
161550d7
EB
1670 } else /* upid > 0 */ {
1671 type = PIDTYPE_PID;
1672 pid = find_get_pid(upid);
1673 }
1674
9e8ae01d
ON
1675 wo.wo_type = type;
1676 wo.wo_pid = pid;
1677 wo.wo_flags = options | WEXITED;
1678 wo.wo_info = NULL;
359566fa 1679 wo.wo_stat = 0;
9e8ae01d
ON
1680 wo.wo_rusage = ru;
1681 ret = do_wait(&wo);
161550d7 1682 put_pid(pid);
359566fa
AV
1683 if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
1684 ret = -EFAULT;
1da177e4 1685
1da177e4
LT
1686 return ret;
1687}
1688
8043fc14
CH
1689int kernel_wait(pid_t pid, int *stat)
1690{
1691 struct wait_opts wo = {
1692 .wo_type = PIDTYPE_PID,
1693 .wo_pid = find_get_pid(pid),
1694 .wo_flags = WEXITED,
1695 };
1696 int ret;
1697
1698 ret = do_wait(&wo);
1699 if (ret > 0 && wo.wo_stat)
1700 *stat = wo.wo_stat;
1701 put_pid(wo.wo_pid);
1702 return ret;
1703}
1704
ce72a16f
AV
1705SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1706 int, options, struct rusage __user *, ru)
1707{
1708 struct rusage r;
1709 long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
1710
1711 if (err > 0) {
1712 if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1713 return -EFAULT;
1714 }
1715 return err;
1716}
1717
1da177e4
LT
1718#ifdef __ARCH_WANT_SYS_WAITPID
1719
1720/*
1721 * sys_waitpid() remains for compatibility. waitpid() should be
1722 * implemented by calling sys_wait4() from libc.a.
1723 */
17da2bd9 1724SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1da177e4 1725{
d300b610 1726 return kernel_wait4(pid, stat_addr, options, NULL);
1da177e4
LT
1727}
1728
1729#endif
7e95a225
AV
1730
1731#ifdef CONFIG_COMPAT
1732COMPAT_SYSCALL_DEFINE4(wait4,
1733 compat_pid_t, pid,
1734 compat_uint_t __user *, stat_addr,
1735 int, options,
1736 struct compat_rusage __user *, ru)
1737{
ce72a16f
AV
1738 struct rusage r;
1739 long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
1740 if (err > 0) {
1741 if (ru && put_compat_rusage(&r, ru))
1742 return -EFAULT;
7e95a225 1743 }
ce72a16f 1744 return err;
7e95a225
AV
1745}
1746
1747COMPAT_SYSCALL_DEFINE5(waitid,
1748 int, which, compat_pid_t, pid,
1749 struct compat_siginfo __user *, infop, int, options,
1750 struct compat_rusage __user *, uru)
1751{
7e95a225 1752 struct rusage ru;
67d7ddde
AV
1753 struct waitid_info info = {.status = 0};
1754 long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
634a8160
AV
1755 int signo = 0;
1756 if (err > 0) {
1757 signo = SIGCHLD;
1758 err = 0;
6c85501f
AV
1759 if (uru) {
1760 /* kernel_waitid() overwrites everything in ru */
1761 if (COMPAT_USE_64BIT_TIME)
1762 err = copy_to_user(uru, &ru, sizeof(ru));
1763 else
1764 err = put_compat_rusage(&ru, uru);
1765 if (err)
1766 return -EFAULT;
1767 }
7e95a225
AV
1768 }
1769
4c48abe9
AV
1770 if (!infop)
1771 return err;
1772
41cd7805 1773 if (!user_write_access_begin(infop, sizeof(*infop)))
1c9fec47 1774 return -EFAULT;
96ca579a 1775
634a8160 1776 unsafe_put_user(signo, &infop->si_signo, Efault);
4c48abe9 1777 unsafe_put_user(0, &infop->si_errno, Efault);
cc731525 1778 unsafe_put_user(info.cause, &infop->si_code, Efault);
4c48abe9
AV
1779 unsafe_put_user(info.pid, &infop->si_pid, Efault);
1780 unsafe_put_user(info.uid, &infop->si_uid, Efault);
1781 unsafe_put_user(info.status, &infop->si_status, Efault);
41cd7805 1782 user_write_access_end();
67d7ddde 1783 return err;
4c48abe9 1784Efault:
41cd7805 1785 user_write_access_end();
4c48abe9 1786 return -EFAULT;
7e95a225
AV
1787}
1788#endif
7c2c11b2 1789
38fd525a
EB
1790/**
1791 * thread_group_exited - check that a thread group has exited
1792 * @pid: tgid of thread group to be checked.
1793 *
1794 * Test if the thread group represented by tgid has exited (all
1795 * threads are zombies, dead or completely gone).
1796 *
1797 * Return: true if the thread group has exited. false otherwise.
1798 */
1799bool thread_group_exited(struct pid *pid)
1800{
1801 struct task_struct *task;
1802 bool exited;
1803
1804 rcu_read_lock();
1805 task = pid_task(pid, PIDTYPE_PID);
1806 exited = !task ||
1807 (READ_ONCE(task->exit_state) && thread_group_empty(task));
1808 rcu_read_unlock();
1809
1810 return exited;
1811}
1812EXPORT_SYMBOL(thread_group_exited);
1813
7c2c11b2
SM
1814__weak void abort(void)
1815{
1816 BUG();
1817
1818 /* if that doesn't kill us, halt */
1819 panic("Oops failed to kill thread");
1820}
dc8635b7 1821EXPORT_SYMBOL(abort);