Home | History | Annotate | Line # | Download | only in kern
kern_fork.c revision 1.213
      1 /*	$NetBSD: kern_fork.c,v 1.213 2019/06/13 20:20:18 kamil Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2001, 2004, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center, by Charles M. Hannum, and by Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     35  *	The Regents of the University of California.  All rights reserved.
     36  * (c) UNIX System Laboratories, Inc.
     37  * All or some portions of this file are derived from material licensed
     38  * to the University of California by American Telephone and Telegraph
     39  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     40  * the permission of UNIX System Laboratories, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. Neither the name of the University nor the names of its contributors
     51  *    may be used to endorse or promote products derived from this software
     52  *    without specific prior written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     64  * SUCH DAMAGE.
     65  *
     66  *	@(#)kern_fork.c	8.8 (Berkeley) 2/14/95
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: kern_fork.c,v 1.213 2019/06/13 20:20:18 kamil Exp $");
     71 
     72 #include "opt_ktrace.h"
     73 #include "opt_dtrace.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/filedesc.h>
     78 #include <sys/kernel.h>
     79 #include <sys/pool.h>
     80 #include <sys/mount.h>
     81 #include <sys/proc.h>
     82 #include <sys/ras.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/vnode.h>
     85 #include <sys/file.h>
     86 #include <sys/acct.h>
     87 #include <sys/ktrace.h>
     88 #include <sys/sched.h>
     89 #include <sys/signalvar.h>
     90 #include <sys/syscall.h>
     91 #include <sys/kauth.h>
     92 #include <sys/atomic.h>
     93 #include <sys/syscallargs.h>
     94 #include <sys/uidinfo.h>
     95 #include <sys/sdt.h>
     96 #include <sys/ptrace.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 
    100 /*
    101  * DTrace SDT provider definitions
    102  */
    103 SDT_PROVIDER_DECLARE(proc);
    104 SDT_PROBE_DEFINE3(proc, kernel, , create,
    105     "struct proc *", /* new process */
    106     "struct proc *", /* parent process */
    107     "int" /* flags */);
    108 
    109 u_int	nprocs __cacheline_aligned = 1;		/* process 0 */
    110 
    111 /*
    112  * Number of ticks to sleep if fork() would fail due to process hitting
    113  * limits. Exported in miliseconds to userland via sysctl.
    114  */
    115 int	forkfsleep = 0;
    116 
    117 int
    118 sys_fork(struct lwp *l, const void *v, register_t *retval)
    119 {
    120 
    121 	return fork1(l, 0, SIGCHLD, NULL, 0, NULL, NULL, retval);
    122 }
    123 
    124 /*
    125  * vfork(2) system call compatible with 4.4BSD (i.e. BSD with Mach VM).
    126  * Address space is not shared, but parent is blocked until child exit.
    127  */
    128 int
    129 sys_vfork(struct lwp *l, const void *v, register_t *retval)
    130 {
    131 
    132 	return fork1(l, FORK_PPWAIT, SIGCHLD, NULL, 0, NULL, NULL,
    133 	    retval);
    134 }
    135 
    136 /*
    137  * New vfork(2) system call for NetBSD, which implements original 3BSD vfork(2)
    138  * semantics.  Address space is shared, and parent is blocked until child exit.
    139  */
    140 int
    141 sys___vfork14(struct lwp *l, const void *v, register_t *retval)
    142 {
    143 
    144 	return fork1(l, FORK_PPWAIT|FORK_SHAREVM, SIGCHLD, NULL, 0,
    145 	    NULL, NULL, retval);
    146 }
    147 
    148 /*
    149  * Linux-compatible __clone(2) system call.
    150  */
    151 int
    152 sys___clone(struct lwp *l, const struct sys___clone_args *uap,
    153     register_t *retval)
    154 {
    155 	/* {
    156 		syscallarg(int) flags;
    157 		syscallarg(void *) stack;
    158 	} */
    159 	int flags, sig;
    160 
    161 	/*
    162 	 * We don't support the CLONE_PID or CLONE_PTRACE flags.
    163 	 */
    164 	if (SCARG(uap, flags) & (CLONE_PID|CLONE_PTRACE))
    165 		return EINVAL;
    166 
    167 	/*
    168 	 * Linux enforces CLONE_VM with CLONE_SIGHAND, do same.
    169 	 */
    170 	if (SCARG(uap, flags) & CLONE_SIGHAND
    171 	    && (SCARG(uap, flags) & CLONE_VM) == 0)
    172 		return EINVAL;
    173 
    174 	flags = 0;
    175 
    176 	if (SCARG(uap, flags) & CLONE_VM)
    177 		flags |= FORK_SHAREVM;
    178 	if (SCARG(uap, flags) & CLONE_FS)
    179 		flags |= FORK_SHARECWD;
    180 	if (SCARG(uap, flags) & CLONE_FILES)
    181 		flags |= FORK_SHAREFILES;
    182 	if (SCARG(uap, flags) & CLONE_SIGHAND)
    183 		flags |= FORK_SHARESIGS;
    184 	if (SCARG(uap, flags) & CLONE_VFORK)
    185 		flags |= FORK_PPWAIT;
    186 
    187 	sig = SCARG(uap, flags) & CLONE_CSIGNAL;
    188 	if (sig < 0 || sig >= _NSIG)
    189 		return EINVAL;
    190 
    191 	/*
    192 	 * Note that the Linux API does not provide a portable way of
    193 	 * specifying the stack area; the caller must know if the stack
    194 	 * grows up or down.  So, we pass a stack size of 0, so that the
    195 	 * code that makes this adjustment is a noop.
    196 	 */
    197 	return fork1(l, flags, sig, SCARG(uap, stack), 0,
    198 	    NULL, NULL, retval);
    199 }
    200 
    201 /*
    202  * Print the 'table full' message once per 10 seconds.
    203  */
    204 static struct timeval fork_tfmrate = { 10, 0 };
    205 
    206 /*
    207  * Check if a process is traced and shall inform about FORK events.
    208  */
    209 static inline bool
    210 tracefork(struct proc *p, int flags)
    211 {
    212 
    213 	return (p->p_slflag & (PSL_TRACEFORK|PSL_TRACED)) ==
    214 	    (PSL_TRACEFORK|PSL_TRACED) && (flags & FORK_PPWAIT) == 0;
    215 }
    216 
    217 /*
    218  * Check if a process is traced and shall inform about VFORK events.
    219  */
    220 static inline bool
    221 tracevfork(struct proc *p, int flags)
    222 {
    223 
    224 	return (p->p_slflag & (PSL_TRACEVFORK|PSL_TRACED)) ==
    225 	    (PSL_TRACEVFORK|PSL_TRACED) && (flags & FORK_PPWAIT) != 0;
    226 }
    227 
    228 /*
    229  * Check if a process is traced and shall inform about VFORK_DONE events.
    230  */
    231 static inline bool
    232 tracevforkdone(struct proc *p, int flags)
    233 {
    234 
    235 	return (p->p_slflag & (PSL_TRACEVFORK_DONE|PSL_TRACED)) ==
    236 	    (PSL_TRACEVFORK_DONE|PSL_TRACED) && (flags & FORK_PPWAIT);
    237 }
    238 
    239 /*
    240  * General fork call.  Note that another LWP in the process may call exec()
    241  * or exit() while we are forking.  It's safe to continue here, because
    242  * neither operation will complete until all LWPs have exited the process.
    243  */
    244 int
    245 fork1(struct lwp *l1, int flags, int exitsig, void *stack, size_t stacksize,
    246     void (*func)(void *), void *arg, register_t *retval)
    247 {
    248 	struct proc	*p1, *p2, *parent;
    249 	struct plimit   *p1_lim;
    250 	uid_t		uid;
    251 	struct lwp	*l2;
    252 	int		count;
    253 	vaddr_t		uaddr;
    254 	int		tnprocs;
    255 	bool		trace_fork, trace_vfork;
    256 	int		error = 0;
    257 
    258 	p1 = l1->l_proc;
    259 	uid = kauth_cred_getuid(l1->l_cred);
    260 	tnprocs = atomic_inc_uint_nv(&nprocs);
    261 
    262 	/*
    263 	 * Although process entries are dynamically created, we still keep
    264 	 * a global limit on the maximum number we will create.
    265 	 */
    266 	if (__predict_false(tnprocs >= maxproc))
    267 		error = -1;
    268 	else
    269 		error = kauth_authorize_process(l1->l_cred,
    270 		    KAUTH_PROCESS_FORK, p1, KAUTH_ARG(tnprocs), NULL, NULL);
    271 
    272 	if (error) {
    273 		static struct timeval lasttfm;
    274 		atomic_dec_uint(&nprocs);
    275 		if (ratecheck(&lasttfm, &fork_tfmrate))
    276 			tablefull("proc", "increase kern.maxproc or NPROC");
    277 		if (forkfsleep)
    278 			kpause("forkmx", false, forkfsleep, NULL);
    279 		return EAGAIN;
    280 	}
    281 
    282 	/*
    283 	 * Enforce limits.
    284 	 */
    285 	count = chgproccnt(uid, 1);
    286 	if (__predict_false(count > p1->p_rlimit[RLIMIT_NPROC].rlim_cur)) {
    287 		if (kauth_authorize_process(l1->l_cred, KAUTH_PROCESS_RLIMIT,
    288 		    p1, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
    289 		    &p1->p_rlimit[RLIMIT_NPROC], KAUTH_ARG(RLIMIT_NPROC)) != 0) {
    290 			(void)chgproccnt(uid, -1);
    291 			atomic_dec_uint(&nprocs);
    292 			if (forkfsleep)
    293 				kpause("forkulim", false, forkfsleep, NULL);
    294 			return EAGAIN;
    295 		}
    296 	}
    297 
    298 	/*
    299 	 * Allocate virtual address space for the U-area now, while it
    300 	 * is still easy to abort the fork operation if we're out of
    301 	 * kernel virtual address space.
    302 	 */
    303 	uaddr = uvm_uarea_alloc();
    304 	if (__predict_false(uaddr == 0)) {
    305 		(void)chgproccnt(uid, -1);
    306 		atomic_dec_uint(&nprocs);
    307 		return ENOMEM;
    308 	}
    309 
    310 	/*
    311 	 * We are now committed to the fork.  From here on, we may
    312 	 * block on resources, but resource allocation may NOT fail.
    313 	 */
    314 
    315 	/* Allocate new proc. */
    316 	p2 = proc_alloc();
    317 
    318 	/*
    319 	 * Make a proc table entry for the new process.
    320 	 * Start by zeroing the section of proc that is zero-initialized,
    321 	 * then copy the section that is copied directly from the parent.
    322 	 */
    323 	memset(&p2->p_startzero, 0,
    324 	    (unsigned) ((char *)&p2->p_endzero - (char *)&p2->p_startzero));
    325 	memcpy(&p2->p_startcopy, &p1->p_startcopy,
    326 	    (unsigned) ((char *)&p2->p_endcopy - (char *)&p2->p_startcopy));
    327 
    328 	TAILQ_INIT(&p2->p_sigpend.sp_info);
    329 
    330 	LIST_INIT(&p2->p_lwps);
    331 	LIST_INIT(&p2->p_sigwaiters);
    332 
    333 	/*
    334 	 * Duplicate sub-structures as needed.
    335 	 * Increase reference counts on shared objects.
    336 	 * Inherit flags we want to keep.  The flags related to SIGCHLD
    337 	 * handling are important in order to keep a consistent behaviour
    338 	 * for the child after the fork.  If we are a 32-bit process, the
    339 	 * child will be too.
    340 	 */
    341 	p2->p_flag =
    342 	    p1->p_flag & (PK_SUGID | PK_NOCLDWAIT | PK_CLDSIGIGN | PK_32);
    343 	p2->p_emul = p1->p_emul;
    344 	p2->p_execsw = p1->p_execsw;
    345 
    346 	if (flags & FORK_SYSTEM) {
    347 		/*
    348 		 * Mark it as a system process.  Set P_NOCLDWAIT so that
    349 		 * children are reparented to init(8) when they exit.
    350 		 * init(8) can easily wait them out for us.
    351 		 */
    352 		p2->p_flag |= (PK_SYSTEM | PK_NOCLDWAIT);
    353 	}
    354 
    355 	mutex_init(&p2->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    356 	mutex_init(&p2->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    357 	rw_init(&p2->p_reflock);
    358 	cv_init(&p2->p_waitcv, "wait");
    359 	cv_init(&p2->p_lwpcv, "lwpwait");
    360 
    361 	/*
    362 	 * Share a lock between the processes if they are to share signal
    363 	 * state: we must synchronize access to it.
    364 	 */
    365 	if (flags & FORK_SHARESIGS) {
    366 		p2->p_lock = p1->p_lock;
    367 		mutex_obj_hold(p1->p_lock);
    368 	} else
    369 		p2->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    370 
    371 	kauth_proc_fork(p1, p2);
    372 
    373 	p2->p_raslist = NULL;
    374 #if defined(__HAVE_RAS)
    375 	ras_fork(p1, p2);
    376 #endif
    377 
    378 	/* bump references to the text vnode (for procfs) */
    379 	p2->p_textvp = p1->p_textvp;
    380 	if (p2->p_textvp)
    381 		vref(p2->p_textvp);
    382 	if (p1->p_path)
    383 		p2->p_path = kmem_strdupsize(p1->p_path, NULL, KM_SLEEP);
    384 	else
    385 		p2->p_path = NULL;
    386 
    387 	if (flags & FORK_SHAREFILES)
    388 		fd_share(p2);
    389 	else if (flags & FORK_CLEANFILES)
    390 		p2->p_fd = fd_init(NULL);
    391 	else
    392 		p2->p_fd = fd_copy();
    393 
    394 	/* XXX racy */
    395 	p2->p_mqueue_cnt = p1->p_mqueue_cnt;
    396 
    397 	if (flags & FORK_SHARECWD)
    398 		cwdshare(p2);
    399 	else
    400 		p2->p_cwdi = cwdinit();
    401 
    402 	/*
    403 	 * Note: p_limit (rlimit stuff) is copy-on-write, so normally
    404 	 * we just need increase pl_refcnt.
    405 	 */
    406 	p1_lim = p1->p_limit;
    407 	if (!p1_lim->pl_writeable) {
    408 		lim_addref(p1_lim);
    409 		p2->p_limit = p1_lim;
    410 	} else {
    411 		p2->p_limit = lim_copy(p1_lim);
    412 	}
    413 
    414 	if (flags & FORK_PPWAIT) {
    415 		/* Mark ourselves as waiting for a child. */
    416 		p2->p_lflag = PL_PPWAIT;
    417 		l1->l_vforkwaiting = true;
    418 		p2->p_vforklwp = l1;
    419 	} else {
    420 		p2->p_lflag = 0;
    421 		l1->l_vforkwaiting = false;
    422 	}
    423 	p2->p_sflag = 0;
    424 	p2->p_slflag = 0;
    425 	parent = (flags & FORK_NOWAIT) ? initproc : p1;
    426 	p2->p_pptr = parent;
    427 	p2->p_ppid = parent->p_pid;
    428 	LIST_INIT(&p2->p_children);
    429 
    430 	p2->p_aio = NULL;
    431 
    432 #ifdef KTRACE
    433 	/*
    434 	 * Copy traceflag and tracefile if enabled.
    435 	 * If not inherited, these were zeroed above.
    436 	 */
    437 	if (p1->p_traceflag & KTRFAC_INHERIT) {
    438 		mutex_enter(&ktrace_lock);
    439 		p2->p_traceflag = p1->p_traceflag;
    440 		if ((p2->p_tracep = p1->p_tracep) != NULL)
    441 			ktradref(p2);
    442 		mutex_exit(&ktrace_lock);
    443 	}
    444 #endif
    445 
    446 	/*
    447 	 * Create signal actions for the child process.
    448 	 */
    449 	p2->p_sigacts = sigactsinit(p1, flags & FORK_SHARESIGS);
    450 	mutex_enter(p1->p_lock);
    451 	p2->p_sflag |=
    452 	    (p1->p_sflag & (PS_STOPFORK | PS_STOPEXEC | PS_NOCLDSTOP));
    453 	sched_proc_fork(p1, p2);
    454 	mutex_exit(p1->p_lock);
    455 
    456 	p2->p_stflag = p1->p_stflag;
    457 
    458 	/*
    459 	 * p_stats.
    460 	 * Copy parts of p_stats, and zero out the rest.
    461 	 */
    462 	p2->p_stats = pstatscopy(p1->p_stats);
    463 
    464 	/*
    465 	 * Set up the new process address space.
    466 	 */
    467 	uvm_proc_fork(p1, p2, (flags & FORK_SHAREVM) ? true : false);
    468 
    469 	/*
    470 	 * Finish creating the child process.
    471 	 * It will return through a different path later.
    472 	 */
    473 	lwp_create(l1, p2, uaddr, (flags & FORK_PPWAIT) ? LWP_VFORK : 0,
    474 	    stack, stacksize, (func != NULL) ? func : child_return, arg, &l2,
    475 	    l1->l_class, &l1->l_sigmask, &l1->l_sigstk);
    476 
    477 	/*
    478 	 * Inherit l_private from the parent.
    479 	 * Note that we cannot use lwp_setprivate() here since that
    480 	 * also sets the CPU TLS register, which is incorrect if the
    481 	 * process has changed that without letting the kernel know.
    482 	 */
    483 	l2->l_private = l1->l_private;
    484 
    485 	/*
    486 	 * If emulation has a process fork hook, call it now.
    487 	 */
    488 	if (p2->p_emul->e_proc_fork)
    489 		(*p2->p_emul->e_proc_fork)(p2, l1, flags);
    490 
    491 	/*
    492 	 * ...and finally, any other random fork hooks that subsystems
    493 	 * might have registered.
    494 	 */
    495 	doforkhooks(p2, p1);
    496 
    497 	SDT_PROBE(proc, kernel, , create, p2, p1, flags, 0, 0);
    498 
    499 	/*
    500 	 * It's now safe for the scheduler and other processes to see the
    501 	 * child process.
    502 	 */
    503 	mutex_enter(proc_lock);
    504 
    505 	if (p1->p_session->s_ttyvp != NULL && p1->p_lflag & PL_CONTROLT)
    506 		p2->p_lflag |= PL_CONTROLT;
    507 
    508 	LIST_INSERT_HEAD(&parent->p_children, p2, p_sibling);
    509 	p2->p_exitsig = exitsig;		/* signal for parent on exit */
    510 
    511 	/*
    512 	 * Trace fork(2) and vfork(2)-like events on demand in a debugger.
    513 	 */
    514 	trace_fork = tracefork(p1, flags);
    515 	trace_vfork = tracevfork(p1, flags);
    516 	if (trace_fork || trace_vfork)
    517 		proc_changeparent(p2, p1->p_pptr);
    518 	if (trace_fork) {
    519 		p1->p_fpid = p2->p_pid;
    520 		p2->p_fpid = p1->p_pid;
    521 	}
    522 	if (trace_vfork) {
    523 		p1->p_vfpid = p2->p_pid;
    524 		p2->p_vfpid = p1->p_pid;
    525 	}
    526 
    527 	LIST_INSERT_AFTER(p1, p2, p_pglist);
    528 	LIST_INSERT_HEAD(&allproc, p2, p_list);
    529 
    530 	p2->p_trace_enabled = trace_is_enabled(p2);
    531 #ifdef __HAVE_SYSCALL_INTERN
    532 	(*p2->p_emul->e_syscall_intern)(p2);
    533 #endif
    534 
    535 	/*
    536 	 * Update stats now that we know the fork was successful.
    537 	 */
    538 	uvmexp.forks++;
    539 	if (flags & FORK_PPWAIT)
    540 		uvmexp.forks_ppwait++;
    541 	if (flags & FORK_SHAREVM)
    542 		uvmexp.forks_sharevm++;
    543 
    544 	if (ktrpoint(KTR_EMUL))
    545 		p2->p_traceflag |= KTRFAC_TRC_EMUL;
    546 
    547 	/*
    548 	 * Notify any interested parties about the new process.
    549 	 */
    550 	if (!SLIST_EMPTY(&p1->p_klist)) {
    551 		mutex_exit(proc_lock);
    552 		KNOTE(&p1->p_klist, NOTE_FORK | p2->p_pid);
    553 		mutex_enter(proc_lock);
    554 	}
    555 
    556 	/*
    557 	 * Make child runnable, set start time, and add to run queue except
    558 	 * if the parent requested the child to start in SSTOP state.
    559 	 */
    560 	mutex_enter(p2->p_lock);
    561 
    562 	/*
    563 	 * Start profiling.
    564 	 */
    565 	if ((p2->p_stflag & PST_PROFIL) != 0) {
    566 		mutex_spin_enter(&p2->p_stmutex);
    567 		startprofclock(p2);
    568 		mutex_spin_exit(&p2->p_stmutex);
    569 	}
    570 
    571 	getmicrotime(&p2->p_stats->p_start);
    572 	p2->p_acflag = AFORK;
    573 	lwp_lock(l2);
    574 	KASSERT(p2->p_nrlwps == 1);
    575 	if (p2->p_sflag & PS_STOPFORK) {
    576 		struct schedstate_percpu *spc = &l2->l_cpu->ci_schedstate;
    577 		p2->p_nrlwps = 0;
    578 		p2->p_stat = SSTOP;
    579 		p2->p_waited = 0;
    580 		p1->p_nstopchild++;
    581 		l2->l_stat = LSSTOP;
    582 		KASSERT(l2->l_wchan == NULL);
    583 		lwp_unlock_to(l2, spc->spc_lwplock);
    584 	} else {
    585 		p2->p_nrlwps = 1;
    586 		p2->p_stat = SACTIVE;
    587 		l2->l_stat = LSRUN;
    588 		sched_enqueue(l2, false);
    589 		lwp_unlock(l2);
    590 	}
    591 
    592 	/*
    593 	 * Return child pid to parent process,
    594 	 * marking us as parent via retval[1].
    595 	 */
    596 	if (retval != NULL) {
    597 		retval[0] = p2->p_pid;
    598 		retval[1] = 0;
    599 	}
    600 
    601 	mutex_exit(p2->p_lock);
    602 
    603 	/*
    604 	 * Let the parent know that we are tracing its child.
    605 	 */
    606 	if (tracefork(p1, flags) || tracevfork(p1, flags)) {
    607 		mutex_enter(p1->p_lock);
    608 		eventswitch(TRAP_CHLD);
    609 		mutex_enter(proc_lock);
    610 	}
    611 
    612 	/*
    613 	 * Preserve synchronization semantics of vfork.  If waiting for
    614 	 * child to exec or exit, sleep until it clears p_vforkwaiting.
    615 	 */
    616 	while (l1->l_vforkwaiting)
    617 		cv_wait(&l1->l_waitcv, proc_lock);
    618 
    619 	/*
    620 	 * Let the parent know that we are tracing its child.
    621 	 */
    622 	if (tracevforkdone(p1, flags)) {
    623 		mutex_enter(p1->p_lock);
    624 		p1->p_vfpid_done = retval[0];
    625 		eventswitch(TRAP_CHLD);
    626 	} else
    627 		mutex_exit(proc_lock);
    628 
    629 	return 0;
    630 }
    631 
    632 /*
    633  * MI code executed in each newly spawned process before returning to userland.
    634  */
    635 void
    636 child_return(void *arg)
    637 {
    638 	struct lwp *l = arg;
    639 	struct proc *p = l->l_proc;
    640 
    641 	if (p->p_slflag & PSL_TRACED) {
    642 		/* Paranoid check */
    643 		mutex_enter(proc_lock);
    644 		if (!(p->p_slflag & PSL_TRACED)) {
    645 			mutex_exit(proc_lock);
    646 			goto my_tracer_is_gone;
    647 		}
    648 
    649 		mutex_enter(p->p_lock);
    650 		eventswitch(TRAP_CHLD);
    651 	}
    652 
    653 my_tracer_is_gone:
    654 	md_child_return(l);
    655 
    656 	/*
    657 	 * Return SYS_fork for all fork types, including vfork(2) and clone(2).
    658 	 *
    659 	 * This approach simplifies the code and avoids extra locking.
    660 	 */
    661 	ktrsysret(SYS_fork, 0, 0);
    662 }
    663