]>
Commit | Line | Data |
---|---|---|
e552b661 PE |
1 | /* |
2 | * resource cgroups | |
3 | * | |
4 | * Copyright 2007 OpenVZ SWsoft Inc | |
5 | * | |
6 | * Author: Pavel Emelianov <[email protected]> | |
7 | * | |
8 | */ | |
9 | ||
10 | #include <linux/types.h> | |
11 | #include <linux/parser.h> | |
12 | #include <linux/fs.h> | |
1aeb272c | 13 | #include <linux/slab.h> |
e552b661 PE |
14 | #include <linux/res_counter.h> |
15 | #include <linux/uaccess.h> | |
856c13aa | 16 | #include <linux/mm.h> |
e552b661 | 17 | |
28dbc4b6 | 18 | void res_counter_init(struct res_counter *counter, struct res_counter *parent) |
e552b661 PE |
19 | { |
20 | spin_lock_init(&counter->lock); | |
0eea1030 | 21 | counter->limit = (unsigned long long)LLONG_MAX; |
28dbc4b6 | 22 | counter->parent = parent; |
e552b661 PE |
23 | } |
24 | ||
25 | int res_counter_charge_locked(struct res_counter *counter, unsigned long val) | |
26 | { | |
27 | if (counter->usage + val > counter->limit) { | |
28 | counter->failcnt++; | |
29 | return -ENOMEM; | |
30 | } | |
31 | ||
32 | counter->usage += val; | |
c84872e1 PE |
33 | if (counter->usage > counter->max_usage) |
34 | counter->max_usage = counter->usage; | |
e552b661 PE |
35 | return 0; |
36 | } | |
37 | ||
28dbc4b6 BS |
38 | int res_counter_charge(struct res_counter *counter, unsigned long val, |
39 | struct res_counter **limit_fail_at) | |
e552b661 PE |
40 | { |
41 | int ret; | |
42 | unsigned long flags; | |
28dbc4b6 | 43 | struct res_counter *c, *u; |
e552b661 | 44 | |
28dbc4b6 BS |
45 | *limit_fail_at = NULL; |
46 | local_irq_save(flags); | |
47 | for (c = counter; c != NULL; c = c->parent) { | |
48 | spin_lock(&c->lock); | |
49 | ret = res_counter_charge_locked(c, val); | |
50 | spin_unlock(&c->lock); | |
51 | if (ret < 0) { | |
52 | *limit_fail_at = c; | |
53 | goto undo; | |
54 | } | |
55 | } | |
56 | ret = 0; | |
57 | goto done; | |
58 | undo: | |
59 | for (u = counter; u != c; u = u->parent) { | |
60 | spin_lock(&u->lock); | |
61 | res_counter_uncharge_locked(u, val); | |
62 | spin_unlock(&u->lock); | |
63 | } | |
64 | done: | |
65 | local_irq_restore(flags); | |
e552b661 PE |
66 | return ret; |
67 | } | |
68 | ||
69 | void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val) | |
70 | { | |
71 | if (WARN_ON(counter->usage < val)) | |
72 | val = counter->usage; | |
73 | ||
74 | counter->usage -= val; | |
75 | } | |
76 | ||
77 | void res_counter_uncharge(struct res_counter *counter, unsigned long val) | |
78 | { | |
79 | unsigned long flags; | |
28dbc4b6 | 80 | struct res_counter *c; |
e552b661 | 81 | |
28dbc4b6 BS |
82 | local_irq_save(flags); |
83 | for (c = counter; c != NULL; c = c->parent) { | |
84 | spin_lock(&c->lock); | |
85 | res_counter_uncharge_locked(c, val); | |
86 | spin_unlock(&c->lock); | |
87 | } | |
88 | local_irq_restore(flags); | |
e552b661 PE |
89 | } |
90 | ||
91 | ||
0eea1030 BS |
92 | static inline unsigned long long * |
93 | res_counter_member(struct res_counter *counter, int member) | |
e552b661 PE |
94 | { |
95 | switch (member) { | |
96 | case RES_USAGE: | |
97 | return &counter->usage; | |
c84872e1 PE |
98 | case RES_MAX_USAGE: |
99 | return &counter->max_usage; | |
e552b661 PE |
100 | case RES_LIMIT: |
101 | return &counter->limit; | |
102 | case RES_FAILCNT: | |
103 | return &counter->failcnt; | |
104 | }; | |
105 | ||
106 | BUG(); | |
107 | return NULL; | |
108 | } | |
109 | ||
110 | ssize_t res_counter_read(struct res_counter *counter, int member, | |
0eea1030 BS |
111 | const char __user *userbuf, size_t nbytes, loff_t *pos, |
112 | int (*read_strategy)(unsigned long long val, char *st_buf)) | |
e552b661 | 113 | { |
0eea1030 | 114 | unsigned long long *val; |
e552b661 PE |
115 | char buf[64], *s; |
116 | ||
117 | s = buf; | |
118 | val = res_counter_member(counter, member); | |
0eea1030 BS |
119 | if (read_strategy) |
120 | s += read_strategy(*val, s); | |
121 | else | |
122 | s += sprintf(s, "%llu\n", *val); | |
e552b661 PE |
123 | return simple_read_from_buffer((void __user *)userbuf, nbytes, |
124 | pos, buf, s - buf); | |
125 | } | |
126 | ||
2c7eabf3 PM |
127 | u64 res_counter_read_u64(struct res_counter *counter, int member) |
128 | { | |
129 | return *res_counter_member(counter, member); | |
130 | } | |
131 | ||
856c13aa PM |
132 | int res_counter_memparse_write_strategy(const char *buf, |
133 | unsigned long long *res) | |
e552b661 | 134 | { |
856c13aa PM |
135 | char *end; |
136 | /* FIXME - make memparse() take const char* args */ | |
137 | *res = memparse((char *)buf, &end); | |
138 | if (*end != '\0') | |
139 | return -EINVAL; | |
e552b661 | 140 | |
856c13aa PM |
141 | *res = PAGE_ALIGN(*res); |
142 | return 0; | |
143 | } | |
e552b661 | 144 | |
856c13aa PM |
145 | int res_counter_write(struct res_counter *counter, int member, |
146 | const char *buf, write_strategy_fn write_strategy) | |
147 | { | |
148 | char *end; | |
149 | unsigned long flags; | |
150 | unsigned long long tmp, *val; | |
e552b661 | 151 | |
0eea1030 | 152 | if (write_strategy) { |
856c13aa PM |
153 | if (write_strategy(buf, &tmp)) |
154 | return -EINVAL; | |
0eea1030 BS |
155 | } else { |
156 | tmp = simple_strtoull(buf, &end, 10); | |
157 | if (*end != '\0') | |
856c13aa | 158 | return -EINVAL; |
0eea1030 BS |
159 | } |
160 | spin_lock_irqsave(&counter->lock, flags); | |
e552b661 PE |
161 | val = res_counter_member(counter, member); |
162 | *val = tmp; | |
0eea1030 | 163 | spin_unlock_irqrestore(&counter->lock, flags); |
856c13aa | 164 | return 0; |
e552b661 | 165 | } |