Skip to content

Commit 7cafe9b

Browse files
committed
KVM: nSVM: Check instead of asserting on nested TSC scaling support
Check for nested TSC scaling support on nested SVM VMRUN instead of asserting that TSC scaling is exposed to L1 if L1's MSR_AMD64_TSC_RATIO has diverged from KVM's default. Userspace can trigger the WARN at will by writing the MSR and then updating guest CPUID to hide the feature (modifying guest CPUID is allowed anytime before KVM_RUN). E.g. hacking KVM's state_test selftest to do vcpu_set_msr(vcpu, MSR_AMD64_TSC_RATIO, 0); vcpu_clear_cpuid_feature(vcpu, X86_FEATURE_TSCRATEMSR); after restoring state in a new VM+vCPU yields an endless supply of: ------------[ cut here ]------------ WARNING: CPU: 164 PID: 62565 at arch/x86/kvm/svm/nested.c:699 nested_vmcb02_prepare_control+0x3d6/0x3f0 [kvm_amd] Call Trace: <TASK> enter_svm_guest_mode+0x114/0x560 [kvm_amd] nested_svm_vmrun+0x260/0x330 [kvm_amd] vmrun_interception+0x29/0x30 [kvm_amd] svm_invoke_exit_handler+0x35/0x100 [kvm_amd] svm_handle_exit+0xe7/0x180 [kvm_amd] kvm_arch_vcpu_ioctl_run+0x1eab/0x2570 [kvm] kvm_vcpu_ioctl+0x4c9/0x5b0 [kvm] __se_sys_ioctl+0x7a/0xc0 __x64_sys_ioctl+0x21/0x30 do_syscall_64+0x41/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x45ca1b Note, the nested #VMEXIT path has the same flaw, but needs a different fix and will be handled separately. Fixes: 5228eb9 ("KVM: x86: nSVM: implement nested TSC scaling") Cc: Maxim Levitsky <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 99b6685 commit 7cafe9b

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

arch/x86/kvm/svm/nested.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,9 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
695695

696696
vmcb02->control.tsc_offset = vcpu->arch.tsc_offset;
697697

698-
if (svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio) {
699-
WARN_ON(!svm->tsc_scaling_enabled);
698+
if (svm->tsc_scaling_enabled &&
699+
svm->tsc_ratio_msr != kvm_caps.default_tsc_scaling_ratio)
700700
nested_svm_update_tsc_ratio_msr(vcpu);
701-
}
702701

703702
vmcb02->control.int_ctl =
704703
(svm->nested.ctl.int_ctl & int_ctl_vmcb12_bits) |

0 commit comments

Comments
 (0)