Skip to contentSkip to conversation

rw_semaphores, optimisations try #4

PATCH In review D.W.Howells 3 participants 3 messages Apr 25, 2001 – Apr 26, 2001

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

PATCH 1/1 rw_semaphores, optimisations try #4

This patch (made against linux-2.4.4-pre6 + rwsem-opt3) somewhat improves performance on the i386 XADD optimised implementation:

A patch against -pre6 can be obtained too:

ftp://infradead.org/pub/people/dwh/rwsem-pre6-opt4.diff

Here's some benchmarks (take with a pinch of salt of course):

TEST NUM READERS NUM WRITERS CONTENTION DURATION
=============== =============== =============== =============== ========
rwsem-r1 1 0 no 10s
rwsem-r2 2 0 no 10s
rwsem-ro 4 0 no 10s
rwsem-w1 0 1 no 10s
rwsem-wo 0 4 w-w only 10s
rwsem-rw 4 2 r-w & w-w 10s
rwsem-worm 30 1 r-w & w-w 10s
rwsem-xx 30 15 r-w & w-w 50s

			rwsem-opt4 (mine)		00_rwsem-9 (Andrea's)
		-------------------------------	-------------------------------

TEST READERS WRITERS READERS WRITERS
=============== =============== =============== ===============
===============
rwsem-r1 30347096 n/a 30130004 n/a

		30362972	n/a		30127882	n/a
rwsem-r2	11031268	n/a		11035072	n/a
		11038232	n/a		11030787	n/a
rwsem-ro	12641408	n/a		12722192	n/a
		12636058	n/a		12729404	n/a
rwsem-w1	n/a		28607326	n/a		28505470
		n/a		28609208	n/a		28508206
rwsem-wo	n/a		1607789		n/a		1783876
		n/a		1608603		n/a		1800982
rwsem-rw	1111545		557071		1106763		554698
		1109773		555901		1103090		552567
rwsem-worm	5229696		54807		1585755		52438
		5219531		54528		1588428		52222
rwsem-xx	5396096		2786619		5361894		2768893
		5398443		2787613		5400716		2788801

I've compared my patch to Andrea's 00_rwsem-9, both built on top of linux-2.4.4-pre6.

David

include/asm-i386/rwsem.h +56 -55
diff -uNr linux-2.4.4-pre6-opt3/include/asm-i386/rwsem.h linux/include/asm-i386/rwsem.h
@@ -3,6 +3,30 @@
  * Written by David Howells ([email protected]).
  *
  * Derived from asm-i386/semaphore.h
+ *
+ *
+ * The MSW of the count is the negated number of active writers and waiting
+ * lockers, and the LSW is the total number of active locks
+ *
+ * The lock count is initialized to 0 (no active and no waiting lockers).
+ *
+ * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
+ * uncontended lock. This can be determined because XADD returns the old value.
+ * Readers increment by 1 and see a positive value when uncontended, negative
+ * if there are writers (and maybe) readers waiting (in which case it goes to
+ * sleep).
+ *
+ * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
+ * be extended to 65534 by manually checking the whole MSW rather than relying
+ * on the S flag.
+ *
+ * The value of ACTIVE_BIAS supports up to 65535 active processes.
+ *
+ * This should be totally fair - if anything is waiting, a process that wants a
+ * lock will go to the back of the queue. When the currently active lock is
+ * released, if there's a writer at the front of the queue, then that and only
+ * that will be woken up; if there's a bunch of consequtive readers at the
+ * front, then they'll all be woken up, but no other readers will be.
  */
 
 #ifndef _I386_RWSEM_H
@@ -19,6 +43,10 @@
 
 struct rwsem_waiter;
 
+extern struct rw_semaphore *FASTCALL(rwsem_down_read_failed(struct rw_semaphore *sem));
+extern struct rw_semaphore *FASTCALL(rwsem_down_write_failed(struct rw_semaphore *sem));
+extern struct rw_semaphore *FASTCALL(rwsem_wake(struct rw_semaphore *));
+
 /*
  * the semaphore definition
  */
@@ -31,8 +59,7 @@
 #define RWSEM_ACTIVE_READ_BIAS		RWSEM_ACTIVE_BIAS
 #define RWSEM_ACTIVE_WRITE_BIAS		(RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS)
 	spinlock_t		wait_lock;
-	struct rwsem_waiter	*wait_front;
-	struct rwsem_waiter	**wait_back;
+	struct list_head	wait_list;
 #if RWSEM_DEBUG
 	int			debug;
 #endif
@@ -48,7 +75,7 @@
 #endif
 
 #define __RWSEM_INITIALIZER(name) \
-{ RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, NULL, &(name).wait_front \
+{ RWSEM_UNLOCKED_VALUE, SPIN_LOCK_UNLOCKED, LIST_HEAD_INIT((name).wait_list) \
 	__RWSEM_DEBUG_INIT }
 
 #define DECLARE_RWSEM(name) \
@@ -58,8 +85,7 @@
 {
 	sem->count = RWSEM_UNLOCKED_VALUE;
 	spin_lock_init(&sem->wait_lock);
-	sem->wait_front = NULL;
-	sem->wait_back = &sem->wait_front;
+	INIT_LIST_HEAD(&sem->wait_list);
 #if RWSEM_DEBUG
 	sem->debug = 0;
 #endif
@@ -85,9 +111,9 @@
 		"  jmp       1b\n"
 		".previous"
 		"# ending down_read\n\t"
-		: "=m"(sem->count)
-		: "a"(sem), "m"(sem->count)
-		: "memory");
+		: "+m"(sem->count)
+		: "a"(sem)
+		: "memory", "cc");
 }
 
 /*
@@ -112,9 +138,9 @@
 		"  jmp       1b\n"
 		".previous\n"
 		"# ending down_write"
-		: "+d"(tmp), "=m"(sem->count)
-		: "a"(sem), "m"(sem->count)
-		: "memory");
+		: "+d"(tmp), "+m"(sem->count)
+		: "a"(sem)
+		: "memory", "cc");
 }
 
 /*
@@ -122,23 +148,25 @@
  */
 static inline void __up_read(struct rw_semaphore *sem)
 {
+	__s32 tmp = -RWSEM_ACTIVE_READ_BIAS;
 	__asm__ __volatile__(
 		"# beginning __up_read\n\t"
-LOCK_PREFIX	"  xadd      %%eax,(%%edx)\n\t" /* subtracts 1, returns the old value */
+LOCK_PREFIX	"  xadd      %%edx,(%%eax)\n\t" /* subtracts 1, returns the old value */
 		"  js        2f\n\t" /* jump if the lock is being waited upon */
 		"1:\n\t"
 		".section .text.lock,\"ax\"\n"
 		"2:\n\t"
-		"  decl      %%eax\n\t" /* xadd gave us the old count */
-		"  testl     %3,%%eax\n\t" /* do nothing if still outstanding active readers */
+		"  decw      %%dx\n\t" /* do nothing if still outstanding active readers */
 		"  jnz       1b\n\t"
-		"  call      rwsem_up_read_wake\n\t"
+		"  pushl     %%ecx\n\t"
+		"  call      rwsem_wake\n\t"
+		"  popl      %%ecx\n\t"
 		"  jmp       1b\n"
 		".previous\n"
 		"# ending __up_read\n"
-		: "=m"(sem->count)
-		: "d"(sem), "a"(-RWSEM_ACTIVE_READ_BIAS), "i"(RWSEM_ACTIVE_MASK), "m"(sem->count)
-		: "memory", "ecx");
+		: "+m"(sem->count), "+d"(tmp)
+		: "a"(sem)
+		: "memory", "cc");
 }
 
 /*
@@ -148,18 +176,23 @@
 {
 	__asm__ __volatile__(
 		"# beginning __up_write\n\t"
-LOCK_PREFIX	"  cmpxchgl  %%ecx,(%%edx)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
+		"  movl      %2,%%edx\n\t"
+LOCK_PREFIX	"  xaddl     %%edx,(%%eax)\n\t" /* tries to transition 0xffff0001 -> 0x00000000 */
 		"  jnz       2f\n\t" /* jump if the lock is being waited upon */
 		"1:\n\t"
 		".section .text.lock,\"ax\"\n"
 		"2:\n\t"
