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