blob: a60bd8046095206366da9959f1ba1f3190abe0f3 [file] [log] [blame]
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -07001/*
2 * Fast batching percpu counters.
3 */
4
5#include <linux/percpu_counter.h>
Andrew Mortonc67ad912007-07-15 23:39:51 -07006#include <linux/notifier.h>
7#include <linux/mutex.h>
8#include <linux/init.h>
9#include <linux/cpu.h>
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070010#include <linux/module.h>
11
Andrew Mortonc67ad912007-07-15 23:39:51 -070012static LIST_HEAD(percpu_counters);
13static DEFINE_MUTEX(percpu_counters_lock);
Andrew Mortonc67ad912007-07-15 23:39:51 -070014
Peter Zijlstra3a587f42007-10-16 23:25:44 -070015void 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}
27EXPORT_SYMBOL(percpu_counter_set);
28
Peter Zijlstra20e89762007-10-16 23:25:43 -070029void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070030{
Peter Zijlstra20e89762007-10-16 23:25:43 -070031 s64 count;
Mingming Cao0216bfc2006-06-23 02:05:41 -070032 s32 *pcount;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070033 int cpu = get_cpu();
34
35 pcount = per_cpu_ptr(fbc->counters, cpu);
36 count = *pcount + amount;
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070037 if (count >= batch || count <= -batch) {
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070038 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}
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070047EXPORT_SYMBOL(__percpu_counter_add);
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070048
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 */
Andrew Morton02d21162008-12-09 13:14:14 -080053s64 __percpu_counter_sum(struct percpu_counter *fbc)
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070054{
Mingming Cao0216bfc2006-06-23 02:05:41 -070055 s64 ret;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070056 int cpu;
57
58 spin_lock(&fbc->lock);
59 ret = fbc->count;
Andrew Mortonb4ef0292007-07-15 23:39:51 -070060 for_each_online_cpu(cpu) {
Mingming Cao0216bfc2006-06-23 02:05:41 -070061 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070062 ret += *pcount;
63 }
64 spin_unlock(&fbc->lock);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070065 return ret;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070066}
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070067EXPORT_SYMBOL(__percpu_counter_sum);
Andrew Mortonc67ad912007-07-15 23:39:51 -070068
Peter Zijlstradc62a302007-10-16 23:25:46 -070069static struct lock_class_key percpu_counter_irqsafe;
70
Peter Zijlstra833f4072007-10-16 23:25:45 -070071int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
Andrew Mortonc67ad912007-07-15 23:39:51 -070072{
73 spin_lock_init(&fbc->lock);
74 fbc->count = amount;
75 fbc->counters = alloc_percpu(s32);
Peter Zijlstra833f4072007-10-16 23:25:45 -070076 if (!fbc->counters)
77 return -ENOMEM;
Andrew Mortonc67ad912007-07-15 23:39:51 -070078#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
Peter Zijlstra833f4072007-10-16 23:25:45 -070083 return 0;
Andrew Mortonc67ad912007-07-15 23:39:51 -070084}
85EXPORT_SYMBOL(percpu_counter_init);
86
Peter Zijlstradc62a302007-10-16 23:25:46 -070087int 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
Andrew Mortonc67ad912007-07-15 23:39:51 -070097void percpu_counter_destroy(struct percpu_counter *fbc)
98{
Peter Zijlstra833f4072007-10-16 23:25:45 -070099 if (!fbc->counters)
100 return;
101
Andrew Mortonc67ad912007-07-15 23:39:51 -0700102#ifdef CONFIG_HOTPLUG_CPU
103 mutex_lock(&percpu_counters_lock);
104 list_del(&fbc->list);
105 mutex_unlock(&percpu_counters_lock);
106#endif
Eric Dumazetfd3d6642008-12-09 13:14:11 -0800107 free_percpu(fbc->counters);
108 fbc->counters = NULL;
Andrew Mortonc67ad912007-07-15 23:39:51 -0700109}
110EXPORT_SYMBOL(percpu_counter_destroy);
111
Eric Dumazet179f7eb2009-01-06 14:41:04 -0800112int percpu_counter_batch __read_mostly = 32;
113EXPORT_SYMBOL(percpu_counter_batch);
114
115static void compute_batch_value(void)
116{
117 int nr = num_online_cpus();
118
119 percpu_counter_batch = max(32, nr*2);
120}
121
Andrew Mortonc67ad912007-07-15 23:39:51 -0700122static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
123 unsigned long action, void *hcpu)
124{
Eric Dumazet179f7eb2009-01-06 14:41:04 -0800125#ifdef CONFIG_HOTPLUG_CPU
Andrew Mortonc67ad912007-07-15 23:39:51 -0700126 unsigned int cpu;
127 struct percpu_counter *fbc;
128
Eric Dumazet179f7eb2009-01-06 14:41:04 -0800129 compute_batch_value();
Andrew Mortonc67ad912007-07-15 23:39:51 -0700130 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;
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700137 unsigned long flags;
Andrew Mortonc67ad912007-07-15 23:39:51 -0700138
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700139 spin_lock_irqsave(&fbc->lock, flags);
Andrew Mortonc67ad912007-07-15 23:39:51 -0700140 pcount = per_cpu_ptr(fbc->counters, cpu);
141 fbc->count += *pcount;
142 *pcount = 0;
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700143 spin_unlock_irqrestore(&fbc->lock, flags);
Andrew Mortonc67ad912007-07-15 23:39:51 -0700144 }
145 mutex_unlock(&percpu_counters_lock);
Eric Dumazet179f7eb2009-01-06 14:41:04 -0800146#endif
Andrew Mortonc67ad912007-07-15 23:39:51 -0700147 return NOTIFY_OK;
148}
149
150static int __init percpu_counter_startup(void)
151{
Eric Dumazet179f7eb2009-01-06 14:41:04 -0800152 compute_batch_value();
Andrew Mortonc67ad912007-07-15 23:39:51 -0700153 hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
154 return 0;
155}
156module_init(percpu_counter_startup);