Re: [PATCH v4 6/6] KVM: selftests: Add svm_pmu_host_guest_test for Host-Only/Guest-Only bits

Sean Christopherson
2026-04-07T01:39:04Z

On Thu, Mar 26, 2026, Yosry Ahmed wrote:
> From: Jim Mattson <jmattson@google.com>
>
> Add a selftest to verify KVM correctly virtualizes the AMD PMU Host-Only
> (bit 41) and Guest-Only (bit 40) event selector bits across all relevant
> SVM state transitions.
>
> The test programs 4 PMCs simultaneously with all combinations of the
> Host-Only and Guest-Only bits, then verifies correct counting behavior:
> 1. SVME=0: all counters count (Host-Only/Guest-Only bits ignored)
> 2. Set SVME=1: Host-Only and neither/both count; Guest-Only stops
> 3. VMRUN to L2: Guest-Only and neither/both count; Host-Only stops
> 4. VMEXIT to L1: Host-Only and neither/both count; Guest-Only stops
> 5. Clear SVME=0: all counters count (bits ignored again)
>
> Signed-off-by: Jim Mattson <jmattson@google.com>
> Signed-off-by: Yosry Ahmed <yosry@kernel.org>
> ---
> tools/testing/selftests/kvm/Makefile.kvm | 1 +
> tools/testing/selftests/kvm/include/x86/pmu.h | 6 +
> .../selftests/kvm/include/x86/processor.h | 2 +
> .../kvm/x86/svm_pmu_host_guest_test.c | 199 ++++++++++++++++++
> 4 files changed, 208 insertions(+)
> create mode 100644 tools/testing/selftests/kvm/x86/svm_pmu_host_guest_test.c
>
> diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
> index 3d372d78a2756..9418c45291231 100644
> --- a/tools/testing/selftests/kvm/Makefile.kvm
> +++ b/tools/testing/selftests/kvm/Makefile.kvm
> @@ -116,6 +116,7 @@ TEST_GEN_PROGS_x86 += x86/svm_nested_invalid_vmcb12_gpa
> TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
> TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
> TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state
> +TEST_GEN_PROGS_x86 += x86/svm_pmu_host_guest_test
> TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
> TEST_GEN_PROGS_x86 += x86/sync_regs_test
> TEST_GEN_PROGS_x86 += x86/ucna_injection_test
> diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h
> index 72575eadb63a0..af9b279c78df4 100644
> --- a/tools/testing/selftests/kvm/include/x86/pmu.h
> +++ b/tools/testing/selftests/kvm/include/x86/pmu.h
> @@ -38,6 +38,12 @@
> #define ARCH_PERFMON_EVENTSEL_INV BIT_ULL(23)
> #define ARCH_PERFMON_EVENTSEL_CMASK GENMASK_ULL(31, 24)
>
> +/*
> + * These are AMD-specific bits.
> + */
> +#define AMD64_EVENTSEL_GUESTONLY BIT_ULL(40)
> +#define AMD64_EVENTSEL_HOSTONLY BIT_ULL(41)
> +
> /* RDPMC control flags, Intel only. */
> #define INTEL_RDPMC_METRICS BIT_ULL(29)
> #define INTEL_RDPMC_FIXED BIT_ULL(30)
> diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h
> index d8634a760a609..4cc1ba8752347 100644
> --- a/tools/testing/selftests/kvm/include/x86/processor.h
> +++ b/tools/testing/selftests/kvm/include/x86/processor.h
> @@ -19,6 +19,8 @@
> #include "kvm_util.h"
> #include "ucall_common.h"
>
> +#define __stack_aligned__ __aligned(16)

I would much prefer to provide a macro helper to declare the stack in a prep patch,
and update the bajillion instances of "unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]"
through KVM selftests.

A blurb in the changelog explaining why _this_ test needs to honor alignment
while we've managed to squeak by without problems in other tests would also be
helpful

> +
> extern bool host_cpu_is_intel;
> extern bool host_cpu_is_amd;
> extern bool host_cpu_is_hygon;