From: Sean Christopherson <seanjc@google.com>
To: Colton Lewis <coltonlewis@google.com>
Cc: kvm@vger.kernel.org, Mingwei Zhang <mizhang@google.com>,
Jinrong Liang <ljr.kernel@gmail.com>,
Jim Mattson <jmattson@google.com>,
Aaron Lewis <aaronlewis@google.com>,
Paolo Bonzini <pbonzini@redhat.com>,
Shuah Khan <shuah@kernel.org>,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 6/6] KVM: x86: selftests: Test PerfMonV2
Date: Wed, 8 Jan 2025 11:42:43 -0800 [thread overview]
Message-ID: <Z37VMzbCZEKkDOmP@google.com> (raw)
In-Reply-To: <20240918205319.3517569-7-coltonlewis@google.com>
On Wed, Sep 18, 2024, Colton Lewis wrote:
> Test PerfMonV2, which defines global registers to enable multiple
> performance counters with a single MSR write, in its own function.
>
> If the feature is available, ensure the global control register has
> the ability to start and stop the performance counters and the global
> status register correctly flags an overflow by the associated counter.
>
> Signed-off-by: Colton Lewis <coltonlewis@google.com>
> ---
> .../selftests/kvm/x86_64/pmu_counters_test.c | 53 +++++++++++++++++++
> 1 file changed, 53 insertions(+)
>
> diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
> index cf2941cc7c4c..a90df8b67a19 100644
> --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
> +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c
> @@ -763,10 +763,63 @@ static void guest_test_core_events(void)
> }
> }
>
> +static void guest_test_perfmon_v2(void)
> +{
> + uint64_t i;
> + uint64_t eventsel = ARCH_PERFMON_EVENTSEL_OS |
> + ARCH_PERFMON_EVENTSEL_ENABLE |
> + AMD_ZEN_CORE_CYCLES;
Hmm. I like the extra coverage, but I think the guts of this test belong is
common code, because the core logic is the same across Intel and AMD (I think),
only the MSRs are different.
Maybe a library helper that takes in the MSRs as parameters? Not sure.
I suspect it'll take some back and forth to figure out how best to validate these
more "advanced" behaviors, so maybe skip this patch for the next version? I.e.
land basic AMD coverage and then we can figure out how to test global control and
status.
> + bool core_ext = this_cpu_has(X86_FEATURE_PERF_CTR_EXT_CORE);
> + uint64_t sel_msr_base = core_ext ? MSR_F15H_PERF_CTL : MSR_K7_EVNTSEL0;
> + uint64_t cnt_msr_base = core_ext ? MSR_F15H_PERF_CTR : MSR_K7_PERFCTR0;
> + uint64_t msr_step = core_ext ? 2 : 1;
> + uint8_t nr_counters = guest_nr_core_counters();
> + bool perfmon_v2 = this_cpu_has(X86_FEATURE_PERFMON_V2);
Zero reason to capture this in a local variable.
> + uint64_t sel_msr;
> + uint64_t cnt_msr;
> +
> + if (!perfmon_v2)
> + return;
> +
> + for (i = 0; i < nr_counters; i++) {
> + sel_msr = sel_msr_base + msr_step * i;
> + cnt_msr = cnt_msr_base + msr_step * i;
> +
> + /* Ensure count stays 0 when global register disables counter. */
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);
> + wrmsr(sel_msr, eventsel);
> + wrmsr(cnt_msr, 0);
> + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
> + GUEST_ASSERT(!_rdpmc(i));
> +
> + /* Ensure counter is >0 when global register enables counter. */
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, BIT_ULL(i));
> + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);
> + GUEST_ASSERT(_rdpmc(i));
> +
> + /* Ensure global status register flags a counter overflow. */
> + wrmsr(cnt_msr, -1);
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, 0xff);
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, BIT_ULL(i));
> + __asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_CTL, 0);
> + GUEST_ASSERT(rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS) &
> + BIT_ULL(i));
> +
> + /* Ensure global status register flag is cleared correctly. */
> + wrmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR, BIT_ULL(i));
> + GUEST_ASSERT(!(rdmsr(MSR_AMD64_PERF_CNTR_GLOBAL_STATUS) &
> + BIT_ULL(i)));
> + }
> +}
> +
> +
> static void guest_test_core_counters(void)
> {
> guest_test_rdwr_core_counters();
> guest_test_core_events();
> + guest_test_perfmon_v2();
> GUEST_DONE();
> }
>
> --
> 2.46.0.662.g92d0881bb0-goog
>
next prev parent reply other threads:[~2025-01-08 19:42 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-18 20:53 [PATCH v2 0/6] Extend pmu_counters_test to AMD CPUs Colton Lewis
2024-09-18 20:53 ` [PATCH v2 1/6] KVM: x86: selftests: Fix typos in macro variable use Colton Lewis
2024-09-18 20:53 ` [PATCH v2 2/6] KVM: x86: selftests: Define AMD PMU CPUID leaves Colton Lewis
2025-01-08 17:58 ` Sean Christopherson
2025-01-20 17:06 ` Colton Lewis
2024-09-18 20:53 ` [PATCH v2 3/6] KVM: x86: selftests: Set up AMD VM in pmu_counters_test Colton Lewis
2025-01-08 18:35 ` Sean Christopherson
2025-01-20 18:03 ` Colton Lewis
2025-01-21 20:56 ` Sean Christopherson
2024-09-18 20:53 ` [PATCH v2 4/6] KVM: x86: selftests: Test read/write core counters Colton Lewis
2025-01-08 18:54 ` Sean Christopherson
2025-01-20 19:54 ` Colton Lewis
2024-09-18 20:53 ` [PATCH v2 5/6] KVM: x86: selftests: Test core events Colton Lewis
2025-01-08 19:31 ` Sean Christopherson
2025-01-20 20:01 ` Colton Lewis
2024-09-18 20:53 ` [PATCH v2 6/6] KVM: x86: selftests: Test PerfMonV2 Colton Lewis
2025-01-08 19:42 ` Sean Christopherson [this message]
2025-01-20 20:07 ` Colton Lewis
2024-10-31 20:09 ` [PATCH v2 0/6] Extend pmu_counters_test to AMD CPUs Colton Lewis
2025-01-09 19:47 ` Sean Christopherson
2025-01-20 20:11 ` Colton Lewis
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=Z37VMzbCZEKkDOmP@google.com \
--to=seanjc@google.com \
--cc=aaronlewis@google.com \
--cc=coltonlewis@google.com \
--cc=jmattson@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=ljr.kernel@gmail.com \
--cc=mizhang@google.com \
--cc=pbonzini@redhat.com \
--cc=shuah@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).