Deleted Added
full compact
kern_rctl.c (234383) kern_rctl.c (242139)
1/*-
2 * Copyright (c) 2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
1/*-
2 * Copyright (c) 2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 12 unchanged lines hidden (view full) ---

21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/kern/kern_rctl.c 234383 2012-04-17 14:31:02Z trasz $
29 * $FreeBSD: head/sys/kern/kern_rctl.c 242139 2012-10-26 16:01:08Z trasz $
30 */
31
32#include <sys/cdefs.h>
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/kern_rctl.c 234383 2012-04-17 14:31:02Z trasz $");
33__FBSDID("$FreeBSD: head/sys/kern/kern_rctl.c 242139 2012-10-26 16:01:08Z trasz $");
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/malloc.h>
38#include <sys/queue.h>
39#include <sys/refcount.h>
40#include <sys/jail.h>
41#include <sys/kernel.h>

--- 29 unchanged lines hidden (view full) ---

71#define HRF_DONT_INHERIT 1
72#define HRF_DONT_ACCUMULATE 2
73
74/* Default buffer size for rctl_get_rules(2). */
75#define RCTL_DEFAULT_BUFSIZE 4096
76#define RCTL_MAX_INBUFLEN 4096
77#define RCTL_LOG_BUFSIZE 128
78
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/malloc.h>
38#include <sys/queue.h>
39#include <sys/refcount.h>
40#include <sys/jail.h>
41#include <sys/kernel.h>

--- 29 unchanged lines hidden (view full) ---

71#define HRF_DONT_INHERIT 1
72#define HRF_DONT_ACCUMULATE 2
73
74/* Default buffer size for rctl_get_rules(2). */
75#define RCTL_DEFAULT_BUFSIZE 4096
76#define RCTL_MAX_INBUFLEN 4096
77#define RCTL_LOG_BUFSIZE 128
78
79#define RCTL_PCPU_SHIFT (10 * 1000000)
80
79/*
80 * 'rctl_rule_link' connects a rule with every racct it's related to.
81 * For example, rule 'user:X:openfiles:deny=N/process' is linked
82 * with uidinfo for user X, and to each process of that user.
83 */
84struct rctl_rule_link {
85 LIST_ENTRY(rctl_rule_link) rrl_next;
86 struct rctl_rule *rrl_rule;

--- 28 unchanged lines hidden (view full) ---

115 { "msgqqueued", RACCT_MSGQQUEUED },
116 { "msgqsize", RACCT_MSGQSIZE },
117 { "nmsgq", RACCT_NMSGQ },
118 { "nsem", RACCT_NSEM },
119 { "nsemop", RACCT_NSEMOP },
120 { "nshm", RACCT_NSHM },
121 { "shmsize", RACCT_SHMSIZE },
122 { "wallclock", RACCT_WALLCLOCK },
81/*
82 * 'rctl_rule_link' connects a rule with every racct it's related to.
83 * For example, rule 'user:X:openfiles:deny=N/process' is linked
84 * with uidinfo for user X, and to each process of that user.
85 */
86struct rctl_rule_link {
87 LIST_ENTRY(rctl_rule_link) rrl_next;
88 struct rctl_rule *rrl_rule;

--- 28 unchanged lines hidden (view full) ---

117 { "msgqqueued", RACCT_MSGQQUEUED },
118 { "msgqsize", RACCT_MSGQSIZE },
119 { "nmsgq", RACCT_NMSGQ },
120 { "nsem", RACCT_NSEM },
121 { "nsemop", RACCT_NSEMOP },
122 { "nshm", RACCT_NSHM },
123 { "shmsize", RACCT_SHMSIZE },
124 { "wallclock", RACCT_WALLCLOCK },
125 { "pcpu", RACCT_PCTCPU },
123 { NULL, -1 }};
124
125static struct dict actionnames[] = {
126 { "sighup", RCTL_ACTION_SIGHUP },
127 { "sigint", RCTL_ACTION_SIGINT },
128 { "sigquit", RCTL_ACTION_SIGQUIT },
129 { "sigill", RCTL_ACTION_SIGILL },
130 { "sigtrap", RCTL_ACTION_SIGTRAP },

--- 135 unchanged lines hidden (view full) ---

266 available = rctl_available_resource(p, rule);
267 if (available >= amount)
268 return (0);
269
270 return (1);
271}
272
273/*
126 { NULL, -1 }};
127
128static struct dict actionnames[] = {
129 { "sighup", RCTL_ACTION_SIGHUP },
130 { "sigint", RCTL_ACTION_SIGINT },
131 { "sigquit", RCTL_ACTION_SIGQUIT },
132 { "sigill", RCTL_ACTION_SIGILL },
133 { "sigtrap", RCTL_ACTION_SIGTRAP },

--- 135 unchanged lines hidden (view full) ---

269 available = rctl_available_resource(p, rule);
270 if (available >= amount)
271 return (0);
272
273 return (1);
274}
275
276/*
277 * Special version of rctl_available() function for the %cpu resource.
278 * We slightly cheat here and return less than we normally would.
279 */
280int64_t
281rctl_pcpu_available(const struct proc *p) {
282 struct rctl_rule *rule;
283 struct rctl_rule_link *link;
284 int64_t available, minavailable, limit;
285
286 minavailable = INT64_MAX;
287 limit = 0;
288
289 rw_rlock(&rctl_lock);
290
291 LIST_FOREACH(link, &p->p_racct->r_rule_links, rrl_next) {
292 rule = link->rrl_rule;
293 if (rule->rr_resource != RACCT_PCTCPU)
294 continue;
295 if (rule->rr_action != RCTL_ACTION_DENY)
296 continue;
297 available = rctl_available_resource(p, rule);
298 if (available < minavailable) {
299 minavailable = available;
300 limit = rule->rr_amount;
301 }
302 }
303
304 rw_runlock(&rctl_lock);
305
306 /*
307 * Return slightly less than actual value of the available
308 * %cpu resource. This makes %cpu throttling more agressive
309 * and lets us act sooner than the limits are already exceeded.
310 */
311 if (limit != 0) {
312 if (limit > 2 * RCTL_PCPU_SHIFT)
313 minavailable -= RCTL_PCPU_SHIFT;
314 else
315 minavailable -= (limit / 2);
316 }
317
318 return (minavailable);
319}
320
321/*
274 * Check whether the proc 'p' can allocate 'amount' of 'resource' in addition
275 * to what it keeps allocated now. Returns non-zero if the allocation should
276 * be denied, 0 otherwise.
277 */
278int
279rctl_enforce(struct proc *p, int resource, uint64_t amount)
280{
281 struct rctl_rule *rule;

--- 1541 unchanged lines hidden ---
322 * Check whether the proc 'p' can allocate 'amount' of 'resource' in addition
323 * to what it keeps allocated now. Returns non-zero if the allocation should
324 * be denied, 0 otherwise.
325 */
326int
327rctl_enforce(struct proc *p, int resource, uint64_t amount)
328{
329 struct rctl_rule *rule;

--- 1541 unchanged lines hidden ---