-		"  call      rwsem_up_write_wake\n\t"
+		"  decw      %%dx\n\t" /* did the active count reduce to 0? */
+		"  jnz       1b\n\t" /* jump back if not */
+		"  pushl     %%ecx\n\t"
+		"  call      rwsem_wake\n\t"
+		"  popl      %%ecx\n\t"
 		"  jmp       1b\n"
 		".previous\n"
 		"# ending __up_write\n"
-		: "=m"(sem->count)
-		: "d"(sem), "a"(RWSEM_ACTIVE_WRITE_BIAS), "c"(0), "m"(sem->count)
-		: "memory");
+		: "+m"(sem->count)
+		: "a"(sem), "i"(-RWSEM_ACTIVE_WRITE_BIAS)
+		: "memory", "cc", "edx");
 }
 
 /*
@@ -187,38 +220,6 @@
 		: "memory");
 
 	return tmp+delta;
-}
-
-/*
- * implement compare and exchange functionality on the rw-semaphore count LSW
- */
-static inline __u16 rwsem_cmpxchgw(struct rw_semaphore *sem, __u16 old, __u16 new)
-{
-	__u16 tmp = old;
-
-	__asm__ __volatile__(
-LOCK_PREFIX	"cmpxchgw %w2,%3"
-		: "=a"(tmp), "=m"(sem->count)
-		: "r"(new), "m1"(sem->count), "a"(tmp)
-		: "memory");
-
-	return tmp;
-}
-
-/*
- * implement compare and exchange functionality on the rw-semaphore count
- */
-static inline signed long rwsem_cmpxchg(struct rw_semaphore *sem, signed long old, signed long new)
-{
-	signed long tmp = old;
-
-	__asm__ __volatile__(
-LOCK_PREFIX	"cmpxchgl %2,%3"
-		: "=a"(tmp), "=m"(sem->count)
-		: "r"(new), "m1"(sem->count), "a"(tmp)
-		: "memory");
-
-	return tmp;
 }
 
 #endif /* __KERNEL__ */
include/linux/rwsem.h +0 -35
diff -uNr linux-2.4.4-pre6-opt3/include/linux/rwsem.h linux/include/linux/rwsem.h
@@ -2,30 +2,6 @@
  *
  * Written by David Howells ([email protected]).
  * Derived from asm-i386/semaphore.h
- *
- *
- * The MSW of the count is the negated number of active writers and waiting
- * lockers, and the LSW is the total number of active locks
- *
- * The lock count is initialized to 0 (no active and no waiting lockers).
- *
- * When a writer subtracts WRITE_BIAS, it'll get 0xffff0001 for the case of an
- * uncontended lock. This can be determined because XADD returns the old value.
- * Readers increment by 1 and see a positive value when uncontended, negative
- * if there are writers (and maybe) readers waiting (in which case it goes to
- * sleep).
- *
- * The value of WAITING_BIAS supports up to 32766 waiting processes. This can
- * be extended to 65534 by manually checking the whole MSW rather than relying
- * on the S flag.
- *
- * The value of ACTIVE_BIAS supports up to 65535 active processes.
- *
- * This should be totally fair - if anything is waiting, a process that wants a
- * lock will go to the back of the queue. When the currently active lock is
- * released, if there's a writer at the front of the queue, then that and only
- * that will be woken up; if there's a bunch of consequtive readers at the
- * front, then they'll all be woken up, but no other readers will be.
  */
 
 #ifndef _LINUX_RWSEM_H
