From: "Jan Beulich" Subject: [PATCH] x86: improve scalability of rwlock implementation Date: Fri, 25 Mar 2011 17:01:31 +0000 Message-ID: <4D8CD87B02000078000386FB@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part1834D47B.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part1834D47B.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline >From an abstract perspective, it should be possible to replace normal spin locks with the write version of rwlocks without correctness issues. The implementation so far had a very small probability of not scaling beyond 128 CPUs (which would become more significant if aforementioned replacement was done), since the sign bit of a 32-bit integer is being used to determine various conditions, and 129 or more CPUs simultaneously subtracting RW_LOCK_BIAS from a single lock would cause an undetected overflow. Therefore, the bias gets adjusted so that it can cover up to 2048 CPUs, and the lock structure gets widened to account for the unlikely case of getting built for and run on a system with even more CPUs. While touching this code, I also used the same .subsection approach that was already used elsewhere to eliminate taken branches from the fast paths. And the output constraints of the affected asm()-s got fixed to use "+" rather than "=3D". Signed-off-by: Jan Beulich --- a/xen/include/asm-x86/spinlock.h +++ b/xen/include/asm-x86/spinlock.h @@ -31,39 +31,67 @@ static always_inline int _raw_spin_trylo return (oldval > 0); } =20 -typedef struct { +#if NR_CPUS <=3D 2048 + +typedef union { volatile int lock; + volatile int write; +} raw_rwlock_t; + +#define RW_LOCK_BIAS 0x00100000 +#define READ_LOCK_SUFFIX "l" +#define WRITE_LOCK_ADD(n) "addl %" #n "," +#define WRITE_LOCK_SUB(n) "subl %" #n "," +#define WRITE_LOCK_CMP(n) "%" #n + +#else + +typedef union { + volatile s64 lock; + struct { + volatile u32 read; + volatile s32 write; + }; } raw_rwlock_t; =20 -#define RW_LOCK_BIAS 0x01000000 +#define RW_LOCK_BIAS (1L << 32) +#define READ_LOCK_SUFFIX "q" +#define WRITE_LOCK_ADD(n) "incl" +#define WRITE_LOCK_SUB(n) "decl" +#define WRITE_LOCK_CMP(n) "$1" + +#endif + #define _RAW_RW_LOCK_UNLOCKED /*(raw_rwlock_t)*/ { RW_LOCK_BIAS } =20 static always_inline void _raw_read_lock(raw_rwlock_t *rw) { asm volatile ( - "1: lock; decl %0 \n" - " jns 3f \n" - " lock; incl %0 \n" - "2: rep; nop \n" - " cmpl $1,%0 \n" - " js 2b \n" - " jmp 1b \n" - "3:" - : "=3Dm" (rw->lock) : : "memory" ); + "1: lock; dec" READ_LOCK_SUFFIX " %0\n" + " js 3f \n" + " .subsection 1 \n" + "3: lock; inc" READ_LOCK_SUFFIX " %0\n" + "2: rep; nop \n" + " cmp" READ_LOCK_SUFFIX " $1,%0 \n" + " js 2b \n" + " jmp 1b \n" + " .subsection 0 \n" + : "+m" (rw->lock) : : "memory" ); } =20 static always_inline void _raw_write_lock(raw_rwlock_t *rw) { asm volatile ( - "1: lock; subl %1,%0 \n" - " jz 3f \n" - " lock; addl %1,%0 \n" - "2: rep; nop \n" - " cmpl %1,%0 \n" - " jne 2b \n" - " jmp 1b \n" - "3:" - : "=3Dm" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory" ); + "1: lock; " WRITE_LOCK_SUB(1) " %0\n" + " jnz 3f \n" + " .subsection 1 \n" + "3: lock; " WRITE_LOCK_ADD(1) " %0\n" + "2: rep; nop \n" + " cmpl " WRITE_LOCK_CMP(1) ", %0\n" + " jne 2b \n" + " jmp 1b \n" + " .subsection 0 \n" + : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory" ); } =20 static always_inline int _raw_write_trylock(raw_rwlock_t *rw) @@ -71,12 +99,12 @@ static always_inline int _raw_write_tryl int rc; =20 asm volatile ( - " lock; subl %2,%0 \n" - " jz 1f \n" - " lock; addl %2,%0 \n" - " dec %1 \n" + " lock; " WRITE_LOCK_SUB(2) " %0\n" + " jz 1f \n" + " lock; " WRITE_LOCK_ADD(2) " %0\n" + " dec %1 \n" "1:" - : "=3Dm" (rw->lock), "=3Dr" (rc) : "i" (RW_LOCK_BIAS), "1" (1) + : "+m" (rw->write), "=3Dr" (rc) : "i" (RW_LOCK_BIAS), "1" (1) : "memory" ); =20 return rc; @@ -85,18 +113,24 @@ static always_inline int _raw_write_tryl static always_inline void _raw_read_unlock(raw_rwlock_t *rw) { asm volatile ( - "lock ; incl %0" - : "=3Dm" ((rw)->lock) : : "memory" ); + " lock ; inc" READ_LOCK_SUFFIX " %0" + : "+m" (rw->lock) : : "memory" ); } =20 static always_inline void _raw_write_unlock(raw_rwlock_t *rw) { asm volatile ( - "lock ; addl %1,%0" - : "=3Dm" ((rw)->lock) : "i" (RW_LOCK_BIAS) : "memory" ); + " lock ; " WRITE_LOCK_ADD(1) " %0" + : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory" ); } =20 #define _raw_rw_is_locked(x) ((x)->lock < RW_LOCK_BIAS) -#define _raw_rw_is_write_locked(x) ((x)->lock <=3D 0) +#define _raw_rw_is_write_locked(x) ((x)->write <=3D 0) + +#undef READ_LOCK_SUFFIX +#undef WRITE_LOCK_ADD +#undef WRITE_LOCK_SUB +#undef WRITE_LOCK_CMP + =20 #endif /* __ASM_SPINLOCK_H */ --=__Part1834D47B.0__= Content-Type: text/plain; name="x86-rwlock-scalability.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-rwlock-scalability.patch" >From an abstract perspective, it should be possible to replace normal=0Aspi= n locks with the write version of rwlocks without correctness=0Aissues. = The implementation so far had a very small probability of not=0Ascaling = beyond 128 CPUs (which would become more significant if=0Aaforementioned = replacement was done), since the sign bit of a 32-bit=0Ainteger is being = used to determine various conditions, and 129 or more=0ACPUs simultaneously= subtracting RW_LOCK_BIAS from a single lock would=0Acause an undetected = overflow.=0A=0ATherefore, the bias gets adjusted so that it can cover up = to 2048=0ACPUs, and the lock structure gets widened to account for the = unlikely=0Acase of getting built for and run on a system with even more = CPUs.=0A=0AWhile touching this code, I also used the same .subsection = approach=0Athat was already used elsewhere to eliminate taken branches = from the=0Afast paths.=0A=0AAnd the output constraints of the affected = asm()-s got fixed to use=0A"+" rather than "=3D".=0A=0ASigned-off-by: Jan = Beulich =0A=0A--- a/xen/include/asm-x86/spinlock.h=0A+= ++ b/xen/include/asm-x86/spinlock.h=0A@@ -31,39 +31,67 @@ static always_inl= ine int _raw_spin_trylo=0A return (oldval > 0);=0A }=0A =0A-typedef = struct {=0A+#if NR_CPUS <=3D 2048=0A+=0A+typedef union {=0A volatile = int lock;=0A+ volatile int write;=0A+} raw_rwlock_t;=0A+=0A+#define = RW_LOCK_BIAS 0x00100000=0A+#define READ_LOCK_SUFFIX "l"=0A+#define = WRITE_LOCK_ADD(n) "addl %" #n ","=0A+#define WRITE_LOCK_SUB(n) "subl %" #n = ","=0A+#define WRITE_LOCK_CMP(n) "%" #n=0A+=0A+#else=0A+=0A+typedef union = {=0A+ volatile s64 lock;=0A+ struct {=0A+ volatile u32 = read;=0A+ volatile s32 write;=0A+ };=0A } raw_rwlock_t;=0A = =0A-#define RW_LOCK_BIAS 0x01000000=0A+#define RW_LOCK_BIAS (1L << = 32)=0A+#define READ_LOCK_SUFFIX "q"=0A+#define WRITE_LOCK_ADD(n) = "incl"=0A+#define WRITE_LOCK_SUB(n) "decl"=0A+#define WRITE_LOCK_CMP(n) = "$1"=0A+=0A+#endif=0A+=0A #define _RAW_RW_LOCK_UNLOCKED /*(raw_rwlock_t)*/ = { RW_LOCK_BIAS }=0A =0A static always_inline void _raw_read_lock(raw_rwlock= _t *rw)=0A {=0A asm volatile (=0A- "1: lock; decl %0 = \n"=0A- " jns 3f \n"=0A- " lock; incl = %0 \n"=0A- "2: rep; nop \n"=0A- " = cmpl $1,%0 \n"=0A- " js 2b \n"=0A- = " jmp 1b \n"=0A- "3:"=0A- : "=3Dm" = (rw->lock) : : "memory" );=0A+ "1: lock; dec" READ_LOCK_SUFFIX " = %0\n"=0A+ " js 3f \n"=0A+ " = .subsection 1 \n"=0A+ "3: lock; inc" READ_LOCK_SU= FFIX " %0\n"=0A+ "2: rep; nop \n"=0A+ = " cmp" READ_LOCK_SUFFIX " $1,%0 \n"=0A+ " js 2b = \n"=0A+ " jmp 1b \n"=0A+ = " .subsection 0 \n"=0A+ : "+m" = (rw->lock) : : "memory" );=0A }=0A =0A static always_inline void _raw_write= _lock(raw_rwlock_t *rw)=0A {=0A asm volatile (=0A- "1: lock; = subl %1,%0 \n"=0A- " jz 3f \n"=0A- " = lock; addl %1,%0 \n"=0A- "2: rep; nop \n"=0A- = " cmpl %1,%0 \n"=0A- " jne 2b = \n"=0A- " jmp 1b \n"=0A- "3:"=0A- = : "=3Dm" (rw->lock) : "i" (RW_LOCK_BIAS) : "memory" );=0A+ "1: = lock; " WRITE_LOCK_SUB(1) " %0\n"=0A+ " jnz 3f = \n"=0A+ " .subsection 1 \n"=0A+ "3: = lock; " WRITE_LOCK_ADD(1) " %0\n"=0A+ "2: rep; nop = \n"=0A+ " cmpl " WRITE_LOCK_CMP(1) ", %0\n"=0A+ " = jne 2b \n"=0A+ " jmp 1b = \n"=0A+ " .subsection 0 \n"=0A+ : = "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory" );=0A }=0A =0A static = always_inline int _raw_write_trylock(raw_rwlock_t *rw)=0A@@ -71,12 +99,12 = @@ static always_inline int _raw_write_tryl=0A int rc;=0A =0A asm = volatile (=0A- " lock; subl %2,%0 \n"=0A- " jz 1f = \n"=0A- " lock; addl %2,%0 \n"=0A- " = dec %1 \n"=0A+ " lock; " WRITE_LOCK_SUB(2) " = %0\n"=0A+ " jz 1f \n"=0A+ " = lock; " WRITE_LOCK_ADD(2) " %0\n"=0A+ " dec %1 = \n"=0A "1:"=0A- : "=3Dm" (rw->lock), "=3Dr" (rc) : "i" = (RW_LOCK_BIAS), "1" (1)=0A+ : "+m" (rw->write), "=3Dr" (rc) : "i" = (RW_LOCK_BIAS), "1" (1)=0A : "memory" );=0A =0A return = rc;=0A@@ -85,18 +113,24 @@ static always_inline int _raw_write_tryl=0A = static always_inline void _raw_read_unlock(raw_rwlock_t *rw)=0A {=0A = asm volatile (=0A- "lock ; incl %0"=0A- : "=3Dm" ((rw)->lock)= : : "memory" );=0A+ " lock ; inc" READ_LOCK_SUFFIX " %0"=0A+ = : "+m" (rw->lock) : : "memory" );=0A }=0A =0A static always_inline = void _raw_write_unlock(raw_rwlock_t *rw)=0A {=0A asm volatile (=0A- = "lock ; addl %1,%0"=0A- : "=3Dm" ((rw)->lock) : "i" (RW_LOCK_BIA= S) : "memory" );=0A+ " lock ; " WRITE_LOCK_ADD(1) " %0"=0A+ = : "+m" (rw->write) : "i" (RW_LOCK_BIAS) : "memory" );=0A }=0A =0A = #define _raw_rw_is_locked(x) ((x)->lock < RW_LOCK_BIAS)=0A-#define = _raw_rw_is_write_locked(x) ((x)->lock <=3D 0)=0A+#define _raw_rw_is_write_l= ocked(x) ((x)->write <=3D 0)=0A+=0A+#undef READ_LOCK_SUFFIX=0A+#undef = WRITE_LOCK_ADD=0A+#undef WRITE_LOCK_SUB=0A+#undef WRITE_LOCK_CMP=0A+=0A = =0A #endif /* __ASM_SPINLOCK_H */=0A --=__Part1834D47B.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part1834D47B.0__=--