| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  linux/kernel/exit.c | 
 | 3 |  * | 
 | 4 |  *  Copyright (C) 1991, 1992  Linus Torvalds | 
 | 5 |  */ | 
 | 6 |  | 
 | 7 | #include <linux/mm.h> | 
 | 8 | #include <linux/slab.h> | 
 | 9 | #include <linux/interrupt.h> | 
 | 10 | #include <linux/module.h> | 
 | 11 | #include <linux/capability.h> | 
 | 12 | #include <linux/completion.h> | 
 | 13 | #include <linux/personality.h> | 
 | 14 | #include <linux/tty.h> | 
 | 15 | #include <linux/iocontext.h> | 
 | 16 | #include <linux/key.h> | 
 | 17 | #include <linux/security.h> | 
 | 18 | #include <linux/cpu.h> | 
 | 19 | #include <linux/acct.h> | 
 | 20 | #include <linux/tsacct_kern.h> | 
 | 21 | #include <linux/file.h> | 
 | 22 | #include <linux/fdtable.h> | 
 | 23 | #include <linux/binfmts.h> | 
 | 24 | #include <linux/nsproxy.h> | 
 | 25 | #include <linux/pid_namespace.h> | 
 | 26 | #include <linux/ptrace.h> | 
 | 27 | #include <linux/profile.h> | 
 | 28 | #include <linux/mount.h> | 
 | 29 | #include <linux/proc_fs.h> | 
 | 30 | #include <linux/kthread.h> | 
 | 31 | #include <linux/mempolicy.h> | 
 | 32 | #include <linux/taskstats_kern.h> | 
 | 33 | #include <linux/delayacct.h> | 
 | 34 | #include <linux/freezer.h> | 
 | 35 | #include <linux/cgroup.h> | 
 | 36 | #include <linux/syscalls.h> | 
 | 37 | #include <linux/signal.h> | 
 | 38 | #include <linux/posix-timers.h> | 
 | 39 | #include <linux/cn_proc.h> | 
 | 40 | #include <linux/mutex.h> | 
 | 41 | #include <linux/futex.h> | 
 | 42 | #include <linux/pipe_fs_i.h> | 
 | 43 | #include <linux/audit.h> /* for audit_free() */ | 
 | 44 | #include <linux/resource.h> | 
 | 45 | #include <linux/blkdev.h> | 
 | 46 | #include <linux/task_io_accounting_ops.h> | 
 | 47 | #include <linux/tracehook.h> | 
 | 48 | #include <linux/fs_struct.h> | 
 | 49 | #include <linux/init_task.h> | 
 | 50 | #include <linux/perf_event.h> | 
 | 51 | #include <trace/events/sched.h> | 
 | 52 | #include <linux/hw_breakpoint.h> | 
 | 53 | #include <linux/oom.h> | 
 | 54 | #include <linux/writeback.h> | 
 | 55 | #include <linux/shm.h> | 
 | 56 |  | 
 | 57 | #include <asm/uaccess.h> | 
 | 58 | #include <asm/unistd.h> | 
 | 59 | #include <asm/pgtable.h> | 
 | 60 | #include <asm/mmu_context.h> | 
 | 61 |  | 
| xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 62 | #ifdef CONFIG_FLAGS_UTILS | 
 | 63 | #include <linux/reboot.h> | 
 | 64 | #include "pub_flags.h" | 
 | 65 | #endif | 
 | 66 |  | 
| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 67 | static void exit_mm(struct task_struct * tsk); | 
 | 68 |  | 
 | 69 | static void __unhash_process(struct task_struct *p, bool group_dead) | 
 | 70 | { | 
 | 71 | 	nr_threads--; | 
 | 72 | 	detach_pid(p, PIDTYPE_PID); | 
 | 73 | 	if (group_dead) { | 
 | 74 | 		detach_pid(p, PIDTYPE_PGID); | 
 | 75 | 		detach_pid(p, PIDTYPE_SID); | 
 | 76 |  | 
 | 77 | 		list_del_rcu(&p->tasks); | 
 | 78 | 		list_del_init(&p->sibling); | 
 | 79 | 		__this_cpu_dec(process_counts); | 
 | 80 | 	} | 
 | 81 | 	list_del_rcu(&p->thread_group); | 
 | 82 | 	list_del_rcu(&p->thread_node); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | /* | 
 | 86 |  * This function expects the tasklist_lock write-locked. | 
 | 87 |  */ | 
 | 88 | static void __exit_signal(struct task_struct *tsk) | 
 | 89 | { | 
 | 90 | 	struct signal_struct *sig = tsk->signal; | 
 | 91 | 	bool group_dead = thread_group_leader(tsk); | 
 | 92 | 	struct sighand_struct *sighand; | 
 | 93 | 	struct tty_struct *uninitialized_var(tty); | 
 | 94 |  | 
 | 95 | 	sighand = rcu_dereference_check(tsk->sighand, | 
 | 96 | 					lockdep_tasklist_lock_is_held()); | 
 | 97 | 	spin_lock(&sighand->siglock); | 
 | 98 |  | 
 | 99 | 	posix_cpu_timers_exit(tsk); | 
 | 100 | 	if (group_dead) { | 
 | 101 | 		posix_cpu_timers_exit_group(tsk); | 
 | 102 | 		tty = sig->tty; | 
 | 103 | 		sig->tty = NULL; | 
 | 104 | 	} else { | 
 | 105 | 		/* | 
 | 106 | 		 * This can only happen if the caller is de_thread(). | 
 | 107 | 		 * FIXME: this is the temporary hack, we should teach | 
 | 108 | 		 * posix-cpu-timers to handle this case correctly. | 
 | 109 | 		 */ | 
 | 110 | 		if (unlikely(has_group_leader_pid(tsk))) | 
 | 111 | 			posix_cpu_timers_exit_group(tsk); | 
 | 112 |  | 
 | 113 | 		/* | 
 | 114 | 		 * If there is any task waiting for the group exit | 
 | 115 | 		 * then notify it: | 
 | 116 | 		 */ | 
 | 117 | 		if (sig->notify_count > 0 && !--sig->notify_count) | 
 | 118 | 			wake_up_process(sig->group_exit_task); | 
 | 119 |  | 
 | 120 | 		if (tsk == sig->curr_target) | 
 | 121 | 			sig->curr_target = next_thread(tsk); | 
 | 122 | 		/* | 
 | 123 | 		 * Accumulate here the counters for all threads but the | 
 | 124 | 		 * group leader as they die, so they can be added into | 
 | 125 | 		 * the process-wide totals when those are taken. | 
 | 126 | 		 * The group leader stays around as a zombie as long | 
 | 127 | 		 * as there are other threads.  When it gets reaped, | 
 | 128 | 		 * the exit.c code will add its counts into these totals. | 
 | 129 | 		 * We won't ever get here for the group leader, since it | 
 | 130 | 		 * will have been the last reference on the signal_struct. | 
 | 131 | 		 */ | 
 | 132 | 		sig->utime += tsk->utime; | 
 | 133 | 		sig->stime += tsk->stime; | 
 | 134 | 		sig->gtime += tsk->gtime; | 
 | 135 | 		sig->min_flt += tsk->min_flt; | 
 | 136 | 		sig->maj_flt += tsk->maj_flt; | 
 | 137 | 		sig->nvcsw += tsk->nvcsw; | 
 | 138 | 		sig->nivcsw += tsk->nivcsw; | 
 | 139 | 		sig->inblock += task_io_get_inblock(tsk); | 
 | 140 | 		sig->oublock += task_io_get_oublock(tsk); | 
 | 141 | 		task_io_accounting_add(&sig->ioac, &tsk->ioac); | 
 | 142 | 		sig->sum_sched_runtime += tsk->se.sum_exec_runtime; | 
 | 143 | 	} | 
 | 144 |  | 
 | 145 | 	sig->nr_threads--; | 
 | 146 | 	__unhash_process(tsk, group_dead); | 
 | 147 |  | 
 | 148 | 	/* | 
 | 149 | 	 * Do this under ->siglock, we can race with another thread | 
 | 150 | 	 * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals. | 
 | 151 | 	 */ | 
 | 152 | 	flush_task_sigqueue(tsk); | 
 | 153 | 	tsk->sighand = NULL; | 
 | 154 | 	spin_unlock(&sighand->siglock); | 
 | 155 |  | 
 | 156 | 	__cleanup_sighand(sighand); | 
 | 157 | 	clear_tsk_thread_flag(tsk,TIF_SIGPENDING); | 
 | 158 | 	if (group_dead) { | 
 | 159 | 		flush_sigqueue(&sig->shared_pending); | 
 | 160 | 		tty_kref_put(tty); | 
 | 161 | 	} | 
 | 162 | } | 
 | 163 |  | 
 | 164 | static void delayed_put_task_struct(struct rcu_head *rhp) | 
 | 165 | { | 
 | 166 | 	struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); | 
 | 167 |  | 
 | 168 | 	perf_event_delayed_put(tsk); | 
 | 169 | 	trace_sched_process_free(tsk); | 
 | 170 | 	put_task_struct(tsk); | 
 | 171 | } | 
 | 172 |  | 
 | 173 |  | 
 | 174 | void release_task(struct task_struct * p) | 
 | 175 | { | 
 | 176 | 	struct task_struct *leader; | 
 | 177 | 	int zap_leader; | 
 | 178 | repeat: | 
 | 179 | 	/* don't need to get the RCU readlock here - the process is dead and | 
 | 180 | 	 * can't be modifying its own credentials. But shut RCU-lockdep up */ | 
 | 181 | 	rcu_read_lock(); | 
 | 182 | 	atomic_dec(&__task_cred(p)->user->processes); | 
 | 183 | 	rcu_read_unlock(); | 
 | 184 |  | 
 | 185 | 	proc_flush_task(p); | 
 | 186 |  | 
 | 187 | 	write_lock_irq(&tasklist_lock); | 
 | 188 | 	ptrace_release_task(p); | 
 | 189 | 	__exit_signal(p); | 
 | 190 |  | 
 | 191 | 	/* | 
 | 192 | 	 * If we are the last non-leader member of the thread | 
 | 193 | 	 * group, and the leader is zombie, then notify the | 
 | 194 | 	 * group leader's parent process. (if it wants notification.) | 
 | 195 | 	 */ | 
 | 196 | 	zap_leader = 0; | 
 | 197 | 	leader = p->group_leader; | 
 | 198 | 	if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) { | 
 | 199 | 		/* | 
 | 200 | 		 * If we were the last child thread and the leader has | 
 | 201 | 		 * exited already, and the leader's parent ignores SIGCHLD, | 
 | 202 | 		 * then we are the one who should release the leader. | 
 | 203 | 		 */ | 
 | 204 | 		zap_leader = do_notify_parent(leader, leader->exit_signal); | 
 | 205 | 		if (zap_leader) | 
 | 206 | 			leader->exit_state = EXIT_DEAD; | 
 | 207 | 	} | 
 | 208 |  | 
 | 209 | 	write_unlock_irq(&tasklist_lock); | 
 | 210 | 	release_thread(p); | 
 | 211 | 	call_rcu(&p->rcu, delayed_put_task_struct); | 
 | 212 |  | 
 | 213 | 	p = leader; | 
 | 214 | 	if (unlikely(zap_leader)) | 
 | 215 | 		goto repeat; | 
 | 216 | } | 
 | 217 |  | 
 | 218 | /* | 
 | 219 |  * This checks not only the pgrp, but falls back on the pid if no | 
 | 220 |  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly | 
 | 221 |  * without this... | 
 | 222 |  * | 
 | 223 |  * The caller must hold rcu lock or the tasklist lock. | 
 | 224 |  */ | 
 | 225 | struct pid *session_of_pgrp(struct pid *pgrp) | 
 | 226 | { | 
 | 227 | 	struct task_struct *p; | 
 | 228 | 	struct pid *sid = NULL; | 
 | 229 |  | 
 | 230 | 	p = pid_task(pgrp, PIDTYPE_PGID); | 
 | 231 | 	if (p == NULL) | 
 | 232 | 		p = pid_task(pgrp, PIDTYPE_PID); | 
 | 233 | 	if (p != NULL) | 
 | 234 | 		sid = task_session(p); | 
 | 235 |  | 
 | 236 | 	return sid; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | /* | 
 | 240 |  * Determine if a process group is "orphaned", according to the POSIX | 
 | 241 |  * definition in 2.2.2.52.  Orphaned process groups are not to be affected | 
 | 242 |  * by terminal-generated stop signals.  Newly orphaned process groups are | 
 | 243 |  * to receive a SIGHUP and a SIGCONT. | 
 | 244 |  * | 
 | 245 |  * "I ask you, have you ever known what it is to be an orphan?" | 
 | 246 |  */ | 
 | 247 | static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task) | 
 | 248 | { | 
 | 249 | 	struct task_struct *p; | 
 | 250 |  | 
 | 251 | 	do_each_pid_task(pgrp, PIDTYPE_PGID, p) { | 
 | 252 | 		if ((p == ignored_task) || | 
 | 253 | 		    (p->exit_state && thread_group_empty(p)) || | 
 | 254 | 		    is_global_init(p->real_parent)) | 
 | 255 | 			continue; | 
 | 256 |  | 
 | 257 | 		if (task_pgrp(p->real_parent) != pgrp && | 
 | 258 | 		    task_session(p->real_parent) == task_session(p)) | 
 | 259 | 			return 0; | 
 | 260 | 	} while_each_pid_task(pgrp, PIDTYPE_PGID, p); | 
 | 261 |  | 
 | 262 | 	return 1; | 
 | 263 | } | 
 | 264 |  | 
 | 265 | int is_current_pgrp_orphaned(void) | 
 | 266 | { | 
 | 267 | 	int retval; | 
 | 268 |  | 
 | 269 | 	read_lock(&tasklist_lock); | 
 | 270 | 	retval = will_become_orphaned_pgrp(task_pgrp(current), NULL); | 
 | 271 | 	read_unlock(&tasklist_lock); | 
 | 272 |  | 
 | 273 | 	return retval; | 
 | 274 | } | 
 | 275 |  | 
 | 276 | static bool has_stopped_jobs(struct pid *pgrp) | 
 | 277 | { | 
 | 278 | 	struct task_struct *p; | 
 | 279 |  | 
 | 280 | 	do_each_pid_task(pgrp, PIDTYPE_PGID, p) { | 
 | 281 | 		if (p->signal->flags & SIGNAL_STOP_STOPPED) | 
 | 282 | 			return true; | 
 | 283 | 	} while_each_pid_task(pgrp, PIDTYPE_PGID, p); | 
 | 284 |  | 
 | 285 | 	return false; | 
 | 286 | } | 
 | 287 |  | 
 | 288 | /* | 
 | 289 |  * Check to see if any process groups have become orphaned as | 
 | 290 |  * a result of our exiting, and if they have any stopped jobs, | 
 | 291 |  * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2) | 
 | 292 |  */ | 
 | 293 | static void | 
 | 294 | kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent) | 
 | 295 | { | 
 | 296 | 	struct pid *pgrp = task_pgrp(tsk); | 
 | 297 | 	struct task_struct *ignored_task = tsk; | 
 | 298 |  | 
 | 299 | 	if (!parent) | 
 | 300 | 		 /* exit: our father is in a different pgrp than | 
 | 301 | 		  * we are and we were the only connection outside. | 
 | 302 | 		  */ | 
 | 303 | 		parent = tsk->real_parent; | 
 | 304 | 	else | 
 | 305 | 		/* reparent: our child is in a different pgrp than | 
 | 306 | 		 * we are, and it was the only connection outside. | 
 | 307 | 		 */ | 
 | 308 | 		ignored_task = NULL; | 
 | 309 |  | 
 | 310 | 	if (task_pgrp(parent) != pgrp && | 
 | 311 | 	    task_session(parent) == task_session(tsk) && | 
 | 312 | 	    will_become_orphaned_pgrp(pgrp, ignored_task) && | 
 | 313 | 	    has_stopped_jobs(pgrp)) { | 
 | 314 | 		__kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp); | 
 | 315 | 		__kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp); | 
 | 316 | 	} | 
 | 317 | } | 
 | 318 |  | 
 | 319 | /** | 
 | 320 |  * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd | 
 | 321 |  * | 
 | 322 |  * If a kernel thread is launched as a result of a system call, or if | 
 | 323 |  * it ever exits, it should generally reparent itself to kthreadd so it | 
 | 324 |  * isn't in the way of other processes and is correctly cleaned up on exit. | 
 | 325 |  * | 
 | 326 |  * The various task state such as scheduling policy and priority may have | 
 | 327 |  * been inherited from a user process, so we reset them to sane values here. | 
 | 328 |  * | 
 | 329 |  * NOTE that reparent_to_kthreadd() gives the caller full capabilities. | 
 | 330 |  */ | 
 | 331 | static void reparent_to_kthreadd(void) | 
 | 332 | { | 
 | 333 | 	write_lock_irq(&tasklist_lock); | 
 | 334 |  | 
 | 335 | 	ptrace_unlink(current); | 
 | 336 | 	/* Reparent to init */ | 
 | 337 | 	current->real_parent = current->parent = kthreadd_task; | 
 | 338 | 	list_move_tail(¤t->sibling, ¤t->real_parent->children); | 
 | 339 |  | 
 | 340 | 	/* Set the exit signal to SIGCHLD so we signal init on exit */ | 
 | 341 | 	current->exit_signal = SIGCHLD; | 
 | 342 |  | 
 | 343 | 	if (task_nice(current) < 0) | 
 | 344 | 		set_user_nice(current, 0); | 
 | 345 | 	/* cpus_allowed? */ | 
 | 346 | 	/* rt_priority? */ | 
 | 347 | 	/* signals? */ | 
 | 348 | 	memcpy(current->signal->rlim, init_task.signal->rlim, | 
 | 349 | 	       sizeof(current->signal->rlim)); | 
 | 350 |  | 
 | 351 | 	atomic_inc(&init_cred.usage); | 
 | 352 | 	commit_creds(&init_cred); | 
 | 353 | 	write_unlock_irq(&tasklist_lock); | 
 | 354 | } | 
 | 355 |  | 
 | 356 | void __set_special_pids(struct pid *pid) | 
 | 357 | { | 
 | 358 | 	struct task_struct *curr = current->group_leader; | 
 | 359 |  | 
 | 360 | 	if (task_session(curr) != pid) | 
 | 361 | 		change_pid(curr, PIDTYPE_SID, pid); | 
 | 362 |  | 
 | 363 | 	if (task_pgrp(curr) != pid) | 
 | 364 | 		change_pid(curr, PIDTYPE_PGID, pid); | 
 | 365 | } | 
 | 366 |  | 
 | 367 | static void set_special_pids(struct pid *pid) | 
 | 368 | { | 
 | 369 | 	write_lock_irq(&tasklist_lock); | 
 | 370 | 	__set_special_pids(pid); | 
 | 371 | 	write_unlock_irq(&tasklist_lock); | 
 | 372 | } | 
 | 373 |  | 
 | 374 | /* | 
 | 375 |  * Let kernel threads use this to say that they allow a certain signal. | 
 | 376 |  * Must not be used if kthread was cloned with CLONE_SIGHAND. | 
 | 377 |  */ | 
 | 378 | int allow_signal(int sig) | 
 | 379 | { | 
 | 380 | 	if (!valid_signal(sig) || sig < 1) | 
 | 381 | 		return -EINVAL; | 
 | 382 |  | 
 | 383 | 	spin_lock_irq(¤t->sighand->siglock); | 
 | 384 | 	/* This is only needed for daemonize()'ed kthreads */ | 
 | 385 | 	sigdelset(¤t->blocked, sig); | 
 | 386 | 	/* | 
 | 387 | 	 * Kernel threads handle their own signals. Let the signal code | 
 | 388 | 	 * know it'll be handled, so that they don't get converted to | 
 | 389 | 	 * SIGKILL or just silently dropped. | 
 | 390 | 	 */ | 
 | 391 | 	current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2; | 
 | 392 | 	recalc_sigpending(); | 
 | 393 | 	spin_unlock_irq(¤t->sighand->siglock); | 
 | 394 | 	return 0; | 
 | 395 | } | 
 | 396 |  | 
 | 397 | EXPORT_SYMBOL(allow_signal); | 
 | 398 |  | 
 | 399 | int disallow_signal(int sig) | 
 | 400 | { | 
 | 401 | 	if (!valid_signal(sig) || sig < 1) | 
 | 402 | 		return -EINVAL; | 
 | 403 |  | 
 | 404 | 	spin_lock_irq(¤t->sighand->siglock); | 
 | 405 | 	current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN; | 
 | 406 | 	recalc_sigpending(); | 
 | 407 | 	spin_unlock_irq(¤t->sighand->siglock); | 
 | 408 | 	return 0; | 
 | 409 | } | 
 | 410 |  | 
 | 411 | EXPORT_SYMBOL(disallow_signal); | 
 | 412 |  | 
 | 413 | /* | 
 | 414 |  *	Put all the gunge required to become a kernel thread without | 
 | 415 |  *	attached user resources in one place where it belongs. | 
 | 416 |  */ | 
 | 417 |  | 
 | 418 | void daemonize(const char *name, ...) | 
 | 419 | { | 
 | 420 | 	va_list args; | 
 | 421 | 	sigset_t blocked; | 
 | 422 |  | 
 | 423 | 	va_start(args, name); | 
 | 424 | 	vsnprintf(current->comm, sizeof(current->comm), name, args); | 
 | 425 | 	va_end(args); | 
 | 426 |  | 
 | 427 | 	/* | 
 | 428 | 	 * If we were started as result of loading a module, close all of the | 
 | 429 | 	 * user space pages.  We don't need them, and if we didn't close them | 
 | 430 | 	 * they would be locked into memory. | 
 | 431 | 	 */ | 
 | 432 | 	exit_mm(current); | 
 | 433 | 	/* | 
 | 434 | 	 * We don't want to get frozen, in case system-wide hibernation | 
 | 435 | 	 * or suspend transition begins right now. | 
 | 436 | 	 */ | 
 | 437 | 	current->flags |= (PF_NOFREEZE | PF_KTHREAD); | 
 | 438 |  | 
 | 439 | 	if (current->nsproxy != &init_nsproxy) { | 
 | 440 | 		get_nsproxy(&init_nsproxy); | 
 | 441 | 		switch_task_namespaces(current, &init_nsproxy); | 
 | 442 | 	} | 
 | 443 | 	set_special_pids(&init_struct_pid); | 
 | 444 | 	proc_clear_tty(current); | 
 | 445 |  | 
 | 446 | 	/* Block and flush all signals */ | 
 | 447 | 	sigfillset(&blocked); | 
 | 448 | 	sigprocmask(SIG_BLOCK, &blocked, NULL); | 
 | 449 | 	flush_signals(current); | 
 | 450 |  | 
 | 451 | 	/* Become as one with the init task */ | 
 | 452 |  | 
 | 453 | 	daemonize_fs_struct(); | 
 | 454 | 	exit_files(current); | 
 | 455 | 	current->files = init_task.files; | 
 | 456 | 	atomic_inc(¤t->files->count); | 
 | 457 |  | 
 | 458 | 	reparent_to_kthreadd(); | 
 | 459 | } | 
 | 460 |  | 
 | 461 | EXPORT_SYMBOL(daemonize); | 
 | 462 |  | 
 | 463 | static void close_files(struct files_struct * files) | 
 | 464 | { | 
 | 465 | 	int i, j; | 
 | 466 | 	struct fdtable *fdt; | 
 | 467 |  | 
 | 468 | 	j = 0; | 
 | 469 |  | 
 | 470 | 	/* | 
 | 471 | 	 * It is safe to dereference the fd table without RCU or | 
 | 472 | 	 * ->file_lock because this is the last reference to the | 
 | 473 | 	 * files structure.  But use RCU to shut RCU-lockdep up. | 
 | 474 | 	 */ | 
 | 475 | 	rcu_read_lock(); | 
 | 476 | 	fdt = files_fdtable(files); | 
 | 477 | 	rcu_read_unlock(); | 
 | 478 | 	for (;;) { | 
 | 479 | 		unsigned long set; | 
 | 480 | 		i = j * BITS_PER_LONG; | 
 | 481 | 		if (i >= fdt->max_fds) | 
 | 482 | 			break; | 
 | 483 | 		set = fdt->open_fds[j++]; | 
 | 484 | 		while (set) { | 
 | 485 | 			if (set & 1) { | 
 | 486 | 				struct file * file = xchg(&fdt->fd[i], NULL); | 
 | 487 | 				if (file) { | 
 | 488 | 					filp_close(file, files); | 
 | 489 | 					cond_resched(); | 
 | 490 | 				} | 
 | 491 | 			} | 
 | 492 | 			i++; | 
 | 493 | 			set >>= 1; | 
 | 494 | 		} | 
 | 495 | 	} | 
 | 496 | } | 
 | 497 |  | 
 | 498 | struct files_struct *get_files_struct(struct task_struct *task) | 
 | 499 | { | 
 | 500 | 	struct files_struct *files; | 
 | 501 |  | 
 | 502 | 	task_lock(task); | 
 | 503 | 	files = task->files; | 
 | 504 | 	if (files) | 
 | 505 | 		atomic_inc(&files->count); | 
 | 506 | 	task_unlock(task); | 
 | 507 |  | 
 | 508 | 	return files; | 
 | 509 | } | 
 | 510 |  | 
 | 511 | void put_files_struct(struct files_struct *files) | 
 | 512 | { | 
 | 513 | 	struct fdtable *fdt; | 
 | 514 |  | 
 | 515 | 	if (atomic_dec_and_test(&files->count)) { | 
 | 516 | 		close_files(files); | 
 | 517 | 		/* | 
 | 518 | 		 * Free the fd and fdset arrays if we expanded them. | 
 | 519 | 		 * If the fdtable was embedded, pass files for freeing | 
 | 520 | 		 * at the end of the RCU grace period. Otherwise, | 
 | 521 | 		 * you can free files immediately. | 
 | 522 | 		 */ | 
 | 523 | 		rcu_read_lock(); | 
 | 524 | 		fdt = files_fdtable(files); | 
 | 525 | 		if (fdt != &files->fdtab) | 
 | 526 | 			kmem_cache_free(files_cachep, files); | 
 | 527 | 		free_fdtable(fdt); | 
 | 528 | 		rcu_read_unlock(); | 
 | 529 | 	} | 
 | 530 | } | 
 | 531 |  | 
 | 532 | void reset_files_struct(struct files_struct *files) | 
 | 533 | { | 
 | 534 | 	struct task_struct *tsk = current; | 
 | 535 | 	struct files_struct *old; | 
 | 536 |  | 
 | 537 | 	old = tsk->files; | 
 | 538 | 	task_lock(tsk); | 
 | 539 | 	tsk->files = files; | 
 | 540 | 	task_unlock(tsk); | 
 | 541 | 	put_files_struct(old); | 
 | 542 | } | 
 | 543 |  | 
 | 544 | void exit_files(struct task_struct *tsk) | 
 | 545 | { | 
 | 546 | 	struct files_struct * files = tsk->files; | 
 | 547 |  | 
 | 548 | 	if (files) { | 
 | 549 | 		task_lock(tsk); | 
 | 550 | 		tsk->files = NULL; | 
 | 551 | 		task_unlock(tsk); | 
 | 552 | 		put_files_struct(files); | 
 | 553 | 	} | 
 | 554 | } | 
 | 555 |  | 
 | 556 | #ifdef CONFIG_MM_OWNER | 
 | 557 | /* | 
 | 558 |  * A task is exiting.   If it owned this mm, find a new owner for the mm. | 
 | 559 |  */ | 
 | 560 | void mm_update_next_owner(struct mm_struct *mm) | 
 | 561 | { | 
 | 562 | 	struct task_struct *c, *g, *p = current; | 
 | 563 |  | 
 | 564 | retry: | 
 | 565 | 	/* | 
 | 566 | 	 * If the exiting or execing task is not the owner, it's | 
 | 567 | 	 * someone else's problem. | 
 | 568 | 	 */ | 
 | 569 | 	if (mm->owner != p) | 
 | 570 | 		return; | 
 | 571 | 	/* | 
 | 572 | 	 * The current owner is exiting/execing and there are no other | 
 | 573 | 	 * candidates.  Do not leave the mm pointing to a possibly | 
 | 574 | 	 * freed task structure. | 
 | 575 | 	 */ | 
 | 576 | 	if (atomic_read(&mm->mm_users) <= 1) { | 
 | 577 | 		mm->owner = NULL; | 
 | 578 | 		return; | 
 | 579 | 	} | 
 | 580 |  | 
 | 581 | 	read_lock(&tasklist_lock); | 
 | 582 | 	/* | 
 | 583 | 	 * Search in the children | 
 | 584 | 	 */ | 
 | 585 | 	list_for_each_entry(c, &p->children, sibling) { | 
 | 586 | 		if (c->mm == mm) | 
 | 587 | 			goto assign_new_owner; | 
 | 588 | 	} | 
 | 589 |  | 
 | 590 | 	/* | 
 | 591 | 	 * Search in the siblings | 
 | 592 | 	 */ | 
 | 593 | 	list_for_each_entry(c, &p->real_parent->children, sibling) { | 
 | 594 | 		if (c->mm == mm) | 
 | 595 | 			goto assign_new_owner; | 
 | 596 | 	} | 
 | 597 |  | 
 | 598 | 	/* | 
 | 599 | 	 * Search through everything else. We should not get | 
 | 600 | 	 * here often | 
 | 601 | 	 */ | 
 | 602 | 	do_each_thread(g, c) { | 
 | 603 | 		if (c->mm == mm) | 
 | 604 | 			goto assign_new_owner; | 
 | 605 | 	} while_each_thread(g, c); | 
 | 606 |  | 
 | 607 | 	read_unlock(&tasklist_lock); | 
 | 608 | 	/* | 
 | 609 | 	 * We found no owner yet mm_users > 1: this implies that we are | 
 | 610 | 	 * most likely racing with swapoff (try_to_unuse()) or /proc or | 
 | 611 | 	 * ptrace or page migration (get_task_mm()).  Mark owner as NULL. | 
 | 612 | 	 */ | 
 | 613 | 	mm->owner = NULL; | 
 | 614 | 	return; | 
 | 615 |  | 
 | 616 | assign_new_owner: | 
 | 617 | 	BUG_ON(c == p); | 
 | 618 | 	get_task_struct(c); | 
 | 619 | 	/* | 
 | 620 | 	 * The task_lock protects c->mm from changing. | 
 | 621 | 	 * We always want mm->owner->mm == mm | 
 | 622 | 	 */ | 
 | 623 | 	task_lock(c); | 
 | 624 | 	/* | 
 | 625 | 	 * Delay read_unlock() till we have the task_lock() | 
 | 626 | 	 * to ensure that c does not slip away underneath us | 
 | 627 | 	 */ | 
 | 628 | 	read_unlock(&tasklist_lock); | 
 | 629 | 	if (c->mm != mm) { | 
 | 630 | 		task_unlock(c); | 
 | 631 | 		put_task_struct(c); | 
 | 632 | 		goto retry; | 
 | 633 | 	} | 
 | 634 | 	mm->owner = c; | 
 | 635 | 	task_unlock(c); | 
 | 636 | 	put_task_struct(c); | 
 | 637 | } | 
 | 638 | #endif /* CONFIG_MM_OWNER */ | 
 | 639 |  | 
 | 640 | /* | 
 | 641 |  * Turn us into a lazy TLB process if we | 
 | 642 |  * aren't already.. | 
 | 643 |  */ | 
 | 644 | static void exit_mm(struct task_struct * tsk) | 
 | 645 | { | 
 | 646 | 	struct mm_struct *mm = tsk->mm; | 
 | 647 | 	struct core_state *core_state; | 
 | 648 |  | 
 | 649 | 	mm_release(tsk, mm); | 
 | 650 | 	if (!mm) | 
 | 651 | 		return; | 
 | 652 | 	sync_mm_rss(mm); | 
 | 653 | 	/* | 
 | 654 | 	 * Serialize with any possible pending coredump. | 
 | 655 | 	 * We must hold mmap_sem around checking core_state | 
 | 656 | 	 * and clearing tsk->mm.  The core-inducing thread | 
 | 657 | 	 * will increment ->nr_threads for each thread in the | 
 | 658 | 	 * group with ->mm != NULL. | 
 | 659 | 	 */ | 
 | 660 | 	down_read(&mm->mmap_sem); | 
 | 661 | 	core_state = mm->core_state; | 
 | 662 | 	if (core_state) { | 
 | 663 | 		struct core_thread self; | 
 | 664 | 		up_read(&mm->mmap_sem); | 
 | 665 |  | 
 | 666 | 		self.task = tsk; | 
 | 667 | 		self.next = xchg(&core_state->dumper.next, &self); | 
 | 668 | 		/* | 
 | 669 | 		 * Implies mb(), the result of xchg() must be visible | 
 | 670 | 		 * to core_state->dumper. | 
 | 671 | 		 */ | 
 | 672 | 		if (atomic_dec_and_test(&core_state->nr_threads)) | 
 | 673 | 			complete(&core_state->startup); | 
 | 674 |  | 
 | 675 | 		for (;;) { | 
 | 676 | 			set_task_state(tsk, TASK_UNINTERRUPTIBLE); | 
 | 677 | 			if (!self.task) /* see coredump_finish() */ | 
 | 678 | 				break; | 
 | 679 | 			schedule(); | 
 | 680 | 		} | 
 | 681 | 		__set_task_state(tsk, TASK_RUNNING); | 
 | 682 | 		down_read(&mm->mmap_sem); | 
 | 683 | 	} | 
 | 684 | 	atomic_inc(&mm->mm_count); | 
 | 685 | 	BUG_ON(mm != tsk->active_mm); | 
 | 686 | 	/* more a memory barrier than a real lock */ | 
 | 687 | 	task_lock(tsk); | 
 | 688 | 	tsk->mm = NULL; | 
 | 689 | 	up_read(&mm->mmap_sem); | 
 | 690 | 	enter_lazy_tlb(mm, current); | 
 | 691 | 	task_unlock(tsk); | 
 | 692 | 	mm_update_next_owner(mm); | 
 | 693 | 	mmput(mm); | 
 | 694 | } | 
 | 695 |  | 
 | 696 | /* | 
 | 697 |  * When we die, we re-parent all our children, and try to: | 
 | 698 |  * 1. give them to another thread in our thread group, if such a member exists | 
 | 699 |  * 2. give it to the first ancestor process which prctl'd itself as a | 
 | 700 |  *    child_subreaper for its children (like a service manager) | 
 | 701 |  * 3. give it to the init process (PID 1) in our pid namespace | 
 | 702 |  */ | 
 | 703 | static struct task_struct *find_new_reaper(struct task_struct *father) | 
 | 704 | 	__releases(&tasklist_lock) | 
 | 705 | 	__acquires(&tasklist_lock) | 
 | 706 | { | 
 | 707 | 	struct pid_namespace *pid_ns = task_active_pid_ns(father); | 
 | 708 | 	struct task_struct *thread; | 
 | 709 |  | 
 | 710 | 	thread = father; | 
 | 711 | 	while_each_thread(father, thread) { | 
 | 712 | 		if (thread->flags & PF_EXITING) | 
 | 713 | 			continue; | 
 | 714 | 		if (unlikely(pid_ns->child_reaper == father)) | 
 | 715 | 			pid_ns->child_reaper = thread; | 
 | 716 | 		return thread; | 
 | 717 | 	} | 
 | 718 |  | 
 | 719 | 	if (unlikely(pid_ns->child_reaper == father)) { | 
 | 720 | 		write_unlock_irq(&tasklist_lock); | 
 | 721 | 		if (unlikely(pid_ns == &init_pid_ns)) { | 
| xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 722 | #ifdef CONFIG_FLAGS_UTILS | 
 | 723 | 			extern int flags_sys_switch(void); | 
 | 724 | 			printk(KERN_EMERG "Attempted to kill init! exitcode=0x%08x\n", | 
 | 725 | 				father->signal->group_exit_code ?: | 
 | 726 | 					father->exit_code); | 
 | 727 | 			if (flags_sys_switch() < 0) | 
 | 728 | 				panic("init: flags_sys_switch fail"); | 
 | 729 | 			else | 
 | 730 | 				kernel_restart("init: Switch to another system, please reset machine"); | 
 | 731 | #endif | 
| lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 732 | 			panic("Attempted to kill init! exitcode=0x%08x\n", | 
 | 733 | 				father->signal->group_exit_code ?: | 
 | 734 | 					father->exit_code); | 
 | 735 | 		} | 
 | 736 |  | 
 | 737 | 		zap_pid_ns_processes(pid_ns); | 
 | 738 | 		write_lock_irq(&tasklist_lock); | 
 | 739 | 		/* | 
 | 740 | 		 * We can not clear ->child_reaper or leave it alone. | 
 | 741 | 		 * There may by stealth EXIT_DEAD tasks on ->children, | 
 | 742 | 		 * forget_original_parent() must move them somewhere. | 
 | 743 | 		 */ | 
 | 744 | 		pid_ns->child_reaper = init_pid_ns.child_reaper; | 
 | 745 | 	} else if (father->signal->has_child_subreaper) { | 
 | 746 | 		struct task_struct *reaper; | 
 | 747 |  | 
 | 748 | 		/* | 
 | 749 | 		 * Find the first ancestor marked as child_subreaper. | 
 | 750 | 		 * Note that the code below checks same_thread_group(reaper, | 
 | 751 | 		 * pid_ns->child_reaper).  This is what we need to DTRT in a | 
 | 752 | 		 * PID namespace. However we still need the check above, see | 
 | 753 | 		 * http://marc.info/?l=linux-kernel&m=131385460420380 | 
 | 754 | 		 */ | 
 | 755 | 		for (reaper = father->real_parent; | 
 | 756 | 		     reaper != &init_task; | 
 | 757 | 		     reaper = reaper->real_parent) { | 
 | 758 | 			if (same_thread_group(reaper, pid_ns->child_reaper)) | 
 | 759 | 				break; | 
 | 760 | 			if (!reaper->signal->is_child_subreaper) | 
 | 761 | 				continue; | 
 | 762 | 			thread = reaper; | 
 | 763 | 			do { | 
 | 764 | 				if (!(thread->flags & PF_EXITING)) | 
 | 765 | 					return reaper; | 
 | 766 | 			} while_each_thread(reaper, thread); | 
 | 767 | 		} | 
 | 768 | 	} | 
 | 769 |  | 
 | 770 | 	return pid_ns->child_reaper; | 
 | 771 | } | 
 | 772 |  | 
 | 773 | /* | 
 | 774 | * Any that need to be release_task'd are put on the @dead list. | 
 | 775 |  */ | 
 | 776 | static void reparent_leader(struct task_struct *father, struct task_struct *p, | 
 | 777 | 				struct list_head *dead) | 
 | 778 | { | 
 | 779 | 	list_move_tail(&p->sibling, &p->real_parent->children); | 
 | 780 | 	/* | 
 | 781 | 	 * If this is a threaded reparent there is no need to | 
 | 782 | 	 * notify anyone anything has happened. | 
 | 783 | 	 */ | 
 | 784 | 	if (same_thread_group(p->real_parent, father)) | 
 | 785 | 		return; | 
 | 786 |  | 
 | 787 | 	/* | 
 | 788 | 	 * We don't want people slaying init. | 
 | 789 | 	 * | 
 | 790 | 	 * Note: we do this even if it is EXIT_DEAD, wait_task_zombie() | 
 | 791 | 	 * can change ->exit_state to EXIT_ZOMBIE. If this is the final | 
 | 792 | 	 * state, do_notify_parent() was already called and ->exit_signal | 
 | 793 | 	 * doesn't matter. | 
 | 794 | 	 */ | 
 | 795 | 	p->exit_signal = SIGCHLD; | 
 | 796 |  | 
 | 797 | 	if (p->exit_state == EXIT_DEAD) | 
 | 798 | 		return; | 
 | 799 |  | 
 | 800 | 	/* If it has exited notify the new parent about this child's death. */ | 
 | 801 | 	if (!p->ptrace && | 
 | 802 | 	    p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) { | 
 | 803 | 		if (do_notify_parent(p, p->exit_signal)) { | 
 | 804 | 			p->exit_state = EXIT_DEAD; | 
 | 805 | 			list_move_tail(&p->sibling, dead); | 
 | 806 | 		} | 
 | 807 | 	} | 
 | 808 |  | 
 | 809 | 	kill_orphaned_pgrp(p, father); | 
 | 810 | } | 
 | 811 |  | 
 | 812 | static void forget_original_parent(struct task_struct *father) | 
 | 813 | { | 
 | 814 | 	struct task_struct *p, *n, *reaper; | 
 | 815 | 	LIST_HEAD(dead_children); | 
 | 816 |  | 
 | 817 | 	write_lock_irq(&tasklist_lock); | 
 | 818 | 	/* | 
 | 819 | 	 * Note that exit_ptrace() and find_new_reaper() might | 
 | 820 | 	 * drop tasklist_lock and reacquire it. | 
 | 821 | 	 */ | 
 | 822 | 	exit_ptrace(father); | 
 | 823 | 	reaper = find_new_reaper(father); | 
 | 824 |  | 
 | 825 | 	list_for_each_entry_safe(p, n, &father->children, sibling) { | 
 | 826 | 		struct task_struct *t = p; | 
 | 827 | 		do { | 
 | 828 | 			t->real_parent = reaper; | 
 | 829 | 			if (t->parent == father) { | 
 | 830 | 				BUG_ON(t->ptrace); | 
 | 831 | 				t->parent = t->real_parent; | 
 | 832 | 			} | 
 | 833 | 			if (t->pdeath_signal) | 
 | 834 | 				group_send_sig_info(t->pdeath_signal, | 
 | 835 | 						    SEND_SIG_NOINFO, t); | 
 | 836 | 		} while_each_thread(p, t); | 
 | 837 | 		reparent_leader(father, p, &dead_children); | 
 | 838 | 	} | 
 | 839 | 	write_unlock_irq(&tasklist_lock); | 
 | 840 |  | 
 | 841 | 	BUG_ON(!list_empty(&father->children)); | 
 | 842 |  | 
 | 843 | 	list_for_each_entry_safe(p, n, &dead_children, sibling) { | 
 | 844 | 		list_del_init(&p->sibling); | 
 | 845 | 		release_task(p); | 
 | 846 | 	} | 
 | 847 | } | 
 | 848 |  | 
 | 849 | /* | 
 | 850 |  * Send signals to all our closest relatives so that they know | 
 | 851 |  * to properly mourn us.. | 
 | 852 |  */ | 
 | 853 | static void exit_notify(struct task_struct *tsk, int group_dead) | 
 | 854 | { | 
 | 855 | 	bool autoreap; | 
 | 856 |  | 
 | 857 | 	/* | 
 | 858 | 	 * This does two things: | 
 | 859 | 	 * | 
 | 860 |   	 * A.  Make init inherit all the child processes | 
 | 861 | 	 * B.  Check to see if any process groups have become orphaned | 
 | 862 | 	 *	as a result of our exiting, and if they have any stopped | 
 | 863 | 	 *	jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2) | 
 | 864 | 	 */ | 
 | 865 | 	forget_original_parent(tsk); | 
 | 866 | 	exit_task_namespaces(tsk); | 
 | 867 |  | 
 | 868 | 	write_lock_irq(&tasklist_lock); | 
 | 869 | 	if (group_dead) | 
 | 870 | 		kill_orphaned_pgrp(tsk->group_leader, NULL); | 
 | 871 |  | 
 | 872 | 	if (unlikely(tsk->ptrace)) { | 
 | 873 | 		int sig = thread_group_leader(tsk) && | 
 | 874 | 				thread_group_empty(tsk) && | 
 | 875 | 				!ptrace_reparented(tsk) ? | 
 | 876 | 			tsk->exit_signal : SIGCHLD; | 
 | 877 | 		autoreap = do_notify_parent(tsk, sig); | 
 | 878 | 	} else if (thread_group_leader(tsk)) { | 
 | 879 | 		autoreap = thread_group_empty(tsk) && | 
 | 880 | 			do_notify_parent(tsk, tsk->exit_signal); | 
 | 881 | 	} else { | 
 | 882 | 		autoreap = true; | 
 | 883 | 	} | 
 | 884 |  | 
 | 885 | 	tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE; | 
 | 886 |  | 
 | 887 | 	/* mt-exec, de_thread() is waiting for group leader */ | 
 | 888 | 	if (unlikely(tsk->signal->notify_count < 0)) | 
 | 889 | 		wake_up_process(tsk->signal->group_exit_task); | 
 | 890 | 	write_unlock_irq(&tasklist_lock); | 
 | 891 |  | 
 | 892 | 	/* If the process is dead, release it - nobody will wait for it */ | 
 | 893 | 	if (autoreap) | 
 | 894 | 		release_task(tsk); | 
 | 895 | } | 
 | 896 |  | 
 | 897 | #ifdef CONFIG_DEBUG_STACK_USAGE | 
 | 898 | static void check_stack_usage(void) | 
 | 899 | { | 
 | 900 | 	static DEFINE_SPINLOCK(low_water_lock); | 
 | 901 | 	static int lowest_to_date = THREAD_SIZE; | 
 | 902 | 	unsigned long free; | 
 | 903 |  | 
 | 904 | 	free = stack_not_used(current); | 
 | 905 |  | 
 | 906 | 	if (free >= lowest_to_date) | 
 | 907 | 		return; | 
 | 908 |  | 
 | 909 | 	spin_lock(&low_water_lock); | 
 | 910 | 	if (free < lowest_to_date) { | 
 | 911 | 		printk(KERN_WARNING "%s used greatest stack depth: %lu bytes " | 
 | 912 | 				"left\n", | 
 | 913 | 				current->comm, free); | 
 | 914 | 		lowest_to_date = free; | 
 | 915 | 	} | 
 | 916 | 	spin_unlock(&low_water_lock); | 
 | 917 | } | 
 | 918 | #else | 
 | 919 | static inline void check_stack_usage(void) {} | 
 | 920 | #endif | 
 | 921 |  | 
 | 922 | void do_exit(long code) | 
 | 923 | { | 
 | 924 | 	struct task_struct *tsk = current; | 
 | 925 | 	int group_dead; | 
 | 926 |  | 
 | 927 | 	profile_task_exit(tsk); | 
 | 928 |  | 
 | 929 | 	WARN_ON(blk_needs_flush_plug(tsk)); | 
 | 930 |  | 
 | 931 | 	if (unlikely(in_interrupt())) | 
 | 932 | 		panic("Aiee, killing interrupt handler!"); | 
 | 933 | 	if (unlikely(!tsk->pid)) | 
 | 934 | 		panic("Attempted to kill the idle task!"); | 
 | 935 |  | 
 | 936 | 	/* | 
 | 937 | 	 * If do_exit is called because this processes oopsed, it's possible | 
 | 938 | 	 * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before | 
 | 939 | 	 * continuing. Amongst other possible reasons, this is to prevent | 
 | 940 | 	 * mm_release()->clear_child_tid() from writing to a user-controlled | 
 | 941 | 	 * kernel address. | 
 | 942 | 	 */ | 
 | 943 | 	set_fs(USER_DS); | 
 | 944 |  | 
 | 945 | 	ptrace_event(PTRACE_EVENT_EXIT, code); | 
 | 946 |  | 
 | 947 | 	validate_creds_for_do_exit(tsk); | 
 | 948 |  | 
 | 949 | 	/* | 
 | 950 | 	 * We're taking recursive faults here in do_exit. Safest is to just | 
 | 951 | 	 * leave this task alone and wait for reboot. | 
 | 952 | 	 */ | 
 | 953 | 	if (unlikely(tsk->flags & PF_EXITING)) { | 
 | 954 | 		printk(KERN_ALERT | 
 | 955 | 			"Fixing recursive fault but reboot is needed!\n"); | 
 | 956 | 		/* | 
 | 957 | 		 * We can do this unlocked here. The futex code uses | 
 | 958 | 		 * this flag just to verify whether the pi state | 
 | 959 | 		 * cleanup has been done or not. In the worst case it | 
 | 960 | 		 * loops once more. We pretend that the cleanup was | 
 | 961 | 		 * done as there is no way to return. Either the | 
 | 962 | 		 * OWNER_DIED bit is set by now or we push the blocked | 
 | 963 | 		 * task into the wait for ever nirwana as well. | 
 | 964 | 		 */ | 
 | 965 | 		tsk->flags |= PF_EXITPIDONE; | 
 | 966 | 		set_current_state(TASK_UNINTERRUPTIBLE); | 
 | 967 | 		schedule(); | 
 | 968 | 	} | 
 | 969 |  | 
 | 970 | 	exit_signals(tsk);  /* sets PF_EXITING */ | 
 | 971 | 	/* | 
 | 972 | 	 * tsk->flags are checked in the futex code to protect against | 
 | 973 | 	 * an exiting task cleaning up the robust pi futexes. | 
 | 974 | 	 */ | 
 | 975 | 	smp_mb(); | 
 | 976 | 	raw_spin_unlock_wait(&tsk->pi_lock); | 
 | 977 |  | 
 | 978 | 	exit_irq_thread(); | 
 | 979 |  | 
 | 980 | 	if (unlikely(in_atomic())) | 
 | 981 | 		printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n", | 
 | 982 | 				current->comm, task_pid_nr(current), | 
 | 983 | 				preempt_count()); | 
 | 984 |  | 
 | 985 | 	acct_update_integrals(tsk); | 
 | 986 | 	/* sync mm's RSS info before statistics gathering */ | 
 | 987 | 	if (tsk->mm) | 
 | 988 | 		sync_mm_rss(tsk->mm); | 
 | 989 | 	group_dead = atomic_dec_and_test(&tsk->signal->live); | 
 | 990 | 	if (group_dead) { | 
 | 991 | 		hrtimer_cancel(&tsk->signal->real_timer); | 
 | 992 | 		exit_itimers(tsk->signal); | 
 | 993 | 		if (tsk->mm) | 
 | 994 | 			setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm); | 
 | 995 | 	} | 
 | 996 | 	acct_collect(code, group_dead); | 
 | 997 | 	if (group_dead) | 
 | 998 | 		tty_audit_exit(); | 
 | 999 | 	audit_free(tsk); | 
 | 1000 |  | 
 | 1001 | 	tsk->exit_code = code; | 
 | 1002 | 	taskstats_exit(tsk, group_dead); | 
 | 1003 |  | 
 | 1004 | 	exit_mm(tsk); | 
 | 1005 |  | 
 | 1006 | 	if (group_dead) | 
 | 1007 | 		acct_process(); | 
 | 1008 | 	trace_sched_process_exit(tsk); | 
 | 1009 |  | 
 | 1010 | 	exit_sem(tsk); | 
 | 1011 | 	exit_shm(tsk); | 
 | 1012 | 	exit_files(tsk); | 
 | 1013 | 	exit_fs(tsk); | 
 | 1014 | 	check_stack_usage(); | 
 | 1015 | 	exit_thread(); | 
 | 1016 |  | 
 | 1017 | 	/* | 
 | 1018 | 	 * Flush inherited counters to the parent - before the parent | 
 | 1019 | 	 * gets woken up by child-exit notifications. | 
 | 1020 | 	 * | 
 | 1021 | 	 * because of cgroup mode, must be called before cgroup_exit() | 
 | 1022 | 	 */ | 
 | 1023 | 	perf_event_exit_task(tsk); | 
 | 1024 |  | 
 | 1025 | 	cgroup_exit(tsk, 1); | 
 | 1026 |  | 
 | 1027 | 	if (group_dead) | 
 | 1028 | 		disassociate_ctty(1); | 
 | 1029 |  | 
 | 1030 | 	module_put(task_thread_info(tsk)->exec_domain->module); | 
 | 1031 |  | 
 | 1032 | 	proc_exit_connector(tsk); | 
 | 1033 |  | 
 | 1034 | 	/* | 
 | 1035 | 	 * FIXME: do that only when needed, using sched_exit tracepoint | 
 | 1036 | 	 */ | 
 | 1037 | 	ptrace_put_breakpoints(tsk); | 
 | 1038 |  | 
 | 1039 | 	exit_notify(tsk, group_dead); | 
 | 1040 | #ifdef CONFIG_NUMA | 
 | 1041 | 	task_lock(tsk); | 
 | 1042 | 	mpol_put(tsk->mempolicy); | 
 | 1043 | 	tsk->mempolicy = NULL; | 
 | 1044 | 	task_unlock(tsk); | 
 | 1045 | #endif | 
 | 1046 | #ifdef CONFIG_FUTEX | 
 | 1047 | 	if (unlikely(current->pi_state_cache)) | 
 | 1048 | 		kfree(current->pi_state_cache); | 
 | 1049 | #endif | 
 | 1050 | 	/* | 
 | 1051 | 	 * Make sure we are holding no locks: | 
 | 1052 | 	 */ | 
 | 1053 | 	debug_check_no_locks_held(tsk); | 
 | 1054 | 	/* | 
 | 1055 | 	 * We can do this unlocked here. The futex code uses this flag | 
 | 1056 | 	 * just to verify whether the pi state cleanup has been done | 
 | 1057 | 	 * or not. In the worst case it loops once more. | 
 | 1058 | 	 */ | 
 | 1059 | 	tsk->flags |= PF_EXITPIDONE; | 
 | 1060 |  | 
 | 1061 | 	if (tsk->io_context) | 
 | 1062 | 		exit_io_context(tsk); | 
 | 1063 |  | 
 | 1064 | 	if (tsk->splice_pipe) | 
 | 1065 | 		__free_pipe_info(tsk->splice_pipe); | 
 | 1066 |  | 
 | 1067 | 	validate_creds_for_do_exit(tsk); | 
 | 1068 |  | 
 | 1069 | 	preempt_disable(); | 
 | 1070 | 	if (tsk->nr_dirtied) | 
 | 1071 | 		__this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied); | 
 | 1072 | 	exit_rcu(); | 
 | 1073 |  | 
 | 1074 | 	/* | 
 | 1075 | 	 * The setting of TASK_RUNNING by try_to_wake_up() may be delayed | 
 | 1076 | 	 * when the following two conditions become true. | 
 | 1077 | 	 *   - There is race condition of mmap_sem (It is acquired by | 
 | 1078 | 	 *     exit_mm()), and | 
 | 1079 | 	 *   - SMI occurs before setting TASK_RUNINNG. | 
 | 1080 | 	 *     (or hypervisor of virtual machine switches to other guest) | 
 | 1081 | 	 *  As a result, we may become TASK_RUNNING after becoming TASK_DEAD | 
 | 1082 | 	 * | 
 | 1083 | 	 * To avoid it, we have to wait for releasing tsk->pi_lock which | 
 | 1084 | 	 * is held by try_to_wake_up() | 
 | 1085 | 	 */ | 
 | 1086 | 	smp_mb(); | 
 | 1087 | 	raw_spin_unlock_wait(&tsk->pi_lock); | 
 | 1088 |  | 
 | 1089 | 	/* causes final put_task_struct in finish_task_switch(). */ | 
 | 1090 | 	tsk->state = TASK_DEAD; | 
 | 1091 | 	tsk->flags |= PF_NOFREEZE;	/* tell freezer to ignore us */ | 
 | 1092 | 	schedule(); | 
 | 1093 | 	BUG(); | 
 | 1094 | 	/* Avoid "noreturn function does return".  */ | 
 | 1095 | 	for (;;) | 
 | 1096 | 		cpu_relax();	/* For when BUG is null */ | 
 | 1097 | } | 
 | 1098 |  | 
 | 1099 | EXPORT_SYMBOL(do_exit); | 
 | 1100 |  | 
 | 1101 | void complete_and_exit(struct completion *comp, long code) | 
 | 1102 | { | 
 | 1103 | 	if (comp) | 
 | 1104 | 		complete(comp); | 
 | 1105 |  | 
 | 1106 | 	do_exit(code); | 
 | 1107 | } | 
 | 1108 |  | 
 | 1109 | EXPORT_SYMBOL(complete_and_exit); | 
 | 1110 |  | 
 | 1111 | SYSCALL_DEFINE1(exit, int, error_code) | 
 | 1112 | { | 
 | 1113 | 	do_exit((error_code&0xff)<<8); | 
 | 1114 | } | 
 | 1115 |  | 
 | 1116 | /* | 
 | 1117 |  * Take down every thread in the group.  This is called by fatal signals | 
 | 1118 |  * as well as by sys_exit_group (below). | 
 | 1119 |  */ | 
 | 1120 | void | 
 | 1121 | do_group_exit(int exit_code) | 
 | 1122 | { | 
 | 1123 | 	struct signal_struct *sig = current->signal; | 
 | 1124 |  | 
 | 1125 | 	BUG_ON(exit_code & 0x80); /* core dumps don't get here */ | 
 | 1126 |  | 
 | 1127 | 	if (signal_group_exit(sig)) | 
 | 1128 | 		exit_code = sig->group_exit_code; | 
 | 1129 | 	else if (!thread_group_empty(current)) { | 
 | 1130 | 		struct sighand_struct *const sighand = current->sighand; | 
 | 1131 | 		spin_lock_irq(&sighand->siglock); | 
 | 1132 | 		if (signal_group_exit(sig)) | 
 | 1133 | 			/* Another thread got here before we took the lock.  */ | 
 | 1134 | 			exit_code = sig->group_exit_code; | 
 | 1135 | 		else { | 
 | 1136 | 			sig->group_exit_code = exit_code; | 
 | 1137 | 			sig->flags = SIGNAL_GROUP_EXIT; | 
 | 1138 | 			zap_other_threads(current); | 
 | 1139 | 		} | 
 | 1140 | 		spin_unlock_irq(&sighand->siglock); | 
 | 1141 | 	} | 
 | 1142 |  | 
 | 1143 | 	do_exit(exit_code); | 
 | 1144 | 	/* NOTREACHED */ | 
 | 1145 | } | 
 | 1146 |  | 
 | 1147 | /* | 
 | 1148 |  * this kills every thread in the thread group. Note that any externally | 
 | 1149 |  * wait4()-ing process will get the correct exit code - even if this | 
 | 1150 |  * thread is not the thread group leader. | 
 | 1151 |  */ | 
 | 1152 | SYSCALL_DEFINE1(exit_group, int, error_code) | 
 | 1153 | { | 
 | 1154 | 	do_group_exit((error_code & 0xff) << 8); | 
 | 1155 | 	/* NOTREACHED */ | 
 | 1156 | 	return 0; | 
 | 1157 | } | 
 | 1158 |  | 
 | 1159 | struct wait_opts { | 
 | 1160 | 	enum pid_type		wo_type; | 
 | 1161 | 	int			wo_flags; | 
 | 1162 | 	struct pid		*wo_pid; | 
 | 1163 |  | 
 | 1164 | 	struct siginfo __user	*wo_info; | 
 | 1165 | 	int __user		*wo_stat; | 
 | 1166 | 	struct rusage __user	*wo_rusage; | 
 | 1167 |  | 
 | 1168 | 	wait_queue_t		child_wait; | 
 | 1169 | 	int			notask_error; | 
 | 1170 | }; | 
 | 1171 |  | 
 | 1172 | static inline | 
 | 1173 | struct pid *task_pid_type(struct task_struct *task, enum pid_type type) | 
 | 1174 | { | 
 | 1175 | 	if (type != PIDTYPE_PID) | 
 | 1176 | 		task = task->group_leader; | 
 | 1177 | 	return task->pids[type].pid; | 
 | 1178 | } | 
 | 1179 |  | 
 | 1180 | static int eligible_pid(struct wait_opts *wo, struct task_struct *p) | 
 | 1181 | { | 
 | 1182 | 	return	wo->wo_type == PIDTYPE_MAX || | 
 | 1183 | 		task_pid_type(p, wo->wo_type) == wo->wo_pid; | 
 | 1184 | } | 
 | 1185 |  | 
 | 1186 | static int eligible_child(struct wait_opts *wo, struct task_struct *p) | 
 | 1187 | { | 
 | 1188 | 	if (!eligible_pid(wo, p)) | 
 | 1189 | 		return 0; | 
 | 1190 | 	/* Wait for all children (clone and not) if __WALL is set; | 
 | 1191 | 	 * otherwise, wait for clone children *only* if __WCLONE is | 
 | 1192 | 	 * set; otherwise, wait for non-clone children *only*.  (Note: | 
 | 1193 | 	 * A "clone" child here is one that reports to its parent | 
 | 1194 | 	 * using a signal other than SIGCHLD.) */ | 
 | 1195 | 	if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE)) | 
 | 1196 | 	    && !(wo->wo_flags & __WALL)) | 
 | 1197 | 		return 0; | 
 | 1198 |  | 
 | 1199 | 	return 1; | 
 | 1200 | } | 
 | 1201 |  | 
 | 1202 | static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p, | 
 | 1203 | 				pid_t pid, uid_t uid, int why, int status) | 
 | 1204 | { | 
 | 1205 | 	struct siginfo __user *infop; | 
 | 1206 | 	int retval = wo->wo_rusage | 
 | 1207 | 		? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0; | 
 | 1208 |  | 
 | 1209 | 	put_task_struct(p); | 
 | 1210 | 	infop = wo->wo_info; | 
 | 1211 | 	if (infop) { | 
 | 1212 | 		if (!retval) | 
 | 1213 | 			retval = put_user(SIGCHLD, &infop->si_signo); | 
 | 1214 | 		if (!retval) | 
 | 1215 | 			retval = put_user(0, &infop->si_errno); | 
 | 1216 | 		if (!retval) | 
 | 1217 | 			retval = put_user((short)why, &infop->si_code); | 
 | 1218 | 		if (!retval) | 
 | 1219 | 			retval = put_user(pid, &infop->si_pid); | 
 | 1220 | 		if (!retval) | 
 | 1221 | 			retval = put_user(uid, &infop->si_uid); | 
 | 1222 | 		if (!retval) | 
 | 1223 | 			retval = put_user(status, &infop->si_status); | 
 | 1224 | 	} | 
 | 1225 | 	if (!retval) | 
 | 1226 | 		retval = pid; | 
 | 1227 | 	return retval; | 
 | 1228 | } | 
 | 1229 |  | 
 | 1230 | /* | 
 | 1231 |  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold | 
 | 1232 |  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold | 
 | 1233 |  * the lock and this task is uninteresting.  If we return nonzero, we have | 
 | 1234 |  * released the lock and the system call should return. | 
 | 1235 |  */ | 
 | 1236 | static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p) | 
 | 1237 | { | 
 | 1238 | 	unsigned long state; | 
 | 1239 | 	int retval, status, traced; | 
 | 1240 | 	pid_t pid = task_pid_vnr(p); | 
 | 1241 | 	uid_t uid = __task_cred(p)->uid; | 
 | 1242 | 	struct siginfo __user *infop; | 
 | 1243 |  | 
 | 1244 | 	if (!likely(wo->wo_flags & WEXITED)) | 
 | 1245 | 		return 0; | 
 | 1246 |  | 
 | 1247 | 	if (unlikely(wo->wo_flags & WNOWAIT)) { | 
 | 1248 | 		int exit_code = p->exit_code; | 
 | 1249 | 		int why; | 
 | 1250 |  | 
 | 1251 | 		get_task_struct(p); | 
 | 1252 | 		read_unlock(&tasklist_lock); | 
 | 1253 | 		if ((exit_code & 0x7f) == 0) { | 
 | 1254 | 			why = CLD_EXITED; | 
 | 1255 | 			status = exit_code >> 8; | 
 | 1256 | 		} else { | 
 | 1257 | 			why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED; | 
 | 1258 | 			status = exit_code & 0x7f; | 
 | 1259 | 		} | 
 | 1260 | 		return wait_noreap_copyout(wo, p, pid, uid, why, status); | 
 | 1261 | 	} | 
 | 1262 |  | 
 | 1263 | 	/* | 
 | 1264 | 	 * Try to move the task's state to DEAD | 
 | 1265 | 	 * only one thread is allowed to do this: | 
 | 1266 | 	 */ | 
 | 1267 | 	state = xchg(&p->exit_state, EXIT_DEAD); | 
 | 1268 | 	if (state != EXIT_ZOMBIE) { | 
 | 1269 | 		BUG_ON(state != EXIT_DEAD); | 
 | 1270 | 		return 0; | 
 | 1271 | 	} | 
 | 1272 |  | 
 | 1273 | 	traced = ptrace_reparented(p); | 
 | 1274 | 	/* | 
 | 1275 | 	 * It can be ptraced but not reparented, check | 
 | 1276 | 	 * thread_group_leader() to filter out sub-threads. | 
 | 1277 | 	 */ | 
 | 1278 | 	if (likely(!traced) && thread_group_leader(p)) { | 
 | 1279 | 		struct signal_struct *psig; | 
 | 1280 | 		struct signal_struct *sig; | 
 | 1281 | 		unsigned long maxrss; | 
 | 1282 | 		cputime_t tgutime, tgstime; | 
 | 1283 |  | 
 | 1284 | 		/* | 
 | 1285 | 		 * The resource counters for the group leader are in its | 
 | 1286 | 		 * own task_struct.  Those for dead threads in the group | 
 | 1287 | 		 * are in its signal_struct, as are those for the child | 
 | 1288 | 		 * processes it has previously reaped.  All these | 
 | 1289 | 		 * accumulate in the parent's signal_struct c* fields. | 
 | 1290 | 		 * | 
 | 1291 | 		 * We don't bother to take a lock here to protect these | 
 | 1292 | 		 * p->signal fields, because they are only touched by | 
 | 1293 | 		 * __exit_signal, which runs with tasklist_lock | 
 | 1294 | 		 * write-locked anyway, and so is excluded here.  We do | 
 | 1295 | 		 * need to protect the access to parent->signal fields, | 
 | 1296 | 		 * as other threads in the parent group can be right | 
 | 1297 | 		 * here reaping other children at the same time. | 
 | 1298 | 		 * | 
 | 1299 | 		 * We use thread_group_times() to get times for the thread | 
 | 1300 | 		 * group, which consolidates times for all threads in the | 
 | 1301 | 		 * group including the group leader. | 
 | 1302 | 		 */ | 
 | 1303 | 		thread_group_times(p, &tgutime, &tgstime); | 
 | 1304 | 		spin_lock_irq(&p->real_parent->sighand->siglock); | 
 | 1305 | 		psig = p->real_parent->signal; | 
 | 1306 | 		sig = p->signal; | 
 | 1307 | 		psig->cutime += tgutime + sig->cutime; | 
 | 1308 | 		psig->cstime += tgstime + sig->cstime; | 
 | 1309 | 		psig->cgtime += p->gtime + sig->gtime + sig->cgtime; | 
 | 1310 | 		psig->cmin_flt += | 
 | 1311 | 			p->min_flt + sig->min_flt + sig->cmin_flt; | 
 | 1312 | 		psig->cmaj_flt += | 
 | 1313 | 			p->maj_flt + sig->maj_flt + sig->cmaj_flt; | 
 | 1314 | 		psig->cnvcsw += | 
 | 1315 | 			p->nvcsw + sig->nvcsw + sig->cnvcsw; | 
 | 1316 | 		psig->cnivcsw += | 
 | 1317 | 			p->nivcsw + sig->nivcsw + sig->cnivcsw; | 
 | 1318 | 		psig->cinblock += | 
 | 1319 | 			task_io_get_inblock(p) + | 
 | 1320 | 			sig->inblock + sig->cinblock; | 
 | 1321 | 		psig->coublock += | 
 | 1322 | 			task_io_get_oublock(p) + | 
 | 1323 | 			sig->oublock + sig->coublock; | 
 | 1324 | 		maxrss = max(sig->maxrss, sig->cmaxrss); | 
 | 1325 | 		if (psig->cmaxrss < maxrss) | 
 | 1326 | 			psig->cmaxrss = maxrss; | 
 | 1327 | 		task_io_accounting_add(&psig->ioac, &p->ioac); | 
 | 1328 | 		task_io_accounting_add(&psig->ioac, &sig->ioac); | 
 | 1329 | 		spin_unlock_irq(&p->real_parent->sighand->siglock); | 
 | 1330 | 	} | 
 | 1331 |  | 
 | 1332 | 	/* | 
 | 1333 | 	 * Now we are sure this task is interesting, and no other | 
 | 1334 | 	 * thread can reap it because we set its state to EXIT_DEAD. | 
 | 1335 | 	 */ | 
 | 1336 | 	read_unlock(&tasklist_lock); | 
 | 1337 |  | 
 | 1338 | 	retval = wo->wo_rusage | 
 | 1339 | 		? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0; | 
 | 1340 | 	status = (p->signal->flags & SIGNAL_GROUP_EXIT) | 
 | 1341 | 		? p->signal->group_exit_code : p->exit_code; | 
 | 1342 | 	if (!retval && wo->wo_stat) | 
 | 1343 | 		retval = put_user(status, wo->wo_stat); | 
 | 1344 |  | 
 | 1345 | 	infop = wo->wo_info; | 
 | 1346 | 	if (!retval && infop) | 
 | 1347 | 		retval = put_user(SIGCHLD, &infop->si_signo); | 
 | 1348 | 	if (!retval && infop) | 
 | 1349 | 		retval = put_user(0, &infop->si_errno); | 
 | 1350 | 	if (!retval && infop) { | 
 | 1351 | 		int why; | 
 | 1352 |  | 
 | 1353 | 		if ((status & 0x7f) == 0) { | 
 | 1354 | 			why = CLD_EXITED; | 
 | 1355 | 			status >>= 8; | 
 | 1356 | 		} else { | 
 | 1357 | 			why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED; | 
 | 1358 | 			status &= 0x7f; | 
 | 1359 | 		} | 
 | 1360 | 		retval = put_user((short)why, &infop->si_code); | 
 | 1361 | 		if (!retval) | 
 | 1362 | 			retval = put_user(status, &infop->si_status); | 
 | 1363 | 	} | 
 | 1364 | 	if (!retval && infop) | 
 | 1365 | 		retval = put_user(pid, &infop->si_pid); | 
 | 1366 | 	if (!retval && infop) | 
 | 1367 | 		retval = put_user(uid, &infop->si_uid); | 
 | 1368 | 	if (!retval) | 
 | 1369 | 		retval = pid; | 
 | 1370 |  | 
 | 1371 | 	if (traced) { | 
 | 1372 | 		write_lock_irq(&tasklist_lock); | 
 | 1373 | 		/* We dropped tasklist, ptracer could die and untrace */ | 
 | 1374 | 		ptrace_unlink(p); | 
 | 1375 | 		/* | 
 | 1376 | 		 * If this is not a sub-thread, notify the parent. | 
 | 1377 | 		 * If parent wants a zombie, don't release it now. | 
 | 1378 | 		 */ | 
 | 1379 | 		if (thread_group_leader(p) && | 
 | 1380 | 		    !do_notify_parent(p, p->exit_signal)) { | 
 | 1381 | 			p->exit_state = EXIT_ZOMBIE; | 
 | 1382 | 			p = NULL; | 
 | 1383 | 		} | 
 | 1384 | 		write_unlock_irq(&tasklist_lock); | 
 | 1385 | 	} | 
 | 1386 | 	if (p != NULL) | 
 | 1387 | 		release_task(p); | 
 | 1388 |  | 
 | 1389 | 	return retval; | 
 | 1390 | } | 
 | 1391 |  | 
 | 1392 | static int *task_stopped_code(struct task_struct *p, bool ptrace) | 
 | 1393 | { | 
 | 1394 | 	if (ptrace) { | 
 | 1395 | 		if (task_is_stopped_or_traced(p) && | 
 | 1396 | 		    !(p->jobctl & JOBCTL_LISTENING)) | 
 | 1397 | 			return &p->exit_code; | 
 | 1398 | 	} else { | 
 | 1399 | 		if (p->signal->flags & SIGNAL_STOP_STOPPED) | 
 | 1400 | 			return &p->signal->group_exit_code; | 
 | 1401 | 	} | 
 | 1402 | 	return NULL; | 
 | 1403 | } | 
 | 1404 |  | 
 | 1405 | /** | 
 | 1406 |  * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED | 
 | 1407 |  * @wo: wait options | 
 | 1408 |  * @ptrace: is the wait for ptrace | 
 | 1409 |  * @p: task to wait for | 
 | 1410 |  * | 
 | 1411 |  * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED. | 
 | 1412 |  * | 
 | 1413 |  * CONTEXT: | 
 | 1414 |  * read_lock(&tasklist_lock), which is released if return value is | 
 | 1415 |  * non-zero.  Also, grabs and releases @p->sighand->siglock. | 
 | 1416 |  * | 
 | 1417 |  * RETURNS: | 
 | 1418 |  * 0 if wait condition didn't exist and search for other wait conditions | 
 | 1419 |  * should continue.  Non-zero return, -errno on failure and @p's pid on | 
 | 1420 |  * success, implies that tasklist_lock is released and wait condition | 
 | 1421 |  * search should terminate. | 
 | 1422 |  */ | 
 | 1423 | static int wait_task_stopped(struct wait_opts *wo, | 
 | 1424 | 				int ptrace, struct task_struct *p) | 
 | 1425 | { | 
 | 1426 | 	struct siginfo __user *infop; | 
 | 1427 | 	int retval, exit_code, *p_code, why; | 
 | 1428 | 	uid_t uid = 0; /* unneeded, required by compiler */ | 
 | 1429 | 	pid_t pid; | 
 | 1430 |  | 
 | 1431 | 	/* | 
 | 1432 | 	 * Traditionally we see ptrace'd stopped tasks regardless of options. | 
 | 1433 | 	 */ | 
 | 1434 | 	if (!ptrace && !(wo->wo_flags & WUNTRACED)) | 
 | 1435 | 		return 0; | 
 | 1436 |  | 
 | 1437 | 	if (!task_stopped_code(p, ptrace)) | 
 | 1438 | 		return 0; | 
 | 1439 |  | 
 | 1440 | 	exit_code = 0; | 
 | 1441 | 	spin_lock_irq(&p->sighand->siglock); | 
 | 1442 |  | 
 | 1443 | 	p_code = task_stopped_code(p, ptrace); | 
 | 1444 | 	if (unlikely(!p_code)) | 
 | 1445 | 		goto unlock_sig; | 
 | 1446 |  | 
 | 1447 | 	exit_code = *p_code; | 
 | 1448 | 	if (!exit_code) | 
 | 1449 | 		goto unlock_sig; | 
 | 1450 |  | 
 | 1451 | 	if (!unlikely(wo->wo_flags & WNOWAIT)) | 
 | 1452 | 		*p_code = 0; | 
 | 1453 |  | 
 | 1454 | 	uid = task_uid(p); | 
 | 1455 | unlock_sig: | 
 | 1456 | 	spin_unlock_irq(&p->sighand->siglock); | 
 | 1457 | 	if (!exit_code) | 
 | 1458 | 		return 0; | 
 | 1459 |  | 
 | 1460 | 	/* | 
 | 1461 | 	 * Now we are pretty sure this task is interesting. | 
 | 1462 | 	 * Make sure it doesn't get reaped out from under us while we | 
 | 1463 | 	 * give up the lock and then examine it below.  We don't want to | 
 | 1464 | 	 * keep holding onto the tasklist_lock while we call getrusage and | 
 | 1465 | 	 * possibly take page faults for user memory. | 
 | 1466 | 	 */ | 
 | 1467 | 	get_task_struct(p); | 
 | 1468 | 	pid = task_pid_vnr(p); | 
 | 1469 | 	why = ptrace ? CLD_TRAPPED : CLD_STOPPED; | 
 | 1470 | 	read_unlock(&tasklist_lock); | 
 | 1471 |  | 
 | 1472 | 	if (unlikely(wo->wo_flags & WNOWAIT)) | 
 | 1473 | 		return wait_noreap_copyout(wo, p, pid, uid, why, exit_code); | 
 | 1474 |  | 
 | 1475 | 	retval = wo->wo_rusage | 
 | 1476 | 		? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0; | 
 | 1477 | 	if (!retval && wo->wo_stat) | 
 | 1478 | 		retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat); | 
 | 1479 |  | 
 | 1480 | 	infop = wo->wo_info; | 
 | 1481 | 	if (!retval && infop) | 
 | 1482 | 		retval = put_user(SIGCHLD, &infop->si_signo); | 
 | 1483 | 	if (!retval && infop) | 
 | 1484 | 		retval = put_user(0, &infop->si_errno); | 
 | 1485 | 	if (!retval && infop) | 
 | 1486 | 		retval = put_user((short)why, &infop->si_code); | 
 | 1487 | 	if (!retval && infop) | 
 | 1488 | 		retval = put_user(exit_code, &infop->si_status); | 
 | 1489 | 	if (!retval && infop) | 
 | 1490 | 		retval = put_user(pid, &infop->si_pid); | 
 | 1491 | 	if (!retval && infop) | 
 | 1492 | 		retval = put_user(uid, &infop->si_uid); | 
 | 1493 | 	if (!retval) | 
 | 1494 | 		retval = pid; | 
 | 1495 | 	put_task_struct(p); | 
 | 1496 |  | 
 | 1497 | 	BUG_ON(!retval); | 
 | 1498 | 	return retval; | 
 | 1499 | } | 
 | 1500 |  | 
 | 1501 | /* | 
 | 1502 |  * Handle do_wait work for one task in a live, non-stopped state. | 
 | 1503 |  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold | 
 | 1504 |  * the lock and this task is uninteresting.  If we return nonzero, we have | 
 | 1505 |  * released the lock and the system call should return. | 
 | 1506 |  */ | 
 | 1507 | static int wait_task_continued(struct wait_opts *wo, struct task_struct *p) | 
 | 1508 | { | 
 | 1509 | 	int retval; | 
 | 1510 | 	pid_t pid; | 
 | 1511 | 	uid_t uid; | 
 | 1512 |  | 
 | 1513 | 	if (!unlikely(wo->wo_flags & WCONTINUED)) | 
 | 1514 | 		return 0; | 
 | 1515 |  | 
 | 1516 | 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) | 
 | 1517 | 		return 0; | 
 | 1518 |  | 
 | 1519 | 	spin_lock_irq(&p->sighand->siglock); | 
 | 1520 | 	/* Re-check with the lock held.  */ | 
 | 1521 | 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) { | 
 | 1522 | 		spin_unlock_irq(&p->sighand->siglock); | 
 | 1523 | 		return 0; | 
 | 1524 | 	} | 
 | 1525 | 	if (!unlikely(wo->wo_flags & WNOWAIT)) | 
 | 1526 | 		p->signal->flags &= ~SIGNAL_STOP_CONTINUED; | 
 | 1527 | 	uid = task_uid(p); | 
 | 1528 | 	spin_unlock_irq(&p->sighand->siglock); | 
 | 1529 |  | 
 | 1530 | 	pid = task_pid_vnr(p); | 
 | 1531 | 	get_task_struct(p); | 
 | 1532 | 	read_unlock(&tasklist_lock); | 
 | 1533 |  | 
 | 1534 | 	if (!wo->wo_info) { | 
 | 1535 | 		retval = wo->wo_rusage | 
 | 1536 | 			? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0; | 
 | 1537 | 		put_task_struct(p); | 
 | 1538 | 		if (!retval && wo->wo_stat) | 
 | 1539 | 			retval = put_user(0xffff, wo->wo_stat); | 
 | 1540 | 		if (!retval) | 
 | 1541 | 			retval = pid; | 
 | 1542 | 	} else { | 
 | 1543 | 		retval = wait_noreap_copyout(wo, p, pid, uid, | 
 | 1544 | 					     CLD_CONTINUED, SIGCONT); | 
 | 1545 | 		BUG_ON(retval == 0); | 
 | 1546 | 	} | 
 | 1547 |  | 
 | 1548 | 	return retval; | 
 | 1549 | } | 
 | 1550 |  | 
 | 1551 | /* | 
 | 1552 |  * Consider @p for a wait by @parent. | 
 | 1553 |  * | 
 | 1554 |  * -ECHILD should be in ->notask_error before the first call. | 
 | 1555 |  * Returns nonzero for a final return, when we have unlocked tasklist_lock. | 
 | 1556 |  * Returns zero if the search for a child should continue; | 
 | 1557 |  * then ->notask_error is 0 if @p is an eligible child, | 
 | 1558 |  * or another error from security_task_wait(), or still -ECHILD. | 
 | 1559 |  */ | 
 | 1560 | static int wait_consider_task(struct wait_opts *wo, int ptrace, | 
 | 1561 | 				struct task_struct *p) | 
 | 1562 | { | 
 | 1563 | 	int ret = eligible_child(wo, p); | 
 | 1564 | 	if (!ret) | 
 | 1565 | 		return ret; | 
 | 1566 |  | 
 | 1567 | 	ret = security_task_wait(p); | 
 | 1568 | 	if (unlikely(ret < 0)) { | 
 | 1569 | 		/* | 
 | 1570 | 		 * If we have not yet seen any eligible child, | 
 | 1571 | 		 * then let this error code replace -ECHILD. | 
 | 1572 | 		 * A permission error will give the user a clue | 
 | 1573 | 		 * to look for security policy problems, rather | 
 | 1574 | 		 * than for mysterious wait bugs. | 
 | 1575 | 		 */ | 
 | 1576 | 		if (wo->notask_error) | 
 | 1577 | 			wo->notask_error = ret; | 
 | 1578 | 		return 0; | 
 | 1579 | 	} | 
 | 1580 |  | 
 | 1581 | 	/* dead body doesn't have much to contribute */ | 
 | 1582 | 	if (unlikely(p->exit_state == EXIT_DEAD)) { | 
 | 1583 | 		/* | 
 | 1584 | 		 * But do not ignore this task until the tracer does | 
 | 1585 | 		 * wait_task_zombie()->do_notify_parent(). | 
 | 1586 | 		 */ | 
 | 1587 | 		if (likely(!ptrace) && unlikely(ptrace_reparented(p))) | 
 | 1588 | 			wo->notask_error = 0; | 
 | 1589 | 		return 0; | 
 | 1590 | 	} | 
 | 1591 |  | 
 | 1592 | 	/* slay zombie? */ | 
 | 1593 | 	if (p->exit_state == EXIT_ZOMBIE) { | 
 | 1594 | 		/* | 
 | 1595 | 		 * A zombie ptracee is only visible to its ptracer. | 
 | 1596 | 		 * Notification and reaping will be cascaded to the real | 
 | 1597 | 		 * parent when the ptracer detaches. | 
 | 1598 | 		 */ | 
 | 1599 | 		if (likely(!ptrace) && unlikely(p->ptrace)) { | 
 | 1600 | 			/* it will become visible, clear notask_error */ | 
 | 1601 | 			wo->notask_error = 0; | 
 | 1602 | 			return 0; | 
 | 1603 | 		} | 
 | 1604 |  | 
 | 1605 | 		/* we don't reap group leaders with subthreads */ | 
 | 1606 | 		if (!delay_group_leader(p)) | 
 | 1607 | 			return wait_task_zombie(wo, p); | 
 | 1608 |  | 
 | 1609 | 		/* | 
 | 1610 | 		 * Allow access to stopped/continued state via zombie by | 
 | 1611 | 		 * falling through.  Clearing of notask_error is complex. | 
 | 1612 | 		 * | 
 | 1613 | 		 * When !@ptrace: | 
 | 1614 | 		 * | 
 | 1615 | 		 * If WEXITED is set, notask_error should naturally be | 
 | 1616 | 		 * cleared.  If not, subset of WSTOPPED|WCONTINUED is set, | 
 | 1617 | 		 * so, if there are live subthreads, there are events to | 
 | 1618 | 		 * wait for.  If all subthreads are dead, it's still safe | 
 | 1619 | 		 * to clear - this function will be called again in finite | 
 | 1620 | 		 * amount time once all the subthreads are released and | 
 | 1621 | 		 * will then return without clearing. | 
 | 1622 | 		 * | 
 | 1623 | 		 * When @ptrace: | 
 | 1624 | 		 * | 
 | 1625 | 		 * Stopped state is per-task and thus can't change once the | 
 | 1626 | 		 * target task dies.  Only continued and exited can happen. | 
 | 1627 | 		 * Clear notask_error if WCONTINUED | WEXITED. | 
 | 1628 | 		 */ | 
 | 1629 | 		if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED))) | 
 | 1630 | 			wo->notask_error = 0; | 
 | 1631 | 	} else { | 
 | 1632 | 		/* | 
 | 1633 | 		 * If @p is ptraced by a task in its real parent's group, | 
 | 1634 | 		 * hide group stop/continued state when looking at @p as | 
 | 1635 | 		 * the real parent; otherwise, a single stop can be | 
 | 1636 | 		 * reported twice as group and ptrace stops. | 
 | 1637 | 		 * | 
 | 1638 | 		 * If a ptracer wants to distinguish the two events for its | 
 | 1639 | 		 * own children, it should create a separate process which | 
 | 1640 | 		 * takes the role of real parent. | 
 | 1641 | 		 */ | 
 | 1642 | 		if (likely(!ptrace) && p->ptrace && !ptrace_reparented(p)) | 
 | 1643 | 			return 0; | 
 | 1644 |  | 
 | 1645 | 		/* | 
 | 1646 | 		 * @p is alive and it's gonna stop, continue or exit, so | 
 | 1647 | 		 * there always is something to wait for. | 
 | 1648 | 		 */ | 
 | 1649 | 		wo->notask_error = 0; | 
 | 1650 | 	} | 
 | 1651 |  | 
 | 1652 | 	/* | 
 | 1653 | 	 * Wait for stopped.  Depending on @ptrace, different stopped state | 
 | 1654 | 	 * is used and the two don't interact with each other. | 
 | 1655 | 	 */ | 
 | 1656 | 	ret = wait_task_stopped(wo, ptrace, p); | 
 | 1657 | 	if (ret) | 
 | 1658 | 		return ret; | 
 | 1659 |  | 
 | 1660 | 	/* | 
 | 1661 | 	 * Wait for continued.  There's only one continued state and the | 
 | 1662 | 	 * ptracer can consume it which can confuse the real parent.  Don't | 
 | 1663 | 	 * use WCONTINUED from ptracer.  You don't need or want it. | 
 | 1664 | 	 */ | 
 | 1665 | 	return wait_task_continued(wo, p); | 
 | 1666 | } | 
 | 1667 |  | 
 | 1668 | /* | 
 | 1669 |  * Do the work of do_wait() for one thread in the group, @tsk. | 
 | 1670 |  * | 
 | 1671 |  * -ECHILD should be in ->notask_error before the first call. | 
 | 1672 |  * Returns nonzero for a final return, when we have unlocked tasklist_lock. | 
 | 1673 |  * Returns zero if the search for a child should continue; then | 
 | 1674 |  * ->notask_error is 0 if there were any eligible children, | 
 | 1675 |  * or another error from security_task_wait(), or still -ECHILD. | 
 | 1676 |  */ | 
 | 1677 | static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk) | 
 | 1678 | { | 
 | 1679 | 	struct task_struct *p; | 
 | 1680 |  | 
 | 1681 | 	list_for_each_entry(p, &tsk->children, sibling) { | 
 | 1682 | 		int ret = wait_consider_task(wo, 0, p); | 
 | 1683 | 		if (ret) | 
 | 1684 | 			return ret; | 
 | 1685 | 	} | 
 | 1686 |  | 
 | 1687 | 	return 0; | 
 | 1688 | } | 
 | 1689 |  | 
 | 1690 | static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk) | 
 | 1691 | { | 
 | 1692 | 	struct task_struct *p; | 
 | 1693 |  | 
 | 1694 | 	list_for_each_entry(p, &tsk->ptraced, ptrace_entry) { | 
 | 1695 | 		int ret = wait_consider_task(wo, 1, p); | 
 | 1696 | 		if (ret) | 
 | 1697 | 			return ret; | 
 | 1698 | 	} | 
 | 1699 |  | 
 | 1700 | 	return 0; | 
 | 1701 | } | 
 | 1702 |  | 
 | 1703 | static int child_wait_callback(wait_queue_t *wait, unsigned mode, | 
 | 1704 | 				int sync, void *key) | 
 | 1705 | { | 
 | 1706 | 	struct wait_opts *wo = container_of(wait, struct wait_opts, | 
 | 1707 | 						child_wait); | 
 | 1708 | 	struct task_struct *p = key; | 
 | 1709 |  | 
 | 1710 | 	if (!eligible_pid(wo, p)) | 
 | 1711 | 		return 0; | 
 | 1712 |  | 
 | 1713 | 	if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent) | 
 | 1714 | 		return 0; | 
 | 1715 |  | 
 | 1716 | 	return default_wake_function(wait, mode, sync, key); | 
 | 1717 | } | 
 | 1718 |  | 
 | 1719 | void __wake_up_parent(struct task_struct *p, struct task_struct *parent) | 
 | 1720 | { | 
 | 1721 | 	__wake_up_sync_key(&parent->signal->wait_chldexit, | 
 | 1722 | 				TASK_INTERRUPTIBLE, 1, p); | 
 | 1723 | } | 
 | 1724 |  | 
 | 1725 | static long do_wait(struct wait_opts *wo) | 
 | 1726 | { | 
 | 1727 | 	struct task_struct *tsk; | 
 | 1728 | 	int retval; | 
 | 1729 |  | 
 | 1730 | 	trace_sched_process_wait(wo->wo_pid); | 
 | 1731 |  | 
 | 1732 | 	init_waitqueue_func_entry(&wo->child_wait, child_wait_callback); | 
 | 1733 | 	wo->child_wait.private = current; | 
 | 1734 | 	add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); | 
 | 1735 | repeat: | 
 | 1736 | 	/* | 
 | 1737 | 	 * If there is nothing that can match our critiera just get out. | 
 | 1738 | 	 * We will clear ->notask_error to zero if we see any child that | 
 | 1739 | 	 * might later match our criteria, even if we are not able to reap | 
 | 1740 | 	 * it yet. | 
 | 1741 | 	 */ | 
 | 1742 | 	wo->notask_error = -ECHILD; | 
 | 1743 | 	if ((wo->wo_type < PIDTYPE_MAX) && | 
 | 1744 | 	   (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type]))) | 
 | 1745 | 		goto notask; | 
 | 1746 |  | 
 | 1747 | 	set_current_state(TASK_INTERRUPTIBLE); | 
 | 1748 | 	read_lock(&tasklist_lock); | 
 | 1749 | 	tsk = current; | 
 | 1750 | 	do { | 
 | 1751 | 		retval = do_wait_thread(wo, tsk); | 
 | 1752 | 		if (retval) | 
 | 1753 | 			goto end; | 
 | 1754 |  | 
 | 1755 | 		retval = ptrace_do_wait(wo, tsk); | 
 | 1756 | 		if (retval) | 
 | 1757 | 			goto end; | 
 | 1758 |  | 
 | 1759 | 		if (wo->wo_flags & __WNOTHREAD) | 
 | 1760 | 			break; | 
 | 1761 | 	} while_each_thread(current, tsk); | 
 | 1762 | 	read_unlock(&tasklist_lock); | 
 | 1763 |  | 
 | 1764 | notask: | 
 | 1765 | 	retval = wo->notask_error; | 
 | 1766 | 	if (!retval && !(wo->wo_flags & WNOHANG)) { | 
 | 1767 | 		retval = -ERESTARTSYS; | 
 | 1768 | 		if (!signal_pending(current)) { | 
 | 1769 | 			schedule(); | 
 | 1770 | 			goto repeat; | 
 | 1771 | 		} | 
 | 1772 | 	} | 
 | 1773 | end: | 
 | 1774 | 	__set_current_state(TASK_RUNNING); | 
 | 1775 | 	remove_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); | 
 | 1776 | 	return retval; | 
 | 1777 | } | 
 | 1778 |  | 
 | 1779 | SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *, | 
 | 1780 | 		infop, int, options, struct rusage __user *, ru) | 
 | 1781 | { | 
 | 1782 | 	struct wait_opts wo; | 
 | 1783 | 	struct pid *pid = NULL; | 
 | 1784 | 	enum pid_type type; | 
 | 1785 | 	long ret; | 
 | 1786 |  | 
 | 1787 | 	if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED)) | 
 | 1788 | 		return -EINVAL; | 
 | 1789 | 	if (!(options & (WEXITED|WSTOPPED|WCONTINUED))) | 
 | 1790 | 		return -EINVAL; | 
 | 1791 |  | 
 | 1792 | 	switch (which) { | 
 | 1793 | 	case P_ALL: | 
 | 1794 | 		type = PIDTYPE_MAX; | 
 | 1795 | 		break; | 
 | 1796 | 	case P_PID: | 
 | 1797 | 		type = PIDTYPE_PID; | 
 | 1798 | 		if (upid <= 0) | 
 | 1799 | 			return -EINVAL; | 
 | 1800 | 		break; | 
 | 1801 | 	case P_PGID: | 
 | 1802 | 		type = PIDTYPE_PGID; | 
 | 1803 | 		if (upid <= 0) | 
 | 1804 | 			return -EINVAL; | 
 | 1805 | 		break; | 
 | 1806 | 	default: | 
 | 1807 | 		return -EINVAL; | 
 | 1808 | 	} | 
 | 1809 |  | 
 | 1810 | 	if (type < PIDTYPE_MAX) | 
 | 1811 | 		pid = find_get_pid(upid); | 
 | 1812 |  | 
 | 1813 | 	wo.wo_type	= type; | 
 | 1814 | 	wo.wo_pid	= pid; | 
 | 1815 | 	wo.wo_flags	= options; | 
 | 1816 | 	wo.wo_info	= infop; | 
 | 1817 | 	wo.wo_stat	= NULL; | 
 | 1818 | 	wo.wo_rusage	= ru; | 
 | 1819 | 	ret = do_wait(&wo); | 
 | 1820 |  | 
 | 1821 | 	if (ret > 0) { | 
 | 1822 | 		ret = 0; | 
 | 1823 | 	} else if (infop) { | 
 | 1824 | 		/* | 
 | 1825 | 		 * For a WNOHANG return, clear out all the fields | 
 | 1826 | 		 * we would set so the user can easily tell the | 
 | 1827 | 		 * difference. | 
 | 1828 | 		 */ | 
 | 1829 | 		if (!ret) | 
 | 1830 | 			ret = put_user(0, &infop->si_signo); | 
 | 1831 | 		if (!ret) | 
 | 1832 | 			ret = put_user(0, &infop->si_errno); | 
 | 1833 | 		if (!ret) | 
 | 1834 | 			ret = put_user(0, &infop->si_code); | 
 | 1835 | 		if (!ret) | 
 | 1836 | 			ret = put_user(0, &infop->si_pid); | 
 | 1837 | 		if (!ret) | 
 | 1838 | 			ret = put_user(0, &infop->si_uid); | 
 | 1839 | 		if (!ret) | 
 | 1840 | 			ret = put_user(0, &infop->si_status); | 
 | 1841 | 	} | 
 | 1842 |  | 
 | 1843 | 	put_pid(pid); | 
 | 1844 |  | 
 | 1845 | 	/* avoid REGPARM breakage on x86: */ | 
 | 1846 | 	asmlinkage_protect(5, ret, which, upid, infop, options, ru); | 
 | 1847 | 	return ret; | 
 | 1848 | } | 
 | 1849 |  | 
 | 1850 | SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr, | 
 | 1851 | 		int, options, struct rusage __user *, ru) | 
 | 1852 | { | 
 | 1853 | 	struct wait_opts wo; | 
 | 1854 | 	struct pid *pid = NULL; | 
 | 1855 | 	enum pid_type type; | 
 | 1856 | 	long ret; | 
 | 1857 |  | 
 | 1858 | 	if (options & ~(WNOHANG|WUNTRACED|WCONTINUED| | 
 | 1859 | 			__WNOTHREAD|__WCLONE|__WALL)) | 
 | 1860 | 		return -EINVAL; | 
 | 1861 |  | 
 | 1862 | 	if (upid == -1) | 
 | 1863 | 		type = PIDTYPE_MAX; | 
 | 1864 | 	else if (upid < 0) { | 
 | 1865 | 		type = PIDTYPE_PGID; | 
 | 1866 | 		pid = find_get_pid(-upid); | 
 | 1867 | 	} else if (upid == 0) { | 
 | 1868 | 		type = PIDTYPE_PGID; | 
 | 1869 | 		pid = get_task_pid(current, PIDTYPE_PGID); | 
 | 1870 | 	} else /* upid > 0 */ { | 
 | 1871 | 		type = PIDTYPE_PID; | 
 | 1872 | 		pid = find_get_pid(upid); | 
 | 1873 | 	} | 
 | 1874 |  | 
 | 1875 | 	wo.wo_type	= type; | 
 | 1876 | 	wo.wo_pid	= pid; | 
 | 1877 | 	wo.wo_flags	= options | WEXITED; | 
 | 1878 | 	wo.wo_info	= NULL; | 
 | 1879 | 	wo.wo_stat	= stat_addr; | 
 | 1880 | 	wo.wo_rusage	= ru; | 
 | 1881 | 	ret = do_wait(&wo); | 
 | 1882 | 	put_pid(pid); | 
 | 1883 |  | 
 | 1884 | 	/* avoid REGPARM breakage on x86: */ | 
 | 1885 | 	asmlinkage_protect(4, ret, upid, stat_addr, options, ru); | 
 | 1886 | 	return ret; | 
 | 1887 | } | 
 | 1888 |  | 
 | 1889 | #ifdef __ARCH_WANT_SYS_WAITPID | 
 | 1890 |  | 
 | 1891 | /* | 
 | 1892 |  * sys_waitpid() remains for compatibility. waitpid() should be | 
 | 1893 |  * implemented by calling sys_wait4() from libc.a. | 
 | 1894 |  */ | 
 | 1895 | SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options) | 
 | 1896 | { | 
 | 1897 | 	return sys_wait4(pid, stat_addr, options, NULL); | 
 | 1898 | } | 
 | 1899 |  | 
 | 1900 | #endif |