xref: /illumos-gate/usr/src/uts/common/sys/rctl.h (revision 91cfa10a8e55050a5103c4b2e83b0bf8d783a7cb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_SYS_RCTL_H
27 #define	_SYS_RCTL_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/kmem.h>
32 #include <sys/resource.h>
33 #include <sys/types.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * Available local actions and flags.
41  */
42 #define	RCTL_LOCAL_NOACTION		0x00000000
43 #define	RCTL_LOCAL_SIGNAL		0x00000001
44 #define	RCTL_LOCAL_DENY			0x00000002
45 
46 #define	RCTL_LOCAL_MAXIMAL		0x80000000
47 
48 #define	RCTL_LOCAL_ACTION_MASK		0xffff0000
49 #define	RCTL_LOCAL_MASK			0x80000003
50 
51 /*
52  * Available global actions and flags.
53  */
54 #define	RCTL_GLOBAL_NOACTION		0x00000000
55 #define	RCTL_GLOBAL_SYSLOG		0x00000001
56 
57 #define	RCTL_GLOBAL_NOBASIC		0x80000000
58 #define	RCTL_GLOBAL_LOWERABLE		0x40000000
59 #define	RCTL_GLOBAL_DENY_ALWAYS		0x20000000
60 #define	RCTL_GLOBAL_DENY_NEVER		0x10000000
61 #define	RCTL_GLOBAL_FILE_SIZE		0x08000000
62 #define	RCTL_GLOBAL_CPU_TIME		0x04000000
63 #define	RCTL_GLOBAL_SIGNAL_NEVER	0x02000000
64 #define	RCTL_GLOBAL_NOLOCALACTION	RCTL_GLOBAL_SIGNAL_NEVER
65 #define	RCTL_GLOBAL_INFINITE		0x01000000
66 #define	RCTL_GLOBAL_UNOBSERVABLE	0x00800000
67 #define	RCTL_GLOBAL_SYSLOG_NEVER	0x00080000
68 
69 #define	RCTL_GLOBAL_BYTES		0x00400000
70 #define	RCTL_GLOBAL_SECONDS		0x00200000
71 #define	RCTL_GLOBAL_COUNT		0x00100000
72 
73 #define	RCTL_GLOBAL_ACTION_MASK		0xffff0000
74 #define	RCTL_GLOBAL_MASK		0xfff80001
75 
76 /*
77  * getrctl(2) flag values
78  */
79 #define	RCTL_FIRST		0x00000000
80 #define	RCTL_NEXT		0x00000001
81 #define	RCTL_USAGE		0x00000002
82 
83 /*
84  * setrctl(2) flag values
85  */
86 
87 #define	RCTL_INSERT		0x00000000
88 #define	RCTL_DELETE		0x00000001
89 #define	RCTL_REPLACE		0x00000002
90 
91 #define	RCTL_USE_RECIPIENT_PID	0x10000000
92 
93 #define	RCTLSYS_ACTION_MASK 	0xffff0000
94 #define	RCTLSYS_MASK		0x10000003
95 
96 /*
97  * rctl_priv_t: rctl privilege defined values
98  *   A large amount of space has been deliberately left between these privileges
99  *   to permit future enrichment of the control privilege value.
100  */
101 #define	RCPRIV_BASIC		0x01000000
102 #define	RCPRIV_PRIVILEGED	0x04000000
103 #define	RCPRIV_SYSTEM		0x07000000
104 
105 typedef u_longlong_t rctl_qty_t; /* resource control numerical values   */
106 typedef int rctl_priv_t;
107 
108 typedef struct rctlblk rctlblk_t;
109 
110 extern int setrctl(const char *, rctlblk_t *, rctlblk_t *, int);
111 extern int getrctl(const char *, rctlblk_t *, rctlblk_t *, int);
112 
113 typedef enum {
114 	RCENTITY_PROCESS,
115 	RCENTITY_TASK,
116 	RCENTITY_PROJECT,
117 	RCENTITY_ZONE
118 } rctl_entity_t;
119 #define	RC_MAX_ENTITY RCENTITY_ZONE
120 
121 #ifndef _KERNEL
122 
123 typedef struct rctl_set rctl_set_t;
124 
125 #else /* _KERNEL */
126 
127 #include <sys/mutex.h>
128 
129 /*
130  * rctl_test return bitfield
131  */
132 #define	RCT_NONE		0x00000000
133 #define	RCT_DENY		0x00000001
134 #define	RCT_SIGNAL		0x00000002
135 #define	RCT_STRLOG		0x00000004
136 
137 #define	RCT_LK_ABANDONED	0x80000000
138 
139 /*
140  * rctl_set_dup flags
141  */
142 #define	RCD_DUP			0x1
143 #define	RCD_CALLBACK		0x2
144 
145 /*
146  * rctl_action/rctl_test action safety states
147  */
148 #define	RCA_SAFE		0x0 /* safe for signal and siginfo delivery */
149 #define	RCA_UNSAFE_SIGINFO	0x1 /* not safe to allocate for siginfo */
150 #define	RCA_UNSAFE_ALL		0x2 /* not safe to send signal */
151 
152 typedef struct rctl_val {
153 	struct rctl_val *rcv_prev;		/* previous (lower) value */
154 	struct rctl_val *rcv_next;		/* next (higher) value */
155 	rctl_priv_t	rcv_privilege;		/* appropriate RCPRIV_* cst */
156 	rctl_qty_t	rcv_value;		/* enforced value of control */
157 	uint_t		rcv_flagaction;		/* properties and actions */
158 	int		rcv_action_signal;	/* signal to send as action */
159 	struct proc	*rcv_action_recipient;	/* process to receive signal */
160 	id_t		rcv_action_recip_pid;	/* pid of that process */
161 	hrtime_t	rcv_firing_time;	/* time rctl_val last fired */
162 } rctl_val_t;
163 
164 typedef int rctl_hndl_t;
165 
166 struct rctl;
167 struct proc;
168 struct task;
169 struct kproject;
170 struct zone;
171 struct kstat;
172 
173 typedef struct rctl_entity_p_struct {
174 	rctl_entity_t rcep_t;
175 	union {
176 		struct proc *proc;
177 		struct task *task;
178 		struct kproject *proj;
179 		struct zone *zone;
180 	} rcep_p;
181 } rctl_entity_p_t;
182 
183 typedef struct rctl_ops {
184 	void		(*rco_action)(struct rctl *, struct proc *,
185 	    rctl_entity_p_t *);
186 	rctl_qty_t	(*rco_get_usage)(struct rctl *, struct proc *);
187 	int		(*rco_set)(struct rctl *, struct proc *,
188 	    rctl_entity_p_t *, rctl_qty_t);
189 	int		(*rco_test)(struct rctl *, struct proc *,
190 	    rctl_entity_p_t *, rctl_val_t *, rctl_qty_t, uint_t);
191 } rctl_ops_t;
192 
193 #define	RCTLOP_ACTION(r, p, e) (r->rc_dict_entry->rcd_ops->rco_action(r, p, e))
194 #define	RCTLOP_GET_USAGE(r, p) (r->rc_dict_entry->rcd_ops->rco_get_usage(r, p))
195 #define	RCTLOP_SET(r, p, e, v) (r->rc_dict_entry->rcd_ops->rco_set(r, p, e, v))
196 #define	RCTLOP_TEST(r, p, e, v, i, f) \
197 	(r->rc_dict_entry->rcd_ops->rco_test(r, p, e, v, i, f))
198 
199 /*
200  * Default resource control callback functions.
201  */
202 void rcop_no_action(struct rctl *, struct proc *, rctl_entity_p_t *);
203 rctl_qty_t rcop_no_usage(struct rctl *, struct proc *);
204 int rcop_no_set(struct rctl *, struct proc *, rctl_entity_p_t *, rctl_qty_t);
205 int rcop_no_test(struct rctl *, struct proc *, rctl_entity_p_t *,
206     struct rctl_val *, rctl_qty_t, uint_t);
207 int rcop_absolute_test(struct rctl *, struct proc *, rctl_entity_p_t *,
208     struct rctl_val *, rctl_qty_t, uint_t);
209 
210 extern rctl_ops_t rctl_default_ops;
211 extern rctl_ops_t rctl_absolute_ops;
212 
213 typedef struct rctl {
214 	struct rctl	*rc_next;		/* next in set hash chain    */
215 	rctl_val_t	*rc_values;		/* list of enforced value    */
216 	rctl_val_t	*rc_cursor;		/* currently enforced value  */
217 	struct rctl_dict_entry *rc_dict_entry;	/* global control properties */
218 	rctl_hndl_t	rc_id;			/* control handle (hash key) */
219 } rctl_t;
220 
221 /*
222  * The rctl_set is the collection of resource controls associated with an
223  * individual entity within the system.  All of the controls are applicable to
224  * the same entity, which we call out explicitly in rcs_entity.
225  */
226 typedef struct rctl_set {
227 	kmutex_t	rcs_lock;		/* global set lock	  */
228 	rctl_entity_t	rcs_entity;		/* entity type		  */
229 	rctl_t		**rcs_ctls;		/* hash table of controls */
230 } rctl_set_t;
231 
232 typedef struct rctl_dict_entry {
233 	struct rctl_dict_entry *rcd_next;	/* next in dict hash chain */
234 	char		*rcd_name;		/* resource control name */
235 	rctl_val_t	*rcd_default_value;	/* system control value */
236 	rctl_ops_t	*rcd_ops;		/* callback operations */
237 	rctl_hndl_t	rcd_id;			/* control handle */
238 	rctl_entity_t	rcd_entity;		/* entity type */
239 	int		rcd_flagaction;		/* global properties/actions */
240 	int		rcd_syslog_level;	/* event syslog level */
241 	int		rcd_strlog_flags;	/* derived from syslog level */
242 	rctl_qty_t	rcd_max_native;		/* native model "infinity" */
243 	rctl_qty_t	rcd_max_ilp32;		/* ILP32 model "infinity" */
244 } rctl_dict_entry_t;
245 
246 typedef struct rctl_alloc_gp {
247 	uint_t	rcag_nctls;	/* number of rctls needed/allocated */
248 	uint_t	rcag_nvals;	/* number of rctl values needed/allocated */
249 	rctl_t	*rcag_ctls;	/* list of allocated rctls */
250 	rctl_val_t *rcag_vals;	/* list of allocated rctl values */
251 } rctl_alloc_gp_t;
252 
253 extern kmem_cache_t *rctl_cache;	/* kmem cache for rctl structures */
254 extern kmem_cache_t *rctl_val_cache;	/* kmem cache for rctl values */
255 
256 extern rctl_hndl_t rctlproc_legacy[];
257 extern uint_t rctlproc_flags[];
258 extern int rctlproc_signals[];
259 
260 void rctl_init(void);
261 void rctlproc_init(void);
262 void rctlproc_default_init(struct proc *, rctl_alloc_gp_t *);
263 
264 rctl_hndl_t rctl_register(const char *, rctl_entity_t, int, rctl_qty_t,
265     rctl_qty_t, rctl_ops_t *);
266 
267 rctl_hndl_t rctl_hndl_lookup(const char *);
268 rctl_dict_entry_t *rctl_dict_lookup(const char *);
269 rctl_dict_entry_t *rctl_dict_lookup_hndl(rctl_hndl_t);
270 void rctl_add_default_limit(const char *, rctl_qty_t, rctl_priv_t, uint_t);
271 void rctl_add_legacy_limit(const char *, const char *, const char *,
272     rctl_qty_t, rctl_qty_t);
273 
274 rctl_qty_t rctl_model_maximum(rctl_dict_entry_t *, struct proc *);
275 rctl_qty_t rctl_model_value(rctl_dict_entry_t *, struct proc *, rctl_qty_t);
276 
277 int rctl_invalid_value(rctl_dict_entry_t *, rctl_val_t *);
278 rctl_qty_t rctl_enforced_value(rctl_hndl_t, rctl_set_t *, struct proc *);
279 
280 int rctl_test(rctl_hndl_t, rctl_set_t *, struct proc *, rctl_qty_t, uint_t);
281 int rctl_action(rctl_hndl_t, rctl_set_t *, struct proc *, uint_t);
282 
283 int rctl_test_entity(rctl_hndl_t, rctl_set_t *, struct proc *,
284     rctl_entity_p_t *, rctl_qty_t, uint_t);
285 int rctl_action_entity(rctl_hndl_t, rctl_set_t *, struct proc *,
286     rctl_entity_p_t *, uint_t);
287 
288 int rctl_val_cmp(rctl_val_t *, rctl_val_t *, int);
289 int rctl_val_list_insert(rctl_val_t **, rctl_val_t *);
290 
291 rctl_set_t *rctl_set_create(void);
292 rctl_alloc_gp_t *rctl_set_init_prealloc(rctl_entity_t);
293 rctl_set_t *rctl_set_init(rctl_entity_t, struct proc *, rctl_entity_p_t *,
294     rctl_set_t *, rctl_alloc_gp_t *);
295 rctl_alloc_gp_t *rctl_set_dup_prealloc(rctl_set_t *);
296 int rctl_set_dup_ready(rctl_set_t *, rctl_alloc_gp_t *);
297 rctl_set_t *rctl_set_dup(rctl_set_t *, struct proc *, struct proc *,
298     rctl_entity_p_t *, rctl_set_t *, rctl_alloc_gp_t *, int);
299 void rctl_set_reset(rctl_set_t *, struct proc *, rctl_entity_p_t *);
300 void rctl_set_tearoff(rctl_set_t *, struct proc *);
301 void rctl_set_free(rctl_set_t *);
302 
303 void rctl_prealloc_destroy(rctl_alloc_gp_t *);
304 
305 size_t rctl_build_name_buf(char **);
306 
307 int rctl_global_get(const char *name, rctl_dict_entry_t *);
308 int rctl_global_set(const char *name, rctl_dict_entry_t *);
309 
310 int rctl_local_delete(rctl_hndl_t, rctl_val_t *, struct proc *p);
311 int rctl_local_insert(rctl_hndl_t, rctl_val_t *, struct proc *p);
312 int rctl_local_get(rctl_hndl_t, rctl_val_t *, rctl_val_t *, struct proc *p);
313 int rctl_local_replace(rctl_hndl_t, rctl_val_t *, rctl_val_t *,
314     struct proc *p);
315 
316 /* tag declaration to appease the compiler */
317 struct cred;
318 rctl_alloc_gp_t *rctl_rlimit_set_prealloc(uint_t);
319 int rctl_rlimit_set(rctl_hndl_t, struct proc *, struct rlimit64 *,
320     rctl_alloc_gp_t *, int, int, const struct cred *);
321 int rctl_rlimit_get(rctl_hndl_t, struct proc *, struct rlimit64 *);
322 
323 /* specific rctl utility functions */
324 int rctl_incr_locked_mem(struct proc *, struct kproject *, rctl_qty_t,
325     int);
326 void rctl_decr_locked_mem(struct proc *, struct kproject *, rctl_qty_t,
327     int);
328 int rctl_incr_swap(struct proc *, struct zone *, size_t);
329 void rctl_decr_swap(struct zone *, size_t);
330 
331 struct kstat *rctl_kstat_create_zone(struct zone *, char *, uchar_t, uint_t,
332     uchar_t);
333 
334 struct kstat *rctl_kstat_create_project(struct kproject *, char *, uchar_t,
335     uint_t, uchar_t);
336 
337 #endif /* _KERNEL */
338 
339 #ifdef	__cplusplus
340 }
341 #endif
342 
343 #endif	/* _SYS_RCTL_H */
344