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