Page MenuHomeFreeBSD

hwpmc: drain a process-mode PMC's runcount when a live target detaches
ClosedPublic

Authored by netchild on Jul 19 2026, 12:22 PM.
Tags
Referenced Files
F168706994: D58342.diff
Sat, Aug 29, 6:01 PM
Unknown Object (File)
Fri, Aug 28, 11:21 AM
Unknown Object (File)
Fri, Aug 28, 12:38 AM
Unknown Object (File)
Wed, Aug 26, 7:28 PM
Unknown Object (File)
Sat, Aug 22, 5:29 PM
Unknown Object (File)
Fri, Aug 21, 9:52 PM
Unknown Object (File)
Mon, Aug 17, 2:12 PM
Unknown Object (File)
Mon, Aug 17, 2:52 AM
Subscribers

Details

Summary

A process-mode PMC's runcount tracks how many CPUs currently have it
loaded in hardware. It is decremented only by the context-switch-out
and process-exit reclaim paths, both of which the scheduler invokes
only for processes flagged P_HWPMC. Detaching a target that still has
the PMC live in hardware dropped the target and cleared P_HWPMC without
taking the PMC off the hardware or dropping the runcount reference, so
the reference leaked. A subsequent release then spun in
pmc_wait_for_pmc_idle() forever waiting for the runcount to reach zero:
on an INVARIANTS kernel this panics ("waiting too long for pmc to be
free"), otherwise it is an unkillable loop holding the hwpmc lock. Any
process able to allocate a PMC can trigger this by attaching a counting
PMC to itself and detaching it before releasing.

Take the PMC off the hardware and drop the runcount reference as part
of detaching, before P_HWPMC is cleared: reclaim it from the detaching
thread's own CPU directly, and, when the detach removes the PMC's last
target, wait for any references held by the target's other threads to
drain while P_HWPMC is still set (they can no longer reload it).

MFC after: 2 weeks
Assisted-by: Claude Code (Fable 5)

Test Plan

unit-tests come in a separate review

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable