From: "Jan Beulich" Subject: [PATCH] IOMMU/x86: avoid pages without GFN in page table creation/updating Date: Thu, 07 May 2015 13:59:55 +0100 Message-ID: <554B7DEB020000780007793D@mail.emea.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartCCF8DFDB.4__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1YqLPH-0008A9-IL for xen-devel@lists.xenproject.org; Thu, 07 May 2015 13:00:03 +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: Kevin Tian , Andrew Cooper , Tim Deegan , Sander Eikelenboom , Aravind Gopalakrishnan , suravee.suthikulpanit@amd.com, Yang Z Zhang 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. --=__PartCCF8DFDB.4__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest. While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard: "Ideally this loop would be iterating over all gfns in the p2m rather than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient." Original by Andrew Cooper , using further suggestions from Tim Deegan . Reported-by: Sander Eikelenboom Signed-off-by: Jan Beulich Tested-by: Sander Eikelenboom Acked-by: Tim Deegan --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -557,6 +557,10 @@ static int update_paging_mode(struct dom unsigned long old_root_mfn; struct hvm_iommu *hd =3D domain_hvm_iommu(d); =20 + if ( gfn =3D=3D INVALID_MFN ) + return -EADDRNOTAVAIL; + ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH)); + level =3D hd->arch.paging_mode; old_root =3D hd->arch.root_table; offset =3D gfn >> (PTE_PER_TABLE_SHIFT * (level - 1)); @@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain * * we might need a deeper page table for lager gfn now */ if ( is_hvm_domain(d) ) { - if ( update_paging_mode(d, gfn) ) + int rc =3D update_paging_mode(d, gfn); + + if ( rc ) { spin_unlock(&hd->arch.mapping_lock); AMD_IOMMU_DEBUG("Update page mode failed gfn =3D %lx\n", = gfn); - domain_crash(d); - return -EFAULT; + if ( rc !=3D -EADDRNOTAVAIL ) + domain_crash(d); + return rc; } } =20 --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -482,7 +482,6 @@ struct qinval_entry { #define VTD_PAGE_TABLE_LEVEL_3 3 #define VTD_PAGE_TABLE_LEVEL_4 4 =20 -#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 #define MAX_IOMMU_REGS 0xc0 =20 extern struct list_head acpi_drhd_units; --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc if ( has_hvm_container_domain(d) || (page->u.inuse.type_info & PGT_type_mask) =3D=3D PGT_writable_= page ) { - BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page)))); - rc =3D hd->platform_ops->map_page( - d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page), - IOMMUF_readable|IOMMUF_writable); + unsigned long mfn =3D page_to_mfn(page); + unsigned long gfn =3D mfn_to_gmfn(d, mfn); + + if ( gfn !=3D INVALID_MFN ) + { + ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH)); + BUG_ON(SHARED_M2P(gfn)); + rc =3D hd->platform_ops->map_page(d, gfn, mfn, + IOMMUF_readable | + IOMMUF_writable); + } if ( rc ) { page_list_add(page, &d->page_list); --- a/xen/include/asm-x86/hvm/iommu.h +++ b/xen/include/asm-x86/hvm/iommu.h @@ -46,6 +46,8 @@ struct g2m_ioport { unsigned int np; }; =20 +#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 + struct arch_hvm_iommu { u64 pgd_maddr; /* io page directory machine address = */ --- a/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +++ b/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h @@ -464,8 +464,6 @@ #define IOMMU_CONTROL_DISABLED 0 #define IOMMU_CONTROL_ENABLED 1 =20 -#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 - /* interrupt remapping table */ #define INT_REMAP_ENTRY_REMAPEN_MASK 0x00000001 #define INT_REMAP_ENTRY_REMAPEN_SHIFT 0 --=__PartCCF8DFDB.4__= Content-Type: text/plain; name="IOMMU-populate-PT-filter-invalid.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="IOMMU-populate-PT-filter-invalid.patch" IOMMU/x86: avoid pages without GFN in page table creation/updating=0A=0AHan= ding INVALID_GFN to functions like hd->platform_ops->map_page()=0Ajust = can't do any good, and the ioreq server code results in such pages=0Abeing = on the list of ones owned by a guest.=0A=0AWhile - as suggested by Tim - = we should use get_gfn()/put_gfn() there=0Ato eliminate races, we really = can't due to holding the domain's page=0Aalloc lock. Ultimately arch_iommu_= populate_page_table() may need to be=0Aswitched to be GFN based. Here is = what Tim said in this regard:=0A"Ideally this loop would be iterating over = all gfns in the p2m rather=0A than over all owned MFNs. As long as = needs_iommu gets set first,=0A such a loop could safely be paused and = restarted without worrying=0A about concurrent updates. The code sould = even stay in this file,=0A though exposing an iterator from the p2m code = would be a lot more=0A efficient."=0A=0AOriginal by Andrew Cooper = , using further=0Asuggestions from Tim Deegan = .=0A=0AReported-by: Sander Eikelenboom = =0ASigned-off-by: Jan Beulich =0ATested-by: Sander = Eikelenboom =0AAcked-by: Tim Deegan =0A= =0A--- a/xen/drivers/passthrough/amd/iommu_map.c=0A+++ b/xen/drivers/passth= rough/amd/iommu_map.c=0A@@ -557,6 +557,10 @@ static int update_paging_mode(= struct dom=0A unsigned long old_root_mfn;=0A struct hvm_iommu *hd = =3D domain_hvm_iommu(d);=0A =0A+ if ( gfn =3D=3D INVALID_MFN )=0A+ = return -EADDRNOTAVAIL;=0A+ ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDT= H));=0A+=0A level =3D hd->arch.paging_mode;=0A old_root =3D = hd->arch.root_table;=0A offset =3D gfn >> (PTE_PER_TABLE_SHIFT * = (level - 1));=0A@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct = domain *=0A * we might need a deeper page table for lager gfn now = */=0A if ( is_hvm_domain(d) )=0A {=0A- if ( update_paging_mo= de(d, gfn) )=0A+ int rc =3D update_paging_mode(d, gfn);=0A+=0A+ = if ( rc )=0A {=0A spin_unlock(&hd->arch.mapping_lock= );=0A AMD_IOMMU_DEBUG("Update page mode failed gfn =3D %lx\n", = gfn);=0A- domain_crash(d);=0A- return -EFAULT;=0A+ = if ( rc !=3D -EADDRNOTAVAIL )=0A+ domain_crash(d);= =0A+ return rc;=0A }=0A }=0A =0A--- a/xen/drivers/pa= ssthrough/vtd/iommu.h=0A+++ b/xen/drivers/passthrough/vtd/iommu.h=0A@@ = -482,7 +482,6 @@ struct qinval_entry {=0A #define VTD_PAGE_TABLE_LEVEL_3 = 3=0A #define VTD_PAGE_TABLE_LEVEL_4 4=0A =0A-#define DEFAULT_DOMAIN_ADDRES= S_WIDTH 48=0A #define MAX_IOMMU_REGS 0xc0=0A =0A extern struct list_head = acpi_drhd_units;=0A--- a/xen/drivers/passthrough/x86/iommu.c=0A+++ = b/xen/drivers/passthrough/x86/iommu.c=0A@@ -59,10 +59,17 @@ int arch_iommu_= populate_page_table(struc=0A if ( has_hvm_container_domain(d) = ||=0A (page->u.inuse.type_info & PGT_type_mask) =3D=3D = PGT_writable_page )=0A {=0A- BUG_ON(SHARED_M2P(mfn_to_gm= fn(d, page_to_mfn(page))));=0A- rc =3D hd->platform_ops->map_pag= e(=0A- d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(pag= e),=0A- IOMMUF_readable|IOMMUF_writable);=0A+ = unsigned long mfn =3D page_to_mfn(page);=0A+ unsigned long gfn = =3D mfn_to_gmfn(d, mfn);=0A+=0A+ if ( gfn !=3D INVALID_MFN = )=0A+ {=0A+ ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRES= S_WIDTH));=0A+ BUG_ON(SHARED_M2P(gfn));=0A+ = rc =3D hd->platform_ops->map_page(d, gfn, mfn,=0A+ = IOMMUF_readable |=0A+ = IOMMUF_writable);=0A+ }=0A if ( rc = )=0A {=0A page_list_add(page, &d->page_list);= =0A--- a/xen/include/asm-x86/hvm/iommu.h=0A+++ b/xen/include/asm-x86/hvm/io= mmu.h=0A@@ -46,6 +46,8 @@ struct g2m_ioport {=0A unsigned int np;=0A = };=0A =0A+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48=0A+=0A struct arch_hvm_io= mmu=0A {=0A u64 pgd_maddr; /* io page directory = machine address */=0A--- a/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h=0A+= ++ b/xen/include/asm-x86/hvm/svm/amd-iommu-defs.h=0A@@ -464,8 +464,6 @@=0A = #define IOMMU_CONTROL_DISABLED 0=0A #define IOMMU_CONTROL_ENABLED = 1=0A =0A-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48=0A-=0A /* interrupt = remapping table */=0A #define INT_REMAP_ENTRY_REMAPEN_MASK 0x00000001=0A= #define INT_REMAP_ENTRY_REMAPEN_SHIFT 0=0A --=__PartCCF8DFDB.4__= 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 --=__PartCCF8DFDB.4__=--