Skip to content

Commit 80dacb0

Browse files
yosrym93Ingo Molnar
authored andcommitted
x86/bugs: Use a static branch to guard IBPB on vCPU switch
Instead of using X86_FEATURE_USE_IBPB to guard the IBPB execution in KVM when a new vCPU is loaded, introduce a static branch, similar to switch_mm_*_ibpb. This makes it obvious in spectre_v2_user_select_mitigation() what exactly is being toggled, instead of the unclear X86_FEATURE_USE_IBPB (which will be shortly removed). It also provides more fine-grained control, making it simpler to change/add paths that control the IBPB in the vCPU switch path without affecting other IBPBs. Signed-off-by: Yosry Ahmed <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Acked-by: Josh Poimboeuf <[email protected]> Acked-by: Sean Christopherson <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent bd9a854 commit 80dacb0

File tree

4 files changed

+9
-2
lines changed

4 files changed

+9
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
552552
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
553553
DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
554554

555+
DECLARE_STATIC_KEY_FALSE(switch_vcpu_ibpb);
556+
555557
DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
556558

557559
DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);

arch/x86/kernel/cpu/bugs.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
113113
/* Control unconditional IBPB in switch_mm() */
114114
DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
115115

116+
/* Control IBPB on vCPU load */
117+
DEFINE_STATIC_KEY_FALSE(switch_vcpu_ibpb);
118+
EXPORT_SYMBOL_GPL(switch_vcpu_ibpb);
119+
116120
/* Control MDS CPU buffer clear before idling (halt, mwait) */
117121
DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
118122
EXPORT_SYMBOL_GPL(mds_idle_clear);
@@ -1365,6 +1369,7 @@ spectre_v2_user_select_mitigation(void)
13651369
/* Initialize Indirect Branch Prediction Barrier */
13661370
if (boot_cpu_has(X86_FEATURE_IBPB)) {
13671371
setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
1372+
static_branch_enable(&switch_vcpu_ibpb);
13681373

13691374
spectre_v2_user_ibpb = mode;
13701375
switch (cmd) {

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
15661566
sd->current_vmcb = svm->vmcb;
15671567

15681568
if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT) &&
1569-
cpu_feature_enabled(X86_FEATURE_USE_IBPB))
1569+
static_branch_likely(&switch_vcpu_ibpb))
15701570
indirect_branch_prediction_barrier();
15711571
}
15721572
if (kvm_vcpu_apicv_active(vcpu))

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
14771477
* performs IBPB on nested VM-Exit (a single nested transition
14781478
* may switch the active VMCS multiple times).
14791479
*/
1480-
if (cpu_feature_enabled(X86_FEATURE_USE_IBPB) &&
1480+
if (static_branch_likely(&switch_vcpu_ibpb) &&
14811481
(!buddy || WARN_ON_ONCE(buddy->vmcs != prev)))
14821482
indirect_branch_prediction_barrier();
14831483
}

0 commit comments

Comments
 (0)