Skip to content

Commit 7cc765a

Browse files
committed
x86/speculation: Enable prctl mode for spectre_v2_user
Now that all prerequisites are in place: - Add the prctl command line option - Default the 'auto' mode to 'prctl' - When SMT state changes, update the static key which controls the conditional STIBP evaluation on context switch. - At init update the static key which controls the conditional IBPB evaluation on context switch. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Tim Chen <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Casey Schaufler <[email protected]> Cc: Asit Mallick <[email protected]> Cc: Arjan van de Ven <[email protected]> Cc: Jon Masters <[email protected]> Cc: Waiman Long <[email protected]> Cc: Greg KH <[email protected]> Cc: Dave Stewart <[email protected]> Cc: Kees Cook <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 9137bb2 commit 7cc765a

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4236,9 +4236,14 @@
42364236
off - Unconditionally disable mitigations. Is
42374237
enforced by spectre_v2=off
42384238

4239+
prctl - Indirect branch speculation is enabled,
4240+
but mitigation can be enabled via prctl
4241+
per thread. The mitigation control state
4242+
is inherited on fork.
4243+
42394244
auto - Kernel selects the mitigation depending on
42404245
the available CPU features and vulnerability.
4241-
Default is off.
4246+
Default is prctl.
42424247

42434248
Not specifying this option is equivalent to
42444249
spectre_v2_user=auto.

arch/x86/kernel/cpu/bugs.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,13 @@ enum spectre_v2_user_cmd {
255255
SPECTRE_V2_USER_CMD_NONE,
256256
SPECTRE_V2_USER_CMD_AUTO,
257257
SPECTRE_V2_USER_CMD_FORCE,
258+
SPECTRE_V2_USER_CMD_PRCTL,
258259
};
259260

260261
static const char * const spectre_v2_user_strings[] = {
261262
[SPECTRE_V2_USER_NONE] = "User space: Vulnerable",
262263
[SPECTRE_V2_USER_STRICT] = "User space: Mitigation: STIBP protection",
264+
[SPECTRE_V2_USER_PRCTL] = "User space: Mitigation: STIBP via prctl",
263265
};
264266

265267
static const struct {
@@ -270,6 +272,7 @@ static const struct {
270272
{ "auto", SPECTRE_V2_USER_CMD_AUTO, false },
271273
{ "off", SPECTRE_V2_USER_CMD_NONE, false },
272274
{ "on", SPECTRE_V2_USER_CMD_FORCE, true },
275+
{ "prctl", SPECTRE_V2_USER_CMD_PRCTL, false },
273276
};
274277

275278
static void __init spec_v2_user_print_cond(const char *reason, bool secure)
@@ -324,12 +327,15 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
324327
smt_possible = false;
325328

326329
switch (spectre_v2_parse_user_cmdline(v2_cmd)) {
327-
case SPECTRE_V2_USER_CMD_AUTO:
328330
case SPECTRE_V2_USER_CMD_NONE:
329331
goto set_mode;
330332
case SPECTRE_V2_USER_CMD_FORCE:
331333
mode = SPECTRE_V2_USER_STRICT;
332334
break;
335+
case SPECTRE_V2_USER_CMD_AUTO:
336+
case SPECTRE_V2_USER_CMD_PRCTL:
337+
mode = SPECTRE_V2_USER_PRCTL;
338+
break;
333339
}
334340

335341
/* Initialize Indirect Branch Prediction Barrier */
@@ -340,6 +346,9 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
340346
case SPECTRE_V2_USER_STRICT:
341347
static_branch_enable(&switch_mm_always_ibpb);
342348
break;
349+
case SPECTRE_V2_USER_PRCTL:
350+
static_branch_enable(&switch_mm_cond_ibpb);
351+
break;
343352
default:
344353
break;
345354
}
@@ -352,6 +361,12 @@ spectre_v2_user_select_mitigation(enum spectre_v2_mitigation_cmd v2_cmd)
352361
if (spectre_v2_enabled == SPECTRE_V2_IBRS_ENHANCED)
353362
return;
354363

364+
/*
365+
* If SMT is not possible or STIBP is not available clear the STIPB
366+
* mode.
367+
*/
368+
if (!smt_possible || !boot_cpu_has(X86_FEATURE_STIBP))
369+
mode = SPECTRE_V2_USER_NONE;
355370
set_mode:
356371
spectre_v2_user = mode;
357372
/* Only print the STIBP mode when SMT possible */
@@ -552,6 +567,15 @@ static void update_stibp_strict(void)
552567
on_each_cpu(update_stibp_msr, NULL, 1);
553568
}
554569

570+
/* Update the static key controlling the evaluation of TIF_SPEC_IB */
571+
static void update_indir_branch_cond(void)
572+
{
573+
if (sched_smt_active())
574+
static_branch_enable(&switch_to_cond_stibp);
575+
else
576+
static_branch_disable(&switch_to_cond_stibp);
577+
}
578+
555579
void arch_smt_update(void)
556580
{
557581
/* Enhanced IBRS implies STIBP. No update required. */
@@ -567,6 +591,7 @@ void arch_smt_update(void)
567591
update_stibp_strict();
568592
break;
569593
case SPECTRE_V2_USER_PRCTL:
594+
update_indir_branch_cond();
570595
break;
571596
}
572597

@@ -1038,22 +1063,20 @@ static char *stibp_state(void)
10381063
case SPECTRE_V2_USER_STRICT:
10391064
return ", STIBP: forced";
10401065
case SPECTRE_V2_USER_PRCTL:
1041-
return "";
1066+
if (static_key_enabled(&switch_to_cond_stibp))
1067+
return ", STIBP: conditional";
10421068
}
10431069
return "";
10441070
}
10451071

10461072
static char *ibpb_state(void)
10471073
{
10481074
if (boot_cpu_has(X86_FEATURE_IBPB)) {
1049-
switch (spectre_v2_user) {
1050-
case SPECTRE_V2_USER_NONE:
1051-
return ", IBPB: disabled";
1052-
case SPECTRE_V2_USER_STRICT:
1075+
if (static_key_enabled(&switch_mm_always_ibpb))
10531076
return ", IBPB: always-on";
1054-
case SPECTRE_V2_USER_PRCTL:
1055-
return "";
1056-
}
1077+
if (static_key_enabled(&switch_mm_cond_ibpb))
1078+
return ", IBPB: conditional";
1079+
return ", IBPB: disabled";
10571080
}
10581081
return "";
10591082
}

0 commit comments

Comments
 (0)