memory controller: soft limit organize cgroups
[linux-block.git] / kernel / res_counter.c
CommitLineData
e552b661
PE
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>
1aeb272c 13#include <linux/slab.h>
e552b661
PE
14#include <linux/res_counter.h>
15#include <linux/uaccess.h>
856c13aa 16#include <linux/mm.h>
e552b661 17
28dbc4b6 18void res_counter_init(struct res_counter *counter, struct res_counter *parent)
e552b661
PE
19{
20 spin_lock_init(&counter->lock);
c5b947b2 21 counter->limit = RESOURCE_MAX;
296c81d8 22 counter->soft_limit = RESOURCE_MAX;
28dbc4b6 23 counter->parent = parent;
e552b661
PE
24}
25
26int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
27{
28 if (counter->usage + val > counter->limit) {
29 counter->failcnt++;
30 return -ENOMEM;
31 }
32
33 counter->usage += val;
c84872e1
PE
34 if (counter->usage > counter->max_usage)
35 counter->max_usage = counter->usage;
e552b661
PE
36 return 0;
37}
38
28dbc4b6 39int res_counter_charge(struct res_counter *counter, unsigned long val,
f64c3f54
BS
40 struct res_counter **limit_fail_at,
41 struct res_counter **soft_limit_fail_at)
e552b661
PE
42{
43 int ret;
44 unsigned long flags;
28dbc4b6 45 struct res_counter *c, *u;
e552b661 46
28dbc4b6 47 *limit_fail_at = NULL;
f64c3f54
BS
48 if (soft_limit_fail_at)
49 *soft_limit_fail_at = NULL;
28dbc4b6
BS
50 local_irq_save(flags);
51 for (c = counter; c != NULL; c = c->parent) {
52 spin_lock(&c->lock);
53 ret = res_counter_charge_locked(c, val);
f64c3f54
BS
54 /*
55 * With soft limits, we return the highest ancestor
56 * that exceeds its soft limit
57 */
58 if (soft_limit_fail_at &&
59 !res_counter_soft_limit_check_locked(c))
60 *soft_limit_fail_at = c;
28dbc4b6
BS
61 spin_unlock(&c->lock);
62 if (ret < 0) {
63 *limit_fail_at = c;
64 goto undo;
65 }
66 }
67 ret = 0;
68 goto done;
69undo:
70 for (u = counter; u != c; u = u->parent) {
71 spin_lock(&u->lock);
72 res_counter_uncharge_locked(u, val);
73 spin_unlock(&u->lock);
74 }
75done:
76 local_irq_restore(flags);
e552b661
PE
77 return ret;
78}
79
80void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
81{
82 if (WARN_ON(counter->usage < val))
83 val = counter->usage;
84
85 counter->usage -= val;
86}
87
f64c3f54
BS
88void res_counter_uncharge(struct res_counter *counter, unsigned long val,
89 bool *was_soft_limit_excess)
e552b661
PE
90{
91 unsigned long flags;
28dbc4b6 92 struct res_counter *c;
e552b661 93
28dbc4b6
BS
94 local_irq_save(flags);
95 for (c = counter; c != NULL; c = c->parent) {
96 spin_lock(&c->lock);
f64c3f54
BS
97 if (was_soft_limit_excess)
98 *was_soft_limit_excess =
99 !res_counter_soft_limit_check_locked(c);
28dbc4b6
BS
100 res_counter_uncharge_locked(c, val);
101 spin_unlock(&c->lock);
102 }
103 local_irq_restore(flags);
e552b661
PE
104}
105
106
0eea1030
BS
107static inline unsigned long long *
108res_counter_member(struct res_counter *counter, int member)
e552b661
PE
109{
110 switch (member) {
111 case RES_USAGE:
112 return &counter->usage;
c84872e1
PE
113 case RES_MAX_USAGE:
114 return &counter->max_usage;
e552b661
PE
115 case RES_LIMIT:
116 return &counter->limit;
117 case RES_FAILCNT:
118 return &counter->failcnt;
296c81d8
BS
119 case RES_SOFT_LIMIT:
120 return &counter->soft_limit;
e552b661
PE
121 };
122
123 BUG();
124 return NULL;
125}
126
127ssize_t res_counter_read(struct res_counter *counter, int member,
0eea1030
BS
128 const char __user *userbuf, size_t nbytes, loff_t *pos,
129 int (*read_strategy)(unsigned long long val, char *st_buf))
e552b661 130{
0eea1030 131 unsigned long long *val;
e552b661
PE
132 char buf[64], *s;
133
134 s = buf;
135 val = res_counter_member(counter, member);
0eea1030
BS
136 if (read_strategy)
137 s += read_strategy(*val, s);
138 else
139 s += sprintf(s, "%llu\n", *val);
e552b661
PE
140 return simple_read_from_buffer((void __user *)userbuf, nbytes,
141 pos, buf, s - buf);
142}
143
2c7eabf3
PM
144u64 res_counter_read_u64(struct res_counter *counter, int member)
145{
146 return *res_counter_member(counter, member);
147}
148
856c13aa
PM
149int res_counter_memparse_write_strategy(const char *buf,
150 unsigned long long *res)
e552b661 151{
856c13aa 152 char *end;
c5b947b2
DN
153
154 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
155 if (*buf == '-') {
156 *res = simple_strtoull(buf + 1, &end, 10);
157 if (*res != 1 || *end != '\0')
158 return -EINVAL;
159 *res = RESOURCE_MAX;
160 return 0;
161 }
162
856c13aa
PM
163 /* FIXME - make memparse() take const char* args */
164 *res = memparse((char *)buf, &end);
165 if (*end != '\0')
166 return -EINVAL;
e552b661 167
856c13aa
PM
168 *res = PAGE_ALIGN(*res);
169 return 0;
170}
e552b661 171
856c13aa
PM
172int res_counter_write(struct res_counter *counter, int member,
173 const char *buf, write_strategy_fn write_strategy)
174{
175 char *end;
176 unsigned long flags;
177 unsigned long long tmp, *val;
e552b661 178
0eea1030 179 if (write_strategy) {
856c13aa
PM
180 if (write_strategy(buf, &tmp))
181 return -EINVAL;
0eea1030
BS
182 } else {
183 tmp = simple_strtoull(buf, &end, 10);
184 if (*end != '\0')
856c13aa 185 return -EINVAL;
0eea1030
BS
186 }
187 spin_lock_irqsave(&counter->lock, flags);
e552b661
PE
188 val = res_counter_member(counter, member);
189 *val = tmp;
0eea1030 190 spin_unlock_irqrestore(&counter->lock, flags);
856c13aa 191 return 0;
e552b661 192}