ALSA: hda/realtek - Fix ADC assignment with a shared HP/Mic pin
[zen-stable.git] / kernel / res_counter.c
blobd508363858b3a147cf757ae06080dde035842091
1 /*
2 * resource cgroups
4 * Copyright 2007 OpenVZ SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
8 */
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>
15 #include <linux/mm.h>
17 void res_counter_init(struct res_counter *counter, struct res_counter *parent)
19 spin_lock_init(&counter->lock);
20 counter->limit = RESOURCE_MAX;
21 counter->soft_limit = RESOURCE_MAX;
22 counter->parent = parent;
25 int res_counter_charge_locked(struct res_counter *counter, unsigned long val)
27 if (counter->usage + val > counter->limit) {
28 counter->failcnt++;
29 return -ENOMEM;
32 counter->usage += val;
33 if (counter->usage > counter->max_usage)
34 counter->max_usage = counter->usage;
35 return 0;
38 int res_counter_charge(struct res_counter *counter, unsigned long val,
39 struct res_counter **limit_fail_at)
41 int ret;
42 unsigned long flags;
43 struct res_counter *c, *u;
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;
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);
64 done:
65 local_irq_restore(flags);
66 return ret;
69 int res_counter_charge_nofail(struct res_counter *counter, unsigned long val,
70 struct res_counter **limit_fail_at)
72 int ret, r;
73 unsigned long flags;
74 struct res_counter *c;
76 r = ret = 0;
77 *limit_fail_at = NULL;
78 local_irq_save(flags);
79 for (c = counter; c != NULL; c = c->parent) {
80 spin_lock(&c->lock);
81 r = res_counter_charge_locked(c, val);
82 if (r)
83 c->usage += val;
84 spin_unlock(&c->lock);
85 if (r < 0 && ret == 0) {
86 *limit_fail_at = c;
87 ret = r;
90 local_irq_restore(flags);
92 return ret;
94 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val)
96 if (WARN_ON(counter->usage < val))
97 val = counter->usage;
99 counter->usage -= val;
102 void res_counter_uncharge(struct res_counter *counter, unsigned long val)
104 unsigned long flags;
105 struct res_counter *c;
107 local_irq_save(flags);
108 for (c = counter; c != NULL; c = c->parent) {
109 spin_lock(&c->lock);
110 res_counter_uncharge_locked(c, val);
111 spin_unlock(&c->lock);
113 local_irq_restore(flags);
117 static inline unsigned long long *
118 res_counter_member(struct res_counter *counter, int member)
120 switch (member) {
121 case RES_USAGE:
122 return &counter->usage;
123 case RES_MAX_USAGE:
124 return &counter->max_usage;
125 case RES_LIMIT:
126 return &counter->limit;
127 case RES_FAILCNT:
128 return &counter->failcnt;
129 case RES_SOFT_LIMIT:
130 return &counter->soft_limit;
133 BUG();
134 return NULL;
137 ssize_t res_counter_read(struct res_counter *counter, int member,
138 const char __user *userbuf, size_t nbytes, loff_t *pos,
139 int (*read_strategy)(unsigned long long val, char *st_buf))
141 unsigned long long *val;
142 char buf[64], *s;
144 s = buf;
145 val = res_counter_member(counter, member);
146 if (read_strategy)
147 s += read_strategy(*val, s);
148 else
149 s += sprintf(s, "%llu\n", *val);
150 return simple_read_from_buffer((void __user *)userbuf, nbytes,
151 pos, buf, s - buf);
154 #if BITS_PER_LONG == 32
155 u64 res_counter_read_u64(struct res_counter *counter, int member)
157 unsigned long flags;
158 u64 ret;
160 spin_lock_irqsave(&counter->lock, flags);
161 ret = *res_counter_member(counter, member);
162 spin_unlock_irqrestore(&counter->lock, flags);
164 return ret;
166 #else
167 u64 res_counter_read_u64(struct res_counter *counter, int member)
169 return *res_counter_member(counter, member);
171 #endif
173 int res_counter_memparse_write_strategy(const char *buf,
174 unsigned long long *res)
176 char *end;
178 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
179 if (*buf == '-') {
180 *res = simple_strtoull(buf + 1, &end, 10);
181 if (*res != 1 || *end != '\0')
182 return -EINVAL;
183 *res = RESOURCE_MAX;
184 return 0;
187 *res = memparse(buf, &end);
188 if (*end != '\0')
189 return -EINVAL;
191 *res = PAGE_ALIGN(*res);
192 return 0;
195 int res_counter_write(struct res_counter *counter, int member,
196 const char *buf, write_strategy_fn write_strategy)
198 char *end;
199 unsigned long flags;
200 unsigned long long tmp, *val;
202 if (write_strategy) {
203 if (write_strategy(buf, &tmp))
204 return -EINVAL;
205 } else {
206 tmp = simple_strtoull(buf, &end, 10);
207 if (*end != '\0')
208 return -EINVAL;
210 spin_lock_irqsave(&counter->lock, flags);
211 val = res_counter_member(counter, member);
212 *val = tmp;
213 spin_unlock_irqrestore(&counter->lock, flags);
214 return 0;