From: "Jan Beulich" Subject: [PATCH v2 5/6] x86/NPT: don't walk entire page tables when globally changing types Date: Tue, 22 Apr 2014 13:31:38 +0100 Message-ID: <53567D4A020000780000ABDA@nat28.tlf.novell.com> References: <53567B1C020000780000AB8C@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part36043E3A.2__=" Return-path: Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WcZrT-0007aB-Eu for xen-devel@lists.xenproject.org; Tue, 22 Apr 2014 12:31:43 +0000 In-Reply-To: <53567B1C020000780000AB8C@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: Kevin Tian , Keir Fraser , suravee.suthikulpanit@amd.com, Tim Deegan , Eddie Dong , Jun Nakajima , Boris Ostrovsky 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. --=__Part36043E3A.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Instead leverage the NPF VM exit enforcement by marking just the top level entries as needing recalculation of their type, building on the respective range type change modifications. Signed-off-by: Jan Beulich --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -214,6 +214,10 @@ void p2m_change_entry_type_global(struct p2m_type_t ot, p2m_type_t nt) { struct p2m_domain *p2m =3D p2m_get_hostp2m(d); + + ASSERT(ot !=3D nt); + ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt)); + p2m_lock(p2m); p2m->change_entry_type_global(p2m, ot, nt); p2m->global_logdirty =3D (nt =3D=3D p2m_ram_logdirty); --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -879,11 +879,9 @@ static void ept_change_entry_type_global { unsigned long mfn =3D ept_get_asr(&p2m->ept); =20 - if ( !mfn || ot =3D=3D nt ) + if ( !mfn ) return; =20 - BUG_ON(!p2m_is_changeable(ot) || !p2m_is_changeable(nt)); - if ( ept_invalidate_emt(_mfn(mfn), 1) ) ept_sync_domain(p2m); } --- a/xen/arch/x86/mm/p2m-pt.c +++ b/xen/arch/x86/mm/p2m-pt.c @@ -827,118 +827,36 @@ pod_retry_l1: return (p2m_is_valid(*t) || p2m_is_grant(*t)) ? mfn : _mfn(INVALID_MFN= ); } =20 -/* Walk the whole p2m table, changing any entries of the old type - * to the new type. This is used in hardware-assisted paging to=20 - * quickly enable or diable log-dirty tracking */ static void p2m_pt_change_entry_type_global(struct p2m_domain *p2m, p2m_type_t ot, p2m_type_t nt) { - unsigned long mfn, gfn, flags; - l1_pgentry_t l1e_content; - l1_pgentry_t *l1e; - l2_pgentry_t *l2e; - mfn_t l1mfn, l2mfn, l3mfn; - unsigned long i1, i2, i3; - l3_pgentry_t *l3e; - l4_pgentry_t *l4e; - unsigned long i4; - - BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt)); - BUG_ON(ot !=3D nt && (ot =3D=3D p2m_mmio_direct || nt =3D=3D = p2m_mmio_direct)); - - if ( !paging_mode_translate(p2m->domain) ) - return; + l1_pgentry_t *tab; + unsigned long gfn =3D 0; + unsigned int i, changed; =20 if ( pagetable_get_pfn(p2m_get_pagetable(p2m)) =3D=3D 0 ) return; =20 ASSERT(p2m_locked_by_me(p2m)); =20 - l4e =3D map_domain_page(mfn_x(pagetable_get_mfn(p2m_get_pagetable(p2m)= ))); - - for ( i4 =3D 0; i4 < L4_PAGETABLE_ENTRIES; i4++ ) + tab =3D map_domain_page(mfn_x(pagetable_get_mfn(p2m_get_pagetable(p2m)= ))); + for ( changed =3D i =3D 0; i < (1 << PAGETABLE_ORDER); ++i ) { - if ( !(l4e_get_flags(l4e[i4]) & _PAGE_PRESENT) ) - { - continue; - } - l3mfn =3D _mfn(l4e_get_pfn(l4e[i4])); - l3e =3D map_domain_page(l4e_get_pfn(l4e[i4])); - for ( i3 =3D 0; - i3 < L3_PAGETABLE_ENTRIES; - i3++ ) - { - if ( !(l3e_get_flags(l3e[i3]) & _PAGE_PRESENT) ) - { - continue; - } - if ( (l3e_get_flags(l3e[i3]) & _PAGE_PSE) ) - { - flags =3D l3e_get_flags(l3e[i3]); - if ( p2m_flags_to_type(flags) !=3D ot ) - continue; - mfn =3D l3e_get_pfn(l3e[i3]); - gfn =3D get_gpfn_from_mfn(mfn); - flags =3D p2m_type_to_flags(nt, _mfn(mfn)); - l1e_content =3D l1e_from_pfn(mfn, flags | _PAGE_PSE); - p2m->write_p2m_entry(p2m, gfn, - (l1_pgentry_t *)&l3e[i3], - l1e_content, 3); - continue; - } - - l2mfn =3D _mfn(l3e_get_pfn(l3e[i3])); - l2e =3D map_domain_page(l3e_get_pfn(l3e[i3])); - for ( i2 =3D 0; i2 < L2_PAGETABLE_ENTRIES; i2++ ) - { - if ( !(l2e_get_flags(l2e[i2]) & _PAGE_PRESENT) ) - { - continue; - } + l1_pgentry_t e =3D tab[i]; =20 - if ( (l2e_get_flags(l2e[i2]) & _PAGE_PSE) ) - { - flags =3D l2e_get_flags(l2e[i2]); - if ( p2m_flags_to_type(flags) !=3D ot ) - continue; - mfn =3D l2e_get_pfn(l2e[i2]); - /* Do not use get_gpfn_from_mfn because it may = return=20 - SHARED_M2P_ENTRY */ - gfn =3D (i2 + (i3 + (i4 * L3_PAGETABLE_ENTRIES)) - * L2_PAGETABLE_ENTRIES) * L1_PAGETABLE_ENTRIES;= =20 - flags =3D p2m_type_to_flags(nt, _mfn(mfn)); - l1e_content =3D l1e_from_pfn(mfn, flags | _PAGE_PSE); - p2m->write_p2m_entry(p2m, gfn, - (l1_pgentry_t *)&l2e[i2], - l1e_content, 2); - continue; - } - - l1mfn =3D _mfn(l2e_get_pfn(l2e[i2])); - l1e =3D map_domain_page(mfn_x(l1mfn)); - - for ( i1 =3D 0; i1 < L1_PAGETABLE_ENTRIES; i1++ ) - { - flags =3D l1e_get_flags(l1e[i1]); - if ( p2m_flags_to_type(flags) !=3D ot ) - continue; - mfn =3D l1e_get_pfn(l1e[i1]); - gfn =3D i1 + (i2 + (i3 + (i4 * L3_PAGETABLE_ENTRIES)) - * L2_PAGETABLE_ENTRIES) * L1_PAGETABLE_ENT= RIES;=20 - /* create a new 1le entry with the new type */ - flags =3D p2m_type_to_flags(nt, _mfn(mfn)); - l1e_content =3D p2m_l1e_from_pfn(mfn, flags); - p2m->write_p2m_entry(p2m, gfn, &l1e[i1], - l1e_content, 1); - } - unmap_domain_page(l1e); - } - unmap_domain_page(l2e); + if ( (l1e_get_flags(e) & _PAGE_PRESENT) && + !needs_recalc(l1, e) ) + { + set_recalc(l1, e); + p2m->write_p2m_entry(p2m, gfn, &tab[i], e, 4); + ++changed; } - unmap_domain_page(l3e); + gfn +=3D 1UL << (L4_PAGETABLE_SHIFT - PAGE_SHIFT); } + unmap_domain_page(tab); =20 - unmap_domain_page(l4e); + if ( changed ) + flush_tlb_mask(p2m->domain->domain_dirty_cpumask); } =20 static int p2m_pt_change_entry_type_range(struct p2m_domain *p2m, --=__Part36043E3A.2__= Content-Type: text/plain; name="NPT-replace-cetg.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="NPT-replace-cetg.patch" x86/NPT: don't walk entire page tables when globally changing types=0A=0AIn= stead leverage the NPF VM exit enforcement by marking just the top=0Alevel = entries as needing recalculation of their type, building on the=0Arespectiv= e range type change modifications.=0A=0ASigned-off-by: Jan Beulich = =0A=0A--- a/xen/arch/x86/mm/p2m.c=0A+++ b/xen/arch/x86/m= m/p2m.c=0A@@ -214,6 +214,10 @@ void p2m_change_entry_type_global(struct=0A = p2m_type_t ot, p2m_type_t nt)=0A {=0A = struct p2m_domain *p2m =3D p2m_get_hostp2m(d);=0A+=0A+ ASSERT(ot !=3D = nt);=0A+ ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));=0A+=0A = p2m_lock(p2m);=0A p2m->change_entry_type_global(p2m, ot, nt);=0A = p2m->global_logdirty =3D (nt =3D=3D p2m_ram_logdirty);=0A--- a/xen/arch/x= 86/mm/p2m-ept.c=0A+++ b/xen/arch/x86/mm/p2m-ept.c=0A@@ -879,11 +879,9 @@ = static void ept_change_entry_type_global=0A {=0A unsigned long mfn =3D = ept_get_asr(&p2m->ept);=0A =0A- if ( !mfn || ot =3D=3D nt )=0A+ if ( = !mfn )=0A return;=0A =0A- BUG_ON(!p2m_is_changeable(ot) || = !p2m_is_changeable(nt));=0A-=0A if ( ept_invalidate_emt(_mfn(mfn), 1) = )=0A ept_sync_domain(p2m);=0A }=0A--- a/xen/arch/x86/mm/p2m-pt.c=0A= +++ b/xen/arch/x86/mm/p2m-pt.c=0A@@ -827,118 +827,36 @@ pod_retry_l1:=0A = return (p2m_is_valid(*t) || p2m_is_grant(*t)) ? mfn : _mfn(INVALID_MFN);= =0A }=0A =0A-/* Walk the whole p2m table, changing any entries of the old = type=0A- * to the new type. This is used in hardware-assisted paging to = =0A- * quickly enable or diable log-dirty tracking */=0A static void = p2m_pt_change_entry_type_global(struct p2m_domain *p2m,=0A = p2m_type_t ot, p2m_type_t nt)=0A {=0A- = unsigned long mfn, gfn, flags;=0A- l1_pgentry_t l1e_content;=0A- = l1_pgentry_t *l1e;=0A- l2_pgentry_t *l2e;=0A- mfn_t l1mfn, l2mfn, = l3mfn;=0A- unsigned long i1, i2, i3;=0A- l3_pgentry_t *l3e;=0A- = l4_pgentry_t *l4e;=0A- unsigned long i4;=0A-=0A- BUG_ON(p2m_is_grant(= ot) || p2m_is_grant(nt));=0A- BUG_ON(ot !=3D nt && (ot =3D=3D p2m_mmio_d= irect || nt =3D=3D p2m_mmio_direct));=0A-=0A- if ( !paging_mode_translat= e(p2m->domain) )=0A- return;=0A+ l1_pgentry_t *tab;=0A+ = unsigned long gfn =3D 0;=0A+ unsigned int i, changed;=0A =0A if ( = pagetable_get_pfn(p2m_get_pagetable(p2m)) =3D=3D 0 )=0A return;=0A = =0A ASSERT(p2m_locked_by_me(p2m));=0A =0A- l4e =3D map_domain_page(m= fn_x(pagetable_get_mfn(p2m_get_pagetable(p2m))));=0A-=0A- for ( i4 =3D = 0; i4 < L4_PAGETABLE_ENTRIES; i4++ )=0A+ tab =3D map_domain_page(mfn_x(p= agetable_get_mfn(p2m_get_pagetable(p2m))));=0A+ for ( changed =3D i =3D = 0; i < (1 << PAGETABLE_ORDER); ++i )=0A {=0A- if ( !(l4e_get_fla= gs(l4e[i4]) & _PAGE_PRESENT) )=0A- {=0A- continue;=0A- = }=0A- l3mfn =3D _mfn(l4e_get_pfn(l4e[i4]));=0A- l3e =3D = map_domain_page(l4e_get_pfn(l4e[i4]));=0A- for ( i3 =3D 0;=0A- = i3 < L3_PAGETABLE_ENTRIES;=0A- i3++ )=0A- = {=0A- if ( !(l3e_get_flags(l3e[i3]) & _PAGE_PRESENT) )=0A- = {=0A- continue;=0A- }=0A- if ( = (l3e_get_flags(l3e[i3]) & _PAGE_PSE) )=0A- {=0A- = flags =3D l3e_get_flags(l3e[i3]);=0A- if ( p2m_flags_to_type= (flags) !=3D ot )=0A- continue;=0A- mfn = =3D l3e_get_pfn(l3e[i3]);=0A- gfn =3D get_gpfn_from_mfn(mfn)= ;=0A- flags =3D p2m_type_to_flags(nt, _mfn(mfn));=0A- = l1e_content =3D l1e_from_pfn(mfn, flags | _PAGE_PSE);=0A- = p2m->write_p2m_entry(p2m, gfn,=0A- = (l1_pgentry_t *)&l3e[i3],=0A- = l1e_content, 3);=0A- continue;=0A- }=0A-=0A- = l2mfn =3D _mfn(l3e_get_pfn(l3e[i3]));=0A- l2e =3D = map_domain_page(l3e_get_pfn(l3e[i3]));=0A- for ( i2 =3D 0; i2 < = L2_PAGETABLE_ENTRIES; i2++ )=0A- {=0A- if ( = !(l2e_get_flags(l2e[i2]) & _PAGE_PRESENT) )=0A- {=0A- = continue;=0A- }=0A+ l1_pgentry_t e =3D = tab[i];=0A =0A- if ( (l2e_get_flags(l2e[i2]) & _PAGE_PSE) = )=0A- {=0A- flags =3D l2e_get_flags(l2e[i= 2]);=0A- if ( p2m_flags_to_type(flags) !=3D ot )=0A- = continue;=0A- mfn =3D l2e_get_pfn(l2= e[i2]);=0A- /* Do not use get_gpfn_from_mfn because it = may return =0A- SHARED_M2P_ENTRY */=0A- = gfn =3D (i2 + (i3 + (i4 * L3_PAGETABLE_ENTRIES))=0A- = * L2_PAGETABLE_ENTRIES) * L1_PAGETABLE_ENTRIES; =0A- = flags =3D p2m_type_to_flags(nt, _mfn(mfn));=0A- = l1e_content =3D l1e_from_pfn(mfn, flags | _PAGE_PSE);=0A- = p2m->write_p2m_entry(p2m, gfn,=0A- = (l1_pgentry_t *)&l2e[i2],=0A- = l1e_content, 2);=0A- continue;=0A- = }=0A-=0A- l1mfn =3D _mfn(l2e_get_pfn(l2e[i2]));=0A- = l1e =3D map_domain_page(mfn_x(l1mfn));=0A-=0A- for ( = i1 =3D 0; i1 < L1_PAGETABLE_ENTRIES; i1++ )=0A- {=0A- = flags =3D l1e_get_flags(l1e[i1]);=0A- if ( = p2m_flags_to_type(flags) !=3D ot )=0A- continue;=0A-= mfn =3D l1e_get_pfn(l1e[i1]);=0A- = gfn =3D i1 + (i2 + (i3 + (i4 * L3_PAGETABLE_ENTRIES))=0A- = * L2_PAGETABLE_ENTRIES) * L1_PAGETABLE_ENTRIES; =0A- = /* create a new 1le entry with the new type */=0A- = flags =3D p2m_type_to_flags(nt, _mfn(mfn));=0A- = l1e_content =3D p2m_l1e_from_pfn(mfn, flags);=0A- = p2m->write_p2m_entry(p2m, gfn, &l1e[i1],=0A- = l1e_content, 1);=0A- }=0A- = unmap_domain_page(l1e);=0A- }=0A- unmap_domain_page(l= 2e);=0A+ if ( (l1e_get_flags(e) & _PAGE_PRESENT) &&=0A+ = !needs_recalc(l1, e) )=0A+ {=0A+ set_recalc(l1, e);=0A+ = p2m->write_p2m_entry(p2m, gfn, &tab[i], e, 4);=0A+ = ++changed;=0A }=0A- unmap_domain_page(l3e);=0A+ gfn = +=3D 1UL << (L4_PAGETABLE_SHIFT - PAGE_SHIFT);=0A }=0A+ unmap_domain= _page(tab);=0A =0A- unmap_domain_page(l4e);=0A+ if ( changed )=0A+ = flush_tlb_mask(p2m->domain->domain_dirty_cpumask);=0A }=0A =0A = static int p2m_pt_change_entry_type_range(struct p2m_domain *p2m,=0A --=__Part36043E3A.2__= 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 --=__Part36043E3A.2__=--