[PATCH v1 1/2] x86/retpoline: Add a function to clear the RETPOLINE_AMD feature

From: Tom Lendacky
Date: Tue Jan 09 2018 - 17:37:49 EST


For AMD hardware, the RETPOLINE_AMD feature is dependent on LFENCE being
a serializing instruction. Create a function to allow RETPOLINE_AMD to
be cleared if it cannot be determined that LFENCE is serializing. In
addition, update the spectre_v2_enabled variable so that sysfs output is
correct.

Signed-off-by: Tom Lendacky <thomas.lendacky@xxxxxxx>
---
arch/x86/include/asm/nospec-branch.h | 1 +
arch/x86/kernel/cpu/bugs.c | 10 ++++++++++
2 files changed, 11 insertions(+)

diff --git a/arch/x86/include/asm/nospec-branch.h b/arch/x86/include/asm/nospec-branch.h
index 8ddf851..5785684 100644
--- a/arch/x86/include/asm/nospec-branch.h
+++ b/arch/x86/include/asm/nospec-branch.h
@@ -153,6 +153,7 @@
#endif

void spectre_v2_check_boottime_disable(void);
+void retpoline_amd_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..a4c594c 100644
--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -170,6 +170,16 @@ void __init spectre_v2_check_boottime_disable(void)
return;
}

+void retpoline_amd_disable(void)
+{
+ if (!boot_cpu_has(X86_FEATURE_RETPOLINE_AMD))
+ return;
+
+ setup_clear_cpu_cap(X86_FEATURE_RETPOLINE_AMD);
+ spectre_v2_enabled = retp_compiler() ?
+ SPECTRE_V2_RETPOLINE_GENERIC : SPECTRE_V2_RETPOLINE_MINIMAL;
+}
+
#ifdef CONFIG_SYSFS
ssize_t cpu_show_meltdown(struct device *dev,
struct device_attribute *attr, char *buf)