Page MenuHomeFreeBSD

D60187.1796272446.diff
No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None

D60187.1796272446.diff

diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -247,6 +247,8 @@
static void pmc_post_callchain_callback(void);
static void pmc_process_allproc(struct pmc *pm);
static void pmc_process_csw_in(struct thread *td);
+static void pmc_process_csw_in_flags(struct thread *td, bool reload);
+static void pmc_load_running_threads(struct pmc *pm);
static void pmc_process_csw_out(struct thread *td);
static void pmc_process_csw_start_all(int cpu);
static void pmc_process_csw_stop_all(int cpu);
@@ -1461,7 +1463,15 @@
static void
pmc_process_csw_in(struct thread *td)
{
- struct pmc *pm;
+
+ pmc_process_csw_in_flags(td, false);
+}
+
+/* With 'reload', the thread is on the CPU: load only missing PMCs. */
+static void
+pmc_process_csw_in_flags(struct thread *td, bool reload)
+{
+ struct pmc *pm, *hwpm;
struct pmc_classdep *pcd;
struct pmc_cpu *pc;
struct pmc_hw *phw __diagused;
@@ -1509,6 +1519,20 @@
if (pm->pm_state != PMC_STATE_RUNNING)
continue;
+ /*
+ * On reload, load only into an empty row. A switch-in can
+ * load this PMC first. A PMC detached while this thread ran
+ * stays on the row until the switch-out removes it. The
+ * next switch-in then loads this PMC.
+ */
+ pcd = pmc_ri_to_classdep(md, ri, &adjri);
+ if (reload) {
+ hwpm = NULL;
+ (void)pcd->pcd_get_config(cpu, adjri, &hwpm);
+ if (hwpm != NULL)
+ continue;
+ }
+
KASSERT(counter_u64_fetch(pm->pm_runcount) >= 0,
("[pmc,%d] pm=%p runcount %ju", __LINE__, pm,
(uintmax_t)counter_u64_fetch(pm->pm_runcount)));
@@ -1517,7 +1541,6 @@
counter_u64_add(pm->pm_runcount, 1);
/* configure the HWPMC we are going to use. */
- pcd = pmc_ri_to_classdep(md, ri, &adjri);
(void)pcd->pcd_config_pmc(cpu, adjri, pm);
phw = pc->pc_hwpmcs[ri];
@@ -1878,6 +1901,59 @@
critical_exit();
}
+/*
+ * TDA_HWPMC handler. Load PMCs started while this thread was running.
+ * The scheduler may keep the thread on the CPU without calling the
+ * switch hooks, so do not wait for a switch.
+ *
+ * The handler can sleep before it enters a critical section. Hold
+ * pmc_sx shared: a detach, an exit or an unload cannot then free the
+ * process descriptor that the reload uses.
+ */
+void
+pmc_ast_load(struct thread *td)
+{
+
+ KASSERT(td == curthread, ("[pmc,%d] td != curthread", __LINE__));
+ if (!PMC_PROC_IS_USING_PMCS(td->td_proc))
+ return;
+ sx_slock(&pmc_sx);
+ if (pmc_hook != NULL && PMC_PROC_IS_USING_PMCS(td->td_proc))
+ pmc_process_csw_in_flags(td, true);
+ sx_sunlock(&pmc_sx);
+}
+
+/*
+ * Send the TDA_HWPMC AST to running target threads. Other threads load
+ * the PMC at switch-in.
+ */
+static void
+pmc_load_running_threads(struct pmc *pm)
+{
+ struct pmc_target *ptgt;
+ struct thread *td;
+ struct proc *p;
+
+ sx_assert(&pmc_sx, SX_XLOCKED);
+ LIST_FOREACH(ptgt, &pm->pm_targets, pt_next) {
+ p = ptgt->pt_process->pp_proc;
+ PROC_LOCK(p);
+ FOREACH_THREAD_IN_PROC(p, td) {
+ if (td == curthread)
+ continue;
+ thread_lock(td);
+ if (TD_IS_RUNNING(td)) {
+ ast_sched_locked(td, TDA_HWPMC);
+#ifdef SMP
+ forward_signal(td);
+#endif
+ }
+ thread_unlock(td);
+ }
+ PROC_UNLOCK(p);
+ }
+}
+
/*
* A new thread for a process.
*/
@@ -3202,6 +3278,7 @@
pm->pm_state = PMC_STATE_RUNNING;
if ((pm->pm_flags & PMC_F_ATTACHED_TO_OWNER) != 0)
pmc_force_context_switch();
+ pmc_load_running_threads(pm);
}
return (error);
diff --git a/sys/dev/hwpmc/hwpmc_soft.c b/sys/dev/hwpmc/hwpmc_soft.c
--- a/sys/dev/hwpmc/hwpmc_soft.c
+++ b/sys/dev/hwpmc/hwpmc_soft.c
@@ -409,8 +409,12 @@
}
static void
-ast_hwpmc(struct thread *td, int tda __unused)
+ast_hwpmc(struct thread *td, int tda)
{
+ /* Load PMCs started while the thread was running. */
+ if ((tda & TDAI(TDA_HWPMC)) != 0)
+ pmc_ast_load(td);
+
/* Handle Software PMC callchain capture. */
if (PMC_IS_PENDING_CALLCHAIN(td))
PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_USER_CALLCHAIN_SOFT,
diff --git a/sys/sys/pmc.h b/sys/sys/pmc.h
--- a/sys/sys/pmc.h
+++ b/sys/sys/pmc.h
@@ -1276,6 +1276,7 @@
void pmc_restore_cpu_binding(struct pmc_binding *pb);
void pmc_save_cpu_binding(struct pmc_binding *pb);
void pmc_select_cpu(int cpu);
+void pmc_ast_load(struct thread *_td);
struct pmc_mdep *pmc_mdep_alloc(int nclasses);
void pmc_mdep_free(struct pmc_mdep *md);
uint64_t pmc_rdtsc(void);
diff --git a/tests/sys/pmc/Makefile b/tests/sys/pmc/Makefile
--- a/tests/sys/pmc/Makefile
+++ b/tests/sys/pmc/Makefile
@@ -1,6 +1,7 @@
TESTSDIR= ${TESTSBASE}/sys/pmc
ATF_TESTS_C+= pmc_api_test
+ATF_TESTS_C+= pmc_attach_running_test
ATF_TESTS_C+= pmc_credexec_test
ATF_TESTS_C+= pmc_detach_test
ATF_TESTS_C+= pmc_exec_test
@@ -27,6 +28,7 @@
TESTS_SUBDIRS+= amd
.endif
+TEST_METADATA.pmc_attach_running_test+= required_user="root"
TEST_METADATA.pmc_exterr_test+= required_user="root"
TEST_METADATA.pmc_callchain_ur_test+= is_exclusive="true"
TEST_METADATA.pmc_cli_test+= is_exclusive="true"
diff --git a/tests/sys/pmc/pmc_attach_running_test.c b/tests/sys/pmc/pmc_attach_running_test.c
new file mode 100644
--- /dev/null
+++ b/tests/sys/pmc/pmc_attach_running_test.c
@@ -0,0 +1,133 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ */
+
+/*
+ * Start a counting PMC on a target that already runs and never blocks.
+ * No context switch loads the PMC, so the start must load it.
+ */
+
+#include <sys/param.h>
+#include <sys/cpuset.h>
+#include <sys/wait.h>
+
+#include <errno.h>
+#include <pmc.h>
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <atf-c.h>
+
+static const char *const hw_events[] = {
+ "instructions", "inst_retired.any", "unhalted-core-cycles",
+ "cycles", "cpu_clk_unhalted.thread", NULL
+};
+
+static void
+require_hwpmc(void)
+{
+
+ if (geteuid() != 0)
+ atf_tc_skip("attaching to another process requires root");
+ if (pmc_init() != 0)
+ atf_tc_skip("hwpmc(4) is not available");
+}
+
+static pmc_id_t
+require_counting_pmc(void)
+{
+ pmc_id_t id;
+ int k;
+
+ for (k = 0; hw_events[k] != NULL; k++) {
+ if (pmc_allocate(hw_events[k], PMC_MODE_TC, 0, PMC_CPU_ANY,
+ &id, 0) == 0)
+ return (id);
+ }
+ atf_tc_skip("no hardware counting PMC is allocatable: %s",
+ strerror(errno));
+}
+
+/* Return a CPU this process may use other than 'other', or skip. */
+static int
+pick_cpu(int other)
+{
+ cpuset_t set;
+ int cpu;
+
+ ATF_REQUIRE(cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,
+ sizeof(set), &set) == 0);
+ for (cpu = CPU_SETSIZE - 1; cpu >= 0; cpu--) {
+ if (CPU_ISSET(cpu, &set) && cpu != other)
+ return (cpu);
+ }
+ atf_tc_skip("needs two CPUs");
+}
+
+static void
+pin(pid_t pid, int cpu)
+{
+ cpuset_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ ATF_REQUIRE_MSG(cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID,
+ pid, sizeof(set), &set) == 0, "pin %d to CPU %d: %s", (int)pid,
+ cpu, strerror(errno));
+}
+
+ATF_TC_WITHOUT_HEAD(start_on_running_target);
+ATF_TC_BODY(start_on_running_target, tc)
+{
+ pmc_value_t v;
+ pmc_id_t id;
+ pid_t child;
+ int cpu, status;
+
+ require_hwpmc();
+ cpu = pick_cpu(-1);
+ pin(getpid(), pick_cpu(cpu));
+ id = require_counting_pmc();
+
+ ATF_REQUIRE((child = fork()) >= 0);
+ if (child == 0) {
+ volatile uint64_t sink = 0;
+
+ /* Spin, and never block. */
+ for (;;)
+ sink++;
+ }
+ pin(child, cpu);
+ /* Let the child reach its CPU and run there. */
+ (void)usleep(200000);
+
+ ATF_REQUIRE_MSG(pmc_attach(id, child) == 0, "pmc_attach: %s",
+ strerror(errno));
+ ATF_REQUIRE_MSG(pmc_start(id) == 0, "pmc_start: %s",
+ strerror(errno));
+ (void)usleep(500000);
+
+ /* The exit saves the final count. */
+ (void)kill(child, SIGKILL);
+ ATF_REQUIRE(waitpid(child, &status, 0) == child);
+
+ ATF_REQUIRE_MSG(pmc_read(id, &v) == 0, "pmc_read: %s",
+ strerror(errno));
+ ATF_CHECK_MSG(v > 0,
+ "a PMC started on a running target counted nothing");
+
+ (void)pmc_stop(id);
+ ATF_CHECK(pmc_release(id) == 0);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, start_on_running_target);
+
+ return (atf_no_error());
+}

File Metadata

Mime Type
text/plain
Expires
Thu, Dec 3, 4:34 AM (11 h, 19 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42127971
Default Alt Text
D60187.1796272446.diff (7 KB)

Event Timeline