Port Linux's percpu_counters
authorBarret Rhoden <brho@cs.berkeley.edu>
Fri, 4 May 2018 22:33:17 +0000 (18:33 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 22 May 2018 21:53:04 +0000 (17:53 -0400)
Note the hokey per_cpu_ptr(x) -> _PERCPU_VARPTR(*x).

Also, these aren't as big as the WARNING says - neither are Linux's, I
think.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/include/percpu_counter.h
kern/lib/Kbuild
kern/lib/percpu_counter.c

index 04f1552..72bf7ef 100644 (file)
@@ -1,20 +1,13 @@
+/* Copyright (C) 1991-2017, the Linux Kernel authors */
+
 #ifndef _LINUX_PERCPU_COUNTER_H
 #define _LINUX_PERCPU_COUNTER_H
 /*
 #ifndef _LINUX_PERCPU_COUNTER_H
 #define _LINUX_PERCPU_COUNTER_H
 /*
- * A simple "approximate counter" for use in ext2 and ext3 superblocks.
- *
- * WARNING: these things are HUGE.  4 kbytes per counter on 32-way P4.
+ * A simple "approximate counter".
  */
 
  */
 
-#include <linux/spinlock.h>
-#include <linux/smp.h>
-#include <linux/list.h>
-#include <linux/threads.h>
-#include <linux/percpu.h>
-#include <linux/types.h>
-#include <linux/gfp.h>
-
-#ifdef CONFIG_SMP
+#include <atomic.h>
+#include <list.h>
 
 struct percpu_counter {
        spinlock_t lock;
 
 struct percpu_counter {
        spinlock_t lock;
@@ -22,21 +15,12 @@ struct percpu_counter {
 #ifdef CONFIG_HOTPLUG_CPU
        struct list_head list;  /* All percpu_counters are on a list */
 #endif
 #ifdef CONFIG_HOTPLUG_CPU
        struct list_head list;  /* All percpu_counters are on a list */
 #endif
-       int32_t __percpu *counters;
+       int32_t *counters;
 };
 
 extern int percpu_counter_batch;
 
 };
 
 extern int percpu_counter_batch;
 
-int __percpu_counter_init(struct percpu_counter *fbc, int64_t amount, gfp_t gfp,
-                         struct lock_class_key *key);
-
-#define percpu_counter_init(fbc, value, gfp)                           \
-       ({                                                              \
-               static struct lock_class_key __key;                     \
-                                                                       \
-               __percpu_counter_init(fbc, value, gfp, &__key);         \
-       })
-
+int percpu_counter_init(struct percpu_counter *fbc, int64_t amount, int gfp);
 void percpu_counter_destroy(struct percpu_counter *fbc);
 void percpu_counter_set(struct percpu_counter *fbc, int64_t amount);
 void percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
 void percpu_counter_destroy(struct percpu_counter *fbc);
 void percpu_counter_set(struct percpu_counter *fbc, int64_t amount);
 void percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
@@ -93,93 +77,6 @@ static inline int percpu_counter_initialized(struct percpu_counter *fbc)
        return (fbc->counters != NULL);
 }
 
        return (fbc->counters != NULL);
 }
 
