xref: /illumos-gate/usr/src/uts/common/os/project.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/fss.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/port_kernel.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/zone.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate int project_hash_size = 64;
46*7c478bd9Sstevel@tonic-gate static kmutex_t project_hash_lock;
47*7c478bd9Sstevel@tonic-gate static kmutex_t projects_list_lock;
48*7c478bd9Sstevel@tonic-gate static mod_hash_t *projects_hash;
49*7c478bd9Sstevel@tonic-gate static kproject_t *projects_list;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_cpu_shares;
52*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_nlwps;
53*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_ntasks;
54*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_msgmni;
55*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_semmni;
56*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_shmmax;
57*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_shmmni;
58*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_portids;
59*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_devlockmem;
60*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_contract;
61*7c478bd9Sstevel@tonic-gate rctl_hndl_t rc_project_crypto_mem;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  * Dummy structure used when comparing projects.  This structure must be kept
65*7c478bd9Sstevel@tonic-gate  * identical to the first two fields of kproject_t.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate struct project_zone {
68*7c478bd9Sstevel@tonic-gate 	projid_t	kpj_id;
69*7c478bd9Sstevel@tonic-gate 	zoneid_t	kpj_zoneid;
70*7c478bd9Sstevel@tonic-gate };
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate /*
73*7c478bd9Sstevel@tonic-gate  * Projects
74*7c478bd9Sstevel@tonic-gate  *
75*7c478bd9Sstevel@tonic-gate  *   A dictionary of all active projects is maintained by the kernel so that we
76*7c478bd9Sstevel@tonic-gate  *   may track project usage and limits.  (By an active project, we mean a
77*7c478bd9Sstevel@tonic-gate  *   project associated with one or more task, and therefore with one or more
78*7c478bd9Sstevel@tonic-gate  *   processes.) We build the dictionary on top of the mod_hash facility, since
79*7c478bd9Sstevel@tonic-gate  *   project additions and deletions are relatively rare events.  An
80*7c478bd9Sstevel@tonic-gate  *   integer-to-pointer mapping is maintained within the hash, representing the
81*7c478bd9Sstevel@tonic-gate  *   map from project id to project structure.  All projects, including the
82*7c478bd9Sstevel@tonic-gate  *   primordial "project 0", are allocated via the project_hold_by_id()
83*7c478bd9Sstevel@tonic-gate  *   interface.
84*7c478bd9Sstevel@tonic-gate  *
85*7c478bd9Sstevel@tonic-gate  *   Currently, the project contains a reference count; the project ID, which is
86*7c478bd9Sstevel@tonic-gate  *   examined by the extended accounting subsystem as well as /proc; a resource
87*7c478bd9Sstevel@tonic-gate  *   control set, which contains the allowable values (and actions on exceeding
88*7c478bd9Sstevel@tonic-gate  *   those values) for controlled project-level resources on the system; and a
89*7c478bd9Sstevel@tonic-gate  *   number of CPU shares, which is used by the fair share scheduling class
90*7c478bd9Sstevel@tonic-gate  *   (FSS) to support its proportion-based scheduling algorithm.
91*7c478bd9Sstevel@tonic-gate  *
92*7c478bd9Sstevel@tonic-gate  * Reference counting convention
93*7c478bd9Sstevel@tonic-gate  *   The dictionary entry does not itself count as a reference--only references
94*7c478bd9Sstevel@tonic-gate  *   outside of the subsystem are tallied.  At the drop of the final external
95*7c478bd9Sstevel@tonic-gate  *   reference, the project entry is removed.  The reference counter keeps
96*7c478bd9Sstevel@tonic-gate  *   track of the number of threads *and* tasks within a project.
97*7c478bd9Sstevel@tonic-gate  *
98*7c478bd9Sstevel@tonic-gate  * Locking
99*7c478bd9Sstevel@tonic-gate  *   Walking the doubly-linked project list must be done while holding
100*7c478bd9Sstevel@tonic-gate  *   projects_list_lock.  Thus, any dereference of kpj_next or kpj_prev must be
101*7c478bd9Sstevel@tonic-gate  *   under projects_list_lock.
102*7c478bd9Sstevel@tonic-gate  *
103*7c478bd9Sstevel@tonic-gate  *   If both the hash lock, project_hash_lock, and the list lock are to be
104*7c478bd9Sstevel@tonic-gate  *   acquired, the hash lock is to be acquired first.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate static void
109*7c478bd9Sstevel@tonic-gate project_data_init(kproject_data_t *data)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	/*
112*7c478bd9Sstevel@tonic-gate 	 * Initialize subsystem-specific data
113*7c478bd9Sstevel@tonic-gate 	 */
114*7c478bd9Sstevel@tonic-gate 	data->kpd_shmmax = 0;
115*7c478bd9Sstevel@tonic-gate 	data->kpd_shmmni = 0;
116*7c478bd9Sstevel@tonic-gate 	data->kpd_semmni = 0;
117*7c478bd9Sstevel@tonic-gate 	data->kpd_msgmni = 0;
118*7c478bd9Sstevel@tonic-gate 	data->kpd_devlockmem = 0;
119*7c478bd9Sstevel@tonic-gate 	data->kpd_contract = 0;
120*7c478bd9Sstevel@tonic-gate 	data->kpd_crypto_mem = 0;
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
124*7c478bd9Sstevel@tonic-gate static uint_t
125*7c478bd9Sstevel@tonic-gate project_hash_by_id(void *hash_data, mod_hash_key_t key)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	struct project_zone *pz = key;
128*7c478bd9Sstevel@tonic-gate 	uint_t mykey;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	/*
131*7c478bd9Sstevel@tonic-gate 	 * Merge the zoneid and projectid together to a 32-bit quantity, and
132*7c478bd9Sstevel@tonic-gate 	 * then pass that in to the existing idhash.
133*7c478bd9Sstevel@tonic-gate 	 */
134*7c478bd9Sstevel@tonic-gate 	mykey = (pz->kpj_zoneid << 16) | pz->kpj_id;
135*7c478bd9Sstevel@tonic-gate 	return (mod_hash_byid(hash_data, (mod_hash_key_t)(uintptr_t)mykey));
136*7c478bd9Sstevel@tonic-gate }
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate static int
139*7c478bd9Sstevel@tonic-gate project_hash_key_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
140*7c478bd9Sstevel@tonic-gate {
141*7c478bd9Sstevel@tonic-gate 	struct project_zone *pz1 = key1, *pz2 = key2;
142*7c478bd9Sstevel@tonic-gate 	int retval;
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	return ((int)((retval = pz1->kpj_id - pz2->kpj_id) != 0 ? retval :
145*7c478bd9Sstevel@tonic-gate 	    pz1->kpj_zoneid - pz2->kpj_zoneid));
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate static void
149*7c478bd9Sstevel@tonic-gate project_hash_val_dtor(mod_hash_val_t val)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	kproject_t *kp = (kproject_t *)val;
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	ASSERT(kp->kpj_count == 0);
154*7c478bd9Sstevel@tonic-gate 	kmem_free(kp, sizeof (kproject_t));
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate /*
158*7c478bd9Sstevel@tonic-gate  * kproject_t *project_hold(kproject_t *)
159*7c478bd9Sstevel@tonic-gate  *
160*7c478bd9Sstevel@tonic-gate  * Overview
161*7c478bd9Sstevel@tonic-gate  *   Record that an additional reference on the indicated project has been
162*7c478bd9Sstevel@tonic-gate  *   taken.
163*7c478bd9Sstevel@tonic-gate  *
164*7c478bd9Sstevel@tonic-gate  * Return values
165*7c478bd9Sstevel@tonic-gate  *   A pointer to the indicated project.
166*7c478bd9Sstevel@tonic-gate  *
167*7c478bd9Sstevel@tonic-gate  * Caller's context
168*7c478bd9Sstevel@tonic-gate  *   project_hash_lock must not be held across the project_hold() call.
169*7c478bd9Sstevel@tonic-gate  */
170*7c478bd9Sstevel@tonic-gate kproject_t *
171*7c478bd9Sstevel@tonic-gate project_hold(kproject_t *p)
172*7c478bd9Sstevel@tonic-gate {
173*7c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
174*7c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL);
175*7c478bd9Sstevel@tonic-gate 	p->kpj_count++;
176*7c478bd9Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
177*7c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
178*7c478bd9Sstevel@tonic-gate 	return (p);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * kproject_t *project_hold_by_id(projid_t, zoneid_t, int)
183*7c478bd9Sstevel@tonic-gate  *
184*7c478bd9Sstevel@tonic-gate  * Overview
185*7c478bd9Sstevel@tonic-gate  *   project_hold_by_id() performs a look-up in the dictionary of projects
186*7c478bd9Sstevel@tonic-gate  *   active on the system by specified project ID + zone ID and puts a hold on
187*7c478bd9Sstevel@tonic-gate  *   it.  The third argument defines the desired behavior in the case when
188*7c478bd9Sstevel@tonic-gate  *   project with given project ID cannot be found:
189*7c478bd9Sstevel@tonic-gate  *
190*7c478bd9Sstevel@tonic-gate  *   PROJECT_HOLD_INSERT	New entry is made in dictionary and the project
191*7c478bd9Sstevel@tonic-gate  *   				is added to the global list.
192*7c478bd9Sstevel@tonic-gate  *
193*7c478bd9Sstevel@tonic-gate  *   PROJECT_HOLD_FIND		Return NULL.
194*7c478bd9Sstevel@tonic-gate  *
195*7c478bd9Sstevel@tonic-gate  *   The project is returned with its reference count incremented by one.
196*7c478bd9Sstevel@tonic-gate  *   A new project derives its resource controls from those of project 0.
197*7c478bd9Sstevel@tonic-gate  *
198*7c478bd9Sstevel@tonic-gate  * Return values
199*7c478bd9Sstevel@tonic-gate  *   A pointer to the held project.
200*7c478bd9Sstevel@tonic-gate  *
201*7c478bd9Sstevel@tonic-gate  * Caller's context
202*7c478bd9Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
203*7c478bd9Sstevel@tonic-gate  */
204*7c478bd9Sstevel@tonic-gate kproject_t *
205*7c478bd9Sstevel@tonic-gate project_hold_by_id(projid_t id, zoneid_t zoneid, int flag)
206*7c478bd9Sstevel@tonic-gate {
207*7c478bd9Sstevel@tonic-gate 	kproject_t *spare_p;
208*7c478bd9Sstevel@tonic-gate 	kproject_t *p;
209*7c478bd9Sstevel@tonic-gate 	mod_hash_hndl_t hndl;
210*7c478bd9Sstevel@tonic-gate 	rctl_set_t *set;
211*7c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp;
212*7c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
213*7c478bd9Sstevel@tonic-gate 	struct project_zone pz;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	pz.kpj_id = id;
216*7c478bd9Sstevel@tonic-gate 	pz.kpj_zoneid = zoneid;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	if (flag == PROJECT_HOLD_FIND) {
219*7c478bd9Sstevel@tonic-gate 		mutex_enter(&project_hash_lock);
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
222*7c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)&p) == MH_ERR_NOTFOUND)
223*7c478bd9Sstevel@tonic-gate 			p = NULL;
224*7c478bd9Sstevel@tonic-gate 		else
225*7c478bd9Sstevel@tonic-gate 			p->kpj_count++;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		mutex_exit(&project_hash_lock);
228*7c478bd9Sstevel@tonic-gate 		return (p);
229*7c478bd9Sstevel@tonic-gate 	}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 	ASSERT(flag == PROJECT_HOLD_INSERT);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 	spare_p = kmem_zalloc(sizeof (kproject_t), KM_SLEEP);
234*7c478bd9Sstevel@tonic-gate 	set = rctl_set_create();
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	gp = rctl_set_init_prealloc(RCENTITY_PROJECT);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	(void) mod_hash_reserve(projects_hash, &hndl);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	mutex_enter(&curproc->p_lock);
241*7c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
242*7c478bd9Sstevel@tonic-gate 	if (mod_hash_find(projects_hash, (mod_hash_key_t)&pz,
243*7c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&p) == MH_ERR_NOTFOUND) {
244*7c478bd9Sstevel@tonic-gate 		p = spare_p;
245*7c478bd9Sstevel@tonic-gate 		p->kpj_id = id;
246*7c478bd9Sstevel@tonic-gate 		p->kpj_zoneid = zoneid;
247*7c478bd9Sstevel@tonic-gate 		p->kpj_count = 0;
248*7c478bd9Sstevel@tonic-gate 		p->kpj_shares = 1;
249*7c478bd9Sstevel@tonic-gate 		p->kpj_nlwps = 0;
250*7c478bd9Sstevel@tonic-gate 		p->kpj_ntasks = 0;
251*7c478bd9Sstevel@tonic-gate 		p->kpj_nlwps_ctl = INT_MAX;
252*7c478bd9Sstevel@tonic-gate 		p->kpj_ntasks_ctl = INT_MAX;
253*7c478bd9Sstevel@tonic-gate 		project_data_init(&p->kpj_data);
254*7c478bd9Sstevel@tonic-gate 		e.rcep_p.proj = p;
255*7c478bd9Sstevel@tonic-gate 		e.rcep_t = RCENTITY_PROJECT;
256*7c478bd9Sstevel@tonic-gate 		p->kpj_rctls = rctl_set_init(RCENTITY_PROJECT, curproc, &e,
257*7c478bd9Sstevel@tonic-gate 		    set, gp);
258*7c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 		if (mod_hash_insert_reserve(projects_hash, (mod_hash_key_t)p,
261*7c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)p, hndl))
262*7c478bd9Sstevel@tonic-gate 			panic("unable to insert project %d(%p)", id, (void *)p);
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 		/*
265*7c478bd9Sstevel@tonic-gate 		 * Insert project into global project list.
266*7c478bd9Sstevel@tonic-gate 		 */
267*7c478bd9Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
268*7c478bd9Sstevel@tonic-gate 		if (id != 0 || zoneid != GLOBAL_ZONEID) {
269*7c478bd9Sstevel@tonic-gate 			p->kpj_next = projects_list;
270*7c478bd9Sstevel@tonic-gate 			p->kpj_prev = projects_list->kpj_prev;
271*7c478bd9Sstevel@tonic-gate 			p->kpj_prev->kpj_next = p;
272*7c478bd9Sstevel@tonic-gate 			projects_list->kpj_prev = p;
273*7c478bd9Sstevel@tonic-gate 		} else {
274*7c478bd9Sstevel@tonic-gate 			/*
275*7c478bd9Sstevel@tonic-gate 			 * Special case: primordial hold on project 0.
276*7c478bd9Sstevel@tonic-gate 			 */
277*7c478bd9Sstevel@tonic-gate 			p->kpj_next = p;
278*7c478bd9Sstevel@tonic-gate 			p->kpj_prev = p;
279*7c478bd9Sstevel@tonic-gate 			projects_list = p;
280*7c478bd9Sstevel@tonic-gate 		}
281*7c478bd9Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
282*7c478bd9Sstevel@tonic-gate 	} else {
283*7c478bd9Sstevel@tonic-gate 		mutex_exit(&curproc->p_lock);
284*7c478bd9Sstevel@tonic-gate 		mod_hash_cancel(projects_hash, &hndl);
285*7c478bd9Sstevel@tonic-gate 		kmem_free(spare_p, sizeof (kproject_t));
286*7c478bd9Sstevel@tonic-gate 		rctl_set_free(set);
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	rctl_prealloc_destroy(gp);
290*7c478bd9Sstevel@tonic-gate 	p->kpj_count++;
291*7c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	return (p);
294*7c478bd9Sstevel@tonic-gate }
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate /*
298*7c478bd9Sstevel@tonic-gate  * void project_rele(kproject_t *)
299*7c478bd9Sstevel@tonic-gate  *
300*7c478bd9Sstevel@tonic-gate  * Overview
301*7c478bd9Sstevel@tonic-gate  *   Advertise that one external reference to this project is no longer needed.
302*7c478bd9Sstevel@tonic-gate  *
303*7c478bd9Sstevel@tonic-gate  * Return values
304*7c478bd9Sstevel@tonic-gate  *   None.
305*7c478bd9Sstevel@tonic-gate  *
306*7c478bd9Sstevel@tonic-gate  * Caller's context
307*7c478bd9Sstevel@tonic-gate  *   No restriction on context.
308*7c478bd9Sstevel@tonic-gate  */
309*7c478bd9Sstevel@tonic-gate void
310*7c478bd9Sstevel@tonic-gate project_rele(kproject_t *p)
311*7c478bd9Sstevel@tonic-gate {
312*7c478bd9Sstevel@tonic-gate 	mutex_enter(&project_hash_lock);
313*7c478bd9Sstevel@tonic-gate 	ASSERT(p->kpj_count != 0);
314*7c478bd9Sstevel@tonic-gate 	p->kpj_count--;
315*7c478bd9Sstevel@tonic-gate 	if (p->kpj_count == 0) {
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 		/*
318*7c478bd9Sstevel@tonic-gate 		 * Remove project from global list.
319*7c478bd9Sstevel@tonic-gate 		 */
320*7c478bd9Sstevel@tonic-gate 		mutex_enter(&projects_list_lock);
321*7c478bd9Sstevel@tonic-gate 		p->kpj_next->kpj_prev = p->kpj_prev;
322*7c478bd9Sstevel@tonic-gate 		p->kpj_prev->kpj_next = p->kpj_next;
323*7c478bd9Sstevel@tonic-gate 		if (projects_list == p)
324*7c478bd9Sstevel@tonic-gate 			projects_list = p->kpj_next;
325*7c478bd9Sstevel@tonic-gate 		mutex_exit(&projects_list_lock);
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		rctl_set_free(p->kpj_rctls);
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 		if (mod_hash_destroy(projects_hash, (mod_hash_key_t)p))
330*7c478bd9Sstevel@tonic-gate 			panic("unable to delete project %d zone %d", p->kpj_id,
331*7c478bd9Sstevel@tonic-gate 			    p->kpj_zoneid);
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 		}
334*7c478bd9Sstevel@tonic-gate 	mutex_exit(&project_hash_lock);
335*7c478bd9Sstevel@tonic-gate }
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate /*
338*7c478bd9Sstevel@tonic-gate  * int project_walk_all(zoneid_t, int (*)(kproject_t *, void *), void *)
339*7c478bd9Sstevel@tonic-gate  *
340*7c478bd9Sstevel@tonic-gate  * Overview
341*7c478bd9Sstevel@tonic-gate  *   Walk the project list for the given zoneid with a callback.
342*7c478bd9Sstevel@tonic-gate  *
343*7c478bd9Sstevel@tonic-gate  * Return values
344*7c478bd9Sstevel@tonic-gate  *   -1 for an invalid walk, number of projects visited otherwise.
345*7c478bd9Sstevel@tonic-gate  *
346*7c478bd9Sstevel@tonic-gate  * Caller's context
347*7c478bd9Sstevel@tonic-gate  *   projects_list_lock must not be held, as it is acquired by
348*7c478bd9Sstevel@tonic-gate  *   project_walk_all().  Accordingly, callbacks may not perform KM_SLEEP
349*7c478bd9Sstevel@tonic-gate  *   allocations.
350*7c478bd9Sstevel@tonic-gate  */
351*7c478bd9Sstevel@tonic-gate int
352*7c478bd9Sstevel@tonic-gate project_walk_all(zoneid_t zoneid, int (*cb)(kproject_t *, void *),
353*7c478bd9Sstevel@tonic-gate     void *walk_data)
354*7c478bd9Sstevel@tonic-gate {
355*7c478bd9Sstevel@tonic-gate 	int cnt = 0;
356*7c478bd9Sstevel@tonic-gate 	kproject_t *kp = proj0p;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 	mutex_enter(&projects_list_lock);
359*7c478bd9Sstevel@tonic-gate 	do {
360*7c478bd9Sstevel@tonic-gate 		if (zoneid != ALL_ZONES && kp->kpj_zoneid != zoneid)
361*7c478bd9Sstevel@tonic-gate 			continue;
362*7c478bd9Sstevel@tonic-gate 		if (cb(kp, walk_data) == -1) {
363*7c478bd9Sstevel@tonic-gate 			cnt = -1;
364*7c478bd9Sstevel@tonic-gate 			break;
365*7c478bd9Sstevel@tonic-gate 		} else {
366*7c478bd9Sstevel@tonic-gate 			cnt++;
367*7c478bd9Sstevel@tonic-gate 		}
368*7c478bd9Sstevel@tonic-gate 	} while ((kp = kp->kpj_next) != proj0p);
369*7c478bd9Sstevel@tonic-gate 	mutex_exit(&projects_list_lock);
370*7c478bd9Sstevel@tonic-gate 	return (cnt);
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate /*
374*7c478bd9Sstevel@tonic-gate  * projid_t curprojid(void)
375*7c478bd9Sstevel@tonic-gate  *
376*7c478bd9Sstevel@tonic-gate  * Overview
377*7c478bd9Sstevel@tonic-gate  *   Return project ID of the current thread
378*7c478bd9Sstevel@tonic-gate  *
379*7c478bd9Sstevel@tonic-gate  * Caller's context
380*7c478bd9Sstevel@tonic-gate  *   No restrictions.
381*7c478bd9Sstevel@tonic-gate  */
382*7c478bd9Sstevel@tonic-gate projid_t
383*7c478bd9Sstevel@tonic-gate curprojid()
384*7c478bd9Sstevel@tonic-gate {
385*7c478bd9Sstevel@tonic-gate 	return (ttoproj(curthread)->kpj_id);
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate 
388*7c478bd9Sstevel@tonic-gate /*
389*7c478bd9Sstevel@tonic-gate  * project.cpu-shares resource control support.
390*7c478bd9Sstevel@tonic-gate  */
391*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
392*7c478bd9Sstevel@tonic-gate static rctl_qty_t
393*7c478bd9Sstevel@tonic-gate project_cpu_shares_usage(rctl_t *rctl, struct proc *p)
394*7c478bd9Sstevel@tonic-gate {
395*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
396*7c478bd9Sstevel@tonic-gate 	return (p->p_task->tk_proj->kpj_shares);
397*7c478bd9Sstevel@tonic-gate }
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
400*7c478bd9Sstevel@tonic-gate static int
401*7c478bd9Sstevel@tonic-gate project_cpu_shares_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
402*7c478bd9Sstevel@tonic-gate     rctl_qty_t nv)
403*7c478bd9Sstevel@tonic-gate {
404*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
405*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
406*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
407*7c478bd9Sstevel@tonic-gate 		return (0);
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_shares = nv;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	return (0);
412*7c478bd9Sstevel@tonic-gate }
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_cpu_shares_ops = {
416*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
417*7c478bd9Sstevel@tonic-gate 	project_cpu_shares_usage,
418*7c478bd9Sstevel@tonic-gate 	project_cpu_shares_set,
419*7c478bd9Sstevel@tonic-gate 	rcop_no_test
420*7c478bd9Sstevel@tonic-gate };
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
423*7c478bd9Sstevel@tonic-gate static rctl_qty_t
424*7c478bd9Sstevel@tonic-gate project_lwps_usage(rctl_t *r, proc_t *p)
425*7c478bd9Sstevel@tonic-gate {
426*7c478bd9Sstevel@tonic-gate 	kproject_t *pj;
427*7c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
430*7c478bd9Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
431*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
432*7c478bd9Sstevel@tonic-gate 	nlwps = pj->kpj_nlwps;
433*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 	return (nlwps);
436*7c478bd9Sstevel@tonic-gate }
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
439*7c478bd9Sstevel@tonic-gate static int
440*7c478bd9Sstevel@tonic-gate project_lwps_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
441*7c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
442*7c478bd9Sstevel@tonic-gate {
443*7c478bd9Sstevel@tonic-gate 	rctl_qty_t nlwps;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
446*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
447*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
448*7c478bd9Sstevel@tonic-gate 		return (0);
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	nlwps = e->rcep_p.proj->kpj_nlwps;
451*7c478bd9Sstevel@tonic-gate 	if (nlwps + incr > rcntl->rcv_value)
452*7c478bd9Sstevel@tonic-gate 		return (1);
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	return (0);
455*7c478bd9Sstevel@tonic-gate }
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
458*7c478bd9Sstevel@tonic-gate static int
459*7c478bd9Sstevel@tonic-gate project_lwps_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
460*7c478bd9Sstevel@tonic-gate     rctl_qty_t nv) {
461*7c478bd9Sstevel@tonic-gate 
462*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
463*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
464*7c478bd9Sstevel@tonic-gate 	if (e->rcep_p.proj == NULL)
465*7c478bd9Sstevel@tonic-gate 		return (0);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_nlwps_ctl = nv;
468*7c478bd9Sstevel@tonic-gate 	return (0);
469*7c478bd9Sstevel@tonic-gate }
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_lwps_ops = {
472*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
473*7c478bd9Sstevel@tonic-gate 	project_lwps_usage,
474*7c478bd9Sstevel@tonic-gate 	project_lwps_set,
475*7c478bd9Sstevel@tonic-gate 	project_lwps_test,
476*7c478bd9Sstevel@tonic-gate };
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
479*7c478bd9Sstevel@tonic-gate static rctl_qty_t
480*7c478bd9Sstevel@tonic-gate project_ntasks_usage(rctl_t *r, proc_t *p)
481*7c478bd9Sstevel@tonic-gate {
482*7c478bd9Sstevel@tonic-gate 	kproject_t *pj;
483*7c478bd9Sstevel@tonic-gate 	rctl_qty_t ntasks;
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
486*7c478bd9Sstevel@tonic-gate 	pj = p->p_task->tk_proj;
487*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
488*7c478bd9Sstevel@tonic-gate 	ntasks = pj->kpj_ntasks;
489*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	return (ntasks);
492*7c478bd9Sstevel@tonic-gate }
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
495*7c478bd9Sstevel@tonic-gate static int
496*7c478bd9Sstevel@tonic-gate project_ntasks_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e, rctl_val_t *rcntl,
497*7c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
498*7c478bd9Sstevel@tonic-gate {
499*7c478bd9Sstevel@tonic-gate 	rctl_qty_t ntasks;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
502*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
503*7c478bd9Sstevel@tonic-gate 	ntasks = e->rcep_p.proj->kpj_ntasks;
504*7c478bd9Sstevel@tonic-gate 	if (ntasks + incr > rcntl->rcv_value)
505*7c478bd9Sstevel@tonic-gate 		return (1);
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	return (0);
508*7c478bd9Sstevel@tonic-gate }
509*7c478bd9Sstevel@tonic-gate 
510*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
511*7c478bd9Sstevel@tonic-gate static int
512*7c478bd9Sstevel@tonic-gate project_ntasks_set(rctl_t *rctl, struct proc *p, rctl_entity_p_t *e,
513*7c478bd9Sstevel@tonic-gate     rctl_qty_t nv) {
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
516*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
517*7c478bd9Sstevel@tonic-gate 	e->rcep_p.proj->kpj_ntasks_ctl = nv;
518*7c478bd9Sstevel@tonic-gate 	return (0);
519*7c478bd9Sstevel@tonic-gate }
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_tasks_ops = {
522*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
523*7c478bd9Sstevel@tonic-gate 	project_ntasks_usage,
524*7c478bd9Sstevel@tonic-gate 	project_ntasks_set,
525*7c478bd9Sstevel@tonic-gate 	project_ntasks_test,
526*7c478bd9Sstevel@tonic-gate };
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate /*
529*7c478bd9Sstevel@tonic-gate  * project.max-shm-memory resource control support.
530*7c478bd9Sstevel@tonic-gate  */
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
533*7c478bd9Sstevel@tonic-gate static int
534*7c478bd9Sstevel@tonic-gate project_shmmax_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
535*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
536*7c478bd9Sstevel@tonic-gate {
537*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
538*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
539*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
540*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_shmmax + inc;
541*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
542*7c478bd9Sstevel@tonic-gate 		return (1);
543*7c478bd9Sstevel@tonic-gate 
544*7c478bd9Sstevel@tonic-gate 	return (0);
545*7c478bd9Sstevel@tonic-gate }
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_shmmax_ops = {
548*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
549*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
550*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
551*7c478bd9Sstevel@tonic-gate 	project_shmmax_test
552*7c478bd9Sstevel@tonic-gate };
553*7c478bd9Sstevel@tonic-gate 
554*7c478bd9Sstevel@tonic-gate /*
555*7c478bd9Sstevel@tonic-gate  * project.max-shm-ids resource control support.
556*7c478bd9Sstevel@tonic-gate  */
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
559*7c478bd9Sstevel@tonic-gate static int
560*7c478bd9Sstevel@tonic-gate project_shmmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
561*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
562*7c478bd9Sstevel@tonic-gate {
563*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
564*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
565*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
566*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_shmmni + inc;
567*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
568*7c478bd9Sstevel@tonic-gate 		return (1);
569*7c478bd9Sstevel@tonic-gate 
570*7c478bd9Sstevel@tonic-gate 	return (0);
571*7c478bd9Sstevel@tonic-gate }
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_shmmni_ops = {
574*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
575*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
576*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
577*7c478bd9Sstevel@tonic-gate 	project_shmmni_test
578*7c478bd9Sstevel@tonic-gate };
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate /*
581*7c478bd9Sstevel@tonic-gate  * project.max-sem-ids resource control support.
582*7c478bd9Sstevel@tonic-gate  */
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
585*7c478bd9Sstevel@tonic-gate static int
586*7c478bd9Sstevel@tonic-gate project_semmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
587*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
588*7c478bd9Sstevel@tonic-gate {
589*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
590*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
591*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
592*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_semmni + inc;
593*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
594*7c478bd9Sstevel@tonic-gate 		return (1);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	return (0);
597*7c478bd9Sstevel@tonic-gate }
598*7c478bd9Sstevel@tonic-gate 
599*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_semmni_ops = {
600*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
601*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
602*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
603*7c478bd9Sstevel@tonic-gate 	project_semmni_test
604*7c478bd9Sstevel@tonic-gate };
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate /*
607*7c478bd9Sstevel@tonic-gate  * project.max-msg-ids resource control support.
608*7c478bd9Sstevel@tonic-gate  */
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
611*7c478bd9Sstevel@tonic-gate static int
612*7c478bd9Sstevel@tonic-gate project_msgmni_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
613*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
614*7c478bd9Sstevel@tonic-gate {
615*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
616*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
617*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
618*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_msgmni + inc;
619*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
620*7c478bd9Sstevel@tonic-gate 		return (1);
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	return (0);
623*7c478bd9Sstevel@tonic-gate }
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_msgmni_ops = {
626*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
627*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
628*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
629*7c478bd9Sstevel@tonic-gate 	project_msgmni_test
630*7c478bd9Sstevel@tonic-gate };
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate /*
633*7c478bd9Sstevel@tonic-gate  * project.max-device-locked-memory resource control support.
634*7c478bd9Sstevel@tonic-gate  */
635*7c478bd9Sstevel@tonic-gate 
636*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
637*7c478bd9Sstevel@tonic-gate static int
638*7c478bd9Sstevel@tonic-gate project_devlockmem_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
639*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
640*7c478bd9Sstevel@tonic-gate {
641*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
642*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
643*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
644*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_devlockmem + inc;
645*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
646*7c478bd9Sstevel@tonic-gate 		return (1);
647*7c478bd9Sstevel@tonic-gate 	return (0);
648*7c478bd9Sstevel@tonic-gate }
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_devlockmem_ops = {
651*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
652*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
653*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
654*7c478bd9Sstevel@tonic-gate 	project_devlockmem_test
655*7c478bd9Sstevel@tonic-gate };
656*7c478bd9Sstevel@tonic-gate 
657*7c478bd9Sstevel@tonic-gate /*
658*7c478bd9Sstevel@tonic-gate  * project.max-contracts resource control support.
659*7c478bd9Sstevel@tonic-gate  */
660*7c478bd9Sstevel@tonic-gate 
661*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
662*7c478bd9Sstevel@tonic-gate static int
663*7c478bd9Sstevel@tonic-gate project_contract_test(struct rctl *rctl, struct proc *p, rctl_entity_p_t *e,
664*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t inc, uint_t flags)
665*7c478bd9Sstevel@tonic-gate {
666*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
669*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_contract + inc;
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	if ((p->p_task != NULL) && (p->p_task->tk_proj) != NULL &&
674*7c478bd9Sstevel@tonic-gate 	    (v > rval->rcv_value))
675*7c478bd9Sstevel@tonic-gate 		return (1);
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 	return (0);
678*7c478bd9Sstevel@tonic-gate }
679*7c478bd9Sstevel@tonic-gate 
680*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_contract_ops = {
681*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
682*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
683*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
684*7c478bd9Sstevel@tonic-gate 	project_contract_test
685*7c478bd9Sstevel@tonic-gate };
686*7c478bd9Sstevel@tonic-gate 
687*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
688*7c478bd9Sstevel@tonic-gate static int
689*7c478bd9Sstevel@tonic-gate project_crypto_test(rctl_t *r, proc_t *p, rctl_entity_p_t *e,
690*7c478bd9Sstevel@tonic-gate     rctl_val_t *rval, rctl_qty_t incr, uint_t flags)
691*7c478bd9Sstevel@tonic-gate {
692*7c478bd9Sstevel@tonic-gate 	rctl_qty_t v;
693*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
694*7c478bd9Sstevel@tonic-gate 	ASSERT(e->rcep_t == RCENTITY_PROJECT);
695*7c478bd9Sstevel@tonic-gate 	v = e->rcep_p.proj->kpj_data.kpd_crypto_mem + incr;
696*7c478bd9Sstevel@tonic-gate 	if (v > rval->rcv_value)
697*7c478bd9Sstevel@tonic-gate 		return (1);
698*7c478bd9Sstevel@tonic-gate 	return (0);
699*7c478bd9Sstevel@tonic-gate }
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate static rctl_ops_t project_crypto_mem_ops = {
702*7c478bd9Sstevel@tonic-gate 	rcop_no_action,
703*7c478bd9Sstevel@tonic-gate 	rcop_no_usage,
704*7c478bd9Sstevel@tonic-gate 	rcop_no_set,
705*7c478bd9Sstevel@tonic-gate 	project_crypto_test
706*7c478bd9Sstevel@tonic-gate };
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate /*
709*7c478bd9Sstevel@tonic-gate  * void project_init(void)
710*7c478bd9Sstevel@tonic-gate  *
711*7c478bd9Sstevel@tonic-gate  * Overview
712*7c478bd9Sstevel@tonic-gate  *   Initialize the project subsystem, including the primordial project 0 entry.
713*7c478bd9Sstevel@tonic-gate  *   Register generic project resource controls, if any.
714*7c478bd9Sstevel@tonic-gate  *
715*7c478bd9Sstevel@tonic-gate  * Return values
716*7c478bd9Sstevel@tonic-gate  *   None.
717*7c478bd9Sstevel@tonic-gate  *
718*7c478bd9Sstevel@tonic-gate  * Caller's context
719*7c478bd9Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
720*7c478bd9Sstevel@tonic-gate  */
721*7c478bd9Sstevel@tonic-gate void
722*7c478bd9Sstevel@tonic-gate project_init(void)
723*7c478bd9Sstevel@tonic-gate {
724*7c478bd9Sstevel@tonic-gate 	rctl_qty_t shmmni, shmmax, qty;
725*7c478bd9Sstevel@tonic-gate 	boolean_t check;
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	projects_hash = mod_hash_create_extended("projects_hash",
728*7c478bd9Sstevel@tonic-gate 	    project_hash_size, mod_hash_null_keydtor, project_hash_val_dtor,
729*7c478bd9Sstevel@tonic-gate 	    project_hash_by_id,
730*7c478bd9Sstevel@tonic-gate 	    (void *)(uintptr_t)mod_hash_iddata_gen(project_hash_size),
731*7c478bd9Sstevel@tonic-gate 	    project_hash_key_cmp, KM_SLEEP);
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate 	rc_project_cpu_shares = rctl_register("project.cpu-shares",
734*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_SIGNAL_NEVER |
735*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_NEVER | RCTL_GLOBAL_NOBASIC |
736*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, FSS_MAXSHARES, FSS_MAXSHARES,
737*7c478bd9Sstevel@tonic-gate 	    &project_cpu_shares_ops);
738*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.cpu-shares", 1, RCPRIV_PRIVILEGED,
739*7c478bd9Sstevel@tonic-gate 	    RCTL_LOCAL_NOACTION);
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	rc_project_nlwps = rctl_register("project.max-lwps", RCENTITY_PROJECT,
742*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
743*7c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_lwps_ops);
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	rc_project_ntasks = rctl_register("project.max-tasks", RCENTITY_PROJECT,
746*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_NOACTION | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_COUNT,
747*7c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_tasks_ops);
748*7c478bd9Sstevel@tonic-gate 
749*7c478bd9Sstevel@tonic-gate 	/*
750*7c478bd9Sstevel@tonic-gate 	 * This rctl handle is used by /dev/crypto. It is here rather than
751*7c478bd9Sstevel@tonic-gate 	 * in misc/kcf or the drv/crypto module because resource controls
752*7c478bd9Sstevel@tonic-gate 	 * currently don't allow modules to be unloaded, and the control
753*7c478bd9Sstevel@tonic-gate 	 * must be registered before init starts.
754*7c478bd9Sstevel@tonic-gate 	 */
755*7c478bd9Sstevel@tonic-gate 	rc_project_crypto_mem = rctl_register("project.max-crypto-memory",
756*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
757*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX,
758*7c478bd9Sstevel@tonic-gate 	    &project_crypto_mem_ops);
759*7c478bd9Sstevel@tonic-gate 
760*7c478bd9Sstevel@tonic-gate 	/*
761*7c478bd9Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
762*7c478bd9Sstevel@tonic-gate 	 */
763*7c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
764*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-crypto-memory", qty,
765*7c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
766*7c478bd9Sstevel@tonic-gate 
767*7c478bd9Sstevel@tonic-gate 	/*
768*7c478bd9Sstevel@tonic-gate 	 * System V IPC resource controls
769*7c478bd9Sstevel@tonic-gate 	 */
770*7c478bd9Sstevel@tonic-gate 	rc_project_semmni = rctl_register("project.max-sem-ids",
771*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
772*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_semmni_ops);
773*7c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-sem-ids", "semsys",
774*7c478bd9Sstevel@tonic-gate 	    "seminfo_semmni", 128, IPC_IDS_MAX);
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 	rc_project_msgmni = rctl_register("project.max-msg-ids",
777*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
778*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_msgmni_ops);
779*7c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-msg-ids", "msgsys",
780*7c478bd9Sstevel@tonic-gate 	    "msginfo_msgmni", 128, IPC_IDS_MAX);
781*7c478bd9Sstevel@tonic-gate 
782*7c478bd9Sstevel@tonic-gate 	rc_project_shmmni = rctl_register("project.max-shm-ids",
783*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
784*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, IPC_IDS_MAX, IPC_IDS_MAX, &project_shmmni_ops);
785*7c478bd9Sstevel@tonic-gate 	rctl_add_legacy_limit("project.max-shm-ids", "shmsys",
786*7c478bd9Sstevel@tonic-gate 	    "shminfo_shmmni", 128, IPC_IDS_MAX);
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 	rc_project_shmmax = rctl_register("project.max-shm-memory",
789*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
790*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_BYTES, UINT64_MAX, UINT64_MAX, &project_shmmax_ops);
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	check = B_FALSE;
793*7c478bd9Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmni", &shmmni))
794*7c478bd9Sstevel@tonic-gate 		shmmni = 100;
795*7c478bd9Sstevel@tonic-gate 	else
796*7c478bd9Sstevel@tonic-gate 		check = B_TRUE;
797*7c478bd9Sstevel@tonic-gate 	if (!mod_sysvar("shmsys", "shminfo_shmmax", &shmmax))
798*7c478bd9Sstevel@tonic-gate 		shmmax = 0x800000;
799*7c478bd9Sstevel@tonic-gate 	else
800*7c478bd9Sstevel@tonic-gate 		check = B_TRUE;
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate 	/*
803*7c478bd9Sstevel@tonic-gate 	 * Default to a quarter of the machine's memory
804*7c478bd9Sstevel@tonic-gate 	 */
805*7c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 2);
806*7c478bd9Sstevel@tonic-gate 	if (check) {
807*7c478bd9Sstevel@tonic-gate 		if ((shmmax > 0) && (UINT64_MAX / shmmax <= shmmni))
808*7c478bd9Sstevel@tonic-gate 			qty = UINT64_MAX;
809*7c478bd9Sstevel@tonic-gate 		else if (shmmni * shmmax > qty)
810*7c478bd9Sstevel@tonic-gate 			qty = shmmni * shmmax;
811*7c478bd9Sstevel@tonic-gate 	}
812*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-shm-memory", qty,
813*7c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 	/*
816*7c478bd9Sstevel@tonic-gate 	 * Event Ports resource controls
817*7c478bd9Sstevel@tonic-gate 	 */
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate 	rc_project_portids = rctl_register("project.max-port-ids",
820*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC |
821*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_COUNT, PORT_MAX_PORTS, PORT_MAX_PORTS,
822*7c478bd9Sstevel@tonic-gate 	    &rctl_absolute_ops);
823*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-port-ids", PORT_DEFAULT_PORTS,
824*7c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	/*
827*7c478bd9Sstevel@tonic-gate 	 * Resource control for locked memory
828*7c478bd9Sstevel@tonic-gate 	 */
829*7c478bd9Sstevel@tonic-gate 	rc_project_devlockmem = rctl_register(
830*7c478bd9Sstevel@tonic-gate 	    "project.max-device-locked-memory", RCENTITY_PROJECT,
831*7c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_NOBASIC | RCTL_GLOBAL_BYTES,
832*7c478bd9Sstevel@tonic-gate 	    UINT64_MAX, UINT64_MAX, &project_devlockmem_ops);
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	/*
835*7c478bd9Sstevel@tonic-gate 	 * Defaults to 1/16th of the machine's memory
836*7c478bd9Sstevel@tonic-gate 	 */
837*7c478bd9Sstevel@tonic-gate 	qty = availrmem_initial << (PAGESHIFT - 4);
838*7c478bd9Sstevel@tonic-gate 
839*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-device-locked-memory", qty,
840*7c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
841*7c478bd9Sstevel@tonic-gate 
842*7c478bd9Sstevel@tonic-gate 	/*
843*7c478bd9Sstevel@tonic-gate 	 * Per project limit on contracts.
844*7c478bd9Sstevel@tonic-gate 	 */
845*7c478bd9Sstevel@tonic-gate 	rc_project_contract = rctl_register("project.max-contracts",
846*7c478bd9Sstevel@tonic-gate 	    RCENTITY_PROJECT, RCTL_GLOBAL_DENY_ALWAYS | RCTL_GLOBAL_COUNT,
847*7c478bd9Sstevel@tonic-gate 	    INT_MAX, INT_MAX, &project_contract_ops);
848*7c478bd9Sstevel@tonic-gate 	rctl_add_default_limit("project.max-contracts", 10000,
849*7c478bd9Sstevel@tonic-gate 	    RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
850*7c478bd9Sstevel@tonic-gate 
851*7c478bd9Sstevel@tonic-gate 	t0.t_proj = proj0p = project_hold_by_id(0, GLOBAL_ZONEID,
852*7c478bd9Sstevel@tonic-gate 	    PROJECT_HOLD_INSERT);
853*7c478bd9Sstevel@tonic-gate 
854*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p0.p_lock);
855*7c478bd9Sstevel@tonic-gate 	proj0p->kpj_nlwps = p0.p_lwpcnt;
856*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p0.p_lock);
857*7c478bd9Sstevel@tonic-gate 	proj0p->kpj_ntasks = 1;
858*7c478bd9Sstevel@tonic-gate }
859