]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/percpu_counter.h
lib: make percpu_counter_add take s64
[mirror_ubuntu-zesty-kernel.git] / include / linux / percpu_counter.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_PERCPU_COUNTER_H
2#define _LINUX_PERCPU_COUNTER_H
3/*
4 * A simple "approximate counter" for use in ext2 and ext3 superblocks.
5 *
6 * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4.
7 */
8
1da177e4
LT
9#include <linux/spinlock.h>
10#include <linux/smp.h>
c67ad917 11#include <linux/list.h>
1da177e4
LT
12#include <linux/threads.h>
13#include <linux/percpu.h>
0216bfcf 14#include <linux/types.h>
1da177e4
LT
15
16#ifdef CONFIG_SMP
17
18struct percpu_counter {
19 spinlock_t lock;
0216bfcf 20 s64 count;
c67ad917
AM
21#ifdef CONFIG_HOTPLUG_CPU
22 struct list_head list; /* All percpu_counters are on a list */
23#endif
0216bfcf 24 s32 *counters;
1da177e4
LT
25};
26
27#if NR_CPUS >= 16
28#define FBC_BATCH (NR_CPUS*2)
29#else
30#define FBC_BATCH (NR_CPUS*4)
31#endif
32
c67ad917
AM
33void percpu_counter_init(struct percpu_counter *fbc, s64 amount);
34void percpu_counter_destroy(struct percpu_counter *fbc);
20e89767 35void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
0216bfcf 36s64 percpu_counter_sum(struct percpu_counter *fbc);
1da177e4 37
20e89767 38static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
252e0ba6
PZ
39{
40 __percpu_counter_add(fbc, amount, FBC_BATCH);
41}
42
0216bfcf 43static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
44{
45 return fbc->count;
46}
47
48/*
49 * It is possible for the percpu_counter_read() to return a small negative
50 * number for some counter which should never be negative.
0216bfcf 51 *
1da177e4 52 */
0216bfcf 53static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4 54{
0216bfcf 55 s64 ret = fbc->count;
1da177e4
LT
56
57 barrier(); /* Prevent reloads of fbc->count */
0216bfcf 58 if (ret >= 0)
1da177e4
LT
59 return ret;
60 return 1;
61}
62
63#else
64
65struct percpu_counter {
0216bfcf 66 s64 count;
1da177e4
LT
67};
68
0216bfcf 69static inline void percpu_counter_init(struct percpu_counter *fbc, s64 amount)
1da177e4 70{
0216bfcf 71 fbc->count = amount;
1da177e4
LT
72}
73
74static inline void percpu_counter_destroy(struct percpu_counter *fbc)
75{
76}
77
252e0ba6
PZ
78#define __percpu_counter_add(fbc, amount, batch) \
79 percpu_counter_add(fbc, amount)
80
1da177e4 81static inline void
20e89767 82percpu_counter_add(struct percpu_counter *fbc, s64 amount)
1da177e4
LT
83{
84 preempt_disable();
85 fbc->count += amount;
86 preempt_enable();
87}
88
0216bfcf 89static inline s64 percpu_counter_read(struct percpu_counter *fbc)
1da177e4
LT
90{
91 return fbc->count;
92}
93
0216bfcf 94static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
1da177e4
LT
95{
96 return fbc->count;
97}
98
0216bfcf 99static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
e2bab3d9
AM
100{
101 return percpu_counter_read_positive(fbc);
102}
103
1da177e4
LT
104#endif /* CONFIG_SMP */
105
106static inline void percpu_counter_inc(struct percpu_counter *fbc)
107{
aa0dff2d 108 percpu_counter_add(fbc, 1);
1da177e4
LT
109}
110
111static inline void percpu_counter_dec(struct percpu_counter *fbc)
112{
aa0dff2d 113 percpu_counter_add(fbc, -1);
1da177e4
LT
114}
115
3cb4f9fa
PZ
116static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
117{
118 percpu_counter_add(fbc, -amount);
119}
120
1da177e4 121#endif /* _LINUX_PERCPU_COUNTER_H */