Skip to contentSkip to conversation

superpages lost after migration of HVM domU

DISCUSSION Olaf Hering 3 participants 7 messages Apr 20, 2017 – Apr 28, 2017

lore
Share & tools
lore
#0 lore raw

Andrew,

with eab806a097b ("tools/libxc: x86 PV restore code") the only call of xc_domain_populate_physmap_exact was added to the new restore code. This call always sets order=0. The old migration code did consider superpages, the new one does not.

What is the reason for not using superpages when populating a HVM domU?

I supposed the first iteration would allocate all of the required memory for a domU, perhaps as superpages. ~ollowing iterations would just refill existing pages.

Olaf

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
Quoted 7 lines · Olaf Hering

Andrew,

with eab806a097b ("tools/libxc: x86 PV restore code") the only call of xc_domain_populate_physmap_exact was added to the new restore code. This call always sets order=0. The old migration code did consider superpages, the new one does not.

Quoted 5 lines

What is the reason for not using superpages when populating a HVM domU?

I supposed the first iteration would allocate all of the required memory for a domU, perhaps as superpages. ~ollowing iterations would just refill existing pages.

That was actually a bugfix for an existing migration failure, and at the time I didn't consider the performance impact. (At the time of migration v2, post-migrate runtime performance was at the very bottom of the priority list).

The calculations of when to use larger order allocations were buggy, and could end up trying to allocate more than nr_pages, which causes a hard failure of the migration. This only ended up being a problem when certain gfns had been ballooned out, but it resulted in a hard failure on the destination side.

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Therefore, it should be easy to tweak xc_sr_restore.c:populate_pfns() to find ranges of 512 consecuative gfns of XEN_DOMCTL_PFINFO_NOTAB and make a single order 9 allocation, rather than an 512 order 0 allocations.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 3 lines · Andrew Cooper

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Is there a way to preserve 1G pages? This 380G domU I'm looking at is built with 4k:461390 2M:2341 1G:365 pages.

Was there any performance testing done with the new code? I think the 15-20% degradion we are seeing might be caused by this.

Olaf

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#3 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 20.04.17 at 18:04, <[email protected]> wrote:
Quoted 3 lines · Andrew Cooper

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Is there a way to preserve 1G pages? This 380G domU I'm looking at is built with 4k:461390 2M:2341 1G:365 pages.

I think we've hashed out a possible way to deal with this, by speculatively allocating 1G pages as long as the allocation cap for the domain allows, subsequently punching holes into those pages if we can't allocate any new pages anymore (due to otherwise overrunning the cap). For ballooned down guests this may add some unnecessary overhead during migration, but I think the post-migration benefit outweighs this. All that would be needed is a second bitmap paralleling the one tracking populated pages, to also track which pages we've actually seen data for (as those obviously aren't candidates for the hole punching).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 20.04.17 at 18:04, <[email protected]> wrote:
Quoted 3 lines · Andrew Cooper

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Quoted 7 lines
Quoted 2 lines

Is there a way to preserve 1G pages? This 380G domU I'm looking at is built with 4k:461390 2M:2341 1G:365 pages.

I think we've hashed out a possible way to deal with this, by speculatively allocating 1G pages as long as the allocation cap for the domain allows, subsequently punching holes into those pages if we can't allocate any new pages anymore (due to otherwise overrunning the cap).

The result is not pretty. This HVM-only approach appears to work for a domU with "memory=3024" and localhost migration.
It is required to punch holes as soon as possible to avoid errors in xenforeignmemory_map due to "Over-allocation". Would be nice if the receiver gets a memory map upfront to avoid all stunts...

Olaf

tools/libxc/xc_sr_common.h +11 -0
index a83f22af4e..36e7891dde 100644
@@ -107,6 +107,9 @@ struct xc_sr_save_ops
  */
 struct xc_sr_restore_ops
 {
+    /* Allocate a MFN for the given PFN */
+    int (*allocate_pfn)(struct xc_sr_context *ctx, xen_pfn_t pfn);
+
     /* Convert a PFN to GFN.  May return ~0UL for an invalid mapping. */
     xen_pfn_t (*pfn_to_gfn)(const struct xc_sr_context *ctx, xen_pfn_t pfn);
 
@@ -331,6 +334,14 @@ struct xc_sr_context
                     /* HVM context blob. */
                     void *context;
                     size_t contextsz;
+
+                    /* Bitmap of currently allocated PFNs during restore. */
+                    xen_pfn_t *sp_extents;
+                    unsigned long *attempted_1g;
+                    unsigned long *attempted_2m;
+                    unsigned long *allocated_pfns;
+                    xen_pfn_t max_allocated_pfn;
+                    unsigned long alloc_cnt;
                 } restore;
             };
         } x86_hvm;
