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