mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing
@ 2018-01-10  0:39 Tom Lendacky
  2018-01-10  0:51 ` [tip:x86/pti] " tip-bot for Tom Lendacky
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Lendacky @ 2018-01-10  0:39 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Rik van Riel, Andi Kleen, Peter Zijlstra, Linus Torvalds,
	Jiri Kosina, Dan Williams, Dave Hansen, Borislav Petkov,
	Andy Lutomirski, Kees Cook, Thomas Gleixner, Tim Chen,
	Greg Kroah-Hartman, David Woodhouse, Paul Turner

The RETPOLINE_AMD feature relies on a serializing LFENCE for speculation
control.  For AMD hardware, only set RETPOLINE_AMD if LFENCE is a
serializing instruction, which is indicated by the LFENCE_RDTSC feature.

The call to spectre_v2_check_boottime_disable() is currently before the
boot CPU is identified and, therefore, able to set the LFENCE_RDTSC
feature.  Move the call to spectre_v2_check_boottime_disable() to after
identify_boot_cpu() in check_bugs().

Also, protect against specifying spectre_v2=retpoline,amd for non-AMD
hardware and fall-back to the generic retpoline.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
---
 arch/x86/include/asm/nospec-branch.h |    2 --
 arch/x86/kernel/cpu/bugs.c           |   22 +++++++++++++++++-----
 arch/x86/kernel/setup.c              |    2 --
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 8ddf851..6bda2c0 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -152,7 +152,5 @@
 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
 #endif
 
-void spectre_v2_check_boottime_disable(void);
-
 #endif /* __ASSEMBLY__ */
 #endif /* __NOSPEC_BRANCH_H__ */
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index b957f77..815dee2 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -24,6 +24,8 @@
 #include <asm/pgtable.h>
 #include <asm/set_memory.h>
 
+static void __init spectre_v2_check_boottime_disable(void);
+
 void __init check_bugs(void)
 {
 	identify_boot_cpu();
@@ -33,6 +35,9 @@ void __init check_bugs(void)
 		print_cpu_info(&boot_cpu_data);
 	}
 
+	/* Select the proper spectre mitigation before patching alternatives */
+	spectre_v2_check_boottime_disable();
+
 #ifdef CONFIG_X86_32
 	/*
 	 * Check whether we are able to run this kernel safely on SMP.
@@ -106,7 +111,7 @@ static inline bool match_option(const char *arg, int arglen, const char *opt)
 	return len == arglen && !strncmp(arg, opt, len);
 }
 
-void __init spectre_v2_check_boottime_disable(void)
+static void __init spectre_v2_check_boottime_disable(void)
 {
 	char arg[20];
 	int ret;
@@ -148,14 +153,21 @@ void __init spectre_v2_check_boottime_disable(void)
 retpoline:
 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
 	retpoline_amd:
+		if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD ||
+		    !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
+			pr_info("AMD retpoline not supported, fall back to generic\n");
+			goto retpoline_generic;
+		}
+
 		spectre_v2_enabled = retp_compiler() ?
 			SPECTRE_V2_RETPOLINE_AMD : SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
-	} else {
-	retpoline_generic:
-		spectre_v2_enabled = retp_compiler() ?
-			SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL;
+		setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
+		return;
 	}
+retpoline_generic:
+	spectre_v2_enabled = retp_compiler() ?
+		SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL;
 	setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
 	return;
 #else
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 9fb4f9d..b5a908b 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -1322,8 +1322,6 @@ void __init setup_arch(char **cmdline_p)
 
 	register_refined_jiffies(CLOCK_TICK_RATE);
 
-	spectre_v2_check_boottime_disable();
-
 #ifdef CONFIG_EFI
 	if (efi_enabled(EFI_BOOT))
 		efi_apply_memmap_quirks();

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [tip:x86/pti] x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing
  2018-01-10  0:39 [PATCH v2] x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing Tom Lendacky
@ 2018-01-10  0:51 ` tip-bot for Tom Lendacky
  0 siblings, 0 replies; 2+ messages in thread
From: tip-bot for Tom Lendacky @ 2018-01-10  0:51 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: dan.j.williams, gregkh, pjt, riel, dwmw, dave.hansen, luto, hpa,
	thomas.lendacky, jikos, tglx, ak, linux-kernel, mingo, torvalds,
	tim.c.chen, keescook, bp, peterz

Commit-ID:  b10d070a67d96db93223d11832c1e74588d7d566
Gitweb:     https://git.kernel.org/tip/b10d070a67d96db93223d11832c1e74588d7d566
Author:     Tom Lendacky <thomas.lendacky@amd.com>
AuthorDate: Tue, 9 Jan 2018 18:39:31 -0600
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Wed, 10 Jan 2018 01:49:13 +0100

