[PATCH 3.2 085/104] x86: Introduce barrier_nospec

2018-03-11 Thread Ben Hutchings
3.2.101-rc1 review patch.  If anyone has any objections, please let me know.

--

From: Dan Williams 

commit b3d7ad85b80bbc404635dca80f5b129f6242bc7a upstream.

Rename the open coded form of this instruction sequence from
rdtsc_ordered() into a generic barrier primitive, barrier_nospec().

One of the mitigations for Spectre variant1 vulnerabilities is to fence
speculative execution after successfully validating a bounds check. I.e.
force the result of a bounds check to resolve in the instruction pipeline
to ensure speculative execution honors that result before potentially
operating on out-of-bounds data.

No functional changes.

Suggested-by: Linus Torvalds 
Suggested-by: Andi Kleen 
Suggested-by: Ingo Molnar 
Signed-off-by: Dan Williams 
Signed-off-by: Thomas Gleixner 
Cc: linux-a...@vger.kernel.org
Cc: Tom Lendacky 
Cc: Kees Cook 
Cc: kernel-harden...@lists.openwall.com
Cc: gre...@linuxfoundation.org
Cc: Al Viro 
Cc: a...@linux.intel.com
Link: 
https://lkml.kernel.org/r/151727415361.33451.9049453007262764675.st...@dwillia2-desk3.amr.corp.intel.com
[bwh: Backported to 3.2: update rdtsc_barrier() instead of rdtsc_ordered()]
Signed-off-by: Ben Hutchings 
---
--- a/arch/x86/include/asm/system.h
+++ b/arch/x86/include/asm/system.h
@@ -478,6 +478,10 @@ static inline unsigned long array_index_
 /* Override the default implementation from linux/nospec.h. */
 #define array_index_mask_nospec array_index_mask_nospec
 
+/* Prevent speculative execution past this barrier. */
+#define barrier_nospec() alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC, 
\
+  "lfence", X86_FEATURE_LFENCE_RDTSC)
+
 /**
  * read_barrier_depends - Flush all pending reads that subsequents reads
  * depend on.
@@ -563,8 +567,7 @@ static inline unsigned long array_index_
  */
 static __always_inline void rdtsc_barrier(void)
 {
-   alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
-   alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
+   barrier_nospec();
 }
 
 /*



[PATCH 3.2 085/104] x86: Introduce barrier_nospec

2018-03-11 Thread Ben Hutchings
3.2.101-rc1 review patch.  If anyone has any objections, please let me know.

--

From: Dan Williams 

commit b3d7ad85b80bbc404635dca80f5b129f6242bc7a upstream.

Rename the open coded form of this instruction sequence from
rdtsc_ordered() into a generic barrier primitive, barrier_nospec().

One of the mitigations for Spectre variant1 vulnerabilities is to fence
speculative execution after successfully validating a bounds check. I.e.
force the result of a bounds check to resolve in the instruction pipeline
to ensure speculative execution honors that result before potentially
operating on out-of-bounds data.

No functional changes.

Suggested-by: Linus Torvalds 
Suggested-by: Andi Kleen 
Suggested-by: Ingo Molnar 
Signed-off-by: Dan Williams 
Signed-off-by: Thomas Gleixner 
Cc: linux-a...@vger.kernel.org
Cc: Tom Lendacky 
Cc: Kees Cook 
Cc: kernel-harden...@lists.openwall.com
Cc: gre...@linuxfoundation.org
Cc: Al Viro 
Cc: a...@linux.intel.com
Link: 
https://lkml.kernel.org/r/151727415361.33451.9049453007262764675.st...@dwillia2-desk3.amr.corp.intel.com
[bwh: Backported to 3.2: update rdtsc_barrier() instead of rdtsc_ordered()]
Signed-off-by: Ben Hutchings 
---
--- a/arch/x86/include/asm/system.h
+++ b/arch/x86/include/asm/system.h
@@ -478,6 +478,10 @@ static inline unsigned long array_index_
 /* Override the default implementation from linux/nospec.h. */
 #define array_index_mask_nospec array_index_mask_nospec
 
+/* Prevent speculative execution past this barrier. */
+#define barrier_nospec() alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC, 
\
+  "lfence", X86_FEATURE_LFENCE_RDTSC)
+
 /**
  * read_barrier_depends - Flush all pending reads that subsequents reads
  * depend on.
@@ -563,8 +567,7 @@ static inline unsigned long array_index_
  */
 static __always_inline void rdtsc_barrier(void)
 {
-   alternative(ASM_NOP3, "mfence", X86_FEATURE_MFENCE_RDTSC);
-   alternative(ASM_NOP3, "lfence", X86_FEATURE_LFENCE_RDTSC);
+   barrier_nospec();
 }
 
 /*