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