x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing

The RETPOLINE_AMD feature relies on a serializing LFENCE for speculation
control.  For AMD hardware, only set RETPOLINE_AMD if LFENCE is a
serializing instruction, which is indicated by the LFENCE_RDTSC feature.

The call to spectre_v2_check_boottime_disable() is currently before the
boot CPU is identified and, therefore, able to set the LFENCE_RDTSC
feature.  Move the call to spectre_v2_check_boottime_disable() to after
identify_boot_cpu() in check_bugs().

Also, protect against specifying spectre_v2=retpoline,amd for non-AMD
hardware and fall-back to the generic retpoline.

54d5103245ff ("x86/spectre: Add boot time option to select Spectre v2 mitigation")
Originally-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Rik van Riel <riel@redhat.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Jiri Kosina <jikos@kernel.org>
Cc: Dave Hansen <dave.hansen@intel.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Kees Cook <keescook@google.com>
Cc: Dan Williams <dan.j.williams@intel.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Greg Kroah-Hartman <gregkh@linux-foundation.org>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Cc: Paul Turner <pjt@google.com>
Link: https://lkml.kernel.org/r/20180110003931.32411.55646.stgit@tlendack-t1.amdoffice.net
---
 arch/x86/include/asm/nospec-branch.h |  2 --
 arch/x86/kernel/cpu/bugs.c           | 22 +++++++++++++++++-----
 arch/x86/kernel/setup.c              |  2 --
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 8ddf851..6bda2c0 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -152,7 +152,5 @@
 # define THUNK_TARGET(addr) [thunk_target] "rm" (addr)
 #endif
 
-void spectre_v2_check_boottime_disable(void);
-
 #endif /* __ASSEMBLY__ */
 #endif /* __NOSPEC_BRANCH_H__ */
diff --git a/arch/x86/kernel/cpu/bugs.c b/arch/x86/kernel/cpu/bugs.c
index b957f77..815dee2 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -24,6 +24,8 @@
 #include <asm/pgtable.h>
 #include <asm/set_memory.h>
 
+static void __init spectre_v2_check_boottime_disable(void);
+
 void __init check_bugs(void)
 {
 	identify_boot_cpu();
@@ -33,6 +35,9 @@ void __init check_bugs(void)
 		print_cpu_info(&boot_cpu_data);
 	}
 
+	/* Select the proper spectre mitigation before patching alternatives */
+	spectre_v2_check_boottime_disable();
+
 #ifdef CONFIG_X86_32
 	/*
 	 * Check whether we are able to run this kernel safely on SMP.
@@ -106,7 +111,7 @@ static inline bool match_option(const char *arg, int arglen, const char *opt)
 	return len == arglen && !strncmp(arg, opt, len);
 }
 
-void __init spectre_v2_check_boottime_disable(void)
+static void __init spectre_v2_check_boottime_disable(void)
 {
 	char arg[20];
 	int ret;
@@ -148,14 +153,21 @@ force:
 retpoline:
 	if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD) {
 	retpoline_amd:
+		if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD ||
+		    !boot_cpu_has(X86_FEATURE_LFENCE_RDTSC)) {
+			pr_info("AMD retpoline not supported, fall back to generic\n");
+			goto retpoline_generic;
+		}
+
 		spectre_v2_enabled = retp_compiler() ?
 			SPECTRE_V2_RETPOLINE_AMD : SPECTRE_V2_RETPOLINE_MINIMAL_AMD;
 		setup_force_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
-	} else {
-	retpoline_generic:
-		spectre_v2_enabled = retp_compiler() ?
-			SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL;
+		setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
+		return;
 	}
+retpoline_generic:
+	spectre_v2_enabled = retp_compiler() ?
+		SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL;
 	setup_force_cpu_cap(X86_FEATURE_RETPOLINE);
 	return;
 #else
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 9fb4f9d..b5a908b 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -1322,8 +1322,6 @@ void __init setup_arch(char **cmdline_p)
 
 	register_refined_jiffies(CLOCK_TICK_RATE);
 
-	spectre_v2_check_boottime_disable();
-
 #ifdef CONFIG_EFI
 	if (efi_enabled(EFI_BOOT))
 		efi_apply_memmap_quirks();

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-01-10  0:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-10  0:39 [PATCH v2] x86/retpoline: Only set RETPOLINE_AMD if LFENCE is serializing Tom Lendacky
2018-01-10  0:51 ` [tip:x86/pti] " tip-bot for Tom Lendacky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome