Skip to contentSkip to conversation

percpu_counter: change inaccurate comment

PATCH In review Eric Dumazet 2 participants 6 messages Oct 7, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 percpu_counter: change inaccurate comment

percpu_counter used to be huge objects, they are not anymore, thanks to fine alloc_percpu() granularity.

We now consume 4 bytes per possible cpu.

---
 include/linux/percpu_counter.h |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/percpu_counter.h +2 -1
index 8a7d510..5e2cb5c 100644
@@ -3,7 +3,8 @@
 /*
  * A simple "approximate counter" for use in ext2 and ext3 superblocks.
  *
- * WARNING: these things are HUGE.  4 kbytes per counter on 32-way P4.
+ * WARNING: these things are big :  4 bytes per possible cpu per counter.
+ * For a 64 cpus machine: 256 bytes + sizeof(struct percpu_counter)
  */
 
 #include <linux/spinlock.h>
#1 lore raw
Quoted 4 lines · Eric Dumazet

percpu_counter used to be huge objects, they are not anymore, thanks to fine alloc_percpu() granularity.

We now consume 4 bytes per possible cpu.

Ahh I did not notice that they switched.... Then we may also want the following patch to improve performance. It would even be better if we could do a this_cpu_add(fbc->counters, amount) there but the 64 bit size fo count looks suspiciously like you are expecting overflows beyond ints max size.

Subject: percpu_counter: Use this_cpu_ptr instead of per_cpu_ptr

this_cpu_ptr avoids an array lookup and can use the percpu offset of the local cpu directly.

---
 lib/percpu_counter.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/percpu_counter.c +3 -3
Index: linux-2.6/lib/percpu_counter.c
===================================================================
@@ -30,9 +30,9 @@ void __percpu_counter_add(struct percpu_
 {
 	s64 count;
 	s32 *pcount;
-	int cpu = get_cpu();
 
-	pcount = per_cpu_ptr(fbc->counters, cpu);
+	preempt_disable();
+	pcount = this_cpu_ptr(fbc->counters);
 	count = *pcount + amount;
 	if (count >= batch || count <= -batch) {
 		spin_lock(&fbc->lock);
@@ -42,7 +42,7 @@ void __percpu_counter_add(struct percpu_
 	} else {
 		*pcount = count;
 	}
-	put_cpu();
+	preempt_enable();
 }
 EXPORT_SYMBOL(__percpu_counter_add);
 
#2 lore raw
Quoted 5 lines · Christoph Lameter
Quoted 3 lines · Eric Dumazet

percpu_counter used to be huge objects, they are not anymore, thanks to fine alloc_percpu() granularity.

Quoted 8 lines
Quoted 1 line

We now consume 4 bytes per possible cpu.

Ahh I did not notice that they switched.... Then we may also want the following patch to improve performance. It would even be better if we could do a this_cpu_add(fbc->counters, amount) there but the 64 bit size fo count looks suspiciously like you are expecting overflows beyond ints max size.

Yes, this 64bit interface is very strange.

Quoted 29 lines

Subject: percpu_counter: Use this_cpu_ptr instead of per_cpu_ptr

this_cpu_ptr avoids an array lookup and can use the percpu offset of the local cpu directly.

---
 lib/percpu_counter.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Index: linux-2.6/lib/percpu_counter.c
===================================================================

lib/percpu_counter.c +2 -2
@@ -30,9 +30,9 @@ void __percpu_counter_add(struct percpu_
 {
 	s64 count;
 	s32 *pcount;
-	int cpu = get_cpu();
 
-	pcount = per_cpu_ptr(fbc->counters, cpu);
+	preempt_disable();
+	pcount = this_cpu_ptr(fbc->counters);
 	count = *pcount + amount;
 	if (count >= batch || count <= -batch) {
 		spin_lock(&fbc->lock);

lib/percpu_counter.c void __percpu_counter_add(struct percpu_ · line 48

Quoted 8 lines
lib/percpu_counter.c +1 -1
@@ -42,7 +42,7 @@ void __percpu_counter_add(struct percpu_
 	} else {
 		*pcount = count;
 	}
-	put_cpu();
+	preempt_enable();
 }
 EXPORT_SYMBOL(__percpu_counter_add);
 

If enclosed by preempt_disable()/preempt_enable(), maybe we could use __this_cpu_ptr() ?

#3 lore raw
Quoted 2 lines · Eric Dumazet

If enclosed by preempt_disable()/preempt_enable(), maybe we could use __this_cpu_ptr() ?

The only difference between __this_cpu_ptr and this_cpu_ptr is that this_cpu_ptr checks that preempt was disabled. __this_cpu_ptr allows use even without preempt. Preempt must be disabled here so the use of this_cpu_ptr is appropriate.

#4 lore raw
Quoted 11 lines · Christoph Lameter
Quoted 2 lines · Eric Dumazet

If enclosed by preempt_disable()/preempt_enable(), maybe we could use __this_cpu_ptr() ?

The only difference between __this_cpu_ptr and this_cpu_ptr is that this_cpu_ptr checks that preempt was disabled. __this_cpu_ptr allows use even without preempt. Preempt must be disabled here so the use of this_cpu_ptr is appropriate.

Thats not how I read the thing.

In both variants, preemption _must_ be disabled, its only the context that can tell how sure we are...

<quote>

commit 7340a0b15280c

__this_cpu_ptr -> Do not check for preemption context
this_cpu_ptr -> Check preemption context

</quote>

If preemption was enabled, both pointers would not be very useful...

We use __this_cpu_ptr() in contexts where cpu _cannot_ change under us, (we just disabled preemption one line above), so its not necessary to perform the check.

vi +316 include/linux/percpu.h

#define _this_cpu_generic_to_op(pcp, val, op)                   \
do {                                                            \
        preempt_disable();                                      \
        *__this_cpu_ptr(&(pcp)) op val;                         \
        preempt_enable();                                       \

} while (0)

...

#define __this_cpu_generic_to_op(pcp, val, op)                  \
do {                                                            \
        *__this_cpu_ptr(&(pcp)) op val;                         \

} while (0)

#5 lore raw
Quoted 8 lines · Eric Dumazet
Quoted 7 lines

The only difference between __this_cpu_ptr and this_cpu_ptr is that this_cpu_ptr checks that preempt was disabled. __this_cpu_ptr allows use even without preempt. Preempt must be disabled here so the use of this_cpu_ptr is appropriate.

Quoted 4 lines

Thats not how I read the thing.

In both variants, preemption _must_ be disabled, its only the context that can tell how sure we are...

Correct. One checks the other one does not check.

Quoted 7 lines

<quote>

commit 7340a0b15280c

__this_cpu_ptr -> Do not check for preemption context
this_cpu_ptr -> Check preemption context

Quoted 7 lines

</quote>

If preemption was enabled, both pointers would not be very useful...

We use __this_cpu_ptr() in contexts where cpu _cannot_ change under us, (we just disabled preemption one line above), so its not necessary to perform the check.

Someone could screw up the code in the future. Thats what its there for.