-#else /* !CONFIG_SMP */
-
-struct percpu_counter {
-       int64_t count;
-};
-
-static inline int percpu_counter_init(struct percpu_counter *fbc, int64_t amount,
-                                     gfp_t gfp)
-{
-       fbc->count = amount;
-       return 0;
-}
-
-static inline void percpu_counter_destroy(struct percpu_counter *fbc)
-{
-}
-
-static inline void percpu_counter_set(struct percpu_counter *fbc,
-                                     int64_t amount)
-{
-       fbc->count = amount;
-}
-
-static inline int percpu_counter_compare(struct percpu_counter *fbc,
-                                        int64_t rhs)
-{
-       if (fbc->count > rhs)
-               return 1;
-       else if (fbc->count < rhs)
-               return -1;
-       else
-               return 0;
-}
-
-static inline int
-__percpu_counter_compare(struct percpu_counter *fbc, int64_t rhs,
-                        int32_t batch)
-{
-       return percpu_counter_compare(fbc, rhs);
-}
-
-static inline void
-percpu_counter_add(struct percpu_counter *fbc, int64_t amount)
-{
-       preempt_disable();
-       fbc->count += amount;
-       preempt_enable();
-}
-
-static inline void
-percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
-                        int32_t batch)
-{
-       percpu_counter_add(fbc, amount);
-}
-
-static inline int64_t percpu_counter_read(struct percpu_counter *fbc)
-{
-       return fbc->count;
-}
-
-/*
- * percpu_counter is intended to track positive numbers. In the UP case the
- * number should never be negative.
- */
-static inline int64_t percpu_counter_read_positive(struct percpu_counter *fbc)
-{
-       return fbc->count;
-}
-
-static inline int64_t percpu_counter_sum_positive(struct percpu_counter *fbc)
-{
-       return percpu_counter_read_positive(fbc);
-}
-
-static inline int64_t percpu_counter_sum(struct percpu_counter *fbc)
-{
-       return percpu_counter_read(fbc);
-}
-
-static inline int percpu_counter_initialized(struct percpu_counter *fbc)
-{
-       return 1;
-}
-
-#endif /* CONFIG_SMP */
-
 static inline void percpu_counter_inc(struct percpu_counter *fbc)
 {
        percpu_counter_add(fbc, 1);
 static inline void percpu_counter_inc(struct percpu_counter *fbc)
 {
        percpu_counter_add(fbc, 1);
index a0e33c3..f8cb282 100644 (file)
@@ -1,6 +1,7 @@
 obj-y                                          += address_range.o
 obj-y                                          += circular_buffer.o
 obj-y                                          += cpio.o
 obj-y                                          += address_range.o
 obj-y                                          += circular_buffer.o
 obj-y                                          += cpio.o
+obj-y                                          += percpu_counter.o
 obj-y                                          += rbtree.o
 obj-y                                          += slice.o
 obj-y                                          += sort.o
 obj-y                                          += rbtree.o
 obj-y                                          += slice.o
 obj-y                                          += sort.o
index c15f7c8..1626e38 100644 (file)
@@ -1,61 +1,11 @@
+/* Copyright (C) 1991-2017, the Linux Kernel authors */
+
 /*
  * Fast batching percpu counters.
  */
 
 /*
  * Fast batching percpu counters.
  */
 
-#include <linux/percpu_counter.h>
-#include <linux/notifier.h>
-#include <linux/mutex.h>
-#include <linux/init.h>
-#include <linux/cpu.h>
-#include <linux/module.h>
-#include <linux/debugobjects.h>
-
-#ifdef CONFIG_HOTPLUG_CPU
-static LIST_HEAD(percpu_counters);
-static DEFINE_SPINLOCK(percpu_counters_lock);
-#endif
-
-#ifdef CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER
-
-static struct debug_obj_descr percpu_counter_debug_descr;
-
-static bool 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 true;
-       default:
-               return false;
-       }
-}
-
-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 */
+#include <percpu_counter.h>
+#include <linux_compat.h>
 
 void percpu_counter_set(struct percpu_counter *fbc, int64_t amount)
 {
 
 void percpu_counter_set(struct percpu_counter *fbc, int64_t amount)
 {
@@ -64,13 +14,12 @@ void percpu_counter_set(struct percpu_counter *fbc, int64_t amount)
 
        spin_lock_irqsave(&fbc->lock);
        for_each_possible_cpu(cpu) {
 
        spin_lock_irqsave(&fbc->lock);
        for_each_possible_cpu(cpu) {
-               int32_t *pcount = per_cpu_ptr(fbc->counters, cpu);
+               int32_t *pcount = _PERCPU_VARPTR(*fbc->counters, cpu);
                *pcount = 0;
        }
        fbc->count = amount;
        spin_unlock_irqsave(&fbc->lock);
 }
                *pcount = 0;
        }
        fbc->count = amount;
        spin_unlock_irqsave(&fbc->lock);
 }
