From: "Jan Beulich" Subject: [PATCH v2] x86/PV: fix unintended dependency of m2p-strict mode on migration-v2 Date: Tue, 02 Feb 2016 08:14:07 -0700 Message-ID: <56B0D5CF02000078000CD941@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartE6D10DAF.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 1aQcej-0003SK-Te for xen-devel@lists.xenproject.org; Tue, 02 Feb 2016 15:14:14 +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: Andrew Cooper , Keir Fraser 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. --=__PartE6D10DAF.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This went unnoticed until a backport of this to an older Xen got used, causing migration of guests enabling this VM assist to fail, because page table pinning there precedes vCPU context loading, and hence L4 tables get initialized for the wrong mode. Fix this by post-processing L4 tables when setting the intended VM assist flags for the guest. Note that this leaves in place a dependency on vCPU 0 getting its guest context restored first, but afaict the logic here is not the only thing depending on that. Signed-off-by: Jan Beulich --- v2: Avoid entering the loop when strict mode was already before, which in turn requires deferring the assignment to d->vm_assist until after the loop. --- a/xen/arch/x86/domain.c +++ b/xen/arch/x86/domain.c @@ -1075,7 +1075,48 @@ int arch_set_info_guest( goto out; =20 if ( v->vcpu_id =3D=3D 0 ) + { + /* + * In the restore case we need to deal with L4 pages which got + * initialized with m2p_strict still clear (and which hence lack = the + * correct initial RO_MPT_VIRT_{START,END} L4 entry). + */ + if ( d !=3D current->domain && !VM_ASSIST(d, m2p_strict) && + is_pv_domain(d) && !is_pv_32bit_domain(d) && + test_bit(VMASST_TYPE_m2p_strict, &c.nat->vm_assist) && + atomic_read(&d->arch.pv_domain.nr_l4_pages) ) + { + bool_t done =3D 0; + + spin_lock_recursive(&d->page_alloc_lock); + + for ( i =3D 0; ; ) + { + struct page_info *page =3D page_list_remove_head(&d->page_= list); + + if ( page_lock(page) ) + { + if ( (page->u.inuse.type_info & PGT_type_mask) =3D=3D + PGT_l4_page_table ) + done =3D !fill_ro_mpt(page_to_mfn(page)); + + page_unlock(page); + } + + page_list_add_tail(page, &d->page_list); + + if ( done || (!(++i & 0xff) && hypercall_preempt_check()) = ) + break; + } + + spin_unlock_recursive(&d->page_alloc_lock); + + if ( !done ) + return -ERESTART; + } + d->vm_assist =3D c(vm_assist); + } =20 rc =3D put_old_guest_table(current); if ( rc ) --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -1463,13 +1463,20 @@ void init_guest_l4_table(l4_pgentry_t l4 l4tab[l4_table_offset(RO_MPT_VIRT_START)] =3D l4e_empty(); } =20 -void fill_ro_mpt(unsigned long mfn) +bool_t fill_ro_mpt(unsigned long mfn) { l4_pgentry_t *l4tab =3D map_domain_page(_mfn(mfn)); + bool_t ret =3D 0; =20 - l4tab[l4_table_offset(RO_MPT_VIRT_START)] =3D - idle_pg_table[l4_table_offset(RO_MPT_VIRT_START)]; + if ( !l4e_get_intpte(l4tab[l4_table_offset(RO_MPT_VIRT_START)]) ) + { + l4tab[l4_table_offset(RO_MPT_VIRT_START)] =3D + idle_pg_table[l4_table_offset(RO_MPT_VIRT_START)]; + ret =3D 1; + } unmap_domain_page(l4tab); + + return ret; } =20 void zap_ro_mpt(unsigned long mfn) @@ -1527,10 +1534,15 @@ static int alloc_l4_table(struct page_in adjust_guest_l4e(pl4e[i], d); } =20 - init_guest_l4_table(pl4e, d, !VM_ASSIST(d, m2p_strict)); + if ( rc >=3D 0 ) + { + init_guest_l4_table(pl4e, d, !VM_ASSIST(d, m2p_strict)); + atomic_inc(&d->arch.pv_domain.nr_l4_pages); + rc =3D 0; + } unmap_domain_page(pl4e); =20 - return rc > 0 ? 0 : rc; + return rc; } =20 static void free_l1_table(struct page_info *page) @@ -1648,7 +1660,13 @@ static int free_l4_table(struct page_inf =20 unmap_domain_page(pl4e); =20 - return rc > 0 ? 0 : rc; + if ( rc >=3D 0 ) + { + atomic_dec(&d->arch.pv_domain.nr_l4_pages); + rc =3D 0; + } + + return rc; } =20 int page_lock(struct page_info *page) --- a/xen/include/asm-x86/domain.h +++ b/xen/include/asm-x86/domain.h @@ -248,6 +248,8 @@ struct pv_domain { l1_pgentry_t **gdt_ldt_l1tab; =20 + atomic_t nr_l4_pages; + /* map_domain_page() mapping cache. */ struct mapcache_domain mapcache; }; --- a/xen/include/asm-x86/mm.h +++ b/xen/include/asm-x86/mm.h @@ -322,7 +322,7 @@ int free_page_type(struct page_info *pag =20 void init_guest_l4_table(l4_pgentry_t[], const struct domain *, bool_t zap_ro_mpt); -void fill_ro_mpt(unsigned long mfn); +bool_t fill_ro_mpt(unsigned long mfn); void zap_ro_mpt(unsigned long mfn); =20 int is_iomem_page(unsigned long mfn); --=__PartE6D10DAF.1__= Content-Type: text/plain; name="x86-m2p-strict-restore.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-m2p-strict-restore.patch" x86/PV: fix unintended dependency of m2p-strict mode on migration-v2=0A=0AT= his went unnoticed until a backport of this to an older Xen got used,=0Acau= sing migration of guests enabling this VM assist to fail, because=0Apage = table pinning there precedes vCPU context loading, and hence L4=0Atables = get initialized for the wrong mode. Fix this by post-processing=0AL4 = tables when setting the intended VM assist flags for the guest.=0A=0ANote = that this leaves in place a dependency on vCPU 0 getting its guest=0Acontex= t restored first, but afaict the logic here is not the only thing=0Adependi= ng on that.=0A=0ASigned-off-by: Jan Beulich =0A---=0Av2:= Avoid entering the loop when strict mode was already before, which=0A = in turn requires deferring the assignment to d->vm_assist until=0A = after the loop.=0A=0A--- a/xen/arch/x86/domain.c=0A+++ b/xen/arch/x86/domai= n.c=0A@@ -1075,7 +1075,48 @@ int arch_set_info_guest(=0A goto = out;=0A =0A if ( v->vcpu_id =3D=3D 0 )=0A+ {=0A+ /*=0A+ = * In the restore case we need to deal with L4 pages which got=0A+ = * initialized with m2p_strict still clear (and which hence lack the=0A+ = * correct initial RO_MPT_VIRT_{START,END} L4 entry).=0A+ = */=0A+ if ( d !=3D current->domain && !VM_ASSIST(d, m2p_strict) = &&=0A+ is_pv_domain(d) && !is_pv_32bit_domain(d) &&=0A+ = test_bit(VMASST_TYPE_m2p_strict, &c.nat->vm_assist) &&=0A+ = atomic_read(&d->arch.pv_domain.nr_l4_pages) )=0A+ {=0A+ = bool_t done =3D 0;=0A+=0A+ spin_lock_recursive(&d->page_alloc_lo= ck);=0A+=0A+ for ( i =3D 0; ; )=0A+ {=0A+ = struct page_info *page =3D page_list_remove_head(&d->page_list);=0A+=0A= + if ( page_lock(page) )=0A+ {=0A+ = if ( (page->u.inuse.type_info & PGT_type_mask) =3D=3D=0A+ = PGT_l4_page_table )=0A+ done =3D = !fill_ro_mpt(page_to_mfn(page));=0A+=0A+ page_unlock(pag= e);=0A+ }=0A+=0A+ page_list_add_tail(page, = &d->page_list);=0A+=0A+ if ( done || (!(++i & 0xff) && = hypercall_preempt_check()) )=0A+ break;=0A+ = }=0A+=0A+ spin_unlock_recursive(&d->page_alloc_lock);=0A+=0A+ = if ( !done )=0A+ return -ERESTART;=0A+ = }=0A+=0A d->vm_assist =3D c(vm_assist);=0A+ }=0A =0A rc =3D = put_old_guest_table(current);=0A if ( rc )=0A--- a/xen/arch/x86/mm.c=0A= +++ b/xen/arch/x86/mm.c=0A@@ -1463,13 +1463,20 @@ void init_guest_l4_table(= l4_pgentry_t l4=0A l4tab[l4_table_offset(RO_MPT_VIRT_START)] =3D = l4e_empty();=0A }=0A =0A-void fill_ro_mpt(unsigned long mfn)=0A+bool_t = fill_ro_mpt(unsigned long mfn)=0A {=0A l4_pgentry_t *l4tab =3D = map_domain_page(_mfn(mfn));=0A+ bool_t ret =3D 0;=0A =0A- l4tab[l4_ta= ble_offset(RO_MPT_VIRT_START)] =3D=0A- idle_pg_table[l4_table_offset= (RO_MPT_VIRT_START)];=0A+ if ( !l4e_get_intpte(l4tab[l4_table_offset(RO_= MPT_VIRT_START)]) )=0A+ {=0A+ l4tab[l4_table_offset(RO_MPT_VIRT_S= TART)] =3D=0A+ idle_pg_table[l4_table_offset(RO_MPT_VIRT_START)]= ;=0A+ ret =3D 1;=0A+ }=0A unmap_domain_page(l4tab);=0A+=0A+ = return ret;=0A }=0A =0A void zap_ro_mpt(unsigned long mfn)=0A@@ -1527,10 = +1534,15 @@ static int alloc_l4_table(struct page_in=0A adjust_gues= t_l4e(pl4e[i], d);=0A }=0A =0A- init_guest_l4_table(pl4e, d, = !VM_ASSIST(d, m2p_strict));=0A+ if ( rc >=3D 0 )=0A+ {=0A+ = init_guest_l4_table(pl4e, d, !VM_ASSIST(d, m2p_strict));=0A+ = atomic_inc(&d->arch.pv_domain.nr_l4_pages);=0A+ rc =3D 0;=0A+ = }=0A unmap_domain_page(pl4e);=0A =0A- return rc > 0 ? 0 : rc;=0A+ = return rc;=0A }=0A =0A static void free_l1_table(struct page_info = *page)=0A@@ -1648,7 +1660,13 @@ static int free_l4_table(struct page_inf=0A= =0A unmap_domain_page(pl4e);=0A =0A- return rc > 0 ? 0 : rc;=0A+ = if ( rc >=3D 0 )=0A+ {=0A+ atomic_dec(&d->arch.pv_domain.nr_l4_p= ages);=0A+ rc =3D 0;=0A+ }=0A+=0A+ return rc;=0A }=0A =0A int = page_lock(struct page_info *page)=0A--- a/xen/include/asm-x86/domain.h=0A++= + b/xen/include/asm-x86/domain.h=0A@@ -248,6 +248,8 @@ struct pv_domain=0A = {=0A l1_pgentry_t **gdt_ldt_l1tab;=0A =0A+ atomic_t nr_l4_pages;=0A+= =0A /* map_domain_page() mapping cache. */=0A struct mapcache_domai= n mapcache;=0A };=0A--- a/xen/include/asm-x86/mm.h=0A+++ b/xen/include/asm-= x86/mm.h=0A@@ -322,7 +322,7 @@ int free_page_type(struct page_info *pag=0A = =0A void init_guest_l4_table(l4_pgentry_t[], const struct domain *,=0A = bool_t zap_ro_mpt);=0A-void fill_ro_mpt(unsigned long = mfn);=0A+bool_t fill_ro_mpt(unsigned long mfn);=0A void zap_ro_mpt(unsigned= long mfn);=0A =0A int is_iomem_page(unsigned long mfn);=0A --=__PartE6D10DAF.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 --=__PartE6D10DAF.1__=--