tools/libxc/xc_sr_restore.c +34 -0
index 3549f0a1ae..2e8d15307f 100644
@@ -135,6 +135,7 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned count,
                   const xen_pfn_t *original_pfns, const uint32_t *types)
 {
     xc_interface *xch = ctx->xch;
+    xen_pfn_t min_pfn = original_pfns[0], max_pfn = original_pfns[0];
     xen_pfn_t *mfns = malloc(count * sizeof(*mfns)),
         *pfns = malloc(count * sizeof(*pfns));
     unsigned i, nr_pfns = 0;
@@ -149,11 +150,18 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned count,
 
     for ( i = 0; i < count; ++i )
     {
+        if (original_pfns[i] < min_pfn)
+            min_pfn = original_pfns[i];
+        if (original_pfns[i] > max_pfn)
+            max_pfn = original_pfns[i];
         if ( (!types || (types &&
                          (types[i] != XEN_DOMCTL_PFINFO_XTAB &&
                           types[i] != XEN_DOMCTL_PFINFO_BROKEN))) &&
              !pfn_is_populated(ctx, original_pfns[i]) )
         {
+            rc = ctx->restore.ops.allocate_pfn(ctx, original_pfns[i]);
+            if ( rc )
+                goto err;
             rc = pfn_set_populated(ctx, original_pfns[i]);
             if ( rc )
                 goto err;
@@ -161,6 +169,16 @@ int populate_pfns(struct xc_sr_context *ctx, unsigned count,
             ++nr_pfns;
         }
     }
+    IPRINTF("checking range %lx %lx\n", min_pfn, max_pfn);
+    while (min_pfn < max_pfn) {
+        if (!pfn_is_populated(ctx, min_pfn) && test_and_clear_bit(min_pfn, ctx->x86_hvm.restore.allocated_pfns)) {
+            xen_pfn_t pfn = min_pfn;
+            rc = xc_domain_decrease_reservation_exact(xch, ctx->domid, 1, 0, &pfn);
+            IPRINTF("free %lx %lx %d\n", min_pfn, pfn, rc);
+        }
+        min_pfn++;
+    }
+    nr_pfns = 0;
 
     if ( nr_pfns )
     {
@@ -723,6 +741,10 @@ static void cleanup(struct xc_sr_context *ctx)
                                    NRPAGES(bitmap_size(ctx->restore.p2m_size)));
     free(ctx->restore.buffered_records);
     free(ctx->restore.populated_pfns);
+    free(ctx->x86_hvm.restore.sp_extents);
+    free(ctx->x86_hvm.restore.attempted_1g);
+    free(ctx->x86_hvm.restore.attempted_2m);
+    free(ctx->x86_hvm.restore.allocated_pfns);
     if ( ctx->restore.ops.cleanup(ctx) )
         PERROR("Failed to clean up");
 }
@@ -810,6 +832,17 @@ static int restore(struct xc_sr_context *ctx)
     saved_errno = errno;
     saved_rc = rc;
     PERROR("Restore failed");
+    {
+        unsigned long i;
+        bool a, p;
+        IPRINTF("alloc_cnt %lu\n", ctx->x86_hvm.restore.alloc_cnt);
+        for (i = 0; i < ctx->restore.p2m_size; i++) {
+            p = test_bit(i, ctx->restore.populated_pfns);
+            a = test_bit(i, ctx->x86_hvm.restore.allocated_pfns);
+            if (p != a)
+                IPRINTF("%lx a %x p %x\n", i, a, p);
+        }
+    }
 
  done:
     cleanup(ctx);
@@ -888,6 +921,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
     }
 
     ctx.restore.p2m_size = nr_pfns;
