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