From owner-svn-src-stable-12@freebsd.org Mon Dec 14 20:48:59 2020 Return-Path: Delivered-To: svn-src-stable-12@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B274A4C2EA0; Mon, 14 Dec 2020 20:48:59 +0000 (UTC) (envelope-from jhb@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Cvtlv4Dwwz3pD6; Mon, 14 Dec 2020 20:48:59 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 7FF8527E59; Mon, 14 Dec 2020 20:48:59 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0BEKmxtZ003629; Mon, 14 Dec 2020 20:48:59 GMT (envelope-from jhb@FreeBSD.org) Received: (from jhb@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0BEKmxpM003628; Mon, 14 Dec 2020 20:48:59 GMT (envelope-from jhb@FreeBSD.org) Message-Id: <202012142048.0BEKmxpM003628@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jhb set sender to jhb@FreeBSD.org using -f From: John Baldwin Date: Mon, 14 Dec 2020 20:48:59 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r368642 - stable/12/sys/amd64/vmm/io X-SVN-Group: stable-12 X-SVN-Commit-Author: jhb X-SVN-Commit-Paths: stable/12/sys/amd64/vmm/io X-SVN-Commit-Revision: 368642 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-12@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for only the 12-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Dec 2020 20:48:59 -0000 Author: jhb Date: Mon Dec 14 20:48:59 2020 New Revision: 368642 URL: https://svnweb.freebsd.org/changeset/base/368642 Log: MFC 368004: Pull the check for VM ownership into ppt_find(). This reduces some code duplication. One behavior change is that ppt_assign_device() will now only succeed if the device is unowned. Previously, a device could be assigned to the same VM multiple times, but each time it was assigned, the device's state was reset. Modified: stable/12/sys/amd64/vmm/io/ppt.c Directory Properties: stable/12/ (props changed) Modified: stable/12/sys/amd64/vmm/io/ppt.c ============================================================================== --- stable/12/sys/amd64/vmm/io/ppt.c Mon Dec 14 20:40:21 2020 (r368641) +++ stable/12/sys/amd64/vmm/io/ppt.c Mon Dec 14 20:48:59 2020 (r368642) @@ -198,8 +198,8 @@ static devclass_t ppt_devclass; DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev)); DRIVER_MODULE(ppt, pci, ppt_driver, ppt_devclass, NULL, NULL); -static struct pptdev * -ppt_find(int bus, int slot, int func) +static int +ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp) { device_t dev; struct pptdev *ppt; @@ -211,9 +211,15 @@ ppt_find(int bus, int slot, int func) s = pci_get_slot(dev); f = pci_get_function(dev); if (bus == b && slot == s && func == f) - return (ppt); + break; } - return (NULL); + + if (ppt == NULL) + return (ENOENT); + if (ppt->vm != vm) /* Make sure we own this device */ + return (EBUSY); + *pptp = ppt; + return (0); } static void @@ -377,50 +383,40 @@ int ppt_assign_device(struct vm *vm, int bus, int slot, int func) { struct pptdev *ppt; + int error; - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - /* - * If this device is owned by a different VM then we - * cannot change its owner. - */ - if (ppt->vm != NULL && ppt->vm != vm) - return (EBUSY); + /* Passing NULL requires the device to be unowned. */ + error = ppt_find(NULL, bus, slot, func, &ppt); + if (error) + return (error); - pci_save_state(ppt->dev); - ppt_pci_reset(ppt->dev); - pci_restore_state(ppt->dev); - ppt->vm = vm; - iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); - return (0); - } - return (ENOENT); + pci_save_state(ppt->dev); + ppt_pci_reset(ppt->dev); + pci_restore_state(ppt->dev); + ppt->vm = vm; + iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); + return (0); } int ppt_unassign_device(struct vm *vm, int bus, int slot, int func) { struct pptdev *ppt; + int error; - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - /* - * If this device is not owned by this 'vm' then bail out. - */ - if (ppt->vm != vm) - return (EBUSY); + error = ppt_find(vm, bus, slot, func, &ppt); + if (error) + return (error); - pci_save_state(ppt->dev); - ppt_pci_reset(ppt->dev); - pci_restore_state(ppt->dev); - ppt_unmap_mmio(vm, ppt); - ppt_teardown_msi(ppt); - ppt_teardown_msix(ppt); - iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); - ppt->vm = NULL; - return (0); - } - return (ENOENT); + pci_save_state(ppt->dev); + ppt_pci_reset(ppt->dev); + pci_restore_state(ppt->dev); + ppt_unmap_mmio(vm, ppt); + ppt_teardown_msi(ppt); + ppt_teardown_msix(ppt); + iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev)); + ppt->vm = NULL; + return (0); } int @@ -451,25 +447,22 @@ ppt_map_mmio(struct vm *vm, int bus, int slot, int fun struct pptseg *seg; struct pptdev *ppt; - ppt = ppt_find(bus, slot, func); - if (ppt != NULL) { - if (ppt->vm != vm) - return (EBUSY); + error = ppt_find(vm, bus, slot, func, &ppt); + if (error) + return (error); - for (i = 0; i < MAX_MMIOSEGS; i++) { - seg = &ppt->mmio[i]; - if (seg->len == 0) { - error = vm_map_mmio(vm, gpa, len, hpa); - if (error == 0) { - seg->gpa = gpa; - seg->len = len; - } - return (error); + for (i = 0; i < MAX_MMIOSEGS; i++) { + seg = &ppt->mmio[i]; + if (seg->len == 0) { + error = vm_map_mmio(vm, gpa, len, hpa); + if (error == 0) { + seg->gpa = gpa; + seg->len = len; } + return (error); } - return (ENOSPC); } - return (ENOENT); + return (ENOSPC); } static int @@ -511,11 +504,9 @@ ppt_setup_msi(struct vm *vm, int vcpu, int bus, int sl if (numvec < 0 || numvec > MAX_MSIMSGS) return (EINVAL); - ppt = ppt_find(bus, slot, func); - if (ppt == NULL) - return (ENOENT); - if (ppt->vm != vm) /* Make sure we own this device */ - return (EBUSY); + error = ppt_find(vm, bus, slot, func, &ppt); + if (error) + return (error); /* Reject attempts to enable MSI while MSI-X is active. */ if (ppt->msix.num_msgs != 0 && numvec != 0) @@ -604,11 +595,9 @@ ppt_setup_msix(struct vm *vm, int vcpu, int bus, int s int numvec, alloced, rid, error; size_t res_size, cookie_size, arg_size; - ppt = ppt_find(bus, slot, func); - if (ppt == NULL) - return (ENOENT); - if (ppt->vm != vm) /* Make sure we own this device */ - return (EBUSY); + error = ppt_find(vm, bus, slot, func, &ppt); + if (error) + return (error); /* Reject attempts to enable MSI-X while MSI is active. */ if (ppt->msi.num_msgs != 0) @@ -712,12 +701,11 @@ int ppt_disable_msix(struct vm *vm, int bus, int slot, int func) { struct pptdev *ppt; + int error; - ppt = ppt_find(bus, slot, func); - if (ppt == NULL) - return (ENOENT); - if (ppt->vm != vm) /* Make sure we own this device */ - return (EBUSY); + error = ppt_find(vm, bus, slot, func, &ppt); + if (error) + return (error); ppt_teardown_msix(ppt); return (0);