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