xref: /illumos-gate/usr/src/uts/common/os/fork.c (revision 13b136d3061155363c62c9f6568d25b8b27da8f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2013, Joyent, Inc. All rights reserved.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/sysmacros.h>
33 #include <sys/signal.h>
34 #include <sys/cred.h>
35 #include <sys/policy.h>
36 #include <sys/user.h>
37 #include <sys/systm.h>
38 #include <sys/cpuvar.h>
39 #include <sys/vfs.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/errno.h>
43 #include <sys/time.h>
44 #include <sys/proc.h>
45 #include <sys/cmn_err.h>
46 #include <sys/acct.h>
47 #include <sys/tuneable.h>
48 #include <sys/class.h>
49 #include <sys/kmem.h>
50 #include <sys/session.h>
51 #include <sys/ucontext.h>
52 #include <sys/stack.h>
53 #include <sys/procfs.h>
54 #include <sys/prsystm.h>
55 #include <sys/vmsystm.h>
56 #include <sys/vtrace.h>
57 #include <sys/debug.h>
58 #include <sys/shm_impl.h>
59 #include <sys/door_data.h>
60 #include <vm/as.h>
61 #include <vm/rm.h>
62 #include <c2/audit.h>
63 #include <sys/var.h>
64 #include <sys/schedctl.h>
65 #include <sys/utrap.h>
66 #include <sys/task.h>
67 #include <sys/resource.h>
68 #include <sys/cyclic.h>
69 #include <sys/lgrp.h>
70 #include <sys/rctl.h>
71 #include <sys/contract_impl.h>
72 #include <sys/contract/process_impl.h>
73 #include <sys/list.h>
74 #include <sys/dtrace.h>
75 #include <sys/pool.h>
76 #include <sys/zone.h>
77 #include <sys/sdt.h>
78 #include <sys/class.h>
79 #include <sys/corectl.h>
80 #include <sys/brand.h>
81 #include <sys/fork.h>
82 
83 static int64_t cfork(int, int, int);
84 static int getproc(proc_t **, pid_t, uint_t);
85 #define	GETPROC_USER	0x0
86 #define	GETPROC_KERNEL	0x1
87 
88 static void fork_fail(proc_t *);
89 static void forklwp_fail(proc_t *);
90 
91 int fork_fail_pending;
92 
93 extern struct kmem_cache *process_cache;
94 
95 /*
96  * The vfork() system call trap is no longer invoked by libc.
97  * It is retained only for the benefit of applications running
98  * within a solaris10 branded zone.  It should be eliminated
99  * when we no longer support solaris10 branded zones.
100  */
101 int64_t
102 vfork(void)
103 {
104 	curthread->t_post_sys = 1;	/* so vfwait() will be called */
105 	return (cfork(1, 1, 0));
106 }
107 
108 /*
109  * forksys system call - forkx, forkallx, vforkx.  This is the
110  * interface invoked by libc for fork1(), forkall(), and vfork()
111  */
112 int64_t
113 forksys(int subcode, int flags)
114 {
115 	switch (subcode) {
116 	case 0:
117 		return (cfork(0, 1, flags));	/* forkx(flags) */
118 	case 1:
119 		return (cfork(0, 0, flags));	/* forkallx(flags) */
120 	case 2:
121 		curthread->t_post_sys = 1;	/* so vfwait() will be called */
122 		return (cfork(1, 1, flags));	/* vforkx(flags) */
123 	default:
124 		return ((int64_t)set_errno(EINVAL));
125 	}
126 }
127 
128 /*
129  * Remove the associations of a child process from its parent and siblings.
130  */
131 static void
132 disown_proc(proc_t *pp, proc_t *cp)
133 {
134 	proc_t **orphpp;
135 
136 	ASSERT(MUTEX_HELD(&pidlock));
137 
138 	orphpp = &pp->p_orphan;
139 	while (*orphpp != cp)
140 		orphpp = &(*orphpp)->p_nextorph;
141 	*orphpp = cp->p_nextorph;
142 
143 	if (pp->p_child == cp)
144 		pp->p_child = cp->p_sibling;
145 	if (cp->p_sibling)
146 		cp->p_sibling->p_psibling = cp->p_psibling;
147 	if (cp->p_psibling)
148 		cp->p_psibling->p_sibling = cp->p_sibling;
149 }
150 
151 /* ARGSUSED */
152 static int64_t
153 cfork(int isvfork, int isfork1, int flags)
154 {
155 	proc_t *p = ttoproc(curthread);
156 	struct as *as;
157 	proc_t *cp;
158 	klwp_t *clone;
159 	kthread_t *t;
160 	task_t *tk;
161 	rval_t	r;
162 	int error;
163 	int i;
164 	rctl_set_t *dup_set;
165 	rctl_alloc_gp_t *dup_gp;
166 	rctl_entity_p_t e;
167 	lwpdir_t *ldp;
168 	lwpent_t *lep;
169 	lwpent_t *clep;
170 
171 	clone = NULL;
172 	/*
173 	 * Allow only these two flags.
174 	 */
175 	if ((flags & ~(FORK_NOSIGCHLD | FORK_WAITPID)) != 0) {
176 		error = EINVAL;
177 		atomic_inc_32(&curproc->p_zone->zone_ffmisc);
178 		goto forkerr;
179 	}
180 
181 	/*
182 	 * fork is not supported for the /proc agent lwp.
183 	 */
184 	if (curthread == p->p_agenttp) {
185 		error = ENOTSUP;
186 		atomic_inc_32(&curproc->p_zone->zone_ffmisc);
187 		goto forkerr;
188 	}
189 
190 	if ((error = secpolicy_basic_fork(CRED())) != 0) {
191 		atomic_inc_32(&p->p_zone->zone_ffmisc);
192 		goto forkerr;
193 	}
194 
195 	/*
196 	 * If the calling lwp is doing a fork1() then the
197 	 * other lwps in this process are not duplicated and
198 	 * don't need to be held where their kernel stacks can be
199 	 * cloned.  If doing forkall(), the process is held with
200 	 * SHOLDFORK, so that the lwps are at a point where their
201 	 * stacks can be copied which is on entry or exit from
202 	 * the kernel.
203 	 */
204 	if (!holdlwps(isfork1 ? SHOLDFORK1 : SHOLDFORK)) {
205 		aston(curthread);
206 		error = EINTR;
207 		atomic_inc_32(&p->p_zone->zone_ffmisc);
208 		goto forkerr;
209 	}
210 
211 #if defined(__sparc)
212 	/*
213 	 * Ensure that the user stack is fully constructed
214 	 * before creating the child process structure.
215 	 */
216 	(void) flush_user_windows_to_stack(NULL);
217 #endif
218 
219 	mutex_enter(&p->p_lock);
220 	/*
221 	 * If this is vfork(), cancel any suspend request we might
222 	 * have gotten from some other thread via lwp_suspend().
223 	 * Otherwise we could end up with a deadlock on return
224 	 * from the vfork() in both the parent and the child.
225 	 */
226 	if (isvfork)
227 		curthread->t_proc_flag &= ~TP_HOLDLWP;
228 	/*
229 	 * Prevent our resource set associations from being changed during fork.
230 	 */
231 	pool_barrier_enter();
232 	mutex_exit(&p->p_lock);
233 
234 	/*
235 	 * Create a child proc struct. Place a VN_HOLD on appropriate vnodes.
236 	 */
237 	if (getproc(&cp, 0, GETPROC_USER) < 0) {
238 		mutex_enter(&p->p_lock);
239 		pool_barrier_exit();
240 		continuelwps(p);
241 		mutex_exit(&p->p_lock);
242 		error = EAGAIN;
243 		goto forkerr;
244 	}
245 
246 	TRACE_2(TR_FAC_PROC, TR_PROC_FORK, "proc_fork:cp %p p %p", cp, p);
247 
248 	/*
249 	 * Assign an address space to child
250 	 */
251 	if (isvfork) {
252 		/*
253 		 * Clear any watched areas and remember the
254 		 * watched pages for restoring in vfwait().
255 		 */
256 		as = p->p_as;
257 		if (avl_numnodes(&as->a_wpage) != 0) {
258 			AS_LOCK_ENTER(as, RW_WRITER);
259 			as_clearwatch(as);
260 			p->p_wpage = as->a_wpage;
261 			avl_create(&as->a_wpage, wp_compare,
262 			    sizeof (struct watched_page),
263 			    offsetof(struct watched_page, wp_link));
264 			AS_LOCK_EXIT(as);
265 		}
266 		cp->p_as = as;
267 		cp->p_flag |= SVFORK;
268 
269 		/*
270 		 * Use the parent's shm segment list information for
271 		 * the child as it uses its address space till it execs.
272 		 */
273 		cp->p_segacct = p->p_segacct;
274 	} else {
275 		/*
276 		 * We need to hold P_PR_LOCK until the address space has
277 		 * been duplicated and we've had a chance to remove from the
278 		 * child any DTrace probes that were in the parent. Holding
279 		 * P_PR_LOCK prevents any new probes from being added and any
280 		 * extant probes from being removed.
281 		 */
282 		mutex_enter(&p->p_lock);
283 		sprlock_proc(p);
284 		p->p_flag |= SFORKING;
285 		mutex_exit(&p->p_lock);
286 
287 		error = as_dup(p->p_as, cp);
288 		if (error != 0) {
289 			mutex_enter(&p->p_lock);
290 			sprunlock(p);
291 			fork_fail(cp);
292 			mutex_enter(&pidlock);
293 			disown_proc(p, cp);
294 			mutex_enter(&cp->p_lock);
295 			tk = cp->p_task;
296 			task_detach(cp);
297 			ASSERT(cp->p_pool->pool_ref > 0);
298 			atomic_dec_32(&cp->p_pool->pool_ref);
299 			mutex_exit(&cp->p_lock);
300 			pid_exit(cp, tk);
301 			mutex_exit(&pidlock);
302 			task_rele(tk);
303 
304 			mutex_enter(&p->p_lock);
305 			p->p_flag &= ~SFORKING;
306 			pool_barrier_exit();
307 			continuelwps(p);
308 			mutex_exit(&p->p_lock);
309 			/*
310 			 * Preserve ENOMEM error condition but
311 			 * map all others to EAGAIN.
312 			 */
313 			error = (error == ENOMEM) ? ENOMEM : EAGAIN;
314 			atomic_inc_32(&p->p_zone->zone_ffnomem);
315 			goto forkerr;
316 		}
317 
318 		/*
319 		 * Remove all DTrace tracepoints from the child process. We
320 		 * need to do this _before_ duplicating USDT providers since
321 		 * any associated probes may be immediately enabled.
322 		 */
323 		if (p->p_dtrace_count > 0)
324 			dtrace_fasttrap_fork(p, cp);
325 
326 		mutex_enter(&p->p_lock);
327 		sprunlock(p);
328 
329 		/* Duplicate parent's shared memory */
330 		if (p->p_segacct)
331 			shmfork(p, cp);
332 
333 		/*
334 		 * Duplicate any helper actions and providers. The SFORKING
335 		 * we set above informs the code to enable USDT probes that
336 		 * sprlock() may fail because the child is being forked.
337 		 */
338 		if (p->p_dtrace_helpers != NULL) {
339 			ASSERT(dtrace_helpers_fork != NULL);
340 			(*dtrace_helpers_fork)(p, cp);
341 		}
342 
343 		mutex_enter(&p->p_lock);
344 		p->p_flag &= ~SFORKING;
345 		mutex_exit(&p->p_lock);
346 	}
347 
348 	/*
349 	 * Duplicate parent's resource controls.
350 	 */
351 	dup_set = rctl_set_create();
352 	for (;;) {
353 		dup_gp = rctl_set_dup_prealloc(p->p_rctls);
354 		mutex_enter(&p->p_rctls->rcs_lock);
355 		if (rctl_set_dup_ready(p->p_rctls, dup_gp))
356 			break;
357 		mutex_exit(&p->p_rctls->rcs_lock);
358 		rctl_prealloc_destroy(dup_gp);
359 	}
360 	e.rcep_p.proc = cp;
361 	e.rcep_t = RCENTITY_PROCESS;
362 	cp->p_rctls = rctl_set_dup(p->p_rctls, p, cp, &e, dup_set, dup_gp,
363 	    RCD_DUP | RCD_CALLBACK);
364 	mutex_exit(&p->p_rctls->rcs_lock);
365 
366 	rctl_prealloc_destroy(dup_gp);
367 
368 	/*
369 	 * Allocate the child's lwp directory and lwpid hash table.
370 	 */
371 	if (isfork1)
372 		cp->p_lwpdir_sz = 2;
373 	else
374 		cp->p_lwpdir_sz = p->p_lwpdir_sz;
375 	cp->p_lwpdir = cp->p_lwpfree = ldp =
376 	    kmem_zalloc(cp->p_lwpdir_sz * sizeof (lwpdir_t), KM_SLEEP);
377 	for (i = 1; i < cp->p_lwpdir_sz; i++, ldp++)
378 		ldp->ld_next = ldp + 1;
379 	cp->p_tidhash_sz = (cp->p_lwpdir_sz + 2) / 2;
380 	cp->p_tidhash =
381 	    kmem_zalloc(cp->p_tidhash_sz * sizeof (tidhash_t), KM_SLEEP);
382 
383 	/*
384 	 * Duplicate parent's lwps.
385 	 * Mutual exclusion is not needed because the process is
386 	 * in the hold state and only the current lwp is running.
387 	 */
388 	klgrpset_clear(cp->p_lgrpset);
389 	if (isfork1) {
390 		clone = forklwp(ttolwp(curthread), cp, curthread->t_tid);
391 		if (clone == NULL)
392 			goto forklwperr;
393 		/*
394 		 * Inherit only the lwp_wait()able flag,
395 		 * Daemon threads should not call fork1(), but oh well...
396 		 */
397 		lwptot(clone)->t_proc_flag |=
398 		    (curthread->t_proc_flag & TP_TWAIT);
399 	} else {
400 		/* this is forkall(), no one can be in lwp_wait() */
401 		ASSERT(p->p_lwpwait == 0 && p->p_lwpdwait == 0);
402 		/* for each entry in the parent's lwp directory... */
403 		for (i = 0, ldp = p->p_lwpdir; i < p->p_lwpdir_sz; i++, ldp++) {
404 			klwp_t *clwp;
405 			kthread_t *ct;
406 
407 			if ((lep = ldp->ld_entry) == NULL)
408 				continue;
409 
410 			if ((t = lep->le_thread) != NULL) {
411 				clwp = forklwp(ttolwp(t), cp, t->t_tid);
412 				if (clwp == NULL)
413 					goto forklwperr;
414 				ct = lwptot(clwp);
415 				/*
416 				 * Inherit lwp_wait()able and daemon flags.
417 				 */
418 				ct->t_proc_flag |=
419 				    (t->t_proc_flag & (TP_TWAIT|TP_DAEMON));
420 				/*
421 				 * Keep track of the clone of curthread to
422 				 * post return values through lwp_setrval().
423 				 * Mark other threads for special treatment
424 				 * by lwp_rtt() / post_syscall().
425 				 */
426 				if (t == curthread)
427 					clone = clwp;
428 				else
429 					ct->t_flag |= T_FORKALL;
430 			} else {
431 				/*
432 				 * Replicate zombie lwps in the child.
433 				 */
434 				clep = kmem_zalloc(sizeof (*clep), KM_SLEEP);
435 				clep->le_lwpid = lep->le_lwpid;
436 				clep->le_start = lep->le_start;
437 				lwp_hash_in(cp, clep,
438 				    cp->p_tidhash, cp->p_tidhash_sz, 0);
439 			}
440 		}
441 	}
442 
443 	/*
444 	 * Put new process in the parent's process contract, or put it
445 	 * in a new one if there is an active process template.  Send a
446 	 * fork event (if requested) to whatever contract the child is
447 	 * a member of.  Fails if the parent has been SIGKILLed.
448 	 */
449 	if (contract_process_fork(NULL, cp, p, B_TRUE) == NULL) {
450 		atomic_inc_32(&p->p_zone->zone_ffmisc);
451 		goto forklwperr;
452 	}
453 
454 	/*
455 	 * No fork failures occur beyond this point.
456 	 */
457 
458 	cp->p_lwpid = p->p_lwpid;
459 	if (!isfork1) {
460 		cp->p_lwpdaemon = p->p_lwpdaemon;
461 		cp->p_zombcnt = p->p_zombcnt;
462 		/*
463 		 * If the parent's lwp ids have wrapped around, so have the
464 		 * child's.
465 		 */
466 		cp->p_flag |= p->p_flag & SLWPWRAP;
467 	}
468 
469 	mutex_enter(&p->p_lock);
470 	corectl_path_hold(cp->p_corefile = p->p_corefile);
471 	corectl_content_hold(cp->p_content = p->p_content);
472 	mutex_exit(&p->p_lock);
473 
474 	/*
475 	 * Duplicate process context ops, if any.
476 	 */
477 	if (p->p_pctx)
478 		forkpctx(p, cp);
479 
480 #ifdef __sparc
481 	utrap_dup(p, cp);
482 #endif
483 	/*
484 	 * If the child process has been marked to stop on exit
485 	 * from this fork, arrange for all other lwps to stop in
486 	 * sympathy with the active lwp.
487 	 */
488 	if (PTOU(cp)->u_systrap &&
489 	    prismember(&PTOU(cp)->u_exitmask, curthread->t_sysnum)) {
490 		mutex_enter(&cp->p_lock);
491 		t = cp->p_tlist;
492 		do {
493 			t->t_proc_flag |= TP_PRSTOP;
494 			aston(t);	/* so TP_PRSTOP will be seen */
495 		} while ((t = t->t_forw) != cp->p_tlist);
496 		mutex_exit(&cp->p_lock);
497 	}
498 	/*
499 	 * If the parent process has been marked to stop on exit
500 	 * from this fork, and its asynchronous-stop flag has not
501 	 * been set, arrange for all other lwps to stop before
502 	 * they return back to user level.
503 	 */
504 	if (!(p->p_proc_flag & P_PR_ASYNC) && PTOU(p)->u_systrap &&
505 	    prismember(&PTOU(p)->u_exitmask, curthread->t_sysnum)) {
506 		mutex_enter(&p->p_lock);
507 		t = p->p_tlist;
508 		do {
509 			t->t_proc_flag |= TP_PRSTOP;
510 			aston(t);	/* so TP_PRSTOP will be seen */
511 		} while ((t = t->t_forw) != p->p_tlist);
512 		mutex_exit(&p->p_lock);
513 	}
514 
515 	if (PROC_IS_BRANDED(p))
516 		BROP(p)->b_lwp_setrval(clone, p->p_pid, 1);
517 	else
518 		lwp_setrval(clone, p->p_pid, 1);
519 
520 	/* set return values for parent */
521 	r.r_val1 = (int)cp->p_pid;
522 	r.r_val2 = 0;
523 
524 	/*
525 	 * pool_barrier_exit() can now be called because the child process has:
526 	 * - all identifying features cloned or set (p_pid, p_task, p_pool)
527 	 * - all resource sets associated (p_tlist->*->t_cpupart, p_as->a_mset)
528 	 * - any other fields set which are used in resource set binding.
529 	 */
530 	mutex_enter(&p->p_lock);
531 	pool_barrier_exit();
532 	mutex_exit(&p->p_lock);
533 
534 	mutex_enter(&pidlock);
535 	mutex_enter(&cp->p_lock);
536 
537 	/*
538 	 * Set flags telling the child what (not) to do on exit.
539 	 */
540 	if (flags & FORK_NOSIGCHLD)
541 		cp->p_pidflag |= CLDNOSIGCHLD;
542 	if (flags & FORK_WAITPID)
543 		cp->p_pidflag |= CLDWAITPID;
544 
545 	/*
546 	 * Now that there are lwps and threads attached, add the new
547 	 * process to the process group.
548 	 */
549 	pgjoin(cp, p->p_pgidp);
550 	cp->p_stat = SRUN;
551 	/*
552 	 * We are now done with all the lwps in the child process.
553 	 */
554 	t = cp->p_tlist;
555 	do {
556 		/*
557 		 * Set the lwp_suspend()ed lwps running.
558 		 * They will suspend properly at syscall exit.
559 		 */
560 		if (t->t_proc_flag & TP_HOLDLWP)
561 			lwp_create_done(t);
562 		else {
563 			/* set TS_CREATE to allow continuelwps() to work */
564 			thread_lock(t);
565 			ASSERT(t->t_state == TS_STOPPED &&
566 			    !(t->t_schedflag & (TS_CREATE|TS_CSTART)));
567 			t->t_schedflag |= TS_CREATE;
568 			thread_unlock(t);
569 		}
570 	} while ((t = t->t_forw) != cp->p_tlist);
571 	mutex_exit(&cp->p_lock);
572 
573 	if (isvfork) {
574 		CPU_STATS_ADDQ(CPU, sys, sysvfork, 1);
575 		mutex_enter(&p->p_lock);
576 		p->p_flag |= SVFWAIT;
577 		curthread->t_flag |= T_VFPARENT;
578 		DTRACE_PROC1(create, proc_t *, cp);
579 		cv_broadcast(&pr_pid_cv[p->p_slot]);	/* inform /proc */
580 		mutex_exit(&p->p_lock);
581 		/*
582 		 * Grab child's p_lock before dropping pidlock to ensure
583 		 * the process will not disappear before we set it running.
584 		 */
585 		mutex_enter(&cp->p_lock);
586 		mutex_exit(&pidlock);
587 		sigdefault(cp);
588 		continuelwps(cp);
589 		mutex_exit(&cp->p_lock);
590 	} else {
591 		CPU_STATS_ADDQ(CPU, sys, sysfork, 1);
592 		DTRACE_PROC1(create, proc_t *, cp);
593 		/*
594 		 * It is CL_FORKRET's job to drop pidlock.
595 		 * If we do it here, the process could be set running
596 		 * and disappear before CL_FORKRET() is called.
597 		 */
598 		CL_FORKRET(curthread, cp->p_tlist);
599 		schedctl_set_cidpri(curthread);
600 		ASSERT(MUTEX_NOT_HELD(&pidlock));
601 	}
602 
603 	return (r.r_vals);
604 
605 forklwperr:
606 	if (isvfork) {
607 		if (avl_numnodes(&p->p_wpage) != 0) {
608 			/* restore watchpoints to parent */
609 			as = p->p_as;
610 			AS_LOCK_ENTER(as, RW_WRITER);
611 			as->a_wpage = p->p_wpage;
612 			avl_create(&p->p_wpage, wp_compare,
613 			    sizeof (struct watched_page),
614 			    offsetof(struct watched_page, wp_link));
615 			as_setwatch(as);
616 			AS_LOCK_EXIT(as);
617 		}
618 	} else {
619 		if (cp->p_segacct)
620 			shmexit(cp);
621 		as = cp->p_as;
622 		cp->p_as = &kas;
623 		as_free(as);
624 	}
625 
626 	if (cp->p_lwpdir) {
627 		for (i = 0, ldp = cp->p_lwpdir; i < cp->p_lwpdir_sz; i++, ldp++)
628 			if ((lep = ldp->ld_entry) != NULL)
629 				kmem_free(lep, sizeof (*lep));
630 		kmem_free(cp->p_lwpdir,
631 		    cp->p_lwpdir_sz * sizeof (*cp->p_lwpdir));
632 	}
633 	cp->p_lwpdir = NULL;
634 	cp->p_lwpfree = NULL;
635 	cp->p_lwpdir_sz = 0;
636 
637 	if (cp->p_tidhash)
638 		kmem_free(cp->p_tidhash,
639 		    cp->p_tidhash_sz * sizeof (*cp->p_tidhash));
640 	cp->p_tidhash = NULL;
641 	cp->p_tidhash_sz = 0;
642 
643 	forklwp_fail(cp);
644 	fork_fail(cp);
645 	if (cp->p_dtrace_helpers != NULL) {
646 		ASSERT(dtrace_helpers_cleanup != NULL);
647 		(*dtrace_helpers_cleanup)(cp);
648 	}
649 	rctl_set_free(cp->p_rctls);
650 	mutex_enter(&pidlock);
651 
652 	/*
653 	 * Detach failed child from task.
654 	 */
655 	mutex_enter(&cp->p_lock);
656 	tk = cp->p_task;
657 	task_detach(cp);
658 	ASSERT(cp->p_pool->pool_ref > 0);
659 	atomic_dec_32(&cp->p_pool->pool_ref);
660 	mutex_exit(&cp->p_lock);
661 
662 	disown_proc(p, cp);
663 	pid_exit(cp, tk);
664 	mutex_exit(&pidlock);
665 
666 	task_rele(tk);
667 
668 	mutex_enter(&p->p_lock);
669 	pool_barrier_exit();
670 	continuelwps(p);
671 	mutex_exit(&p->p_lock);
672 	error = EAGAIN;
673 forkerr:
674 	return ((int64_t)set_errno(error));
675 }
676 
677 /*
678  * Free allocated resources from getproc() if a fork failed.
679  */
680 static void
681 fork_fail(proc_t *cp)
682 {
683 	uf_info_t *fip = P_FINFO(cp);
684 
685 	fcnt_add(fip, -1);
686 	sigdelq(cp, NULL, 0);
687 
688 	mutex_enter(&pidlock);
689 	upcount_dec(crgetruid(cp->p_cred), crgetzoneid(cp->p_cred));
690 	mutex_exit(&pidlock);
691 
692 	/*
693 	 * single threaded, so no locking needed here
694 	 */
695 	crfree(cp->p_cred);
696 
697 	kmem_free(fip->fi_list, fip->fi_nfiles * sizeof (uf_entry_t));
698 
699 	VN_RELE(PTOU(curproc)->u_cdir);
700 	if (PTOU(curproc)->u_rdir)
701 		VN_RELE(PTOU(curproc)->u_rdir);
702 	if (cp->p_exec)
703 		VN_RELE(cp->p_exec);
704 	if (cp->p_execdir)
705 		VN_RELE(cp->p_execdir);
706 	if (PTOU(curproc)->u_cwd)
707 		refstr_rele(PTOU(curproc)->u_cwd);
708 	if (PROC_IS_BRANDED(cp)) {
709 		brand_clearbrand(cp, B_TRUE);
710 	}
711 }
712 
713 /*
714  * Clean up the lwps already created for this child process.
715  * The fork failed while duplicating all the lwps of the parent
716  * and those lwps already created must be freed.
717  * This process is invisible to the rest of the system,
718  * so we don't need to hold p->p_lock to protect the list.
719  */
720 static void
721 forklwp_fail(proc_t *p)
722 {
723 	kthread_t *t;
724 	task_t *tk;
725 	int branded = 0;
726 
727 	if (PROC_IS_BRANDED(p))
728 		branded = 1;
729 
730 	while ((t = p->p_tlist) != NULL) {
731 		/*
732 		 * First remove the lwp from the process's p_tlist.
733 		 */
734 		if (t != t->t_forw)
735 			p->p_tlist = t->t_forw;
736 		else
737 			p->p_tlist = NULL;
738 		p->p_lwpcnt--;
739 		t->t_forw->t_back = t->t_back;
740 		t->t_back->t_forw = t->t_forw;
741 
742 		tk = p->p_task;
743 		mutex_enter(&p->p_zone->zone_nlwps_lock);
744 		tk->tk_nlwps--;
745 		tk->tk_proj->kpj_nlwps--;
746 		p->p_zone->zone_nlwps--;
747 		mutex_exit(&p->p_zone->zone_nlwps_lock);
748 
749 		ASSERT(t->t_schedctl == NULL);
750 
751 		if (branded)
752 			BROP(p)->b_freelwp(ttolwp(t));
753 
754 		if (t->t_door != NULL) {
755 			kmem_free(t->t_door, sizeof (door_data_t));
756 			t->t_door = NULL;
757 		}
758 		lwp_ctmpl_clear(ttolwp(t));
759 
760 		/*
761 		 * Remove the thread from the all threads list.
762 		 * We need to hold pidlock for this.
763 		 */
764 		mutex_enter(&pidlock);
765 		t->t_next->t_prev = t->t_prev;
766 		t->t_prev->t_next = t->t_next;
767 		CL_EXIT(t);	/* tell the scheduler that we're exiting */
768 		cv_broadcast(&t->t_joincv);	/* tell anyone in thread_join */
769 		mutex_exit(&pidlock);
770 
771 		/*
772 		 * Let the lgroup load averages know that this thread isn't
773 		 * going to show up (i.e. un-do what was done on behalf of
774 		 * this thread by the earlier lgrp_move_thread()).
775 		 */
776 		kpreempt_disable();
777 		lgrp_move_thread(t, NULL, 1);
778 		kpreempt_enable();
779 
780 		/*
781 		 * The thread was created TS_STOPPED.
782 		 * We change it to TS_FREE to avoid an
783 		 * ASSERT() panic in thread_free().
784 		 */
785 		t->t_state = TS_FREE;
786 		thread_rele(t);
787 		thread_free(t);
788 	}
789 }
790 
791 extern struct as kas;
792 
793 /*
794  * fork a kernel process.
795  */
796 int
797 newproc(void (*pc)(), caddr_t arg, id_t cid, int pri, struct contract **ct,
798     pid_t pid)
799 {
800 	proc_t *p;
801 	struct user *up;
802 	kthread_t *t;
803 	cont_process_t *ctp = NULL;
804 	rctl_entity_p_t e;
805 
806 	ASSERT(cid != sysdccid);
807 	ASSERT(cid != syscid || ct == NULL);
808 	if (CLASS_KERNEL(cid)) {
809 		rctl_alloc_gp_t *init_gp;
810 		rctl_set_t *init_set;
811 
812 		ASSERT(pid != 1);
813 
814 		if (getproc(&p, pid, GETPROC_KERNEL) < 0)
815 			return (EAGAIN);
816 
817 		/*
818 		 * Release the hold on the p_exec and p_execdir, these
819 		 * were acquired in getproc()
820 		 */
821 		if (p->p_execdir != NULL)
822 			VN_RELE(p->p_execdir);
823 		if (p->p_exec != NULL)
824 			VN_RELE(p->p_exec);
825 		p->p_flag |= SNOWAIT;
826 		p->p_exec = NULL;
827 		p->p_execdir = NULL;
828 
829 		init_set = rctl_set_create();
830 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
831 
832 		/*
833 		 * kernel processes do not inherit /proc tracing flags.
834 		 */
835 		sigemptyset(&p->p_sigmask);
836 		premptyset(&p->p_fltmask);
837 		up = PTOU(p);
838 		up->u_systrap = 0;
839 		premptyset(&(up->u_entrymask));
840 		premptyset(&(up->u_exitmask));
841 		mutex_enter(&p->p_lock);
842 		e.rcep_p.proc = p;
843 		e.rcep_t = RCENTITY_PROCESS;
844 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
845 		    init_gp);
846 		mutex_exit(&p->p_lock);
847 
848 		rctl_prealloc_destroy(init_gp);
849 
850 		t = lwp_kernel_create(p, pc, arg, TS_STOPPED, pri);
851 	} else {
852 		rctl_alloc_gp_t *init_gp, *default_gp;
853 		rctl_set_t *init_set;
854 		task_t *tk, *tk_old;
855 		klwp_t *lwp;
856 
857 		if (getproc(&p, pid, GETPROC_USER) < 0)
858 			return (EAGAIN);
859 		/*
860 		 * init creates a new task, distinct from the task
861 		 * containing kernel "processes".
862 		 */
863 		tk = task_create(0, p->p_zone);
864 		mutex_enter(&tk->tk_zone->zone_nlwps_lock);
865 		tk->tk_proj->kpj_ntasks++;
866 		tk->tk_nprocs++;
867 		mutex_exit(&tk->tk_zone->zone_nlwps_lock);
868 
869 		default_gp = rctl_rlimit_set_prealloc(RLIM_NLIMITS);
870 		init_gp = rctl_set_init_prealloc(RCENTITY_PROCESS);
871 		init_set = rctl_set_create();
872 
873 		mutex_enter(&pidlock);
874 		mutex_enter(&p->p_lock);
875 		tk_old = p->p_task;	/* switch to new task */
876 
877 		task_detach(p);
878 		task_begin(tk, p);
879 		mutex_exit(&pidlock);
880 
881 		mutex_enter(&tk_old->tk_zone->zone_nlwps_lock);
882 		tk_old->tk_nprocs--;
883 		mutex_exit(&tk_old->tk_zone->zone_nlwps_lock);
884 
885 		e.rcep_p.proc = p;
886 		e.rcep_t = RCENTITY_PROCESS;
887 		p->p_rctls = rctl_set_init(RCENTITY_PROCESS, p, &e, init_set,
888 		    init_gp);
889 		rctlproc_default_init(p, default_gp);
890 		mutex_exit(&p->p_lock);
891 
892 		task_rele(tk_old);
893 		rctl_prealloc_destroy(default_gp);
894 		rctl_prealloc_destroy(init_gp);
895 
896 		if ((lwp = lwp_create(pc, arg, 0, p, TS_STOPPED, pri,
897 		    &curthread->t_hold, cid, 1)) == NULL) {
898 			task_t *tk;
899 
900 			fork_fail(p);
901 			mutex_enter(&pidlock);
902 			disown_proc(p->p_parent, p);
903 
904 			mutex_enter(&p->p_lock);
905 			tk = p->p_task;
906 			task_detach(p);
907 			ASSERT(p->p_pool->pool_ref > 0);
908 			atomic_add_32(&p->p_pool->pool_ref, -1);
909 			mutex_exit(&p->p_lock);
910 
911 			pid_exit(p, tk);
912 			mutex_exit(&pidlock);
913 			task_rele(tk);
914 			return (EAGAIN);
915 		}
916 		t = lwptot(lwp);
917 
918 		ctp = contract_process_fork(sys_process_tmpl, p, curproc,
919 		    B_FALSE);
920 		ASSERT(ctp != NULL);
921 		if (ct != NULL)
922 			*ct = &ctp->conp_contract;
923 	}
924 
925 	ASSERT3U(t->t_tid, ==, 1);
926 	p->p_lwpid = 1;
927 	mutex_enter(&pidlock);
928 	pgjoin(p, p->p_parent->p_pgidp);
929 	p->p_stat = SRUN;
930 	mutex_enter(&p->p_lock);
931 	t->t_proc_flag &= ~TP_HOLDLWP;
932 	lwp_create_done(t);
933 	mutex_exit(&p->p_lock);
934 	mutex_exit(&pidlock);
935 	return (0);
936 }
937 
938 /*
939  * create a child proc struct.
940  */
941 static int
942 getproc(proc_t **cpp, pid_t pid, uint_t flags)
943 {
944 	proc_t		*pp, *cp;
945 	pid_t		newpid;
946 	struct user	*uarea;
947 	extern uint_t	nproc;
948 	struct cred	*cr;
949 	uid_t		ruid;
950 	zoneid_t	zoneid;
951 	task_t		*task;
952 	kproject_t	*proj;
953 	zone_t		*zone;
954 	int		rctlfail = 0;
955 
956 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)
957 		return (-1);	/* no point in starting new processes */
958 
959 	pp = (flags & GETPROC_KERNEL) ? &p0 : curproc;
960 	task = pp->p_task;
961 	proj = task->tk_proj;
962 	zone = pp->p_zone;
963 
964 	mutex_enter(&pp->p_lock);
965 	mutex_enter(&zone->zone_nlwps_lock);
966 	if (proj != proj0p) {
967 		if (task->tk_nprocs >= task->tk_nprocs_ctl)
968 			if (rctl_test(rc_task_nprocs, task->tk_rctls,
969 			    pp, 1, 0) & RCT_DENY)
970 				rctlfail = 1;
971 
972 		if (proj->kpj_nprocs >= proj->kpj_nprocs_ctl)
973 			if (rctl_test(rc_project_nprocs, proj->kpj_rctls,
974 			    pp, 1, 0) & RCT_DENY)
975 				rctlfail = 1;
976 
977 		if (zone->zone_nprocs >= zone->zone_nprocs_ctl)
978 			if (rctl_test(rc_zone_nprocs, zone->zone_rctls,
979 			    pp, 1, 0) & RCT_DENY)
980 				rctlfail = 1;
981 
982 		if (rctlfail) {
983 			mutex_exit(&zone->zone_nlwps_lock);
984 			mutex_exit(&pp->p_lock);
985 			atomic_inc_32(&zone->zone_ffcap);
986 			goto punish;
987 		}
988 	}
989 	task->tk_nprocs++;
990 	proj->kpj_nprocs++;
991 	zone->zone_nprocs++;
992 	mutex_exit(&zone->zone_nlwps_lock);
993 	mutex_exit(&pp->p_lock);
994 
995 	cp = kmem_cache_alloc(process_cache, KM_SLEEP);
996 	bzero(cp, sizeof (proc_t));
997 
998 	/*
999 	 * Make proc entry for child process
1000 	 */
1001 	mutex_init(&cp->p_splock, NULL, MUTEX_DEFAULT, NULL);
1002 	mutex_init(&cp->p_crlock, NULL, MUTEX_DEFAULT, NULL);
1003 	mutex_init(&cp->p_pflock, NULL, MUTEX_DEFAULT, NULL);
1004 #if defined(__x86)
1005 	mutex_init(&cp->p_ldtlock, NULL, MUTEX_DEFAULT, NULL);
1006 #endif
1007 	mutex_init(&cp->p_maplock, NULL, MUTEX_DEFAULT, NULL);
1008 	cp->p_stat = SIDL;
1009 	cp->p_mstart = gethrtime();
1010 	cp->p_as = &kas;
1011 	/*
1012 	 * p_zone must be set before we call pid_allocate since the process
1013 	 * will be visible after that and code such as prfind_zone will
1014 	 * look at the p_zone field.
1015 	 */
1016 	cp->p_zone = pp->p_zone;
1017 	cp->p_t1_lgrpid = LGRP_NONE;
1018 	cp->p_tr_lgrpid = LGRP_NONE;
1019 
1020 	if ((newpid = pid_allocate(cp, pid, PID_ALLOC_PROC)) == -1) {
1021 		if (nproc == v.v_proc) {
1022 			CPU_STATS_ADDQ(CPU, sys, procovf, 1);
1023 			cmn_err(CE_WARN, "out of processes");
1024 		}
1025 		goto bad;
1026 	}
1027 
1028 	mutex_enter(&pp->p_lock);
1029 	cp->p_exec = pp->p_exec;
1030 	cp->p_execdir = pp->p_execdir;
1031 	mutex_exit(&pp->p_lock);
1032 
1033 	if (cp->p_exec) {
1034 		VN_HOLD(cp->p_exec);
1035 		/*
1036 		 * Each VOP_OPEN() must be paired with a corresponding
1037 		 * VOP_CLOSE(). In this case, the executable will be
1038 		 * closed for the child in either proc_exit() or gexec().
1039 		 */
1040 		if (VOP_OPEN(&cp->p_exec, FREAD, CRED(), NULL) != 0) {
1041 			VN_RELE(cp->p_exec);
1042 			cp->p_exec = NULLVP;
1043 			cp->p_execdir = NULLVP;
1044 			goto bad;
1045 		}
1046 	}
1047 	if (cp->p_execdir)
1048 		VN_HOLD(cp->p_execdir);
1049 
1050 	/*
1051 	 * If not privileged make sure that this user hasn't exceeded
1052 	 * v.v_maxup processes, and that users collectively haven't
1053 	 * exceeded v.v_maxupttl processes.
1054 	 */
1055 	mutex_enter(&pidlock);
1056 	ASSERT(nproc < v.v_proc);	/* otherwise how'd we get our pid? */
1057 	cr = CRED();
1058 	ruid = crgetruid(cr);
1059 	zoneid = crgetzoneid(cr);
1060 	if (nproc >= v.v_maxup && 	/* short-circuit; usually false */
1061 	    (nproc >= v.v_maxupttl ||
1062 	    upcount_get(ruid, zoneid) >= v.v_maxup) &&
1063 	    secpolicy_newproc(cr) != 0) {
1064 		mutex_exit(&pidlock);
1065 		zcmn_err(zoneid, CE_NOTE,
1066 		    "out of per-user processes for uid %d", ruid);
1067 		goto bad;
1068 	}
1069 
1070 	/*
1071 	 * Everything is cool, put the new proc on the active process list.
1072 	 * It is already on the pid list and in /proc.
1073 	 * Increment the per uid process count (upcount).
1074 	 */
1075 	nproc++;
1076 	upcount_inc(ruid, zoneid);
1077 
1078 	cp->p_next = practive;
1079 	practive->p_prev = cp;
1080 	practive = cp;
1081 
1082 	cp->p_ignore = pp->p_ignore;
1083 	cp->p_siginfo = pp->p_siginfo;
1084 	cp->p_flag = pp->p_flag & (SJCTL|SNOWAIT|SNOCD);
1085 	cp->p_sessp = pp->p_sessp;
1086 	sess_hold(pp);
1087 	cp->p_brand = pp->p_brand;
1088 	if (PROC_IS_BRANDED(pp))
1089 		BROP(pp)->b_copy_procdata(cp, pp);
1090 	cp->p_bssbase = pp->p_bssbase;
1091 	cp->p_brkbase = pp->p_brkbase;
1092 	cp->p_brksize = pp->p_brksize;
1093 	cp->p_brkpageszc = pp->p_brkpageszc;
1094 	cp->p_stksize = pp->p_stksize;
1095 	cp->p_stkpageszc = pp->p_stkpageszc;
1096 	cp->p_stkprot = pp->p_stkprot;
1097 	cp->p_datprot = pp->p_datprot;
1098 	cp->p_usrstack = pp->p_usrstack;
1099 	cp->p_model = pp->p_model;
1100 	cp->p_ppid = pp->p_pid;
1101 	cp->p_ancpid = pp->p_pid;
1102 	cp->p_portcnt = pp->p_portcnt;
1103 	/*
1104 	 * Security flags are preserved on fork, the inherited copy come into
1105 	 * effect on exec
1106 	 */
1107 	cp->p_secflags = pp->p_secflags;
1108 
1109 	/*
1110 	 * Initialize watchpoint structures
1111 	 */
1112 	avl_create(&cp->p_warea, wa_compare, sizeof (struct watched_area),
1113 	    offsetof(struct watched_area, wa_link));
1114 
1115 	/*
1116 	 * Initialize immediate resource control values.
1117 	 */
1118 	cp->p_stk_ctl = pp->p_stk_ctl;
1119 	cp->p_fsz_ctl = pp->p_fsz_ctl;
1120 	cp->p_vmem_ctl = pp->p_vmem_ctl;
1121 	cp->p_fno_ctl = pp->p_fno_ctl;
1122 
1123 	/*
1124 	 * Link up to parent-child-sibling chain.  No need to lock
1125 	 * in general since only a call to freeproc() (done by the
1126 	 * same parent as newproc()) diddles with the child chain.
1127 	 */
1128 	cp->p_sibling = pp->p_child;
1129 	if (pp->p_child)
1130 		pp->p_child->p_psibling = cp;
1131 
1132 	cp->p_parent = pp;
1133 	pp->p_child = cp;
1134 
1135 	cp->p_child_ns = NULL;
1136 	cp->p_sibling_ns = NULL;
1137 
1138 	cp->p_nextorph = pp->p_orphan;
1139 	cp->p_nextofkin = pp;
1140 	pp->p_orphan = cp;
1141 
1142 	/*
1143 	 * Inherit profiling state; do not inherit REALPROF profiling state.
1144 	 */
1145 	cp->p_prof = pp->p_prof;
1146 	cp->p_rprof_cyclic = CYCLIC_NONE;
1147 
1148 	/*
1149 	 * Inherit pool pointer from the parent.  Kernel processes are
1150 	 * always bound to the default pool.
1151 	 */
1152 	mutex_enter(&pp->p_lock);
1153 	if (flags & GETPROC_KERNEL) {
1154 		cp->p_pool = pool_default;
1155 		cp->p_flag |= SSYS;
1156 	} else {
1157 		cp->p_pool = pp->p_pool;
1158 	}
1159 	atomic_inc_32(&cp->p_pool->pool_ref);
1160 	mutex_exit(&pp->p_lock);
1161 
1162 	/*
1163 	 * Add the child process to the current task.  Kernel processes
1164 	 * are always attached to task0.
1165 	 */
1166 	mutex_enter(&cp->p_lock);
1167 	if (flags & GETPROC_KERNEL)
1168 		task_attach(task0p, cp);
1169 	else
1170 		task_attach(pp->p_task, cp);
1171 	mutex_exit(&cp->p_lock);
1172 	mutex_exit(&pidlock);
1173 
1174 	avl_create(&cp->p_ct_held, contract_compar, sizeof (contract_t),
1175 	    offsetof(contract_t, ct_ctlist));
1176 
1177 	/*
1178 	 * Duplicate any audit information kept in the process table
1179 	 */
1180 	if (audit_active)	/* copy audit data to cp */
1181 		audit_newproc(cp);
1182 
1183 	crhold(cp->p_cred = cr);
1184 
1185 	/*
1186 	 * Bump up the counts on the file structures pointed at by the
1187 	 * parent's file table since the child will point at them too.
1188 	 */
1189 	fcnt_add(P_FINFO(pp), 1);
1190 
1191 	if (PTOU(pp)->u_cdir) {
1192 		VN_HOLD(PTOU(pp)->u_cdir);
1193 	} else {
1194 		ASSERT(pp == &p0);
1195 		/*
1196 		 * We must be at or before vfs_mountroot(); it will take care of
1197 		 * assigning our current directory.
1198 		 */
1199 	}
1200 	if (PTOU(pp)->u_rdir)
1201 		VN_HOLD(PTOU(pp)->u_rdir);
1202 	if (PTOU(pp)->u_cwd)
1203 		refstr_hold(PTOU(pp)->u_cwd);
1204 
1205 	/*
1206 	 * copy the parent's uarea.
1207 	 */
1208 	uarea = PTOU(cp);
1209 	bcopy(PTOU(pp), uarea, sizeof (*uarea));
1210 	flist_fork(P_FINFO(pp), P_FINFO(cp));
1211 
1212 	gethrestime(&uarea->u_start);
1213 	uarea->u_ticks = ddi_get_lbolt();
1214 	uarea->u_mem = rm_asrss(pp->p_as);
1215 	uarea->u_acflag = AFORK;
1216 
1217 	/*
1218 	 * If inherit-on-fork, copy /proc tracing flags to child.
1219 	 */
1220 	if ((pp->p_proc_flag & P_PR_FORK) != 0) {
1221 		cp->p_proc_flag |= pp->p_proc_flag & (P_PR_TRACE|P_PR_FORK);
1222 		cp->p_sigmask = pp->p_sigmask;
1223 		cp->p_fltmask = pp->p_fltmask;
1224 	} else {
1225 		sigemptyset(&cp->p_sigmask);
1226 		premptyset(&cp->p_fltmask);
1227 		uarea->u_systrap = 0;
1228 		premptyset(&uarea->u_entrymask);
1229 		premptyset(&uarea->u_exitmask);
1230 	}
1231 	/*
1232 	 * If microstate accounting is being inherited, mark child
1233 	 */
1234 	if ((pp->p_flag & SMSFORK) != 0)
1235 		cp->p_flag |= pp->p_flag & (SMSFORK|SMSACCT);
1236 
1237 	/*
1238 	 * Inherit fixalignment flag from the parent
1239 	 */
1240 	cp->p_fixalignment = pp->p_fixalignment;
1241 
1242 	*cpp = cp;
1243 	return (0);
1244 
1245 bad:
1246 	ASSERT(MUTEX_NOT_HELD(&pidlock));
1247 
1248 	mutex_destroy(&cp->p_crlock);
1249 	mutex_destroy(&cp->p_pflock);
1250 #if defined(__x86)
1251 	mutex_destroy(&cp->p_ldtlock);
1252 #endif
1253 	if (newpid != -1) {
1254 		proc_entry_free(cp->p_pidp);
1255 		(void) pid_rele(cp->p_pidp);
1256 	}
1257 	kmem_cache_free(process_cache, cp);
1258 
1259 	mutex_enter(&zone->zone_nlwps_lock);
1260 	task->tk_nprocs--;
1261 	proj->kpj_nprocs--;
1262 	zone->zone_nprocs--;
1263 	mutex_exit(&zone->zone_nlwps_lock);
1264 	atomic_inc_32(&zone->zone_ffnoproc);
1265 
1266 punish:
1267 	/*
1268 	 * We most likely got into this situation because some process is
1269 	 * forking out of control.  As punishment, put it to sleep for a
1270 	 * bit so it can't eat the machine alive.  Sleep interval is chosen
1271 	 * to allow no more than one fork failure per cpu per clock tick
1272 	 * on average (yes, I just made this up).  This has two desirable
1273 	 * properties: (1) it sets a constant limit on the fork failure
1274 	 * rate, and (2) the busier the system is, the harsher the penalty
1275 	 * for abusing it becomes.
1276 	 */
1277 	INCR_COUNT(&fork_fail_pending, &pidlock);
1278 	delay(fork_fail_pending / ncpus + 1);
1279 	DECR_COUNT(&fork_fail_pending, &pidlock);
1280 
1281 	return (-1); /* out of memory or proc slots */
1282 }
1283 
1284 /*
1285  * Release virtual memory.
1286  * In the case of vfork(), the child was given exclusive access to its
1287  * parent's address space.  The parent is waiting in vfwait() for the
1288  * child to release its exclusive claim via relvm().
1289  */
1290 void
1291 relvm()
1292 {
1293 	proc_t *p = curproc;
1294 
1295 	ASSERT((unsigned)p->p_lwpcnt <= 1);
1296 
1297 	prrelvm();	/* inform /proc */
1298 
1299 	if (p->p_flag & SVFORK) {
1300 		proc_t *pp = p->p_parent;
1301 		/*
1302 		 * The child process is either exec'ing or exit'ing.
1303 		 * The child is now separated from the parent's address
1304 		 * space.  The parent process is made dispatchable.
1305 		 *
1306 		 * This is a delicate locking maneuver, involving
1307 		 * both the parent's p_lock and the child's p_lock.
1308 		 * As soon as the SVFORK flag is turned off, the
1309 		 * parent is free to run, but it must not run until
1310 		 * we wake it up using its p_cv because it might
1311 		 * exit and we would be referencing invalid memory.
1312 		 * Therefore, we hold the parent with its p_lock
1313 		 * while protecting our p_flags with our own p_lock.
1314 		 */
1315 try_again:
1316 		mutex_enter(&p->p_lock);	/* grab child's lock first */
1317 		prbarrier(p);		/* make sure /proc is blocked out */
1318 		mutex_enter(&pp->p_lock);
1319 
1320 		/*
1321 		 * Check if parent is locked by /proc.
1322 		 */
1323 		if (pp->p_proc_flag & P_PR_LOCK) {
1324 			/*
1325 			 * Delay until /proc is done with the parent.
1326 			 * We must drop our (the child's) p->p_lock, wait
1327 			 * via prbarrier() on the parent, then start over.
1328 			 */
1329 			mutex_exit(&p->p_lock);
1330 			prbarrier(pp);
1331 			mutex_exit(&pp->p_lock);
1332 			goto try_again;
1333 		}
1334 		p->p_flag &= ~SVFORK;
1335 		kpreempt_disable();
1336 		p->p_as = &kas;
1337 
1338 		/*
1339 		 * notify hat of change in thread's address space
1340 		 */
1341 		hat_thread_exit(curthread);
1342 		kpreempt_enable();
1343 
1344 		/*
1345 		 * child sizes are copied back to parent because
1346 		 * child may have grown.
1347 		 */
1348 		pp->p_brkbase = p->p_brkbase;
1349 		pp->p_brksize = p->p_brksize;
1350 		pp->p_stksize = p->p_stksize;
1351 
1352 		/*
1353 		 * Copy back the shm accounting information
1354 		 * to the parent process.
1355 		 */
1356 		pp->p_segacct = p->p_segacct;
1357 		p->p_segacct = NULL;
1358 
1359 		/*
1360 		 * The parent is no longer waiting for the vfork()d child.
1361 		 * Restore the parent's watched pages, if any.  This is
1362 		 * safe because we know the parent is not locked by /proc
1363 		 */
1364 		pp->p_flag &= ~SVFWAIT;
1365 		if (avl_numnodes(&pp->p_wpage) != 0) {
1366 			pp->p_as->a_wpage = pp->p_wpage;
1367 			avl_create(&pp->p_wpage, wp_compare,
1368 			    sizeof (struct watched_page),
1369 			    offsetof(struct watched_page, wp_link));
1370 		}
1371 		cv_signal(&pp->p_cv);
1372 		mutex_exit(&pp->p_lock);
1373 		mutex_exit(&p->p_lock);
1374 	} else {
1375 		if (p->p_as != &kas) {
1376 			struct as *as;
1377 
1378 			if (p->p_segacct)
1379 				shmexit(p);
1380 
1381 			/*
1382 			 * We grab p_lock for the benefit of /proc
1383 			 */
1384 			kpreempt_disable();
1385 			mutex_enter(&p->p_lock);
1386 			prbarrier(p);	/* make sure /proc is blocked out */
1387 			as = p->p_as;
1388 			p->p_as = &kas;
1389 			mutex_exit(&p->p_lock);
1390 
1391 			/*
1392 			 * notify hat of change in thread's address space
1393 			 */
1394 			hat_thread_exit(curthread);
1395 			kpreempt_enable();
1396 
1397 			as_free(as);
1398 			p->p_tr_lgrpid = LGRP_NONE;
1399 		}
1400 	}
1401 }
1402 
1403 /*
1404  * Wait for child to exec or exit.
1405  * Called by parent of vfork'ed process.
1406  * See important comments in relvm(), above.
1407  */
1408 void
1409 vfwait(pid_t pid)
1410 {
1411 	int signalled = 0;
1412 	proc_t *pp = ttoproc(curthread);
1413 	proc_t *cp;
1414 
1415 	/*
1416 	 * Wait for child to exec or exit.
1417 	 */
1418 	for (;;) {
1419 		mutex_enter(&pidlock);
1420 		cp = prfind(pid);
1421 		if (cp == NULL || cp->p_parent != pp) {
1422 			/*
1423 			 * Child has exit()ed.
1424 			 */
1425 			mutex_exit(&pidlock);
1426 			break;
1427 		}
1428 		/*
1429 		 * Grab the child's p_lock before releasing pidlock.
1430 		 * Otherwise, the child could exit and we would be
1431 		 * referencing invalid memory.
1432 		 */
1433 		mutex_enter(&cp->p_lock);
1434 		mutex_exit(&pidlock);
1435 		if (!(cp->p_flag & SVFORK)) {
1436 			/*
1437 			 * Child has exec()ed or is exit()ing.
1438 			 */
1439 			mutex_exit(&cp->p_lock);
1440 			break;
1441 		}
1442 		mutex_enter(&pp->p_lock);
1443 		mutex_exit(&cp->p_lock);
1444 		/*
1445 		 * We might be waked up spuriously from the cv_wait().
1446 		 * We have to do the whole operation over again to be
1447 		 * sure the child's SVFORK flag really is turned off.
1448 		 * We cannot make reference to the child because it can
1449 		 * exit before we return and we would be referencing
1450 		 * invalid memory.
1451 		 *
1452 		 * Because this is potentially a very long-term wait,
1453 		 * we call cv_wait_sig() (for its jobcontrol and /proc
1454 		 * side-effects) unless there is a current signal, in
1455 		 * which case we use cv_wait() because we cannot return
1456 		 * from this function until the child has released the
1457 		 * address space.  Calling cv_wait_sig() with a current
1458 		 * signal would lead to an indefinite loop here because
1459 		 * cv_wait_sig() returns immediately in this case.
1460 		 */
1461 		if (signalled)
1462 			cv_wait(&pp->p_cv, &pp->p_lock);
1463 		else
1464 			signalled = !cv_wait_sig(&pp->p_cv, &pp->p_lock);
1465 		mutex_exit(&pp->p_lock);
1466 	}
1467 
1468 	/* restore watchpoints to parent */
1469 	if (pr_watch_active(pp)) {
1470 		struct as *as = pp->p_as;
1471 		AS_LOCK_ENTER(as, RW_WRITER);
1472 		as_setwatch(as);
1473 		AS_LOCK_EXIT(as);
1474 	}
1475 
1476 	mutex_enter(&pp->p_lock);
1477 	prbarrier(pp);	/* barrier against /proc locking */
1478 	continuelwps(pp);
1479 	mutex_exit(&pp->p_lock);
1480 }
1481