History

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.

Peter Zijlstra committed 4 years ago

Commit c779bc11 parent 2dbb8873 files · +14 −8Browse files

Unified · Split

3 files
  1. arch/x86/include/asm/nospec-branch.h +1 −1
  2. arch/x86/kernel/cpu/bugs.c +12 −6
  3. arch/x86/kernel/process.c +1 −1
arch/x86/include/asm/nospec-branch.h+1−1
@@ -253,7 +253,7 @@ static inline void indirect_branch_prediction_barrier(void) · 252 unmodified lines
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+12−6
@@ -63,13 +63,19 @@ static DEFINE_MUTEX(spec_ctrl_mutex); · 62 unmodified lines
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) · 1221 unmodified lines
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) · 48 unmodified lines
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) · 236 unmodified lines
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) · 244 unmodified lines
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+1−1
@@ -600,7 +600,7 @@ static __always_inline void __speculation_ctrl_update(unsigned long tifp, · 599 unmodified lines
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)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t