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;
46 *limit_fail_at = NULL;
47 local_irq_save(flags);
48 for (c = counter; c != limit; c = c->parent) {
50 ret = res_counter_charge_locked(c, val);
51 spin_unlock(&c->lock);
60 for (u = counter; u != c; u = u->parent) {
62 res_counter_uncharge_locked(u, val);
63 spin_unlock(&u->lock);
66 local_irq_restore(flags);
70 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
72 if (WARN_ON(counter->usage < val))
75 counter->usage -= val;
78 void res_counter_uncharge_until(struct res_counter *counter,
79 struct res_counter *limit,
83 struct res_counter *c;
85 local_irq_save(flags);
86 for (c = counter; c != limit; c = c->parent) {
88 res_counter_uncharge_locked(c, val);
89 spin_unlock(&c->lock);
91 local_irq_restore(flags);
95 static inline unsigned long long *
96 res_counter_member(struct res_counter *counter, int member)
100 return &counter->usage;
102 return &counter->max_usage;
104 return &counter->limit;
106 return &counter->failcnt;
108 return &counter->soft_limit;
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))
119 unsigned long long *val;
123 val = res_counter_member(counter, member);
125 s += read_strategy(*val, s);
127 s += sprintf(s, "%llu\n", *val);
128 return simple_read_from_buffer((void __user *)userbuf, nbytes,
132 #if BITS_PER_LONG == 32
133 u64 res_counter_read_u64(struct res_counter *counter, int member)
138 spin_lock_irqsave(&counter->lock, flags);
139 ret = *res_counter_member(counter, member);
140 spin_unlock_irqrestore(&counter->lock, flags);
145 u64 res_counter_read_u64(struct res_counter *counter, int member)
147 return *res_counter_member(counter, member);
151 int res_counter_memparse_write_strategy(const char *buf,
152 unsigned long long *res)
156 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
158 *res = simple_strtoull(buf + 1, &end, 10);
159 if (*res != 1 || *end != '\0')
165 /* FIXME - make memparse() take const char* args */
166 *res = memparse((char *)buf, &end);
170 *res = PAGE_ALIGN(*res);
174 void res_counter_write_u64(struct res_counter *counter, int member, u64 val)
176 unsigned long long *target;
180 * We need the lock to protect against concurrent add/dec on 32 bits.
181 * No need to ifdef it's seldom used.
183 spin_lock_irqsave(&counter->lock, flags);
184 target = res_counter_member(counter, member);
186 spin_unlock_irqrestore(&counter->lock, flags);
189 int res_counter_write(struct res_counter *counter, int member,
190 const char *buf, write_strategy_fn write_strategy)
193 unsigned long long tmp;
195 if (write_strategy) {
196 if (write_strategy(buf, &tmp))
199 tmp = simple_strtoull(buf, &end, 10);
204 res_counter_write_u64(counter, member, tmp);
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.
215 void res_counter_inherit(struct res_counter *counter, int member)
217 struct res_counter *parent;
218 unsigned long long val;
220 parent = counter->parent;
222 val = res_counter_read_u64(parent, member);
223 res_counter_write_u64(counter, member, val);