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