4 * Copyright 2007 OpenVZ SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
10 #include <linux/types.h>
11 #include <linux/parser.h>
13 #include <linux/res_counter.h>
14 #include <linux/uaccess.h>
17 void res_counter_init(struct res_counter *counter, struct res_counter *parent)
19 spin_lock_init(&counter->lock);
20 counter->limit = RESOURCE_MAX;
21 counter->soft_limit = RESOURCE_MAX;
22 counter->parent = parent;
25 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
27 if (counter->usage + val > counter->limit) {
32 counter->usage += val;
33 if (counter->usage > counter->max_usage)
34 counter->max_usage = counter->usage;
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)
44 struct res_counter *c, *u;
47 *limit_fail_at = NULL;
48 local_irq_save(flags);
49 for (c = counter; c != limit; c = c->parent) {
51 ret = res_counter_charge_locked(c, val);
52 spin_unlock(&c->lock);
62 for (u = counter; u != c; u = u->parent) {
64 res_counter_uncharge_locked(u, val);
65 spin_unlock(&u->lock);
68 local_irq_restore(flags);
72 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
74 if (WARN_ON(counter->usage < val))
77 counter->usage -= val;
80 void res_counter_uncharge_until(struct res_counter *counter,
81 struct res_counter *limit,
85 struct res_counter *c;
87 local_irq_save(flags);
88 for (c = counter; c != limit; c = c->parent) {
90 res_counter_uncharge_locked(c, val);
91 spin_unlock(&c->lock);
93 local_irq_restore(flags);
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.
101 res_counter_common_ancestor(struct res_counter *r1, struct res_counter *r2)
103 struct res_counter *iter;
104 int r1_depth = 0, r2_depth = 0;
106 for (iter = r1; iter; iter = iter->parent)
109 for (iter = r2; iter; iter = iter->parent)
112 while (r1_depth > r2_depth) {
117 while (r2_depth > r1_depth) {
130 static inline unsigned long long *
131 res_counter_member(struct res_counter *counter, int member)
135 return &counter->usage;
137 return &counter->max_usage;
139 return &counter->limit;
141 return &counter->failcnt;
143 return &counter->soft_limit;
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))
154 unsigned long long *val;
158 val = res_counter_member(counter, member);
160 s += read_strategy(*val, s);
162 s += sprintf(s, "%llu\n", *val);
163 return simple_read_from_buffer((void __user *)userbuf, nbytes,
167 #if BITS_PER_LONG == 32
168 u64 res_counter_read_u64(struct res_counter *counter, int member)
173 spin_lock_irqsave(&counter->lock, flags);
174 ret = *res_counter_member(counter, member);
175 spin_unlock_irqrestore(&counter->lock, flags);
180 u64 res_counter_read_u64(struct res_counter *counter, int member)
182 return *res_counter_member(counter, member);
186 int res_counter_memparse_write_strategy(const char *buf,
187 unsigned long long *res)
191 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
193 *res = simple_strtoull(buf + 1, &end, 10);
194 if (*res != 1 || *end != '\0')
200 /* FIXME - make memparse() take const char* args */
201 *res = memparse((char *)buf, &end);
205 *res = PAGE_ALIGN(*res);
209 void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
211 unsigned long long *target;
215 * We need the lock to protect against concurrent add/dec on 32 bits.
216 * No need to ifdef it's seldom used.
218 spin_lock_irqsave(&counter->lock, flags);
219 target = res_counter_member(counter, member);
221 spin_unlock_irqrestore(&counter->lock, flags);
224 int res_counter_write(struct res_counter *counter, int member,
225 const char *buf, write_strategy_fn write_strategy)
228 unsigned long long tmp;
230 if (write_strategy) {
231 if (write_strategy(buf, &tmp))
234 tmp = simple_strtoull(buf, &end, 10);
239 res_counter_write_u64(counter, member, tmp);
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.
250 void res_counter_inherit(struct res_counter *counter, int member)
252 struct res_counter *parent;
253 unsigned long long val;
255 parent = counter->parent;
257 val = res_counter_read_u64(parent, member);
258 res_counter_write_u64(counter, member, val);