From: "Jan Beulich" Subject: [PATCH v2 2/6] x86/EPT: don't walk entire page tables when changing types on a range Date: Tue, 22 Apr 2014 13:29:24 +0100 Message-ID: <53567CC4020000780000ABA7@nat28.tlf.novell.com> References: <53567B1C020000780000AB8C@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartB98BB1B4.2__=" Return-path: Received: from mail6.bemta4.messagelabs.com ([85.158.143.247]) by lists.xen.org with esmtp (Exim 4.72) (envelope-from ) id 1WcZpJ-000755-7u for xen-devel@lists.xenproject.org; Tue, 22 Apr 2014 12:29:29 +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. --=__PartB98BB1B4.2__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This requires a new P2M backend hook and a little bit of extra care on accounting in the generic function. Note that even on leaf entries we must not immediately set the new type (in an attempt to avoid the EPT_MISCONFIG VM exits), since the global accounting in p2m_change_type_range() gets intentionally done only after updating page tables (or else the update there would conflict with the function's own use of p2m_is_logdirty_range()), and the correct type can only be calculated with that in place. Signed-off-by: Jan Beulich --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -730,17 +730,33 @@ void p2m_change_type_range(struct domain { p2m_access_t a; p2m_type_t pt; - unsigned long gfn; + unsigned long gfn =3D start; mfn_t mfn; struct p2m_domain *p2m =3D p2m_get_hostp2m(d); int rc =3D 0; =20 - BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt)); + ASSERT(ot !=3D nt); + ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt)); =20 p2m_lock(p2m); p2m->defer_nested_flush =3D 1; =20 - for ( gfn =3D start; gfn < end; ) + if ( unlikely(end > p2m->max_mapped_pfn) ) + { + if ( !gfn ) + { + p2m->change_entry_type_global(p2m, ot, nt); + gfn =3D end; + } + end =3D p2m->max_mapped_pfn + 1; + } + + if ( gfn < end && p2m->change_entry_type_range ) + { + rc =3D p2m->change_entry_type_range(p2m, ot, nt, gfn, end - 1); + gfn =3D end; + } + while ( !rc && gfn < end ) { unsigned int order; =20 @@ -769,12 +785,18 @@ void p2m_change_type_range(struct domain order =3D PAGE_ORDER_4K; } if ( pt =3D=3D ot ) - p2m_set_entry(p2m, gfn, mfn, order, nt, a); + rc =3D p2m_set_entry(p2m, gfn, mfn, order, nt, a); gfn +=3D 1UL << order; gfn &=3D -1UL << order; if ( !gfn ) break; } + if ( rc ) + { + printk(XENLOG_G_ERR "Error %d changing Dom%d GFNs [%lx,%lx] from = %d to %d\n", + rc, d->domain_id, start, end - 1, ot, nt); + domain_crash(d); + } =20 switch ( nt ) { --- a/xen/arch/x86/mm/p2m-ept.c +++ b/xen/arch/x86/mm/p2m-ept.c @@ -301,6 +301,75 @@ static bool_t ept_invalidate_emt(mfn_t m } =20 /* + * Just like ept_invalidate_emt() except that not all entries at the + * targeted level will need processing. The passed in range is guaranteed + * to not cross a page (table) boundary at the targeted level. + */ +static int ept_invalidate_emt_range(struct p2m_domain *p2m, + unsigned int target, + unsigned long first_gfn, + unsigned long last_gfn) +{ + ept_entry_t *table; + unsigned long gfn_remainder =3D first_gfn; + unsigned int i, index; + int rc =3D 0, ret =3D GUEST_TABLE_MAP_FAILED; + + table =3D map_domain_page(pagetable_get_pfn(p2m_get_pagetable(p2m))); + for ( i =3D ept_get_wl(&p2m->ept); i > target; --i ) + { + ret =3D ept_next_level(p2m, 1, &table, &gfn_remainder, i); + if ( ret =3D=3D GUEST_TABLE_MAP_FAILED ) + goto out; + if ( ret !=3D GUEST_TABLE_NORMAL_PAGE ) + break; + } + + if ( i > target ) + { + /* We need to split the original page. */ + ept_entry_t split_ept_entry; + + index =3D gfn_remainder >> (i * EPT_TABLE_ORDER); + split_ept_entry =3D atomic_read_ept_entry(&table[index]); + ASSERT(is_epte_superpage(&split_ept_entry)); + if ( !ept_split_super_page(p2m, &split_ept_entry, i, target) ) + { + ept_free_entry(p2m, &split_ept_entry, i); + rc =3D -ENOMEM; + goto out; + } + atomic_write_ept_entry(&table[index], split_ept_entry); + + for ( ; i > target; --i ) + if ( !ept_next_level(p2m, 1, &table, &gfn_remainder, i) ) + break; + ASSERT(i =3D=3D target); + } + + index =3D gfn_remainder >> (i * EPT_TABLE_ORDER); + i =3D (last_gfn >> (i * EPT_TABLE_ORDER)) & (EPT_PAGETABLE_ENTRIES - = 1); + for ( ; index <=3D i; ++index ) + { + ept_entry_t e =3D atomic_read_ept_entry(&table[index]); + + if ( is_epte_valid(&e) && is_epte_present(&e) && + (e.emt !=3D MTRR_NUM_TYPES || !e.recalc) ) + { + e.emt =3D MTRR_NUM_TYPES; + e.recalc =3D 1; + atomic_write_ept_entry(&table[index], e); + rc =3D 1; + } + } + + out: + unmap_domain_page(table); + + return rc; +} + +/* * Resolve deliberately mis-configured (EMT field set to an invalid = value) * entries in the page table hierarchy for the given GFN: * - calculate the correct value for the EMT field @@ -819,6 +888,53 @@ static void ept_change_entry_type_global ept_sync_domain(p2m); } =20 +static int ept_change_entry_type_range(struct p2m_domain *p2m, + p2m_type_t ot, p2m_type_t nt, + unsigned long first_gfn, + unsigned long last_gfn) +{ + unsigned int i, wl =3D ept_get_wl(&p2m->ept); + unsigned long mask =3D (1 << EPT_TABLE_ORDER) - 1; + int rc =3D 0, sync =3D 0; + + if ( !ept_get_asr(&p2m->ept) ) + return -EINVAL; + + for ( i =3D 0; i <=3D wl; ) + { + if ( first_gfn & mask ) + { + unsigned long end_gfn =3D min(first_gfn | mask, last_gfn); + + rc =3D ept_invalidate_emt_range(p2m, i, first_gfn, end_gfn); + sync |=3D rc; + if ( rc < 0 || end_gfn >=3D last_gfn ) + break; + first_gfn =3D end_gfn + 1; + } + else if ( (last_gfn & mask) !=3D mask ) + { + unsigned long start_gfn =3D max(first_gfn, last_gfn & ~mask); + + rc =3D ept_invalidate_emt_range(p2m, i, start_gfn, last_gfn); + sync |=3D rc; + if ( rc < 0 || start_gfn <=3D first_gfn ) + break; + last_gfn =3D start_gfn - 1; + } + else + { + ++i; + mask |=3D mask << EPT_TABLE_ORDER; + } + } + + if ( sync ) + ept_sync_domain(p2m); + + return rc < 0 ? rc : 0; +} + static void ept_memory_type_changed(struct p2m_domain *p2m) { unsigned long mfn =3D ept_get_asr(&p2m->ept); @@ -867,6 +983,7 @@ int ept_p2m_init(struct p2m_domain *p2m) p2m->set_entry =3D ept_set_entry; p2m->get_entry =3D ept_get_entry; p2m->change_entry_type_global =3D ept_change_entry_type_global; + p2m->change_entry_type_range =3D ept_change_entry_type_range; p2m->memory_type_changed =3D ept_memory_type_changed; p2m->audit_p2m =3D NULL; =20 --- a/xen/include/asm-x86/p2m.h +++ b/xen/include/asm-x86/p2m.h @@ -243,6 +243,10 @@ struct p2m_domain { void (*change_entry_type_global)(struct p2m_domain = *p2m, p2m_type_t ot, p2m_type_t nt); + int (*change_entry_type_range)(struct p2m_domain *p2m, + p2m_type_t ot, = p2m_type_t nt, + unsigned long first_gfn,= + unsigned long last_gfn);= void (*memory_type_changed)(struct p2m_domain *p2m); =20 void (*write_p2m_entry)(struct p2m_domain *p2m, --=__PartB98BB1B4.2__= Content-Type: text/plain; name="EPT-implement-cetr.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="EPT-implement-cetr.patch" x86/EPT: don't walk entire page tables when changing types on a range=0A=0A= This requires a new P2M backend hook and a little bit of extra care = on=0Aaccounting in the generic function.=0A=0ANote that even on leaf = entries we must not immediately set the new=0Atype (in an attempt to avoid = the EPT_MISCONFIG VM exits), since the=0Aglobal accounting in p2m_change_ty= pe_range() gets intentionally done=0Aonly after updating page tables (or = else the update there would=0Aconflict with the function's own use of = p2m_is_logdirty_range()), and=0Athe correct type can only be calculated = with that in place.=0A=0ASigned-off-by: Jan Beulich =0A= =0A--- a/xen/arch/x86/mm/p2m.c=0A+++ b/xen/arch/x86/mm/p2m.c=0A@@ -730,17 = +730,33 @@ void p2m_change_type_range(struct domain=0A {=0A p2m_access_= t a;=0A p2m_type_t pt;=0A- unsigned long gfn;=0A+ unsigned long = gfn =3D start;=0A mfn_t mfn;=0A struct p2m_domain *p2m =3D = p2m_get_hostp2m(d);=0A int rc =3D 0;=0A =0A- BUG_ON(p2m_is_grant(ot)= || p2m_is_grant(nt));=0A+ ASSERT(ot !=3D nt);=0A+ ASSERT(p2m_is_chan= geable(ot) && p2m_is_changeable(nt));=0A =0A p2m_lock(p2m);=0A = p2m->defer_nested_flush =3D 1;=0A =0A- for ( gfn =3D start; gfn < end; = )=0A+ if ( unlikely(end > p2m->max_mapped_pfn) )=0A+ {=0A+ if = ( !gfn )=0A+ {=0A+ p2m->change_entry_type_global(p2m, = ot, nt);=0A+ gfn =3D end;=0A+ }=0A+ end =3D = p2m->max_mapped_pfn + 1;=0A+ }=0A+=0A+ if ( gfn < end && p2m->change_= entry_type_range )=0A+ {=0A+ rc =3D p2m->change_entry_type_range(= p2m, ot, nt, gfn, end - 1);=0A+ gfn =3D end;=0A+ }=0A+ while = ( !rc && gfn < end )=0A {=0A unsigned int order;=0A =0A@@ = -769,12 +785,18 @@ void p2m_change_type_range(struct domain=0A = order =3D PAGE_ORDER_4K;=0A }=0A if ( pt =3D=3D ot = )=0A- p2m_set_entry(p2m, gfn, mfn, order, nt, a);=0A+ = rc =3D p2m_set_entry(p2m, gfn, mfn, order, nt, a);=0A gfn +=3D = 1UL << order;=0A gfn &=3D -1UL << order;=0A if ( !gfn )=0A = break;=0A }=0A+ if ( rc )=0A+ {=0A+ printk(XEN= LOG_G_ERR "Error %d changing Dom%d GFNs [%lx,%lx] from %d to %d\n",=0A+ = rc, d->domain_id, start, end - 1, ot, nt);=0A+ domain_cra= sh(d);=0A+ }=0A =0A switch ( nt )=0A {=0A--- a/xen/arch/x86/mm/p= 2m-ept.c=0A+++ b/xen/arch/x86/mm/p2m-ept.c=0A@@ -301,6 +301,75 @@ static = bool_t ept_invalidate_emt(mfn_t m=0A }=0A =0A /*=0A+ * Just like ept_invali= date_emt() except that not all entries at the=0A+ * targeted level will = need processing. The passed in range is guaranteed=0A+ * to not cross a = page (table) boundary at the targeted level.=0A+ */=0A+static int = ept_invalidate_emt_range(struct p2m_domain *p2m,=0A+ = unsigned int target,=0A+ = unsigned long first_gfn,=0A+ unsigned = long last_gfn)=0A+{=0A+ ept_entry_t *table;=0A+ unsigned long = gfn_remainder =3D first_gfn;=0A+ unsigned int i, index;=0A+ int rc = =3D 0, ret =3D GUEST_TABLE_MAP_FAILED;=0A+=0A+ table =3D map_domain_page= (pagetable_get_pfn(p2m_get_pagetable(p2m)));=0A+ for ( i =3D ept_get_wl(= &p2m->ept); i > target; --i )=0A+ {=0A+ ret =3D ept_next_level(p2= m, 1, &table, &gfn_remainder, i);=0A+ if ( ret =3D=3D GUEST_TABLE_MA= P_FAILED )=0A+ goto out;=0A+ if ( ret !=3D GUEST_TABLE_NO= RMAL_PAGE )=0A+ break;=0A+ }=0A+=0A+ if ( i > target = )=0A+ {=0A+ /* We need to split the original page. */=0A+ = ept_entry_t split_ept_entry;=0A+=0A+ index =3D gfn_remainder >> (i = * EPT_TABLE_ORDER);=0A+ split_ept_entry =3D atomic_read_ept_entry(&t= able[index]);=0A+ ASSERT(is_epte_superpage(&split_ept_entry));=0A+ = if ( !ept_split_super_page(p2m, &split_ept_entry, i, target) )=0A+ = {=0A+ ept_free_entry(p2m, &split_ept_entry, i);=0A+ = rc =3D -ENOMEM;=0A+ goto out;=0A+ }=0A+ = atomic_write_ept_entry(&table[index], split_ept_entry);=0A+=0A+ for = ( ; i > target; --i )=0A+ if ( !ept_next_level(p2m, 1, &table, = &gfn_remainder, i) )=0A+ break;=0A+ ASSERT(i =3D=3D = target);=0A+ }=0A+=0A+ index =3D gfn_remainder >> (i * EPT_TABLE_ORDE= R);=0A+ i =3D (last_gfn >> (i * EPT_TABLE_ORDER)) & (EPT_PAGETABLE_ENTRI= ES - 1);=0A+ for ( ; index <=3D i; ++index )=0A+ {=0A+ = ept_entry_t e =3D atomic_read_ept_entry(&table[index]);=0A+=0A+ if = ( is_epte_valid(&e) && is_epte_present(&e) &&=0A+ (e.emt !=3D = MTRR_NUM_TYPES || !e.recalc) )=0A+ {=0A+ e.emt =3D = MTRR_NUM_TYPES;=0A+ e.recalc =3D 1;=0A+ atomic_write_= ept_entry(&table[index], e);=0A+ rc =3D 1;=0A+ }=0A+ = }=0A+=0A+ out:=0A+ unmap_domain_page(table);=0A+=0A+ return = rc;=0A+}=0A+=0A+/*=0A * Resolve deliberately mis-configured (EMT field = set to an invalid value)=0A * entries in the page table hierarchy for the = given GFN:=0A * - calculate the correct value for the EMT field=0A@@ = -819,6 +888,53 @@ static void ept_change_entry_type_global=0A = ept_sync_domain(p2m);=0A }=0A =0A+static int ept_change_entry_type_range(st= ruct p2m_domain *p2m,=0A+ p2m_type_t = ot, p2m_type_t nt,=0A+ unsigned long = first_gfn,=0A+ unsigned long = last_gfn)=0A+{=0A+ unsigned int i, wl =3D ept_get_wl(&p2m->ept);=0A+ = unsigned long mask =3D (1 << EPT_TABLE_ORDER) - 1;=0A+ int rc =3D 0, = sync =3D 0;=0A+=0A+ if ( !ept_get_asr(&p2m->ept) )=0A+ return = -EINVAL;=0A+=0A+ for ( i =3D 0; i <=3D wl; )=0A+ {=0A+ if ( = first_gfn & mask )=0A+ {=0A+ unsigned long end_gfn =3D = min(first_gfn | mask, last_gfn);=0A+=0A+ rc =3D ept_invalidate_e= mt_range(p2m, i, first_gfn, end_gfn);=0A+ sync |=3D rc;=0A+ = if ( rc < 0 || end_gfn >=3D last_gfn )=0A+ break;=0A+= first_gfn =3D end_gfn + 1;=0A+ }=0A+ else if ( = (last_gfn & mask) !=3D mask )=0A+ {=0A+ unsigned long = start_gfn =3D max(first_gfn, last_gfn & ~mask);=0A+=0A+ rc =3D = ept_invalidate_emt_range(p2m, i, start_gfn, last_gfn);=0A+ sync = |=3D rc;=0A+ if ( rc < 0 || start_gfn <=3D first_gfn )=0A+ = break;=0A+ last_gfn =3D start_gfn - 1;=0A+ = }=0A+ else=0A+ {=0A+ ++i;=0A+ mask = |=3D mask << EPT_TABLE_ORDER;=0A+ }=0A+ }=0A+=0A+ if ( sync = )=0A+ ept_sync_domain(p2m);=0A+=0A+ return rc < 0 ? rc : = 0;=0A+}=0A+=0A static void ept_memory_type_changed(struct p2m_domain = *p2m)=0A {=0A unsigned long mfn =3D ept_get_asr(&p2m->ept);=0A@@ = -867,6 +983,7 @@ int ept_p2m_init(struct p2m_domain *p2m)=0A p2m->set_e= ntry =3D ept_set_entry;=0A p2m->get_entry =3D ept_get_entry;=0A = p2m->change_entry_type_global =3D ept_change_entry_type_global;=0A+ = p2m->change_entry_type_range =3D ept_change_entry_type_range;=0A = p2m->memory_type_changed =3D ept_memory_type_changed;=0A p2m->audit_p2m= =3D NULL;=0A =0A--- a/xen/include/asm-x86/p2m.h=0A+++ b/xen/include/asm-x8= 6/p2m.h=0A@@ -243,6 +243,10 @@ struct p2m_domain {=0A void = (*change_entry_type_global)(struct p2m_domain *p2m,=0A = p2m_type_t ot,=0A = p2m_type_t nt);=0A+ int = (*change_entry_type_range)(struct p2m_domain *p2m,=0A+ = p2m_type_t ot, p2m_type_t nt,=0A+ = unsigned long first_gfn,=0A+ = unsigned long last_gfn);=0A = void (*memory_type_changed)(struct p2m_domain *p2m);=0A = =0A void (*write_p2m_entry)(struct p2m_domain *p2m,=0A --=__PartB98BB1B4.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 --=__PartB98BB1B4.2__=--