Mark res_counter_charge(_locked) with __must_check
[linux-2.6/mini2440.git] / include / linux / res_counter.h
blob125660e7793f082c570583ed68b1d677bc9fe0c8
1 #ifndef __RES_COUNTER_H__
2 #define __RES_COUNTER_H__
4 /*
5 * Resource Counters
6 * Contain common data types and routines for resource accounting
8 * Copyright 2007 OpenVZ SWsoft Inc
10 * Author: Pavel Emelianov <xemul@openvz.org>
12 * See Documentation/controllers/resource_counter.txt for more
13 * info about what this counter is.
16 #include <linux/cgroup.h>
19 * The core object. the cgroup that wishes to account for some
20 * resource may include this counter into its structures and use
21 * the helpers described beyond
24 struct res_counter {
26 * the current resource consumption level
28 unsigned long long usage;
30 * the maximal value of the usage from the counter creation
32 unsigned long long max_usage;
34 * the limit that usage cannot exceed
36 unsigned long long limit;
38 * the number of unsuccessful attempts to consume the resource
40 unsigned long long failcnt;
42 * the lock to protect all of the above.
43 * the routines below consider this to be IRQ-safe
45 spinlock_t lock;
48 /**
49 * Helpers to interact with userspace
50 * res_counter_read_u64() - returns the value of the specified member.
51 * res_counter_read/_write - put/get the specified fields from the
52 * res_counter struct to/from the user
54 * @counter: the counter in question
55 * @member: the field to work with (see RES_xxx below)
56 * @buf: the buffer to opeate on,...
57 * @nbytes: its size...
58 * @pos: and the offset.
61 u64 res_counter_read_u64(struct res_counter *counter, int member);
63 ssize_t res_counter_read(struct res_counter *counter, int member,
64 const char __user *buf, size_t nbytes, loff_t *pos,
65 int (*read_strategy)(unsigned long long val, char *s));
66 ssize_t res_counter_write(struct res_counter *counter, int member,
67 const char __user *buf, size_t nbytes, loff_t *pos,
68 int (*write_strategy)(char *buf, unsigned long long *val));
71 * the field descriptors. one for each member of res_counter
74 enum {
75 RES_USAGE,
76 RES_MAX_USAGE,
77 RES_LIMIT,
78 RES_FAILCNT,
82 * helpers for accounting
85 void res_counter_init(struct res_counter *counter);
88 * charge - try to consume more resource.
90 * @counter: the counter
91 * @val: the amount of the resource. each controller defines its own
92 * units, e.g. numbers, bytes, Kbytes, etc
94 * returns 0 on success and <0 if the counter->usage will exceed the
95 * counter->limit _locked call expects the counter->lock to be taken
98 int __must_check res_counter_charge_locked(struct res_counter *counter,
99 unsigned long val);
100 int __must_check res_counter_charge(struct res_counter *counter,
101 unsigned long val);
104 * uncharge - tell that some portion of the resource is released
106 * @counter: the counter
107 * @val: the amount of the resource
109 * these calls check for usage underflow and show a warning on the console
110 * _locked call expects the counter->lock to be taken
113 void res_counter_uncharge_locked(struct res_counter *counter, unsigned long val);
114 void res_counter_uncharge(struct res_counter *counter, unsigned long val);
116 static inline bool res_counter_limit_check_locked(struct res_counter *cnt)
118 if (cnt->usage < cnt->limit)
119 return true;
121 return false;
125 * Helper function to detect if the cgroup is within it's limit or
126 * not. It's currently called from cgroup_rss_prepare()
128 static inline bool res_counter_check_under_limit(struct res_counter *cnt)
130 bool ret;
131 unsigned long flags;
133 spin_lock_irqsave(&cnt->lock, flags);
134 ret = res_counter_limit_check_locked(cnt);
135 spin_unlock_irqrestore(&cnt->lock, flags);
136 return ret;
139 static inline void res_counter_reset_max(struct res_counter *cnt)
141 unsigned long flags;
143 spin_lock_irqsave(&cnt->lock, flags);
144 cnt->max_usage = cnt->usage;
145 spin_unlock_irqrestore(&cnt->lock, flags);
148 static inline void res_counter_reset_failcnt(struct res_counter *cnt)
150 unsigned long flags;
152 spin_lock_irqsave(&cnt->lock, flags);
153 cnt->failcnt = 0;
154 spin_unlock_irqrestore(&cnt->lock, flags);
156 #endif