git: e5a48c8e0dd0 - main - hwpmc: release a PMC whose ID could not be copied out

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Fri, 06 Nov 2026 15:22:35 UTC
The branch main has been updated by mhorne:

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

commit e5a48c8e0dd026ba953cf401c4dec02f76078aa2
Author:     Andre Silva <andasilv@amd.com>
AuthorDate: 2026-11-06 15:20:08 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2026-11-06 15:21:45 +0000

    hwpmc: release a PMC whose ID could not be copied out
    
    If copyout of the newly allocated PMC ID fails, the caller cannot use or
    release the PMC. Release it in the kernel.
    
    Reviewed by:    mhorne
    MFC after:      3 days
    Sponsored by:   Advanced Micro Devices, Inc.
    Differential Revision:  https://reviews.freebsd.org/D60562
---
 sys/dev/hwpmc/hwpmc_mod.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c
index 7ffabee4004f..82079e7660ea 100644
--- a/sys/dev/hwpmc/hwpmc_mod.c
+++ b/sys/dev/hwpmc/hwpmc_mod.c
@@ -4367,6 +4367,12 @@ pmc_syscall_handler(struct thread *td, void *syscall_args)
 			break;
 
 		error = copyout(&pa, arg, sizeof(pa));
+		/*
+		 * The caller never learns the ID of an unpublished PMC; do
+		 * not leave it (and the row it holds) behind.
+		 */
+		if (error != 0)
+			(void)pmc_do_op_pmcrelease(pa.pm_pmcid);
 	}
 	break;