Page MenuHomeFreeBSD

hwpmc: PMC group syscalls, attach hook, and process-exit drain
Needs ReviewPublic

Authored by afscoelho_gmail.com on Jun 18 2026, 7:06 AM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Oct 12, 1:59 PM
Unknown Object (File)
Mon, Oct 12, 4:43 AM
Unknown Object (File)
Sat, Oct 10, 3:47 AM
Unknown Object (File)
Fri, Oct 9, 3:36 PM
Unknown Object (File)
Wed, Oct 7, 1:29 AM
Unknown Object (File)
Tue, Oct 6, 10:50 PM
Unknown Object (File)
Tue, Oct 6, 12:48 AM
Unknown Object (File)
Mon, Oct 5, 12:48 PM
Subscribers
None

Details

Summary

Wire the new PMU layer into hwpmc_mod.c:

  • PMC_OP_PMCGROUPCREATE / GROUPADD / GROUPCOMMIT / GROUPRELEASE handlers that drive pmu_group_*.
  • Deferred allocate path: when PMC_F_GROUP_DEFER is set, build a pmu_event via pmu_group_on_allocate and assign a unique per-owner deferred handle (struct pmc::pm_handle, value in PMC_HANDLE_DEFERRED_{BASE,MAX}). Userland receives pm_handle as the pmcid; the kernel keeps pm_id at PMC_ROW_UNASSIGNED until the group assigner rewrites it with the real HW row. pmc_find_pmc / pmc_find_pmc_descriptor_in_process now match against pm_handle so userland keeps working with the original pmcid even after the assigner rewrites pm_id.
  • Lazy attach assignment: pmc_attach_one_process runs pmu_assign_group against the target proc the first time a sibling of a committed-but-unassigned group is attached, so subsequent siblings see a valid ri. The PMCSTART / PMCSTOP / GETCAPS KASSERTs that previously compared pmcid to pm_id now compare to pm_handle (no semantic change for non-deferred PMCs since pm_handle == pm_id at allocation).
  • Process-exit drain: pmc_process_exit invokes pmu_pp_release_all before freeing pp. This severs every pmu_group still hooked off pp and tears down the per-pp rotation kthread, which itself holds pp as its arg. Without this, releasing a group after the target process exited would walk a freed pp and panic LIST_REMOVE with "Bad link prev->next != elm".
  • pmu_group_on_release is wired into pmc_release_pmc_descriptor and removes the pmu_event from the group TAILQ before freeing it, so a later sibling release does not walk a freed pe.
  • PMC_F_GROUP_MUX commit fallback: pmu_group_commit accepts groups with nevents > slots when MUX is requested and the multiplex layer time-slices them.
  • Lock shims for hwpmc_pmu.c. The new PMU layer needs to assert that pmc_sx is held exclusive and to drop/re-acquire it around the rotation kthread's tsleep. Rather than expose pmc_sx as a global symbol to a separate translation unit, hwpmc_mod.c provides hwpmc_pmu_sx_xlock / _xunlock / _assert_xlocked / _sleep wrappers and hwpmc_pmu.c calls those. These shims, like the group entry points, resolve to the PMU layer only on architectures that build it; on others hwpmc_mod.c simply never calls them.

Sponsored by: AMD
Signed-off-by: Raghavendra K T <raghavendra.kt@amd.com>

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 77471
Build 74354: arc lint + arc unit

Event Timeline

afscoelho_gmail.com updated this revision to Diff 185777.

Update to the rebuilt PerfMonV2 grouping and multiplexing stack. The former D57634 build-wiring change is folded into this revision because it is the activation switch for the PMU implementation.

Rebase onto main after D58256 landed.

Changes:

  • main removed pmc_can_attach() in cbf3fe8549b6 (SA-26:56). Group attach now uses p_candebug() for each target, like PMCATTACH. On a credential-changing exec, groups stay only if the owner has PRIV_DEBUG_DIFFCRED, like a single PMC.
  • The process-exit fix for PerfMonV2 is now in D60141.

Keep the exclusive lock in PMC_OP_PMCSTOP for an SS PMC. pmc_stop() changes pmc_ss_owners, and an ungrouped SS stop hit the SX_XLOCKED assert.

Rebase on main with D60141 committed (pmc_process_csw_out_prepare() is gone). No functional change.