Skip to content

Commit 764f3c2

Browse files
konradwilkKAGA-KOKO
authored andcommitted
x86/bugs/AMD: Add support to disable RDS on Fam[15,16,17]h if requested
AMD does not need the Speculative Store Bypass mitigation to be enabled. The parameters for this are already available and can be done via MSR C001_1020. Each family uses a different bit in that MSR for this. [ tglx: Expose the bit mask via a variable and move the actual MSR fiddling into the bugs code as that's the right thing to do and also required to prepare for dynamic enable/disable ] Suggested-by: Borislav Petkov <[email protected]> Signed-off-by: Konrad Rzeszutek Wilk <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]>
1 parent 1115a85 commit 764f3c2

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
#define X86_FEATURE_USE_IBPB ( 7*32+21) /* "" Indirect Branch Prediction Barrier enabled */
216216
#define X86_FEATURE_USE_IBRS_FW ( 7*32+22) /* "" Use IBRS during runtime firmware calls */
217217
#define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23) /* "" Disable Speculative Store Bypass. */
218+
#define X86_FEATURE_AMD_RDS (7*32+24) /* "" AMD RDS implementation */
218219

219220
/* Virtualization flags: Linux defined, word 8 */
220221
#define X86_FEATURE_TPR_SHADOW ( 8*32+ 0) /* Intel TPR Shadow */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ enum ssb_mitigation {
244244
SPEC_STORE_BYPASS_DISABLE,
245245
};
246246

247+
/* AMD specific Speculative Store Bypass MSR data */
248+
extern u64 x86_amd_ls_cfg_base;
249+
extern u64 x86_amd_ls_cfg_rds_mask;
250+
247251
extern char __indirect_thunk_start[];
248252
extern char __indirect_thunk_end[];
249253

arch/x86/kernel/cpu/amd.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <asm/processor.h>
1111
#include <asm/apic.h>
1212
#include <asm/cpu.h>
13+
#include <asm/nospec-branch.h>
1314
#include <asm/smp.h>
1415
#include <asm/pci-direct.h>
1516
#include <asm/delay.h>
@@ -554,6 +555,26 @@ static void bsp_init_amd(struct cpuinfo_x86 *c)
554555
rdmsrl(MSR_FAM10H_NODE_ID, value);
555556
nodes_per_socket = ((value >> 3) & 7) + 1;
556557
}
558+
559+
if (c->x86 >= 0x15 && c->x86 <= 0x17) {
560+
unsigned int bit;
561+
562+
switch (c->x86) {
563+
case 0x15: bit = 54; break;
564+
case 0x16: bit = 33; break;
565+
case 0x17: bit = 10; break;
566+
default: return;
567+
}
568+
/*
569+
* Try to cache the base value so further operations can
570+
* avoid RMW. If that faults, do not enable RDS.
571+
*/
572+
if (!rdmsrl_safe(MSR_AMD64_LS_CFG, &x86_amd_ls_cfg_base)) {
573+
setup_force_cpu_cap(X86_FEATURE_RDS);
574+
setup_force_cpu_cap(X86_FEATURE_AMD_RDS);
575+
x86_amd_ls_cfg_rds_mask = 1ULL << bit;
576+
}
577+
}
557578
}
558579

559580
static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)
@@ -898,6 +919,11 @@ static void init_amd(struct cpuinfo_x86 *c)
898919
/* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */
899920
if (!cpu_has(c, X86_FEATURE_XENPV))
900921
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
922+
923+
if (boot_cpu_has(X86_FEATURE_AMD_RDS)) {
924+
set_cpu_cap(c, X86_FEATURE_RDS);
925+
set_cpu_cap(c, X86_FEATURE_AMD_RDS);
926+
}
901927
}
902928

903929
#ifdef CONFIG_X86_32

arch/x86/kernel/cpu/bugs.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ static u64 __ro_after_init x86_spec_ctrl_base;
4141
*/
4242
static u64 __ro_after_init x86_spec_ctrl_mask = ~SPEC_CTRL_IBRS;
4343

44+
/*
45+
* AMD specific MSR info for Speculative Store Bypass control.
46+
* x86_amd_ls_cfg_rds_mask is initialized in identify_boot_cpu().
47+
*/
48+
u64 __ro_after_init x86_amd_ls_cfg_base;
49+
u64 __ro_after_init x86_amd_ls_cfg_rds_mask;
50+
4451
void __init check_bugs(void)
4552
{
4653
identify_boot_cpu();
@@ -52,7 +59,8 @@ void __init check_bugs(void)
5259

5360
/*
5461
* Read the SPEC_CTRL MSR to account for reserved bits which may
55-
* have unknown values.
62+
* have unknown values. AMD64_LS_CFG MSR is cached in the early AMD
63+
* init code as it is not enumerated and depends on the family.
5664
*/
5765
if (boot_cpu_has(X86_FEATURE_IBRS))
5866
rdmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
@@ -154,6 +162,14 @@ void x86_spec_ctrl_restore_host(u64 guest_spec_ctrl)
154162
}
155163
EXPORT_SYMBOL_GPL(x86_spec_ctrl_restore_host);
156164

165+
static void x86_amd_rds_enable(void)
166+
{
167+
u64 msrval = x86_amd_ls_cfg_base | x86_amd_ls_cfg_rds_mask;
168+
169+
if (boot_cpu_has(X86_FEATURE_AMD_RDS))
170+
wrmsrl(MSR_AMD64_LS_CFG, msrval);
171+
}
172+
157173
#ifdef RETPOLINE
158174
static bool spectre_v2_bad_module;
159175

@@ -443,6 +459,11 @@ static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
443459

444460
switch (cmd) {
445461
case SPEC_STORE_BYPASS_CMD_AUTO:
462+
/*
463+
* AMD platforms by default don't need SSB mitigation.
464+
*/
465+
if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
466+
break;
446467
case SPEC_STORE_BYPASS_CMD_ON:
447468
mode = SPEC_STORE_BYPASS_DISABLE;
448469
break;
@@ -469,6 +490,7 @@ static enum ssb_mitigation_cmd __init __ssb_select_mitigation(void)
469490
x86_spec_ctrl_set(SPEC_CTRL_RDS);
470491
break;
471492
case X86_VENDOR_AMD:
493+
x86_amd_rds_enable();
472494
break;
473495
}
474496
}
@@ -490,6 +512,9 @@ void x86_spec_ctrl_setup_ap(void)
490512
{
491513
if (boot_cpu_has(X86_FEATURE_IBRS))
492514
x86_spec_ctrl_set(x86_spec_ctrl_base & ~x86_spec_ctrl_mask);
515+
516+
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
517+
x86_amd_rds_enable();
493518
}
494519

495520
#ifdef CONFIG_SYSFS

arch/x86/kernel/cpu/common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,10 @@ static const __initconst struct x86_cpu_id cpu_no_spec_store_bypass[] = {
943943
{ X86_VENDOR_CENTAUR, 5, },
944944
{ X86_VENDOR_INTEL, 5, },
945945
{ X86_VENDOR_NSC, 5, },
946+
{ X86_VENDOR_AMD, 0x12, },
947+
{ X86_VENDOR_AMD, 0x11, },
948+
{ X86_VENDOR_AMD, 0x10, },
949+
{ X86_VENDOR_AMD, 0xf, },
946950
{ X86_VENDOR_ANY, 4, },
947951
{}
948952
};

0 commit comments

Comments
 (0)