From nobody Thu Nov 12 16:21:24 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4jQQch27Xxz70rLd for ; Thu, 12 Nov 2026 16:21:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 4jQQch1B8Qz4FyP for ; Thu, 12 Nov 2026 16:21:24 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794500484; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UaXFGE0+oKJ/YB+c8HYAiNEJYou7qU4FDOSOPn03p1k=; b=SG8KtMdHITN7hhOzEMprNkiQpVnKoR69A3B1RXQnZPMaWMe/li2diKoScKEn7cJzdqqGrP OkLEGgJr/wU5OOApMwcvNTyIDDaBus7Ok2mYdlgQALNol0Gd4GrSHdi8mmaa48oYgDO2g1 dG0j1jAg30LmGyHD3PD+CPde047XQKeIu+uLuE4G6vHMaiezolDPihUek4Krw/A6HRaB05 tsYsEMEWcG+CweM+Mib0KQN5H8bpcEjHIXnA6ODC/ZwT5a1GcoFdU9s55DqU8qFHdT23MV /WIu8NMfqMmYW/KkxnyaJWv869CEsL2ldJjzBjyLMJ6FI0MqBExgVdIC7YhZjA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1794500484; b=kJYGaYk19f2VkZl0p7c0Yw0VaDdiHF/dr/v6uRqSYM0KQ5YHRSttpXV5txRQ2P4cBv1uQN Vb8BgfGweJr0Ma7aXUtNDfmw2gVxlN6JFy+L+jQoluUjD/lhR99z9d6tjpbRIn5vaQAyAG GfjoTwBVfwJ3RoPAzRnNzA66kUtdvPV74cB9oSau6sYELAHfYKtko07xgGuOCuVo1Dq5IG 1xk/1tBD4dY0bQXO1R0vmPAdyQYrTf4Qss4g/vmzs5LG+OJqE3QDj/UNCMLVCyIYmNWJfV 7ExDYP9a20G4zDzkpFdah1d23tEh/FQOPIvYOQGCK2I4qRmP6pdB6Eo1j090yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1794500484; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=UaXFGE0+oKJ/YB+c8HYAiNEJYou7qU4FDOSOPn03p1k=; b=Ejm8cNBWEIUcM2gAwvuu6MUIf0XEL9kceIdzDWYhMuks2HXnHgteHjhRnjHRZ5aUvtSU+w Bv6CumkJyO6K9/2hDcG956pM0iR7Sb4EhvIVlY2sO7b0Ke7rKNc2dAtK2Se7Qs5zWxIU7g qkyCzilHue4dFddLKpLPvBuGvulHTq1+++AKeg+sC+f0DPKUNCwh9mJmbiV5JIdpeYJJwY 0YoAzHLaXMuOCS2Dy8++yb1FNEOrbGNlZYVBcQKJm7zFXo+/O83gYokmlxUiulOVpEhdfn XP9zLW8PlNZ7qlxPnXogipfb3c7TArm+4me9D1AW+RLgHQiQyjrxZSspt3vzjA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4jQQch0Y78z18td for ; Thu, 12 Nov 2026 16:21:24 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 2423 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 12 Nov 2026 16:21:24 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Andre Silva From: Mitchell Horne Subject: git: c37b83055406 - main - hwpmc: refuse MOD_UNLOAD while PMC owners exist List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all X-BeenThere: dev-commits-src-all@freebsd.org List-Id: Commit messages for all branches of the src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: c37b830554064fcc46ae383f3dc5725ea63a80c4 Auto-Submitted: auto-generated Date: Thu, 12 Nov 2026 16:21:24 +0000 Message-Id: <6af5e784.2423.d36f117@gitrepo.freebsd.org> The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=c37b830554064fcc46ae383f3dc5725ea63a80c4 commit c37b830554064fcc46ae383f3dc5725ea63a80c4 Author: Andre Silva AuthorDate: 2026-11-12 16:19:02 +0000 Commit: Mitchell Horne CommitDate: 2026-11-12 16:21:14 +0000 hwpmc: refuse MOD_UNLOAD while PMC owners exist Unloading hwpmc while processes own PMCs frees state they still use. Return EBUSY instead. Use a try-lock on pmc_sx, since MOD_UNLOAD runs with kld_sx held. MOD_SHUTDOWN still always cleans up. Reviewed by: mhorne MFC after: 1 week Sponsored by: Advanced Micro Devices, Inc. Differential Revision: https://reviews.freebsd.org/D60561 --- sys/dev/hwpmc/hwpmc_mod.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/sys/dev/hwpmc/hwpmc_mod.c b/sys/dev/hwpmc/hwpmc_mod.c index 82079e7660ea..662c4f52fc5d 100644 --- a/sys/dev/hwpmc/hwpmc_mod.c +++ b/sys/dev/hwpmc/hwpmc_mod.c @@ -5941,12 +5941,14 @@ pmc_cleanup(void) PMCDBG0(MOD,INI,0, "cleanup"); + /* The caller holds pmc_sx from the owner check through unhooking. */ + sx_assert(&pmc_sx, SX_XLOCKED); + /* switch off sampling */ CPU_FOREACH(cpu) DPCPU_ID_SET(cpu, pmc_sampled, 0); pmc_intr = NULL; - sx_xlock(&pmc_sx); if (pmc_hook == NULL) { /* being unloaded already */ sx_xunlock(&pmc_sx); return; @@ -6101,6 +6103,33 @@ pmc_cleanup(void) sx_xunlock(&pmc_sx); /* we are done */ } +/* + * Do not unhook sampling while owners can still hold queued samples. + * MOD_UNLOAD runs under kld_sx. A blocking acquisition of pmc_sx would + * invert the sampling path's pmc_sx -> kld_sx order, so refuse on lock + * contention. Keep the successful try-lock through cleanup to exclude + * new owners between checking the hash and disabling the hooks. + */ +static int +pmc_unload(void) +{ + struct pmc_ownerhash *ph; + + if (!sx_try_xlock(&pmc_sx)) + return (EXTERROR(EBUSY, "PMC state lock is busy")); + if (pmc_ownerhash != NULL) { + for (ph = pmc_ownerhash; + ph <= &pmc_ownerhash[pmc_ownerhashmask]; ph++) { + if (!LIST_EMPTY(ph)) { + sx_xunlock(&pmc_sx); + return (EXTERROR(EBUSY, "PMC owners still exist")); + } + } + } + pmc_cleanup(); + return (0); +} + /* * The function called at load/unload. */ @@ -6121,7 +6150,13 @@ load(struct module *module __unused, int cmd, void *arg __unused) pmc_cpu_max()); break; case MOD_UNLOAD: + error = pmc_unload(); + if (error == 0) + PMCDBG0(MOD,INI,1, "unloaded"); + break; case MOD_SHUTDOWN: + /* Shutdown cannot be vetoed by PMC owners. */ + sx_xlock(&pmc_sx); pmc_cleanup(); PMCDBG0(MOD,INI,1, "unloaded"); break;