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
253 253
254/* The Intel SPEC CTRL MSR base value cache */254/* The Intel SPEC CTRL MSR base value cache */
255extern u64 x86_spec_ctrl_base;255extern u64 x86_spec_ctrl_base;
256extern void write_spec_ctrl_current(u64 val);256extern void write_spec_ctrl_current(u64 val, bool force);
257 257
258/*258/*
259 * With retpoline, we must use IBRS to restrict branch prediction259 * 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
63 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ63 * Keep track of the SPEC_CTRL MSR value for the current task, which may differ
64 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().64 * from x86_spec_ctrl_base due to STIBP/SSB in __speculation_ctrl_update().
65 */65 */
66void write_spec_ctrl_current(u64 val)66void write_spec_ctrl_current(u64 val, bool force)
67{67{
68 if (this_cpu_read(x86_spec_ctrl_current) == val)68 if (this_cpu_read(x86_spec_ctrl_current) == val)
69 return;69 return;
70 70
71 this_cpu_write(x86_spec_ctrl_current, val);71 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);
73}79}
74 80
75/*81/*
@@ -1297,7 +1303,7 @@ static void __init spectre_v2_select_mitigation(void) · 1221 unmodified lines
1297 if (spectre_v2_in_eibrs_mode(mode)) {1303 if (spectre_v2_in_eibrs_mode(mode)) {
1298 /* Force it so VMEXIT will restore correctly */1304 /* Force it so VMEXIT will restore correctly */
1299 x86_spec_ctrl_base |= SPEC_CTRL_IBRS;1305 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);
1301 }1307 }
1302 1308
1303 switch (mode) {1309 switch (mode) {
@@ -1352,7 +1358,7 @@ static void __init spectre_v2_select_mitigation(void) · 48 unmodified lines
1352 1358
1353static void update_stibp_msr(void * __unused)1359static void update_stibp_msr(void * __unused)
1354{1360{
1355 write_spec_ctrl_current(x86_spec_ctrl_base);1361 write_spec_ctrl_current(x86_spec_ctrl_base, true);
1356}1362}
1357 1363
1358/* Update x86_spec_ctrl_base in case SMT state changed. */1364/* 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
1595 x86_amd_ssb_disable();1601 x86_amd_ssb_disable();
1596 } else {1602 } else {
1597 x86_spec_ctrl_base |= SPEC_CTRL_SSBD;1603 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);
1599 }1605 }
1600 }1606 }
1601 1607
@@ -1846,7 +1852,7 @@ int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which) · 244 unmodified lines
1846void x86_spec_ctrl_setup_ap(void)1852void x86_spec_ctrl_setup_ap(void)
1847{1853{
1848 if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))1854 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);
1850 1856
1851 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)1857 if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
1852 x86_amd_ssb_disable();1858 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
600 }600 }
601 601
602 if (updmsr)602 if (updmsr)
603 write_spec_ctrl_current(msr);603 write_spec_ctrl_current(msr, false);
604}604}
605 605
606static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)606static 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