git: a87de9c045db - main - tests/sys/pmc/amd: Skip in guests without a PMU

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 14:12:07 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=a87de9c045db50dac1550fad513d93f23ba86b86

commit a87de9c045db50dac1550fad513d93f23ba86b86
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-26 14:04:11 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-26 14:12:03 +0000

    tests/sys/pmc/amd: Skip in guests without a PMU
    
    In a guest that shows an AMD Zen CPU but has no PMU, hwpmc(4) registers
    neither the AMD core class nor IBS.  pmc_rowalloc_test, pmu_mux_test and
    pmu_detect_test check only the CPU and pmc_init(), and
    pmc_ibs_fetch_op_test checks only CPUID leaf 0x8000001B, which bhyve
    passes through.  All four fail there instead of skipping.
    
    Add amd_test_skip_unless_class() and use it in pmc_rowalloc_test,
    pmu_mux_test and pmc_ibs_fetch_op_test.  hwpmc_amd registers a class
    only after amd_hwcheck() passes, so a missing class means there are no
    usable counters; rapl_domains_test already skips that way.
    pmu_detect_test asserts that the class is registered, so gate it on
    PerfCtrExtCore instead, which bhyve clears and KVM sets only with a
    vPMU.
    
    PR:             300856
    Reviewed by:    mhorne
    Fixes:          546a76463123 ("tests/sys/pmc: add pidtrack, callchain, pmc(8), RAPL and AMD tests")
    Fixes:          7c743e3ba6c1 ("tests/sys/pmc/amd: add IBS, row allocation, PerfEvtSel and PerfMonV2 tests")
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D61691
---
 tests/sys/pmc/amd/amd_pmc_test_common.h   | 17 +++++++++++++++++
 tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c |  1 +
 tests/sys/pmc/amd/pmc_rowalloc_test.c     |  1 +
 tests/sys/pmc/amd/pmu_detect_test.c       |  6 +++++-
 tests/sys/pmc/amd/pmu_mux_test.c          |  1 +
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/tests/sys/pmc/amd/amd_pmc_test_common.h b/tests/sys/pmc/amd/amd_pmc_test_common.h
index e2c82546ad4a..ea43f3384264 100644
--- a/tests/sys/pmc/amd/amd_pmc_test_common.h
+++ b/tests/sys/pmc/amd/amd_pmc_test_common.h
@@ -243,6 +243,23 @@ amd_test_find_classinfo(const struct pmc_cpuinfo *cpuinfo, enum pmc_class cl)
 	return (NULL);
 }
 
+/*
+ * hwpmc_amd registers its classes only after amd_hwcheck() passes, so a
+ * missing class means there are no usable counters (a guest without a PMU,
+ * or counters held by firmware).
+ */
+static inline void
+amd_test_skip_unless_class(enum pmc_class cl)
+{
+	const struct pmc_cpuinfo *cpuinfo;
+
+	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
+	    "pmc_cpuinfo() failed: %s", strerror(errno));
+	if (amd_test_find_classinfo(cpuinfo, cl) == NULL)
+		atf_tc_skip("hwpmc did not register %s",
+		    pmc_name_of_class(cl));
+}
+
 static inline size_t
 amd_test_count_pmc_rows_with_prefix(int cpu, const char *prefix)
 {
diff --git a/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c b/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c
index 02025ff636e7..8210c0764905 100644
--- a/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c
+++ b/tests/sys/pmc/amd/pmc_ibs_fetch_op_test.c
@@ -263,6 +263,7 @@ setup(void)
 	    (CPUID_IBSID_FETCHSAM | CPUID_IBSID_OPSAM))
 		atf_tc_skip("CPU lacks IBS Fetch or Op sampling");
 	amd_test_skip_unless_hwpmc();
+	amd_test_skip_unless_class(PMC_CLASS_IBS);
 
 	ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1,
 	    sizeof(mask), &mask) == 0);
diff --git a/tests/sys/pmc/amd/pmc_rowalloc_test.c b/tests/sys/pmc/amd/pmc_rowalloc_test.c
index c578046e8128..cfd6aa3bb628 100644
--- a/tests/sys/pmc/amd/pmc_rowalloc_test.c
+++ b/tests/sys/pmc/amd/pmc_rowalloc_test.c
@@ -83,6 +83,7 @@ skip_unless_ready(void)
 
 	amd_test_skip_unless_zen(&cpu);
 	amd_test_skip_unless_hwpmc();
+	amd_test_skip_unless_class(PMC_CLASS_AMD);
 	if (!pmc_pmu_enabled())
 		atf_tc_skip("pmu-events not enabled");
 }
diff --git a/tests/sys/pmc/amd/pmu_detect_test.c b/tests/sys/pmc/amd/pmu_detect_test.c
index 277f7432e7ed..03a12bb0c716 100644
--- a/tests/sys/pmc/amd/pmu_detect_test.c
+++ b/tests/sys/pmc/amd/pmu_detect_test.c
@@ -9,7 +9,8 @@
 /*
  * Checks that hwpmc registers the AMD core PMU on Zen CPUs: the K8 class is
  * present with 48-bit counters and at least six core rows, alongside the
- * TSC class.
+ * TSC class.  bhyve clears PerfCtrExtCore and KVM sets it only with a vPMU,
+ * so the cases are gated on that bit rather than on the class.
  */
 
 #include <sys/param.h>
@@ -40,6 +41,7 @@ ATF_TC_BODY(pmu_detect_k8_class_present, tc)
 	struct amd_test_cpu cpu;
 
 	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_perfctr_extcore();
 	amd_test_skip_unless_hwpmc();
 
 	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
@@ -63,6 +65,7 @@ ATF_TC_BODY(pmu_detect_counter_width, tc)
 	struct amd_test_cpu cpu;
 
 	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_perfctr_extcore();
 	amd_test_skip_unless_hwpmc();
 
 	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
@@ -112,6 +115,7 @@ ATF_TC_BODY(pmu_detect_tsc_class_coexists, tc)
 	struct amd_test_cpu cpu;
 
 	amd_test_skip_unless_zen(&cpu);
+	amd_test_skip_unless_perfctr_extcore();
 	amd_test_skip_unless_hwpmc();
 
 	ATF_REQUIRE_MSG(pmc_cpuinfo(&cpuinfo) == 0,
diff --git a/tests/sys/pmc/amd/pmu_mux_test.c b/tests/sys/pmc/amd/pmu_mux_test.c
index 14369c46efb4..104b0e6fec0b 100644
--- a/tests/sys/pmc/amd/pmu_mux_test.c
+++ b/tests/sys/pmc/amd/pmu_mux_test.c
@@ -39,6 +39,7 @@ core_counters_or_skip(void)
 
 	amd_test_skip_unless_zen(&cpu);
 	amd_test_skip_unless_hwpmc();
+	amd_test_skip_unless_class(PMC_CLASS_AMD);
 	if (!pmc_pmu_enabled())
 		atf_tc_skip("pmu-events not enabled");
 	n = amd_test_core_counters();