History

KVM: x86: SVM: fix nested PAUSE filtering when L0 intercepts PAUSE

Commit 74fd41ed16fd ("KVM: x86: nSVM: support PAUSE filtering when L0 doesn't intercept PAUSE") introduced passthrough support for nested pause filtering, (when the host doesn't intercept PAUSE) (either disabled with kvm module param, or disabled with '-overcommit cpu-pm=on') Before this commit, L1 KVM didn't intercept PAUSE at all; afterwards, the feature was exposed as supported by KVM cpuid unconditionally, thus if L1 could try to use it even when the L0 KVM can't really support it. In this case the fallback caused KVM to intercept each PAUSE instruction; in some cases, such intercept can slow down the nested guest so much that it can fail to boot. Instead, before the problematic commit KVM was already setting both thresholds to 0 in vmcb02, but after the first userspace VM exit shrink_ple_window was called and would reset the pause_filter_count to the default value. To fix this, change the fallback strategy - ignore the guest threshold values, but use/update the host threshold values unless the guest specifically requests disabling PAUSE filtering (either simple or advanced). Also fix a minor bug: on nested VM exit, when PAUSE filter counter were copied back to vmcb01, a dirty bit was not set. Thanks a lot to Suravee Suthikulpanit for debugging this!

Paolo Bonzini committed 4 years ago

Commit e3cdaab1 parent ba8ec272 files · +23 −20Browse files

Unified · Split

2 files
  1. arch/x86/kvm/svm/nested.c +21 −18
  2. arch/x86/kvm/svm/svm.c +2 −2
arch/x86/kvm/svm/nested.c+21−18
@@ -616,6 +616,8 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) · 615 unmodified lines
616 struct kvm_vcpu *vcpu = &svm->vcpu;616 struct kvm_vcpu *vcpu = &svm->vcpu;
617 struct vmcb *vmcb01 = svm->vmcb01.ptr;617 struct vmcb *vmcb01 = svm->vmcb01.ptr;
618 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;618 struct vmcb *vmcb02 = svm->nested.vmcb02.ptr;
619 u32 pause_count12;
620 u32 pause_thresh12;
619 621
620 /*622 /*
621 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,623 * Filled at exit: exit_code, exit_code_hi, exit_info_1, exit_info_2,
@@ -671,27 +673,25 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm) · 49 unmodified lines
671 if (!nested_vmcb_needs_vls_intercept(svm))673 if (!nested_vmcb_needs_vls_intercept(svm))
672 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;674 vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
673 675
676 pause_count12 = svm->pause_filter_enabled ? svm->nested.ctl.pause_filter_count : 0;
677 pause_thresh12 = svm->pause_threshold_enabled ? svm->nested.ctl.pause_filter_thresh : 0;
674 if (kvm_pause_in_guest(svm->vcpu.kvm)) {678 if (kvm_pause_in_guest(svm->vcpu.kvm)) {
675 /* use guest values since host doesn't use them */679 /* use guest values since host doesn't intercept PAUSE */
676 vmcb02->control.pause_filter_count =680 vmcb02->control.pause_filter_count = pause_count12;
677 svm->pause_filter_enabled ?681 vmcb02->control.pause_filter_thresh = pause_thresh12;
678 svm->nested.ctl.pause_filter_count : 0;
679 682
680 vmcb02->control.pause_filter_thresh =683 } else {
681 svm->pause_threshold_enabled ?684 /* start from host values otherwise */
682 svm->nested.ctl.pause_filter_thresh : 0;
683
684 } else if (!vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
685 /* use host values when guest doesn't use them */
686 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;685 vmcb02->control.pause_filter_count = vmcb01->control.pause_filter_count;
687 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;686 vmcb02->control.pause_filter_thresh = vmcb01->control.pause_filter_thresh;
688 } else {687
689 /*688 /* ... but ensure filtering is disabled if so requested. */
690 * Intercept every PAUSE otherwise and689 if (vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_PAUSE)) {
691 * ignore both host and guest values690 if (!pause_count12)
692 */691 vmcb02->control.pause_filter_count = 0;
693 vmcb02->control.pause_filter_count = 0;692 if (!pause_thresh12)
694 vmcb02->control.pause_filter_thresh = 0;693 vmcb02->control.pause_filter_thresh = 0;
694 }
695 }695 }
696 696
697 nested_svm_transition_tlb_flush(vcpu);697 nested_svm_transition_tlb_flush(vcpu);
@@ -951,8 +951,11 @@ int nested_svm_vmexit(struct vcpu_svm *svm) · 253 unmodified lines
951 vmcb12->control.event_inj = svm->nested.ctl.event_inj;951 vmcb12->control.event_inj = svm->nested.ctl.event_inj;
952 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;952 vmcb12->control.event_inj_err = svm->nested.ctl.event_inj_err;
953 953
954 if (!kvm_pause_in_guest(vcpu->kvm) && vmcb02->control.pause_filter_count)954 if (!kvm_pause_in_guest(vcpu->kvm)) {
955 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;955 vmcb01->control.pause_filter_count = vmcb02->control.pause_filter_count;
956 vmcb_mark_dirty(vmcb01, VMCB_INTERCEPTS);
957
958 }
956 959
957 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);960 nested_svm_copy_common_state(svm->nested.vmcb02.ptr, svm->vmcb01.ptr);
958 961
arch/x86/kvm/svm/svm.c+2−2
@@ -921,7 +921,7 @@ static void grow_ple_window(struct kvm_vcpu *vcpu) · 920 unmodified lines
921 struct vmcb_control_area *control = &svm->vmcb->control;921 struct vmcb_control_area *control = &svm->vmcb->control;
922 int old = control->pause_filter_count;922 int old = control->pause_filter_count;
923 923
924 if (kvm_pause_in_guest(vcpu->kvm) || !old)924 if (kvm_pause_in_guest(vcpu->kvm))
925 return;925 return;
926 926
927 control->pause_filter_count = __grow_ple_window(old,927 control->pause_filter_count = __grow_ple_window(old,
@@ -942,7 +942,7 @@ static void shrink_ple_window(struct kvm_vcpu *vcpu) · 14 unmodified lines
942 struct vmcb_control_area *control = &svm->vmcb->control;942 struct vmcb_control_area *control = &svm->vmcb->control;
943 int old = control->pause_filter_count;943 int old = control->pause_filter_count;
944 944
945 if (kvm_pause_in_guest(vcpu->kvm) || !old)945 if (kvm_pause_in_guest(vcpu->kvm))
946 return;946 return;
947 947
948 control->pause_filter_count =948 control->pause_filter_count =

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t