From: "Jan Beulich" Subject: [PATCH] VT-d/ATS: misc fixes Date: Tue, 18 Jan 2011 11:32:41 +0000 Message-ID: <4D358869020000780002CDB4@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part230F5649.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part230F5649.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline First of all there were three places potentially de-referencing NULL (two after an allocation failure, and one after a failed lookup). Second, if ATS_ENABLE was already set, the device would not have got added to the ats_devices list, potentially resulting in dev_invalidate_iotlb() doing an incomplete job. Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/vtd/x86/ats.c +++ b/xen/drivers/passthrough/vtd/x86/ats.c @@ -93,6 +93,9 @@ int ats_device(int seg, int bus, int dev return 0; =20 pdev =3D pci_get_pdev(bus, devfn); + if ( !pdev ) + return 0; + drhd =3D acpi_find_matched_drhd_unit(pdev); if ( !drhd ) return 0; @@ -110,6 +113,8 @@ int ats_device(int seg, int bus, int dev if ( pos && (ats_drhd =3D=3D NULL) ) { new_drhd =3D xmalloc(struct acpi_drhd_unit); + if ( !new_drhd ) + return 0; memcpy(new_drhd, drhd, sizeof(struct acpi_drhd_unit)); list_add_tail(&new_drhd->list, &ats_dev_drhd_units); } @@ -118,9 +123,8 @@ int ats_device(int seg, int bus, int dev =20 int enable_ats_device(int seg, int bus, int devfn) { - struct pci_ats_dev *pdev; + struct pci_ats_dev *pdev =3D NULL; u32 value; - u16 queue_depth; int pos; =20 if ( !acpi_find_matched_atsr_unit(bus, devfn) ) @@ -144,26 +148,43 @@ int enable_ats_device(int seg, int bus,=20 =20 /* BUGBUG: add back seg when multi-seg platform support is enabled */ value =3D pci_conf_read16(bus, PCI_SLOT(devfn), - PCI_FUNC(devfn), pos + ATS_REG_CAP); - queue_depth =3D value & ATS_QUEUE_DEPTH_MASK; - - value =3D pci_conf_read16(bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pos + ATS_REG_CTL); if ( value & ATS_ENABLE ) - return 0; + { + list_for_each_entry ( pdev, &ats_devices, list ) + { + if ( pdev->bus =3D=3D bus && pdev->devfn =3D=3D devfn ) + { + pos =3D 0; + break; + } + } + } + if ( pos ) + pdev =3D xmalloc(struct pci_ats_dev); + if ( !pdev ) + return -ENOMEM; =20 - value |=3D ATS_ENABLE; - pci_conf_write16(bus, PCI_SLOT(devfn), PCI_FUNC(devfn), - pos + ATS_REG_CTL, value); + if ( !(value & ATS_ENABLE) ) + { + value |=3D ATS_ENABLE; + pci_conf_write16(bus, PCI_SLOT(devfn), PCI_FUNC(devfn), + pos + ATS_REG_CTL, value); + } + + if ( pos ) + { + pdev->bus =3D bus; + pdev->devfn =3D devfn; + value =3D pci_conf_read16(bus, PCI_SLOT(devfn), + PCI_FUNC(devfn), pos + ATS_REG_CAP); + pdev->ats_queue_depth =3D value & ATS_QUEUE_DEPTH_MASK; + list_add(&pdev->list, &ats_devices); + } =20 - pdev =3D xmalloc(struct pci_ats_dev); - pdev->bus =3D bus; - pdev->devfn =3D devfn; - pdev->ats_queue_depth =3D queue_depth; - list_add(&(pdev->list), &ats_devices); if ( iommu_verbose ) - dprintk(XENLOG_INFO VTDPREFIX, "%x:%x.%x: ATS is enabled\n", - bus, PCI_SLOT(devfn), PCI_FUNC(devfn)); + dprintk(XENLOG_INFO VTDPREFIX, "%x:%x.%x: ATS %s enabled\n", + bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pos ? "is" : = "was"); =20 return pos; } --=__Part230F5649.0__= Content-Type: text/plain; name="vtd-ats-null-deref.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="vtd-ats-null-deref.patch" First of all there were three places potentially de-referencing NULL=0A(two= after an allocation failure, and one after a failed lookup).=0A=0ASecond, = if ATS_ENABLE was already set, the device would not have got=0Aadded to = the ats_devices list, potentially resulting in=0Adev_invalidate_iotlb() = doing an incomplete job.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/drivers/passthrough/vtd/x86/ats.c=0A+++ b/xen/drivers/pa= ssthrough/vtd/x86/ats.c=0A@@ -93,6 +93,9 @@ int ats_device(int seg, int = bus, int dev=0A return 0;=0A =0A pdev =3D pci_get_pdev(bus, = devfn);=0A+ if ( !pdev )=0A+ return 0;=0A+=0A drhd =3D = acpi_find_matched_drhd_unit(pdev);=0A if ( !drhd )=0A return = 0;=0A@@ -110,6 +113,8 @@ int ats_device(int seg, int bus, int dev=0A = if ( pos && (ats_drhd =3D=3D NULL) )=0A {=0A new_drhd =3D = xmalloc(struct acpi_drhd_unit);=0A+ if ( !new_drhd )=0A+ = return 0;=0A memcpy(new_drhd, drhd, sizeof(struct acpi_drhd_unit));= =0A list_add_tail(&new_drhd->list, &ats_dev_drhd_units);=0A = }=0A@@ -118,9 +123,8 @@ int ats_device(int seg, int bus, int dev=0A =0A = int enable_ats_device(int seg, int bus, int devfn)=0A {=0A- struct = pci_ats_dev *pdev;=0A+ struct pci_ats_dev *pdev =3D NULL;=0A u32 = value;=0A- u16 queue_depth;=0A int pos;=0A =0A if ( !acpi_find_m= atched_atsr_unit(bus, devfn) )=0A@@ -144,26 +148,43 @@ int enable_ats_devic= e(int seg, int bus, =0A =0A /* BUGBUG: add back seg when multi-seg = platform support is enabled */=0A value =3D pci_conf_read16(bus, = PCI_SLOT(devfn),=0A- PCI_FUNC(devfn), pos + = ATS_REG_CAP);=0A- queue_depth =3D value & ATS_QUEUE_DEPTH_MASK;=0A-=0A- = value =3D pci_conf_read16(bus, PCI_SLOT(devfn),=0A = PCI_FUNC(devfn), pos + ATS_REG_CTL);=0A if ( value & ATS_ENABLE = )=0A- return 0;=0A+ {=0A+ list_for_each_entry ( pdev, = &ats_devices, list )=0A+ {=0A+ if ( pdev->bus =3D=3D bus = && pdev->devfn =3D=3D devfn )=0A+ {=0A+ pos =3D = 0;=0A+ break;=0A+ }=0A+ }=0A+ }=0A+ = if ( pos )=0A+ pdev =3D xmalloc(struct pci_ats_dev);=0A+ if ( = !pdev )=0A+ return -ENOMEM;=0A =0A- value |=3D ATS_ENABLE;=0A- = pci_conf_write16(bus, PCI_SLOT(devfn), PCI_FUNC(devfn),=0A- = pos + ATS_REG_CTL, value);=0A+ if ( !(value & ATS_ENABLE) )=0A+ = {=0A+ value |=3D ATS_ENABLE;=0A+ pci_conf_write16(bus, = PCI_SLOT(devfn), PCI_FUNC(devfn),=0A+ pos + = ATS_REG_CTL, value);=0A+ }=0A+=0A+ if ( pos )=0A+ {=0A+ = pdev->bus =3D bus;=0A+ pdev->devfn =3D devfn;=0A+ value =3D = pci_conf_read16(bus, PCI_SLOT(devfn),=0A+ = PCI_FUNC(devfn), pos + ATS_REG_CAP);=0A+ pdev->ats_queue_depth =3D = value & ATS_QUEUE_DEPTH_MASK;=0A+ list_add(&pdev->list, &ats_devices= );=0A+ }=0A =0A- pdev =3D xmalloc(struct pci_ats_dev);=0A- = pdev->bus =3D bus;=0A- pdev->devfn =3D devfn;=0A- pdev->ats_queue_dep= th =3D queue_depth;=0A- list_add(&(pdev->list), &ats_devices);=0A = if ( iommu_verbose )=0A- dprintk(XENLOG_INFO VTDPREFIX, "%x:%x.%x: = ATS is enabled\n",=0A- bus, PCI_SLOT(devfn), PCI_FUNC(devfn)= );=0A+ dprintk(XENLOG_INFO VTDPREFIX, "%x:%x.%x: ATS %s enabled\n",= =0A+ bus, PCI_SLOT(devfn), PCI_FUNC(devfn), pos ? "is" : = "was");=0A =0A return pos;=0A }=0A --=__Part230F5649.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part230F5649.0__=--