From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.9 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796117729; b=zt2tM7q3GLujUYbivihhLz87dvscByobLuq6kHG7kKRoHNKcW6fwFqku/vyL4gi1kzI3 QgOYBUrs4JTgTHLPdOoQfeyQD4kFHZTU+gGBiYmd9sXC59Hjy/dhRLF+BOc+s18SkADaE HsN7ZVw1wUzUcaJg0/N/pb8O59JKuwB035Yx2xQH7zWijA4uOIDA6rORvGm4nUxO1KE9n /hoX8ap2xic3V1iXPf5Pd7qh4+sqjIG/jiDQPYwRsRzXxHmeuR5PnoQ7EREsgM+99Hrbx XRYyHiZVJTO719BejbG+hzun9GXlawK98b2Ph22LjdtOjt5F01wmxXDRNGeqfQcko9g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796117729; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=8aelul2aOqoOpTgeE6cvyXE+pjT+3ec4nRO+eYkbv28=; b=nRbsAENbrMjE//tHQ0oTpJp1g7h0Rg13dYDqiqH+GSLE+1dgGar3FOAr3f0sK8hljs62 eM0NxG2rFchdyR7D2rGkgnCaJyrYiaY24S9D1lLe7TLAJ5rjJxRobWQmM+Slk3Ky+P9vJ QaBDeg9uFM6yNCpEmOo3kDuCUsuNp5+PaQshIDJwaTrWt9UE4WCP3ipUJPGiufWI+uzaR olABzfWUCh7UWpC5bo2WKOq/AvY1kicdS1k2YKvwunNXjioP8gg9J7870i36AKBppLmi/ vYWofDCoX3j3B73rf4FRie2nBuUI/iDI3Or7GgUIZiQE5ozrdgwBfkK95Sxs/Iiv4XQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=JVH4r6ZY; arc=none smtp.remote-ip=192.198.163.9 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90EA8606A31A; Tue, 1 Dec 2026 09:35:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 90EA8606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=JVH4r6ZY DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796117729; x=1827653729; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=8aelul2aOqoOpTgeE6cvyXE+pjT+3ec4nRO+eYkbv28=; b=JVH4r6ZYT465530w9WON7QqlUNiESJ1g2uYN2xPM+nV6Yw16aUx2mvyj oRoWsvUpsuSile7GmkKadl0CH+wZvebUxIEP8gEezZbYqu6mvpO2Plr/I tKJ/JreNU3whMzrfwMf+f6uWpuXymthv1Q3w9OofihFV2L0C0+1RzfFCL 6X1PbCOBhtsqrWOWDyKeMn069NZ4A1RSQZIkxICLdkbTZxSpVappy7Ile Z/8pYZQAINKns+JaUYDLVflO0QrmOlshfC5h/zOqCsT00qLVvpr8l5mxu ZlkLgfUBmBOPfxQXlLQ01AQogr8kObjX2mO916PhhOjPCVEVDNNW+uHko g==; X-CSE-ConnectionGUID: +fzhgmo7R0aE8H388pzKXA== X-CSE-MsgGUID: f2g6+0TpTaiW3pcmwLWwSw== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4838621" X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="4838621" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 01:35:28 -0800 X-CSE-ConnectionGUID: L5+8u2DVTQC/YFM7zdk7YA== X-CSE-MsgGUID: 99oIVS2LSqmI+37P0us9Nw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,244,1787036400"; d="scan'208";a="13359628" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Dec 2026 01:35:24 -0800 Message-ID: <52cf1381-d759-490e-aff1-b824372a77fe@linux.intel.com> Date: Tue, 1 Dec 2026 17:35:21 +0800 X-Mailing-List: kvm@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test To: Zide Chen , Sean Christopherson , Paolo Bonzini , Peter Zijlstra Cc: kvm@vger.kernel.org, Jim Mattson , Yosry Ahmed , Andi Kleen , Stephane Eranian , linux-kernel@vger.kernel.org, Mingwei Zhang , Das Sandipan , Shukla Manali , Xudong Hao References: <20261124213901.120572-1-zide.chen@intel.com> <20261124213901.120572-19-zide.chen@intel.com> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: <20261124213901.120572-19-zide.chen@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/25/2026 5:39 AM, Zide Chen wrote: > Add a dedicated selftest to verify Intel Topdown (PERF_METRICS) support > under the mediated vPMU. > > Verify across PMU versions and capability combinations: > - Positive Topdown counting. > - RDPMC Metrics Clear Mode. > - RDPMC (native and FEP), RDMSR, and WRMSR access to PERF_METRICS. > - Behavior when fixed counter 3 is absent. > - Behavior when PERF_CAP_PERF_METRICS is unadvertised. > - Host userspace save/restore via KVM_SET_MSRS and KVM_GET_MSRS. > > Signed-off-by: Zide Chen > --- > v10: > - Move Topdown metrics tests to a dedicated file to avoid bloating > pmu_counters_test.c. > - Add coverage for RDPMC Metrics Clear Mode, host MSR access, and > capability permutations. > --- > tools/arch/x86/include/asm/msr-index.h | 2 + > tools/testing/selftests/kvm/Makefile.kvm | 1 + > tools/testing/selftests/kvm/include/x86/pmu.h | 6 + > .../selftests/kvm/x86/pmu_perf_metrics_test.c | 391 ++++++++++++++++++ > 4 files changed, 400 insertions(+) > create mode 100644 tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c > > diff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/include/asm/msr-index.h > index 18c4be75e927..851f5c6b9ade 100644 > --- a/tools/arch/x86/include/asm/msr-index.h > +++ b/tools/arch/x86/include/asm/msr-index.h > @@ -331,7 +331,9 @@ > #define PERF_CAP_PEBS_FORMAT 0xf00 > #define PERF_CAP_FW_WRITES BIT_ULL(13) > #define PERF_CAP_PEBS_BASELINE BIT_ULL(14) > +#define PERF_CAP_PERF_METRICS BIT_ULL(15) > #define PERF_CAP_PEBS_TIMING_INFO BIT_ULL(17) > +#define PERF_CAP_RDPMC_METRICS_CLEAR BIT_ULL(19) > #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ > PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ > PERF_CAP_PEBS_TIMING_INFO) > diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm > index be5eb2dd85aa..71e0b36089e2 100644 > --- a/tools/testing/selftests/kvm/Makefile.kvm > +++ b/tools/testing/selftests/kvm/Makefile.kvm > @@ -106,6 +106,7 @@ TEST_GEN_PROGS_x86 += x86/nested_x2apic_test > TEST_GEN_PROGS_x86 += x86/platform_info_test > TEST_GEN_PROGS_x86 += x86/pmu_counters_test > TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test > +TEST_GEN_PROGS_x86 += x86/pmu_perf_metrics_test > TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test > TEST_GEN_PROGS_x86 += x86/private_mem_kvm_exits_test > TEST_GEN_PROGS_x86 += x86/save_restore_pf_stress_test > diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h > index c06ac78db01f..8ffb26a1d824 100644 > --- a/tools/testing/selftests/kvm/include/x86/pmu.h > +++ b/tools/testing/selftests/kvm/include/x86/pmu.h > @@ -50,11 +50,17 @@ > #define INTEL_RDPMC_FAST BIT_ULL(31) > > /* Fixed PMC controls, Intel only. */ > +#define FIXED_PMC_SLOTS_IDX 3 > #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx) BIT_ULL((32 + (_idx))) > > +/* PERF_METRICS enable, Intel only. */ > +#define GLOBAL_CTRL_EN_PERF_METRICS BIT_ULL(48) > + > #define FIXED_PMC_KERNEL BIT_ULL(0) > #define FIXED_PMC_USER BIT_ULL(1) > #define FIXED_PMC_ANYTHREAD BIT_ULL(2) > +/* Fixed counter 3 reuses bit 2 for METRICS_CLEAR_EN (Intel only). */ > +#define FIXED_PMC_METRICS_CLEAR BIT_ULL(2) > #define FIXED_PMC_ENABLE_PMI BIT_ULL(3) > #define FIXED_PMC_NR_BITS 4 > #define FIXED_PMC_CTRL(_idx, _val) ((_val) << ((_idx) * FIXED_PMC_NR_BITS)) > diff --git a/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c > new file mode 100644 > index 000000000000..07a6c948e4cd > --- /dev/null > +++ b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c > @@ -0,0 +1,391 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +/* > + * Test for KVM Topdown Microarchitecture Analysis (TMA) and PERF_METRICS. > + * > + * Copyright (C) 2026, Intel Corporation > + */ > + > +#include > + > +#include "processor.h" > +#include "pmu.h" > + > +/* Number of iterations of the loop for the guest measurement payload. */ > +#define NUM_LOOPS 10 > +#define PERF_METRICS_TEST_VAL 0x12345678ULL The sum of 4 metric fields is 0x114, which is far away from 0xff. Could we change it to an available value? > +#define SLOTS_TEST_VAL 0x12345ULL > + > +static void guest_test_rdpmc(u32 rdpmc_idx, bool expect_success, u64 expected_val) > +{ > + u8 vector; > + u64 val; > + > + vector = rdpmc_safe(rdpmc_idx, &val); > + __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR, > + "Expected %s on RDPMC(0x%x), got %s", > + expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector)); > + if (expect_success) > + GUEST_ASSERT_EQ(val, expected_val); > + > + if (!is_forced_emulation_enabled) > + return; > + > + vector = rdpmc_safe_fep(rdpmc_idx, &val); > + __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR, > + "Expected %s on FEP RDPMC(0x%x), got %s", > + expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector)); > + if (expect_success) > + GUEST_ASSERT_EQ(val, expected_val); > +} There is a similar guest_test_rdpmc() helper in pmu_counters_test.c. Better move the helper into a common header and then avoid the duplication. Others look good to me. Thanks. > + > +static void assert_rdpmc_metrics_cleared(u8 vector, u64 val) > +{ > + __GUEST_ASSERT(!vector, "Expected no fault on RDPMC(0x%llx), got %s", > + INTEL_RDPMC_METRICS, ex_str(vector)); > + GUEST_ASSERT_EQ(val, PERF_METRICS_TEST_VAL); > + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0); > + GUEST_ASSERT_EQ(rdmsr(MSR_CORE_PERF_FIXED_CTR3), 0); > +} > + > +static void guest_test_no_perf_metrics(void) > +{ > + u64 metrics; > + u8 vector; > + > + /* > + * When PERF_METRICS is not exposed to the guest, all accesses > + * must fault (#GP): MSR_PERF_METRICS, GLOBAL_CTRL bit 48, > + * and RDPMC of metrics type. > + */ > + vector = rdmsr_safe(MSR_PERF_METRICS, &metrics); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + vector = wrmsr_safe(MSR_PERF_METRICS, 0); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL, > + GLOBAL_CTRL_EN_PERF_METRICS); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + guest_test_rdpmc(INTEL_RDPMC_METRICS, false, 0); > + > + /* > + * If RDPMC Metrics Clear capability is set without PERF_METRICS, > + * setting METRICS_CLEAR_EN in FIXED_CTR_CTRL must still #GP. > + */ > + if (this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS) > FIXED_PMC_SLOTS_IDX) { > + vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, > + FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR)); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + } > +} > + > +static void guest_test_no_slots_counter(void) > +{ > + u8 vector; > + > + /* > + * Without fixed counter 3, all metric fields are reserved: reads > + * return 0, writing 0 succeeds, and non-zero writes #GP. > + */ > + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0); > + > + vector = wrmsr_safe(MSR_PERF_METRICS, 0); > + GUEST_ASSERT_EQ(vector, 0); > + > + vector = wrmsr_safe(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL, > + GLOBAL_CTRL_EN_PERF_METRICS); > + GUEST_ASSERT_EQ(vector, 0); > + > + vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR3, 0); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_METRICS_CLEAR)); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + > + guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0); > +} > + > +static void guest_test_full_perf_metrics(void) > +{ > + int retiring, bad_spec, fe_bound, be_bound, sum, loops; > + u64 global_ctrl, metrics, val; > + u8 vector; > + > + wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0); > + wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0); > + wrmsr(MSR_PERF_METRICS, 0); > + > + /* Enable fixed counter 3 (TOPDOWN.SLOTS) and PERF_METRICS. */ > + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_KERNEL)); > + global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(FIXED_PMC_SLOTS_IDX) | > + GLOBAL_CTRL_EN_PERF_METRICS; > + > + GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "", NUM_LOOPS); > + > + /* Check test results. */ > + metrics = rdmsr(MSR_PERF_METRICS); > + vector = rdpmc_safe(INTEL_RDPMC_METRICS, &val); > + GUEST_ASSERT_EQ(vector, 0); > + GUEST_ASSERT_EQ(val, metrics); > + > + retiring = FIELD_GET(GENMASK_ULL(7, 0), metrics); > + bad_spec = FIELD_GET(GENMASK_ULL(15, 8), metrics); > + fe_bound = FIELD_GET(GENMASK_ULL(23, 16), metrics); > + be_bound = FIELD_GET(GENMASK_ULL(31, 24), metrics); > + > + /* > + * Be conservative: the measured payload definitely retires work, so > + * Retiring should be non-zero. > + */ > + GUEST_ASSERT_NE(retiring, 0); > + > + /* > + * Each level-1 Topdown metric is an integer fraction of 0xff. > + * A +/-3 error margin is chosen for a loose sanity check. > + */ > + sum = retiring + bad_spec + fe_bound + be_bound; > + GUEST_ASSERT(sum >= 0xfc && sum <= 0x102); > + > + /* > + * Burn some cycles; MSR_CORE_PERF_GLOBAL_CTRL is left zeroed after > + * GUEST_RUN_PAYLOAD() returns, so PERF_METRICS is not counting. > + */ > + loops = NUM_LOOPS; > + > + __asm__ __volatile__("loop ." : "+c"(loops)); > + > + /* Confirm PERF_METRICS did not advance while counting was disabled. */ > + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics); > + > + /* > + * Only use the 4 level-1 metric fields (bits [31:0]), which are present > + * whether the CPU supports 4 or 8 Topdown events. > + */ > + wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + > + /* Force a VM-exit to verify KVM restores MSR_PERF_METRICS on VM-entry. */ > + GUEST_SYNC(0); > + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), PERF_METRICS_TEST_VAL); > + > + guest_test_rdpmc(INTEL_RDPMC_METRICS, true, PERF_METRICS_TEST_VAL); > + > + /* > + * Test RDPMC Metrics Clear Mode if supported, or verify that setting > + * METRICS_CLEAR_EN injects #GP if the capability is absent. > + */ > + if (rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_RDPMC_METRICS_CLEAR) { > + wrmsr(MSR_CORE_PERF_FIXED_CTR3, SLOTS_TEST_VAL); > + wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + > + /* Enable Metrics Clear Mode for fixed counter 3. */ > + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, > + FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR)); > + > + /* Confirm RDMSR does not clear either register. */ > + GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), PERF_METRICS_TEST_VAL); > + GUEST_ASSERT_EQ(rdmsr(MSR_CORE_PERF_FIXED_CTR3), SLOTS_TEST_VAL); > + > + /* RDPMC of PERF_METRICS must return the value and clear both. */ > + vector = rdpmc_safe(INTEL_RDPMC_METRICS, &val); > + assert_rdpmc_metrics_cleared(vector, val); > + > + if (is_forced_emulation_enabled) { > + wrmsr(MSR_CORE_PERF_FIXED_CTR3, SLOTS_TEST_VAL); > + wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + vector = rdpmc_safe_fep(INTEL_RDPMC_METRICS, &val); > + assert_rdpmc_metrics_cleared(vector, val); > + } > + > + /* Restore FIXED_CTR_CTRL to normal counting. */ > + wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_KERNEL)); > + } else { > + vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL, > + FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, > + FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR)); > + GUEST_ASSERT_EQ(vector, GP_VECTOR); > + } > +} > + > +static void guest_test_perf_metrics(void) > +{ > + if (!(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS)) > + guest_test_no_perf_metrics(); > + else if (this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS) <= FIXED_PMC_SLOTS_IDX) > + guest_test_no_slots_counter(); > + else > + guest_test_full_perf_metrics(); > + > + GUEST_DONE(); > +} > + > +static void run_vcpu(struct kvm_vcpu *vcpu) > +{ > + struct ucall uc; > + > + do { > + vcpu_run(vcpu); > + switch (get_ucall(vcpu, &uc)) { > + case UCALL_SYNC: > + break; > + case UCALL_ABORT: > + REPORT_GUEST_ASSERT(uc); > + break; > + case UCALL_PRINTF: > + pr_info("%s", uc.buffer); > + break; > + case UCALL_DONE: > + break; > + default: > + TEST_FAIL("Unexpected ucall: %lu", uc.cmd); > + } > + } while (uc.cmd != UCALL_DONE); > +} > + > +static void test_one_perf_metrics(u8 pmu_version, u64 perf_capabilities, > + u8 nr_fixed_counters) > +{ > + struct kvm_vcpu *vcpu; > + struct kvm_vm *vm; > + > + vm = vm_create_with_one_vcpu(&vcpu, guest_test_perf_metrics); > + > + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, perf_capabilities); > + > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_VERSION, pmu_version); > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, nr_fixed_counters); > + > + run_vcpu(vcpu); > + > + kvm_vm_free(vm); > +} > + > +static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities) > +{ > + u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS); > + > + pr_info("Testing Perf Metrics, PMU version %u, perf_caps = 0x%lx\n", > + pmu_version, perf_capabilities); > + > + /* Test with full/supported fixed counters (fixed counter 3 present if supported). */ > + test_one_perf_metrics(pmu_version, perf_capabilities, nr_fixed_counters); > + > + /* Test with fixed counter 3 absent. */ > + if (nr_fixed_counters > FIXED_PMC_SLOTS_IDX) > + test_one_perf_metrics(pmu_version, perf_capabilities, FIXED_PMC_SLOTS_IDX); > +} > + > +static void test_guest_perf_metrics(void) > +{ > + u8 pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION); > + const u64 perf_caps[] = { > + 0, > + PERF_CAP_PERF_METRICS, > + PERF_CAP_PERF_METRICS | PERF_CAP_RDPMC_METRICS_CLEAR, > + PERF_CAP_RDPMC_METRICS_CLEAR, > + }; > + u64 advertised_perf_caps; > + u8 max_pmu_version; > + unsigned int i; > + u8 v; > + > + max_pmu_version = max_t(typeof(pmu_version), pmu_version, 5); > + advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); > + > + for (v = 2; v <= max_pmu_version; v++) { > + for (i = 0; i < ARRAY_SIZE(perf_caps); i++) { > + /* Ignore unsupported features. */ > + if (perf_caps[i] & ~advertised_perf_caps) > + continue; > + > + test_perf_metrics(v, perf_caps[i]); > + } > + } > +} > + > +static void test_host_perf_metrics(void) > +{ > + u64 advertised_perf_caps; > + struct kvm_vcpu *vcpu; > + struct kvm_vm *vm; > + u64 val; > + int ret; > + > + advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES); > + if (!(advertised_perf_caps & PERF_CAP_PERF_METRICS)) > + return; > + > + vm = vm_create_with_one_vcpu(&vcpu, NULL); > + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, advertised_perf_caps); > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_VERSION, 2); > + > + /* > + * Case 1: Fixed counter 3 present and PERF_METRICS exposed. > + * Host can write 0 and valid non-zero values to MSR_PERF_METRICS. > + */ > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, > + FIXED_PMC_SLOTS_IDX + 1); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + TEST_ASSERT_EQ(ret, 1); > + val = vcpu_get_msr(vcpu, MSR_PERF_METRICS); > + TEST_ASSERT_EQ(val, PERF_METRICS_TEST_VAL); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0); > + TEST_ASSERT_EQ(ret, 1); > + val = vcpu_get_msr(vcpu, MSR_PERF_METRICS); > + TEST_ASSERT_EQ(val, 0); > + > + /* > + * Case 2: Fixed counter 3 absent. > + * Host writing 0 succeeds (for migration/probe), non-zero fails. > + */ > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, > + FIXED_PMC_SLOTS_IDX); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + TEST_ASSERT_EQ(ret, 0); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0); > + TEST_ASSERT_EQ(ret, 1); > + > + /* > + * Case 3: PERF_CAP_PERF_METRICS not exposed (even with fixed counter 3 present). > + * Non-zero writes must fail. Writing 0 succeeds as KVM allows > + * writing 0 to advertised feature MSRs for compatibility. > + */ > + vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, > + FIXED_PMC_SLOTS_IDX + 1); > + vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL); > + TEST_ASSERT_EQ(ret, 0); > + > + ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0); > + TEST_ASSERT_EQ(ret, 1); > + > + kvm_vm_free(vm); > +} > + > +int main(int argc, char *argv[]) > +{ > + TEST_REQUIRE(kvm_is_pmu_enabled()); > + > + TEST_REQUIRE(host_cpu_is_intel); > + TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION)); > + TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) >= 2); > + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM)); > + > + test_host_perf_metrics(); > + test_guest_perf_metrics(); > + > + return 0; > +}