xref: /illumos-gate/usr/src/uts/common/os/project.c (revision 824c205f)
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
519f92332Sml93401  * Common Development and Distribution License (the "License").
619f92332Sml93401  * 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  */
217c478bd9Sstevel@tonic-gate /*
2219f92332Sml93401  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/project.h>
297c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
307c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
327c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/proc.h>
357c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/fss.h>
387c478bd9Sstevel@tonic-gate #include <sys/systm.h>
397c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
407c478bd9Sstevel@tonic-gate #include <sys/port_kernel.h>
417c478bd9Sstevel@tonic-gate #include <sys/task.h>
427c478bd9Sstevel@tonic-gate #include <sys/zone.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate int project_hash_size = 64;
457c478bd9Sstevel@tonic-gate static kmutex_t project_hash_lock;
467c478bd9Sstevel@tonic-gate static kmutex_t projects_list_lock;
477c478bd9Sstevel@tonic-gate static mod_hash_t *projects_hash;
487c478bd9Sstevel@tonic-gate static kproject_t *projects_list;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_cpu_shares;
517c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_nlwps;
527c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_ntasks;
537c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_msgmni;
547c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_semmni;
557c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_shmmax;
567c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_shmmni;
577c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_portids;
587c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_devlockmem;
597c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_contract;
607c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_crypto_mem;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Dummy structure used when comparing projects.  This structure must be kept
647c478bd9Sstevel@tonic-gate  * identical to the first two fields of kproject_t.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate struct project_zone {
677c478bd9Sstevel@tonic-gate 	projid_t	kpj_id;
687c478bd9Sstevel@tonic-gate 	zoneid_t	kpj_zoneid;
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Projects
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   A dictionary of all active projects is maintained by the kernel so that we
757c478bd9Sstevel@tonic-gate  *   may track project usage and limits.  (By an active project, we mean a
767c478bd9Sstevel@tonic-gate  *   project associated with one or more task, and therefore with one or more
777c478bd9Sstevel@tonic-gate  *   processes.) We build the dictionary on top of the mod_hash facility, since
787c478bd9Sstevel@tonic-gate  *   project additions and deletions are relatively rare events.  An
797c478bd9Sstevel@tonic-gate  *   integer-to-pointer mapping is maintained within the hash, representing the
807c478bd9Sstevel@tonic-gate  *   map from project id to project structure.  All projects, including the
817c478bd9Sstevel@tonic-gate  *   primordial "project 0", are allocated via the project_hold_by_id()
827c478bd9Sstevel@tonic-gate  *   interface.
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  *   Currently, the project contains a reference count; the project ID, which is
857c478bd9Sstevel@tonic-gate  *   examined by the extended accounting subsystem as well as /proc; a resource
867c478bd9Sstevel@tonic-gate  *   control set, which contains the allowable values (and actions on exceeding
877c478bd9Sstevel@tonic-gate  *   those values) for controlled project-level resources on the system; and a
887c478bd9Sstevel@tonic-gate  *   number of CPU shares, which is used by the fair share scheduling class
897c478bd9Sstevel@tonic-gate  *   (FSS) to support its proportion-based scheduling algorithm.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * Reference counting convention
927c478bd9Sstevel@tonic-gate  *   The dictionary entry does not itself count as a reference--only references
937c478bd9Sstevel@tonic-gate  *   outside of the subsystem are tallied.  At the drop of the final external
947c478bd9Sstevel@tonic-gate  *   reference, the project entry is removed.  The reference counter keeps
957c478bd9Sstevel@tonic-gate  *   track of the number of threads *and* tasks within a project.
967c478bd9Sstevel@tonic-gate  *
977c478bd9Sstevel@tonic-gate  * Locking
987c478bd9Sstevel@tonic-gate  *   Walking the doubly-linked project list must be done while holding
997c478bd9Sstevel@tonic-gate  *   projects_list_lock.  Thus, any dereference of kpj_next or kpj_prev must be
1007c478bd9Sstevel@tonic-gate  *   under projects_list_lock.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  *   If both the hash lock, project_hash_lock, and the list lock are to be
1037c478bd9Sstevel@tonic-gate  *   acquired, the hash lock is to be acquired first.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static void
1087c478bd9Sstevel@tonic-gate project_data_init(kproject_data_t *data)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Initialize subsystem-specific data
1127c478bd9Sstevel@tonic-gate 	 */
1137c478bd9Sstevel@tonic-gate 	data->kpd_shmmax = 0;
114*824c205fSml93401 	data->kpd_ipc.ipcq_shmmni = 0;
115*824c205fSml93401 	data->kpd_ipc.ipcq_semmni = 0;
116*824c205fSml93401 	data->kpd_ipc.ipcq_msgmni = 0;
1177c478bd9Sstevel@tonic-gate 	data->kpd_devlockmem = 0;
1187c478bd9Sstevel@tonic-gate 	data->kpd_contract = 0;
1197c478bd9Sstevel@tonic-gate 	data->kpd_crypto_mem = 0;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1237c478bd9Sstevel@tonic-gate static uint_t
1247c478bd9Sstevel@tonic-gate project_hash_by_id(void *hash_data, mod_hash_key_t key)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct project_zone *pz = key;
1277c478bd9Sstevel@tonic-gate 	uint_t mykey;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * Merge the zoneid and projectid together to a 32-bit quantity, and
1317c478bd9Sstevel@tonic-gate 	 * then pass that in to the existing idhash.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	mykey = (pz->kpj_zoneid << 16) | pz->kpj_id;
1347c478bd9Sstevel@tonic-gate 	return (mod_hash_byid(hash_data, (mod_hash_key_t)(uintptr_t)mykey));
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static int
1387c478bd9Sstevel@tonic-gate project_hash_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1397c478bd9Sstevel@tonic-gate {
1407c478bd9Sstevel@tonic-gate 	struct project_zone *pz1 = key1, *pz2 = key2;
1417c478bd9Sstevel@tonic-gate 	int retval;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return ((int)((retval = pz1->kpj_id - pz2->kpj_id) != 0 ? retval :
1447c478bd9Sstevel@tonic-gate 	    pz1->kpj_zoneid - pz2->kpj_zoneid));
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static void
1487c478bd9Sstevel@tonic-gate project_hash_val_dtor(mod_hash_val_t val)
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	kproject_t *kp = (kproject_t *)val;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	ASSERT(kp->kpj_count == 0);
1537c478bd9Sstevel@tonic-gate 	kmem_free(kp, sizeof (kproject_t));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate /*
1577c478bd9Sstevel@tonic-gate  * kproject_t *project_hold(kproject_t *)
1587c478bd9Sstevel@tonic-gate  *
1597c478bd9Sstevel@tonic-gate  * Overview
1607c478bd9Sstevel@tonic-gate  *   Record that an additional reference on the indicated project has been
1617c478bd9Sstevel@tonic-gate  *   taken.
1627c478bd9Sstevel@tonic-gate  *
1637c478bd9Sstevel@tonic-gate  * Return values
1647c478bd9Sstevel@tonic-gate  *   A pointer to the indicated project.
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  * Caller's context
1677c478bd9Sstevel@tonic-gate  *   project_hash_lock must not be held across the project_hold() call.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate kproject_t *
1707c478bd9Sstevel@tonic-gate project_hold(kproject_t *p)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
1737c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL);
1747c478bd9Sstevel@tonic-gate 	p->kpj_count++;
1757c478bd9Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
1767c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
1777c478bd9Sstevel@tonic-gate 	return (p);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * kproject_t *project_hold_by_id(projid_t, zoneid_t, int)
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * Overview
1847c478bd9Sstevel@tonic-gate  *   project_hold_by_id() performs a look-up in the dictionary of projects
1857c478bd9Sstevel@tonic-gate  *   active on the system by specified project ID + zone ID and puts a hold on
1867c478bd9Sstevel@tonic-gate  *   it.  The third argument defines the desired behavior in the case when
1877c478bd9Sstevel@tonic-gate  *   project with given project ID cannot be found:
1887c478bd9Sstevel@tonic-gate  *
1897c478bd9Sstevel@tonic-gate  *   PROJECT_HOLD_INSERT	New entry is made in dictionary and the project
1907c478bd9Sstevel@tonic-gate  *   				is added to the global list.
1917c478bd9Sstevel@tonic-gate  *
1927c478bd9Sstevel@tonic-gate  *   PROJECT_HOLD_FIND		Return NULL.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  *   The project is returned with its reference count incremented by one.
1957c478bd9Sstevel@tonic-gate  *   A new project derives its resource controls from those of project 0.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Return values
1987c478bd9Sstevel@tonic-gate  *   A pointer to the held project.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * Caller's context
2017c478bd9Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate kproject_t *
2047c478bd9Sstevel@tonic-gate project_hold_by_id(projid_t id, zoneid_t zoneid, int flag)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	kproject_t *spare_p;
2077c478bd9Sstevel@tonic-gate 	kproject_t *p;
2087c478bd9Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
2097c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
2107c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
2117c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
2127c478bd9Sstevel@tonic-gate 	struct project_zone pz;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	pz.kpj_id = id;
2157c478bd9Sstevel@tonic-gate 	pz.kpj_zoneid = zoneid;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (flag == PROJECT_HOLD_FIND) {
2187c478bd9Sstevel@tonic-gate 		mutex_enter(&project_hash_lock);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
2217c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)&p) == MH_ERR_NOTFOUND)
2227c478bd9Sstevel@tonic-gate 			p = NULL;
2237c478bd9Sstevel@tonic-gate 		else
2247c478bd9Sstevel@tonic-gate 			p->kpj_count++;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		mutex_exit(&project_hash_lock);
2277c478bd9Sstevel@tonic-gate 		return (p);
2287c478bd9Sstevel@tonic-gate 	}
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	ASSERT(flag == PROJECT_HOLD_INSERT);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	spare_p = kmem_zalloc(sizeof (kproject_t), KM_SLEEP);
2337c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_PROJECT);
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	(void) mod_hash_reserve(projects_hash, &hndl);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
2407c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
2417c478bd9Sstevel@tonic-gate 	if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
2427c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&p) == MH_ERR_NOTFOUND) {
2437c478bd9Sstevel@tonic-gate 		p = spare_p;
2447c478bd9Sstevel@tonic-gate 		p->kpj_id = id;
2457c478bd9Sstevel@tonic-gate 		p->kpj_zoneid = zoneid;
2467c478bd9Sstevel@tonic-gate 		p->kpj_count = 0;
2477c478bd9Sstevel@tonic-gate 		p->kpj_shares = 1;
2487c478bd9Sstevel@tonic-gate 		p->kpj_nlwps = 0;
2497c478bd9Sstevel@tonic-gate 		p->kpj_ntasks = 0;
2507c478bd9Sstevel@tonic-gate 		p->kpj_nlwps_ctl = INT_MAX;
2517c478bd9Sstevel@tonic-gate 		p->kpj_ntasks_ctl = INT_MAX;
2527c478bd9Sstevel@tonic-gate 		project_data_init(&p->kpj_data);
2537c478bd9Sstevel@tonic-gate 		e.rcep_p.proj = p;
2547c478bd9Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROJECT;
2557c478bd9Sstevel@tonic-gate 		p->kpj_rctls = rctl_set_init(RCENTITY_PROJECT, curproc, &e,
2567c478bd9Sstevel@tonic-gate 		    set, gp);
2577c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 		if (mod_hash_insert_reserve(projects_hash, (mod_hash_key_t)p,
2607c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)p, hndl))
2617c478bd9Sstevel@tonic-gate 			panic("unable to insert project %d(%p)", id, (void *)p);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 		/*
2647c478bd9Sstevel@tonic-gate 		 * Insert project into global project list.
2657c478bd9Sstevel@tonic-gate 		 */
2667c478bd9Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
2677c478bd9Sstevel@tonic-gate 		if (id != 0 || zoneid != GLOBAL_ZONEID) {
2687c478bd9Sstevel@tonic-gate 			p->kpj_next = projects_list;
2697c478bd9Sstevel@tonic-gate 			p->kpj_prev = projects_list->kpj_prev;
2707c478bd9Sstevel@tonic-gate 			p->kpj_prev->kpj_next = p;
2717c478bd9Sstevel@tonic-gate 			projects_list->kpj_prev = p;
2727c478bd9Sstevel@tonic-gate 		} else {
2737c478bd9Sstevel@tonic-gate 			/*
2747c478bd9Sstevel@tonic-gate 			 * Special case: primordial hold on project 0.
2757c478bd9Sstevel@tonic-gate 			 */
2767c478bd9Sstevel@tonic-gate 			p->kpj_next = p;
2777c478bd9Sstevel@tonic-gate 			p->kpj_prev = p;
2787c478bd9Sstevel@tonic-gate 			projects_list = p;
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
2817c478bd9Sstevel@tonic-gate 	} else {
2827c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
2837c478bd9Sstevel@tonic-gate 		mod_hash_cancel(projects_hash, &hndl);
2847c478bd9Sstevel@tonic-gate 		kmem_free(spare_p, sizeof (kproject_t));
2857c478bd9Sstevel@tonic-gate 		rctl_set_free(set);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
2897c478bd9Sstevel@tonic-gate 	p->kpj_count++;
2907c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (p);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * void project_rele(kproject_t *)
2987c478bd9Sstevel@tonic-gate  *
2997c478bd9Sstevel@tonic-gate  * Overview
3007c478bd9Sstevel@tonic-gate  *   Advertise that one external reference to this project is no longer needed.
3017c478bd9Sstevel@tonic-gate  *
3027c478bd9Sstevel@tonic-gate  * Return values
3037c478bd9Sstevel@tonic-gate  *   None.
3047c478bd9Sstevel@tonic-gate  *
3057c478bd9Sstevel@tonic-gate  * Caller's context
3067c478bd9Sstevel@tonic-gate  *   No restriction on context.
3077c478bd9Sstevel@tonic-gate  */
3087c478bd9Sstevel@tonic-gate void
3097c478bd9Sstevel@tonic-gate project_rele(kproject_t *p)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
3127c478bd9Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
3137c478bd9Sstevel@tonic-gate 	p->kpj_count--;
3147c478bd9Sstevel@tonic-gate 	if (p->kpj_count == 0) {
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		/*
3177c478bd9Sstevel@tonic-gate 		 * Remove project from global list.
3187c478bd9Sstevel@tonic-gate 		 */
3197c478bd9Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
3207c478bd9Sstevel@tonic-gate 		p->kpj_next->kpj_prev = p->kpj_prev;
3217c478bd9Sstevel@tonic-gate 		p->kpj_prev->kpj_next = p->kpj_next;
3227c478bd9Sstevel@tonic-gate 		if (projects_list == p)
3237c478bd9Sstevel@tonic-gate 			projects_list = p->kpj_next;
3247c478bd9Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		rctl_set_free(p->kpj_rctls);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		if (mod_hash_destroy(projects_hash, (mod_hash_key_t)p))
3297c478bd9Sstevel@tonic-gate 			panic("unable to delete project %d zone %d", p->kpj_id,
3307c478bd9Sstevel@tonic-gate 			    p->kpj_zoneid);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * int project_walk_all(zoneid_t, int (*)(kproject_t *, void *), void *)
3387c478bd9Sstevel@tonic-gate  *
3397c478bd9Sstevel@tonic-gate  * Overview
3407c478bd9Sstevel@tonic-gate  *   Walk the project list for the given zoneid with a callback.
3417c478bd9Sstevel@tonic-gate  *
3427c478bd9Sstevel@tonic-gate  * Return values
3437c478bd9Sstevel@tonic-gate  *   -1 for an invalid walk, number of projects visited otherwise.
3447c478bd9Sstevel@tonic-gate  *
3457c478bd9Sstevel@tonic-gate  * Caller's context
3467c478bd9Sstevel@tonic-gate  *   projects_list_lock must not be held, as it is acquired by
3477c478bd9Sstevel@tonic-gate  *   project_walk_all().  Accordingly, callbacks may not perform KM_SLEEP
3487c478bd9Sstevel@tonic-gate  *   allocations.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate int
3517c478bd9Sstevel@tonic-gate project_walk_all(zoneid_t zoneid, int (*cb)(kproject_t *, void *),
3527c478bd9Sstevel@tonic-gate     void *walk_data)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	int cnt = 0;
3557c478bd9Sstevel@tonic-gate 	kproject_t *kp = proj0p;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	mutex_enter(&projects_list_lock);
3587c478bd9Sstevel@tonic-gate 	do {
3597c478bd9Sstevel@tonic-gate 		if (zoneid != ALL_ZONES && kp->kpj_zoneid != zoneid)
3607c478bd9Sstevel@tonic-gate 			continue;
3617c478bd9Sstevel@tonic-gate 		if (cb(kp, walk_data) == -1) {
3627c478bd9Sstevel@tonic-gate 			cnt = -1;
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		} else {
3657c478bd9Sstevel@tonic-gate 			cnt++;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 	} while ((kp = kp->kpj_next) != proj0p);
3687c478bd9Sstevel@tonic-gate 	mutex_exit(&projects_list_lock);
3697c478bd9Sstevel@tonic-gate 	return (cnt);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * projid_t curprojid(void)
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * Overview
3767c478bd9Sstevel@tonic-gate  *   Return project ID of the current thread
3777c478bd9Sstevel@tonic-gate  *
3787c478bd9Sstevel@tonic-gate  * Caller's context
3797c478bd9Sstevel@tonic-gate  *   No restrictions.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate projid_t
3827c478bd9Sstevel@tonic-gate curprojid()
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	return (ttoproj(curthread)->kpj_id);
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * project.cpu-shares resource control support.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3917c478bd9Sstevel@tonic-gate static rctl_qty_t
3927c478bd9Sstevel@tonic-gate project_cpu_shares_usage(rctl_t *rctl, struct proc *p)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
3957c478bd9Sstevel@tonic-gate 	return (p->p_task->tk_proj->kpj_shares);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3997c478bd9Sstevel@tonic-gate static int
4007c478bd9Sstevel@tonic-gate project_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
4017c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4047c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4057c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4067c478bd9Sstevel@tonic-gate 		return (0);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_shares = nv;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (0);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate static rctl_ops_t project_cpu_shares_ops = {
4157c478bd9Sstevel@tonic-gate 	rcop_no_action,
4167c478bd9Sstevel@tonic-gate 	project_cpu_shares_usage,
4177c478bd9Sstevel@tonic-gate 	project_cpu_shares_set,
4187c478bd9Sstevel@tonic-gate 	rcop_no_test
4197c478bd9Sstevel@tonic-gate };
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4227c478bd9Sstevel@tonic-gate static rctl_qty_t
4237c478bd9Sstevel@tonic-gate project_lwps_usage(rctl_t *r, proc_t *p)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	kproject_t *pj;
4267c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4297c478bd9Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
4307c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
4317c478bd9Sstevel@tonic-gate 	nlwps = pj->kpj_nlwps;
4327c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (nlwps);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4387c478bd9Sstevel@tonic-gate static int
4397c478bd9Sstevel@tonic-gate project_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
4407c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4457c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4467c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4477c478bd9Sstevel@tonic-gate 		return (0);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.proj->kpj_nlwps;
4507c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
4517c478bd9Sstevel@tonic-gate 		return (1);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	return (0);
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4577c478bd9Sstevel@tonic-gate static int
4587c478bd9Sstevel@tonic-gate project_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
4597c478bd9Sstevel@tonic-gate     rctl_qty_t nv) {
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4627c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4637c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4647c478bd9Sstevel@tonic-gate 		return (0);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_nlwps_ctl = nv;
4677c478bd9Sstevel@tonic-gate 	return (0);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate static rctl_ops_t project_lwps_ops = {
4717c478bd9Sstevel@tonic-gate 	rcop_no_action,
4727c478bd9Sstevel@tonic-gate 	project_lwps_usage,
4737c478bd9Sstevel@tonic-gate 	project_lwps_set,
4747c478bd9Sstevel@tonic-gate 	project_lwps_test,
4757c478bd9Sstevel@tonic-gate };
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4787c478bd9Sstevel@tonic-gate static rctl_qty_t
4797c478bd9Sstevel@tonic-gate project_ntasks_usage(rctl_t *r, proc_t *p)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	kproject_t *pj;
4827c478bd9Sstevel@tonic-gate 	rctl_qty_t ntasks;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4857c478bd9Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
4867c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
4877c478bd9Sstevel@tonic-gate 	ntasks = pj->kpj_ntasks;
4887c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	return (ntasks);
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4947c478bd9Sstevel@tonic-gate static int
4957c478bd9Sstevel@tonic-gate project_ntasks_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
4967c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	rctl_qty_t ntasks;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5017c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5027c478bd9Sstevel@tonic-gate 	ntasks = e->rcep_p.proj->kpj_ntasks;
5037c478bd9Sstevel@tonic-gate 	if (ntasks + incr > rcntl->rcv_value)
5047c478bd9Sstevel@tonic-gate 		return (1);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (0);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5107c478bd9Sstevel@tonic-gate static int
5117c478bd9Sstevel@tonic-gate project_ntasks_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
5127c478bd9Sstevel@tonic-gate     rctl_qty_t nv) {
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5157c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5167c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_ntasks_ctl = nv;
5177c478bd9Sstevel@tonic-gate 	return (0);
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate static rctl_ops_t project_tasks_ops = {
5217c478bd9Sstevel@tonic-gate 	rcop_no_action,
5227c478bd9Sstevel@tonic-gate 	project_ntasks_usage,
5237c478bd9Sstevel@tonic-gate 	project_ntasks_set,
5247c478bd9Sstevel@tonic-gate 	project_ntasks_test,
5257c478bd9Sstevel@tonic-gate };
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * project.max-shm-memory resource control support.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5327c478bd9Sstevel@tonic-gate static int
5337c478bd9Sstevel@tonic-gate project_shmmax_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
5347c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
5357c478bd9Sstevel@tonic-gate {
5367c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
5377c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5387c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5397c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_shmmax + inc;
5407c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
5417c478bd9Sstevel@tonic-gate 		return (1);
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	return (0);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate static rctl_ops_t project_shmmax_ops = {
5477c478bd9Sstevel@tonic-gate 	rcop_no_action,
5487c478bd9Sstevel@tonic-gate 	rcop_no_usage,
5497c478bd9Sstevel@tonic-gate 	rcop_no_set,
5507c478bd9Sstevel@tonic-gate 	project_shmmax_test
5517c478bd9Sstevel@tonic-gate };
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate  * project.max-shm-ids resource control support.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5587c478bd9Sstevel@tonic-gate static int
5597c478bd9Sstevel@tonic-gate project_shmmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
5607c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
5637c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5647c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
565*824c205fSml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_shmmni + inc;
5667c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
5677c478bd9Sstevel@tonic-gate 		return (1);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	return (0);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate static rctl_ops_t project_shmmni_ops = {
5737c478bd9Sstevel@tonic-gate 	rcop_no_action,
5747c478bd9Sstevel@tonic-gate 	rcop_no_usage,
5757c478bd9Sstevel@tonic-gate 	rcop_no_set,
5767c478bd9Sstevel@tonic-gate 	project_shmmni_test
5777c478bd9Sstevel@tonic-gate };
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * project.max-sem-ids resource control support.
5817c478bd9Sstevel@tonic-gate  */
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5847c478bd9Sstevel@tonic-gate static int
5857c478bd9Sstevel@tonic-gate project_semmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
5867c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
5877c478bd9Sstevel@tonic-gate {
5887c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
5897c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5907c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
591*824c205fSml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_semmni + inc;
5927c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
5937c478bd9Sstevel@tonic-gate 		return (1);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	return (0);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate static rctl_ops_t project_semmni_ops = {
5997c478bd9Sstevel@tonic-gate 	rcop_no_action,
6007c478bd9Sstevel@tonic-gate 	rcop_no_usage,
6017c478bd9Sstevel@tonic-gate 	rcop_no_set,
6027c478bd9Sstevel@tonic-gate 	project_semmni_test
6037c478bd9Sstevel@tonic-gate };
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * project.max-msg-ids resource control support.
6077c478bd9Sstevel@tonic-gate  */
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6107c478bd9Sstevel@tonic-gate static int
6117c478bd9Sstevel@tonic-gate project_msgmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6127c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
6157c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6167c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
617*824c205fSml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_msgmni + inc;
6187c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
6197c478bd9Sstevel@tonic-gate 		return (1);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	return (0);
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate static rctl_ops_t project_msgmni_ops = {
6257c478bd9Sstevel@tonic-gate 	rcop_no_action,
6267c478bd9Sstevel@tonic-gate 	rcop_no_usage,
6277c478bd9Sstevel@tonic-gate 	rcop_no_set,
6287c478bd9Sstevel@tonic-gate 	project_msgmni_test
6297c478bd9Sstevel@tonic-gate };
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate  * project.max-device-locked-memory resource control support.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6367c478bd9Sstevel@tonic-gate static int
6377c478bd9Sstevel@tonic-gate project_devlockmem_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6387c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
6417c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6427c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6437c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_devlockmem + inc;
6447c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
6457c478bd9Sstevel@tonic-gate 		return (1);
6467c478bd9Sstevel@tonic-gate 	return (0);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static rctl_ops_t project_devlockmem_ops = {
6507c478bd9Sstevel@tonic-gate 	rcop_no_action,
6517c478bd9Sstevel@tonic-gate 	rcop_no_usage,
6527c478bd9Sstevel@tonic-gate 	rcop_no_set,
6537c478bd9Sstevel@tonic-gate 	project_devlockmem_test
6547c478bd9Sstevel@tonic-gate };
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate  * project.max-contracts resource control support.
6587c478bd9Sstevel@tonic-gate  */
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6617c478bd9Sstevel@tonic-gate static int
6627c478bd9Sstevel@tonic-gate project_contract_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6637c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6687c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_contract + inc;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	if ((p->p_task != NULL) && (p->p_task->tk_proj) != NULL &&
6737c478bd9Sstevel@tonic-gate 	    (v > rval->rcv_value))
6747c478bd9Sstevel@tonic-gate 		return (1);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	return (0);
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate static rctl_ops_t project_contract_ops = {
6807c478bd9Sstevel@tonic-gate 	rcop_no_action,
6817c478bd9Sstevel@tonic-gate 	rcop_no_usage,
6827c478bd9Sstevel@tonic-gate 	rcop_no_set,
6837c478bd9Sstevel@tonic-gate 	project_contract_test
6847c478bd9Sstevel@tonic-gate };
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6877c478bd9Sstevel@tonic-gate static int
6887c478bd9Sstevel@tonic-gate project_crypto_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
6897c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t incr, uint_t flags)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
6927c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6937c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6947c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_crypto_mem + incr;
6957c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
6967c478bd9Sstevel@tonic-gate 		return (1);
6977c478bd9Sstevel@tonic-gate 	return (0);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate static rctl_ops_t project_crypto_mem_ops = {
7017c478bd9Sstevel@tonic-gate 	rcop_no_action,
7027c478bd9Sstevel@tonic-gate 	rcop_no_usage,
7037c478bd9Sstevel@tonic-gate 	rcop_no_set,
7047c478bd9Sstevel@tonic-gate 	project_crypto_test
7057c478bd9Sstevel@tonic-gate };
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate /*
7087c478bd9Sstevel@tonic-gate  * void project_init(void)
7097c478bd9Sstevel@tonic-gate  *
7107c478bd9Sstevel@tonic-gate  * Overview
7117c478bd9Sstevel@tonic-gate  *   Initialize the project subsystem, including the primordial project 0 entry.
7127c478bd9Sstevel@tonic-gate  *   Register generic project resource controls, if any.
7137c478bd9Sstevel@tonic-gate  *
7147c478bd9Sstevel@tonic-gate  * Return values
7157c478bd9Sstevel@tonic-gate  *   None.
7167c478bd9Sstevel@tonic-gate  *
7177c478bd9Sstevel@tonic-gate  * Caller's context
7187c478bd9Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate void
7217c478bd9Sstevel@tonic-gate project_init(void)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	rctl_qty_t shmmni, shmmax, qty;
7247c478bd9Sstevel@tonic-gate 	boolean_t check;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	projects_hash = mod_hash_create_extended("projects_hash",
7277c478bd9Sstevel@tonic-gate 	    project_hash_size, mod_hash_null_keydtor, project_hash_val_dtor,
7287c478bd9Sstevel@tonic-gate 	    project_hash_by_id,
7297c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)mod_hash_iddata_gen(project_hash_size),
7307c478bd9Sstevel@tonic-gate 	    project_hash_key_cmp, KM_SLEEP);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	rc_project_cpu_shares = rctl_register("project.cpu-shares",
7337c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_SIGNAL_NEVER |
7347c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_NEVER | RCTL_GLOBAL_NOBASIC |
73519f92332Sml93401 	    RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
73619f92332Sml93401 	    FSS_MAXSHARES, FSS_MAXSHARES,
7377c478bd9Sstevel@tonic-gate 	    &project_cpu_shares_ops);
7387c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.cpu-shares", 1, RCPRIV_PRIVILEGED,
7397c478bd9Sstevel@tonic-gate 	    RCTL_LOCAL_NOACTION);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	rc_project_nlwps = rctl_register("project.max-lwps", RCENTITY_PROJECT,
7427c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
7437c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_lwps_ops);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	rc_project_ntasks = rctl_register("project.max-tasks", RCENTITY_PROJECT,
7467c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
7477c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_tasks_ops);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * This rctl handle is used by /dev/crypto. It is here rather than
7517c478bd9Sstevel@tonic-gate 	 * in misc/kcf or the drv/crypto module because resource controls
7527c478bd9Sstevel@tonic-gate 	 * currently don't allow modules to be unloaded, and the control
7537c478bd9Sstevel@tonic-gate 	 * must be registered before init starts.
7547c478bd9Sstevel@tonic-gate 	 */
7557c478bd9Sstevel@tonic-gate 	rc_project_crypto_mem = rctl_register("project.max-crypto-memory",
7567c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
7577c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX,
7587c478bd9Sstevel@tonic-gate 	    &project_crypto_mem_ops);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
7617c478bd9Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
7647c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-crypto-memory", qty,
7657c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * System V IPC resource controls
7697c478bd9Sstevel@tonic-gate 	 */
7707c478bd9Sstevel@tonic-gate 	rc_project_semmni = rctl_register("project.max-sem-ids",
7717c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
7727c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_semmni_ops);
7737c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-sem-ids", "semsys",
7747c478bd9Sstevel@tonic-gate 	    "seminfo_semmni", 128, IPC_IDS_MAX);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	rc_project_msgmni = rctl_register("project.max-msg-ids",
7777c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
7787c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_msgmni_ops);
7797c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-msg-ids", "msgsys",
7807c478bd9Sstevel@tonic-gate 	    "msginfo_msgmni", 128, IPC_IDS_MAX);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	rc_project_shmmni = rctl_register("project.max-shm-ids",
7837c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
7847c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_shmmni_ops);
7857c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-shm-ids", "shmsys",
7867c478bd9Sstevel@tonic-gate 	    "shminfo_shmmni", 128, IPC_IDS_MAX);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	rc_project_shmmax = rctl_register("project.max-shm-memory",
7897c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
7907c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &project_shmmax_ops);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	check = B_FALSE;
7937c478bd9Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmni", &shmmni))
7947c478bd9Sstevel@tonic-gate 		shmmni = 100;
7957c478bd9Sstevel@tonic-gate 	else
7967c478bd9Sstevel@tonic-gate 		check = B_TRUE;
7977c478bd9Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmax", &shmmax))
7987c478bd9Sstevel@tonic-gate 		shmmax = 0x800000;
7997c478bd9Sstevel@tonic-gate 	else
8007c478bd9Sstevel@tonic-gate 		check = B_TRUE;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/*
8037c478bd9Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
8047c478bd9Sstevel@tonic-gate 	 */
8057c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
8067c478bd9Sstevel@tonic-gate 	if (check) {
8077c478bd9Sstevel@tonic-gate 		if ((shmmax > 0) && (UINT64_MAX / shmmax <= shmmni))
8087c478bd9Sstevel@tonic-gate 			qty = UINT64_MAX;
8097c478bd9Sstevel@tonic-gate 		else if (shmmni * shmmax > qty)
8107c478bd9Sstevel@tonic-gate 			qty = shmmni * shmmax;
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-shm-memory", qty,
8137c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/*
8167c478bd9Sstevel@tonic-gate 	 * Event Ports resource controls
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	rc_project_portids = rctl_register("project.max-port-ids",
8207c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8217c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, PORT_MAX_PORTS, PORT_MAX_PORTS,
8227c478bd9Sstevel@tonic-gate 	    &rctl_absolute_ops);
8237c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-port-ids", PORT_DEFAULT_PORTS,
8247c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	/*
8277c478bd9Sstevel@tonic-gate 	 * Resource control for locked memory
8287c478bd9Sstevel@tonic-gate 	 */
8297c478bd9Sstevel@tonic-gate 	rc_project_devlockmem = rctl_register(
8307c478bd9Sstevel@tonic-gate 	    "project.max-device-locked-memory", RCENTITY_PROJECT,
8317c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES,
8327c478bd9Sstevel@tonic-gate 	    UINT64_MAX, UINT64_MAX, &project_devlockmem_ops);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/*
8357c478bd9Sstevel@tonic-gate 	 * Defaults to 1/16th of the machine's memory
8367c478bd9Sstevel@tonic-gate 	 */
8377c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 4);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-device-locked-memory", qty,
8407c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/*
8437c478bd9Sstevel@tonic-gate 	 * Per project limit on contracts.
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 	rc_project_contract = rctl_register("project.max-contracts",
8467c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_COUNT,
8477c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_contract_ops);
8487c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-contracts", 10000,
8497c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	t0.t_proj = proj0p = project_hold_by_id(0, GLOBAL_ZONEID,
8527c478bd9Sstevel@tonic-gate 	    PROJECT_HOLD_INSERT);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
8557c478bd9Sstevel@tonic-gate 	proj0p->kpj_nlwps = p0.p_lwpcnt;
8567c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
8577c478bd9Sstevel@tonic-gate 	proj0p->kpj_ntasks = 1;
8587c478bd9Sstevel@tonic-gate }
859