Spatch Linux's percpu_counter
authorBarret Rhoden <brho@cs.berkeley.edu>
Thu, 3 May 2018 19:44:51 +0000 (15:44 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 22 May 2018 21:53:04 +0000 (17:53 -0400)
Some of these might have been done manually.  I was fixing up the cocci
files concurrently.

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

index ec06538..04f1552 100644 (file)
 #ifdef CONFIG_SMP
 
 struct percpu_counter {
 #ifdef CONFIG_SMP
 
 struct percpu_counter {
-       raw_spinlock_t lock;
-       s64 count;
+       spinlock_t lock;
+       int64_t count;
 #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
-       s32 __percpu *counters;
+       int32_t __percpu *counters;
 };
 
 extern int percpu_counter_batch;
 
 };
 
 extern int percpu_counter_batch;
 
-int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
+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)                           \
                          struct lock_class_key *key);
 
 #define percpu_counter_init(fbc, value, gfp)                           \
@@ -38,34 +38,37 @@ int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
        })
 
 void percpu_counter_destroy(struct percpu_counter *fbc);
        })
 
 void percpu_counter_destroy(struct percpu_counter *fbc);
-void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
-void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount,
-                             s32 batch);
-s64 __percpu_counter_sum(struct percpu_counter *fbc);
-int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch);
+void percpu_counter_set(struct percpu_counter *fbc, int64_t amount);
+void percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
+                             int32_t batch);
+int64_t __percpu_counter_sum(struct percpu_counter *fbc);
+int __percpu_counter_compare(struct percpu_counter *fbc, int64_t rhs,
+                            int32_t batch);
 
 
-static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
+static inline int percpu_counter_compare(struct percpu_counter *fbc,
+                                        int64_t rhs)
 {
        return __percpu_counter_compare(fbc, rhs, percpu_counter_batch);
 }
 
 {
        return __percpu_counter_compare(fbc, rhs, percpu_counter_batch);
 }
 
-static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount)
+static inline void percpu_counter_add(struct percpu_counter *fbc,
+                                     int64_t amount)
 {
        percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
 }
 
 {
        percpu_counter_add_batch(fbc, amount, percpu_counter_batch);
 }
 
-static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_sum_positive(struct percpu_counter *fbc)
 {
 {
-       s64 ret = __percpu_counter_sum(fbc);
+       int64_t ret = __percpu_counter_sum(fbc);
        return ret < 0 ? 0 : ret;
 }
 
        return ret < 0 ? 0 : ret;
 }
 
-static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_sum(struct percpu_counter *fbc)
 {
        return __percpu_counter_sum(fbc);
 }
 
 {
        return __percpu_counter_sum(fbc);
 }
 
