git: c0c2c80b647f - main - hwpmc_amd: Service the core counters when IBS claims the NMI

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 14:29:23 UTC
The branch main has been updated by mhorne:

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

commit c0c2c80b647f712798c507cc76cbdf882c132a9c
Author:     Osvaldo Janeri Filho <ojanerif@amd.com>
AuthorDate: 2026-11-23 14:28:28 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-23 14:29:21 +0000

    hwpmc_amd: Service the core counters when IBS claims the NMI
    
    IBS and the core counters share the PMC NMI.  amd_intr() and
    amd_intr_v2() return as soon as pmc_ibs_intr() claims an NMI, so a core
    counter overflow that arrives with the same NMI is not serviced: the
    counter is not reloaded and sampling with it stops.
    
    Service the core counters after pmc_ibs_intr() and claim the NMI if
    either side found work.  An NMI that IBS claimed no longer uses up a
    core stray-NMI credit.  With no core counter sampling, amd_intr_v2()
    still returns right after IBS, without freezing the core counters.
    
    Add a test that samples unhalted cycles on cpu0 alone and then next to
    IBS op, and requires the second phase to keep at least half of the
    core samples.
    
    Reviewed by:    mhorne
    Sponsored by:   Advanced Micro Devices, Inc.
    Differential Revision:  https://reviews.freebsd.org/D61451
---
 sys/dev/hwpmc/hwpmc_amd.c         |  48 ++++++----
 tests/sys/pmc/amd/Makefile        |   2 +
 tests/sys/pmc/amd/ibs_core_test.c | 183 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 216 insertions(+), 17 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
index 101c583352fd..3274a2bbec16 100644
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -948,7 +948,7 @@ amd_intr(struct trapframe *tf)
 	pmc_value_t v;
 	uint64_t config, evsel, perfctr;
 	uint32_t active = 0, count = 0;
-	int i, error, retval, cpu;
+	int i, error, ibs, retval, cpu;
 
 	cpu = curcpu;
 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
@@ -960,9 +960,13 @@ amd_intr(struct trapframe *tf)
 
 	pac = amd_pcpu[cpu];
 
-	retval = pmc_ibs_intr(tf);
-	if (retval)
-		goto done;
+	/*
+	 * IBS and the core counters share the NMI, and one NMI can stand for
+	 * both.  Service the core counters even when IBS claims the NMI: an
+	 * overflow that is not serviced is never reloaded, and its counter
+	 * stops sampling.
+	 */
+	ibs = pmc_ibs_intr(tf);
 
 	/*
 	 * look for all PMCs that have interrupted:
@@ -1037,14 +1041,16 @@ amd_intr(struct trapframe *tf)
 	 */
 	if (retval) {
 		DPCPU_SET(nmi_counter, min(2, active));
-	} else {
+	} else if (ibs == 0) {
+		/* An NMI claimed by IBS does not use up a core credit. */
 		if ((count = DPCPU_GET(nmi_counter))) {
 			retval = 1;
 			DPCPU_SET(nmi_counter, --count);
 		}
 	}
+	if (ibs != 0)
+		retval = 1;
 
-done:
 	if (retval)
 		counter_u64_add(pmc_stats.pm_intr_processed, 1);
 	else
@@ -1066,7 +1072,7 @@ amd_intr_v2(struct trapframe *tf)
 	pmc_value_t v;
 	uint64_t status, pending;
 	uint32_t active = 0, count = 0;
