Skip to contentSkip to conversation

i386: spinlock optimization

PATCH In review Chuck Ebbert 5 participants 7 messages Oct 14, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386: spinlock optimization

Attempt to acquire spinlock sooner after spinning and then noticing it has become available. Also adds a slight delay before testing the spinlock again when it's not available, reducing bus traffic.

This change makes spinlocks fairer in the case where the owner drops the lock and then immediately tries to take it again.

---

 include/asm-i386/spinlock.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
include/asm-i386/spinlock.h +2 -2
@@ -28,8 +28,8 @@
 	"2:\t" \
 	"rep;nop\n\t" \
 	"cmpb $0,%0\n\t" \
-	"jle 2b\n\t" \
-	"jmp 1b\n" \
+	"jg 1b\n\t" \
+	"jmp 2b\n" \
 	"3:\n\t"
 
 #define __raw_spin_lock_string_flags \
#1 lore raw
Quoted 3 lines · Chuck Ebbert

Attempt to acquire spinlock sooner after spinning and then noticing it has become available. Also adds a slight delay before testing the spinlock again when it's not available, reducing bus traffic.

I doubt your change adds any noticeable delay on a aggressive OOO CPU, which are pretty much all modern x86s. It's probably a nop.

-Andi

#2 lore raw
In reply to Chuck Ebbert
Quoted 17 lines · Chuck Ebbert

Attempt to acquire spinlock sooner after spinning and then noticing it has become available. Also adds a slight delay before testing the spinlock again when it's not available, reducing bus traffic.

This change makes spinlocks fairer in the case where the owner drops the lock and then immediately tries to take it again.

---

include/asm-i386/spinlock.h | 4 ++--

1 files changed, 2 insertions(+), 2 deletions(-)
include/asm-i386/spinlock.h +0 -0
@@ -28,8 +28,8 @@
 	"2:\t" \

include/asm-i386/spinlock.h

Quoted 10 lines
include/asm-i386/spinlock.h +2 -3
 	"rep;nop\n\t" \
 	"cmpb $0,%0\n\t" \
-	"jle 2b\n\t" \
-	"jmp 1b\n" \
+	"jg 1b\n\t" \
+	"jmp 2b\n" \
 	"3:\n\t"
 
 #define __raw_spin_lock_string_flags \
-

I just noticed in linux-2.6.13.4 that the spinlocks are wrong. You are trying to optimize a path that should have been granted in the first place:

Somehow, these spin-locks got all screwed up.

Given: nobody has the lock. The lock variable is 0.

#define spin_lock_string \
"\n1:\t" \
"lock ; decb %0\n\t" \
"jns 3f\n" \ # This should have been 'js'
"2:\t" \
"rep;nop\n\t" \
"cmpb $0,%0\n\t" \
"jle 2b\n\t" \
"jmp 1b\n" \
"3:\n\t" # Got the lock, exit loop

The spin-locks should have been:

#define spin_lock_string \
"\n1:\t" \
"lock ; decb %0\n\t" \ # Bump the lock variable
"js 2f\n\t" \ # Good, we got the lock
"lock ; incb %0\n\t" \ # Release your attempt __important__
"rep ; nop \n\t" \ # pause
"jmp 1b\n" \ # Try again
"2:\n" # Exit the lock

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.46 BogoMips). Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#3 lore raw
Quoted 4 lines · linux-os \(Dick Johnson\)

Somehow, these spin-locks got all screwed up.

Given: nobody has the lock. The lock variable is 0.

Unlocked locks have .raw_lock.slock = 1, not 0.

#4 lore raw
Quoted 8 lines · Andrew Morton
Quoted 4 lines · linux-os \(Dick Johnson\)

Somehow, these spin-locks got all screwed up.

Given: nobody has the lock. The lock variable is 0.

Unlocked locks have .raw_lock.slock = 1, not 0.

Hmmm. Okay. Doesn't that add more code?

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.46 BogoMips). Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.

#5 lore raw
In reply to linux-os (Dick Johnson)
Quoted 2 lines · linux-os (Dick Johnson)

Somehow, these spin-locks got all screwed up.

Nope.

Quoted 1 line

Given: nobody has the lock. The lock variable is 0.

Your "given" is wrong.

UNLOCKED is 1, locked is 0 or negative.

Linus

#6 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 2 lines · linux-os (Dick Johnson)

Somehow, these spin-locks got all screwed up.

Nope.

Quoted 8 lines
Quoted 1 line

Given: nobody has the lock. The lock variable is 0.

Your "given" is wrong.

UNLOCKED is 1, locked is 0 or negative.

Linus

Okay. Thanks.

Cheers,
Dick Johnson
Penguin : Linux version 2.6.13.4 on an i686 machine (5589.46 BogoMips). Warning : 98.36% of all statistics are fiction.
.

****************************************************************
The information transmitted in this message is confidential and may be privileged. Any review, retransmission, dissemination, or other use of this information by persons or entities other than the intended recipient is prohibited. If you are not the intended recipient, please notify Analogic Corporation immediately - by replying to this message or by sending an email to [email protected] - and destroy all copies of this information, including any attachments, without reading or disclosing them.

Thank you.