]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/res_counter.c
cgroups: pull up res counter charge failure interpretation to caller
[karo-tx-linux.git] / kernel / res_counter.c
1 /*
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>
13 #include <linux/res_counter.h>
14 #include <linux/uaccess.h>
15 #include <linux/mm.h>
16
17 void res_counter_init(struct res_counter *counter, struct res_counter *parent)
18 {
19         spin_lock_init(&counter->lock);
20         counter->limit = RESOURCE_MAX;
21         counter->soft_limit = RESOURCE_MAX;
22         counter->parent = parent;
23 }
24
25 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
26 {
27         if (counter->usage + val > counter->limit) {
28                 counter->failcnt++;
29                 return -1;
30         }
31
32         counter->usage += val;
33         if (counter->usage > counter->max_usage)
34                 counter->max_usage = counter->usage;
35         return 0;
36 }
37
38 int res_counter_charge_until(struct res_counter *counter,
39                              struct res_counter *limit, unsigned long val,
40                              struct res_counter **limit_fail_at)
41 {
42         int ret;
43         unsigned long flags;
44         struct res_counter *c, *u;
45
46         if (limit_fail_at)
47                 *limit_fail_at = NULL;
48         local_irq_save(flags);
49         for (c = counter; c != limit; c = c->parent) {
50                 spin_lock(&c->lock);
51                 ret = res_counter_charge_locked(c, val);
52                 spin_unlock(&c->lock);
53                 if (ret < 0) {
54                         if (limit_fail_at)
55                                 *limit_fail_at = c;
56                         goto undo;
57                 }
58         }
59         ret = 0;
60         goto done;
61 undo:
62         for (u = counter; u != c; u = u->parent) {
63                 spin_lock(&u->lock);
64                 res_counter_uncharge_locked(u, val);
65                 spin_unlock(&u->lock);
66         }
67 done:
68         local_irq_restore(flags);
69         return ret;
70 }
71
72 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
73 {
74         if (WARN_ON(counter->usage < val))
75                 val = counter->usage;
76
77         counter->usage -= val;
78 }
79
80 void res_counter_uncharge_until(struct res_counter *counter,
81                                 struct res_counter *limit,
82                                 unsigned long val)
83 {
84         unsigned long flags;
85         struct res_counter *c;
86
87         local_irq_save(flags);
88         for (c = counter; c != limit; c = c->parent) {
89                 spin_lock(&c->lock);
90                 res_counter_uncharge_locked(c, val);
91                 spin_unlock(&c->lock);
92         }
93         local_irq_restore(flags);
94 }
95
96 /*
97  * Walk through r1 and r2 parents and try to find the closest common one
98  * between both. If none is found, it returns NULL.
99  */
100 struct res_counter *
101 res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
102 {
103         struct res_counter *iter;
104         int r1_depth = 0, r2_depth = 0;
105
106         for (iter = r1; iter; iter = iter->parent)
107                 r1_depth++;
108
109         for (iter = r2; iter; iter = iter->parent)
110                 r2_depth++;
111
112         while (r1_depth > r2_depth) {
113                 r1 = r1->parent;
114                 r1_depth--;
115         }
116
117         while (r2_depth > r1_depth) {
118                 r2 = r2->parent;
119                 r2_depth--;
120         }
121
122         while (r1 != r2) {
123                 r1 = r1->parent;
124                 r2 = r2->parent;
125         }
126
127         return r1;
128 }
129
130 static inline unsigned long long *
131 res_counter_member(struct res_counter *counter, int member)
132 {
133         switch (member) {
134         case RES_USAGE:
135                 return &counter->usage;
136         case RES_MAX_USAGE:
137                 return &counter->max_usage;
138         case RES_LIMIT:
139                 return &counter->limit;
140         case RES_FAILCNT:
141                 return &counter->failcnt;
142         case RES_SOFT_LIMIT:
143                 return &counter->soft_limit;
144         };
145
146         BUG();
147         return NULL;
148 }
149
150 ssize_t res_counter_read(struct res_counter *counter, int member,
151                 const char __user *userbuf, size_t nbytes, loff_t *pos,
152                 int (*read_strategy)(unsigned long long val, char *st_buf))
153 {
154         unsigned long long *val;
155         char buf[64], *s;
156
157         s = buf;
158         val = res_counter_member(counter, member);
159         if (read_strategy)
160                 s += read_strategy(*val, s);
161         else
162                 s += sprintf(s, "%llu\n", *val);
163         return simple_read_from_buffer((void __user *)userbuf, nbytes,
164                         pos, buf, s - buf);
165 }
166
167 #if BITS_PER_LONG == 32
168 u64 res_counter_read_u64(struct res_counter *counter, int member)
169 {
170         unsigned long flags;
171         u64 ret;
172
173         spin_lock_irqsave(&counter->lock, flags);
174         ret = *res_counter_member(counter, member);
175         spin_unlock_irqrestore(&counter->lock, flags);
176
177         return ret;
178 }
179 #else
180 u64 res_counter_read_u64(struct res_counter *counter, int member)
181 {
182         return *res_counter_member(counter, member);
183 }
184 #endif
185
186 int res_counter_memparse_write_strategy(const char *buf,
187                                         unsigned long long *res)
188 {
189         char *end;
190
191         /* return RESOURCE_MAX(unlimited) if "-1" is specified */
192         if (*buf == '-') {
193                 *res = simple_strtoull(buf + 1, &end, 10);
194                 if (*res != 1 || *end != '\0')
195                         return -EINVAL;
196                 *res = RESOURCE_MAX;
197                 return 0;
198         }
199
200         /* FIXME - make memparse() take const char* args */
201         *res = memparse((char *)buf, &end);
202         if (*end != '\0')
203                 return -EINVAL;
204
205         *res = PAGE_ALIGN(*res);
206         return 0;
207 }
208
209 void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
210 {
211         unsigned long long *target;
212         unsigned long flags;
213
214         /*
215          * We need the lock to protect against concurrent add/dec on 32 bits.
216          * No need to ifdef it's seldom used.
217          */
218         spin_lock_irqsave(&counter->lock, flags);
219         target = res_counter_member(counter, member);
220         *target = val;
221         spin_unlock_irqrestore(&counter->lock, flags);
222 }
223
224 int res_counter_write(struct res_counter *counter, int member,
225                       const char *buf, write_strategy_fn write_strategy)
226 {
227         char *end;
228         unsigned long long tmp;
229
230         if (write_strategy) {
231                 if (write_strategy(buf, &tmp))
232                         return -EINVAL;
233         } else {
234                 tmp = simple_strtoull(buf, &end, 10);
235                 if (*end != '\0')
236                         return -EINVAL;
237         }
238
239         res_counter_write_u64(counter, member, tmp);
240
241         return 0;
242 }
243
244 /*
245  * Simple inheritance implementation to get the same value
246  * than a parent. However this doesn't enforce the child value
247  * to be always below the one of the parent. But the child is
248  * subject to its parent limitation anyway.
249  */
250 void res_counter_inherit(struct res_counter *counter, int member)
251 {
252         struct res_counter *parent;
253         unsigned long long val;
254
255         parent = counter->parent;
256         if (parent) {
257                 val = res_counter_read_u64(parent, member);
258                 res_counter_write_u64(counter, member, val);
259         }
260 }