All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Mi, Dapeng" <dapeng1.mi@linux.intel.com>
To: "Chen, Zide" <zide.chen@intel.com>,
	Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>
Cc: kvm@vger.kernel.org, Jim Mattson <jmattson@google.com>,
	Yosry Ahmed <yosry@kernel.org>, Andi Kleen <ak@linux.intel.com>,
	Stephane Eranian <eranian@google.com>,
	linux-kernel@vger.kernel.org, Mingwei Zhang <mizhang@google.com>,
	Das Sandipan <Sandipan.Das@amd.com>,
	Shukla Manali <Manali.Shukla@amd.com>,
	Xudong Hao <xudong.hao@intel.com>
Subject: Re: [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test
Date: Wed, 2 Dec 2026 08:27:17 +0800	[thread overview]
Message-ID: <5ec75901-7128-44f4-bc3f-0c4bba677dc8@linux.intel.com> (raw)
In-Reply-To: <cae348d8-6fc0-4035-b742-863965dc0474@intel.com>


On 12/2/2026 2:21 AM, Chen, Zide wrote:
>
> On 12/1/2026 3:35 AM, Mi, Dapeng wrote:
>
>>> 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 <linux/bitfield.h>
>>> +
>>> +#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?
> Nice catch!
>
> While WRMSR does not enforce the arithmetic sum of the Level-1
> metric fields (only validating reserved bits), using an architecturally
> valid value whose Level-1 metrics sum to 0xff avoids confusion.

It's true that the PERF_METRICS MSR allows to write any data, but the
invalid starting data could lead to the subsequent calculation is corrupted.


>
> I will update it to 0x12345663ULL (0x12 + 0x34 + 0x56 + 0x63 == 0xff).
>
>>> +#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.
> guest_test_rdpmc() is just ~15 lines of test assertion glue. Moving it
> into pmu.h requires exporting ad-hoc assertion macros like
> GUEST_ASSERT_PMC_MSR_ACCESS and GUEST_ASSERT_PMC_VALUE, and it encodes
> test-specific expectations (#GP vs exact match) into an architectural
> header.
>
> I guess in selftests, a little duplicated assertion is often preferred
> over polluting common headers. This is unlike GUEST_MEASURE_EVENT(),
> which is a complex inline assembly primitive where duplication would be
> error-prone and a maintenance burden.
>
> That said, if you still feel strongly about deduplicating it, I am open
> to moving it.

guest_test_rdpmc() is a general function and could be used again in the new test cases. I suppose we'd better move it into a common header.
Thanks.


>> Others look good to me. Thanks.

      reply	other threads:[~2026-12-02  0:28 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
2026-12-01  5:39   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests Zide Chen
2026-12-01  5:43   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities Zide Chen
2026-12-01  6:01   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM Zide Chen
2026-12-01  6:03   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events " Zide Chen
2026-11-24 21:38 ` [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Zide Chen
2026-11-24 21:38 ` [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR Zide Chen
2026-12-01  6:07   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Zide Chen
2026-11-24 21:38 ` [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
2026-12-01  6:15   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS Zide Chen
2026-12-01  6:22   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Zide Chen
2026-11-24 21:38 ` [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics Zide Chen
2026-11-24 21:38 ` [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode Zide Chen
2026-12-01  6:50   ` Mi, Dapeng
2026-11-24 21:38 ` [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands Zide Chen
2026-11-24 21:39 ` [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers Zide Chen
2026-11-24 21:39 ` [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test Zide Chen
2026-12-01  9:35   ` Mi, Dapeng
2026-12-01 18:21     ` Chen, Zide
2026-12-02  0:27       ` Mi, Dapeng [this message]

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=5ec75901-7128-44f4-bc3f-0c4bba677dc8@linux.intel.com \
    --to=dapeng1.mi@linux.intel.com \
    --cc=Manali.Shukla@amd.com \
    --cc=Sandipan.Das@amd.com \
    --cc=ak@linux.intel.com \
    --cc=eranian@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mizhang@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=xudong.hao@intel.com \
    --cc=yosry@kernel.org \
    --cc=zide.chen@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.