-static inline s64 percpu_counter_read(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_read(struct percpu_counter *fbc)
 {
        return fbc->count;
 }
 {
        return fbc->count;
 }
@@ -75,11 +78,11 @@ static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  * number for some counter which should never be negative.
  *
  */
  * number for some counter which should never be negative.
  *
  */
-static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_read_positive(struct percpu_counter *fbc)
 {
 {
-       s64 ret = fbc->count;
+       int64_t ret = fbc->count;
 
 
-       barrier();              /* Prevent reloads of fbc->count */
+       cmb();          /* Prevent reloads of fbc->count */
        if (ret >= 0)
                return ret;
        return 0;
        if (ret >= 0)
                return ret;
        return 0;
@@ -93,10 +96,10 @@ static inline int percpu_counter_initialized(struct percpu_counter *fbc)
 #else /* !CONFIG_SMP */
 
 struct percpu_counter {
 #else /* !CONFIG_SMP */
 
 struct percpu_counter {
-       s64 count;
+       int64_t count;
 };
 
 };
 
-static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount,
+static inline int percpu_counter_init(struct percpu_counter *fbc, int64_t amount,
                                      gfp_t gfp)
 {
        fbc->count = amount;
                                      gfp_t gfp)
 {
        fbc->count = amount;
@@ -107,12 +110,14 @@ static inline void percpu_counter_destroy(struct percpu_counter *fbc)
 {
 }
 
 {
 }
 
-static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
+static inline void percpu_counter_set(struct percpu_counter *fbc,
+                                     int64_t amount)
 {
        fbc->count = amount;
 }
 
 {
        fbc->count = amount;
 }
 
-static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
+static inline int percpu_counter_compare(struct percpu_counter *fbc,
+                                        int64_t rhs)
 {
        if (fbc->count > rhs)
                return 1;
 {
        if (fbc->count > rhs)
                return 1;
@@ -123,13 +128,14 @@ static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
 }
 
 static inline int
 }
 
 static inline int
-__percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch)
+__percpu_counter_compare(struct percpu_counter *fbc, int64_t rhs,
+                        int32_t batch)
 {
        return percpu_counter_compare(fbc, rhs);
 }
 
 static inline void
 {
        return percpu_counter_compare(fbc, rhs);
 }
 
 static inline void
-percpu_counter_add(struct percpu_counter *fbc, s64 amount)
+percpu_counter_add(struct percpu_counter *fbc, int64_t amount)
 {
        preempt_disable();
        fbc->count += amount;
 {
        preempt_disable();
        fbc->count += amount;
@@ -137,12 +143,13 @@ percpu_counter_add(struct percpu_counter *fbc, s64 amount)
 }
 
 static inline void
 }
 
 static inline void
-percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
+percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
+                        int32_t batch)
 {
        percpu_counter_add(fbc, amount);
 }
 
 {
        percpu_counter_add(fbc, amount);
 }
 
-static inline s64 percpu_counter_read(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_read(struct percpu_counter *fbc)
 {
        return fbc->count;
 }
 {
        return fbc->count;
 }
@@ -151,17 +158,17 @@ static inline s64 percpu_counter_read(struct percpu_counter *fbc)
  * percpu_counter is intended to track positive numbers. In the UP case the
  * number should never be negative.
  */
  * percpu_counter is intended to track positive numbers. In the UP case the
  * number should never be negative.
  */
-static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_read_positive(struct percpu_counter *fbc)
 {
        return fbc->count;
 }
 
 {
        return fbc->count;
 }
 
-static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_sum_positive(struct percpu_counter *fbc)
 {
        return percpu_counter_read_positive(fbc);
 }
 
 {
        return percpu_counter_read_positive(fbc);
 }
 
-static inline s64 percpu_counter_sum(struct percpu_counter *fbc)
+static inline int64_t percpu_counter_sum(struct percpu_counter *fbc)
 {
        return percpu_counter_read(fbc);
 }
 {
        return percpu_counter_read(fbc);
 }
@@ -183,7 +190,8 @@ static inline void percpu_counter_dec(struct percpu_counter *fbc)
        percpu_counter_add(fbc, -1);
 }
 
        percpu_counter_add(fbc, -1);
 }
 
-static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount)
+static inline void percpu_counter_sub(struct percpu_counter *fbc,
+                                     int64_t amount)
 {
        percpu_counter_add(fbc, -amount);
 }
 {
        percpu_counter_add(fbc, -amount);
 }
index 3bf4a99..c15f7c8 100644 (file)
@@ -57,18 +57,18 @@ static inline void debug_percpu_counter_deactivate(struct percpu_counter *fbc)
 { }
 #endif /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
 
 { }
 #endif /* CONFIG_DEBUG_OBJECTS_PERCPU_COUNTER */
 
-void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
+void percpu_counter_set(struct percpu_counter *fbc, int64_t amount)
 {
        int cpu;
        unsigned long flags;
 
 {
        int cpu;
        unsigned long flags;
 
-       raw_spin_lock_irqsave(&fbc->lock, flags);
+       spin_lock_irqsave(&fbc->lock);
        for_each_possible_cpu(cpu) {
        for_each_possible_cpu(cpu) {
-               s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
+               int32_t *pcount = per_cpu_ptr(fbc->counters, cpu);
                *pcount = 0;
        }
        fbc->count = amount;
                *pcount = 0;
        }
        fbc->count = amount;
-       raw_spin_unlock_irqrestore(&fbc->lock, flags);
+       spin_unlock_irqsave(&fbc->lock);
 }
 EXPORT_SYMBOL(percpu_counter_set);
 
 }
 EXPORT_SYMBOL(percpu_counter_set);
 
