| 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 |  | 
 | 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 | 	 */ | 
| Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 28 | 	unsigned long long usage; | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 29 | 	/* | 
| Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 30 | 	 * the maximal value of the usage from the counter creation | 
 | 31 | 	 */ | 
 | 32 | 	unsigned long long max_usage; | 
 | 33 | 	/* | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 34 | 	 * the limit that usage cannot exceed | 
 | 35 | 	 */ | 
| Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 36 | 	unsigned long long limit; | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 37 | 	/* | 
 | 38 | 	 * the number of unsuccessful attempts to consume the resource | 
 | 39 | 	 */ | 
| Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 40 | 	unsigned long long failcnt; | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 41 | 	/* | 
 | 42 | 	 * the lock to protect all of the above. | 
 | 43 | 	 * the routines below consider this to be IRQ-safe | 
 | 44 | 	 */ | 
 | 45 | 	spinlock_t lock; | 
| Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 46 | 	/* | 
 | 47 | 	 * Parent counter, used for hierarchial resource accounting | 
 | 48 | 	 */ | 
 | 49 | 	struct res_counter *parent; | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 50 | }; | 
 | 51 |  | 
| Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 52 | #define RESOURCE_MAX (unsigned long long)LLONG_MAX | 
 | 53 |  | 
| Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 54 | /** | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 55 |  * Helpers to interact with userspace | 
| Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 56 |  * res_counter_read_u64() - returns the value of the specified member. | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 57 |  * res_counter_read/_write - put/get the specified fields from the | 
 | 58 |  * res_counter struct to/from the user | 
 | 59 |  * | 
 | 60 |  * @counter:     the counter in question | 
 | 61 |  * @member:  the field to work with (see RES_xxx below) | 
 | 62 |  * @buf:     the buffer to opeate on,... | 
 | 63 |  * @nbytes:  its size... | 
 | 64 |  * @pos:     and the offset. | 
 | 65 |  */ | 
 | 66 |  | 
| Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 67 | u64 res_counter_read_u64(struct res_counter *counter, int member); | 
 | 68 |  | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 69 | ssize_t res_counter_read(struct res_counter *counter, int member, | 
| Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 70 | 		const char __user *buf, size_t nbytes, loff_t *pos, | 
 | 71 | 		int (*read_strategy)(unsigned long long val, char *s)); | 
| Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 72 |  | 
 | 73 | typedef int (*write_strategy_fn)(const char *buf, unsigned long long *val); | 
 | 74 |  | 
 | 75 | int res_counter_memparse_write_strategy(const char *buf, | 
 | 76 | 					unsigned long long *res); | 
 | 77 |  | 
 | 78 | int res_counter_write(struct res_counter *counter, int member, | 
 | 79 | 		      const char *buffer, write_strategy_fn write_strategy); | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 80 |  | 
 | 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, | 
 | 90 | }; | 
 | 91 |  | 
 | 92 | /* | 
 | 93 |  * helpers for accounting | 
 | 94 |  */ | 
 | 95 |  | 
| Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 96 | void res_counter_init(struct res_counter *counter, struct res_counter *parent); | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 97 |  | 
 | 98 | /* | 
 | 99 |  * charge - try to consume more resource. | 
 | 100 |  * | 
 | 101 |  * @counter: the counter | 
 | 102 |  * @val: the amount of the resource. each controller defines its own | 
 | 103 |  *       units, e.g. numbers, bytes, Kbytes, etc | 
 | 104 |  * | 
 | 105 |  * returns 0 on success and <0 if the counter->usage will exceed the | 
 | 106 |  * counter->limit _locked call expects the counter->lock to be taken | 
 | 107 |  */ | 
 | 108 |  | 
