git: 1504307c4891 - stable/14 - hwpmc: fix false runcount assertion in user callchain capture

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Wed, 04 Nov 2026 17:40:05 UTC
The branch stable/14 has been updated by mhorne:

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

commit 1504307c4891c2f5e2efd441c2d7e94d41985141
Author:     Andre Silva <andasilv@amd.com>
AuthorDate: 2026-08-11 16:16:20 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-04 17:36:03 +0000

    hwpmc: fix false runcount assertion in user callchain capture
    
    pmc_capture_user_callchain() checks a PMC's runcount before walking
    the user stack, but reads it without holding the spinlock that
    protects it. hardclock() can run on the same CPU during the capture
    and drop the runcount to zero in between, tripping the assertion and
    panicking INVARIANTS kernels under load.
    
    Move the check inside the existing spinlock, right where the code
    already confirms the sample is still valid. No functional change on
    kernels built without INVARIANTS.
    
    Signed-off-by:  Andre Silva <andasilv@amd.com>
    Reviewed by:    mhorne
    MFC after:      1 week
    Sponsored by:   AMD
    Differential Revision:  https://reviews.freebsd.org/D58571
    
    (cherry picked from commit 66118c3f1011d7852bce8b659899179a83781897)
---
 sys/dev/hwpmc/hwpmc_mod.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
index 0a5ee8c18e13..5495c9f793dc 100644
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -4857,9 +4857,6 @@ restart:
 		KASSERT(pm->pm_flags & PMC_F_CALLCHAIN,
 		    ("[pmc,%d] Retrieving callchain for PMC that doesn't "
 		    "want it", __LINE__));
-		KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
-		    ("[pmc,%d] runcount %ju", __LINE__,
-		    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
 
 		if (ring == PMC_UR) {
 			nsamples = ps->ps_nsamples_actual;
@@ -4886,6 +4883,10 @@ restart:
 		 * Verify that the sample hasn't been dropped in the meantime.
 		 */
 		if (ps->ps_nsamples == PMC_USER_CALLCHAIN_PENDING) {
+			KASSERT(counter_u64_fetch(pm->pm_runcount) > 0,
+			    ("[pmc,%d] runcount %ju", __LINE__,
+			    (uintmax_t)counter_u64_fetch(pm->pm_runcount)));
+
 			ps->ps_nsamples = nsamples;
 			/*
 			 * If we couldn't get a sample, simply drop the