KVM: selftests: Move 0/initial value PERF_CAPS checks to dedicated sub-test
authorSean Christopherson <seanjc@google.com>
Sat, 11 Mar 2023 00:46:06 +0000 (16:46 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 6 Apr 2023 21:58:43 +0000 (14:58 -0700)
Use a separate sub-test to verify userspace can clear PERF_CAPABILITIES
and restore it to the KVM-supported value, as the testcase isn't unique
to the LBR format.

Link: https://lore.kernel.org/r/20230311004618.920745-10-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c

index ac08c0fdd84d56ca18f076eca496ce892800b15b..c3b0738e361b6222d12c64421bf11977332fea49 100644 (file)
@@ -41,6 +41,24 @@ static void guest_code(void)
        wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
 }
 
+/*
+ * Verify KVM allows writing PERF_CAPABILITIES with all KVM-supported features
+ * enabled, as well as '0' (to disable all features).
+ */
+static void test_basic_perf_capabilities(union perf_capabilities host_cap)
+{
+       struct kvm_vcpu *vcpu;
+       struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
+
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
+       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0);
+
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
+       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
+
+       kvm_vm_free(vm);
+}
+
 static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
 {
        struct kvm_vcpu *vcpu;
@@ -66,12 +84,6 @@ static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
        uint64_t val;
        int ret;
 
-       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
-       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0);
-
-       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.lbr_format);
-       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), (u64)host_cap.lbr_format);
-
        /*
         * KVM only supports the host's native LBR format, as well as '0' (to
         * disable LBR support).  Verify KVM rejects all other LBR formats.
@@ -98,6 +110,7 @@ int main(int argc, char *argv[])
        host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
        host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
 
+       test_basic_perf_capabilities(host_cap);
        test_fungible_perf_capabilities(host_cap);
        test_immutable_perf_capabilities(host_cap);