| Pavel Emelyanov | f2992db | 2008-07-25 01:46:55 -0700 | [diff] [blame] | 109 | int __must_check res_counter_charge_locked(struct res_counter *counter, | 
 | 110 | 		unsigned long val); | 
 | 111 | int __must_check res_counter_charge(struct res_counter *counter, | 
| Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 112 | 		unsigned long val, struct res_counter **limit_fail_at); | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 113 |  | 
 | 114 | /* | 
 | 115 |  * uncharge - tell that some portion of the resource is released | 
 | 116 |  * | 
 | 117 |  * @counter: the counter | 
 | 118 |  * @val: the amount of the resource | 
 | 119 |  * | 
 | 120 |  * these calls check for usage underflow and show a warning on the console | 
 | 121 |  * _locked call expects the counter->lock to be taken | 
 | 122 |  */ | 
 | 123 |  | 
 | 124 | void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val); | 
 | 125 | void res_counter_uncharge(struct res_counter *counter, unsigned long val); | 
 | 126 |  | 
| Balbir Singh | 66e1707 | 2008-02-07 00:13:56 -0800 | [diff] [blame] | 127 | static inline bool res_counter_limit_check_locked(struct res_counter *cnt) | 
 | 128 | { | 
 | 129 | 	if (cnt->usage < cnt->limit) | 
 | 130 | 		return true; | 
 | 131 |  | 
 | 132 | 	return false; | 
 | 133 | } | 
 | 134 |  | 
 | 135 | /* | 
 | 136 |  * Helper function to detect if the cgroup is within it's limit or | 
 | 137 |  * not. It's currently called from cgroup_rss_prepare() | 
 | 138 |  */ | 
 | 139 | static inline bool res_counter_check_under_limit(struct res_counter *cnt) | 
 | 140 | { | 
 | 141 | 	bool ret; | 
 | 142 | 	unsigned long flags; | 
 | 143 |  | 
 | 144 | 	spin_lock_irqsave(&cnt->lock, flags); | 
 | 145 | 	ret = res_counter_limit_check_locked(cnt); | 
 | 146 | 	spin_unlock_irqrestore(&cnt->lock, flags); | 
 | 147 | 	return ret; | 
 | 148 | } | 
 | 149 |  | 
| Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 150 | static inline void res_counter_reset_max(struct res_counter *cnt) | 
 | 151 | { | 
 | 152 | 	unsigned long flags; | 
 | 153 |  | 
 | 154 | 	spin_lock_irqsave(&cnt->lock, flags); | 
 | 155 | 	cnt->max_usage = cnt->usage; | 
 | 156 | 	spin_unlock_irqrestore(&cnt->lock, flags); | 
 | 157 | } | 
 | 158 |  | 
| Pavel Emelyanov | 29f2a4d | 2008-04-29 01:00:21 -0700 | [diff] [blame] | 159 | static inline void res_counter_reset_failcnt(struct res_counter *cnt) | 
 | 160 | { | 
 | 161 | 	unsigned long flags; | 
 | 162 |  | 
 | 163 | 	spin_lock_irqsave(&cnt->lock, flags); | 
 | 164 | 	cnt->failcnt = 0; | 
 | 165 | 	spin_unlock_irqrestore(&cnt->lock, flags); | 
 | 166 | } | 
| KAMEZAWA Hiroyuki | 12b9804 | 2008-07-25 01:47:19 -0700 | [diff] [blame] | 167 |  | 
 | 168 | static inline int res_counter_set_limit(struct res_counter *cnt, | 
 | 169 | 		unsigned long long limit) | 
 | 170 | { | 
 | 171 | 	unsigned long flags; | 
 | 172 | 	int ret = -EBUSY; | 
 | 173 |  | 
 | 174 | 	spin_lock_irqsave(&cnt->lock, flags); | 
| Li Zefan | 11d55d2 | 2008-09-05 14:00:18 -0700 | [diff] [blame] | 175 | 	if (cnt->usage <= limit) { | 
| KAMEZAWA Hiroyuki | 12b9804 | 2008-07-25 01:47:19 -0700 | [diff] [blame] | 176 | 		cnt->limit = limit; | 
 | 177 | 		ret = 0; | 
 | 178 | 	} | 
 | 179 | 	spin_unlock_irqrestore(&cnt->lock, flags); | 
 | 180 | 	return ret; | 
 | 181 | } | 
 | 182 |  | 
| Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 183 | #endif |