1761 lines · 43.1 KB · C
RawDownload
	__releases(&tasklist_lock)	__acquires(&tasklist_lock){	struct pid_namespace *pid_ns = task_active_pid_ns(father);	struct task_struct *reaper = pid_ns->child_reaper;	struct task_struct *p, *n; 	if (likely(reaper != father))		return reaper; 	reaper = find_alive_thread(father);	if (reaper) {		pid_ns->child_reaper = reaper;		return reaper;	} 	write_unlock_irq(&tasklist_lock); 	list_for_each_entry_safe(p, n, dead, ptrace_entry) {		list_del_init(&p->ptrace_entry);		release_task(p);	} 	zap_pid_ns_processes(pid_ns);	write_lock_irq(&tasklist_lock); 	return father;} /* * When we die, we re-parent all our children, and try to: * 1. give them to another thread in our thread group, if such a member exists * 2. give it to the first ancestor process which prctl'd itself as a *    child_subreaper for its children (like a service manager) * 3. give it to the init process (PID 1) in our pid namespace */static struct task_struct *find_new_reaper(struct task_struct *father,					   struct task_struct *child_reaper){	struct task_struct *thread, *reaper; 	thread = find_alive_thread(father);	if (thread)		return thread; 	if (father->signal->has_child_subreaper) {		unsigned int ns_level = task_pid(father)->level;		/*		 * Find the first ->is_child_subreaper ancestor in our pid_ns.		 * We can't check reaper != child_reaper to ensure we do not		 * cross the namespaces, the exiting parent could be injected		 * by setns() + fork().		 * We check pid->level, this is slightly more efficient than		 * task_active_pid_ns(reaper) != task_active_pid_ns(father).		 */		for (reaper = father->real_parent;		     task_pid(reaper)->level == ns_level;		     reaper = reaper->real_parent) {			if (reaper == &init_task)				break;			if (!reaper->signal->is_child_subreaper)				continue;			thread = find_alive_thread(reaper);			if (thread)				return thread;		}	} 	return child_reaper;} /** Any that need to be release_task'd are put on the @dead list. */static void reparent_leader(struct task_struct *father, struct task_struct *p,				struct list_head *dead){	if (unlikely(p->exit_state == EXIT_DEAD))		return; 	/* We don't want people slaying init. */	p->exit_signal = SIGCHLD; 	/* If it has exited notify the new parent about this child's death. */	if (!p->ptrace &&	    p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {		if (do_notify_parent(p, p->exit_signal)) {			p->exit_state = EXIT_DEAD;			list_add(&p->ptrace_entry, dead);		}	} 	kill_orphaned_pgrp(p, father);} /* * This does two things: * * A.  Make init inherit all the child processes * B.  Check to see if any process groups have become orphaned *	as a result of our exiting, and if they have any stopped *	jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2) */static void forget_original_parent(struct task_struct *father,					struct list_head *dead){	struct task_struct *p, *t, *reaper; 	if (unlikely(!list_empty(&father->ptraced)))		exit_ptrace(father, dead); 	/* Can drop and reacquire tasklist_lock */	reaper = find_child_reaper(father, dead);	if (list_empty(&father->children))		return; 	reaper = find_new_reaper(father, reaper);	list_for_each_entry(p, &father->children, sibling) {		for_each_thread(p, t) {			RCU_INIT_POINTER(t->real_parent, reaper);			BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));			if (likely(!t->ptrace))				t->parent = t->real_parent;			if (t->pdeath_signal)				group_send_sig_info(t->pdeath_signal,						    SEND_SIG_NOINFO, t,						    PIDTYPE_TGID);		}		/*		 * If this is a threaded reparent there is no need to		 * notify anyone anything has happened.		 */		if (!same_thread_group(reaper, father))			reparent_leader(father, p, dead);	}	list_splice_tail_init(&father->children, &reaper->children);} /* * Send signals to all our closest relatives so that they know * to properly mourn us.. */static void exit_notify(struct task_struct *tsk, int group_dead){	bool autoreap;	struct task_struct *p, *n;	LIST_HEAD(dead); 	write_lock_irq(&tasklist_lock);	forget_original_parent(tsk, &dead); 	if (group_dead)		kill_orphaned_pgrp(tsk->group_leader, NULL); 	tsk->exit_state = EXIT_ZOMBIE;	if (unlikely(tsk->ptrace)) {		int sig = thread_group_leader(tsk) &&				thread_group_empty(tsk) &&				!ptrace_reparented(tsk) ?			tsk->exit_signal : SIGCHLD;		autoreap = do_notify_parent(tsk, sig);	} else if (thread_group_leader(tsk)) {		autoreap = thread_group_empty(tsk) &&			do_notify_parent(tsk, tsk->exit_signal);	} else {		autoreap = true;	} 	if (autoreap) {		tsk->exit_state = EXIT_DEAD;		list_add(&tsk->ptrace_entry, &dead);	} 	/* mt-exec, de_thread() is waiting for group leader */	if (unlikely(tsk->signal->notify_count < 0))		wake_up_process(tsk->signal->group_exit_task);	write_unlock_irq(&tasklist_lock); 	list_for_each_entry_safe(p, n, &dead, ptrace_entry) {		list_del_init(&p->ptrace_entry);		release_task(p);	}} #ifdef CONFIG_DEBUG_STACK_USAGEstatic void check_stack_usage(void){	static DEFINE_SPINLOCK(low_water_lock);	static int lowest_to_date = THREAD_SIZE;	unsigned long free; 	free = stack_not_used(current); 	if (free >= lowest_to_date)		return; 	spin_lock(&low_water_lock);	if (free < lowest_to_date) {		pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",			current->comm, task_pid_nr(current), free);		lowest_to_date = free;	}	spin_unlock(&low_water_lock);}#elsestatic inline void check_stack_usage(void) {}#endif void __noreturn do_exit(long code){	struct task_struct *tsk = current;	int group_dead; 	/*	 * 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.	 */ 	WARN_ON(blk_needs_flush_plug(tsk)); 	if (unlikely(in_interrupt()))		panic("Aiee, killing interrupt handler!");	if (unlikely(!tsk->pid))		panic("Attempted to kill the idle task!"); 	/*	 * If do_exit is called because this processes oopsed, it's possible	 * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before	 * continuing. Amongst other possible reasons, this is to prevent	 * mm_release()->clear_child_tid() from writing to a user-controlled	 * kernel address.	 */	force_uaccess_begin(); 	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);	} 	profile_task_exit(tsk);	kcov_task_exit(tsk); 	ptrace_event(PTRACE_EVENT_EXIT, code); 	validate_creds_for_do_exit(tsk); 	/*	 * We're taking recursive faults here in do_exit. 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);		set_current_state(TASK_UNINTERRUPTIBLE);		schedule();	} 	exit_signals(tsk);  /* sets PF_EXITING */ 	/* sync mm's RSS info before statistics gathering */	if (tsk->mm)		sync_mm_rss(tsk->mm);	acct_update_integrals(tsk);	group_dead = atomic_dec_and_test(&tsk->signal->live);	if (group_dead) {		/*		 * If the last thread of global init has exited, panic		 * immediately to get a useable coredump.		 */		if (unlikely(is_global_init(tsk)))			panic("Attempted to kill init! exitcode=0x%08x\n",				tsk->signal->group_exit_code ?: (int)code); #ifdef CONFIG_POSIX_TIMERS		hrtimer_cancel(&tsk->signal->real_timer);		exit_itimers(tsk->signal);#endif		if (tsk->mm)			setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);	}	acct_collect(code, group_dead);	if (group_dead)		tty_audit_exit();	audit_free(tsk); 	tsk->exit_code = code;	taskstats_exit(tsk, group_dead); 	exit_mm(); 	if (group_dead)		acct_process();	trace_sched_process_exit(tsk); 	exit_sem(tsk);	exit_shm(tsk);	exit_files(tsk);	exit_fs(tsk);	if (group_dead)		disassociate_ctty(1);	exit_task_namespaces(tsk);	exit_task_work(tsk);	exit_thread(tsk); 	/*	 * Flush inherited counters to the parent - before the parent	 * gets woken up by child-exit notifications.	 *	 * because of cgroup mode, must be called before cgroup_exit()	 */	perf_event_exit_task(tsk); 	sched_autogroup_exit_task(tsk);	cgroup_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->pi_state_cache))		kfree(current->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); 	validate_creds_for_do_exit(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_GPL(do_exit); void complete_and_exit(struct completion *comp, long code){	if (comp)		complete(comp); 	do_exit(code);}EXPORT_SYMBOL(complete_and_exit); 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). */voiddo_group_exit(int exit_code){	struct signal_struct *sig = current->signal; 	BUG_ON(exit_code & 0x80); /* core dumps don't get here */ 	if (signal_group_exit(sig))		exit_code = sig->group_exit_code;	else if (!thread_group_empty(current)) {		struct sighand_struct *const sighand = current->sighand; 		spin_lock_irq(&sighand->siglock);		if (signal_group_exit(sig))			/* Another thread got here before we took the lock.  */			exit_code = sig->group_exit_code;		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;} struct waitid_info {	pid_t pid;	uid_t uid;	int status;	int cause;}; struct wait_opts {	enum pid_type		wo_type;	int			wo_flags;	struct pid		*wo_pid; 	struct waitid_info	*wo_info;	int			wo_stat;	struct rusage		*wo_rusage; 	wait_queue_entry_t		child_wait;	int			notask_error;}; 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->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.	 */

Lines 501–1000 of 1761. 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