Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / include / linux / percpu_counter.h
blob4f052496cdfd79b4db4ac104384b212c5790f1ad
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PERCPU_COUNTER_H
3 #define _LINUX_PERCPU_COUNTER_H
4 /*
5 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
7 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
8 */
10 #include <linux/spinlock.h>
11 #include <linux/smp.h>
12 #include <linux/list.h>
13 #include <linux/threads.h>
14 #include <linux/percpu.h>
15 #include <linux/types.h>
16 #include <linux/gfp.h>
18 #ifdef CONFIG_SMP
20 struct percpu_counter {
21 raw_spinlock_t lock;
22 s64 count;
23 #ifdef CONFIG_HOTPLUG_CPU
24 struct list_head list; /* All percpu_counters are on a list */
25 #endif
26 s32 __percpu *counters;
29 extern int percpu_counter_batch;
31 int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
32 struct lock_class_key *key);
34 #define percpu_counter_init(fbc, value, gfp) \
35 ({ \
36 static struct lock_class_key __key; \
38 __percpu_counter_init(fbc, value, gfp, &__key); \
41 void percpu_counter_destroy(struct percpu_counter *fbc);
42 void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
43 void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,
44 s32 batch);
45 s64 __percpu_counter_sum(struct percpu_counter *fbc);
46 int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);
48 static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
50 return __percpu_counter_compare(fbc, rhs, percpu_counter_batch);
53 static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
55 percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
58 static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
60 s64 ret = __percpu_counter_sum(fbc);
61 return ret < 0 ? 0 : ret;
64 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
66 return __percpu_counter_sum(fbc);
69 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
71 return fbc->count;
75 * It is possible for the percpu_counter_read() to return a small negative
76 * number for some counter which should never be negative.
79 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
81 s64 ret = fbc->count;
83 barrier(); /* Prevent reloads of fbc->count */
84 if (ret >= 0)
85 return ret;
86 return 0;
89 static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
91 return (fbc->counters != NULL);
94 #else /* !CONFIG_SMP */
96 struct percpu_counter {
97 s64 count;
100 static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount,
101 gfp_t gfp)
103 fbc->count = amount;
104 return 0;
107 static inline void percpu_counter_destroy(struct percpu_counter *fbc)
111 static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
113 fbc->count = amount;
116 static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
118 if (fbc->count > rhs)
119 return 1;
120 else if (fbc->count < rhs)
121 return -1;
122 else
123 return 0;
126 static inline int
127 __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
129 return percpu_counter_compare(fbc, rhs);
132 static inline void
133 percpu_counter_add(struct percpu_counter *fbc, s64 amount)
135 preempt_disable();
136 fbc->count += amount;
137 preempt_enable();
140 static inline void
141 percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
143 percpu_counter_add(fbc, amount);
146 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
148 return fbc->count;
152 * percpu_counter is intended to track positive numbers. In the UP case the
153 * number should never be negative.
155 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
157 return fbc->count;
160 static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
162 return percpu_counter_read_positive(fbc);
165 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
167 return percpu_counter_read(fbc);
170 static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
172 return true;
175 #endif /* CONFIG_SMP */
177 static inline void percpu_counter_inc(struct percpu_counter *fbc)
179 percpu_counter_add(fbc, 1);
182 static inline void percpu_counter_dec(struct percpu_counter *fbc)
184 percpu_counter_add(fbc, -1);
187 static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
189 percpu_counter_add(fbc, -amount);
192 #endif /* _LINUX_PERCPU_COUNTER_H */