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