KVM: selftests: Test all fungible features in PERF_CAPABILITIES
authorSean Christopherson <seanjc@google.com>
Sat, 11 Mar 2023 00:46:11 +0000 (16:46 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 6 Apr 2023 21:58:44 +0000 (14:58 -0700)
Verify that userspace can set all fungible features in PERF_CAPABILITIES.
Drop the now unused #define of the "full-width writes" flag.

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

index f7a27b5c949b02320936a41fee77dfae29e93195..2647282ff3806f87769b9cd1f968b9375455ef24 100644 (file)
 #define _GNU_SOURCE /* for program_invocation_short_name */
 #include <sys/ioctl.h>
 
+#include <linux/bitmap.h>
+
 #include "kvm_util.h"
 #include "vmx.h"
 
-#define PMU_CAP_FW_WRITES      (1ULL << 13)
 #define PMU_CAP_LBR_FMT                0x3f
 
 union perf_capabilities {
@@ -36,6 +37,18 @@ union perf_capabilities {
        u64     capabilities;
 };
 
+/*
+ * The LBR format and most PEBS features are immutable, all other features are
+ * fungible (if supported by the host and KVM).
+ */
+static const union perf_capabilities immutable_caps = {
+       .lbr_format = -1,
+       .pebs_trap  = 1,
+       .pebs_arch_reg = 1,
+       .pebs_format = -1,
+       .pebs_baseline = 1,
+};
+
 static void guest_code(void)
 {
        wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
@@ -58,15 +71,22 @@ static void test_basic_perf_capabilities(union perf_capabilities host_cap)
 
 static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
 {
+       const uint64_t fungible_caps = host_cap.capabilities & ~immutable_caps.capabilities;
+
        struct kvm_vcpu *vcpu;
        struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+       int bit;
 
-       /* testcase 1, set capabilities when we have PDCM bit */
-       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES);
+       for_each_set_bit(bit, &fungible_caps, 64) {
+               vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, BIT_ULL(bit));
+               vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES,
+                            host_cap.capabilities & ~BIT_ULL(bit));
+       }
+       vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.capabilities);
 
        /* check whatever we write with KVM_SET_MSR is _not_ modified */
        vcpu_run(vcpu);
-       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRITES);
+       ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), host_cap.capabilities);
 
        kvm_vm_free(vm);
 }
@@ -102,7 +122,6 @@ int main(int argc, char *argv[])
        TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
 
        host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
-       host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
 
        TEST_ASSERT(host_cap.full_width_write,
                    "Full-width writes should always be supported");