]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - lib/percpu_counter.c
percpu_counter: FBC_BATCH should be a variable
[mirror_ubuntu-zesty-kernel.git] / lib / percpu_counter.c
1 /*
2 * Fast batching percpu counters.
3 */
4
5 #include <linux/percpu_counter.h>
6 #include <linux/notifier.h>
7 #include <linux/mutex.h>
8 #include <linux/init.h>
9 #include <linux/cpu.h>
10 #include <linux/module.h>
11
12 static LIST_HEAD(percpu_counters);
13 static DEFINE_MUTEX(percpu_counters_lock);
14
15 void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
16 {
17 int cpu;
18
19 spin_lock(&fbc->lock);
20 for_each_possible_cpu(cpu) {
21 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
22 *pcount = 0;
23 }
24 fbc->count = amount;
25 spin_unlock(&fbc->lock);
26 }
27 EXPORT_SYMBOL(percpu_counter_set);
28
29 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
30 {
31 s64 count;
32 s32 *pcount;
33 int cpu = get_cpu();
34
35 pcount = per_cpu_ptr(fbc->counters, cpu);
36 count = *pcount + amount;
37 if (count >= batch || count <= -batch) {
38 spin_lock(&fbc->lock);
39 fbc->count += count;
40 *pcount = 0;
41 spin_unlock(&fbc->lock);
42 } else {
43 *pcount = count;
44 }
45 put_cpu();
46 }
47 EXPORT_SYMBOL(__percpu_counter_add);
48
49 /*
50 * Add up all the per-cpu counts, return the result. This is a more accurate
51 * but much slower version of percpu_counter_read_positive()
52 */
53 s64 __percpu_counter_sum(struct percpu_counter *fbc)
54 {
55 s64 ret;
56 int cpu;
57
58 spin_lock(&fbc->lock);
59 ret = fbc->count;
60 for_each_online_cpu(cpu) {
61 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
62 ret += *pcount;
63 }
64 spin_unlock(&fbc->lock);
65 return ret;
66 }
67 EXPORT_SYMBOL(__percpu_counter_sum);
68
69 static struct lock_class_key percpu_counter_irqsafe;
70
71 int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
72 {
73 spin_lock_init(&fbc->lock);
74 fbc->count = amount;
75 fbc->counters = alloc_percpu(s32);
76 if (!fbc->counters)
77 return -ENOMEM;
78 #ifdef CONFIG_HOTPLUG_CPU
79 mutex_lock(&percpu_counters_lock);
80 list_add(&fbc->list, &percpu_counters);
81 mutex_unlock(&percpu_counters_lock);
82 #endif
83 return 0;
84 }
85 EXPORT_SYMBOL(percpu_counter_init);
86
87 int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount)
88 {
89 int err;
90
91 err = percpu_counter_init(fbc, amount);
92 if (!err)
93 lockdep_set_class(&fbc->lock, &percpu_counter_irqsafe);
94 return err;
95 }
96
97 void percpu_counter_destroy(struct percpu_counter *fbc)
98 {
99 if (!fbc->counters)
100 return;
101
102 #ifdef CONFIG_HOTPLUG_CPU
103 mutex_lock(&percpu_counters_lock);
104 list_del(&fbc->list);
105 mutex_unlock(&percpu_counters_lock);
106 #endif
107 free_percpu(fbc->counters);
108 fbc->counters = NULL;
109 }
110 EXPORT_SYMBOL(percpu_counter_destroy);
111
112 int percpu_counter_batch __read_mostly = 32;
113 EXPORT_SYMBOL(percpu_counter_batch);
114
115 static void compute_batch_value(void)
116 {
117 int nr = num_online_cpus();
118
119 percpu_counter_batch = max(32, nr*2);
120 }
121
122 static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
123 unsigned long action, void *hcpu)
124 {
125 #ifdef CONFIG_HOTPLUG_CPU
126 unsigned int cpu;
127 struct percpu_counter *fbc;
128
129 compute_batch_value();
130 if (action != CPU_DEAD)
131 return NOTIFY_OK;
132
133 cpu = (unsigned long)hcpu;
134 mutex_lock(&percpu_counters_lock);
135 list_for_each_entry(fbc, &percpu_counters, list) {
136 s32 *pcount;
137 unsigned long flags;
138
139 spin_lock_irqsave(&fbc->lock, flags);
140 pcount = per_cpu_ptr(fbc->counters, cpu);
141 fbc->count += *pcount;
142 *pcount = 0;
143 spin_unlock_irqrestore(&fbc->lock, flags);
144 }
145 mutex_unlock(&percpu_counters_lock);
146 #endif
147 return NOTIFY_OK;
148 }
149
150 static int __init percpu_counter_startup(void)
151 {
152 compute_batch_value();
153 hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
154 return 0;
155 }
156 module_init(percpu_counter_startup);