+    IPRINTF("p2m_size %lx\n", ctx.restore.p2m_size);
 
     if ( ctx.dominfo.hvm )
     {
tools/libxc/xc_sr_restore_x86_hvm.c +149 -0

169 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_pv.c +7 -0
index 50e25c162c..c426f14c73 100644
@@ -1152,8 +1152,15 @@ static int x86_pv_cleanup(struct xc_sr_context *ctx)
     return 0;
 }
 
+static int x86_pv_allocate_pfn(struct xc_sr_context *ctx, xen_pfn_t pfn)
+{
+    errno = ENOMEM;
+    return -1;
+}
+
 struct xc_sr_restore_ops restore_ops_x86_pv =
 {
+    .allocate_pfn    = x86_pv_allocate_pfn,
     .pfn_is_valid    = x86_pv_pfn_is_valid,
     .pfn_to_gfn      = pfn_to_mfn,
     .set_page_type   = x86_pv_set_page_type,

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#5 lore raw
Quoted 15 lines · Olaf Hering
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 20.04.17 at 18:04, <[email protected]> wrote:
Quoted 3 lines · Andrew Cooper

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Quoted 7 lines
Quoted 2 lines

Is there a way to preserve 1G pages? This 380G domU I'm looking at is built with 4k:461390 2M:2341 1G:365 pages.

I think we've hashed out a possible way to deal with this, by speculatively allocating 1G pages as long as the allocation cap for the domain allows, subsequently punching holes into those pages if we can't allocate any new pages anymore (due to otherwise overrunning the cap).

Quoted 5 lines

The result is not pretty. This HVM-only approach appears to work for a domU with "memory=3024" and localhost migration.
It is required to punch holes as soon as possible to avoid errors in xenforeignmemory_map due to "Over-allocation". Would be nice if the receiver gets a memory map upfront to avoid all stunts...

Oh - I was about to start working on this. This is a pleasant surprise. :)

One of the many outstanding problems with migration is that there is not a memory map at all. There really should be one, and it should be at the head of the migration stream, along with other things currently missing such as the CPUID policy. (I'm working on this, but it isn't going very fast.)

I'll review the patch as soon as I am free.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Olaf Hering
Quoted 13 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 20.04.17 at 18:04, <[email protected]> wrote:
Quoted 3 lines · Andrew Cooper

As it currently stands, the sending side iterates from 0 to p2m_size, and sends every frame on the first pass. This means we get PAGE_DATA records linearly, in batches of 1024, or two aligned 2M superpages.

Is there a way to preserve 1G pages? This 380G domU I'm looking at is built with 4k:461390 2M:2341 1G:365 pages.

I think we've hashed out a possible way to deal with this, by speculatively allocating 1G pages as long as the allocation cap for the domain allows, subsequently punching holes into those pages if we can't allocate any new pages anymore (due to otherwise overrunning the cap).

Quoted 7 lines
Quoted 5 lines

The result is not pretty. This HVM-only approach appears to work for a domU with "memory=3024" and localhost migration.
It is required to punch holes as soon as possible to avoid errors in xenforeignmemory_map due to "Over-allocation". Would be nice if the receiver gets a memory map upfront to avoid all stunts...

Oh - I was about to start working on this. This is a pleasant surprise. :)

Here is a variant that actually works for migration between two dom0s.

tools/libxc/xc_sr_common.h +56 -2
@@ -107,6 +107,9 @@ struct xc_sr_save_ops
  */
 struct xc_sr_restore_ops
 {
+    /* Allocate a MFN for the given PFN */
+    int (*allocate_pfn)(struct xc_sr_context *ctx, xen_pfn_t pfn);
+
     /* Convert a PFN to GFN.  May return ~0UL for an invalid mapping. */
     xen_pfn_t (*pfn_to_gfn)(const struct xc_sr_context *ctx, xen_pfn_t pfn);
 
@@ -172,6 +175,52 @@ struct xc_sr_x86_pv_restore_vcpu
     size_t basicsz, extdsz, xsavesz, msrsz;
 };
 
