Skip to content

Commit c779bc1

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
x86/bugs: Optimize SPEC_CTRL MSR writes
When changing SPEC_CTRL for user control, the WRMSR can be delayed until return-to-user when KERNEL_IBRS has been enabled. This avoids an MSR write during context switch. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Josh Poimboeuf <jpoimboe@kernel.org> Signed-off-by: Borislav Petkov <bp@suse.de>
1 parent 2dbb887 commit c779bc1

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static inline void indirect_branch_prediction_barrier(void)
253253

254254
/* The Intel SPEC CTRL MSR base value cache */
255255
extern u64 x86_spec_ctrl_base;
256-
extern void write_spec_ctrl_current(u64 val);
256+
extern void write_spec_ctrl_current(u64 val, bool force);
257257

258258
/*
259259
* With retpoline, we must use IBRS to restrict branch prediction

arch/x86/kernel/cpu/bugs.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,19 @@ static DEFINE_MUTEX(spec_ctrl_mutex);
6363
* Keep track of the SPEC_CTRL MSR value for the current task, which may differ
6464
* from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
6565
*/
66-
void write_spec_ctrl_current(u64 val)
66+
void write_spec_ctrl_current(u64 val, bool force)
6767
{
6868
if (this_cpu_read(x86_spec_ctrl_current) == val)
6969
return;
7070

7171
this_cpu_write(x86_spec_ctrl_current, val);
72-
wrmsrl(MSR_IA32_SPEC_CTRL, val);
72+
73+
/*
74+
* When KERNEL_IBRS this MSR is written on return-to-user, unless
75+
* forced the update can be delayed until that time.
76+
*/
77+
if (force || !cpu_feature_enabled(X86_FEATURE_KERNEL_IBRS))
78+
wrmsrl(MSR_IA32_SPEC_CTRL, val);
7379
}
7480

7581
/*
@@ -1297,7 +1303,7 @@ static void __init spectre_v2_select_mitigation(void)
12971303
if (spectre_v2_in_eibrs_mode(mode)) {
12981304
/* Force it so VMEXIT will restore correctly */
12991305
x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1300-
write_spec_ctrl_current(x86_spec_ctrl_base);
1306+
write_spec_ctrl_current(x86_spec_ctrl_base, true);
13011307
}
13021308

13031309
switch (mode) {
@@ -1352,7 +1358,7 @@ static void __init spectre_v2_select_mitigation(void)
13521358

13531359
static void update_stibp_msr(void * __unused)
13541360
{
1355-
write_spec_ctrl_current(x86_spec_ctrl_base);
1361+
write_spec_ctrl_current(x86_spec_ctrl_base, true);
13561362
}
13571363

13581364
/* Update x86_spec_ctrl_base in case SMT state changed. */
@@ -1595,7 +1601,7 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
15951601
x86_amd_ssb_disable();
15961602
} else {
15971603
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1598-
write_spec_ctrl_current(x86_spec_ctrl_base);
1604+
write_spec_ctrl_current(x86_spec_ctrl_base, true);
15991605
}
16001606
}
16011607

@@ -1846,7 +1852,7 @@ int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
18461852
void x86_spec_ctrl_setup_ap(void)
18471853
{
18481854
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1849-
write_spec_ctrl_current(x86_spec_ctrl_base);
1855+
write_spec_ctrl_current(x86_spec_ctrl_base, true);
18501856

18511857
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
18521858
x86_amd_ssb_disable();

arch/x86/kernel/process.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static __always_inline void __speculation_ctrl_update(unsigned long tifp,
600600
}
601601

602602
if (updmsr)
603-
write_spec_ctrl_current(msr);
603+
write_spec_ctrl_current(msr, false);
604604
}
605605

606606
static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)

0 commit comments

Comments
 (0)