]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/res_counter.h
109d1183ebf29bba4b90e49ce302a59a601894ab
[karo-tx-linux.git] / include / linux / res_counter.h
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  *
12  * See Documentation/cgroups/resource_counter.txt for more
13  * info about what this counter is.
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          */
28         unsigned long long usage;
29         /*
30          * the maximal value of the usage from the counter creation
31          */
32         unsigned long long max_usage;
33         /*
34          * the limit that usage cannot exceed
35          */
36         unsigned long long limit;
37         /*
38          * the limit that usage can be exceed
39          */
40         unsigned long long soft_limit;
41         /*
42          * the number of unsuccessful attempts to consume the resource
43          */
44         unsigned long long failcnt;
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;
50         /*
51          * Parent counter, used for hierarchial resource accounting
52          */
53         struct res_counter *parent;
54 };
55
56 #define RESOURCE_MAX (unsigned long long)LLONG_MAX
57
58 /**
59  * Helpers to interact with userspace
60  * res_counter_read_u64() - returns the value of the specified member.
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
71 u64 res_counter_read_u64(struct res_counter *counter, int member);
72
73 ssize_t res_counter_read(struct res_counter *counter, int member,
74                 const char __user *buf, size_t nbytes, loff_t *pos,
75                 int (*read_strategy)(unsigned long long val, char *s));
76
77 typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
78
79 int res_counter_memparse_write_strategy(const char *buf,
80                                         unsigned long long *res);
81
82 int res_counter_write(struct res_counter *counter, int member,
83                       const char *buffer, write_strategy_fn write_strategy);
84
85 void res_counter_write_u64(struct res_counter *counter, int member, u64 val);
86
87 void res_counter_inherit(struct res_counter *counter, int member);
88
89 /*
90  * the field descriptors. one for each member of res_counter
91  */
92
93 enum {
94         RES_USAGE,
95         RES_MAX_USAGE,
96         RES_LIMIT,
97         RES_FAILCNT,
98         RES_SOFT_LIMIT,
99 };
100
101 /*
102  * helpers for accounting
103  */
104
105 void res_counter_init(struct res_counter *counter, struct res_counter *parent);
106
107 /*
108  * charge - try to consume more resource.
109  *
110  * @counter: the counter
111  * @val: the amount of the resource. each controller defines its own
112  *       units, e.g. numbers, bytes, Kbytes, etc
113  *
114  * returns 0 on success and <0 if the counter->usage will exceed the
115  * counter->limit _locked call expects the counter->lock to be taken
116  */
117
118 int __must_check res_counter_charge_locked(struct res_counter *counter,
119                 unsigned long val);
120 int __must_check res_counter_charge(struct res_counter *counter,
121                 unsigned long val, struct res_counter **limit_fail_at);
122
123 /*
124  * uncharge - tell that some portion of the resource is released
125  *
126  * @counter: the counter
127  * @val: the amount of the resource
128  *
129  * these calls check for usage underflow and show a warning on the console
130  * _locked call expects the counter->lock to be taken
131  */
132
133 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
134 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
135
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)
144 {
145         unsigned long long margin;
146         unsigned long flags;
147
148         spin_lock_irqsave(&cnt->lock, flags);
149         margin = cnt->limit - cnt->usage;
150         spin_unlock_irqrestore(&cnt->lock, flags);
151         return margin;
152 }
153
154 /**
155  * Get the difference between the usage and the soft limit
156  * @cnt: The counter
157  *
158  * Returns 0 if usage is less than or equal to soft limit
159  * The difference between usage and soft limit, otherwise.
160  */
161 static inline unsigned long long
162 res_counter_soft_limit_excess(struct res_counter *cnt)
163 {
164         unsigned long long excess;
165         unsigned long flags;
166
167         spin_lock_irqsave(&cnt->lock, flags);
168         if (cnt->usage <= cnt->soft_limit)
169                 excess = 0;
170         else
171                 excess = cnt->usage - cnt->soft_limit;
172         spin_unlock_irqrestore(&cnt->lock, flags);
173         return excess;
174 }
175
176 static inline void res_counter_reset_max(struct res_counter *cnt)
177 {
178         unsigned long flags;
179
180         spin_lock_irqsave(&cnt->lock, flags);
181         cnt->max_usage = cnt->usage;
182         spin_unlock_irqrestore(&cnt->lock, flags);
183 }
184
185 static inline void res_counter_reset_failcnt(struct res_counter *cnt)
186 {
187         unsigned long flags;
188
189         spin_lock_irqsave(&cnt->lock, flags);
190         cnt->failcnt = 0;
191         spin_unlock_irqrestore(&cnt->lock, flags);
192 }
193
194 static inline int res_counter_set_limit(struct res_counter *cnt,
195                 unsigned long long limit)
196 {
197         unsigned long flags;
198         int ret = -EBUSY;
199
200         spin_lock_irqsave(&cnt->lock, flags);
201         if (cnt->usage <= limit) {
202                 cnt->limit = limit;
203                 ret = 0;
204         }
205         spin_unlock_irqrestore(&cnt->lock, flags);
206         return ret;
207 }
208
209 static inline int
210 res_counter_set_soft_limit(struct res_counter *cnt,
211                                 unsigned long long soft_limit)
212 {
213         unsigned long flags;
214
215         spin_lock_irqsave(&cnt->lock, flags);
216         cnt->soft_limit = soft_limit;
217         spin_unlock_irqrestore(&cnt->lock, flags);
218         return 0;
219 }
220
221 #endif