From nobody Sat Aug 8 07:22:53 2026 X-Original-To: dev-commits-src-main@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 4hHCCf4GWsz6nBsr for ; Sat, 08 Aug 2026 07:22:54 +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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hHCCf1DPDz3qTY for ; Sat, 08 Aug 2026 07:22:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786173774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=uQOH35i/8nijj8qFp0R6JxdO0a8E51KSmfYfrWP81Zk=; b=Shaqt2brKHiW7HMFEsfCTn5vOTrG4cke6AEWJapCqpx0aQlFclJMc33pMr5BhFKeSFAc2r 4xpuPYoWbPDxyM92L2b0yDQWyKM7OK6lZXadIuSzsQsoFGwPyRpPwmPyaljC3KugsV2bea +ZQbzsKilX4jJHnkVZnMb5rb6p5+gcAGEHTKbTPTKsOrGjSnJgZnYOKfPQD6OpwQWzoT9C Q0BpOJ3X7FY4sLdjdCVb96J8mC3+zUhqfPYYpX/CfxGznKqc27cDp4eid3Gl4IRWP4KfTK wk0hkieiZ0/D8UF0qZfN8L4tKBCVe7PJg+ICQ9Txut6GHc7jlQfT1+wuYz2fIw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1786173774; a=rsa-sha256; cv=none; b=g6nJIznlBbLXO6a8J33CZESJnX+0K33e+kSeEBlQBkep4SXVO4WRQrjxj7nS9dW/6LSXeg 4YlWgZGApwKCJ5D7zJqh7fO77rQp/IcXgnFJ82TjK9iOHo4O1nAm1CBh64iUtmaoQhbcUd fCwh18RxByjT14xo4mxL8XpupbgHyBu2bUoTWzceoDDqu+OBzN2En7cnrEUIVquCUEGbsi cXBi2SNS+7QOqj4BE0dwzGyvp9lCl1bSryJRFISQv3ICYNAJp3sIjbX+D+rpYwGWVhL5Yx XfGAkVBN0I6Bm4fdV+4D7kupQpHv3m+Fj8c2KLCOC7ybQaO4wtuOIgKHla8YBg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786173774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=uQOH35i/8nijj8qFp0R6JxdO0a8E51KSmfYfrWP81Zk=; b=BrhsfzkK5TKnGjMeB3eko8Nw3S1gIbwk38ID/nIpN6yVGvas1hq0tMq97p3WAMPS1KmgUP 65NaTDgmWXukY9XfY58JFPwSJXfHULNMPPppMjtdpPSC1rjNNwxSpIUkY3AvvH36Lk5PZS vYFCOp0Q28VflguoUZQwfr1fxfdH0IP/s4lEbDp6HyqgbHmaDUQc9EnDSvpbGXU9qz47Wm Xd9HYXu5xNRW8nmtSwzlUjg2Ed+MILTzNNeIuBH4OXreGDuCd5G1264RYJ1jGiRpdt9cZE 390VRemTAyiJI1lOkQzYnkuj4zG58NwDHAg68MhjVI4p6388azgmPRzHjP+Xyw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hHCCd755GzCtL for ; Sat, 08 Aug 2026 07:22:53 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 428b0 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sat, 08 Aug 2026 07:22:53 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kevin Bowling Subject: git: 42d54a8fd466 - main - vmm: Tear down the IOMMU before AMD-Vi detach List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: 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: kbowling X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 42d54a8fd4665b97f56f91f450e310c61d4aee2c Auto-Submitted: auto-generated Date: Sat, 08 Aug 2026 07:22:53 +0000 Message-Id: <6a76d94d.428b0.788486bd@gitrepo.freebsd.org> The branch main has been updated by kbowling: URL: https://cgit.FreeBSD.org/src/commit/?id=42d54a8fd4665b97f56f91f450e310c61d4aee2c commit 42d54a8fd4665b97f56f91f450e310c61d4aee2c Author: Kevin Bowling AuthorDate: 2026-08-06 11:06:00 +0000 Commit: Kevin Bowling CommitDate: 2026-08-08 07:22:48 +0000 vmm: Tear down the IOMMU before AMD-Vi detach Register the vmm module handler after both the bundled device drivers and SMP. On platforms without EARLY_AP_STARTUP, SI_SUB_SMP follows SI_SUB_DRIVERS; using the later subsystem preserves the smp_rendezvous() requirement. The resulting reverse unload order performs IOMMU cleanup while every IVHD softc remains valid. Refuse an independent IVHD detach while translation state remains initialized. MFC after: 2 weeks --- sys/amd64/vmm/amd/ivrs_drv.c | 14 +++++++++----- sys/amd64/vmm/io/iommu.c | 12 ++++++++++-- sys/amd64/vmm/io/iommu.h | 1 + sys/dev/vmm/vmm_dev.c | 10 ++++++++-- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/sys/amd64/vmm/amd/ivrs_drv.c b/sys/amd64/vmm/amd/ivrs_drv.c index 85171a5d51be..125c49839d9f 100644 --- a/sys/amd64/vmm/amd/ivrs_drv.c +++ b/sys/amd64/vmm/amd/ivrs_drv.c @@ -711,15 +711,19 @@ ivhd_detach(device_t dev) { struct amdvi_softc *softc; + /* + * amdvi_disable() and domain teardown require every IVHD softc. The + * vmm module handler normally performs that cleanup before the IVHD + * driver is unloaded. Refuse an out-of-order detach rather than leave + * enabled translation hardware referring to resources freed below. + */ + if (iommu_is_initialized()) + return (EBUSY); + softc = device_get_softc(dev); amdvi_teardown_hw(softc); free(softc->dev_cfg, M_DEVBUF); - - /* - * XXX: delete the device. - * don't allow detach, return EBUSY. - */ return (0); } diff --git a/sys/amd64/vmm/io/iommu.c b/sys/amd64/vmm/io/iommu.c index 9fc612244699..71d3cb9ab883 100644 --- a/sys/amd64/vmm/io/iommu.c +++ b/sys/amd64/vmm/io/iommu.c @@ -194,7 +194,7 @@ iommu_init(void) if (error) return; - iommu_avail = 1; + atomic_store_rel_int(&iommu_avail, 1); /* * Create a domain for the devices owned by the host @@ -205,7 +205,7 @@ iommu_init(void) printf("iommu_init: unable to create a host domain"); IOMMU_CLEANUP(); ops = NULL; - iommu_avail = 0; + atomic_store_rel_int(&iommu_avail, 0); return; } @@ -268,6 +268,7 @@ iommu_cleanup_int(bool iommu_disable) IOMMU_DESTROY_DOMAIN(host_domain); host_domain = NULL; IOMMU_CLEANUP(); + atomic_store_rel_int(&iommu_avail, 0); } void @@ -276,6 +277,13 @@ iommu_cleanup(void) iommu_cleanup_int(true); } +bool +iommu_is_initialized(void) +{ + + return (atomic_load_acq_int(&iommu_avail) != 0); +} + void * iommu_create_domain(vm_paddr_t maxaddr) { diff --git a/sys/amd64/vmm/io/iommu.h b/sys/amd64/vmm/io/iommu.h index 5294a9d92a6b..3224b4884e9a 100644 --- a/sys/amd64/vmm/io/iommu.h +++ b/sys/amd64/vmm/io/iommu.h @@ -62,6 +62,7 @@ extern const struct iommu_ops iommu_ops_intel; extern const struct iommu_ops iommu_ops_amd; void iommu_cleanup(void); +bool iommu_is_initialized(void); void *iommu_host_domain(void); void *iommu_create_domain(vm_paddr_t maxaddr); void iommu_destroy_domain(void *dom); diff --git a/sys/dev/vmm/vmm_dev.c b/sys/dev/vmm/vmm_dev.c index a2775023838a..21cbd06ae2d0 100644 --- a/sys/dev/vmm/vmm_dev.c +++ b/sys/dev/vmm/vmm_dev.c @@ -1301,9 +1301,15 @@ static moduledata_t vmm_kmod = { * * - Initialization requires smp_rendezvous() and therefore must happen * after SMP is fully functional (after SI_SUB_SMP). - * - vmm device initialization requires an initialized devfs. + * - vmm device initialization requires an initialized devfs + * (SI_SUB_DRIVERS is after SI_SUB_DEVFS). + * - On amd64, vmm.ko also contains device drivers such as ppt and AMD-Vi/IVHD. + * Load this module handler after SI_SUB_DRIVERS so reverse-order unload runs + * iommu_cleanup() before those drivers detach. AMD-Vi disable still needs + * the IVHD softcs, and ivhd_detach() refuses while the IOMMU is initialized. */ -DECLARE_MODULE(vmm, vmm_kmod, MAX(SI_SUB_SMP, SI_SUB_DEVFS) + 1, SI_ORDER_ANY); +DECLARE_MODULE(vmm, vmm_kmod, MAX(SI_SUB_DRIVERS, SI_SUB_SMP) + 1, + SI_ORDER_ANY); MODULE_VERSION(vmm, 1); static int