Skip to content

Commit 98f6288

Browse files
bp3tk0vgregkh
authored andcommitted
x86/srso: Add SRSO_NO support
Upstream commit: 1b5277c Add support for the CPUID flag which denotes that the CPU is not affected by SRSO. Signed-off-by: Borislav Petkov (AMD) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 9139f4b commit 98f6288

File tree

7 files changed

+39
-15
lines changed

7 files changed

+39
-15
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@
426426
#define X86_FEATURE_V_TSC_AUX (19*32+ 9) /* "" Virtual TSC_AUX */
427427
#define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */
428428

429+
#define X86_FEATURE_SBPB (20*32+27) /* "" Selective Branch Prediction Barrier */
429430
#define X86_FEATURE_IBPB_BRTYPE (20*32+28) /* "" MSR_PRED_CMD[IBPB] flushes all branch type predictions */
431+
#define X86_FEATURE_SRSO_NO (20*32+29) /* "" CPU is not affected by SRSO */
430432

431433
/*
432434
* BUG word(s)

arch/x86/include/asm/msr-index.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060

6161
#define MSR_IA32_PRED_CMD 0x00000049 /* Prediction Command */
6262
#define PRED_CMD_IBPB BIT(0) /* Indirect Branch Prediction Barrier */
63+
#define PRED_CMD_SBPB BIT(7) /* Selective Branch Prediction Barrier */
6364

6465
#define MSR_PPIN_CTL 0x0000004e
6566
#define MSR_PPIN 0x0000004f

arch/x86/include/asm/nospec-branch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ void alternative_msr_write(unsigned int msr, u64 val, unsigned int feature)
318318
: "memory");
319319
}
320320

321+
extern u64 x86_pred_cmd;
322+
321323
static inline void indirect_branch_prediction_barrier(void)
322324
{
323-
u64 val = PRED_CMD_IBPB;
324-
325-
alternative_msr_write(MSR_IA32_PRED_CMD, val, X86_FEATURE_USE_IBPB);
325+
alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
326326
}
327327

328328
/* The Intel SPEC CTRL MSR base value cache */

arch/x86/kernel/cpu/amd.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,14 +1249,14 @@ bool cpu_has_ibpb_brtype_microcode(void)
12491249
{
12501250
u8 fam = boot_cpu_data.x86;
12511251

1252-
if (fam == 0x17) {
1253-
/* Zen1/2 IBPB flushes branch type predictions too. */
1252+
/* Zen1/2 IBPB flushes branch type predictions too. */
1253+
if (fam == 0x17)
12541254
return boot_cpu_has(X86_FEATURE_AMD_IBPB);
1255-
} else if (fam == 0x19) {
1255+
/* Poke the MSR bit on Zen3/4 to check its presence. */
1256+
else if (fam == 0x19)
1257+
return !wrmsrl_safe(MSR_IA32_PRED_CMD, PRED_CMD_SBPB);
1258+
else
12561259
return false;
1257-
}
1258-
1259-
return false;
12601260
}
12611261

12621262
static void zenbleed_check_cpu(void *unused)

arch/x86/kernel/cpu/bugs.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
5757
DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
5858
EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
5959

60+
u64 x86_pred_cmd __ro_after_init = PRED_CMD_IBPB;
61+
EXPORT_SYMBOL_GPL(x86_pred_cmd);
62+
6063
static DEFINE_MUTEX(spec_ctrl_mutex);
6164

6265
/* Update SPEC_CTRL MSR and its cached copy unconditionally */
@@ -2354,7 +2357,7 @@ static void __init srso_select_mitigation(void)
23542357
bool has_microcode;
23552358

23562359
if (!boot_cpu_has_bug(X86_BUG_SRSO) || cpu_mitigations_off())
2357-
return;
2360+
goto pred_cmd;
23582361

23592362
/*
23602363
* The first check is for the kernel running as a guest in order
@@ -2367,9 +2370,18 @@ static void __init srso_select_mitigation(void)
23672370
} else {
23682371
/*
23692372
* Enable the synthetic (even if in a real CPUID leaf)
2370-
* flag for guests.
2373+
* flags for guests.
23712374
*/
23722375
setup_force_cpu_cap(X86_FEATURE_IBPB_BRTYPE);
2376+
setup_force_cpu_cap(X86_FEATURE_SBPB);
2377+
2378+
/*
2379+
* Zen1/2 with SMT off aren't vulnerable after the right
2380+
* IBPB microcode has been applied.
2381+
*/
2382+
if ((boot_cpu_data.x86 < 0x19) &&
2383+
(cpu_smt_control == CPU_SMT_DISABLED))
2384+
setup_force_cpu_cap(X86_FEATURE_SRSO_NO);
23732385
}
23742386

23752387
switch (srso_cmd) {
@@ -2392,16 +2404,20 @@ static void __init srso_select_mitigation(void)
23922404
srso_mitigation = SRSO_MITIGATION_SAFE_RET;
23932405
} else {
23942406
pr_err("WARNING: kernel not compiled with CPU_SRSO.\n");
2395-
return;
2407+
goto pred_cmd;
23962408
}
23972409
break;
23982410

23992411
default:
24002412
break;
2401-
24022413
}
24032414

24042415
pr_info("%s%s\n", srso_strings[srso_mitigation], (has_microcode ? "" : ", no microcode"));
2416+
2417+
pred_cmd:
2418+
if (boot_cpu_has(X86_FEATURE_SRSO_NO) ||
2419+
srso_cmd == SRSO_CMD_OFF)
2420+
x86_pred_cmd = PRED_CMD_SBPB;
24052421
}
24062422

24072423
#undef pr_fmt

arch/x86/kernel/cpu/common.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,10 @@ static void __init cpu_set_bug_bits(struct cpuinfo_x86 *c)
14141414
boot_cpu_has(X86_FEATURE_AVX))
14151415
setup_force_cpu_bug(X86_BUG_GDS);
14161416

1417-
if (cpu_matches(cpu_vuln_blacklist, SRSO))
1418-
setup_force_cpu_bug(X86_BUG_SRSO);
1417+
if (!cpu_has(c, X86_FEATURE_SRSO_NO)) {
1418+
if (cpu_matches(cpu_vuln_blacklist, SRSO))
1419+
setup_force_cpu_bug(X86_BUG_SRSO);
1420+
}
14191421

14201422
if (cpu_matches(cpu_vuln_whitelist, NO_MELTDOWN))
14211423
return;

arch/x86/kvm/cpuid.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ void kvm_set_cpu_caps(void)
736736
F(PMM) | F(PMM_EN)
737737
);
738738

739+
if (cpu_feature_enabled(X86_FEATURE_SRSO_NO))
740+
kvm_cpu_cap_set(X86_FEATURE_SRSO_NO);
741+
739742
/*
740743
* Hide RDTSCP and RDPID if either feature is reported as supported but
741744
* probing MSR_TSC_AUX failed. This is purely a sanity check and

0 commit comments

Comments
 (0)