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