Skip to contentSkip to conversation

percpu_counter: bug fix and enhancement

PATCH In review Shaohua Li 2 participants 6 messages May 17, 2011

Subsystems (2)
lore
Share & tools
lore

Shaohua Li

author
#0 lore raw

v3 cover percpu_counter: bug fix and enhancement

The patch sets do two things.
1. fix bug for 32-bit system. percpu_counter uses s64 counter. Without any locking reading s64 in 32-bit system isn't safe and can cause bad side effect.
2. improve scalability for __percpu_counter_add. In some cases, _add could cause heavy lock contention (see patch 2 for detailed infomation and data). The patches will remove the contention and speed up it a bit. Last post
(http://marc.info/?l=linux-kernel&m=130259547913607&w=2) simpliy uses atomic64 for percpu_counter, but Tejun pointed out this could cause deviation in __percpu_counter_sum.

In this impelmentation, we track _sum and _add state. When _sum starts, _add will wait _sum to finish. This sounds scaring, since _add is fast path. But since _sum is called rare, at most time _add doesn't need wait.

patch 1 fix s64 read bug for 32-bit system for UP
patch 2,3 fix s64 read bug for 32-bit system for MP. And it also improve the scalability for __percpu_counter_add.

I did some benchmarks with the patches applied:
Test1:
24 CPUs do:
while {
mmap(32M);
munmap(32M);
}
Each CPU is about 7x faster to do the loop.

Test2:
One CPU does:
while {
__percpu_counter_add(+count)
__percpu_counter_add(-count)
}
the loop do 10000000 times.
in _add fast path (no locking hold):
before my patch:

real    0m0.133s
user    0m0.000s
sys     0m0.124s
after:
real    0m0.129s
user    0m0.000s
sys     0m0.120s

the difference is variation

in _add slow path (locking hold):
before my patch:

real    0m0.374s
user    0m0.000s
sys     0m0.372s
after:
real    0m0.245s
user    0m0.000s
sys     0m0.020s

Test3:
One CPU runs percpu_counter_sum, 23 CPUs run percpu_counter_add. In _add fast path (don't hold) lock, _sum runs a little slow (about 20% slower). In _add slow path (hold lock), _sum runs much faster (about 9x faster)

So overall my patches make percpu_counter API faster. The only exception is _sum has a little slower in one case, but _sum is called rare, the 20% slower doesn't matter.

Thanks,
Shaohua

author
#1 lore raw

PATCH v3 1/3 percpu_counter: fix code for 32bit systems for UP

percpu_counter.counter is a 's64'. Accessing it in 32-bit system is racing. we need some locking to protect it otherwise some very wrong value could be accessed.

---
 include/linux/percpu_counter.h |   31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
include/linux/percpu_counter.h +23 -8
Index: linux/include/linux/percpu_counter.h
===================================================================
@@ -101,14 +101,34 @@ static inline void percpu_counter_destro
 
 static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount)
 {
+#if BITS_PER_LONG == 32
+	preempt_disable();
+	fbc->count = amount;
+	preempt_enable();
+#else
 	fbc->count = amount;
+#endif
+}
+
+static inline s64 percpu_counter_read(struct percpu_counter *fbc)
+{
+#if BITS_PER_LONG == 32
+	s64 count;
+	preempt_disable();
+	count = fbc->count;
+	preempt_enable();
+	return count;
+#else
+	return fbc->count;
+#endif
 }
 
 static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs)
 {
-	if (fbc->count > rhs)
+	s64 count = percpu_counter_read(fbc);
+	if (count > rhs)
 		return 1;
-	else if (fbc->count < rhs)
+	else if (count < rhs)
 		return -1;
 	else
 		return 0;
@@ -128,18 +148,13 @@ __percpu_counter_add(struct percpu_count
 	percpu_counter_add(fbc, amount);
 }
 
-static inline s64 percpu_counter_read(struct percpu_counter *fbc)
-{
-	return fbc->count;
-}
-
 /*
  * percpu_counter is intended to track positive number. In UP case, the number
  * should never be negative.
  */
 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
 {
-	return fbc->count;
+	return percpu_counter_read(fbc);
 }
 
 static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc)
