2038 lines · 50.6 KB · C
RawDownload
 	exit_sem(tsk);	exit_shm(tsk);	exit_files(tsk);	exit_fs(tsk);	if (group_dead)		disassociate_ctty(1);	exit_nsproxy_namespaces(tsk);	exit_task_work(tsk);	exit_thread(tsk); 	sched_autogroup_exit_task(tsk);	cgroup_task_exit(tsk); 	/*	 * FIXME: do that only when needed, using sched_exit tracepoint	 */	flush_ptrace_hw_breakpoint(tsk); 	exit_tasks_rcu_start();	exit_notify(tsk, group_dead);	proc_exit_connector(tsk);	mpol_put_task_policy(tsk);#ifdef CONFIG_FUTEX	if (unlikely(current->futex.pi_state_cache))		kfree(current->futex.pi_state_cache);#endif	/*	 * Make sure we are holding no locks:	 */	debug_check_no_locks_held(); 	if (tsk->io_context)		exit_io_context(tsk); 	if (tsk->splice_pipe)		free_pipe_info(tsk->splice_pipe); 	if (tsk->task_frag.page)		put_page(tsk->task_frag.page); 	exit_task_stack_account(tsk); 	check_stack_usage();	preempt_disable();	if (tsk->nr_dirtied)		__this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);	exit_rcu();	exit_tasks_rcu_finish(); 	lockdep_free_task(tsk);	do_task_dead();}EXPORT_SYMBOL(do_exit); void __noreturn make_task_dead(int signr){	/*	 * Take the task off the cpu after something catastrophic has	 * happened.	 *	 * We can get here from a kernel oops, sometimes with preemption off.	 * Start by checking for critical errors.	 * Then fix up important state like USER_DS and preemption.	 * Then do everything else.	 */	struct task_struct *tsk = current;	unsigned int limit; 	if (unlikely(in_interrupt()))		panic("Aiee, killing interrupt handler!");	if (unlikely(!tsk->pid))		panic("Attempted to kill the idle task!"); 	if (unlikely(irqs_disabled())) {		pr_info("note: %s[%d] exited with irqs disabled\n",			current->comm, task_pid_nr(current));		local_irq_enable();	}	if (unlikely(in_atomic())) {		pr_info("note: %s[%d] exited with preempt_count %d\n",			current->comm, task_pid_nr(current),			preempt_count());		preempt_count_set(PREEMPT_ENABLED);	} 	/*	 * Every time the system oopses, if the oops happens while a reference	 * to an object was held, the reference leaks.	 * If the oops doesn't also leak memory, repeated oopsing can cause	 * reference counters to wrap around (if they're not using refcount_t).	 * This means that repeated oopsing can make unexploitable-looking bugs	 * exploitable through repeated oopsing.	 * To make sure this can't happen, place an upper bound on how often the	 * kernel may oops without panic().	 */	limit = READ_ONCE(oops_limit);	if (atomic_inc_return(&oops_count) >= limit && limit)		panic("Oopsed too often (kernel.oops_limit is %d)", limit); 	/*	 * We're taking recursive faults here in make_task_dead. Safest is to just	 * leave this task alone and wait for reboot.	 */	if (unlikely(tsk->flags & PF_EXITING)) {		pr_alert("Fixing recursive fault but reboot is needed!\n");		futex_exit_recursive(tsk);		tsk->exit_state = EXIT_DEAD;		refcount_inc(&tsk->rcu_users);		preempt_disable();		do_task_dead();	} 	do_exit(signr);} SYSCALL_DEFINE1(exit, int, error_code){	do_exit((error_code&0xff)<<8);} /* * Take down every thread in the group.  This is called by fatal signals * as well as by sys_exit_group (below). */void __noreturndo_group_exit(int exit_code){	struct signal_struct *sig = current->signal; 	if (sig->flags & SIGNAL_GROUP_EXIT)		exit_code = sig->group_exit_code;	else if (sig->group_exec_task)		exit_code = 0;	else {		struct sighand_struct *const sighand = current->sighand; 		spin_lock_irq(&sighand->siglock);		if (sig->flags & SIGNAL_GROUP_EXIT)			/* Another thread got here before we took the lock.  */			exit_code = sig->group_exit_code;		else if (sig->group_exec_task)			exit_code = 0;		else {			sig->group_exit_code = exit_code;			sig->flags = SIGNAL_GROUP_EXIT;			zap_other_threads(current);		}		spin_unlock_irq(&sighand->siglock);	} 	do_exit(exit_code);	/* NOTREACHED */} /* * this kills every thread in the thread group. Note that any externally * wait4()-ing process will get the correct exit code - even if this * thread is not the thread group leader. */SYSCALL_DEFINE1(exit_group, int, error_code){	do_group_exit((error_code & 0xff) << 8);	/* NOTREACHED */	return 0;} static int eligible_pid(struct wait_opts *wo, struct task_struct *p){	return	wo->wo_type == PIDTYPE_MAX ||		task_pid_type(p, wo->wo_type) == wo->wo_pid;} static inteligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p){	if (!eligible_pid(wo, p))		return 0; 	/*	 * Wait for all children (clone and not) if __WALL is set or	 * if it is traced by us.	 */	if (ptrace || (wo->wo_flags & __WALL))		return 1; 	/*	 * Otherwise, wait for clone children *only* if __WCLONE is set;	 * otherwise, wait for non-clone children *only*.	 *	 * Note: a "clone" child here is one that reports to its parent	 * using a signal other than SIGCHLD, or a non-leader thread which	 * we can only see if it is traced by us.	 */	if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))		return 0; 	return 1;} /* * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold * the lock and this task is uninteresting.  If we return nonzero, we have * released the lock and the system call should return. */static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p){	int state, status;	pid_t pid = task_pid_vnr(p);	uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));	struct waitid_info *infop; 	if (!likely(wo->wo_flags & WEXITED))		return 0; 	if (unlikely(wo->wo_flags & WNOWAIT)) {		status = (p->signal->flags & SIGNAL_GROUP_EXIT)			? p->signal->group_exit_code : p->exit_code;		get_task_struct(p);		read_unlock(&tasklist_lock);		sched_annotate_sleep();		if (wo->wo_rusage)			getrusage(p, RUSAGE_BOTH, wo->wo_rusage);		put_task_struct(p);		goto out_info;	}	/*	 * Move the task's state to DEAD/TRACE, only one thread can do this.	 */	state = (ptrace_reparented(p) && thread_group_leader(p)) ?		EXIT_TRACE : EXIT_DEAD;	if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)		return 0;	/*	 * We own this thread, nobody else can reap it.	 */	read_unlock(&tasklist_lock);	sched_annotate_sleep(); 	/*	 * Check thread_group_leader() to exclude the traced sub-threads.	 */	if (state == EXIT_DEAD && thread_group_leader(p)) {		struct signal_struct *sig = p->signal;		struct signal_struct *psig = current->signal;		unsigned long maxrss;		u64 tgutime, tgstime; 		/*		 * The resource counters for the group leader are in its		 * own task_struct.  Those for dead threads in the group		 * are in its signal_struct, as are those for the child		 * processes it has previously reaped.  All these		 * accumulate in the parent's signal_struct c* fields.		 *		 * We don't bother to take a lock here to protect these		 * p->signal fields because the whole thread group is dead		 * and nobody can change them.		 *		 * psig->stats_lock also protects us from our sub-threads		 * which can reap other children at the same time.		 *		 * We use thread_group_cputime_adjusted() to get times for		 * the thread group, which consolidates times for all threads		 * in the group including the group leader.		 */		thread_group_cputime_adjusted(p, &tgutime, &tgstime);		write_seqlock_irq(&psig->stats_lock);		psig->cutime += tgutime + sig->cutime;		psig->cstime += tgstime + sig->cstime;		psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;		psig->cmin_flt +=			p->min_flt + sig->min_flt + sig->cmin_flt;		psig->cmaj_flt +=			p->maj_flt + sig->maj_flt + sig->cmaj_flt;		psig->cnvcsw +=			p->nvcsw + sig->nvcsw + sig->cnvcsw;		psig->cnivcsw +=			p->nivcsw + sig->nivcsw + sig->cnivcsw;		psig->cinblock +=			task_io_get_inblock(p) +			sig->inblock + sig->cinblock;		psig->coublock +=			task_io_get_oublock(p) +			sig->oublock + sig->coublock;		maxrss = max(sig->maxrss, sig->cmaxrss);		if (psig->cmaxrss < maxrss)			psig->cmaxrss = maxrss;		task_io_accounting_add(&psig->ioac, &p->ioac);		task_io_accounting_add(&psig->ioac, &sig->ioac);		write_sequnlock_irq(&psig->stats_lock);	} 	if (wo->wo_rusage)		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);	status = (p->signal->flags & SIGNAL_GROUP_EXIT)		? p->signal->group_exit_code : p->exit_code;	wo->wo_stat = status; 	if (state == EXIT_TRACE) {		write_lock_irq(&tasklist_lock);		/* We dropped tasklist, ptracer could die and untrace */		ptrace_unlink(p); 		/* If parent wants a zombie, don't release it now */		state = EXIT_ZOMBIE;		if (do_notify_parent(p, p->exit_signal))			state = EXIT_DEAD;		p->exit_state = state;		write_unlock_irq(&tasklist_lock);	}	if (state == EXIT_DEAD)		release_task(p); out_info:	infop = wo->wo_info;	if (infop) {		if ((status & 0x7f) == 0) {			infop->cause = CLD_EXITED;			infop->status = status >> 8;		} else {			infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;			infop->status = status & 0x7f;		}		infop->pid = pid;		infop->uid = uid;	} 	return pid;} static int *task_stopped_code(struct task_struct *p, bool ptrace){	if (ptrace) {		if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))			return &p->exit_code;	} else {		if (p->signal->flags & SIGNAL_STOP_STOPPED)			return &p->signal->group_exit_code;	}	return NULL;} /** * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED * @wo: wait options * @ptrace: is the wait for ptrace * @p: task to wait for * * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED. * * CONTEXT: * read_lock(&tasklist_lock), which is released if return value is * non-zero.  Also, grabs and releases @p->sighand->siglock. * * RETURNS: * 0 if wait condition didn't exist and search for other wait conditions * should continue.  Non-zero return, -errno on failure and @p's pid on * success, implies that tasklist_lock is released and wait condition * search should terminate. */static int wait_task_stopped(struct wait_opts *wo,				int ptrace, struct task_struct *p){	struct waitid_info *infop;	int exit_code, *p_code, why;	uid_t uid = 0; /* unneeded, required by compiler */	pid_t pid; 	/*	 * Traditionally we see ptrace'd stopped tasks regardless of options.	 */	if (!ptrace && !(wo->wo_flags & WUNTRACED))		return 0; 	if (!task_stopped_code(p, ptrace))		return 0; 	exit_code = 0;	spin_lock_irq(&p->sighand->siglock); 	p_code = task_stopped_code(p, ptrace);	if (unlikely(!p_code))		goto unlock_sig; 	exit_code = *p_code;	if (!exit_code)		goto unlock_sig; 	if (!unlikely(wo->wo_flags & WNOWAIT))		*p_code = 0; 	uid = from_kuid_munged(current_user_ns(), task_uid(p));unlock_sig:	spin_unlock_irq(&p->sighand->siglock);	if (!exit_code)		return 0; 	/*	 * Now we are pretty sure this task is interesting.	 * Make sure it doesn't get reaped out from under us while we	 * give up the lock and then examine it below.  We don't want to	 * keep holding onto the tasklist_lock while we call getrusage and	 * possibly take page faults for user memory.	 */	get_task_struct(p);	pid = task_pid_vnr(p);	why = ptrace ? CLD_TRAPPED : CLD_STOPPED;	read_unlock(&tasklist_lock);	sched_annotate_sleep();	if (wo->wo_rusage)		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);	put_task_struct(p); 	if (likely(!(wo->wo_flags & WNOWAIT)))		wo->wo_stat = (exit_code << 8) | 0x7f; 	infop = wo->wo_info;	if (infop) {		infop->cause = why;		infop->status = exit_code;		infop->pid = pid;		infop->uid = uid;	}	return pid;} /* * Handle do_wait work for one task in a live, non-stopped state. * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold * the lock and this task is uninteresting.  If we return nonzero, we have * released the lock and the system call should return. */static int wait_task_continued(struct wait_opts *wo, struct task_struct *p){	struct waitid_info *infop;	pid_t pid;	uid_t uid; 	if (!unlikely(wo->wo_flags & WCONTINUED))		return 0; 	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))		return 0; 	spin_lock_irq(&p->sighand->siglock);	/* Re-check with the lock held.  */	if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {		spin_unlock_irq(&p->sighand->siglock);		return 0;	}	if (!unlikely(wo->wo_flags & WNOWAIT))		p->signal->flags &= ~SIGNAL_STOP_CONTINUED;	uid = from_kuid_munged(current_user_ns(), task_uid(p));	spin_unlock_irq(&p->sighand->siglock); 	pid = task_pid_vnr(p);	get_task_struct(p);	read_unlock(&tasklist_lock);	sched_annotate_sleep();	if (wo->wo_rusage)		getrusage(p, RUSAGE_BOTH, wo->wo_rusage);	put_task_struct(p); 	infop = wo->wo_info;	if (!infop) {		wo->wo_stat = 0xffff;	} else {		infop->cause = CLD_CONTINUED;		infop->pid = pid;		infop->uid = uid;		infop->status = SIGCONT;	}	return pid;} /* * Consider @p for a wait by @parent. * * -ECHILD should be in ->notask_error before the first call. * Returns nonzero for a final return, when we have unlocked tasklist_lock. * Returns zero if the search for a child should continue; * then ->notask_error is 0 if @p is an eligible child, * or still -ECHILD. */static int wait_consider_task(struct wait_opts *wo, int ptrace,				struct task_struct *p){	/*	 * We can race with wait_task_zombie() from another thread.	 * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition	 * can't confuse the checks below.	 */	int exit_state = READ_ONCE(p->exit_state);	int ret; 	if (unlikely(exit_state == EXIT_DEAD))		return 0; 

Lines 1001–1500 of 2038. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t