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