Skip to content

Commit 6f0f2d5

Browse files
tlendackybonzini
authored andcommitted
KVM: x86: Mitigate the cross-thread return address predictions bug
By default, KVM/SVM will intercept attempts by the guest to transition out of C0. However, the KVM_CAP_X86_DISABLE_EXITS capability can be used by a VMM to change this behavior. To mitigate the cross-thread return address predictions bug (X86_BUG_SMT_RSB), a VMM must not be allowed to override the default behavior to intercept C0 transitions. Use a module parameter to control the mitigation on processors that are vulnerable to X86_BUG_SMT_RSB. If the processor is vulnerable to the X86_BUG_SMT_RSB bug and the module parameter is set to mitigate the bug, KVM will not allow the disabling of the HLT, MWAIT and CSTATE exits. Signed-off-by: Tom Lendacky <thomas.lendacky@amd.com> Message-Id: <4019348b5e07148eb4d593380a5f6713b93c9a16.1675956146.git.thomas.lendacky@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent be8de49 commit 6f0f2d5

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

arch/x86/kvm/x86.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ module_param(enable_pmu, bool, 0444);
191191
bool __read_mostly eager_page_split = true;
192192
module_param(eager_page_split, bool, 0644);
193193

194+
/* Enable/disable SMT_RSB bug mitigation */
195+
bool __read_mostly mitigate_smt_rsb;
196+
module_param(mitigate_smt_rsb, bool, 0444);
197+
194198
/*
195199
* Restoring the host value for MSRs that are only consumed when running in
196200
* usermode, e.g. SYSCALL MSRs and TSC_AUX, can be deferred until the CPU
@@ -4448,10 +4452,15 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
44484452
r = KVM_CLOCK_VALID_FLAGS;
44494453
break;
44504454
case KVM_CAP_X86_DISABLE_EXITS:
4451-
r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
4452-
KVM_X86_DISABLE_EXITS_CSTATE;
4453-
if(kvm_can_mwait_in_guest())
4454-
r |= KVM_X86_DISABLE_EXITS_MWAIT;
4455+
r = KVM_X86_DISABLE_EXITS_PAUSE;
4456+
4457+
if (!mitigate_smt_rsb) {
4458+
r |= KVM_X86_DISABLE_EXITS_HLT |
4459+
KVM_X86_DISABLE_EXITS_CSTATE;
4460+
4461+
if (kvm_can_mwait_in_guest())
4462+
r |= KVM_X86_DISABLE_EXITS_MWAIT;
4463+
}
44554464
break;
44564465
case KVM_CAP_X86_SMM:
44574466
if (!IS_ENABLED(CONFIG_KVM_SMM))
@@ -6227,15 +6236,26 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
62276236
if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
62286237
break;
62296238

6230-
if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
6231-
kvm_can_mwait_in_guest())
6232-
kvm->arch.mwait_in_guest = true;
6233-
if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
6234-
kvm->arch.hlt_in_guest = true;
62356239
if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
62366240
kvm->arch.pause_in_guest = true;
6237-
if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
6238-
kvm->arch.cstate_in_guest = true;
6241+
6242+
#define SMT_RSB_MSG "This processor is affected by the Cross-Thread Return Predictions vulnerability. " \
6243+
"KVM_CAP_X86_DISABLE_EXITS should only be used with SMT disabled or trusted guests."
6244+
6245+
if (!mitigate_smt_rsb) {
6246+
if (boot_cpu_has_bug(X86_BUG_SMT_RSB) && cpu_smt_possible() &&
6247+
(cap->args[0] & ~KVM_X86_DISABLE_EXITS_PAUSE))
6248+
pr_warn_once(SMT_RSB_MSG);
6249+
6250+
if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
6251+
kvm_can_mwait_in_guest())
6252+
kvm->arch.mwait_in_guest = true;
6253+
if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
6254+
kvm->arch.hlt_in_guest = true;
6255+
if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
6256+
kvm->arch.cstate_in_guest = true;
6257+
}
6258+
62396259
r = 0;
62406260
break;
62416261
case KVM_CAP_MSR_PLATFORM_INFO:
@@ -13456,6 +13476,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit);
1345613476
static int __init kvm_x86_init(void)
1345713477
{
1345813478
kvm_mmu_x86_module_init();
13479+
mitigate_smt_rsb &= boot_cpu_has_bug(X86_BUG_SMT_RSB) && cpu_smt_possible();
1345913480
return 0;
1346013481
}
1346113482
module_init(kvm_x86_init);

0 commit comments

Comments
 (0)