From owner-svn-src-all@FreeBSD.ORG Wed Mar 4 21:04:53 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 27DA31065707; Wed, 4 Mar 2009 21:04:53 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 943238FC1B; Wed, 4 Mar 2009 21:04:52 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n24L4qF2053294; Wed, 4 Mar 2009 21:04:52 GMT (envelope-from jhb@svn.freebsd.org) Received: (from jhb@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n24L4qMc053293; Wed, 4 Mar 2009 21:04:52 GMT (envelope-from jhb@svn.freebsd.org) Message-Id: <200903042104.n24L4qMc053293@svn.freebsd.org> From: John Baldwin Date: Wed, 4 Mar 2009 21:04:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r189373 - head/sys/dev/pci X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Mar 2009 21:04:54 -0000 Author: jhb Date: Wed Mar 4 21:04:52 2009 New Revision: 189373 URL: http://svn.freebsd.org/changeset/base/189373 Log: The recent PCI resource allocation fixes exposed a bug where the same BAR could be allocated twice by different children of a vgapci0 device. To fix this, change the vgapci0 device to track references on its associated resources so that they are only allocated once from the parent PCI bus and released when no children are using them. Previously this leaked a small amount of KVA on at least some architectures. Modified: head/sys/dev/pci/vga_pci.c Modified: head/sys/dev/pci/vga_pci.c ============================================================================== --- head/sys/dev/pci/vga_pci.c Wed Mar 4 20:58:22 2009 (r189372) +++ head/sys/dev/pci/vga_pci.c Wed Mar 4 21:04:52 2009 (r189373) @@ -42,12 +42,20 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include #include #include +struct vga_resource { + struct resource *vr_res; + int vr_refs; +}; + struct vga_pci_softc { device_t vga_msi_child; /* Child driver using MSI. */ + struct vga_resource vga_res[PCIR_MAX_BAR_0 + 1]; }; static int @@ -130,7 +138,27 @@ static struct resource * vga_pci_alloc_resource(device_t dev, device_t child, int type, int *rid, u_long start, u_long end, u_long count, u_int flags) { + struct vga_pci_softc *sc; + int bar; + switch (type) { + case SYS_RES_MEMORY: + case SYS_RES_IOPORT: + /* + * For BARs, we cache the resource so that we only allocate it + * from the PCI bus once. + */ + bar = PCI_RID2BAR(*rid); + if (bar < 0 || bar > PCIR_MAX_BAR_0) + return (NULL); + sc = device_get_softc(dev); + if (sc->vga_res[bar].vr_res == NULL) + sc->vga_res[bar].vr_res = bus_alloc_resource(dev, type, + rid, start, end, count, flags); + if (sc->vga_res[bar].vr_res != NULL) + sc->vga_res[bar].vr_refs++; + return (sc->vga_res[bar].vr_res); + } return (bus_alloc_resource(dev, type, rid, start, end, count, flags)); } @@ -138,6 +166,37 @@ static int vga_pci_release_resource(device_t dev, device_t child, int type, int rid, struct resource *r) { + struct vga_pci_softc *sc; + int bar, error; + + switch (type) { + case SYS_RES_MEMORY: + case SYS_RES_IOPORT: + /* + * For BARs, we release the resource from the PCI bus + * when the last child reference goes away. + */ + bar = PCI_RID2BAR(rid); + if (bar < 0 || bar > PCIR_MAX_BAR_0) + return (EINVAL); + sc = device_get_softc(dev); + if (sc->vga_res[bar].vr_res == NULL) + return (EINVAL); + KASSERT(sc->vga_res[bar].vr_res == r, + ("vga_pci resource mismatch")); + if (sc->vga_res[bar].vr_refs > 1) { + sc->vga_res[bar].vr_refs--; + return (0); + } + KASSERT(sc->vga_res[bar].vr_refs > 0, + ("vga_pci resource reference count underflow")); + error = bus_release_resource(dev, type, rid, r); + if (error == 0) { + sc->vga_res[bar].vr_res = NULL; + sc->vga_res[bar].vr_refs = 0; + } + return (error); + } return (bus_release_resource(dev, type, rid, r)); }