From nobody Mon Nov 23 14:29:23 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jY7cN0gSfz6w4wd for ; Mon, 23 Nov 2026 14:29:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jY7cM5Vw4z4hCK for ; Mon, 23 Nov 2026 14:29:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795444163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=N4NXH7RcSda0rRn6IxCBklEWZvQol1/uY0gI7IaZED4=; b=dRGzWK3ISTTIqOKRSN+R9DtCU5Jcry6MUSCkxzEQmg7b/mZEphBPuzBdLCdhFxDao4yJD6 ckim3bISkmLizUGmsS4YGkRyZmPQ+AbiBksCoikfRhC2xTCmNlKQZePg+WazUVzNuE9JSk D8g7tGxlj69s4Tw534DMtF3D0fqSeyHDSsm4ZVZt4lxy1DMqBJh6yLf/wxqq8xyX6k+slI ly8KM2lJtRyXXx+VQEmcrTSjptmJzS1/XwsKp0eLdPbPh0fjqahj/A1VqQlX9hOUYFdU97 2omub7Foc4dPjGC/lrEPr7QW9zLJ5dmVC/kiVArAQ0MmsEbW9j2wzqkyFVj7GQ== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1795444163; b=w/fokjABj/sw95TZxKGsDxcVg35jjAsRyjVQVPGBsH7aXTqCv1clv4trmaq0oEFBMYaQeF HrVMdVtc6Ds+YSwihfGkGqAIQ5Gamkg/4mnKaEZ3qDb7bDtTlxRcy9DfMvgCgnWgu/kx6M pThzPYhso73Rd+Eb9ItkEYeOcK8jpYrRgneTaKzb1elGYT/XJelWm6dnUhFD0zbE0gmfDl qXtQaFc2L2yGQavOPUf5F+pCdBPbZy7jaDPDwmpPegQ8qLN8byU/hhOJm692MhV+ajlFPZ o9/ECrL9s8hBN3E3FeS+cstt1UuYJWo+jgCqg1mPjurH847xb7EPbhdnD53K8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1795444163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=N4NXH7RcSda0rRn6IxCBklEWZvQol1/uY0gI7IaZED4=; b=C4nXaVS9Dr/DXXX1aQj2hqt74f0mvAW6Gue/2qevSDj2lP2hqGWegMq3VDQrOnfPMyPL0P q+7l+fqFHH3xGZLOdg5w5rrqJbJB9Kk25vmQ0advlu3GFU2EZZjgdhULTO/7XMIfsNEmmU PRuSedFv6SSjA6pSWWIxpFaQWQ5UlTWigmwoYatZ8YoYk1utw0ovb5ojg3MehaGfDTYA7K FPkJxmZAvGXfaIJZCvfsJM8mRLnCxfNuvl3jeIYKbQebLPw4d1HWmms6CAmF1YBNrR3mEx o7ymYwXMSc49ijhqs1kFdA2r11w7LcAdM7IsxnCPfBf7OAiN+KG6vsg54X48OQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jY7cM4SshzvkG for ; Mon, 23 Nov 2026 14:29:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id d45a by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 23 Nov 2026 14:29:23 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Osvaldo Janeri Filho From: Mitchell Horne Subject: git: c0c2c80b647f - main - hwpmc_amd: Service the core counters when IBS claims the NMI List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c0c2c80b647f712798c507cc76cbdf882c132a9c Auto-Submitted: auto-generated Date: Mon, 23 Nov 2026 14:29:23 +0000 Message-Id: <6b044dc3.d45a.426cea35@gitrepo.freebsd.org> The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=c0c2c80b647f712798c507cc76cbdf882c132a9c commit c0c2c80b647f712798c507cc76cbdf882c132a9c Author: Osvaldo Janeri Filho AuthorDate: 2026-11-23 14:28:28 +0000 Commit: Mitchell Horne 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 + * + * 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 +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#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()); +}