Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 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 | * |
Li Zefan | 45ce80f | 2009-01-15 13:50:59 -0800 | [diff] [blame] | 12 | * See Documentation/cgroups/resource_counter.txt for more |
Pavel Emelyanov | faebe9f | 2008-04-29 01:00:18 -0700 | [diff] [blame] | 13 | * info about what this counter is. |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Li Zefan | 9259826 | 2013-03-05 11:37:50 +0800 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
Andrew Morton | ebf47be | 2013-03-13 14:59:30 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 18 | |
| 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 | |
| 25 | struct res_counter { |
| 26 | /* |
| 27 | * the current resource consumption level |
| 28 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 29 | unsigned long long usage; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 30 | /* |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 31 | * the maximal value of the usage from the counter creation |
| 32 | */ |
| 33 | unsigned long long max_usage; |
| 34 | /* |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 35 | * the limit that usage cannot exceed |
| 36 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 37 | unsigned long long limit; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 38 | /* |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 39 | * the limit that usage can be exceed |
| 40 | */ |
| 41 | unsigned long long soft_limit; |
| 42 | /* |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 43 | * the number of unsuccessful attempts to consume the resource |
| 44 | */ |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 45 | unsigned long long failcnt; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 46 | /* |
| 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 Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 51 | /* |
| 52 | * Parent counter, used for hierarchial resource accounting |
| 53 | */ |
| 54 | struct res_counter *parent; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 55 | }; |
| 56 | |
Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 57 | #define RESOURCE_MAX (unsigned long long)LLONG_MAX |
| 58 | |
Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 59 | /** |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 60 | * Helpers to interact with userspace |
Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 61 | * res_counter_read_u64() - returns the value of the specified member. |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 62 | * 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 Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 72 | u64 res_counter_read_u64(struct res_counter *counter, int member); |
| 73 | |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 74 | ssize_t res_counter_read(struct res_counter *counter, int member, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 75 | const char __user *buf, size_t nbytes, loff_t *pos, |
| 76 | int (*read_strategy)(unsigned long long val, char *s)); |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 77 | |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 78 | int res_counter_memparse_write_strategy(const char *buf, |
| 79 | unsigned long long *res); |
| 80 | |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 81 | /* |
| 82 | * the field descriptors. one for each member of res_counter |
| 83 | */ |
| 84 | |
| 85 | enum { |
| 86 | RES_USAGE, |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 87 | RES_MAX_USAGE, |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 88 | RES_LIMIT, |
| 89 | RES_FAILCNT, |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 90 | RES_SOFT_LIMIT, |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /* |
| 94 | * helpers for accounting |
| 95 | */ |
| 96 | |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 97 | void res_counter_init(struct res_counter *counter, struct res_counter *parent); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 98 | |
| 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 |
| 107 | * counter->limit _locked call expects the counter->lock to be taken |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 108 | * |
| 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 Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 112 | */ |
| 113 | |
Pavel Emelyanov | f2992db | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 114 | int __must_check res_counter_charge_locked(struct res_counter *counter, |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 115 | unsigned long val, bool force); |
Pavel Emelyanov | f2992db | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 116 | int __must_check res_counter_charge(struct res_counter *counter, |
KAMEZAWA Hiroyuki | 4e64915 | 2009-10-01 15:44:11 -0700 | [diff] [blame] | 117 | unsigned long val, struct res_counter **limit_fail_at); |
Glauber Costa | 04eac7f | 2012-05-29 15:07:05 -0700 | [diff] [blame] | 118 | int res_counter_charge_nofail(struct res_counter *counter, |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 119 | unsigned long val, struct res_counter **limit_fail_at); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * uncharge - tell that some portion of the resource is released |
| 123 | * |
| 124 | * @counter: the counter |
| 125 | * @val: the amount of the resource |
| 126 | * |
| 127 | * these calls check for usage underflow and show a warning on the console |
| 128 | * _locked call expects the counter->lock to be taken |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 129 | * |
| 130 | * returns the total charges still present in @counter. |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 131 | */ |
| 132 | |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 133 | u64 res_counter_uncharge_locked(struct res_counter *counter, unsigned long val); |
| 134 | u64 res_counter_uncharge(struct res_counter *counter, unsigned long val); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 135 | |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 136 | u64 res_counter_uncharge_until(struct res_counter *counter, |
| 137 | struct res_counter *top, |
| 138 | unsigned long val); |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 139 | /** |
| 140 | * res_counter_margin - calculate chargeable space of a counter |
| 141 | * @cnt: the counter |
| 142 | * |
| 143 | * Returns the difference between the hard limit and the current usage |
| 144 | * of resource counter @cnt. |
| 145 | */ |
| 146 | static inline unsigned long long res_counter_margin(struct res_counter *cnt) |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 147 | { |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 148 | unsigned long long margin; |
| 149 | unsigned long flags; |
Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 150 | |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 151 | spin_lock_irqsave(&cnt->lock, flags); |
Glauber Costa | 8cfd14a | 2012-01-20 04:57:15 +0000 | [diff] [blame] | 152 | if (cnt->limit > cnt->usage) |
| 153 | margin = cnt->limit - cnt->usage; |
| 154 | else |
| 155 | margin = 0; |
Johannes Weiner | 9d11ea9 | 2011-03-23 16:42:21 -0700 | [diff] [blame] | 156 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 157 | return margin; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get the difference between the usage and the soft limit |
| 162 | * @cnt: The counter |
| 163 | * |
| 164 | * Returns 0 if usage is less than or equal to soft limit |
| 165 | * The difference between usage and soft limit, otherwise. |
| 166 | */ |
| 167 | static inline unsigned long long |
| 168 | res_counter_soft_limit_excess(struct res_counter *cnt) |
| 169 | { |
| 170 | unsigned long long excess; |
| 171 | unsigned long flags; |
| 172 | |
| 173 | spin_lock_irqsave(&cnt->lock, flags); |
| 174 | if (cnt->usage <= cnt->soft_limit) |
| 175 | excess = 0; |
| 176 | else |
| 177 | excess = cnt->usage - cnt->soft_limit; |
| 178 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 179 | return excess; |
| 180 | } |
| 181 | |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 182 | static inline void res_counter_reset_max(struct res_counter *cnt) |
| 183 | { |
| 184 | unsigned long flags; |
| 185 | |
| 186 | spin_lock_irqsave(&cnt->lock, flags); |
| 187 | cnt->max_usage = cnt->usage; |
| 188 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 189 | } |
| 190 | |
Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 191 | static inline void res_counter_reset_failcnt(struct res_counter *cnt) |
| 192 | { |
| 193 | unsigned long flags; |
| 194 | |
| 195 | spin_lock_irqsave(&cnt->lock, flags); |
| 196 | cnt->failcnt = 0; |
| 197 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 198 | } |
KAMEZAWA Hiroyuki | 12b9804 | 2008-07-25 01:47:19 -0700 | [diff] [blame] | 199 | |
| 200 | static inline int res_counter_set_limit(struct res_counter *cnt, |
| 201 | unsigned long long limit) |
| 202 | { |
| 203 | unsigned long flags; |
| 204 | int ret = -EBUSY; |
| 205 | |
| 206 | spin_lock_irqsave(&cnt->lock, flags); |
Li Zefan | 11d55d2 | 2008-09-05 14:00:18 -0700 | [diff] [blame] | 207 | if (cnt->usage <= limit) { |
KAMEZAWA Hiroyuki | 12b9804 | 2008-07-25 01:47:19 -0700 | [diff] [blame] | 208 | cnt->limit = limit; |
| 209 | ret = 0; |
| 210 | } |
| 211 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 212 | return ret; |
| 213 | } |
| 214 | |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 215 | static inline int |
| 216 | res_counter_set_soft_limit(struct res_counter *cnt, |
| 217 | unsigned long long soft_limit) |
| 218 | { |
| 219 | unsigned long flags; |
| 220 | |
| 221 | spin_lock_irqsave(&cnt->lock, flags); |
| 222 | cnt->soft_limit = soft_limit; |
| 223 | spin_unlock_irqrestore(&cnt->lock, flags); |
| 224 | return 0; |
| 225 | } |
| 226 | |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 227 | #endif |