Re: [cpuops cmpxchg V2 5/5] cpuops: Use cmpxchg for xchg to avoidlock semantics

From: Christoph Lameter
Date: Tue Dec 14 2010 - 11:56:06 EST


On Tue, 14 Dec 2010, Eric Dumazet wrote:

> You should use the fact that the failed cmpxchg loads in al/ax/eax/rax
> the current value, so :
>
> "\n\tmov "__percpu_arg(1)",%%al"
> "\n1:\tcmpxchgb %2, "__percpu_arg(1)
> "\n\tjnz 1b"
>
> (No need to reload the value again)

Subject: cpuops xchg: Exploit the fact that failed cmpxchges return old value

We do not need to reload the value as pointed out by Eric. It is already in
the correct register so just rerun the cmpxchg without the load.

Signed-off-by: Christoph Lameter <cl@xxxxxxxxx>

---
arch/x86/include/asm/percpu.h | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)

Index: linux-2.6/arch/x86/include/asm/percpu.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/percpu.h 2010-12-14 10:51:57.000000000 -0600
+++ linux-2.6/arch/x86/include/asm/percpu.h 2010-12-14 10:54:46.000000000 -0600
@@ -223,32 +223,32 @@ do { \
typeof(var) __new = (nval); \
switch (sizeof(var)) { \
case 1: \
- asm("\n1:mov "__percpu_arg(1)",%%al" \
- "\n\tcmpxchgb %2, "__percpu_arg(1) \
+ asm("\n\tmov "__percpu_arg(1)",%%al" \
+ "\n1:cmpxchgb %2, "__percpu_arg(1) \
"\n\tjnz 1b" \
: "=a" (__ret), "+m" (var) \
: "q" (__new) \
: "memory"); \
break; \
case 2: \
- asm("\n1:mov "__percpu_arg(1)",%%ax" \
- "\n\tcmpxchgw %2, "__percpu_arg(1) \
+ asm("\n\tmov "__percpu_arg(1)",%%ax" \
+ "\n1:cmpxchgw %2, "__percpu_arg(1) \
"\n\tjnz 1b" \
: "=a" (__ret), "+m" (var) \
: "r" (__new) \
: "memory"); \
break; \
case 4: \
- asm("\n1:mov "__percpu_arg(1)",%%eax" \
- "\n\tcmpxchgl %2, "__percpu_arg(1) \
+ asm("\n\tmov "__percpu_arg(1)",%%eax" \
+ "\n1:cmpxchgl %2, "__percpu_arg(1) \
"\n\tjnz 1b" \
: "=a" (__ret), "+m" (var) \
: "r" (__new) \
: "memory"); \
break; \
case 8: \
- asm("\n1:mov "__percpu_arg(1)",%%rax" \
- "\n\tcmpxchgq %2, "__percpu_arg(1) \
+ asm("\n\tmov "__percpu_arg(1)",%%rax" \
+ "\n1:cmpxchgq %2, "__percpu_arg(1) \
"\n\tjnz 1b" \
: "=a" (__ret), "+m" (var) \
: "r" (__new) \
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/