KVM: SVM: GIF and EFER.SVME are independent
v3 cover KVM: SVM: GIF and EFER.SVME are independent
Clearing EFER.SVME is not architected to set GIF, so GIF may be clear even when EFER.SVME is clear.
This is covered in the discussion at [1].
v2 -> v3:
- Keep setting GIF when force-leaving nested (Sean).
- Moved the relevant selftests patches from the series at [2] here (Sean).
v2: https://lore.kernel.org/kvm/[email protected]/
[1]https://lore.kernel.org/all/[email protected]/ [2]https://lore.kernel.org/kvm/[email protected]/
Jim Mattson (2):
KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
KVM: SVM: Don't set GIF when clearing EFER.SVME
Yosry Ahmed (2):
KVM: selftests: Use TEST_ASSERT_EQ() in test_vmx_nested_state()
KVM: selftests: Extend vmx_set_nested_state_test to cover SVM
arch/x86/kvm/svm/nested.c | 6 +-
arch/x86/kvm/svm/svm.c | 1 -
tools/testing/selftests/kvm/Makefile.kvm | 2 +-
...d_state_test.c => nested_set_state_test.c} | 128 ++++++++++++++++--
4 files changed, 120 insertions(+), 17 deletions(-)
rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => nested_set_state_test.c} (70%)base-commit: 115d5de2eef32ac5cd488404b44b38789362dbe6
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
PATCH v3 1/4 KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
From: Jim Mattson <[email protected]>
GIF==0 together with EFER.SVME==0 is a valid architectural state. Don't return -EINVAL for KVM_SET_NESTED_STATE when this combination is specified.
--- arch/x86/kvm/svm/nested.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/nested.c +2 -2
@@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. */ if (!(vcpu->arch.efer &EFER_SVME )) { -/* GIF=1 and no guest mode are required if SVME=0. */ -if (kvm_state->flags !=KVM_STATE_NESTED_GIF_SET ) +/* GUEST_MODE must be clear when SVME==0 */ +if (kvm_state->flags &KVM_STATE_NESTED_GUEST_MODE )return -EINVAL ; }
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Quoted 19 lines · Yosry Ahmed
From: Jim Mattson <[email protected]>
GIF==0 together with EFER.SVME==0 is a valid architectural state. Don't return
-EINVALforKVM_SET_NESTED_STATEwhen this combination is specified.Fixes cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")Signed-off-by Jim Mattson <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/kvm/svm/nested.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/svm/nested.c +0
-0
arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1788
Quoted 7 lines
arch/x86/kvm/svm/nested.c +2
-2@@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. */ if (!(vcpu->arch.efer &EFER_SVME )) { -/* GIF=1 and no guest mode are required if SVME=0. */ -if (kvm_state->flags !=KVM_STATE_NESTED_GIF_SET ) +/* GUEST_MODE must be clear when SVME==0 */ +if (kvm_state->flags &KVM_STATE_NESTED_GUEST_MODE )
Hmm, this is technically wrong, as it will allow KVM_STATE_NESTED_RUN_PENDING. Now, arguably KVM already has a flaw there as KVM allows KVM_STATE_NESTED_RUN_PENDING without KVM_STATE_NESTED_GUEST_MODE for SVME=1, but I'd prefer not to make the hole bigger.
The nested if-statement is also unnecessary.
How about this instead? (not yet tested)
/* * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. * If SVME is disabled, the only valid states are "none" and GIF=1 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed). */ if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags && kvm_state->flags != KVM_STATE_NESTED_GIF_SET) return -EINVAL;
Re: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Quoted 37 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
From: Jim Mattson <[email protected]>
GIF==0 together with EFER.SVME==0 is a valid architectural state. Don't return
-EINVALforKVM_SET_NESTED_STATEwhen this combination is specified.Fixes cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")Signed-off-by Jim Mattson <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/kvm/svm/nested.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/svm/nested.c +0
-0arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1788
Quoted 7 lines
arch/x86/kvm/svm/nested.c +2
-2@@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. */ if (!(vcpu->arch.efer &EFER_SVME )) { -/* GIF=1 and no guest mode are required if SVME=0. */ -if (kvm_state->flags !=KVM_STATE_NESTED_GIF_SET ) +/* GUEST_MODE must be clear when SVME==0 */ +if (kvm_state->flags &KVM_STATE_NESTED_GUEST_MODE )Hmm, this is technically wrong, as it will allow
KVM_STATE_NESTED_RUN_PENDING. Now, arguably KVM already has a flaw there as KVM allowsKVM_STATE_NESTED_RUN_PENDINGwithoutKVM_STATE_NESTED_GUEST_MODEfor SVME=1, but I'd prefer not to make the hole bigger.The nested if-statement is also unnecessary.
How about this instead? (not yet tested)
Quoted 9 lines
/* * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. * If SVME is disabled, the only valid states are "none" and GIF=1 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed). */ if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags && kvm_state->flags != KVM_STATE_NESTED_GIF_SET) return -EINVAL;
Looks good to me, with the tiny exception that at this point clearing SVME does set GIF. Maybe re-order the patches?
Let me know if you want me to send a new version or if you'll fix it up while applying.
Sean Christopherson
maintainerRe: [PATCH v3 1/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0
Quoted 48 lines · Yosry Ahmed
Quoted 37 lines · Sean Christopherson
Quoted 19 lines · Yosry Ahmed
From: Jim Mattson <[email protected]>
GIF==0 together with EFER.SVME==0 is a valid architectural state. Don't return
-EINVALforKVM_SET_NESTED_STATEwhen this combination is specified.Fixes cc440cdad5b7 ("KVM: nSVM: implement KVM_GET_NESTED_STATE and KVM_SET_NESTED_STATE")Signed-off-by Jim Mattson <[email protected]>Reviewed-by Yosry Ahmed <[email protected]>Signed-off-by Yosry Ahmed <[email protected]>--- arch/x86/kvm/svm/nested.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kvm/svm/nested.c +0
-0arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 1788
Quoted 7 lines
arch/x86/kvm/svm/nested.c +2
-2@@ -1784,8 +1784,8 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,* EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. */ if (!(vcpu->arch.efer &EFER_SVME )) { -/* GIF=1 and no guest mode are required if SVME=0. */ -if (kvm_state->flags !=KVM_STATE_NESTED_GIF_SET ) +/* GUEST_MODE must be clear when SVME==0 */ +if (kvm_state->flags &KVM_STATE_NESTED_GUEST_MODE )Hmm, this is technically wrong, as it will allow
KVM_STATE_NESTED_RUN_PENDING. Now, arguably KVM already has a flaw there as KVM allowsKVM_STATE_NESTED_RUN_PENDINGwithoutKVM_STATE_NESTED_GUEST_MODEfor SVME=1, but I'd prefer not to make the hole bigger.The nested if-statement is also unnecessary.
How about this instead? (not yet tested)
Quoted 9 lines
/* * If in guest mode, vcpu->arch.efer actually refers to the L2 guest's * EFER.SVME, but EFER.SVME still has to be 1 for VMRUN to succeed. * If SVME is disabled, the only valid states are "none" and GIF=1 * (clearing SVME does NOT set GIF, i.e. GIF=0 is allowed). */ if (!(vcpu->arch.efer & EFER_SVME) && kvm_state->flags && kvm_state->flags != KVM_STATE_NESTED_GIF_SET) return -EINVAL;
Quoted 5 lines
Looks good to me, with the tiny exception that at this point clearing SVME does set GIF. Maybe re-order the patches?
Let me know if you want me to send a new version or if you'll fix it up while applying.
No need for a new version.
PATCH v3 2/4 KVM: SVM: Don't set GIF when clearing EFER.SVME
In reply to Yosry AhmedFrom: Jim Mattson <[email protected]>
Clearing EFER.SVME is not architected to set GIF. Don't set GIF when emulating a change to EFER that clears EFER.SVME.
However, keep setting GIF if clearing EFER.SVME causes force-leaving the nested guest through svm_leave_nested(), to maintain a sane behavior of not leaving GIF cleared after exiting the guest. In every other path, setting GIF is either correct/desirable, or irrelevant because the caller immediately and unconditionally sets/clears GIF.
This is more-or-less KVM defining HW behavior, but leaving GIF cleared would also be defining HW behavior anyway.
Note that if force-leaving the nested guest is considered a SHUTDOWN, then this could violate the APM-specified behavior:
If the processor enters the shutdown state (due to a triple fault for instance) while GIF is clear, it can only be restarted by means of a RESET.
However, a SHUTDOWN leaves the VMCB undefined, so there's not a lot that KVM can do in this case. Also, if vGIF is enabled on SHUTDOWN, KVM has no way of finding out of GIF was cleared.
The only way for KVM to handle this without making up HW behavior is to completely terminate the VM, so settle for doing the relatively "sane" thing of setting GIF when force-leaving nested.
--- arch/x86/kvm/svm/nested.c | 2 ++ arch/x86/kvm/svm/svm.c | 1 - 2 files changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +2 -0
@@ -1363,6 +1363,8 @@ void svm_leave_nested(struct kvm_vcpu *vcpu)nested_svm_uninit_mmu_context (vcpu);vmcb_mark_all_dirty (svm->vmcb); +svm_set_gif (svm, true); +if (kvm_apicv_activated (vcpu->kvm))kvm_make_request (KVM_REQ_APICV_UPDATE , vcpu); }
arch/x86/kvm/svm/svm.c +0 -1
@@ -215,7 +215,6 @@ int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)if ((old_efer &EFER_SVME ) != (efer &EFER_SVME )) {if (!(efer &EFER_SVME )) {svm_leave_nested (vcpu); -svm_set_gif (svm, true);/* #GP intercept is still needed for vmware backdoor */ if (!enable_vmware_backdoor)clr_exception_intercept (svm,GP_VECTOR );
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
PATCH v3 3/4 KVM: selftests: Use TEST_ASSERT_EQ() in test_vmx_nested_state()
In reply to Yosry AhmedThe assert messages do not add much value, so use TEST_ASSERT_EQ(), which also nicely displays the addresses in hex. While at it, also assert the values of state->flags.
--- tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +4 -2
@@ -241,8 +241,10 @@ void test_vmx_nested_state(struct kvm_vcpu *vcpu)TEST_ASSERT (state->size >=sizeof (*state) && state->size <= state_sz,"Size must be between %ld and %d. The size returned was %d." ,sizeof (*state), state_sz, state->size); -TEST_ASSERT (state->hdr.vmx.vmxon_pa ==-1ull ,"vmxon_pa must be -1ull." ); -TEST_ASSERT (state->hdr.vmx.vmcs12_pa ==-1ull ,"vmcs_pa must be -1ull." ); + +TEST_ASSERT_EQ (state->hdr.vmx.vmxon_pa,-1ull ); +TEST_ASSERT_EQ (state->hdr.vmx.vmcs12_pa,-1ull ); +TEST_ASSERT_EQ (state->flags,0 );free (state); }
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
PATCH v3 4/4 KVM: selftests: Extend vmx_set_nested_state_test to cover SVM
In reply to Yosry AhmedAdd test cases for the validation checks in svm_set_nested_state(), and allow the test to run with SVM as well as VMX. The SVM test also makes sure that KVM_SET_NESTED_STATE accepts GIF being set or cleared if EFER.SVME is cleared, verifying a recently fixed bug where GIF was incorrectly expected to always be set when EFER.SVME is cleared.
---
tools/testing/selftests/kvm/Makefile.kvm | 2 +-
...d_state_test.c => nested_set_state_test.c} | 122 ++++++++++++++++--
2 files changed, 112 insertions(+), 12 deletions(-)
rename tools/testing/selftests/kvm/x86/{vmx_set_nested_state_test.c => nested_set_state_test.c} (71%)tools/testing/selftests/kvm/Makefile.kvm +1 -1
@@ -92,6 +92,7 @@ TEST_GEN_PROGS_x86 += x86/nested_close_kvm_test TEST_GEN_PROGS_x86 += x86/nested_emulation_test TEST_GEN_PROGS_x86 += x86/nested_exceptions_test TEST_GEN_PROGS_x86 += x86/nested_invalid_cr3_test +TEST_GEN_PROGS_x86 += x86/nested_set_state_test TEST_GEN_PROGS_x86 += x86/nested_tsc_adjust_test TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test TEST_GEN_PROGS_x86 += x86/platform_info_test
@@ -120,7 +121,6 @@ TEST_GEN_PROGS_x86 += x86/vmx_exception_with_invalid_guest_state TEST_GEN_PROGS_x86 += x86/vmx_msrs_test TEST_GEN_PROGS_x86 += x86/vmx_invalid_nested_guest_state TEST_GEN_PROGS_x86 += x86/vmx_nested_la57_state_test -TEST_GEN_PROGS_x86 += x86/vmx_set_nested_state_test TEST_GEN_PROGS_x86 += x86/apic_bus_clock_test TEST_GEN_PROGS_x86 += x86/xapic_ipi_test TEST_GEN_PROGS_x86 += x86/xapic_state_test
tools/testing/selftests/kvm/x86/nested_set_state_test.c renamed from tools/testing/selftests/kvm/x86/vmx_set_nested_state_test.c +111 -11
157 lines not on this page · raw message
Signature · 1 line
2.52.0.rc2.455.g230fcf2819-goog
Quoted 11 lines · Yosry Ahmed
Clearing EFER.SVME is not architected to set GIF, so GIF may be clear even when EFER.SVME is clear.
This is covered in the discussion at [1].
v2 -> v3:
- Keep setting GIF when force-leaving nested (Sean).
- Moved the relevant selftests patches from the series at [2] here (Sean).[...]
Applied to kvm-x86 svm, with my suggested fixup and the order of 1 and 2 swapped. Thanks!
[1/4] KVM: SVM: Don't set GIF when clearing EFER.SVME
https://github.com/kvm-x86/linux/commit/8312f1b9dd71 [2/4] KVM: SVM: Allow KVM_SET_NESTED_STATE to clear GIF when SVME==0 https://github.com/kvm-x86/linux/commit/6f4d3ebc24c6 [3/4] KVM: selftests: Use TEST_ASSERT_EQ() in test_vmx_nested_state() https://github.com/kvm-x86/linux/commit/bda6ae6f2966 [4/4] KVM: selftests: Extend vmx_set_nested_state_test to cover SVM https://github.com/kvm-x86/linux/commit/ca2eccb953fd