lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20130911131323.GQ31370@twins.programming.kicks-ass.net>
Date:	Wed, 11 Sep 2013 15:13:23 +0200
From:	Peter Zijlstra <peterz@...radead.org>
To:	Linus Torvalds <torvalds@...ux-foundation.org>
Cc:	Ingo Molnar <mingo@...nel.org>, Andi Kleen <ak@...ux.intel.com>,
	Peter Anvin <hpa@...or.com>,
	Mike Galbraith <bitbucket@...ine.de>,
	Thomas Gleixner <tglx@...utronix.de>,
	Arjan van de Ven <arjan@...ux.intel.com>,
	Frederic Weisbecker <fweisbec@...il.com>,
	Linux Kernel Mailing List <linux-kernel@...r.kernel.org>,
	"linux-arch@...r.kernel.org" <linux-arch@...r.kernel.org>
Subject: Re: [PATCH 0/7] preempt_count rework -v2

On Tue, Sep 10, 2013 at 02:43:06PM -0700, Linus Torvalds wrote:
> That said, looking at your patch, I get the *very* strong feeling that
> we could make a macro that does all the repetitions for us, and then
> have a
> 
>   GENERATE_RMW(atomic_sub_and_test, LOCK_PREFIX "subl", "e", "")

The below seems to compile..

---
 arch/x86/include/asm/atomic.h      |   29 +--------
 arch/x86/include/asm/atomic64_64.h |   28 +--------
 arch/x86/include/asm/local.h       |   28 +--------
 arch/x86/include/asm/addcc.h       |  115 +++++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 72 deletions(-)

Index: linux-2.6/arch/x86/include/asm/atomic.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/atomic.h
+++ linux-2.6/arch/x86/include/asm/atomic.h
@@ -6,6 +6,7 @@
 #include <asm/processor.h>
 #include <asm/alternative.h>
 #include <asm/cmpxchg.h>
