From c2c6e0b9c27650607a5d15aca0d598ae7251678e Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Fri, 16 Nov 2018 16:28:25 +0000 Subject: [PATCH 2/2] p2m: change_range_type: Only invalidate remapped gfns change_range_type() invalidates gfn ranges to lazily change the type of a range of gfns, and also modifies the logdirty rangesets of that p2m. At the moment, it clips both down by the hostp2m. While this will result in correct behavior, it's not entirely efficient, since invalidated entries outside that range will, on fault, simply be modified back to "empty" before faulting normally again. Separate out the calculation of the two ranges. Keep using the hostp2m's max_mapped_pfn to clip the logdirty ranges, but use {max,min}_remapped_gfn to further clip the invalidation range for alternate p2ms. Signed-off-by: George Dunlap --- xen/arch/x86/mm/p2m.c | 59 ++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/xen/arch/x86/mm/p2m.c b/xen/arch/x86/mm/p2m.c index 6d764d1e22..5a68e7fd78 100644 --- a/xen/arch/x86/mm/p2m.c +++ b/xen/arch/x86/mm/p2m.c @@ -1043,41 +1043,64 @@ static void change_type_range(struct p2m_domain *p2m, p2m_type_t ot, p2m_type_t nt) { unsigned long rangeset_start, rangeset_end; + unsigned long invalidate_start, invalidate_end; struct domain *d = p2m->domain; unsigned long host_max_pfn = p2m_get_hostp2m(d)->max_mapped_pfn; + unsigned long min_pfn = 0, max_pfn = p2m->max_mapped_pfn; int rc = 0; - rangeset_start = start; - rangeset_end = end - 1; + /* + * If we have an altp2m, the logdirty rangeset range needs to + * match that of the hostp2m, but for efficiency, we want to clip + * down the the invalidation range according to the mapped values + * in the altp2m. Keep track of and clip the ranges separately. + */ + rangeset_start = invalidate_start = start; + rangeset_end = invalidate_end = end - 1; - /* Always clip the rangeset down to the host p2m */ + /* Clip down to the host p2m */ if ( unlikely(rangeset_end > host_max_pfn) ) - rangeset_end = host_max_pfn; + rangeset_end = invalidate_end = host_max_pfn; /* If the requested range is out of scope, return doing nothing */ if ( rangeset_start > rangeset_end ) return; + if ( p2m_is_altp2m(p2m) ) + { + max_pfn = p2m->max_remapped_gfn; + min_pfn = p2m->min_remapped_gfn; + invalidate_end = min(invalidate_end, max_pfn); + invalidate_start = max(invalidate_start, min_pfn); + } + p2m->defer_nested_flush = 1; /* - * If all valid gfns are in the invalidation range, just do a - * global type change. Otherwise, invalidate only the range we - * need. + * If the p2m is empty, or the range is outside the currently + * mapped range, no need to do the invalidation; just update the + * rangeset. */ - if ( !rangeset_start && rangeset_end >= p2m->max_mapped_pfn) - p2m->change_entry_type_global(p2m, ot, nt); - else - rc = p2m->change_entry_type_range(p2m, ot, nt, - rangeset_start, rangeset_end); - - if ( rc ) + if ( invalidate_start < invalidate_end ) { - printk(XENLOG_G_ERR "Error %d changing Dom%d GFNs [%lx,%lx] from %d to %d\n", - rc, d->domain_id, rangeset_start, rangeset_end, ot, nt); - domain_crash(d); + /* + * If all valid gfns are in the invalidation range, just do a + * global type change. Otherwise, invalidate only the range + * we need. + */ + if ( invalidate_start <= min_pfn && invalidate_end >= max_pfn) + p2m->change_entry_type_global(p2m, ot, nt); + else + rc = p2m->change_entry_type_range(p2m, ot, nt, + invalidate_start, invalidate_end); + if ( rc ) + { + printk(XENLOG_G_ERR "Error %d changing Dom%d GFNs [%lx,%lx] from %d to %d\n", + rc, d->domain_id, invalidate_start, invalidate_end, ot, nt); + domain_crash(d); + } } - + switch ( nt ) { case p2m_ram_rw: -- 2.19.1