1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PERCPU_COUNTER_H
3 #define _LINUX_PERCPU_COUNTER_H
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.
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>
17 /* percpu_counter batch for local add or sub */
18 #define PERCPU_COUNTER_LOCAL_BATCH INT_MAX
22 struct percpu_counter {
25 #ifdef CONFIG_HOTPLUG_CPU
26 struct list_head list; /* All percpu_counters are on a list */
28 s32 __percpu *counters;
31 extern int percpu_counter_batch;
33 int __percpu_counter_init_many(struct percpu_counter *fbc, s64 amount,
34 gfp_t gfp, u32 nr_counters,
35 struct lock_class_key *key);
37 #define percpu_counter_init_many(fbc, value, gfp, nr_counters) \
39 static struct lock_class_key __key; \
41 __percpu_counter_init_many(fbc, value, gfp, nr_counters,\
46 #define percpu_counter_init(fbc, value, gfp) \
47 percpu_counter_init_many(fbc, value, gfp, 1)
49 void percpu_counter_destroy_many(struct percpu_counter *fbc, u32 nr_counters);
50 static inline void percpu_counter_destroy(struct percpu_counter *fbc)
52 percpu_counter_destroy_many(fbc, 1);
55 void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
56 void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,
58 s64 __percpu_counter_sum(struct percpu_counter *fbc);
59 int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);
60 void percpu_counter_sync(struct percpu_counter *fbc);
62 static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
64 return __percpu_counter_compare(fbc, rhs, percpu_counter_batch);
67 static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
69 percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
73 * With percpu_counter_add_local() and percpu_counter_sub_local(), counts
74 * are accumulated in local per cpu counter and not in fbc->count until
75 * local count overflows PERCPU_COUNTER_LOCAL_BATCH. This makes counter
77 * But percpu_counter_sum(), instead of percpu_counter_read(), needs to be
78 * used to add up the counts from each CPU to account for all the local
79 * counts. So percpu_counter_add_local() and percpu_counter_sub_local()
80 * should be used when a counter is updated frequently and read rarely.
83 percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
85 percpu_counter_add_batch(fbc, amount, PERCPU_COUNTER_LOCAL_BATCH);
88 static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
90 s64 ret = __percpu_counter_sum(fbc);
91 return ret < 0 ? 0 : ret;
94 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
96 return __percpu_counter_sum(fbc);
99 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
105 * It is possible for the percpu_counter_read() to return a small negative
106 * number for some counter which should never be negative.
109 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
111 /* Prevent reloads of fbc->count */
112 s64 ret = READ_ONCE(fbc->count);
119 static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
121 return (fbc->counters != NULL);
124 #else /* !CONFIG_SMP */
126 struct percpu_counter {
130 static inline int percpu_counter_init_many(struct percpu_counter *fbc,
131 s64 amount, gfp_t gfp,
136 for (i = 0; i < nr_counters; i++)
137 fbc[i].count = amount;
142 static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount,
145 return percpu_counter_init_many(fbc, amount, gfp, 1);
148 static inline void percpu_counter_destroy_many(struct percpu_counter *fbc,
153 static inline void percpu_counter_destroy(struct percpu_counter *fbc)
157 static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
162 static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
164 if (fbc->count > rhs)
166 else if (fbc->count < rhs)
173 __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
175 return percpu_counter_compare(fbc, rhs);
179 percpu_counter_add(struct percpu_counter *fbc, s64 amount)
183 local_irq_save(flags);
184 fbc->count += amount;
185 local_irq_restore(flags);
188 /* non-SMP percpu_counter_add_local is the same with percpu_counter_add */
190 percpu_counter_add_local(struct percpu_counter *fbc, s64 amount)
192 percpu_counter_add(fbc, amount);
196 percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
198 percpu_counter_add(fbc, amount);
201 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
207 * percpu_counter is intended to track positive numbers. In the UP case the
208 * number should never be negative.
210 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
215 static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
217 return percpu_counter_read_positive(fbc);
220 static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
222 return percpu_counter_read(fbc);
225 static inline bool percpu_counter_initialized(struct percpu_counter *fbc)
230 static inline void percpu_counter_sync(struct percpu_counter *fbc)
233 #endif /* CONFIG_SMP */
235 static inline void percpu_counter_inc(struct percpu_counter *fbc)
237 percpu_counter_add(fbc, 1);
240 static inline void percpu_counter_dec(struct percpu_counter *fbc)
242 percpu_counter_add(fbc, -1);
245 static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
247 percpu_counter_add(fbc, -amount);
251 percpu_counter_sub_local(struct percpu_counter *fbc, s64 amount)
253 percpu_counter_add_local(fbc, -amount);
256 #endif /* _LINUX_PERCPU_COUNTER_H */