Commit | Line | Data |
---|---|---|
e552b661 PE |
1 | #ifndef __RES_COUNTER_H__ |
2 | #define __RES_COUNTER_H__ | |
3 | ||
4 | /* | |
5 | * Resource Counters | |
6 | * Contain common data types and routines for resource accounting | |
7 | * | |
8 | * Copyright 2007 OpenVZ SWsoft Inc | |
9 | * | |
10 | * Author: Pavel Emelianov <xemul@openvz.org> | |
11 | * | |
45ce80fb | 12 | * See Documentation/cgroups/resource_counter.txt for more |
faebe9fd | 13 | * info about what this counter is. |
e552b661 PE |
14 | */ |
15 | ||
9259826c | 16 | #include <linux/spinlock.h> |
ebf47beb | 17 | #include <linux/errno.h> |
e552b661 PE |
18 | |
19 | /* | |
20 | * The core object. the cgroup that wishes to account for some | |
21 | * resource may include this counter into its structures and use | |
22 | * the helpers described beyond | |
23 | */ | |
24 | ||
25 | struct res_counter { | |
26 | /* | |
27 | * the current resource consumption level | |
28 | */ | |
0eea1030 | 29 | unsigned long long usage; |
c84872e1 PE |
30 | /* |
31 | * the maximal value of the usage from the counter creation | |
32 | */ | |
33 | unsigned long long max_usage; | |
e552b661 PE |
34 | /* |
35 | * the limit that usage cannot exceed | |
36 | */ | |
0eea1030 | 37 | unsigned long long limit; |
296c81d8 BS |
38 | /* |
39 | * the limit that usage can be exceed | |
40 | */ | |
41 | unsigned long long soft_limit; | |
e552b661 PE |
42 | /* |
43 | * the number of unsuccessful attempts to consume the resource | |
44 | */ | |
0eea1030 | 45 | unsigned long long failcnt; |
e552b661 PE |
46 | /* |
47 | * the lock to protect all of the above. | |
48 | * the routines below consider this to be IRQ-safe | |
49 | */ | |
50 | spinlock_t lock; | |
28dbc4b6 BS |
51 | /* |
52 | * Parent counter, used for hierarchial resource accounting | |
53 | */ | |
54 | struct res_counter *parent; | |
e552b661 PE |
55 | }; |
56 | ||
6de5a8bf | 57 | #define RES_COUNTER_MAX ULLONG_MAX |
c5b947b2 | 58 | |
2c7eabf3 | 59 | /** |
e552b661 | 60 | * Helpers to interact with userspace |
2c7eabf3 | 61 | * res_counter_read_u64() - returns the value of the specified member. |
e552b661 PE |
62 | * res_counter_read/_write - put/get the specified fields from the |
63 | * res_counter struct to/from the user | |
64 | * | |
65 | * @counter: the counter in question | |
66 | * @member: the field to work with (see RES_xxx below) | |
67 | * @buf: the buffer to opeate on,... | |
68 | * @nbytes: its size... | |
69 | * @pos: and the offset. | |
70 | */ | |
71 | ||
2c7eabf3 PM |
72 | u64 res_counter_read_u64(struct res_counter *counter, int member); |
73 | ||
e552b661 | 74 | ssize_t res_counter_read(struct res_counter *counter, int member, |
0eea1030 BS |
75 | const char __user *buf, size_t nbytes, loff_t *pos, |
76 | int (*read_strategy)(unsigned long long val, char *s)); | |
856c13aa | 77 | |
856c13aa PM |
78 | int res_counter_memparse_write_strategy(const char *buf, |
79 | unsigned long long *res); | |
80 | ||
e552b661 PE |
81 | /* |
82 | * the field descriptors. one for each member of res_counter | |
83 | */ | |
84 | ||
85 | enum { | |
86 | RES_USAGE, | |
c84872e1 | 87 | RES_MAX_USAGE, |
e552b661 PE |
88 | RES_LIMIT, |
89 | RES_FAILCNT, | |
296c81d8 | 90 | RES_SOFT_LIMIT, |
e552b661 PE |
91 | }; |
92 | ||
93 | /* | |
94 | * helpers for accounting | |
95 | */ | |
96 | ||
28dbc4b6 | 97 | void res_counter_init(struct res_counter *counter, struct res_counter *parent); |
e552b661 PE |
98 | |
99 | /* | |
100 | * charge - try to consume more resource. | |
101 | * | |
102 | * @counter: the counter | |
103 | * @val: the amount of the resource. each controller defines its own | |
104 | * units, e.g. numbers, bytes, Kbytes, etc | |
105 | * | |
106 | * returns 0 on success and <0 if the counter->usage will exceed the | |
539a13b4 | 107 | * counter->limit |
0e90b31f GC |
108 | * |
109 | * charge_nofail works the same, except that it charges the resource | |
110 | * counter unconditionally, and returns < 0 if the after the current | |
111 | * charge we are over limit. | |
e552b661 PE |
112 | */ |
113 | ||
f2992db2 | 114 | int __must_check res_counter_charge(struct res_counter *counter, |
4e649152 | 115 | unsigned long val, struct res_counter **limit_fail_at); |
04eac7ff | 116 | int res_counter_charge_nofail(struct res_counter *counter, |
0e90b31f | 117 | unsigned long val, struct res_counter **limit_fail_at); |
e552b661 PE |
118 | |
119 | /* | |
120 | * uncharge - tell that some portion of the resource is released | |
121 | * | |
122 | * @counter: the counter | |
123 | * @val: the amount of the resource | |
124 | * | |
125 | * these calls check for usage underflow and show a warning on the console | |
50bdd430 GC |
126 | * |
127 | * returns the total charges still present in @counter. | |
e552b661 PE |
128 | */ |
129 | ||
50bdd430 | 130 | u64 res_counter_uncharge(struct res_counter *counter, unsigned long val); |
e552b661 | 131 | |
50bdd430 GC |
132 | u64 res_counter_uncharge_until(struct res_counter *counter, |
133 | struct res_counter *top, | |
134 | unsigned long val); | |
9d11ea9f JW |
135 | /** |
136 | * res_counter_margin - calculate chargeable space of a counter | |
137 | * @cnt: the counter | |
138 | * | |
139 | * Returns the difference between the hard limit and the current usage | |
140 | * of resource counter @cnt. | |
141 | */ | |
142 | static inline unsigned long long res_counter_margin(struct res_counter *cnt) | |
296c81d8 | 143 | { |
9d11ea9f JW |
144 | unsigned long long margin; |
145 | unsigned long flags; | |
296c81d8 | 146 | |
9d11ea9f | 147 | spin_lock_irqsave(&cnt->lock, flags); |
8cfd14ad GC |
148 | if (cnt->limit > cnt->usage) |
149 | margin = cnt->limit - cnt->usage; | |
150 | else | |
151 | margin = 0; | |
9d11ea9f JW |
152 | spin_unlock_irqrestore(&cnt->lock, flags); |
153 | return margin; | |
296c81d8 BS |
154 | } |
155 | ||
156 | /** | |
157 | * Get the difference between the usage and the soft limit | |
158 | * @cnt: The counter | |
159 | * | |
160 | * Returns 0 if usage is less than or equal to soft limit | |
161 | * The difference between usage and soft limit, otherwise. | |
162 | */ | |
163 | static inline unsigned long long | |
164 | res_counter_soft_limit_excess(struct res_counter *cnt) | |
165 | { | |
166 | unsigned long long excess; | |
167 | unsigned long flags; | |
168 | ||
169 | spin_lock_irqsave(&cnt->lock, flags); | |
170 | if (cnt->usage <= cnt->soft_limit) | |
171 | excess = 0; | |
172 | else | |
173 | excess = cnt->usage - cnt->soft_limit; | |
174 | spin_unlock_irqrestore(&cnt->lock, flags); | |
175 | return excess; | |
176 | } | |
177 | ||
c84872e1 PE |
178 | static inline void res_counter_reset_max(struct res_counter *cnt) |
179 | { | |
180 | unsigned long flags; | |
181 | ||
182 | spin_lock_irqsave(&cnt->lock, flags); | |
183 | cnt->max_usage = cnt->usage; | |
184 | spin_unlock_irqrestore(&cnt->lock, flags); | |
185 | } | |
186 | ||
29f2a4da PE |
187 | static inline void res_counter_reset_failcnt(struct res_counter *cnt) |
188 | { | |
189 | unsigned long flags; | |
190 | ||
191 | spin_lock_irqsave(&cnt->lock, flags); | |
192 | cnt->failcnt = 0; | |
193 | spin_unlock_irqrestore(&cnt->lock, flags); | |
194 | } | |
12b98044 KH |
195 | |
196 | static inline int res_counter_set_limit(struct res_counter *cnt, | |
197 | unsigned long long limit) | |
198 | { | |
199 | unsigned long flags; | |
200 | int ret = -EBUSY; | |
201 | ||
202 | spin_lock_irqsave(&cnt->lock, flags); | |
11d55d2c | 203 | if (cnt->usage <= limit) { |
12b98044 KH |
204 | cnt->limit = limit; |
205 | ret = 0; | |
206 | } | |
207 | spin_unlock_irqrestore(&cnt->lock, flags); | |
208 | return ret; | |
209 | } | |
210 | ||
296c81d8 BS |
211 | static inline int |
212 | res_counter_set_soft_limit(struct res_counter *cnt, | |
213 | unsigned long long soft_limit) | |
214 | { | |
215 | unsigned long flags; | |
216 | ||
217 | spin_lock_irqsave(&cnt->lock, flags); | |
218 | cnt->soft_limit = soft_limit; | |
219 | spin_unlock_irqrestore(&cnt->lock, flags); | |
220 | return 0; | |
221 | } | |
222 | ||
e552b661 | 223 | #endif |