Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Fast batching percpu counters. |
| 3 | */ |
| 4 | |
| 5 | #include <linux/percpu_counter.h> |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 6 | #include <linux/notifier.h> |
| 7 | #include <linux/mutex.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/cpu.h> |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 10 | #include <linux/module.h> |
| 11 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 12 | #ifdef CONFIG_HOTPLUG_CPU |
| 13 | static LIST_HEAD(percpu_counters); |
| 14 | static DEFINE_MUTEX(percpu_counters_lock); |
| 15 | #endif |
| 16 | |
Peter Zijlstra | 3a587f4 | 2007-10-16 23:25:44 -0700 | [diff] [blame] | 17 | void 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 | } |
| 29 | EXPORT_SYMBOL(percpu_counter_set); |
| 30 | |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 31 | void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch) |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 32 | { |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 33 | s64 count; |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 34 | s32 *pcount; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 35 | int cpu = get_cpu(); |
| 36 | |
| 37 | pcount = per_cpu_ptr(fbc->counters, cpu); |
| 38 | count = *pcount + amount; |
Peter Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 39 | if (count >= batch || count <= -batch) { |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 40 | 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 Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 49 | EXPORT_SYMBOL(__percpu_counter_add); |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 50 | |
| 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 | */ |
Mingming Cao | 1f7c14c | 2008-10-09 12:50:59 -0400 | [diff] [blame] | 55 | s64 __percpu_counter_sum(struct percpu_counter *fbc) |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 56 | { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 57 | s64 ret; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 58 | int cpu; |
| 59 | |
| 60 | spin_lock(&fbc->lock); |
| 61 | ret = fbc->count; |
Andrew Morton | b4ef029 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 62 | for_each_online_cpu(cpu) { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 63 | s32 *pcount = per_cpu_ptr(fbc->counters, cpu); |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 64 | ret += *pcount; |
Mingming Cao | 1f7c14c | 2008-10-09 12:50:59 -0400 | [diff] [blame] | 65 | *pcount = 0; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 66 | } |
Mingming Cao | 1f7c14c | 2008-10-09 12:50:59 -0400 | [diff] [blame] | 67 | fbc->count = ret; |
Mingming Cao | e8ced39 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 68 | |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 69 | spin_unlock(&fbc->lock); |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 70 | return ret; |
Ravikiran G Thirumalai | 3cbc564 | 2006-06-23 02:05:40 -0700 | [diff] [blame] | 71 | } |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 72 | EXPORT_SYMBOL(__percpu_counter_sum); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 73 | |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame^] | 74 | int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, |
| 75 | struct lock_class_key *key) |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 76 | { |
| 77 | spin_lock_init(&fbc->lock); |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame^] | 78 | lockdep_set_class(&fbc->lock, key); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 79 | fbc->count = amount; |
| 80 | fbc->counters = alloc_percpu(s32); |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 81 | if (!fbc->counters) |
| 82 | return -ENOMEM; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_HOTPLUG_CPU |
| 84 | mutex_lock(&percpu_counters_lock); |
| 85 | list_add(&fbc->list, &percpu_counters); |
| 86 | mutex_unlock(&percpu_counters_lock); |
| 87 | #endif |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 88 | return 0; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 89 | } |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame^] | 90 | EXPORT_SYMBOL(__percpu_counter_init); |
Peter Zijlstra | dc62a30 | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 91 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 92 | void percpu_counter_destroy(struct percpu_counter *fbc) |
| 93 | { |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 94 | if (!fbc->counters) |
| 95 | return; |
| 96 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 97 | free_percpu(fbc->counters); |
Peter Zijlstra | cf0ca9f | 2008-04-30 00:54:32 -0700 | [diff] [blame] | 98 | fbc->counters = NULL; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 99 | #ifdef CONFIG_HOTPLUG_CPU |
| 100 | mutex_lock(&percpu_counters_lock); |
| 101 | list_del(&fbc->list); |
| 102 | mutex_unlock(&percpu_counters_lock); |
| 103 | #endif |
| 104 | } |
| 105 | EXPORT_SYMBOL(percpu_counter_destroy); |
| 106 | |
| 107 | #ifdef CONFIG_HOTPLUG_CPU |
| 108 | static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb, |
| 109 | unsigned long action, void *hcpu) |
| 110 | { |
| 111 | unsigned int cpu; |
| 112 | struct percpu_counter *fbc; |
| 113 | |
| 114 | if (action != CPU_DEAD) |
| 115 | return NOTIFY_OK; |
| 116 | |
| 117 | cpu = (unsigned long)hcpu; |
| 118 | mutex_lock(&percpu_counters_lock); |
| 119 | list_for_each_entry(fbc, &percpu_counters, list) { |
| 120 | s32 *pcount; |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 121 | unsigned long flags; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 122 | |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 123 | spin_lock_irqsave(&fbc->lock, flags); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 124 | pcount = per_cpu_ptr(fbc->counters, cpu); |
| 125 | fbc->count += *pcount; |
| 126 | *pcount = 0; |
Gautham R Shenoy | d2b20b1 | 2007-10-18 23:40:47 -0700 | [diff] [blame] | 127 | spin_unlock_irqrestore(&fbc->lock, flags); |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 128 | } |
| 129 | mutex_unlock(&percpu_counters_lock); |
| 130 | return NOTIFY_OK; |
| 131 | } |
| 132 | |
| 133 | static int __init percpu_counter_startup(void) |
| 134 | { |
| 135 | hotcpu_notifier(percpu_counter_hotcpu_callback, 0); |
| 136 | return 0; |
| 137 | } |
| 138 | module_init(percpu_counter_startup); |
| 139 | #endif |