xref: /titanic_44/usr/src/uts/common/os/pid.c (revision 6a634c9dca3093f3922e4b7ab826d7bdf17bf78e)
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
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2197eda132Sraf 
227c478bd9Sstevel@tonic-gate /*
239ff75adeSSurya Prakki  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/proc.h>
337c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
357c478bd9Sstevel@tonic-gate #include <sys/var.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/systm.h>
387c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/session.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
437c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <c2/audit.h>
46*4bcbe6e7SMenno Lageman #include <sys/project.h>
47*4bcbe6e7SMenno Lageman #include <sys/task.h>
487c478bd9Sstevel@tonic-gate #include <sys/zone.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* directory entries for /proc */
517c478bd9Sstevel@tonic-gate union procent {
527c478bd9Sstevel@tonic-gate 	proc_t *pe_proc;
537c478bd9Sstevel@tonic-gate 	union procent *pe_next;
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate struct pid pid0 = {
577c478bd9Sstevel@tonic-gate 	0,		/* pid_prinactive */
587c478bd9Sstevel@tonic-gate 	1,		/* pid_pgorphaned */
597c478bd9Sstevel@tonic-gate 	0,		/* pid_padding	*/
607c478bd9Sstevel@tonic-gate 	0,		/* pid_prslot	*/
617c478bd9Sstevel@tonic-gate 	0,		/* pid_id	*/
627c478bd9Sstevel@tonic-gate 	NULL,		/* pid_pglink	*/
63e44bd21cSsusans 	NULL,		/* pid_pgtail	*/
647c478bd9Sstevel@tonic-gate 	NULL,		/* pid_link	*/
657c478bd9Sstevel@tonic-gate 	3		/* pid_ref	*/
667c478bd9Sstevel@tonic-gate };
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static int pid_hashlen = 4;	/* desired average hash chain length */
697c478bd9Sstevel@tonic-gate static int pid_hashsz;		/* number of buckets in the hash table */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	HASHPID(pid)	(pidhash[((pid)&(pid_hashsz-1))])
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate extern uint_t nproc;
747c478bd9Sstevel@tonic-gate extern struct kmem_cache *process_cache;
757c478bd9Sstevel@tonic-gate static void	upcount_init(void);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate kmutex_t	pidlock;	/* global process lock */
787c478bd9Sstevel@tonic-gate kmutex_t	pr_pidlock;	/* /proc global process lock */
797c478bd9Sstevel@tonic-gate kcondvar_t	*pr_pid_cv;	/* for /proc, one per process slot */
807c478bd9Sstevel@tonic-gate struct plock	*proc_lock;	/* persistent array of p_lock's */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * See the comment above pid_getlockslot() for a detailed explanation of this
847c478bd9Sstevel@tonic-gate  * constant.  Note that a PLOCK_SHIFT of 3 implies 64-byte coherence
857c478bd9Sstevel@tonic-gate  * granularity; if the coherence granularity is ever changed, this constant
867c478bd9Sstevel@tonic-gate  * should be modified to reflect the change to minimize proc_lock false
877c478bd9Sstevel@tonic-gate  * sharing (correctness, however, is guaranteed regardless of the coherence
887c478bd9Sstevel@tonic-gate  * granularity).
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate #define	PLOCK_SHIFT	3
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static kmutex_t	pidlinklock;
937c478bd9Sstevel@tonic-gate static struct pid **pidhash;
947c478bd9Sstevel@tonic-gate static pid_t minpid;
9535a5a358SJonathan Adams static pid_t mpid = FAMOUS_PIDS;	/* one more than the last famous pid */
967c478bd9Sstevel@tonic-gate static union procent *procdir;
977c478bd9Sstevel@tonic-gate static union procent *procentfree;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static struct pid *
pid_lookup(pid_t pid)1007c478bd9Sstevel@tonic-gate pid_lookup(pid_t pid)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	struct pid *pidp;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlinklock));
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	for (pidp = HASHPID(pid); pidp; pidp = pidp->pid_link) {
1077c478bd9Sstevel@tonic-gate 		if (pidp->pid_id == pid) {
1087c478bd9Sstevel@tonic-gate 			ASSERT(pidp->pid_ref > 0);
1097c478bd9Sstevel@tonic-gate 			break;
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	return (pidp);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate void
pid_setmin(void)1167c478bd9Sstevel@tonic-gate pid_setmin(void)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	if (jump_pid && jump_pid > mpid)
1197c478bd9Sstevel@tonic-gate 		minpid = mpid = jump_pid;
1207c478bd9Sstevel@tonic-gate 	else
12135a5a358SJonathan Adams 		minpid = mpid;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate /*
1257c478bd9Sstevel@tonic-gate  * When prslots are simply used as an index to determine a process' p_lock,
1267c478bd9Sstevel@tonic-gate  * adjacent prslots share adjacent p_locks.  On machines where the size
1277c478bd9Sstevel@tonic-gate  * of a mutex is smaller than that of a cache line (which, as of this writing,
1287c478bd9Sstevel@tonic-gate  * is true for all machines on which Solaris runs), this can potentially
1297c478bd9Sstevel@tonic-gate  * induce false sharing.  The standard solution for false sharing is to pad
1307c478bd9Sstevel@tonic-gate  * out one's data structures (in this case, struct plock).  However,
1317c478bd9Sstevel@tonic-gate  * given the size and (generally) sparse use of the proc_lock array, this
1327c478bd9Sstevel@tonic-gate  * is suboptimal.  We therefore stride through the proc_lock array with
1337c478bd9Sstevel@tonic-gate  * a stride of PLOCK_SHIFT.  PLOCK_SHIFT should be defined as:
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *   log_2 (coherence_granularity / sizeof (kmutex_t))
1367c478bd9Sstevel@tonic-gate  *
1377c478bd9Sstevel@tonic-gate  * Under this scheme, false sharing is still possible -- but only when
1387c478bd9Sstevel@tonic-gate  * the number of active processes is very large.  Note that the one-to-one
1397c478bd9Sstevel@tonic-gate  * mapping between prslots and lockslots is maintained.
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate static int
pid_getlockslot(int prslot)1427c478bd9Sstevel@tonic-gate pid_getlockslot(int prslot)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 	int even = (v.v_proc >> PLOCK_SHIFT) << PLOCK_SHIFT;
1457c478bd9Sstevel@tonic-gate 	int perlap = even >> PLOCK_SHIFT;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (prslot >= even)
1487c478bd9Sstevel@tonic-gate 		return (prslot);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	return (((prslot % perlap) << PLOCK_SHIFT) + (prslot / perlap));
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1549acbbeafSnn35248  * This function allocates a pid structure, a free pid, and optionally a
1559acbbeafSnn35248  * slot in the proc table for it.
1567c478bd9Sstevel@tonic-gate  *
1579acbbeafSnn35248  * pid_allocate() returns the new pid on success, -1 on failure.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate pid_t
pid_allocate(proc_t * prp,pid_t pid,int flags)16035a5a358SJonathan Adams pid_allocate(proc_t *prp, pid_t pid, int flags)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	struct pid *pidp;
1637c478bd9Sstevel@tonic-gate 	union procent *pep;
1647c478bd9Sstevel@tonic-gate 	pid_t newpid, startpid;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	pidp = kmem_zalloc(sizeof (struct pid), KM_SLEEP);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
1699acbbeafSnn35248 	if ((flags & PID_ALLOC_PROC) && (pep = procentfree) == NULL) {
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * ran out of /proc directory entries
1727c478bd9Sstevel@tonic-gate 		 */
1737c478bd9Sstevel@tonic-gate 		goto failed;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
17635a5a358SJonathan Adams 	if (pid != 0) {
17735a5a358SJonathan Adams 		VERIFY(minpid == 0);
17835a5a358SJonathan Adams 		VERIFY3P(pid, <, mpid);
17935a5a358SJonathan Adams 		VERIFY3P(pid_lookup(pid), ==, NULL);
18035a5a358SJonathan Adams 		newpid = pid;
18135a5a358SJonathan Adams 	} else {
1827c478bd9Sstevel@tonic-gate 		/*
1837c478bd9Sstevel@tonic-gate 		 * Allocate a pid
1847c478bd9Sstevel@tonic-gate 		 */
1858f14225bSRoger A. Faulkner 		ASSERT(minpid <= mpid && mpid < maxpid);
1867c478bd9Sstevel@tonic-gate 
18735a5a358SJonathan Adams 		startpid = mpid;
18835a5a358SJonathan Adams 		for (;;) {
18935a5a358SJonathan Adams 			newpid = mpid;
1908f14225bSRoger A. Faulkner 			if (++mpid == maxpid)
19135a5a358SJonathan Adams 				mpid = minpid;
19235a5a358SJonathan Adams 
19335a5a358SJonathan Adams 			if (pid_lookup(newpid) == NULL)
19435a5a358SJonathan Adams 				break;
19535a5a358SJonathan Adams 
19635a5a358SJonathan Adams 			if (mpid == startpid)
1977c478bd9Sstevel@tonic-gate 				goto failed;
1987c478bd9Sstevel@tonic-gate 		}
19935a5a358SJonathan Adams 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	/*
2027c478bd9Sstevel@tonic-gate 	 * Put pid into the pid hash table.
2037c478bd9Sstevel@tonic-gate 	 */
2047c478bd9Sstevel@tonic-gate 	pidp->pid_link = HASHPID(newpid);
2057c478bd9Sstevel@tonic-gate 	HASHPID(newpid) = pidp;
2067c478bd9Sstevel@tonic-gate 	pidp->pid_ref = 1;
2077c478bd9Sstevel@tonic-gate 	pidp->pid_id = newpid;
2089acbbeafSnn35248 
2099acbbeafSnn35248 	if (flags & PID_ALLOC_PROC) {
2109acbbeafSnn35248 		procentfree = pep->pe_next;
2117c478bd9Sstevel@tonic-gate 		pidp->pid_prslot = pep - procdir;
2129acbbeafSnn35248 		pep->pe_proc = prp;
2139acbbeafSnn35248 		prp->p_pidp = pidp;
2147c478bd9Sstevel@tonic-gate 		prp->p_lockp = &proc_lock[pid_getlockslot(pidp->pid_prslot)];
2159acbbeafSnn35248 	} else {
2169acbbeafSnn35248 		pidp->pid_prslot = 0;
2179acbbeafSnn35248 	}
2189acbbeafSnn35248 
2197c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return (newpid);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate failed:
2247c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2257c478bd9Sstevel@tonic-gate 	kmem_free(pidp, sizeof (struct pid));
2267c478bd9Sstevel@tonic-gate 	return (-1);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * decrement the reference count for pid
2317c478bd9Sstevel@tonic-gate  */
2327c478bd9Sstevel@tonic-gate int
pid_rele(struct pid * pidp)2337c478bd9Sstevel@tonic-gate pid_rele(struct pid *pidp)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	struct pid **pidpp;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
2387c478bd9Sstevel@tonic-gate 	ASSERT(pidp != &pid0);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	pidpp = &HASHPID(pidp->pid_id);
2417c478bd9Sstevel@tonic-gate 	for (;;) {
2427c478bd9Sstevel@tonic-gate 		ASSERT(*pidpp != NULL);
2437c478bd9Sstevel@tonic-gate 		if (*pidpp == pidp)
2447c478bd9Sstevel@tonic-gate 			break;
2457c478bd9Sstevel@tonic-gate 		pidpp = &(*pidpp)->pid_link;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	*pidpp = pidp->pid_link;
2497c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	kmem_free(pidp, sizeof (*pidp));
2527c478bd9Sstevel@tonic-gate 	return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate void
proc_entry_free(struct pid * pidp)2567c478bd9Sstevel@tonic-gate proc_entry_free(struct pid *pidp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
2597c478bd9Sstevel@tonic-gate 	pidp->pid_prinactive = 1;
2607c478bd9Sstevel@tonic-gate 	procdir[pidp->pid_prslot].pe_next = procentfree;
2617c478bd9Sstevel@tonic-gate 	procentfree = &procdir[pidp->pid_prslot];
2627c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
265*4bcbe6e7SMenno Lageman /*
266*4bcbe6e7SMenno Lageman  * The original task needs to be passed in since the process has already been
267*4bcbe6e7SMenno Lageman  * detached from the task at this point in time.
268*4bcbe6e7SMenno Lageman  */
2697c478bd9Sstevel@tonic-gate void
pid_exit(proc_t * prp,struct task * tk)270*4bcbe6e7SMenno Lageman pid_exit(proc_t *prp, struct task *tk)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	struct pid *pidp;
273*4bcbe6e7SMenno Lageman 	zone_t	*zone = prp->p_zone;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Exit process group.  If it is NULL, it's because fork failed
2797c478bd9Sstevel@tonic-gate 	 * before calling pgjoin().
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	ASSERT(prp->p_pgidp != NULL || prp->p_stat == SIDL);
2827c478bd9Sstevel@tonic-gate 	if (prp->p_pgidp != NULL)
2837c478bd9Sstevel@tonic-gate 		pgexit(prp);
2847c478bd9Sstevel@tonic-gate 
2859acbbeafSnn35248 	sess_rele(prp->p_sessp, B_TRUE);
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	pidp = prp->p_pidp;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	proc_entry_free(pidp);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (audit_active)
2927c478bd9Sstevel@tonic-gate 		audit_pfree(prp);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (practive == prp) {
2957c478bd9Sstevel@tonic-gate 		practive = prp->p_next;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	if (prp->p_next) {
2997c478bd9Sstevel@tonic-gate 		prp->p_next->p_prev = prp->p_prev;
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	if (prp->p_prev) {
3027c478bd9Sstevel@tonic-gate 		prp->p_prev->p_next = prp->p_next;
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	PID_RELE(pidp);
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	mutex_destroy(&prp->p_crlock);
3087c478bd9Sstevel@tonic-gate 	kmem_cache_free(process_cache, prp);
3097c478bd9Sstevel@tonic-gate 	nproc--;
310*4bcbe6e7SMenno Lageman 
311*4bcbe6e7SMenno Lageman 	/*
312*4bcbe6e7SMenno Lageman 	 * Decrement the process counts of the original task, project and zone.
313*4bcbe6e7SMenno Lageman 	 */
314*4bcbe6e7SMenno Lageman 	mutex_enter(&zone->zone_nlwps_lock);
315*4bcbe6e7SMenno Lageman 	tk->tk_nprocs--;
316*4bcbe6e7SMenno Lageman 	tk->tk_proj->kpj_nprocs--;
317*4bcbe6e7SMenno Lageman 	zone->zone_nprocs--;
318*4bcbe6e7SMenno Lageman 	mutex_exit(&zone->zone_nlwps_lock);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate /*
3227c478bd9Sstevel@tonic-gate  * Find a process visible from the specified zone given its process ID.
3237c478bd9Sstevel@tonic-gate  */
3247c478bd9Sstevel@tonic-gate proc_t *
prfind_zone(pid_t pid,zoneid_t zoneid)3257c478bd9Sstevel@tonic-gate prfind_zone(pid_t pid, zoneid_t zoneid)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	struct pid *pidp;
3287c478bd9Sstevel@tonic-gate 	proc_t *p;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
3337c478bd9Sstevel@tonic-gate 	pidp = pid_lookup(pid);
3347c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
3357c478bd9Sstevel@tonic-gate 	if (pidp != NULL && pidp->pid_prinactive == 0) {
3367c478bd9Sstevel@tonic-gate 		p = procdir[pidp->pid_prslot].pe_proc;
3377c478bd9Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || p->p_zone->zone_id == zoneid)
3387c478bd9Sstevel@tonic-gate 			return (p);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	return (NULL);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate /*
3447c478bd9Sstevel@tonic-gate  * Find a process given its process ID.  This obeys zone restrictions,
3457c478bd9Sstevel@tonic-gate  * so if the caller is in a non-global zone it won't find processes
3467c478bd9Sstevel@tonic-gate  * associated with other zones.  Use prfind_zone(pid, ALL_ZONES) to
3477c478bd9Sstevel@tonic-gate  * bypass this restriction.
3487c478bd9Sstevel@tonic-gate  */
3497c478bd9Sstevel@tonic-gate proc_t *
prfind(pid_t pid)3507c478bd9Sstevel@tonic-gate prfind(pid_t pid)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3557c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3567c478bd9Sstevel@tonic-gate 	else
3577c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
3587c478bd9Sstevel@tonic-gate 	return (prfind_zone(pid, zoneid));
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate proc_t *
pgfind_zone(pid_t pgid,zoneid_t zoneid)3627c478bd9Sstevel@tonic-gate pgfind_zone(pid_t pgid, zoneid_t zoneid)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	struct pid *pidp;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
3697c478bd9Sstevel@tonic-gate 	pidp = pid_lookup(pgid);
3707c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
3717c478bd9Sstevel@tonic-gate 	if (pidp != NULL) {
3727c478bd9Sstevel@tonic-gate 		proc_t *p = pidp->pid_pglink;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if (zoneid == ALL_ZONES || pgid == 0 || p == NULL ||
3757c478bd9Sstevel@tonic-gate 		    p->p_zone->zone_id == zoneid)
3767c478bd9Sstevel@tonic-gate 			return (p);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	return (NULL);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate  * return the head of the list of processes whose process group ID is 'pgid',
3837c478bd9Sstevel@tonic-gate  * or NULL, if no such process group
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate proc_t *
pgfind(pid_t pgid)3867c478bd9Sstevel@tonic-gate pgfind(pid_t pgid)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
3917c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
3927c478bd9Sstevel@tonic-gate 	else
3937c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
3947c478bd9Sstevel@tonic-gate 	return (pgfind_zone(pgid, zoneid));
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*
3980209230bSgjelinek  * Sets P_PR_LOCK on a non-system process.  Process must be fully created
3990209230bSgjelinek  * and not exiting to succeed.
4000209230bSgjelinek  *
4010209230bSgjelinek  * Returns 0 on success.
4020209230bSgjelinek  * Returns 1 if P_PR_LOCK is set.
4030209230bSgjelinek  * Returns -1 if proc is in invalid state.
4040209230bSgjelinek  */
4050209230bSgjelinek int
sprtrylock_proc(proc_t * p)4060209230bSgjelinek sprtrylock_proc(proc_t *p)
4070209230bSgjelinek {
4080209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
4090209230bSgjelinek 
4100209230bSgjelinek 	/* skip system and incomplete processes */
4110209230bSgjelinek 	if (p->p_stat == SIDL || p->p_stat == SZOMB ||
4120209230bSgjelinek 	    (p->p_flag & (SSYS | SEXITING | SEXITLWPS))) {
4130209230bSgjelinek 		return (-1);
4140209230bSgjelinek 	}
4150209230bSgjelinek 
4160209230bSgjelinek 	if (p->p_proc_flag & P_PR_LOCK)
4170209230bSgjelinek 		return (1);
4180209230bSgjelinek 
4190209230bSgjelinek 	p->p_proc_flag |= P_PR_LOCK;
4200209230bSgjelinek 	THREAD_KPRI_REQUEST();
4210209230bSgjelinek 
4220209230bSgjelinek 	return (0);
4230209230bSgjelinek }
4240209230bSgjelinek 
4250209230bSgjelinek /*
4260209230bSgjelinek  * Wait for P_PR_LOCK to become clear.  Returns with p_lock dropped,
4270209230bSgjelinek  * and the proc pointer no longer valid, as the proc may have exited.
4280209230bSgjelinek  */
4290209230bSgjelinek void
sprwaitlock_proc(proc_t * p)4300209230bSgjelinek sprwaitlock_proc(proc_t *p)
4310209230bSgjelinek {
4320209230bSgjelinek 	kmutex_t *mp;
4330209230bSgjelinek 
4340209230bSgjelinek 	ASSERT(MUTEX_HELD(&p->p_lock));
4350209230bSgjelinek 	ASSERT(p->p_proc_flag & P_PR_LOCK);
4360209230bSgjelinek 
4370209230bSgjelinek 	/*
4380209230bSgjelinek 	 * p_lock is persistent, but p itself is not -- it could
4390209230bSgjelinek 	 * vanish during cv_wait().  Load p->p_lock now so we can
4400209230bSgjelinek 	 * drop it after cv_wait() without referencing p.
4410209230bSgjelinek 	 */
4420209230bSgjelinek 	mp = &p->p_lock;
4430209230bSgjelinek 	cv_wait(&pr_pid_cv[p->p_slot], mp);
4440209230bSgjelinek 	mutex_exit(mp);
4450209230bSgjelinek }
4460209230bSgjelinek 
4470209230bSgjelinek /*
4487c478bd9Sstevel@tonic-gate  * If pid exists, find its proc, acquire its p_lock and mark it P_PR_LOCK.
4497c478bd9Sstevel@tonic-gate  * Returns the proc pointer on success, NULL on failure.  sprlock() is
4507c478bd9Sstevel@tonic-gate  * really just a stripped-down version of pr_p_lock() to allow practive
4517c478bd9Sstevel@tonic-gate  * walkers like dofusers() and dumpsys() to synchronize with /proc.
4527c478bd9Sstevel@tonic-gate  */
4537c478bd9Sstevel@tonic-gate proc_t *
sprlock_zone(pid_t pid,zoneid_t zoneid)4547c478bd9Sstevel@tonic-gate sprlock_zone(pid_t pid, zoneid_t zoneid)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	proc_t *p;
4570209230bSgjelinek 	int ret;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	for (;;) {
4607c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
4617c478bd9Sstevel@tonic-gate 		if ((p = prfind_zone(pid, zoneid)) == NULL) {
4627c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
4637c478bd9Sstevel@tonic-gate 			return (NULL);
4647c478bd9Sstevel@tonic-gate 		}
4650209230bSgjelinek 		mutex_enter(&p->p_lock);
4667c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
4670209230bSgjelinek 
4687c478bd9Sstevel@tonic-gate 		if (panicstr)
4697c478bd9Sstevel@tonic-gate 			return (p);
4700209230bSgjelinek 
4710209230bSgjelinek 		ret = sprtrylock_proc(p);
4720209230bSgjelinek 		if (ret == -1) {
4730209230bSgjelinek 			mutex_exit(&p->p_lock);
4740209230bSgjelinek 			return (NULL);
4750209230bSgjelinek 		} else if (ret == 0) {
4767c478bd9Sstevel@tonic-gate 			break;
4777c478bd9Sstevel@tonic-gate 		}
4780209230bSgjelinek 		sprwaitlock_proc(p);
4790209230bSgjelinek 	}
4807c478bd9Sstevel@tonic-gate 	return (p);
4817c478bd9Sstevel@tonic-gate }
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate proc_t *
sprlock(pid_t pid)4847c478bd9Sstevel@tonic-gate sprlock(pid_t pid)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	zoneid_t zoneid;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	if (INGLOBALZONE(curproc))
4897c478bd9Sstevel@tonic-gate 		zoneid = ALL_ZONES;
4907c478bd9Sstevel@tonic-gate 	else
4917c478bd9Sstevel@tonic-gate 		zoneid = getzoneid();
4927c478bd9Sstevel@tonic-gate 	return (sprlock_zone(pid, zoneid));
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate void
sprlock_proc(proc_t * p)4967c478bd9Sstevel@tonic-gate sprlock_proc(proc_t *p)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	while (p->p_proc_flag & P_PR_LOCK) {
5017c478bd9Sstevel@tonic-gate 		cv_wait(&pr_pid_cv[p->p_slot], &p->p_lock);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	p->p_proc_flag |= P_PR_LOCK;
5057c478bd9Sstevel@tonic-gate 	THREAD_KPRI_REQUEST();
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate void
sprunlock(proc_t * p)5097c478bd9Sstevel@tonic-gate sprunlock(proc_t *p)
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	if (panicstr) {
5127c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5137c478bd9Sstevel@tonic-gate 		return;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	ASSERT(p->p_proc_flag & P_PR_LOCK);
5177c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	cv_signal(&pr_pid_cv[p->p_slot]);
5207c478bd9Sstevel@tonic-gate 	p->p_proc_flag &= ~P_PR_LOCK;
5217c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
5227c478bd9Sstevel@tonic-gate 	THREAD_KPRI_RELEASE();
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate void
pid_init(void)5267c478bd9Sstevel@tonic-gate pid_init(void)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	int i;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	pid_hashsz = 1 << highbit(v.v_proc / pid_hashlen);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	pidhash = kmem_zalloc(sizeof (struct pid *) * pid_hashsz, KM_SLEEP);
5337c478bd9Sstevel@tonic-gate 	procdir = kmem_alloc(sizeof (union procent) * v.v_proc, KM_SLEEP);
5347c478bd9Sstevel@tonic-gate 	pr_pid_cv = kmem_zalloc(sizeof (kcondvar_t) * v.v_proc, KM_SLEEP);
5357c478bd9Sstevel@tonic-gate 	proc_lock = kmem_zalloc(sizeof (struct plock) * v.v_proc, KM_SLEEP);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	nproc = 1;
5387c478bd9Sstevel@tonic-gate 	practive = proc_sched;
5397c478bd9Sstevel@tonic-gate 	proc_sched->p_next = NULL;
5407c478bd9Sstevel@tonic-gate 	procdir[0].pe_proc = proc_sched;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	procentfree = &procdir[1];
5437c478bd9Sstevel@tonic-gate 	for (i = 1; i < v.v_proc - 1; i++)
5447c478bd9Sstevel@tonic-gate 		procdir[i].pe_next = &procdir[i+1];
5457c478bd9Sstevel@tonic-gate 	procdir[i].pe_next = NULL;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 	HASHPID(0) = &pid0;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	upcount_init();
5507c478bd9Sstevel@tonic-gate }
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate proc_t *
pid_entry(int slot)5537c478bd9Sstevel@tonic-gate pid_entry(int slot)
5547c478bd9Sstevel@tonic-gate {
5557c478bd9Sstevel@tonic-gate 	union procent *pep;
5567c478bd9Sstevel@tonic-gate 	proc_t *prp;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
5597c478bd9Sstevel@tonic-gate 	ASSERT(slot >= 0 && slot < v.v_proc);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	pep = procdir[slot].pe_next;
5627c478bd9Sstevel@tonic-gate 	if (pep >= procdir && pep < &procdir[v.v_proc])
5637c478bd9Sstevel@tonic-gate 		return (NULL);
5647c478bd9Sstevel@tonic-gate 	prp = procdir[slot].pe_proc;
5657c478bd9Sstevel@tonic-gate 	if (prp != 0 && prp->p_stat == SIDL)
5667c478bd9Sstevel@tonic-gate 		return (NULL);
5677c478bd9Sstevel@tonic-gate 	return (prp);
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate  * Send the specified signal to all processes whose process group ID is
5727c478bd9Sstevel@tonic-gate  * equal to 'pgid'
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate void
signal(pid_t pgid,int sig)5767c478bd9Sstevel@tonic-gate signal(pid_t pgid, int sig)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	struct pid *pidp;
5797c478bd9Sstevel@tonic-gate 	proc_t *prp;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
5827c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlinklock);
5837c478bd9Sstevel@tonic-gate 	if (pgid == 0 || (pidp = pid_lookup(pgid)) == NULL) {
5847c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlinklock);
5857c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
5867c478bd9Sstevel@tonic-gate 		return;
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlinklock);
5897c478bd9Sstevel@tonic-gate 	for (prp = pidp->pid_pglink; prp; prp = prp->p_pglink) {
5907c478bd9Sstevel@tonic-gate 		mutex_enter(&prp->p_lock);
5917c478bd9Sstevel@tonic-gate 		sigtoproc(prp, NULL, sig);
5927c478bd9Sstevel@tonic-gate 		mutex_exit(&prp->p_lock);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate  * Send the specified signal to the specified process
5997c478bd9Sstevel@tonic-gate  */
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate void
prsignal(struct pid * pidp,int sig)6027c478bd9Sstevel@tonic-gate prsignal(struct pid *pidp, int sig)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	if (!(pidp->pid_prinactive))
6057c478bd9Sstevel@tonic-gate 		psignal(procdir[pidp->pid_prslot].pe_proc, sig);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * DDI/DKI interfaces for drivers to send signals to processes
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * obtain an opaque reference to a process for signaling
6167c478bd9Sstevel@tonic-gate  */
6177c478bd9Sstevel@tonic-gate void *
proc_ref(void)6187c478bd9Sstevel@tonic-gate proc_ref(void)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	struct pid *pidp;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
6237c478bd9Sstevel@tonic-gate 	pidp = curproc->p_pidp;
6247c478bd9Sstevel@tonic-gate 	PID_HOLD(pidp);
6257c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	return (pidp);
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate /*
6317c478bd9Sstevel@tonic-gate  * release a reference to a process
6327c478bd9Sstevel@tonic-gate  * - a process can exit even if a driver has a reference to it
6337c478bd9Sstevel@tonic-gate  * - one proc_unref for every proc_ref
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate void
proc_unref(void * pref)6367c478bd9Sstevel@tonic-gate proc_unref(void *pref)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
6397c478bd9Sstevel@tonic-gate 	PID_RELE((struct pid *)pref);
6407c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate  * send a signal to a process
6457c478bd9Sstevel@tonic-gate  *
6467c478bd9Sstevel@tonic-gate  * - send the process the signal
6477c478bd9Sstevel@tonic-gate  * - if the process went away, return a -1
6487c478bd9Sstevel@tonic-gate  * - if the process is still there return 0
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate int
proc_signal(void * pref,int sig)6517c478bd9Sstevel@tonic-gate proc_signal(void *pref, int sig)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	struct pid *pidp = pref;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	prsignal(pidp, sig);
6567c478bd9Sstevel@tonic-gate 	return (pidp->pid_prinactive ? -1 : 0);
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate static struct upcount	**upc_hash;	/* a boot time allocated array */
6617c478bd9Sstevel@tonic-gate static ulong_t		upc_hashmask;
6627c478bd9Sstevel@tonic-gate #define	UPC_HASH(x, y)	((ulong_t)(x ^ y) & upc_hashmask)
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Get us off the ground.  Called once at boot.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate void
upcount_init(void)6687c478bd9Sstevel@tonic-gate upcount_init(void)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	ulong_t	upc_hashsize;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/*
6737c478bd9Sstevel@tonic-gate 	 * An entry per MB of memory is our current guess
6747c478bd9Sstevel@tonic-gate 	 */
6757c478bd9Sstevel@tonic-gate 	/*
6767c478bd9Sstevel@tonic-gate 	 * 2^20 is a meg, so shifting right by 20 - PAGESHIFT
6777c478bd9Sstevel@tonic-gate 	 * converts pages to megs (without overflowing a u_int
6787c478bd9Sstevel@tonic-gate 	 * if you have more than 4G of memory, like ptob(physmem)/1M
6797c478bd9Sstevel@tonic-gate 	 * would).
6807c478bd9Sstevel@tonic-gate 	 */
6817c478bd9Sstevel@tonic-gate 	upc_hashsize = (1 << highbit(physmem >> (20 - PAGESHIFT)));
6827c478bd9Sstevel@tonic-gate 	upc_hashmask = upc_hashsize - 1;
6837c478bd9Sstevel@tonic-gate 	upc_hash = kmem_zalloc(upc_hashsize * sizeof (struct upcount *),
6847c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * Increment the number of processes associated with a given uid and zoneid.
6897c478bd9Sstevel@tonic-gate  */
6907c478bd9Sstevel@tonic-gate void
upcount_inc(uid_t uid,zoneid_t zoneid)6917c478bd9Sstevel@tonic-gate upcount_inc(uid_t uid, zoneid_t zoneid)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	struct upcount	**upc, **hupc;
6947c478bd9Sstevel@tonic-gate 	struct upcount	*new;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6977c478bd9Sstevel@tonic-gate 	new = NULL;
6987c478bd9Sstevel@tonic-gate 	hupc = &upc_hash[UPC_HASH(uid, zoneid)];
6997c478bd9Sstevel@tonic-gate top:
7007c478bd9Sstevel@tonic-gate 	upc = hupc;
7017c478bd9Sstevel@tonic-gate 	while ((*upc) != NULL) {
7027c478bd9Sstevel@tonic-gate 		if ((*upc)->up_uid == uid && (*upc)->up_zoneid == zoneid) {
7037c478bd9Sstevel@tonic-gate 			(*upc)->up_count++;
7047c478bd9Sstevel@tonic-gate 			if (new) {
7057c478bd9Sstevel@tonic-gate 				/*
7067c478bd9Sstevel@tonic-gate 				 * did not need `new' afterall.
7077c478bd9Sstevel@tonic-gate 				 */
7087c478bd9Sstevel@tonic-gate 				kmem_free(new, sizeof (*new));
7097c478bd9Sstevel@tonic-gate 			}
7107c478bd9Sstevel@tonic-gate 			return;
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 		upc = &(*upc)->up_next;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * There is no entry for this <uid,zoneid> pair.
7177c478bd9Sstevel@tonic-gate 	 * Allocate one.  If we have to drop pidlock, check
7187c478bd9Sstevel@tonic-gate 	 * again.
7197c478bd9Sstevel@tonic-gate 	 */
7207c478bd9Sstevel@tonic-gate 	if (new == NULL) {
7217c478bd9Sstevel@tonic-gate 		new = (struct upcount *)kmem_alloc(sizeof (*new), KM_NOSLEEP);
7227c478bd9Sstevel@tonic-gate 		if (new == NULL) {
7237c478bd9Sstevel@tonic-gate 			mutex_exit(&pidlock);
7247c478bd9Sstevel@tonic-gate 			new = (struct upcount *)kmem_alloc(sizeof (*new),
7257c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
7267c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
7277c478bd9Sstevel@tonic-gate 			goto top;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * On the assumption that a new user is going to do some
7347c478bd9Sstevel@tonic-gate 	 * more forks, put the new upcount structure on the front.
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	upc = hupc;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	new->up_uid = uid;
7397c478bd9Sstevel@tonic-gate 	new->up_zoneid = zoneid;
7407c478bd9Sstevel@tonic-gate 	new->up_count = 1;
7417c478bd9Sstevel@tonic-gate 	new->up_next = *upc;
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	*upc = new;
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate  * Decrement the number of processes a given uid and zoneid has.
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate void
upcount_dec(uid_t uid,zoneid_t zoneid)7507c478bd9Sstevel@tonic-gate upcount_dec(uid_t uid, zoneid_t zoneid)
7517c478bd9Sstevel@tonic-gate {
7527c478bd9Sstevel@tonic-gate 	struct	upcount **upc;
7537c478bd9Sstevel@tonic-gate 	struct	upcount *done;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	upc = &upc_hash[UPC_HASH(uid, zoneid)];
7587c478bd9Sstevel@tonic-gate 	while ((*upc) != NULL) {
7597c478bd9Sstevel@tonic-gate 		if ((*upc)->up_uid == uid && (*upc)->up_zoneid == zoneid) {
7607c478bd9Sstevel@tonic-gate 			(*upc)->up_count--;
7617c478bd9Sstevel@tonic-gate 			if ((*upc)->up_count == 0) {
7627c478bd9Sstevel@tonic-gate 				done = *upc;
7637c478bd9Sstevel@tonic-gate 				*upc = (*upc)->up_next;
7647c478bd9Sstevel@tonic-gate 				kmem_free(done, sizeof (*done));
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 			return;
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 		upc = &(*upc)->up_next;
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	cmn_err(CE_PANIC, "decr_upcount-off the end");
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * Returns the number of processes a uid has.
7757c478bd9Sstevel@tonic-gate  * Non-existent uid's are assumed to have no processes.
7767c478bd9Sstevel@tonic-gate  */
7777c478bd9Sstevel@tonic-gate int
upcount_get(uid_t uid,zoneid_t zoneid)7787c478bd9Sstevel@tonic-gate upcount_get(uid_t uid, zoneid_t zoneid)
7797c478bd9Sstevel@tonic-gate {
7807c478bd9Sstevel@tonic-gate 	struct	upcount *upc;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	upc = upc_hash[UPC_HASH(uid, zoneid)];
7857c478bd9Sstevel@tonic-gate 	while (upc != NULL) {
7867c478bd9Sstevel@tonic-gate 		if (upc->up_uid == uid && upc->up_zoneid == zoneid) {
7877c478bd9Sstevel@tonic-gate 			return (upc->up_count);
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		upc = upc->up_next;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 	return (0);
7927c478bd9Sstevel@tonic-gate }
793