@@ -42,17 +18,6 @@
 #include <asm/atomic.h>
 
 struct rw_semaphore;
-
-/* defined contention handler functions for the generic case
- * - these are also used for the exchange-and-add based algorithm
- */
-#ifdef CONFIG_RWSEM_XCHGADD_ALGORITHM
-/* we use FASTCALL convention for the helpers */
-extern struct rw_semaphore *FASTCALL(rwsem_down_read_failed(struct rw_semaphore *sem));
-extern struct rw_semaphore *FASTCALL(rwsem_down_write_failed(struct rw_semaphore *sem));
-extern void FASTCALL(rwsem_up_read_wake(signed long, struct rw_semaphore *));
-extern void FASTCALL(rwsem_up_write_wake(signed long, struct rw_semaphore *));
-#endif
 
 #ifdef CONFIG_RWSEM_GENERIC_SPINLOCK
 #include <linux/rwsem-spinlock.h> /* use a generic implementation */
lib/rwsem.c +74 -129

329 lines not on this page · raw message

#1 lore raw
Quoted 2 lines · D . W . Howells

This patch (made against linux-2.4.4-pre6 + rwsem-opt3) somewhat improves performance on the i386 XADD optimised implementation:

It seems more similar to my code btw (you finally killed the useless chmxchg ;).

I only had a short low at your attached patch, but the results are quite suspect to my eyes beacuse we should still be equally fast in the fast path and I should still beat you on the write fast path because I do a much faster subl; js while you do movl -1; xadd ; js, while according to your results you beat me on both. Do you have an explanation or you don't know the reason either? I will re-benchmark the whole thing shortly. But before re-benchmark if you have time could you fix the benchmark to use the variable pointer and send me a new tarball? For your code it probably doesn't matter because you dereference the pointer by hand anyways, but it matters for mine and we want to benchmark real world fast path of course.

Andrea

#2 lore raw
Quoted 2 lines · Andrea Arcangeli

It seems more similar to my code btw (you finally killed the useless chmxchg ;).

CMPXCHG ought to make things better by avoiding the XADD(+1)/XADD(-1) loop, however, I tried various combinations and XADD beats CMPXCHG significantly.

Here's a quote from Borland assembler manual I managed to dig out, giving i486 timings on memory access:

ADDL/SUBL 3 cycles
XADDL 4 cycles
CMPXCHG 8 cycles (success) / 10 cycles (failure)
LOCK +1 cycle minimum on this CPU

In reality, however, XADDL gives at least as good a result as ADDL/SUBL, maybe just a little bit better, but its hard to say. However, the penalty imposed on the other CPU (when it has to flush it's cache) probably more than makes up for the difference.

Quoted 6 lines

I only had a short low at your attached patch, but the results are quite suspect to my eyes beacuse we should still be equally fast in the fast path and I should still beat you on the write fast path because I do a much faster subl; js while you do movl -1; xadd ; js, while according to your results you beat me on both. Do you have an explanation or you don't know the reason either?

	MOVL $1,EDX
	SUBL EDX,(EAX)

Works out faster than:

	SUBL $1,(EAX)

as well... probably due to an avoided stall when the instruction before the snippet loads EAX from memory. Oh yes... "STC, SUBL" may also be faster too.

Quoted 5 lines

I will re-benchmark the whole thing shortly. But before re-benchmark if you have time could you fix the benchmark to use the variable pointer and send me a new tarball? For your code it probably doesn't matter because you dereference the pointer by hand anyways, but it matters for mine and we want to benchmark real world fast path of course.

No, not till this evening now, I'm afraid.

As for real-world benchmarks, I suspect the fastpath is going to be sufficiently few cycles that it's drowned out by whatever bit of code is actually using it, like my Wine server module, which is where all this started for me.

David