2006-06-23 02:05:40 -07:00
|
|
|
/*
|
|
|
|
* Fast batching percpu counters.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/percpu_counter.h>
|
2007-07-15 23:39:51 -07:00
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/cpu.h>
|
2006-06-23 02:05:40 -07:00
|
|
|
#include <linux/module.h>
|
2010-10-26 14:23:05 -07:00
|
|
|
#include <linux/debugobjects.h>
|
2006-06-23 02:05:40 -07:00
|
|
|
|
2007-07-15 23:39:51 -07:00
|
|
|
static LIST_HEAD(percpu_counters);
|
|
|
|
static DEFINE_MUTEX(percpu_counters_lock);
|
|
|
|
|
2010-10-26 14:23:05 -07:00
|
|
|
#ifdef CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER
|
|
|
|
|
|
|
|
static struct debug_obj_descr percpu_counter_debug_descr;
|
|
|
|
|
|
|
|
static int percpu_counter_fixup_free(void *addr, enum debug_obj_state state)
|
|
|
|
{
|
|
|
|
struct percpu_counter *fbc = addr;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case ODEBUG_STATE_ACTIVE:
|
|
|
|
percpu_counter_destroy(fbc);
|
|
|
|
debug_object_free(fbc, &percpu_counter_debug_descr);
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct debug_obj_descr percpu_counter_debug_descr = {
|
|
|
|
.name = "percpu_counter",
|
|
|
|
.fixup_free = percpu_counter_fixup_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
debug_object_init(fbc, &percpu_counter_debug_descr);
|
|
|
|
debug_object_activate(fbc, &percpu_counter_debug_descr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
|
|
|
|
{
|
|
|
|
debug_object_deactivate(fbc, &percpu_counter_debug_descr);
|
|
|
|
debug_object_free(fbc, &percpu_counter_debug_descr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
|
|
|
|
static inline void debug_percpu_counter_activate(struct percpu_counter *fbc)
|
|
|
|
{ }
|
|
|
|
static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
|
|
|
|
{ }
|
|
|
|
#endif /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
|
|
|
|
|
2007-10-16 23:25:44 -07:00
|
|
|
void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
|
|
|
|
spin_lock(&fbc->lock);
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
|
|
s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
|
|
|
|
*pcount = 0;
|
|
|
|
}
|
|
|
|
fbc->count = amount;
|
|
|
|
spin_unlock(&fbc->lock);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(percpu_counter_set);
|
|
|
|
|
2007-10-16 23:25:43 -07:00
|
|
|
void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch)
|
2006-06-23 02:05:40 -07:00
|
|
|
{
|
2007-10-16 23:25:43 -07:00
|
|
|
s64 count;
|
2006-06-23 02:05:41 -07:00
|
|
|
s32 *pcount;
|
2006-06-23 02:05:40 -07:00
|
|
|
|
2010-10-26 14:23:09 -07:00
|
|
|
preempt_disable();
|
|
|
|
pcount = this_cpu_ptr(fbc->counters);
|
2006-06-23 02:05:40 -07:00
|
|
|
count = *pcount + amount;
|
2007-10-16 23:25:43 -07:00
|
|
|
if (count >= batch || count <= -batch) {
|
2006-06-23 02:05:40 -07:00
|
|
|
spin_lock(&fbc->lock);
|
|
|
|
fbc->count += count;
|
|
|
|
*pcount = 0;
|
|
|
|
spin_unlock(&fbc->lock);
|
|
|
|
} else {
|
|
|
|
*pcount = count;
|
|
|
|
}
|
2010-10-26 14:23:09 -07:00
|
|
|
preempt_enable();
|
2006-06-23 02:05:40 -07:00
|
|
|
}
|
2007-10-16 23:25:43 -07:00
|
|
|
EXPORT_SYMBOL(__percpu_counter_add);
|
2006-06-23 02:05:40 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add up all the per-cpu counts, return the result. This is a more accurate
|
|
|
|
* but much slower version of percpu_counter_read_positive()
|
|
|
|
*/
|
2008-12-09 14:14:14 -07:00
|
|
|
s64 __percpu_counter_sum(struct percpu_counter *fbc)
|
2006-06-23 02:05:40 -07:00
|
|
|
{
|
2006-06-23 02:05:41 -07:00
|
|
|
s64 ret;
|
2006-06-23 02:05:40 -07:00
|
|
|
int cpu;
|
|
|
|
|
|
|
|
spin_lock(&fbc->lock);
|
|
|
|
ret = fbc->count;
|
2007-07-15 23:39:51 -07:00
|
|
|
for_each_online_cpu(cpu) {
|
2006-06-23 02:05:41 -07:00
|
|
|
s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
|
2006-06-23 02:05:40 -07:00
|
|
|
ret += *pcount;
|
|
|
|
}
|
|
|
|
spin_unlock(&fbc->lock);
|
2007-10-16 23:25:45 -07:00
|
|
|
return ret;
|
2006-06-23 02:05:40 -07:00
|
|
|
}
|
2007-10-16 23:25:45 -07:00
|
|
|
EXPORT_SYMBOL(__percpu_counter_sum);
|
2007-07-15 23:39:51 -07:00
|
|
|
|
2008-12-26 07:08:55 -07:00
|
|
|
int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
|
|
|
|
struct lock_class_key *key)
|
2007-07-15 23:39:51 -07:00
|
|
|
{
|
|
|
|
spin_lock_init(&fbc->lock);
|
2008-12-26 07:08:55 -07:00
|
|
|
lockdep_set_class(&fbc->lock, key);
|
2007-07-15 23:39:51 -07:00
|
|
|
fbc->count = amount;
|
|
|
|
fbc->counters = alloc_percpu(s32);
|
2007-10-16 23:25:45 -07:00
|
|
|
if (!fbc->counters)
|
|
|
|
return -ENOMEM;
|
2010-10-26 14:23:05 -07:00
|
|
|
|
|
|
|
debug_percpu_counter_activate(fbc);
|
|
|
|
|
2007-07-15 23:39:51 -07:00
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
2010-10-26 14:21:20 -07:00
|
|
|
INIT_LIST_HEAD(&fbc->list);
|
2007-07-15 23:39:51 -07:00
|
|
|
mutex_lock(&percpu_counters_lock);
|
|
|
|
list_add(&fbc->list, &percpu_counters);
|
|
|
|
mutex_unlock(&percpu_counters_lock);
|
|
|
|
#endif
|
2007-10-16 23:25:45 -07:00
|
|
|
return 0;
|
2007-07-15 23:39:51 -07:00
|
|
|
}
|
2008-12-26 07:08:55 -07:00
|
|
|
EXPORT_SYMBOL(__percpu_counter_init);
|
2007-07-15 23:39:51 -07:00
|
|
|
|
|
|
|
void percpu_counter_destroy(struct percpu_counter *fbc)
|
|
|
|
{
|
2007-10-16 23:25:45 -07:00
|
|
|
if (!fbc->counters)
|
|
|
|
return;
|
|
|
|
|
2010-10-26 14:23:05 -07:00
|
|
|
debug_percpu_counter_deactivate(fbc);
|
|
|
|
|
2007-07-15 23:39:51 -07:00
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
|
|
mutex_lock(&percpu_counters_lock);
|
|
|
|
list_del(&fbc->list);
|
|
|
|
mutex_unlock(&percpu_counters_lock);
|
|
|
|
#endif
|
2008-12-09 14:14:11 -07:00
|
|
|
free_percpu(fbc->counters);
|
|
|
|
fbc->counters = NULL;
|
2007-07-15 23:39:51 -07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(percpu_counter_destroy);
|
|
|
|
|
2009-01-06 15:41:04 -07:00
|
|
|
int percpu_counter_batch __read_mostly = 32;
|
|
|
|
EXPORT_SYMBOL(percpu_counter_batch);
|
|
|
|
|
|
|
|
static void compute_batch_value(void)
|
|
|
|
{
|
|
|
|
int nr = num_online_cpus();
|
|
|
|
|
|
|
|
percpu_counter_batch = max(32, nr*2);
|
|
|
|
}
|
|
|
|
|
2007-07-15 23:39:51 -07:00
|
|
|
static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
|
|
|
|
unsigned long action, void *hcpu)
|
|
|
|
{
|
2009-01-06 15:41:04 -07:00
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
2007-07-15 23:39:51 -07:00
|
|
|
unsigned int cpu;
|
|
|
|
struct percpu_counter *fbc;
|
|
|
|
|
2009-01-06 15:41:04 -07:00
|
|
|
compute_batch_value();
|
2007-07-15 23:39:51 -07:00
|
|
|
if (action != CPU_DEAD)
|
|
|
|
return NOTIFY_OK;
|
|
|
|
|
|
|
|
cpu = (unsigned long)hcpu;
|
|
|
|
mutex_lock(&percpu_counters_lock);
|
|
|
|
list_for_each_entry(fbc, &percpu_counters, list) {
|
|
|
|
s32 *pcount;
|
2007-10-18 23:40:47 -07:00
|
|
|
unsigned long flags;
|
2007-07-15 23:39:51 -07:00
|
|
|
|
2007-10-18 23:40:47 -07:00
|
|
|
spin_lock_irqsave(&fbc->lock, flags);
|
2007-07-15 23:39:51 -07:00
|
|
|
pcount = per_cpu_ptr(fbc->counters, cpu);
|
|
|
|
fbc->count += *pcount;
|
|
|
|
*pcount = 0;
|
2007-10-18 23:40:47 -07:00
|
|
|
spin_unlock_irqrestore(&fbc->lock, flags);
|
2007-07-15 23:39:51 -07:00
|
|
|
}
|
|
|
|
mutex_unlock(&percpu_counters_lock);
|
2009-01-06 15:41:04 -07:00
|
|
|
#endif
|
2007-07-15 23:39:51 -07:00
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-09 17:19:04 -07:00
|
|
|
/*
|
|
|
|
* Compare counter against given value.
|
|
|
|
* Return 1 if greater, 0 if equal and -1 if less
|
|
|
|
*/
|
|
|
|
int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
|
|
|
|
{
|
|
|
|
s64 count;
|
|
|
|
|
|
|
|
count = percpu_counter_read(fbc);
|
|
|
|
/* Check to see if rough count will be sufficient for comparison */
|
|
|
|
if (abs(count - rhs) > (percpu_counter_batch*num_online_cpus())) {
|
|
|
|
if (count > rhs)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Need to use precise count */
|
|
|
|
count = percpu_counter_sum(fbc);
|
|
|
|
if (count > rhs)
|
|
|
|
return 1;
|
|
|
|
else if (count < rhs)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(percpu_counter_compare);
|
|
|
|
|
2007-07-15 23:39:51 -07:00
|
|
|
static int __init percpu_counter_startup(void)
|
|
|
|
{
|
2009-01-06 15:41:04 -07:00
|
|
|
compute_batch_value();
|
2007-07-15 23:39:51 -07:00
|
|
|
hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(percpu_counter_startup);
|