From: "Jan Beulich" Subject: [PATCH v2 2/5] IOMMU: make page table deallocation preemptible Date: Fri, 13 Dec 2013 14:00:21 +0000 Message-ID: <52AB2105020000780010D064@nat28.tlf.novell.com> References: <52A744B7020000780010BEF1@nat28.tlf.novell.com> <52A7456A020000780010BF23@nat28.tlf.novell.com> <52A8B683.4050705@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part665592E5.4__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VrTI0-0003Uy-Do for xen-devel@lists.xenproject.org; Fri, 13 Dec 2013 14:00:24 +0000 In-Reply-To: <52A8B683.4050705@citrix.com> 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: George Dunlap , Andrew Cooper , Keir Fraser , xiantao.zhang@intel.com, suravee.suthikulpanit@amd.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. --=__Part665592E5.4__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This too can take an arbitrary amount of time. In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths. Signed-off-by: Jan Beulich --- v2: abstract out tasklet logic --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -405,11 +405,21 @@ static int amd_iommu_assign_device(struc return reassign_device(dom0, d, devfn, pdev); } =20 -static void deallocate_next_page_table(struct page_info* pg, int level) +static void deallocate_next_page_table(struct page_info *pg, int level) +{ + PFN_ORDER(pg) =3D level; + spin_lock(&iommu_pt_cleanup_lock); + page_list_add_tail(pg, &iommu_pt_cleanup_list); + spin_unlock(&iommu_pt_cleanup_lock); +} + +static void deallocate_page_table(struct page_info *pg) { void *table_vaddr, *pde; u64 next_table_maddr; - int index, next_level; + unsigned int index, level =3D PFN_ORDER(pg), next_level; + + PFN_ORDER(pg) =3D 0; =20 if ( level <=3D 1 ) { @@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops =3D { .teardown =3D amd_iommu_domain_destroy, .map_page =3D amd_iommu_map_page, .unmap_page =3D amd_iommu_unmap_page, + .free_page_table =3D deallocate_page_table, .reassign_device =3D reassign_device, .get_device_group_id =3D amd_iommu_group_id, .update_ire_from_apic =3D amd_iommu_ioapic_update_ire, --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -58,6 +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in =20 DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb); =20 +DEFINE_SPINLOCK(iommu_pt_cleanup_lock); +PAGE_LIST_HEAD(iommu_pt_cleanup_list); +static struct tasklet iommu_pt_cleanup_tasklet; + static struct keyhandler iommu_p2m_table =3D { .diagnostic =3D 0, .u.fn =3D iommu_dump_p2m_table, @@ -235,6 +239,15 @@ int iommu_remove_device(struct pci_dev * return hd->platform_ops->remove_device(pdev->devfn, pdev); } =20 +static void iommu_teardown(struct domain *d) +{ + const struct hvm_iommu *hd =3D domain_hvm_iommu(d); + + d->need_iommu =3D 0; + hd->platform_ops->teardown(d); + tasklet_schedule(&iommu_pt_cleanup_tasklet); +} + /* * If the device isn't owned by dom0, it means it already * has been assigned to other domain, or it doesn't exist. @@ -309,10 +322,7 @@ static int assign_device(struct domain * =20 done: if ( !has_arch_pdevs(d) && need_iommu(d) ) - { - d->need_iommu =3D 0; - hd->platform_ops->teardown(d); - } + iommu_teardown(d); spin_unlock(&pcidevs_lock); =20 return rc; @@ -377,10 +387,7 @@ static int iommu_populate_page_table(str if ( !rc ) iommu_iotlb_flush_all(d); else if ( rc !=3D -ERESTART ) - { - d->need_iommu =3D 0; - hd->platform_ops->teardown(d); - } + iommu_teardown(d); =20 return rc; } @@ -397,10 +404,7 @@ void iommu_domain_destroy(struct domain=20 return; =20 if ( need_iommu(d) ) - { - d->need_iommu =3D 0; - hd->platform_ops->teardown(d); - } + iommu_teardown(d); =20 list_for_each_safe ( ioport_list, tmp, &hd->g2m_ioport_list ) { @@ -438,6 +442,23 @@ int iommu_unmap_page(struct domain *d, u return hd->platform_ops->unmap_page(d, gfn); } =20 +static void iommu_free_pagetables(unsigned long unused) +{ + do { + struct page_info *pg; + + spin_lock(&iommu_pt_cleanup_lock); + pg =3D page_list_remove_head(&iommu_pt_cleanup_list); + spin_unlock(&iommu_pt_cleanup_lock); + if ( !pg ) + return; + iommu_get_ops()->free_page_table(pg); + } while ( !softirq_pending(smp_processor_id()) ); + + tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet, + cpumask_cycle(smp_processor_id(), &cpu_online_= map)); +} + void iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int = page_count) { struct hvm_iommu *hd =3D domain_hvm_iommu(d); @@ -500,10 +521,7 @@ int deassign_device(struct domain *d, u1 pdev->fault.count =3D 0; =20 if ( !has_arch_pdevs(d) && need_iommu(d) ) - { - d->need_iommu =3D 0; - hd->platform_ops->teardown(d); - } + iommu_teardown(d); =20 return ret; } @@ -542,6 +560,7 @@ int __init iommu_setup(void) iommu_passthrough ? "Passthrough" : iommu_dom0_strict ? "Strict" : "Relaxed"); printk("Interrupt remapping %sabled\n", iommu_intremap ? "en" : = "dis"); + tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, = 0); } =20 return rc; --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom =20 static void iommu_free_pagetable(u64 pt_maddr, int level) { - int i; - struct dma_pte *pt_vaddr, *pte; - int next_level =3D level - 1; + struct page_info *pg =3D maddr_to_page(pt_maddr); =20 if ( pt_maddr =3D=3D 0 ) return; =20 + PFN_ORDER(pg) =3D level; + spin_lock(&iommu_pt_cleanup_lock); + page_list_add_tail(pg, &iommu_pt_cleanup_list); + spin_unlock(&iommu_pt_cleanup_lock); +} + +static void iommu_free_page_table(struct page_info *pg) +{ + unsigned int i, next_level =3D PFN_ORDER(pg) - 1; + u64 pt_maddr =3D page_to_maddr(pg); + struct dma_pte *pt_vaddr, *pte; + + PFN_ORDER(pg) =3D 0; pt_vaddr =3D (struct dma_pte *)map_vtd_domain_page(pt_maddr); =20 for ( i =3D 0; i < PTE_NUM; i++ ) @@ -2430,6 +2441,7 @@ const struct iommu_ops intel_iommu_ops =3D .teardown =3D iommu_domain_teardown, .map_page =3D intel_iommu_map_page, .unmap_page =3D intel_iommu_unmap_page, + .free_page_table =3D iommu_free_page_table, .reassign_device =3D reassign_device_ownership, .get_device_group_id =3D intel_iommu_group_id, .update_ire_from_apic =3D io_apic_write_remap_rte, --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags) =20 struct msi_desc; struct msi_msg; +struct page_info; =20 struct iommu_ops { int (*init)(struct domain *d); @@ -100,6 +101,7 @@ struct iommu_ops { int (*map_page)(struct domain *d, unsigned long gfn, unsigned long = mfn, unsigned int flags); int (*unmap_page)(struct domain *d, unsigned long gfn); + void (*free_page_table)(struct page_info *); int (*reassign_device)(struct domain *s, struct domain *t, u8 devfn, struct pci_dev *); int (*get_device_group_id)(u16 seg, u8 bus, u8 devfn); @@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void); */ DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb); =20 +extern struct spinlock iommu_pt_cleanup_lock; +extern struct page_list_head iommu_pt_cleanup_list; + #endif /* _IOMMU_H_ */ --=__Part665592E5.4__= Content-Type: text/plain; name="IOMMU-deallocate-pt-continuation.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="IOMMU-deallocate-pt-continuation.patch" IOMMU: make page table deallocation preemptible=0A=0AThis too can take an = arbitrary amount of time.=0A=0AIn fact, the bulk of the work is being = moved to a tasklet, as handling=0Athe necessary preemption logic in line = seems close to impossible given=0Athat the teardown may also be invoked on = error paths.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2= : abstract out tasklet logic=0A=0A--- a/xen/drivers/passthrough/amd/pci_amd= _iommu.c=0A+++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c=0A@@ -405,11 = +405,21 @@ static int amd_iommu_assign_device(struc=0A return = reassign_device(dom0, d, devfn, pdev);=0A }=0A =0A-static void deallocate_n= ext_page_table(struct page_info* pg, int level)=0A+static void deallocate_n= ext_page_table(struct page_info *pg, int level)=0A+{=0A+ PFN_ORDER(pg) = =3D level;=0A+ spin_lock(&iommu_pt_cleanup_lock);=0A+ page_list_add_t= ail(pg, &iommu_pt_cleanup_list);=0A+ spin_unlock(&iommu_pt_cleanup_lock)= ;=0A+}=0A+=0A+static void deallocate_page_table(struct page_info *pg)=0A = {=0A void *table_vaddr, *pde;=0A u64 next_table_maddr;=0A- int = index, next_level;=0A+ unsigned int index, level =3D PFN_ORDER(pg), = next_level;=0A+=0A+ PFN_ORDER(pg) =3D 0;=0A =0A if ( level <=3D 1 = )=0A {=0A@@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops =3D = {=0A .teardown =3D amd_iommu_domain_destroy,=0A .map_page =3D = amd_iommu_map_page,=0A .unmap_page =3D amd_iommu_unmap_page,=0A+ = .free_page_table =3D deallocate_page_table,=0A .reassign_device =3D = reassign_device,=0A .get_device_group_id =3D amd_iommu_group_id,=0A = .update_ire_from_apic =3D amd_iommu_ioapic_update_ire,=0A--- a/xen/drivers= /passthrough/iommu.c=0A+++ b/xen/drivers/passthrough/iommu.c=0A@@ -58,6 = +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in=0A =0A DEFINE_PER_CPU(bo= ol_t, iommu_dont_flush_iotlb);=0A =0A+DEFINE_SPINLOCK(iommu_pt_cleanup_lock= );=0A+PAGE_LIST_HEAD(iommu_pt_cleanup_list);=0A+static struct tasklet = iommu_pt_cleanup_tasklet;=0A+=0A static struct keyhandler iommu_p2m_table = =3D {=0A .diagnostic =3D 0,=0A .u.fn =3D iommu_dump_p2m_table,=0A@@= -235,6 +239,15 @@ int iommu_remove_device(struct pci_dev *=0A return = hd->platform_ops->remove_device(pdev->devfn, pdev);=0A }=0A =0A+static = void iommu_teardown(struct domain *d)=0A+{=0A+ const struct hvm_iommu = *hd =3D domain_hvm_iommu(d);=0A+=0A+ d->need_iommu =3D 0;=0A+ = hd->platform_ops->teardown(d);=0A+ tasklet_schedule(&iommu_pt_cleanup_ta= sklet);=0A+}=0A+=0A /*=0A * If the device isn't owned by dom0, it means = it already=0A * has been assigned to other domain, or it doesn't = exist.=0A@@ -309,10 +322,7 @@ static int assign_device(struct domain *=0A = =0A done:=0A if ( !has_arch_pdevs(d) && need_iommu(d) )=0A- {=0A- = d->need_iommu =3D 0;=0A- hd->platform_ops->teardown(d);=0A- = }=0A+ iommu_teardown(d);=0A spin_unlock(&pcidevs_lock);=0A =0A = return rc;=0A@@ -377,10 +387,7 @@ static int iommu_populate_page_table(= str=0A if ( !rc )=0A iommu_iotlb_flush_all(d);=0A else if = ( rc !=3D -ERESTART )=0A- {=0A- d->need_iommu =3D 0;=0A- = hd->platform_ops->teardown(d);=0A- }=0A+ iommu_teardown(d);=0A = =0A return rc;=0A }=0A@@ -397,10 +404,7 @@ void iommu_domain_destroy(st= ruct domain =0A return;=0A =0A if ( need_iommu(d) )=0A- = {=0A- d->need_iommu =3D 0;=0A- hd->platform_ops->teardown(d);= =0A- }=0A+ iommu_teardown(d);=0A =0A list_for_each_safe ( = ioport_list, tmp, &hd->g2m_ioport_list )=0A {=0A@@ -438,6 +442,23 @@ = int iommu_unmap_page(struct domain *d, u=0A return hd->platform_ops->un= map_page(d, gfn);=0A }=0A =0A+static void iommu_free_pagetables(unsigned = long unused)=0A+{=0A+ do {=0A+ struct page_info *pg;=0A+=0A+ = spin_lock(&iommu_pt_cleanup_lock);=0A+ pg =3D page_list_remove_he= ad(&iommu_pt_cleanup_list);=0A+ spin_unlock(&iommu_pt_cleanup_lock);= =0A+ if ( !pg )=0A+ return;=0A+ iommu_get_ops()->f= ree_page_table(pg);=0A+ } while ( !softirq_pending(smp_processor_id()) = );=0A+=0A+ tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,=0A+ = cpumask_cycle(smp_processor_id(), &cpu_online_map));= =0A+}=0A+=0A void iommu_iotlb_flush(struct domain *d, unsigned long gfn, = unsigned int page_count)=0A {=0A struct hvm_iommu *hd =3D domain_hvm_io= mmu(d);=0A@@ -500,10 +521,7 @@ int deassign_device(struct domain *d, u1=0A = pdev->fault.count =3D 0;=0A =0A if ( !has_arch_pdevs(d) && = need_iommu(d) )=0A- {=0A- d->need_iommu =3D 0;=0A- = hd->platform_ops->teardown(d);=0A- }=0A+ iommu_teardown(d);=0A = =0A return ret;=0A }=0A@@ -542,6 +560,7 @@ int __init iommu_setup(void)= =0A iommu_passthrough ? "Passthrough" :=0A = iommu_dom0_strict ? "Strict" : "Relaxed");=0A printk("Interrupt = remapping %sabled\n", iommu_intremap ? "en" : "dis");=0A+ tasklet_in= it(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0);=0A }=0A =0A = return rc;=0A--- a/xen/drivers/passthrough/vtd/iommu.c=0A+++ b/xen/driver= s/passthrough/vtd/iommu.c=0A@@ -668,13 +668,24 @@ static void dma_pte_clear= _one(struct dom=0A =0A static void iommu_free_pagetable(u64 pt_maddr, int = level)=0A {=0A- int i;=0A- struct dma_pte *pt_vaddr, *pte;=0A- = int next_level =3D level - 1;=0A+ struct page_info *pg =3D maddr_to_page= (pt_maddr);=0A =0A if ( pt_maddr =3D=3D 0 )=0A return;=0A =0A+ = PFN_ORDER(pg) =3D level;=0A+ spin_lock(&iommu_pt_cleanup_lock);=0A+ = page_list_add_tail(pg, &iommu_pt_cleanup_list);=0A+ spin_unlock(&iommu= _pt_cleanup_lock);=0A+}=0A+=0A+static void iommu_free_page_table(struct = page_info *pg)=0A+{=0A+ unsigned int i, next_level =3D PFN_ORDER(pg) - = 1;=0A+ u64 pt_maddr =3D page_to_maddr(pg);=0A+ struct dma_pte = *pt_vaddr, *pte;=0A+=0A+ PFN_ORDER(pg) =3D 0;=0A pt_vaddr =3D = (struct dma_pte *)map_vtd_domain_page(pt_maddr);=0A =0A for ( i =3D 0; = i < PTE_NUM; i++ )=0A@@ -2430,6 +2441,7 @@ const struct iommu_ops = intel_iommu_ops =3D=0A .teardown =3D iommu_domain_teardown,=0A = .map_page =3D intel_iommu_map_page,=0A .unmap_page =3D intel_iommu_unma= p_page,=0A+ .free_page_table =3D iommu_free_page_table,=0A = .reassign_device =3D reassign_device_ownership,=0A .get_device_group_id= =3D intel_iommu_group_id,=0A .update_ire_from_apic =3D io_apic_write_r= emap_rte,=0A--- a/xen/include/xen/iommu.h=0A+++ b/xen/include/xen/iommu.h= =0A@@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags)=0A =0A = struct msi_desc;=0A struct msi_msg;=0A+struct page_info;=0A =0A struct = iommu_ops {=0A int (*init)(struct domain *d);=0A@@ -100,6 +101,7 @@ = struct iommu_ops {=0A int (*map_page)(struct domain *d, unsigned long = gfn, unsigned long mfn,=0A unsigned int flags);=0A = int (*unmap_page)(struct domain *d, unsigned long gfn);=0A+ void = (*free_page_table)(struct page_info *);=0A int (*reassign_device)(struc= t domain *s, struct domain *t,=0A u8 devfn, = struct pci_dev *);=0A int (*get_device_group_id)(u16 seg, u8 bus, u8 = devfn);=0A@@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void);=0A = */=0A DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);=0A =0A+extern = struct spinlock iommu_pt_cleanup_lock;=0A+extern struct page_list_head = iommu_pt_cleanup_list;=0A+=0A #endif /* _IOMMU_H_ */=0A --=__Part665592E5.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 --=__Part665592E5.4__=--