xref: /onnv-gate/usr/src/uts/common/os/rctl.c (revision 12725:334fd88ae67c)
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
52447Snf202958  * Common Development and Distribution License (the "License").
62447Snf202958  * 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 /*
2212633Sjohn.levon@sun.com  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/atomic.h>
260Sstevel@tonic-gate #include <sys/cmn_err.h>
270Sstevel@tonic-gate #include <sys/id_space.h>
280Sstevel@tonic-gate #include <sys/kmem.h>
293247Sgjelinek #include <sys/kstat.h>
300Sstevel@tonic-gate #include <sys/log.h>
310Sstevel@tonic-gate #include <sys/modctl.h>
320Sstevel@tonic-gate #include <sys/modhash.h>
330Sstevel@tonic-gate #include <sys/mutex.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/procset.h>
360Sstevel@tonic-gate #include <sys/project.h>
370Sstevel@tonic-gate #include <sys/resource.h>
380Sstevel@tonic-gate #include <sys/rctl.h>
390Sstevel@tonic-gate #include <sys/siginfo.h>
400Sstevel@tonic-gate #include <sys/strlog.h>
410Sstevel@tonic-gate #include <sys/systm.h>
420Sstevel@tonic-gate #include <sys/task.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/policy.h>
450Sstevel@tonic-gate #include <sys/zone.h>
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Resource controls (rctls)
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  *   The rctl subsystem provides a mechanism for kernel components to
510Sstevel@tonic-gate  *   register their individual resource controls with the system as a whole,
520Sstevel@tonic-gate  *   such that those controls can subscribe to specific actions while being
530Sstevel@tonic-gate  *   associated with the various process-model entities provided by the kernel:
540Sstevel@tonic-gate  *   the process, the task, the project, and the zone.  (In principle, only
550Sstevel@tonic-gate  *   minor modifications would be required to connect the resource control
560Sstevel@tonic-gate  *   functionality to non-process-model entities associated with the system.)
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *   Subsystems register their rctls via rctl_register().  Subsystems
590Sstevel@tonic-gate  *   also wishing to provide additional limits on a given rctl can modify
600Sstevel@tonic-gate  *   them once they have the rctl handle.  Each subsystem should store the
610Sstevel@tonic-gate  *   handle to their rctl for direct access.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  *   A primary dictionary, rctl_dict, contains a hash of id to the default
640Sstevel@tonic-gate  *   control definition for each controlled resource-entity pair on the system.
650Sstevel@tonic-gate  *   A secondary dictionary, rctl_dict_by_name, contains a hash of name to
660Sstevel@tonic-gate  *   resource control handles.  The resource control handles are distributed by
670Sstevel@tonic-gate  *   the rctl_ids ID space.  The handles are private and not to be
680Sstevel@tonic-gate  *   advertised to userland; all userland interactions are via the rctl
690Sstevel@tonic-gate  *   names.
700Sstevel@tonic-gate  *
710Sstevel@tonic-gate  *   Entities inherit their rctls from their predecessor.  Since projects have
720Sstevel@tonic-gate  *   no ancestor, they inherit their rctls from the rctl dict for project
730Sstevel@tonic-gate  *   rctls.  It is expected that project controls will be set to their
740Sstevel@tonic-gate  *   appropriate values shortly after project creation, presumably from a
750Sstevel@tonic-gate  *   policy source such as the project database.
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Data structures
780Sstevel@tonic-gate  *   The rctl_set_t attached to each of the process model entities is a simple
790Sstevel@tonic-gate  *   hash table keyed on the rctl handle assigned at registration.  The entries
800Sstevel@tonic-gate  *   in the hash table are rctl_t's, whose relationship with the active control
810Sstevel@tonic-gate  *   values on that resource and with the global state of the resource we
820Sstevel@tonic-gate  *   illustrate below:
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  *   rctl_dict[key] --> rctl_dict_entry
850Sstevel@tonic-gate  *			   ^
860Sstevel@tonic-gate  *			   |
870Sstevel@tonic-gate  *			+--+---+
880Sstevel@tonic-gate  *   rctl_set[key] ---> | rctl | --> value <-> value <-> system value --> NULL
890Sstevel@tonic-gate  *			+--+---+		 ^
900Sstevel@tonic-gate  *			   |			 |
910Sstevel@tonic-gate  *			   +------- cursor ------+
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  *   That is, the rctl contains a back pointer to the global resource control
940Sstevel@tonic-gate  *   state for this resource, which is also available in the rctl_dict hash
950Sstevel@tonic-gate  *   table mentioned earlier.  The rctl contains two pointers to resource
960Sstevel@tonic-gate  *   control values:  one, values, indicates the entire sequence of control
970Sstevel@tonic-gate  *   values; the other, cursor, indicates the currently active control
980Sstevel@tonic-gate  *   value--the next value to be enforced.  The value list itself is an open,
990Sstevel@tonic-gate  *   doubly-linked list, the last non-NULL member of which is the system value
1000Sstevel@tonic-gate  *   for that resource (being the theoretical/conventional maximum allowable
1010Sstevel@tonic-gate  *   value for the resource on this OS instance).
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  * Ops Vector
1040Sstevel@tonic-gate  *   Subsystems publishing rctls need not provide instances of all of the
1050Sstevel@tonic-gate  *   functions specified by the ops vector.  In particular, if general
1060Sstevel@tonic-gate  *   rctl_*() entry points are not being called, certain functions can be
1070Sstevel@tonic-gate  *   omitted.  These align as follows:
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  *   rctl_set()
1100Sstevel@tonic-gate  *     You may wish to provide a set callback if locking circumstances prevent
1110Sstevel@tonic-gate  *     it or if the performance cost of requesting the enforced value from the
1120Sstevel@tonic-gate  *     resource control is prohibitively expensive.  For instance, the currently
1130Sstevel@tonic-gate  *     enforced file size limit is stored on the process in the p_fsz_ctl to
1140Sstevel@tonic-gate  *     maintain read()/write() performance.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  *   rctl_test()
1170Sstevel@tonic-gate  *     You must provide a test callback if you are using the rctl_test()
1180Sstevel@tonic-gate  *     interface.  An action callback is optional.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  *   rctl_action()
1210Sstevel@tonic-gate  *     You may wish to provide an action callback.
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  * Registration
1240Sstevel@tonic-gate  *   New resource controls can be added to a running instance by loaded modules
1250Sstevel@tonic-gate  *   via registration.  (The current implementation does not support unloadable
1260Sstevel@tonic-gate  *   modules; this functionality can be added if needed, via an
1270Sstevel@tonic-gate  *   activation/deactivation interface involving the manipulation of the
1280Sstevel@tonic-gate  *   ops vector for the resource control(s) needing to support unloading.)
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Control value ordering
1310Sstevel@tonic-gate  *   Because the rctl_val chain on each rctl must be navigable in a
1320Sstevel@tonic-gate  *   deterministic way, we have to define an ordering on the rctl_val_t's.  The
1330Sstevel@tonic-gate  *   defined order is (flags & [maximal], value, flags & [deny-action],
1340Sstevel@tonic-gate  *   privilege).
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * Locking
1370Sstevel@tonic-gate  *   rctl_dict_lock must be acquired prior to rctl_lists_lock.  Since
1380Sstevel@tonic-gate  *   rctl_dict_lock or rctl_lists_lock can be called at the enforcement point
1390Sstevel@tonic-gate  *   of any subsystem, holding subsystem locks, it is at all times inappropriate
1400Sstevel@tonic-gate  *   to call kmem_alloc(., KM_SLEEP) while holding either of these locks.
1410Sstevel@tonic-gate  *   Traversing any of the various resource control entity lists requires
1420Sstevel@tonic-gate  *   holding rctl_lists_lock.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  *   Each individual resource control set associated with an entity must have
1450Sstevel@tonic-gate  *   its rcs_lock held for the duration of any operations that would add
1460Sstevel@tonic-gate  *   resource controls or control values to the set.
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  *   The locking subsequence of interest is: p_lock, rctl_dict_lock,
1490Sstevel@tonic-gate  *   rctl_lists_lock, entity->rcs_lock.
1503684Srd117015  *
1513684Srd117015  * The projects(4) database and project entity resource controls
1523684Srd117015  *   A special case is made for RCENTITY_PROJECT values set through the
1533684Srd117015  *   setproject(3PROJECT) interface.  setproject() makes use of a private
1543684Srd117015  *   interface, setprojrctl(), which passes through an array of resource control
1553684Srd117015  *   blocks that need to be set while holding the entity->rcs_lock.  This
1563684Srd117015  *   ensures that the act of modifying a project's resource controls is
1573684Srd117015  *   "atomic" within the kernel.
1583684Srd117015  *
1593684Srd117015  *   Within the rctl sub-system, we provide two interfaces that are only used by
1603684Srd117015  *   the setprojrctl() code path - rctl_local_insert_all() and
1613684Srd117015  *   rctl_local_replace_all().  rctl_local_insert_all() will ensure that the
1623684Srd117015  *   resource values specified in *new_values are applied.
1633684Srd117015  *   rctl_local_replace_all() will purge the current rctl->rc_projdb and
1643684Srd117015  *   rctl->rc_values entries, and apply the *new_values.
1653684Srd117015  *
1663684Srd117015  *   These functions modify not only the linked list of active resource controls
1673684Srd117015  *   (rctl->rc_values), but also a "cached" linked list (rctl->rc_projdb) of
1683684Srd117015  *   values set through these interfaces.  To clarify:
1693684Srd117015  *
1703684Srd117015  *      rctl->rc_values - a linked list of rctl_val_t.  These are the active
1713684Srd117015  *      resource values associated with this rctl, and may have been set by
1723684Srd117015  *      setrctl() - via prctl(1M), or by setprojrctl() - via
1733684Srd117015  *      setproject(3PROJECT).
1743684Srd117015  *
1753684Srd117015  *      rctl->rc_projdb - a linked list of rctl_val_t.  These reflect the
1763684Srd117015  *      resource values set by the setprojrctl() code path.  rc_projdb is not
1773684Srd117015  *      referenced by any other component of the rctl sub-system.
1783684Srd117015  *
1793684Srd117015  *   As various locks are held when calling these functions, we ensure that all
1803684Srd117015  *   the possible memory allocations are performed prior to calling the
1813684Srd117015  *   function.  *alloc_values is a linked list of uninitialized rctl_val_t,
1823684Srd117015  *   which may be used to duplicate a new resource control value (passed in as
1833684Srd117015  *   one of the members of the *new_values linked list), in order to populate
1843684Srd117015  *   rctl->rc_values.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate id_t max_rctl_hndl = 32768;
1880Sstevel@tonic-gate int rctl_dict_size = 64;
1890Sstevel@tonic-gate int rctl_set_size = 8;
1900Sstevel@tonic-gate kmutex_t rctl_dict_lock;
1910Sstevel@tonic-gate mod_hash_t *rctl_dict;
1920Sstevel@tonic-gate mod_hash_t *rctl_dict_by_name;
1930Sstevel@tonic-gate id_space_t *rctl_ids;
1940Sstevel@tonic-gate kmem_cache_t *rctl_cache;	/* kmem cache for rctl structures */
1950Sstevel@tonic-gate kmem_cache_t *rctl_val_cache;	/* kmem cache for rctl values */
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate kmutex_t rctl_lists_lock;
1980Sstevel@tonic-gate rctl_dict_entry_t *rctl_lists[RC_MAX_ENTITY + 1];
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * Default resource control operations and ops vector
2020Sstevel@tonic-gate  *   To be used if the particular rcontrol has no specific actions defined, or
2030Sstevel@tonic-gate  *   if the subsystem providing the control is quiescing (in preparation for
2040Sstevel@tonic-gate  *   unloading, presumably.)
2050Sstevel@tonic-gate  *
2060Sstevel@tonic-gate  *   Resource controls with callbacks should fill the unused operations with the
2070Sstevel@tonic-gate  *   appropriate default impotent callback.
2080Sstevel@tonic-gate  */
2090Sstevel@tonic-gate /*ARGSUSED*/
2100Sstevel@tonic-gate void
rcop_no_action(struct rctl * r,struct proc * p,rctl_entity_p_t * e)2110Sstevel@tonic-gate rcop_no_action(struct rctl *r, struct proc *p, rctl_entity_p_t *e)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*ARGSUSED*/
2160Sstevel@tonic-gate rctl_qty_t
rcop_no_usage(struct rctl * r,struct proc * p)2170Sstevel@tonic-gate rcop_no_usage(struct rctl *r, struct proc *p)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	return (0);
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*ARGSUSED*/
2230Sstevel@tonic-gate int
rcop_no_set(struct rctl * r,struct proc * p,rctl_entity_p_t * e,rctl_qty_t l)2240Sstevel@tonic-gate rcop_no_set(struct rctl *r, struct proc *p, rctl_entity_p_t *e, rctl_qty_t l)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	return (0);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*ARGSUSED*/
2300Sstevel@tonic-gate int
rcop_no_test(struct rctl * r,struct proc * p,rctl_entity_p_t * e,struct rctl_val * rv,rctl_qty_t i,uint_t f)2310Sstevel@tonic-gate rcop_no_test(struct rctl *r, struct proc *p, rctl_entity_p_t *e,
2320Sstevel@tonic-gate     struct rctl_val *rv, rctl_qty_t i, uint_t f)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	return (0);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate rctl_ops_t rctl_default_ops = {
2380Sstevel@tonic-gate 	rcop_no_action,
2390Sstevel@tonic-gate 	rcop_no_usage,
2400Sstevel@tonic-gate 	rcop_no_set,
2410Sstevel@tonic-gate 	rcop_no_test
2420Sstevel@tonic-gate };
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /*
2450Sstevel@tonic-gate  * Default "absolute" resource control operation and ops vector
2460Sstevel@tonic-gate  *   Useful if there is no usage associated with the
2470Sstevel@tonic-gate  *   resource control.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate /*ARGSUSED*/
2500Sstevel@tonic-gate int
rcop_absolute_test(struct rctl * r,struct proc * p,rctl_entity_p_t * e,struct rctl_val * rv,rctl_qty_t i,uint_t f)2510Sstevel@tonic-gate rcop_absolute_test(struct rctl *r, struct proc *p, rctl_entity_p_t *e,
2520Sstevel@tonic-gate     struct rctl_val *rv, rctl_qty_t i, uint_t f)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate 	return (i > rv->rcv_value);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate rctl_ops_t rctl_absolute_ops = {
2580Sstevel@tonic-gate 	rcop_no_action,
2590Sstevel@tonic-gate 	rcop_no_usage,
2600Sstevel@tonic-gate 	rcop_no_set,
2610Sstevel@tonic-gate 	rcop_absolute_test
2620Sstevel@tonic-gate };
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*ARGSUSED*/
2650Sstevel@tonic-gate static uint_t
rctl_dict_hash_by_id(void * hash_data,mod_hash_key_t key)2660Sstevel@tonic-gate rctl_dict_hash_by_id(void *hash_data, mod_hash_key_t key)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	return ((uint_t)(uintptr_t)key % rctl_dict_size);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static int
rctl_dict_id_cmp(mod_hash_key_t key1,mod_hash_key_t key2)2720Sstevel@tonic-gate rctl_dict_id_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	uint_t u1 = (uint_t)(uintptr_t)key1;
2750Sstevel@tonic-gate 	uint_t u2 = (uint_t)(uintptr_t)key2;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (u1 > u2)
2780Sstevel@tonic-gate 		return (1);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if (u1 == u2)
2810Sstevel@tonic-gate 		return (0);
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	return (-1);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate static void
rctl_dict_val_dtor(mod_hash_val_t val)2870Sstevel@tonic-gate rctl_dict_val_dtor(mod_hash_val_t val)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	rctl_dict_entry_t *kr = (rctl_dict_entry_t *)val;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	kmem_free(kr, sizeof (rctl_dict_entry_t));
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * size_t rctl_build_name_buf()
2960Sstevel@tonic-gate  *
2970Sstevel@tonic-gate  * Overview
2980Sstevel@tonic-gate  *   rctl_build_name_buf() walks all active resource controls in the dictionary,
2990Sstevel@tonic-gate  *   building a buffer of continguous NUL-terminated strings.
3000Sstevel@tonic-gate  *
3010Sstevel@tonic-gate  * Return values
3020Sstevel@tonic-gate  *   The size of the buffer is returned, the passed pointer's contents are
3030Sstevel@tonic-gate  *   modified to that of the location of the buffer.
3040Sstevel@tonic-gate  *
3050Sstevel@tonic-gate  * Caller's context
3060Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate size_t
rctl_build_name_buf(char ** rbufp)3090Sstevel@tonic-gate rctl_build_name_buf(char **rbufp)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	size_t req_size, cpy_size;
3120Sstevel@tonic-gate 	char *rbufloc;
3130Sstevel@tonic-gate 	int i;
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate rctl_rebuild_name_buf:
3160Sstevel@tonic-gate 	req_size = cpy_size = 0;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	/*
3190Sstevel@tonic-gate 	 * Calculate needed buffer length.
3200Sstevel@tonic-gate 	 */
3210Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
3220Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
3230Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 		for (rde = rctl_lists[i];
3260Sstevel@tonic-gate 		    rde != NULL;
3270Sstevel@tonic-gate 		    rde = rde->rcd_next)
3280Sstevel@tonic-gate 			req_size += strlen(rde->rcd_name) + 1;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	rbufloc = *rbufp = kmem_alloc(req_size, KM_SLEEP);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * Copy rctl names into our buffer.  If the copy length exceeds the
3360Sstevel@tonic-gate 	 * allocate length (due to registration changes), stop copying, free the
3370Sstevel@tonic-gate 	 * buffer, and start again.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
3400Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
3410Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		for (rde = rctl_lists[i];
3440Sstevel@tonic-gate 		    rde != NULL;
3450Sstevel@tonic-gate 		    rde = rde->rcd_next) {
3460Sstevel@tonic-gate 			size_t length = strlen(rde->rcd_name) + 1;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 			cpy_size += length;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 			if (cpy_size > req_size) {
3510Sstevel@tonic-gate 				kmem_free(*rbufp, req_size);
3520Sstevel@tonic-gate 				mutex_exit(&rctl_lists_lock);
3530Sstevel@tonic-gate 				goto rctl_rebuild_name_buf;
3540Sstevel@tonic-gate 			}
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 			bcopy(rde->rcd_name, rbufloc, length);
3570Sstevel@tonic-gate 			rbufloc += length;
3580Sstevel@tonic-gate 		}
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	return (req_size);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * rctl_dict_entry_t *rctl_dict_lookup(const char *)
3670Sstevel@tonic-gate  *
3680Sstevel@tonic-gate  * Overview
3690Sstevel@tonic-gate  *   rctl_dict_lookup() returns the resource control dictionary entry for the
3700Sstevel@tonic-gate  *   named resource control.
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  * Return values
3730Sstevel@tonic-gate  *   A pointer to the appropriate resource control dictionary entry, or NULL if
3740Sstevel@tonic-gate  *   no such named entry exists.
3750Sstevel@tonic-gate  *
3760Sstevel@tonic-gate  * Caller's context
3770Sstevel@tonic-gate  *   Caller must not be holding rctl_dict_lock.
3780Sstevel@tonic-gate  */
3790Sstevel@tonic-gate rctl_dict_entry_t *
rctl_dict_lookup(const char * name)3800Sstevel@tonic-gate rctl_dict_lookup(const char *name)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	mutex_enter(&rctl_dict_lock);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict_by_name, (mod_hash_key_t)name,
3870Sstevel@tonic-gate 	    (mod_hash_val_t *)&rde) == MH_ERR_NOTFOUND) {
3880Sstevel@tonic-gate 		mutex_exit(&rctl_dict_lock);
3890Sstevel@tonic-gate 		return (NULL);
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	mutex_exit(&rctl_dict_lock);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return (rde);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * rctl_hndl_t rctl_hndl_lookup(const char *)
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  * Overview
4010Sstevel@tonic-gate  *   rctl_hndl_lookup() returns the resource control id (the "handle") for the
4020Sstevel@tonic-gate  *   named resource control.
4030Sstevel@tonic-gate  *
4040Sstevel@tonic-gate  * Return values
4050Sstevel@tonic-gate  *   The appropriate id, or -1 if no such named entry exists.
4060Sstevel@tonic-gate  *
4070Sstevel@tonic-gate  * Caller's context
4080Sstevel@tonic-gate  *   Caller must not be holding rctl_dict_lock.
4090Sstevel@tonic-gate  */
4100Sstevel@tonic-gate rctl_hndl_t
rctl_hndl_lookup(const char * name)4110Sstevel@tonic-gate rctl_hndl_lookup(const char *name)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((rde = rctl_dict_lookup(name)) == NULL)
4160Sstevel@tonic-gate 		return (-1);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	return (rde->rcd_id);
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate  * rctl_dict_entry_t * rctl_dict_lookup_hndl(rctl_hndl_t)
4230Sstevel@tonic-gate  *
4240Sstevel@tonic-gate  * Overview
4250Sstevel@tonic-gate  *   rctl_dict_lookup_hndl() completes the public lookup functions, by returning
4260Sstevel@tonic-gate  *   the resource control dictionary entry matching a given resource control id.
4270Sstevel@tonic-gate  *
4280Sstevel@tonic-gate  * Return values
4290Sstevel@tonic-gate  *   A pointer to the matching resource control dictionary entry, or NULL if the
4300Sstevel@tonic-gate  *   id does not match any existing entries.
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * Caller's context
4330Sstevel@tonic-gate  *   Caller must not be holding rctl_lists_lock.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate rctl_dict_entry_t *
rctl_dict_lookup_hndl(rctl_hndl_t hndl)4360Sstevel@tonic-gate rctl_dict_lookup_hndl(rctl_hndl_t hndl)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate 	uint_t i;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
4410Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
4420Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 		for (rde = rctl_lists[i];
4450Sstevel@tonic-gate 		    rde != NULL;
4460Sstevel@tonic-gate 		    rde = rde->rcd_next)
4470Sstevel@tonic-gate 			if (rde->rcd_id == hndl) {
4480Sstevel@tonic-gate 				mutex_exit(&rctl_lists_lock);
4490Sstevel@tonic-gate 				return (rde);
4500Sstevel@tonic-gate 			}
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 	return (NULL);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate  * void rctl_add_default_limit(const char *name, rctl_qty_t value,
4590Sstevel@tonic-gate  *     rctl_priv_t privilege, uint_t action)
4600Sstevel@tonic-gate  *
4610Sstevel@tonic-gate  * Overview
4620Sstevel@tonic-gate  *   Create a default limit with specified value, privilege, and action.
4630Sstevel@tonic-gate  *
4640Sstevel@tonic-gate  * Return value
4650Sstevel@tonic-gate  *   No value returned.
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate void
rctl_add_default_limit(const char * name,rctl_qty_t value,rctl_priv_t privilege,uint_t action)4680Sstevel@tonic-gate rctl_add_default_limit(const char *name, rctl_qty_t value,
4690Sstevel@tonic-gate     rctl_priv_t privilege, uint_t action)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate 	rctl_val_t *dval;
4720Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4750Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
4760Sstevel@tonic-gate 	dval->rcv_value = value;
4770Sstevel@tonic-gate 	dval->rcv_privilege = privilege;
4780Sstevel@tonic-gate 	dval->rcv_flagaction = action;
4790Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	rde = rctl_dict_lookup(name);
4820Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate  * void rctl_add_legacy_limit(const char *name, const char *mname,
4870Sstevel@tonic-gate  *     const char *lname, rctl_qty_t dflt)
4880Sstevel@tonic-gate  *
4890Sstevel@tonic-gate  * Overview
4900Sstevel@tonic-gate  *   Create a default privileged limit, using the value obtained from
4910Sstevel@tonic-gate  *   /etc/system if it exists and is greater than the specified default
4920Sstevel@tonic-gate  *   value.  Exists primarily for System V IPC.
4930Sstevel@tonic-gate  *
4940Sstevel@tonic-gate  * Return value
4950Sstevel@tonic-gate  *   No value returned.
4960Sstevel@tonic-gate  */
4970Sstevel@tonic-gate void
rctl_add_legacy_limit(const char * name,const char * mname,const char * lname,rctl_qty_t dflt,rctl_qty_t max)4980Sstevel@tonic-gate rctl_add_legacy_limit(const char *name, const char *mname, const char *lname,
4990Sstevel@tonic-gate     rctl_qty_t dflt, rctl_qty_t max)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	rctl_qty_t qty;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	if (!mod_sysvar(mname, lname, &qty) || (qty < dflt))
5040Sstevel@tonic-gate 		qty = dflt;
5050Sstevel@tonic-gate 
5060Sstevel@tonic-gate 	if (qty > max)
5070Sstevel@tonic-gate 		qty = max;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	rctl_add_default_limit(name, qty, RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate 
5129640SMenno.Lageman@Sun.COM rctl_set_t *
rctl_entity_obtain_rset(rctl_dict_entry_t * rcd,struct proc * p)5130Sstevel@tonic-gate rctl_entity_obtain_rset(rctl_dict_entry_t *rcd, struct proc *p)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate 	rctl_set_t *rset = NULL;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (rcd == NULL)
5180Sstevel@tonic-gate 		return (NULL);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	switch (rcd->rcd_entity) {
5210Sstevel@tonic-gate 	case RCENTITY_PROCESS:
5220Sstevel@tonic-gate 		rset = p->p_rctls;
5230Sstevel@tonic-gate 		break;
5240Sstevel@tonic-gate 	case RCENTITY_TASK:
5250Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5260Sstevel@tonic-gate 		if (p->p_task != NULL)
5270Sstevel@tonic-gate 			rset = p->p_task->tk_rctls;
5280Sstevel@tonic-gate 		break;
5290Sstevel@tonic-gate 	case RCENTITY_PROJECT:
5300Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5310Sstevel@tonic-gate 		if (p->p_task != NULL &&
5320Sstevel@tonic-gate 		    p->p_task->tk_proj != NULL)
5330Sstevel@tonic-gate 			rset = p->p_task->tk_proj->kpj_rctls;
5340Sstevel@tonic-gate 		break;
5350Sstevel@tonic-gate 	case RCENTITY_ZONE:
5360Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5370Sstevel@tonic-gate 		if (p->p_zone != NULL)
5380Sstevel@tonic-gate 			rset = p->p_zone->zone_rctls;
5390Sstevel@tonic-gate 		break;
5400Sstevel@tonic-gate 	default:
5410Sstevel@tonic-gate 		panic("unknown rctl entity type %d seen", rcd->rcd_entity);
5420Sstevel@tonic-gate 		break;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	return (rset);
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate static void
rctl_entity_obtain_entity_p(rctl_entity_t entity,struct proc * p,rctl_entity_p_t * e)5490Sstevel@tonic-gate rctl_entity_obtain_entity_p(rctl_entity_t entity, struct proc *p,
5500Sstevel@tonic-gate     rctl_entity_p_t *e)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	e->rcep_p.proc = NULL;
5530Sstevel@tonic-gate 	e->rcep_t = entity;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	switch (entity) {
5560Sstevel@tonic-gate 	case RCENTITY_PROCESS:
5570Sstevel@tonic-gate 		e->rcep_p.proc = p;
5580Sstevel@tonic-gate 		break;
5590Sstevel@tonic-gate 	case RCENTITY_TASK:
5600Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5610Sstevel@tonic-gate 		if (p->p_task != NULL)
5620Sstevel@tonic-gate 			e->rcep_p.task = p->p_task;
5630Sstevel@tonic-gate 		break;
5640Sstevel@tonic-gate 	case RCENTITY_PROJECT:
5650Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5660Sstevel@tonic-gate 		if (p->p_task != NULL &&
5670Sstevel@tonic-gate 		    p->p_task->tk_proj != NULL)
5680Sstevel@tonic-gate 			e->rcep_p.proj = p->p_task->tk_proj;
5690Sstevel@tonic-gate 		break;
5700Sstevel@tonic-gate 	case RCENTITY_ZONE:
5710Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5720Sstevel@tonic-gate 		if (p->p_zone != NULL)
5730Sstevel@tonic-gate 			e->rcep_p.zone = p->p_zone;
5740Sstevel@tonic-gate 		break;
5750Sstevel@tonic-gate 	default:
5760Sstevel@tonic-gate 		panic("unknown rctl entity type %d seen", entity);
5770Sstevel@tonic-gate 		break;
5780Sstevel@tonic-gate 	}
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate static void
rctl_gp_alloc(rctl_alloc_gp_t * rcgp)5820Sstevel@tonic-gate rctl_gp_alloc(rctl_alloc_gp_t *rcgp)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate 	uint_t i;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	if (rcgp->rcag_nctls > 0) {
5870Sstevel@tonic-gate 		rctl_t *prev = kmem_cache_alloc(rctl_cache, KM_SLEEP);
5880Sstevel@tonic-gate 		rctl_t *rctl = prev;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		rcgp->rcag_ctls = prev;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		for (i = 1; i < rcgp->rcag_nctls; i++) {
5930Sstevel@tonic-gate 			rctl = kmem_cache_alloc(rctl_cache, KM_SLEEP);
5940Sstevel@tonic-gate 			prev->rc_next = rctl;
5950Sstevel@tonic-gate 			prev = rctl;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		rctl->rc_next = NULL;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	if (rcgp->rcag_nvals > 0) {
6020Sstevel@tonic-gate 		rctl_val_t *prev = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
6030Sstevel@tonic-gate 		rctl_val_t *rval = prev;
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 		rcgp->rcag_vals = prev;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 		for (i = 1; i < rcgp->rcag_nvals; i++) {
6080Sstevel@tonic-gate 			rval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
6090Sstevel@tonic-gate 			prev->rcv_next = rval;
6100Sstevel@tonic-gate 			prev = rval;
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		rval->rcv_next = NULL;
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate }
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate static rctl_val_t *
rctl_gp_detach_val(rctl_alloc_gp_t * rcgp)6190Sstevel@tonic-gate rctl_gp_detach_val(rctl_alloc_gp_t *rcgp)
6200Sstevel@tonic-gate {
6210Sstevel@tonic-gate 	rctl_val_t *rval = rcgp->rcag_vals;
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	ASSERT(rcgp->rcag_nvals > 0);
6240Sstevel@tonic-gate 	rcgp->rcag_nvals--;
6250Sstevel@tonic-gate 	rcgp->rcag_vals = rval->rcv_next;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	rval->rcv_next = NULL;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	return (rval);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate static rctl_t *
rctl_gp_detach_ctl(rctl_alloc_gp_t * rcgp)6330Sstevel@tonic-gate rctl_gp_detach_ctl(rctl_alloc_gp_t *rcgp)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	rctl_t *rctl = rcgp->rcag_ctls;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	ASSERT(rcgp->rcag_nctls > 0);
6380Sstevel@tonic-gate 	rcgp->rcag_nctls--;
6390Sstevel@tonic-gate 	rcgp->rcag_ctls = rctl->rc_next;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	rctl->rc_next = NULL;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (rctl);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate static void
rctl_gp_free(rctl_alloc_gp_t * rcgp)6480Sstevel@tonic-gate rctl_gp_free(rctl_alloc_gp_t *rcgp)
6490Sstevel@tonic-gate {
6500Sstevel@tonic-gate 	rctl_val_t *rval = rcgp->rcag_vals;
6510Sstevel@tonic-gate 	rctl_t *rctl = rcgp->rcag_ctls;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	while (rval != NULL) {
6540Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, rval);
6570Sstevel@tonic-gate 		rval = next;
6580Sstevel@tonic-gate 	}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	while (rctl != NULL) {
6610Sstevel@tonic-gate 		rctl_t *next = rctl->rc_next;
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate 		kmem_cache_free(rctl_cache, rctl);
6640Sstevel@tonic-gate 		rctl = next;
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * void rctl_prealloc_destroy(rctl_alloc_gp_t *)
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * Overview
6720Sstevel@tonic-gate  *   Release all unused memory allocated via one of the "prealloc" functions:
6730Sstevel@tonic-gate  *   rctl_set_init_prealloc, rctl_set_dup_prealloc, or rctl_rlimit_set_prealloc.
6740Sstevel@tonic-gate  *
6750Sstevel@tonic-gate  * Return values
6760Sstevel@tonic-gate  *   None.
6770Sstevel@tonic-gate  *
6780Sstevel@tonic-gate  * Caller's context
6790Sstevel@tonic-gate  *   No restrictions on context.
6800Sstevel@tonic-gate  */
6810Sstevel@tonic-gate void
rctl_prealloc_destroy(rctl_alloc_gp_t * gp)6820Sstevel@tonic-gate rctl_prealloc_destroy(rctl_alloc_gp_t *gp)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate 	rctl_gp_free(gp);
6850Sstevel@tonic-gate 	kmem_free(gp, sizeof (rctl_alloc_gp_t));
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate /*
6890Sstevel@tonic-gate  * int rctl_val_cmp(rctl_val_t *, rctl_val_t *, int)
6900Sstevel@tonic-gate  *
6910Sstevel@tonic-gate  * Overview
6920Sstevel@tonic-gate  *   This function defines an ordering to rctl_val_t's in order to allow
6930Sstevel@tonic-gate  *   for correct placement in value lists. When the imprecise flag is set,
6940Sstevel@tonic-gate  *   the action recipient is ignored. This is to facilitate insert,
6950Sstevel@tonic-gate  *   delete, and replace operations by rctlsys.
6960Sstevel@tonic-gate  *
6970Sstevel@tonic-gate  * Return values
6980Sstevel@tonic-gate  *   0 if the val_t's are are considered identical
6990Sstevel@tonic-gate  *   -1 if a is ordered lower than b
7000Sstevel@tonic-gate  *   1 if a is lowered higher than b
7010Sstevel@tonic-gate  *
7020Sstevel@tonic-gate  * Caller's context
7030Sstevel@tonic-gate  *   No restrictions on context.
7040Sstevel@tonic-gate  */
7050Sstevel@tonic-gate int
rctl_val_cmp(rctl_val_t * a,rctl_val_t * b,int imprecise)7060Sstevel@tonic-gate rctl_val_cmp(rctl_val_t *a, rctl_val_t *b, int imprecise)
7070Sstevel@tonic-gate {
7080Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_MAXIMAL) <
7090Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_MAXIMAL))
7100Sstevel@tonic-gate 		return (-1);
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_MAXIMAL) >
7130Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_MAXIMAL))
7140Sstevel@tonic-gate 		return (1);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	if (a->rcv_value < b->rcv_value)
7170Sstevel@tonic-gate 		return (-1);
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if (a->rcv_value > b->rcv_value)
7200Sstevel@tonic-gate 		return (1);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_DENY) <
7230Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_DENY))
7240Sstevel@tonic-gate 		return (-1);
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_DENY) >
7270Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_DENY))
7280Sstevel@tonic-gate 		return (1);
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	if (a->rcv_privilege < b->rcv_privilege)
7310Sstevel@tonic-gate 		return (-1);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (a->rcv_privilege > b->rcv_privilege)
7340Sstevel@tonic-gate 		return (1);
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (imprecise)
7370Sstevel@tonic-gate 		return (0);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (a->rcv_action_recip_pid < b->rcv_action_recip_pid)
7400Sstevel@tonic-gate 		return (-1);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	if (a->rcv_action_recip_pid > b->rcv_action_recip_pid)
7430Sstevel@tonic-gate 		return (1);
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	return (0);
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate static rctl_val_t *
rctl_val_list_find(rctl_val_t ** head,rctl_val_t * cval)7490Sstevel@tonic-gate rctl_val_list_find(rctl_val_t **head, rctl_val_t *cval)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate 	rctl_val_t *rval = *head;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	while (rval != NULL) {
7540Sstevel@tonic-gate 		if (rctl_val_cmp(cval, rval, 0) == 0)
7550Sstevel@tonic-gate 			return (rval);
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		rval = rval->rcv_next;
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	return (NULL);
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate  * int rctl_val_list_insert(rctl_val_t **, rctl_val_t *)
7660Sstevel@tonic-gate  *
7670Sstevel@tonic-gate  * Overview
7680Sstevel@tonic-gate  *   This function inserts the rctl_val_t into the value list provided.
7690Sstevel@tonic-gate  *   The insert is always successful unless if the value is a duplicate
7700Sstevel@tonic-gate  *   of one already in the list.
7710Sstevel@tonic-gate  *
7720Sstevel@tonic-gate  * Return values
7730Sstevel@tonic-gate  *    1 if the value was a duplicate of an existing value in the list.
7740Sstevel@tonic-gate  *    0 if the insert was successful.
7750Sstevel@tonic-gate  */
7760Sstevel@tonic-gate int
rctl_val_list_insert(rctl_val_t ** root,rctl_val_t * rval)7770Sstevel@tonic-gate rctl_val_list_insert(rctl_val_t **root, rctl_val_t *rval)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	rctl_val_t *prev;
7800Sstevel@tonic-gate 	int equiv;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	rval->rcv_next = NULL;
7830Sstevel@tonic-gate 	rval->rcv_prev = NULL;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if (*root == NULL) {
7860Sstevel@tonic-gate 		*root = rval;
7870Sstevel@tonic-gate 		return (0);
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	equiv = rctl_val_cmp(rval, *root, 0);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	if (equiv == 0)
7930Sstevel@tonic-gate 		return (1);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if (equiv < 0) {
7960Sstevel@tonic-gate 		rval->rcv_next = *root;
7970Sstevel@tonic-gate 		rval->rcv_next->rcv_prev = rval;
7980Sstevel@tonic-gate 		*root = rval;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 		return (0);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	prev = *root;
8040Sstevel@tonic-gate 	while (prev->rcv_next != NULL &&
8050Sstevel@tonic-gate 	    (equiv = rctl_val_cmp(rval, prev->rcv_next, 0)) > 0) {
8060Sstevel@tonic-gate 		prev = prev->rcv_next;
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (equiv == 0)
8100Sstevel@tonic-gate 		return (1);
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	rval->rcv_next = prev->rcv_next;
8130Sstevel@tonic-gate 	if (rval->rcv_next != NULL)
8140Sstevel@tonic-gate 		rval->rcv_next->rcv_prev = rval;
8150Sstevel@tonic-gate 	prev->rcv_next = rval;
8160Sstevel@tonic-gate 	rval->rcv_prev = prev;
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	return (0);
8190Sstevel@tonic-gate }
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate static int
rctl_val_list_delete(rctl_val_t ** root,rctl_val_t * rval)8220Sstevel@tonic-gate rctl_val_list_delete(rctl_val_t **root, rctl_val_t *rval)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	rctl_val_t *prev;
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	if (*root == NULL)
8270Sstevel@tonic-gate 		return (-1);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	prev = *root;
8300Sstevel@tonic-gate 	if (rctl_val_cmp(rval, prev, 0) == 0) {
8310Sstevel@tonic-gate 		*root = prev->rcv_next;
8325039Srd117015 		if (*root != NULL)
8335039Srd117015 			(*root)->rcv_prev = NULL;
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, prev);
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 		return (0);
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	while (prev->rcv_next != NULL &&
8410Sstevel@tonic-gate 	    rctl_val_cmp(rval, prev->rcv_next, 0) != 0) {
8420Sstevel@tonic-gate 		prev = prev->rcv_next;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 	if (prev->rcv_next == NULL) {
8460Sstevel@tonic-gate 		/*
8470Sstevel@tonic-gate 		 * If we navigate the entire list and cannot find a match, then
8480Sstevel@tonic-gate 		 * return failure.
8490Sstevel@tonic-gate 		 */
8500Sstevel@tonic-gate 		return (-1);
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	prev = prev->rcv_next;
8540Sstevel@tonic-gate 	prev->rcv_prev->rcv_next = prev->rcv_next;
8550Sstevel@tonic-gate 	if (prev->rcv_next != NULL)
8560Sstevel@tonic-gate 		prev->rcv_next->rcv_prev = prev->rcv_prev;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, prev);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	return (0);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate static rctl_val_t *
rctl_val_list_dup(rctl_val_t * rval,rctl_alloc_gp_t * ragp,struct proc * oldp,struct proc * newp)8640Sstevel@tonic-gate rctl_val_list_dup(rctl_val_t *rval, rctl_alloc_gp_t *ragp, struct proc *oldp,
8650Sstevel@tonic-gate     struct proc *newp)
8660Sstevel@tonic-gate {
8670Sstevel@tonic-gate 	rctl_val_t *head = NULL;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next) {
8700Sstevel@tonic-gate 		rctl_val_t *dval = rctl_gp_detach_val(ragp);
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 		bcopy(rval, dval, sizeof (rctl_val_t));
8730Sstevel@tonic-gate 		dval->rcv_prev = dval->rcv_next = NULL;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		if (oldp == NULL ||
8760Sstevel@tonic-gate 		    rval->rcv_action_recipient == NULL ||
8770Sstevel@tonic-gate 		    rval->rcv_action_recipient == oldp) {
8780Sstevel@tonic-gate 			if (rval->rcv_privilege == RCPRIV_BASIC) {
8790Sstevel@tonic-gate 				dval->rcv_action_recipient = newp;
8800Sstevel@tonic-gate 				dval->rcv_action_recip_pid = newp->p_pid;
8810Sstevel@tonic-gate 			} else {
8820Sstevel@tonic-gate 				dval->rcv_action_recipient = NULL;
8830Sstevel@tonic-gate 				dval->rcv_action_recip_pid = -1;
8840Sstevel@tonic-gate 			}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			(void) rctl_val_list_insert(&head, dval);
8870Sstevel@tonic-gate 		} else {
8880Sstevel@tonic-gate 			kmem_cache_free(rctl_val_cache, dval);
8890Sstevel@tonic-gate 		}
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	return (head);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate static void
rctl_val_list_reset(rctl_val_t * rval)8960Sstevel@tonic-gate rctl_val_list_reset(rctl_val_t *rval)
8970Sstevel@tonic-gate {
8980Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next)
8990Sstevel@tonic-gate 		rval->rcv_firing_time = 0;
9000Sstevel@tonic-gate }
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate static uint_t
rctl_val_list_count(rctl_val_t * rval)9030Sstevel@tonic-gate rctl_val_list_count(rctl_val_t *rval)
9040Sstevel@tonic-gate {
9050Sstevel@tonic-gate 	uint_t n = 0;
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next)
9080Sstevel@tonic-gate 		n++;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	return (n);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate static void
rctl_val_list_free(rctl_val_t * rval)9150Sstevel@tonic-gate rctl_val_list_free(rctl_val_t *rval)
9160Sstevel@tonic-gate {
9170Sstevel@tonic-gate 	while (rval != NULL) {
9180Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, rval);
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		rval = next;
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate /*
9270Sstevel@tonic-gate  * rctl_qty_t rctl_model_maximum(rctl_dict_entry_t *, struct proc *)
9280Sstevel@tonic-gate  *
9290Sstevel@tonic-gate  * Overview
9300Sstevel@tonic-gate  *   In cases where the operating system supports more than one process
9310Sstevel@tonic-gate  *   addressing model, the operating system capabilities will exceed those of
9320Sstevel@tonic-gate  *   one or more of these models.  Processes in a less capable model must have
9330Sstevel@tonic-gate  *   their resources accurately controlled, without diluting those of their
9340Sstevel@tonic-gate  *   descendants reached via exec().  rctl_model_maximum() returns the governing
9350Sstevel@tonic-gate  *   value for the specified process with respect to a resource control, such
9360Sstevel@tonic-gate  *   that the value can used for the RCTLOP_SET callback or compatability
9370Sstevel@tonic-gate  *   support.
9380Sstevel@tonic-gate  *
9390Sstevel@tonic-gate  * Return values
9400Sstevel@tonic-gate  *   The maximum value for the given process for the specified resource control.
9410Sstevel@tonic-gate  *
9420Sstevel@tonic-gate  * Caller's context
9430Sstevel@tonic-gate  *   No restrictions on context.
9440Sstevel@tonic-gate  */
9450Sstevel@tonic-gate rctl_qty_t
rctl_model_maximum(rctl_dict_entry_t * rde,struct proc * p)9460Sstevel@tonic-gate rctl_model_maximum(rctl_dict_entry_t *rde, struct proc *p)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE)
9490Sstevel@tonic-gate 		return (rde->rcd_max_native);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	return (rde->rcd_max_ilp32);
9520Sstevel@tonic-gate }
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /*
9550Sstevel@tonic-gate  * rctl_qty_t rctl_model_value(rctl_dict_entry_t *, struct proc *, rctl_qty_t)
9560Sstevel@tonic-gate  *
9570Sstevel@tonic-gate  * Overview
9580Sstevel@tonic-gate  *   Convenience function wrapping the rctl_model_maximum() functionality.
9590Sstevel@tonic-gate  *
9600Sstevel@tonic-gate  * Return values
9610Sstevel@tonic-gate  *   The lesser of the process's maximum value and the given value for the
9620Sstevel@tonic-gate  *   specified resource control.
9630Sstevel@tonic-gate  *
9640Sstevel@tonic-gate  * Caller's context
9650Sstevel@tonic-gate  *   No restrictions on context.
9660Sstevel@tonic-gate  */
9670Sstevel@tonic-gate rctl_qty_t
rctl_model_value(rctl_dict_entry_t * rde,struct proc * p,rctl_qty_t value)9680Sstevel@tonic-gate rctl_model_value(rctl_dict_entry_t *rde, struct proc *p, rctl_qty_t value)
9690Sstevel@tonic-gate {
9700Sstevel@tonic-gate 	rctl_qty_t max = rctl_model_maximum(rde, p);
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	return (value < max ? value : max);
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate static void
rctl_set_insert(rctl_set_t * set,rctl_hndl_t hndl,rctl_t * rctl)9760Sstevel@tonic-gate rctl_set_insert(rctl_set_t *set, rctl_hndl_t hndl, rctl_t *rctl)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	uint_t index = hndl % rctl_set_size;
9790Sstevel@tonic-gate 	rctl_t *next_ctl, *prev_ctl;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	rctl->rc_next = NULL;
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	if (set->rcs_ctls[index] == NULL) {
9860Sstevel@tonic-gate 		set->rcs_ctls[index] = rctl;
9870Sstevel@tonic-gate 		return;
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if (hndl < set->rcs_ctls[index]->rc_id) {
9910Sstevel@tonic-gate 		rctl->rc_next = set->rcs_ctls[index];
9920Sstevel@tonic-gate 		set->rcs_ctls[index] = rctl;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		return;
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	for (next_ctl = set->rcs_ctls[index]->rc_next,
9980Sstevel@tonic-gate 	    prev_ctl = set->rcs_ctls[index];
9990Sstevel@tonic-gate 	    next_ctl != NULL;
10000Sstevel@tonic-gate 	    prev_ctl = next_ctl,
10010Sstevel@tonic-gate 	    next_ctl = next_ctl->rc_next) {
10020Sstevel@tonic-gate 		if (next_ctl->rc_id > hndl) {
10030Sstevel@tonic-gate 			rctl->rc_next = next_ctl;
10040Sstevel@tonic-gate 			prev_ctl->rc_next = rctl;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 			return;
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	rctl->rc_next = next_ctl;
10110Sstevel@tonic-gate 	prev_ctl->rc_next = rctl;
10120Sstevel@tonic-gate }
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate /*
10150Sstevel@tonic-gate  * rctl_set_t *rctl_set_create()
10160Sstevel@tonic-gate  *
10170Sstevel@tonic-gate  * Overview
10180Sstevel@tonic-gate  *   Create an empty resource control set, suitable for attaching to a
10190Sstevel@tonic-gate  *   controlled entity.
10200Sstevel@tonic-gate  *
10210Sstevel@tonic-gate  * Return values
10220Sstevel@tonic-gate  *   A pointer to the newly created set.
10230Sstevel@tonic-gate  *
10240Sstevel@tonic-gate  * Caller's context
10250Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
10260Sstevel@tonic-gate  */
10270Sstevel@tonic-gate rctl_set_t *
rctl_set_create()10280Sstevel@tonic-gate rctl_set_create()
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	rctl_set_t *rset = kmem_zalloc(sizeof (rctl_set_t), KM_SLEEP);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	mutex_init(&rset->rcs_lock, NULL, MUTEX_DEFAULT, NULL);
10330Sstevel@tonic-gate 	rset->rcs_ctls = kmem_zalloc(rctl_set_size * sizeof (rctl_t *),
10340Sstevel@tonic-gate 	    KM_SLEEP);
10350Sstevel@tonic-gate 	rset->rcs_entity = -1;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	return (rset);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate  * rctl_gp_alloc_t *rctl_set_init_prealloc(rctl_entity_t)
10420Sstevel@tonic-gate  *
10430Sstevel@tonic-gate  * Overview
10440Sstevel@tonic-gate  *    rctl_set_init_prealloc() examines the globally defined resource controls
10450Sstevel@tonic-gate  *    and their default values and returns a resource control allocation group
10460Sstevel@tonic-gate  *    populated with sufficient controls and values to form a representative
10470Sstevel@tonic-gate  *    resource control set for the specified entity.
10480Sstevel@tonic-gate  *
10490Sstevel@tonic-gate  * Return values
10500Sstevel@tonic-gate  *    A pointer to the newly created allocation group.
10510Sstevel@tonic-gate  *
10520Sstevel@tonic-gate  * Caller's context
10530Sstevel@tonic-gate  *    Caller must be in a context suitable for KM_SLEEP allocations.
10540Sstevel@tonic-gate  */
10550Sstevel@tonic-gate rctl_alloc_gp_t *
rctl_set_init_prealloc(rctl_entity_t entity)10560Sstevel@tonic-gate rctl_set_init_prealloc(rctl_entity_t entity)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
10590Sstevel@tonic-gate 	rctl_alloc_gp_t *ragp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	if (rctl_lists[entity] == NULL)
10640Sstevel@tonic-gate 		return (ragp);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	for (rde = rctl_lists[entity]; rde != NULL; rde = rde->rcd_next) {
10690Sstevel@tonic-gate 		ragp->rcag_nctls++;
10700Sstevel@tonic-gate 		ragp->rcag_nvals += rctl_val_list_count(rde->rcd_default_value);
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate 	rctl_gp_alloc(ragp);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	return (ragp);
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate /*
10810Sstevel@tonic-gate  * rctl_set_t *rctl_set_init(rctl_entity_t)
10820Sstevel@tonic-gate  *
10830Sstevel@tonic-gate  * Overview
10840Sstevel@tonic-gate  *   rctl_set_create() creates a resource control set, initialized with the
10850Sstevel@tonic-gate  *   system infinite values on all registered controls, for attachment to a
10860Sstevel@tonic-gate  *   system entity requiring resource controls, such as a process or a task.
10870Sstevel@tonic-gate  *
10880Sstevel@tonic-gate  * Return values
10890Sstevel@tonic-gate  *   A pointer to the newly filled set.
10900Sstevel@tonic-gate  *
10910Sstevel@tonic-gate  * Caller's context
10920Sstevel@tonic-gate  *   Caller must be holding p_lock on entry so that RCTLOP_SET() functions
10930Sstevel@tonic-gate  *   may modify task and project members based on the proc structure
10940Sstevel@tonic-gate  *   they are passed.
10950Sstevel@tonic-gate  */
10960Sstevel@tonic-gate rctl_set_t *
rctl_set_init(rctl_entity_t entity,struct proc * p,rctl_entity_p_t * e,rctl_set_t * rset,rctl_alloc_gp_t * ragp)10970Sstevel@tonic-gate rctl_set_init(rctl_entity_t entity, struct proc *p, rctl_entity_p_t *e,
10980Sstevel@tonic-gate     rctl_set_t *rset, rctl_alloc_gp_t *ragp)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
11030Sstevel@tonic-gate 	ASSERT(e);
11040Sstevel@tonic-gate 	rset->rcs_entity = entity;
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	if (rctl_lists[entity] == NULL)
11070Sstevel@tonic-gate 		return (rset);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
11100Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 	for (rde = rctl_lists[entity]; rde != NULL; rde = rde->rcd_next) {
11130Sstevel@tonic-gate 		rctl_t *rctl = rctl_gp_detach_ctl(ragp);
11140Sstevel@tonic-gate 
11150Sstevel@tonic-gate 		rctl->rc_dict_entry = rde;
11160Sstevel@tonic-gate 		rctl->rc_id = rde->rcd_id;
11173684Srd117015 		rctl->rc_projdb = NULL;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 		rctl->rc_values = rctl_val_list_dup(rde->rcd_default_value,
11200Sstevel@tonic-gate 		    ragp, NULL, p);
11210Sstevel@tonic-gate 		rctl->rc_cursor = rctl->rc_values;
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 		rctl_set_insert(rset, rde->rcd_id, rctl);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
11280Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
11290Sstevel@tonic-gate 	}
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
11320Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	return (rset);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate static rctl_t *
rctl_dup(rctl_t * rctl,rctl_alloc_gp_t * ragp,struct proc * oldp,struct proc * newp)11380Sstevel@tonic-gate rctl_dup(rctl_t *rctl, rctl_alloc_gp_t *ragp, struct proc *oldp,
11390Sstevel@tonic-gate     struct proc *newp)
11400Sstevel@tonic-gate {
11410Sstevel@tonic-gate 	rctl_t *dup = rctl_gp_detach_ctl(ragp);
11420Sstevel@tonic-gate 	rctl_val_t *dval;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	dup->rc_id = rctl->rc_id;
11450Sstevel@tonic-gate 	dup->rc_dict_entry = rctl->rc_dict_entry;
11460Sstevel@tonic-gate 	dup->rc_next = NULL;
11470Sstevel@tonic-gate 	dup->rc_cursor = NULL;
11480Sstevel@tonic-gate 	dup->rc_values = rctl_val_list_dup(rctl->rc_values, ragp, oldp, newp);
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	for (dval = dup->rc_values;
11510Sstevel@tonic-gate 	    dval != NULL; dval = dval->rcv_next) {
11520Sstevel@tonic-gate 		if (rctl_val_cmp(rctl->rc_cursor, dval, 0) >= 0) {
11530Sstevel@tonic-gate 			dup->rc_cursor = dval;
11540Sstevel@tonic-gate 			break;
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	if (dup->rc_cursor == NULL)
11590Sstevel@tonic-gate 		dup->rc_cursor = dup->rc_values;
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	return (dup);
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate static void
rctl_set_fill_alloc_gp(rctl_set_t * set,rctl_alloc_gp_t * ragp)11650Sstevel@tonic-gate rctl_set_fill_alloc_gp(rctl_set_t *set, rctl_alloc_gp_t *ragp)
11660Sstevel@tonic-gate {
11670Sstevel@tonic-gate 	uint_t i;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	bzero(ragp, sizeof (rctl_alloc_gp_t));
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
11720Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		while (r != NULL) {
11750Sstevel@tonic-gate 			ragp->rcag_nctls++;
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 			ragp->rcag_nvals += rctl_val_list_count(r->rc_values);
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			r = r->rc_next;
11800Sstevel@tonic-gate 		}
11810Sstevel@tonic-gate 	}
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate /*
11850Sstevel@tonic-gate  * rctl_alloc_gp_t *rctl_set_dup_prealloc(rctl_set_t *)
11860Sstevel@tonic-gate  *
11870Sstevel@tonic-gate  * Overview
11880Sstevel@tonic-gate  *   Given a resource control set, allocate a sufficiently large allocation
11890Sstevel@tonic-gate  *   group to contain a duplicate of the set.
11900Sstevel@tonic-gate  *
11910Sstevel@tonic-gate  * Return value
11920Sstevel@tonic-gate  *   A pointer to the newly created allocation group.
11930Sstevel@tonic-gate  *
11940Sstevel@tonic-gate  * Caller's context
11950Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
11960Sstevel@tonic-gate  */
11970Sstevel@tonic-gate rctl_alloc_gp_t *
rctl_set_dup_prealloc(rctl_set_t * set)11980Sstevel@tonic-gate rctl_set_dup_prealloc(rctl_set_t *set)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	rctl_alloc_gp_t *ragp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
12050Sstevel@tonic-gate 	rctl_set_fill_alloc_gp(set, ragp);
12060Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	rctl_gp_alloc(ragp);
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	return (ragp);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate /*
12140Sstevel@tonic-gate  * int rctl_set_dup_ready(rctl_set_t *, rctl_alloc_gp_t *)
12150Sstevel@tonic-gate  *
12160Sstevel@tonic-gate  * Overview
12170Sstevel@tonic-gate  *   Verify that the allocation group provided is large enough to allow a
12180Sstevel@tonic-gate  *   duplicate of the given resource control set to be constructed from its
12190Sstevel@tonic-gate  *   contents.
12200Sstevel@tonic-gate  *
12210Sstevel@tonic-gate  * Return values
12220Sstevel@tonic-gate  *   1 if the allocation group is sufficiently large, 0 otherwise.
12230Sstevel@tonic-gate  *
12240Sstevel@tonic-gate  * Caller's context
12250Sstevel@tonic-gate  *   rcs_lock must be held prior to entry.
12260Sstevel@tonic-gate  */
12270Sstevel@tonic-gate int
rctl_set_dup_ready(rctl_set_t * set,rctl_alloc_gp_t * ragp)12280Sstevel@tonic-gate rctl_set_dup_ready(rctl_set_t *set, rctl_alloc_gp_t *ragp)
12290Sstevel@tonic-gate {
12300Sstevel@tonic-gate 	rctl_alloc_gp_t curr_gp;
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	rctl_set_fill_alloc_gp(set, &curr_gp);
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	if (curr_gp.rcag_nctls <= ragp->rcag_nctls &&
12370Sstevel@tonic-gate 	    curr_gp.rcag_nvals <= ragp->rcag_nvals)
12380Sstevel@tonic-gate 		return (1);
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	return (0);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate /*
12440Sstevel@tonic-gate  * rctl_set_t *rctl_set_dup(rctl_set_t *, struct proc *, struct proc *,
12450Sstevel@tonic-gate  *   rctl_set_t *, rctl_alloc_gp_t *, int)
12460Sstevel@tonic-gate  *
12470Sstevel@tonic-gate  * Overview
12480Sstevel@tonic-gate  *   Make a duplicate of the resource control set.  The proc pointers are those
12490Sstevel@tonic-gate  *   of the owning process and of the process associated with the entity
12500Sstevel@tonic-gate  *   receiving the duplicate.
12510Sstevel@tonic-gate  *
12520Sstevel@tonic-gate  *   Duplication is a 3 stage process. Stage 1 is memory allocation for
12530Sstevel@tonic-gate  *   the duplicate set, which is taken care of by rctl_set_dup_prealloc().
12540Sstevel@tonic-gate  *   Stage 2 consists of copying all rctls and values from the old set into
12550Sstevel@tonic-gate  *   the new. Stage 3 completes the duplication by performing the appropriate
12560Sstevel@tonic-gate  *   callbacks for each rctl in the new set.
12570Sstevel@tonic-gate  *
12580Sstevel@tonic-gate  *   Stages 2 and 3 are handled by calling rctl_set_dup with the RCD_DUP and
12590Sstevel@tonic-gate  *   RCD_CALLBACK functions, respectively. The RCD_CALLBACK flag may only
12600Sstevel@tonic-gate  *   be supplied if the newp proc structure reflects the new task and
12610Sstevel@tonic-gate  *   project linkage.
12620Sstevel@tonic-gate  *
12630Sstevel@tonic-gate  * Return value
12640Sstevel@tonic-gate  *   A pointer to the duplicate set.
12650Sstevel@tonic-gate  *
12660Sstevel@tonic-gate  * Caller's context
12670Sstevel@tonic-gate  *   The rcs_lock of the set to be duplicated must be held prior to entry.
12680Sstevel@tonic-gate  */
12690Sstevel@tonic-gate rctl_set_t *
rctl_set_dup(rctl_set_t * set,struct proc * oldp,struct proc * newp,rctl_entity_p_t * e,rctl_set_t * dup,rctl_alloc_gp_t * ragp,int flag)12700Sstevel@tonic-gate rctl_set_dup(rctl_set_t *set, struct proc *oldp, struct proc *newp,
12710Sstevel@tonic-gate     rctl_entity_p_t *e, rctl_set_t *dup, rctl_alloc_gp_t *ragp, int flag)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	uint_t i;
12740Sstevel@tonic-gate 	rctl_set_t	*iter;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	ASSERT((flag & RCD_DUP) || (flag & RCD_CALLBACK));
12770Sstevel@tonic-gate 	ASSERT(e);
12780Sstevel@tonic-gate 	/*
12790Sstevel@tonic-gate 	 * When copying the old set, iterate over that. Otherwise, when
12800Sstevel@tonic-gate 	 * only callbacks have been requested, iterate over the dup set.
12810Sstevel@tonic-gate 	 */
12820Sstevel@tonic-gate 	if (flag & RCD_DUP) {
12830Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&set->rcs_lock));
12840Sstevel@tonic-gate 		iter = set;
12850Sstevel@tonic-gate 		dup->rcs_entity = set->rcs_entity;
12860Sstevel@tonic-gate 	} else {
12870Sstevel@tonic-gate 		iter = dup;
12880Sstevel@tonic-gate 	}
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	mutex_enter(&dup->rcs_lock);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
12930Sstevel@tonic-gate 		rctl_t *r = iter->rcs_ctls[i];
12940Sstevel@tonic-gate 		rctl_t *d;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 		while (r != NULL) {
12970Sstevel@tonic-gate 			if (flag & RCD_DUP) {
12980Sstevel@tonic-gate 				d = rctl_dup(r, ragp, oldp, newp);
12990Sstevel@tonic-gate 				rctl_set_insert(dup, r->rc_id, d);
13000Sstevel@tonic-gate 			} else {
13010Sstevel@tonic-gate 				d = r;
13020Sstevel@tonic-gate 			}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 			if (flag & RCD_CALLBACK)
13050Sstevel@tonic-gate 				RCTLOP_SET(d, newp, e,
13060Sstevel@tonic-gate 				    rctl_model_value(d->rc_dict_entry, newp,
13070Sstevel@tonic-gate 				    d->rc_cursor->rcv_value));
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 			r = r->rc_next;
13100Sstevel@tonic-gate 		}
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	mutex_exit(&dup->rcs_lock);
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	return (dup);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate /*
13190Sstevel@tonic-gate  * void rctl_set_free(rctl_set_t *)
13200Sstevel@tonic-gate  *
13210Sstevel@tonic-gate  * Overview
13220Sstevel@tonic-gate  *   Delete resource control set and all attached values.
13230Sstevel@tonic-gate  *
13240Sstevel@tonic-gate  * Return values
13250Sstevel@tonic-gate  *   No value returned.
13260Sstevel@tonic-gate  *
13270Sstevel@tonic-gate  * Caller's context
13280Sstevel@tonic-gate  *   No restrictions on context.
13290Sstevel@tonic-gate  */
13300Sstevel@tonic-gate void
rctl_set_free(rctl_set_t * set)13310Sstevel@tonic-gate rctl_set_free(rctl_set_t *set)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	uint_t i;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
13360Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
13370Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		while (r != NULL) {
13400Sstevel@tonic-gate 			rctl_val_t *v = r->rc_values;
13410Sstevel@tonic-gate 			rctl_t *n = r->rc_next;
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 			kmem_cache_free(rctl_cache, r);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 			rctl_val_list_free(v);
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 			r = n;
13480Sstevel@tonic-gate 		}
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 	kmem_free(set->rcs_ctls, sizeof (rctl_t *) * rctl_set_size);
13530Sstevel@tonic-gate 	kmem_free(set, sizeof (rctl_set_t));
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate /*
13570Sstevel@tonic-gate  * void rctl_set_reset(rctl_set_t *)
13580Sstevel@tonic-gate  *
13590Sstevel@tonic-gate  * Overview
13600Sstevel@tonic-gate  *   Resets all rctls within the set such that the lowest value becomes active.
13610Sstevel@tonic-gate  *
13620Sstevel@tonic-gate  * Return values
13630Sstevel@tonic-gate  *   No value returned.
13640Sstevel@tonic-gate  *
13650Sstevel@tonic-gate  * Caller's context
13660Sstevel@tonic-gate  *   No restrictions on context.
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate void
rctl_set_reset(rctl_set_t * set,struct proc * p,rctl_entity_p_t * e)13690Sstevel@tonic-gate rctl_set_reset(rctl_set_t *set, struct proc *p, rctl_entity_p_t *e)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	uint_t i;
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 	ASSERT(e);
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
13760Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
13770Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
13780Sstevel@tonic-gate 
13790Sstevel@tonic-gate 		while (r != NULL) {
13800Sstevel@tonic-gate 			r->rc_cursor = r->rc_values;
13810Sstevel@tonic-gate 			rctl_val_list_reset(r->rc_cursor);
13820Sstevel@tonic-gate 			RCTLOP_SET(r, p, e, rctl_model_value(r->rc_dict_entry,
13830Sstevel@tonic-gate 			    p, r->rc_cursor->rcv_value));
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 			ASSERT(r->rc_cursor != NULL);
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 			r = r->rc_next;
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate /*
13950Sstevel@tonic-gate  * void rctl_set_tearoff(rctl_set *, struct proc *)
13960Sstevel@tonic-gate  *
13970Sstevel@tonic-gate  * Overview
13980Sstevel@tonic-gate  *   Tear off any resource control values on this set with an action recipient
13990Sstevel@tonic-gate  *   equal to the specified process (as they are becoming invalid with the
14000Sstevel@tonic-gate  *   process's departure from this set as an observer).
14010Sstevel@tonic-gate  *
14020Sstevel@tonic-gate  * Return values
14030Sstevel@tonic-gate  *   No value returned.
14040Sstevel@tonic-gate  *
14050Sstevel@tonic-gate  * Caller's context
14060Sstevel@tonic-gate  *   No restrictions on context
14070Sstevel@tonic-gate  */
14080Sstevel@tonic-gate void
rctl_set_tearoff(rctl_set_t * set,struct proc * p)14090Sstevel@tonic-gate rctl_set_tearoff(rctl_set_t *set, struct proc *p)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 	uint_t i;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
14140Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
14150Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 		while (r != NULL) {
14180Sstevel@tonic-gate 			rctl_val_t *rval;
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate tearoff_rewalk_list:
14210Sstevel@tonic-gate 			rval = r->rc_values;
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate 			while (rval != NULL) {
14240Sstevel@tonic-gate 				if (rval->rcv_privilege == RCPRIV_BASIC &&
14250Sstevel@tonic-gate 				    rval->rcv_action_recipient == p) {
14260Sstevel@tonic-gate 					if (r->rc_cursor == rval)
14270Sstevel@tonic-gate 						r->rc_cursor = rval->rcv_next;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 					(void) rctl_val_list_delete(
14300Sstevel@tonic-gate 					    &r->rc_values, rval);
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 					goto tearoff_rewalk_list;
14330Sstevel@tonic-gate 				}
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 				rval = rval->rcv_next;
14360Sstevel@tonic-gate 			}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 			ASSERT(r->rc_cursor != NULL);
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 			r = r->rc_next;
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate 
14479640SMenno.Lageman@Sun.COM int
rctl_set_find(rctl_set_t * set,rctl_hndl_t hndl,rctl_t ** rctl)14480Sstevel@tonic-gate rctl_set_find(rctl_set_t *set, rctl_hndl_t hndl, rctl_t **rctl)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	uint_t index = hndl % rctl_set_size;
14510Sstevel@tonic-gate 	rctl_t *curr_ctl;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 	for (curr_ctl = set->rcs_ctls[index]; curr_ctl != NULL;
14560Sstevel@tonic-gate 	    curr_ctl = curr_ctl->rc_next) {
14570Sstevel@tonic-gate 		if (curr_ctl->rc_id == hndl) {
14580Sstevel@tonic-gate 			*rctl = curr_ctl;
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 			return (0);
14610Sstevel@tonic-gate 		}
14620Sstevel@tonic-gate 	}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 	return (-1);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate /*
14680Sstevel@tonic-gate  * rlim64_t rctl_enforced_value(rctl_hndl_t, rctl_set_t *, struct proc *)
14690Sstevel@tonic-gate  *
14700Sstevel@tonic-gate  * Overview
14710Sstevel@tonic-gate  *   Given a process, get the next enforced value on the rctl of the specified
14720Sstevel@tonic-gate  *   handle.
14730Sstevel@tonic-gate  *
14740Sstevel@tonic-gate  * Return value
14750Sstevel@tonic-gate  *   The enforced value.
14760Sstevel@tonic-gate  *
14770Sstevel@tonic-gate  * Caller's context
14780Sstevel@tonic-gate  *   For controls on process collectives, p->p_lock must be held across the
14790Sstevel@tonic-gate  *   operation.
14800Sstevel@tonic-gate  */
14810Sstevel@tonic-gate /*ARGSUSED*/
14820Sstevel@tonic-gate rctl_qty_t
rctl_enforced_value(rctl_hndl_t hndl,rctl_set_t * rset,struct proc * p)14830Sstevel@tonic-gate rctl_enforced_value(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p)
14840Sstevel@tonic-gate {
14850Sstevel@tonic-gate 	rctl_t *rctl;
14860Sstevel@tonic-gate 	rlim64_t ret;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &rctl) == -1)
14910Sstevel@tonic-gate 		panic("unknown resource control handle %d requested", hndl);
14920Sstevel@tonic-gate 	else
14930Sstevel@tonic-gate 		ret = rctl_model_value(rctl->rc_dict_entry, p,
14940Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	return (ret);
14990Sstevel@tonic-gate }
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate /*
15020Sstevel@tonic-gate  * int rctl_global_get(const char *, rctl_dict_entry_t *)
15030Sstevel@tonic-gate  *
15040Sstevel@tonic-gate  * Overview
15050Sstevel@tonic-gate  *   Copy a sanitized version of the global rctl for a given resource control
15060Sstevel@tonic-gate  *   name.  (By sanitization, we mean that the unsafe data pointers have been
15070Sstevel@tonic-gate  *   zeroed.)
15080Sstevel@tonic-gate  *
15090Sstevel@tonic-gate  * Return value
15100Sstevel@tonic-gate  *   -1 if name not defined, 0 otherwise.
15110Sstevel@tonic-gate  *
15120Sstevel@tonic-gate  * Caller's context
15130Sstevel@tonic-gate  *   No restrictions on context.  rctl_dict_lock must not be held.
15140Sstevel@tonic-gate  */
15150Sstevel@tonic-gate int
rctl_global_get(const char * name,rctl_dict_entry_t * drde)15160Sstevel@tonic-gate rctl_global_get(const char *name, rctl_dict_entry_t *drde)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup(name);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	if (rde == NULL)
15210Sstevel@tonic-gate 		return (-1);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	bcopy(rde, drde, sizeof (rctl_dict_entry_t));
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	drde->rcd_next = NULL;
15260Sstevel@tonic-gate 	drde->rcd_ops = NULL;
15270Sstevel@tonic-gate 
15280Sstevel@tonic-gate 	return (0);
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate 
15310Sstevel@tonic-gate /*
15320Sstevel@tonic-gate  * int rctl_global_set(const char *, rctl_dict_entry_t *)
15330Sstevel@tonic-gate  *
15340Sstevel@tonic-gate  * Overview
15350Sstevel@tonic-gate  *   Transfer the settable fields of the named rctl to the global rctl matching
15360Sstevel@tonic-gate  *   the given resource control name.
15370Sstevel@tonic-gate  *
15380Sstevel@tonic-gate  * Return value
15390Sstevel@tonic-gate  *   -1 if name not defined, 0 otherwise.
15400Sstevel@tonic-gate  *
15410Sstevel@tonic-gate  * Caller's context
15420Sstevel@tonic-gate  *   No restrictions on context.  rctl_dict_lock must not be held.
15430Sstevel@tonic-gate  */
15440Sstevel@tonic-gate int
rctl_global_set(const char * name,rctl_dict_entry_t * drde)15450Sstevel@tonic-gate rctl_global_set(const char *name, rctl_dict_entry_t *drde)
15460Sstevel@tonic-gate {
15470Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup(name);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if (rde == NULL)
15500Sstevel@tonic-gate 		return (-1);
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate 	rde->rcd_flagaction = drde->rcd_flagaction;
15530Sstevel@tonic-gate 	rde->rcd_syslog_level = drde->rcd_syslog_level;
15540Sstevel@tonic-gate 	rde->rcd_strlog_flags = drde->rcd_strlog_flags;
15550Sstevel@tonic-gate 
15560Sstevel@tonic-gate 	return (0);
15570Sstevel@tonic-gate }
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate static int
rctl_local_op(rctl_hndl_t hndl,rctl_val_t * oval,rctl_val_t * nval,int (* cbop)(rctl_hndl_t,struct proc * p,rctl_entity_p_t * e,rctl_t *,rctl_val_t *,rctl_val_t *),struct proc * p)15600Sstevel@tonic-gate rctl_local_op(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
15610Sstevel@tonic-gate     int (*cbop)(rctl_hndl_t, struct proc *p, rctl_entity_p_t *e, rctl_t *,
15620Sstevel@tonic-gate     rctl_val_t *, rctl_val_t *), struct proc *p)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	rctl_t *rctl;
15650Sstevel@tonic-gate 	rctl_set_t *rset;
15660Sstevel@tonic-gate 	rctl_entity_p_t e;
15670Sstevel@tonic-gate 	int ret = 0;
15680Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup_hndl(hndl);
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate local_op_retry:
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	rset = rctl_entity_obtain_rset(rde, p);
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	if (rset == NULL) {
15770Sstevel@tonic-gate 		return (-1);
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 	rctl_entity_obtain_entity_p(rset->rcs_entity, p, &e);
15800Sstevel@tonic-gate 
15810Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	/* using rctl's hndl, get rctl from local set */
15840Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &rctl) == -1) {
15850Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
15860Sstevel@tonic-gate 		return (-1);
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	ret = cbop(hndl, p, &e, rctl, oval, nval);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
15920Sstevel@tonic-gate 	return (ret);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*ARGSUSED*/
15960Sstevel@tonic-gate static int
rctl_local_get_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * oval,rctl_val_t * nval)15970Sstevel@tonic-gate rctl_local_get_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
15980Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
15990Sstevel@tonic-gate {
16000Sstevel@tonic-gate 	if (oval == NULL) {
16010Sstevel@tonic-gate 		/*
16020Sstevel@tonic-gate 		 * RCTL_FIRST
16030Sstevel@tonic-gate 		 */
16040Sstevel@tonic-gate 		bcopy(rctl->rc_values, nval, sizeof (rctl_val_t));
16050Sstevel@tonic-gate 	} else {
16060Sstevel@tonic-gate 		/*
16070Sstevel@tonic-gate 		 * RCTL_NEXT
16080Sstevel@tonic-gate 		 */
16090Sstevel@tonic-gate 		rctl_val_t *tval = rctl_val_list_find(&rctl->rc_values, oval);
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 		if (tval == NULL)
16120Sstevel@tonic-gate 			return (ESRCH);
16130Sstevel@tonic-gate 		else if (tval->rcv_next == NULL)
16140Sstevel@tonic-gate 			return (ENOENT);
16150Sstevel@tonic-gate 		else
16160Sstevel@tonic-gate 			bcopy(tval->rcv_next, nval, sizeof (rctl_val_t));
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	return (0);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate /*
16230Sstevel@tonic-gate  * int rctl_local_get(rctl_hndl_t, rctl_val_t *)
16240Sstevel@tonic-gate  *
16250Sstevel@tonic-gate  * Overview
16260Sstevel@tonic-gate  *   Get the rctl value for the given flags.
16270Sstevel@tonic-gate  *
16280Sstevel@tonic-gate  * Return values
16290Sstevel@tonic-gate  *   0 for successful get, errno otherwise.
16300Sstevel@tonic-gate  */
16310Sstevel@tonic-gate int
rctl_local_get(rctl_hndl_t hndl,rctl_val_t * oval,rctl_val_t * nval,struct proc * p)16320Sstevel@tonic-gate rctl_local_get(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
16330Sstevel@tonic-gate     struct proc *p)
16340Sstevel@tonic-gate {
16350Sstevel@tonic-gate 	return (rctl_local_op(hndl, oval, nval, rctl_local_get_cb, p));
16360Sstevel@tonic-gate }
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate /*ARGSUSED*/
16390Sstevel@tonic-gate static int
rctl_local_delete_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * oval,rctl_val_t * nval)16400Sstevel@tonic-gate rctl_local_delete_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
16410Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
16420Sstevel@tonic-gate {
16430Sstevel@tonic-gate 	if ((oval = rctl_val_list_find(&rctl->rc_values, nval)) == NULL)
16440Sstevel@tonic-gate 		return (ESRCH);
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	if (rctl->rc_cursor == oval) {
16470Sstevel@tonic-gate 		rctl->rc_cursor = oval->rcv_next;
16480Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
16490Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
16500Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
16530Sstevel@tonic-gate 	}
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	(void) rctl_val_list_delete(&rctl->rc_values, oval);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	return (0);
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate /*
16610Sstevel@tonic-gate  * int rctl_local_delete(rctl_hndl_t, rctl_val_t *)
16620Sstevel@tonic-gate  *
16630Sstevel@tonic-gate  * Overview
16640Sstevel@tonic-gate  *   Delete the rctl value for the given flags.
16650Sstevel@tonic-gate  *
16660Sstevel@tonic-gate  * Return values
16670Sstevel@tonic-gate  *   0 for successful delete, errno otherwise.
16680Sstevel@tonic-gate  */
16690Sstevel@tonic-gate int
rctl_local_delete(rctl_hndl_t hndl,rctl_val_t * val,struct proc * p)16700Sstevel@tonic-gate rctl_local_delete(rctl_hndl_t hndl, rctl_val_t *val, struct proc *p)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	return (rctl_local_op(hndl, NULL, val, rctl_local_delete_cb, p));
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate /*
16760Sstevel@tonic-gate  * rctl_local_insert_cb()
16770Sstevel@tonic-gate  *
16780Sstevel@tonic-gate  * Overview
16790Sstevel@tonic-gate  *   Insert a new value into the rctl's val list. If an error occurs,
16800Sstevel@tonic-gate  *   the val list must be left in the same state as when the function
16810Sstevel@tonic-gate  *   was entered.
16820Sstevel@tonic-gate  *
16830Sstevel@tonic-gate  * Return Values
16840Sstevel@tonic-gate  *   0 for successful insert, EINVAL if the value is duplicated in the
16850Sstevel@tonic-gate  *   existing list.
16860Sstevel@tonic-gate  */
16870Sstevel@tonic-gate /*ARGSUSED*/
16880Sstevel@tonic-gate static int
rctl_local_insert_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * oval,rctl_val_t * nval)16890Sstevel@tonic-gate rctl_local_insert_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
16900Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	/*
16930Sstevel@tonic-gate 	 * Before inserting, confirm there are no duplicates of this value
16940Sstevel@tonic-gate 	 * and flag level. If there is a duplicate, flag an error and do
16950Sstevel@tonic-gate 	 * nothing.
16960Sstevel@tonic-gate 	 */
16970Sstevel@tonic-gate 	if (rctl_val_list_insert(&rctl->rc_values, nval) != 0)
16980Sstevel@tonic-gate 		return (EINVAL);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 	if (rctl_val_cmp(nval, rctl->rc_cursor, 0) < 0) {
17010Sstevel@tonic-gate 		rctl->rc_cursor = nval;
17020Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
17030Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
17040Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	return (0);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate /*
17130Sstevel@tonic-gate  * int rctl_local_insert(rctl_hndl_t, rctl_val_t *)
17140Sstevel@tonic-gate  *
17150Sstevel@tonic-gate  * Overview
17160Sstevel@tonic-gate  *   Insert the rctl value into the appropriate rctl set for the calling
17170Sstevel@tonic-gate  *   process, given the handle.
17180Sstevel@tonic-gate  */
17190Sstevel@tonic-gate int
rctl_local_insert(rctl_hndl_t hndl,rctl_val_t * val,struct proc * p)17200Sstevel@tonic-gate rctl_local_insert(rctl_hndl_t hndl, rctl_val_t *val, struct proc *p)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	return (rctl_local_op(hndl, NULL, val, rctl_local_insert_cb, p));
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate 
17253684Srd117015 /*
17263684Srd117015  * rctl_local_insert_all_cb()
17273684Srd117015  *
17283684Srd117015  * Overview
17293684Srd117015  *   Called for RCENTITY_PROJECT rctls only, via rctlsys_projset().
17303684Srd117015  *
17313684Srd117015  *   Inserts new values from the project database (new_values).  alloc_values
17323684Srd117015  *   should be a linked list of pre-allocated rctl_val_t, which are used to
17333684Srd117015  *   populate (rc_projdb).
17343684Srd117015  *
17353684Srd117015  *   Should the *new_values linked list match the contents of the rctl's
17363684Srd117015  *   rp_projdb then we do nothing.
17373684Srd117015  *
17383684Srd117015  * Return Values
17393684Srd117015  *   0 is always returned.
17403684Srd117015  */
17413684Srd117015 /*ARGSUSED*/
17423684Srd117015 static int
rctl_local_insert_all_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * new_values,rctl_val_t * alloc_values)17433684Srd117015 rctl_local_insert_all_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
17443684Srd117015     rctl_t *rctl, rctl_val_t *new_values, rctl_val_t *alloc_values)
17453684Srd117015 {
17463684Srd117015 	rctl_val_t *val;
17473684Srd117015 	rctl_val_t *tmp_val;
17483684Srd117015 	rctl_val_t *next;
17493684Srd117015 	int modified = 0;
17503684Srd117015 
17513684Srd117015 	/*
17523684Srd117015 	 * If this the first time we've set this project rctl, then we delete
17533684Srd117015 	 * all the privilege values.  These privilege values have been set by
17543684Srd117015 	 * rctl_add_default_limit().
17553684Srd117015 	 *
17563684Srd117015 	 * We save some cycles here by not calling rctl_val_list_delete().
17573684Srd117015 	 */
17583684Srd117015 	if (rctl->rc_projdb == NULL) {
17593684Srd117015 		val = rctl->rc_values;
17603684Srd117015 
17613684Srd117015 		while (val != NULL) {
17623684Srd117015 			if (val->rcv_privilege == RCPRIV_PRIVILEGED) {
17633684Srd117015 				if (val->rcv_prev != NULL)
17643684Srd117015 					val->rcv_prev->rcv_next = val->rcv_next;
17653684Srd117015 				else
17663684Srd117015 					rctl->rc_values = val->rcv_next;
17673684Srd117015 
17683684Srd117015 				if (val->rcv_next != NULL)
17693684Srd117015 					val->rcv_next->rcv_prev = val->rcv_prev;
17703684Srd117015 
17713684Srd117015 				tmp_val = val;
17723684Srd117015 				val = val->rcv_next;
17733684Srd117015 				kmem_cache_free(rctl_val_cache, tmp_val);
17743684Srd117015 			} else {
17753684Srd117015 				val = val->rcv_next;
17763684Srd117015 			}
17773684Srd117015 		}
17783684Srd117015 		modified = 1;
17793684Srd117015 	}
17803684Srd117015 
17813684Srd117015 	/*
17823684Srd117015 	 * Delete active values previously set through the project database.
17833684Srd117015 	 */
17843684Srd117015 	val = rctl->rc_projdb;
17853684Srd117015 
17863684Srd117015 	while (val != NULL) {
17873684Srd117015 
17883684Srd117015 		/* Is the old value found in the new values? */
17893684Srd117015 		if (rctl_val_list_find(&new_values, val) == NULL) {
17903684Srd117015 
17913684Srd117015 			/*
17923684Srd117015 			 * Delete from the active values if it originated from
17933684Srd117015 			 * the project database.
17943684Srd117015 			 */
17953684Srd117015 			if (((tmp_val = rctl_val_list_find(&rctl->rc_values,
17963684Srd117015 			    val)) != NULL) &&
17973684Srd117015 			    (tmp_val->rcv_flagaction & RCTL_LOCAL_PROJDB)) {
17983684Srd117015 				(void) rctl_val_list_delete(&rctl->rc_values,
17993684Srd117015 				    tmp_val);
18003684Srd117015 			}
18013684Srd117015 
18023684Srd117015 			tmp_val = val->rcv_next;
18033684Srd117015 			(void) rctl_val_list_delete(&rctl->rc_projdb, val);
18043684Srd117015 			val = tmp_val;
18053684Srd117015 			modified = 1;
18063684Srd117015 
18073684Srd117015 		} else
18083684Srd117015 			val = val->rcv_next;
18093684Srd117015 	}
18103684Srd117015 
18113684Srd117015 	/*
18123684Srd117015 	 * Insert new values from the project database.
18133684Srd117015 	 */
18143684Srd117015 	while (new_values != NULL) {
18153684Srd117015 		next = new_values->rcv_next;
18163684Srd117015 
18173684Srd117015 		/*
18183684Srd117015 		 * Insert this new value into the rc_projdb, and duplicate this
18193684Srd117015 		 * entry to the active list.
18203684Srd117015 		 */
18213684Srd117015 		if (rctl_val_list_insert(&rctl->rc_projdb, new_values) == 0) {
18223684Srd117015 
18233684Srd117015 			tmp_val = alloc_values->rcv_next;
18243684Srd117015 			bcopy(new_values, alloc_values, sizeof (rctl_val_t));
18253684Srd117015 			alloc_values->rcv_next = tmp_val;
18263684Srd117015 
18273684Srd117015 			if (rctl_val_list_insert(&rctl->rc_values,
18287240Srh87107 			    alloc_values) == 0) {
18293684Srd117015 				/* inserted move alloc_values on */
18303684Srd117015 				alloc_values = tmp_val;
18313684Srd117015 				modified = 1;
18323684Srd117015 			}
18333684Srd117015 		} else {
18343684Srd117015 			/*
18353684Srd117015 			 * Unlike setrctl() we don't want to return an error on
18363684Srd117015 			 * a duplicate entry; we are concerned solely with
18373684Srd117015 			 * ensuring that all the values specified are set.
18383684Srd117015 			 */
18393684Srd117015 			kmem_cache_free(rctl_val_cache, new_values);
18403684Srd117015 		}
18413684Srd117015 		new_values = next;
18423684Srd117015 	}
18433684Srd117015 
18443684Srd117015 	/* Teardown any unused rctl_val_t */
18453684Srd117015 	while (alloc_values != NULL) {
18463684Srd117015 		tmp_val = alloc_values;
18473684Srd117015 		alloc_values = alloc_values->rcv_next;
18483684Srd117015 		kmem_cache_free(rctl_val_cache, tmp_val);
18493684Srd117015 	}
18503684Srd117015 
18513684Srd117015 	/* Reset the cursor if rctl values have been modified */
18523684Srd117015 	if (modified) {
18533684Srd117015 		rctl->rc_cursor = rctl->rc_values;
18543684Srd117015 		rctl_val_list_reset(rctl->rc_cursor);
18553684Srd117015 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
18563684Srd117015 		    rctl->rc_cursor->rcv_value));
18573684Srd117015 	}
18583684Srd117015 
18593684Srd117015 	return (0);
18603684Srd117015 }
18613684Srd117015 
18623684Srd117015 int
rctl_local_insert_all(rctl_hndl_t hndl,rctl_val_t * new_values,rctl_val_t * alloc_values,struct proc * p)18633684Srd117015 rctl_local_insert_all(rctl_hndl_t hndl, rctl_val_t *new_values,
18643684Srd117015     rctl_val_t *alloc_values, struct proc *p)
18653684Srd117015 {
18663684Srd117015 	return (rctl_local_op(hndl, new_values, alloc_values,
18673684Srd117015 	    rctl_local_insert_all_cb, p));
18683684Srd117015 }
18693684Srd117015 
18703684Srd117015 /*
18713684Srd117015  * rctl_local_replace_all_cb()
18723684Srd117015  *
18733684Srd117015  * Overview
18743684Srd117015  *   Called for RCENTITY_PROJECT rctls only, via rctlsys_projset().
18753684Srd117015  *
18763684Srd117015  *   Clears the active rctl values (rc_values), and stored values from the
18773684Srd117015  *   previous insertions from the project database (rc_projdb).
18783684Srd117015  *
18793684Srd117015  *   Inserts new values from the project database (new_values).  alloc_values
18803684Srd117015  *   should be a linked list of pre-allocated rctl_val_t, which are used to
18813684Srd117015  *   populate (rc_projdb).
18823684Srd117015  *
18833684Srd117015  * Return Values
18843684Srd117015  *   0 is always returned.
18853684Srd117015  */
18863684Srd117015 /*ARGSUSED*/
18873684Srd117015 static int
rctl_local_replace_all_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * new_values,rctl_val_t * alloc_values)18883684Srd117015 rctl_local_replace_all_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
18893684Srd117015     rctl_t *rctl, rctl_val_t *new_values, rctl_val_t *alloc_values)
18903684Srd117015 {
18913684Srd117015 	rctl_val_t *val;
18923684Srd117015 	rctl_val_t *next;
18933684Srd117015 	rctl_val_t *tmp_val;
18943684Srd117015 
18953684Srd117015 	/* Delete all the privilege vaules */
18963684Srd117015 	val = rctl->rc_values;
18973684Srd117015 
18983684Srd117015 	while (val != NULL) {
18993684Srd117015 		if (val->rcv_privilege == RCPRIV_PRIVILEGED) {
19003684Srd117015 			if (val->rcv_prev != NULL)
19013684Srd117015 				val->rcv_prev->rcv_next = val->rcv_next;
19023684Srd117015 			else
19033684Srd117015 				rctl->rc_values = val->rcv_next;
19043684Srd117015 
19053684Srd117015 			if (val->rcv_next != NULL)
19063684Srd117015 				val->rcv_next->rcv_prev = val->rcv_prev;
19073684Srd117015 
19083684Srd117015 			tmp_val = val;
19093684Srd117015 			val = val->rcv_next;
19103684Srd117015 			kmem_cache_free(rctl_val_cache, tmp_val);
19113684Srd117015 		} else {
19123684Srd117015 			val = val->rcv_next;
19133684Srd117015 		}
19143684Srd117015 	}
19153684Srd117015 
19163684Srd117015 	/* Delete the contents of rc_projdb */
19173684Srd117015 	val = rctl->rc_projdb;
19183684Srd117015 	while (val != NULL) {
19193684Srd117015 
19203684Srd117015 		tmp_val = val;
19213684Srd117015 		val = val->rcv_next;
19223684Srd117015 		kmem_cache_free(rctl_val_cache, tmp_val);
19233684Srd117015 	}
19243684Srd117015 	rctl->rc_projdb = NULL;
19253684Srd117015 
19263684Srd117015 	/*
19273684Srd117015 	 * Insert new values from the project database.
19283684Srd117015 	 */
19293684Srd117015 	while (new_values != NULL) {
19303684Srd117015 		next = new_values->rcv_next;
19313684Srd117015 
19323684Srd117015 		if (rctl_val_list_insert(&rctl->rc_projdb, new_values) == 0) {
19333684Srd117015 			tmp_val = alloc_values->rcv_next;
19343684Srd117015 			bcopy(new_values, alloc_values, sizeof (rctl_val_t));
19353684Srd117015 			alloc_values->rcv_next = tmp_val;
19363684Srd117015 
19373684Srd117015 			if (rctl_val_list_insert(&rctl->rc_values,
19387240Srh87107 			    alloc_values) == 0) {
19393684Srd117015 				/* inserted, so move alloc_values on */
19403684Srd117015 				alloc_values = tmp_val;
19413684Srd117015 			}
19423684Srd117015 		} else {
19433684Srd117015 			/*
19443684Srd117015 			 * Unlike setrctl() we don't want to return an error on
19453684Srd117015 			 * a duplicate entry; we are concerned solely with
19463684Srd117015 			 * ensuring that all the values specified are set.
19473684Srd117015 			 */
19483684Srd117015 			kmem_cache_free(rctl_val_cache, new_values);
19493684Srd117015 		}
19503684Srd117015 
19513684Srd117015 		new_values = next;
19523684Srd117015 	}
19533684Srd117015 
19543684Srd117015 	/* Teardown any unused rctl_val_t */
19553684Srd117015 	while (alloc_values != NULL) {
19563684Srd117015 		tmp_val = alloc_values;
19573684Srd117015 		alloc_values = alloc_values->rcv_next;
19583684Srd117015 		kmem_cache_free(rctl_val_cache, tmp_val);
19593684Srd117015 	}
19603684Srd117015 
19613684Srd117015 	/* Always reset the cursor */
19623684Srd117015 	rctl->rc_cursor = rctl->rc_values;
19633684Srd117015 	rctl_val_list_reset(rctl->rc_cursor);
19643684Srd117015 	RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
19653684Srd117015 	    rctl->rc_cursor->rcv_value));
19663684Srd117015 
19673684Srd117015 	return (0);
19683684Srd117015 }
19693684Srd117015 
19703684Srd117015 int
rctl_local_replace_all(rctl_hndl_t hndl,rctl_val_t * new_values,rctl_val_t * alloc_values,struct proc * p)19713684Srd117015 rctl_local_replace_all(rctl_hndl_t hndl, rctl_val_t *new_values,
19723684Srd117015     rctl_val_t *alloc_values, struct proc *p)
19733684Srd117015 {
19743684Srd117015 	return (rctl_local_op(hndl, new_values, alloc_values,
19753684Srd117015 	    rctl_local_replace_all_cb, p));
19763684Srd117015 }
19773684Srd117015 
19780Sstevel@tonic-gate static int
rctl_local_replace_cb(rctl_hndl_t hndl,struct proc * p,rctl_entity_p_t * e,rctl_t * rctl,rctl_val_t * oval,rctl_val_t * nval)19790Sstevel@tonic-gate rctl_local_replace_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
19800Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
19810Sstevel@tonic-gate {
19820Sstevel@tonic-gate 	int ret;
19833251Ssl108498 	rctl_val_t *tmp;
19843251Ssl108498 
19853251Ssl108498 	/* Verify that old will be delete-able */
19863251Ssl108498 	tmp = rctl_val_list_find(&rctl->rc_values, oval);
19873251Ssl108498 	if (tmp == NULL)
19883251Ssl108498 		return (ESRCH);
19893251Ssl108498 	/*
19903251Ssl108498 	 * Caller should verify that value being deleted is not the
19913251Ssl108498 	 * system value.
19923251Ssl108498 	 */
19933251Ssl108498 	ASSERT(tmp->rcv_privilege != RCPRIV_SYSTEM);
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/*
19960Sstevel@tonic-gate 	 * rctl_local_insert_cb() does the job of flagging an error
19970Sstevel@tonic-gate 	 * for any duplicate values. So, call rctl_local_insert_cb()
19980Sstevel@tonic-gate 	 * for the new value first, then do deletion of the old value.
19990Sstevel@tonic-gate 	 * Since this is a callback function to rctl_local_op, we can
20000Sstevel@tonic-gate 	 * count on rcs_lock being held at this point. This guarantees
20010Sstevel@tonic-gate 	 * that there is at no point a visible list which contains both
20020Sstevel@tonic-gate 	 * new and old values.
20030Sstevel@tonic-gate 	 */
20040Sstevel@tonic-gate 	if (ret = rctl_local_insert_cb(hndl, p, e, rctl, NULL, nval))
20050Sstevel@tonic-gate 		return (ret);
20060Sstevel@tonic-gate 
20073251Ssl108498 	ret = rctl_local_delete_cb(hndl, p, e, rctl, NULL, oval);
20083251Ssl108498 	ASSERT(ret == 0);
20093251Ssl108498 	return (0);
20100Sstevel@tonic-gate }
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate /*
20130Sstevel@tonic-gate  * int rctl_local_replace(rctl_hndl_t, void *, int, uint64_t *)
20140Sstevel@tonic-gate  *
20150Sstevel@tonic-gate  * Overview
20160Sstevel@tonic-gate  *   Replace the rctl value with a new one.
20170Sstevel@tonic-gate  *
20180Sstevel@tonic-gate  * Return values
20190Sstevel@tonic-gate  *   0 for successful replace, errno otherwise.
20200Sstevel@tonic-gate  */
20210Sstevel@tonic-gate int
rctl_local_replace(rctl_hndl_t hndl,rctl_val_t * oval,rctl_val_t * nval,struct proc * p)20220Sstevel@tonic-gate rctl_local_replace(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
20230Sstevel@tonic-gate     struct proc *p)
20240Sstevel@tonic-gate {
20250Sstevel@tonic-gate 	return (rctl_local_op(hndl, oval, nval, rctl_local_replace_cb, p));
20260Sstevel@tonic-gate }
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate /*
20290Sstevel@tonic-gate  * int rctl_rlimit_get(rctl_hndl_t, struct proc *, struct rlimit64 *)
20300Sstevel@tonic-gate  *
20310Sstevel@tonic-gate  * Overview
20320Sstevel@tonic-gate  *   To support rlimit compatibility, we need a function which takes a 64-bit
20330Sstevel@tonic-gate  *   rlimit and encodes it as appropriate rcontrol values on the given rcontrol.
20340Sstevel@tonic-gate  *   This operation is only intended for legacy rlimits.
20350Sstevel@tonic-gate  */
20360Sstevel@tonic-gate int
rctl_rlimit_get(rctl_hndl_t rc,struct proc * p,struct rlimit64 * rlp64)20370Sstevel@tonic-gate rctl_rlimit_get(rctl_hndl_t rc, struct proc *p, struct rlimit64 *rlp64)
20380Sstevel@tonic-gate {
20390Sstevel@tonic-gate 	rctl_t *rctl;
20400Sstevel@tonic-gate 	rctl_val_t *rval;
20410Sstevel@tonic-gate 	rctl_set_t *rset = p->p_rctls;
20420Sstevel@tonic-gate 	int soft_limit_seen = 0;
20430Sstevel@tonic-gate 	int test_for_deny = 1;
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
20460Sstevel@tonic-gate 	if (rctl_set_find(rset, rc, &rctl) == -1) {
20470Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
20480Sstevel@tonic-gate 		return (-1);
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	rval = rctl->rc_values;
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	if (rctl->rc_dict_entry->rcd_flagaction & (RCTL_GLOBAL_DENY_NEVER |
20540Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_ALWAYS))
20550Sstevel@tonic-gate 		test_for_deny = 0;
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate 	/*
20580Sstevel@tonic-gate 	 * 1.  Find the first control value with the RCTL_LOCAL_DENY bit set.
20590Sstevel@tonic-gate 	 */
20600Sstevel@tonic-gate 	while (rval != NULL && rval->rcv_privilege != RCPRIV_SYSTEM) {
20610Sstevel@tonic-gate 		if (test_for_deny &&
20620Sstevel@tonic-gate 		    (rval->rcv_flagaction & RCTL_LOCAL_DENY) == 0) {
20630Sstevel@tonic-gate 			rval = rval->rcv_next;
20640Sstevel@tonic-gate 			continue;
20650Sstevel@tonic-gate 		}
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 		/*
20680Sstevel@tonic-gate 		 * 2.  If this is an RCPRIV_BASIC value, then we've found the
20690Sstevel@tonic-gate 		 * effective soft limit and should set rlim_cur.  We should then
20700Sstevel@tonic-gate 		 * continue looking for another control value with the DENY bit
20710Sstevel@tonic-gate 		 * set.
20720Sstevel@tonic-gate 		 */
20730Sstevel@tonic-gate 		if (rval->rcv_privilege == RCPRIV_BASIC) {
20740Sstevel@tonic-gate 			if (soft_limit_seen) {
20750Sstevel@tonic-gate 				rval = rval->rcv_next;
20760Sstevel@tonic-gate 				continue;
20770Sstevel@tonic-gate 			}
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 			if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
20800Sstevel@tonic-gate 			    rval->rcv_value < rctl_model_maximum(
20810Sstevel@tonic-gate 			    rctl->rc_dict_entry, p))
20820Sstevel@tonic-gate 				rlp64->rlim_cur = rval->rcv_value;
20830Sstevel@tonic-gate 			else
20840Sstevel@tonic-gate 				rlp64->rlim_cur = RLIM64_INFINITY;
20850Sstevel@tonic-gate 			soft_limit_seen = 1;
20860Sstevel@tonic-gate 
20870Sstevel@tonic-gate 			rval = rval->rcv_next;
20880Sstevel@tonic-gate 			continue;
20890Sstevel@tonic-gate 		}
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 		/*
20920Sstevel@tonic-gate 		 * 3.  This is an RCPRIV_PRIVILEGED value.  If we haven't found
20930Sstevel@tonic-gate 		 * a soft limit candidate, then we've found the effective hard
20940Sstevel@tonic-gate 		 * and soft limits and should set both  If we had found a soft
20950Sstevel@tonic-gate 		 * limit, then this is only the hard limit and we need only set
20960Sstevel@tonic-gate 		 * rlim_max.
20970Sstevel@tonic-gate 		 */
20980Sstevel@tonic-gate 		if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
20990Sstevel@tonic-gate 		    rval->rcv_value < rctl_model_maximum(rctl->rc_dict_entry,
21000Sstevel@tonic-gate 		    p))
21010Sstevel@tonic-gate 			rlp64->rlim_max = rval->rcv_value;
21020Sstevel@tonic-gate 		else
21030Sstevel@tonic-gate 			rlp64->rlim_max = RLIM64_INFINITY;
21040Sstevel@tonic-gate 		if (!soft_limit_seen)
21050Sstevel@tonic-gate 			rlp64->rlim_cur = rlp64->rlim_max;
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
21080Sstevel@tonic-gate 		return (0);
21090Sstevel@tonic-gate 	}
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	if (rval == NULL) {
21120Sstevel@tonic-gate 		/*
21130Sstevel@tonic-gate 		 * This control sequence is corrupt, as it is not terminated by
21140Sstevel@tonic-gate 		 * a system privileged control value.
21150Sstevel@tonic-gate 		 */
21160Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
21170Sstevel@tonic-gate 		return (-1);
21180Sstevel@tonic-gate 	}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 	/*
21210Sstevel@tonic-gate 	 * 4.  If we run into a RCPRIV_SYSTEM value, then the hard limit (and
21220Sstevel@tonic-gate 	 * the soft, if we haven't a soft candidate) should be the value of the
21230Sstevel@tonic-gate 	 * system control value.
21240Sstevel@tonic-gate 	 */
21250Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
21260Sstevel@tonic-gate 	    rval->rcv_value < rctl_model_maximum(rctl->rc_dict_entry, p))
21270Sstevel@tonic-gate 		rlp64->rlim_max = rval->rcv_value;
21280Sstevel@tonic-gate 	else
21290Sstevel@tonic-gate 		rlp64->rlim_max = RLIM64_INFINITY;
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	if (!soft_limit_seen)
21320Sstevel@tonic-gate 		rlp64->rlim_cur = rlp64->rlim_max;
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
21350Sstevel@tonic-gate 	return (0);
21360Sstevel@tonic-gate }
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate /*
21390Sstevel@tonic-gate  * rctl_alloc_gp_t *rctl_rlimit_set_prealloc(uint_t)
21400Sstevel@tonic-gate  *
21410Sstevel@tonic-gate  * Overview
21420Sstevel@tonic-gate  *   Before making a series of calls to rctl_rlimit_set(), we must have a
21430Sstevel@tonic-gate  *   preallocated batch of resource control values, as rctl_rlimit_set() can
21440Sstevel@tonic-gate  *   potentially consume two resource control values per call.
21450Sstevel@tonic-gate  *
21460Sstevel@tonic-gate  * Return values
21470Sstevel@tonic-gate  *   A populated resource control allocation group with 2n resource control
21480Sstevel@tonic-gate  *   values.
21490Sstevel@tonic-gate  *
21500Sstevel@tonic-gate  * Caller's context
21510Sstevel@tonic-gate  *   Must be safe for KM_SLEEP allocations.
21520Sstevel@tonic-gate  */
21530Sstevel@tonic-gate rctl_alloc_gp_t *
rctl_rlimit_set_prealloc(uint_t n)21540Sstevel@tonic-gate rctl_rlimit_set_prealloc(uint_t n)
21550Sstevel@tonic-gate {
21560Sstevel@tonic-gate 	rctl_alloc_gp_t *gp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	gp->rcag_nvals = 2 * n;
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate 	rctl_gp_alloc(gp);
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	return (gp);
21650Sstevel@tonic-gate }
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate /*
21680Sstevel@tonic-gate  * int rctl_rlimit_set(rctl_hndl_t, struct proc *, struct rlimit64 *, int,
21690Sstevel@tonic-gate  *   int)
21700Sstevel@tonic-gate  *
21710Sstevel@tonic-gate  * Overview
21720Sstevel@tonic-gate  *   To support rlimit compatibility, we need a function which takes a 64-bit
21730Sstevel@tonic-gate  *   rlimit and encodes it as appropriate rcontrol values on the given rcontrol.
21740Sstevel@tonic-gate  *   This operation is only intended for legacy rlimits.
21750Sstevel@tonic-gate  *
21760Sstevel@tonic-gate  *   The implementation of rctl_rlimit_set() is a bit clever, as it tries to
21770Sstevel@tonic-gate  *   minimize the number of values placed on the value sequence in various
21780Sstevel@tonic-gate  *   cases.  Furthermore, we don't allow multiple identical privilege-action
21790Sstevel@tonic-gate  *   values on the same sequence.  (That is, we don't want a sequence like
21800Sstevel@tonic-gate  *   "while (1) { rlim.rlim_cur++; setrlimit(..., rlim); }" to exhaust kernel
21810Sstevel@tonic-gate  *   memory.)  So we want to delete any values with the same privilege value and
21820Sstevel@tonic-gate  *   action.
21830Sstevel@tonic-gate  *
21840Sstevel@tonic-gate  * Return values
21850Sstevel@tonic-gate  *   0 for successful set, errno otherwise. Errno will be either EINVAL
21860Sstevel@tonic-gate  *   or EPERM, in keeping with defined errnos for ulimit() and setrlimit()
21870Sstevel@tonic-gate  *   system calls.
21880Sstevel@tonic-gate  */
21890Sstevel@tonic-gate /*ARGSUSED*/
21900Sstevel@tonic-gate int
rctl_rlimit_set(rctl_hndl_t rc,struct proc * p,struct rlimit64 * rlp64,rctl_alloc_gp_t * ragp,int flagaction,int signal,const cred_t * cr)21910Sstevel@tonic-gate rctl_rlimit_set(rctl_hndl_t rc, struct proc *p, struct rlimit64 *rlp64,
21920Sstevel@tonic-gate     rctl_alloc_gp_t *ragp, int flagaction, int signal, const cred_t *cr)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate 	rctl_t *rctl;
21950Sstevel@tonic-gate 	rctl_val_t *rval, *rval_priv, *rval_basic;
21960Sstevel@tonic-gate 	rctl_set_t *rset = p->p_rctls;
21970Sstevel@tonic-gate 	rctl_qty_t max;
21980Sstevel@tonic-gate 	rctl_entity_p_t e;
21990Sstevel@tonic-gate 	struct rlimit64 cur_rl;
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
22020Sstevel@tonic-gate 	e.rcep_p.proc = p;
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate 	if (rlp64->rlim_cur > rlp64->rlim_max)
22050Sstevel@tonic-gate 		return (EINVAL);
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate 	if (rctl_rlimit_get(rc, p, &cur_rl) == -1)
22080Sstevel@tonic-gate 		return (EINVAL);
22090Sstevel@tonic-gate 
22100Sstevel@tonic-gate 	/*
22110Sstevel@tonic-gate 	 * If we are not privileged, we can only lower the hard limit.
22120Sstevel@tonic-gate 	 */
22130Sstevel@tonic-gate 	if ((rlp64->rlim_max > cur_rl.rlim_max) &&
22140Sstevel@tonic-gate 	    cur_rl.rlim_max != RLIM64_INFINITY &&
22150Sstevel@tonic-gate 	    secpolicy_resource(cr) != 0)
22160Sstevel@tonic-gate 		return (EPERM);
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	if (rctl_set_find(rset, rc, &rctl) == -1) {
22210Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
22220Sstevel@tonic-gate 		return (EINVAL);
22230Sstevel@tonic-gate 	}
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	rval_priv = rctl_gp_detach_val(ragp);
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate 	rval = rctl->rc_values;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	while (rval != NULL) {
22300Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
22310Sstevel@tonic-gate 
22320Sstevel@tonic-gate 		if (rval->rcv_privilege == RCPRIV_SYSTEM)
22330Sstevel@tonic-gate 			break;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 		if ((rval->rcv_privilege == RCPRIV_BASIC) ||
22360Sstevel@tonic-gate 		    (rval->rcv_flagaction & ~RCTL_LOCAL_ACTION_MASK) ==
22370Sstevel@tonic-gate 		    (flagaction & ~RCTL_LOCAL_ACTION_MASK)) {
22380Sstevel@tonic-gate 			if (rctl->rc_cursor == rval) {
22390Sstevel@tonic-gate 				rctl->rc_cursor = rval->rcv_next;
22400Sstevel@tonic-gate 				rctl_val_list_reset(rctl->rc_cursor);
22410Sstevel@tonic-gate 				RCTLOP_SET(rctl, p, &e, rctl_model_value(
22420Sstevel@tonic-gate 				    rctl->rc_dict_entry, p,
22430Sstevel@tonic-gate 				    rctl->rc_cursor->rcv_value));
22440Sstevel@tonic-gate 			}
22450Sstevel@tonic-gate 			(void) rctl_val_list_delete(&rctl->rc_values, rval);
22460Sstevel@tonic-gate 		}
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate 		rval = next;
22490Sstevel@tonic-gate 	}
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 	rval_priv->rcv_privilege = RCPRIV_PRIVILEGED;
22520Sstevel@tonic-gate 	rval_priv->rcv_flagaction = flagaction;
22530Sstevel@tonic-gate 	if (rlp64->rlim_max == RLIM64_INFINITY) {
22540Sstevel@tonic-gate 		rval_priv->rcv_flagaction |= RCTL_LOCAL_MAXIMAL;
22550Sstevel@tonic-gate 		max = rctl->rc_dict_entry->rcd_max_native;
22560Sstevel@tonic-gate 	} else {
22570Sstevel@tonic-gate 		max = rlp64->rlim_max;
22580Sstevel@tonic-gate 	}
22590Sstevel@tonic-gate 	rval_priv->rcv_value = max;
22600Sstevel@tonic-gate 	rval_priv->rcv_action_signal = signal;
22610Sstevel@tonic-gate 	rval_priv->rcv_action_recipient = NULL;
22620Sstevel@tonic-gate 	rval_priv->rcv_action_recip_pid = -1;
22630Sstevel@tonic-gate 	rval_priv->rcv_firing_time = 0;
22640Sstevel@tonic-gate 	rval_priv->rcv_prev = rval_priv->rcv_next = NULL;
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rctl->rc_values, rval_priv);
22670Sstevel@tonic-gate 	rctl->rc_cursor = rval_priv;
22680Sstevel@tonic-gate 	rctl_val_list_reset(rctl->rc_cursor);
22690Sstevel@tonic-gate 	RCTLOP_SET(rctl, p, &e, rctl_model_value(rctl->rc_dict_entry, p,
22700Sstevel@tonic-gate 	    rctl->rc_cursor->rcv_value));
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate 	if (rlp64->rlim_cur != RLIM64_INFINITY && rlp64->rlim_cur < max) {
22730Sstevel@tonic-gate 		rval_basic = rctl_gp_detach_val(ragp);
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 		rval_basic->rcv_privilege = RCPRIV_BASIC;
22760Sstevel@tonic-gate 		rval_basic->rcv_value = rlp64->rlim_cur;
22770Sstevel@tonic-gate 		rval_basic->rcv_flagaction = flagaction;
22780Sstevel@tonic-gate 		rval_basic->rcv_action_signal = signal;
22790Sstevel@tonic-gate 		rval_basic->rcv_action_recipient = p;
22800Sstevel@tonic-gate 		rval_basic->rcv_action_recip_pid = p->p_pid;
22810Sstevel@tonic-gate 		rval_basic->rcv_firing_time = 0;
22820Sstevel@tonic-gate 		rval_basic->rcv_prev = rval_basic->rcv_next = NULL;
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 		(void) rctl_val_list_insert(&rctl->rc_values, rval_basic);
22850Sstevel@tonic-gate 		rctl->rc_cursor = rval_basic;
22860Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
22870Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, &e, rctl_model_value(rctl->rc_dict_entry, p,
22880Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
22890Sstevel@tonic-gate 	}
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	ASSERT(rctl->rc_cursor != NULL);
22920Sstevel@tonic-gate 
22930Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
22940Sstevel@tonic-gate 	return (0);
22950Sstevel@tonic-gate }
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate /*
22990Sstevel@tonic-gate  * rctl_hndl_t rctl_register(const char *, rctl_entity_t, int, rlim64_t,
23000Sstevel@tonic-gate  *   rlim64_t, rctl_ops_t *)
23010Sstevel@tonic-gate  *
23020Sstevel@tonic-gate  * Overview
23030Sstevel@tonic-gate  *   rctl_register() performs a look-up in the dictionary of rctls
23040Sstevel@tonic-gate  *   active on the system; if a rctl of that name is absent, an entry is
23050Sstevel@tonic-gate  *   made into the dictionary.  The rctl is returned with its reference
23060Sstevel@tonic-gate  *   count incremented by one.  If the rctl name already exists, we panic.
23070Sstevel@tonic-gate  *   (Were the resource control system to support dynamic loading and unloading,
23080Sstevel@tonic-gate  *   which it is structured for, duplicate registration should lead to load
23090Sstevel@tonic-gate  *   failure instead of panicking.)
23100Sstevel@tonic-gate  *
23110Sstevel@tonic-gate  *   Each registered rctl has a requirement that a RCPRIV_SYSTEM limit be
23120Sstevel@tonic-gate  *   defined.  This limit contains the highest possible value for this quantity
23130Sstevel@tonic-gate  *   on the system.  Furthermore, the registered control must provide infinite
23140Sstevel@tonic-gate  *   values for all applicable address space models supported by the operating
23150Sstevel@tonic-gate  *   system.  Attempts to set resource control values beyond the system limit
23160Sstevel@tonic-gate  *   will fail.
23170Sstevel@tonic-gate  *
23180Sstevel@tonic-gate  * Return values
23190Sstevel@tonic-gate  *   The rctl's ID.
23200Sstevel@tonic-gate  *
23210Sstevel@tonic-gate  * Caller's context
23220Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
23230Sstevel@tonic-gate  */
23240Sstevel@tonic-gate rctl_hndl_t
rctl_register(const char * name,rctl_entity_t entity,int global_flags,rlim64_t max_native,rlim64_t max_ilp32,rctl_ops_t * ops)23250Sstevel@tonic-gate rctl_register(
23260Sstevel@tonic-gate     const char *name,
23270Sstevel@tonic-gate     rctl_entity_t entity,
23280Sstevel@tonic-gate     int global_flags,
23290Sstevel@tonic-gate     rlim64_t max_native,
23300Sstevel@tonic-gate     rlim64_t max_ilp32,
23310Sstevel@tonic-gate     rctl_ops_t *ops)
23320Sstevel@tonic-gate {
23330Sstevel@tonic-gate 	rctl_t *rctl = kmem_cache_alloc(rctl_cache, KM_SLEEP);
23340Sstevel@tonic-gate 	rctl_val_t *rctl_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
23350Sstevel@tonic-gate 	rctl_dict_entry_t *rctl_de = kmem_zalloc(sizeof (rctl_dict_entry_t),
23360Sstevel@tonic-gate 	    KM_SLEEP);
23370Sstevel@tonic-gate 	rctl_t *old_rctl;
23380Sstevel@tonic-gate 	rctl_hndl_t rhndl;
23390Sstevel@tonic-gate 	int localflags;
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate 	ASSERT(ops != NULL);
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	bzero(rctl, sizeof (rctl_t));
23440Sstevel@tonic-gate 	bzero(rctl_val, sizeof (rctl_val_t));
23450Sstevel@tonic-gate 
23460Sstevel@tonic-gate 	if (global_flags & RCTL_GLOBAL_DENY_NEVER)
23470Sstevel@tonic-gate 		localflags = RCTL_LOCAL_MAXIMAL;
23480Sstevel@tonic-gate 	else
23490Sstevel@tonic-gate 		localflags = RCTL_LOCAL_MAXIMAL | RCTL_LOCAL_DENY;
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	rctl_val->rcv_privilege = RCPRIV_SYSTEM;
23520Sstevel@tonic-gate 	rctl_val->rcv_value = max_native;
23530Sstevel@tonic-gate 	rctl_val->rcv_flagaction = localflags;
23540Sstevel@tonic-gate 	rctl_val->rcv_action_signal = 0;
23550Sstevel@tonic-gate 	rctl_val->rcv_action_recipient = NULL;
23560Sstevel@tonic-gate 	rctl_val->rcv_action_recip_pid = -1;
23570Sstevel@tonic-gate 	rctl_val->rcv_firing_time = 0;
23580Sstevel@tonic-gate 	rctl_val->rcv_next = NULL;
23590Sstevel@tonic-gate 	rctl_val->rcv_prev = NULL;
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate 	rctl_de->rcd_name = (char *)name;
23620Sstevel@tonic-gate 	rctl_de->rcd_default_value = rctl_val;
23630Sstevel@tonic-gate 	rctl_de->rcd_max_native = max_native;
23640Sstevel@tonic-gate 	rctl_de->rcd_max_ilp32 = max_ilp32;
23650Sstevel@tonic-gate 	rctl_de->rcd_entity = entity;
23660Sstevel@tonic-gate 	rctl_de->rcd_ops = ops;
23670Sstevel@tonic-gate 	rctl_de->rcd_flagaction = global_flags;
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	rctl->rc_dict_entry = rctl_de;
23700Sstevel@tonic-gate 	rctl->rc_values = rctl_val;
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	/*
23730Sstevel@tonic-gate 	 * 1.  Take global lock, validate nonexistence of name, get ID.
23740Sstevel@tonic-gate 	 */
23750Sstevel@tonic-gate 	mutex_enter(&rctl_dict_lock);
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict_by_name, (mod_hash_key_t)name,
23780Sstevel@tonic-gate 	    (mod_hash_val_t *)&rhndl) != MH_ERR_NOTFOUND)
23790Sstevel@tonic-gate 		panic("duplicate registration of rctl %s", name);
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	rhndl = rctl_de->rcd_id = rctl->rc_id =
23820Sstevel@tonic-gate 	    (rctl_hndl_t)id_alloc(rctl_ids);
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 	/*
23850Sstevel@tonic-gate 	 * 2.  Insert name-entry pair in rctl_dict_by_name.
23860Sstevel@tonic-gate 	 */
23870Sstevel@tonic-gate 	if (mod_hash_insert(rctl_dict_by_name, (mod_hash_key_t)name,
23880Sstevel@tonic-gate 	    (mod_hash_val_t)rctl_de))
23890Sstevel@tonic-gate 		panic("unable to insert rctl dict entry for %s (%u)", name,
23900Sstevel@tonic-gate 		    (uint_t)rctl->rc_id);
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	/*
23930Sstevel@tonic-gate 	 * 3.  Insert ID-rctl_t * pair in rctl_dict.
23940Sstevel@tonic-gate 	 */
23950Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict, (mod_hash_key_t)(uintptr_t)rctl->rc_id,
23960Sstevel@tonic-gate 	    (mod_hash_val_t *)&old_rctl) != MH_ERR_NOTFOUND)
23970Sstevel@tonic-gate 		panic("duplicate rctl ID %u registered", rctl->rc_id);
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if (mod_hash_insert(rctl_dict, (mod_hash_key_t)(uintptr_t)rctl->rc_id,
24000Sstevel@tonic-gate 	    (mod_hash_val_t)rctl))
24010Sstevel@tonic-gate 		panic("unable to insert rctl %s/%u (%p)", name,
24027240Srh87107 		    (uint_t)rctl->rc_id, (void *)rctl);
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate 	/*
24050Sstevel@tonic-gate 	 * 3a. Insert rctl_dict_entry_t * in appropriate entity list.
24060Sstevel@tonic-gate 	 */
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
24090Sstevel@tonic-gate 
24100Sstevel@tonic-gate 	switch (entity) {
24110Sstevel@tonic-gate 	case RCENTITY_ZONE:
24120Sstevel@tonic-gate 	case RCENTITY_PROJECT:
24130Sstevel@tonic-gate 	case RCENTITY_TASK:
24140Sstevel@tonic-gate 	case RCENTITY_PROCESS:
24150Sstevel@tonic-gate 		rctl_de->rcd_next = rctl_lists[entity];
24160Sstevel@tonic-gate 		rctl_lists[entity] = rctl_de;
24170Sstevel@tonic-gate 		break;
24180Sstevel@tonic-gate 	default:
24190Sstevel@tonic-gate 		panic("registering unknown rctl entity %d (%s)", entity,
24200Sstevel@tonic-gate 		    name);
24210Sstevel@tonic-gate 		break;
24220Sstevel@tonic-gate 	}
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 	/*
24270Sstevel@tonic-gate 	 * 4.  Drop lock.
24280Sstevel@tonic-gate 	 */
24290Sstevel@tonic-gate 	mutex_exit(&rctl_dict_lock);
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	return (rhndl);
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate /*
24350Sstevel@tonic-gate  * static int rctl_global_action(rctl_t *r, rctl_set_t *rset, struct proc *p,
24360Sstevel@tonic-gate  *    rctl_val_t *v)
24370Sstevel@tonic-gate  *
24380Sstevel@tonic-gate  * Overview
24390Sstevel@tonic-gate  *   rctl_global_action() takes, in according with the flags on the rctl_dict
24400Sstevel@tonic-gate  *   entry for the given control, the appropriate actions on the exceeded
24410Sstevel@tonic-gate  *   control value.  Additionally, rctl_global_action() updates the firing time
24420Sstevel@tonic-gate  *   on the exceeded value.
24430Sstevel@tonic-gate  *
24440Sstevel@tonic-gate  * Return values
24450Sstevel@tonic-gate  *   A bitmask reflecting the actions actually taken.
24460Sstevel@tonic-gate  *
24470Sstevel@tonic-gate  * Caller's context
24480Sstevel@tonic-gate  *   No restrictions on context.
24490Sstevel@tonic-gate  */
24500Sstevel@tonic-gate /*ARGSUSED*/
24510Sstevel@tonic-gate static int
rctl_global_action(rctl_t * r,rctl_set_t * rset,struct proc * p,rctl_val_t * v)24520Sstevel@tonic-gate rctl_global_action(rctl_t *r, rctl_set_t *rset, struct proc *p, rctl_val_t *v)
24530Sstevel@tonic-gate {
24540Sstevel@tonic-gate 	rctl_dict_entry_t *rde = r->rc_dict_entry;
24552447Snf202958 	const char *pr, *en, *idstr;
24560Sstevel@tonic-gate 	id_t id;
24572447Snf202958 	enum {
24582447Snf202958 		SUFFIX_NONE,	/* id consumed directly */
24592447Snf202958 		SUFFIX_NUMERIC,	/* id consumed in suffix */
24602447Snf202958 		SUFFIX_STRING	/* idstr consumed in suffix */
24612447Snf202958 	} suffix = SUFFIX_NONE;
24620Sstevel@tonic-gate 	int ret = 0;
24630Sstevel@tonic-gate 
24640Sstevel@tonic-gate 	v->rcv_firing_time = gethrtime();
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 	switch (v->rcv_privilege) {
24670Sstevel@tonic-gate 	case RCPRIV_BASIC:
24680Sstevel@tonic-gate 		pr = "basic";
24690Sstevel@tonic-gate 		break;
24700Sstevel@tonic-gate 	case RCPRIV_PRIVILEGED:
24710Sstevel@tonic-gate 		pr = "privileged";
24720Sstevel@tonic-gate 		break;
24730Sstevel@tonic-gate 	case RCPRIV_SYSTEM:
24740Sstevel@tonic-gate 		pr = "system";
24750Sstevel@tonic-gate 		break;
24760Sstevel@tonic-gate 	default:
24770Sstevel@tonic-gate 		pr = "unknown";
24780Sstevel@tonic-gate 		break;
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	switch (rde->rcd_entity) {
24820Sstevel@tonic-gate 	case RCENTITY_PROCESS:
24830Sstevel@tonic-gate 		en = "process";
24840Sstevel@tonic-gate 		id = p->p_pid;
24852447Snf202958 		suffix = SUFFIX_NONE;
24860Sstevel@tonic-gate 		break;
24870Sstevel@tonic-gate 	case RCENTITY_TASK:
24880Sstevel@tonic-gate 		en = "task";
24890Sstevel@tonic-gate 		id = p->p_task->tk_tkid;
24902447Snf202958 		suffix = SUFFIX_NUMERIC;
24910Sstevel@tonic-gate 		break;
24920Sstevel@tonic-gate 	case RCENTITY_PROJECT:
24930Sstevel@tonic-gate 		en = "project";
24940Sstevel@tonic-gate 		id = p->p_task->tk_proj->kpj_id;
24952447Snf202958 		suffix = SUFFIX_NUMERIC;
24960Sstevel@tonic-gate 		break;
24970Sstevel@tonic-gate 	case RCENTITY_ZONE:
24980Sstevel@tonic-gate 		en = "zone";
24992447Snf202958 		idstr = p->p_zone->zone_name;
25002447Snf202958 		suffix = SUFFIX_STRING;
25010Sstevel@tonic-gate 		break;
25020Sstevel@tonic-gate 	default:
25032447Snf202958 		en = "unknown entity associated with process";
25040Sstevel@tonic-gate 		id = p->p_pid;
25052447Snf202958 		suffix = SUFFIX_NONE;
25060Sstevel@tonic-gate 		break;
25070Sstevel@tonic-gate 	}
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	if (rde->rcd_flagaction & RCTL_GLOBAL_SYSLOG) {
25102447Snf202958 		switch (suffix) {
25112447Snf202958 		default:
25122447Snf202958 		case SUFFIX_NONE:
25132447Snf202958 			(void) strlog(0, 0, 0,
25142447Snf202958 			    rde->rcd_strlog_flags | log_global.lz_active,
25152447Snf202958 			    "%s rctl %s (value %llu) exceeded by %s %d.",
25162447Snf202958 			    pr, rde->rcd_name, v->rcv_value, en, id);
25172447Snf202958 			break;
25182447Snf202958 		case SUFFIX_NUMERIC:
25192447Snf202958 			(void) strlog(0, 0, 0,
25202447Snf202958 			    rde->rcd_strlog_flags | log_global.lz_active,
25212447Snf202958 			    "%s rctl %s (value %llu) exceeded by process %d"
25222447Snf202958 			    " in %s %d.",
25232447Snf202958 			    pr, rde->rcd_name, v->rcv_value, p->p_pid,
25242447Snf202958 			    en, id);
25252447Snf202958 			break;
25262447Snf202958 		case SUFFIX_STRING:
25272447Snf202958 			(void) strlog(0, 0, 0,
25282447Snf202958 			    rde->rcd_strlog_flags | log_global.lz_active,
25292447Snf202958 			    "%s rctl %s (value %llu) exceeded by process %d"
25302447Snf202958 			    " in %s %s.",
25312447Snf202958 			    pr, rde->rcd_name, v->rcv_value, p->p_pid,
25322447Snf202958 			    en, idstr);
25332447Snf202958 			break;
25342447Snf202958 		}
25350Sstevel@tonic-gate 	}
25360Sstevel@tonic-gate 
25370Sstevel@tonic-gate 	if (rde->rcd_flagaction & RCTL_GLOBAL_DENY_ALWAYS)
25380Sstevel@tonic-gate 		ret |= RCT_DENY;
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	return (ret);
25410Sstevel@tonic-gate }
25420Sstevel@tonic-gate 
25430Sstevel@tonic-gate static int
rctl_local_action(rctl_t * r,rctl_set_t * rset,struct proc * p,rctl_val_t * v,uint_t safety)25440Sstevel@tonic-gate rctl_local_action(rctl_t *r, rctl_set_t *rset, struct proc *p, rctl_val_t *v,
25450Sstevel@tonic-gate     uint_t safety)
25460Sstevel@tonic-gate {
25470Sstevel@tonic-gate 	int ret = 0;
25480Sstevel@tonic-gate 	sigqueue_t *sqp = NULL;
25490Sstevel@tonic-gate 	rctl_dict_entry_t *rde = r->rc_dict_entry;
25500Sstevel@tonic-gate 	int unobservable = (rde->rcd_flagaction & RCTL_GLOBAL_UNOBSERVABLE);
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	proc_t *recipient = v->rcv_action_recipient;
25530Sstevel@tonic-gate 	id_t recip_pid = v->rcv_action_recip_pid;
25540Sstevel@tonic-gate 	int recip_signal = v->rcv_action_signal;
25550Sstevel@tonic-gate 	uint_t flagaction = v->rcv_flagaction;
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	if (safety == RCA_UNSAFE_ALL) {
25580Sstevel@tonic-gate 		if (flagaction & RCTL_LOCAL_DENY) {
25590Sstevel@tonic-gate 			ret |= RCT_DENY;
25600Sstevel@tonic-gate 		}
25610Sstevel@tonic-gate 		return (ret);
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	if (flagaction & RCTL_LOCAL_SIGNAL) {
25650Sstevel@tonic-gate 		/*
25660Sstevel@tonic-gate 		 * We can build a siginfo only in the case that it is
25670Sstevel@tonic-gate 		 * safe for us to drop p_lock.  (For asynchronous
25680Sstevel@tonic-gate 		 * checks this is currently not true.)
25690Sstevel@tonic-gate 		 */
25700Sstevel@tonic-gate 		if (safety == RCA_SAFE) {
25710Sstevel@tonic-gate 			mutex_exit(&rset->rcs_lock);
25720Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
25730Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
25740Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
25750Sstevel@tonic-gate 			mutex_enter(&rset->rcs_lock);
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 			sqp->sq_info.si_signo = recip_signal;
25780Sstevel@tonic-gate 			sqp->sq_info.si_code = SI_RCTL;
25790Sstevel@tonic-gate 			sqp->sq_info.si_errno = 0;
25800Sstevel@tonic-gate 			sqp->sq_info.si_entity = (int)rde->rcd_entity;
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 
25830Sstevel@tonic-gate 		if (recipient == NULL || recipient == p) {
25840Sstevel@tonic-gate 			ret |= RCT_SIGNAL;
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate 			if (sqp == NULL) {
25870Sstevel@tonic-gate 				sigtoproc(p, NULL, recip_signal);
25880Sstevel@tonic-gate 			} else if (p == curproc) {
25890Sstevel@tonic-gate 				/*
25900Sstevel@tonic-gate 				 * Then this is a synchronous test and we can
25910Sstevel@tonic-gate 				 * direct the signal at the violating thread.
25920Sstevel@tonic-gate 				 */
25930Sstevel@tonic-gate 				sigaddqa(curproc, curthread, sqp);
25940Sstevel@tonic-gate 			} else {
25950Sstevel@tonic-gate 				sigaddqa(p, NULL, sqp);
25960Sstevel@tonic-gate 			}
25970Sstevel@tonic-gate 		} else if (!unobservable) {
25980Sstevel@tonic-gate 			proc_t *rp;
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 			mutex_exit(&rset->rcs_lock);
26010Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
26020Sstevel@tonic-gate 
26030Sstevel@tonic-gate 			mutex_enter(&pidlock);
26040Sstevel@tonic-gate 			if ((rp = prfind(recip_pid)) == recipient) {
26050Sstevel@tonic-gate 				/*
26060Sstevel@tonic-gate 				 * Recipient process is still alive, but may not
26070Sstevel@tonic-gate 				 * be in this task or project any longer.  In
26080Sstevel@tonic-gate 				 * this case, the recipient's resource control
26090Sstevel@tonic-gate 				 * set pertinent to this control will have
26100Sstevel@tonic-gate 				 * changed--and we will not deliver the signal,
26110Sstevel@tonic-gate 				 * as the recipient process is trying to tear
26120Sstevel@tonic-gate 				 * itself off of its former set.
26130Sstevel@tonic-gate 				 */
26140Sstevel@tonic-gate 				mutex_enter(&rp->p_lock);
26150Sstevel@tonic-gate 				mutex_exit(&pidlock);
26160Sstevel@tonic-gate 
26170Sstevel@tonic-gate 				if (rctl_entity_obtain_rset(rde, rp) == rset) {
26180Sstevel@tonic-gate 					ret |= RCT_SIGNAL;
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate 					if (sqp == NULL)
26210Sstevel@tonic-gate 						sigtoproc(rp, NULL,
26220Sstevel@tonic-gate 						    recip_signal);
26230Sstevel@tonic-gate 					else
26240Sstevel@tonic-gate 						sigaddqa(rp, NULL, sqp);
26250Sstevel@tonic-gate 				} else if (sqp) {
26260Sstevel@tonic-gate 					kmem_free(sqp, sizeof (sigqueue_t));
26270Sstevel@tonic-gate 				}
26280Sstevel@tonic-gate 				mutex_exit(&rp->p_lock);
26290Sstevel@tonic-gate 			} else {
26300Sstevel@tonic-gate 				mutex_exit(&pidlock);
26310Sstevel@tonic-gate 				if (sqp)
26320Sstevel@tonic-gate 					kmem_free(sqp, sizeof (sigqueue_t));
26330Sstevel@tonic-gate 			}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
26360Sstevel@tonic-gate 			/*
26370Sstevel@tonic-gate 			 * Since we dropped p_lock, we may no longer be in the
26380Sstevel@tonic-gate 			 * same task or project as we were at entry.  It is thus
26390Sstevel@tonic-gate 			 * unsafe for us to reacquire the set lock at this
26400Sstevel@tonic-gate 			 * point; callers of rctl_local_action() must handle
26410Sstevel@tonic-gate 			 * this possibility.
26420Sstevel@tonic-gate 			 */
26430Sstevel@tonic-gate 			ret |= RCT_LK_ABANDONED;
26440Sstevel@tonic-gate 		} else if (sqp) {
26450Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
26460Sstevel@tonic-gate 		}
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate 	if ((flagaction & RCTL_LOCAL_DENY) &&
26500Sstevel@tonic-gate 	    (recipient == NULL || recipient == p)) {
26510Sstevel@tonic-gate 		ret |= RCT_DENY;
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	return (ret);
26550Sstevel@tonic-gate }
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate /*
26580Sstevel@tonic-gate  * int rctl_action(rctl_hndl_t, rctl_set_t *, struct proc *, uint_t)
26590Sstevel@tonic-gate  *
26600Sstevel@tonic-gate  * Overview
26610Sstevel@tonic-gate  *   Take the action associated with the enforced value (as defined by
26620Sstevel@tonic-gate  *   rctl_get_enforced_value()) being exceeded or encountered.  Possibly perform
26630Sstevel@tonic-gate  *   a restricted subset of the available actions, if circumstances dictate that
26640Sstevel@tonic-gate  *   we cannot safely allocate memory (for a sigqueue_t) or guarantee process
26650Sstevel@tonic-gate  *   persistence across the duration of the function (an asynchronous action).
26660Sstevel@tonic-gate  *
26670Sstevel@tonic-gate  * Return values
26680Sstevel@tonic-gate  *   Actions taken, according to the rctl_test bitmask.
26690Sstevel@tonic-gate  *
26700Sstevel@tonic-gate  * Caller's context
26710Sstevel@tonic-gate  *   Safe to acquire rcs_lock.
26720Sstevel@tonic-gate  */
26730Sstevel@tonic-gate int
rctl_action(rctl_hndl_t hndl,rctl_set_t * rset,struct proc * p,uint_t safety)26740Sstevel@tonic-gate rctl_action(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p, uint_t safety)
26750Sstevel@tonic-gate {
26760Sstevel@tonic-gate 	return (rctl_action_entity(hndl, rset, p, NULL, safety));
26770Sstevel@tonic-gate }
26780Sstevel@tonic-gate 
26790Sstevel@tonic-gate int
rctl_action_entity(rctl_hndl_t hndl,rctl_set_t * rset,struct proc * p,rctl_entity_p_t * e,uint_t safety)26800Sstevel@tonic-gate rctl_action_entity(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p,
26810Sstevel@tonic-gate     rctl_entity_p_t *e, uint_t safety)
26820Sstevel@tonic-gate {
26830Sstevel@tonic-gate 	int ret = RCT_NONE;
26840Sstevel@tonic-gate 	rctl_t *lrctl;
26850Sstevel@tonic-gate 	rctl_entity_p_t e_tmp;
26860Sstevel@tonic-gate 
26870Sstevel@tonic-gate rctl_action_acquire:
26880Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
26890Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &lrctl) == -1) {
26900Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
26910Sstevel@tonic-gate 		return (ret);
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate 	if (e == NULL) {
26950Sstevel@tonic-gate 		rctl_entity_obtain_entity_p(lrctl->rc_dict_entry->rcd_entity,
26967240Srh87107 		    p, &e_tmp);
26970Sstevel@tonic-gate 		e = &e_tmp;
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	if ((ret & RCT_LK_ABANDONED) == 0) {
27010Sstevel@tonic-gate 		ret |= rctl_global_action(lrctl, rset, p, lrctl->rc_cursor);
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 		RCTLOP_ACTION(lrctl, p, e);
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 		ret |= rctl_local_action(lrctl, rset, p,
27060Sstevel@tonic-gate 		    lrctl->rc_cursor, safety);
27070Sstevel@tonic-gate 
27080Sstevel@tonic-gate 		if (ret & RCT_LK_ABANDONED)
27090Sstevel@tonic-gate 			goto rctl_action_acquire;
27100Sstevel@tonic-gate 	}
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	ret &= ~RCT_LK_ABANDONED;
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 	if (!(ret & RCT_DENY) &&
27150Sstevel@tonic-gate 	    lrctl->rc_cursor->rcv_next != NULL) {
27160Sstevel@tonic-gate 		lrctl->rc_cursor = lrctl->rc_cursor->rcv_next;
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 		RCTLOP_SET(lrctl, p, e, rctl_model_value(lrctl->rc_dict_entry,
27190Sstevel@tonic-gate 		    p, lrctl->rc_cursor->rcv_value));
27200Sstevel@tonic-gate 
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	return (ret);
27250Sstevel@tonic-gate }
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate /*
27280Sstevel@tonic-gate  * int rctl_test(rctl_hndl_t, rctl_set_t *, struct proc *, rctl_qty_t, uint_t)
27290Sstevel@tonic-gate  *
27300Sstevel@tonic-gate  * Overview
27310Sstevel@tonic-gate  *   Increment the resource associated with the given handle, returning zero if
27320Sstevel@tonic-gate  *   the incremented value does not exceed the threshold for the current limit
27330Sstevel@tonic-gate  *   on the resource.
27340Sstevel@tonic-gate  *
27350Sstevel@tonic-gate  * Return values
27360Sstevel@tonic-gate  *   Actions taken, according to the rctl_test bitmask.
27370Sstevel@tonic-gate  *
27380Sstevel@tonic-gate  * Caller's context
27390Sstevel@tonic-gate  *   p_lock held by caller.
27400Sstevel@tonic-gate  */
27410Sstevel@tonic-gate /*ARGSUSED*/
27420Sstevel@tonic-gate int
rctl_test(rctl_hndl_t rhndl,rctl_set_t * rset,struct proc * p,rctl_qty_t incr,uint_t flags)27430Sstevel@tonic-gate rctl_test(rctl_hndl_t rhndl, rctl_set_t *rset, struct proc *p,
27440Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
27450Sstevel@tonic-gate {
27460Sstevel@tonic-gate 	return (rctl_test_entity(rhndl, rset, p, NULL, incr, flags));
27470Sstevel@tonic-gate }
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate int
rctl_test_entity(rctl_hndl_t rhndl,rctl_set_t * rset,struct proc * p,rctl_entity_p_t * e,rctl_qty_t incr,uint_t flags)27500Sstevel@tonic-gate rctl_test_entity(rctl_hndl_t rhndl, rctl_set_t *rset, struct proc *p,
27510Sstevel@tonic-gate     rctl_entity_p_t *e, rctl_qty_t incr, uint_t flags)
27520Sstevel@tonic-gate {
27530Sstevel@tonic-gate 	rctl_t *lrctl;
27540Sstevel@tonic-gate 	int ret = RCT_NONE;
27550Sstevel@tonic-gate 	rctl_entity_p_t e_tmp;
27560Sstevel@tonic-gate 	if (p == &p0) {
27570Sstevel@tonic-gate 		/*
27580Sstevel@tonic-gate 		 * We don't enforce rctls on the kernel itself.
27590Sstevel@tonic-gate 		 */
27600Sstevel@tonic-gate 		return (ret);
27610Sstevel@tonic-gate 	}
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate rctl_test_acquire:
27640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	/*
27690Sstevel@tonic-gate 	 * Dereference from rctl_set.  We don't enforce newly loaded controls
27700Sstevel@tonic-gate 	 * that haven't been set on this entity (since the only valid value is
27710Sstevel@tonic-gate 	 * the infinite system value).
27720Sstevel@tonic-gate 	 */
27730Sstevel@tonic-gate 	if (rctl_set_find(rset, rhndl, &lrctl) == -1) {
27740Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
27750Sstevel@tonic-gate 		return (ret);
27760Sstevel@tonic-gate 	}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	/*
27790Sstevel@tonic-gate 	 * This control is currently unenforced:  maximal value on control
27800Sstevel@tonic-gate 	 * supporting infinitely available resource.
27810Sstevel@tonic-gate 	 */
27820Sstevel@tonic-gate 	if ((lrctl->rc_dict_entry->rcd_flagaction & RCTL_GLOBAL_INFINITE) &&
27830Sstevel@tonic-gate 	    (lrctl->rc_cursor->rcv_flagaction & RCTL_LOCAL_MAXIMAL)) {
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
27860Sstevel@tonic-gate 		return (ret);
27870Sstevel@tonic-gate 	}
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	/*
27900Sstevel@tonic-gate 	 * If we have been called by rctl_test, look up the entity pointer
27910Sstevel@tonic-gate 	 * from the proc pointer.
27920Sstevel@tonic-gate 	 */
27930Sstevel@tonic-gate 	if (e == NULL) {
27940Sstevel@tonic-gate 		rctl_entity_obtain_entity_p(lrctl->rc_dict_entry->rcd_entity,
27957240Srh87107 		    p, &e_tmp);
27960Sstevel@tonic-gate 		e = &e_tmp;
27970Sstevel@tonic-gate 	}
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	/*
28000Sstevel@tonic-gate 	 * Get enforced rctl value and current usage.  Test the increment
28010Sstevel@tonic-gate 	 * with the current usage against the enforced value--take action as
28020Sstevel@tonic-gate 	 * necessary.
28030Sstevel@tonic-gate 	 */
28040Sstevel@tonic-gate 	while (RCTLOP_TEST(lrctl, p, e, lrctl->rc_cursor, incr, flags)) {
28050Sstevel@tonic-gate 		if ((ret & RCT_LK_ABANDONED) == 0) {
28060Sstevel@tonic-gate 			ret |= rctl_global_action(lrctl, rset, p,
28070Sstevel@tonic-gate 			    lrctl->rc_cursor);
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate 			RCTLOP_ACTION(lrctl, p, e);
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 			ret |= rctl_local_action(lrctl, rset, p,
28120Sstevel@tonic-gate 			    lrctl->rc_cursor, flags);
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 			if (ret & RCT_LK_ABANDONED)
28150Sstevel@tonic-gate 				goto rctl_test_acquire;
28160Sstevel@tonic-gate 		}
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 		ret &= ~RCT_LK_ABANDONED;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 		if ((ret & RCT_DENY) == RCT_DENY ||
28210Sstevel@tonic-gate 		    lrctl->rc_cursor->rcv_next == NULL) {
28220Sstevel@tonic-gate 			ret |= RCT_DENY;
28230Sstevel@tonic-gate 			break;
28240Sstevel@tonic-gate 		}
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 		lrctl->rc_cursor = lrctl->rc_cursor->rcv_next;
28270Sstevel@tonic-gate 		RCTLOP_SET(lrctl, p, e, rctl_model_value(lrctl->rc_dict_entry,
28280Sstevel@tonic-gate 		    p, lrctl->rc_cursor->rcv_value));
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	return (ret);
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate /*
28370Sstevel@tonic-gate  * void rctl_init(void)
28380Sstevel@tonic-gate  *
28390Sstevel@tonic-gate  * Overview
28400Sstevel@tonic-gate  *   Initialize the rctl subsystem, including the primoridal rctls
28410Sstevel@tonic-gate  *   provided by the system.  New subsystem-specific rctls should _not_ be
28420Sstevel@tonic-gate  *   initialized here.  (Do it in your own file.)
28430Sstevel@tonic-gate  *
28440Sstevel@tonic-gate  * Return values
28450Sstevel@tonic-gate  *   None.
28460Sstevel@tonic-gate  *
28470Sstevel@tonic-gate  * Caller's context
28480Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.  Must be called prior to any process model
28490Sstevel@tonic-gate  *   initialization.
28500Sstevel@tonic-gate  */
28510Sstevel@tonic-gate void
rctl_init(void)28520Sstevel@tonic-gate rctl_init(void)
28530Sstevel@tonic-gate {
28540Sstevel@tonic-gate 	rctl_cache = kmem_cache_create("rctl_cache", sizeof (rctl_t),
28550Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
28560Sstevel@tonic-gate 	rctl_val_cache = kmem_cache_create("rctl_val_cache",
28570Sstevel@tonic-gate 	    sizeof (rctl_val_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
28580Sstevel@tonic-gate 
28590Sstevel@tonic-gate 	rctl_dict = mod_hash_create_extended("rctl_dict",
28600Sstevel@tonic-gate 	    rctl_dict_size, mod_hash_null_keydtor, rctl_dict_val_dtor,
28610Sstevel@tonic-gate 	    rctl_dict_hash_by_id, NULL, rctl_dict_id_cmp, KM_SLEEP);
28620Sstevel@tonic-gate 	rctl_dict_by_name = mod_hash_create_strhash(
28630Sstevel@tonic-gate 	    "rctl_handles_by_name", rctl_dict_size,
28640Sstevel@tonic-gate 	    mod_hash_null_valdtor);
28650Sstevel@tonic-gate 	rctl_ids = id_space_create("rctl_ids", 1, max_rctl_hndl);
28660Sstevel@tonic-gate 	bzero(rctl_lists, (RC_MAX_ENTITY + 1) * sizeof (rctl_dict_entry_t *));
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	rctlproc_init();
28690Sstevel@tonic-gate }
28702768Ssl108498 
28712768Ssl108498 /*
28727914SRobert.Harris@Sun.COM  * rctl_incr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
28737914SRobert.Harris@Sun.COM  *     int chargeproc)
28742768Ssl108498  *
28752768Ssl108498  * Increments the amount of locked memory on a project, and
28767914SRobert.Harris@Sun.COM  * zone. If proj is non-NULL the project must be held by the
28777914SRobert.Harris@Sun.COM  * caller; if it is NULL the proj and zone of proc_t p are used.
28787914SRobert.Harris@Sun.COM  * If chargeproc is non-zero, then the charged amount is cached
28797914SRobert.Harris@Sun.COM  * on p->p_locked_mem so that the charge can be migrated when a
28807914SRobert.Harris@Sun.COM  * process changes projects.
28812768Ssl108498  *
28822768Ssl108498  * Return values
28832768Ssl108498  *    0 - success
28842768Ssl108498  *    EAGAIN - attempting to increment locked memory is denied by one
28852768Ssl108498  *      or more resource entities.
28862768Ssl108498  */
28872768Ssl108498 int
rctl_incr_locked_mem(proc_t * p,kproject_t * proj,rctl_qty_t inc,int chargeproc)28882768Ssl108498 rctl_incr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
28892768Ssl108498     int chargeproc)
28902768Ssl108498 {
28912768Ssl108498 	kproject_t *projp;
28922768Ssl108498 	zone_t *zonep;
28932768Ssl108498 	rctl_entity_p_t e;
28942768Ssl108498 	int ret = 0;
28952768Ssl108498 
28962768Ssl108498 	ASSERT(p != NULL);
28972768Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
28982768Ssl108498 	if (proj != NULL) {
28992768Ssl108498 		projp = proj;
29007914SRobert.Harris@Sun.COM 		zonep = proj->kpj_zone;
29012768Ssl108498 	} else {
29022768Ssl108498 		projp = p->p_task->tk_proj;
29032768Ssl108498 		zonep = p->p_zone;
29042768Ssl108498 	}
29052768Ssl108498 
29063247Sgjelinek 	mutex_enter(&zonep->zone_mem_lock);
29072768Ssl108498 
29082768Ssl108498 	e.rcep_p.proj = projp;
29092768Ssl108498 	e.rcep_t = RCENTITY_PROJECT;
29109121SVamsi.Krishna@Sun.COM 
29119121SVamsi.Krishna@Sun.COM 	/* check for overflow */
29129121SVamsi.Krishna@Sun.COM 	if ((projp->kpj_data.kpd_locked_mem + inc) <
29139121SVamsi.Krishna@Sun.COM 	    projp->kpj_data.kpd_locked_mem) {
29149121SVamsi.Krishna@Sun.COM 		ret = EAGAIN;
29159121SVamsi.Krishna@Sun.COM 		goto out;
29169121SVamsi.Krishna@Sun.COM 	}
29172768Ssl108498 	if (projp->kpj_data.kpd_locked_mem + inc >
29182768Ssl108498 	    projp->kpj_data.kpd_locked_mem_ctl) {
29192768Ssl108498 		if (rctl_test_entity(rc_project_locked_mem, projp->kpj_rctls,
29202768Ssl108498 		    p, &e, inc, 0) & RCT_DENY) {
29212768Ssl108498 			ret = EAGAIN;
29222768Ssl108498 			goto out;
29232768Ssl108498 		}
29242768Ssl108498 	}
29252768Ssl108498 	e.rcep_p.zone = zonep;
29262768Ssl108498 	e.rcep_t = RCENTITY_ZONE;
29279121SVamsi.Krishna@Sun.COM 
29289121SVamsi.Krishna@Sun.COM 	/* Check for overflow */
29299121SVamsi.Krishna@Sun.COM 	if ((zonep->zone_locked_mem + inc) < zonep->zone_locked_mem) {
29309121SVamsi.Krishna@Sun.COM 		ret = EAGAIN;
29319121SVamsi.Krishna@Sun.COM 		goto out;
29329121SVamsi.Krishna@Sun.COM 	}
29332768Ssl108498 	if (zonep->zone_locked_mem + inc > zonep->zone_locked_mem_ctl) {
29342768Ssl108498 		if (rctl_test_entity(rc_zone_locked_mem, zonep->zone_rctls,
29352768Ssl108498 		    p, &e, inc, 0) & RCT_DENY) {
29362768Ssl108498 			ret = EAGAIN;
29372768Ssl108498 			goto out;
29382768Ssl108498 		}
29392768Ssl108498 	}
29402768Ssl108498 
29412768Ssl108498 	zonep->zone_locked_mem += inc;
29422768Ssl108498 	projp->kpj_data.kpd_locked_mem += inc;
29432768Ssl108498 	if (chargeproc != 0) {
29442768Ssl108498 		p->p_locked_mem += inc;
29452768Ssl108498 	}
29462768Ssl108498 out:
29473247Sgjelinek 	mutex_exit(&zonep->zone_mem_lock);
29482768Ssl108498 	return (ret);
29492768Ssl108498 }
29502768Ssl108498 
29512768Ssl108498 /*
29527914SRobert.Harris@Sun.COM  * rctl_decr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
29537914SRobert.Harris@Sun.COM  *     int creditproc)
29542768Ssl108498  *
29552768Ssl108498  * Decrements the amount of locked memory on a project and
29567914SRobert.Harris@Sun.COM  * zone.  If proj is non-NULL the project must be held by the
29577914SRobert.Harris@Sun.COM  * caller; if it is NULL the proj and zone of proc_t p are used.
29587914SRobert.Harris@Sun.COM  * If creditproc is non-zero, then the quantity of locked memory
29597914SRobert.Harris@Sun.COM  * is subtracted from p->p_locked_mem.
29602768Ssl108498  *
29612768Ssl108498  * Return values
29622768Ssl108498  *   none
29632768Ssl108498  */
29642768Ssl108498 void
rctl_decr_locked_mem(proc_t * p,kproject_t * proj,rctl_qty_t inc,int creditproc)29652768Ssl108498 rctl_decr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
29662768Ssl108498     int creditproc)
29672768Ssl108498 {
29682768Ssl108498 	kproject_t *projp;
29692768Ssl108498 	zone_t *zonep;
29702768Ssl108498 
29712768Ssl108498 	if (proj != NULL) {
29722768Ssl108498 		projp = proj;
29737914SRobert.Harris@Sun.COM 		zonep = proj->kpj_zone;
29742768Ssl108498 	} else {
29752768Ssl108498 		ASSERT(p != NULL);
29762768Ssl108498 		ASSERT(MUTEX_HELD(&p->p_lock));
29772768Ssl108498 		projp = p->p_task->tk_proj;
29782768Ssl108498 		zonep = p->p_zone;
29792768Ssl108498 	}
29802768Ssl108498 
29813247Sgjelinek 	mutex_enter(&zonep->zone_mem_lock);
29822768Ssl108498 	zonep->zone_locked_mem -= inc;
29832768Ssl108498 	projp->kpj_data.kpd_locked_mem -= inc;
29842768Ssl108498 	if (creditproc != 0) {
29852768Ssl108498 		ASSERT(p != NULL);
29862768Ssl108498 		ASSERT(MUTEX_HELD(&p->p_lock));
29872768Ssl108498 		p->p_locked_mem -= inc;
29882768Ssl108498 	}
29893247Sgjelinek 	mutex_exit(&zonep->zone_mem_lock);
29902768Ssl108498 }
29913247Sgjelinek 
29923247Sgjelinek /*
29933247Sgjelinek  * rctl_incr_swap(proc_t *, zone_t *, size_t)
29943247Sgjelinek  *
29953247Sgjelinek  * Overview
29963247Sgjelinek  *   Increments the swap charge on the specified zone.
29973247Sgjelinek  *
29983247Sgjelinek  * Return values
29993247Sgjelinek  *   0 on success.  EAGAIN if swap increment fails due an rctl value
30003247Sgjelinek  *   on the zone.
30013247Sgjelinek  *
30023247Sgjelinek  * Callers context
30033247Sgjelinek  *   p_lock held on specified proc.
30043247Sgjelinek  *   swap must be even multiple of PAGESIZE
30053247Sgjelinek  */
30063247Sgjelinek int
rctl_incr_swap(proc_t * proc,zone_t * zone,size_t swap)30073247Sgjelinek rctl_incr_swap(proc_t *proc, zone_t *zone, size_t swap)
30083247Sgjelinek {
30093247Sgjelinek 	rctl_entity_p_t e;
30103247Sgjelinek 
30113247Sgjelinek 	ASSERT(MUTEX_HELD(&proc->p_lock));
30123247Sgjelinek 	ASSERT((swap & PAGEOFFSET) == 0);
30133247Sgjelinek 	e.rcep_p.zone = zone;
30143247Sgjelinek 	e.rcep_t = RCENTITY_ZONE;
30153247Sgjelinek 
30163247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
30173247Sgjelinek 
30189121SVamsi.Krishna@Sun.COM 	/* Check for overflow */
30199121SVamsi.Krishna@Sun.COM 	if ((zone->zone_max_swap + swap) < zone->zone_max_swap) {
30209121SVamsi.Krishna@Sun.COM 		mutex_exit(&zone->zone_mem_lock);
30219121SVamsi.Krishna@Sun.COM 		return (EAGAIN);
30229121SVamsi.Krishna@Sun.COM 	}
30233247Sgjelinek 	if ((zone->zone_max_swap + swap) >
30243247Sgjelinek 	    zone->zone_max_swap_ctl) {
30253247Sgjelinek 
30263247Sgjelinek 		if (rctl_test_entity(rc_zone_max_swap, zone->zone_rctls,
30273247Sgjelinek 		    proc, &e, swap, 0) & RCT_DENY) {
30283247Sgjelinek 			mutex_exit(&zone->zone_mem_lock);
30293247Sgjelinek 			return (EAGAIN);
30303247Sgjelinek 		}
30313247Sgjelinek 	}
30323247Sgjelinek 	zone->zone_max_swap += swap;
30333247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
30343247Sgjelinek 	return (0);
30353247Sgjelinek }
30363247Sgjelinek 
30373247Sgjelinek /*
30383247Sgjelinek  * rctl_decr_swap(zone_t *, size_t)
30393247Sgjelinek  *
30403247Sgjelinek  * Overview
30413247Sgjelinek  *   Decrements the swap charge on the specified zone.
30423247Sgjelinek  *
30433247Sgjelinek  * Return values
30443247Sgjelinek  *   None
30453247Sgjelinek  *
30463247Sgjelinek  * Callers context
30473247Sgjelinek  *   swap must be even multiple of PAGESIZE
30483247Sgjelinek  */
30493247Sgjelinek void
rctl_decr_swap(zone_t * zone,size_t swap)30503247Sgjelinek rctl_decr_swap(zone_t *zone, size_t swap)
30513247Sgjelinek {
30523247Sgjelinek 	ASSERT((swap & PAGEOFFSET) == 0);
30533247Sgjelinek 	mutex_enter(&zone->zone_mem_lock);
30543247Sgjelinek 	ASSERT(zone->zone_max_swap >= swap);
30553247Sgjelinek 	zone->zone_max_swap -= swap;
30563247Sgjelinek 	mutex_exit(&zone->zone_mem_lock);
30573247Sgjelinek }
30583247Sgjelinek 
30593247Sgjelinek /*
306012633Sjohn.levon@sun.com  * rctl_incr_lofi(proc_t *, zone_t *, size_t)
306112633Sjohn.levon@sun.com  *
306212633Sjohn.levon@sun.com  * Overview
306312633Sjohn.levon@sun.com  *   Increments the number of lofi devices for the zone.
306412633Sjohn.levon@sun.com  *
306512633Sjohn.levon@sun.com  * Return values
306612633Sjohn.levon@sun.com  *   0 on success.  EAGAIN if increment fails due an rctl value
306712633Sjohn.levon@sun.com  *   on the zone.
306812633Sjohn.levon@sun.com  *
306912633Sjohn.levon@sun.com  * Callers context
307012633Sjohn.levon@sun.com  *   p_lock held on specified proc.
307112633Sjohn.levon@sun.com  */
307212633Sjohn.levon@sun.com int
rctl_incr_lofi(proc_t * proc,zone_t * zone,size_t incr)307312633Sjohn.levon@sun.com rctl_incr_lofi(proc_t *proc, zone_t *zone, size_t incr)
307412633Sjohn.levon@sun.com {
307512633Sjohn.levon@sun.com 	rctl_entity_p_t e;
307612633Sjohn.levon@sun.com 
307712633Sjohn.levon@sun.com 	ASSERT(MUTEX_HELD(&proc->p_lock));
307812633Sjohn.levon@sun.com 	ASSERT(incr > 0);
307912633Sjohn.levon@sun.com 
308012633Sjohn.levon@sun.com 	e.rcep_p.zone = zone;
308112633Sjohn.levon@sun.com 	e.rcep_t = RCENTITY_ZONE;
308212633Sjohn.levon@sun.com 
308312633Sjohn.levon@sun.com 	mutex_enter(&zone->zone_rctl_lock);
308412633Sjohn.levon@sun.com 
308512633Sjohn.levon@sun.com 	/* Check for overflow */
308612633Sjohn.levon@sun.com 	if ((zone->zone_max_lofi + incr) < zone->zone_max_lofi) {
308712633Sjohn.levon@sun.com 		mutex_exit(&zone->zone_rctl_lock);
308812633Sjohn.levon@sun.com 		return (EAGAIN);
308912633Sjohn.levon@sun.com 	}
309012633Sjohn.levon@sun.com 	if ((zone->zone_max_lofi + incr) > zone->zone_max_lofi_ctl) {
309112633Sjohn.levon@sun.com 		if (rctl_test_entity(rc_zone_max_lofi, zone->zone_rctls,
309212633Sjohn.levon@sun.com 		    proc, &e, incr, 0) & RCT_DENY) {
309312633Sjohn.levon@sun.com 			mutex_exit(&zone->zone_rctl_lock);
309412633Sjohn.levon@sun.com 			return (EAGAIN);
309512633Sjohn.levon@sun.com 		}
309612633Sjohn.levon@sun.com 	}
309712633Sjohn.levon@sun.com 	zone->zone_max_lofi += incr;
309812633Sjohn.levon@sun.com 	mutex_exit(&zone->zone_rctl_lock);
309912633Sjohn.levon@sun.com 	return (0);
310012633Sjohn.levon@sun.com }
310112633Sjohn.levon@sun.com 
310212633Sjohn.levon@sun.com /*
310312633Sjohn.levon@sun.com  * rctl_decr_lofi(zone_t *, size_t)
310412633Sjohn.levon@sun.com  *
310512633Sjohn.levon@sun.com  * Overview
310612633Sjohn.levon@sun.com  *   Decrements the number of lofi devices for the zone.
310712633Sjohn.levon@sun.com  */
310812633Sjohn.levon@sun.com void
rctl_decr_lofi(zone_t * zone,size_t decr)310912633Sjohn.levon@sun.com rctl_decr_lofi(zone_t *zone, size_t decr)
311012633Sjohn.levon@sun.com {
311112633Sjohn.levon@sun.com 	mutex_enter(&zone->zone_rctl_lock);
311212633Sjohn.levon@sun.com 	ASSERT(zone->zone_max_lofi >= decr);
311312633Sjohn.levon@sun.com 	zone->zone_max_lofi -= decr;
311412633Sjohn.levon@sun.com 	mutex_exit(&zone->zone_rctl_lock);
311512633Sjohn.levon@sun.com }
311612633Sjohn.levon@sun.com 
311712633Sjohn.levon@sun.com /*
31183247Sgjelinek  * Create resource kstat
31193247Sgjelinek  */
31203247Sgjelinek static kstat_t *
rctl_kstat_create_common(char * ks_name,int ks_instance,char * ks_class,uchar_t ks_type,uint_t ks_ndata,uchar_t ks_flags,int ks_zoneid)31213247Sgjelinek rctl_kstat_create_common(char *ks_name, int ks_instance, char *ks_class,
31223247Sgjelinek     uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags, int ks_zoneid)
31233247Sgjelinek {
31243247Sgjelinek 	kstat_t *ksp = NULL;
31253247Sgjelinek 	char name[KSTAT_STRLEN];
31263247Sgjelinek 
31273247Sgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_%d", ks_name, ks_instance);
31283247Sgjelinek 
31293247Sgjelinek 	if ((ksp = kstat_create_zone("caps", ks_zoneid,
31307240Srh87107 	    name, ks_class, ks_type,
31317240Srh87107 	    ks_ndata, ks_flags, ks_zoneid)) != NULL) {
31323247Sgjelinek 		if (ks_zoneid != GLOBAL_ZONEID)
31333247Sgjelinek 			kstat_zone_add(ksp, GLOBAL_ZONEID);
31343247Sgjelinek 	}
31353247Sgjelinek 	return (ksp);
31363247Sgjelinek }
31373247Sgjelinek 
31383247Sgjelinek /*
31393247Sgjelinek  * Create zone-specific resource kstat
31403247Sgjelinek  */
31413247Sgjelinek kstat_t *
rctl_kstat_create_zone(zone_t * zone,char * ks_name,uchar_t ks_type,uint_t ks_ndata,uchar_t ks_flags)31423247Sgjelinek rctl_kstat_create_zone(zone_t *zone, char *ks_name, uchar_t ks_type,
31433247Sgjelinek     uint_t ks_ndata, uchar_t ks_flags)
31443247Sgjelinek {
31453247Sgjelinek 	char name[KSTAT_STRLEN];
31463247Sgjelinek 
31473247Sgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_zone", ks_name);
31483247Sgjelinek 
31493247Sgjelinek 	return (rctl_kstat_create_common(name, zone->zone_id, "zone_caps",
31503247Sgjelinek 	    ks_type, ks_ndata, ks_flags, zone->zone_id));
31513247Sgjelinek }
31523247Sgjelinek 
31533247Sgjelinek /*
31543247Sgjelinek  * Create project-specific resource kstat
31553247Sgjelinek  */
31563247Sgjelinek kstat_t *
rctl_kstat_create_project(kproject_t * kpj,char * ks_name,uchar_t ks_type,uint_t ks_ndata,uchar_t ks_flags)31573247Sgjelinek rctl_kstat_create_project(kproject_t *kpj, char *ks_name, uchar_t ks_type,
31583247Sgjelinek     uint_t ks_ndata, uchar_t ks_flags)
31593247Sgjelinek {
31603247Sgjelinek 	char name[KSTAT_STRLEN];
31613247Sgjelinek 
31623247Sgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_project", ks_name);
31633247Sgjelinek 
31643247Sgjelinek 	return (rctl_kstat_create_common(name, kpj->kpj_id, "project_caps",
31653247Sgjelinek 	    ks_type, ks_ndata, ks_flags, kpj->kpj_zoneid));
31663247Sgjelinek }
3167*12725SMenno.Lageman@Sun.COM 
3168*12725SMenno.Lageman@Sun.COM /*
3169*12725SMenno.Lageman@Sun.COM  * Create task-specific resource kstat
3170*12725SMenno.Lageman@Sun.COM  */
3171*12725SMenno.Lageman@Sun.COM kstat_t *
rctl_kstat_create_task(task_t * tk,char * ks_name,uchar_t ks_type,uint_t ks_ndata,uchar_t ks_flags)3172*12725SMenno.Lageman@Sun.COM rctl_kstat_create_task(task_t *tk, char *ks_name, uchar_t ks_type,
3173*12725SMenno.Lageman@Sun.COM     uint_t ks_ndata, uchar_t ks_flags)
3174*12725SMenno.Lageman@Sun.COM {
3175*12725SMenno.Lageman@Sun.COM 	char name[KSTAT_STRLEN];
3176*12725SMenno.Lageman@Sun.COM 
3177*12725SMenno.Lageman@Sun.COM 	(void) snprintf(name, KSTAT_STRLEN, "%s_task", ks_name);
3178*12725SMenno.Lageman@Sun.COM 
3179*12725SMenno.Lageman@Sun.COM 	return (rctl_kstat_create_common(name, tk->tk_tkid, "task_caps",
3180*12725SMenno.Lageman@Sun.COM 	    ks_type, ks_ndata, ks_flags, tk->tk_proj->kpj_zoneid));
3181*12725SMenno.Lageman@Sun.COM }
3182