Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * resource cgroups |
| 3 | * |
| 4 | * Copyright 2007 OpenVZ SWsoft Inc |
| 5 | * |
| 6 | * Author: Pavel Emelianov <xemul@openvz.org> |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/parser.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/res_counter.h> |
| 14 | #include <linux/uaccess.h> |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 16 | |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 17 | void res_counter_init(struct res_counter *counter, struct res_counter *parent) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 18 | { |
| 19 | spin_lock_init(&counter->lock); |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 20 | counter->limit = RES_COUNTER_MAX; |
| 21 | counter->soft_limit = RES_COUNTER_MAX; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 22 | counter->parent = parent; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 23 | } |
| 24 | |
David Rientjes | 539a13b | 2014-04-07 15:37:32 -0700 | [diff] [blame] | 25 | static u64 res_counter_uncharge_locked(struct res_counter *counter, |
| 26 | unsigned long val) |
| 27 | { |
| 28 | if (WARN_ON(counter->usage < val)) |
| 29 | val = counter->usage; |
| 30 | |
| 31 | counter->usage -= val; |
| 32 | return counter->usage; |
| 33 | } |
| 34 | |
| 35 | static int res_counter_charge_locked(struct res_counter *counter, |
| 36 | unsigned long val, bool force) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 37 | { |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 38 | int ret = 0; |
| 39 | |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 40 | if (counter->usage + val > counter->limit) { |
| 41 | counter->failcnt++; |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 42 | ret = -ENOMEM; |
| 43 | if (!force) |
| 44 | return ret; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | counter->usage += val; |
Frederic Weisbecker | 0d4dde1 | 2012-04-25 01:11:36 +0200 | [diff] [blame] | 48 | if (counter->usage > counter->max_usage) |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 49 | counter->max_usage = counter->usage; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 50 | return ret; |
| 51 | } |
| 52 | |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 53 | static int __res_counter_charge(struct res_counter *counter, unsigned long val, |
| 54 | struct res_counter **limit_fail_at, bool force) |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 55 | { |
| 56 | int ret, r; |
| 57 | unsigned long flags; |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 58 | struct res_counter *c, *u; |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 59 | |
| 60 | r = ret = 0; |
| 61 | *limit_fail_at = NULL; |
| 62 | local_irq_save(flags); |
| 63 | for (c = counter; c != NULL; c = c->parent) { |
| 64 | spin_lock(&c->lock); |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 65 | r = res_counter_charge_locked(c, val, force); |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 66 | spin_unlock(&c->lock); |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 67 | if (r < 0 && !ret) { |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 68 | ret = r; |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 69 | *limit_fail_at = c; |
| 70 | if (!force) |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (ret < 0 && !force) { |
| 76 | for (u = counter; u != c; u = u->parent) { |
| 77 | spin_lock(&u->lock); |
| 78 | res_counter_uncharge_locked(u, val); |
| 79 | spin_unlock(&u->lock); |
Glauber Costa | 0e90b31 | 2012-01-20 04:57:16 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | local_irq_restore(flags); |
| 83 | |
| 84 | return ret; |
| 85 | } |
Frederic Weisbecker | 4d8438f | 2012-04-25 01:11:35 +0200 | [diff] [blame] | 86 | |
| 87 | int res_counter_charge(struct res_counter *counter, unsigned long val, |
| 88 | struct res_counter **limit_fail_at) |
| 89 | { |
| 90 | return __res_counter_charge(counter, val, limit_fail_at, false); |
| 91 | } |
| 92 | |
| 93 | int res_counter_charge_nofail(struct res_counter *counter, unsigned long val, |
| 94 | struct res_counter **limit_fail_at) |
| 95 | { |
| 96 | return __res_counter_charge(counter, val, limit_fail_at, true); |
| 97 | } |
| 98 | |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 99 | u64 res_counter_uncharge_until(struct res_counter *counter, |
| 100 | struct res_counter *top, |
| 101 | unsigned long val) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 102 | { |
| 103 | unsigned long flags; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 104 | struct res_counter *c; |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 105 | u64 ret = 0; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 106 | |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 107 | local_irq_save(flags); |
Frederic Weisbecker | 2bb2ba9 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 108 | for (c = counter; c != top; c = c->parent) { |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 109 | u64 r; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 110 | spin_lock(&c->lock); |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 111 | r = res_counter_uncharge_locked(c, val); |
| 112 | if (c == counter) |
| 113 | ret = r; |
Balbir Singh | 28dbc4b | 2009-01-07 18:08:05 -0800 | [diff] [blame] | 114 | spin_unlock(&c->lock); |
| 115 | } |
| 116 | local_irq_restore(flags); |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 117 | return ret; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 120 | u64 res_counter_uncharge(struct res_counter *counter, unsigned long val) |
Frederic Weisbecker | 2bb2ba9 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 121 | { |
Glauber Costa | 50bdd43 | 2012-12-18 14:22:04 -0800 | [diff] [blame] | 122 | return res_counter_uncharge_until(counter, NULL, val); |
Frederic Weisbecker | 2bb2ba9 | 2012-05-29 15:07:03 -0700 | [diff] [blame] | 123 | } |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 124 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 125 | static inline unsigned long long * |
| 126 | res_counter_member(struct res_counter *counter, int member) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 127 | { |
| 128 | switch (member) { |
| 129 | case RES_USAGE: |
| 130 | return &counter->usage; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 131 | case RES_MAX_USAGE: |
| 132 | return &counter->max_usage; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 133 | case RES_LIMIT: |
| 134 | return &counter->limit; |
| 135 | case RES_FAILCNT: |
| 136 | return &counter->failcnt; |
Balbir Singh | 296c81d | 2009-09-23 15:56:36 -0700 | [diff] [blame] | 137 | case RES_SOFT_LIMIT: |
| 138 | return &counter->soft_limit; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | BUG(); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | ssize_t res_counter_read(struct res_counter *counter, int member, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 146 | const char __user *userbuf, size_t nbytes, loff_t *pos, |
| 147 | int (*read_strategy)(unsigned long long val, char *st_buf)) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 148 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 149 | unsigned long long *val; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 150 | char buf[64], *s; |
| 151 | |
| 152 | s = buf; |
| 153 | val = res_counter_member(counter, member); |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 154 | if (read_strategy) |
| 155 | s += read_strategy(*val, s); |
| 156 | else |
| 157 | s += sprintf(s, "%llu\n", *val); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 158 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
| 159 | pos, buf, s - buf); |
| 160 | } |
| 161 | |
KAMEZAWA Hiroyuki | 6c191cd | 2011-03-23 16:42:18 -0700 | [diff] [blame] | 162 | #if BITS_PER_LONG == 32 |
| 163 | u64 res_counter_read_u64(struct res_counter *counter, int member) |
| 164 | { |
| 165 | unsigned long flags; |
| 166 | u64 ret; |
| 167 | |
| 168 | spin_lock_irqsave(&counter->lock, flags); |
| 169 | ret = *res_counter_member(counter, member); |
| 170 | spin_unlock_irqrestore(&counter->lock, flags); |
| 171 | |
| 172 | return ret; |
| 173 | } |
| 174 | #else |
Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 175 | u64 res_counter_read_u64(struct res_counter *counter, int member) |
| 176 | { |
| 177 | return *res_counter_member(counter, member); |
| 178 | } |
KAMEZAWA Hiroyuki | 6c191cd | 2011-03-23 16:42:18 -0700 | [diff] [blame] | 179 | #endif |
Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 180 | |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 181 | int res_counter_memparse_write_strategy(const char *buf, |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 182 | unsigned long long *resp) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 183 | { |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 184 | char *end; |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 185 | unsigned long long res; |
Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 186 | |
Sha Zhengju | 6de5a8b | 2013-09-12 15:13:47 -0700 | [diff] [blame] | 187 | /* return RES_COUNTER_MAX(unlimited) if "-1" is specified */ |
Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 188 | if (*buf == '-') { |
Fabian Frederick | 6c5a53c | 2014-06-04 16:11:24 -0700 | [diff] [blame] | 189 | int rc = kstrtoull(buf + 1, 10, &res); |
| 190 | |
| 191 | if (rc) |
| 192 | return rc; |
| 193 | if (res != 1) |
Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 194 | return -EINVAL; |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 195 | *resp = RES_COUNTER_MAX; |
Daisuke Nishimura | c5b947b | 2009-06-17 16:27:20 -0700 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 199 | res = memparse(buf, &end); |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 200 | if (*end != '\0') |
| 201 | return -EINVAL; |
| 202 | |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 203 | if (PAGE_ALIGN(res) >= res) |
| 204 | res = PAGE_ALIGN(res); |
Sha Zhengju | 3af3351 | 2013-09-12 15:13:48 -0700 | [diff] [blame] | 205 | else |
Sha Zhengju | 1a36e59 | 2013-09-12 15:13:49 -0700 | [diff] [blame] | 206 | res = RES_COUNTER_MAX; |
| 207 | |
| 208 | *resp = res; |
Sha Zhengju | 3af3351 | 2013-09-12 15:13:48 -0700 | [diff] [blame] | 209 | |
Paul Menage | 856c13a | 2008-07-25 01:47:04 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |