4 * Copyright 2007 OpenVZ SWsoft Inc
6 * Author: Pavel Emelianov <xemul@openvz.org>
10 #include <linux/types.h>
11 #include <linux/parser.h>
13 #include <linux/res_counter.h>
14 #include <linux/uaccess.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
) {
32 counter
->usage
+= val
;
33 if (counter
->usage
> counter
->max_usage
)
34 counter
->max_usage
= counter
->usage
;
38 int res_counter_charge(struct res_counter
*counter
, unsigned long val
,
39 struct res_counter
**limit_fail_at
)
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
) {
49 ret
= res_counter_charge_locked(c
, val
);
50 spin_unlock(&c
->lock
);
59 for (u
= counter
; u
!= c
; u
= u
->parent
) {
61 res_counter_uncharge_locked(u
, val
);
62 spin_unlock(&u
->lock
);
65 local_irq_restore(flags
);
69 void res_counter_uncharge_locked(struct res_counter
*counter
, unsigned long val
)
71 if (WARN_ON(counter
->usage
< val
))
74 counter
->usage
-= val
;
77 void res_counter_uncharge(struct res_counter
*counter
, unsigned long val
)
80 struct res_counter
*c
;
82 local_irq_save(flags
);
83 for (c
= counter
; c
!= NULL
; c
= c
->parent
) {
85 res_counter_uncharge_locked(c
, val
);
86 spin_unlock(&c
->lock
);
88 local_irq_restore(flags
);
92 static inline unsigned long long *
93 res_counter_member(struct res_counter
*counter
, int member
)
97 return &counter
->usage
;
99 return &counter
->max_usage
;
101 return &counter
->limit
;
103 return &counter
->failcnt
;
105 return &counter
->soft_limit
;
112 ssize_t
res_counter_read(struct res_counter
*counter
, int member
,
113 const char __user
*userbuf
, size_t nbytes
, loff_t
*pos
,
114 int (*read_strategy
)(unsigned long long val
, char *st_buf
))
116 unsigned long long *val
;
120 val
= res_counter_member(counter
, member
);
122 s
+= read_strategy(*val
, s
);
124 s
+= sprintf(s
, "%llu\n", *val
);
125 return simple_read_from_buffer((void __user
*)userbuf
, nbytes
,
129 u64
res_counter_read_u64(struct res_counter
*counter
, int member
)
131 return *res_counter_member(counter
, member
);
134 int res_counter_memparse_write_strategy(const char *buf
,
135 unsigned long long *res
)
139 /* return RESOURCE_MAX(unlimited) if "-1" is specified */
141 *res
= simple_strtoull(buf
+ 1, &end
, 10);
142 if (*res
!= 1 || *end
!= '\0')
148 /* FIXME - make memparse() take const char* args */
149 *res
= memparse((char *)buf
, &end
);
153 *res
= PAGE_ALIGN(*res
);
157 int res_counter_write(struct res_counter
*counter
, int member
,
158 const char *buf
, write_strategy_fn write_strategy
)
162 unsigned long long tmp
, *val
;
164 if (write_strategy
) {
165 if (write_strategy(buf
, &tmp
))
168 tmp
= simple_strtoull(buf
, &end
, 10);
172 spin_lock_irqsave(&counter
->lock
, flags
);
173 val
= res_counter_member(counter
, member
);
175 spin_unlock_irqrestore(&counter
->lock
, flags
);