git: bf7bb731d8a2 - main - hwpmc: call the thread-create hook for Linux and kernel threads

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 16 Nov 2026 19:50:57 UTC
The branch main has been updated by mhorne:

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

commit bf7bb731d8a2f42f2eb5d6f1f7a2183e40915545
Author:     Andre Silva <andasilv@amd.com>
AuthorDate: 2026-11-16 19:47:46 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-16 19:50:52 +0000

    hwpmc: call the thread-create hook for Linux and kernel threads
    
    linux_clone_thread() and kthread_add() did not call the
    PMC_FN_THR_CREATE hook.  Their new threads had no hwpmc thread
    descriptor.  If the process had a sampling PMC, the first switch-in
    of such a thread hit KASSERT(pt != NULL) in pmc_process_csw_in().
    An unprivileged user could panic an INVARIANTS kernel with
    "pmcstat -P" on a threaded Linux program.
    
    Call the hook in both functions, as thread_create() does.
    
    kern_kthread.c did not include opt_hwpmc_hooks.h, so its hwpmc code
    never compiled.  Include it, and use newtd in kthread_add1().
    Remove the exit log call from kthread_exit(), because thread_exit()
    already logs the exit.
    
    Reported by:    llm-postcr
    Reviewed by:    kib, mhorne
    Fixes:          6161b98c9935 ("hwpmc: Implement per-thread counters for PMC sampling")
    Differential Revision:  https://reviews.freebsd.org/D61524
---
 sys/compat/linux/linux_fork.c | 11 +++++++++++
 sys/kern/kern_kthread.c       | 15 +++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/sys/compat/linux/linux_fork.c b/sys/compat/linux/linux_fork.c
index 24c5d3351623..89fe93c039f2 100644
--- a/sys/compat/linux/linux_fork.c
+++ b/sys/compat/linux/linux_fork.c
@@ -28,10 +28,15 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include "opt_hwpmc_hooks.h"
+
 #include <sys/param.h>
 #include <sys/ktr.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
+#ifdef HWPMC_HOOKS
+#include <sys/pmckern.h>
+#endif
 #include <sys/proc.h>
 #include <sys/ptrace.h>
 #include <sys/racct.h>
@@ -318,6 +323,12 @@ linux_clone_thread(struct thread *td, struct l_clone_args *args)
 	if (p->p_ptevents & PTRACE_LWP)
 		newtd->td_dbgflags |= TDB_BORN;
 	PROC_UNLOCK(p);
+#ifdef HWPMC_HOOKS
+	if (PMC_PROC_IS_USING_PMCS(p))
+		PMC_CALL_HOOK(newtd, PMC_FN_THR_CREATE, NULL);
+	else if (PMC_SYSTEM_SAMPLING_ACTIVE())
+		PMC_CALL_HOOK_UNLOCKED(newtd, PMC_FN_THR_CREATE_LOG, NULL);
+#endif
 
 	tidhash_add(newtd);
 
diff --git a/sys/kern/kern_kthread.c b/sys/kern/kern_kthread.c
index 6650ab868f78..565631c4f213 100644
--- a/sys/kern/kern_kthread.c
+++ b/sys/kern/kern_kthread.c
@@ -26,12 +26,17 @@
  * SUCH DAMAGE.
  */
 
+#include "opt_hwpmc_hooks.h"
+
 #include <sys/param.h>
 #include <sys/systm.h>
 #include <sys/cpuset.h>
 #include <sys/kthread.h>
 #include <sys/lock.h>
 #include <sys/mutex.h>
+#ifdef HWPMC_HOOKS
+#include <sys/pmckern.h>
+#endif
 #include <sys/proc.h>
 #include <sys/resourcevar.h>
 #include <sys/rwlock.h>
@@ -329,8 +334,10 @@ kthread_add1(void (*func)(void *), void *arg, struct proc *p,
 	/* Avoid inheriting affinity from a random parent. */
 	cpuset_kernthread(newtd);
 #ifdef HWPMC_HOOKS
-	if (PMC_SYSTEM_SAMPLING_ACTIVE())
-		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_CREATE_LOG, NULL);
+	if (PMC_PROC_IS_USING_PMCS(p))
+		PMC_CALL_HOOK(newtd, PMC_FN_THR_CREATE, NULL);
+	else if (PMC_SYSTEM_SAMPLING_ACTIVE())
+		PMC_CALL_HOOK_UNLOCKED(newtd, PMC_FN_THR_CREATE_LOG, NULL);
 #endif
 	/* Delay putting it on the run queue until now. */
 	if ((flags & RFSTOPPED) == 0) {
@@ -366,10 +373,6 @@ kthread_exit(void)
 	td = curthread;
 	p = td->td_proc;
 
-#ifdef HWPMC_HOOKS
-	if (PMC_SYSTEM_SAMPLING_ACTIVE())
-		PMC_CALL_HOOK_UNLOCKED(td, PMC_FN_THR_EXIT_LOG, NULL);
-#endif
 	/* A module may be waiting for us to exit. */
 	wakeup(td);