From: "Jan Beulich" Subject: [PATCH 2/5] IOMMU: make page table deallocation preemptible Date: Tue, 10 Dec 2013 15:46:34 +0000 Message-ID: <52A7456A020000780010BF23@nat28.tlf.novell.com> References: <52A744B7020000780010BEF1@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartE0D3184A.3__=" Return-path: Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VqPWC-0004nX-4y for xen-devel@lists.xenproject.org; Tue, 10 Dec 2013 15:46:40 +0000 In-Reply-To: <52A744B7020000780010BEF1@nat28.tlf.novell.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 , 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. --=__PartE0D3184A.3__= 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 --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -195,6 +195,8 @@ static int __init amd_iommu_setup_dom0_d return 0; } =20 +static void deallocate_page_tables(unsigned long unused); + int __init amd_iov_detect(void) { INIT_LIST_HEAD(&amd_iommu_head); @@ -215,6 +217,7 @@ int __init amd_iov_detect(void) return -ENODEV; } =20 + tasklet_init(&iommu_pt_cleanup_tasklet, deallocate_page_tables, 0); init_done =3D 1; =20 if ( !amd_iommu_perdev_intremap ) @@ -405,11 +408,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) +{ + spin_lock(&iommu_pt_cleanup_lock); + PFN_ORDER(pg) =3D level; + 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 ) { @@ -439,6 +452,23 @@ static void deallocate_next_page_table(s free_amd_iommu_pgtable(pg); } =20 +static void deallocate_page_tables(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; + deallocate_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)); +} + static void deallocate_iommu_page_tables(struct domain *d) { struct hvm_iommu *hd =3D domain_hvm_iommu(d); @@ -451,6 +481,7 @@ static void deallocate_iommu_page_tables { deallocate_next_page_table(hd->root_table, hd->paging_mode); hd->root_table =3D NULL; + tasklet_schedule(&iommu_pt_cleanup_tasklet); } spin_unlock(&hd->mapping_lock); } --- 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); +struct tasklet iommu_pt_cleanup_tasklet; + static struct keyhandler iommu_p2m_table =3D { .diagnostic =3D 0, .u.fn =3D iommu_dump_p2m_table, --- 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 + spin_lock(&iommu_pt_cleanup_lock); + PFN_ORDER(pg) =3D level; + 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++ ) @@ -694,6 +705,23 @@ static void iommu_free_pagetable(u64 pt_ free_pgtable_maddr(pt_maddr); } =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_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)); +} + static int iommu_set_root_entry(struct iommu *iommu) { u32 sts; @@ -1704,6 +1732,8 @@ void iommu_domain_teardown(struct domain iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw)); hd->pgd_maddr =3D 0; spin_unlock(&hd->mapping_lock); + + tasklet_schedule(&iommu_pt_cleanup_tasklet); } =20 static int intel_iommu_map_page( @@ -2204,6 +2234,7 @@ int __init intel_vtd_setup(void) if ( ret ) goto error; =20 + tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0); register_keyhandler('V', &dump_iommu_info_keyhandler); =20 return 0; --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -151,4 +151,8 @@ 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; +extern struct tasklet iommu_pt_cleanup_tasklet; + #endif /* _IOMMU_H_ */ --=__PartE0D3184A.3__= 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=0A--- = a/xen/drivers/passthrough/amd/pci_amd_iommu.c=0A+++ b/xen/drivers/passthrou= gh/amd/pci_amd_iommu.c=0A@@ -195,6 +195,8 @@ static int __init amd_iommu_se= tup_dom0_d=0A return 0;=0A }=0A =0A+static void deallocate_page_tables(= unsigned long unused);=0A+=0A int __init amd_iov_detect(void)=0A {=0A = INIT_LIST_HEAD(&amd_iommu_head);=0A@@ -215,6 +217,7 @@ int __init = amd_iov_detect(void)=0A return -ENODEV;=0A }=0A =0A+ = tasklet_init(&iommu_pt_cleanup_tasklet, deallocate_page_tables, 0);=0A = init_done =3D 1;=0A =0A if ( !amd_iommu_perdev_intremap )=0A@@ -405,11 = +408,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+ spin_lock(&iomm= u_pt_cleanup_lock);=0A+ PFN_ORDER(pg) =3D level;=0A+ page_list_add_ta= il(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@@ -439,6 +452,23 @@ static void deallocate_next_page_table(s= =0A free_amd_iommu_pgtable(pg);=0A }=0A =0A+static void deallocate_page= _tables(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_head(&iommu_pt_cleanup_list);=0A+ spin_unlock(&iomm= u_pt_cleanup_lock);=0A+ if ( !pg )=0A+ return;=0A+ = deallocate_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_m= ap));=0A+}=0A+=0A static void deallocate_iommu_page_tables(struct domain = *d)=0A {=0A struct hvm_iommu *hd =3D domain_hvm_iommu(d);=0A@@ -451,6 = +481,7 @@ static void deallocate_iommu_page_tables=0A {=0A = deallocate_next_page_table(hd->root_table, hd->paging_mode);=0A = hd->root_table =3D NULL;=0A+ tasklet_schedule(&iommu_pt_cleanup_task= let);=0A }=0A spin_unlock(&hd->mapping_lock);=0A }=0A--- a/xen/driv= ers/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+struct tasklet iommu_pt_cle= anup_tasklet;=0A+=0A static struct keyhandler iommu_p2m_table =3D {=0A = .diagnostic =3D 0,=0A .u.fn =3D iommu_dump_p2m_table,=0A--- a/xen/drive= rs/passthrough/vtd/iommu.c=0A+++ b/xen/drivers/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+ spin_lock(&iommu_pt_clean= up_lock);=0A+ PFN_ORDER(pg) =3D level;=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_p= age(pt_maddr);=0A =0A for ( i =3D 0; i < PTE_NUM; i++ )=0A@@ -694,6 = +705,23 @@ static void iommu_free_pagetable(u64 pt_=0A free_pgtable_mad= dr(pt_maddr);=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_head(= &iommu_pt_cleanup_list);=0A+ spin_unlock(&iommu_pt_cleanup_lock);=0A= + if ( !pg )=0A+ return;=0A+ iommu_free_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 = static int iommu_set_root_entry(struct iommu *iommu)=0A {=0A u32 = sts;=0A@@ -1704,6 +1732,8 @@ void iommu_domain_teardown(struct domain=0A = iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw));=0A = hd->pgd_maddr =3D 0;=0A spin_unlock(&hd->mapping_lock);=0A+=0A+ = tasklet_schedule(&iommu_pt_cleanup_tasklet);=0A }=0A =0A static int = intel_iommu_map_page(=0A@@ -2204,6 +2234,7 @@ int __init intel_vtd_setup(vo= id)=0A if ( ret )=0A goto error;=0A =0A+ tasklet_init(&iommu= _pt_cleanup_tasklet, iommu_free_pagetables, 0);=0A register_keyhandler(= 'V', &dump_iommu_info_keyhandler);=0A =0A return 0;=0A--- a/xen/include= /xen/iommu.h=0A+++ b/xen/include/xen/iommu.h=0A@@ -151,4 +151,8 @@ 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_loc= k;=0A+extern struct page_list_head iommu_pt_cleanup_list;=0A+extern struct = tasklet iommu_pt_cleanup_tasklet;=0A+=0A #endif /* _IOMMU_H_ */=0A --=__PartE0D3184A.3__= 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 --=__PartE0D3184A.3__=--