author
#2 lore raw

PATCH v3 2/3 percpu_counter: use atomic64 for counter in SMP

In reply to Shaohua Li

Uses atomic64 for percpu_counter, because it is cheaper than spinlock. This doesn't slow fast path (percpu_counter_read). atomic64_read equals to read fbc->count for 64-bit system, or equals to spin_lock-read-spin_unlock for 32-bit system. Note, originally the percpu_counter_read for 32-bit system doesn't hold spin_lock, but that is buggy and might cause very wrong value accessed. This patch fixes the issue.

We use sum_start and add_start to make sure _sum doesn't see deviation when _add slow path is running. When _sum is running, _add will wait _sum finish. This is scaring that _add is slow down, but actually not, because _sum is called very rare. We could make _sum waits _add finish, but since _add is called frequently, this will make _sum run very slow.

This can also improve some workloads with percpu_counter->lock heavily contented. For example, vm_committed_as sometimes causes the contention. We should tune the batch count, but if we can make percpu_counter better, why not? In a 24 CPUs system and 24 processes, each runs:

while (1) {
	mmap(128M);
	munmap(128M);
}

we then measure how many loops each process can take:
The atomic method gives 4x faster.

---
 include/linux/percpu_counter.h |   19 ++++-------------
 lib/percpu_counter.c           |   45 +++++++++++++++++++++++------------------
 2 files changed, 31 insertions(+), 33 deletions(-)
