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