-	int i, error, retval, cpu;
+	int i, error, ibs, retval, cpu;
 
 	cpu = curcpu;
 	KASSERT(cpu >= 0 && cpu < pmc_cpu_max(),
@@ -1077,15 +1083,8 @@ amd_intr_v2(struct trapframe *tf)
 	retval = 0;
 	pac = amd_pcpu[cpu];
 
-	retval = pmc_ibs_intr(tf);
-	if (retval)
-		goto done;
-
-	amd_v2_freeze_core(cpu);
-
-	/* Read the overflow bitmap once. */
-	status = rdmsr(AMD_PMC_GLOBAL_STATUS);
-	status &= amd_global_cntr_mask;
+	/* Service the core counters even when IBS claims the NMI. */
+	ibs = pmc_ibs_intr(tf);
 
 	/*
 	 * Count all active sampling PMCs, not just the ones that
@@ -1098,6 +1097,18 @@ amd_intr_v2(struct trapframe *tf)
 			active++;
 	}
 
+	/* An IBS NMI with no core counter sampling has nothing more to do. */
+	if (ibs != 0 && active == 0) {
+		retval = 1;
+		goto done;
+	}
+
+	amd_v2_freeze_core(cpu);
+
+	/* Read the overflow bitmap once. */
+	status = rdmsr(AMD_PMC_GLOBAL_STATUS);
+	status &= amd_global_cntr_mask;
+
 	/* ffsll() returns a 1-based bit index, or 0 when no bits are set. */
 	pending = status;
 	while ((i = ffsll(pending) - 1) != -1) {
@@ -1140,12 +1151,15 @@ amd_intr_v2(struct trapframe *tf)
 	 */
 	if (retval) {
 		DPCPU_SET(nmi_counter, min(2, active));
-	} else {
+	} else if (ibs == 0) {
+		/* An NMI claimed by IBS does not use up a core credit. */
 		if ((count = DPCPU_GET(nmi_counter))) {
 			retval = 1;
 			DPCPU_SET(nmi_counter, --count);
 		}
 	}
+	if (ibs != 0)
+		retval = 1;
 
 done:
 	if (retval)
diff --git a/tests/sys/pmc/amd/Makefile b/tests/sys/pmc/amd/Makefile
index 577517b1aa9e..e007568c22de 100644
--- a/tests/sys/pmc/amd/Makefile
+++ b/tests/sys/pmc/amd/Makefile
@@ -1,5 +1,6 @@
 TESTSDIR=	${TESTSBASE}/sys/pmc/amd
 
+ATF_TESTS_C+=	ibs_core_test
 ATF_TESTS_C+=	pmc_ibs_fetch_op_test
 ATF_TESTS_C+=	pmc_ibs_test
 ATF_TESTS_C+=	pmc_rowalloc_test
@@ -14,6 +15,7 @@ ATF_TESTS_SH+=	pmc_frontend_test
 ATF_TESTS_SH+=	pmcstat_ibs_test
 
 # These need the counters of cpu0 to themselves.
+TEST_METADATA.ibs_core_test+=		is_exclusive="true"
 TEST_METADATA.pmc_ibs_fetch_op_test+=	is_exclusive="true"
 TEST_METADATA.pmc_ibs_test+=		is_exclusive="true"
 TEST_METADATA.pmc_rowalloc_test+=	is_exclusive="true"
diff --git a/tests/sys/pmc/amd/ibs_core_test.c b/tests/sys/pmc/amd/ibs_core_test.c
new file mode 100644
index 000000000000..dfaab8b93bc6
--- /dev/null
+++ b/tests/sys/pmc/amd/ibs_core_test.c
@@ -0,0 +1,183 @@
+/*
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Author: Osvaldo Janeri Filho <ojanerif@amd.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * Core counter sampling next to IBS op sampling.  IBS and the core counters
+ * share the PMC NMI.  amd_intr() and amd_intr_v2() used to return as soon as
+ * the IBS handler claimed an NMI, so a core counter overflow delivered with
+ * the same NMI was not serviced: the counter was not reloaded and stopped
+ * sampling.  Sample unhalted cycles on a busy cpu0, first alone and then
+ * together with IBS op, and require the second phase to collect at least
+ * half as many core samples as the first.
+ */
+
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <atf-c.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <pmc.h>
+#include <pmclog.h>
+#include <stdint.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "amd_pmc_test_common.h"
+
+#define	PHASE_SECONDS	2
+#define	CORE_RATE	1000000
+#define	IBS_RATE	65536
+
+#define	LOG_FILE	"pmc.log"
+
+static const char *const core_events[] = {
+	"unhalted-cycles", "unhalted-core-cycles", "ls_not_halted_cyc", NULL
+};
+
+static pmc_id_t
+alloc_core(void)
+{
+	pmc_id_t id;
+	int k;
+
+	for (k = 0; core_events[k] != NULL; k++) {
+		if (pmc_allocate(core_events[k], PMC_MODE_SS, 0, 0, &id,
+		    CORE_RATE) == 0)
+			return (id);
+	}
+	atf_tc_skip("no core cycle counter can sample on cpu0: %s",
+	    strerror(errno));
+}
+
+/* Keep cpu0 busy, so that the cycle counter keeps counting. */
+static void
+spin(void)
+{
+	struct timespec end, now;
+
+	ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &end) == 0);
+	end.tv_sec += PHASE_SECONDS;
+	do {
+		ATF_REQUIRE(clock_gettime(CLOCK_MONOTONIC, &now) == 0);
+	} while (now.tv_sec < end.tv_sec ||
+	    (now.tv_sec == end.tv_sec && now.tv_nsec < end.tv_nsec));
+}
+
+/*
+ * pmc_close_logfile() only starts the close, and the log thread writes the
+ * rest of the log afterwards: wait until the file stops growing.
+ */
+static void
+wait_log_written(int fd)
+{
+	struct stat st;
+	off_t last;
+	int i, stable;
+
+	last = -1;
+	stable = 0;
+	for (i = 0; i < 100 && stable < 5; i++) {
+		usleep(100000);
+		ATF_REQUIRE(fstat(fd, &st) == 0);
+		if (st.st_size == last)
+			stable++;
+		else
+			stable = 0;
+		last = st.st_size;
+	}
+	ATF_REQUIRE_MSG(stable == 5, "the log was still growing after 10 s");
+}
+
+/* Count the samples logged for PMCs a and b. */
+static void
+count_samples(int fd, pmc_id_t a, pmc_id_t b, uintmax_t *na, uintmax_t *nb)
+{
+	struct pmclog_ev ev;
+	void *log;
+
+	*na = *nb = 0;
+	ATF_REQUIRE(lseek(fd, 0, SEEK_SET) == 0);
+	log = pmclog_open(fd);
+	ATF_REQUIRE_MSG(log != NULL, "pmclog_open: %s", strerror(errno));
+	while (pmclog_read(log, &ev) == 0) {
+		if (ev.pl_type != PMCLOG_TYPE_CALLCHAIN)
+			continue;
+		if (ev.pl_u.pl_cc.pl_pmcid == a)
+			(*na)++;
+		else if (ev.pl_u.pl_cc.pl_pmcid == b)
+			(*nb)++;
+	}
+	ATF_CHECK_MSG(ev.pl_state == PMCLOG_EOF,
+	    "the log did not parse to its end (state %d)", ev.pl_state);
+	pmclog_close(log);
+}
+
+ATF_TC(core_sampling_with_ibs);
+ATF_TC_HEAD(core_sampling_with_ibs, tc)
+{
+	atf_tc_set_md_var(tc, "descr",
+	    "A core counter keeps sampling while IBS op samples the same CPU");
+	atf_tc_set_md_var(tc, "require.user", "root");
+}
+
+ATF_TC_BODY(core_sampling_with_ibs, tc)
+{
+	pmc_id_t alone, ibs, with_ibs;
+	uintmax_t na, nb;
+	int fd;
+
+	amd_test_skip_unless_amd();
+	amd_test_skip_unless_hwpmc();
+	amd_test_bind_cpu0();
+
+	fd = open(LOG_FILE, O_RDWR | O_CREAT | O_TRUNC, 0600);
+	ATF_REQUIRE_MSG(fd >= 0, "open %s: %s", LOG_FILE, strerror(errno));
+	ATF_REQUIRE_MSG(pmc_configure_logfile(fd) == 0,
+	    "pmc_configure_logfile: %s", strerror(errno));
+
+	if (pmc_allocate("ibs-op", PMC_MODE_SS, 0, 0, &ibs, IBS_RATE) != 0)
+		atf_tc_skip("no IBS op PMC on cpu0: %s", strerror(errno));
+	/* Both core PMCs exist at once, so their ids differ. */
+	alone = alloc_core();
+	with_ibs = alloc_core();
+
+	ATF_REQUIRE(pmc_start(alone) == 0);
+	spin();
+	ATF_REQUIRE(pmc_stop(alone) == 0);
+
+	ATF_REQUIRE(pmc_start(with_ibs) == 0);
+	ATF_REQUIRE(pmc_start(ibs) == 0);
+	spin();
+	ATF_REQUIRE(pmc_stop(ibs) == 0);
+	ATF_REQUIRE(pmc_stop(with_ibs) == 0);
+
+	ATF_REQUIRE(pmc_flush_logfile() == 0);
+	amd_test_release_pmc(ibs);
+	amd_test_release_pmc(with_ibs);
+	amd_test_release_pmc(alone);
+	ATF_REQUIRE(pmc_close_logfile() == 0);
+
+	wait_log_written(fd);
+	count_samples(fd, alone, with_ibs, &na, &nb);
+	(void)close(fd);
+
+	ATF_REQUIRE_MSG(na > 0, "the core counter took no samples on its own");
+	ATF_CHECK_MSG(nb * 2 >= na,
+	    "the core counter took %ju samples on its own but only %ju with "
+	    "IBS op sampling the same CPU", na, nb);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, core_sampling_with_ibs);
+
+	return (atf_no_error());
+}