From nobody Fri Dec 22 17:34:33 2023 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 4SxZDT60L1z54F9D; Fri, 22 Dec 2023 17:34:33 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4SxZDT5LGyz3Zyn; Fri, 22 Dec 2023 17:34:33 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703266473; 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=DjDSGgIzhZgSp3LTrWb4mw6iw/w4hH0rP6tN+Gwziqk=; b=LYwbm2qpdbZQRn5dzMieutkJci4BJRBxcivLbSUORC1/53Er9LsHxp/Nc11yojQcl9hwZk H1l4hITfZW9P0snnrk7OqF+MhTMy6Uy2xiGHzYjGfVlVtWoSMBYyQ9p9erG+N9W1PspJ63 x9Me6S6Mg9Kef1y1EG/4vMwCMBjk+bkLrM/hzwNP4LU+hx2jM17YegBg3xDk1XJYTBFz18 0/Xr7dkEkvcf75Ks8zyWiIrMlvvoALw6TGeou1HycOw0wKM6Mt1MYlTxu/02oIEFxTO/XC 0DErOnvYlL4JkWy1U2YcOZdxpbXFYYe8R/vCV4mSwIOjpO/1LMmEKGClCZ6GCg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703266473; a=rsa-sha256; cv=none; b=WSXtb2MnAdQjHh4sVJXUu9p8L7Zb6J9/EU5imT+UNmsDwB+u8KC1FQOG5Vix0tyIIIyxPQ QAXY2a59dMVDM7b/DffmIZvY6Y0ocz6mBQekrS9nvwJw75iseFakzR3bWb+hMUvm8WXV8L MZxX6lK5yzP7FdQv/5sa6DcMmIdqzdL5t7tMRZZn2guJEt372CU7G/sy/I5HmqQUjEGdcY mN1knzIXA2Vfg5uGi/Oqzi8F1fZ6KkNs3RxsJE+pPX8EWMPCY/KGamG1JTABO+B5RPZi2u bDCiu54VZBIqXzDfG9jngrwHHoudVZH/xCxujU+J7nFmZ/sfqBgjbQewIlRckg== 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=1703266473; 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=DjDSGgIzhZgSp3LTrWb4mw6iw/w4hH0rP6tN+Gwziqk=; b=qaZ0qQ5CaI/Mcp1VcInhwdZ2i8y+mIb4V20emyo1KLBldxN8WefJ27LEaM3Y6fT1Ejuid6 awzTndNUc4zXLsToa9siPFWxRlPPlEOPGXZHiY5fNz8X9nUlbzQSZ1XGElyac6m0oMBV22 dxPL3PgTakZX4mKnT4FeASPEXonK0RUmnOL+a/kFKCosuowdScCnc1j+9+MlUkaJppfIxL shdKH4jOssPOnG8MxxM4aXu9PHiw98pQhBfqBzbmZBcB3xD0EUbF15OThwH+hbvVsSJ1O9 pguZ3hMeJWa+5mQpOZqiwunQXctzXJTlrvdR++0TSfFjtKGkeqvEFMqZ2QAO/g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4SxZDT3xKkz9tm; Fri, 22 Dec 2023 17:34:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BMHYXN2078769; Fri, 22 Dec 2023 17:34:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BMHYXRA078766; Fri, 22 Dec 2023 17:34:33 GMT (envelope-from git) Date: Fri, 22 Dec 2023 17:34:33 GMT Message-Id: <202312221734.3BMHYXRA078766@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 671a00491d7a - main - vm_iommu_map()/unmap(): stop transiently wiring already wired pages 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: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 671a00491d7ac9d6663cdc597ff8c13024eda00d Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=671a00491d7ac9d6663cdc597ff8c13024eda00d commit 671a00491d7ac9d6663cdc597ff8c13024eda00d Author: Konstantin Belousov AuthorDate: 2023-12-21 00:12:37 +0000 Commit: Konstantin Belousov CommitDate: 2023-12-22 17:34:27 +0000 vm_iommu_map()/unmap(): stop transiently wiring already wired pages Namely, switch from vm_fault_quick_hold() to pmap_extract() KPI to translate gpa to hpa. Assert that the looked up hpa belongs to the wired page, as it should be for the VM which is configured for pass-throu (this is theoretically a restriction that could be removed on newer DMARs). Noted by: alc Reviewed by: alc, jhb, markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D43140 --- sys/amd64/vmm/vmm.c | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c index 32ae9f9050a7..f399f876717d 100644 --- a/sys/amd64/vmm/vmm.c +++ b/sys/amd64/vmm/vmm.c @@ -1041,9 +1041,10 @@ vm_iommu_map(struct vm *vm) { vm_paddr_t gpa, hpa; struct mem_map *mm; - void *vp, *cookie; int i; + sx_assert(&vm->mem_segs_lock, SX_LOCKED); + for (i = 0; i < VM_MAX_MEMMAPS; i++) { mm = &vm->mem_maps[i]; if (!sysmem_mapping(vm, mm)) @@ -1057,13 +1058,24 @@ vm_iommu_map(struct vm *vm) mm->flags |= VM_MEMMAP_F_IOMMU; for (gpa = mm->gpa; gpa < mm->gpa + mm->len; gpa += PAGE_SIZE) { - vp = vm_gpa_hold_global(vm, gpa, PAGE_SIZE, - VM_PROT_WRITE, &cookie); - KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx", - vm_name(vm), gpa)); - vm_gpa_release(cookie); + hpa = pmap_extract(vmspace_pmap(vm->vmspace), gpa); + + /* + * All mappings in the vmm vmspace must be + * present since they are managed by vmm in this way. + * Because we are in pass-through mode, the + * mappings must also be wired. This implies + * that all pages must be mapped and wired, + * allowing to use pmap_extract() and avoiding the + * need to use vm_gpa_hold_global(). + * + * This could change if/when we start + * supporting page faults on IOMMU maps. + */ + KASSERT(vm_page_wired(PHYS_TO_VM_PAGE(hpa)), + ("vm_iommu_map: vm %p gpa %jx hpa %jx not wired", + vm, (uintmax_t)gpa, (uintmax_t)hpa)); - hpa = DMAP_TO_PHYS((uintptr_t)vp); iommu_create_mapping(vm->iommu, gpa, hpa, PAGE_SIZE); } } @@ -1076,9 +1088,10 @@ vm_iommu_unmap(struct vm *vm) { vm_paddr_t gpa; struct mem_map *mm; - void *vp, *cookie; int i; + sx_assert(&vm->mem_segs_lock, SX_LOCKED); + for (i = 0; i < VM_MAX_MEMMAPS; i++) { mm = &vm->mem_maps[i]; if (!sysmem_mapping(vm, mm)) @@ -1092,12 +1105,10 @@ vm_iommu_unmap(struct vm *vm) mm->gpa, mm->len, mm->flags)); for (gpa = mm->gpa; gpa < mm->gpa + mm->len; gpa += PAGE_SIZE) { - vp = vm_gpa_hold_global(vm, gpa, PAGE_SIZE, - VM_PROT_WRITE, &cookie); - KASSERT(vp != NULL, ("vm(%s) could not map gpa %#lx", - vm_name(vm), gpa)); - vm_gpa_release(cookie); - + KASSERT(vm_page_wired(PHYS_TO_VM_PAGE(pmap_extract( + vmspace_pmap(vm->vmspace), gpa))), + ("vm_iommu_unmap: vm %p gpa %jx not wired", + vm, (uintmax_t)gpa)); iommu_remove_mapping(vm->iommu, gpa, PAGE_SIZE); } }