xref: /titanic_51/usr/src/uts/common/os/rctl.c (revision c1c0ebd597fd6db650197255bc1248c9f60afad8)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5aa4a4f3bSnf202958  * Common Development and Distribution License (the "License").
6aa4a4f3bSnf202958  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22aa4a4f3bSnf202958  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
297c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
307c478bd9Sstevel@tonic-gate #include <sys/id_space.h>
317c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
320209230bSgjelinek #include <sys/kstat.h>
337c478bd9Sstevel@tonic-gate #include <sys/log.h>
347c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
357c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
367c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/procset.h>
397c478bd9Sstevel@tonic-gate #include <sys/project.h>
407c478bd9Sstevel@tonic-gate #include <sys/resource.h>
417c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
427c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
437c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
447c478bd9Sstevel@tonic-gate #include <sys/systm.h>
457c478bd9Sstevel@tonic-gate #include <sys/task.h>
467c478bd9Sstevel@tonic-gate #include <sys/types.h>
477c478bd9Sstevel@tonic-gate #include <sys/policy.h>
487c478bd9Sstevel@tonic-gate #include <sys/zone.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Resource controls (rctls)
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  *   The rctl subsystem provides a mechanism for kernel components to
547c478bd9Sstevel@tonic-gate  *   register their individual resource controls with the system as a whole,
557c478bd9Sstevel@tonic-gate  *   such that those controls can subscribe to specific actions while being
567c478bd9Sstevel@tonic-gate  *   associated with the various process-model entities provided by the kernel:
577c478bd9Sstevel@tonic-gate  *   the process, the task, the project, and the zone.  (In principle, only
587c478bd9Sstevel@tonic-gate  *   minor modifications would be required to connect the resource control
597c478bd9Sstevel@tonic-gate  *   functionality to non-process-model entities associated with the system.)
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *   Subsystems register their rctls via rctl_register().  Subsystems
627c478bd9Sstevel@tonic-gate  *   also wishing to provide additional limits on a given rctl can modify
637c478bd9Sstevel@tonic-gate  *   them once they have the rctl handle.  Each subsystem should store the
647c478bd9Sstevel@tonic-gate  *   handle to their rctl for direct access.
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  *   A primary dictionary, rctl_dict, contains a hash of id to the default
677c478bd9Sstevel@tonic-gate  *   control definition for each controlled resource-entity pair on the system.
687c478bd9Sstevel@tonic-gate  *   A secondary dictionary, rctl_dict_by_name, contains a hash of name to
697c478bd9Sstevel@tonic-gate  *   resource control handles.  The resource control handles are distributed by
707c478bd9Sstevel@tonic-gate  *   the rctl_ids ID space.  The handles are private and not to be
717c478bd9Sstevel@tonic-gate  *   advertised to userland; all userland interactions are via the rctl
727c478bd9Sstevel@tonic-gate  *   names.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  *   Entities inherit their rctls from their predecessor.  Since projects have
757c478bd9Sstevel@tonic-gate  *   no ancestor, they inherit their rctls from the rctl dict for project
767c478bd9Sstevel@tonic-gate  *   rctls.  It is expected that project controls will be set to their
777c478bd9Sstevel@tonic-gate  *   appropriate values shortly after project creation, presumably from a
787c478bd9Sstevel@tonic-gate  *   policy source such as the project database.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * Data structures
817c478bd9Sstevel@tonic-gate  *   The rctl_set_t attached to each of the process model entities is a simple
827c478bd9Sstevel@tonic-gate  *   hash table keyed on the rctl handle assigned at registration.  The entries
837c478bd9Sstevel@tonic-gate  *   in the hash table are rctl_t's, whose relationship with the active control
847c478bd9Sstevel@tonic-gate  *   values on that resource and with the global state of the resource we
857c478bd9Sstevel@tonic-gate  *   illustrate below:
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  *   rctl_dict[key] --> rctl_dict_entry
887c478bd9Sstevel@tonic-gate  *			   ^
897c478bd9Sstevel@tonic-gate  *			   |
907c478bd9Sstevel@tonic-gate  *			+--+---+
917c478bd9Sstevel@tonic-gate  *   rctl_set[key] ---> | rctl | --> value <-> value <-> system value --> NULL
927c478bd9Sstevel@tonic-gate  *			+--+---+		 ^
937c478bd9Sstevel@tonic-gate  *			   |			 |
947c478bd9Sstevel@tonic-gate  *			   +------- cursor ------+
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  *   That is, the rctl contains a back pointer to the global resource control
977c478bd9Sstevel@tonic-gate  *   state for this resource, which is also available in the rctl_dict hash
987c478bd9Sstevel@tonic-gate  *   table mentioned earlier.  The rctl contains two pointers to resource
997c478bd9Sstevel@tonic-gate  *   control values:  one, values, indicates the entire sequence of control
1007c478bd9Sstevel@tonic-gate  *   values; the other, cursor, indicates the currently active control
1017c478bd9Sstevel@tonic-gate  *   value--the next value to be enforced.  The value list itself is an open,
1027c478bd9Sstevel@tonic-gate  *   doubly-linked list, the last non-NULL member of which is the system value
1037c478bd9Sstevel@tonic-gate  *   for that resource (being the theoretical/conventional maximum allowable
1047c478bd9Sstevel@tonic-gate  *   value for the resource on this OS instance).
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * Ops Vector
1077c478bd9Sstevel@tonic-gate  *   Subsystems publishing rctls need not provide instances of all of the
1087c478bd9Sstevel@tonic-gate  *   functions specified by the ops vector.  In particular, if general
1097c478bd9Sstevel@tonic-gate  *   rctl_*() entry points are not being called, certain functions can be
1107c478bd9Sstevel@tonic-gate  *   omitted.  These align as follows:
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  *   rctl_set()
1137c478bd9Sstevel@tonic-gate  *     You may wish to provide a set callback if locking circumstances prevent
1147c478bd9Sstevel@tonic-gate  *     it or if the performance cost of requesting the enforced value from the
1157c478bd9Sstevel@tonic-gate  *     resource control is prohibitively expensive.  For instance, the currently
1167c478bd9Sstevel@tonic-gate  *     enforced file size limit is stored on the process in the p_fsz_ctl to
1177c478bd9Sstevel@tonic-gate  *     maintain read()/write() performance.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  *   rctl_test()
1207c478bd9Sstevel@tonic-gate  *     You must provide a test callback if you are using the rctl_test()
1217c478bd9Sstevel@tonic-gate  *     interface.  An action callback is optional.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  *   rctl_action()
1247c478bd9Sstevel@tonic-gate  *     You may wish to provide an action callback.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * Registration
1277c478bd9Sstevel@tonic-gate  *   New resource controls can be added to a running instance by loaded modules
1287c478bd9Sstevel@tonic-gate  *   via registration.  (The current implementation does not support unloadable
1297c478bd9Sstevel@tonic-gate  *   modules; this functionality can be added if needed, via an
1307c478bd9Sstevel@tonic-gate  *   activation/deactivation interface involving the manipulation of the
1317c478bd9Sstevel@tonic-gate  *   ops vector for the resource control(s) needing to support unloading.)
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * Control value ordering
1347c478bd9Sstevel@tonic-gate  *   Because the rctl_val chain on each rctl must be navigable in a
1357c478bd9Sstevel@tonic-gate  *   deterministic way, we have to define an ordering on the rctl_val_t's.  The
1367c478bd9Sstevel@tonic-gate  *   defined order is (flags & [maximal], value, flags & [deny-action],
1377c478bd9Sstevel@tonic-gate  *   privilege).
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * Locking
1407c478bd9Sstevel@tonic-gate  *   rctl_dict_lock must be acquired prior to rctl_lists_lock.  Since
1417c478bd9Sstevel@tonic-gate  *   rctl_dict_lock or rctl_lists_lock can be called at the enforcement point
1427c478bd9Sstevel@tonic-gate  *   of any subsystem, holding subsystem locks, it is at all times inappropriate
1437c478bd9Sstevel@tonic-gate  *   to call kmem_alloc(., KM_SLEEP) while holding either of these locks.
1447c478bd9Sstevel@tonic-gate  *   Traversing any of the various resource control entity lists requires
1457c478bd9Sstevel@tonic-gate  *   holding rctl_lists_lock.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  *   Each individual resource control set associated with an entity must have
1487c478bd9Sstevel@tonic-gate  *   its rcs_lock held for the duration of any operations that would add
1497c478bd9Sstevel@tonic-gate  *   resource controls or control values to the set.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  *   The locking subsequence of interest is: p_lock, rctl_dict_lock,
1527c478bd9Sstevel@tonic-gate  *   rctl_lists_lock, entity->rcs_lock.
1537c478bd9Sstevel@tonic-gate  */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate id_t max_rctl_hndl = 32768;
1567c478bd9Sstevel@tonic-gate int rctl_dict_size = 64;
1577c478bd9Sstevel@tonic-gate int rctl_set_size = 8;
1587c478bd9Sstevel@tonic-gate kmutex_t rctl_dict_lock;
1597c478bd9Sstevel@tonic-gate mod_hash_t *rctl_dict;
1607c478bd9Sstevel@tonic-gate mod_hash_t *rctl_dict_by_name;
1617c478bd9Sstevel@tonic-gate id_space_t *rctl_ids;
1627c478bd9Sstevel@tonic-gate kmem_cache_t *rctl_cache;	/* kmem cache for rctl structures */
1637c478bd9Sstevel@tonic-gate kmem_cache_t *rctl_val_cache;	/* kmem cache for rctl values */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate kmutex_t rctl_lists_lock;
1667c478bd9Sstevel@tonic-gate rctl_dict_entry_t *rctl_lists[RC_MAX_ENTITY + 1];
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate  * Default resource control operations and ops vector
1707c478bd9Sstevel@tonic-gate  *   To be used if the particular rcontrol has no specific actions defined, or
1717c478bd9Sstevel@tonic-gate  *   if the subsystem providing the control is quiescing (in preparation for
1727c478bd9Sstevel@tonic-gate  *   unloading, presumably.)
1737c478bd9Sstevel@tonic-gate  *
1747c478bd9Sstevel@tonic-gate  *   Resource controls with callbacks should fill the unused operations with the
1757c478bd9Sstevel@tonic-gate  *   appropriate default impotent callback.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1787c478bd9Sstevel@tonic-gate void
1797c478bd9Sstevel@tonic-gate rcop_no_action(struct rctl *r, struct proc *p, rctl_entity_p_t *e)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1847c478bd9Sstevel@tonic-gate rctl_qty_t
1857c478bd9Sstevel@tonic-gate rcop_no_usage(struct rctl *r, struct proc *p)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 	return (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1917c478bd9Sstevel@tonic-gate int
1927c478bd9Sstevel@tonic-gate rcop_no_set(struct rctl *r, struct proc *p, rctl_entity_p_t *e, rctl_qty_t l)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	return (0);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1987c478bd9Sstevel@tonic-gate int
1997c478bd9Sstevel@tonic-gate rcop_no_test(struct rctl *r, struct proc *p, rctl_entity_p_t *e,
2007c478bd9Sstevel@tonic-gate     struct rctl_val *rv, rctl_qty_t i, uint_t f)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	return (0);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate rctl_ops_t rctl_default_ops = {
2067c478bd9Sstevel@tonic-gate 	rcop_no_action,
2077c478bd9Sstevel@tonic-gate 	rcop_no_usage,
2087c478bd9Sstevel@tonic-gate 	rcop_no_set,
2097c478bd9Sstevel@tonic-gate 	rcop_no_test
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Default "absolute" resource control operation and ops vector
2147c478bd9Sstevel@tonic-gate  *   Useful if there is no usage associated with the
2157c478bd9Sstevel@tonic-gate  *   resource control.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2187c478bd9Sstevel@tonic-gate int
2197c478bd9Sstevel@tonic-gate rcop_absolute_test(struct rctl *r, struct proc *p, rctl_entity_p_t *e,
2207c478bd9Sstevel@tonic-gate     struct rctl_val *rv, rctl_qty_t i, uint_t f)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	return (i > rv->rcv_value);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate rctl_ops_t rctl_absolute_ops = {
2267c478bd9Sstevel@tonic-gate 	rcop_no_action,
2277c478bd9Sstevel@tonic-gate 	rcop_no_usage,
2287c478bd9Sstevel@tonic-gate 	rcop_no_set,
2297c478bd9Sstevel@tonic-gate 	rcop_absolute_test
2307c478bd9Sstevel@tonic-gate };
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2337c478bd9Sstevel@tonic-gate static uint_t
2347c478bd9Sstevel@tonic-gate rctl_dict_hash_by_id(void *hash_data, mod_hash_key_t key)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	return ((uint_t)(uintptr_t)key % rctl_dict_size);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static int
2407c478bd9Sstevel@tonic-gate rctl_dict_id_cmp(mod_hash_key_t key1, mod_hash_key_t key2)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	uint_t u1 = (uint_t)(uintptr_t)key1;
2437c478bd9Sstevel@tonic-gate 	uint_t u2 = (uint_t)(uintptr_t)key2;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (u1 > u2)
2467c478bd9Sstevel@tonic-gate 		return (1);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (u1 == u2)
2497c478bd9Sstevel@tonic-gate 		return (0);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	return (-1);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static void
2557c478bd9Sstevel@tonic-gate rctl_dict_val_dtor(mod_hash_val_t val)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *kr = (rctl_dict_entry_t *)val;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	kmem_free(kr, sizeof (rctl_dict_entry_t));
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate /*
2637c478bd9Sstevel@tonic-gate  * size_t rctl_build_name_buf()
2647c478bd9Sstevel@tonic-gate  *
2657c478bd9Sstevel@tonic-gate  * Overview
2667c478bd9Sstevel@tonic-gate  *   rctl_build_name_buf() walks all active resource controls in the dictionary,
2677c478bd9Sstevel@tonic-gate  *   building a buffer of continguous NUL-terminated strings.
2687c478bd9Sstevel@tonic-gate  *
2697c478bd9Sstevel@tonic-gate  * Return values
2707c478bd9Sstevel@tonic-gate  *   The size of the buffer is returned, the passed pointer's contents are
2717c478bd9Sstevel@tonic-gate  *   modified to that of the location of the buffer.
2727c478bd9Sstevel@tonic-gate  *
2737c478bd9Sstevel@tonic-gate  * Caller's context
2747c478bd9Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate size_t
2777c478bd9Sstevel@tonic-gate rctl_build_name_buf(char **rbufp)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	size_t req_size, cpy_size;
2807c478bd9Sstevel@tonic-gate 	char *rbufloc;
2817c478bd9Sstevel@tonic-gate 	int i;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate rctl_rebuild_name_buf:
2847c478bd9Sstevel@tonic-gate 	req_size = cpy_size = 0;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * Calculate needed buffer length.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
2907c478bd9Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
2917c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		for (rde = rctl_lists[i];
2947c478bd9Sstevel@tonic-gate 		    rde != NULL;
2957c478bd9Sstevel@tonic-gate 		    rde = rde->rcd_next)
2967c478bd9Sstevel@tonic-gate 			req_size += strlen(rde->rcd_name) + 1;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	rbufloc = *rbufp = kmem_alloc(req_size, KM_SLEEP);
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	/*
3037c478bd9Sstevel@tonic-gate 	 * Copy rctl names into our buffer.  If the copy length exceeds the
3047c478bd9Sstevel@tonic-gate 	 * allocate length (due to registration changes), stop copying, free the
3057c478bd9Sstevel@tonic-gate 	 * buffer, and start again.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
3087c478bd9Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
3097c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		for (rde = rctl_lists[i];
3127c478bd9Sstevel@tonic-gate 		    rde != NULL;
3137c478bd9Sstevel@tonic-gate 		    rde = rde->rcd_next) {
3147c478bd9Sstevel@tonic-gate 			size_t length = strlen(rde->rcd_name) + 1;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 			cpy_size += length;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			if (cpy_size > req_size) {
3197c478bd9Sstevel@tonic-gate 				kmem_free(*rbufp, req_size);
3207c478bd9Sstevel@tonic-gate 				mutex_exit(&rctl_lists_lock);
3217c478bd9Sstevel@tonic-gate 				goto rctl_rebuild_name_buf;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 			bcopy(rde->rcd_name, rbufloc, length);
3257c478bd9Sstevel@tonic-gate 			rbufloc += length;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (req_size);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * rctl_dict_entry_t *rctl_dict_lookup(const char *)
3357c478bd9Sstevel@tonic-gate  *
3367c478bd9Sstevel@tonic-gate  * Overview
3377c478bd9Sstevel@tonic-gate  *   rctl_dict_lookup() returns the resource control dictionary entry for the
3387c478bd9Sstevel@tonic-gate  *   named resource control.
3397c478bd9Sstevel@tonic-gate  *
3407c478bd9Sstevel@tonic-gate  * Return values
3417c478bd9Sstevel@tonic-gate  *   A pointer to the appropriate resource control dictionary entry, or NULL if
3427c478bd9Sstevel@tonic-gate  *   no such named entry exists.
3437c478bd9Sstevel@tonic-gate  *
3447c478bd9Sstevel@tonic-gate  * Caller's context
3457c478bd9Sstevel@tonic-gate  *   Caller must not be holding rctl_dict_lock.
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate rctl_dict_entry_t *
3487c478bd9Sstevel@tonic-gate rctl_dict_lookup(const char *name)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_dict_lock);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict_by_name, (mod_hash_key_t)name,
3557c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&rde) == MH_ERR_NOTFOUND) {
3567c478bd9Sstevel@tonic-gate 		mutex_exit(&rctl_dict_lock);
3577c478bd9Sstevel@tonic-gate 		return (NULL);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_dict_lock);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	return (rde);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate  * rctl_hndl_t rctl_hndl_lookup(const char *)
3677c478bd9Sstevel@tonic-gate  *
3687c478bd9Sstevel@tonic-gate  * Overview
3697c478bd9Sstevel@tonic-gate  *   rctl_hndl_lookup() returns the resource control id (the "handle") for the
3707c478bd9Sstevel@tonic-gate  *   named resource control.
3717c478bd9Sstevel@tonic-gate  *
3727c478bd9Sstevel@tonic-gate  * Return values
3737c478bd9Sstevel@tonic-gate  *   The appropriate id, or -1 if no such named entry exists.
3747c478bd9Sstevel@tonic-gate  *
3757c478bd9Sstevel@tonic-gate  * Caller's context
3767c478bd9Sstevel@tonic-gate  *   Caller must not be holding rctl_dict_lock.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate rctl_hndl_t
3797c478bd9Sstevel@tonic-gate rctl_hndl_lookup(const char *name)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	if ((rde = rctl_dict_lookup(name)) == NULL)
3847c478bd9Sstevel@tonic-gate 		return (-1);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	return (rde->rcd_id);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * rctl_dict_entry_t * rctl_dict_lookup_hndl(rctl_hndl_t)
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * Overview
3937c478bd9Sstevel@tonic-gate  *   rctl_dict_lookup_hndl() completes the public lookup functions, by returning
3947c478bd9Sstevel@tonic-gate  *   the resource control dictionary entry matching a given resource control id.
3957c478bd9Sstevel@tonic-gate  *
3967c478bd9Sstevel@tonic-gate  * Return values
3977c478bd9Sstevel@tonic-gate  *   A pointer to the matching resource control dictionary entry, or NULL if the
3987c478bd9Sstevel@tonic-gate  *   id does not match any existing entries.
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * Caller's context
4017c478bd9Sstevel@tonic-gate  *   Caller must not be holding rctl_lists_lock.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate rctl_dict_entry_t *
4047c478bd9Sstevel@tonic-gate rctl_dict_lookup_hndl(rctl_hndl_t hndl)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	uint_t i;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
4097c478bd9Sstevel@tonic-gate 	for (i = 0; i < RC_MAX_ENTITY + 1; i++) {
4107c478bd9Sstevel@tonic-gate 		rctl_dict_entry_t *rde;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		for (rde = rctl_lists[i];
4137c478bd9Sstevel@tonic-gate 		    rde != NULL;
4147c478bd9Sstevel@tonic-gate 		    rde = rde->rcd_next)
4157c478bd9Sstevel@tonic-gate 			if (rde->rcd_id == hndl) {
4167c478bd9Sstevel@tonic-gate 				mutex_exit(&rctl_lists_lock);
4177c478bd9Sstevel@tonic-gate 				return (rde);
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	return (NULL);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * void rctl_add_default_limit(const char *name, rctl_qty_t value,
4277c478bd9Sstevel@tonic-gate  *     rctl_priv_t privilege, uint_t action)
4287c478bd9Sstevel@tonic-gate  *
4297c478bd9Sstevel@tonic-gate  * Overview
4307c478bd9Sstevel@tonic-gate  *   Create a default limit with specified value, privilege, and action.
4317c478bd9Sstevel@tonic-gate  *
4327c478bd9Sstevel@tonic-gate  * Return value
4337c478bd9Sstevel@tonic-gate  *   No value returned.
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate void
4367c478bd9Sstevel@tonic-gate rctl_add_default_limit(const char *name, rctl_qty_t value,
4377c478bd9Sstevel@tonic-gate     rctl_priv_t privilege, uint_t action)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
4407c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	dval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
4437c478bd9Sstevel@tonic-gate 	bzero(dval, sizeof (rctl_val_t));
4447c478bd9Sstevel@tonic-gate 	dval->rcv_value = value;
4457c478bd9Sstevel@tonic-gate 	dval->rcv_privilege = privilege;
4467c478bd9Sstevel@tonic-gate 	dval->rcv_flagaction = action;
4477c478bd9Sstevel@tonic-gate 	dval->rcv_action_recip_pid = -1;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	rde = rctl_dict_lookup(name);
4507c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rde->rcd_default_value, dval);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * void rctl_add_legacy_limit(const char *name, const char *mname,
4557c478bd9Sstevel@tonic-gate  *     const char *lname, rctl_qty_t dflt)
4567c478bd9Sstevel@tonic-gate  *
4577c478bd9Sstevel@tonic-gate  * Overview
4587c478bd9Sstevel@tonic-gate  *   Create a default privileged limit, using the value obtained from
4597c478bd9Sstevel@tonic-gate  *   /etc/system if it exists and is greater than the specified default
4607c478bd9Sstevel@tonic-gate  *   value.  Exists primarily for System V IPC.
4617c478bd9Sstevel@tonic-gate  *
4627c478bd9Sstevel@tonic-gate  * Return value
4637c478bd9Sstevel@tonic-gate  *   No value returned.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate void
4667c478bd9Sstevel@tonic-gate rctl_add_legacy_limit(const char *name, const char *mname, const char *lname,
4677c478bd9Sstevel@tonic-gate     rctl_qty_t dflt, rctl_qty_t max)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	rctl_qty_t qty;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	if (!mod_sysvar(mname, lname, &qty) || (qty < dflt))
4727c478bd9Sstevel@tonic-gate 		qty = dflt;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (qty > max)
4757c478bd9Sstevel@tonic-gate 		qty = max;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	rctl_add_default_limit(name, qty, RCPRIV_PRIVILEGED, RCTL_LOCAL_DENY);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static rctl_set_t *
4817c478bd9Sstevel@tonic-gate rctl_entity_obtain_rset(rctl_dict_entry_t *rcd, struct proc *p)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	rctl_set_t *rset = NULL;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	if (rcd == NULL)
4867c478bd9Sstevel@tonic-gate 		return (NULL);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	switch (rcd->rcd_entity) {
4897c478bd9Sstevel@tonic-gate 	case RCENTITY_PROCESS:
4907c478bd9Sstevel@tonic-gate 		rset = p->p_rctls;
4917c478bd9Sstevel@tonic-gate 		break;
4927c478bd9Sstevel@tonic-gate 	case RCENTITY_TASK:
4937c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
4947c478bd9Sstevel@tonic-gate 		if (p->p_task != NULL)
4957c478bd9Sstevel@tonic-gate 			rset = p->p_task->tk_rctls;
4967c478bd9Sstevel@tonic-gate 		break;
4977c478bd9Sstevel@tonic-gate 	case RCENTITY_PROJECT:
4987c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
4997c478bd9Sstevel@tonic-gate 		if (p->p_task != NULL &&
5007c478bd9Sstevel@tonic-gate 		    p->p_task->tk_proj != NULL)
5017c478bd9Sstevel@tonic-gate 			rset = p->p_task->tk_proj->kpj_rctls;
5027c478bd9Sstevel@tonic-gate 		break;
5037c478bd9Sstevel@tonic-gate 	case RCENTITY_ZONE:
5047c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5057c478bd9Sstevel@tonic-gate 		if (p->p_zone != NULL)
5067c478bd9Sstevel@tonic-gate 			rset = p->p_zone->zone_rctls;
5077c478bd9Sstevel@tonic-gate 		break;
5087c478bd9Sstevel@tonic-gate 	default:
5097c478bd9Sstevel@tonic-gate 		panic("unknown rctl entity type %d seen", rcd->rcd_entity);
5107c478bd9Sstevel@tonic-gate 		break;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	return (rset);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static void
5177c478bd9Sstevel@tonic-gate rctl_entity_obtain_entity_p(rctl_entity_t entity, struct proc *p,
5187c478bd9Sstevel@tonic-gate     rctl_entity_p_t *e)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	e->rcep_p.proc = NULL;
5217c478bd9Sstevel@tonic-gate 	e->rcep_t = entity;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	switch (entity) {
5247c478bd9Sstevel@tonic-gate 	case RCENTITY_PROCESS:
5257c478bd9Sstevel@tonic-gate 		e->rcep_p.proc = p;
5267c478bd9Sstevel@tonic-gate 		break;
5277c478bd9Sstevel@tonic-gate 	case RCENTITY_TASK:
5287c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5297c478bd9Sstevel@tonic-gate 		if (p->p_task != NULL)
5307c478bd9Sstevel@tonic-gate 			e->rcep_p.task = p->p_task;
5317c478bd9Sstevel@tonic-gate 		break;
5327c478bd9Sstevel@tonic-gate 	case RCENTITY_PROJECT:
5337c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5347c478bd9Sstevel@tonic-gate 		if (p->p_task != NULL &&
5357c478bd9Sstevel@tonic-gate 		    p->p_task->tk_proj != NULL)
5367c478bd9Sstevel@tonic-gate 			e->rcep_p.proj = p->p_task->tk_proj;
5377c478bd9Sstevel@tonic-gate 		break;
5387c478bd9Sstevel@tonic-gate 	case RCENTITY_ZONE:
5397c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5407c478bd9Sstevel@tonic-gate 		if (p->p_zone != NULL)
5417c478bd9Sstevel@tonic-gate 			e->rcep_p.zone = p->p_zone;
5427c478bd9Sstevel@tonic-gate 		break;
5437c478bd9Sstevel@tonic-gate 	default:
5447c478bd9Sstevel@tonic-gate 		panic("unknown rctl entity type %d seen", entity);
5457c478bd9Sstevel@tonic-gate 		break;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate static void
5507c478bd9Sstevel@tonic-gate rctl_gp_alloc(rctl_alloc_gp_t *rcgp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	uint_t i;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (rcgp->rcag_nctls > 0) {
5557c478bd9Sstevel@tonic-gate 		rctl_t *prev = kmem_cache_alloc(rctl_cache, KM_SLEEP);
5567c478bd9Sstevel@tonic-gate 		rctl_t *rctl = prev;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 		rcgp->rcag_ctls = prev;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		for (i = 1; i < rcgp->rcag_nctls; i++) {
5617c478bd9Sstevel@tonic-gate 			rctl = kmem_cache_alloc(rctl_cache, KM_SLEEP);
5627c478bd9Sstevel@tonic-gate 			prev->rc_next = rctl;
5637c478bd9Sstevel@tonic-gate 			prev = rctl;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		rctl->rc_next = NULL;
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (rcgp->rcag_nvals > 0) {
5707c478bd9Sstevel@tonic-gate 		rctl_val_t *prev = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
5717c478bd9Sstevel@tonic-gate 		rctl_val_t *rval = prev;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		rcgp->rcag_vals = prev;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 		for (i = 1; i < rcgp->rcag_nvals; i++) {
5767c478bd9Sstevel@tonic-gate 			rval = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
5777c478bd9Sstevel@tonic-gate 			prev->rcv_next = rval;
5787c478bd9Sstevel@tonic-gate 			prev = rval;
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		rval->rcv_next = NULL;
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate static rctl_val_t *
5877c478bd9Sstevel@tonic-gate rctl_gp_detach_val(rctl_alloc_gp_t *rcgp)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	rctl_val_t *rval = rcgp->rcag_vals;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	ASSERT(rcgp->rcag_nvals > 0);
5927c478bd9Sstevel@tonic-gate 	rcgp->rcag_nvals--;
5937c478bd9Sstevel@tonic-gate 	rcgp->rcag_vals = rval->rcv_next;
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	rval->rcv_next = NULL;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 	return (rval);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate static rctl_t *
6017c478bd9Sstevel@tonic-gate rctl_gp_detach_ctl(rctl_alloc_gp_t *rcgp)
6027c478bd9Sstevel@tonic-gate {
6037c478bd9Sstevel@tonic-gate 	rctl_t *rctl = rcgp->rcag_ctls;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	ASSERT(rcgp->rcag_nctls > 0);
6067c478bd9Sstevel@tonic-gate 	rcgp->rcag_nctls--;
6077c478bd9Sstevel@tonic-gate 	rcgp->rcag_ctls = rctl->rc_next;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	rctl->rc_next = NULL;
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	return (rctl);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate static void
6167c478bd9Sstevel@tonic-gate rctl_gp_free(rctl_alloc_gp_t *rcgp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	rctl_val_t *rval = rcgp->rcag_vals;
6197c478bd9Sstevel@tonic-gate 	rctl_t *rctl = rcgp->rcag_ctls;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	while (rval != NULL) {
6227c478bd9Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, rval);
6257c478bd9Sstevel@tonic-gate 		rval = next;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	while (rctl != NULL) {
6297c478bd9Sstevel@tonic-gate 		rctl_t *next = rctl->rc_next;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_cache, rctl);
6327c478bd9Sstevel@tonic-gate 		rctl = next;
6337c478bd9Sstevel@tonic-gate 	}
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * void rctl_prealloc_destroy(rctl_alloc_gp_t *)
6387c478bd9Sstevel@tonic-gate  *
6397c478bd9Sstevel@tonic-gate  * Overview
6407c478bd9Sstevel@tonic-gate  *   Release all unused memory allocated via one of the "prealloc" functions:
6417c478bd9Sstevel@tonic-gate  *   rctl_set_init_prealloc, rctl_set_dup_prealloc, or rctl_rlimit_set_prealloc.
6427c478bd9Sstevel@tonic-gate  *
6437c478bd9Sstevel@tonic-gate  * Return values
6447c478bd9Sstevel@tonic-gate  *   None.
6457c478bd9Sstevel@tonic-gate  *
6467c478bd9Sstevel@tonic-gate  * Caller's context
6477c478bd9Sstevel@tonic-gate  *   No restrictions on context.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate void
6507c478bd9Sstevel@tonic-gate rctl_prealloc_destroy(rctl_alloc_gp_t *gp)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	rctl_gp_free(gp);
6537c478bd9Sstevel@tonic-gate 	kmem_free(gp, sizeof (rctl_alloc_gp_t));
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /*
6577c478bd9Sstevel@tonic-gate  * int rctl_val_cmp(rctl_val_t *, rctl_val_t *, int)
6587c478bd9Sstevel@tonic-gate  *
6597c478bd9Sstevel@tonic-gate  * Overview
6607c478bd9Sstevel@tonic-gate  *   This function defines an ordering to rctl_val_t's in order to allow
6617c478bd9Sstevel@tonic-gate  *   for correct placement in value lists. When the imprecise flag is set,
6627c478bd9Sstevel@tonic-gate  *   the action recipient is ignored. This is to facilitate insert,
6637c478bd9Sstevel@tonic-gate  *   delete, and replace operations by rctlsys.
6647c478bd9Sstevel@tonic-gate  *
6657c478bd9Sstevel@tonic-gate  * Return values
6667c478bd9Sstevel@tonic-gate  *   0 if the val_t's are are considered identical
6677c478bd9Sstevel@tonic-gate  *   -1 if a is ordered lower than b
6687c478bd9Sstevel@tonic-gate  *   1 if a is lowered higher than b
6697c478bd9Sstevel@tonic-gate  *
6707c478bd9Sstevel@tonic-gate  * Caller's context
6717c478bd9Sstevel@tonic-gate  *   No restrictions on context.
6727c478bd9Sstevel@tonic-gate  */
6737c478bd9Sstevel@tonic-gate int
6747c478bd9Sstevel@tonic-gate rctl_val_cmp(rctl_val_t *a, rctl_val_t *b, int imprecise)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_MAXIMAL) <
6777c478bd9Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_MAXIMAL))
6787c478bd9Sstevel@tonic-gate 		return (-1);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_MAXIMAL) >
6817c478bd9Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_MAXIMAL))
6827c478bd9Sstevel@tonic-gate 		return (1);
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (a->rcv_value < b->rcv_value)
6857c478bd9Sstevel@tonic-gate 		return (-1);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (a->rcv_value > b->rcv_value)
6887c478bd9Sstevel@tonic-gate 		return (1);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_DENY) <
6917c478bd9Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_DENY))
6927c478bd9Sstevel@tonic-gate 		return (-1);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	if ((a->rcv_flagaction & RCTL_LOCAL_DENY) >
6957c478bd9Sstevel@tonic-gate 	    (b->rcv_flagaction & RCTL_LOCAL_DENY))
6967c478bd9Sstevel@tonic-gate 		return (1);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	if (a->rcv_privilege < b->rcv_privilege)
6997c478bd9Sstevel@tonic-gate 		return (-1);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (a->rcv_privilege > b->rcv_privilege)
7027c478bd9Sstevel@tonic-gate 		return (1);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (imprecise)
7057c478bd9Sstevel@tonic-gate 		return (0);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (a->rcv_action_recip_pid < b->rcv_action_recip_pid)
7087c478bd9Sstevel@tonic-gate 		return (-1);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (a->rcv_action_recip_pid > b->rcv_action_recip_pid)
7117c478bd9Sstevel@tonic-gate 		return (1);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 	return (0);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate static rctl_val_t *
7177c478bd9Sstevel@tonic-gate rctl_val_list_find(rctl_val_t **head, rctl_val_t *cval)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	rctl_val_t *rval = *head;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	while (rval != NULL) {
7227c478bd9Sstevel@tonic-gate 		if (rctl_val_cmp(cval, rval, 0) == 0)
7237c478bd9Sstevel@tonic-gate 			return (rval);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		rval = rval->rcv_next;
7267c478bd9Sstevel@tonic-gate 	}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	return (NULL);
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate /*
7337c478bd9Sstevel@tonic-gate  * int rctl_val_list_insert(rctl_val_t **, rctl_val_t *)
7347c478bd9Sstevel@tonic-gate  *
7357c478bd9Sstevel@tonic-gate  * Overview
7367c478bd9Sstevel@tonic-gate  *   This function inserts the rctl_val_t into the value list provided.
7377c478bd9Sstevel@tonic-gate  *   The insert is always successful unless if the value is a duplicate
7387c478bd9Sstevel@tonic-gate  *   of one already in the list.
7397c478bd9Sstevel@tonic-gate  *
7407c478bd9Sstevel@tonic-gate  * Return values
7417c478bd9Sstevel@tonic-gate  *    1 if the value was a duplicate of an existing value in the list.
7427c478bd9Sstevel@tonic-gate  *    0 if the insert was successful.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate int
7457c478bd9Sstevel@tonic-gate rctl_val_list_insert(rctl_val_t **root, rctl_val_t *rval)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	rctl_val_t *prev;
7487c478bd9Sstevel@tonic-gate 	int equiv;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	rval->rcv_next = NULL;
7517c478bd9Sstevel@tonic-gate 	rval->rcv_prev = NULL;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (*root == NULL) {
7547c478bd9Sstevel@tonic-gate 		*root = rval;
7557c478bd9Sstevel@tonic-gate 		return (0);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	equiv = rctl_val_cmp(rval, *root, 0);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	if (equiv == 0)
7617c478bd9Sstevel@tonic-gate 		return (1);
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	if (equiv < 0) {
7647c478bd9Sstevel@tonic-gate 		rval->rcv_next = *root;
7657c478bd9Sstevel@tonic-gate 		rval->rcv_next->rcv_prev = rval;
7667c478bd9Sstevel@tonic-gate 		*root = rval;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		return (0);
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	prev = *root;
7727c478bd9Sstevel@tonic-gate 	while (prev->rcv_next != NULL &&
7737c478bd9Sstevel@tonic-gate 	    (equiv = rctl_val_cmp(rval, prev->rcv_next, 0)) > 0) {
7747c478bd9Sstevel@tonic-gate 		prev = prev->rcv_next;
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	if (equiv == 0)
7787c478bd9Sstevel@tonic-gate 		return (1);
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	rval->rcv_next = prev->rcv_next;
7817c478bd9Sstevel@tonic-gate 	if (rval->rcv_next != NULL)
7827c478bd9Sstevel@tonic-gate 		rval->rcv_next->rcv_prev = rval;
7837c478bd9Sstevel@tonic-gate 	prev->rcv_next = rval;
7847c478bd9Sstevel@tonic-gate 	rval->rcv_prev = prev;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	return (0);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate static int
7907c478bd9Sstevel@tonic-gate rctl_val_list_delete(rctl_val_t **root, rctl_val_t *rval)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	rctl_val_t *prev;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	if (*root == NULL)
7957c478bd9Sstevel@tonic-gate 		return (-1);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	prev = *root;
7987c478bd9Sstevel@tonic-gate 	if (rctl_val_cmp(rval, prev, 0) == 0) {
7997c478bd9Sstevel@tonic-gate 		*root = prev->rcv_next;
8007c478bd9Sstevel@tonic-gate 		(*root)->rcv_prev = NULL;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, prev);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		return (0);
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	while (prev->rcv_next != NULL &&
8087c478bd9Sstevel@tonic-gate 	    rctl_val_cmp(rval, prev->rcv_next, 0) != 0) {
8097c478bd9Sstevel@tonic-gate 		prev = prev->rcv_next;
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 	if (prev->rcv_next == NULL) {
8137c478bd9Sstevel@tonic-gate 		/*
8147c478bd9Sstevel@tonic-gate 		 * If we navigate the entire list and cannot find a match, then
8157c478bd9Sstevel@tonic-gate 		 * return failure.
8167c478bd9Sstevel@tonic-gate 		 */
8177c478bd9Sstevel@tonic-gate 		return (-1);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	prev = prev->rcv_next;
8217c478bd9Sstevel@tonic-gate 	prev->rcv_prev->rcv_next = prev->rcv_next;
8227c478bd9Sstevel@tonic-gate 	if (prev->rcv_next != NULL)
8237c478bd9Sstevel@tonic-gate 		prev->rcv_next->rcv_prev = prev->rcv_prev;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	kmem_cache_free(rctl_val_cache, prev);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	return (0);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate static rctl_val_t *
8317c478bd9Sstevel@tonic-gate rctl_val_list_dup(rctl_val_t *rval, rctl_alloc_gp_t *ragp, struct proc *oldp,
8327c478bd9Sstevel@tonic-gate     struct proc *newp)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	rctl_val_t *head = NULL;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next) {
8377c478bd9Sstevel@tonic-gate 		rctl_val_t *dval = rctl_gp_detach_val(ragp);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 		bcopy(rval, dval, sizeof (rctl_val_t));
8407c478bd9Sstevel@tonic-gate 		dval->rcv_prev = dval->rcv_next = NULL;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (oldp == NULL ||
8437c478bd9Sstevel@tonic-gate 		    rval->rcv_action_recipient == NULL ||
8447c478bd9Sstevel@tonic-gate 		    rval->rcv_action_recipient == oldp) {
8457c478bd9Sstevel@tonic-gate 			if (rval->rcv_privilege == RCPRIV_BASIC) {
8467c478bd9Sstevel@tonic-gate 				dval->rcv_action_recipient = newp;
8477c478bd9Sstevel@tonic-gate 				dval->rcv_action_recip_pid = newp->p_pid;
8487c478bd9Sstevel@tonic-gate 			} else {
8497c478bd9Sstevel@tonic-gate 				dval->rcv_action_recipient = NULL;
8507c478bd9Sstevel@tonic-gate 				dval->rcv_action_recip_pid = -1;
8517c478bd9Sstevel@tonic-gate 			}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 			(void) rctl_val_list_insert(&head, dval);
8547c478bd9Sstevel@tonic-gate 		} else {
8557c478bd9Sstevel@tonic-gate 			kmem_cache_free(rctl_val_cache, dval);
8567c478bd9Sstevel@tonic-gate 		}
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	return (head);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate static void
8637c478bd9Sstevel@tonic-gate rctl_val_list_reset(rctl_val_t *rval)
8647c478bd9Sstevel@tonic-gate {
8657c478bd9Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next)
8667c478bd9Sstevel@tonic-gate 		rval->rcv_firing_time = 0;
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate static uint_t
8707c478bd9Sstevel@tonic-gate rctl_val_list_count(rctl_val_t *rval)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	uint_t n = 0;
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	for (; rval != NULL; rval = rval->rcv_next)
8757c478bd9Sstevel@tonic-gate 		n++;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	return (n);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate static void
8827c478bd9Sstevel@tonic-gate rctl_val_list_free(rctl_val_t *rval)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	while (rval != NULL) {
8857c478bd9Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		kmem_cache_free(rctl_val_cache, rval);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 		rval = next;
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate /*
8947c478bd9Sstevel@tonic-gate  * rctl_qty_t rctl_model_maximum(rctl_dict_entry_t *, struct proc *)
8957c478bd9Sstevel@tonic-gate  *
8967c478bd9Sstevel@tonic-gate  * Overview
8977c478bd9Sstevel@tonic-gate  *   In cases where the operating system supports more than one process
8987c478bd9Sstevel@tonic-gate  *   addressing model, the operating system capabilities will exceed those of
8997c478bd9Sstevel@tonic-gate  *   one or more of these models.  Processes in a less capable model must have
9007c478bd9Sstevel@tonic-gate  *   their resources accurately controlled, without diluting those of their
9017c478bd9Sstevel@tonic-gate  *   descendants reached via exec().  rctl_model_maximum() returns the governing
9027c478bd9Sstevel@tonic-gate  *   value for the specified process with respect to a resource control, such
9037c478bd9Sstevel@tonic-gate  *   that the value can used for the RCTLOP_SET callback or compatability
9047c478bd9Sstevel@tonic-gate  *   support.
9057c478bd9Sstevel@tonic-gate  *
9067c478bd9Sstevel@tonic-gate  * Return values
9077c478bd9Sstevel@tonic-gate  *   The maximum value for the given process for the specified resource control.
9087c478bd9Sstevel@tonic-gate  *
9097c478bd9Sstevel@tonic-gate  * Caller's context
9107c478bd9Sstevel@tonic-gate  *   No restrictions on context.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate rctl_qty_t
9137c478bd9Sstevel@tonic-gate rctl_model_maximum(rctl_dict_entry_t *rde, struct proc *p)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_NATIVE)
9167c478bd9Sstevel@tonic-gate 		return (rde->rcd_max_native);
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	return (rde->rcd_max_ilp32);
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * rctl_qty_t rctl_model_value(rctl_dict_entry_t *, struct proc *, rctl_qty_t)
9237c478bd9Sstevel@tonic-gate  *
9247c478bd9Sstevel@tonic-gate  * Overview
9257c478bd9Sstevel@tonic-gate  *   Convenience function wrapping the rctl_model_maximum() functionality.
9267c478bd9Sstevel@tonic-gate  *
9277c478bd9Sstevel@tonic-gate  * Return values
9287c478bd9Sstevel@tonic-gate  *   The lesser of the process's maximum value and the given value for the
9297c478bd9Sstevel@tonic-gate  *   specified resource control.
9307c478bd9Sstevel@tonic-gate  *
9317c478bd9Sstevel@tonic-gate  * Caller's context
9327c478bd9Sstevel@tonic-gate  *   No restrictions on context.
9337c478bd9Sstevel@tonic-gate  */
9347c478bd9Sstevel@tonic-gate rctl_qty_t
9357c478bd9Sstevel@tonic-gate rctl_model_value(rctl_dict_entry_t *rde, struct proc *p, rctl_qty_t value)
9367c478bd9Sstevel@tonic-gate {
9377c478bd9Sstevel@tonic-gate 	rctl_qty_t max = rctl_model_maximum(rde, p);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	return (value < max ? value : max);
9407c478bd9Sstevel@tonic-gate }
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate static void
9437c478bd9Sstevel@tonic-gate rctl_set_insert(rctl_set_t *set, rctl_hndl_t hndl, rctl_t *rctl)
9447c478bd9Sstevel@tonic-gate {
9457c478bd9Sstevel@tonic-gate 	uint_t index = hndl % rctl_set_size;
9467c478bd9Sstevel@tonic-gate 	rctl_t *next_ctl, *prev_ctl;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	rctl->rc_next = NULL;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	if (set->rcs_ctls[index] == NULL) {
9537c478bd9Sstevel@tonic-gate 		set->rcs_ctls[index] = rctl;
9547c478bd9Sstevel@tonic-gate 		return;
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (hndl < set->rcs_ctls[index]->rc_id) {
9587c478bd9Sstevel@tonic-gate 		rctl->rc_next = set->rcs_ctls[index];
9597c478bd9Sstevel@tonic-gate 		set->rcs_ctls[index] = rctl;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		return;
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	for (next_ctl = set->rcs_ctls[index]->rc_next,
9657c478bd9Sstevel@tonic-gate 	    prev_ctl = set->rcs_ctls[index];
9667c478bd9Sstevel@tonic-gate 	    next_ctl != NULL;
9677c478bd9Sstevel@tonic-gate 	    prev_ctl = next_ctl,
9687c478bd9Sstevel@tonic-gate 	    next_ctl = next_ctl->rc_next) {
9697c478bd9Sstevel@tonic-gate 		if (next_ctl->rc_id > hndl) {
9707c478bd9Sstevel@tonic-gate 			rctl->rc_next = next_ctl;
9717c478bd9Sstevel@tonic-gate 			prev_ctl->rc_next = rctl;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 			return;
9747c478bd9Sstevel@tonic-gate 		}
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	rctl->rc_next = next_ctl;
9787c478bd9Sstevel@tonic-gate 	prev_ctl->rc_next = rctl;
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate  * rctl_set_t *rctl_set_create()
9837c478bd9Sstevel@tonic-gate  *
9847c478bd9Sstevel@tonic-gate  * Overview
9857c478bd9Sstevel@tonic-gate  *   Create an empty resource control set, suitable for attaching to a
9867c478bd9Sstevel@tonic-gate  *   controlled entity.
9877c478bd9Sstevel@tonic-gate  *
9887c478bd9Sstevel@tonic-gate  * Return values
9897c478bd9Sstevel@tonic-gate  *   A pointer to the newly created set.
9907c478bd9Sstevel@tonic-gate  *
9917c478bd9Sstevel@tonic-gate  * Caller's context
9927c478bd9Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
9937c478bd9Sstevel@tonic-gate  */
9947c478bd9Sstevel@tonic-gate rctl_set_t *
9957c478bd9Sstevel@tonic-gate rctl_set_create()
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate 	rctl_set_t *rset = kmem_zalloc(sizeof (rctl_set_t), KM_SLEEP);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	mutex_init(&rset->rcs_lock, NULL, MUTEX_DEFAULT, NULL);
10007c478bd9Sstevel@tonic-gate 	rset->rcs_ctls = kmem_zalloc(rctl_set_size * sizeof (rctl_t *),
10017c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
10027c478bd9Sstevel@tonic-gate 	rset->rcs_entity = -1;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	return (rset);
10057c478bd9Sstevel@tonic-gate }
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * rctl_gp_alloc_t *rctl_set_init_prealloc(rctl_entity_t)
10097c478bd9Sstevel@tonic-gate  *
10107c478bd9Sstevel@tonic-gate  * Overview
10117c478bd9Sstevel@tonic-gate  *    rctl_set_init_prealloc() examines the globally defined resource controls
10127c478bd9Sstevel@tonic-gate  *    and their default values and returns a resource control allocation group
10137c478bd9Sstevel@tonic-gate  *    populated with sufficient controls and values to form a representative
10147c478bd9Sstevel@tonic-gate  *    resource control set for the specified entity.
10157c478bd9Sstevel@tonic-gate  *
10167c478bd9Sstevel@tonic-gate  * Return values
10177c478bd9Sstevel@tonic-gate  *    A pointer to the newly created allocation group.
10187c478bd9Sstevel@tonic-gate  *
10197c478bd9Sstevel@tonic-gate  * Caller's context
10207c478bd9Sstevel@tonic-gate  *    Caller must be in a context suitable for KM_SLEEP allocations.
10217c478bd9Sstevel@tonic-gate  */
10227c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *
10237c478bd9Sstevel@tonic-gate rctl_set_init_prealloc(rctl_entity_t entity)
10247c478bd9Sstevel@tonic-gate {
10257c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
10267c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *ragp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (rctl_lists[entity] == NULL)
10317c478bd9Sstevel@tonic-gate 		return (ragp);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	for (rde = rctl_lists[entity]; rde != NULL; rde = rde->rcd_next) {
10367c478bd9Sstevel@tonic-gate 		ragp->rcag_nctls++;
10377c478bd9Sstevel@tonic-gate 		ragp->rcag_nvals += rctl_val_list_count(rde->rcd_default_value);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	rctl_gp_alloc(ragp);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	return (ragp);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate /*
10487c478bd9Sstevel@tonic-gate  * rctl_set_t *rctl_set_init(rctl_entity_t)
10497c478bd9Sstevel@tonic-gate  *
10507c478bd9Sstevel@tonic-gate  * Overview
10517c478bd9Sstevel@tonic-gate  *   rctl_set_create() creates a resource control set, initialized with the
10527c478bd9Sstevel@tonic-gate  *   system infinite values on all registered controls, for attachment to a
10537c478bd9Sstevel@tonic-gate  *   system entity requiring resource controls, such as a process or a task.
10547c478bd9Sstevel@tonic-gate  *
10557c478bd9Sstevel@tonic-gate  * Return values
10567c478bd9Sstevel@tonic-gate  *   A pointer to the newly filled set.
10577c478bd9Sstevel@tonic-gate  *
10587c478bd9Sstevel@tonic-gate  * Caller's context
10597c478bd9Sstevel@tonic-gate  *   Caller must be holding p_lock on entry so that RCTLOP_SET() functions
10607c478bd9Sstevel@tonic-gate  *   may modify task and project members based on the proc structure
10617c478bd9Sstevel@tonic-gate  *   they are passed.
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate rctl_set_t *
10647c478bd9Sstevel@tonic-gate rctl_set_init(rctl_entity_t entity, struct proc *p, rctl_entity_p_t *e,
10657c478bd9Sstevel@tonic-gate     rctl_set_t *rset, rctl_alloc_gp_t *ragp)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
10707c478bd9Sstevel@tonic-gate 	ASSERT(e);
10717c478bd9Sstevel@tonic-gate 	rset->rcs_entity = entity;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	if (rctl_lists[entity] == NULL)
10747c478bd9Sstevel@tonic-gate 		return (rset);
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
10777c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	for (rde = rctl_lists[entity]; rde != NULL; rde = rde->rcd_next) {
10807c478bd9Sstevel@tonic-gate 		rctl_t *rctl = rctl_gp_detach_ctl(ragp);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		rctl->rc_dict_entry = rde;
10837c478bd9Sstevel@tonic-gate 		rctl->rc_id = rde->rcd_id;
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 		rctl->rc_values = rctl_val_list_dup(rde->rcd_default_value,
10867c478bd9Sstevel@tonic-gate 		    ragp, NULL, p);
10877c478bd9Sstevel@tonic-gate 		rctl->rc_cursor = rctl->rc_values;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 		rctl_set_insert(rset, rde->rcd_id, rctl);
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
10947c478bd9Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
10987c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	return (rset);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate static rctl_t *
11047c478bd9Sstevel@tonic-gate rctl_dup(rctl_t *rctl, rctl_alloc_gp_t *ragp, struct proc *oldp,
11057c478bd9Sstevel@tonic-gate     struct proc *newp)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	rctl_t *dup = rctl_gp_detach_ctl(ragp);
11087c478bd9Sstevel@tonic-gate 	rctl_val_t *dval;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	dup->rc_id = rctl->rc_id;
11117c478bd9Sstevel@tonic-gate 	dup->rc_dict_entry = rctl->rc_dict_entry;
11127c478bd9Sstevel@tonic-gate 	dup->rc_next = NULL;
11137c478bd9Sstevel@tonic-gate 	dup->rc_cursor = NULL;
11147c478bd9Sstevel@tonic-gate 	dup->rc_values = rctl_val_list_dup(rctl->rc_values, ragp, oldp, newp);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	for (dval = dup->rc_values;
11177c478bd9Sstevel@tonic-gate 	    dval != NULL; dval = dval->rcv_next) {
11187c478bd9Sstevel@tonic-gate 		if (rctl_val_cmp(rctl->rc_cursor, dval, 0) >= 0) {
11197c478bd9Sstevel@tonic-gate 			dup->rc_cursor = dval;
11207c478bd9Sstevel@tonic-gate 			break;
11217c478bd9Sstevel@tonic-gate 		}
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	if (dup->rc_cursor == NULL)
11257c478bd9Sstevel@tonic-gate 		dup->rc_cursor = dup->rc_values;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	return (dup);
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate static void
11317c478bd9Sstevel@tonic-gate rctl_set_fill_alloc_gp(rctl_set_t *set, rctl_alloc_gp_t *ragp)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate 	uint_t i;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	bzero(ragp, sizeof (rctl_alloc_gp_t));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
11387c478bd9Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 		while (r != NULL) {
11417c478bd9Sstevel@tonic-gate 			ragp->rcag_nctls++;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 			ragp->rcag_nvals += rctl_val_list_count(r->rc_values);
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 			r = r->rc_next;
11467c478bd9Sstevel@tonic-gate 		}
11477c478bd9Sstevel@tonic-gate 	}
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate  * rctl_alloc_gp_t *rctl_set_dup_prealloc(rctl_set_t *)
11527c478bd9Sstevel@tonic-gate  *
11537c478bd9Sstevel@tonic-gate  * Overview
11547c478bd9Sstevel@tonic-gate  *   Given a resource control set, allocate a sufficiently large allocation
11557c478bd9Sstevel@tonic-gate  *   group to contain a duplicate of the set.
11567c478bd9Sstevel@tonic-gate  *
11577c478bd9Sstevel@tonic-gate  * Return value
11587c478bd9Sstevel@tonic-gate  *   A pointer to the newly created allocation group.
11597c478bd9Sstevel@tonic-gate  *
11607c478bd9Sstevel@tonic-gate  * Caller's context
11617c478bd9Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.
11627c478bd9Sstevel@tonic-gate  */
11637c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *
11647c478bd9Sstevel@tonic-gate rctl_set_dup_prealloc(rctl_set_t *set)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *ragp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
11717c478bd9Sstevel@tonic-gate 	rctl_set_fill_alloc_gp(set, ragp);
11727c478bd9Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	rctl_gp_alloc(ragp);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	return (ragp);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate /*
11807c478bd9Sstevel@tonic-gate  * int rctl_set_dup_ready(rctl_set_t *, rctl_alloc_gp_t *)
11817c478bd9Sstevel@tonic-gate  *
11827c478bd9Sstevel@tonic-gate  * Overview
11837c478bd9Sstevel@tonic-gate  *   Verify that the allocation group provided is large enough to allow a
11847c478bd9Sstevel@tonic-gate  *   duplicate of the given resource control set to be constructed from its
11857c478bd9Sstevel@tonic-gate  *   contents.
11867c478bd9Sstevel@tonic-gate  *
11877c478bd9Sstevel@tonic-gate  * Return values
11887c478bd9Sstevel@tonic-gate  *   1 if the allocation group is sufficiently large, 0 otherwise.
11897c478bd9Sstevel@tonic-gate  *
11907c478bd9Sstevel@tonic-gate  * Caller's context
11917c478bd9Sstevel@tonic-gate  *   rcs_lock must be held prior to entry.
11927c478bd9Sstevel@tonic-gate  */
11937c478bd9Sstevel@tonic-gate int
11947c478bd9Sstevel@tonic-gate rctl_set_dup_ready(rctl_set_t *set, rctl_alloc_gp_t *ragp)
11957c478bd9Sstevel@tonic-gate {
11967c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t curr_gp;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 	rctl_set_fill_alloc_gp(set, &curr_gp);
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if (curr_gp.rcag_nctls <= ragp->rcag_nctls &&
12037c478bd9Sstevel@tonic-gate 	    curr_gp.rcag_nvals <= ragp->rcag_nvals)
12047c478bd9Sstevel@tonic-gate 		return (1);
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	return (0);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * rctl_set_t *rctl_set_dup(rctl_set_t *, struct proc *, struct proc *,
12117c478bd9Sstevel@tonic-gate  *   rctl_set_t *, rctl_alloc_gp_t *, int)
12127c478bd9Sstevel@tonic-gate  *
12137c478bd9Sstevel@tonic-gate  * Overview
12147c478bd9Sstevel@tonic-gate  *   Make a duplicate of the resource control set.  The proc pointers are those
12157c478bd9Sstevel@tonic-gate  *   of the owning process and of the process associated with the entity
12167c478bd9Sstevel@tonic-gate  *   receiving the duplicate.
12177c478bd9Sstevel@tonic-gate  *
12187c478bd9Sstevel@tonic-gate  *   Duplication is a 3 stage process. Stage 1 is memory allocation for
12197c478bd9Sstevel@tonic-gate  *   the duplicate set, which is taken care of by rctl_set_dup_prealloc().
12207c478bd9Sstevel@tonic-gate  *   Stage 2 consists of copying all rctls and values from the old set into
12217c478bd9Sstevel@tonic-gate  *   the new. Stage 3 completes the duplication by performing the appropriate
12227c478bd9Sstevel@tonic-gate  *   callbacks for each rctl in the new set.
12237c478bd9Sstevel@tonic-gate  *
12247c478bd9Sstevel@tonic-gate  *   Stages 2 and 3 are handled by calling rctl_set_dup with the RCD_DUP and
12257c478bd9Sstevel@tonic-gate  *   RCD_CALLBACK functions, respectively. The RCD_CALLBACK flag may only
12267c478bd9Sstevel@tonic-gate  *   be supplied if the newp proc structure reflects the new task and
12277c478bd9Sstevel@tonic-gate  *   project linkage.
12287c478bd9Sstevel@tonic-gate  *
12297c478bd9Sstevel@tonic-gate  * Return value
12307c478bd9Sstevel@tonic-gate  *   A pointer to the duplicate set.
12317c478bd9Sstevel@tonic-gate  *
12327c478bd9Sstevel@tonic-gate  * Caller's context
12337c478bd9Sstevel@tonic-gate  *   The rcs_lock of the set to be duplicated must be held prior to entry.
12347c478bd9Sstevel@tonic-gate  */
12357c478bd9Sstevel@tonic-gate rctl_set_t *
12367c478bd9Sstevel@tonic-gate rctl_set_dup(rctl_set_t *set, struct proc *oldp, struct proc *newp,
12377c478bd9Sstevel@tonic-gate     rctl_entity_p_t *e, rctl_set_t *dup, rctl_alloc_gp_t *ragp, int flag)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	uint_t i;
12407c478bd9Sstevel@tonic-gate 	rctl_set_t	*iter;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	ASSERT((flag & RCD_DUP) || (flag & RCD_CALLBACK));
12437c478bd9Sstevel@tonic-gate 	ASSERT(e);
12447c478bd9Sstevel@tonic-gate 	/*
12457c478bd9Sstevel@tonic-gate 	 * When copying the old set, iterate over that. Otherwise, when
12467c478bd9Sstevel@tonic-gate 	 * only callbacks have been requested, iterate over the dup set.
12477c478bd9Sstevel@tonic-gate 	 */
12487c478bd9Sstevel@tonic-gate 	if (flag & RCD_DUP) {
12497c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&set->rcs_lock));
12507c478bd9Sstevel@tonic-gate 		iter = set;
12517c478bd9Sstevel@tonic-gate 		dup->rcs_entity = set->rcs_entity;
12527c478bd9Sstevel@tonic-gate 	} else {
12537c478bd9Sstevel@tonic-gate 		iter = dup;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	mutex_enter(&dup->rcs_lock);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
12597c478bd9Sstevel@tonic-gate 		rctl_t *r = iter->rcs_ctls[i];
12607c478bd9Sstevel@tonic-gate 		rctl_t *d;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 		while (r != NULL) {
12637c478bd9Sstevel@tonic-gate 			if (flag & RCD_DUP) {
12647c478bd9Sstevel@tonic-gate 				d = rctl_dup(r, ragp, oldp, newp);
12657c478bd9Sstevel@tonic-gate 				rctl_set_insert(dup, r->rc_id, d);
12667c478bd9Sstevel@tonic-gate 			} else {
12677c478bd9Sstevel@tonic-gate 				d = r;
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 			if (flag & RCD_CALLBACK)
12717c478bd9Sstevel@tonic-gate 				RCTLOP_SET(d, newp, e,
12727c478bd9Sstevel@tonic-gate 				    rctl_model_value(d->rc_dict_entry, newp,
12737c478bd9Sstevel@tonic-gate 				    d->rc_cursor->rcv_value));
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 			r = r->rc_next;
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	mutex_exit(&dup->rcs_lock);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	return (dup);
12827c478bd9Sstevel@tonic-gate }
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate /*
12857c478bd9Sstevel@tonic-gate  * void rctl_set_free(rctl_set_t *)
12867c478bd9Sstevel@tonic-gate  *
12877c478bd9Sstevel@tonic-gate  * Overview
12887c478bd9Sstevel@tonic-gate  *   Delete resource control set and all attached values.
12897c478bd9Sstevel@tonic-gate  *
12907c478bd9Sstevel@tonic-gate  * Return values
12917c478bd9Sstevel@tonic-gate  *   No value returned.
12927c478bd9Sstevel@tonic-gate  *
12937c478bd9Sstevel@tonic-gate  * Caller's context
12947c478bd9Sstevel@tonic-gate  *   No restrictions on context.
12957c478bd9Sstevel@tonic-gate  */
12967c478bd9Sstevel@tonic-gate void
12977c478bd9Sstevel@tonic-gate rctl_set_free(rctl_set_t *set)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	uint_t i;
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
13027c478bd9Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
13037c478bd9Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 		while (r != NULL) {
13067c478bd9Sstevel@tonic-gate 			rctl_val_t *v = r->rc_values;
13077c478bd9Sstevel@tonic-gate 			rctl_t *n = r->rc_next;
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 			kmem_cache_free(rctl_cache, r);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 			rctl_val_list_free(v);
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 			r = n;
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 	}
13167c478bd9Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	kmem_free(set->rcs_ctls, sizeof (rctl_t *) * rctl_set_size);
13197c478bd9Sstevel@tonic-gate 	kmem_free(set, sizeof (rctl_set_t));
13207c478bd9Sstevel@tonic-gate }
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate /*
13237c478bd9Sstevel@tonic-gate  * void rctl_set_reset(rctl_set_t *)
13247c478bd9Sstevel@tonic-gate  *
13257c478bd9Sstevel@tonic-gate  * Overview
13267c478bd9Sstevel@tonic-gate  *   Resets all rctls within the set such that the lowest value becomes active.
13277c478bd9Sstevel@tonic-gate  *
13287c478bd9Sstevel@tonic-gate  * Return values
13297c478bd9Sstevel@tonic-gate  *   No value returned.
13307c478bd9Sstevel@tonic-gate  *
13317c478bd9Sstevel@tonic-gate  * Caller's context
13327c478bd9Sstevel@tonic-gate  *   No restrictions on context.
13337c478bd9Sstevel@tonic-gate  */
13347c478bd9Sstevel@tonic-gate void
13357c478bd9Sstevel@tonic-gate rctl_set_reset(rctl_set_t *set, struct proc *p, rctl_entity_p_t *e)
13367c478bd9Sstevel@tonic-gate {
13377c478bd9Sstevel@tonic-gate 	uint_t i;
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate 	ASSERT(e);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
13427c478bd9Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
13437c478bd9Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		while (r != NULL) {
13467c478bd9Sstevel@tonic-gate 			r->rc_cursor = r->rc_values;
13477c478bd9Sstevel@tonic-gate 			rctl_val_list_reset(r->rc_cursor);
13487c478bd9Sstevel@tonic-gate 			RCTLOP_SET(r, p, e, rctl_model_value(r->rc_dict_entry,
13497c478bd9Sstevel@tonic-gate 			    p, r->rc_cursor->rcv_value));
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 			ASSERT(r->rc_cursor != NULL);
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 			r = r->rc_next;
13547c478bd9Sstevel@tonic-gate 		}
13557c478bd9Sstevel@tonic-gate 	}
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * void rctl_set_tearoff(rctl_set *, struct proc *)
13627c478bd9Sstevel@tonic-gate  *
13637c478bd9Sstevel@tonic-gate  * Overview
13647c478bd9Sstevel@tonic-gate  *   Tear off any resource control values on this set with an action recipient
13657c478bd9Sstevel@tonic-gate  *   equal to the specified process (as they are becoming invalid with the
13667c478bd9Sstevel@tonic-gate  *   process's departure from this set as an observer).
13677c478bd9Sstevel@tonic-gate  *
13687c478bd9Sstevel@tonic-gate  * Return values
13697c478bd9Sstevel@tonic-gate  *   No value returned.
13707c478bd9Sstevel@tonic-gate  *
13717c478bd9Sstevel@tonic-gate  * Caller's context
13727c478bd9Sstevel@tonic-gate  *   No restrictions on context
13737c478bd9Sstevel@tonic-gate  */
13747c478bd9Sstevel@tonic-gate void
13757c478bd9Sstevel@tonic-gate rctl_set_tearoff(rctl_set_t *set, struct proc *p)
13767c478bd9Sstevel@tonic-gate {
13777c478bd9Sstevel@tonic-gate 	uint_t i;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	mutex_enter(&set->rcs_lock);
13807c478bd9Sstevel@tonic-gate 	for (i = 0; i < rctl_set_size; i++) {
13817c478bd9Sstevel@tonic-gate 		rctl_t *r = set->rcs_ctls[i];
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 		while (r != NULL) {
13847c478bd9Sstevel@tonic-gate 			rctl_val_t *rval;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate tearoff_rewalk_list:
13877c478bd9Sstevel@tonic-gate 			rval = r->rc_values;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 			while (rval != NULL) {
13907c478bd9Sstevel@tonic-gate 				if (rval->rcv_privilege == RCPRIV_BASIC &&
13917c478bd9Sstevel@tonic-gate 				    rval->rcv_action_recipient == p) {
13927c478bd9Sstevel@tonic-gate 					if (r->rc_cursor == rval)
13937c478bd9Sstevel@tonic-gate 						r->rc_cursor = rval->rcv_next;
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 					(void) rctl_val_list_delete(
13967c478bd9Sstevel@tonic-gate 					    &r->rc_values, rval);
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 					goto tearoff_rewalk_list;
13997c478bd9Sstevel@tonic-gate 				}
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 				rval = rval->rcv_next;
14027c478bd9Sstevel@tonic-gate 			}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 			ASSERT(r->rc_cursor != NULL);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 			r = r->rc_next;
14077c478bd9Sstevel@tonic-gate 		}
14087c478bd9Sstevel@tonic-gate 	}
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	mutex_exit(&set->rcs_lock);
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate static int
14147c478bd9Sstevel@tonic-gate rctl_set_find(rctl_set_t *set, rctl_hndl_t hndl, rctl_t **rctl)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate 	uint_t index = hndl % rctl_set_size;
14177c478bd9Sstevel@tonic-gate 	rctl_t *curr_ctl;
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&set->rcs_lock));
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	for (curr_ctl = set->rcs_ctls[index]; curr_ctl != NULL;
14227c478bd9Sstevel@tonic-gate 	    curr_ctl = curr_ctl->rc_next) {
14237c478bd9Sstevel@tonic-gate 		if (curr_ctl->rc_id == hndl) {
14247c478bd9Sstevel@tonic-gate 			*rctl = curr_ctl;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 			return (0);
14277c478bd9Sstevel@tonic-gate 		}
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	return (-1);
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate  * rlim64_t rctl_enforced_value(rctl_hndl_t, rctl_set_t *, struct proc *)
14357c478bd9Sstevel@tonic-gate  *
14367c478bd9Sstevel@tonic-gate  * Overview
14377c478bd9Sstevel@tonic-gate  *   Given a process, get the next enforced value on the rctl of the specified
14387c478bd9Sstevel@tonic-gate  *   handle.
14397c478bd9Sstevel@tonic-gate  *
14407c478bd9Sstevel@tonic-gate  * Return value
14417c478bd9Sstevel@tonic-gate  *   The enforced value.
14427c478bd9Sstevel@tonic-gate  *
14437c478bd9Sstevel@tonic-gate  * Caller's context
14447c478bd9Sstevel@tonic-gate  *   For controls on process collectives, p->p_lock must be held across the
14457c478bd9Sstevel@tonic-gate  *   operation.
14467c478bd9Sstevel@tonic-gate  */
14477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14487c478bd9Sstevel@tonic-gate rctl_qty_t
14497c478bd9Sstevel@tonic-gate rctl_enforced_value(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p)
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate 	rctl_t *rctl;
14527c478bd9Sstevel@tonic-gate 	rlim64_t ret;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &rctl) == -1)
14577c478bd9Sstevel@tonic-gate 		panic("unknown resource control handle %d requested", hndl);
14587c478bd9Sstevel@tonic-gate 	else
14597c478bd9Sstevel@tonic-gate 		ret = rctl_model_value(rctl->rc_dict_entry, p,
14607c478bd9Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value);
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	return (ret);
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate  * int rctl_global_get(const char *, rctl_dict_entry_t *)
14697c478bd9Sstevel@tonic-gate  *
14707c478bd9Sstevel@tonic-gate  * Overview
14717c478bd9Sstevel@tonic-gate  *   Copy a sanitized version of the global rctl for a given resource control
14727c478bd9Sstevel@tonic-gate  *   name.  (By sanitization, we mean that the unsafe data pointers have been
14737c478bd9Sstevel@tonic-gate  *   zeroed.)
14747c478bd9Sstevel@tonic-gate  *
14757c478bd9Sstevel@tonic-gate  * Return value
14767c478bd9Sstevel@tonic-gate  *   -1 if name not defined, 0 otherwise.
14777c478bd9Sstevel@tonic-gate  *
14787c478bd9Sstevel@tonic-gate  * Caller's context
14797c478bd9Sstevel@tonic-gate  *   No restrictions on context.  rctl_dict_lock must not be held.
14807c478bd9Sstevel@tonic-gate  */
14817c478bd9Sstevel@tonic-gate int
14827c478bd9Sstevel@tonic-gate rctl_global_get(const char *name, rctl_dict_entry_t *drde)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup(name);
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	if (rde == NULL)
14877c478bd9Sstevel@tonic-gate 		return (-1);
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	bcopy(rde, drde, sizeof (rctl_dict_entry_t));
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	drde->rcd_next = NULL;
14927c478bd9Sstevel@tonic-gate 	drde->rcd_ops = NULL;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	return (0);
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate /*
14987c478bd9Sstevel@tonic-gate  * int rctl_global_set(const char *, rctl_dict_entry_t *)
14997c478bd9Sstevel@tonic-gate  *
15007c478bd9Sstevel@tonic-gate  * Overview
15017c478bd9Sstevel@tonic-gate  *   Transfer the settable fields of the named rctl to the global rctl matching
15027c478bd9Sstevel@tonic-gate  *   the given resource control name.
15037c478bd9Sstevel@tonic-gate  *
15047c478bd9Sstevel@tonic-gate  * Return value
15057c478bd9Sstevel@tonic-gate  *   -1 if name not defined, 0 otherwise.
15067c478bd9Sstevel@tonic-gate  *
15077c478bd9Sstevel@tonic-gate  * Caller's context
15087c478bd9Sstevel@tonic-gate  *   No restrictions on context.  rctl_dict_lock must not be held.
15097c478bd9Sstevel@tonic-gate  */
15107c478bd9Sstevel@tonic-gate int
15117c478bd9Sstevel@tonic-gate rctl_global_set(const char *name, rctl_dict_entry_t *drde)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup(name);
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (rde == NULL)
15167c478bd9Sstevel@tonic-gate 		return (-1);
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	rde->rcd_flagaction = drde->rcd_flagaction;
15197c478bd9Sstevel@tonic-gate 	rde->rcd_syslog_level = drde->rcd_syslog_level;
15207c478bd9Sstevel@tonic-gate 	rde->rcd_strlog_flags = drde->rcd_strlog_flags;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	return (0);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate static int
15267c478bd9Sstevel@tonic-gate rctl_local_op(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
15277c478bd9Sstevel@tonic-gate     int (*cbop)(rctl_hndl_t, struct proc *p, rctl_entity_p_t *e, rctl_t *,
15287c478bd9Sstevel@tonic-gate     rctl_val_t *, rctl_val_t *), struct proc *p)
15297c478bd9Sstevel@tonic-gate {
15307c478bd9Sstevel@tonic-gate 	rctl_t *rctl;
15317c478bd9Sstevel@tonic-gate 	rctl_set_t *rset;
15327c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
15337c478bd9Sstevel@tonic-gate 	int ret = 0;
15347c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde = rctl_dict_lookup_hndl(hndl);
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate local_op_retry:
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 	rset = rctl_entity_obtain_rset(rde, p);
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 	if (rset == NULL) {
15437c478bd9Sstevel@tonic-gate 		return (-1);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	rctl_entity_obtain_entity_p(rset->rcs_entity, p, &e);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/* using rctl's hndl, get rctl from local set */
15507c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &rctl) == -1) {
15517c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
15527c478bd9Sstevel@tonic-gate 		return (-1);
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	ret = cbop(hndl, p, &e, rctl, oval, nval);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
15587c478bd9Sstevel@tonic-gate 	return (ret);
15597c478bd9Sstevel@tonic-gate }
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15627c478bd9Sstevel@tonic-gate static int
15637c478bd9Sstevel@tonic-gate rctl_local_get_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
15647c478bd9Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	if (oval == NULL) {
15677c478bd9Sstevel@tonic-gate 		/*
15687c478bd9Sstevel@tonic-gate 		 * RCTL_FIRST
15697c478bd9Sstevel@tonic-gate 		 */
15707c478bd9Sstevel@tonic-gate 		bcopy(rctl->rc_values, nval, sizeof (rctl_val_t));
15717c478bd9Sstevel@tonic-gate 	} else {
15727c478bd9Sstevel@tonic-gate 		/*
15737c478bd9Sstevel@tonic-gate 		 * RCTL_NEXT
15747c478bd9Sstevel@tonic-gate 		 */
15757c478bd9Sstevel@tonic-gate 		rctl_val_t *tval = rctl_val_list_find(&rctl->rc_values, oval);
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		if (tval == NULL)
15787c478bd9Sstevel@tonic-gate 			return (ESRCH);
15797c478bd9Sstevel@tonic-gate 		else if (tval->rcv_next == NULL)
15807c478bd9Sstevel@tonic-gate 			return (ENOENT);
15817c478bd9Sstevel@tonic-gate 		else
15827c478bd9Sstevel@tonic-gate 			bcopy(tval->rcv_next, nval, sizeof (rctl_val_t));
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	return (0);
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate /*
15897c478bd9Sstevel@tonic-gate  * int rctl_local_get(rctl_hndl_t, rctl_val_t *)
15907c478bd9Sstevel@tonic-gate  *
15917c478bd9Sstevel@tonic-gate  * Overview
15927c478bd9Sstevel@tonic-gate  *   Get the rctl value for the given flags.
15937c478bd9Sstevel@tonic-gate  *
15947c478bd9Sstevel@tonic-gate  * Return values
15957c478bd9Sstevel@tonic-gate  *   0 for successful get, errno otherwise.
15967c478bd9Sstevel@tonic-gate  */
15977c478bd9Sstevel@tonic-gate int
15987c478bd9Sstevel@tonic-gate rctl_local_get(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
15997c478bd9Sstevel@tonic-gate     struct proc *p)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	return (rctl_local_op(hndl, oval, nval, rctl_local_get_cb, p));
16027c478bd9Sstevel@tonic-gate }
16037c478bd9Sstevel@tonic-gate 
16047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16057c478bd9Sstevel@tonic-gate static int
16067c478bd9Sstevel@tonic-gate rctl_local_delete_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
16077c478bd9Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
16087c478bd9Sstevel@tonic-gate {
16097c478bd9Sstevel@tonic-gate 	if ((oval = rctl_val_list_find(&rctl->rc_values, nval)) == NULL)
16107c478bd9Sstevel@tonic-gate 		return (ESRCH);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (rctl->rc_cursor == oval) {
16137c478bd9Sstevel@tonic-gate 		rctl->rc_cursor = oval->rcv_next;
16147c478bd9Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
16157c478bd9Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
16167c478bd9Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
16197c478bd9Sstevel@tonic-gate 	}
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_delete(&rctl->rc_values, oval);
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	return (0);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate /*
16277c478bd9Sstevel@tonic-gate  * int rctl_local_delete(rctl_hndl_t, rctl_val_t *)
16287c478bd9Sstevel@tonic-gate  *
16297c478bd9Sstevel@tonic-gate  * Overview
16307c478bd9Sstevel@tonic-gate  *   Delete the rctl value for the given flags.
16317c478bd9Sstevel@tonic-gate  *
16327c478bd9Sstevel@tonic-gate  * Return values
16337c478bd9Sstevel@tonic-gate  *   0 for successful delete, errno otherwise.
16347c478bd9Sstevel@tonic-gate  */
16357c478bd9Sstevel@tonic-gate int
16367c478bd9Sstevel@tonic-gate rctl_local_delete(rctl_hndl_t hndl, rctl_val_t *val, struct proc *p)
16377c478bd9Sstevel@tonic-gate {
16387c478bd9Sstevel@tonic-gate 	return (rctl_local_op(hndl, NULL, val, rctl_local_delete_cb, p));
16397c478bd9Sstevel@tonic-gate }
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate /*
16427c478bd9Sstevel@tonic-gate  * rctl_local_insert_cb()
16437c478bd9Sstevel@tonic-gate  *
16447c478bd9Sstevel@tonic-gate  * Overview
16457c478bd9Sstevel@tonic-gate  *   Insert a new value into the rctl's val list. If an error occurs,
16467c478bd9Sstevel@tonic-gate  *   the val list must be left in the same state as when the function
16477c478bd9Sstevel@tonic-gate  *   was entered.
16487c478bd9Sstevel@tonic-gate  *
16497c478bd9Sstevel@tonic-gate  * Return Values
16507c478bd9Sstevel@tonic-gate  *   0 for successful insert, EINVAL if the value is duplicated in the
16517c478bd9Sstevel@tonic-gate  *   existing list.
16527c478bd9Sstevel@tonic-gate  */
16537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16547c478bd9Sstevel@tonic-gate static int
16557c478bd9Sstevel@tonic-gate rctl_local_insert_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
16567c478bd9Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Before inserting, confirm there are no duplicates of this value
16607c478bd9Sstevel@tonic-gate 	 * and flag level. If there is a duplicate, flag an error and do
16617c478bd9Sstevel@tonic-gate 	 * nothing.
16627c478bd9Sstevel@tonic-gate 	 */
16637c478bd9Sstevel@tonic-gate 	if (rctl_val_list_insert(&rctl->rc_values, nval) != 0)
16647c478bd9Sstevel@tonic-gate 		return (EINVAL);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (rctl_val_cmp(nval, rctl->rc_cursor, 0) < 0) {
16677c478bd9Sstevel@tonic-gate 		rctl->rc_cursor = nval;
16687c478bd9Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
16697c478bd9Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, e, rctl_model_value(rctl->rc_dict_entry, p,
16707c478bd9Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		ASSERT(rctl->rc_cursor != NULL);
16737c478bd9Sstevel@tonic-gate 	}
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 	return (0);
16767c478bd9Sstevel@tonic-gate }
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate /*
16797c478bd9Sstevel@tonic-gate  * int rctl_local_insert(rctl_hndl_t, rctl_val_t *)
16807c478bd9Sstevel@tonic-gate  *
16817c478bd9Sstevel@tonic-gate  * Overview
16827c478bd9Sstevel@tonic-gate  *   Insert the rctl value into the appropriate rctl set for the calling
16837c478bd9Sstevel@tonic-gate  *   process, given the handle.
16847c478bd9Sstevel@tonic-gate  */
16857c478bd9Sstevel@tonic-gate int
16867c478bd9Sstevel@tonic-gate rctl_local_insert(rctl_hndl_t hndl, rctl_val_t *val, struct proc *p)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	return (rctl_local_op(hndl, NULL, val, rctl_local_insert_cb, p));
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate static int
16927c478bd9Sstevel@tonic-gate rctl_local_replace_cb(rctl_hndl_t hndl, struct proc *p, rctl_entity_p_t *e,
16937c478bd9Sstevel@tonic-gate     rctl_t *rctl, rctl_val_t *oval, rctl_val_t *nval)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	int ret;
1696*c1c0ebd5Ssl108498 	rctl_val_t *tmp;
1697*c1c0ebd5Ssl108498 
1698*c1c0ebd5Ssl108498 	/* Verify that old will be delete-able */
1699*c1c0ebd5Ssl108498 	tmp = rctl_val_list_find(&rctl->rc_values, oval);
1700*c1c0ebd5Ssl108498 	if (tmp == NULL)
1701*c1c0ebd5Ssl108498 		return (ESRCH);
1702*c1c0ebd5Ssl108498 	/*
1703*c1c0ebd5Ssl108498 	 * Caller should verify that value being deleted is not the
1704*c1c0ebd5Ssl108498 	 * system value.
1705*c1c0ebd5Ssl108498 	 */
1706*c1c0ebd5Ssl108498 	ASSERT(tmp->rcv_privilege != RCPRIV_SYSTEM);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	/*
17097c478bd9Sstevel@tonic-gate 	 * rctl_local_insert_cb() does the job of flagging an error
17107c478bd9Sstevel@tonic-gate 	 * for any duplicate values. So, call rctl_local_insert_cb()
17117c478bd9Sstevel@tonic-gate 	 * for the new value first, then do deletion of the old value.
17127c478bd9Sstevel@tonic-gate 	 * Since this is a callback function to rctl_local_op, we can
17137c478bd9Sstevel@tonic-gate 	 * count on rcs_lock being held at this point. This guarantees
17147c478bd9Sstevel@tonic-gate 	 * that there is at no point a visible list which contains both
17157c478bd9Sstevel@tonic-gate 	 * new and old values.
17167c478bd9Sstevel@tonic-gate 	 */
17177c478bd9Sstevel@tonic-gate 	if (ret = rctl_local_insert_cb(hndl, p, e, rctl, NULL, nval))
17187c478bd9Sstevel@tonic-gate 		return (ret);
17197c478bd9Sstevel@tonic-gate 
1720*c1c0ebd5Ssl108498 	ret = rctl_local_delete_cb(hndl, p, e, rctl, NULL, oval);
1721*c1c0ebd5Ssl108498 	ASSERT(ret == 0);
1722*c1c0ebd5Ssl108498 	return (0);
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate /*
17267c478bd9Sstevel@tonic-gate  * int rctl_local_replace(rctl_hndl_t, void *, int, uint64_t *)
17277c478bd9Sstevel@tonic-gate  *
17287c478bd9Sstevel@tonic-gate  * Overview
17297c478bd9Sstevel@tonic-gate  *   Replace the rctl value with a new one.
17307c478bd9Sstevel@tonic-gate  *
17317c478bd9Sstevel@tonic-gate  * Return values
17327c478bd9Sstevel@tonic-gate  *   0 for successful replace, errno otherwise.
17337c478bd9Sstevel@tonic-gate  */
17347c478bd9Sstevel@tonic-gate int
17357c478bd9Sstevel@tonic-gate rctl_local_replace(rctl_hndl_t hndl, rctl_val_t *oval, rctl_val_t *nval,
17367c478bd9Sstevel@tonic-gate     struct proc *p)
17377c478bd9Sstevel@tonic-gate {
17387c478bd9Sstevel@tonic-gate 	return (rctl_local_op(hndl, oval, nval, rctl_local_replace_cb, p));
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate /*
17427c478bd9Sstevel@tonic-gate  * int rctl_rlimit_get(rctl_hndl_t, struct proc *, struct rlimit64 *)
17437c478bd9Sstevel@tonic-gate  *
17447c478bd9Sstevel@tonic-gate  * Overview
17457c478bd9Sstevel@tonic-gate  *   To support rlimit compatibility, we need a function which takes a 64-bit
17467c478bd9Sstevel@tonic-gate  *   rlimit and encodes it as appropriate rcontrol values on the given rcontrol.
17477c478bd9Sstevel@tonic-gate  *   This operation is only intended for legacy rlimits.
17487c478bd9Sstevel@tonic-gate  */
17497c478bd9Sstevel@tonic-gate int
17507c478bd9Sstevel@tonic-gate rctl_rlimit_get(rctl_hndl_t rc, struct proc *p, struct rlimit64 *rlp64)
17517c478bd9Sstevel@tonic-gate {
17527c478bd9Sstevel@tonic-gate 	rctl_t *rctl;
17537c478bd9Sstevel@tonic-gate 	rctl_val_t *rval;
17547c478bd9Sstevel@tonic-gate 	rctl_set_t *rset = p->p_rctls;
17557c478bd9Sstevel@tonic-gate 	int soft_limit_seen = 0;
17567c478bd9Sstevel@tonic-gate 	int test_for_deny = 1;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
17597c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, rc, &rctl) == -1) {
17607c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
17617c478bd9Sstevel@tonic-gate 		return (-1);
17627c478bd9Sstevel@tonic-gate 	}
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	rval = rctl->rc_values;
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	if (rctl->rc_dict_entry->rcd_flagaction & (RCTL_GLOBAL_DENY_NEVER |
17677c478bd9Sstevel@tonic-gate 	    RCTL_GLOBAL_DENY_ALWAYS))
17687c478bd9Sstevel@tonic-gate 		test_for_deny = 0;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	/*
17717c478bd9Sstevel@tonic-gate 	 * 1.  Find the first control value with the RCTL_LOCAL_DENY bit set.
17727c478bd9Sstevel@tonic-gate 	 */
17737c478bd9Sstevel@tonic-gate 	while (rval != NULL && rval->rcv_privilege != RCPRIV_SYSTEM) {
17747c478bd9Sstevel@tonic-gate 		if (test_for_deny &&
17757c478bd9Sstevel@tonic-gate 		    (rval->rcv_flagaction & RCTL_LOCAL_DENY) == 0) {
17767c478bd9Sstevel@tonic-gate 			rval = rval->rcv_next;
17777c478bd9Sstevel@tonic-gate 			continue;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		/*
17817c478bd9Sstevel@tonic-gate 		 * 2.  If this is an RCPRIV_BASIC value, then we've found the
17827c478bd9Sstevel@tonic-gate 		 * effective soft limit and should set rlim_cur.  We should then
17837c478bd9Sstevel@tonic-gate 		 * continue looking for another control value with the DENY bit
17847c478bd9Sstevel@tonic-gate 		 * set.
17857c478bd9Sstevel@tonic-gate 		 */
17867c478bd9Sstevel@tonic-gate 		if (rval->rcv_privilege == RCPRIV_BASIC) {
17877c478bd9Sstevel@tonic-gate 			if (soft_limit_seen) {
17887c478bd9Sstevel@tonic-gate 				rval = rval->rcv_next;
17897c478bd9Sstevel@tonic-gate 				continue;
17907c478bd9Sstevel@tonic-gate 			}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 			if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
17937c478bd9Sstevel@tonic-gate 			    rval->rcv_value < rctl_model_maximum(
17947c478bd9Sstevel@tonic-gate 			    rctl->rc_dict_entry, p))
17957c478bd9Sstevel@tonic-gate 				rlp64->rlim_cur = rval->rcv_value;
17967c478bd9Sstevel@tonic-gate 			else
17977c478bd9Sstevel@tonic-gate 				rlp64->rlim_cur = RLIM64_INFINITY;
17987c478bd9Sstevel@tonic-gate 			soft_limit_seen = 1;
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 			rval = rval->rcv_next;
18017c478bd9Sstevel@tonic-gate 			continue;
18027c478bd9Sstevel@tonic-gate 		}
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 		/*
18057c478bd9Sstevel@tonic-gate 		 * 3.  This is an RCPRIV_PRIVILEGED value.  If we haven't found
18067c478bd9Sstevel@tonic-gate 		 * a soft limit candidate, then we've found the effective hard
18077c478bd9Sstevel@tonic-gate 		 * and soft limits and should set both  If we had found a soft
18087c478bd9Sstevel@tonic-gate 		 * limit, then this is only the hard limit and we need only set
18097c478bd9Sstevel@tonic-gate 		 * rlim_max.
18107c478bd9Sstevel@tonic-gate 		 */
18117c478bd9Sstevel@tonic-gate 		if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
18127c478bd9Sstevel@tonic-gate 		    rval->rcv_value < rctl_model_maximum(rctl->rc_dict_entry,
18137c478bd9Sstevel@tonic-gate 		    p))
18147c478bd9Sstevel@tonic-gate 			rlp64->rlim_max = rval->rcv_value;
18157c478bd9Sstevel@tonic-gate 		else
18167c478bd9Sstevel@tonic-gate 			rlp64->rlim_max = RLIM64_INFINITY;
18177c478bd9Sstevel@tonic-gate 		if (!soft_limit_seen)
18187c478bd9Sstevel@tonic-gate 			rlp64->rlim_cur = rlp64->rlim_max;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
18217c478bd9Sstevel@tonic-gate 		return (0);
18227c478bd9Sstevel@tonic-gate 	}
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	if (rval == NULL) {
18257c478bd9Sstevel@tonic-gate 		/*
18267c478bd9Sstevel@tonic-gate 		 * This control sequence is corrupt, as it is not terminated by
18277c478bd9Sstevel@tonic-gate 		 * a system privileged control value.
18287c478bd9Sstevel@tonic-gate 		 */
18297c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
18307c478bd9Sstevel@tonic-gate 		return (-1);
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	/*
18347c478bd9Sstevel@tonic-gate 	 * 4.  If we run into a RCPRIV_SYSTEM value, then the hard limit (and
18357c478bd9Sstevel@tonic-gate 	 * the soft, if we haven't a soft candidate) should be the value of the
18367c478bd9Sstevel@tonic-gate 	 * system control value.
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 	if ((rval->rcv_flagaction & RCTL_LOCAL_MAXIMAL) == 0 &&
18397c478bd9Sstevel@tonic-gate 	    rval->rcv_value < rctl_model_maximum(rctl->rc_dict_entry, p))
18407c478bd9Sstevel@tonic-gate 		rlp64->rlim_max = rval->rcv_value;
18417c478bd9Sstevel@tonic-gate 	else
18427c478bd9Sstevel@tonic-gate 		rlp64->rlim_max = RLIM64_INFINITY;
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	if (!soft_limit_seen)
18457c478bd9Sstevel@tonic-gate 		rlp64->rlim_cur = rlp64->rlim_max;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
18487c478bd9Sstevel@tonic-gate 	return (0);
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate /*
18527c478bd9Sstevel@tonic-gate  * rctl_alloc_gp_t *rctl_rlimit_set_prealloc(uint_t)
18537c478bd9Sstevel@tonic-gate  *
18547c478bd9Sstevel@tonic-gate  * Overview
18557c478bd9Sstevel@tonic-gate  *   Before making a series of calls to rctl_rlimit_set(), we must have a
18567c478bd9Sstevel@tonic-gate  *   preallocated batch of resource control values, as rctl_rlimit_set() can
18577c478bd9Sstevel@tonic-gate  *   potentially consume two resource control values per call.
18587c478bd9Sstevel@tonic-gate  *
18597c478bd9Sstevel@tonic-gate  * Return values
18607c478bd9Sstevel@tonic-gate  *   A populated resource control allocation group with 2n resource control
18617c478bd9Sstevel@tonic-gate  *   values.
18627c478bd9Sstevel@tonic-gate  *
18637c478bd9Sstevel@tonic-gate  * Caller's context
18647c478bd9Sstevel@tonic-gate  *   Must be safe for KM_SLEEP allocations.
18657c478bd9Sstevel@tonic-gate  */
18667c478bd9Sstevel@tonic-gate rctl_alloc_gp_t *
18677c478bd9Sstevel@tonic-gate rctl_rlimit_set_prealloc(uint_t n)
18687c478bd9Sstevel@tonic-gate {
18697c478bd9Sstevel@tonic-gate 	rctl_alloc_gp_t *gp = kmem_zalloc(sizeof (rctl_alloc_gp_t), KM_SLEEP);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&curproc->p_lock));
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 	gp->rcag_nvals = 2 * n;
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	rctl_gp_alloc(gp);
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	return (gp);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate /*
18817c478bd9Sstevel@tonic-gate  * int rctl_rlimit_set(rctl_hndl_t, struct proc *, struct rlimit64 *, int,
18827c478bd9Sstevel@tonic-gate  *   int)
18837c478bd9Sstevel@tonic-gate  *
18847c478bd9Sstevel@tonic-gate  * Overview
18857c478bd9Sstevel@tonic-gate  *   To support rlimit compatibility, we need a function which takes a 64-bit
18867c478bd9Sstevel@tonic-gate  *   rlimit and encodes it as appropriate rcontrol values on the given rcontrol.
18877c478bd9Sstevel@tonic-gate  *   This operation is only intended for legacy rlimits.
18887c478bd9Sstevel@tonic-gate  *
18897c478bd9Sstevel@tonic-gate  *   The implementation of rctl_rlimit_set() is a bit clever, as it tries to
18907c478bd9Sstevel@tonic-gate  *   minimize the number of values placed on the value sequence in various
18917c478bd9Sstevel@tonic-gate  *   cases.  Furthermore, we don't allow multiple identical privilege-action
18927c478bd9Sstevel@tonic-gate  *   values on the same sequence.  (That is, we don't want a sequence like
18937c478bd9Sstevel@tonic-gate  *   "while (1) { rlim.rlim_cur++; setrlimit(..., rlim); }" to exhaust kernel
18947c478bd9Sstevel@tonic-gate  *   memory.)  So we want to delete any values with the same privilege value and
18957c478bd9Sstevel@tonic-gate  *   action.
18967c478bd9Sstevel@tonic-gate  *
18977c478bd9Sstevel@tonic-gate  * Return values
18987c478bd9Sstevel@tonic-gate  *   0 for successful set, errno otherwise. Errno will be either EINVAL
18997c478bd9Sstevel@tonic-gate  *   or EPERM, in keeping with defined errnos for ulimit() and setrlimit()
19007c478bd9Sstevel@tonic-gate  *   system calls.
19017c478bd9Sstevel@tonic-gate  */
19027c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19037c478bd9Sstevel@tonic-gate int
19047c478bd9Sstevel@tonic-gate rctl_rlimit_set(rctl_hndl_t rc, struct proc *p, struct rlimit64 *rlp64,
19057c478bd9Sstevel@tonic-gate     rctl_alloc_gp_t *ragp, int flagaction, int signal, const cred_t *cr)
19067c478bd9Sstevel@tonic-gate {
19077c478bd9Sstevel@tonic-gate 	rctl_t *rctl;
19087c478bd9Sstevel@tonic-gate 	rctl_val_t *rval, *rval_priv, *rval_basic;
19097c478bd9Sstevel@tonic-gate 	rctl_set_t *rset = p->p_rctls;
19107c478bd9Sstevel@tonic-gate 	rctl_qty_t max;
19117c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e;
19127c478bd9Sstevel@tonic-gate 	struct rlimit64 cur_rl;
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	e.rcep_t = RCENTITY_PROCESS;
19157c478bd9Sstevel@tonic-gate 	e.rcep_p.proc = p;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 	if (rlp64->rlim_cur > rlp64->rlim_max)
19187c478bd9Sstevel@tonic-gate 		return (EINVAL);
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	if (rctl_rlimit_get(rc, p, &cur_rl) == -1)
19217c478bd9Sstevel@tonic-gate 		return (EINVAL);
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	/*
19247c478bd9Sstevel@tonic-gate 	 * If we are not privileged, we can only lower the hard limit.
19257c478bd9Sstevel@tonic-gate 	 */
19267c478bd9Sstevel@tonic-gate 	if ((rlp64->rlim_max > cur_rl.rlim_max) &&
19277c478bd9Sstevel@tonic-gate 	    cur_rl.rlim_max != RLIM64_INFINITY &&
19287c478bd9Sstevel@tonic-gate 	    secpolicy_resource(cr) != 0)
19297c478bd9Sstevel@tonic-gate 		return (EPERM);
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, rc, &rctl) == -1) {
19347c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
19357c478bd9Sstevel@tonic-gate 		return (EINVAL);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	rval_priv = rctl_gp_detach_val(ragp);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	rval = rctl->rc_values;
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	while (rval != NULL) {
19437c478bd9Sstevel@tonic-gate 		rctl_val_t *next = rval->rcv_next;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 		if (rval->rcv_privilege == RCPRIV_SYSTEM)
19467c478bd9Sstevel@tonic-gate 			break;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 		if ((rval->rcv_privilege == RCPRIV_BASIC) ||
19497c478bd9Sstevel@tonic-gate 		    (rval->rcv_flagaction & ~RCTL_LOCAL_ACTION_MASK) ==
19507c478bd9Sstevel@tonic-gate 		    (flagaction & ~RCTL_LOCAL_ACTION_MASK)) {
19517c478bd9Sstevel@tonic-gate 			if (rctl->rc_cursor == rval) {
19527c478bd9Sstevel@tonic-gate 				rctl->rc_cursor = rval->rcv_next;
19537c478bd9Sstevel@tonic-gate 				rctl_val_list_reset(rctl->rc_cursor);
19547c478bd9Sstevel@tonic-gate 				RCTLOP_SET(rctl, p, &e, rctl_model_value(
19557c478bd9Sstevel@tonic-gate 				    rctl->rc_dict_entry, p,
19567c478bd9Sstevel@tonic-gate 				    rctl->rc_cursor->rcv_value));
19577c478bd9Sstevel@tonic-gate 			}
19587c478bd9Sstevel@tonic-gate 			(void) rctl_val_list_delete(&rctl->rc_values, rval);
19597c478bd9Sstevel@tonic-gate 		}
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 		rval = next;
19627c478bd9Sstevel@tonic-gate 	}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 	rval_priv->rcv_privilege = RCPRIV_PRIVILEGED;
19657c478bd9Sstevel@tonic-gate 	rval_priv->rcv_flagaction = flagaction;
19667c478bd9Sstevel@tonic-gate 	if (rlp64->rlim_max == RLIM64_INFINITY) {
19677c478bd9Sstevel@tonic-gate 		rval_priv->rcv_flagaction |= RCTL_LOCAL_MAXIMAL;
19687c478bd9Sstevel@tonic-gate 		max = rctl->rc_dict_entry->rcd_max_native;
19697c478bd9Sstevel@tonic-gate 	} else {
19707c478bd9Sstevel@tonic-gate 		max = rlp64->rlim_max;
19717c478bd9Sstevel@tonic-gate 	}
19727c478bd9Sstevel@tonic-gate 	rval_priv->rcv_value = max;
19737c478bd9Sstevel@tonic-gate 	rval_priv->rcv_action_signal = signal;
19747c478bd9Sstevel@tonic-gate 	rval_priv->rcv_action_recipient = NULL;
19757c478bd9Sstevel@tonic-gate 	rval_priv->rcv_action_recip_pid = -1;
19767c478bd9Sstevel@tonic-gate 	rval_priv->rcv_firing_time = 0;
19777c478bd9Sstevel@tonic-gate 	rval_priv->rcv_prev = rval_priv->rcv_next = NULL;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	(void) rctl_val_list_insert(&rctl->rc_values, rval_priv);
19807c478bd9Sstevel@tonic-gate 	rctl->rc_cursor = rval_priv;
19817c478bd9Sstevel@tonic-gate 	rctl_val_list_reset(rctl->rc_cursor);
19827c478bd9Sstevel@tonic-gate 	RCTLOP_SET(rctl, p, &e, rctl_model_value(rctl->rc_dict_entry, p,
19837c478bd9Sstevel@tonic-gate 	    rctl->rc_cursor->rcv_value));
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	if (rlp64->rlim_cur != RLIM64_INFINITY && rlp64->rlim_cur < max) {
19867c478bd9Sstevel@tonic-gate 		rval_basic = rctl_gp_detach_val(ragp);
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 		rval_basic->rcv_privilege = RCPRIV_BASIC;
19897c478bd9Sstevel@tonic-gate 		rval_basic->rcv_value = rlp64->rlim_cur;
19907c478bd9Sstevel@tonic-gate 		rval_basic->rcv_flagaction = flagaction;
19917c478bd9Sstevel@tonic-gate 		rval_basic->rcv_action_signal = signal;
19927c478bd9Sstevel@tonic-gate 		rval_basic->rcv_action_recipient = p;
19937c478bd9Sstevel@tonic-gate 		rval_basic->rcv_action_recip_pid = p->p_pid;
19947c478bd9Sstevel@tonic-gate 		rval_basic->rcv_firing_time = 0;
19957c478bd9Sstevel@tonic-gate 		rval_basic->rcv_prev = rval_basic->rcv_next = NULL;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 		(void) rctl_val_list_insert(&rctl->rc_values, rval_basic);
19987c478bd9Sstevel@tonic-gate 		rctl->rc_cursor = rval_basic;
19997c478bd9Sstevel@tonic-gate 		rctl_val_list_reset(rctl->rc_cursor);
20007c478bd9Sstevel@tonic-gate 		RCTLOP_SET(rctl, p, &e, rctl_model_value(rctl->rc_dict_entry, p,
20017c478bd9Sstevel@tonic-gate 		    rctl->rc_cursor->rcv_value));
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	ASSERT(rctl->rc_cursor != NULL);
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
20077c478bd9Sstevel@tonic-gate 	return (0);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate /*
20127c478bd9Sstevel@tonic-gate  * rctl_hndl_t rctl_register(const char *, rctl_entity_t, int, rlim64_t,
20137c478bd9Sstevel@tonic-gate  *   rlim64_t, rctl_ops_t *)
20147c478bd9Sstevel@tonic-gate  *
20157c478bd9Sstevel@tonic-gate  * Overview
20167c478bd9Sstevel@tonic-gate  *   rctl_register() performs a look-up in the dictionary of rctls
20177c478bd9Sstevel@tonic-gate  *   active on the system; if a rctl of that name is absent, an entry is
20187c478bd9Sstevel@tonic-gate  *   made into the dictionary.  The rctl is returned with its reference
20197c478bd9Sstevel@tonic-gate  *   count incremented by one.  If the rctl name already exists, we panic.
20207c478bd9Sstevel@tonic-gate  *   (Were the resource control system to support dynamic loading and unloading,
20217c478bd9Sstevel@tonic-gate  *   which it is structured for, duplicate registration should lead to load
20227c478bd9Sstevel@tonic-gate  *   failure instead of panicking.)
20237c478bd9Sstevel@tonic-gate  *
20247c478bd9Sstevel@tonic-gate  *   Each registered rctl has a requirement that a RCPRIV_SYSTEM limit be
20257c478bd9Sstevel@tonic-gate  *   defined.  This limit contains the highest possible value for this quantity
20267c478bd9Sstevel@tonic-gate  *   on the system.  Furthermore, the registered control must provide infinite
20277c478bd9Sstevel@tonic-gate  *   values for all applicable address space models supported by the operating
20287c478bd9Sstevel@tonic-gate  *   system.  Attempts to set resource control values beyond the system limit
20297c478bd9Sstevel@tonic-gate  *   will fail.
20307c478bd9Sstevel@tonic-gate  *
20317c478bd9Sstevel@tonic-gate  * Return values
20327c478bd9Sstevel@tonic-gate  *   The rctl's ID.
20337c478bd9Sstevel@tonic-gate  *
20347c478bd9Sstevel@tonic-gate  * Caller's context
20357c478bd9Sstevel@tonic-gate  *   Caller must be in a context suitable for KM_SLEEP allocations.
20367c478bd9Sstevel@tonic-gate  */
20377c478bd9Sstevel@tonic-gate rctl_hndl_t
20387c478bd9Sstevel@tonic-gate rctl_register(
20397c478bd9Sstevel@tonic-gate     const char *name,
20407c478bd9Sstevel@tonic-gate     rctl_entity_t entity,
20417c478bd9Sstevel@tonic-gate     int global_flags,
20427c478bd9Sstevel@tonic-gate     rlim64_t max_native,
20437c478bd9Sstevel@tonic-gate     rlim64_t max_ilp32,
20447c478bd9Sstevel@tonic-gate     rctl_ops_t *ops)
20457c478bd9Sstevel@tonic-gate {
20467c478bd9Sstevel@tonic-gate 	rctl_t *rctl = kmem_cache_alloc(rctl_cache, KM_SLEEP);
20477c478bd9Sstevel@tonic-gate 	rctl_val_t *rctl_val = kmem_cache_alloc(rctl_val_cache, KM_SLEEP);
20487c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rctl_de = kmem_zalloc(sizeof (rctl_dict_entry_t),
20497c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
20507c478bd9Sstevel@tonic-gate 	rctl_t *old_rctl;
20517c478bd9Sstevel@tonic-gate 	rctl_hndl_t rhndl;
20527c478bd9Sstevel@tonic-gate 	int localflags;
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	ASSERT(ops != NULL);
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	bzero(rctl, sizeof (rctl_t));
20577c478bd9Sstevel@tonic-gate 	bzero(rctl_val, sizeof (rctl_val_t));
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if (global_flags & RCTL_GLOBAL_DENY_NEVER)
20607c478bd9Sstevel@tonic-gate 		localflags = RCTL_LOCAL_MAXIMAL;
20617c478bd9Sstevel@tonic-gate 	else
20627c478bd9Sstevel@tonic-gate 		localflags = RCTL_LOCAL_MAXIMAL | RCTL_LOCAL_DENY;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	rctl_val->rcv_privilege = RCPRIV_SYSTEM;
20657c478bd9Sstevel@tonic-gate 	rctl_val->rcv_value = max_native;
20667c478bd9Sstevel@tonic-gate 	rctl_val->rcv_flagaction = localflags;
20677c478bd9Sstevel@tonic-gate 	rctl_val->rcv_action_signal = 0;
20687c478bd9Sstevel@tonic-gate 	rctl_val->rcv_action_recipient = NULL;
20697c478bd9Sstevel@tonic-gate 	rctl_val->rcv_action_recip_pid = -1;
20707c478bd9Sstevel@tonic-gate 	rctl_val->rcv_firing_time = 0;
20717c478bd9Sstevel@tonic-gate 	rctl_val->rcv_next = NULL;
20727c478bd9Sstevel@tonic-gate 	rctl_val->rcv_prev = NULL;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	rctl_de->rcd_name = (char *)name;
20757c478bd9Sstevel@tonic-gate 	rctl_de->rcd_default_value = rctl_val;
20767c478bd9Sstevel@tonic-gate 	rctl_de->rcd_max_native = max_native;
20777c478bd9Sstevel@tonic-gate 	rctl_de->rcd_max_ilp32 = max_ilp32;
20787c478bd9Sstevel@tonic-gate 	rctl_de->rcd_entity = entity;
20797c478bd9Sstevel@tonic-gate 	rctl_de->rcd_ops = ops;
20807c478bd9Sstevel@tonic-gate 	rctl_de->rcd_flagaction = global_flags;
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 	rctl->rc_dict_entry = rctl_de;
20837c478bd9Sstevel@tonic-gate 	rctl->rc_values = rctl_val;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	/*
20867c478bd9Sstevel@tonic-gate 	 * 1.  Take global lock, validate nonexistence of name, get ID.
20877c478bd9Sstevel@tonic-gate 	 */
20887c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_dict_lock);
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict_by_name, (mod_hash_key_t)name,
20917c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&rhndl) != MH_ERR_NOTFOUND)
20927c478bd9Sstevel@tonic-gate 		panic("duplicate registration of rctl %s", name);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	rhndl = rctl_de->rcd_id = rctl->rc_id =
20957c478bd9Sstevel@tonic-gate 	    (rctl_hndl_t)id_alloc(rctl_ids);
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	/*
20987c478bd9Sstevel@tonic-gate 	 * 2.  Insert name-entry pair in rctl_dict_by_name.
20997c478bd9Sstevel@tonic-gate 	 */
21007c478bd9Sstevel@tonic-gate 	if (mod_hash_insert(rctl_dict_by_name, (mod_hash_key_t)name,
21017c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)rctl_de))
21027c478bd9Sstevel@tonic-gate 		panic("unable to insert rctl dict entry for %s (%u)", name,
21037c478bd9Sstevel@tonic-gate 		    (uint_t)rctl->rc_id);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	/*
21067c478bd9Sstevel@tonic-gate 	 * 3.  Insert ID-rctl_t * pair in rctl_dict.
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 	if (mod_hash_find(rctl_dict, (mod_hash_key_t)(uintptr_t)rctl->rc_id,
21097c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&old_rctl) != MH_ERR_NOTFOUND)
21107c478bd9Sstevel@tonic-gate 		panic("duplicate rctl ID %u registered", rctl->rc_id);
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 	if (mod_hash_insert(rctl_dict, (mod_hash_key_t)(uintptr_t)rctl->rc_id,
21137c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)rctl))
21147c478bd9Sstevel@tonic-gate 		panic("unable to insert rctl %s/%u (%p)", name,
21157c478bd9Sstevel@tonic-gate 		    (uint_t)rctl->rc_id, rctl);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	/*
21187c478bd9Sstevel@tonic-gate 	 * 3a. Insert rctl_dict_entry_t * in appropriate entity list.
21197c478bd9Sstevel@tonic-gate 	 */
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	mutex_enter(&rctl_lists_lock);
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	switch (entity) {
21247c478bd9Sstevel@tonic-gate 	case RCENTITY_ZONE:
21257c478bd9Sstevel@tonic-gate 	case RCENTITY_PROJECT:
21267c478bd9Sstevel@tonic-gate 	case RCENTITY_TASK:
21277c478bd9Sstevel@tonic-gate 	case RCENTITY_PROCESS:
21287c478bd9Sstevel@tonic-gate 		rctl_de->rcd_next = rctl_lists[entity];
21297c478bd9Sstevel@tonic-gate 		rctl_lists[entity] = rctl_de;
21307c478bd9Sstevel@tonic-gate 		break;
21317c478bd9Sstevel@tonic-gate 	default:
21327c478bd9Sstevel@tonic-gate 		panic("registering unknown rctl entity %d (%s)", entity,
21337c478bd9Sstevel@tonic-gate 		    name);
21347c478bd9Sstevel@tonic-gate 		break;
21357c478bd9Sstevel@tonic-gate 	}
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_lists_lock);
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate 	/*
21407c478bd9Sstevel@tonic-gate 	 * 4.  Drop lock.
21417c478bd9Sstevel@tonic-gate 	 */
21427c478bd9Sstevel@tonic-gate 	mutex_exit(&rctl_dict_lock);
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	return (rhndl);
21457c478bd9Sstevel@tonic-gate }
21467c478bd9Sstevel@tonic-gate 
21477c478bd9Sstevel@tonic-gate /*
21487c478bd9Sstevel@tonic-gate  * static int rctl_global_action(rctl_t *r, rctl_set_t *rset, struct proc *p,
21497c478bd9Sstevel@tonic-gate  *    rctl_val_t *v)
21507c478bd9Sstevel@tonic-gate  *
21517c478bd9Sstevel@tonic-gate  * Overview
21527c478bd9Sstevel@tonic-gate  *   rctl_global_action() takes, in according with the flags on the rctl_dict
21537c478bd9Sstevel@tonic-gate  *   entry for the given control, the appropriate actions on the exceeded
21547c478bd9Sstevel@tonic-gate  *   control value.  Additionally, rctl_global_action() updates the firing time
21557c478bd9Sstevel@tonic-gate  *   on the exceeded value.
21567c478bd9Sstevel@tonic-gate  *
21577c478bd9Sstevel@tonic-gate  * Return values
21587c478bd9Sstevel@tonic-gate  *   A bitmask reflecting the actions actually taken.
21597c478bd9Sstevel@tonic-gate  *
21607c478bd9Sstevel@tonic-gate  * Caller's context
21617c478bd9Sstevel@tonic-gate  *   No restrictions on context.
21627c478bd9Sstevel@tonic-gate  */
21637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21647c478bd9Sstevel@tonic-gate static int
21657c478bd9Sstevel@tonic-gate rctl_global_action(rctl_t *r, rctl_set_t *rset, struct proc *p, rctl_val_t *v)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde = r->rc_dict_entry;
2168aa4a4f3bSnf202958 	const char *pr, *en, *idstr;
21697c478bd9Sstevel@tonic-gate 	id_t id;
2170aa4a4f3bSnf202958 	enum {
2171aa4a4f3bSnf202958 		SUFFIX_NONE,	/* id consumed directly */
2172aa4a4f3bSnf202958 		SUFFIX_NUMERIC,	/* id consumed in suffix */
2173aa4a4f3bSnf202958 		SUFFIX_STRING	/* idstr consumed in suffix */
2174aa4a4f3bSnf202958 	} suffix = SUFFIX_NONE;
21757c478bd9Sstevel@tonic-gate 	int ret = 0;
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate 	v->rcv_firing_time = gethrtime();
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	switch (v->rcv_privilege) {
21807c478bd9Sstevel@tonic-gate 	case RCPRIV_BASIC:
21817c478bd9Sstevel@tonic-gate 		pr = "basic";
21827c478bd9Sstevel@tonic-gate 		break;
21837c478bd9Sstevel@tonic-gate 	case RCPRIV_PRIVILEGED:
21847c478bd9Sstevel@tonic-gate 		pr = "privileged";
21857c478bd9Sstevel@tonic-gate 		break;
21867c478bd9Sstevel@tonic-gate 	case RCPRIV_SYSTEM:
21877c478bd9Sstevel@tonic-gate 		pr = "system";
21887c478bd9Sstevel@tonic-gate 		break;
21897c478bd9Sstevel@tonic-gate 	default:
21907c478bd9Sstevel@tonic-gate 		pr = "unknown";
21917c478bd9Sstevel@tonic-gate 		break;
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	switch (rde->rcd_entity) {
21957c478bd9Sstevel@tonic-gate 	case RCENTITY_PROCESS:
21967c478bd9Sstevel@tonic-gate 		en = "process";
21977c478bd9Sstevel@tonic-gate 		id = p->p_pid;
2198aa4a4f3bSnf202958 		suffix = SUFFIX_NONE;
21997c478bd9Sstevel@tonic-gate 		break;
22007c478bd9Sstevel@tonic-gate 	case RCENTITY_TASK:
22017c478bd9Sstevel@tonic-gate 		en = "task";
22027c478bd9Sstevel@tonic-gate 		id = p->p_task->tk_tkid;
2203aa4a4f3bSnf202958 		suffix = SUFFIX_NUMERIC;
22047c478bd9Sstevel@tonic-gate 		break;
22057c478bd9Sstevel@tonic-gate 	case RCENTITY_PROJECT:
22067c478bd9Sstevel@tonic-gate 		en = "project";
22077c478bd9Sstevel@tonic-gate 		id = p->p_task->tk_proj->kpj_id;
2208aa4a4f3bSnf202958 		suffix = SUFFIX_NUMERIC;
22097c478bd9Sstevel@tonic-gate 		break;
22107c478bd9Sstevel@tonic-gate 	case RCENTITY_ZONE:
22117c478bd9Sstevel@tonic-gate 		en = "zone";
2212aa4a4f3bSnf202958 		idstr = p->p_zone->zone_name;
2213aa4a4f3bSnf202958 		suffix = SUFFIX_STRING;
22147c478bd9Sstevel@tonic-gate 		break;
22157c478bd9Sstevel@tonic-gate 	default:
2216aa4a4f3bSnf202958 		en = "unknown entity associated with process";
22177c478bd9Sstevel@tonic-gate 		id = p->p_pid;
2218aa4a4f3bSnf202958 		suffix = SUFFIX_NONE;
22197c478bd9Sstevel@tonic-gate 		break;
22207c478bd9Sstevel@tonic-gate 	}
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	if (rde->rcd_flagaction & RCTL_GLOBAL_SYSLOG) {
2223aa4a4f3bSnf202958 		switch (suffix) {
2224aa4a4f3bSnf202958 		default:
2225aa4a4f3bSnf202958 		case SUFFIX_NONE:
22267c478bd9Sstevel@tonic-gate 			(void) strlog(0, 0, 0,
22277c478bd9Sstevel@tonic-gate 			    rde->rcd_strlog_flags | log_global.lz_active,
2228aa4a4f3bSnf202958 			    "%s rctl %s (value %llu) exceeded by %s %d.",
2229aa4a4f3bSnf202958 			    pr, rde->rcd_name, v->rcv_value, en, id);
2230aa4a4f3bSnf202958 			break;
2231aa4a4f3bSnf202958 		case SUFFIX_NUMERIC:
2232aa4a4f3bSnf202958 			(void) strlog(0, 0, 0,
2233aa4a4f3bSnf202958 			    rde->rcd_strlog_flags | log_global.lz_active,
2234aa4a4f3bSnf202958 			    "%s rctl %s (value %llu) exceeded by process %d"
2235aa4a4f3bSnf202958 			    " in %s %d.",
2236aa4a4f3bSnf202958 			    pr, rde->rcd_name, v->rcv_value, p->p_pid,
2237aa4a4f3bSnf202958 			    en, id);
2238aa4a4f3bSnf202958 			break;
2239aa4a4f3bSnf202958 		case SUFFIX_STRING:
2240aa4a4f3bSnf202958 			(void) strlog(0, 0, 0,
2241aa4a4f3bSnf202958 			    rde->rcd_strlog_flags | log_global.lz_active,
2242aa4a4f3bSnf202958 			    "%s rctl %s (value %llu) exceeded by process %d"
2243aa4a4f3bSnf202958 			    " in %s %s.",
2244aa4a4f3bSnf202958 			    pr, rde->rcd_name, v->rcv_value, p->p_pid,
2245aa4a4f3bSnf202958 			    en, idstr);
2246aa4a4f3bSnf202958 			break;
2247aa4a4f3bSnf202958 		}
22487c478bd9Sstevel@tonic-gate 	}
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	if (rde->rcd_flagaction & RCTL_GLOBAL_DENY_ALWAYS)
22517c478bd9Sstevel@tonic-gate 		ret |= RCT_DENY;
22527c478bd9Sstevel@tonic-gate 
22537c478bd9Sstevel@tonic-gate 	return (ret);
22547c478bd9Sstevel@tonic-gate }
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate static int
22577c478bd9Sstevel@tonic-gate rctl_local_action(rctl_t *r, rctl_set_t *rset, struct proc *p, rctl_val_t *v,
22587c478bd9Sstevel@tonic-gate     uint_t safety)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	int ret = 0;
22617c478bd9Sstevel@tonic-gate 	sigqueue_t *sqp = NULL;
22627c478bd9Sstevel@tonic-gate 	rctl_dict_entry_t *rde = r->rc_dict_entry;
22637c478bd9Sstevel@tonic-gate 	int unobservable = (rde->rcd_flagaction & RCTL_GLOBAL_UNOBSERVABLE);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	proc_t *recipient = v->rcv_action_recipient;
22667c478bd9Sstevel@tonic-gate 	id_t recip_pid = v->rcv_action_recip_pid;
22677c478bd9Sstevel@tonic-gate 	int recip_signal = v->rcv_action_signal;
22687c478bd9Sstevel@tonic-gate 	uint_t flagaction = v->rcv_flagaction;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	if (safety == RCA_UNSAFE_ALL) {
22717c478bd9Sstevel@tonic-gate 		if (flagaction & RCTL_LOCAL_DENY) {
22727c478bd9Sstevel@tonic-gate 			ret |= RCT_DENY;
22737c478bd9Sstevel@tonic-gate 		}
22747c478bd9Sstevel@tonic-gate 		return (ret);
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	if (flagaction & RCTL_LOCAL_SIGNAL) {
22787c478bd9Sstevel@tonic-gate 		/*
22797c478bd9Sstevel@tonic-gate 		 * We can build a siginfo only in the case that it is
22807c478bd9Sstevel@tonic-gate 		 * safe for us to drop p_lock.  (For asynchronous
22817c478bd9Sstevel@tonic-gate 		 * checks this is currently not true.)
22827c478bd9Sstevel@tonic-gate 		 */
22837c478bd9Sstevel@tonic-gate 		if (safety == RCA_SAFE) {
22847c478bd9Sstevel@tonic-gate 			mutex_exit(&rset->rcs_lock);
22857c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
22867c478bd9Sstevel@tonic-gate 			sqp = kmem_zalloc(sizeof (sigqueue_t), KM_SLEEP);
22877c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
22887c478bd9Sstevel@tonic-gate 			mutex_enter(&rset->rcs_lock);
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 			sqp->sq_info.si_signo = recip_signal;
22917c478bd9Sstevel@tonic-gate 			sqp->sq_info.si_code = SI_RCTL;
22927c478bd9Sstevel@tonic-gate 			sqp->sq_info.si_errno = 0;
22937c478bd9Sstevel@tonic-gate 			sqp->sq_info.si_entity = (int)rde->rcd_entity;
22947c478bd9Sstevel@tonic-gate 		}
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 		if (recipient == NULL || recipient == p) {
22977c478bd9Sstevel@tonic-gate 			ret |= RCT_SIGNAL;
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 			if (sqp == NULL) {
23007c478bd9Sstevel@tonic-gate 				sigtoproc(p, NULL, recip_signal);
23017c478bd9Sstevel@tonic-gate 			} else if (p == curproc) {
23027c478bd9Sstevel@tonic-gate 				/*
23037c478bd9Sstevel@tonic-gate 				 * Then this is a synchronous test and we can
23047c478bd9Sstevel@tonic-gate 				 * direct the signal at the violating thread.
23057c478bd9Sstevel@tonic-gate 				 */
23067c478bd9Sstevel@tonic-gate 				sigaddqa(curproc, curthread, sqp);
23077c478bd9Sstevel@tonic-gate 			} else {
23087c478bd9Sstevel@tonic-gate 				sigaddqa(p, NULL, sqp);
23097c478bd9Sstevel@tonic-gate 			}
23107c478bd9Sstevel@tonic-gate 		} else if (!unobservable) {
23117c478bd9Sstevel@tonic-gate 			proc_t *rp;
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 			mutex_exit(&rset->rcs_lock);
23147c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 			mutex_enter(&pidlock);
23177c478bd9Sstevel@tonic-gate 			if ((rp = prfind(recip_pid)) == recipient) {
23187c478bd9Sstevel@tonic-gate 				/*
23197c478bd9Sstevel@tonic-gate 				 * Recipient process is still alive, but may not
23207c478bd9Sstevel@tonic-gate 				 * be in this task or project any longer.  In
23217c478bd9Sstevel@tonic-gate 				 * this case, the recipient's resource control
23227c478bd9Sstevel@tonic-gate 				 * set pertinent to this control will have
23237c478bd9Sstevel@tonic-gate 				 * changed--and we will not deliver the signal,
23247c478bd9Sstevel@tonic-gate 				 * as the recipient process is trying to tear
23257c478bd9Sstevel@tonic-gate 				 * itself off of its former set.
23267c478bd9Sstevel@tonic-gate 				 */
23277c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->p_lock);
23287c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 				if (rctl_entity_obtain_rset(rde, rp) == rset) {
23317c478bd9Sstevel@tonic-gate 					ret |= RCT_SIGNAL;
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 					if (sqp == NULL)
23347c478bd9Sstevel@tonic-gate 						sigtoproc(rp, NULL,
23357c478bd9Sstevel@tonic-gate 						    recip_signal);
23367c478bd9Sstevel@tonic-gate 					else
23377c478bd9Sstevel@tonic-gate 						sigaddqa(rp, NULL, sqp);
23387c478bd9Sstevel@tonic-gate 				} else if (sqp) {
23397c478bd9Sstevel@tonic-gate 					kmem_free(sqp, sizeof (sigqueue_t));
23407c478bd9Sstevel@tonic-gate 				}
23417c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->p_lock);
23427c478bd9Sstevel@tonic-gate 			} else {
23437c478bd9Sstevel@tonic-gate 				mutex_exit(&pidlock);
23447c478bd9Sstevel@tonic-gate 				if (sqp)
23457c478bd9Sstevel@tonic-gate 					kmem_free(sqp, sizeof (sigqueue_t));
23467c478bd9Sstevel@tonic-gate 			}
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
23497c478bd9Sstevel@tonic-gate 			/*
23507c478bd9Sstevel@tonic-gate 			 * Since we dropped p_lock, we may no longer be in the
23517c478bd9Sstevel@tonic-gate 			 * same task or project as we were at entry.  It is thus
23527c478bd9Sstevel@tonic-gate 			 * unsafe for us to reacquire the set lock at this
23537c478bd9Sstevel@tonic-gate 			 * point; callers of rctl_local_action() must handle
23547c478bd9Sstevel@tonic-gate 			 * this possibility.
23557c478bd9Sstevel@tonic-gate 			 */
23567c478bd9Sstevel@tonic-gate 			ret |= RCT_LK_ABANDONED;
23577c478bd9Sstevel@tonic-gate 		} else if (sqp) {
23587c478bd9Sstevel@tonic-gate 			kmem_free(sqp, sizeof (sigqueue_t));
23597c478bd9Sstevel@tonic-gate 		}
23607c478bd9Sstevel@tonic-gate 	}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 	if ((flagaction & RCTL_LOCAL_DENY) &&
23637c478bd9Sstevel@tonic-gate 	    (recipient == NULL || recipient == p)) {
23647c478bd9Sstevel@tonic-gate 		ret |= RCT_DENY;
23657c478bd9Sstevel@tonic-gate 	}
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 	return (ret);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate  * int rctl_action(rctl_hndl_t, rctl_set_t *, struct proc *, uint_t)
23727c478bd9Sstevel@tonic-gate  *
23737c478bd9Sstevel@tonic-gate  * Overview
23747c478bd9Sstevel@tonic-gate  *   Take the action associated with the enforced value (as defined by
23757c478bd9Sstevel@tonic-gate  *   rctl_get_enforced_value()) being exceeded or encountered.  Possibly perform
23767c478bd9Sstevel@tonic-gate  *   a restricted subset of the available actions, if circumstances dictate that
23777c478bd9Sstevel@tonic-gate  *   we cannot safely allocate memory (for a sigqueue_t) or guarantee process
23787c478bd9Sstevel@tonic-gate  *   persistence across the duration of the function (an asynchronous action).
23797c478bd9Sstevel@tonic-gate  *
23807c478bd9Sstevel@tonic-gate  * Return values
23817c478bd9Sstevel@tonic-gate  *   Actions taken, according to the rctl_test bitmask.
23827c478bd9Sstevel@tonic-gate  *
23837c478bd9Sstevel@tonic-gate  * Caller's context
23847c478bd9Sstevel@tonic-gate  *   Safe to acquire rcs_lock.
23857c478bd9Sstevel@tonic-gate  */
23867c478bd9Sstevel@tonic-gate int
23877c478bd9Sstevel@tonic-gate rctl_action(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p, uint_t safety)
23887c478bd9Sstevel@tonic-gate {
23897c478bd9Sstevel@tonic-gate 	return (rctl_action_entity(hndl, rset, p, NULL, safety));
23907c478bd9Sstevel@tonic-gate }
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate int
23937c478bd9Sstevel@tonic-gate rctl_action_entity(rctl_hndl_t hndl, rctl_set_t *rset, struct proc *p,
23947c478bd9Sstevel@tonic-gate     rctl_entity_p_t *e, uint_t safety)
23957c478bd9Sstevel@tonic-gate {
23967c478bd9Sstevel@tonic-gate 	int ret = RCT_NONE;
23977c478bd9Sstevel@tonic-gate 	rctl_t *lrctl;
23987c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e_tmp;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate rctl_action_acquire:
24017c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
24027c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, hndl, &lrctl) == -1) {
24037c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
24047c478bd9Sstevel@tonic-gate 		return (ret);
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	if (e == NULL) {
24087c478bd9Sstevel@tonic-gate 		rctl_entity_obtain_entity_p(lrctl->rc_dict_entry->rcd_entity,
24097c478bd9Sstevel@tonic-gate 		p, &e_tmp);
24107c478bd9Sstevel@tonic-gate 		e = &e_tmp;
24117c478bd9Sstevel@tonic-gate 	}
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	if ((ret & RCT_LK_ABANDONED) == 0) {
24147c478bd9Sstevel@tonic-gate 		ret |= rctl_global_action(lrctl, rset, p, lrctl->rc_cursor);
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 		RCTLOP_ACTION(lrctl, p, e);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 		ret |= rctl_local_action(lrctl, rset, p,
24197c478bd9Sstevel@tonic-gate 		    lrctl->rc_cursor, safety);
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate 		if (ret & RCT_LK_ABANDONED)
24227c478bd9Sstevel@tonic-gate 			goto rctl_action_acquire;
24237c478bd9Sstevel@tonic-gate 	}
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	ret &= ~RCT_LK_ABANDONED;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	if (!(ret & RCT_DENY) &&
24287c478bd9Sstevel@tonic-gate 	    lrctl->rc_cursor->rcv_next != NULL) {
24297c478bd9Sstevel@tonic-gate 		lrctl->rc_cursor = lrctl->rc_cursor->rcv_next;
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 		RCTLOP_SET(lrctl, p, e, rctl_model_value(lrctl->rc_dict_entry,
24327c478bd9Sstevel@tonic-gate 		    p, lrctl->rc_cursor->rcv_value));
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	}
24357c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	return (ret);
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate /*
24417c478bd9Sstevel@tonic-gate  * int rctl_test(rctl_hndl_t, rctl_set_t *, struct proc *, rctl_qty_t, uint_t)
24427c478bd9Sstevel@tonic-gate  *
24437c478bd9Sstevel@tonic-gate  * Overview
24447c478bd9Sstevel@tonic-gate  *   Increment the resource associated with the given handle, returning zero if
24457c478bd9Sstevel@tonic-gate  *   the incremented value does not exceed the threshold for the current limit
24467c478bd9Sstevel@tonic-gate  *   on the resource.
24477c478bd9Sstevel@tonic-gate  *
24487c478bd9Sstevel@tonic-gate  * Return values
24497c478bd9Sstevel@tonic-gate  *   Actions taken, according to the rctl_test bitmask.
24507c478bd9Sstevel@tonic-gate  *
24517c478bd9Sstevel@tonic-gate  * Caller's context
24527c478bd9Sstevel@tonic-gate  *   p_lock held by caller.
24537c478bd9Sstevel@tonic-gate  */
24547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24557c478bd9Sstevel@tonic-gate int
24567c478bd9Sstevel@tonic-gate rctl_test(rctl_hndl_t rhndl, rctl_set_t *rset, struct proc *p,
24577c478bd9Sstevel@tonic-gate     rctl_qty_t incr, uint_t flags)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate 	return (rctl_test_entity(rhndl, rset, p, NULL, incr, flags));
24607c478bd9Sstevel@tonic-gate }
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate int
24637c478bd9Sstevel@tonic-gate rctl_test_entity(rctl_hndl_t rhndl, rctl_set_t *rset, struct proc *p,
24647c478bd9Sstevel@tonic-gate     rctl_entity_p_t *e, rctl_qty_t incr, uint_t flags)
24657c478bd9Sstevel@tonic-gate {
24667c478bd9Sstevel@tonic-gate 	rctl_t *lrctl;
24677c478bd9Sstevel@tonic-gate 	int ret = RCT_NONE;
24687c478bd9Sstevel@tonic-gate 	rctl_entity_p_t e_tmp;
24697c478bd9Sstevel@tonic-gate 	if (p == &p0) {
24707c478bd9Sstevel@tonic-gate 		/*
24717c478bd9Sstevel@tonic-gate 		 * We don't enforce rctls on the kernel itself.
24727c478bd9Sstevel@tonic-gate 		 */
24737c478bd9Sstevel@tonic-gate 		return (ret);
24747c478bd9Sstevel@tonic-gate 	}
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate rctl_test_acquire:
24777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 	mutex_enter(&rset->rcs_lock);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	/*
24827c478bd9Sstevel@tonic-gate 	 * Dereference from rctl_set.  We don't enforce newly loaded controls
24837c478bd9Sstevel@tonic-gate 	 * that haven't been set on this entity (since the only valid value is
24847c478bd9Sstevel@tonic-gate 	 * the infinite system value).
24857c478bd9Sstevel@tonic-gate 	 */
24867c478bd9Sstevel@tonic-gate 	if (rctl_set_find(rset, rhndl, &lrctl) == -1) {
24877c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
24887c478bd9Sstevel@tonic-gate 		return (ret);
24897c478bd9Sstevel@tonic-gate 	}
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	/*
24927c478bd9Sstevel@tonic-gate 	 * This control is currently unenforced:  maximal value on control
24937c478bd9Sstevel@tonic-gate 	 * supporting infinitely available resource.
24947c478bd9Sstevel@tonic-gate 	 */
24957c478bd9Sstevel@tonic-gate 	if ((lrctl->rc_dict_entry->rcd_flagaction & RCTL_GLOBAL_INFINITE) &&
24967c478bd9Sstevel@tonic-gate 	    (lrctl->rc_cursor->rcv_flagaction & RCTL_LOCAL_MAXIMAL)) {
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 		mutex_exit(&rset->rcs_lock);
24997c478bd9Sstevel@tonic-gate 		return (ret);
25007c478bd9Sstevel@tonic-gate 	}
25017c478bd9Sstevel@tonic-gate 
25027c478bd9Sstevel@tonic-gate 	/*
25037c478bd9Sstevel@tonic-gate 	 * If we have been called by rctl_test, look up the entity pointer
25047c478bd9Sstevel@tonic-gate 	 * from the proc pointer.
25057c478bd9Sstevel@tonic-gate 	 */
25067c478bd9Sstevel@tonic-gate 	if (e == NULL) {
25077c478bd9Sstevel@tonic-gate 		rctl_entity_obtain_entity_p(lrctl->rc_dict_entry->rcd_entity,
25087c478bd9Sstevel@tonic-gate 		p, &e_tmp);
25097c478bd9Sstevel@tonic-gate 		e = &e_tmp;
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	/*
25137c478bd9Sstevel@tonic-gate 	 * Get enforced rctl value and current usage.  Test the increment
25147c478bd9Sstevel@tonic-gate 	 * with the current usage against the enforced value--take action as
25157c478bd9Sstevel@tonic-gate 	 * necessary.
25167c478bd9Sstevel@tonic-gate 	 */
25177c478bd9Sstevel@tonic-gate 	while (RCTLOP_TEST(lrctl, p, e, lrctl->rc_cursor, incr, flags)) {
25187c478bd9Sstevel@tonic-gate 		if ((ret & RCT_LK_ABANDONED) == 0) {
25197c478bd9Sstevel@tonic-gate 			ret |= rctl_global_action(lrctl, rset, p,
25207c478bd9Sstevel@tonic-gate 			    lrctl->rc_cursor);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 			RCTLOP_ACTION(lrctl, p, e);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 			ret |= rctl_local_action(lrctl, rset, p,
25257c478bd9Sstevel@tonic-gate 			    lrctl->rc_cursor, flags);
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 			if (ret & RCT_LK_ABANDONED)
25287c478bd9Sstevel@tonic-gate 				goto rctl_test_acquire;
25297c478bd9Sstevel@tonic-gate 		}
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 		ret &= ~RCT_LK_ABANDONED;
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 		if ((ret & RCT_DENY) == RCT_DENY ||
25347c478bd9Sstevel@tonic-gate 		    lrctl->rc_cursor->rcv_next == NULL) {
25357c478bd9Sstevel@tonic-gate 			ret |= RCT_DENY;
25367c478bd9Sstevel@tonic-gate 			break;
25377c478bd9Sstevel@tonic-gate 		}
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 		lrctl->rc_cursor = lrctl->rc_cursor->rcv_next;
25407c478bd9Sstevel@tonic-gate 		RCTLOP_SET(lrctl, p, e, rctl_model_value(lrctl->rc_dict_entry,
25417c478bd9Sstevel@tonic-gate 		    p, lrctl->rc_cursor->rcv_value));
25427c478bd9Sstevel@tonic-gate 	}
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	mutex_exit(&rset->rcs_lock);
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	return (ret);
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate /*
25507c478bd9Sstevel@tonic-gate  * void rctl_init(void)
25517c478bd9Sstevel@tonic-gate  *
25527c478bd9Sstevel@tonic-gate  * Overview
25537c478bd9Sstevel@tonic-gate  *   Initialize the rctl subsystem, including the primoridal rctls
25547c478bd9Sstevel@tonic-gate  *   provided by the system.  New subsystem-specific rctls should _not_ be
25557c478bd9Sstevel@tonic-gate  *   initialized here.  (Do it in your own file.)
25567c478bd9Sstevel@tonic-gate  *
25577c478bd9Sstevel@tonic-gate  * Return values
25587c478bd9Sstevel@tonic-gate  *   None.
25597c478bd9Sstevel@tonic-gate  *
25607c478bd9Sstevel@tonic-gate  * Caller's context
25617c478bd9Sstevel@tonic-gate  *   Safe for KM_SLEEP allocations.  Must be called prior to any process model
25627c478bd9Sstevel@tonic-gate  *   initialization.
25637c478bd9Sstevel@tonic-gate  */
25647c478bd9Sstevel@tonic-gate void
25657c478bd9Sstevel@tonic-gate rctl_init(void)
25667c478bd9Sstevel@tonic-gate {
25677c478bd9Sstevel@tonic-gate 	rctl_cache = kmem_cache_create("rctl_cache", sizeof (rctl_t),
25687c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
25697c478bd9Sstevel@tonic-gate 	rctl_val_cache = kmem_cache_create("rctl_val_cache",
25707c478bd9Sstevel@tonic-gate 	    sizeof (rctl_val_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	rctl_dict = mod_hash_create_extended("rctl_dict",
25737c478bd9Sstevel@tonic-gate 	    rctl_dict_size, mod_hash_null_keydtor, rctl_dict_val_dtor,
25747c478bd9Sstevel@tonic-gate 	    rctl_dict_hash_by_id, NULL, rctl_dict_id_cmp, KM_SLEEP);
25757c478bd9Sstevel@tonic-gate 	rctl_dict_by_name = mod_hash_create_strhash(
25767c478bd9Sstevel@tonic-gate 	    "rctl_handles_by_name", rctl_dict_size,
25777c478bd9Sstevel@tonic-gate 	    mod_hash_null_valdtor);
25787c478bd9Sstevel@tonic-gate 	rctl_ids = id_space_create("rctl_ids", 1, max_rctl_hndl);
25797c478bd9Sstevel@tonic-gate 	bzero(rctl_lists, (RC_MAX_ENTITY + 1) * sizeof (rctl_dict_entry_t *));
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	rctlproc_init();
25827c478bd9Sstevel@tonic-gate }
2583c6939658Ssl108498 
2584c6939658Ssl108498 /*
2585c6939658Ssl108498  * rctl_incr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc)
2586c6939658Ssl108498  *
2587c6939658Ssl108498  * Increments the amount of locked memory on a project, and
2588c6939658Ssl108498  * zone. If proj is NULL, the proj and zone of proc_t p is used.  If
2589c6939658Ssl108498  * chargeproc is non-zero, then the charged amount is cached on p->p_locked_mem
2590c6939658Ssl108498  * so that the charge can be migrated when a process changes projects.
2591c6939658Ssl108498  *
2592c6939658Ssl108498  * Return values
2593c6939658Ssl108498  *    0 - success
2594c6939658Ssl108498  *    EAGAIN - attempting to increment locked memory is denied by one
2595c6939658Ssl108498  *      or more resource entities.
2596c6939658Ssl108498  */
2597c6939658Ssl108498 int
2598c6939658Ssl108498 rctl_incr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
2599c6939658Ssl108498     int chargeproc)
2600c6939658Ssl108498 {
2601c6939658Ssl108498 	kproject_t *projp;
2602c6939658Ssl108498 	zone_t *zonep;
2603c6939658Ssl108498 	rctl_entity_p_t e;
2604c6939658Ssl108498 	int ret = 0;
2605c6939658Ssl108498 
2606c6939658Ssl108498 	ASSERT(p != NULL);
2607c6939658Ssl108498 	ASSERT(MUTEX_HELD(&p->p_lock));
2608c6939658Ssl108498 	if (proj != NULL) {
2609c6939658Ssl108498 		projp = proj;
2610c6939658Ssl108498 		zonep = zone_find_by_id(projp->kpj_zoneid);
2611c6939658Ssl108498 	} else {
2612c6939658Ssl108498 		projp = p->p_task->tk_proj;
2613c6939658Ssl108498 		zonep = p->p_zone;
2614c6939658Ssl108498 	}
2615c6939658Ssl108498 
26160209230bSgjelinek 	mutex_enter(&zonep->zone_mem_lock);
2617c6939658Ssl108498 
2618c6939658Ssl108498 	e.rcep_p.proj = projp;
2619c6939658Ssl108498 	e.rcep_t = RCENTITY_PROJECT;
2620c6939658Ssl108498 	if (projp->kpj_data.kpd_locked_mem + inc >
2621c6939658Ssl108498 	    projp->kpj_data.kpd_locked_mem_ctl) {
2622c6939658Ssl108498 		if (rctl_test_entity(rc_project_locked_mem, projp->kpj_rctls,
2623c6939658Ssl108498 		    p, &e, inc, 0) & RCT_DENY) {
2624c6939658Ssl108498 			ret = EAGAIN;
2625c6939658Ssl108498 			goto out;
2626c6939658Ssl108498 		}
2627c6939658Ssl108498 	}
2628c6939658Ssl108498 	e.rcep_p.zone = zonep;
2629c6939658Ssl108498 	e.rcep_t = RCENTITY_ZONE;
2630c6939658Ssl108498 	if (zonep->zone_locked_mem + inc > zonep->zone_locked_mem_ctl) {
2631c6939658Ssl108498 		if (rctl_test_entity(rc_zone_locked_mem, zonep->zone_rctls,
2632c6939658Ssl108498 		    p, &e, inc, 0) & RCT_DENY) {
2633c6939658Ssl108498 			ret = EAGAIN;
2634c6939658Ssl108498 			goto out;
2635c6939658Ssl108498 		}
2636c6939658Ssl108498 	}
2637c6939658Ssl108498 
2638c6939658Ssl108498 	zonep->zone_locked_mem += inc;
2639c6939658Ssl108498 	projp->kpj_data.kpd_locked_mem += inc;
2640c6939658Ssl108498 	if (chargeproc != 0) {
2641c6939658Ssl108498 		p->p_locked_mem += inc;
2642c6939658Ssl108498 	}
2643c6939658Ssl108498 out:
26440209230bSgjelinek 	mutex_exit(&zonep->zone_mem_lock);
2645c6939658Ssl108498 	if (proj != NULL)
2646c6939658Ssl108498 		zone_rele(zonep);
2647c6939658Ssl108498 	return (ret);
2648c6939658Ssl108498 }
2649c6939658Ssl108498 
2650c6939658Ssl108498 /*
2651c6939658Ssl108498  * rctl_decr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc)
2652c6939658Ssl108498  *
2653c6939658Ssl108498  * Decrements the amount of locked memory on a project and
2654c6939658Ssl108498  * zone.  If proj is NULL, the proj and zone of proc_t p is used.  If
2655c6939658Ssl108498  * creditproc is non-zero, then the quantity of locked memory is subtracted
2656c6939658Ssl108498  * from p->p_locked_mem.
2657c6939658Ssl108498  *
2658c6939658Ssl108498  * Return values
2659c6939658Ssl108498  *   none
2660c6939658Ssl108498  */
2661c6939658Ssl108498 void
2662c6939658Ssl108498 rctl_decr_locked_mem(proc_t *p, kproject_t *proj, rctl_qty_t inc,
2663c6939658Ssl108498     int creditproc)
2664c6939658Ssl108498 {
2665c6939658Ssl108498 	kproject_t *projp;
2666c6939658Ssl108498 	zone_t *zonep;
2667c6939658Ssl108498 
2668c6939658Ssl108498 	if (proj != NULL) {
2669c6939658Ssl108498 		projp = proj;
2670c6939658Ssl108498 		zonep = zone_find_by_id(projp->kpj_zoneid);
2671c6939658Ssl108498 	} else {
2672c6939658Ssl108498 		ASSERT(p != NULL);
2673c6939658Ssl108498 		ASSERT(MUTEX_HELD(&p->p_lock));
2674c6939658Ssl108498 		projp = p->p_task->tk_proj;
2675c6939658Ssl108498 		zonep = p->p_zone;
2676c6939658Ssl108498 	}
2677c6939658Ssl108498 
26780209230bSgjelinek 	mutex_enter(&zonep->zone_mem_lock);
2679c6939658Ssl108498 	zonep->zone_locked_mem -= inc;
2680c6939658Ssl108498 	projp->kpj_data.kpd_locked_mem -= inc;
2681c6939658Ssl108498 	if (creditproc != 0) {
2682c6939658Ssl108498 		ASSERT(p != NULL);
2683c6939658Ssl108498 		ASSERT(MUTEX_HELD(&p->p_lock));
2684c6939658Ssl108498 		p->p_locked_mem -= inc;
2685c6939658Ssl108498 	}
26860209230bSgjelinek 	mutex_exit(&zonep->zone_mem_lock);
2687c6939658Ssl108498 	if (proj != NULL)
2688c6939658Ssl108498 		zone_rele(zonep);
2689c6939658Ssl108498 }
26900209230bSgjelinek 
26910209230bSgjelinek /*
26920209230bSgjelinek  * rctl_incr_swap(proc_t *, zone_t *, size_t)
26930209230bSgjelinek  *
26940209230bSgjelinek  * Overview
26950209230bSgjelinek  *   Increments the swap charge on the specified zone.
26960209230bSgjelinek  *
26970209230bSgjelinek  * Return values
26980209230bSgjelinek  *   0 on success.  EAGAIN if swap increment fails due an rctl value
26990209230bSgjelinek  *   on the zone.
27000209230bSgjelinek  *
27010209230bSgjelinek  * Callers context
27020209230bSgjelinek  *   p_lock held on specified proc.
27030209230bSgjelinek  *   swap must be even multiple of PAGESIZE
27040209230bSgjelinek  */
27050209230bSgjelinek int
27060209230bSgjelinek rctl_incr_swap(proc_t *proc, zone_t *zone, size_t swap)
27070209230bSgjelinek {
27080209230bSgjelinek 	rctl_entity_p_t e;
27090209230bSgjelinek 
27100209230bSgjelinek 	ASSERT(MUTEX_HELD(&proc->p_lock));
27110209230bSgjelinek 	ASSERT((swap & PAGEOFFSET) == 0);
27120209230bSgjelinek 	e.rcep_p.zone = zone;
27130209230bSgjelinek 	e.rcep_t = RCENTITY_ZONE;
27140209230bSgjelinek 
27150209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
27160209230bSgjelinek 
27170209230bSgjelinek 	if ((zone->zone_max_swap + swap) >
27180209230bSgjelinek 	    zone->zone_max_swap_ctl) {
27190209230bSgjelinek 
27200209230bSgjelinek 		if (rctl_test_entity(rc_zone_max_swap, zone->zone_rctls,
27210209230bSgjelinek 		    proc, &e, swap, 0) & RCT_DENY) {
27220209230bSgjelinek 			mutex_exit(&zone->zone_mem_lock);
27230209230bSgjelinek 			return (EAGAIN);
27240209230bSgjelinek 		}
27250209230bSgjelinek 	}
27260209230bSgjelinek 	zone->zone_max_swap += swap;
27270209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
27280209230bSgjelinek 	return (0);
27290209230bSgjelinek }
27300209230bSgjelinek 
27310209230bSgjelinek /*
27320209230bSgjelinek  * rctl_decr_swap(zone_t *, size_t)
27330209230bSgjelinek  *
27340209230bSgjelinek  * Overview
27350209230bSgjelinek  *   Decrements the swap charge on the specified zone.
27360209230bSgjelinek  *
27370209230bSgjelinek  * Return values
27380209230bSgjelinek  *   None
27390209230bSgjelinek  *
27400209230bSgjelinek  * Callers context
27410209230bSgjelinek  *   swap must be even multiple of PAGESIZE
27420209230bSgjelinek  */
27430209230bSgjelinek void
27440209230bSgjelinek rctl_decr_swap(zone_t *zone, size_t swap)
27450209230bSgjelinek {
27460209230bSgjelinek 	ASSERT((swap & PAGEOFFSET) == 0);
27470209230bSgjelinek 	mutex_enter(&zone->zone_mem_lock);
27480209230bSgjelinek 	ASSERT(zone->zone_max_swap >= swap);
27490209230bSgjelinek 	zone->zone_max_swap -= swap;
27500209230bSgjelinek 	mutex_exit(&zone->zone_mem_lock);
27510209230bSgjelinek }
27520209230bSgjelinek 
27530209230bSgjelinek /*
27540209230bSgjelinek  * Create resource kstat
27550209230bSgjelinek  */
27560209230bSgjelinek static kstat_t *
27570209230bSgjelinek rctl_kstat_create_common(char *ks_name, int ks_instance, char *ks_class,
27580209230bSgjelinek     uchar_t ks_type, uint_t ks_ndata, uchar_t ks_flags, int ks_zoneid)
27590209230bSgjelinek {
27600209230bSgjelinek 	kstat_t *ksp = NULL;
27610209230bSgjelinek 	char name[KSTAT_STRLEN];
27620209230bSgjelinek 
27630209230bSgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_%d", ks_name, ks_instance);
27640209230bSgjelinek 
27650209230bSgjelinek 	if ((ksp = kstat_create_zone("caps", ks_zoneid,
27660209230bSgjelinek 		name, ks_class, ks_type,
27670209230bSgjelinek 		ks_ndata, ks_flags, ks_zoneid)) != NULL) {
27680209230bSgjelinek 		if (ks_zoneid != GLOBAL_ZONEID)
27690209230bSgjelinek 			kstat_zone_add(ksp, GLOBAL_ZONEID);
27700209230bSgjelinek 	}
27710209230bSgjelinek 	return (ksp);
27720209230bSgjelinek }
27730209230bSgjelinek 
27740209230bSgjelinek /*
27750209230bSgjelinek  * Create zone-specific resource kstat
27760209230bSgjelinek  */
27770209230bSgjelinek kstat_t *
27780209230bSgjelinek rctl_kstat_create_zone(zone_t *zone, char *ks_name, uchar_t ks_type,
27790209230bSgjelinek     uint_t ks_ndata, uchar_t ks_flags)
27800209230bSgjelinek {
27810209230bSgjelinek 	char name[KSTAT_STRLEN];
27820209230bSgjelinek 
27830209230bSgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_zone", ks_name);
27840209230bSgjelinek 
27850209230bSgjelinek 	return (rctl_kstat_create_common(name, zone->zone_id, "zone_caps",
27860209230bSgjelinek 	    ks_type, ks_ndata, ks_flags, zone->zone_id));
27870209230bSgjelinek }
27880209230bSgjelinek 
27890209230bSgjelinek /*
27900209230bSgjelinek  * Create project-specific resource kstat
27910209230bSgjelinek  */
27920209230bSgjelinek kstat_t *
27930209230bSgjelinek rctl_kstat_create_project(kproject_t *kpj, char *ks_name, uchar_t ks_type,
27940209230bSgjelinek     uint_t ks_ndata, uchar_t ks_flags)
27950209230bSgjelinek {
27960209230bSgjelinek 	char name[KSTAT_STRLEN];
27970209230bSgjelinek 
27980209230bSgjelinek 	(void) snprintf(name, KSTAT_STRLEN, "%s_project", ks_name);
27990209230bSgjelinek 
28000209230bSgjelinek 	return (rctl_kstat_create_common(name, kpj->kpj_id, "project_caps",
28010209230bSgjelinek 	    ks_type, ks_ndata, ks_flags, kpj->kpj_zoneid));
28020209230bSgjelinek }
2803