Skip to content

Commit e4d86fb

Browse files
committed
KVM: selftests: Split PMU caps sub-tests to avoid writing MSR after KVM_RUN
Split the PERF_CAPABILITIES subtests into two parts so that the LBR format testcases don't execute after KVM_RUN. Similar to the guest CPUID model, KVM will soon disallow changing PERF_CAPABILITIES after KVM_RUN, at which point attempting to set the MSR after KVM_RUN will yield false positives and/or false negatives depending on what the test is trying to do. Land the LBR format test in a more generic "immutable features" test in anticipation of expanding its scope to other immutable features. Link: https://lore.kernel.org/r/20230311004618.920745-6-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9eb6ba3 commit e4d86fb

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,10 @@ static void guest_code(void)
4141
wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
4242
}
4343

44-
int main(int argc, char *argv[])
44+
static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
4545
{
46-
struct kvm_vm *vm;
4746
struct kvm_vcpu *vcpu;
48-
int ret;
49-
union perf_capabilities host_cap;
50-
uint64_t val;
51-
52-
host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
53-
host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
54-
55-
/* Create VM */
56-
vm = vm_create_with_one_vcpu(&vcpu, guest_code);
57-
58-
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
59-
60-
TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
61-
TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
47+
struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
6248

6349
/* testcase 1, set capabilities when we have PDCM bit */
6450
vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES);
@@ -70,16 +56,25 @@ int main(int argc, char *argv[])
7056
vcpu_run(vcpu);
7157
ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRITES);
7258

73-
/* testcase 2, check valid LBR formats are accepted */
59+
kvm_vm_free(vm);
60+
}
61+
62+
static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
63+
{
64+
struct kvm_vcpu *vcpu;
65+
struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
66+
uint64_t val;
67+
int ret;
68+
7469
vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
7570
ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), 0);
7671

7772
vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, host_cap.lbr_format);
7873
ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), (u64)host_cap.lbr_format);
7974

8075
/*
81-
* Testcase 3, check that an "invalid" LBR format is rejected. Only an
82-
* exact match of the host's format (and 0/disabled) is allowed.
76+
* KVM only supports the host's native LBR format, as well as '0' (to
77+
* disable LBR support). Verify KVM rejects all other LBR formats.
8378
*/
8479
for (val = 1; val <= PMU_CAP_LBR_FMT; val++) {
8580
if (val == (host_cap.capabilities & PMU_CAP_LBR_FMT))
@@ -88,7 +83,23 @@ int main(int argc, char *argv[])
8883
ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
8984
TEST_ASSERT(!ret, "Bad LBR FMT = 0x%lx didn't fail", val);
9085
}
86+
kvm_vm_free(vm);
87+
}
88+
89+
int main(int argc, char *argv[])
90+
{
91+
union perf_capabilities host_cap;
92+
93+
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
94+
95+
TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
96+
TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
97+
98+
host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
99+
host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
100+
101+
test_fungible_perf_capabilities(host_cap);
102+
test_immutable_perf_capabilities(host_cap);
91103

92104
printf("Completed perf capability tests.\n");
93-
kvm_vm_free(vm);
94105
}

0 commit comments

Comments
 (0)