public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jinrong Liang <ljr.kernel@gmail.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Like Xu <like.xu.linux@gmail.com>,
	Jinrong Liang <cloudliang@tencent.com>,
	linux-kselftest@vger.kernel.org, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] KVM: selftests: Add PEBS test for MSR_IA32_PERF_CAPABILITIES
Date: Thu,  8 Jun 2023 19:34:20 +0800	[thread overview]
Message-ID: <20230608113420.14695-3-cloudliang@tencent.com> (raw)
In-Reply-To: <20230608113420.14695-1-cloudliang@tencent.com>

From: Jinrong Liang <cloudliang@tencent.com>

This commit adds a PEBS test that verifies all possible combinations
of PEBS-related bits in MSR_IA32_PERF_CAPABILITIES. This comprehensive
test ensures the accuracy of the PEBS feature.

Signed-off-by: Jinrong Liang <cloudliang@tencent.com>
---
 .../selftests/kvm/x86_64/vmx_pmu_caps_test.c  | 71 +++++++++++++++++++
 1 file changed, 71 insertions(+)

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 02903084598f..c1b1ba44bc26 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
@@ -21,6 +21,12 @@
 
 #define MAX_LINEAR_ADDR_MASK		GENMASK_ULL(15, 8)
 #define ADDR_OFS_BIT			8
+#define PMU_CAP_LBR_FMT		0x3f
+#define PMU_CAP_SMM_FREEZE		BIT_ULL(12)
+#define PMU_CAP_FW_WRITES		BIT_ULL(13)
+#define PMU_CAP_PERF_METRICS_AVAILABLE	BIT_ULL(PERF_CAP_METRICS_IDX)
+#define PMU_CAP_PEBS_OUTPUT_PT_AVAIL	BIT_ULL(PERF_CAP_PT_IDX)
+#define PMU_CAP_PEBS_ALL		(PERF_CAP_PEBS_MASK | PMU_CAP_PEBS_OUTPUT_PT_AVAIL)
 
 union perf_capabilities {
 	struct {
@@ -331,6 +337,70 @@ static void test_ds_area_noncanonical_address(union perf_capabilities host_cap)
 	kvm_vm_free(vm);
 }
 
+static void test_pebs_bit_combinations(union perf_capabilities host_cap)
+{
+	int ret;
+	uint64_t pebs_val, val;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm = vm_create_with_one_vcpu(&vcpu, NULL);
+
+	TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) > 1);
+	TEST_REQUIRE(host_cap.capabilities & PERF_CAP_PEBS_FORMAT);
+	TEST_REQUIRE(vcpu_get_msr(vcpu, MSR_IA32_MISC_ENABLE) &
+		     MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL);
+
+	/*
+	 * Test if PEBS_REC_FMT is set and the value is the same as host,
+	 * the other PEBS bits are allowed to be set only if they are the
+	 * same as host.
+	 */
+	pebs_val = host_cap.capabilities & PMU_CAP_PEBS_ALL;
+
+	vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, pebs_val);
+	ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES),
+		  (u64)pebs_val);
+
+	/* Test all PEBS bit combinations. */
+	for (val = 0x0; val <= (~0ul & PMU_CAP_PEBS_ALL); val++) {
+		/* Skips values that are not related to PEBS. */
+		if (val & (PMU_CAP_LBR_FMT | PMU_CAP_SMM_FREEZE |
+		    PMU_CAP_FW_WRITES | PMU_CAP_PERF_METRICS_AVAILABLE))
+			continue;
+
+		/*
+		 * Test that value of PEBS is rejected when the KVM doesn't
+		 * supports Intel PT.
+		 */
+		if ((val & PMU_CAP_PEBS_OUTPUT_PT_AVAIL) &&
+		    (!(host_cap.capabilities & PMU_CAP_PEBS_OUTPUT_PT_AVAIL))) {
+			ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
+			TEST_ASSERT(!ret, "Bad PEBS auxiliary bits = 0x%lx didn't fail", val);
+
+			continue;
+		}
+
+		/*
+		 * Test that value of PEBS is rejected when carrying
+		 * PEBS_REC_FMT if the value of PEBS is not equal to host.
+		 */
+		if ((val & PERF_CAP_PEBS_FORMAT) && val != pebs_val) {
+			ret = _vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
+			TEST_ASSERT(!ret, "Bad PEBS auxiliary bits = 0x%lx didn't fail", val);
+
+			continue;
+		}
+
+		/*
+		 * Test that PEBS bits can be written simultaneously or
+		 * independently if PEBS_REC_FMT is not carried.
+		 */
+		vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, val);
+		ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_PERF_CAPABILITIES), val);
+	}
+
+	kvm_vm_free(vm);
+}
+
 int main(int argc, char *argv[])
 {
 	union perf_capabilities host_cap;
@@ -352,4 +422,5 @@ int main(int argc, char *argv[])
 	test_guest_wrmsr_perf_capabilities(host_cap);
 	test_lbr_perf_capabilities(host_cap);
 	test_ds_area_noncanonical_address(host_cap);
+	test_pebs_bit_combinations(host_cap);
 }
-- 
2.31.1


  parent reply	other threads:[~2023-06-08 11:35 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 11:34 [PATCH 0/2] KVM: selftests: Add tests for PEBS and MSR_IA32_DS_AREA Jinrong Liang
2023-06-08 11:34 ` [PATCH 1/2] KVM: selftests: Test consistency of setting MSR_IA32_DS_AREA Jinrong Liang
2023-06-28 21:48   ` Sean Christopherson
2023-06-08 11:34 ` Jinrong Liang [this message]
2023-06-28 21:55   ` [PATCH 2/2] KVM: selftests: Add PEBS test for MSR_IA32_PERF_CAPABILITIES Sean Christopherson
2023-06-30  8:59     ` Jinrong Liang

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=20230608113420.14695-3-cloudliang@tencent.com \
    --to=ljr.kernel@gmail.com \
    --cc=cloudliang@tencent.com \
    --cc=kvm@vger.kernel.org \
    --cc=like.xu.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=wanpengli@tencent.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