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