git: 805405189b23 - main - hwpmc: claim stray PMI NMIs on Intel

From: Andrew Gallatin <gallatin_at_FreeBSD.org>
Date: Sun, 22 Nov 2026 15:42:28 UTC
The branch main has been updated by gallatin:

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

commit 805405189b23d702ce89674a55ca15c0d7c8142b
Author:     Nick Banks <nickbanks@netflix.com>
AuthorDate: 2026-11-22 15:35:49 +0000
Commit:     Andrew Gallatin <gallatin@FreeBSD.org>
CommitDate: 2026-11-22 15:38:18 +0000

    hwpmc: claim stray PMI NMIs on Intel
    
    core2_intr() claims an NMI only when IA_GLOBAL_STATUS shows an
    overflow. It re-enables the LAPIC PMC vector before acking the
    status bits, so a PMI raised while the handler runs can latch a
    second NMI that finds the status already clear. That NMI goes
    unclaimed and, with the default machdep.panic_on_nmi, panics the
    machine. hwpmc_amd.c already absorbs such NMIs with a per-CPU
    nmi_counter.
    
    Give core2_intr() one credit per serviced PMI and spend it on the
    next NMI on that CPU that finds no overflow.
    
    Reproduced on a Xeon D-1518 (Broadwell-DE) running system-wide
    CPU_CYCLES sampling: the unpatched kernel panicked with "NMI" after
    about 47k PMIs. With this change it ran 12.5 hours (485k PMIs),
    absorbed 6 empty NMIs, and did not panic.
    
    Sponsored by: Netflix
    Reviewed by: bnovkov, gallatin
    Differential Revision: https://reviews.freebsd.org/D61771
---
 sys/dev/hwpmc/hwpmc_core.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/sys/dev/hwpmc/hwpmc_core.c b/sys/dev/hwpmc/hwpmc_core.c
index 83784b93718e..7c6217a59f4b 100644
--- a/sys/dev/hwpmc/hwpmc_core.c
+++ b/sys/dev/hwpmc/hwpmc_core.c
@@ -32,6 +32,7 @@
 
 #include <sys/param.h>
 #include <sys/bus.h>
+#include <sys/pcpu.h>
 #include <sys/pmc.h>
 #include <sys/pmckern.h>
 #include <sys/smp.h>
@@ -104,6 +105,9 @@ static int core_iap_wroffset;
 static u_int pmc_alloc_refs;
 static bool pmc_tsx_force_abort_set;
 
+/* Stray-NMI credits, as in hwpmc_amd.c. */
+DPCPU_DEFINE_STATIC(uint32_t, core_nmi_counter);
+
 static int
 core_pcpu_noop(struct pmc_mdep *md, int cpu)
 {
@@ -1144,6 +1148,18 @@ core2_intr(struct trapframe *tf)
 		wrmsr(core_iap_wroffset + IAP_PMC0 + n, v);
 	}
 
+	/*
+	 * A PMI raised while this handler runs can latch a second NMI
+	 * whose status bits were already acked here.  Claim one empty
+	 * NMI per serviced PMI so it does not hit machdep.panic_on_nmi.
+	 */
+	if (found_interrupt) {
+		DPCPU_SET(core_nmi_counter, 1);
+	} else if (DPCPU_GET(core_nmi_counter) != 0) {
+		DPCPU_SET(core_nmi_counter, 0);
+		found_interrupt = 1;
+	}
+
 	if (found_interrupt)
 		counter_u64_add(pmc_stats.pm_intr_processed, 1);
 	else