Skip to content

Commit 01daf56

Browse files
pdxChenKAGA-KOKO
authored andcommitted
x86/speculation: Reorganize speculation control MSRs update
The logic to detect whether there's a change in the previous and next task's flag relevant to update speculation control MSRs is spread out across multiple functions. Consolidate all checks needed for updating speculation control MSRs into the new __speculation_ctrl_update() helper function. This makes it easy to pick the right speculation control MSR and the bits in MSR_IA32_SPEC_CTRL that need updating based on TIF flags changes. Originally-by: Thomas Lendacky <[email protected]> Signed-off-by: Tim Chen <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Tom Lendacky <[email protected]> Cc: Josh Poimboeuf <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: David Woodhouse <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Dave Hansen <[email protected]> Cc: Casey Schaufler <[email protected]> Cc: Asit Mallick <[email protected]> Cc: Arjan van de Ven <[email protected]> Cc: Jon Masters <[email protected]> Cc: Waiman Long <[email protected]> Cc: Greg KH <[email protected]> Cc: Dave Stewart <[email protected]> Cc: Kees Cook <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected]
1 parent 26c4d75 commit 01daf56

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

arch/x86/kernel/process.c

+29-17
Original file line numberDiff line numberDiff line change
@@ -395,27 +395,40 @@ static __always_inline void amd_set_ssb_virt_state(unsigned long tifn)
395395
wrmsrl(MSR_AMD64_VIRT_SPEC_CTRL, ssbd_tif_to_spec_ctrl(tifn));
396396
}
397397

398-
static __always_inline void spec_ctrl_update_msr(unsigned long tifn)
399-
{
400-
u64 msr = x86_spec_ctrl_base | ssbd_tif_to_spec_ctrl(tifn);
401-
402-
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
403-
}
398+
/*
399+
* Update the MSRs managing speculation control, during context switch.
400+
*
401+
* tifp: Previous task's thread flags
402+
* tifn: Next task's thread flags
403+
*/
404+
static __always_inline void __speculation_ctrl_update(unsigned long tifp,
405+
unsigned long tifn)
406+
{
407+
u64 msr = x86_spec_ctrl_base;
408+
bool updmsr = false;
409+
410+
/* If TIF_SSBD is different, select the proper mitigation method */
411+
if ((tifp ^ tifn) & _TIF_SSBD) {
412+
if (static_cpu_has(X86_FEATURE_VIRT_SSBD)) {
413+
amd_set_ssb_virt_state(tifn);
414+
} else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD)) {
415+
amd_set_core_ssb_state(tifn);
416+
} else if (static_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) ||
417+
static_cpu_has(X86_FEATURE_AMD_SSBD)) {
418+
msr |= ssbd_tif_to_spec_ctrl(tifn);
419+
updmsr = true;
420+
}
421+
}
404422

405-
static __always_inline void __speculation_ctrl_update(unsigned long tifn)
406-
{
407-
if (static_cpu_has(X86_FEATURE_VIRT_SSBD))
408-
amd_set_ssb_virt_state(tifn);
409-
else if (static_cpu_has(X86_FEATURE_LS_CFG_SSBD))
410-
amd_set_core_ssb_state(tifn);
411-
else
412-
spec_ctrl_update_msr(tifn);
423+
if (updmsr)
424+
wrmsrl(MSR_IA32_SPEC_CTRL, msr);
413425
}
414426

415427
void speculation_ctrl_update(unsigned long tif)
416428
{
429+
/* Forced update. Make sure all relevant TIF flags are different */
417430
preempt_disable();
418-
__speculation_ctrl_update(tif);
431+
__speculation_ctrl_update(~tif, tif);
419432
preempt_enable();
420433
}
421434

@@ -451,8 +464,7 @@ void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
451464
if ((tifp ^ tifn) & _TIF_NOCPUID)
452465
set_cpuid_faulting(!!(tifn & _TIF_NOCPUID));
453466

454-
if ((tifp ^ tifn) & _TIF_SSBD)
455-
__speculation_ctrl_update(tifn);
467+
__speculation_ctrl_update(tifp, tifn);
456468
}
457469

458470
/*

0 commit comments

Comments
 (0)