]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/exit.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf
[mirror_ubuntu-bionic-kernel.git] / kernel / exit.c
1 /*
2 * linux/kernel/exit.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/capability.h>
12 #include <linux/completion.h>
13 #include <linux/personality.h>
14 #include <linux/tty.h>
15 #include <linux/iocontext.h>
16 #include <linux/key.h>
17 #include <linux/cpu.h>
18 #include <linux/acct.h>
19 #include <linux/tsacct_kern.h>
20 #include <linux/file.h>
21 #include <linux/fdtable.h>
22 #include <linux/freezer.h>
23 #include <linux/binfmts.h>
24 #include <linux/nsproxy.h>
25 #include <linux/pid_namespace.h>
26 #include <linux/ptrace.h>
27 #include <linux/profile.h>
28 #include <linux/mount.h>
29 #include <linux/proc_fs.h>
30 #include <linux/kthread.h>
31 #include <linux/mempolicy.h>
32 #include <linux/taskstats_kern.h>
33 #include <linux/delayacct.h>
34 #include <linux/cgroup.h>
35 #include <linux/syscalls.h>
36 #include <linux/signal.h>
37 #include <linux/posix-timers.h>
38 #include <linux/cn_proc.h>
39 #include <linux/mutex.h>
40 #include <linux/futex.h>
41 #include <linux/pipe_fs_i.h>
42 #include <linux/audit.h> /* for audit_free() */
43 #include <linux/resource.h>
44 #include <linux/blkdev.h>
45 #include <linux/task_io_accounting_ops.h>
46 #include <linux/tracehook.h>
47 #include <linux/fs_struct.h>
48 #include <linux/init_task.h>
49 #include <linux/perf_event.h>
50 #include <trace/events/sched.h>
51 #include <linux/hw_breakpoint.h>
52 #include <linux/oom.h>
53 #include <linux/writeback.h>
54 #include <linux/shm.h>
55 #include <linux/kcov.h>
56 #include <linux/random.h>
57 #include <linux/rcuwait.h>
58
59 #include <linux/uaccess.h>
60 #include <asm/unistd.h>
61 #include <asm/pgtable.h>
62 #include <asm/mmu_context.h>
63
64 static void __unhash_process(struct task_struct *p, bool group_dead)
65 {
66 nr_threads--;
67 detach_pid(p, PIDTYPE_PID);
68 if (group_dead) {
69 detach_pid(p, PIDTYPE_PGID);
70 detach_pid(p, PIDTYPE_SID);
71
72 list_del_rcu(&p->tasks);
73 list_del_init(&p->sibling);
74 __this_cpu_dec(process_counts);
75 }
76 list_del_rcu(&p->thread_group);
77 list_del_rcu(&p->thread_node);
78 }
79
80 /*
81 * This function expects the tasklist_lock write-locked.
82 */
83 static void __exit_signal(struct task_struct *tsk)
84 {
85 struct signal_struct *sig = tsk->signal;
86 bool group_dead = thread_group_leader(tsk);
87 struct sighand_struct *sighand;
88 struct tty_struct *uninitialized_var(tty);
89 u64 utime, stime;
90
91 sighand = rcu_dereference_check(tsk->sighand,
92 lockdep_tasklist_lock_is_held());
93 spin_lock(&sighand->siglock);
94
95 #ifdef CONFIG_POSIX_TIMERS
96 posix_cpu_timers_exit(tsk);
97 if (group_dead) {
98 posix_cpu_timers_exit_group(tsk);
99 } else {
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);
107 }
108 #endif
109
110 if (group_dead) {
111 tty = sig->tty;
112 sig->tty = NULL;
113 } else {
114 /*
115 * If there is any task waiting for the group exit
116 * then notify it:
117 */
118 if (sig->notify_count > 0 && !--sig->notify_count)
119 wake_up_process(sig->group_exit_task);
120
121 if (tsk == sig->curr_target)
122 sig->curr_target = next_thread(tsk);
123 }
124
125 add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
126 sizeof(unsigned long long));
127
128 /*
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.
133 */
134 task_cputime(tsk, &utime, &stime);
135 write_seqlock(&sig->stats_lock);
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;
147 sig->nr_threads--;
148 __unhash_process(tsk, group_dead);
149 write_sequnlock(&sig->stats_lock);
150
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);
156 tsk->sighand = NULL;
157 spin_unlock(&sighand->siglock);
158
159 __cleanup_sighand(sighand);
160 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
161 if (group_dead) {
162 flush_sigqueue(&sig->shared_pending);
163 tty_kref_put(tty);
164 }
165 }
166
167 static void delayed_put_task_struct(struct rcu_head *rhp)
168 {
169 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
170
171 perf_event_delayed_put(tsk);
172 trace_sched_process_free(tsk);
173 put_task_struct(tsk);
174 }
175
176
177 void release_task(struct task_struct *p)
178 {
179 struct task_struct *leader;
180 int zap_leader;
181 repeat:
182 /* don't need to get the RCU readlock here - the process is dead and
183 * can't be modifying its own credentials. But shut RCU-lockdep up */
184 rcu_read_lock();
185 atomic_dec(&__task_cred(p)->user->processes);
186 rcu_read_unlock();
187
188 proc_flush_task(p);
189
190 write_lock_irq(&tasklist_lock);
191 ptrace_release_task(p);
192 __exit_signal(p);
193
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;
201 if (leader != p && thread_group_empty(leader)
202 && leader->exit_state == EXIT_ZOMBIE) {
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.
207 */
208 zap_leader = do_notify_parent(leader, leader->exit_signal);
209 if (zap_leader)
210 leader->exit_state = EXIT_DEAD;
211 }
212
213 write_unlock_irq(&tasklist_lock);
214 release_thread(p);
215 call_rcu(&p->rcu, delayed_put_task_struct);
216
217 p = leader;
218 if (unlikely(zap_leader))
219 goto repeat;
220 }
221
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 */
226 struct 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 */
237 retry:
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
285 void 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
314 struct 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
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 */
335 static int will_become_orphaned_pgrp(struct pid *pgrp,
336 struct task_struct *ignored_task)
337 {
338 struct task_struct *p;
339
340 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
341 if ((p == ignored_task) ||
342 (p->exit_state && thread_group_empty(p)) ||
343 is_global_init(p->real_parent))
344 continue;
345
346 if (task_pgrp(p->real_parent) != pgrp &&
347 task_session(p->real_parent) == task_session(p))
348 return 0;
349 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
350
351 return 1;
352 }
353
354 int is_current_pgrp_orphaned(void)
355 {
356 int retval;
357
358 read_lock(&tasklist_lock);
359 retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
360 read_unlock(&tasklist_lock);
361
362 return retval;
363 }
364
365 static bool has_stopped_jobs(struct pid *pgrp)
366 {
367 struct task_struct *p;
368
369 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
370 if (p->signal->flags & SIGNAL_STOP_STOPPED)
371 return true;
372 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
373
374 return false;
375 }
376
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 */
382 static void
383 kill_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)
389 /* exit: our father is in a different pgrp than
390 * we are and we were the only connection outside.
391 */
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
408 #ifdef CONFIG_MEMCG
409 /*
410 * A task is exiting. If it owned this mm, find a new owner for the mm.
411 */
412 void mm_update_next_owner(struct mm_struct *mm)
413 {
414 struct task_struct *c, *g, *p = current;
415
416 retry:
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)
422 return;
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 }
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 */
445 list_for_each_entry(c, &p->real_parent->children, sibling) {
446 if (c->mm == mm)
447 goto assign_new_owner;
448 }
449
450 /*
451 * Search through everything else, we should not get here often.
452 */
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 }
462 }
463 read_unlock(&tasklist_lock);
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
467 * ptrace or page migration (get_task_mm()). Mark owner as NULL.
468 */
469 mm->owner = NULL;
470 return;
471
472 assign_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);
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);
485 if (c->mm != mm) {
486 task_unlock(c);
487 put_task_struct(c);
488 goto retry;
489 }
490 mm->owner = c;
491 task_unlock(c);
492 put_task_struct(c);
493 }
494 #endif /* CONFIG_MEMCG */
495
496 /*
497 * Turn us into a lazy TLB process if we
498 * aren't already..
499 */
500 static void exit_mm(void)
501 {
502 struct mm_struct *mm = current->mm;
503 struct core_state *core_state;
504
505 mm_release(current, mm);
506 if (!mm)
507 return;
508 sync_mm_rss(mm);
509 /*
510 * Serialize with any possible pending coredump.
511 * We must hold mmap_sem around checking core_state
512 * and clearing tsk->mm. The core-inducing thread
513 * will increment ->nr_threads for each thread in the
514 * group with ->mm != NULL.
515 */
516 down_read(&mm->mmap_sem);
517 core_state = mm->core_state;
518 if (core_state) {
519 struct core_thread self;
520
521 up_read(&mm->mmap_sem);
522
523 self.task = current;
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);
531
532 for (;;) {
533 set_current_state(TASK_UNINTERRUPTIBLE);
534 if (!self.task) /* see coredump_finish() */
535 break;
536 freezable_schedule();
537 }
538 __set_current_state(TASK_RUNNING);
539 down_read(&mm->mmap_sem);
540 }
541 atomic_inc(&mm->mm_count);
542 BUG_ON(mm != current->active_mm);
543 /* more a memory barrier than a real lock */
544 task_lock(current);
545 current->mm = NULL;
546 up_read(&mm->mmap_sem);
547 enter_lazy_tlb(mm, current);
548 task_unlock(current);
549 mm_update_next_owner(mm);
550 mmput(mm);
551 if (test_thread_flag(TIF_MEMDIE))
552 exit_oom_victim();
553 }
554
555 static 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
566 static 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
576 reaper = find_alive_thread(father);
577 if (reaper) {
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
593 /*
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
599 */
600 static struct task_struct *find_new_reaper(struct task_struct *father,
601 struct task_struct *child_reaper)
602 {
603 struct task_struct *thread, *reaper;
604
605 thread = find_alive_thread(father);
606 if (thread)
607 return thread;
608
609 if (father->signal->has_child_subreaper) {
610 unsigned int ns_level = task_pid(father)->level;
611 /*
612 * Find the first ->is_child_subreaper ancestor in our pid_ns.
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).
618 */
619 for (reaper = father->real_parent;
620 task_pid(reaper)->level == ns_level;
621 reaper = reaper->real_parent) {
622 if (reaper == &init_task)
623 break;
624 if (!reaper->signal->is_child_subreaper)
625 continue;
626 thread = find_alive_thread(reaper);
627 if (thread)
628 return thread;
629 }
630 }
631
632 return child_reaper;
633 }
634
635 /*
636 * Any that need to be release_task'd are put on the @dead list.
637 */
638 static void reparent_leader(struct task_struct *father, struct task_struct *p,
639 struct list_head *dead)
640 {
641 if (unlikely(p->exit_state == EXIT_DEAD))
642 return;
643
644 /* We don't want people slaying init. */
645 p->exit_signal = SIGCHLD;
646
647 /* If it has exited notify the new parent about this child's death. */
648 if (!p->ptrace &&
649 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
650 if (do_notify_parent(p, p->exit_signal)) {
651 p->exit_state = EXIT_DEAD;
652 list_add(&p->ptrace_entry, dead);
653 }
654 }
655
656 kill_orphaned_pgrp(p, father);
657 }
658
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 */
667 static void forget_original_parent(struct task_struct *father,
668 struct list_head *dead)
669 {
670 struct task_struct *p, *t, *reaper;
671
672 if (unlikely(!list_empty(&father->ptraced)))
673 exit_ptrace(father, dead);
674
675 /* Can drop and reacquire tasklist_lock */
676 reaper = find_child_reaper(father);
677 if (list_empty(&father->children))
678 return;
679
680 reaper = find_new_reaper(father, reaper);
681 list_for_each_entry(p, &father->children, sibling) {
682 for_each_thread(p, t) {
683 t->real_parent = reaper;
684 BUG_ON((!t->ptrace) != (t->parent == father));
685 if (likely(!t->ptrace))
686 t->parent = t->real_parent;
687 if (t->pdeath_signal)
688 group_send_sig_info(t->pdeath_signal,
689 SEND_SIG_NOINFO, t);
690 }
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))
696 reparent_leader(father, p, dead);
697 }
698 list_splice_tail_init(&father->children, &reaper->children);
699 }
700
701 /*
702 * Send signals to all our closest relatives so that they know
703 * to properly mourn us..
704 */
705 static void exit_notify(struct task_struct *tsk, int group_dead)
706 {
707 bool autoreap;
708 struct task_struct *p, *n;
709 LIST_HEAD(dead);
710
711 write_lock_irq(&tasklist_lock);
712 forget_original_parent(tsk, &dead);
713
714 if (group_dead)
715 kill_orphaned_pgrp(tsk->group_leader, NULL);
716
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 }
729
730 tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
731 if (tsk->exit_state == EXIT_DEAD)
732 list_add(&tsk->ptrace_entry, &dead);
733
734 /* mt-exec, de_thread() is waiting for group leader */
735 if (unlikely(tsk->signal->notify_count < 0))
736 wake_up_process(tsk->signal->group_exit_task);
737 write_unlock_irq(&tasklist_lock);
738
739 list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
740 list_del_init(&p->ptrace_entry);
741 release_task(p);
742 }
743 }
744
745 #ifdef CONFIG_DEBUG_STACK_USAGE
746 static void check_stack_usage(void)
747 {
748 static DEFINE_SPINLOCK(low_water_lock);
749 static int lowest_to_date = THREAD_SIZE;
750 unsigned long free;
751
752 free = stack_not_used(current);
753
754 if (free >= lowest_to_date)
755 return;
756
757 spin_lock(&low_water_lock);
758 if (free < lowest_to_date) {
759 pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
760 current->comm, task_pid_nr(current), free);
761 lowest_to_date = free;
762 }
763 spin_unlock(&low_water_lock);
764 }
765 #else
766 static inline void check_stack_usage(void) {}
767 #endif
768
769 void __noreturn do_exit(long code)
770 {
771 struct task_struct *tsk = current;
772 int group_dead;
773 TASKS_RCU(int tasks_rcu_i);
774
775 profile_task_exit(tsk);
776 kcov_task_exit(tsk);
777
778 WARN_ON(blk_needs_flush_plug(tsk));
779
780 if (unlikely(in_interrupt()))
781 panic("Aiee, killing interrupt handler!");
782 if (unlikely(!tsk->pid))
783 panic("Attempted to kill the idle task!");
784
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
794 ptrace_event(PTRACE_EVENT_EXIT, code);
795
796 validate_creds_for_do_exit(tsk);
797
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)) {
803 pr_alert("Fixing recursive fault but reboot is needed!\n");
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;
814 set_current_state(TASK_UNINTERRUPTIBLE);
815 schedule();
816 }
817
818 exit_signals(tsk); /* sets PF_EXITING */
819 /*
820 * Ensure that all new tsk->pi_lock acquisitions must observe
821 * PF_EXITING. Serializes against futex.c:attach_to_pi_owner().
822 */
823 smp_mb();
824 /*
825 * Ensure that we must observe the pi_state in exit_mm() ->
826 * mm_release() -> exit_pi_state_list().
827 */
828 raw_spin_unlock_wait(&tsk->pi_lock);
829
830 if (unlikely(in_atomic())) {
831 pr_info("note: %s[%d] exited with preempt_count %d\n",
832 current->comm, task_pid_nr(current),
833 preempt_count());
834 preempt_count_set(PREEMPT_ENABLED);
835 }
836
837 /* sync mm's RSS info before statistics gathering */
838 if (tsk->mm)
839 sync_mm_rss(tsk->mm);
840 acct_update_integrals(tsk);
841 group_dead = atomic_dec_and_test(&tsk->signal->live);
842 if (group_dead) {
843 #ifdef CONFIG_POSIX_TIMERS
844 hrtimer_cancel(&tsk->signal->real_timer);
845 exit_itimers(tsk->signal);
846 #endif
847 if (tsk->mm)
848 setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
849 }
850 acct_collect(code, group_dead);
851 if (group_dead)
852 tty_audit_exit();
853 audit_free(tsk);
854
855 tsk->exit_code = code;
856 taskstats_exit(tsk, group_dead);
857
858 exit_mm();
859
860 if (group_dead)
861 acct_process();
862 trace_sched_process_exit(tsk);
863
864 exit_sem(tsk);
865 exit_shm(tsk);
866 exit_files(tsk);
867 exit_fs(tsk);
868 if (group_dead)
869 disassociate_ctty(1);
870 exit_task_namespaces(tsk);
871 exit_task_work(tsk);
872 exit_thread(tsk);
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
882 sched_autogroup_exit_task(tsk);
883 cgroup_exit(tsk);
884
885 /*
886 * FIXME: do that only when needed, using sched_exit tracepoint
887 */
888 flush_ptrace_hw_breakpoint(tsk);
889
890 TASKS_RCU(preempt_disable());
891 TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
892 TASKS_RCU(preempt_enable());
893 exit_notify(tsk, group_dead);
894 proc_exit_connector(tsk);
895 mpol_put_task_policy(tsk);
896 #ifdef CONFIG_FUTEX
897 if (unlikely(current->pi_state_cache))
898 kfree(current->pi_state_cache);
899 #endif
900 /*
901 * Make sure we are holding no locks:
902 */
903 debug_check_no_locks_held();
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;
910
911 if (tsk->io_context)
912 exit_io_context(tsk);
913
914 if (tsk->splice_pipe)
915 free_pipe_info(tsk->splice_pipe);
916
917 if (tsk->task_frag.page)
918 put_page(tsk->task_frag.page);
919
920 validate_creds_for_do_exit(tsk);
921
922 check_stack_usage();
923 preempt_disable();
924 if (tsk->nr_dirtied)
925 __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
926 exit_rcu();
927 TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
928
929 do_task_dead();
930 }
931 EXPORT_SYMBOL_GPL(do_exit);
932
933 void complete_and_exit(struct completion *comp, long code)
934 {
935 if (comp)
936 complete(comp);
937
938 do_exit(code);
939 }
940 EXPORT_SYMBOL(complete_and_exit);
941
942 SYSCALL_DEFINE1(exit, int, error_code)
943 {
944 do_exit((error_code&0xff)<<8);
945 }
946
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 */
951 void
952 do_group_exit(int exit_code)
953 {
954 struct signal_struct *sig = current->signal;
955
956 BUG_ON(exit_code & 0x80); /* core dumps don't get here */
957
958 if (signal_group_exit(sig))
959 exit_code = sig->group_exit_code;
960 else if (!thread_group_empty(current)) {
961 struct sighand_struct *const sighand = current->sighand;
962
963 spin_lock_irq(&sighand->siglock);
964 if (signal_group_exit(sig))
965 /* Another thread got here before we took the lock. */
966 exit_code = sig->group_exit_code;
967 else {
968 sig->group_exit_code = exit_code;
969 sig->flags = SIGNAL_GROUP_EXIT;
970 zap_other_threads(current);
971 }
972 spin_unlock_irq(&sighand->siglock);
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 */
984 SYSCALL_DEFINE1(exit_group, int, error_code)
985 {
986 do_group_exit((error_code & 0xff) << 8);
987 /* NOTREACHED */
988 return 0;
989 }
990
991 struct wait_opts {
992 enum pid_type wo_type;
993 int wo_flags;
994 struct pid *wo_pid;
995
996 struct siginfo __user *wo_info;
997 int __user *wo_stat;
998 struct rusage __user *wo_rusage;
999
1000 wait_queue_t child_wait;
1001 int notask_error;
1002 };
1003
1004 static inline
1005 struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
1006 {
1007 if (type != PIDTYPE_PID)
1008 task = task->group_leader;
1009 return task->pids[type].pid;
1010 }
1011
1012 static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
1013 {
1014 return wo->wo_type == PIDTYPE_MAX ||
1015 task_pid_type(p, wo->wo_type) == wo->wo_pid;
1016 }
1017
1018 static int
1019 eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
1020 {
1021 if (!eligible_pid(wo, p))
1022 return 0;
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))
1040 return 0;
1041
1042 return 1;
1043 }
1044
1045 static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
1046 pid_t pid, uid_t uid, int why, int status)
1047 {
1048 struct siginfo __user *infop;
1049 int retval = wo->wo_rusage
1050 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1051
1052 put_task_struct(p);
1053 infop = wo->wo_info;
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 }
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 */
1079 static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1080 {
1081 int state, retval, status;
1082 pid_t pid = task_pid_vnr(p);
1083 uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
1084 struct siginfo __user *infop;
1085
1086 if (!likely(wo->wo_flags & WEXITED))
1087 return 0;
1088
1089 if (unlikely(wo->wo_flags & WNOWAIT)) {
1090 int exit_code = p->exit_code;
1091 int why;
1092
1093 get_task_struct(p);
1094 read_unlock(&tasklist_lock);
1095 sched_annotate_sleep();
1096
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 }
1104 return wait_noreap_copyout(wo, p, pid, uid, why, status);
1105 }
1106 /*
1107 * Move the task's state to DEAD/TRACE, only one thread can do this.
1108 */
1109 state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1110 EXIT_TRACE : EXIT_DEAD;
1111 if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1112 return 0;
1113 /*
1114 * We own this thread, nobody else can reap it.
1115 */
1116 read_unlock(&tasklist_lock);
1117 sched_annotate_sleep();
1118
1119 /*
1120 * Check thread_group_leader() to exclude the traced sub-threads.
1121 */
1122 if (state == EXIT_DEAD && thread_group_leader(p)) {
1123 struct signal_struct *sig = p->signal;
1124 struct signal_struct *psig = current->signal;
1125 unsigned long maxrss;
1126 u64 tgutime, tgstime;
1127
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
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.
1143 *
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.
1147 */
1148 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1149 spin_lock_irq(&current->sighand->siglock);
1150 write_seqlock(&psig->stats_lock);
1151 psig->cutime += tgutime + sig->cutime;
1152 psig->cstime += tgstime + sig->cstime;
1153 psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
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;
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;
1168 maxrss = max(sig->maxrss, sig->cmaxrss);
1169 if (psig->cmaxrss < maxrss)
1170 psig->cmaxrss = maxrss;
1171 task_io_accounting_add(&psig->ioac, &p->ioac);
1172 task_io_accounting_add(&psig->ioac, &sig->ioac);
1173 write_sequnlock(&psig->stats_lock);
1174 spin_unlock_irq(&current->sighand->siglock);
1175 }
1176
1177 retval = wo->wo_rusage
1178 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1179 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1180 ? p->signal->group_exit_code : p->exit_code;
1181 if (!retval && wo->wo_stat)
1182 retval = put_user(status, wo->wo_stat);
1183
1184 infop = wo->wo_info;
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)
1204 retval = put_user(pid, &infop->si_pid);
1205 if (!retval && infop)
1206 retval = put_user(uid, &infop->si_uid);
1207 if (!retval)
1208 retval = pid;
1209
1210 if (state == EXIT_TRACE) {
1211 write_lock_irq(&tasklist_lock);
1212 /* We dropped tasklist, ptracer could die and untrace */
1213 ptrace_unlink(p);
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;
1219 p->exit_state = state;
1220 write_unlock_irq(&tasklist_lock);
1221 }
1222 if (state == EXIT_DEAD)
1223 release_task(p);
1224
1225 return retval;
1226 }
1227
1228 static int *task_stopped_code(struct task_struct *p, bool ptrace)
1229 {
1230 if (ptrace) {
1231 if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
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
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.
1257 */
1258 static int wait_task_stopped(struct wait_opts *wo,
1259 int ptrace, struct task_struct *p)
1260 {
1261 struct siginfo __user *infop;
1262 int retval, exit_code, *p_code, why;
1263 uid_t uid = 0; /* unneeded, required by compiler */
1264 pid_t pid;
1265
1266 /*
1267 * Traditionally we see ptrace'd stopped tasks regardless of options.
1268 */
1269 if (!ptrace && !(wo->wo_flags & WUNTRACED))
1270 return 0;
1271
1272 if (!task_stopped_code(p, ptrace))
1273 return 0;
1274
1275 exit_code = 0;
1276 spin_lock_irq(&p->sighand->siglock);
1277
1278 p_code = task_stopped_code(p, ptrace);
1279 if (unlikely(!p_code))
1280 goto unlock_sig;
1281
1282 exit_code = *p_code;
1283 if (!exit_code)
1284 goto unlock_sig;
1285
1286 if (!unlikely(wo->wo_flags & WNOWAIT))
1287 *p_code = 0;
1288
1289 uid = from_kuid_munged(current_user_ns(), task_uid(p));
1290 unlock_sig:
1291 spin_unlock_irq(&p->sighand->siglock);
1292 if (!exit_code)
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);
1303 pid = task_pid_vnr(p);
1304 why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1305 read_unlock(&tasklist_lock);
1306 sched_annotate_sleep();
1307
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);
1315
1316 infop = wo->wo_info;
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)
1322 retval = put_user((short)why, &infop->si_code);
1323 if (!retval && infop)
1324 retval = put_user(exit_code, &infop->si_status);
1325 if (!retval && infop)
1326 retval = put_user(pid, &infop->si_pid);
1327 if (!retval && infop)
1328 retval = put_user(uid, &infop->si_uid);
1329 if (!retval)
1330 retval = pid;
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 */
1343 static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1344 {
1345 int retval;
1346 pid_t pid;
1347 uid_t uid;
1348
1349 if (!unlikely(wo->wo_flags & WCONTINUED))
1350 return 0;
1351
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 }
1361 if (!unlikely(wo->wo_flags & WNOWAIT))
1362 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1363 uid = from_kuid_munged(current_user_ns(), task_uid(p));
1364 spin_unlock_irq(&p->sighand->siglock);
1365
1366 pid = task_pid_vnr(p);
1367 get_task_struct(p);
1368 read_unlock(&tasklist_lock);
1369 sched_annotate_sleep();
1370
1371 if (!wo->wo_info) {
1372 retval = wo->wo_rusage
1373 ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
1374 put_task_struct(p);
1375 if (!retval && wo->wo_stat)
1376 retval = put_user(0xffff, wo->wo_stat);
1377 if (!retval)
1378 retval = pid;
1379 } else {
1380 retval = wait_noreap_copyout(wo, p, pid, uid,
1381 CLD_CONTINUED, SIGCONT);
1382 BUG_ON(retval == 0);
1383 }
1384
1385 return retval;
1386 }
1387
1388 /*
1389 * Consider @p for a wait by @parent.
1390 *
1391 * -ECHILD should be in ->notask_error before the first call.
1392 * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1393 * Returns zero if the search for a child should continue;
1394 * then ->notask_error is 0 if @p is an eligible child,
1395 * or still -ECHILD.
1396 */
1397 static int wait_consider_task(struct wait_opts *wo, int ptrace,
1398 struct task_struct *p)
1399 {
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);
1406 int ret;
1407
1408 if (unlikely(exit_state == EXIT_DEAD))
1409 return 0;
1410
1411 ret = eligible_child(wo, ptrace, p);
1412 if (!ret)
1413 return ret;
1414
1415 if (unlikely(exit_state == EXIT_TRACE)) {
1416 /*
1417 * ptrace == 0 means we are the natural parent. In this case
1418 * we should clear notask_error, debugger will notify us.
1419 */
1420 if (likely(!ptrace))
1421 wo->notask_error = 0;
1422 return 0;
1423 }
1424
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
1441 /* slay zombie? */
1442 if (exit_state == EXIT_ZOMBIE) {
1443 /* we don't reap group leaders with subthreads */
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 }
1453
1454 /*
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.
1480 */
1481 wo->notask_error = 0;
1482 }
1483
1484 /*
1485 * Wait for stopped. Depending on @ptrace, different stopped state
1486 * is used and the two don't interact with each other.
1487 */
1488 ret = wait_task_stopped(wo, ptrace, p);
1489 if (ret)
1490 return ret;
1491
1492 /*
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.
1496 */
1497 return wait_task_continued(wo, p);
1498 }
1499
1500 /*
1501 * Do the work of do_wait() for one thread in the group, @tsk.
1502 *
1503 * -ECHILD should be in ->notask_error before the first call.
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
1506 * ->notask_error is 0 if there were any eligible children,
1507 * or still -ECHILD.
1508 */
1509 static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
1510 {
1511 struct task_struct *p;
1512
1513 list_for_each_entry(p, &tsk->children, sibling) {
1514 int ret = wait_consider_task(wo, 0, p);
1515
1516 if (ret)
1517 return ret;
1518 }
1519
1520 return 0;
1521 }
1522
1523 static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
1524 {
1525 struct task_struct *p;
1526
1527 list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1528 int ret = wait_consider_task(wo, 1, p);
1529
1530 if (ret)
1531 return ret;
1532 }
1533
1534 return 0;
1535 }
1536
1537 static 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
1544 if (!eligible_pid(wo, p))
1545 return 0;
1546
1547 if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
1548 return 0;
1549
1550 return default_wake_function(wait, mode, sync, key);
1551 }
1552
1553 void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1554 {
1555 __wake_up_sync_key(&parent->signal->wait_chldexit,
1556 TASK_INTERRUPTIBLE, 1, p);
1557 }
1558
1559 static long do_wait(struct wait_opts *wo)
1560 {
1561 struct task_struct *tsk;
1562 int retval;
1563
1564 trace_sched_process_wait(wo->wo_pid);
1565
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);
1569 repeat:
1570 /*
1571 * If there is nothing that can match our criteria, just get out.
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.
1575 */
1576 wo->notask_error = -ECHILD;
1577 if ((wo->wo_type < PIDTYPE_MAX) &&
1578 (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
1579 goto notask;
1580
1581 set_current_state(TASK_INTERRUPTIBLE);
1582 read_lock(&tasklist_lock);
1583 tsk = current;
1584 do {
1585 retval = do_wait_thread(wo, tsk);
1586 if (retval)
1587 goto end;
1588
1589 retval = ptrace_do_wait(wo, tsk);
1590 if (retval)
1591 goto end;
1592
1593 if (wo->wo_flags & __WNOTHREAD)
1594 break;
1595 } while_each_thread(current, tsk);
1596 read_unlock(&tasklist_lock);
1597
1598 notask:
1599 retval = wo->notask_error;
1600 if (!retval && !(wo->wo_flags & WNOHANG)) {
1601 retval = -ERESTARTSYS;
1602 if (!signal_pending(current)) {
1603 schedule();
1604 goto repeat;
1605 }
1606 }
1607 end:
1608 __set_current_state(TASK_RUNNING);
1609 remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1610 return retval;
1611 }
1612
1613 SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1614 infop, int, options, struct rusage __user *, ru)
1615 {
1616 struct wait_opts wo;
1617 struct pid *pid = NULL;
1618 enum pid_type type;
1619 long ret;
1620
1621 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
1622 __WNOTHREAD|__WCLONE|__WALL))
1623 return -EINVAL;
1624 if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1625 return -EINVAL;
1626
1627 switch (which) {
1628 case P_ALL:
1629 type = PIDTYPE_MAX;
1630 break;
1631 case P_PID:
1632 type = PIDTYPE_PID;
1633 if (upid <= 0)
1634 return -EINVAL;
1635 break;
1636 case P_PGID:
1637 type = PIDTYPE_PGID;
1638 if (upid <= 0)
1639 return -EINVAL;
1640 break;
1641 default:
1642 return -EINVAL;
1643 }
1644
1645 if (type < PIDTYPE_MAX)
1646 pid = find_get_pid(upid);
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);
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
1678 put_pid(pid);
1679 return ret;
1680 }
1681
1682 SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1683 int, options, struct rusage __user *, ru)
1684 {
1685 struct wait_opts wo;
1686 struct pid *pid = NULL;
1687 enum pid_type type;
1688 long ret;
1689
1690 if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1691 __WNOTHREAD|__WCLONE|__WALL))
1692 return -EINVAL;
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;
1701 pid = get_task_pid(current, PIDTYPE_PGID);
1702 } else /* upid > 0 */ {
1703 type = PIDTYPE_PID;
1704 pid = find_get_pid(upid);
1705 }
1706
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);
1714 put_pid(pid);
1715
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 */
1725 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1726 {
1727 return sys_wait4(pid, stat_addr, options, NULL);
1728 }
1729
1730 #endif