Skip to content

Commit caa0ff2

Browse files
Peter Zijlstrasuryasaimadhu
authored andcommitted
x86/bugs: Keep a per-CPU IA32_SPEC_CTRL value
Due to TIF_SSBD and TIF_SPEC_IB the actual IA32_SPEC_CTRL value can differ from x86_spec_ctrl_base. As such, keep a per-CPU value reflecting the current task's MSR content. [jpoimboe: rename] 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 e8ec1b6 commit caa0ff2

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +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);
256257

257258
/*
258259
* With retpoline, we must use IBRS to restrict branch prediction

arch/x86/kernel/cpu/bugs.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,29 @@ static void __init mmio_select_mitigation(void);
4949
static void __init srbds_select_mitigation(void);
5050
static void __init l1d_flush_select_mitigation(void);
5151

52-
/* The base value of the SPEC_CTRL MSR that always has to be preserved. */
52+
/* The base value of the SPEC_CTRL MSR without task-specific bits set */
5353
u64 x86_spec_ctrl_base;
5454
EXPORT_SYMBOL_GPL(x86_spec_ctrl_base);
55+
56+
/* The current value of the SPEC_CTRL MSR with task-specific bits set */
57+
DEFINE_PER_CPU(u64, x86_spec_ctrl_current);
58+
EXPORT_SYMBOL_GPL(x86_spec_ctrl_current);
59+
5560
static DEFINE_MUTEX(spec_ctrl_mutex);
5661

62+
/*
63+
* 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().
65+
*/
66+
void write_spec_ctrl_current(u64 val)
67+
{
68+
if (this_cpu_read(x86_spec_ctrl_current) == val)
69+
return;
70+
71+
this_cpu_write(x86_spec_ctrl_current, val);
72+
wrmsrl(MSR_IA32_SPEC_CTRL, val);
73+
}
74+
5775
/*
5876
* The vendor and possibly platform specific bits which can be modified in
5977
* x86_spec_ctrl_base.
@@ -1279,7 +1297,7 @@ static void __init spectre_v2_select_mitigation(void)
12791297
if (spectre_v2_in_eibrs_mode(mode)) {
12801298
/* Force it so VMEXIT will restore correctly */
12811299
x86_spec_ctrl_base |= SPEC_CTRL_IBRS;
1282-
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1300+
write_spec_ctrl_current(x86_spec_ctrl_base);
12831301
}
12841302

12851303
switch (mode) {
@@ -1334,7 +1352,7 @@ static void __init spectre_v2_select_mitigation(void)
13341352

13351353
static void update_stibp_msr(void * __unused)
13361354
{
1337-
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1355+
write_spec_ctrl_current(x86_spec_ctrl_base);
13381356
}
13391357

13401358
/* Update x86_spec_ctrl_base in case SMT state changed. */
@@ -1577,7 +1595,7 @@ static enum ssb_mitigation __init __ssb_select_mitigation(void)
15771595
x86_amd_ssb_disable();
15781596
} else {
15791597
x86_spec_ctrl_base |= SPEC_CTRL_SSBD;
1580-
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1598+
write_spec_ctrl_current(x86_spec_ctrl_base);
15811599
}
15821600
}
15831601

@@ -1828,7 +1846,7 @@ int arch_prctl_spec_ctrl_get(struct task_struct *task, unsigned long which)
18281846
void x86_spec_ctrl_setup_ap(void)
18291847
{
18301848
if (boot_cpu_has(X86_FEATURE_MSR_SPEC_CTRL))
1831-
wrmsrl(MSR_IA32_SPEC_CTRL, x86_spec_ctrl_base);
1849+
write_spec_ctrl_current(x86_spec_ctrl_base);
18321850

18331851
if (ssb_mode == SPEC_STORE_BYPASS_DISABLE)
18341852
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-
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
603+
write_spec_ctrl_current(msr);
604604
}
605605

606606
static unsigned long speculation_ctrl_update_tif(struct task_struct *tsk)

0 commit comments

Comments
 (0)