xref: /titanic_53/usr/src/uts/common/os/pid.c (revision 97eda132fb49582e04504d6a221b98750a14f5b3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*97eda132Sraf 
237c478bd9Sstevel@tonic-gate /*
24*97eda132Sraf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
407c478bd9Sstevel@tonic-gate #include <sys/var.h>
417c478bd9Sstevel@tonic-gate #include <sys/cred.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
447c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
457c478bd9Sstevel@tonic-gate #include <sys/session.h>
467c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
477c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
487c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
497c478bd9Sstevel@tonic-gate #include <sys/debug.h>
507c478bd9Sstevel@tonic-gate #include <c2/audit.h>
517c478bd9Sstevel@tonic-gate #include <sys/zone.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* directory entries for /proc */
547c478bd9Sstevel@tonic-gate union procent {
557c478bd9Sstevel@tonic-gate 	proc_t *pe_proc;
567c478bd9Sstevel@tonic-gate 	union procent *pe_next;
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct pid pid0 = {
607c478bd9Sstevel@tonic-gate 	0,		/* pid_prinactive */
617c478bd9Sstevel@tonic-gate 	1,		/* pid_pgorphaned */
627c478bd9Sstevel@tonic-gate 	0,		/* pid_padding	*/
637c478bd9Sstevel@tonic-gate 	0,		/* pid_prslot	*/
647c478bd9Sstevel@tonic-gate 	0,		/* pid_id	*/
657c478bd9Sstevel@tonic-gate 	NULL,		/* pid_pglink	*/
667c478bd9Sstevel@tonic-gate 	NULL,		/* pid_link	*/
677c478bd9Sstevel@tonic-gate 	3		/* pid_ref	*/
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int pid_hashlen = 4;	/* desired average hash chain length */
717c478bd9Sstevel@tonic-gate static int pid_hashsz;		/* number of buckets in the hash table */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	HASHPID(pid)	(pidhash[((pid)&(pid_hashsz-1))])
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate extern uint_t nproc;
767c478bd9Sstevel@tonic-gate extern struct kmem_cache *process_cache;
777c478bd9Sstevel@tonic-gate static void	upcount_init(void);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate kmutex_t	pidlock;	/* global process lock */
807c478bd9Sstevel@tonic-gate kmutex_t	pr_pidlock;	/* /proc global process lock */
817c478bd9Sstevel@tonic-gate kcondvar_t	*pr_pid_cv;	/* for /proc, one per process slot */
827c478bd9Sstevel@tonic-gate struct plock	*proc_lock;	/* persistent array of p_lock's */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * See the comment above pid_getlockslot() for a detailed explanation of this
867c478bd9Sstevel@tonic-gate  * constant.  Note that a PLOCK_SHIFT of 3 implies 64-byte coherence
877c478bd9Sstevel@tonic-gate  * granularity; if the coherence granularity is ever changed, this constant
887c478bd9Sstevel@tonic-gate  * should be modified to reflect the change to minimize proc_lock false
897c478bd9Sstevel@tonic-gate  * sharing (correctness, however, is guaranteed regardless of the coherence
907c478bd9Sstevel@tonic-gate  * granularity).
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate #define	PLOCK_SHIFT	3
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static kmutex_t	pidlinklock;
957c478bd9Sstevel@tonic-gate static struct pid **pidhash;
967c478bd9Sstevel@tonic-gate static pid_t minpid;
977c478bd9Sstevel@tonic-gate static pid_t mpid;
987c478bd9Sstevel@tonic-gate static union procent *procdir;
997c478bd9Sstevel@tonic-gate static union procent *procentfree;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static struct pid *
1027c478bd9Sstevel@tonic-gate pid_lookup(pid_t pid)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	struct pid *pidp;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlinklock));
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	for (pidp = HASHPID(pid); pidp; pidp = pidp->pid_link) {
1097c478bd9Sstevel@tonic-gate 		if (pidp->pid_id == pid) {
1107c478bd9Sstevel@tonic-gate 			ASSERT(pidp->pid_ref > 0);
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	return (pidp);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate void
1187c478bd9Sstevel@tonic-gate pid_setmin(void)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	if (jump_pid && jump_pid > mpid)
1217c478bd9Sstevel@tonic-gate 		minpid = mpid = jump_pid;
1227c478bd9Sstevel@tonic-gate 	else
1237c478bd9Sstevel@tonic-gate 		minpid = mpid + 1;
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * When prslots are simply used as an index to determine a process' p_lock,
1287c478bd9Sstevel@tonic-gate  * adjacent prslots share adjacent p_locks.  On machines where the size
1297c478bd9Sstevel@tonic-gate  * of a mutex is smaller than that of a cache line (which, as of this writing,
1307c478bd9Sstevel@tonic-gate  * is true for all machines on which Solaris runs), this can potentially
1317c478bd9Sstevel@tonic-gate  * induce false sharing.  The standard solution for false sharing is to pad
1327c478bd9Sstevel@tonic-gate  * out one's data structures (in this case, struct plock).  However,
1337c478bd9Sstevel@tonic-gate  * given the size and (generally) sparse use of the proc_lock array, this
1347c478bd9Sstevel@tonic-gate  * is suboptimal.  We therefore stride through the proc_lock array with
1357c478bd9Sstevel@tonic-gate  * a stride of PLOCK_SHIFT.  PLOCK_SHIFT should be defined as:
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  *   log_2 (coherence_granularity / sizeof (kmutex_t))
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * Under this scheme, false sharing is still possible -- but only when
1407c478bd9Sstevel@tonic-gate  * the number of active processes is very large.  Note that the one-to-one
1417c478bd9Sstevel@tonic-gate  * mapping between prslots and lockslots is maintained.
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate static int
1447c478bd9Sstevel@tonic-gate pid_getlockslot(int prslot)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate 	int even = (v.v_proc >> PLOCK_SHIFT) << PLOCK_SHIFT;
1477c478bd9Sstevel@tonic-gate 	int perlap = even >> PLOCK_SHIFT;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (prslot >= even)
1507c478bd9Sstevel@tonic-gate 		return (prslot);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	return (((prslot % perlap) << PLOCK_SHIFT) + (prslot / perlap));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * This function assigns a pid for use in a fork request.  It allocates
1577c478bd9Sstevel@tonic-gate  * a pid structure, tries to find an empty slot in the proc table,
1587c478bd9Sstevel@tonic-gate  * and selects the process id.
1597c478bd9Sstevel@tonic-gate  *
1607c478bd9Sstevel@tonic-gate  * pid_assign() returns the new pid on success, -1 on failure.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate pid_t
1637c478bd9Sstevel@tonic-gate pid_assign(proc_t *prp)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	struct pid *pidp;
1667c478bd9Sstevel@tonic-gate 	union procent *pep;
1677c478bd9Sstevel@tonic-gate 	pid_t newpid, startpid;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	pidp = kmem_zalloc(sizeof (struct pid), KM_SLEEP);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
1727c478bd9Sstevel@tonic-gate 	if ((pep = procentfree) == NULL) {
1737c478bd9Sstevel@tonic-gate 		/*
1747c478bd9Sstevel@tonic-gate 		 * ran out of /proc directory entries
1757c478bd9Sstevel@tonic-gate 		 */
1767c478bd9Sstevel@tonic-gate 		goto failed;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * Allocate a pid
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	startpid = mpid;
1837c478bd9Sstevel@tonic-gate 	do  {
1847c478bd9Sstevel@tonic-gate 		newpid = (++mpid == maxpid ? mpid = minpid : mpid);
1857c478bd9Sstevel@tonic-gate 	} while (pid_lookup(newpid) && newpid != startpid);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (newpid == startpid && pid_lookup(newpid)) {
1887c478bd9Sstevel@tonic-gate 		/* couldn't find a free pid */
1897c478bd9Sstevel@tonic-gate 		goto failed;
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	procentfree = pep->pe_next;
1937c478bd9Sstevel@tonic-gate 	pep->pe_proc = prp;
1947c478bd9Sstevel@tonic-gate 	prp->p_pidp = pidp;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Put pid into the pid hash table.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	pidp->pid_link = HASHPID(newpid);
2007c478bd9Sstevel@tonic-gate 	HASHPID(newpid) = pidp;
2017c478bd9Sstevel@tonic-gate 	pidp->pid_ref = 1;
2027c478bd9Sstevel@tonic-gate 	pidp->pid_id = newpid;
2037c478bd9Sstevel@tonic-gate 	pidp->pid_prslot = pep - procdir;
2047c478bd9Sstevel@tonic-gate 	prp->p_lockp = &proc_lock[pid_getlockslot(pidp->pid_prslot)];
2057c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (newpid);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate failed:
2107c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2117c478bd9Sstevel@tonic-gate 	kmem_free(pidp, sizeof (struct pid));
2127c478bd9Sstevel@tonic-gate 	return (-1);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * decrement the reference count for pid
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate int
2197c478bd9Sstevel@tonic-gate pid_rele(struct pid *pidp)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	struct pid **pidpp;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
2247c478bd9Sstevel@tonic-gate 	ASSERT(pidp != &pid0);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	pidpp = &HASHPID(pidp->pid_id);
2277c478bd9Sstevel@tonic-gate 	for (;;) {
2287c478bd9Sstevel@tonic-gate 		ASSERT(*pidpp != NULL);
2297c478bd9Sstevel@tonic-gate 		if (*pidpp == pidp)
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 		pidpp = &(*pidpp)->pid_link;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	*pidpp = pidp->pid_link;
2357c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	kmem_free(pidp, sizeof (*pidp));
2387c478bd9Sstevel@tonic-gate 	return (0);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate void
2427c478bd9Sstevel@tonic-gate proc_entry_free(struct pid *pidp)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
2457c478bd9Sstevel@tonic-gate 	pidp->pid_prinactive = 1;
2467c478bd9Sstevel@tonic-gate 	procdir[pidp->pid_prslot].pe_next = procentfree;
2477c478bd9Sstevel@tonic-gate 	procentfree = &procdir[pidp->pid_prslot];
2487c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate void
2527c478bd9Sstevel@tonic-gate pid_exit(proc_t *prp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	struct pid *pidp;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * Exit process group.  If it is NULL, it's because fork failed
2607c478bd9Sstevel@tonic-gate 	 * before calling pgjoin().
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	ASSERT(prp->p_pgidp != NULL || prp->p_stat == SIDL);
2637c478bd9Sstevel@tonic-gate 	if (prp->p_pgidp != NULL)
2647c478bd9Sstevel@tonic-gate 		pgexit(prp);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	SESS_RELE(prp->p_sessp);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	pidp = prp->p_pidp;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	proc_entry_free(pidp);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
2737c478bd9Sstevel@tonic-gate 	if (audit_active)
2747c478bd9Sstevel@tonic-gate 		audit_pfree(prp);
2757c478bd9Sstevel@tonic-gate #endif
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (practive == prp) {
2787c478bd9Sstevel@tonic-gate 		practive = prp->p_next;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (prp->p_next) {
2827c478bd9Sstevel@tonic-gate 		prp->p_next->p_prev = prp->p_prev;
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 	if (prp->p_prev) {
2857c478bd9Sstevel@tonic-gate 		prp->p_prev->p_next = prp->p_next;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	PID_RELE(pidp);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	mutex_destroy(&prp->p_crlock);
2917c478bd9Sstevel@tonic-gate 	kmem_cache_free(process_cache, prp);
2927c478bd9Sstevel@tonic-gate 	nproc--;
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * Find a process visible from the specified zone given its process ID.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate proc_t *
2997c478bd9Sstevel@tonic-gate prfind_zone(pid_t pid, zoneid_t zoneid)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	struct pid *pidp;
3027c478bd9Sstevel@tonic-gate 	proc_t *p;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
3077c478bd9Sstevel@tonic-gate 	pidp = pid_lookup(pid);
3087c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
3097c478bd9Sstevel@tonic-gate 	if (pidp != NULL && pidp->pid_prinactive == 0) {
3107c478bd9Sstevel@tonic-gate 		p = procdir[pidp->pid_prslot].pe_proc;
3117c478bd9Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid)
3127c478bd9Sstevel@tonic-gate 			return (p);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 	return (NULL);
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * Find a process given its process ID.  This obeys zone restrictions,
3197c478bd9Sstevel@tonic-gate  * so if the caller is in a non-global zone it won't find processes
3207c478bd9Sstevel@tonic-gate  * associated with other zones.  Use prfind_zone(pid, ALL_ZONES) to
3217c478bd9Sstevel@tonic-gate  * bypass this restriction.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate proc_t *
3247c478bd9Sstevel@tonic-gate prfind(pid_t pid)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3297c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3307c478bd9Sstevel@tonic-gate 	else
3317c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
3327c478bd9Sstevel@tonic-gate 	return (prfind_zone(pid, zoneid));
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate proc_t *
3367c478bd9Sstevel@tonic-gate pgfind_zone(pid_t pgid, zoneid_t zoneid)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	struct pid *pidp;
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
3437c478bd9Sstevel@tonic-gate 	pidp = pid_lookup(pgid);
3447c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
3457c478bd9Sstevel@tonic-gate 	if (pidp != NULL) {
3467c478bd9Sstevel@tonic-gate 		proc_t *p = pidp->pid_pglink;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || pgid == 0 || p == NULL ||
3497c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_id == zoneid)
3507c478bd9Sstevel@tonic-gate 			return (p);
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 	return (NULL);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * return the head of the list of processes whose process group ID is 'pgid',
3577c478bd9Sstevel@tonic-gate  * or NULL, if no such process group
3587c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate proc_t *
3607c478bd9Sstevel@tonic-gate pgfind(pid_t pgid)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3657c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3667c478bd9Sstevel@tonic-gate 	else
3677c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
3687c478bd9Sstevel@tonic-gate 	return (pgfind_zone(pgid, zoneid));
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * If pid exists, find its proc, acquire its p_lock and mark it P_PR_LOCK.
3737c478bd9Sstevel@tonic-gate  * Returns the proc pointer on success, NULL on failure.  sprlock() is
3747c478bd9Sstevel@tonic-gate  * really just a stripped-down version of pr_p_lock() to allow practive
3757c478bd9Sstevel@tonic-gate  * walkers like dofusers() and dumpsys() to synchronize with /proc.
3767c478bd9Sstevel@tonic-gate  */
3777c478bd9Sstevel@tonic-gate proc_t *
3787c478bd9Sstevel@tonic-gate sprlock_zone(pid_t pid, zoneid_t zoneid)
3797c478bd9Sstevel@tonic-gate {
3807c478bd9Sstevel@tonic-gate 	proc_t *p;
3817c478bd9Sstevel@tonic-gate 	kmutex_t *mp;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	for (;;) {
3847c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
3857c478bd9Sstevel@tonic-gate 		if ((p = prfind_zone(pid, zoneid)) == NULL) {
3867c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
3877c478bd9Sstevel@tonic-gate 			return (NULL);
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 		/*
3907c478bd9Sstevel@tonic-gate 		 * p_lock is persistent, but p itself is not -- it could
3917c478bd9Sstevel@tonic-gate 		 * vanish during cv_wait().  Load p->p_lock now so we can
3927c478bd9Sstevel@tonic-gate 		 * drop it after cv_wait() without referencing p.
3937c478bd9Sstevel@tonic-gate 		 */
3947c478bd9Sstevel@tonic-gate 		mp = &p->p_lock;
3957c478bd9Sstevel@tonic-gate 		mutex_enter(mp);
3967c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
3977c478bd9Sstevel@tonic-gate 		/*
3987c478bd9Sstevel@tonic-gate 		 * If the process is in some half-baked state, fail.
3997c478bd9Sstevel@tonic-gate 		 */
4007c478bd9Sstevel@tonic-gate 		if (p->p_stat == SZOMB || p->p_stat == SIDL ||
401*97eda132Sraf 		    (p->p_flag & (SEXITING | SEXITLWPS))) {
4027c478bd9Sstevel@tonic-gate 			mutex_exit(mp);
4037c478bd9Sstevel@tonic-gate 			return (NULL);
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 		if (panicstr)
4067c478bd9Sstevel@tonic-gate 			return (p);
4077c478bd9Sstevel@tonic-gate 		if (!(p->p_proc_flag & P_PR_LOCK))
4087c478bd9Sstevel@tonic-gate 			break;
4097c478bd9Sstevel@tonic-gate 		cv_wait(&pr_pid_cv[p->p_slot], mp);
4107c478bd9Sstevel@tonic-gate 		mutex_exit(mp);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 	p->p_proc_flag |= P_PR_LOCK;
4137c478bd9Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();
4147c478bd9Sstevel@tonic-gate 	return (p);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate proc_t *
4187c478bd9Sstevel@tonic-gate sprlock(pid_t pid)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
4237c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
4247c478bd9Sstevel@tonic-gate 	else
4257c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
4267c478bd9Sstevel@tonic-gate 	return (sprlock_zone(pid, zoneid));
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate void
4307c478bd9Sstevel@tonic-gate sprlock_proc(proc_t *p)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	while (p->p_proc_flag & P_PR_LOCK) {
4357c478bd9Sstevel@tonic-gate 		cv_wait(&pr_pid_cv[p->p_slot], &p->p_lock);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	p->p_proc_flag |= P_PR_LOCK;
4397c478bd9Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate void
4437c478bd9Sstevel@tonic-gate sprunlock(proc_t *p)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	if (panicstr) {
4467c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
4477c478bd9Sstevel@tonic-gate 		return;
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
4517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	cv_signal(&pr_pid_cv[p->p_slot]);
4547c478bd9Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_LOCK;
4557c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
4567c478bd9Sstevel@tonic-gate 	THREAD_KPRI_RELEASE();
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate void
4607c478bd9Sstevel@tonic-gate pid_init(void)
4617c478bd9Sstevel@tonic-gate {
4627c478bd9Sstevel@tonic-gate 	int i;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	pid_hashsz = 1 << highbit(v.v_proc / pid_hashlen);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	pidhash = kmem_zalloc(sizeof (struct pid *) * pid_hashsz, KM_SLEEP);
4677c478bd9Sstevel@tonic-gate 	procdir = kmem_alloc(sizeof (union procent) * v.v_proc, KM_SLEEP);
4687c478bd9Sstevel@tonic-gate 	pr_pid_cv = kmem_zalloc(sizeof (kcondvar_t) * v.v_proc, KM_SLEEP);
4697c478bd9Sstevel@tonic-gate 	proc_lock = kmem_zalloc(sizeof (struct plock) * v.v_proc, KM_SLEEP);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	nproc = 1;
4727c478bd9Sstevel@tonic-gate 	practive = proc_sched;
4737c478bd9Sstevel@tonic-gate 	proc_sched->p_next = NULL;
4747c478bd9Sstevel@tonic-gate 	procdir[0].pe_proc = proc_sched;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	procentfree = &procdir[1];
4777c478bd9Sstevel@tonic-gate 	for (i = 1; i < v.v_proc - 1; i++)
4787c478bd9Sstevel@tonic-gate 		procdir[i].pe_next = &procdir[i+1];
4797c478bd9Sstevel@tonic-gate 	procdir[i].pe_next = NULL;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	HASHPID(0) = &pid0;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	upcount_init();
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate proc_t *
4877c478bd9Sstevel@tonic-gate pid_entry(int slot)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	union procent *pep;
4907c478bd9Sstevel@tonic-gate 	proc_t *prp;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
4937c478bd9Sstevel@tonic-gate 	ASSERT(slot >= 0 && slot < v.v_proc);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	pep = procdir[slot].pe_next;
4967c478bd9Sstevel@tonic-gate 	if (pep >= procdir && pep < &procdir[v.v_proc])
4977c478bd9Sstevel@tonic-gate 		return (NULL);
4987c478bd9Sstevel@tonic-gate 	prp = procdir[slot].pe_proc;
4997c478bd9Sstevel@tonic-gate 	if (prp != 0 && prp->p_stat == SIDL)
5007c478bd9Sstevel@tonic-gate 		return (NULL);
5017c478bd9Sstevel@tonic-gate 	return (prp);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate  * Send the specified signal to all processes whose process group ID is
5067c478bd9Sstevel@tonic-gate  * equal to 'pgid'
5077c478bd9Sstevel@tonic-gate  */
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate void
5107c478bd9Sstevel@tonic-gate signal(pid_t pgid, int sig)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct pid *pidp;
5137c478bd9Sstevel@tonic-gate 	proc_t *prp;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
5167c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
5177c478bd9Sstevel@tonic-gate 	if (pgid == 0 || (pidp = pid_lookup(pgid)) == NULL) {
5187c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlinklock);
5197c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5207c478bd9Sstevel@tonic-gate 		return;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
5237c478bd9Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
5247c478bd9Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
5257c478bd9Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
5267c478bd9Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Send the specified signal to the specified process
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate void
5367c478bd9Sstevel@tonic-gate prsignal(struct pid *pidp, int sig)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 	if (!(pidp->pid_prinactive))
5397c478bd9Sstevel@tonic-gate 		psignal(procdir[pidp->pid_prslot].pe_proc, sig);
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * DDI/DKI interfaces for drivers to send signals to processes
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate /*
5497c478bd9Sstevel@tonic-gate  * obtain an opaque reference to a process for signaling
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate void *
5527c478bd9Sstevel@tonic-gate proc_ref(void)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	struct pid *pidp;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
5577c478bd9Sstevel@tonic-gate 	pidp = curproc->p_pidp;
5587c478bd9Sstevel@tonic-gate 	PID_HOLD(pidp);
5597c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	return (pidp);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * release a reference to a process
5667c478bd9Sstevel@tonic-gate  * - a process can exit even if a driver has a reference to it
5677c478bd9Sstevel@tonic-gate  * - one proc_unref for every proc_ref
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate void
5707c478bd9Sstevel@tonic-gate proc_unref(void *pref)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
5737c478bd9Sstevel@tonic-gate 	PID_RELE((struct pid *)pref);
5747c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  * send a signal to a process
5797c478bd9Sstevel@tonic-gate  *
5807c478bd9Sstevel@tonic-gate  * - send the process the signal
5817c478bd9Sstevel@tonic-gate  * - if the process went away, return a -1
5827c478bd9Sstevel@tonic-gate  * - if the process is still there return 0
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate int
5857c478bd9Sstevel@tonic-gate proc_signal(void *pref, int sig)
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	struct pid *pidp = pref;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	prsignal(pidp, sig);
5907c478bd9Sstevel@tonic-gate 	return (pidp->pid_prinactive ? -1 : 0);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate static struct upcount	**upc_hash;	/* a boot time allocated array */
5957c478bd9Sstevel@tonic-gate static ulong_t		upc_hashmask;
5967c478bd9Sstevel@tonic-gate #define	UPC_HASH(x, y)	((ulong_t)(x ^ y) & upc_hashmask)
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * Get us off the ground.  Called once at boot.
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate void
6027c478bd9Sstevel@tonic-gate upcount_init(void)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	ulong_t	upc_hashsize;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/*
6077c478bd9Sstevel@tonic-gate 	 * An entry per MB of memory is our current guess
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * 2^20 is a meg, so shifting right by 20 - PAGESHIFT
6117c478bd9Sstevel@tonic-gate 	 * converts pages to megs (without overflowing a u_int
6127c478bd9Sstevel@tonic-gate 	 * if you have more than 4G of memory, like ptob(physmem)/1M
6137c478bd9Sstevel@tonic-gate 	 * would).
6147c478bd9Sstevel@tonic-gate 	 */
6157c478bd9Sstevel@tonic-gate 	upc_hashsize = (1 << highbit(physmem >> (20 - PAGESHIFT)));
6167c478bd9Sstevel@tonic-gate 	upc_hashmask = upc_hashsize - 1;
6177c478bd9Sstevel@tonic-gate 	upc_hash = kmem_zalloc(upc_hashsize * sizeof (struct upcount *),
6187c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * Increment the number of processes associated with a given uid and zoneid.
6237c478bd9Sstevel@tonic-gate  */
6247c478bd9Sstevel@tonic-gate void
6257c478bd9Sstevel@tonic-gate upcount_inc(uid_t uid, zoneid_t zoneid)
6267c478bd9Sstevel@tonic-gate {
6277c478bd9Sstevel@tonic-gate 	struct upcount	**upc, **hupc;
6287c478bd9Sstevel@tonic-gate 	struct upcount	*new;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6317c478bd9Sstevel@tonic-gate 	new = NULL;
6327c478bd9Sstevel@tonic-gate 	hupc = &upc_hash[UPC_HASH(uid, zoneid)];
6337c478bd9Sstevel@tonic-gate top:
6347c478bd9Sstevel@tonic-gate 	upc = hupc;
6357c478bd9Sstevel@tonic-gate 	while ((*upc) != NULL) {
6367c478bd9Sstevel@tonic-gate 		if ((*upc)->up_uid == uid && (*upc)->up_zoneid == zoneid) {
6377c478bd9Sstevel@tonic-gate 			(*upc)->up_count++;
6387c478bd9Sstevel@tonic-gate 			if (new) {
6397c478bd9Sstevel@tonic-gate 				/*
6407c478bd9Sstevel@tonic-gate 				 * did not need `new' afterall.
6417c478bd9Sstevel@tonic-gate 				 */
6427c478bd9Sstevel@tonic-gate 				kmem_free(new, sizeof (*new));
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 			return;
6457c478bd9Sstevel@tonic-gate 		}
6467c478bd9Sstevel@tonic-gate 		upc = &(*upc)->up_next;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/*
6507c478bd9Sstevel@tonic-gate 	 * There is no entry for this <uid,zoneid> pair.
6517c478bd9Sstevel@tonic-gate 	 * Allocate one.  If we have to drop pidlock, check
6527c478bd9Sstevel@tonic-gate 	 * again.
6537c478bd9Sstevel@tonic-gate 	 */
6547c478bd9Sstevel@tonic-gate 	if (new == NULL) {
6557c478bd9Sstevel@tonic-gate 		new = (struct upcount *)kmem_alloc(sizeof (*new), KM_NOSLEEP);
6567c478bd9Sstevel@tonic-gate 		if (new == NULL) {
6577c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
6587c478bd9Sstevel@tonic-gate 			new = (struct upcount *)kmem_alloc(sizeof (*new),
6597c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
6607c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
6617c478bd9Sstevel@tonic-gate 			goto top;
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	/*
6677c478bd9Sstevel@tonic-gate 	 * On the assumption that a new user is going to do some
6687c478bd9Sstevel@tonic-gate 	 * more forks, put the new upcount structure on the front.
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	upc = hupc;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	new->up_uid = uid;
6737c478bd9Sstevel@tonic-gate 	new->up_zoneid = zoneid;
6747c478bd9Sstevel@tonic-gate 	new->up_count = 1;
6757c478bd9Sstevel@tonic-gate 	new->up_next = *upc;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	*upc = new;
6787c478bd9Sstevel@tonic-gate }
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate /*
6817c478bd9Sstevel@tonic-gate  * Decrement the number of processes a given uid and zoneid has.
6827c478bd9Sstevel@tonic-gate  */
6837c478bd9Sstevel@tonic-gate void
6847c478bd9Sstevel@tonic-gate upcount_dec(uid_t uid, zoneid_t zoneid)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	struct	upcount **upc;
6877c478bd9Sstevel@tonic-gate 	struct	upcount *done;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	upc = &upc_hash[UPC_HASH(uid, zoneid)];
6927c478bd9Sstevel@tonic-gate 	while ((*upc) != NULL) {
6937c478bd9Sstevel@tonic-gate 		if ((*upc)->up_uid == uid && (*upc)->up_zoneid == zoneid) {
6947c478bd9Sstevel@tonic-gate 			(*upc)->up_count--;
6957c478bd9Sstevel@tonic-gate 			if ((*upc)->up_count == 0) {
6967c478bd9Sstevel@tonic-gate 				done = *upc;
6977c478bd9Sstevel@tonic-gate 				*upc = (*upc)->up_next;
6987c478bd9Sstevel@tonic-gate 				kmem_free(done, sizeof (*done));
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 			return;
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 		upc = &(*upc)->up_next;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "decr_upcount-off the end");
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * Returns the number of processes a uid has.
7097c478bd9Sstevel@tonic-gate  * Non-existent uid's are assumed to have no processes.
7107c478bd9Sstevel@tonic-gate  */
7117c478bd9Sstevel@tonic-gate int
7127c478bd9Sstevel@tonic-gate upcount_get(uid_t uid, zoneid_t zoneid)
7137c478bd9Sstevel@tonic-gate {
7147c478bd9Sstevel@tonic-gate 	struct	upcount *upc;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	upc = upc_hash[UPC_HASH(uid, zoneid)];
7197c478bd9Sstevel@tonic-gate 	while (upc != NULL) {
7207c478bd9Sstevel@tonic-gate 		if (upc->up_uid == uid && upc->up_zoneid == zoneid) {
7217c478bd9Sstevel@tonic-gate 			return (upc->up_count);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 		upc = upc->up_next;
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 	return (0);
7267c478bd9Sstevel@tonic-gate }
727