net: introduce res_counter_charge_nofail() for socket allocations
[linux-2.6/libata-dev.git] / include / linux / res_counter.h
blobda81af086eaf765ea701247cd332944ba7b3c570
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
113 * charge_nofail works the same, except that it charges the resource
114 * counter unconditionally, and returns < 0 if the after the current
115 * charge we are over limit.
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 int __must_check res_counter_charge_nofail(struct res_counter *counter,
123 unsigned long val, struct res_counter **limit_fail_at);
126 * uncharge - tell that some portion of the resource is released
128 * @counter: the counter
129 * @val: the amount of the resource
131 * these calls check for usage underflow and show a warning on the console
132 * _locked call expects the counter->lock to be taken
135 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
136 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
139 * res_counter_margin - calculate chargeable space of a counter
140 * @cnt: the counter
142 * Returns the difference between the hard limit and the current usage
143 * of resource counter @cnt.
145 static inline unsigned long long res_counter_margin(struct res_counter *cnt)
147 unsigned long long margin;
148 unsigned long flags;
150 spin_lock_irqsave(&cnt->lock, flags);
151 if (cnt->limit > cnt->usage)
152 margin = cnt->limit - cnt->usage;
153 else
154 margin = 0;
155 spin_unlock_irqrestore(&cnt->lock, flags);
156 return margin;
160 * Get the difference between the usage and the soft limit
161 * @cnt: The counter
163 * Returns 0 if usage is less than or equal to soft limit
164 * The difference between usage and soft limit, otherwise.
166 static inline unsigned long long
167 res_counter_soft_limit_excess(struct res_counter *cnt)
169 unsigned long long excess;
170 unsigned long flags;
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;
181 static inline void res_counter_reset_max(struct res_counter *cnt)
183 unsigned long flags;
185 spin_lock_irqsave(&cnt->lock, flags);
186 cnt->max_usage = cnt->usage;
187 spin_unlock_irqrestore(&cnt->lock, flags);
190 static inline void res_counter_reset_failcnt(struct res_counter *cnt)
192 unsigned long flags;
194 spin_lock_irqsave(&cnt->lock, flags);
195 cnt->failcnt = 0;
196 spin_unlock_irqrestore(&cnt->lock, flags);
199 static inline int res_counter_set_limit(struct res_counter *cnt,
200 unsigned long long limit)
202 unsigned long flags;
203 int ret = -EBUSY;
205 spin_lock_irqsave(&cnt->lock, flags);
206 if (cnt->usage <= limit) {
207 cnt->limit = limit;
208 ret = 0;
210 spin_unlock_irqrestore(&cnt->lock, flags);
211 return ret;
214 static inline int
215 res_counter_set_soft_limit(struct res_counter *cnt,
216 unsigned long long soft_limit)
218 unsigned long flags;
220 spin_lock_irqsave(&cnt->lock, flags);
221 cnt->soft_limit = soft_limit;
222 spin_unlock_irqrestore(&cnt->lock, flags);
223 return 0;
226 #endif