-EXPORT_SYMBOL(percpu_counter_set);
 
 /**
  * This function is both preempt and irq safe. The former is due to explicit
 
 /**
  * This function is both preempt and irq safe. The former is due to explicit
@@ -97,7 +46,6 @@ void percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
        }
        preempt_enable();
 }
        }
        preempt_enable();
 }
-EXPORT_SYMBOL(percpu_counter_add_batch);
 
 /*
  * Add up all the per-cpu counts, return the result.  This is a more accurate
 
 /*
  * Add up all the per-cpu counts, return the result.  This is a more accurate
@@ -112,37 +60,24 @@ int64_t __percpu_counter_sum(struct percpu_counter *fbc)
        spin_lock_irqsave(&fbc->lock);
        ret = fbc->count;
        for_each_online_cpu(cpu) {
        spin_lock_irqsave(&fbc->lock);
        ret = fbc->count;
        for_each_online_cpu(cpu) {
-               int32_t *pcount = per_cpu_ptr(fbc->counters, cpu);
+               int32_t *pcount = _PERCPU_VARPTR(*fbc->counters, cpu);
                ret += *pcount;
        }
        spin_unlock_irqsave(&fbc->lock);
        return ret;
 }
                ret += *pcount;
        }
        spin_unlock_irqsave(&fbc->lock);
        return ret;
 }
-EXPORT_SYMBOL(__percpu_counter_sum);
 
 
-int __percpu_counter_init(struct percpu_counter *fbc, int64_t amount, gfp_t gfp,
-                         struct lock_class_key *key)
+int percpu_counter_init(struct percpu_counter *fbc, int64_t amount, gfp_t gfp)
 {
        unsigned long flags __maybe_unused;
 
        spinlock_init_irqsave(&fbc->lock);
 {
        unsigned long flags __maybe_unused;
 
        spinlock_init_irqsave(&fbc->lock);
-       lockdep_set_class(&fbc->lock, key);
        fbc->count = amount;
        fbc->counters = alloc_percpu_gfp(int32_t, gfp);
        if (!fbc->counters)
                return -ENOMEM;
        fbc->count = amount;
        fbc->counters = alloc_percpu_gfp(int32_t, gfp);
        if (!fbc->counters)
                return -ENOMEM;
-
-       debug_percpu_counter_activate(fbc);
-
-#ifdef CONFIG_HOTPLUG_CPU
-       INIT_LIST_HEAD(&fbc->list);
-       spin_lock_irqsave(&percpu_counters_lock);
-       list_add(&fbc->list, &percpu_counters);
-       spin_unlock_irqsave(&percpu_counters_lock);
-#endif
        return 0;
 }
        return 0;
 }
-EXPORT_SYMBOL(__percpu_counter_init);
 
 void percpu_counter_destroy(struct percpu_counter *fbc)
 {
 
 void percpu_counter_destroy(struct percpu_counter *fbc)
 {
@@ -150,51 +85,11 @@ void percpu_counter_destroy(struct percpu_counter *fbc)
 
        if (!fbc->counters)
                return;
 
        if (!fbc->counters)
                return;
-
-       debug_percpu_counter_deactivate(fbc);
-
-#ifdef CONFIG_HOTPLUG_CPU
-       spin_lock_irqsave(&percpu_counters_lock);
-       list_del(&fbc->list);
-       spin_unlock_irqsave(&percpu_counters_lock);
-#endif
        free_percpu(fbc->counters);
        fbc->counters = NULL;
 }
        free_percpu(fbc->counters);
        fbc->counters = NULL;
 }
-EXPORT_SYMBOL(percpu_counter_destroy);
 
 int percpu_counter_batch __read_mostly = 32;
 
 int percpu_counter_batch __read_mostly = 32;
-EXPORT_SYMBOL(percpu_counter_batch);
-
-static int compute_batch_value(unsigned int cpu)
-{
-       int nr = num_online_cpus();
-
-       percpu_counter_batch = MAX(32, nr * 2);
-       return 0;
-}
-
-static int percpu_counter_cpu_dead(unsigned int cpu)
-{
-#ifdef CONFIG_HOTPLUG_CPU
-       struct percpu_counter *fbc;
-
-       compute_batch_value(cpu);
-
-       spin_lock_irqsave(&percpu_counters_lock);
-       list_for_each_entry(fbc, &percpu_counters, list) {
-               int32_t *pcount;
-
-               spin_lock_irqsave(&fbc->lock);
-               pcount = per_cpu_ptr(fbc->counters, cpu);
-               fbc->count += *pcount;
-               *pcount = 0;
-               spin_unlock_irqsave(&fbc->lock);
-       }
-       spin_unlock_irqsave(&percpu_counters_lock);
-#endif
-       return 0;
-}
 
 /*
  * Compare counter against given value.
 
 /*
  * Compare counter against given value.
@@ -222,19 +117,3 @@ int __percpu_counter_compare(struct percpu_counter *fbc, int64_t rhs,
        else
                return 0;
 }
        else
                return 0;
 }
-EXPORT_SYMBOL(__percpu_counter_compare);
-
-static int __init percpu_counter_startup(void)
-{
-       int ret;
-
-       ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
-                               compute_batch_value, NULL);
-       warn_on(ret < 0);
-       ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
-                                       "lib/percpu_cnt:dead", NULL,
-                                       percpu_counter_cpu_dead);
-       warn_on(ret < 0);
-       return 0;
-}
-module_init(percpu_counter_startup);