blob: 56b7bc32db4f89aa924e5c02ae929af3a32fe83d [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>
Andrew Mortonebf47be2013-03-13 14:59:30 -070017#include <linux/errno.h>
Pavel Emelianove552b662008-02-07 00:13:49 -080018
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
25struct res_counter {
26 /*
27 * the current resource consumption level
28 */
Balbir Singh0eea1032008-02-07 00:13:57 -080029 unsigned long long usage;
Pavel Emelianove552b662008-02-07 00:13:49 -080030 /*
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070031 * the maximal value of the usage from the counter creation
32 */
33 unsigned long long max_usage;
34 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080035 * the limit that usage cannot exceed
36 */
Balbir Singh0eea1032008-02-07 00:13:57 -080037 unsigned long long limit;
Pavel Emelianove552b662008-02-07 00:13:49 -080038 /*
Balbir Singh296c81d2009-09-23 15:56:36 -070039 * the limit that usage can be exceed
40 */
41 unsigned long long soft_limit;
42 /*
Pavel Emelianove552b662008-02-07 00:13:49 -080043 * the number of unsuccessful attempts to consume the resource
44 */
Balbir Singh0eea1032008-02-07 00:13:57 -080045 unsigned long long failcnt;
Pavel Emelianove552b662008-02-07 00:13:49 -080046 /*
47 * the lock to protect all of the above.
48 * the routines below consider this to be IRQ-safe
49 */
50 spinlock_t lock;
Balbir Singh28dbc4b2009-01-07 18:08:05 -080051 /*
52 * Parent counter, used for hierarchial resource accounting
53 */
54 struct res_counter *parent;
Pavel Emelianove552b662008-02-07 00:13:49 -080055};
56
Sha Zhengju6de5a8b2013-09-12 15:13:47 -070057#define RES_COUNTER_MAX ULLONG_MAX
Daisuke Nishimurac5b947b2009-06-17 16:27:20 -070058
Paul Menage2c7eabf2008-04-29 00:59:58 -070059/**
Pavel Emelianove552b662008-02-07 00:13:49 -080060 * Helpers to interact with userspace
Paul Menage2c7eabf2008-04-29 00:59:58 -070061 * res_counter_read_u64() - returns the value of the specified member.
Pavel Emelianove552b662008-02-07 00:13:49 -080062 * 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
Paul Menage2c7eabf2008-04-29 00:59:58 -070072u64 res_counter_read_u64(struct res_counter *counter, int member);
73
Pavel Emelianove552b662008-02-07 00:13:49 -080074ssize_t res_counter_read(struct res_counter *counter, int member,
Balbir Singh0eea1032008-02-07 00:13:57 -080075 const char __user *buf, size_t nbytes, loff_t *pos,
76 int (*read_strategy)(unsigned long long val, char *s));
Paul Menage856c13a2008-07-25 01:47:04 -070077
Paul Menage856c13a2008-07-25 01:47:04 -070078int res_counter_memparse_write_strategy(const char *buf,
79 unsigned long long *res);
80
Pavel Emelianove552b662008-02-07 00:13:49 -080081/*
82 * the field descriptors. one for each member of res_counter
83 */
84
85enum {
86 RES_USAGE,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -070087 RES_MAX_USAGE,
Pavel Emelianove552b662008-02-07 00:13:49 -080088 RES_LIMIT,
89 RES_FAILCNT,
Balbir Singh296c81d2009-09-23 15:56:36 -070090 RES_SOFT_LIMIT,
Pavel Emelianove552b662008-02-07 00:13:49 -080091};
92
93/*
94 * helpers for accounting
95 */
96
Balbir Singh28dbc4b2009-01-07 18:08:05 -080097void res_counter_init(struct res_counter *counter, struct res_counter *parent);
Pavel Emelianove552b662008-02-07 00:13:49 -080098
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
David Rientjes539a13b2014-04-07 15:37:32 -0700107 * counter->limit
Glauber Costa0e90b312012-01-20 04:57:16 +0000108 *
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.
Pavel Emelianove552b662008-02-07 00:13:49 -0800112 */
113
Pavel Emelyanovf2992db2008-07-25 01:46:55 -0700114int __must_check res_counter_charge(struct res_counter *counter,
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700115 unsigned long val, struct res_counter **limit_fail_at);
Glauber Costa04eac7f2012-05-29 15:07:05 -0700116int res_counter_charge_nofail(struct res_counter *counter,
Glauber Costa0e90b312012-01-20 04:57:16 +0000117 unsigned long val, struct res_counter **limit_fail_at);
Pavel Emelianove552b662008-02-07 00:13:49 -0800118
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
Glauber Costa50bdd432012-12-18 14:22:04 -0800126 *
127 * returns the total charges still present in @counter.
Pavel Emelianove552b662008-02-07 00:13:49 -0800128 */
129
Glauber Costa50bdd432012-12-18 14:22:04 -0800130u64 res_counter_uncharge(struct res_counter *counter, unsigned long val);
Pavel Emelianove552b662008-02-07 00:13:49 -0800131
Glauber Costa50bdd432012-12-18 14:22:04 -0800132u64 res_counter_uncharge_until(struct res_counter *counter,
133 struct res_counter *top,
134 unsigned long val);
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700135/**
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 */
142static inline unsigned long long res_counter_margin(struct res_counter *cnt)
Balbir Singh66e17072008-02-07 00:13:56 -0800143{
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700144 unsigned long long margin;
145 unsigned long flags;
Balbir Singh66e17072008-02-07 00:13:56 -0800146
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700147 spin_lock_irqsave(&cnt->lock, flags);
Glauber Costa8cfd14a2012-01-20 04:57:15 +0000148 if (cnt->limit > cnt->usage)
149 margin = cnt->limit - cnt->usage;
150 else
151 margin = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -0700152 spin_unlock_irqrestore(&cnt->lock, flags);
153 return margin;
Balbir Singh296c81d2009-09-23 15:56:36 -0700154}
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 */
163static inline unsigned long long
164res_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
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700178static 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
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700187static 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}
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700195
196static 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);
Li Zefan11d55d22008-09-05 14:00:18 -0700203 if (cnt->usage <= limit) {
KAMEZAWA Hiroyuki12b98042008-07-25 01:47:19 -0700204 cnt->limit = limit;
205 ret = 0;
206 }
207 spin_unlock_irqrestore(&cnt->lock, flags);
208 return ret;
209}
210
Balbir Singh296c81d2009-09-23 15:56:36 -0700211static inline int
212res_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
Pavel Emelianove552b662008-02-07 00:13:49 -0800223#endif