blob: 88faec23e83301e5b64490318aba2ac722dfb00c [file] [log] [blame]
Pavel Emelianove552b662008-02-07 00:13:49 -08001/*
2 * resource cgroups
3 *
4 * Copyright 2007 OpenVZ SWsoft Inc
5 *
6 * Author: Pavel Emelianov <xemul@openvz.org>
7 *
8 */
9
10#include <linux/types.h>
11#include <linux/parser.h>
12#include <linux/fs.h>
Robert P. J. Day1aeb2722008-04-29 00:59:25 -070013#include <linux/slab.h>
Pavel Emelianove552b662008-02-07 00:13:49 -080014#include <linux/res_counter.h>
15#include <linux/uaccess.h>
Paul Menage856c13a2008-07-25 01:47:04 -070016#include <linux/mm.h>
Pavel Emelianove552b662008-02-07 00:13:49 -080017
Balbir Singh28dbc4b2009-01-07 18:08:05 -080018void res_counter_init(struct res_counter *counter, struct res_counter *parent)
Pavel Emelianove552b662008-02-07 00:13:49 -080019{
20 spin_lock_init(&counter->lock);
Daisuke Nishimurac5b947b2009-06-17 16:27:20 -070021 counter->limit = RESOURCE_MAX;
Balbir Singh296c81d2009-09-23 15:56:36 -070022 counter->soft_limit = RESOURCE_MAX;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080023 counter->parent = parent;
Pavel Emelianove552b662008-02-07 00:13:49 -080024}
25
26int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
27{
28 if (counter->usage + val > counter->limit) {
29 counter->failcnt++;
30 return -ENOMEM;
31 }
32
33 counter->usage += val;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070034 if (counter->usage > counter->max_usage)
35 counter->max_usage = counter->usage;
Pavel Emelianove552b662008-02-07 00:13:49 -080036 return 0;
37}
38
Balbir Singh28dbc4b2009-01-07 18:08:05 -080039int res_counter_charge(struct res_counter *counter, unsigned long val,
Balbir Singhf64c3f52009-09-23 15:56:37 -070040 struct res_counter **limit_fail_at,
41 struct res_counter **soft_limit_fail_at)
Pavel Emelianove552b662008-02-07 00:13:49 -080042{
43 int ret;
44 unsigned long flags;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080045 struct res_counter *c, *u;
Pavel Emelianove552b662008-02-07 00:13:49 -080046
Balbir Singh28dbc4b2009-01-07 18:08:05 -080047 *limit_fail_at = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -070048 if (soft_limit_fail_at)
49 *soft_limit_fail_at = NULL;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080050 local_irq_save(flags);
51 for (c = counter; c != NULL; c = c->parent) {
52 spin_lock(&c->lock);
53 ret = res_counter_charge_locked(c, val);
Balbir Singhf64c3f52009-09-23 15:56:37 -070054 /*
55 * With soft limits, we return the highest ancestor
56 * that exceeds its soft limit
57 */
58 if (soft_limit_fail_at &&
59 !res_counter_soft_limit_check_locked(c))
60 *soft_limit_fail_at = c;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080061 spin_unlock(&c->lock);
62 if (ret < 0) {
63 *limit_fail_at = c;
64 goto undo;
65 }
66 }
67 ret = 0;
68 goto done;
69undo:
70 for (u = counter; u != c; u = u->parent) {
71 spin_lock(&u->lock);
72 res_counter_uncharge_locked(u, val);
73 spin_unlock(&u->lock);
74 }
75done:
76 local_irq_restore(flags);
Pavel Emelianove552b662008-02-07 00:13:49 -080077 return ret;
78}
79
80void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
81{
82 if (WARN_ON(counter->usage < val))
83 val = counter->usage;
84
85 counter->usage -= val;
86}
87
Balbir Singhf64c3f52009-09-23 15:56:37 -070088void res_counter_uncharge(struct res_counter *counter, unsigned long val,
89 bool *was_soft_limit_excess)
Pavel Emelianove552b662008-02-07 00:13:49 -080090{
91 unsigned long flags;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080092 struct res_counter *c;
Pavel Emelianove552b662008-02-07 00:13:49 -080093
Balbir Singh28dbc4b2009-01-07 18:08:05 -080094 local_irq_save(flags);
95 for (c = counter; c != NULL; c = c->parent) {
96 spin_lock(&c->lock);
Balbir Singhf64c3f52009-09-23 15:56:37 -070097 if (was_soft_limit_excess)
98 *was_soft_limit_excess =
99 !res_counter_soft_limit_check_locked(c);
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800100 res_counter_uncharge_locked(c, val);
101 spin_unlock(&c->lock);
102 }
103 local_irq_restore(flags);
Pavel Emelianove552b662008-02-07 00:13:49 -0800104}
105
106
Balbir Singh0eea1032008-02-07 00:13:57 -0800107static inline unsigned long long *
108res_counter_member(struct res_counter *counter, int member)
Pavel Emelianove552b662008-02-07 00:13:49 -0800109{
110 switch (member) {
111 case RES_USAGE:
112 return &counter->usage;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700113 case RES_MAX_USAGE:
114 return &counter->max_usage;
Pavel Emelianove552b662008-02-07 00:13:49 -0800115 case RES_LIMIT:
116 return &counter->limit;
117 case RES_FAILCNT:
118 return &counter->failcnt;
Balbir Singh296c81d2009-09-23 15:56:36 -0700119 case RES_SOFT_LIMIT:
120 return &counter->soft_limit;
Pavel Emelianove552b662008-02-07 00:13:49 -0800121 };
122
123 BUG();
124 return NULL;
125}
126
127ssize_t res_counter_read(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -0800128 const char __user *userbuf, size_t nbytes, loff_t *pos,
129 int (*read_strategy)(unsigned long long val, char *st_buf))
Pavel Emelianove552b662008-02-07 00:13:49 -0800130{
Balbir Singh0eea1032008-02-07 00:13:57 -0800131 unsigned long long *val;
Pavel Emelianove552b662008-02-07 00:13:49 -0800132 char buf[64], *s;
133
134 s = buf;
135 val = res_counter_member(counter, member);
Balbir Singh0eea1032008-02-07 00:13:57 -0800136 if (read_strategy)
137 s += read_strategy(*val, s);
138 else
139 s += sprintf(s, "%llu\n", *val);
Pavel Emelianove552b662008-02-07 00:13:49 -0800140 return simple_read_from_buffer((void __user *)userbuf, nbytes,
141 pos, buf, s - buf);
142}
143
Paul Menage2c7eabf2008-04-29 00:59:58 -0700144u64 res_counter_read_u64(struct res_counter *counter, int member)
145{
146 return *res_counter_member(counter, member);
147}
148
Paul Menage856c13a2008-07-25 01:47:04 -0700149int res_counter_memparse_write_strategy(const char *buf,
150 unsigned long long *res)
Pavel Emelianove552b662008-02-07 00:13:49 -0800151{
Paul Menage856c13a2008-07-25 01:47:04 -0700152 char *end;
Daisuke Nishimurac5b947b2009-06-17 16:27:20 -0700153
154 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
155 if (*buf == '-') {
156 *res = simple_strtoull(buf + 1, &end, 10);
157 if (*res != 1 || *end != '\0')
158 return -EINVAL;
159 *res = RESOURCE_MAX;
160 return 0;
161 }
162
Paul Menage856c13a2008-07-25 01:47:04 -0700163 /* FIXME - make memparse() take const char* args */
164 *res = memparse((char *)buf, &end);
165 if (*end != '\0')
166 return -EINVAL;
167
168 *res = PAGE_ALIGN(*res);
169 return 0;
170}
171
172int res_counter_write(struct res_counter *counter, int member,
173 const char *buf, write_strategy_fn write_strategy)
174{
175 char *end;
Balbir Singh0eea1032008-02-07 00:13:57 -0800176 unsigned long flags;
177 unsigned long long tmp, *val;
Pavel Emelianove552b662008-02-07 00:13:49 -0800178
Balbir Singh0eea1032008-02-07 00:13:57 -0800179 if (write_strategy) {
Paul Menage856c13a2008-07-25 01:47:04 -0700180 if (write_strategy(buf, &tmp))
181 return -EINVAL;
Balbir Singh0eea1032008-02-07 00:13:57 -0800182 } else {
183 tmp = simple_strtoull(buf, &end, 10);
184 if (*end != '\0')
Paul Menage856c13a2008-07-25 01:47:04 -0700185 return -EINVAL;
Balbir Singh0eea1032008-02-07 00:13:57 -0800186 }
187 spin_lock_irqsave(&counter->lock, flags);
Pavel Emelianove552b662008-02-07 00:13:49 -0800188 val = res_counter_member(counter, member);
189 *val = tmp;
Balbir Singh0eea1032008-02-07 00:13:57 -0800190 spin_unlock_irqrestore(&counter->lock, flags);
Paul Menage856c13a2008-07-25 01:47:04 -0700191 return 0;
Pavel Emelianove552b662008-02-07 00:13:49 -0800192}