git: d2e7fcb6dd79 - main - hwpmc: do not register RAPL when the unit register reads as zero

From: Alexander Leidinger <netchild_at_FreeBSD.org>
Date: Sun, 23 Aug 2026 08:11:35 UTC
The branch main has been updated by netchild:

URL: https://cgit.FreeBSD.org/src/commit/?id=d2e7fcb6dd79691d336c3700e4727a887e5cabdc

commit d2e7fcb6dd79691d336c3700e4727a887e5cabdc
Author:     Alexander Leidinger <netchild@FreeBSD.org>
AuthorDate: 2026-08-22 07:59:03 +0000
Commit:     Alexander Leidinger <netchild@FreeBSD.org>
CommitDate: 2026-08-23 08:09:36 +0000

    hwpmc: do not register RAPL when the unit register reads as zero
    
    An energy status unit of zero means one joule per raw tick, which no part
    reports; it is what a hypervisor returns for an MSR it does not implement.
    Both energy rows are scaled by that field, so the class would be
    registered with counters that read zero forever.
    
    Refuse it, as the class is already refused when no energy MSR responds.
    
    Assisted-by:    Claude Code (Opus 5)
---
 sys/dev/hwpmc/hwpmc_rapl.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/sys/dev/hwpmc/hwpmc_rapl.c b/sys/dev/hwpmc/hwpmc_rapl.c
index 4ca76bc12403..c505ae35c248 100644
--- a/sys/dev/hwpmc/hwpmc_rapl.c
+++ b/sys/dev/hwpmc/hwpmc_rapl.c
@@ -535,6 +535,11 @@ pmc_rapl_initialize(struct pmc_mdep *md, int maxcpu, int classindex)
 	if (rdmsr_safe(unit_msr, &unit_val) != 0)
 		return (ENXIO);
 	esu = (unit_val >> 8) & 0x1f;
+
+	/* A zero unit is a hypervisor's answer for an MSR it does not have. */
+	if (esu == 0)
+		return (ENXIO);
+
 	dram_unit = rapl_intel_fixed_dram_unit() ? 16 : esu;
 
 	/* Build the event table from the MSRs that actually respond. */