x86: Use LOCK ADD instead of MFENCE for smp_mb()

MFENCE is overly heavyweight for SMP semantics on WB memory, because it also
orders weaker cached writes, and flushes the WC buffers.

This technique was used as an optimisation in Java[1], and later adopted by
Linux[2] where it was measured to have a 60% performance improvement in VirtIO
benchmarks.

The stack is used because it is hot in the L1 cache, and a -4 offset is used
to avoid creating a false data dependency on live data.

For 64bit userspace, the Red Zone needs to be considered.  Use -32 to allow
for a reasonable quantity of Red Zone data, but still have a 50% chance of
hitting the same cache line as %rsp.

Fix up the 32 bit definitions in HVMLoader and libxc to avoid a false data
dependency.

[1] https://shipilev.net/blog/2014/on-the-fence-with-dependencies/
[2] https://git.kernel.org/torvalds/c/450cbdd0125cfa5d7bbf9e2a6b6961cc48d29730

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Wei Liu <wl@xen.org>
This commit is contained in:
Andrew Cooper committed 2020-10-01 11:14:22 +01:00
1 parent 707eb41ae2
commit de16a8fa0d
3 files changed
+4 -4

No files matched your search

+1 -1
View File
@@ -133,7 +133,7 @@ static inline void cpu_relax(void)
#define barrier() asm volatile ( "" : : : "memory" )
#define rmb() barrier()
#define wmb() barrier()
#define mb() asm volatile ( "lock; addl $0,0(%%esp)" : : : "memory" )
#define mb() asm volatile ( "lock addl $0, -4(%%esp)" ::: "memory" )
/*
* Divide a 64-bit dividend by a 32-bit divisor.
+2 -2
View File
@@ -68,11 +68,11 @@
#define xen_barrier() asm volatile ( "" : : : "memory")
#if defined(__i386__)
#define xen_mb() asm volatile ( "lock; addl $0,0(%%esp)" : : : "memory" )
#define xen_mb() asm volatile ( "lock addl $0, -4(%%esp)" ::: "memory" )
#define xen_rmb() xen_barrier()
#define xen_wmb() xen_barrier()
#elif defined(__x86_64__)
#define xen_mb() asm volatile ( "mfence" : : : "memory")
#define xen_mb() asm volatile ( "lock addl $0, -32(%%rsp)" ::: "memory" )
#define xen_rmb() xen_barrier()
#define xen_wmb() xen_barrier()
#elif defined(__arm__)
+1 -1
View File
@@ -220,7 +220,7 @@ static always_inline unsigned long __xadd(
*
* Refer to the vendor system programming manuals for further details.
*/
#define smp_mb() mb()
#define smp_mb() asm volatile ( "lock addl $0, -4(%%rsp)" ::: "memory" )
#define smp_rmb() barrier()
#define smp_wmb() barrier()
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.