Merge branch 'firewire-kernel-streaming' of git://git.alsa-project.org/alsa-kprivate
[firewire-audio.git] / include / linux / res_counter.h
bloba5930cb6614577517223ba642854b78ba339197b
1 #ifndef __RES_COUNTER_H__
2 #define __RES_COUNTER_H__
4 /*
5 * Resource Counters
6 * Contain common data types and routines for resource accounting
8 * Copyright 2007 OpenVZ SWsoft Inc
10 * Author: Pavel Emelianov <xemul@openvz.org>
12 * See Documentation/cgroups/resource_counter.txt for more
13 * info about what this counter is.
16 #include <linux/cgroup.h>
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
24 struct res_counter {
26 * the current resource consumption level
28 unsigned long long usage;
30 * the maximal value of the usage from the counter creation
32 unsigned long long max_usage;
34 * the limit that usage cannot exceed
36 unsigned long long limit;
38 * the limit that usage can be exceed
40 unsigned long long soft_limit;
42 * the number of unsuccessful attempts to consume the resource
44 unsigned long long failcnt;
46 * the lock to protect all of the above.
47 * the routines below consider this to be IRQ-safe
49 spinlock_t lock;
51 * Parent counter, used for hierarchial resource accounting
53 struct res_counter *parent;
56 #define RESOURCE_MAX (unsigned long long)LLONG_MAX
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
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.
71 u64 res_counter_read_u64(struct res_counter *counter, int member);
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));
77 typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val);
79 int res_counter_memparse_write_strategy(const char *buf,
80 unsigned long long *res);
82 int res_counter_write(struct res_counter *counter, int member,
83 const char *buffer, write_strategy_fn write_strategy);
86 * the field descriptors. one for each member of res_counter
89 enum {
90 RES_USAGE,
91 RES_MAX_USAGE,
92 RES_LIMIT,
93 RES_FAILCNT,
94 RES_SOFT_LIMIT,
98 * helpers for accounting
101 void res_counter_init(struct res_counter *counter, struct res_counter *parent);
104 * charge - try to consume more resource.
106 * @counter: the counter
107 * @val: the amount of the resource. each controller defines its own
108 * units, e.g. numbers, bytes, Kbytes, etc
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
114 int __must_check res_counter_charge_locked(struct res_counter *counter,
115 unsigned long val);
116 int __must_check res_counter_charge(struct res_counter *counter,
117 unsigned long val, struct res_counter **limit_fail_at);
120 * uncharge - tell that some portion of the resource is released
122 * @counter: the counter
123 * @val: the amount of the resource
125 * these calls check for usage underflow and show a warning on the console
126 * _locked call expects the counter->lock to be taken
129 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
130 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
132 static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
134 if (cnt->usage < cnt->limit)
135 return true;
137 return false;
140 static inline bool res_counter_soft_limit_check_locked(struct res_counter *cnt)
142 if (cnt->usage < cnt->soft_limit)
143 return true;
145 return false;
149 * Get the difference between the usage and the soft limit
150 * @cnt: The counter
152 * Returns 0 if usage is less than or equal to soft limit
153 * The difference between usage and soft limit, otherwise.
155 static inline unsigned long long
156 res_counter_soft_limit_excess(struct res_counter *cnt)
158 unsigned long long excess;
159 unsigned long flags;
161 spin_lock_irqsave(&cnt->lock, flags);
162 if (cnt->usage <= cnt->soft_limit)
163 excess = 0;
164 else
165 excess = cnt->usage - cnt->soft_limit;
166 spin_unlock_irqrestore(&cnt->lock, flags);
167 return excess;
171 * Helper function to detect if the cgroup is within it's limit or
172 * not. It's currently called from cgroup_rss_prepare()
174 static inline bool res_counter_check_under_limit(struct res_counter *cnt)
176 bool ret;
177 unsigned long flags;
179 spin_lock_irqsave(&cnt->lock, flags);
180 ret = res_counter_limit_check_locked(cnt);
181 spin_unlock_irqrestore(&cnt->lock, flags);
182 return ret;
186 * res_counter_check_margin - check if the counter allows charging
187 * @cnt: the resource counter to check
188 * @bytes: the number of bytes to check the remaining space against
190 * Returns a boolean value on whether the counter can be charged
191 * @bytes or whether this would exceed the limit.
193 static inline bool res_counter_check_margin(struct res_counter *cnt,
194 unsigned long bytes)
196 bool ret;
197 unsigned long flags;
199 spin_lock_irqsave(&cnt->lock, flags);
200 ret = cnt->limit - cnt->usage >= bytes;
201 spin_unlock_irqrestore(&cnt->lock, flags);
202 return ret;
205 static inline bool res_counter_check_under_soft_limit(struct res_counter *cnt)
207 bool ret;
208 unsigned long flags;
210 spin_lock_irqsave(&cnt->lock, flags);
211 ret = res_counter_soft_limit_check_locked(cnt);
212 spin_unlock_irqrestore(&cnt->lock, flags);
213 return ret;
216 static inline void res_counter_reset_max(struct res_counter *cnt)
218 unsigned long flags;
220 spin_lock_irqsave(&cnt->lock, flags);
221 cnt->max_usage = cnt->usage;
222 spin_unlock_irqrestore(&cnt->lock, flags);
225 static inline void res_counter_reset_failcnt(struct res_counter *cnt)
227 unsigned long flags;
229 spin_lock_irqsave(&cnt->lock, flags);
230 cnt->failcnt = 0;
231 spin_unlock_irqrestore(&cnt->lock, flags);
234 static inline int res_counter_set_limit(struct res_counter *cnt,
235 unsigned long long limit)
237 unsigned long flags;
238 int ret = -EBUSY;
240 spin_lock_irqsave(&cnt->lock, flags);
241 if (cnt->usage <= limit) {
242 cnt->limit = limit;
243 ret = 0;
245 spin_unlock_irqrestore(&cnt->lock, flags);
246 return ret;
249 static inline int
250 res_counter_set_soft_limit(struct res_counter *cnt,
251 unsigned long long soft_limit)
253 unsigned long flags;
255 spin_lock_irqsave(&cnt->lock, flags);
256 cnt->soft_limit = soft_limit;
257 spin_unlock_irqrestore(&cnt->lock, flags);
258 return 0;
261 #endif