@@ -29,6 +29,8 @@
#define NSEC_PER_SEC 1000000000L
+extern bool kvm_has_needs_completion;
+
struct userspace_mem_region {
struct kvm_userspace_memory_region2 region;
struct sparsebit *unused_phy_pages;
@@ -634,9 +636,11 @@ static inline int __vcpu_run(struct kvm_vcpu *vcpu)
void vcpu_run_immediate_exit(struct kvm_vcpu *vcpu);
-static inline void vcpu_run_complete_io(struct kvm_vcpu *vcpu)
+static inline void vcpu_run_complete_exit(struct kvm_vcpu *vcpu)
{
- vcpu_run_immediate_exit(vcpu);
+ if (!kvm_has_needs_completion ||
+ (vcpu->run->flags & KVM_RUN_NEEDS_COMPLETION))
+ vcpu_run_immediate_exit(vcpu);
}
struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu);
@@ -19,6 +19,8 @@
#define KVM_UTIL_MIN_PFN 2
+bool kvm_has_needs_completion;
+
uint32_t guest_random_seed;
struct guest_random_state guest_rng;
static uint32_t last_guest_seed;
@@ -2253,6 +2255,8 @@ void __attribute((constructor)) kvm_selftest_init(void)
/* Tell stdout not to buffer its content. */
setbuf(stdout, NULL);
+ kvm_has_needs_completion = kvm_check_cap(KVM_CAP_NEEDS_COMPLETION);
+
guest_random_seed = last_guest_seed = random();
pr_info("Random seed: 0x%x\n", guest_random_seed);
@@ -154,7 +154,7 @@ uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)
"Guest failed to allocate ucall struct");
memcpy(uc, addr, sizeof(*uc));
- vcpu_run_complete_io(vcpu);
+ vcpu_run_complete_exit(vcpu);
} else {
memset(uc, 0, sizeof(*uc));
}
@@ -1077,13 +1077,7 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vcpu *vcpu)
nested_size, sizeof(state->nested_));
}
- /*
- * When KVM exits to userspace with KVM_EXIT_IO, KVM guarantees
- * guest state is consistent only after userspace re-enters the
- * kernel with KVM_RUN. Complete IO prior to migrating state
- * to a new VM.
- */
- vcpu_run_complete_io(vcpu);
+ vcpu_run_complete_exit(vcpu);
state = malloc(sizeof(*state) + msr_list->nmsrs * sizeof(state->msrs.entries[0]));
TEST_ASSERT(state, "-ENOMEM when allocating kvm state");
@@ -97,8 +97,7 @@ int main(void)
events.flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT;
events.triple_fault.pending = true;
vcpu_events_set(vcpu, &events);
- run->immediate_exit = true;
- vcpu_run_complete_io(vcpu);
+ vcpu_run_complete_exit(vcpu);
vcpu_events_get(vcpu, &events);
TEST_ASSERT(events.flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT,
Add selftests coverage for KVM_RUN_NEEDS_COMPLETION by redoing KVM_RUN if and only if KVM states that completion is required. Opportunistically rename the helper to replace "io" with "exit", as exits that require completion are no longer limited to I/O. Signed-off-by: Sean Christopherson <seanjc@google.com> --- tools/testing/selftests/kvm/include/kvm_util.h | 8 ++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 4 ++++ tools/testing/selftests/kvm/lib/ucall_common.c | 2 +- tools/testing/selftests/kvm/lib/x86/processor.c | 8 +------- tools/testing/selftests/kvm/x86/triple_fault_event_test.c | 3 +-- 5 files changed, 13 insertions(+), 12 deletions(-)