blob: 731af71cddc9b1a8f3fb326f08f4d0bec048e08e [file] [log] [blame]
Pavel Emelianove552b662008-02-07 00:13:49 -08001#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 *
Li Zefan45ce80f2009-01-15 13:50:59 -080012 * See Documentation/cgroups/resource_counter.txt for more
Pavel Emelyanovfaebe9f2008-04-29 01:00:18 -070013 * info about what this counter is.
Pavel Emelianove552b662008-02-07 00:13:49 -080014 */
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
24struct res_counter {
25 /*
26 * the current resource consumption level
27 */
Balbir Singh0eea1032008-02-07 00:13:57 -080028 unsigned long long usage;
Pavel Emelianove552b662008-02-07 00:13:49 -080029 /*
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070030 * the maximal value of the usage from the counter creation
31 */
32 unsigned long long max_usage;
33 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080034 * the limit that usage cannot exceed
35 */
Balbir Singh0eea1032008-02-07 00:13:57 -080036 unsigned long long limit;
Pavel Emelianove552b662008-02-07 00:13:49 -080037 /*
Balbir Singh296c81d2009-09-23 15:56:36 -070038 * the limit that usage can be exceed
39 */
40 unsigned long long soft_limit;
41 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080042 * the number of unsuccessful attempts to consume the resource
43 */
Balbir Singh0eea1032008-02-07 00:13:57 -080044 unsigned long long failcnt;
Pavel Emelianove552b662008-02-07 00:13:49 -080045 /*
46 * the lock to protect all of the above.
47 * the routines below consider this to be IRQ-safe
48 */
49 spinlock_t lock;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080050 /*
51 * Parent counter, used for hierarchial resource accounting
52 */
53 struct res_counter *parent;
Pavel Emelianove552b662008-02-07 00:13:49 -080054};
55
Daisuke Nishimurac5b947b2009-06-17 16:27:20 -070056#define RESOURCE_MAX (unsigned long long)LLONG_MAX
57
Paul Menage2c7eabf2008-04-29 00:59:58 -070058/**
Pavel Emelianove552b662008-02-07 00:13:49 -080059 * Helpers to interact with userspace
Paul Menage2c7eabf2008-04-29 00:59:58 -070060 * res_counter_read_u64() - returns the value of the specified member.
Pavel Emelianove552b662008-02-07 00:13:49 -080061 * 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
Paul Menage2c7eabf2008-04-29 00:59:58 -070071u64 res_counter_read_u64(struct res_counter *counter, int member);
72
Pavel Emelianove552b662008-02-07 00:13:49 -080073ssize_t res_counter_read(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -080074 const char __user *buf, size_t nbytes, loff_t *pos,
75 int (*read_strategy)(unsigned long long val, char *s));
Paul Menage856c13a2008-07-25 01:47:04 -070076
77typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
78
79int res_counter_memparse_write_strategy(const char *buf,
80 unsigned long long *res);
81
82int res_counter_write(struct res_counter *counter, int member,
83 const char *buffer, write_strategy_fn write_strategy);
Pavel Emelianove552b662008-02-07 00:13:49 -080084
85/*
86 * the field descriptors. one for each member of res_counter
87 */
88
89enum {
90 RES_USAGE,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070091 RES_MAX_USAGE,
Pavel Emelianove552b662008-02-07 00:13:49 -080092 RES_LIMIT,
93 RES_FAILCNT,
Balbir Singh296c81d2009-09-23 15:56:36 -070094 RES_SOFT_LIMIT,
Pavel Emelianove552b662008-02-07 00:13:49 -080095};
96
97/*
98 * helpers for accounting
99 */
100
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800101void res_counter_init(struct res_counter *counter, struct res_counter *parent);
Pavel Emelianove552b662008-02-07 00:13:49 -0800102
103/*
104 * charge - try to consume more resource.
105 *
106 * @counter: the counter
107 * @val: the amount of the resource. each controller defines its own
108 * units, e.g. numbers, bytes, Kbytes, etc
109 *
110 * returns 0 on success and <0 if the counter->usage will exceed the
111 * counter->limit _locked call expects the counter->lock to be taken
112 */
113
Pavel Emelyanovf2992db2008-07-25 01:46:55 -0700114int __must_check res_counter_charge_locked(struct res_counter *counter,
115 unsigned long val);
116int __must_check res_counter_charge(struct res_counter *counter,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700117 unsigned long val, struct res_counter **limit_fail_at,
118 struct res_counter **soft_limit_at);
Pavel Emelianove552b662008-02-07 00:13:49 -0800119
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
130void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700131void res_counter_uncharge(struct res_counter *counter, unsigned long val,
132 bool *was_soft_limit_excess);
Pavel Emelianove552b662008-02-07 00:13:49 -0800133
Balbir Singh66e17072008-02-07 00:13:56 -0800134static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
135{
136 if (cnt->usage < cnt->limit)
137 return true;
138
139 return false;
140}
141
Balbir Singh296c81d2009-09-23 15:56:36 -0700142static inline bool res_counter_soft_limit_check_locked(struct res_counter *cnt)
143{
144 if (cnt->usage < cnt->soft_limit)
145 return true;
146
147 return false;
148}
149
150/**
151 * Get the difference between the usage and the soft limit
152 * @cnt: The counter
153 *
154 * Returns 0 if usage is less than or equal to soft limit
155 * The difference between usage and soft limit, otherwise.
156 */
157static inline unsigned long long
158res_counter_soft_limit_excess(struct res_counter *cnt)
159{
160 unsigned long long excess;
161 unsigned long flags;
162
163 spin_lock_irqsave(&cnt->lock, flags);
164 if (cnt->usage <= cnt->soft_limit)
165 excess = 0;
166 else
167 excess = cnt->usage - cnt->soft_limit;
168 spin_unlock_irqrestore(&cnt->lock, flags);
169 return excess;
170}
171
Balbir Singh66e17072008-02-07 00:13:56 -0800172/*
173 * Helper function to detect if the cgroup is within it's limit or
174 * not. It's currently called from cgroup_rss_prepare()
175 */
176static inline bool res_counter_check_under_limit(struct res_counter *cnt)
177{
178 bool ret;
179 unsigned long flags;
180
181 spin_lock_irqsave(&cnt->lock, flags);
182 ret = res_counter_limit_check_locked(cnt);
183 spin_unlock_irqrestore(&cnt->lock, flags);
184 return ret;
185}
186
Balbir Singh296c81d2009-09-23 15:56:36 -0700187static inline bool res_counter_check_under_soft_limit(struct res_counter *cnt)
188{
189 bool ret;
190 unsigned long flags;
191
192 spin_lock_irqsave(&cnt->lock, flags);
193 ret = res_counter_soft_limit_check_locked(cnt);
194 spin_unlock_irqrestore(&cnt->lock, flags);
195 return ret;
196}
197
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700198static inline void res_counter_reset_max(struct res_counter *cnt)
199{
200 unsigned long flags;
201
202 spin_lock_irqsave(&cnt->lock, flags);
203 cnt->max_usage = cnt->usage;
204 spin_unlock_irqrestore(&cnt->lock, flags);
205}
206
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700207static inline void res_counter_reset_failcnt(struct res_counter *cnt)
208{
209 unsigned long flags;
210
211 spin_lock_irqsave(&cnt->lock, flags);
212 cnt->failcnt = 0;
213 spin_unlock_irqrestore(&cnt->lock, flags);
214}
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700215
216static inline int res_counter_set_limit(struct res_counter *cnt,
217 unsigned long long limit)
218{
219 unsigned long flags;
220 int ret = -EBUSY;
221
222 spin_lock_irqsave(&cnt->lock, flags);
Li Zefan11d55d22008-09-05 14:00:18 -0700223 if (cnt->usage <= limit) {
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700224 cnt->limit = limit;
225 ret = 0;
226 }
227 spin_unlock_irqrestore(&cnt->lock, flags);
228 return ret;
229}
230
Balbir Singh296c81d2009-09-23 15:56:36 -0700231static inline int
232res_counter_set_soft_limit(struct res_counter *cnt,
233 unsigned long long soft_limit)
234{
235 unsigned long flags;
236
237 spin_lock_irqsave(&cnt->lock, flags);
238 cnt->soft_limit = soft_limit;
239 spin_unlock_irqrestore(&cnt->lock, flags);
240 return 0;
241}
242
Pavel Emelianove552b662008-02-07 00:13:49 -0800243#endif