x86/P2M: reduce time bulk type changes take
v3 cover x86/P2M: reduce time bulk type changes take
1: EPT: don't walk entire page tables when globally changing types 2: EPT: don't walk entire page tables when changing types on a range 3: NPT: don't walk entire page tables when changing types on a range 4: NPT: don't walk entire page tables when globally changing types
Beyond this series it might also be worthwhile considering to make HVMOP_set_mem_type use p2m_change_type_range(), avoiding the need for explicit preemption there.
---
v3: Various adjustments according to review comments (see individual
patches).
PATCH v3 1/4 x86/EPT: don't walk entire page tables when globally changing types
Instead leverage the EPT_MISCONFIG VM exit by marking just the top level entries as needing recalculation of their type, propagating the the recalculation state down as necessary such that the actual recalculation gets done upon access.
For this to work, we have to
- restrict the types between which conversions can be done (right now only the two types involved in log dirty tracking need to be taken care of)
- remember the ranges that log dirty tracking was requested for as well as whether global log dirty tracking is in effect
---
v3: Incorporate review comments by Tim and a couple of other minor
adjustments.
xen/arch/x86/mm/hap/hap.c +12 -0
@@ -110,11 +110,18 @@ int hap_track_dirty_vram(struct domain *if ( begin_pfn != dirty_vram->begin_pfn || begin_pfn + nr != dirty_vram->end_pfn ) { +unsigned long ostart = dirty_vram->begin_pfn; +unsigned long oend = dirty_vram->end_pfn; + dirty_vram->begin_pfn = begin_pfn; dirty_vram->end_pfn = begin_pfn + nr;paging_unlock (d); +if ( oend > ostart ) +p2m_change_type_range (d, ostart, oend, + p2m_ram_logdirty, p2m_ram_rw); +/* set l1e entries of range within P2M table to be read-only. */ p2m_change_type_range (d, begin_pfn, begin_pfn + nr, p2m_ram_rw, p2m_ram_logdirty);
@@ -150,11 +157,16 @@ int hap_track_dirty_vram(struct domain ** If zero pages specified while tracking dirty vram * then stop tracking */ + begin_pfn = dirty_vram->begin_pfn; + nr = dirty_vram->end_pfn - dirty_vram->begin_pfn;xfree (dirty_vram); d->arch.hvm_domain.dirty_vram =NULL ; }paging_unlock (d); +if ( nr ) +p2m_change_type_range (d, begin_pfn, begin_pfn + nr, + p2m_ram_logdirty, p2m_ram_rw); } out:if ( dirty_bitmap )
xen/arch/x86/mm/p2m.c +59 -7
108 lines not on this page · raw message
xen/arch/x86/mm/p2m-ept.c +111 -67
334 lines not on this page · raw message
xen/include/asm-x86/hvm/vmx/vmx.h +1 -1
@@ -38,7 +38,7 @@ typedef union { ipat : 1, /* bit 6 - Ignore PAT memory type */ sp : 1, /* bit 7 - Is this a superpage? */ rsvd1 : 2, /* bits 9:8 - Reserved for future use */ - avail1 : 1, /* bit 10 - Software available 1 */ + recalc : 1, /* bit 10 - Software available 1 */ snp : 1, /* bit 11 - VT-d snoop control in shared EPT/VT-d usage */ mfn : 40, /* bits 51:12 - Machine physical frame number */
xen/include/asm-x86/p2m.h +13 -0
@@ -139,6 +139,10 @@ typedef unsigned int p2m_query_t; | p2m_to_mask(p2m_grant_map_ro) \ | p2m_to_mask(p2m_ram_shared) ) +/* Types that can be subject to bulk transitions. */ +#define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) \ + | p2m_to_mask(p2m_ram_logdirty) ) +#define P2M_POD_TYPES (p2m_to_mask(p2m_populate_on_demand))/* Pageable types */
@@ -167,6 +171,7 @@ typedef unsigned int p2m_query_t;#define p2m_is_hole (_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)#define p2m_is_mmio (_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)#define p2m_is_readonly (_t) (p2m_to_mask(_t) & P2M_RO_TYPES) +#define p2m_is_changeable (_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES)#define p2m_is_pod (_t) (p2m_to_mask(_t) & P2M_POD_TYPES)#define p2m_is_grant (_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES)/* Grant types are *not* considered valid, because they can be
@@ -209,6 +214,11 @@ struct p2m_domain {* threaded on in LRU order. */ struct list_head np2m_list; +/* Host p2m: Log-dirty ranges registered for the domain. */ +struct rangeset *logdirty_ranges; + +/* Host p2m: Global log-dirty mode enabled for the domain. */ +bool_t global_logdirty;/* Host p2m: when this flag is set, don't flush all the nested-p2m * tables on every host-p2m change. The setter of this flag
@@ -509,6 +519,9 @@ p2m_type_t p2m_change_type(struct domain/* Report a change affecting memory types. */ void p2m_memory_type_changed (struct domain *d); +int p2m_is_logdirty_range (struct p2m_domain *,unsigned long start, +unsigned long end); +/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn);int clear_mmio_p2m_entry (struct domain *d,unsigned long gfn);
x86/EPT: don't walk entire page tables when globally changing types
Instead leverage the EPT_MISCONFIG VM exit by marking just the top level entries as needing recalculation of their type, propagating the the recalculation state down as necessary such that the actual recalculation gets done upon access.
For this to work, we have to
- restrict the types between which conversions can be done (right now only the two types involved in log dirty tracking need to be taken care of)
- remember the ranges that log dirty tracking was requested for as well as whether global log dirty tracking is in effect
---
v3: Incorporate review comments by Tim and a couple of other minor
adjustments.
xen/arch/x86/mm/hap/hap.c +12 -0
@@ -110,11 +110,18 @@ int hap_track_dirty_vram(struct domain *if ( begin_pfn != dirty_vram->begin_pfn || begin_pfn + nr != dirty_vram->end_pfn ) { +unsigned long ostart = dirty_vram->begin_pfn; +unsigned long oend = dirty_vram->end_pfn; + dirty_vram->begin_pfn = begin_pfn; dirty_vram->end_pfn = begin_pfn + nr;paging_unlock (d); +if ( oend > ostart ) +p2m_change_type_range (d, ostart, oend, + p2m_ram_logdirty, p2m_ram_rw); +/* set l1e entries of range within P2M table to be read-only. */ p2m_change_type_range (d, begin_pfn, begin_pfn + nr, p2m_ram_rw, p2m_ram_logdirty);
@@ -150,11 +157,16 @@ int hap_track_dirty_vram(struct domain ** If zero pages specified while tracking dirty vram * then stop tracking */ + begin_pfn = dirty_vram->begin_pfn; + nr = dirty_vram->end_pfn - dirty_vram->begin_pfn;xfree (dirty_vram); d->arch.hvm_domain.dirty_vram =NULL ; }paging_unlock (d); +if ( nr ) +p2m_change_type_range (d, begin_pfn, begin_pfn + nr, + p2m_ram_logdirty, p2m_ram_rw); } out:if ( dirty_bitmap )
xen/arch/x86/mm/p2m.c +59 -7
108 lines not on this page · raw message
xen/arch/x86/mm/p2m-ept.c +111 -67
334 lines not on this page · raw message
xen/include/asm-x86/hvm/vmx/vmx.h +1 -1
@@ -38,7 +38,7 @@ typedef union { ipat : 1, /* bit 6 - Ignore PAT memory type */ sp : 1, /* bit 7 - Is this a superpage? */ rsvd1 : 2, /* bits 9:8 - Reserved for future use */ - avail1 : 1, /* bit 10 - Software available 1 */ + recalc : 1, /* bit 10 - Software available 1 */ snp : 1, /* bit 11 - VT-d snoop control in shared EPT/VT-d usage */ mfn : 40, /* bits 51:12 - Machine physical frame number */
xen/include/asm-x86/p2m.h +13 -0
@@ -139,6 +139,10 @@ typedef unsigned int p2m_query_t; | p2m_to_mask(p2m_grant_map_ro) \ | p2m_to_mask(p2m_ram_shared) ) +/* Types that can be subject to bulk transitions. */ +#define P2M_CHANGEABLE_TYPES (p2m_to_mask(p2m_ram_rw) \ + | p2m_to_mask(p2m_ram_logdirty) ) +#define P2M_POD_TYPES (p2m_to_mask(p2m_populate_on_demand))/* Pageable types */
@@ -167,6 +171,7 @@ typedef unsigned int p2m_query_t;#define p2m_is_hole (_t) (p2m_to_mask(_t) & P2M_HOLE_TYPES)#define p2m_is_mmio (_t) (p2m_to_mask(_t) & P2M_MMIO_TYPES)#define p2m_is_readonly (_t) (p2m_to_mask(_t) & P2M_RO_TYPES) +#define p2m_is_changeable (_t) (p2m_to_mask(_t) & P2M_CHANGEABLE_TYPES)#define p2m_is_pod (_t) (p2m_to_mask(_t) & P2M_POD_TYPES)#define p2m_is_grant (_t) (p2m_to_mask(_t) & P2M_GRANT_TYPES)/* Grant types are *not* considered valid, because they can be
@@ -209,6 +214,11 @@ struct p2m_domain {* threaded on in LRU order. */ struct list_head np2m_list; +/* Host p2m: Log-dirty ranges registered for the domain. */ +struct rangeset *logdirty_ranges; + +/* Host p2m: Global log-dirty mode enabled for the domain. */ +bool_t global_logdirty;/* Host p2m: when this flag is set, don't flush all the nested-p2m * tables on every host-p2m change. The setter of this flag
@@ -509,6 +519,9 @@ p2m_type_t p2m_change_type(struct domain/* Report a change affecting memory types. */ void p2m_memory_type_changed (struct domain *d); +int p2m_is_logdirty_range (struct p2m_domain *,unsigned long start, +unsigned long end); +/* Set mmio addresses in the p2m table (for pass-through) */ int set_mmio_p2m_entry (struct domain *d,unsigned long gfn,mfn_t mfn);int clear_mmio_p2m_entry (struct domain *d,unsigned long gfn);
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 2/4 x86/EPT: don't walk entire page tables when changing types on a range
In reply to Jan BeulichThis 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.
---
v3: Adjust the comment on ept_invalidate_emt_range().
xen/arch/x86/mm/p2m.c +26 -4
@@ -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 = start;mfn_t mfn;struct p2m_domain *p2m =p2m_get_hostp2m (d);int rc =0 ; -BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); +ASSERT (ot != nt); +ASSERT (p2m_is_changeable (ot) &&p2m_is_changeable (nt));p2m_lock (p2m); p2m->defer_nested_flush =1 ; -for ( gfn = start; gfn < end; ) +if (unlikely (end > p2m->max_mapped_pfn) ) + { +if ( !gfn ) + { + p2m->change_entry_type_global (p2m, ot, nt); + gfn = end; + } + end = p2m->max_mapped_pfn +1 ; + } + +if ( gfn < end && p2m->change_entry_type_range ) + { + rc = p2m->change_entry_type_range (p2m, ot, nt, gfn, end -1 ); + gfn = end; + } +while ( !rc && gfn < end ) {unsigned int order;
@@ -769,12 +785,18 @@ void p2m_change_type_range(struct domain order =PAGE_ORDER_4K ; }if ( pt == ot ) -p2m_set_entry (p2m, gfn, mfn, order, nt, a); + rc =p2m_set_entry (p2m, gfn, mfn, order, nt, a); gfn +=1UL << order; gfn &=-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); + }switch ( nt ) {
xen/arch/x86/mm/p2m-ept.c +119 -0
137 lines not on this page · raw message
xen/include/asm-x86/p2m.h +4 -0
@@ -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); void (*write_p2m_entry)(struct p2m_domain *p2m,
x86/EPT: don't walk entire page tables when changing types on a range
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.
---
v3: Adjust the comment on ept_invalidate_emt_range().
xen/arch/x86/mm/p2m.c +26 -4
@@ -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 = start;mfn_t mfn;struct p2m_domain *p2m =p2m_get_hostp2m (d);int rc =0 ; -BUG_ON (p2m_is_grant (ot) ||p2m_is_grant (nt)); +ASSERT (ot != nt); +ASSERT (p2m_is_changeable (ot) &&p2m_is_changeable (nt));p2m_lock (p2m); p2m->defer_nested_flush =1 ; -for ( gfn = start; gfn < end; ) +if (unlikely (end > p2m->max_mapped_pfn) ) + { +if ( !gfn ) + { + p2m->change_entry_type_global (p2m, ot, nt); + gfn = end; + } + end = p2m->max_mapped_pfn +1 ; + } + +if ( gfn < end && p2m->change_entry_type_range ) + { + rc = p2m->change_entry_type_range (p2m, ot, nt, gfn, end -1 ); + gfn = end; + } +while ( !rc && gfn < end ) {unsigned int order;
@@ -769,12 +785,18 @@ void p2m_change_type_range(struct domain order =PAGE_ORDER_4K ; }if ( pt == ot ) -p2m_set_entry (p2m, gfn, mfn, order, nt, a); + rc =p2m_set_entry (p2m, gfn, mfn, order, nt, a); gfn +=1UL << order; gfn &=-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); + }switch ( nt ) {
xen/arch/x86/mm/p2m-ept.c +119 -0
137 lines not on this page · raw message
xen/include/asm-x86/p2m.h +4 -0
@@ -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); void (*write_p2m_entry)(struct p2m_domain *p2m,
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 3/4 x86/NPT: don't walk entire page tables when changing types on a range
In reply to Jan BeulichThis builds on the fact that in order for no NPF VM exit to occur, _PAGE_USER must always be set. I.e. by clearing the flag we can force a VM exit allowing us to do similar lazy type changes as on EPT.
That way, the generic entry-wise code can go away, and we could remove the range restriction in enforced on HVMOP_track_dirty_vram for XSA-27.
---
v3: Rename p2m_npt_fault() to p2m_pt_handle_deferred_changes() and
don't call it on not-present faults (we don't ever mark non-present entries for re-calculation).
xen/arch/x86/hvm/svm/svm.c +11 -1
@@ -2557,7 +2557,17 @@ void svm_vmexit_handler(struct cpu_user_perfc_incra (svmexits,VMEXIT_NPF_PERFC );if ( cpu_has_svm_decode ) v->arch.hvm_svm.cached_insn_len = vmcb->guest_ins_len &0xf ; -svm_do_nested_pgfault (v, regs, vmcb->exitinfo1, vmcb->exitinfo2); + rc = vmcb->exitinfo1 & PFEC_page_present + ?p2m_pt_handle_deferred_changes (vmcb->exitinfo2) :0 ; +if ( rc >=0 ) +svm_do_nested_pgfault (v, regs, vmcb->exitinfo1, vmcb->exitinfo2); +else + { +printk (XENLOG_G_ERR +"%pv: Error %d handling NPF (gpa=%08lx ec=%04lx)\n" , + v, rc, vmcb->exitinfo2, vmcb->exitinfo1); +domain_crash (v->domain); + } v->arch.hvm_svm.cached_insn_len =0 ;break ;
xen/arch/x86/mm/p2m.c +1 -43
58 lines not on this page · raw message
xen/arch/x86/mm/p2m-pt.c +282 -13
400 lines not on this page · raw message
xen/include/asm-x86/p2m.h +2 -0
@@ -668,6 +668,8 @@ static inline p2m_type_t p2m_flags_to_tyreturn (flags >>12 ) &0x7f ; } +int p2m_pt_handle_deferred_changes (uint64_t gpa); +/* * Nested p2m: shadow p2m tables used for nested HVM virtualization */
x86/NPT: don't walk entire page tables when changing types on a range
This builds on the fact that in order for no NPF VM exit to occur, _PAGE_USER must always be set. I.e. by clearing the flag we can force a VM exit allowing us to do similar lazy type changes as on EPT.
That way, the generic entry-wise code can go away, and we could remove the range restriction in enforced on HVMOP_track_dirty_vram for XSA-27.
---
v3: Rename p2m_npt_fault() to p2m_pt_handle_deferred_changes() and
don't call it on not-present faults (we don't ever mark non-present entries for re-calculation).
xen/arch/x86/hvm/svm/svm.c +11 -1
@@ -2557,7 +2557,17 @@ void svm_vmexit_handler(struct cpu_user_perfc_incra (svmexits,VMEXIT_NPF_PERFC );if ( cpu_has_svm_decode ) v->arch.hvm_svm.cached_insn_len = vmcb->guest_ins_len &0xf ; -svm_do_nested_pgfault (v, regs, vmcb->exitinfo1, vmcb->exitinfo2); + rc = vmcb->exitinfo1 & PFEC_page_present + ?p2m_pt_handle_deferred_changes (vmcb->exitinfo2) :0 ; +if ( rc >=0 ) +svm_do_nested_pgfault (v, regs, vmcb->exitinfo1, vmcb->exitinfo2); +else + { +printk (XENLOG_G_ERR +"%pv: Error %d handling NPF (gpa=%08lx ec=%04lx)\n" , + v, rc, vmcb->exitinfo2, vmcb->exitinfo1); +domain_crash (v->domain); + } v->arch.hvm_svm.cached_insn_len =0 ;break ;
xen/arch/x86/mm/p2m.c +1 -43
58 lines not on this page · raw message
xen/arch/x86/mm/p2m-pt.c +282 -13
400 lines not on this page · raw message
xen/include/asm-x86/p2m.h +2 -0
@@ -668,6 +668,8 @@ static inline p2m_type_t p2m_flags_to_tyreturn (flags >>12 ) &0x7f ; } +int p2m_pt_handle_deferred_changes (uint64_t gpa); +/* * Nested p2m: shadow p2m tables used for nested HVM virtualization */
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 4/4 x86/NPT: don't walk entire page tables when globally changing types
In reply to Jan BeulichInstead 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.
---
v3: Replace the rather pointless assertion of p2m_locked_by_me() in
p2m_pt_change_entry_type_global() (this is a backend function for generic P2M code after all, where the correct locking is easily provable) by one of hap_enabled(), thus matching p2m_pt_change_entry_type_global().
xen/arch/x86/mm/p2m.c +4 -0
@@ -214,6 +214,10 @@ void p2m_change_entry_type_global(struct p2m_type_t ot, p2m_type_t nt) {struct p2m_domain *p2m =p2m_get_hostp2m (d); + +ASSERT (ot != nt); +ASSERT (p2m_is_changeable (ot) &&p2m_is_changeable (nt)); +p2m_lock (p2m); p2m->change_entry_type_global (p2m, ot, nt); p2m->global_logdirty = (nt == p2m_ram_logdirty);
xen/arch/x86/mm/p2m-ept.c +1 -3
@@ -889,11 +889,9 @@ static void ept_change_entry_type_global {unsigned long mfn =ept_get_asr (&p2m->ept); -if ( !mfn || ot == nt ) +if ( !mfn )return ; -BUG_ON (!p2m_is_changeable (ot) || !p2m_is_changeable (nt)); -if (ept_invalidate_emt (_mfn (mfn),1 ) )ept_sync_domain (p2m); }
xen/arch/x86/mm/p2m-pt.c +17 -99
135 lines not on this page · raw message
x86/NPT: don't walk entire page tables when globally changing types
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.
---
v3: Replace the rather pointless assertion of p2m_locked_by_me() in
p2m_pt_change_entry_type_global() (this is a backend function for generic P2M code after all, where the correct locking is easily provable) by one of hap_enabled(), thus matching p2m_pt_change_entry_type_global().
xen/arch/x86/mm/p2m.c +4 -0
@@ -214,6 +214,10 @@ void p2m_change_entry_type_global(struct p2m_type_t ot, p2m_type_t nt) {struct p2m_domain *p2m =p2m_get_hostp2m (d); + +ASSERT (ot != nt); +ASSERT (p2m_is_changeable (ot) &&p2m_is_changeable (nt)); +p2m_lock (p2m); p2m->change_entry_type_global (p2m, ot, nt); p2m->global_logdirty = (nt == p2m_ram_logdirty);
xen/arch/x86/mm/p2m-ept.c +1 -3
@@ -889,11 +889,9 @@ static void ept_change_entry_type_global {unsigned long mfn =ept_get_asr (&p2m->ept); -if ( !mfn || ot == nt ) +if ( !mfn )return ; -BUG_ON (!p2m_is_changeable (ot) || !p2m_is_changeable (nt)); -if (ept_invalidate_emt (_mfn (mfn),1 ) )ept_sync_domain (p2m); }
xen/arch/x86/mm/p2m-pt.c +17 -99
135 lines not on this page · raw message
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 10 lines · Jan Beulich
1: EPT: don't walk entire page tables when globally changing types 2: EPT: don't walk entire page tables when changing types on a range 3: NPT: don't walk entire page tables when changing types on a range 4: NPT: don't walk entire page tables when globally changing types
Beyond this series it might also be worthwhile considering to make
HVMOP_set_mem_typeusep2m_change_type_range(), avoiding the need for explicit preemption there.Signed-off-by Jan Beulich <[email protected]>