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> |
Robert P. J. Day | 1aeb272 | 2008-04-29 00:59:25 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 14 | #include <linux/res_counter.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | |
| 17 | void res_counter_init(struct res_counter *counter) |
| 18 | { |
| 19 | spin_lock_init(&counter->lock); |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 20 | counter->limit = (unsigned long long)LLONG_MAX; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | int res_counter_charge_locked(struct res_counter *counter, unsigned long val) |
| 24 | { |
| 25 | if (counter->usage + val > counter->limit) { |
| 26 | counter->failcnt++; |
| 27 | return -ENOMEM; |
| 28 | } |
| 29 | |
| 30 | counter->usage += val; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 31 | if (counter->usage > counter->max_usage) |
| 32 | counter->max_usage = counter->usage; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | int res_counter_charge(struct res_counter *counter, unsigned long val) |
| 37 | { |
| 38 | int ret; |
| 39 | unsigned long flags; |
| 40 | |
| 41 | spin_lock_irqsave(&counter->lock, flags); |
| 42 | ret = res_counter_charge_locked(counter, val); |
| 43 | spin_unlock_irqrestore(&counter->lock, flags); |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val) |
| 48 | { |
| 49 | if (WARN_ON(counter->usage < val)) |
| 50 | val = counter->usage; |
| 51 | |
| 52 | counter->usage -= val; |
| 53 | } |
| 54 | |
| 55 | void res_counter_uncharge(struct res_counter *counter, unsigned long val) |
| 56 | { |
| 57 | unsigned long flags; |
| 58 | |
| 59 | spin_lock_irqsave(&counter->lock, flags); |
| 60 | res_counter_uncharge_locked(counter, val); |
| 61 | spin_unlock_irqrestore(&counter->lock, flags); |
| 62 | } |
| 63 | |
| 64 | |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 65 | static inline unsigned long long * |
| 66 | res_counter_member(struct res_counter *counter, int member) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 67 | { |
| 68 | switch (member) { |
| 69 | case RES_USAGE: |
| 70 | return &counter->usage; |
Pavel Emelyanov | c84872e | 2008-04-29 01:00:17 -0700 | [diff] [blame] | 71 | case RES_MAX_USAGE: |
| 72 | return &counter->max_usage; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 73 | case RES_LIMIT: |
| 74 | return &counter->limit; |
| 75 | case RES_FAILCNT: |
| 76 | return &counter->failcnt; |
| 77 | }; |
| 78 | |
| 79 | BUG(); |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | ssize_t res_counter_read(struct res_counter *counter, int member, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 84 | const char __user *userbuf, size_t nbytes, loff_t *pos, |
| 85 | int (*read_strategy)(unsigned long long val, char *st_buf)) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 86 | { |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 87 | unsigned long long *val; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 88 | char buf[64], *s; |
| 89 | |
| 90 | s = buf; |
| 91 | val = res_counter_member(counter, member); |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 92 | if (read_strategy) |
| 93 | s += read_strategy(*val, s); |
| 94 | else |
| 95 | s += sprintf(s, "%llu\n", *val); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 96 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
| 97 | pos, buf, s - buf); |
| 98 | } |
| 99 | |
Paul Menage | 2c7eabf | 2008-04-29 00:59:58 -0700 | [diff] [blame] | 100 | u64 res_counter_read_u64(struct res_counter *counter, int member) |
| 101 | { |
| 102 | return *res_counter_member(counter, member); |
| 103 | } |
| 104 | |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 105 | ssize_t res_counter_write(struct res_counter *counter, int member, |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 106 | const char __user *userbuf, size_t nbytes, loff_t *pos, |
| 107 | int (*write_strategy)(char *st_buf, unsigned long long *val)) |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 108 | { |
| 109 | int ret; |
| 110 | char *buf, *end; |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 111 | unsigned long flags; |
| 112 | unsigned long long tmp, *val; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 113 | |
| 114 | buf = kmalloc(nbytes + 1, GFP_KERNEL); |
| 115 | ret = -ENOMEM; |
| 116 | if (buf == NULL) |
| 117 | goto out; |
| 118 | |
| 119 | buf[nbytes] = '\0'; |
| 120 | ret = -EFAULT; |
| 121 | if (copy_from_user(buf, userbuf, nbytes)) |
| 122 | goto out_free; |
| 123 | |
| 124 | ret = -EINVAL; |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 125 | |
Balbir Singh | fb78922 | 2008-03-04 14:28:24 -0800 | [diff] [blame] | 126 | strstrip(buf); |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 127 | if (write_strategy) { |
| 128 | if (write_strategy(buf, &tmp)) { |
| 129 | goto out_free; |
| 130 | } |
| 131 | } else { |
| 132 | tmp = simple_strtoull(buf, &end, 10); |
| 133 | if (*end != '\0') |
| 134 | goto out_free; |
| 135 | } |
| 136 | spin_lock_irqsave(&counter->lock, flags); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 137 | val = res_counter_member(counter, member); |
| 138 | *val = tmp; |
Balbir Singh | 0eea103 | 2008-02-07 00:13:57 -0800 | [diff] [blame] | 139 | spin_unlock_irqrestore(&counter->lock, flags); |
Pavel Emelianov | e552b66 | 2008-02-07 00:13:49 -0800 | [diff] [blame] | 140 | ret = nbytes; |
| 141 | out_free: |
| 142 | kfree(buf); |
| 143 | out: |
| 144 | return ret; |
| 145 | } |