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