From: "Jan Beulich" Subject: [PATCH v2 1/5] IOMMU: make page table population preemptible Date: Fri, 13 Dec 2013 13:59:39 +0000 Message-ID: <52AB20DB020000780010D060@nat28.tlf.novell.com> References: <52A744B7020000780010BEF1@nat28.tlf.novell.com> <52A74539020000780010BF1F@nat28.tlf.novell.com> <52A8B1A5.9040302@citrix.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part586BACDB.3__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1VrTHL-00038N-EY for xen-devel@lists.xenproject.org; Fri, 13 Dec 2013 13:59:43 +0000 In-Reply-To: <52A8B1A5.9040302@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, Tim Deegan 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. --=__Part586BACDB.3__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation. The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining. Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it. Signed-off-by: Jan Beulich --- v2: Extend comment on struct domain's need_iommu field. --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1924,6 +1924,12 @@ int domain_relinquish_resources(struct d } =20 d->arch.relmem =3D RELMEM_xen; + + spin_lock(&d->page_alloc_lock); + page_list_splice(&d->arch.relmem_list, &d->page_list); + INIT_PAGE_LIST_HEAD(&d->arch.relmem_list); + spin_unlock(&d->page_alloc_lock); + /* Fallthrough. Relinquish every page of memory. */ case RELMEM_xen: ret =3D relinquish_memory(d, &d->xenpage_list, ~0UL); --- a/xen/arch/x86/mm/p2m-pod.c +++ b/xen/arch/x86/mm/p2m-pod.c @@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag =20 pod_hit: lock_page_alloc(p2m); - page_list_add_tail(p, &d->arch.relmem_list); + /* Insertion must be at list head (see iommu_populate_page_table()). = */ + page_list_add(p, &d->arch.relmem_list); unlock_page_alloc(p2m); pod_unlock(p2m); return 1; --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -265,7 +266,23 @@ static int assign_device(struct domain * d->mem_event->paging.ring_page)) ) return -EXDEV; =20 - spin_lock(&pcidevs_lock); + if ( !spin_trylock(&pcidevs_lock) ) + return -ERESTART; + + if ( need_iommu(d) <=3D 0 ) + { + if ( !iommu_use_hap_pt(d) ) + { + rc =3D iommu_populate_page_table(d); + if ( rc ) + { + spin_unlock(&pcidevs_lock); + return rc; + } + } + d->need_iommu =3D 1; + } + pdev =3D pci_get_pdev_by_domain(dom0, seg, bus, devfn); if ( !pdev ) { @@ -290,15 +307,14 @@ static int assign_device(struct domain * rc); } =20 - if ( has_arch_pdevs(d) && !need_iommu(d) ) + done: + if ( !has_arch_pdevs(d) && need_iommu(d) ) { - d->need_iommu =3D 1; - if ( !iommu_use_hap_pt(d) ) - rc =3D iommu_populate_page_table(d); - goto done; + d->need_iommu =3D 0; + hd->platform_ops->teardown(d); } -done: spin_unlock(&pcidevs_lock); + return rc; } =20 @@ -306,12 +322,17 @@ static int iommu_populate_page_table(str { struct hvm_iommu *hd =3D domain_hvm_iommu(d); struct page_info *page; - int rc =3D 0; + int rc =3D 0, n =3D 0; + + d->need_iommu =3D -1; =20 this_cpu(iommu_dont_flush_iotlb) =3D 1; spin_lock(&d->page_alloc_lock); =20 - page_list_for_each ( page, &d->page_list ) + if ( unlikely(d->is_dying) ) + rc =3D -ESRCH; + + while ( !rc && (page =3D page_list_remove_head(&d->page_list)) ) { if ( is_hvm_domain(d) || (page->u.inuse.type_info & PGT_type_mask) =3D=3D PGT_writable_= page ) @@ -321,7 +342,32 @@ static int iommu_populate_page_table(str d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page), IOMMUF_readable|IOMMUF_writable); if ( rc ) + { + page_list_add(page, &d->page_list); break; + } + } + page_list_add_tail(page, &d->arch.relmem_list); + if ( !(++n & 0xff) && !page_list_empty(&d->page_list) && + hypercall_preempt_check() ) + rc =3D -ERESTART; + } + + if ( !rc ) + { + /* + * The expectation here is that generally there are many normal = pages + * on relmem_list (the ones we put there) and only few being in = an + * offline/broken state. The latter ones are always at the head = of the + * list. Hence we first move the whole list, and then move back = the + * first few entries. + */ + page_list_move(&d->page_list, &d->arch.relmem_list); + while ( (page =3D page_list_first(&d->page_list)) !=3D NULL && + (page->count_info & (PGC_state|PGC_broken)) ) + { + page_list_del(page, &d->page_list); + page_list_add_tail(page, &d->arch.relmem_list); } } =20 @@ -330,8 +376,11 @@ static int iommu_populate_page_table(str =20 if ( !rc ) iommu_iotlb_flush_all(d); - else + else if ( rc !=3D -ERESTART ) + { + d->need_iommu =3D 0; hd->platform_ops->teardown(d); + } =20 return rc; } @@ -688,7 +737,10 @@ int iommu_do_domctl( =20 ret =3D device_assigned(seg, bus, devfn) ?: assign_device(d, seg, bus, devfn); - if ( ret ) + if ( ret =3D=3D -ERESTART ) + ret =3D hypercall_create_continuation(__HYPERVISOR_domctl, + "h", u_domctl); + else if ( ret ) printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device: " "assign %04x:%02x:%02x.%u to dom%d failed (%d)\n", seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn), --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -322,8 +322,8 @@ struct domain enum guest_type guest_type; =20 #ifdef HAS_PASSTHROUGH - /* Does this guest need iommu mappings? */ - bool_t need_iommu; + /* Does this guest need iommu mappings (-1 meaning "being set up")? = */ + s8 need_iommu; #endif /* is node-affinity automatically computed? */ bool_t auto_node_affinity; --=__Part586BACDB.3__= Content-Type: text/plain; name="IOMMU-populate-pt-continuation.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="IOMMU-populate-pt-continuation.patch" IOMMU: make page table population preemptible=0A=0ASince this can take an = arbitrary amount of time, the rooting domctl as=0Awell as all involved = code must become aware of this requiring a=0Acontinuation.=0A=0AThe = subject domain's rel_mem_list is being (ab)used for this, in a way=0Asimila= r to and compatible with broken page offlining.=0A=0AFurther, operations = get slightly re-ordered in assign_device(): IOMMU=0Apage tables now get = set up _before_ the first device gets assigned, at=0Aonce closing a small = timing window in which the guest may already see=0Athe device but wouldn't = be able to access it.=0A=0ASigned-off-by: Jan Beulich = =0A---=0Av2: Extend comment on struct domain's need_iommu field.=0A=0A--- = a/xen/arch/x86/domain.c=0A+++ b/xen/arch/x86/domain.c=0A@@ -1924,6 = +1924,12 @@ int domain_relinquish_resources(struct d=0A }=0A =0A = d->arch.relmem =3D RELMEM_xen;=0A+=0A+ spin_lock(&d->page_allo= c_lock);=0A+ page_list_splice(&d->arch.relmem_list, &d->page_list);= =0A+ INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);=0A+ = spin_unlock(&d->page_alloc_lock);=0A+=0A /* Fallthrough. Relinquish= every page of memory. */=0A case RELMEM_xen:=0A ret =3D = relinquish_memory(d, &d->xenpage_list, ~0UL);=0A--- a/xen/arch/x86/mm/p2m-p= od.c=0A+++ b/xen/arch/x86/mm/p2m-pod.c=0A@@ -459,7 +459,8 @@ p2m_pod_offlin= e_or_broken_hit(struct pag=0A =0A pod_hit:=0A lock_page_alloc(p2m);=0A-= page_list_add_tail(p, &d->arch.relmem_list);=0A+ /* Insertion must = be at list head (see iommu_populate_page_table()). */=0A+ page_list_add(= p, &d->arch.relmem_list);=0A unlock_page_alloc(p2m);=0A pod_unlock(= p2m);=0A return 1;=0A--- a/xen/drivers/passthrough/iommu.c=0A+++ = b/xen/drivers/passthrough/iommu.c=0A@@ -18,6 +18,7 @@=0A #include = =0A #include =0A #include =0A+#include =0A #include =0A #include = =0A #include =0A@@ -265,7 +266,23 @@ static = int assign_device(struct domain *=0A d->mem_event->paging.ring= _page)) )=0A return -EXDEV;=0A =0A- spin_lock(&pcidevs_lock);=0A= + if ( !spin_trylock(&pcidevs_lock) )=0A+ return -ERESTART;=0A+= =0A+ if ( need_iommu(d) <=3D 0 )=0A+ {=0A+ if ( !iommu_use_hap= _pt(d) )=0A+ {=0A+ rc =3D iommu_populate_page_table(d);= =0A+ if ( rc )=0A+ {=0A+ spin_unlock(&= pcidevs_lock);=0A+ return rc;=0A+ }=0A+ = }=0A+ d->need_iommu =3D 1;=0A+ }=0A+=0A pdev =3D pci_get_pdev= _by_domain(dom0, seg, bus, devfn);=0A if ( !pdev )=0A {=0A@@ = -290,15 +307,14 @@ static int assign_device(struct domain *=0A = rc);=0A }=0A =0A- if ( has_arch_pdevs(d) && !need_iommu(d) = )=0A+ done:=0A+ if ( !has_arch_pdevs(d) && need_iommu(d) )=0A {=0A- = d->need_iommu =3D 1;=0A- if ( !iommu_use_hap_pt(d) )=0A- = rc =3D iommu_populate_page_table(d);=0A- goto done;=0A+ = d->need_iommu =3D 0;=0A+ hd->platform_ops->teardown(d);=0A = }=0A-done:=0A spin_unlock(&pcidevs_lock);=0A+=0A return rc;=0A = }=0A =0A@@ -306,12 +322,17 @@ static int iommu_populate_page_table(str=0A = {=0A struct hvm_iommu *hd =3D domain_hvm_iommu(d);=0A struct = page_info *page;=0A- int rc =3D 0;=0A+ int rc =3D 0, n =3D 0;=0A+=0A+= d->need_iommu =3D -1;=0A =0A this_cpu(iommu_dont_flush_iotlb) =3D = 1;=0A spin_lock(&d->page_alloc_lock);=0A =0A- page_list_for_each ( = page, &d->page_list )=0A+ if ( unlikely(d->is_dying) )=0A+ rc = =3D -ESRCH;=0A+=0A+ while ( !rc && (page =3D page_list_remove_head(&d->p= age_list)) )=0A {=0A if ( is_hvm_domain(d) ||=0A = (page->u.inuse.type_info & PGT_type_mask) =3D=3D PGT_writable_page )=0A@@ = -321,7 +342,32 @@ static int iommu_populate_page_table(str=0A = d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),=0A = IOMMUF_readable|IOMMUF_writable);=0A if ( rc )=0A+ = {=0A+ page_list_add(page, &d->page_list);=0A = break;=0A+ }=0A+ }=0A+ page_list_add_tail(page= , &d->arch.relmem_list);=0A+ if ( !(++n & 0xff) && !page_list_empty(= &d->page_list) &&=0A+ hypercall_preempt_check() )=0A+ = rc =3D -ERESTART;=0A+ }=0A+=0A+ if ( !rc )=0A+ {=0A+ = /*=0A+ * The expectation here is that generally there are many = normal pages=0A+ * on relmem_list (the ones we put there) and only = few being in an=0A+ * offline/broken state. The latter ones are = always at the head of the=0A+ * list. Hence we first move the = whole list, and then move back the=0A+ * first few entries.=0A+ = */=0A+ page_list_move(&d->page_list, &d->arch.relmem_list);=0A+= while ( (page =3D page_list_first(&d->page_list)) !=3D NULL &&=0A+ = (page->count_info & (PGC_state|PGC_broken)) )=0A+ = {=0A+ page_list_del(page, &d->page_list);=0A+ = page_list_add_tail(page, &d->arch.relmem_list);=0A }=0A }=0A = =0A@@ -330,8 +376,11 @@ static int iommu_populate_page_table(str=0A =0A = if ( !rc )=0A iommu_iotlb_flush_all(d);=0A- else=0A+ else = if ( rc !=3D -ERESTART )=0A+ {=0A+ d->need_iommu =3D 0;=0A = hd->platform_ops->teardown(d);=0A+ }=0A =0A return rc;=0A }=0A@@ = -688,7 +737,10 @@ int iommu_do_domctl(=0A =0A ret =3D device_assign= ed(seg, bus, devfn) ?:=0A assign_device(d, seg, bus, = devfn);=0A- if ( ret )=0A+ if ( ret =3D=3D -ERESTART )=0A+ = ret =3D hypercall_create_continuation(__HYPERVISOR_domctl,=0A+ = "h", u_domctl);=0A+ = else if ( ret )=0A printk(XENLOG_G_ERR "XEN_DOMCTL_assign_devic= e: "=0A "assign %04x:%02x:%02x.%u to dom%d failed = (%d)\n",=0A seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),= =0A--- a/xen/include/xen/sched.h=0A+++ b/xen/include/xen/sched.h=0A@@ = -322,8 +322,8 @@ struct domain=0A enum guest_type guest_type;=0A =0A = #ifdef HAS_PASSTHROUGH=0A- /* Does this guest need iommu mappings? = */=0A- bool_t need_iommu;=0A+ /* Does this guest need = iommu mappings (-1 meaning "being set up")? */=0A+ s8 = need_iommu;=0A #endif=0A /* is node-affinity automatically computed? = */=0A bool_t auto_node_affinity;=0A --=__Part586BACDB.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 --=__Part586BACDB.3__=--