]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/res_counter.c
cgroups: ability to stop res charge propagation on bounded ancestor
[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 -ENOMEM;
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         *limit_fail_at = NULL;
47         local_irq_save(flags);
48         for (c = counter; c != limit; c = c->parent) {
49                 spin_lock(&c->lock);
50                 ret = res_counter_charge_locked(c, val);
51                 spin_unlock(&c->lock);
52                 if (ret < 0) {
53                         *limit_fail_at = c;
54                         goto undo;
55                 }
56         }
57         ret = 0;
58         goto done;
59 undo:
60         for (u = counter; u != c; u = u->parent) {
61                 spin_lock(&u->lock);
62                 res_counter_uncharge_locked(u, val);
63                 spin_unlock(&u->lock);
64         }
65 done:
66         local_irq_restore(flags);
67         return ret;
68 }
69
70 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
71 {
72         if (WARN_ON(counter->usage < val))
73                 val = counter->usage;
74
75         counter->usage -= val;
76 }
77
78 void res_counter_uncharge_until(struct res_counter *counter,
79                                 struct res_counter *limit,
80                                 unsigned long val)
81 {
82         unsigned long flags;
83         struct res_counter *c;
84
85         local_irq_save(flags);
86         for (c = counter; c != limit; c = c->parent) {
87                 spin_lock(&c->lock);
88                 res_counter_uncharge_locked(c, val);
89                 spin_unlock(&c->lock);
90         }
91         local_irq_restore(flags);
92 }
93
94
95 static inline unsigned long long *
96 res_counter_member(struct res_counter *counter, int member)
97 {
98         switch (member) {
99         case RES_USAGE:
100                 return &counter->usage;
101         case RES_MAX_USAGE:
102                 return &counter->max_usage;
103         case RES_LIMIT:
104                 return &counter->limit;
105         case RES_FAILCNT:
106                 return &counter->failcnt;
107         case RES_SOFT_LIMIT:
108                 return &counter->soft_limit;
109         };
110
111         BUG();
112         return NULL;
113 }
114
115 ssize_t res_counter_read(struct res_counter *counter, int member,
116                 const char __user *userbuf, size_t nbytes, loff_t *pos,
117                 int (*read_strategy)(unsigned long long val, char *st_buf))
118 {
119         unsigned long long *val;
120         char buf[64], *s;
121
122         s = buf;
123         val = res_counter_member(counter, member);
124         if (read_strategy)
125                 s += read_strategy(*val, s);
126         else
127                 s += sprintf(s, "%llu\n", *val);
128         return simple_read_from_buffer((void __user *)userbuf, nbytes,
129                         pos, buf, s - buf);
130 }
131
132 #if BITS_PER_LONG == 32
133 u64 res_counter_read_u64(struct res_counter *counter, int member)
134 {
135         unsigned long flags;
136         u64 ret;
137
138         spin_lock_irqsave(&counter->lock, flags);
139         ret = *res_counter_member(counter, member);
140         spin_unlock_irqrestore(&counter->lock, flags);
141
142         return ret;
143 }
144 #else
145 u64 res_counter_read_u64(struct res_counter *counter, int member)
146 {
147         return *res_counter_member(counter, member);
148 }
149 #endif
150
151 int res_counter_memparse_write_strategy(const char *buf,
152                                         unsigned long long *res)
153 {
154         char *end;
155
156         /* return RESOURCE_MAX(unlimited) if "-1" is specified */
157         if (*buf == '-') {
158                 *res = simple_strtoull(buf + 1, &end, 10);
159                 if (*res != 1 || *end != '\0')
160                         return -EINVAL;
161                 *res = RESOURCE_MAX;
162                 return 0;
163         }
164
165         /* FIXME - make memparse() take const char* args */
166         *res = memparse((char *)buf, &end);
167         if (*end != '\0')
168                 return -EINVAL;
169
170         *res = PAGE_ALIGN(*res);
171         return 0;
172 }
173
174 void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
175 {
176         unsigned long long *target;
177         unsigned long flags;
178
179         /*
180          * We need the lock to protect against concurrent add/dec on 32 bits.
181          * No need to ifdef it's seldom used.
182          */
183         spin_lock_irqsave(&counter->lock, flags);
184         target = res_counter_member(counter, member);
185         *target = val;
186         spin_unlock_irqrestore(&counter->lock, flags);
187 }
188
189 int res_counter_write(struct res_counter *counter, int member,
190                       const char *buf, write_strategy_fn write_strategy)
191 {
192         char *end;
193         unsigned long long tmp;
194
195         if (write_strategy) {
196                 if (write_strategy(buf, &tmp))
197                         return -EINVAL;
198         } else {
199                 tmp = simple_strtoull(buf, &end, 10);
200                 if (*end != '\0')
201                         return -EINVAL;
202         }
203
204         res_counter_write_u64(counter, member, tmp);
205
206         return 0;
207 }
208
209 /*
210  * Simple inheritance implementation to get the same value
211  * than a parent. However this doesn't enforce the child value
212  * to be always below the one of the parent. But the child is
213  * subject to its parent limitation anyway.
214  */
215 void res_counter_inherit(struct res_counter *counter, int member)
216 {
217         struct res_counter *parent;
218         unsigned long long val;
219
220         parent = counter->parent;
221         if (parent) {
222                 val = res_counter_read_u64(parent, member);
223                 res_counter_write_u64(counter, member, val);
224         }
225 }