@@ -79,18 +79,19 @@ EXPORT_SYMBOL(percpu_counter_set);
  * this_cpu_add which is irq-safe by definition. Hence there is no need muck
  * with irq state before calling this one
  */
  * this_cpu_add which is irq-safe by definition. Hence there is no need muck
  * with irq state before calling this one
  */
-void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch)
+void percpu_counter_add_batch(struct percpu_counter *fbc, int64_t amount,
+                             int32_t batch)
 {
 {
-       s64 count;
+       int64_t count;
 
        preempt_disable();
        count = __this_cpu_read(*fbc->counters) + amount;
        if (count >= batch || count <= -batch) {
                unsigned long flags;
 
        preempt_disable();
        count = __this_cpu_read(*fbc->counters) + amount;
        if (count >= batch || count <= -batch) {
                unsigned long flags;
-               raw_spin_lock_irqsave(&fbc->lock, flags);
+               spin_lock_irqsave(&fbc->lock);
                fbc->count += count;
                __this_cpu_sub(*fbc->counters, count - amount);
                fbc->count += count;
                __this_cpu_sub(*fbc->counters, count - amount);
-               raw_spin_unlock_irqrestore(&fbc->lock, flags);
+               spin_unlock_irqsave(&fbc->lock);
        } else {
                this_cpu_add(*fbc->counters, amount);
        }
        } else {
                this_cpu_add(*fbc->counters, amount);
        }
@@ -102,32 +103,32 @@ EXPORT_SYMBOL(percpu_counter_add_batch);
  * Add up all the per-cpu counts, return the result.  This is a more accurate
  * but much slower version of percpu_counter_read_positive()
  */
  * Add up all the per-cpu counts, return the result.  This is a more accurate
  * but much slower version of percpu_counter_read_positive()
  */
-s64 __percpu_counter_sum(struct percpu_counter *fbc)
+int64_t __percpu_counter_sum(struct percpu_counter *fbc)
 {
 {
-       s64 ret;
+       int64_t ret;
        int cpu;
        unsigned long flags;
 
        int cpu;
        unsigned long flags;
 
-       raw_spin_lock_irqsave(&fbc->lock, flags);
+       spin_lock_irqsave(&fbc->lock);
        ret = fbc->count;
        for_each_online_cpu(cpu) {
        ret = fbc->count;
        for_each_online_cpu(cpu) {
-               s32 *pcount = per_cpu_ptr(fbc->counters, cpu);
+               int32_t *pcount = per_cpu_ptr(fbc->counters, cpu);
                ret += *pcount;
        }
                ret += *pcount;
        }
-       raw_spin_unlock_irqrestore(&fbc->lock, flags);
+       spin_unlock_irqsave(&fbc->lock);
        return ret;
 }
 EXPORT_SYMBOL(__percpu_counter_sum);
 
        return ret;
 }
 EXPORT_SYMBOL(__percpu_counter_sum);
 
-int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
+int __percpu_counter_init(struct percpu_counter *fbc, int64_t amount, gfp_t gfp,
                          struct lock_class_key *key)
 {
        unsigned long flags __maybe_unused;
 
                          struct lock_class_key *key)
 {
        unsigned long flags __maybe_unused;
 
-       raw_spin_lock_init(&fbc->lock);
+       spinlock_init_irqsave(&fbc->lock);
        lockdep_set_class(&fbc->lock, key);
        fbc->count = amount;
        lockdep_set_class(&fbc->lock, key);
        fbc->count = amount;
-       fbc->counters = alloc_percpu_gfp(s32, gfp);
+       fbc->counters = alloc_percpu_gfp(int32_t, gfp);
        if (!fbc->counters)
                return -ENOMEM;
 
        if (!fbc->counters)
                return -ENOMEM;
 
@@ -135,9 +136,9 @@ int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp,
 
 #ifdef CONFIG_HOTPLUG_CPU
        INIT_LIST_HEAD(&fbc->list);
 
 #ifdef CONFIG_HOTPLUG_CPU
        INIT_LIST_HEAD(&fbc->list);
-       spin_lock_irqsave(&percpu_counters_lock, flags);
+       spin_lock_irqsave(&percpu_counters_lock);
        list_add(&fbc->list, &percpu_counters);
        list_add(&fbc->list, &percpu_counters);
-       spin_unlock_irqrestore(&percpu_counters_lock, flags);
+       spin_unlock_irqsave(&percpu_counters_lock);
 #endif
        return 0;
 }
 #endif
        return 0;
 }
@@ -153,9 +154,9 @@ void percpu_counter_destroy(struct percpu_counter *fbc)
        debug_percpu_counter_deactivate(fbc);
 
 #ifdef CONFIG_HOTPLUG_CPU
        debug_percpu_counter_deactivate(fbc);
 
 #ifdef CONFIG_HOTPLUG_CPU
-       spin_lock_irqsave(&percpu_counters_lock, flags);
+       spin_lock_irqsave(&percpu_counters_lock);
        list_del(&fbc->list);
        list_del(&fbc->list);
-       spin_unlock_irqrestore(&percpu_counters_lock, flags);
+       spin_unlock_irqsave(&percpu_counters_lock);
 #endif
        free_percpu(fbc->counters);
        fbc->counters = NULL;
 #endif
        free_percpu(fbc->counters);
        fbc->counters = NULL;
@@ -169,7 +170,7 @@ static int compute_batch_value(unsigned int cpu)
 {
        int nr = num_online_cpus();
 
 {
        int nr = num_online_cpus();
 
-       percpu_counter_batch = max(32, nr*2);
+       percpu_counter_batch = MAX(32, nr * 2);
        return 0;
 }
 
        return 0;
 }
 
@@ -180,17 +181,17 @@ static int percpu_counter_cpu_dead(unsigned int cpu)
 
        compute_batch_value(cpu);
 
 
        compute_batch_value(cpu);
 
-       spin_lock_irq(&percpu_counters_lock);
+       spin_lock_irqsave(&percpu_counters_lock);
        list_for_each_entry(fbc, &percpu_counters, list) {
        list_for_each_entry(fbc, &percpu_counters, list) {
-               s32 *pcount;
+               int32_t *pcount;
 
 
-               raw_spin_lock(&fbc->lock);
+               spin_lock_irqsave(&fbc->lock);
                pcount = per_cpu_ptr(fbc->counters, cpu);
                fbc->count += *pcount;
                *pcount = 0;
                pcount = per_cpu_ptr(fbc->counters, cpu);
                fbc->count += *pcount;
                *pcount = 0;
-               raw_spin_unlock(&fbc->lock);
+               spin_unlock_irqsave(&fbc->lock);
        }
        }
-       spin_unlock_irq(&percpu_counters_lock);
+       spin_unlock_irqsave(&percpu_counters_lock);
 #endif
        return 0;
 }
 #endif
        return 0;
 }
