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/slab.h>
14 #include <linux/res_counter.h>
15 #include <linux/uaccess.h>
18 void res_counter_init(struct res_counter *counter, struct res_counter *parent)
20 spin_lock_init(&counter->lock);
21 counter->limit = RESOURCE_MAX;
22 counter->soft_limit = RESOURCE_MAX;
23 counter->parent = parent;
26 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
28 if (counter->usage + val > counter->limit) {
33 counter->usage += val;
34 if (counter->usage > counter->max_usage)
35 counter->max_usage = counter->usage;
39 int res_counter_charge(struct res_counter *counter, 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 != NULL; 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(struct res_counter *counter, unsigned long val)
81 struct res_counter *c;
83 local_irq_save(flags);
84 for (c = counter; c != NULL; c = c->parent) {
86 res_counter_uncharge_locked(c, val);
87 spin_unlock(&c->lock);
89 local_irq_restore(flags);
93 static inline unsigned long long *
94 res_counter_member(struct res_counter *counter, int member)
98 return &counter->usage;
100 return &counter->max_usage;
102 return &counter->limit;
104 return &counter->failcnt;
106 return &counter->soft_limit;
113 ssize_t res_counter_read(struct res_counter *counter, int member,
114 const char __user *userbuf, size_t nbytes, loff_t *pos,
115 int (*read_strategy)(unsigned long long val, char *st_buf))
117 unsigned long long *val;
121 val = res_counter_member(counter, member);
123 s += read_strategy(*val, s);
125 s += sprintf(s, "%llu\n", *val);
126 return simple_read_from_buffer((void __user *)userbuf, nbytes,
130 u64 res_counter_read_u64(struct res_counter *counter, int member)
132 return *res_counter_member(counter, member);
135 int res_counter_memparse_write_strategy(const char *buf,
136 unsigned long long *res)
140 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
142 *res = simple_strtoull(buf + 1, &end, 10);
143 if (*res != 1 || *end != '\0')
149 /* FIXME - make memparse() take const char* args */
150 *res = memparse((char *)buf, &end);
154 *res = PAGE_ALIGN(*res);
158 int res_counter_write(struct res_counter *counter, int member,
159 const char *buf, write_strategy_fn write_strategy)
163 unsigned long long tmp, *val;
165 if (write_strategy) {
166 if (write_strategy(buf, &tmp))
169 tmp = simple_strtoull(buf, &end, 10);
173 spin_lock_irqsave(&counter->lock, flags);
174 val = res_counter_member(counter, member);
176 spin_unlock_irqrestore(&counter->lock, flags);