xref: /onnv-gate/usr/src/uts/common/os/project.c (revision 3620:3e35d13fa9ae)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51996Sml93401  * Common Development and Distribution License (the "License").
61996Sml93401  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3620Skrishna  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/project.h>
290Sstevel@tonic-gate #include <sys/modhash.h>
300Sstevel@tonic-gate #include <sys/modctl.h>
310Sstevel@tonic-gate #include <sys/kmem.h>
323247Sgjelinek #include <sys/kstat.h>
330Sstevel@tonic-gate #include <sys/atomic.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/proc.h>
360Sstevel@tonic-gate #include <sys/rctl.h>
370Sstevel@tonic-gate #include <sys/sunddi.h>
380Sstevel@tonic-gate #include <sys/fss.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/ipc_impl.h>
410Sstevel@tonic-gate #include <sys/port_kernel.h>
420Sstevel@tonic-gate #include <sys/task.h>
430Sstevel@tonic-gate #include <sys/zone.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate int project_hash_size = 64;
460Sstevel@tonic-gate static kmutex_t project_hash_lock;
470Sstevel@tonic-gate static kmutex_t projects_list_lock;
480Sstevel@tonic-gate static mod_hash_t *projects_hash;
490Sstevel@tonic-gate static kproject_t *projects_list;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate rctl_hndl_t rc_project_cpu_shares;
520Sstevel@tonic-gate rctl_hndl_t rc_project_nlwps;
530Sstevel@tonic-gate rctl_hndl_t rc_project_ntasks;
540Sstevel@tonic-gate rctl_hndl_t rc_project_msgmni;
550Sstevel@tonic-gate rctl_hndl_t rc_project_semmni;
560Sstevel@tonic-gate rctl_hndl_t rc_project_shmmax;
570Sstevel@tonic-gate rctl_hndl_t rc_project_shmmni;
580Sstevel@tonic-gate rctl_hndl_t rc_project_portids;
592768Ssl108498 rctl_hndl_t rc_project_locked_mem;
600Sstevel@tonic-gate rctl_hndl_t rc_project_contract;
610Sstevel@tonic-gate rctl_hndl_t rc_project_crypto_mem;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Dummy structure used when comparing projects.  This structure must be kept
650Sstevel@tonic-gate  * identical to the first two fields of kproject_t.
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate struct project_zone {
680Sstevel@tonic-gate 	projid_t	kpj_id;
690Sstevel@tonic-gate 	zoneid_t	kpj_zoneid;
700Sstevel@tonic-gate };
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Projects
740Sstevel@tonic-gate  *
750Sstevel@tonic-gate  *   A dictionary of all active projects is maintained by the kernel so that we
760Sstevel@tonic-gate  *   may track project usage and limits.  (By an active project, we mean a
770Sstevel@tonic-gate  *   project associated with one or more task, and therefore with one or more
780Sstevel@tonic-gate  *   processes.) We build the dictionary on top of the mod_hash facility, since
790Sstevel@tonic-gate  *   project additions and deletions are relatively rare events.  An
800Sstevel@tonic-gate  *   integer-to-pointer mapping is maintained within the hash, representing the
810Sstevel@tonic-gate  *   map from project id to project structure.  All projects, including the
820Sstevel@tonic-gate  *   primordial "project 0", are allocated via the project_hold_by_id()
830Sstevel@tonic-gate  *   interface.
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  *   Currently, the project contains a reference count; the project ID, which is
860Sstevel@tonic-gate  *   examined by the extended accounting subsystem as well as /proc; a resource
870Sstevel@tonic-gate  *   control set, which contains the allowable values (and actions on exceeding
880Sstevel@tonic-gate  *   those values) for controlled project-level resources on the system; and a
890Sstevel@tonic-gate  *   number of CPU shares, which is used by the fair share scheduling class
900Sstevel@tonic-gate  *   (FSS) to support its proportion-based scheduling algorithm.
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * Reference counting convention
930Sstevel@tonic-gate  *   The dictionary entry does not itself count as a reference--only references
940Sstevel@tonic-gate  *   outside of the subsystem are tallied.  At the drop of the final external
950Sstevel@tonic-gate  *   reference, the project entry is removed.  The reference counter keeps
960Sstevel@tonic-gate  *   track of the number of threads *and* tasks within a project.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * Locking
990Sstevel@tonic-gate  *   Walking the doubly-linked project list must be done while holding
1000Sstevel@tonic-gate  *   projects_list_lock.  Thus, any dereference of kpj_next or kpj_prev must be
1010Sstevel@tonic-gate  *   under projects_list_lock.
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  *   If both the hash lock, project_hash_lock, and the list lock are to be
1040Sstevel@tonic-gate  *   acquired, the hash lock is to be acquired first.
1050Sstevel@tonic-gate  */
1060Sstevel@tonic-gate 
1073247Sgjelinek static kstat_t *project_kstat_create(kproject_t *pj, zone_t *zone);
1083247Sgjelinek static void project_kstat_delete(kproject_t *pj);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static void
1110Sstevel@tonic-gate project_data_init(kproject_data_t *data)
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Initialize subsystem-specific data
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	data->kpd_shmmax = 0;
1172677Sml93401 	data->kpd_ipc.ipcq_shmmni = 0;
1182677Sml93401 	data->kpd_ipc.ipcq_semmni = 0;
1192677Sml93401 	data->kpd_ipc.ipcq_msgmni = 0;
1202768Ssl108498 	data->kpd_locked_mem = 0;
1212768Ssl108498 	data->kpd_locked_mem_ctl = UINT64_MAX;
1220Sstevel@tonic-gate 	data->kpd_contract = 0;
1230Sstevel@tonic-gate 	data->kpd_crypto_mem = 0;
124*3620Skrishna 	data->kpd_crypto_mem_ctl = UINT64_MAX;
1253247Sgjelinek 	data->kpd_lockedmem_kstat = NULL;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate /*ARGSUSED*/
1290Sstevel@tonic-gate static uint_t
1300Sstevel@tonic-gate project_hash_by_id(void *hash_data, mod_hash_key_t key)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	struct project_zone *pz = key;
1330Sstevel@tonic-gate 	uint_t mykey;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	/*
1360Sstevel@tonic-gate 	 * Merge the zoneid and projectid together to a 32-bit quantity, and
1370Sstevel@tonic-gate 	 * then pass that in to the existing idhash.
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	mykey = (pz->kpj_zoneid << 16) | pz->kpj_id;
1400Sstevel@tonic-gate 	return (mod_hash_byid(hash_data, (mod_hash_key_t)(uintptr_t)mykey));
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static int
1440Sstevel@tonic-gate project_hash_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	struct project_zone *pz1 = key1, *pz2 = key2;
1470Sstevel@tonic-gate 	int retval;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return ((int)((retval = pz1->kpj_id - pz2->kpj_id) != 0 ? retval :
1500Sstevel@tonic-gate 	    pz1->kpj_zoneid - pz2->kpj_zoneid));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static void
1540Sstevel@tonic-gate project_hash_val_dtor(mod_hash_val_t val)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	kproject_t *kp = (kproject_t *)val;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	ASSERT(kp->kpj_count == 0);
1590Sstevel@tonic-gate 	kmem_free(kp, sizeof (kproject_t));
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * kproject_t *project_hold(kproject_t *)
1640Sstevel@tonic-gate  *
1650Sstevel@tonic-gate  * Overview
1660Sstevel@tonic-gate  *   Record that an additional reference on the indicated project has been
1670Sstevel@tonic-gate  *   taken.
1680Sstevel@tonic-gate  *
1690Sstevel@tonic-gate  * Return values
1700Sstevel@tonic-gate  *   A pointer to the indicated project.
1710Sstevel@tonic-gate  *
1720Sstevel@tonic-gate  * Caller's context
1730Sstevel@tonic-gate  *   project_hash_lock must not be held across the project_hold() call.
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate kproject_t *
1760Sstevel@tonic-gate project_hold(kproject_t *p)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
1790Sstevel@tonic-gate 	ASSERT(p != NULL);
1800Sstevel@tonic-gate 	p->kpj_count++;
1810Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
1820Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
1830Sstevel@tonic-gate 	return (p);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1873247Sgjelinek  * kproject_t *project_hold_by_id(projid_t, zone_t *, int)
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  * Overview
1900Sstevel@tonic-gate  *   project_hold_by_id() performs a look-up in the dictionary of projects
1913247Sgjelinek  *   active on the system by specified project ID + zone and puts a hold on
1920Sstevel@tonic-gate  *   it.  The third argument defines the desired behavior in the case when
1930Sstevel@tonic-gate  *   project with given project ID cannot be found:
1940Sstevel@tonic-gate  *
1950Sstevel@tonic-gate  *   PROJECT_HOLD_INSERT	New entry is made in dictionary and the project
1960Sstevel@tonic-gate  *   				is added to the global list.
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  *   PROJECT_HOLD_FIND		Return NULL.
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  *   The project is returned with its reference count incremented by one.
2010Sstevel@tonic-gate  *   A new project derives its resource controls from those of project 0.
2020Sstevel@tonic-gate  *
2030Sstevel@tonic-gate  * Return values
2040Sstevel@tonic-gate  *   A pointer to the held project.
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  * Caller's context
2070Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate kproject_t *
2103247Sgjelinek project_hold_by_id(projid_t id, zone_t *zone, int flag)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	kproject_t *spare_p;
2130Sstevel@tonic-gate 	kproject_t *p;
2140Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
2150Sstevel@tonic-gate 	rctl_set_t *set;
2160Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
2170Sstevel@tonic-gate 	rctl_entity_p_t e;
2180Sstevel@tonic-gate 	struct project_zone pz;
2193247Sgjelinek 	boolean_t create = B_FALSE;
2203247Sgjelinek 	kstat_t *ksp;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	pz.kpj_id = id;
2233247Sgjelinek 	pz.kpj_zoneid = zone->zone_id;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (flag == PROJECT_HOLD_FIND) {
2260Sstevel@tonic-gate 		mutex_enter(&project_hash_lock);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
2290Sstevel@tonic-gate 		    (mod_hash_val_t)&p) == MH_ERR_NOTFOUND)
2300Sstevel@tonic-gate 			p = NULL;
2310Sstevel@tonic-gate 		else
2320Sstevel@tonic-gate 			p->kpj_count++;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		mutex_exit(&project_hash_lock);
2350Sstevel@tonic-gate 		return (p);
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	ASSERT(flag == PROJECT_HOLD_INSERT);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	spare_p = kmem_zalloc(sizeof (kproject_t), KM_SLEEP);
2410Sstevel@tonic-gate 	set = rctl_set_create();
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_PROJECT);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	(void) mod_hash_reserve(projects_hash, &hndl);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
2480Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
2490Sstevel@tonic-gate 	if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
2500Sstevel@tonic-gate 	    (mod_hash_val_t *)&p) == MH_ERR_NOTFOUND) {
2513247Sgjelinek 
2520Sstevel@tonic-gate 		p = spare_p;
2530Sstevel@tonic-gate 		p->kpj_id = id;
2543247Sgjelinek 		p->kpj_zoneid = zone->zone_id;
2550Sstevel@tonic-gate 		p->kpj_count = 0;
2560Sstevel@tonic-gate 		p->kpj_shares = 1;
2570Sstevel@tonic-gate 		p->kpj_nlwps = 0;
2580Sstevel@tonic-gate 		p->kpj_ntasks = 0;
2590Sstevel@tonic-gate 		p->kpj_nlwps_ctl = INT_MAX;
2600Sstevel@tonic-gate 		p->kpj_ntasks_ctl = INT_MAX;
2610Sstevel@tonic-gate 		project_data_init(&p->kpj_data);
2620Sstevel@tonic-gate 		e.rcep_p.proj = p;
2630Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROJECT;
2640Sstevel@tonic-gate 		p->kpj_rctls = rctl_set_init(RCENTITY_PROJECT, curproc, &e,
2650Sstevel@tonic-gate 		    set, gp);
2660Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		if (mod_hash_insert_reserve(projects_hash, (mod_hash_key_t)p,
2690Sstevel@tonic-gate 		    (mod_hash_val_t)p, hndl))
2700Sstevel@tonic-gate 			panic("unable to insert project %d(%p)", id, (void *)p);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		/*
2730Sstevel@tonic-gate 		 * Insert project into global project list.
2740Sstevel@tonic-gate 		 */
2750Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
2763247Sgjelinek 		if (id != 0 || zone != &zone0) {
2770Sstevel@tonic-gate 			p->kpj_next = projects_list;
2780Sstevel@tonic-gate 			p->kpj_prev = projects_list->kpj_prev;
2790Sstevel@tonic-gate 			p->kpj_prev->kpj_next = p;
2800Sstevel@tonic-gate 			projects_list->kpj_prev = p;
2810Sstevel@tonic-gate 		} else {
2820Sstevel@tonic-gate 			/*
2830Sstevel@tonic-gate 			 * Special case: primordial hold on project 0.
2840Sstevel@tonic-gate 			 */
2850Sstevel@tonic-gate 			p->kpj_next = p;
2860Sstevel@tonic-gate 			p->kpj_prev = p;
2870Sstevel@tonic-gate 			projects_list = p;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
2903247Sgjelinek 		create = B_TRUE;
2910Sstevel@tonic-gate 	} else {
2920Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
2930Sstevel@tonic-gate 		mod_hash_cancel(projects_hash, &hndl);
2940Sstevel@tonic-gate 		kmem_free(spare_p, sizeof (kproject_t));
2950Sstevel@tonic-gate 		rctl_set_free(set);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
2990Sstevel@tonic-gate 	p->kpj_count++;
3000Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
3010Sstevel@tonic-gate 
3023247Sgjelinek 	/*
3033247Sgjelinek 	 * The kstat stores the project's zone name, as zoneid's may change
3043247Sgjelinek 	 * across reboots.
3053247Sgjelinek 	 */
3063247Sgjelinek 	if (create == B_TRUE) {
3073247Sgjelinek 		ksp = project_kstat_create(p, zone);
3083247Sgjelinek 		mutex_enter(&project_hash_lock);
3093247Sgjelinek 		ASSERT(p->kpj_data.kpd_lockedmem_kstat == NULL);
3103247Sgjelinek 		p->kpj_data.kpd_lockedmem_kstat = ksp;
3113247Sgjelinek 		mutex_exit(&project_hash_lock);
3123247Sgjelinek 	}
3130Sstevel@tonic-gate 	return (p);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * void project_rele(kproject_t *)
3180Sstevel@tonic-gate  *
3190Sstevel@tonic-gate  * Overview
3200Sstevel@tonic-gate  *   Advertise that one external reference to this project is no longer needed.
3210Sstevel@tonic-gate  *
3220Sstevel@tonic-gate  * Return values
3230Sstevel@tonic-gate  *   None.
3240Sstevel@tonic-gate  *
3250Sstevel@tonic-gate  * Caller's context
3260Sstevel@tonic-gate  *   No restriction on context.
3270Sstevel@tonic-gate  */
3280Sstevel@tonic-gate void
3290Sstevel@tonic-gate project_rele(kproject_t *p)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
3320Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
3330Sstevel@tonic-gate 	p->kpj_count--;
3340Sstevel@tonic-gate 	if (p->kpj_count == 0) {
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 		/*
3370Sstevel@tonic-gate 		 * Remove project from global list.
3380Sstevel@tonic-gate 		 */
3390Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
3400Sstevel@tonic-gate 		p->kpj_next->kpj_prev = p->kpj_prev;
3410Sstevel@tonic-gate 		p->kpj_prev->kpj_next = p->kpj_next;
3420Sstevel@tonic-gate 		if (projects_list == p)
3430Sstevel@tonic-gate 			projects_list = p->kpj_next;
3440Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		rctl_set_free(p->kpj_rctls);
3473247Sgjelinek 		project_kstat_delete(p);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		if (mod_hash_destroy(projects_hash, (mod_hash_key_t)p))
3500Sstevel@tonic-gate 			panic("unable to delete project %d zone %d", p->kpj_id,
3510Sstevel@tonic-gate 			    p->kpj_zoneid);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * int project_walk_all(zoneid_t, int (*)(kproject_t *, void *), void *)
3590Sstevel@tonic-gate  *
3600Sstevel@tonic-gate  * Overview
3610Sstevel@tonic-gate  *   Walk the project list for the given zoneid with a callback.
3620Sstevel@tonic-gate  *
3630Sstevel@tonic-gate  * Return values
3640Sstevel@tonic-gate  *   -1 for an invalid walk, number of projects visited otherwise.
3650Sstevel@tonic-gate  *
3660Sstevel@tonic-gate  * Caller's context
3670Sstevel@tonic-gate  *   projects_list_lock must not be held, as it is acquired by
3680Sstevel@tonic-gate  *   project_walk_all().  Accordingly, callbacks may not perform KM_SLEEP
3690Sstevel@tonic-gate  *   allocations.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate int
3720Sstevel@tonic-gate project_walk_all(zoneid_t zoneid, int (*cb)(kproject_t *, void *),
3730Sstevel@tonic-gate     void *walk_data)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	int cnt = 0;
3760Sstevel@tonic-gate 	kproject_t *kp = proj0p;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	mutex_enter(&projects_list_lock);
3790Sstevel@tonic-gate 	do {
3800Sstevel@tonic-gate 		if (zoneid != ALL_ZONES && kp->kpj_zoneid != zoneid)
3810Sstevel@tonic-gate 			continue;
3820Sstevel@tonic-gate 		if (cb(kp, walk_data) == -1) {
3830Sstevel@tonic-gate 			cnt = -1;
3840Sstevel@tonic-gate 			break;
3850Sstevel@tonic-gate 		} else {
3860Sstevel@tonic-gate 			cnt++;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	} while ((kp = kp->kpj_next) != proj0p);
3890Sstevel@tonic-gate 	mutex_exit(&projects_list_lock);
3900Sstevel@tonic-gate 	return (cnt);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * projid_t curprojid(void)
3950Sstevel@tonic-gate  *
3960Sstevel@tonic-gate  * Overview
3970Sstevel@tonic-gate  *   Return project ID of the current thread
3980Sstevel@tonic-gate  *
3990Sstevel@tonic-gate  * Caller's context
4000Sstevel@tonic-gate  *   No restrictions.
4010Sstevel@tonic-gate  */
4020Sstevel@tonic-gate projid_t
4030Sstevel@tonic-gate curprojid()
4040Sstevel@tonic-gate {
4050Sstevel@tonic-gate 	return (ttoproj(curthread)->kpj_id);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate  * project.cpu-shares resource control support.
4100Sstevel@tonic-gate  */
4110Sstevel@tonic-gate /*ARGSUSED*/
4120Sstevel@tonic-gate static rctl_qty_t
4130Sstevel@tonic-gate project_cpu_shares_usage(rctl_t *rctl, struct proc *p)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4160Sstevel@tonic-gate 	return (p->p_task->tk_proj->kpj_shares);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate /*ARGSUSED*/
4200Sstevel@tonic-gate static int
4210Sstevel@tonic-gate project_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
4220Sstevel@tonic-gate     rctl_qty_t nv)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4250Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4260Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4270Sstevel@tonic-gate 		return (0);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	e->rcep_p.proj->kpj_shares = nv;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	return (0);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate static rctl_ops_t project_cpu_shares_ops = {
4360Sstevel@tonic-gate 	rcop_no_action,
4370Sstevel@tonic-gate 	project_cpu_shares_usage,
4380Sstevel@tonic-gate 	project_cpu_shares_set,
4390Sstevel@tonic-gate 	rcop_no_test
4400Sstevel@tonic-gate };
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*ARGSUSED*/
4430Sstevel@tonic-gate static rctl_qty_t
4440Sstevel@tonic-gate project_lwps_usage(rctl_t *r, proc_t *p)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	kproject_t *pj;
4470Sstevel@tonic-gate 	rctl_qty_t nlwps;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4500Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
4510Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
4520Sstevel@tonic-gate 	nlwps = pj->kpj_nlwps;
4530Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	return (nlwps);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /*ARGSUSED*/
4590Sstevel@tonic-gate static int
4600Sstevel@tonic-gate project_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
4610Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	rctl_qty_t nlwps;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4662768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_zone->zone_nlwps_lock));
4670Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4680Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4690Sstevel@tonic-gate 		return (0);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	nlwps = e->rcep_p.proj->kpj_nlwps;
4720Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
4730Sstevel@tonic-gate 		return (1);
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	return (0);
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate /*ARGSUSED*/
4790Sstevel@tonic-gate static int
4800Sstevel@tonic-gate project_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
4810Sstevel@tonic-gate     rctl_qty_t nv) {
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
4840Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
4850Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
4860Sstevel@tonic-gate 		return (0);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	e->rcep_p.proj->kpj_nlwps_ctl = nv;
4890Sstevel@tonic-gate 	return (0);
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate static rctl_ops_t project_lwps_ops = {
4930Sstevel@tonic-gate 	rcop_no_action,
4940Sstevel@tonic-gate 	project_lwps_usage,
4950Sstevel@tonic-gate 	project_lwps_set,
4960Sstevel@tonic-gate 	project_lwps_test,
4970Sstevel@tonic-gate };
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate /*ARGSUSED*/
5000Sstevel@tonic-gate static rctl_qty_t
5010Sstevel@tonic-gate project_ntasks_usage(rctl_t *r, proc_t *p)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate 	kproject_t *pj;
5040Sstevel@tonic-gate 	rctl_qty_t ntasks;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5070Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
5080Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
5090Sstevel@tonic-gate 	ntasks = pj->kpj_ntasks;
5100Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	return (ntasks);
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate /*ARGSUSED*/
5160Sstevel@tonic-gate static int
5170Sstevel@tonic-gate project_ntasks_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
5180Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
5190Sstevel@tonic-gate {
5200Sstevel@tonic-gate 	rctl_qty_t ntasks;
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5230Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5240Sstevel@tonic-gate 	ntasks = e->rcep_p.proj->kpj_ntasks;
5250Sstevel@tonic-gate 	if (ntasks + incr > rcntl->rcv_value)
5260Sstevel@tonic-gate 		return (1);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	return (0);
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate /*ARGSUSED*/
5320Sstevel@tonic-gate static int
5330Sstevel@tonic-gate project_ntasks_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
5340Sstevel@tonic-gate     rctl_qty_t nv) {
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5370Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5380Sstevel@tonic-gate 	e->rcep_p.proj->kpj_ntasks_ctl = nv;
5390Sstevel@tonic-gate 	return (0);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static rctl_ops_t project_tasks_ops = {
5430Sstevel@tonic-gate 	rcop_no_action,
5440Sstevel@tonic-gate 	project_ntasks_usage,
5450Sstevel@tonic-gate 	project_ntasks_set,
5460Sstevel@tonic-gate 	project_ntasks_test,
5470Sstevel@tonic-gate };
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate /*
5500Sstevel@tonic-gate  * project.max-shm-memory resource control support.
5510Sstevel@tonic-gate  */
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*ARGSUSED*/
5540Sstevel@tonic-gate static int
5550Sstevel@tonic-gate project_shmmax_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
5560Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	rctl_qty_t v;
5590Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5600Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5610Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_shmmax + inc;
5620Sstevel@tonic-gate 	if (v > rval->rcv_value)
5630Sstevel@tonic-gate 		return (1);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	return (0);
5660Sstevel@tonic-gate }
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate static rctl_ops_t project_shmmax_ops = {
5690Sstevel@tonic-gate 	rcop_no_action,
5700Sstevel@tonic-gate 	rcop_no_usage,
5710Sstevel@tonic-gate 	rcop_no_set,
5720Sstevel@tonic-gate 	project_shmmax_test
5730Sstevel@tonic-gate };
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5760Sstevel@tonic-gate  * project.max-shm-ids resource control support.
5770Sstevel@tonic-gate  */
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate /*ARGSUSED*/
5800Sstevel@tonic-gate static int
5810Sstevel@tonic-gate project_shmmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
5820Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	rctl_qty_t v;
5850Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
5860Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
5872677Sml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_shmmni + inc;
5880Sstevel@tonic-gate 	if (v > rval->rcv_value)
5890Sstevel@tonic-gate 		return (1);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	return (0);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate static rctl_ops_t project_shmmni_ops = {
5950Sstevel@tonic-gate 	rcop_no_action,
5960Sstevel@tonic-gate 	rcop_no_usage,
5970Sstevel@tonic-gate 	rcop_no_set,
5980Sstevel@tonic-gate 	project_shmmni_test
5990Sstevel@tonic-gate };
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * project.max-sem-ids resource control support.
6030Sstevel@tonic-gate  */
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate /*ARGSUSED*/
6060Sstevel@tonic-gate static int
6070Sstevel@tonic-gate project_semmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6080Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6090Sstevel@tonic-gate {
6100Sstevel@tonic-gate 	rctl_qty_t v;
6110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6120Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6132677Sml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_semmni + inc;
6140Sstevel@tonic-gate 	if (v > rval->rcv_value)
6150Sstevel@tonic-gate 		return (1);
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	return (0);
6180Sstevel@tonic-gate }
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate static rctl_ops_t project_semmni_ops = {
6210Sstevel@tonic-gate 	rcop_no_action,
6220Sstevel@tonic-gate 	rcop_no_usage,
6230Sstevel@tonic-gate 	rcop_no_set,
6240Sstevel@tonic-gate 	project_semmni_test
6250Sstevel@tonic-gate };
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate /*
6280Sstevel@tonic-gate  * project.max-msg-ids resource control support.
6290Sstevel@tonic-gate  */
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*ARGSUSED*/
6320Sstevel@tonic-gate static int
6330Sstevel@tonic-gate project_msgmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6340Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	rctl_qty_t v;
6370Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6380Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6392677Sml93401 	v = e->rcep_p.proj->kpj_data.kpd_ipc.ipcq_msgmni + inc;
6400Sstevel@tonic-gate 	if (v > rval->rcv_value)
6410Sstevel@tonic-gate 		return (1);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (0);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate static rctl_ops_t project_msgmni_ops = {
6470Sstevel@tonic-gate 	rcop_no_action,
6480Sstevel@tonic-gate 	rcop_no_usage,
6490Sstevel@tonic-gate 	rcop_no_set,
6500Sstevel@tonic-gate 	project_msgmni_test
6510Sstevel@tonic-gate };
6520Sstevel@tonic-gate 
6532768Ssl108498 /*ARGSUSED*/
6542768Ssl108498 static rctl_qty_t
6552768Ssl108498 project_locked_mem_usage(rctl_t *rctl, struct proc *p)
6562768Ssl108498 {
6572768Ssl108498 	rctl_qty_t q;
6582768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
6593247Sgjelinek 	mutex_enter(&p->p_zone->zone_mem_lock);
6602768Ssl108498 	q = p->p_task->tk_proj->kpj_data.kpd_locked_mem;
6613247Sgjelinek 	mutex_exit(&p->p_zone->zone_mem_lock);
6622768Ssl108498 	return (q);
6632768Ssl108498 }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate /*ARGSUSED*/
6660Sstevel@tonic-gate static int
6672768Ssl108498 project_locked_mem_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
6680Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
6690Sstevel@tonic-gate {
6702768Ssl108498 	rctl_qty_t q;
6710Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6723247Sgjelinek 	ASSERT(MUTEX_HELD(&p->p_zone->zone_mem_lock));
6732768Ssl108498 	q = p->p_task->tk_proj->kpj_data.kpd_locked_mem;
6742768Ssl108498 	if (q + inc > rval->rcv_value)
6750Sstevel@tonic-gate 		return (1);
6760Sstevel@tonic-gate 	return (0);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6792768Ssl108498 /*ARGSUSED*/
6802768Ssl108498 static int
6812768Ssl108498 project_locked_mem_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
6822768Ssl108498     rctl_qty_t nv) {
6832768Ssl108498 
6842768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
6852768Ssl108498 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
6862768Ssl108498 	if (e->rcep_p.proj == NULL)
6872768Ssl108498 		return (0);
6882768Ssl108498 
6892768Ssl108498 	e->rcep_p.proj->kpj_data.kpd_locked_mem_ctl = nv;
6902768Ssl108498 	return (0);
6912768Ssl108498 }
6922768Ssl108498 
6932768Ssl108498 static rctl_ops_t project_locked_mem_ops = {
6940Sstevel@tonic-gate 	rcop_no_action,
6952768Ssl108498 	project_locked_mem_usage,
6962768Ssl108498 	project_locked_mem_set,
6972768Ssl108498 	project_locked_mem_test
6980Sstevel@tonic-gate };
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate /*
7010Sstevel@tonic-gate  * project.max-contracts resource control support.
7020Sstevel@tonic-gate  */
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate /*ARGSUSED*/
7050Sstevel@tonic-gate static int
7060Sstevel@tonic-gate project_contract_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
7070Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate 	rctl_qty_t v;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
7120Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_contract + inc;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if ((p->p_task != NULL) && (p->p_task->tk_proj) != NULL &&
7170Sstevel@tonic-gate 	    (v > rval->rcv_value))
7180Sstevel@tonic-gate 		return (1);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	return (0);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate static rctl_ops_t project_contract_ops = {
7240Sstevel@tonic-gate 	rcop_no_action,
7250Sstevel@tonic-gate 	rcop_no_usage,
7260Sstevel@tonic-gate 	rcop_no_set,
7270Sstevel@tonic-gate 	project_contract_test
7280Sstevel@tonic-gate };
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate /*ARGSUSED*/
731*3620Skrishna static rctl_qty_t
732*3620Skrishna project_crypto_usage(rctl_t *r, proc_t *p)
733*3620Skrishna {
734*3620Skrishna 	ASSERT(MUTEX_HELD(&p->p_lock));
735*3620Skrishna 	return (p->p_task->tk_proj->kpj_data.kpd_crypto_mem);
736*3620Skrishna }
737*3620Skrishna 
738*3620Skrishna /*ARGSUSED*/
739*3620Skrishna static int
740*3620Skrishna project_crypto_set(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
741*3620Skrishna     rctl_qty_t nv)
742*3620Skrishna {
743*3620Skrishna 	ASSERT(MUTEX_HELD(&p->p_lock));
744*3620Skrishna 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
745*3620Skrishna 	if (e->rcep_p.proj == NULL)
746*3620Skrishna 		return (0);
747*3620Skrishna 
748*3620Skrishna 	e->rcep_p.proj->kpj_data.kpd_crypto_mem_ctl = nv;
749*3620Skrishna 	return (0);
750*3620Skrishna }
751*3620Skrishna 
752*3620Skrishna /*ARGSUSED*/
7530Sstevel@tonic-gate static int
7540Sstevel@tonic-gate project_crypto_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
7550Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t incr, uint_t flags)
7560Sstevel@tonic-gate {
7570Sstevel@tonic-gate 	rctl_qty_t v;
7580Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
7590Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
7600Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_crypto_mem + incr;
7610Sstevel@tonic-gate 	if (v > rval->rcv_value)
7620Sstevel@tonic-gate 		return (1);
7630Sstevel@tonic-gate 	return (0);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate static rctl_ops_t project_crypto_mem_ops = {
7670Sstevel@tonic-gate 	rcop_no_action,
768*3620Skrishna 	project_crypto_usage,
769*3620Skrishna 	project_crypto_set,
7700Sstevel@tonic-gate 	project_crypto_test
7710Sstevel@tonic-gate };
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate  * void project_init(void)
7750Sstevel@tonic-gate  *
7760Sstevel@tonic-gate  * Overview
7770Sstevel@tonic-gate  *   Initialize the project subsystem, including the primordial project 0 entry.
7780Sstevel@tonic-gate  *   Register generic project resource controls, if any.
7790Sstevel@tonic-gate  *
7800Sstevel@tonic-gate  * Return values
7810Sstevel@tonic-gate  *   None.
7820Sstevel@tonic-gate  *
7830Sstevel@tonic-gate  * Caller's context
7840Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
7850Sstevel@tonic-gate  */
7860Sstevel@tonic-gate void
7870Sstevel@tonic-gate project_init(void)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	rctl_qty_t shmmni, shmmax, qty;
7900Sstevel@tonic-gate 	boolean_t check;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	projects_hash = mod_hash_create_extended("projects_hash",
7930Sstevel@tonic-gate 	    project_hash_size, mod_hash_null_keydtor, project_hash_val_dtor,
7940Sstevel@tonic-gate 	    project_hash_by_id,
7950Sstevel@tonic-gate 	    (void *)(uintptr_t)mod_hash_iddata_gen(project_hash_size),
7960Sstevel@tonic-gate 	    project_hash_key_cmp, KM_SLEEP);
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	rc_project_cpu_shares = rctl_register("project.cpu-shares",
7990Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_SIGNAL_NEVER |
8000Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_NEVER | RCTL_GLOBAL_NOBASIC |
8011996Sml93401 	    RCTL_GLOBAL_COUNT | RCTL_GLOBAL_SYSLOG_NEVER,
8021996Sml93401 	    FSS_MAXSHARES, FSS_MAXSHARES,
8030Sstevel@tonic-gate 	    &project_cpu_shares_ops);
8040Sstevel@tonic-gate 	rctl_add_default_limit("project.cpu-shares", 1, RCPRIV_PRIVILEGED,
8050Sstevel@tonic-gate 	    RCTL_LOCAL_NOACTION);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	rc_project_nlwps = rctl_register("project.max-lwps", RCENTITY_PROJECT,
8080Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
8090Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_lwps_ops);
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	rc_project_ntasks = rctl_register("project.max-tasks", RCENTITY_PROJECT,
8120Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
8130Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_tasks_ops);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * This rctl handle is used by /dev/crypto. It is here rather than
8170Sstevel@tonic-gate 	 * in misc/kcf or the drv/crypto module because resource controls
8180Sstevel@tonic-gate 	 * currently don't allow modules to be unloaded, and the control
8190Sstevel@tonic-gate 	 * must be registered before init starts.
8200Sstevel@tonic-gate 	 */
8210Sstevel@tonic-gate 	rc_project_crypto_mem = rctl_register("project.max-crypto-memory",
8220Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8230Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX,
8240Sstevel@tonic-gate 	    &project_crypto_mem_ops);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	/*
8270Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
8300Sstevel@tonic-gate 	rctl_add_default_limit("project.max-crypto-memory", qty,
8310Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	/*
8340Sstevel@tonic-gate 	 * System V IPC resource controls
8350Sstevel@tonic-gate 	 */
8360Sstevel@tonic-gate 	rc_project_semmni = rctl_register("project.max-sem-ids",
8370Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8380Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_semmni_ops);
8390Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-sem-ids", "semsys",
8400Sstevel@tonic-gate 	    "seminfo_semmni", 128, IPC_IDS_MAX);
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	rc_project_msgmni = rctl_register("project.max-msg-ids",
8430Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8440Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_msgmni_ops);
8450Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-msg-ids", "msgsys",
8460Sstevel@tonic-gate 	    "msginfo_msgmni", 128, IPC_IDS_MAX);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	rc_project_shmmni = rctl_register("project.max-shm-ids",
8490Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8500Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_shmmni_ops);
8510Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-shm-ids", "shmsys",
8520Sstevel@tonic-gate 	    "shminfo_shmmni", 128, IPC_IDS_MAX);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	rc_project_shmmax = rctl_register("project.max-shm-memory",
8550Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8560Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &project_shmmax_ops);
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	check = B_FALSE;
8590Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmni", &shmmni))
8600Sstevel@tonic-gate 		shmmni = 100;
8610Sstevel@tonic-gate 	else
8620Sstevel@tonic-gate 		check = B_TRUE;
8630Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmax", &shmmax))
8640Sstevel@tonic-gate 		shmmax = 0x800000;
8650Sstevel@tonic-gate 	else
8660Sstevel@tonic-gate 		check = B_TRUE;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	/*
8690Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
8700Sstevel@tonic-gate 	 */
8710Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
8720Sstevel@tonic-gate 	if (check) {
8730Sstevel@tonic-gate 		if ((shmmax > 0) && (UINT64_MAX / shmmax <= shmmni))
8740Sstevel@tonic-gate 			qty = UINT64_MAX;
8750Sstevel@tonic-gate 		else if (shmmni * shmmax > qty)
8760Sstevel@tonic-gate 			qty = shmmni * shmmax;
8770Sstevel@tonic-gate 	}
8780Sstevel@tonic-gate 	rctl_add_default_limit("project.max-shm-memory", qty,
8790Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/*
8820Sstevel@tonic-gate 	 * Event Ports resource controls
8830Sstevel@tonic-gate 	 */
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	rc_project_portids = rctl_register("project.max-port-ids",
8860Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
8870Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, PORT_MAX_PORTS, PORT_MAX_PORTS,
8880Sstevel@tonic-gate 	    &rctl_absolute_ops);
8890Sstevel@tonic-gate 	rctl_add_default_limit("project.max-port-ids", PORT_DEFAULT_PORTS,
8900Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Resource control for locked memory
8940Sstevel@tonic-gate 	 */
8952768Ssl108498 	rc_project_locked_mem = rctl_register(
8962768Ssl108498 	    "project.max-locked-memory", RCENTITY_PROJECT,
8970Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES,
8982768Ssl108498 	    UINT64_MAX, UINT64_MAX, &project_locked_mem_ops);
8990Sstevel@tonic-gate 
9002768Ssl108498 	/* Default value equals that of max-shm-memory. */
9012768Ssl108498 	rctl_add_default_limit("project.max-locked-memory", qty,
9020Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Per project limit on contracts.
9060Sstevel@tonic-gate 	 */
9070Sstevel@tonic-gate 	rc_project_contract = rctl_register("project.max-contracts",
9080Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_COUNT,
9090Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_contract_ops);
9100Sstevel@tonic-gate 	rctl_add_default_limit("project.max-contracts", 10000,
9110Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
9120Sstevel@tonic-gate 
9133247Sgjelinek 	t0.t_proj = proj0p = project_hold_by_id(0, &zone0,
9140Sstevel@tonic-gate 	    PROJECT_HOLD_INSERT);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
9170Sstevel@tonic-gate 	proj0p->kpj_nlwps = p0.p_lwpcnt;
9180Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
9190Sstevel@tonic-gate 	proj0p->kpj_ntasks = 1;
9200Sstevel@tonic-gate }
9213247Sgjelinek 
9223247Sgjelinek static int
9233247Sgjelinek project_lockedmem_kstat_update(kstat_t *ksp, int rw)
9243247Sgjelinek {
9253247Sgjelinek 	kproject_t *pj = ksp->ks_private;
9263247Sgjelinek 	kproject_kstat_t *kpk = ksp->ks_data;
9273247Sgjelinek 
9283247Sgjelinek 	if (rw == KSTAT_WRITE)
9293247Sgjelinek 		return (EACCES);
9303247Sgjelinek 
9313247Sgjelinek 	kpk->kpk_usage.value.ui64 = pj->kpj_data.kpd_locked_mem;
9323247Sgjelinek 	kpk->kpk_value.value.ui64 = pj->kpj_data.kpd_locked_mem_ctl;
9333247Sgjelinek 	return (0);
9343247Sgjelinek }
9353247Sgjelinek 
9363247Sgjelinek static kstat_t *
9373247Sgjelinek project_kstat_create(kproject_t *pj, zone_t *zone)
9383247Sgjelinek {
9393247Sgjelinek 	kstat_t *ksp;
9403247Sgjelinek 	kproject_kstat_t *kpk;
9413247Sgjelinek 	char *zonename = zone->zone_name;
9423247Sgjelinek 
9433247Sgjelinek 	ksp = rctl_kstat_create_project(pj, "lockedmem", KSTAT_TYPE_NAMED,
9443247Sgjelinek 	    sizeof (kproject_kstat_t) / sizeof (kstat_named_t),
9453247Sgjelinek 	    KSTAT_FLAG_VIRTUAL);
9463247Sgjelinek 
9473247Sgjelinek 	if (ksp == NULL)
9483247Sgjelinek 		return (NULL);
9493247Sgjelinek 
9503247Sgjelinek 	kpk = ksp->ks_data = kmem_alloc(sizeof (kproject_kstat_t), KM_SLEEP);
9513247Sgjelinek 	ksp->ks_data_size += strlen(zonename) + 1;
9523247Sgjelinek 	kstat_named_init(&kpk->kpk_zonename, "zonename", KSTAT_DATA_STRING);
9533247Sgjelinek 	kstat_named_setstr(&kpk->kpk_zonename, zonename);
9543247Sgjelinek 	kstat_named_init(&kpk->kpk_usage, "usage", KSTAT_DATA_UINT64);
9553247Sgjelinek 	kstat_named_init(&kpk->kpk_value, "value", KSTAT_DATA_UINT64);
9563247Sgjelinek 	ksp->ks_update = project_lockedmem_kstat_update;
9573247Sgjelinek 	ksp->ks_private = pj;
9583247Sgjelinek 	kstat_install(ksp);
9593247Sgjelinek 
9603247Sgjelinek 	return (ksp);
9613247Sgjelinek }
9623247Sgjelinek 
9633247Sgjelinek static void
9643247Sgjelinek project_kstat_delete(kproject_t *pj)
9653247Sgjelinek {
9663247Sgjelinek 	void *data;
9673247Sgjelinek 
9683247Sgjelinek 	if (pj->kpj_data.kpd_lockedmem_kstat != NULL) {
9693247Sgjelinek 		data = pj->kpj_data.kpd_lockedmem_kstat->ks_data;
9703247Sgjelinek 		kstat_delete(pj->kpj_data.kpd_lockedmem_kstat);
9713247Sgjelinek 		kmem_free(data, sizeof (zone_kstat_t));
9723247Sgjelinek 	}
9733247Sgjelinek 	pj->kpj_data.kpd_lockedmem_kstat = NULL;
9743247Sgjelinek }
975