@@ -199,9 +200,10 @@ static int percpu_counter_cpu_dead(unsigned int cpu)
  * Compare counter against given value.
  * Return 1 if greater, 0 if equal and -1 if less
  */
  * 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, s32 batch)
+int __percpu_counter_compare(struct percpu_counter *fbc, int64_t rhs,
+                            int32_t batch)
 {
 {
-       s64     count;
+       int64_t count;
 
        count = percpu_counter_read(fbc);
        /* Check to see if rough count will be sufficient for comparison */
 
        count = percpu_counter_read(fbc);
        /* Check to see if rough count will be sufficient for comparison */
@@ -228,11 +230,11 @@ static int __init percpu_counter_startup(void)
 
        ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
                                compute_batch_value, NULL);
 
        ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "lib/percpu_cnt:online",
                                compute_batch_value, NULL);
-       WARN_ON(ret < 0);
+       warn_on(ret < 0);
        ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
                                        "lib/percpu_cnt:dead", NULL,
                                        percpu_counter_cpu_dead);
        ret = cpuhp_setup_state_nocalls(CPUHP_PERCPU_CNT_DEAD,
                                        "lib/percpu_cnt:dead", NULL,
                                        percpu_counter_cpu_dead);
-       WARN_ON(ret < 0);
+       warn_on(ret < 0);
        return 0;
 }
 module_init(percpu_counter_startup);
        return 0;
 }
 module_init(percpu_counter_startup);