Page MenuHomeFreeBSD

D57632.1796207117.diff
No OneTemporary

Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None

D57632.1796207117.diff

diff --git a/sys/dev/hwpmc/hwpmc_amd.c b/sys/dev/hwpmc/hwpmc_amd.c
--- a/sys/dev/hwpmc/hwpmc_amd.c
+++ b/sys/dev/hwpmc/hwpmc_amd.c
@@ -43,6 +43,8 @@
#include <sys/sysctl.h>
#include <sys/systm.h>
+#include "hwpmc_pmu.h"
+
#include <machine/cpu.h>
#include <machine/cpufunc.h>
#include <machine/md_var.h>
@@ -380,11 +382,10 @@
}
/*
- * Check if a given PMC allocation is feasible.
+ * Validate whether an event can use row 'ri' (used by grouping assign).
*/
-static int
-amd_allocate_pmc(int cpu __unused, int ri, struct pmc *pm,
- const struct pmc_op_pmcallocate *a)
+int
+amd_can_assign_pmc(int ri, struct pmc *pm, const struct pmc_op_pmcallocate *a)
{
const struct pmc_descr *pd;
uint64_t allowed_unitmask, caps, config, unitmask;
@@ -396,7 +397,6 @@
pd = &amd_pmcdesc[ri].pm_descr;
- /* check class match */
if (pd->pd_class != a->pm_class)
return (EINVAL);
@@ -409,9 +409,6 @@
((pd->pd_caps & PMC_CAP_PRECISE) == 0))
return (EINVAL);
- PMCDBG2(MDP, ALL, 1,"amd-allocate ri=%d caps=0x%x", ri, caps);
-
- /* Validate sub-class. */
if (amd_pmcdesc[ri].pm_subclass != a->pm_md.pm_amd.pm_amd_sub_class)
return (EINVAL);
@@ -420,18 +417,10 @@
if ((config & ~amd_config_mask(amd_pmcdesc[ri].pm_subclass,
caps)) != 0)
return (EINVAL);
- pm->pm_md.pm_amd.pm_amd_evsel = config;
- PMCDBG2(MDP, ALL, 2, "amd-allocate ri=%d -> config=0x%jx",
- ri, (uintmax_t)config);
return (0);
}
- /*
- * Everything below this is for supporting older processors.
- */
pe = a->pm_ev;
-
- /* map ev to the correct event mask code */
config = allowed_unitmask = 0;
for (i = 0; i < amd_event_codes_size; i++) {
if (amd_event_codes[i].pe_ev == pe) {
@@ -446,16 +435,105 @@
return (EINVAL);
unitmask = a->pm_md.pm_amd.pm_amd_config & AMD_PMC_UNITMASK;
- if ((unitmask & ~allowed_unitmask) != 0) /* disallow reserved bits */
+ if ((unitmask & ~allowed_unitmask) != 0)
return (EINVAL);
+ return (0);
+}
+
+/*
+ * Build a pmc_sched_constraint for the requested allocation. Row
+ * mask is derived from the dynamically-detected
+ * amd_{core,l3,df}_npmcs - the same counters init populates from
+ * EXTPERFMON or the per-family defaults - so this works on every Zen
+ * generation without per-uarch code paths.
+ */
+int
+amd_get_sched_constraint(struct pmc *pm __unused,
+ const struct pmc_op_pmcallocate *a, pmc_sched_constraint_t *cons)
+{
+ uint32_t mask;
+ u_int first, count, i;
+
+ if (cons == NULL || a == NULL)
+ return (EINVAL);
+ bzero(cons, sizeof(*cons));
+
+ switch (a->pm_md.pm_amd.pm_amd_sub_class) {
+ case PMC_AMD_SUB_CLASS_CORE:
+ first = 0;
+ count = amd_core_npmcs;
+ break;
+ case PMC_AMD_SUB_CLASS_L3_CACHE:
+ first = amd_core_npmcs;
+ count = amd_l3_npmcs;
+ break;
+ case PMC_AMD_SUB_CLASS_DATA_FABRIC:
+ first = amd_core_npmcs + amd_l3_npmcs;
+ count = amd_df_npmcs;
+ break;
+ default:
+ return (EOPNOTSUPP);
+ }
+ if (count == 0 || first + count > 32)
+ return (EOPNOTSUPP);
+
+ mask = 0;
+ for (i = 0; i < count; i++)
+ mask |= 1u << (first + i);
+ cons->pc_allowed_rows = mask;
+ cons->pc_weight = (uint8_t)count;
+ cons->pc_flags = PMC_SC_F_SHARED;
+ cons->pc_fixed_row = (uint8_t)first;
+ return (0);
+}
+
+/*
+ * Check if a given PMC allocation is feasible.
+ */
+static int
+amd_allocate_pmc(int cpu __unused, int ri, struct pmc *pm,
+ const struct pmc_op_pmcallocate *a)
+{
+ uint64_t config;
+ enum pmc_event pe;
+ int error, i;
+ uint64_t allowed_unitmask, caps, unitmask;
+
+ error = amd_can_assign_pmc(ri, pm, a);
+ if (error != 0)
+ return (error);
+
+ caps = pm->pm_caps;
+ PMCDBG2(MDP, ALL, 1,"amd-allocate ri=%d caps=0x%x", ri, caps);
+
+ if (strlen(pmc_cpuid) != 0) {
+ config = a->pm_md.pm_amd.pm_amd_config;
+ pm->pm_md.pm_amd.pm_amd_evsel = config;
+ PMCDBG2(MDP, ALL, 2, "amd-allocate ri=%d -> config=0x%jx",
+ ri, (uintmax_t)config);
+ return (0);
+ }
+
+ pe = a->pm_ev;
+ config = allowed_unitmask = 0;
+ for (i = 0; i < amd_event_codes_size; i++) {
+ if (amd_event_codes[i].pe_ev == pe) {
+ config =
+ AMD_PMC_TO_EVENTMASK(amd_event_codes[i].pe_code);
+ allowed_unitmask =
+ AMD_PMC_TO_UNITMASK(amd_event_codes[i].pe_mask);
+ break;
+ }
+ }
+
+ unitmask = a->pm_md.pm_amd.pm_amd_config & AMD_PMC_UNITMASK;
if (unitmask && (caps & PMC_CAP_QUALIFIER) != 0)
config |= unitmask;
if ((caps & PMC_CAP_THRESHOLD) != 0)
config |= a->pm_md.pm_amd.pm_amd_config & AMD_PMC_COUNTERMASK;
- /* Set at least one of the 'usr' or 'os' caps. */
if ((caps & PMC_CAP_USER) != 0)
config |= AMD_PMC_USR;
if ((caps & PMC_CAP_SYSTEM) != 0)
@@ -470,7 +548,7 @@
if ((caps & PMC_CAP_INTERRUPT) != 0)
config |= AMD_PMC_INT;
- pm->pm_md.pm_amd.pm_amd_evsel = config; /* save config value */
+ pm->pm_md.pm_amd.pm_amd_evsel = config;
PMCDBG2(MDP, ALL, 2, "amd-allocate ri=%d -> config=0x%x", ri, config);
diff --git a/sys/dev/hwpmc/hwpmc_assign.c b/sys/dev/hwpmc/hwpmc_assign.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/hwpmc/hwpmc_assign.c
@@ -0,0 +1,383 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Advanced Micro Devices, Inc.
+ *
+ * Most-constrained-first greedy assigner for hwpmc grouping/multiplex.
+ *
+ * Algorithm:
+ * 1. Each event has a pmc_sched_constraint published by its class
+ * backend (today: amd_get_sched_constraint). pc_allowed_rows is
+ * a bitmask in the backend's row-index namespace; pc_weight is
+ * popcount(pc_allowed_rows).
+ * 2. pmu_assign_group walks events in increasing pc_weight order.
+ * Lower weight == more constrained == placed first. Ties are
+ * broken by insertion order (TAILQ position) for determinism.
+ * 3. For each event we scan its allowed rows, pick the first that
+ * is not already used by this group and that the existing
+ * pcd_allocate_pmc accepts.
+ * 4. Failure to place ANY event triggers pmu_unassign_group, which
+ * releases everything that was placed earlier (all-or-none).
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/pmc.h>
+#include <sys/proc.h>
+
+#include "hwpmc_pmu.h"
+#include "hwpmc_amd.h"
+
+/*
+ * Map class+sub-class to a constraint provider. Today only AMD is
+ * wired up; other backends fall through and the event is rejected by
+ * pmu_validate_group below.
+ */
+int
+pmu_event_get_constraint(pmu_event_t *pe,
+ pmc_sched_constraint_t *cons)
+{
+ const struct pmc_op_pmcallocate *a;
+
+ if (pe == NULL || cons == NULL)
+ return (EINVAL);
+ a = &pe->pe_alloc;
+ switch (a->pm_class) {
+ case PMC_CLASS_K8:
+ return (amd_get_sched_constraint(pe->pe_pmc, a, cons));
+ default:
+ break;
+ }
+ return (EOPNOTSUPP);
+}
+
+u_int
+pmu_count_core_hw_slots(pmu_group_t *pg, struct proc *p)
+{
+ pmu_event_t *pe;
+ struct pmc_mdep *mdep;
+ struct pmc_classdep *pcd;
+ enum pmc_mode mode;
+ u_int n, slots;
+ int adjri;
+
+ mdep = hwpmc_get_mdep();
+ if (mdep == NULL || pg == NULL || pg->pg_leader == NULL)
+ return (0);
+
+ pe = pg->pg_leader;
+ mode = pe->pe_alloc.pm_mode;
+ slots = 0;
+ for (n = 0; n < mdep->pmd_npmc; n++) {
+ /*
+ * Restrict to rows belonging to the same PMC class as the
+ * leader, then translate the global row index `n' into the
+ * class-relative `adjri' before calling class-specific
+ * helpers like amd_can_assign_pmc(). Class helpers index
+ * into per-class descriptor tables (e.g., amd_pmcdesc[])
+ * sized to amd_npmcs, so feeding them the global `n' would
+ * trip their KASSERT bounds check or read the wrong row.
+ */
+ pcd = hwpmc_ri_to_classdep(n, &adjri);
+ if (pcd == NULL || pcd->pcd_caps == 0 ||
+ pcd->pcd_class != pe->pe_alloc.pm_class)
+ continue;
+ if (!hwpmc_can_allocate_row(n, mode) ||
+ !hwpmc_can_allocate_rowindex(p, n, PMC_CPU_ANY))
+ continue;
+ if (pe->pe_alloc.pm_class == PMC_CLASS_K8 &&
+ amd_can_assign_pmc(adjri, pe->pe_pmc, &pe->pe_alloc) != 0)
+ continue;
+ slots++;
+ }
+ PMCDBG3(MDP, ALL, 2, "count_slots: gid=%u class=%d slots=%u",
+ pg->pg_id, pe->pe_alloc.pm_class, slots);
+ return (slots);
+}
+
+/*
+ * Total number of HW rows that belong to the leader's PMC class on
+ * this kernel, regardless of whether they are currently in use.
+ * pmu_group_commit uses this to reject groups that could never fit
+ * even on a freshly-booted system (nevents > class_total -> ENOSPC).
+ * Per-process / per-row contention is the job of pmu_count_core_hw_slots.
+ */
+u_int
+pmu_count_class_total(pmu_group_t *pg)
+{
+ pmu_event_t *pe;
+ struct pmc_mdep *mdep;
+ struct pmc_classdep *pcd;
+ u_int n, slots;
+ int adjri;
+
+ mdep = hwpmc_get_mdep();
+ if (mdep == NULL || pg == NULL || pg->pg_leader == NULL)
+ return (0);
+
+ pe = pg->pg_leader;
+ slots = 0;
+ for (n = 0; n < mdep->pmd_npmc; n++) {
+ pcd = hwpmc_ri_to_classdep(n, &adjri);
+ if (pcd == NULL || pcd->pcd_caps == 0)
+ continue;
+ if (pcd->pcd_class != pe->pe_alloc.pm_class)
+ continue;
+ slots++;
+ }
+ return (slots);
+}
+
+/*
+ * Try to bind one event to a row. used_mask tracks rows already
+ * consumed by earlier events in this group.
+ */
+static int
+pmu_assign_one(pmu_event_t *pe, struct proc *p, int cpu,
+ uint32_t *used_mask)
+{
+ struct pmc *pm;
+ struct pmc_classdep *pcd;
+ struct pmc_mdep *mdep;
+ enum pmc_mode mode;
+ uint32_t allowed;
+ int adjri, n;
+
+ /*
+ * pe_cons.pc_allowed_rows / *used_mask / pc_fixed_row all live in
+ * the per-class adjri namespace (e.g., bits 0..5 for AMD CORE).
+ * Global ri = pcd->pcd_ri + adjri. We iterate adjri space and
+ * convert to global ri only where the framework requires it.
+ */
+ mdep = hwpmc_get_mdep();
+ pm = pe->pe_pmc;
+ mode = pe->pe_alloc.pm_mode;
+ allowed = pe->pe_cons.pc_allowed_rows;
+
+ if ((pe->pe_cons.pc_flags & PMC_SC_F_FIXED) != 0) {
+ adjri = pe->pe_cons.pc_fixed_row;
+ if (((1u << adjri) & allowed) == 0 ||
+ (*used_mask & (1u << adjri)) != 0)
+ return (EBUSY);
+ } else {
+ adjri = -1;
+ }
+
+ for (;;) {
+ uint32_t free_mask;
+ int dummy_adjri;
+
+ if (adjri < 0) {
+ free_mask = allowed & ~*used_mask;
+ if (free_mask == 0)
+ return (EBUSY);
+ adjri = ffs(free_mask) - 1;
+ }
+ /* Locate the global ri that owns this adjri for our class. */
+ pcd = NULL;
+ for (n = 0; n < (int)mdep->pmd_npmc; n++) {
+ pcd = hwpmc_ri_to_classdep(n, &dummy_adjri);
+ if (pcd != NULL &&
+ pcd->pcd_class == pe->pe_alloc.pm_class &&
+ dummy_adjri == adjri)
+ break;
+ pcd = NULL;
+ }
+ if (pcd == NULL || n >= (int)mdep->pmd_npmc)
+ goto skip;
+ if (!hwpmc_can_allocate_row(n, mode) ||
+ !hwpmc_can_allocate_rowindex(p, n, PMC_CPU_ANY))
+ goto skip;
+ if (pe->pe_alloc.pm_class == PMC_CLASS_K8 &&
+ amd_can_assign_pmc(adjri, pm, &pe->pe_alloc) != 0)
+ goto skip;
+ if (pcd->pcd_allocate_pmc(cpu, adjri, pm, &pe->pe_alloc) != 0)
+ goto skip;
+
+ *used_mask |= 1u << adjri;
+ pm->pm_id = PMC_ID_MAKE_ID(PMC_CPU_ANY, mode,
+ pe->pe_alloc.pm_class, n);
+ hwpmc_mark_row_thread(n);
+ pe->pe_state = PMU_EVENT_STATE_ACTIVE;
+ pe->pe_assigned_row = n;
+ return (0);
+
+skip:
+ if ((pe->pe_cons.pc_flags & PMC_SC_F_FIXED) != 0)
+ return (EBUSY);
+ allowed &= ~(1u << adjri);
+ adjri = -1;
+ }
+}
+
+/*
+ * Atomically release every HW row currently held by pg. All-or-none
+ * scheduling means there is no "partial" path: either pg is fully
+ * scheduled in (every sibling on a row) or fully out, so this single
+ * function suffices.
+ */
+void
+pmu_unassign_group(pmu_group_t *pg, int cpu)
+{
+ pmu_event_t *pe;
+ struct pmc *pm;
+ struct pmc_classdep *pcd;
+ enum pmc_mode mode;
+ int adjri, n;
+
+ /*
+ * AMD's pcd_release_pmc is logically a no-op but its KASSERT
+ * still requires cpu >= 0. PMC_CPU_ANY (-1) is a perfectly
+ * legitimate "any CPU" value for virtual-mode PMCs, so coerce
+ * it to CPU 0 here rather than fan it out across the per-class
+ * back-ends.
+ */
+ if (cpu < 0)
+ cpu = 0;
+
+ TAILQ_FOREACH(pe, &pg->pg_events, pe_sibling) {
+ pm = pe->pe_pmc;
+ if (PMC_ROW_IS_UNASSIGNED(pm))
+ continue;
+ n = PMC_TO_ROWINDEX(pm);
+ mode = PMC_TO_MODE(pm);
+ pcd = hwpmc_ri_to_classdep(n, &adjri);
+ if (pcd != NULL)
+ (void)pcd->pcd_release_pmc(cpu, adjri, pm);
+ pm->pm_id = PMC_ID_MAKE_ID(PMC_CPU_ANY, mode,
+ pe->pe_alloc.pm_class, PMC_ROW_UNASSIGNED);
+ hwpmc_mark_row_free(n);
+ pe->pe_state = PMU_EVENT_STATE_INACTIVE;
+ pe->pe_assigned_row = -1;
+ }
+ pg->pg_used_rows_mask = 0;
+ pg->pg_assigned = false;
+}
+
+/*
+ * Sort events by pe_cons.pc_weight ascending into 'order[]'. Stable
+ * insertion-sort; ties keep insertion order.
+ */
+static void
+pmu_sort_by_weight(pmu_group_t *pg, pmu_event_t **order, u_int n)
+{
+ pmu_event_t *pe, *cur;
+ u_int i, j;
+
+ i = 0;
+ TAILQ_FOREACH(pe, &pg->pg_events, pe_sibling) {
+ if (i >= n)
+ break;
+ order[i++] = pe;
+ }
+ for (i = 1; i < n; i++) {
+ cur = order[i];
+ j = i;
+ while (j > 0 &&
+ order[j - 1]->pe_cons.pc_weight > cur->pe_cons.pc_weight) {
+ order[j] = order[j - 1];
+ j--;
+ }
+ order[j] = cur;
+ }
+}
+
+int
+pmu_assign_group(pmu_group_t *pg, struct proc *p, int cpu)
+{
+ pmu_event_t **order;
+ struct pmc_mdep *mdep;
+ uint32_t used_mask;
+ u_int i;
+ int error;
+
+ if (pg == NULL || p == NULL)
+ return (EINVAL);
+ if (pg->pg_assigned)
+ return (0);
+
+ /*
+ * pmu_assign_group is called both from pmu_group_commit (where
+ * pg_committed is still false because we are mid-commit) and
+ * from pmu_pp_schedule_in (where pg_committed is true). Don't
+ * gate on pg_committed; pmu_validate_group below covers all the
+ * structural invariants we actually care about.
+ */
+ error = pmu_validate_group(pg);
+ if (error != 0)
+ return (error);
+
+ mdep = hwpmc_get_mdep();
+ if (mdep == NULL || pg->pg_nevents == 0)
+ return (EINVAL);
+
+ /* See comment in pmu_unassign_group: AMD insists on cpu >= 0. */
+ if (cpu < 0)
+ cpu = 0;
+
+ order = malloc(sizeof(*order) * pg->pg_nevents, M_PMC,
+ M_WAITOK | M_ZERO);
+ pmu_sort_by_weight(pg, order, pg->pg_nevents);
+
+ used_mask = 0;
+ error = 0;
+ for (i = 0; i < pg->pg_nevents; i++) {
+ error = pmu_assign_one(order[i], p, cpu, &used_mask);
+ if (error != 0)
+ break;
+ }
+ free(order, M_PMC);
+
+ if (error != 0) {
+ pmu_unassign_group(pg, cpu);
+ return (error);
+ }
+
+ pg->pg_used_rows_mask = used_mask;
+ pg->pg_assigned = true;
+ return (0);
+}
+
+int
+pmu_validate_group(pmu_group_t *pg)
+{
+ pmu_event_t *pe;
+ pmc_sched_constraint_t cons;
+ int rc;
+
+ if (pg == NULL || pg->pg_leader == NULL)
+ return (EINVAL);
+ if (pg->pg_nevents < 2) {
+ PMCDBG2(PMC, OPS, 1, "validate: gid=%u nevents=%u < 2",
+ pg->pg_id, pg->pg_nevents);
+ return (EINVAL);
+ }
+
+ TAILQ_FOREACH(pe, &pg->pg_events, pe_sibling) {
+ if (pe->pe_pmc == NULL)
+ return (EINVAL);
+ if (!PMC_ROW_IS_UNASSIGNED(pe->pe_pmc)) {
+ PMCDBG2(PMC, OPS, 1,
+ "validate: gid=%u pm_id=0x%jx already assigned",
+ pg->pg_id, (uintmax_t)pe->pe_pmc->pm_id);
+ return (EINVAL);
+ }
+ if ((pe->pe_alloc.pm_flags & PMC_F_GROUP_DEFER) == 0)
+ return (EINVAL);
+ if (!PMC_IS_VIRTUAL_MODE(pe->pe_alloc.pm_mode))
+ return (EINVAL);
+ rc = pmu_event_get_constraint(pe, &cons);
+ if (rc != 0) {
+ PMCDBG3(PMC, OPS, 1,
+ "validate: constraint err=%d class=%d sub=%d",
+ rc, (int)pe->pe_alloc.pm_class,
+ (int)pe->pe_alloc.pm_md.pm_amd.pm_amd_sub_class);
+ return (EOPNOTSUPP);
+ }
+ if (cons.pc_allowed_rows == 0)
+ return (EOPNOTSUPP);
+ }
+ return (0);
+}

File Metadata

Mime Type
text/plain
Expires
Wed, Dec 2, 10:25 AM (7 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42127287
Default Alt Text
D57632.1796207117.diff (15 KB)

Event Timeline