]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
x86/bugs: Use a static branch to guard IBPB on vCPU switch
authorYosry Ahmed <yosry.ahmed@linux.dev>
Thu, 27 Feb 2025 01:27:10 +0000 (01:27 +0000)
committerIngo Molnar <mingo@kernel.org>
Thu, 27 Feb 2025 09:57:20 +0000 (10:57 +0100)
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 <yosry.ahmed@linux.dev>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Acked-by: Josh Poimboeuf <jpoimboe@kernel.org>
Acked-by: Sean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20250227012712.3193063-5-yosry.ahmed@linux.dev
arch/x86/include/asm/nospec-branch.h
arch/x86/kernel/cpu/bugs.c
arch/x86/kvm/svm/svm.c
arch/x86/kvm/vmx/vmx.c

index 7cbb76a2434b9f5b9cbee2e351258ec31579ef7f..36bc395bdef321a863b4b770109845f9f2c607dd 100644 (file)
@@ -552,6 +552,8 @@ DECLARE_STATIC_KEY_FALSE(switch_to_cond_stibp);
 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
 DECLARE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
 
+DECLARE_STATIC_KEY_FALSE(switch_vcpu_ibpb);
+
 DECLARE_STATIC_KEY_FALSE(mds_idle_clear);
 
 DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
index 1d7afc40f2272bcf2838f466dcc6254d522eab1a..7f904d0b0b04fb27354d4cec6c2115fba188320e 100644 (file)
@@ -113,6 +113,10 @@ DEFINE_STATIC_KEY_FALSE(switch_mm_cond_ibpb);
 /* Control unconditional IBPB in switch_mm() */
 DEFINE_STATIC_KEY_FALSE(switch_mm_always_ibpb);
 
+/* Control IBPB on vCPU load */
+DEFINE_STATIC_KEY_FALSE(switch_vcpu_ibpb);
+EXPORT_SYMBOL_GPL(switch_vcpu_ibpb);
+
 /* Control MDS CPU buffer clear before idling (halt, mwait) */
 DEFINE_STATIC_KEY_FALSE(mds_idle_clear);
 EXPORT_SYMBOL_GPL(mds_idle_clear);
@@ -1365,6 +1369,7 @@ spectre_v2_user_select_mitigation(void)
        /* Initialize Indirect Branch Prediction Barrier */
        if (boot_cpu_has(X86_FEATURE_IBPB)) {
                setup_force_cpu_cap(X86_FEATURE_USE_IBPB);
+               static_branch_enable(&switch_vcpu_ibpb);
 
                spectre_v2_user_ibpb = mode;
                switch (cmd) {
index 57222c3b56592bd03d5c46a83685539ec5512efd..a73875ffbc3df14e51709f7ca60007c4fc8d4254 100644 (file)
@@ -1566,7 +1566,7 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
                sd->current_vmcb = svm->vmcb;
 
                if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT) &&
-                   cpu_feature_enabled(X86_FEATURE_USE_IBPB))
+                   static_branch_likely(&switch_vcpu_ibpb))
                        indirect_branch_prediction_barrier();
        }
        if (kvm_vcpu_apicv_active(vcpu))
index 042b7a88157b071b95c363f6ad4e336133a8a7b9..956f9143a3c4ce72380a2cd1096e9fe785486b1c 100644 (file)
@@ -1477,7 +1477,7 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
                 * performs IBPB on nested VM-Exit (a single nested transition
                 * may switch the active VMCS multiple times).
                 */
-               if (cpu_feature_enabled(X86_FEATURE_USE_IBPB) &&
+               if (static_branch_likely(&switch_vcpu_ibpb) &&
                    (!buddy || WARN_ON_ONCE(buddy->vmcs != prev)))
                        indirect_branch_prediction_barrier();
        }