[patch RFC 2/5] x86/spectre: Simplify spectre code a bit

From: Thomas Gleixner
Date: Tue Jan 09 2018 - 20:22:11 EST


Make the sysfs print use a string array, use __defined(RETPOLINE) instead
of the ugly ifdefs.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
arch/x86/kernel/cpu/bugs.c | 31 +++++++++++++------------------
1 file changed, 13 insertions(+), 18 deletions(-)

--- a/arch/x86/kernel/cpu/bugs.c
+++ b/arch/x86/kernel/cpu/bugs.c
@@ -78,10 +78,18 @@ enum spectre_v2_mitigation {
SPECTRE_V2_RETPOLINE_AMD,
};

+static const char *spectre_v2_strings[] = {
+ [SPECTRE_V2_NONE] = "Vulnerable",
+ [SPECTRE_V2_RETPOLINE_MINIMAL] = "Mitigation: Minimal generic ASM retpoline",
+ [SPECTRE_V2_RETPOLINE_MINIMAL_AMD] = "Mitigation: Minimal AMD ASM retpoline",
+ [SPECTRE_V2_RETPOLINE_GENERIC] = "Mitigation: Full generic retpoline",
+ [SPECTRE_V2_RETPOLINE_AMD] = "Mitigation: Full AMD retpoline",
+};
+
#undef pr_fmt
#define pr_fmt(fmt) "Spectre V2 mitigation: " fmt

-static int spectre_v2_enabled = SPECTRE_V2_NONE;
+static enum spectre_v2_mitigation spectre_v2_enabled = SPECTRE_V2_NONE;

static void __init spec2_print_if_insecure(const char *reason)
{
@@ -97,11 +105,7 @@ static void __init spec2_print_if_secure

static inline bool retp_compiler(void)
{
-#ifdef RETPOLINE
- return true;
-#else
- return false;
-#endif
+ return __is_defined(RETPOLINE);
}

static inline bool match_option(const char *arg, int arglen, const char *opt)
@@ -182,6 +186,8 @@ static void __init spectre_v2_check_boot
return;
}

+#undef pr_fmt
+
#ifdef CONFIG_SYSFS
ssize_t cpu_show_meltdown(struct device *dev,
struct device_attribute *attr, char *buf)
@@ -207,17 +213,6 @@ ssize_t cpu_show_spectre_v2(struct devic
if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
return sprintf(buf, "Not affected\n");

- switch (spectre_v2_enabled) {
- case SPECTRE_V2_RETPOLINE_MINIMAL:
- return sprintf(buf, "Mitigation: Minimal generic ASM retpoline\n");
- case SPECTRE_V2_RETPOLINE_MINIMAL_AMD:
- return sprintf(buf, "Mitigation: Minimal AMD ASM retpoline\n");
- case SPECTRE_V2_RETPOLINE_GENERIC:
- return sprintf(buf, "Mitigation: Full generic retpoline\n");
- case SPECTRE_V2_RETPOLINE_AMD:
- return sprintf(buf, "Mitigation: Full AMD retpoline\n");
- default:
- return sprintf(buf, "Vulnerable\n");
- }
+ return sprintf(buf, "%s\n", spectre_v2_strings[spectre_v2_enabled]);
}
#endif