git: 9462179a2e7d - main - hwpmc_amd: Allow only system counting on L3, DF and UMC counters

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

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

commit 9462179a2e7dd483c8593edd761e211e65476838
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-26 14:24:03 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-26 14:25:44 +0000

    hwpmc_amd: Allow only system counting on L3, DF and UMC counters
    
    These counters are shared by several CPUs, a CCX in the case of L3.  A
    process-mode PMC is loaded on every CPU that runs a target thread, so
    two CPUs can program the same counter.  Without PerfMonV2, an INVARIANTS
    kernel then panics with "Starting active PMC"; otherwise the counts are
    wrong.  No privilege is needed: on Zen 1-3, "pmcstat -P LLC_MISSES" is
    enough.  System sampling is accepted too, but no interrupt handler
    checks these counters.
    
    Refuse every mode but PMC_MODE_SC, as the RAPL, PERF and TSC classes do.
    Two system-mode PMCs on CPUs that share a counter can still conflict;
    that is left to userland, as before.
    
    PR:             296001
    Reported by:    asomers
    Reviewed by:    mhorne
    Sponsored by:   Advanced Micro Devices, Inc.
    Signed-off-by:  Osvaldo Janeri Filho <ojanerif@amd.com>
    Differential Revision:  https://reviews.freebsd.org/D61949
---
 sys/dev/hwpmc/hwpmc_amd.c             | 10 ++++
 tests/sys/pmc/amd/pmc_rowalloc_test.c | 97 +++++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index 3274a2bbec16..5706f3056568 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -596,6 +596,16 @@ amd_can_assign_pmc(int ri, struct pmc *pm, const struct pmc_op_pmcallocate *a)
 	if (amd_pmcdesc[ri].pm_subclass != a->pm_md.pm_amd.pm_amd_sub_class)
 		return (EINVAL);
 
+	/*
+	 * The L3, DF and UMC counters are shared by several CPUs, so a
+	 * process-mode PMC would be loaded on more than one of them, and
+	 * amd_intr() does not handle their overflow.
+	 */
+	if (amd_pmcdesc[ri].pm_subclass != PMC_AMD_SUB_CLASS_CORE &&
+	    a->pm_mode != PMC_MODE_SC)
+		return (EXTERROR(EINVAL,
+		    "AMD L3, DF and UMC counters support only system counting"));
+
 	if (((caps & PMC_CAP_PRECISE) != 0) &&
 	    ((pd->pd_caps & PMC_CAP_PRECISE) == 0))
 		return (EINVAL);
diff --git a/tests/sys/pmc/amd/pmc_rowalloc_test.c b/tests/sys/pmc/amd/pmc_rowalloc_test.c
index cfd6aa3bb628..16c4af489773 100644
--- a/tests/sys/pmc/amd/pmc_rowalloc_test.c
+++ b/tests/sys/pmc/amd/pmc_rowalloc_test.c
@@ -14,6 +14,7 @@
 #include <atf-c.h>
 
 #include <errno.h>
+#include <stdbool.h>
 #include <pmc.h>
 #include <stdlib.h>
 #include <string.h>
@@ -277,11 +278,107 @@ ATF_TC_BODY(thread_rows_refuse_system_pmc, tc)
 	amd_test_release_pmc(sys);
 }
 
+/*
+ * Skip unless hwpmc has L3 rows on cpu0, and return an L3 event of this CPU.
+ */
+static const char *
+l3_event(void)
+{
+	static const char *const events[] = {
+		"l3_lookup_state.all_coherent_accesses_to_l3",	/* Zen 4+ */
+		"l3_request_g1.caching_l3_cache_accesses",	/* Zen 1-3 */
+	};
+	struct pmc_pmcinfo *pmcinfo;
+	bool found;
+	size_t i;
+	int npmc;
+
+	ATF_REQUIRE_MSG(pmc_pmcinfo(0, &pmcinfo) == 0,
+	    "pmc_pmcinfo(0) failed: %s", strerror(errno));
+	npmc = pmc_npmc(0);
+	ATF_REQUIRE(npmc > 0);
+	found = false;
+	for (i = 0; i < (size_t)npmc; i++)
+		if (strncmp(pmcinfo->pm_pmcs[i].pm_name, "AMD-L3-", 7) == 0)
+			found = true;
+	free(pmcinfo);
+	if (!found)
+		atf_tc_skip("hwpmc has no AMD L3 counters");
+
+	for (i = 0; i < nitems(events); i++)
+		if (pmc_pmu_idx_get_by_event(NULL, events[i]) >= 0)
+			return (events[i]);
+	atf_tc_skip("no known L3 event in this CPU's pmu-events table");
+}
+
+ATF_TC(l3_process_mode_refused);
+ATF_TC_HEAD(l3_process_mode_refused, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "L3 counters are shared by a CCX, so process-mode and sampling "
+	    "PMCs on them are refused with EINVAL (PR 296001)");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(l3_process_mode_refused, tc)
+{
+	static const enum pmc_mode modes[] = {
+		PMC_MODE_TC, PMC_MODE_TS, PMC_MODE_SS
+	};
+	const char *ev;
+	pmc_id_t id;
+	size_t i;
+	int rv;
+
+	skip_unless_ready();
+	ev = l3_event();
+
+	for (i = 0; i < nitems(modes); i++) {
+		id = PMC_ID_INVALID;
+		rv = pmc_allocate(ev, modes[i], 0,
+		    PMC_IS_SYSTEM_MODE(modes[i]) ? 0 : PMC_CPU_ANY, &id,
+		    PMC_IS_SAMPLING_MODE(modes[i]) ? 100000 : 0);
+		if (rv == 0)
+			amd_test_release_pmc(id);
+		ATF_CHECK_MSG(rv != 0 && errno == EINVAL,
+		    "%s %s allocation: rv %d, %s", pmc_name_of_mode(modes[i]),
+		    ev, rv, rv == 0 ? "accepted" : strerror(errno));
+	}
+}
+
+ATF_TC(l3_system_mode_allowed);
+ATF_TC_HEAD(l3_system_mode_allowed, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "A system-mode counting PMC on an L3 counter can be allocated, "
+	    "started and read");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+ATF_TC_BODY(l3_system_mode_allowed, tc)
+{
+	const char *ev;
+	pmc_value_t v;
+	pmc_id_t id;
+
+	skip_unless_ready();
+	ev = l3_event();
+
+	ATF_REQUIRE_MSG(pmc_allocate(ev, PMC_MODE_SC, 0, 0, &id, 0) == 0,
+	    "pmc_allocate(%s, SC) failed: %s", ev, strerror(errno));
+	ATF_REQUIRE_MSG(pmc_start(id) == 0, "pmc_start failed: %s",
+	    strerror(errno));
+	ATF_REQUIRE_MSG(pmc_read(id, &v) == 0, "pmc_read failed: %s",
+	    strerror(errno));
+	ATF_REQUIRE(pmc_stop(id) == 0);
+	amd_test_release_pmc(id);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 	ATF_TP_ADD_TC(tp, alloc_reserves_distinct_rows);
 	ATF_TP_ADD_TC(tp, failed_alloc_leaves_no_trace);
 	ATF_TP_ADD_TC(tp, thread_rows_refuse_system_pmc);
+	ATF_TP_ADD_TC(tp, l3_process_mode_refused);
+	ATF_TP_ADD_TC(tp, l3_system_mode_allowed);
 
 	return (atf_no_error());
 }