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