Extend svm_pmu_host_guest_test to set EFER.SVME from userspace via KVM_SET_SREGS and verify that Host-Only counters resume counting. Switch the existing filter syncs to GUEST_SYNC2() so the guest can select between updating the PMU event filter and setting EFER.SVME. Setting EFER.SVME via KVM_SET_SREGS exercises the __set_sregs_common() -> svm_set_efer() -> svm_pmu_handle_nested_transition() path from userspace (and specifically from KVM_SET_SREGS, where kvm->srcu is not held). Signed-off-by: Yosry Ahmed --- .../kvm/x86/svm_pmu_host_guest_test.c | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c b/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c index c5b5cd788d93f..3f743dbaf851f 100644 --- a/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c +++ b/tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c @@ -37,6 +37,11 @@ #define LOOP_INSNS 1000 +enum sync_cmd { + SYNC_SET_FILTER, + SYNC_SET_SVME, +}; + static __always_inline void run_instruction_loop(void) { unsigned int i; @@ -128,12 +133,12 @@ static void l1_guest_code(struct svm_test_data *svm) assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG)); /* Step 5: Set KVM_PMU_EVENT_DENY - all counters stop */ - GUEST_SYNC(KVM_PMU_EVENT_DENY); + GUEST_SYNC2(SYNC_SET_FILTER, KVM_PMU_EVENT_DENY); run_and_measure(deltas); assert_pmc_counts(deltas, 0); /* Step 6: Set KVM_PMU_EVENT_ALLOW - back to all except Guest-only */ - GUEST_SYNC(KVM_PMU_EVENT_ALLOW); + GUEST_SYNC2(SYNC_SET_FILTER, KVM_PMU_EVENT_ALLOW); run_and_measure(deltas); assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG)); @@ -153,6 +158,11 @@ static void l1_guest_code(struct svm_test_data *svm) run_and_measure(deltas); assert_pmc_counts(deltas, BIT(PMC_NONE)); + /* Step 10: Set SVME via KVM_SET_SREGS - Host-Only counters resume */ + GUEST_SYNC1(SYNC_SET_SVME); + run_and_measure(deltas); + assert_pmc_counts(deltas, BIT(PMC_NONE) | BIT(PMC_H) | BIT(PMC_HG)); + GUEST_DONE(); } @@ -174,6 +184,7 @@ static struct kvm_pmu_event_filter *alloc_event_filter(u64 event) int main(int argc, char *argv[]) { struct kvm_pmu_event_filter *filter; + struct kvm_sregs sregs; struct kvm_vcpu *vcpu; struct kvm_vm *vm; struct ucall uc; @@ -201,8 +212,20 @@ int main(int argc, char *argv[]) case UCALL_DONE: goto done; case UCALL_SYNC: - filter->action = uc.args[1]; - vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, filter); + switch (uc.args[0]) { + case SYNC_SET_FILTER: + filter->action = uc.args[1]; + vm_ioctl(vm, KVM_SET_PMU_EVENT_FILTER, filter); + break; + case SYNC_SET_SVME: + vcpu_sregs_get(vcpu, &sregs); + sregs.efer |= EFER_SVME; + vcpu_sregs_set(vcpu, &sregs); + break; + default: + TEST_FAIL("Unknown sync cmd %lu", uc.args[0]); + goto done; + } break; default: TEST_FAIL("Unknown ucall %lu", uc.cmd); -- 2.56.1.839.g22a8290db9-goog