]> Pileus Git - ~andy/linux/blobdiff - lib/percpu_counter.c
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[~andy/linux] / lib / percpu_counter.c
index b255b939bc1b85bd4fc385df4e1322e9472b2b26..ec9048e74f448ce3a79522aef9ac5f4c4b350737 100644 (file)
@@ -9,10 +9,8 @@
 #include <linux/cpu.h>
 #include <linux/module.h>
 
-#ifdef CONFIG_HOTPLUG_CPU
 static LIST_HEAD(percpu_counters);
 static DEFINE_MUTEX(percpu_counters_lock);
-#endif
 
 void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
 {
@@ -68,11 +66,11 @@ s64 __percpu_counter_sum(struct percpu_counter *fbc)
 }
 EXPORT_SYMBOL(__percpu_counter_sum);
 
-static struct lock_class_key percpu_counter_irqsafe;
-
-int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
+int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
+                         struct lock_class_key *key)
 {
        spin_lock_init(&fbc->lock);
+       lockdep_set_class(&fbc->lock, key);
        fbc->count = amount;
        fbc->counters = alloc_percpu(s32);
        if (!fbc->counters)
@@ -84,17 +82,7 @@ int percpu_counter_init(struct percpu_counter *fbc, s64 amount)
 #endif
        return 0;
 }
-EXPORT_SYMBOL(percpu_counter_init);
-
-int percpu_counter_init_irq(struct percpu_counter *fbc, s64 amount)
-{
-       int err;
-
-       err = percpu_counter_init(fbc, amount);
-       if (!err)
-               lockdep_set_class(&fbc->lock, &percpu_counter_irqsafe);
-       return err;
-}
+EXPORT_SYMBOL(__percpu_counter_init);
 
 void percpu_counter_destroy(struct percpu_counter *fbc)
 {
@@ -111,13 +99,24 @@ void percpu_counter_destroy(struct percpu_counter *fbc)
 }
 EXPORT_SYMBOL(percpu_counter_destroy);
 
-#ifdef CONFIG_HOTPLUG_CPU
+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);
+}
+
 static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
                                        unsigned long action, void *hcpu)
 {
+#ifdef CONFIG_HOTPLUG_CPU
        unsigned int cpu;
        struct percpu_counter *fbc;
 
+       compute_batch_value();
        if (action != CPU_DEAD)
                return NOTIFY_OK;
 
@@ -134,13 +133,41 @@ static int __cpuinit percpu_counter_hotcpu_callback(struct notifier_block *nb,
                spin_unlock_irqrestore(&fbc->lock, flags);
        }
        mutex_unlock(&percpu_counters_lock);
+#endif
        return NOTIFY_OK;
 }
 
+/*
+ * 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);
+
 static int __init percpu_counter_startup(void)
 {
+       compute_batch_value();
        hotcpu_notifier(percpu_counter_hotcpu_callback, 0);
        return 0;
 }
 module_init(percpu_counter_startup);
-#endif