thinkpad-acpi: R52 brightness_mode has been confirmed
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / lib / percpu_counter.c
blob4950d887c90ba968f64a9ceb4f47e5b7e155f314
1 /*
2 * Fast batching percpu counters.
3 */
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>
12 #ifdef CONFIG_HOTPLUG_CPU
13 static LIST_HEAD(percpu_counters);
14 static DEFINE_MUTEX(percpu_counters_lock);
15 #endif
17 void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
19 int cpu;
21 spin_lock(&fbc->lock);
22 for_each_possible_cpu(cpu) {
23 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
24 *pcount = 0;
26 fbc->count = amount;
27 spin_unlock(&fbc->lock);
29 EXPORT_SYMBOL(percpu_counter_set);
31 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
33 s64 count;
34 s32 *pcount;
35 int cpu = get_cpu();
37 pcount = per_cpu_ptr(fbc->counters, cpu);
38 count = *pcount + amount;
39 if (count >= batch || count <= -batch) {
40 spin_lock(&fbc->lock);
41 fbc->count += count;
42 *pcount = 0;
43 spin_unlock(&fbc->lock);
44 } else {
45 *pcount = count;
47 put_cpu();
49 EXPORT_SYMBOL(__percpu_counter_add);
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()
55 s64 __percpu_counter_sum(struct percpu_counter *fbc)
57 s64 ret;
58 int cpu;
60 spin_lock(&fbc->lock);
61 ret = fbc->count;
62 for_each_online_cpu(cpu) {
63 s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
64 ret += *pcount;
65 *pcount = 0;
67 fbc->count = ret;
69 spin_unlock(&fbc->lock);
70 return ret;
72 EXPORT_SYMBOL(__percpu_counter_sum);
74 static struct lock_class_key percpu_counter_irqsafe;
76 int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
78 spin_lock_init(&fbc->lock);
79 fbc->count = amount;
80 fbc->counters = alloc_percpu(s32);
81 if (!fbc->counters)
82 return -ENOMEM;
83 #ifdef CONFIG_HOTPLUG_CPU
84 INIT_LIST_HEAD(&fbc->list);
85 mutex_lock(&percpu_counters_lock);
86 list_add(&fbc->list, &percpu_counters);
87 mutex_unlock(&percpu_counters_lock);
88 #endif
89 return 0;
91 EXPORT_SYMBOL(percpu_counter_init);
93 int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount)
95 int err;
97 err = percpu_counter_init(fbc, amount);
98 if (!err)
99 lockdep_set_class(&fbc->lock, &percpu_counter_irqsafe);
100 return err;
103 void percpu_counter_destroy(struct percpu_counter *fbc)
105 if (!fbc->counters)
106 return;
108 free_percpu(fbc->counters);
109 fbc->counters = NULL;
110 #ifdef CONFIG_HOTPLUG_CPU
111 mutex_lock(&percpu_counters_lock);
112 list_del(&fbc->list);
113 mutex_unlock(&percpu_counters_lock);
114 #endif
116 EXPORT_SYMBOL(percpu_counter_destroy);
118 #ifdef CONFIG_HOTPLUG_CPU
119 static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
120 unsigned long action, void *hcpu)
122 unsigned int cpu;
123 struct percpu_counter *fbc;
125 if (action != CPU_DEAD)
126 return NOTIFY_OK;
128 cpu = (unsigned long)hcpu;
129 mutex_lock(&percpu_counters_lock);
130 list_for_each_entry(fbc, &percpu_counters, list) {
131 s32 *pcount;
132 unsigned long flags;
134 spin_lock_irqsave(&fbc->lock, flags);
135 pcount = per_cpu_ptr(fbc->counters, cpu);
136 fbc->count += *pcount;
137 *pcount = 0;
138 spin_unlock_irqrestore(&fbc->lock, flags);
140 mutex_unlock(&percpu_counters_lock);
141 return NOTIFY_OK;
144 static int __init percpu_counter_startup(void)
146 hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
147 return 0;
149 module_init(percpu_counter_startup);
150 #endif