mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Xiaoyao Li <xiaoyao.li@intel.com>,
	Like Xu <like.xu.linux@gmail.com>
Subject: [PATCH v2 08/21] KVM: selftests: Split PMU caps sub-tests to avoid writing MSR after KVM_RUN
Date: Fri, 10 Feb 2023 00:31:35 +0000	[thread overview]
Message-ID: <20230210003148.2646712-9-seanjc@google.com> (raw)
In-Reply-To: <20230210003148.2646712-1-seanjc@google.com>

Split the PERF_CAPABILITIES subtests into two parts so that the LBR format
testcases don't execute after KVM_RUN.  Now that KVM disallows changing
PERF_CAPABILITIES after KVM_RUN (same as guest CPUID), 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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  | 51 +++++++++++--------
 1 file changed, 31 insertions(+), 20 deletions(-)

diff --git a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
index c280ba1e6572..ac08c0fdd84d 100644
--- a/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
+++ b/tools/testing/selftests/kvm/x86_64/vmx_pmu_caps_test.c
@@ -41,24 +41,10 @@ static void guest_code(void)
 	wrmsr(MSR_IA32_PERF_CAPABILITIES, PMU_CAP_LBR_FMT);
 }
 
-int main(int argc, char *argv[])
+static void test_fungible_perf_capabilities(union perf_capabilities host_cap)
 {
-	struct kvm_vm *vm;
 	struct kvm_vcpu *vcpu;
-	int ret;
-	union perf_capabilities host_cap;
-	uint64_t val;
-
-	host_cap.capabilities = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
-	host_cap.capabilities &= (PMU_CAP_FW_WRITES | PMU_CAP_LBR_FMT);
-
-	/* Create VM */
-	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
-
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
-
-	TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
-	TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 0);
+	struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, guest_code);
 
 	/* testcase 1, set capabilities when we have PDCM bit */
 	vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, PMU_CAP_FW_WRITES);
@@ -70,7 +56,16 @@ int main(int argc, char *argv[])
 	vcpu_run(vcpu);
 	ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), PMU_CAP_FW_WRITES);
 
-	/* testcase 2, check valid LBR formats are accepted */
+	kvm_vm_free(vm);
+}
+
+static void test_immutable_perf_capabilities(union perf_capabilities host_cap)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
+	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);
 
@@ -78,8 +73,8 @@ int main(int argc, char *argv[])
 	ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), (u64)host_cap.lbr_format);
 
 	/*
-	 * Testcase 3, check that an "invalid" LBR format is rejected.  Only an
-	 * exact match of the host's format (and 0/disabled) is allowed.
+	 * KVM only supports the host's native LBR format, as well as '0' (to
+	 * disable LBR support).  Verify KVM rejects all other LBR formats.
 	 */
 	for (val = 1; val <= PMU_CAP_LBR_FMT; val++) {
 		if (val == (host_cap.capabilities & PMU_CAP_LBR_FMT))
@@ -88,7 +83,23 @@ int main(int argc, char *argv[])
 		ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
 		TEST_ASSERT(!ret, "Bad LBR FMT = 0x%lx didn't fail", val);
 	}
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+	union perf_capabilities host_cap;
+
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
+
+	TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
+	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_fungible_perf_capabilities(host_cap);
+	test_immutable_perf_capabilities(host_cap);
 
 	printf("Completed perf capability tests.\n");
-	kvm_vm_free(vm);
 }
-- 
2.39.1.581.gbfd45094c4-goog


  parent reply	other threads:[~2023-02-10  0:32 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-10  0:31 [PATCH v2 00/21] KVM: x86: Disallow writes to feature MSRs post-KVM_RUN Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 01/21] KVM: x86: Rename kvm_init_msr_list() to clarify it inits multiple lists Sean Christopherson
2023-02-14  6:34   ` Xiaoyao Li
2023-02-10  0:31 ` [PATCH v2 02/21] KVM: x86: Add a helper to query whether or not a vCPU has ever run Sean Christopherson
2023-02-14  6:35   ` Xiaoyao Li
2023-02-10  0:31 ` [PATCH v2 03/21] KVM: x86: Add macros to track first...last VMX feature MSRs Sean Christopherson
2023-02-14 10:48   ` Like Xu
2023-02-14 18:51     ` Sean Christopherson
2023-02-15  3:20   ` Xiaoyao Li
2023-02-10  0:31 ` [PATCH v2 04/21] KVM: x86: Generate set of VMX feature MSRs using first/last definitions Sean Christopherson
2023-02-14  6:36   ` Xiaoyao Li
2023-02-10  0:31 ` [PATCH v2 05/21] KVM: x86: Disallow writes to immutable feature MSRs after KVM_RUN Sean Christopherson
2023-02-10 13:01   ` Yu Zhang
2023-02-10 16:31     ` Sean Christopherson
2023-02-14  6:39   ` Xiaoyao Li
2023-02-14 11:39   ` Like Xu
2023-02-15 22:36     ` Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 06/21] KVM: x86/pmu: WARN and bug the VM if PMU is refreshed after vCPU has run Sean Christopherson
2023-02-15 11:53   ` Like Xu
2023-02-15 15:20     ` Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 07/21] KVM: x86/pmu: Zero out LBR capabilities during PMU refresh Sean Christopherson
2023-02-15 12:00   ` Like Xu
2023-02-15 15:17     ` Sean Christopherson
2023-02-10  0:31 ` Sean Christopherson [this message]
2023-02-14  7:27   ` [PATCH v2 08/21] KVM: selftests: Split PMU caps sub-tests to avoid writing MSR after KVM_RUN Like Xu
2023-02-14 17:42     ` Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 09/21] KVM: selftests: Move 0/initial value PERF_CAPS checks to dedicated sub-test Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 10/21] KVM: selftests: Assert that full-width PMC writes are supported if PDCM=1 Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 11/21] KVM: selftests: Print out failing MSR and value in vcpu_set_msr() Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 12/21] KVM: selftests: Verify KVM preserves userspace writes to "durable" MSRs Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 13/21] KVM: selftests: Drop now-redundant checks on PERF_CAPABILITIES writes Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 14/21] KVM: selftests: Test all fungible features in PERF_CAPABILITIES Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 15/21] KVM: selftests: Test all immutable non-format bits " Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 16/21] KVM: selftests: Expand negative testing of guest writes to PERF_CAPABILITIES Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 17/21] KVM: selftests: Test post-KVM_RUN " Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 18/21] KVM: selftests: Drop "all done!" printf() from PERF_CAPABILITIES test Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 19/21] KVM: selftests: Refactor LBR_FMT test to avoid use of separate macro Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 20/21] KVM: selftests: Add negative testcase for PEBS format in PERF_CAPABILITIES Sean Christopherson
2023-02-10  0:31 ` [PATCH v2 21/21] KVM: selftests: Verify LBRs are disabled if vPMU is disabled Sean Christopherson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230210003148.2646712-9-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=like.xu.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=xiaoyao.li@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®