+#include <asm/addcc.h>
 
 /*
  * Atomic operations that C can't guarantee us.  Useful for
@@ -76,12 +77,7 @@ static inline void atomic_sub(int i, ato
  */
 static inline int atomic_sub_and_test(int i, atomic_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "subl %2,%0; sete %1"
-		     : "+m" (v->counter), "=qm" (c)
-		     : "ir" (i) : "memory");
-	return c;
+	GENERATE_ADDcc(v->counter, -i, LOCK_PREFIX, "e");
 }
 
 /**
@@ -118,12 +114,7 @@ static inline void atomic_dec(atomic_t *
  */
 static inline int atomic_dec_and_test(atomic_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "decl %0; sete %1"
-		     : "+m" (v->counter), "=qm" (c)
-		     : : "memory");
-	return c != 0;
+	GENERATE_ADDcc(v->counter, -1, LOCK_PREFIX, "e");
 }
 
 /**
@@ -136,12 +127,7 @@ static inline int atomic_dec_and_test(at
  */
 static inline int atomic_inc_and_test(atomic_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "incl %0; sete %1"
-		     : "+m" (v->counter), "=qm" (c)
-		     : : "memory");
-	return c != 0;
+	GENERATE_ADDcc(v->counter, 1, LOCK_PREFIX, "e");
 }
 
 /**
@@ -155,12 +141,7 @@ static inline int atomic_inc_and_test(at
  */
 static inline int atomic_add_negative(int i, atomic_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "addl %2,%0; sets %1"
-		     : "+m" (v->counter), "=qm" (c)
-		     : "ir" (i) : "memory");
-	return c;
+	GENERATE_ADDcc(v->counter, i, LOCK_PREFIX, "s");
 }
 
 /**
Index: linux-2.6/arch/x86/include/asm/atomic64_64.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/atomic64_64.h
+++ linux-2.6/arch/x86/include/asm/atomic64_64.h
@@ -72,12 +72,7 @@ static inline void atomic64_sub(long i,
  */
 static inline int atomic64_sub_and_test(long i, atomic64_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "subq %2,%0; sete %1"
-		     : "=m" (v->counter), "=qm" (c)
-		     : "er" (i), "m" (v->counter) : "memory");
-	return c;
+	GENERATE_ADDcc(v->counter, -i, LOCK_PREFIX, "e");
 }
 
 /**
@@ -116,12 +111,7 @@ static inline void atomic64_dec(atomic64
  */
 static inline int atomic64_dec_and_test(atomic64_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "decq %0; sete %1"
-		     : "=m" (v->counter), "=qm" (c)
-		     : "m" (v->counter) : "memory");
-	return c != 0;
+	GENERATE_ADDcc(v->counter, -1, LOCK_PREFIX, "e");
 }
 
 /**
@@ -134,12 +124,7 @@ static inline int atomic64_dec_and_test(
  */
 static inline int atomic64_inc_and_test(atomic64_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "incq %0; sete %1"
-		     : "=m" (v->counter), "=qm" (c)
-		     : "m" (v->counter) : "memory");
-	return c != 0;
+	GENERATE_ADDcc(v->counter, 1, LOCK_PREFIX, "e");
 }
 
 /**
@@ -153,12 +138,7 @@ static inline int atomic64_inc_and_test(
  */
 static inline int atomic64_add_negative(long i, atomic64_t *v)
 {
-	unsigned char c;
-
-	asm volatile(LOCK_PREFIX "addq %2,%0; sets %1"
-		     : "=m" (v->counter), "=qm" (c)
-		     : "er" (i), "m" (v->counter) : "memory");
-	return c;
+	GENERATE_ADDcc(v->counter, i, LOCK_PREFIX, "s");
 }
 
 /**
Index: linux-2.6/arch/x86/include/asm/local.h
===================================================================
--- linux-2.6.orig/arch/x86/include/asm/local.h
+++ linux-2.6/arch/x86/include/asm/local.h
@@ -52,12 +52,7 @@ static inline void local_sub(long i, loc
  */
 static inline int local_sub_and_test(long i, local_t *l)
 {
-	unsigned char c;
-
-	asm volatile(_ASM_SUB "%2,%0; sete %1"
-		     : "+m" (l->a.counter), "=qm" (c)
-		     : "ir" (i) : "memory");
-	return c;
+	GENERATE_ADDcc(l->a.counter, -i, "", "e");
 }
 
 /**
@@ -70,12 +65,7 @@ static inline int local_sub_and_test(lon
  */
 static inline int local_dec_and_test(local_t *l)
 {
-	unsigned char c;
-
-	asm volatile(_ASM_DEC "%0; sete %1"
-		     : "+m" (l->a.counter), "=qm" (c)
-		     : : "memory");
-	return c != 0;
+	GENERATE_ADDcc(l->a.counter, -1, "", "e");
 }
 
 /**
@@ -88,12 +78,7 @@ static inline int local_dec_and_test(loc
  */
 static inline int local_inc_and_test(local_t *l)
 {
-	unsigned char c;
-
-	asm volatile(_ASM_INC "%0; sete %1"
-		     : "+m" (l->a.counter), "=qm" (c)
-		     : : "memory");
-	return c != 0;
+	GENERATE_ADDcc(l->a.counter, 1, "", "e");
 }
 
 /**
@@ -107,12 +92,7 @@ static inline int local_inc_and_test(loc
  */
 static inline int local_add_negative(long i, local_t *l)
 {
-	unsigned char c;
-
-	asm volatile(_ASM_ADD "%2,%0; sets %1"
-		     : "+m" (l->a.counter), "=qm" (c)
-		     : "ir" (i) : "memory");
-	return c;
+	GENERATE_ADDcc(l->a.counter, i, "", "s");
 }
 
 /**
Index: linux-2.6/arch/x86/include/asm/addcc.h
===================================================================
--- /dev/null
+++ linux-2.6/arch/x86/include/asm/addcc.h
@@ -0,0 +1,115 @@
+#ifndef _ASM_X86_ADDcc
+#define _ASM_X86_ADDcc
+
+extern void __bad_addcc_size(void);
+
+#ifdef CC_HAVE_ASM_GOTO
+
+#define GENERATE_ADDcc(var, val, lock, cc)				\
+do {									\
+	const int add_ID__ = (__builtin_constant_p(val) &&		\
+			((val) == 1 || (val) == -1)) ? (val) : 0;	\
+									\
+	switch (sizeof(var)) {						\
+	case 4:								\
+		if (add_ID__ == 1) {					\
+			asm volatile goto(lock "incl %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var)			\
+					  : "memory" : cc_label);	\
+		} else if (add_ID__ == -1) {				\
+			asm volatile goto(lock "decl %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var)			\
+					  : "memory" : cc_label);	\
+		} else {						\
+			asm volatile goto(lock "addl %1, %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var), "er" (val)	\
+					  : "memory" : cc_label);	\
+		}							\
+		break;							\
+									\
+	case 8:								\
+		if (add_ID__ == 1) {					\
+			asm volatile goto(lock "incq %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var)			\
+					  : "memory" : cc_label);	\
+		} else if (add_ID__ == -1) {				\
+			asm volatile goto(lock "decq %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var)			\
+					  : "memory" : cc_label);	\
+		} else {						\
+			asm volatile goto(lock "addq %1, %0;"		\
+					  "j" cc " %l[cc_label]"	\
+					  : : "m" (var), "er" (val)	\
+					  : "memory" : cc_label);	\
+		}							\
+		break;							\
+									\
+	default: __bad_addcc_size();					\
+	}								\
+									\
+	return 0;							\
+cc_label:								\
+	return 1;							\
+} while (0)
+
+#else /* !CC_HAVE_ASM_GOTO */
+
+#define GENERATE_ADDcc(var, val, lock, cc)				\
+do {									\
+	const int add_ID__ = (__builtin_constant_p(val) &&		\
+			((val) == 1 || (val) == -1)) ? (val) : 0;	\
+	char c;								\
+									\
+	switch (sizeof(var)) {						\
+	case 4:								\
+		if (add_ID__ == 1) {					\
+			asm volatile (lock "incl %0;"			\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : : "memory");		\
+		} else if (add_ID__ == -1) {				\
+			asm volatile (lock "decl %0;"			\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : : "memory");		\
+		} else {						\
+			asm volatile (lock "addl %2, %0;"		\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : "er" (val) : "memory");	\
+		}							\
+		break;							\
+									\
+	case 8:								\
+		if (add_ID__ == 1) {					\
+			asm volatile (lock "incq %0;"			\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : : "memory");		\
+		} else if (add_ID__ == -1) {				\
+			asm volatile (lock "decq %0;"			\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : : "memory");		\
+		} else {						\
+			asm volatile (lock "addq %2, %0;"		\
+					  "set" cc " %1"		\
+					  : "+m" (var), "=qm" (c)	\
+					  : "er" (val) : "memory");	\
+		}							\
+		break;							\
+									\
+	default: __bad_addcc_size();					\
+	}								\
+									\
+	return c != 0;							\
+} while (0)
+
+#endif /* CC_HAVE_ASM_GOTO */
+
+#endif /* _ASM_X86_ADDcc */
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