Page MenuHomeFreeBSD

hwpmc: handle counter wraparound for process-mode counting PMCs
ClosedPublic

Authored by netchild on Jul 19 2026, 12:10 PM.
Tags
Referenced Files
F168519481: D58340.diff
Fri, Aug 28, 7:38 PM
Unknown Object (File)
Thu, Aug 13, 11:28 PM
Unknown Object (File)
Sun, Aug 9, 7:46 AM
Unknown Object (File)
Sun, Aug 9, 6:21 AM
Unknown Object (File)
Sun, Aug 9, 2:01 AM
Unknown Object (File)
Sat, Aug 8, 6:52 PM
Unknown Object (File)
Sat, Aug 8, 6:39 PM
Unknown Object (File)
Sat, Aug 8, 12:25 PM
Subscribers

Details

Summary

The accumulated count of a process-mode counting PMC is kept in a
64-bit software counter and seeded into the hardware counter at every
context switch in. Hardware counters are narrower than that - each
PMC class discovers and records its own counter width, e.g. 48 bits
on current x86 (queried from CPUID on Intel, architectural on AMD) -
so once the accumulated count approaches the end of the hardware
counter range, the counter wraps during a time slice and the value
read back at switch out is smaller than the value seeded. The
increment was computed assuming a full 64-bit counter: on INVARIANTS
kernels a long enough counting run panics with "negative increment"
the moment the accumulated count first crosses the hardware counter
range, and on other kernels the totals silently lose a full counter
range per wrap.

Compute the increment modulo the per-class hardware counter width
instead, in both places that accumulate switch-out deltas.

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

Test Plan

unit test in a follow-up review

Diff Detail

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

Event Timeline

adrian added a subscriber: adrian.

oh good catch! (I've found/fixed these width bugs in the deep past, they can be a pain to find as not every counter is the damned same... :-) )

This revision is now accepted and ready to land.Jul 19 2026, 4:55 PM