x86: faster mb()+documentation tweaks
Subsystems (2)
Michael S. Tsirkin
maintainerv3 cover x86: faster mb()+documentation tweaks
mb() typically uses mfence on modern x86, but a micro-benchmark shows that it's 2 to 3 times slower than lock; addl that we use on older CPUs.
So let's use the locked variant everywhere.
While I was at it, I found some inconsistencies in comments in arch/x86/include/asm/barrier.h
The documentation fixes are included first - I verified that they do not change the generated code at all. They should be safe to apply directly.
The last patch changes mb() to lock addl. I was unable to measure a speed difference on a macro benchmark, but I noted that even doing
#define mb() barrier()
seems to make no difference for most benchmarks
(it causes hangs sometimes, of course).
HPA asked that the last patch is deferred until we hear back from intel, which makes sense of course. So it needs HPA's ack.
I hope I'm not splitting this up too much - the reason is I wanted to isolate the code changes (that people might want to test for performance) from comment changes approved by Linus, from (so far unreviewed) changes I came up with myself.
Changes from v2:
add patch adding cc clobber for addl
tweak commit log for patch 2
use addl at SP-4 (as opposed to SP) to reduce data dependencies
Michael S. Tsirkin (4):
x86: add cc clobber for addl
x86: drop a comment left over from X86_OOSTORE
x86: tweak the comment about use of wmb for IO
x86: drop mfence in favor of lock+addl
arch/x86/include/asm/barrier.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 1/4 x86: add cc clobber for addl
addl clobbers flags (such as CF) but barrier.h didn't tell this to gcc. Historically, gcc doesn't need one on x86, and always considers flags clobbered. We are probably missing the cc clobber in a *lot* of places for this reason.
But even if not necessary, it's probably a good thing to add for documentation, and in case gcc semantcs ever change.
--- arch/x86/include/asm/barrier.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/barrier.h +6 -3
@@ -15,9 +15,12 @@* Some non-Intel clones support out of order store. wmb() ceases to be a * nop for these. */ -#define mb () alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2) -#define rmb () alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2) -#define wmb () alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM) +#define mb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "mfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc") +#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "lfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc") +#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "sfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc")#else #define mb () asm volatile("mfence":::"memory")#define rmb () asm volatile("lfence":::"memory")
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 1/4 x86: add cc clobber for addl
In reply to Michael S. Tsirkinaddl clobbers flags (such as CF) but barrier.h didn't tell this to gcc. Historically, gcc doesn't need one on x86, and always considers flags clobbered. We are probably missing the cc clobber in a *lot* of places for this reason.
But even if not necessary, it's probably a good thing to add for documentation, and in case gcc semantcs ever change.
--- arch/x86/include/asm/barrier.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/include/asm/barrier.h +6 -3
@@ -15,9 +15,12 @@* Some non-Intel clones support out of order store. wmb() ceases to be a * nop for these. */ -#define mb () alternative("lock; addl $0,0(%%esp)", "mfence", X86_FEATURE_XMM2) -#define rmb () alternative("lock; addl $0,0(%%esp)", "lfence", X86_FEATURE_XMM2) -#define wmb () alternative("lock; addl $0,0(%%esp)", "sfence", X86_FEATURE_XMM) +#define mb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "mfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc") +#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "lfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc") +#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "sfence", \ + X86_FEATURE_XMM2) ::: "memory", "cc")#else #define mb () asm volatile("mfence":::"memory")#define rmb () asm volatile("lfence":::"memory")
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 2/4 x86: drop a comment left over from X86_OOSTORE
In reply to Michael S. TsirkinThe comment about wmb being non-nop to deal with non-intel CPUs is a left over from before commit 09df7c4c8097 ("x86: Remove CONFIG_X86_OOSTORE").
It makes no sense now: in particular, wmb is not a nop even for regular intel CPUs because of weird use-cases e.g. dealing with WC memory.
Drop this comment.
--- arch/x86/include/asm/barrier.h | 4 ---- 1 file changed, 4 deletions(-)
arch/x86/include/asm/barrier.h +0 -4
@@ -11,10 +11,6 @@*/ #ifdef CONFIG_X86_32 -/* -* Some non-Intel clones support out of order store. wmb() ceases to be a -* nop for these. -*/ #define mb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "mfence", \ X86_FEATURE_XMM2) ::: "memory", "cc")#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "lfence", \
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 3/4 x86: tweak the comment about use of wmb for IO
In reply to Michael S. TsirkinOn x86, we *do* still use the non-nop rmb/wmb for IO barriers, but even that is generally questionable.
Leave them around as historial unless somebody can point to a case where they care about the performance, but tweak the comment so people don't think they are strictly required in all cases.
--- arch/x86/include/asm/barrier.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/barrier.h +1 -1
@@ -6,7 +6,7 @@/* * Force strict CPU ordering. -* And yes, this is required on UP too when we're talking +* And yes, this might be required on UP too when we're talking * to devices. */
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 3/4 x86: tweak the comment about use of wmb for IO
In reply to Michael S. TsirkinOn x86, we *do* still use the non-nop rmb/wmb for IO barriers, but even that is generally questionable.
Leave them around as historial unless somebody can point to a case where they care about the performance, but tweak the comment so people don't think they are strictly required in all cases.
--- arch/x86/include/asm/barrier.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/barrier.h +1 -1
@@ -6,7 +6,7 @@/* * Force strict CPU ordering. -* And yes, this is required on UP too when we're talking +* And yes, this might be required on UP too when we're talking * to devices. */
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 4/4 x86: drop mfence in favor of lock+addl
In reply to Michael S. Tsirkinmfence appears to be way slower than a locked instruction - let's use lock+add unconditionally, as we always did on old 32-bit.
Just poking at SP would be the most natural, but if we then read the value from SP, we get a false dependency which will slow us down.
This was noted in this article:
http://shipilev.net/blog/2014/on-the-fence-with-dependencies/
And is easy to reproduce by sticking a barrier in a small non-inline function.
So let's use a negative offset - which avoids this problem since we build with the red zone disabled.
Update rmb/wmb on 32 bit to use the negative offset, too, for consistency.
--- arch/x86/include/asm/barrier.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/include/asm/barrier.h +6 -7
@@ -11,16 +11,15 @@*/ #ifdef CONFIG_X86_32 -#define mb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "mfence", \ - X86_FEATURE_XMM2) ::: "memory", "cc") -#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "lfence", \ +#define mb () asm volatile("lock; addl $0,-4(%%esp)" ::: "memory", "cc") +#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,-4(%%esp)", "lfence", \ X86_FEATURE_XMM2) ::: "memory", "cc") -#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "sfence", \ +#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,-4(%%esp)", "sfence", \ X86_FEATURE_XMM2) ::: "memory", "cc")#else -#define mb () asm volatile("mfence":::"memory") -#define rmb () asm volatile("lfence":::"memory") -#define wmb () asm volatile("sfence" ::: "memory") +#define mb () asm volatile("lock; addl $0,-4(%%rsp)" ::: "memory", "cc") +#define rmb () asm volatile("lfence" ::: "memory") +#define wmb () asm volatile("sfence" ::: "memory")#endif #ifdef CONFIG_X86_PPRO_FENCE
Signature · 1 line
MST
Michael S. Tsirkin
maintainerPATCH v3 4/4 x86: drop mfence in favor of lock+addl
In reply to Michael S. Tsirkinmfence appears to be way slower than a locked instruction - let's use lock+add unconditionally, as we always did on old 32-bit.
Just poking at SP would be the most natural, but if we then read the value from SP, we get a false dependency which will slow us down.
This was noted in this article:
http://shipilev.net/blog/2014/on-the-fence-with-dependencies/
And is easy to reproduce by sticking a barrier in a small non-inline function.
So let's use a negative offset - which avoids this problem since we build with the red zone disabled.
Update rmb/wmb on 32 bit to use the negative offset, too, for consistency.
--- arch/x86/include/asm/barrier.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/x86/include/asm/barrier.h +6 -7
@@ -11,16 +11,15 @@*/ #ifdef CONFIG_X86_32 -#define mb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "mfence", \ - X86_FEATURE_XMM2) ::: "memory", "cc") -#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "lfence", \ +#define mb () asm volatile("lock; addl $0,-4(%%esp)" ::: "memory", "cc") +#define rmb () asm volatile(ALTERNATIVE("lock; addl $0,-4(%%esp)", "lfence", \ X86_FEATURE_XMM2) ::: "memory", "cc") -#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,0(%%esp)", "sfence", \ +#define wmb () asm volatile(ALTERNATIVE("lock; addl $0,-4(%%esp)", "sfence", \ X86_FEATURE_XMM2) ::: "memory", "cc")#else -#define mb () asm volatile("mfence":::"memory") -#define rmb () asm volatile("lfence":::"memory") -#define wmb () asm volatile("sfence" ::: "memory") +#define mb () asm volatile("lock; addl $0,-4(%%rsp)" ::: "memory", "cc") +#define rmb () asm volatile("lfence" ::: "memory") +#define wmb () asm volatile("sfence" ::: "memory")#endif #ifdef CONFIG_X86_PPRO_FENCE
Signature · 1 line
MST
Borislav Petkov
maintainerQuoted 32 lines · Michael S. Tsirkin
mb()typically uses mfence on modern x86, but a micro-benchmark shows that it's 2 to 3 times slower than lock; addl that we use on older CPUs.So let's use the locked variant everywhere.
While I was at it, I found some inconsistencies in comments in
arch/x86/include/asm/barrier.hThe documentation fixes are included first - I verified that they do not change the generated code at all. They should be safe to apply directly.
The last patch changes
mb()to lock addl. I was unable to measure a speed difference on a macro benchmark, but I noted that even doing#define mb() barrier()seems to make no difference for most benchmarks
(it causes hangs sometimes, of course).HPA asked that the last patch is deferred until we hear back from intel, which makes sense of course. So it needs HPA's ack.
I hope I'm not splitting this up too much - the reason is I wanted to isolate the code changes (that people might want to test for performance) from comment changes approved by Linus, from (so far unreviewed) changes I came up with myself.
Changes from v2:
add patch adding cc clobber for addl
tweak commit log for patch 2
use addl at SP-4 (as opposed to SP) to reduce data dependencies
Quoted 4 lines
Michael S. Tsirkin (4):
x86: add cc clobber for addl
x86: drop a comment left over fromX86_OOSTORE
x86: tweak the comment about use of wmb for IO
First three look ok to me regardless of what happens with 4. So applied.
Thanks.
Signature · 4 lines
Regards/Gruss,
Boris.
ECO tip #101: Trim your mails when you reply.Michael S. Tsirkin
maintainerQuoted 6 lines · Borislav Petkov
Quoted 4 lines
Michael S. Tsirkin (4):
x86: add cc clobber for addl
x86: drop a comment left over fromX86_OOSTORE
x86: tweak the comment about use of wmb for IOFirst three look ok to me regardless of what happens with 4. So applied.
Sorry - in which tree are these applied?
Thanks,
Signature · 1 line
MST
Boris Petkov
maintainerQuoted 1 line · Michael S. Tsirkin
Sorry - in which tree are these applied?
They'll appear in tip at some point.
Signature · 1 line
Sent from a small device: formatting sux and brevity is inevitable.