IBPB cleanups and a fixup
v2 cover IBPB cleanups and a fixup
This series removes X86_FEATURE_USE_IBPB, and fixes a KVM nVMX bug in the process. The motivation is mostly the confusing name of X86_FEATURE_USE_IBPB, which sounds like it controls IBPBs in general, but it only controls IBPBs for spectre_v2_mitigation. A side effect of this confusion is the nVMX bug, where virtualizing IBRS correctly depends on the spectre_v2_user mitigation.
The feature bit is mostly redundant, except in controlling the IBPB in the vCPU load path. For that, a separate static branch is introduced, similar to switch_mm_*_ibpb.
We should also unify indirect_branch_prediction_barrier() with entry_ibpb() to have a single IBPB primitive that always stuffs the RSB if needed. Josh will be sending follow up changes for that.
v1 -> v2:
- Collected Ack/Review tags (thanks everyone!).
- Combined the if statements in vmx_vcpu_load_vmcs() and moved the static branch check ahead (Sean Christopherson).
- Renamed the static branch to switch_vcpu_ibpb (Sean Christopherson).
- Fixed a typo in the commit message (Jim Mattson).
Yosry Ahmed (6):
x86/bugs: Move the X86_FEATURE_USE_IBPB check into callers x86/mm: Remove X86_FEATURE_USE_IBPB checks in cond_mitigation() x86/bugs: Remove the X86_FEATURE_USE_IBPB check in ib_prctl_set() x86/bugs: Use a static branch to guard IBPB on vCPU switch
KVM: nVMX: Always use IBPB to properly virtualize IBRS x86/bugs: Remove X86_FEATURE_USE_IBPB
arch/x86/include/asm/cpufeatures.h | 1 - arch/x86/include/asm/nospec-branch.h | 4 +++- arch/x86/kernel/cpu/bugs.c | 6 +++++- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 3 ++- arch/x86/mm/tlb.c | 3 +-- tools/arch/x86/include/asm/cpufeatures.h | 1 - 7 files changed, 13 insertions(+), 8 deletions(-)
Signature · 1 line
2.48.1.658.g4767266eb4-goog
PATCH v2 1/6 x86/bugs: Move the X86_FEATURE_USE_IBPB check into callers
indirect_branch_prediction_barrier() only performs the MSR write if X86_FEATURE_USE_IBPB is set, using alternative_msr_write(). In preparation for removing X86_FEATURE_USE_IBPB, move the feature check into the callers so that they can be addressed one-by-one, and use X86_FEATURE_IBPB instead to guard the MSR write.
--- arch/x86/include/asm/nospec-branch.h | 2 +- arch/x86/kernel/cpu/bugs.c | 2 +- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/vmx/nested.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 3 ++- arch/x86/mm/tlb.c | 7 ++++--- 6 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/include/asm/nospec-branch.h +1 -1
@@ -515,7 +515,7 @@ extern u64 x86_pred_cmd;static inline void indirect_branch_prediction_barrier (void ) { -alternative_msr_write (MSR_IA32_PRED_CMD , x86_pred_cmd,X86_FEATURE_USE_IBPB ); +alternative_msr_write (MSR_IA32_PRED_CMD , x86_pred_cmd,X86_FEATURE_IBPB ); }/* The Intel SPEC CTRL MSR base value cache */
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -2272,7 +2272,7 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)if (ctrl ==PR_SPEC_FORCE_DISABLE )task_set_spec_ib_force_disable (task);task_update_spec_tif (task); -if (task == current) +if (task == current &&cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier ();break ;default :
arch/x86/kvm/svm/svm.c +2 -1
@@ -1565,7 +1565,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)if (sd->current_vmcb != svm->vmcb) { sd->current_vmcb = svm->vmcb; -if (!cpu_feature_enabled (X86_FEATURE_IBPB_ON_VMEXIT )) +if (!cpu_feature_enabled (X86_FEATURE_IBPB_ON_VMEXIT ) && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }if (kvm_vcpu_apicv_active (vcpu))
arch/x86/kvm/vmx/nested.c +2 -1
@@ -5026,7 +5026,8 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,* doesn't isolate different VMCSs, i.e. in this case, doesn't provide * separate modes for L2 vs L1. */ -if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL )) +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ) && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier ();/* Update any VMCS fields that might have changed while L2 ran */
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -1477,7 +1477,8 @@ 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 (!buddy ||WARN_ON_ONCE (buddy->vmcs != prev)) +if (cpu_feature_enabled (X86_FEATURE_USE_IBPB ) && + (!buddy ||WARN_ON_ONCE (buddy->vmcs != prev)))indirect_branch_prediction_barrier (); }
arch/x86/mm/tlb.c +4 -3
@@ -437,7 +437,8 @@ static void cond_mitigation(struct task_struct *next)* both have the IBPB bit set. */ if (next_mm != prev_mm && - (next_mm | prev_mm) &LAST_USER_MM_IBPB ) + (next_mm | prev_mm) &LAST_USER_MM_IBPB && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }
@@ -447,8 +448,8 @@ static void cond_mitigation(struct task_struct *next)* different context than the user space task which ran * last on this CPU. */ -if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != - (unsigned long )next->mm) +if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }
Signature · 1 line
2.48.1.658.g4767266eb4-goog
Re: [PATCH v2 1/6] x86/bugs: Move the X86_FEATURE_USE_IBPB check into callers
Quoted 9 lines · Yosry Ahmed
indirect_branch_prediction_barrier()only performs the MSR write ifX86_FEATURE_USE_IBPBis set, usingalternative_msr_write(). In preparation for removingX86_FEATURE_USE_IBPB, move the feature check into the callers so that they can be addressed one-by-one, and useX86_FEATURE_IBPBinstead to guard the MSR write.Signed-off-by Yosry Ahmed <[email protected]>Acked-by Josh Poimboeuf <[email protected]>---
[tip: x86/bugs] x86/bugs: Move the X86_FEATURE_USE_IBPB check into callers
In reply to Yosry AhmedThe following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: 549435aab49ae83d60a08795de6cf0e866f3b2ec Gitweb: https://git.kernel.org/tip/549435aab49ae83d60a08795de6cf0e866f3b2ec Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:07 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:20 +01:00
x86/bugs: Move the X86_FEATURE_USE_IBPB check into callers
indirect_branch_prediction_barrier() only performs the MSR write if X86_FEATURE_USE_IBPB is set, using alternative_msr_write(). In preparation for removing X86_FEATURE_USE_IBPB, move the feature check into the callers so that they can be addressed one-by-one, and use X86_FEATURE_IBPB instead to guard the MSR write.
--- arch/x86/include/asm/nospec-branch.h | 2 +- arch/x86/kernel/cpu/bugs.c | 2 +- arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/vmx/nested.c | 3 ++- arch/x86/kvm/vmx/vmx.c | 3 ++- arch/x86/mm/tlb.c | 7 ++++--- 6 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/include/asm/nospec-branch.h +1 -1
@@ -515,7 +515,7 @@ extern u64 x86_pred_cmd;static inline void indirect_branch_prediction_barrier (void ) { -alternative_msr_write (MSR_IA32_PRED_CMD , x86_pred_cmd,X86_FEATURE_USE_IBPB ); +alternative_msr_write (MSR_IA32_PRED_CMD , x86_pred_cmd,X86_FEATURE_IBPB ); }/* The Intel SPEC CTRL MSR base value cache */
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -2272,7 +2272,7 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)if (ctrl ==PR_SPEC_FORCE_DISABLE )task_set_spec_ib_force_disable (task);task_update_spec_tif (task); -if (task == current) +if (task == current &&cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier ();break ;default :
arch/x86/kvm/svm/svm.c +2 -1
@@ -1565,7 +1565,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)if (sd->current_vmcb != svm->vmcb) { sd->current_vmcb = svm->vmcb; -if (!cpu_feature_enabled (X86_FEATURE_IBPB_ON_VMEXIT )) +if (!cpu_feature_enabled (X86_FEATURE_IBPB_ON_VMEXIT ) && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }if (kvm_vcpu_apicv_active (vcpu))
arch/x86/kvm/vmx/nested.c +2 -1
@@ -5026,7 +5026,8 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,* doesn't isolate different VMCSs, i.e. in this case, doesn't provide * separate modes for L2 vs L1. */ -if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL )) +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ) && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier ();/* Update any VMCS fields that might have changed while L2 ran */
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -1477,7 +1477,8 @@ 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 (!buddy ||WARN_ON_ONCE (buddy->vmcs != prev)) +if (cpu_feature_enabled (X86_FEATURE_USE_IBPB ) && + (!buddy ||WARN_ON_ONCE (buddy->vmcs != prev)))indirect_branch_prediction_barrier (); }
arch/x86/mm/tlb.c +4 -3
@@ -437,7 +437,8 @@ static void cond_mitigation(struct task_struct *next)* both have the IBPB bit set. */ if (next_mm != prev_mm && - (next_mm | prev_mm) &LAST_USER_MM_IBPB ) + (next_mm | prev_mm) &LAST_USER_MM_IBPB && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }
@@ -447,8 +448,8 @@ static void cond_mitigation(struct task_struct *next)* different context than the user space task which ran * last on this CPU. */ -if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != - (unsigned long )next->mm) +if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm && +cpu_feature_enabled (X86_FEATURE_USE_IBPB ))indirect_branch_prediction_barrier (); }
PATCH v2 2/6 x86/mm: Remove X86_FEATURE_USE_IBPB checks in cond_mitigation()
In reply to Yosry AhmedThe check is performed when either switch_mm_cond_ibpb or switch_mm_always_ibpb is set. In both cases, X86_FEATURE_USE_IBPB is always set. Remove the redundant check.
--- arch/x86/mm/tlb.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/mm/tlb.c +2 -4
@@ -437,8 +437,7 @@ static void cond_mitigation(struct task_struct *next)* both have the IBPB bit set. */ if (next_mm != prev_mm && - (next_mm | prev_mm) &LAST_USER_MM_IBPB && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) + (next_mm | prev_mm) &LAST_USER_MM_IBPB )indirect_branch_prediction_barrier (); }
@@ -448,8 +447,7 @@ static void cond_mitigation(struct task_struct *next)* different context than the user space task which ran * last on this CPU. */ -if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm)indirect_branch_prediction_barrier (); }
Signature · 1 line
2.48.1.658.g4767266eb4-goog
[tip: x86/bugs] x86/mm: Remove X86_FEATURE_USE_IBPB checks in cond_mitigation()
The following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: a48dc42614cad39fac1ef55d690847fd07f0e3c6 Gitweb: https://git.kernel.org/tip/a48dc42614cad39fac1ef55d690847fd07f0e3c6 Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:08 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:20 +01:00
x86/mm: Remove X86_FEATURE_USE_IBPB checks in cond_mitigation()
The check is performed when either switch_mm_cond_ibpb or switch_mm_always_ibpb is set. In both cases, X86_FEATURE_USE_IBPB is always set. Remove the redundant check.
--- arch/x86/mm/tlb.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
arch/x86/mm/tlb.c +2 -4
@@ -437,8 +437,7 @@ static void cond_mitigation(struct task_struct *next)* both have the IBPB bit set. */ if (next_mm != prev_mm && - (next_mm | prev_mm) &LAST_USER_MM_IBPB && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) + (next_mm | prev_mm) &LAST_USER_MM_IBPB )indirect_branch_prediction_barrier (); }
@@ -448,8 +447,7 @@ static void cond_mitigation(struct task_struct *next)* different context than the user space task which ran * last on this CPU. */ -if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if ((prev_mm & ~LAST_USER_MM_SPEC_MASK ) != (unsigned long )next->mm)indirect_branch_prediction_barrier (); }
PATCH v2 3/6 x86/bugs: Remove the X86_FEATURE_USE_IBPB check in ib_prctl_set()
In reply to Yosry AhmedIf X86_FEATURE_USE_IBPB is not set, then both spectre_v2_user_ibpb and spectre_v2_user_stibp are set to SPECTRE_V2_USER_NONE in spectre_v2_user_select_mitigation(). Since ib_prctl_set() already checks for this before performing the IBPB, the X86_FEATURE_USE_IBPB check is redundant. Remove it.
--- arch/x86/kernel/cpu/bugs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -2272,7 +2272,7 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)if (ctrl ==PR_SPEC_FORCE_DISABLE )task_set_spec_ib_force_disable (task);task_update_spec_tif (task); -if (task == current &&cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if (task == current)indirect_branch_prediction_barrier ();break ;default :
Signature · 1 line
2.48.1.658.g4767266eb4-goog
[tip: x86/bugs] x86/bugs: Remove the X86_FEATURE_USE_IBPB check in ib_prctl_set()
The following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: bd9a8542ceccce1b1d5d5fd5e47be57fe42f9bb9 Gitweb: https://git.kernel.org/tip/bd9a8542ceccce1b1d5d5fd5e47be57fe42f9bb9 Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:09 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:20 +01:00
x86/bugs: Remove the X86_FEATURE_USE_IBPB check in ib_prctl_set()
If X86_FEATURE_USE_IBPB is not set, then both spectre_v2_user_ibpb and spectre_v2_user_stibp are set to SPECTRE_V2_USER_NONE in spectre_v2_user_select_mitigation(). Since ib_prctl_set() already checks for this before performing the IBPB, the X86_FEATURE_USE_IBPB check is redundant. Remove it.
--- arch/x86/kernel/cpu/bugs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/bugs.c +1 -1
@@ -2272,7 +2272,7 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)if (ctrl ==PR_SPEC_FORCE_DISABLE )task_set_spec_ib_force_disable (task);task_update_spec_tif (task); -if (task == current &&cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if (task == current)indirect_branch_prediction_barrier ();break ;default :
PATCH v2 4/6 x86/bugs: Use a static branch to guard IBPB on vCPU switch
In reply to Yosry AhmedInstead 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.
--- arch/x86/include/asm/nospec-branch.h | 2 ++ arch/x86/kernel/cpu/bugs.c | 5 +++++ arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/include/asm/nospec-branch.h +2 -0
@@ -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);
arch/x86/kernel/cpu/bugs.c +5 -0
@@ -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) {
arch/x86/kvm/svm/svm.c +1 -1
@@ -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))
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -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 (); }
Signature · 1 line
2.48.1.658.g4767266eb4-goog
Re: [PATCH v2 4/6] x86/bugs: Use a static branch to guard IBPB on vCPU switch
Quoted 10 lines · Yosry Ahmed
Instead of using
X86_FEATURE_USE_IBPBto 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 unclearX86_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.
Quoted 3 lines
Signed-off-by Yosry Ahmed <[email protected]>Acked-by Josh Poimboeuf <[email protected]>---
[tip: x86/bugs] x86/bugs: Use a static branch to guard IBPB on vCPU switch
In reply to Yosry AhmedThe following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: 80dacb080461edfc1d854721ee6933a4cfa3b602 Gitweb: https://git.kernel.org/tip/80dacb080461edfc1d854721ee6933a4cfa3b602 Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:10 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:20 +01:00
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.
--- arch/x86/include/asm/nospec-branch.h | 2 ++ arch/x86/kernel/cpu/bugs.c | 5 +++++ arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- 4 files changed, 9 insertions(+), 2 deletions(-)
arch/x86/include/asm/nospec-branch.h +2 -0
@@ -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);
arch/x86/kernel/cpu/bugs.c +5 -0
@@ -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) {
arch/x86/kvm/svm/svm.c +1 -1
@@ -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))
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -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 (); }
PATCH v2 5/6 KVM: nVMX: Always use IBPB to properly virtualize IBRS
In reply to Yosry AhmedOn synthesized nested VM-exits in VMX, an IBPB is performed if IBRS is advertised to the guest to properly provide separate prediction domains for L1 and L2. However, this is currently conditional on X86_FEATURE_USE_IBPB, which depends on the host spectre_v2_user mitigation.
In short, if spectre_v2_user=no, IBRS is not virtualized correctly and L1 becomes susceptible to attacks from L2. Fix this by performing the IBPB regardless of X86_FEATURE_USE_IBPB.
--- arch/x86/kvm/vmx/nested.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -2
@@ -5026,8 +5026,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,* doesn't isolate different VMCSs, i.e. in this case, doesn't provide * separate modes for L2 vs L1. */ -if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ) && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ))indirect_branch_prediction_barrier ();/* Update any VMCS fields that might have changed while L2 ran */
Signature · 1 line
2.48.1.658.g4767266eb4-goog
[tip: x86/bugs] KVM: nVMX: Always use IBPB to properly virtualize IBRS
The following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: 8c4f28cd81fe86033918eec69d5280b532c05842 Gitweb: https://git.kernel.org/tip/8c4f28cd81fe86033918eec69d5280b532c05842 Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:11 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:21 +01:00
KVM: nVMX: Always use IBPB to properly virtualize IBRS
On synthesized nested VM-exits in VMX, an IBPB is performed if IBRS is advertised to the guest to properly provide separate prediction domains for L1 and L2. However, this is currently conditional on X86_FEATURE_USE_IBPB, which depends on the host spectre_v2_user mitigation.
In short, if spectre_v2_user=no, IBRS is not virtualized correctly and L1 becomes susceptible to attacks from L2. Fix this by performing the IBPB regardless of X86_FEATURE_USE_IBPB.
--- arch/x86/kvm/vmx/nested.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/vmx/nested.c +1 -2
@@ -5026,8 +5026,7 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,* doesn't isolate different VMCSs, i.e. in this case, doesn't provide * separate modes for L2 vs L1. */ -if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ) && -cpu_feature_enabled (X86_FEATURE_USE_IBPB )) +if (guest_cpu_cap_has (vcpu,X86_FEATURE_SPEC_CTRL ))indirect_branch_prediction_barrier ();/* Update any VMCS fields that might have changed while L2 ran */
PATCH v2 6/6 x86/bugs: Remove X86_FEATURE_USE_IBPB
In reply to Yosry AhmedX86_FEATURE_USE_IBPB was introduced in commit 2961298efe1e ("x86/cpufeatures: Clean up Spectre v2 related CPUID flags") to have separate flags for when the CPU supports IBPB (i.e. X86_FEATURE_IBPB) and when an IBPB is actually used to mitigate Spectre v2.
Ever since then, the uses of IBPB expanded. The name became confusing because it does not control all IBPB executions in the kernel. Furthermore, because its name is generic and it's buried within indirect_branch_prediction_barrier(), it's easy to use it not knowing that it is specific to Spectre v2.
X86_FEATURE_USE_IBPB is no longer needed because all the IBPB executions it used to control are now controlled through other means (e.g. switch_mm_*_ibpb static branches). Remove the unused feature bit.
--- arch/x86/include/asm/cpufeatures.h | 1 - arch/x86/kernel/cpu/bugs.c | 1 - tools/arch/x86/include/asm/cpufeatures.h | 1 - 3 files changed, 3 deletions(-)
arch/x86/include/asm/cpufeatures.h +0 -1
@@ -210,7 +210,6 @@#define X86_FEATURE_MBA ( 7*32+18)/* "mba" Memory Bandwidth Allocation */ #define X86_FEATURE_RSB_CTXSW ( 7*32+19)/* Fill RSB on context switches */ #define X86_FEATURE_PERFMON_V2 ( 7*32+20)/* "perfmon_v2" AMD Performance Monitoring Version 2 */ -#define X86_FEATURE_USE_IBPB ( 7*32+21)/* Indirect Branch Prediction Barrier enabled */ #define X86_FEATURE_USE_IBRS_FW ( 7*32+22)/* Use IBRS during runtime firmware calls */ #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23)/* Disable Speculative Store Bypass. */ #define X86_FEATURE_LS_CFG_SSBD ( 7*32+24)/* AMD SSBD implementation via LS_CFG MSR */
arch/x86/kernel/cpu/bugs.c +0 -1
@@ -1368,7 +1368,6 @@ 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;
tools/arch/x86/include/asm/cpufeatures.h +0 -1
@@ -210,7 +210,6 @@#define X86_FEATURE_MBA ( 7*32+18)/* "mba" Memory Bandwidth Allocation */ #define X86_FEATURE_RSB_CTXSW ( 7*32+19)/* Fill RSB on context switches */ #define X86_FEATURE_PERFMON_V2 ( 7*32+20)/* "perfmon_v2" AMD Performance Monitoring Version 2 */ -#define X86_FEATURE_USE_IBPB ( 7*32+21)/* Indirect Branch Prediction Barrier enabled */ #define X86_FEATURE_USE_IBRS_FW ( 7*32+22)/* Use IBRS during runtime firmware calls */ #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23)/* Disable Speculative Store Bypass. */ #define X86_FEATURE_LS_CFG_SSBD ( 7*32+24)/* AMD SSBD implementation via LS_CFG MSR */
Signature · 1 line
2.48.1.658.g4767266eb4-goog
[tip: x86/bugs] x86/bugs: Remove X86_FEATURE_USE_IBPB
The following commit has been merged into the x86/bugs branch of tip:
The following commit has been merged into the x86/bugs branch of tip:
Commit-ID: 8f64eee70cdd3bb8c3ec7d30f0d1f52922aaef7c Gitweb: https://git.kernel.org/tip/8f64eee70cdd3bb8c3ec7d30f0d1f52922aaef7c Author: Yosry Ahmed <[email protected]> AuthorDate: Thu, 27 Feb 2025 01:27:12 Committer: Ingo Molnar <[email protected]> CommitterDate: Thu, 27 Feb 2025 10:57:21 +01:00
x86/bugs: Remove X86_FEATURE_USE_IBPB
X86_FEATURE_USE_IBPB was introduced in:
2961298efe1e ("x86/cpufeatures: Clean up Spectre v2 related CPUID flags")
to have separate flags for when the CPU supports IBPB (i.e. X86_FEATURE_IBPB) and when an IBPB is actually used to mitigate Spectre v2.
Ever since then, the uses of IBPB expanded. The name became confusing because it does not control all IBPB executions in the kernel. Furthermore, because its name is generic and it's buried within indirect_branch_prediction_barrier(), it's easy to use it not knowing that it is specific to Spectre v2.
X86_FEATURE_USE_IBPB is no longer needed because all the IBPB executions it used to control are now controlled through other means (e.g. switch_mm_*_ibpb static branches).
Remove the unused feature bit.
--- arch/x86/include/asm/cpufeatures.h | 1 - arch/x86/kernel/cpu/bugs.c | 1 - tools/arch/x86/include/asm/cpufeatures.h | 1 - 3 files changed, 3 deletions(-)
arch/x86/include/asm/cpufeatures.h +0 -1
@@ -210,7 +210,6 @@#define X86_FEATURE_MBA ( 7*32+18)/* "mba" Memory Bandwidth Allocation */ #define X86_FEATURE_RSB_CTXSW ( 7*32+19)/* Fill RSB on context switches */ #define X86_FEATURE_PERFMON_V2 ( 7*32+20)/* "perfmon_v2" AMD Performance Monitoring Version 2 */ -#define X86_FEATURE_USE_IBPB ( 7*32+21)/* Indirect Branch Prediction Barrier enabled */ #define X86_FEATURE_USE_IBRS_FW ( 7*32+22)/* Use IBRS during runtime firmware calls */ #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23)/* Disable Speculative Store Bypass. */ #define X86_FEATURE_LS_CFG_SSBD ( 7*32+24)/* AMD SSBD implementation via LS_CFG MSR */
arch/x86/kernel/cpu/bugs.c +0 -1
@@ -1368,7 +1368,6 @@ 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;
tools/arch/x86/include/asm/cpufeatures.h +0 -1
@@ -210,7 +210,6 @@#define X86_FEATURE_MBA ( 7*32+18)/* "mba" Memory Bandwidth Allocation */ #define X86_FEATURE_RSB_CTXSW ( 7*32+19)/* Fill RSB on context switches */ #define X86_FEATURE_PERFMON_V2 ( 7*32+20)/* "perfmon_v2" AMD Performance Monitoring Version 2 */ -#define X86_FEATURE_USE_IBPB ( 7*32+21)/* Indirect Branch Prediction Barrier enabled */ #define X86_FEATURE_USE_IBRS_FW ( 7*32+22)/* Use IBRS during runtime firmware calls */ #define X86_FEATURE_SPEC_STORE_BYPASS_DISABLE ( 7*32+23)/* Disable Speculative Store Bypass. */ #define X86_FEATURE_LS_CFG_SSBD ( 7*32+24)/* AMD SSBD implementation via LS_CFG MSR */