Skip to content

Commit 9005c68

Browse files
KarimAllah AhmedKAGA-KOKO
authored andcommitted
x86/spectre: Simplify spectre_v2 command line parsing
[dwmw2: Use ARRAY_SIZE] Signed-off-by: KarimAllah Ahmed <[email protected]> Signed-off-by: David Woodhouse <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 66f7930 commit 9005c68

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

arch/x86/kernel/cpu/bugs.c

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ static inline const char *spectre_v2_module_string(void) { return ""; }
119119
static void __init spec2_print_if_insecure(const char *reason)
120120
{
121121
if (boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
122-
pr_info("%s\n", reason);
122+
pr_info("%s selected on command line.\n", reason);
123123
}
124124

125125
static void __init spec2_print_if_secure(const char *reason)
126126
{
127127
if (!boot_cpu_has_bug(X86_BUG_SPECTRE_V2))
128-
pr_info("%s\n", reason);
128+
pr_info("%s selected on command line.\n", reason);
129129
}
130130

131131
static inline bool retp_compiler(void)
@@ -140,42 +140,68 @@ static inline bool match_option(const char *arg, int arglen, const char *opt)
140140
return len == arglen && !strncmp(arg, opt, len);
141141
}
142142

143+
static const struct {
144+
const char *option;
145+
enum spectre_v2_mitigation_cmd cmd;
146+
bool secure;
147+
} mitigation_options[] = {
148+
{ "off", SPECTRE_V2_CMD_NONE, false },
149+
{ "on", SPECTRE_V2_CMD_FORCE, true },
150+
{ "retpoline", SPECTRE_V2_CMD_RETPOLINE, false },
151+
{ "retpoline,amd", SPECTRE_V2_CMD_RETPOLINE_AMD, false },
152+
{ "retpoline,generic", SPECTRE_V2_CMD_RETPOLINE_GENERIC, false },
153+
{ "auto", SPECTRE_V2_CMD_AUTO, false },
154+
};
155+
143156
static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void)
144157
{
145158
char arg[20];
146-
int ret;
147-
148-
ret = cmdline_find_option(boot_command_line, "spectre_v2", arg,
149-
sizeof(arg));
150-
if (ret > 0) {
151-
if (match_option(arg, ret, "off")) {
152-
goto disable;
153-
} else if (match_option(arg, ret, "on")) {
154-
spec2_print_if_secure("force enabled on command line.");
155-
return SPECTRE_V2_CMD_FORCE;
156-
} else if (match_option(arg, ret, "retpoline")) {
157-
spec2_print_if_insecure("retpoline selected on command line.");
158-
return SPECTRE_V2_CMD_RETPOLINE;
159-
} else if (match_option(arg, ret, "retpoline,amd")) {
160-
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
161-
pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
162-
return SPECTRE_V2_CMD_AUTO;
163-
}
164-
spec2_print_if_insecure("AMD retpoline selected on command line.");
165-
return SPECTRE_V2_CMD_RETPOLINE_AMD;
166-
} else if (match_option(arg, ret, "retpoline,generic")) {
167-
spec2_print_if_insecure("generic retpoline selected on command line.");
168-
return SPECTRE_V2_CMD_RETPOLINE_GENERIC;
169-
} else if (match_option(arg, ret, "auto")) {
159+
int ret, i;
160+
enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_CMD_AUTO;
161+
162+
if (cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
163+
return SPECTRE_V2_CMD_NONE;
164+
else {
165+
ret = cmdline_find_option(boot_command_line, "spectre_v2", arg,
166+
sizeof(arg));
167+
if (ret < 0)
168+
return SPECTRE_V2_CMD_AUTO;
169+
170+
for (i = 0; i < ARRAY_SIZE(mitigation_options); i++) {
171+
if (!match_option(arg, ret, mitigation_options[i].option))
172+
continue;
173+
cmd = mitigation_options[i].cmd;
174+
break;
175+
}
176+
177+
if (i >= ARRAY_SIZE(mitigation_options)) {
178+
pr_err("unknown option (%s). Switching to AUTO select\n",
179+
mitigation_options[i].option);
170180
return SPECTRE_V2_CMD_AUTO;
171181
}
172182
}
173183

174-
if (!cmdline_find_option_bool(boot_command_line, "nospectre_v2"))
184+
if ((cmd == SPECTRE_V2_CMD_RETPOLINE ||
185+
cmd == SPECTRE_V2_CMD_RETPOLINE_AMD ||
186+
cmd == SPECTRE_V2_CMD_RETPOLINE_GENERIC) &&
187+
!IS_ENABLED(CONFIG_RETPOLINE)) {
188+
pr_err("%s selected but not compiled in. Switching to AUTO select\n",
189+
mitigation_options[i].option);
175190
return SPECTRE_V2_CMD_AUTO;
176-
disable:
177-
spec2_print_if_insecure("disabled on command line.");
178-
return SPECTRE_V2_CMD_NONE;
191+
}
192+
193+
if (cmd == SPECTRE_V2_CMD_RETPOLINE_AMD &&
194+
boot_cpu_data.x86_vendor != X86_VENDOR_AMD) {
195+
pr_err("retpoline,amd selected but CPU is not AMD. Switching to AUTO select\n");
196+
return SPECTRE_V2_CMD_AUTO;
197+
}
198+
199+
if (mitigation_options[i].secure)
200+
spec2_print_if_secure(mitigation_options[i].option);
201+
else
202+
spec2_print_if_insecure(mitigation_options[i].option);
203+
204+
return cmd;
179205
}
180206

181207
/* Check for Skylake-like CPUs (for RSB handling) */

0 commit comments

Comments
 (0)