Skip to contentSkip to conversation

i386 spinlocks: disable interrupts only if we enabled them

PATCH In review Chuck Ebbert 5 participants 6 messages Mar 7, 2006 – Mar 8, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i386 spinlocks: disable interrupts only if we enabled them

_raw_spin_lock_flags() is entered with interrupts disabled. If it cannot obtain a spinlock, it checks the flags that were passed and re-enables interrupts before spinning if that's how the flags are set. When the spinlock might be available, it disables interrupts (even if they are already disabled) before trying to get the lock. Change that so interrupts are only disabled if they have been enabled. This costs nine bytes of duplicated spinloop code.

Fastpath before patch:

        jle <keep looping>      not-taken conditional jump
        cli                     disable interrupts
        jmp <try for lock>      unconditional jump

Fastpath after patch, if interrupts were not enabled:
jg <try for lock> taken conditional branch

include/asm-i386/spinlock.h +9 -4
@@ -35,18 +35,23 @@
 #define __raw_spin_lock_string_flags \
 	"\n1:\t" \
 	"lock ; decb %0\n\t" \
-	"jns 4f\n\t" \
+	"jns 5f\n" \
 	"2:\t" \
 	"testl $0x200, %1\n\t" \
-	"jz 3f\n\t" \
-	"sti\n\t" \
+	"jz 4f\n\t" \
+	"sti\n" \
 	"3:\t" \
 	"rep;nop\n\t" \
 	"cmpb $0, %0\n\t" \
 	"jle 3b\n\t" \
 	"cli\n\t" \
 	"jmp 1b\n" \
-	"4:\n\t"
+	"4:\t" \
+	"rep;nop\n\t" \
+	"cmpb $0, %0\n\t" \
+	"jg 1b\n\t" \
+	"jmp 4b\n" \
+	"5:\n\t"
 
 static inline void __raw_spin_lock(raw_spinlock_t *lock)
 {
Signature · 2 lines
Chuck
"Penguins don't come from next door, they come from the Antarctic!"
#1 lore raw
Quoted 9 lines · Chuck Ebbert

_raw_spin_lock_flags() is entered with interrupts disabled. If it cannot obtain a spinlock, it checks the flags that were passed and re-enables interrupts before spinning if that's how the flags are set. When the spinlock might be available, it disables interrupts (even if they are already disabled) before trying to get the lock. Change that so interrupts are only disabled if they have been enabled. This costs nine bytes of duplicated spinloop code.

Quoted 8 lines

Fastpath before patch:

        jle <keep looping>      not-taken conditional jump
        cli                     disable interrupts
        jmp <try for lock>      unconditional jump

Fastpath after patch, if interrupts were not enabled:
jg <try for lock> taken conditional branch

Well no. The fastpath is:

jns 4f we got the lock.

Quoted 19 lines
include/asm-i386/spinlock.h +3 -3
@@ -35,18 +35,23 @@
 #define __raw_spin_lock_string_flags \
 	"\n1:\t" \
 	"lock ; decb %0\n\t" \
-	"jns 4f\n\t" \
+	"jns 5f\n" \
 	"2:\t" \
 	"testl $0x200, %1\n\t" \
-	"jz 3f\n\t" \
-	"sti\n\t" \
+	"jz 4f\n\t" \
+	"sti\n" \
 	"3:\t" \
 	"rep;nop\n\t" \
 	"cmpb $0, %0\n\t" \
 	"jle 3b\n\t" \

include/asm-i386/spinlock.h line 55

Quoted 10 lines
include/asm-i386/spinlock.h +6 -1
 	"cli\n\t" \
 	"jmp 1b\n" \
-	"4:\n\t"
+	"4:\t" \
+	"rep;nop\n\t" \
+	"cmpb $0, %0\n\t" \
+	"jg 1b\n\t" \
+	"jmp 4b\n" \
+	"5:\n\t"
 

So this is speeding up the slowpath, which really shouldn't matter unless we have bigger problems.

And it's increasing text size. Which wouldn't be a big problem if the spinning code was still in an out-of-line section, but it isn't any more.

(I forget why we undid that optimisation. What was wrong with it?)

Acked-by
#2 lore raw
Quoted 5 lines · Andrew Morton

And it's increasing text size. Which wouldn't be a big problem if the spinning code was still in an out-of-line section, but it isn't any more.

(I forget why we undid that optimisation. What was wrong with it?)

we dont inline that code anymore. So i think the optimization is fine.

Ingo

#3 lore raw
Quoted 1 line · Ingo Molnar

we dont inline that code anymore. So i think the optimization is fine.

Why is that? It adds memory traffic that has to be synchronized before the lock occurs and clobbered registers now in the caller.

-ben

Signature · 2 lines
"Time is of no importance, Mr. President, only life is important."
Don't Email: <[email protected]>.
#4 lore raw
Quoted 6 lines · Benjamin LaHaise
Quoted 1 line · Ingo Molnar

we dont inline that code anymore. So i think the optimization is fine.

Why is that? It adds memory traffic that has to be synchronized before the lock occurs and clobbered registers now in the caller.

Is the inlined lock;decb+jns likely to worsen the text size? I doubt it. Overall text will get bigger due to the out-of-line stuff, but that's OK.

I'm sure we went over all this, but I don't recall the thinking.

#5 lore raw
Quoted 11 lines · Andrew Morton
Quoted 4 lines · Benjamin LaHaise
Quoted 1 line · Ingo Molnar

we dont inline that code anymore. So i think the optimization is fine.

Why is that? It adds memory traffic that has to be synchronized before the lock occurs and clobbered registers now in the caller.

Is the inlined lock;decb+jns likely to worsen the text size? I doubt it. Overall text will get bigger due to the out-of-line stuff, but that's OK.

I'm sure we went over all this, but I don't recall the thinking.

Seems like a very good idea not to clobber any registers in lock fastpaths. I don't see how that could have been a win (especially for i386) but still, Ingo must have had a reason behind it.

Signature · 2 lines
SUSE Labs, Novell Inc.
Send instant messages to your online friends http://au.messenger.yahoo.com