+struct xc_sr_bitmap
+{
+    void *p;
+    unsigned long bits;
+};
+
+extern bool _xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits);
+static inline bool xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits)
+{
+    if (bits > bm->bits)
+        return _xc_sr_bitmap_resize(bm, bits);
+    return true;
+}
+
+static inline void xc_sr_bitmap_free(struct xc_sr_bitmap *bm)
+{
+    free(bm->p);
+}
+
+static inline bool xc_sr_set_bit(unsigned long bit, struct xc_sr_bitmap *bm)
+{
+    if (!xc_sr_bitmap_resize(bm, bit))
+        return false;
+
+    set_bit(bit, bm->p);
+    return true;
+}
+
+static inline bool xc_sr_test_bit(unsigned long bit, struct xc_sr_bitmap *bm)
+{
+    if (bit > bm->bits)
+        return false;
+    return !!test_bit(bit, bm->p);
+}
+
+static inline int xc_sr_test_and_clear_bit(unsigned long bit, struct xc_sr_bitmap *bm)
+{
+    return test_and_clear_bit(bit, bm->p);
+}
+
+static inline int xc_sr_test_and_set_bit(unsigned long bit, struct xc_sr_bitmap *bm)
+{
+    return test_and_set_bit(bit, bm->p);
+}
+
+
 struct xc_sr_context
 {
     xc_interface *xch;
@@ -256,8 +305,7 @@ struct xc_sr_context
             domid_t      xenstore_domid,  console_domid;
 
             /* Bitmap of currently populated PFNs during restore. */
-            unsigned long *populated_pfns;
-            xen_pfn_t max_populated_pfn;
+            struct xc_sr_bitmap populated_pfns;
 
             /* Sender has invoked verify mode on the stream. */
             bool verify;
@@ -332,6 +380,12 @@ struct xc_sr_context
                     /* HVM context blob. */
                     void *context;
                     size_t contextsz;
+
+                    /* Bitmap of currently allocated PFNs during restore. */
+                    struct xc_sr_bitmap attempted_1g;
+                    struct xc_sr_bitmap attempted_2m;
+                    struct xc_sr_bitmap allocated_pfns;
+                    unsigned long alloc_cnt;
                 } restore;
             };
         } x86_hvm;
tools/libxc/xc_sr_restore.c +47 -43

152 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_hvm.c +117 -0

137 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_pv.c +7 -0
@@ -1141,8 +1141,15 @@ static int x86_pv_cleanup(struct xc_sr_c
     return 0;
 }
 
+static int x86_pv_allocate_pfn(struct xc_sr_context *ctx, xen_pfn_t pfn)
+{
+    errno = ENOMEM;
+    return -1;
+}
+
 struct xc_sr_restore_ops restore_ops_x86_pv =
 {
+    .allocate_pfn    = x86_pv_allocate_pfn,
     .pfn_is_valid    = x86_pv_pfn_is_valid,
     .pfn_to_gfn      = pfn_to_mfn,
     .set_page_type   = x86_pv_set_page_type,
tools/libxc/xc_sr_common.c +36 -0
@@ -153,6 +153,42 @@ static void __attribute__((unused)) buil
     XC_BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params)        != 8);
 }
 
+bool _xc_sr_bitmap_resize(struct xc_sr_bitmap *bm, unsigned long bits)
+{
+    if (bits > bm->bits)
+    {
+        size_t new_max;
+        size_t old_sz, new_sz;
+        void *p;
+
+        /* Round up to the nearest power of two larger than bit, less 1. */
+        new_max = bits;
+        new_max |= new_max >> 1;
+        new_max |= new_max >> 2;
+        new_max |= new_max >> 4;
+        new_max |= new_max >> 8;
+        new_max |= new_max >> 16;
+#ifdef __x86_64__
+        new_max |= new_max >> 32;
+#endif
+
+        old_sz = bitmap_size(bm->bits + 1);
+        new_sz = bitmap_size(new_max + 1);
+        p = realloc(bm->p, new_sz);
+        if (!p)
+            return false;
+
+        if (bm->p)
+            memset(p + old_sz, 0, new_sz - old_sz);
+        else
+            memset(p, 0, new_sz);
+
+        bm->p = p;
+        bm->bits = new_max;
+    }
+    return true;
+}
+
 /*
  * Local variables:
  * mode: C

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel