Skip to content

Commit 549435a

Browse files
yosrym93Ingo Molnar
authored andcommitted
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. 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 8442df2 commit 549435a

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ extern u64 x86_pred_cmd;
515515

516516
static inline void indirect_branch_prediction_barrier(void)
517517
{
518-
alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_USE_IBPB);
518+
alternative_msr_write(MSR_IA32_PRED_CMD, x86_pred_cmd, X86_FEATURE_IBPB);
519519
}
520520

521521
/* The Intel SPEC CTRL MSR base value cache */

arch/x86/kernel/cpu/bugs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,7 @@ static int ib_prctl_set(struct task_struct *task, unsigned long ctrl)
22722272
if (ctrl == PR_SPEC_FORCE_DISABLE)
22732273
task_set_spec_ib_force_disable(task);
22742274
task_update_spec_tif(task);
2275-
if (task == current)
2275+
if (task == current && cpu_feature_enabled(X86_FEATURE_USE_IBPB))
22762276
indirect_branch_prediction_barrier();
22772277
break;
22782278
default:

arch/x86/kvm/svm/svm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,8 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
15651565
if (sd->current_vmcb != svm->vmcb) {
15661566
sd->current_vmcb = svm->vmcb;
15671567

1568-
if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT))
1568+
if (!cpu_feature_enabled(X86_FEATURE_IBPB_ON_VMEXIT) &&
1569+
cpu_feature_enabled(X86_FEATURE_USE_IBPB))
15691570
indirect_branch_prediction_barrier();
15701571
}
15711572
if (kvm_vcpu_apicv_active(vcpu))

arch/x86/kvm/vmx/nested.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5026,7 +5026,8 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason,
50265026
* doesn't isolate different VMCSs, i.e. in this case, doesn't provide
50275027
* separate modes for L2 vs L1.
50285028
*/
5029-
if (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL))
5029+
if (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
5030+
cpu_feature_enabled(X86_FEATURE_USE_IBPB))
50305031
indirect_branch_prediction_barrier();
50315032

50325033
/* Update any VMCS fields that might have changed while L2 ran */

arch/x86/kvm/vmx/vmx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,8 @@ 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 (!buddy || WARN_ON_ONCE(buddy->vmcs != prev))
1480+
if (cpu_feature_enabled(X86_FEATURE_USE_IBPB) &&
1481+
(!buddy || WARN_ON_ONCE(buddy->vmcs != prev)))
14811482
indirect_branch_prediction_barrier();
14821483
}
14831484

arch/x86/mm/tlb.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ static void cond_mitigation(struct task_struct *next)
437437
* both have the IBPB bit set.
438438
*/
439439
if (next_mm != prev_mm &&
440-
(next_mm | prev_mm) & LAST_USER_MM_IBPB)
440+
(next_mm | prev_mm) & LAST_USER_MM_IBPB &&
441+
cpu_feature_enabled(X86_FEATURE_USE_IBPB))
441442
indirect_branch_prediction_barrier();
442443
}
443444

@@ -447,8 +448,8 @@ static void cond_mitigation(struct task_struct *next)
447448
* different context than the user space task which ran
448449
* last on this CPU.
449450
*/
450-
if ((prev_mm & ~LAST_USER_MM_SPEC_MASK) !=
451-
(unsigned long)next->mm)
451+
if ((prev_mm & ~LAST_USER_MM_SPEC_MASK) != (unsigned long)next->mm &&
452+
cpu_feature_enabled(X86_FEATURE_USE_IBPB))
452453
indirect_branch_prediction_barrier();
453454
}
454455

0 commit comments

Comments
 (0)