blob: a83a849bf1d360f713b1449a6c993815c4800048 [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
Li Zefan92598262013-03-05 11:37:50 +080016#include <linux/spinlock.h>
Pavel Emelianove552b662008-02-07 00:13:49 -080017
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
Paul Menage856c13a2008-07-25 01:47:04 -070077int res_counter_memparse_write_strategy(const char *buf,
78 unsigned long long *res);
79
Pavel Emelianove552b662008-02-07 00:13:49 -080080/*
81 * the field descriptors. one for each member of res_counter
82 */
83
84enum {
85 RES_USAGE,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070086 RES_MAX_USAGE,
Pavel Emelianove552b662008-02-07 00:13:49 -080087 RES_LIMIT,
88 RES_FAILCNT,
Balbir Singh296c81d2009-09-23 15:56:36 -070089 RES_SOFT_LIMIT,
Pavel Emelianove552b662008-02-07 00:13:49 -080090};
91
92/*
93 * helpers for accounting
94 */
95
Balbir Singh28dbc4b2009-01-07 18:08:05 -080096void res_counter_init(struct res_counter *counter, struct res_counter *parent);
Pavel Emelianove552b662008-02-07 00:13:49 -080097
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
Glauber Costa0e90b312012-01-20 04:57:16 +0000107 *
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.
Pavel Emelianove552b662008-02-07 00:13:49 -0800111 */
112
Pavel Emelyanovf2992db2008-07-25 01:46:55 -0700113int __must_check res_counter_charge_locked(struct res_counter *counter,
Frederic Weisbecker4d8438f2012-04-25 01:11:35 +0200114 unsigned long val, bool force);
Pavel Emelyanovf2992db2008-07-25 01:46:55 -0700115int __must_check res_counter_charge(struct res_counter *counter,
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700116 unsigned long val, struct res_counter **limit_fail_at);
Glauber Costa04eac7f2012-05-29 15:07:05 -0700117int res_counter_charge_nofail(struct res_counter *counter,
Glauber Costa0e90b312012-01-20 04:57:16 +0000118 unsigned long val, struct res_counter **limit_fail_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
Glauber Costa50bdd432012-12-18 14:22:04 -0800128 *
129 * returns the total charges still present in @counter.
Pavel Emelianove552b662008-02-07 00:13:49 -0800130 */
131
Glauber Costa50bdd432012-12-18 14:22:04 -0800132u64 res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
133u64 res_counter_uncharge(struct res_counter *counter, unsigned long val);
Pavel Emelianove552b662008-02-07 00:13:49 -0800134
Glauber Costa50bdd432012-12-18 14:22:04 -0800135u64 res_counter_uncharge_until(struct res_counter *counter,
136 struct res_counter *top,
137 unsigned long val);
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700138/**
139 * res_counter_margin - calculate chargeable space of a counter
140 * @cnt: the counter
141 *
142 * Returns the difference between the hard limit and the current usage
143 * of resource counter @cnt.
144 */
145static inline unsigned long long res_counter_margin(struct res_counter *cnt)
Balbir Singh66e17072008-02-07 00:13:56 -0800146{
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700147 unsigned long long margin;
148 unsigned long flags;
Balbir Singh66e17072008-02-07 00:13:56 -0800149
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700150 spin_lock_irqsave(&cnt->lock, flags);
Glauber Costa8cfd14a2012-01-20 04:57:15 +0000151 if (cnt->limit > cnt->usage)
152 margin = cnt->limit - cnt->usage;
153 else
154 margin = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700155 spin_unlock_irqrestore(&cnt->lock, flags);
156 return margin;
Balbir Singh296c81d2009-09-23 15:56:36 -0700157}
158
159/**
160 * Get the difference between the usage and the soft limit
161 * @cnt: The counter
162 *
163 * Returns 0 if usage is less than or equal to soft limit
164 * The difference between usage and soft limit, otherwise.
165 */
166static inline unsigned long long
167res_counter_soft_limit_excess(struct res_counter *cnt)
168{
169 unsigned long long excess;
170 unsigned long flags;
171
172 spin_lock_irqsave(&cnt->lock, flags);
173 if (cnt->usage <= cnt->soft_limit)
174 excess = 0;
175 else
176 excess = cnt->usage - cnt->soft_limit;
177 spin_unlock_irqrestore(&cnt->lock, flags);
178 return excess;
179}
180
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700181static inline void res_counter_reset_max(struct res_counter *cnt)
182{
183 unsigned long flags;
184
185 spin_lock_irqsave(&cnt->lock, flags);
186 cnt->max_usage = cnt->usage;
187 spin_unlock_irqrestore(&cnt->lock, flags);
188}
189
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700190static inline void res_counter_reset_failcnt(struct res_counter *cnt)
191{
192 unsigned long flags;
193
194 spin_lock_irqsave(&cnt->lock, flags);
195 cnt->failcnt = 0;
196 spin_unlock_irqrestore(&cnt->lock, flags);
197}
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700198
199static inline int res_counter_set_limit(struct res_counter *cnt,
200 unsigned long long limit)
201{
202 unsigned long flags;
203 int ret = -EBUSY;
204
205 spin_lock_irqsave(&cnt->lock, flags);
Li Zefan11d55d22008-09-05 14:00:18 -0700206 if (cnt->usage <= limit) {
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700207 cnt->limit = limit;
208 ret = 0;
209 }
210 spin_unlock_irqrestore(&cnt->lock, flags);
211 return ret;
212}
213
Balbir Singh296c81d2009-09-23 15:56:36 -0700214static inline int
215res_counter_set_soft_limit(struct res_counter *cnt,
216 unsigned long long soft_limit)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&cnt->lock, flags);
221 cnt->soft_limit = soft_limit;
222 spin_unlock_irqrestore(&cnt->lock, flags);
223 return 0;
224}
225
Pavel Emelianove552b662008-02-07 00:13:49 -0800226#endif