include/linux/percpu_counter.h +5 -14
Index: linux/include/linux/percpu_counter.h
===================================================================
@@ -16,8 +16,8 @@
 #ifdef CONFIG_SMP
 
 struct percpu_counter {
-	spinlock_t lock;
-	s64 count;
+	atomic_t sum_start, add_start;
+	atomic64_t count;
 #ifdef CONFIG_HOTPLUG_CPU
 	struct list_head list;	/* All percpu_counters are on a list */
 #endif
@@ -26,16 +26,7 @@ struct percpu_counter {
 
 extern int percpu_counter_batch;
 
-int __percpu_counter_init(struct percpu_counter *fbc, s64 amount,
-			  struct lock_class_key *key);
-
-#define percpu_counter_init(fbc, value)					\
-	({								\
-		static struct lock_class_key __key;			\
-									\
-		__percpu_counter_init(fbc, value, &__key);		\
-	})
-
+int percpu_counter_init(struct percpu_counter *fbc, s64 amount);
 void percpu_counter_destroy(struct percpu_counter *fbc);
 void percpu_counter_set(struct percpu_counter *fbc, s64 amount);
 void __percpu_counter_add(struct percpu_counter *fbc, s64 amount, s32 batch);
@@ -60,7 +51,7 @@ static inline s64 percpu_counter_sum(str
 
 static inline s64 percpu_counter_read(struct percpu_counter *fbc)
 {
-	return fbc->count;
+	return atomic64_read(&fbc->count);
 }
 
 /*
@@ -70,7 +61,7 @@ static inline s64 percpu_counter_read(st
  */
 static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc)
 {
-	s64 ret = fbc->count;
+	s64 ret = percpu_counter_read(fbc);
 
 	barrier();		/* Prevent reloads of fbc->count */
 	if (ret >= 0)
lib/percpu_counter.c +26 -19

92 lines not on this page · raw message

#3 lore raw

Re: [patch v3 2/3] percpu_counter: use atomic64 for counter in SMP

Quoted 23 lines · Shaohua Li

pièce jointe document texte brut (percpu-counter-atomic.patch) Uses atomic64 for percpu_counter, because it is cheaper than spinlock. This doesn't slow fast path (percpu_counter_read). atomic64_read equals to read fbc->count for 64-bit system, or equals to spin_lock-read-spin_unlock for 32-bit system. Note, originally the percpu_counter_read for 32-bit system doesn't hold spin_lock, but that is buggy and might cause very wrong value accessed. This patch fixes the issue.

We use sum_start and add_start to make sure _sum doesn't see deviation when _add slow path is running. When _sum is running, _add will wait _sum finish. This is scaring that _add is slow down, but actually not, because _sum is called very rare. We could make _sum waits _add finish, but since _add is called frequently, this will make _sum run very slow.

This can also improve some workloads with percpu_counter->lock heavily contented. For example, vm_committed_as sometimes causes the contention. We should tune the batch count, but if we can make percpu_counter better, why not? In a 24 CPUs system and 24 processes, each runs:

while (1) {
	mmap(128M);
	munmap(128M);
}
Quoted 5 lines

we then measure how many loops each process can take:
The atomic method gives 4x faster.

--

I NACK this patch, its not necessary, since percpu_counter doesnt provide a precise count api anyway.

Please resubmit your original patches, without the bloat.

Thanks

author
#4 lore raw

PATCH v3 3/3 percpu_counter: use percpu data to track add start

In reply to Shaohua Li

add_start causes a lot of cache bouncing because it's updated by all cpus. We can actually make it a percpu variable. This will completely reduce the cache bouncing.
With the patch and last patch, I get about 7x faster running the workload that last patch described. Only with last patch, the workload is only about 4x faster.
This doesn't slow down _sum because we removed lock for _sum. I did a stress test. 23 CPU run _add, one cpu runs _sum. In _add fast path (don't hold) lock, _sum runs a little slow (about 20% slower). In _add slow path (hold lock), _sum runs much faster (about 9x faster);

V2: uses one percpu data as Eric suggested.

---
 include/linux/percpu_counter.h |   11 ++++++--
 lib/percpu_counter.c           |   51 ++++++++++++++++++++---------------------
 2 files changed, 33 insertions(+), 29 deletions(-)
include/linux/percpu_counter.h +8 -3
Index: linux/include/linux/percpu_counter.h
===================================================================
@@ -15,13 +15,18 @@
 
 #ifdef CONFIG_SMP
 
+struct percpu_counter_data {
+	s32 counter;
+	char add_start;
+};
+
 struct percpu_counter {
-	atomic_t sum_start, add_start;
+	atomic_t sum_start;
 	atomic64_t count;
 #ifdef CONFIG_HOTPLUG_CPU
 	struct list_head list;	/* All percpu_counters are on a list */
 #endif
-	s32 __percpu *counters;
+	struct percpu_counter_data __percpu *cpu_data;
 };
 
 extern int percpu_counter_batch;
@@ -71,7 +76,7 @@ static inline s64 percpu_counter_read_po
 
 static inline int percpu_counter_initialized(struct percpu_counter *fbc)
 {
-	return (fbc->counters != NULL);
+	return (fbc->cpu_data != NULL);
 }
 
 #else
lib/percpu_counter.c +25 -26

108 lines not on this page · raw message

#5 lore raw

Re: [patch v3 3/3] percpu_counter: use percpu data to track add start

Quoted 12 lines · Shaohua Li

pièce jointe document texte brut (percpu-counter-percpustart.patch) add_start causes a lot of cache bouncing because it's updated by all cpus. We can actually make it a percpu variable. This will completely reduce the cache bouncing.
With the patch and last patch, I get about 7x faster running the workload that last patch described. Only with last patch, the workload is only about 4x faster.
This doesn't slow down _sum because we removed lock for _sum. I did a stress test. 23 CPU run _add, one cpu runs _sum. In _add fast path (don't hold) lock, _sum runs a little slow (about 20% slower). In _add slow path (hold lock), _sum runs much faster (about 9x faster);

Quoted 1 line

V2: uses one percpu data as Eric suggested.

I NACK this patch very much.