From: "Jan Beulich" Subject: [PATCH] VT-d: fix RMRR handling Date: Mon, 10 Feb 2014 13:42:53 +0000 Message-ID: <52F8E56D020000780011AC43@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part586BFF4D.1__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WCr8V-00025k-3q for xen-devel@lists.xenproject.org; Mon, 10 Feb 2014 13:42:59 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel Cc: xiantao.zhang@intel.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. --=__Part586BFF4D.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Removing mapped RMRR tracking structures in dma_pte_clear_one() is wrong for two reasons: First, these regions may cover more than a single page. And second, multiple devices (and hence multiple devices assigned to any particular guest) may share a single RMRR (whether assigning such devices to distinct guests is a safe thing to do is another question). Therefore move the removal of the tracking structures into the counterpart function to the one doing the insertion - intel_iommu_remove_device(), and add a reference count to the tracking structure. Further, for the handling of the mappings of the respective memory regions to be correct, RMRRs must not overlap. Add a respective check to acpi_parse_one_rmrr(). And finally, with all of this being VT-d specific, move the cleanup of the list as well as the structure type definition where it belongs - in VT-d specific rather than IOMMU generic code. Note that this doesn't address yet another issue associated with RMRR handling: The purpose of the RMRRs as well as the way the respective IOMMU page table mappings get inserted both suggest that these regions would need to be marked E820_RESERVED in all (HVM?) guests' memory maps, yet nothing like this is being done in hvmloader. (For PV guests this would also seem to be necessary, but may conflict with PV guests possibly assuming there to be just a single E820 entry representing all of its RAM.) Signed-off-by: Jan Beulich --- a/xen/drivers/passthrough/vtd/dmar.c +++ b/xen/drivers/passthrough/vtd/dmar.c @@ -580,6 +580,16 @@ acpi_parse_one_rmrr(struct acpi_dmar_hea if ( (ret =3D acpi_dmar_check_length(header, sizeof(*rmrr))) !=3D 0 ) return ret; =20 + list_for_each_entry(rmrru, &acpi_rmrr_units, list) + if ( base_addr <=3D rmrru->end_address && rmrru->base_address <=3D = end_addr ) + { + printk(XENLOG_ERR VTDPREFIX + "Overlapping RMRRs [%"PRIx64",%"PRIx64"] and [%"PRIx64",= %"PRIx64"]\n", + rmrru->base_address, rmrru->end_address, + base_addr, end_addr); + return -EEXIST; + } + /* This check is here simply to detect when RMRR values are * not properly represented in the system memory map and * inform the user --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -412,9 +412,8 @@ static int iommu_populate_page_table(str void iommu_domain_destroy(struct domain *d) { struct hvm_iommu *hd =3D domain_hvm_iommu(d); - struct list_head *ioport_list, *rmrr_list, *tmp; + struct list_head *ioport_list, *tmp; struct g2m_ioport *ioport; - struct mapped_rmrr *mrmrr; =20 if ( !iommu_enabled || !hd->platform_ops ) return; @@ -428,13 +427,6 @@ void iommu_domain_destroy(struct domain=20 list_del(&ioport->list); xfree(ioport); } - - list_for_each_safe ( rmrr_list, tmp, &hd->mapped_rmrrs ) - { - mrmrr =3D list_entry(rmrr_list, struct mapped_rmrr, list); - list_del(&mrmrr->list); - xfree(mrmrr); - } } =20 int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long = mfn, --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -43,6 +43,12 @@ #include "vtd.h" #include "../ats.h" =20 +struct mapped_rmrr { + struct list_head list; + u64 base, end; + unsigned int count; +}; + /* Possible unfiltered LAPIC/MSI messages from untrusted sources? */ bool_t __read_mostly untrusted_msi; =20 @@ -620,7 +626,6 @@ static void dma_pte_clear_one(struct dom struct hvm_iommu *hd =3D domain_hvm_iommu(domain); struct dma_pte *page =3D NULL, *pte =3D NULL; u64 pg_maddr; - struct mapped_rmrr *mrmrr; =20 spin_lock(&hd->mapping_lock); /* get last level pte */ @@ -649,21 +654,6 @@ static void dma_pte_clear_one(struct dom __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K, 1, 1); =20 unmap_vtd_domain_page(page); - - /* if the cleared address is between mapped RMRR region, - * remove the mapped RMRR - */ - spin_lock(&hd->mapping_lock); - list_for_each_entry ( mrmrr, &hd->mapped_rmrrs, list ) - { - if ( addr >=3D mrmrr->base && addr <=3D mrmrr->end ) - { - list_del(&mrmrr->list); - xfree(mrmrr); - break; - } - } - spin_unlock(&hd->mapping_lock); } =20 static void iommu_free_pagetable(u64 pt_maddr, int level) @@ -1704,10 +1694,17 @@ static int reassign_device_ownership( void iommu_domain_teardown(struct domain *d) { struct hvm_iommu *hd =3D domain_hvm_iommu(d); + struct mapped_rmrr *mrmrr, *tmp; =20 if ( list_empty(&acpi_drhd_units) ) return; =20 + list_for_each_entry_safe ( mrmrr, tmp, &hd->mapped_rmrrs, list ) + { + list_del(&mrmrr->list); + xfree(mrmrr); + } + if ( iommu_use_hap_pt(d) ) return; =20 @@ -1852,14 +1849,17 @@ static int rmrr_identity_mapping(struct=20 ASSERT(rmrr->base_address < rmrr->end_address); =20 /* - * No need to acquire hd->mapping_lock, as the only theoretical race = is - * with the insertion below (impossible due to holding pcidevs_lock). + * No need to acquire hd->mapping_lock: Both insertion and removal + * get done while holding pcidevs_lock. */ list_for_each_entry( mrmrr, &hd->mapped_rmrrs, list ) { if ( mrmrr->base =3D=3D rmrr->base_address && mrmrr->end =3D=3D rmrr->end_address ) + { + ++mrmrr->count; return 0; + } } =20 base =3D rmrr->base_address & PAGE_MASK_4K; @@ -1880,9 +1880,8 @@ static int rmrr_identity_mapping(struct=20 return -ENOMEM; mrmrr->base =3D rmrr->base_address; mrmrr->end =3D rmrr->end_address; - spin_lock(&hd->mapping_lock); + mrmrr->count =3D 1; list_add_tail(&mrmrr->list, &hd->mapped_rmrrs); - spin_unlock(&hd->mapping_lock); =20 return 0; } @@ -1944,17 +1943,50 @@ static int intel_iommu_remove_device(u8=20 if ( !pdev->domain ) return -EINVAL; =20 - /* If the device belongs to dom0, and it has RMRR, don't remove it - * from dom0, because BIOS may use RMRR at booting time. - */ - if ( pdev->domain->domain_id =3D=3D 0 ) + for_each_rmrr_device ( rmrr, bdf, i ) { - for_each_rmrr_device ( rmrr, bdf, i ) + struct hvm_iommu *hd; + struct mapped_rmrr *mrmrr, *tmp; + + if ( rmrr->segment !=3D pdev->seg || + PCI_BUS(bdf) !=3D pdev->bus || + PCI_DEVFN2(bdf) !=3D devfn ) + continue; + + /* + * If the device belongs to dom0, and it has RMRR, don't remove + * it from dom0, because BIOS may use RMRR at booting time. + */ + if ( is_hardware_domain(pdev->domain) ) + return 0; + + hd =3D domain_hvm_iommu(pdev->domain); + + /* + * No need to acquire hd->mapping_lock: Both insertion and = removal + * get done while holding pcidevs_lock. + */ + ASSERT(spin_is_locked(&pcidevs_lock)); + list_for_each_entry_safe ( mrmrr, tmp, &hd->mapped_rmrrs, list ) { - if ( rmrr->segment =3D=3D pdev->seg && - PCI_BUS(bdf) =3D=3D pdev->bus && - PCI_DEVFN2(bdf) =3D=3D devfn ) - return 0; + unsigned long base_pfn, end_pfn; + + if ( rmrr->base_address !=3D mrmrr->base || + rmrr->end_address !=3D mrmrr->end || + --mrmrr->count ) + continue; + + base_pfn =3D (mrmrr->base & PAGE_MASK_4K) >> PAGE_SHIFT_4K; + end_pfn =3D PAGE_ALIGN_4K(mrmrr->end) >> PAGE_SHIFT_4K; + while ( base_pfn < end_pfn ) + { + if ( intel_iommu_unmap_page(pdev->domain, base_pfn) ) + return -ENXIO; + base_pfn++; + } + + list_del(&mrmrr->list); + xfree(mrmrr); } } =20 --- a/xen/include/xen/hvm/iommu.h +++ b/xen/include/xen/hvm/iommu.h @@ -29,12 +29,6 @@ struct g2m_ioport { unsigned int np; }; =20 -struct mapped_rmrr { - struct list_head list; - u64 base; - u64 end; -}; - struct hvm_iommu { u64 pgd_maddr; /* io page directory machine address = */ spinlock_t mapping_lock; /* io page table lock */ --=__Part586BFF4D.1__= Content-Type: text/plain; name="VT-d-RMRR-handling.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="VT-d-RMRR-handling.patch" VT-d: fix RMRR handling=0A=0ARemoving mapped RMRR tracking structures in = dma_pte_clear_one() is=0Awrong for two reasons: First, these regions may = cover more than a=0Asingle page. And second, multiple devices (and hence = multiple devices=0Aassigned to any particular guest) may share a single = RMRR (whether=0Aassigning such devices to distinct guests is a safe thing = to do is=0Aanother question).=0A=0ATherefore move the removal of the = tracking structures into the=0Acounterpart function to the one doing the = insertion -=0Aintel_iommu_remove_device(), and add a reference count to = the tracking=0Astructure.=0A=0AFurther, for the handling of the mappings = of the respective memory=0Aregions to be correct, RMRRs must not overlap. = Add a respective check=0Ato acpi_parse_one_rmrr().=0A=0AAnd finally, with = all of this being VT-d specific, move the cleanup=0Aof the list as well as = the structure type definition where it belongs -=0Ain VT-d specific rather = than IOMMU generic code.=0A=0ANote that this doesn't address yet another = issue associated with RMRR=0Ahandling: The purpose of the RMRRs as well as = the way the respective=0AIOMMU page table mappings get inserted both = suggest that these regions=0Awould need to be marked E820_RESERVED in all = (HVM?) guests' memory=0Amaps, yet nothing like this is being done in = hvmloader. (For PV guests=0Athis would also seem to be necessary, but may = conflict with PV guests=0Apossibly assuming there to be just a single E820 = entry representing all=0Aof its RAM.)=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/drivers/passthrough/vtd/dmar.c=0A+++ = b/xen/drivers/passthrough/vtd/dmar.c=0A@@ -580,6 +580,16 @@ acpi_parse_one_= rmrr(struct acpi_dmar_hea=0A if ( (ret =3D acpi_dmar_check_length(heade= r, sizeof(*rmrr))) !=3D 0 )=0A return ret;=0A =0A+ list_for_each= _entry(rmrru, &acpi_rmrr_units, list)=0A+ if ( base_addr <=3D = rmrru->end_address && rmrru->base_address <=3D end_addr )=0A+ {=0A+ = printk(XENLOG_ERR VTDPREFIX=0A+ "Overlapping = RMRRs [%"PRIx64",%"PRIx64"] and [%"PRIx64",%"PRIx64"]\n",=0A+ = rmrru->base_address, rmrru->end_address,=0A+ = base_addr, end_addr);=0A+ return -EEXIST;=0A+ }=0A+=0A = /* This check is here simply to detect when RMRR values are=0A * not = properly represented in the system memory map and=0A * inform the = user=0A--- a/xen/drivers/passthrough/iommu.c=0A+++ b/xen/drivers/passthroug= h/iommu.c=0A@@ -412,9 +412,8 @@ static int iommu_populate_page_table(str=0A= void iommu_domain_destroy(struct domain *d)=0A {=0A struct hvm_iommu = *hd =3D domain_hvm_iommu(d);=0A- struct list_head *ioport_list, = *rmrr_list, *tmp;=0A+ struct list_head *ioport_list, *tmp;=0A = struct g2m_ioport *ioport;=0A- struct mapped_rmrr *mrmrr;=0A =0A if = ( !iommu_enabled || !hd->platform_ops )=0A return;=0A@@ -428,13 = +427,6 @@ void iommu_domain_destroy(struct domain =0A list_del(&iop= ort->list);=0A xfree(ioport);=0A }=0A-=0A- list_for_each_saf= e ( rmrr_list, tmp, &hd->mapped_rmrrs )=0A- {=0A- mrmrr =3D = list_entry(rmrr_list, struct mapped_rmrr, list);=0A- list_del(&mrmrr= ->list);=0A- xfree(mrmrr);=0A- }=0A }=0A =0A int iommu_map_page(s= truct domain *d, unsigned long gfn, unsigned long mfn,=0A--- a/xen/drivers/= passthrough/vtd/iommu.c=0A+++ b/xen/drivers/passthrough/vtd/iommu.c=0A@@ = -43,6 +43,12 @@=0A #include "vtd.h"=0A #include "../ats.h"=0A =0A+struct = mapped_rmrr {=0A+ struct list_head list;=0A+ u64 base, end;=0A+ = unsigned int count;=0A+};=0A+=0A /* Possible unfiltered LAPIC/MSI messages = from untrusted sources? */=0A bool_t __read_mostly untrusted_msi;=0A =0A@@ = -620,7 +626,6 @@ static void dma_pte_clear_one(struct dom=0A struct = hvm_iommu *hd =3D domain_hvm_iommu(domain);=0A struct dma_pte *page = =3D NULL, *pte =3D NULL;=0A u64 pg_maddr;=0A- struct mapped_rmrr = *mrmrr;=0A =0A spin_lock(&hd->mapping_lock);=0A /* get last level = pte */=0A@@ -649,21 +654,6 @@ static void dma_pte_clear_one(struct dom=0A = __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K, 1, 1);=0A = =0A unmap_vtd_domain_page(page);=0A-=0A- /* if the cleared address = is between mapped RMRR region,=0A- * remove the mapped RMRR=0A- = */=0A- spin_lock(&hd->mapping_lock);=0A- list_for_each_entry ( = mrmrr, &hd->mapped_rmrrs, list )=0A- {=0A- if ( addr >=3D = mrmrr->base && addr <=3D mrmrr->end )=0A- {=0A- = list_del(&mrmrr->list);=0A- xfree(mrmrr);=0A- = break;=0A- }=0A- }=0A- spin_unlock(&hd->mapping_lock);=0A = }=0A =0A static void iommu_free_pagetable(u64 pt_maddr, int level)=0A@@ = -1704,10 +1694,17 @@ static int reassign_device_ownership(=0A void = iommu_domain_teardown(struct domain *d)=0A {=0A struct hvm_iommu *hd = =3D domain_hvm_iommu(d);=0A+ struct mapped_rmrr *mrmrr, *tmp;=0A =0A = if ( list_empty(&acpi_drhd_units) )=0A return;=0A =0A+ = list_for_each_entry_safe ( mrmrr, tmp, &hd->mapped_rmrrs, list )=0A+ = {=0A+ list_del(&mrmrr->list);=0A+ xfree(mrmrr);=0A+ = }=0A+=0A if ( iommu_use_hap_pt(d) )=0A return;=0A =0A@@ = -1852,14 +1849,17 @@ static int rmrr_identity_mapping(struct =0A = ASSERT(rmrr->base_address < rmrr->end_address);=0A =0A /*=0A- * No = need to acquire hd->mapping_lock, as the only theoretical race is=0A- = * with the insertion below (impossible due to holding pcidevs_lock).=0A+ = * No need to acquire hd->mapping_lock: Both insertion and removal=0A+ = * get done while holding pcidevs_lock.=0A */=0A list_for_each_ent= ry( mrmrr, &hd->mapped_rmrrs, list )=0A {=0A if ( mrmrr->base = =3D=3D rmrr->base_address &&=0A mrmrr->end =3D=3D rmrr->end_ad= dress )=0A+ {=0A+ ++mrmrr->count;=0A return = 0;=0A+ }=0A }=0A =0A base =3D rmrr->base_address & = PAGE_MASK_4K;=0A@@ -1880,9 +1880,8 @@ static int rmrr_identity_mapping(stru= ct =0A return -ENOMEM;=0A mrmrr->base =3D rmrr->base_address;= =0A mrmrr->end =3D rmrr->end_address;=0A- spin_lock(&hd->mapping_loc= k);=0A+ mrmrr->count =3D 1;=0A list_add_tail(&mrmrr->list, = &hd->mapped_rmrrs);=0A- spin_unlock(&hd->mapping_lock);=0A =0A = return 0;=0A }=0A@@ -1944,17 +1943,50 @@ static int intel_iommu_remove_devi= ce(u8 =0A if ( !pdev->domain )=0A return -EINVAL;=0A =0A- = /* If the device belongs to dom0, and it has RMRR, don't remove it=0A- = * from dom0, because BIOS may use RMRR at booting time.=0A- */=0A- = if ( pdev->domain->domain_id =3D=3D 0 )=0A+ for_each_rmrr_device ( = rmrr, bdf, i )=0A {=0A- for_each_rmrr_device ( rmrr, bdf, i = )=0A+ struct hvm_iommu *hd;=0A+ struct mapped_rmrr *mrmrr, = *tmp;=0A+=0A+ if ( rmrr->segment !=3D pdev->seg ||=0A+ = PCI_BUS(bdf) !=3D pdev->bus ||=0A+ PCI_DEVFN2(bdf) !=3D devfn = )=0A+ continue;=0A+=0A+ /*=0A+ * If the device = belongs to dom0, and it has RMRR, don't remove=0A+ * it from dom0, = because BIOS may use RMRR at booting time.=0A+ */=0A+ if ( = is_hardware_domain(pdev->domain) )=0A+ return 0;=0A+=0A+ = hd =3D domain_hvm_iommu(pdev->domain);=0A+=0A+ /*=0A+ * No = need to acquire hd->mapping_lock: Both insertion and removal=0A+ * = get done while holding pcidevs_lock.=0A+ */=0A+ ASSERT(spin_= is_locked(&pcidevs_lock));=0A+ list_for_each_entry_safe ( mrmrr, = tmp, &hd->mapped_rmrrs, list )=0A {=0A- if ( rmrr->segme= nt =3D=3D pdev->seg &&=0A- PCI_BUS(bdf) =3D=3D pdev->bus = &&=0A- PCI_DEVFN2(bdf) =3D=3D devfn )=0A- = return 0;=0A+ unsigned long base_pfn, end_pfn;=0A+=0A+ = if ( rmrr->base_address !=3D mrmrr->base ||=0A+ = rmrr->end_address !=3D mrmrr->end ||=0A+ --mrmrr->count = )=0A+ continue;=0A+=0A+ base_pfn =3D (mrmrr->base= & PAGE_MASK_4K) >> PAGE_SHIFT_4K;=0A+ end_pfn =3D PAGE_ALIGN_4K= (mrmrr->end) >> PAGE_SHIFT_4K;=0A+ while ( base_pfn < end_pfn = )=0A+ {=0A+ if ( intel_iommu_unmap_page(pdev->dom= ain, base_pfn) )=0A+ return -ENXIO;=0A+ = base_pfn++;=0A+ }=0A+=0A+ list_del(&mrmrr->list);=0A+= xfree(mrmrr);=0A }=0A }=0A =0A--- a/xen/include/xen= /hvm/iommu.h=0A+++ b/xen/include/xen/hvm/iommu.h=0A@@ -29,12 +29,6 @@ = struct g2m_ioport {=0A unsigned int np;=0A };=0A =0A-struct mapped_rmrr= {=0A- struct list_head list;=0A- u64 base;=0A- u64 end;=0A-};=0A-= =0A struct hvm_iommu {=0A u64 pgd_maddr; /* io page = directory machine address */=0A spinlock_t mapping_lock; /* io = page table lock */=0A --=__Part586BFF4D.1__= 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.xen.org http://lists.xen.org/xen-devel --=__Part586BFF4D.1__=--