blob: 4bb0ed350e73a60aef1524335de4daf36455a36b [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 -070012#ifdef CONFIG_HOTPLUG_CPU
13static LIST_HEAD(percpu_counters);
14static DEFINE_MUTEX(percpu_counters_lock);
15#endif
16
Peter Zijlstra3a587f42007-10-16 23:25:44 -070017void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
18{
19 int cpu;
20
21 spin_lock(&fbc->lock);
22 for_each_possible_cpu(cpu) {
23 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
24 *pcount = 0;
25 }
26 fbc->count = amount;
27 spin_unlock(&fbc->lock);
28}
29EXPORT_SYMBOL(percpu_counter_set);
30
Peter Zijlstra20e89762007-10-16 23:25:43 -070031void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070032{
Peter Zijlstra20e89762007-10-16 23:25:43 -070033 s64 count;
Mingming Cao0216bfc2006-06-23 02:05:41 -070034 s32 *pcount;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070035 int cpu = get_cpu();
36
37 pcount = per_cpu_ptr(fbc->counters, cpu);
38 count = *pcount + amount;
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070039 if (count >= batch || count <= -batch) {
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070040 spin_lock(&fbc->lock);
41 fbc->count += count;
42 *pcount = 0;
43 spin_unlock(&fbc->lock);
44 } else {
45 *pcount = count;
46 }
47 put_cpu();
48}
Peter Zijlstra252e0ba2007-10-16 23:25:43 -070049EXPORT_SYMBOL(__percpu_counter_add);
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070050
51/*
52 * Add up all the per-cpu counts, return the result. This is a more accurate
53 * but much slower version of percpu_counter_read_positive()
54 */
Andrew Morton02d21162008-12-09 13:14:14 -080055s64 __percpu_counter_sum(struct percpu_counter *fbc)
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070056{
Mingming Cao0216bfc2006-06-23 02:05:41 -070057 s64 ret;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070058 int cpu;
59
60 spin_lock(&fbc->lock);
61 ret = fbc->count;
Andrew Mortonb4ef02962007-07-15 23:39:51 -070062 for_each_online_cpu(cpu) {
Mingming Cao0216bfc2006-06-23 02:05:41 -070063 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070064 ret += *pcount;
65 }
66 spin_unlock(&fbc->lock);
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070067 return ret;
Ravikiran G Thirumalai3cbc5642006-06-23 02:05:40 -070068}
Peter Zijlstrabf1d89c2007-10-16 23:25:45 -070069EXPORT_SYMBOL(__percpu_counter_sum);
Andrew Mortonc67ad912007-07-15 23:39:51 -070070
Peter Zijlstraea319512008-12-26 15:08:55 +010071int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
72 struct lock_class_key *key)
Andrew Mortonc67ad912007-07-15 23:39:51 -070073{
74 spin_lock_init(&fbc->lock);
Peter Zijlstraea319512008-12-26 15:08:55 +010075 lockdep_set_class(&fbc->lock, key);
Andrew Mortonc67ad912007-07-15 23:39:51 -070076 fbc->count = amount;
77 fbc->counters = alloc_percpu(s32);
Peter Zijlstra833f4072007-10-16 23:25:45 -070078 if (!fbc->counters)
79 return -ENOMEM;
Andrew Mortonc67ad912007-07-15 23:39:51 -070080#ifdef CONFIG_HOTPLUG_CPU
81 mutex_lock(&percpu_counters_lock);
82 list_add(&fbc->list, &percpu_counters);
83 mutex_unlock(&percpu_counters_lock);
84#endif
Peter Zijlstra833f4072007-10-16 23:25:45 -070085 return 0;
Andrew Mortonc67ad912007-07-15 23:39:51 -070086}
Peter Zijlstraea319512008-12-26 15:08:55 +010087EXPORT_SYMBOL(__percpu_counter_init);
Peter Zijlstradc62a302007-10-16 23:25:46 -070088
Andrew Mortonc67ad912007-07-15 23:39:51 -070089void percpu_counter_destroy(struct percpu_counter *fbc)
90{
Peter Zijlstra833f4072007-10-16 23:25:45 -070091 if (!fbc->counters)
92 return;
93
Andrew Mortonc67ad912007-07-15 23:39:51 -070094#ifdef CONFIG_HOTPLUG_CPU
95 mutex_lock(&percpu_counters_lock);
96 list_del(&fbc->list);
97 mutex_unlock(&percpu_counters_lock);
98#endif
Eric Dumazetfd3d6642008-12-09 13:14:11 -080099 free_percpu(fbc->counters);
100 fbc->counters = NULL;
Andrew Mortonc67ad912007-07-15 23:39:51 -0700101}
102EXPORT_SYMBOL(percpu_counter_destroy);
103
104#ifdef CONFIG_HOTPLUG_CPU
105static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
106 unsigned long action, void *hcpu)
107{
108 unsigned int cpu;
109 struct percpu_counter *fbc;
110
111 if (action != CPU_DEAD)
112 return NOTIFY_OK;
113
114 cpu = (unsigned long)hcpu;
115 mutex_lock(&percpu_counters_lock);
116 list_for_each_entry(fbc, &percpu_counters, list) {
117 s32 *pcount;
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700118 unsigned long flags;
Andrew Mortonc67ad912007-07-15 23:39:51 -0700119
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700120 spin_lock_irqsave(&fbc->lock, flags);
Andrew Mortonc67ad912007-07-15 23:39:51 -0700121 pcount = per_cpu_ptr(fbc->counters, cpu);
122 fbc->count += *pcount;
123 *pcount = 0;
Gautham R Shenoyd2b20b12007-10-18 23:40:47 -0700124 spin_unlock_irqrestore(&fbc->lock, flags);
Andrew Mortonc67ad912007-07-15 23:39:51 -0700125 }
126 mutex_unlock(&percpu_counters_lock);
127 return NOTIFY_OK;
128}
129
130static int __init percpu_counter_startup(void)
131{
132 hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
133 return 0;
134}
135module_init(percpu_counter_startup);
136#endif