Skip to contentSkip to conversation

tools/libxc: use superpages

PATCH Superseded Olaf Hering 2 participants 11 messages Aug 24, 2017 – Aug 25, 2017

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v3 cover tools/libxc: use superpages

Using superpages on the receiving dom0 will avoid performance regressions.

Olaf

v3:
clear pointer in xc_sr_bitmap_free
some coding style changes
use getdomaininfo.max_pages to avoid Over-allocation check trim bitmap function names, drop trailing "_bit" add some comments
v2:
split into individual commits

Olaf Hering (3):
tools/libxc: move SUPERPAGE macros to common header tools/libxc: add API for bitmap access for restore tools/libxc: use superpages during restore of HVM guest

 tools/libxc/xc_dom_x86.c            |   5 -
 tools/libxc/xc_private.h            |   5 +
 tools/libxc/xc_sr_common.c          |  41 ++++++++
 tools/libxc/xc_sr_common.h          |  93 +++++++++++++++--
 tools/libxc/xc_sr_restore.c         | 141 ++-----------------------
 tools/libxc/xc_sr_restore_x86_hvm.c | 202 ++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_sr_restore_x86_pv.c  |  72 ++++++++++++-
 7 files changed, 411 insertions(+), 148 deletions(-)

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

#1 lore raw

PATCH v3 1/3 tools/libxc: move SUPERPAGE macros to common header

The macros SUPERPAGE_2MB_SHIFT and SUPERPAGE_1GB_SHIFT will be used by other code in libxc. Move the macros to a header file.

---
 tools/libxc/xc_dom_x86.c | 5 -----
 tools/libxc/xc_private.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
tools/libxc/xc_dom_x86.c +0 -5
index cb68efcbd3..5aff5cad58 100644
@@ -43,11 +43,6 @@
 
 #define SUPERPAGE_BATCH_SIZE 512
 
-#define SUPERPAGE_2MB_SHIFT   9
-#define SUPERPAGE_2MB_NR_PFNS (1UL << SUPERPAGE_2MB_SHIFT)
-#define SUPERPAGE_1GB_SHIFT   18
-#define SUPERPAGE_1GB_NR_PFNS (1UL << SUPERPAGE_1GB_SHIFT)
-
 #define X86_CR0_PE 0x01
 #define X86_CR0_ET 0x10
 
tools/libxc/xc_private.h +5 -0
index 1c27b0fded..d581f850b0 100644
@@ -66,6 +66,11 @@ struct iovec {
 #define DECLARE_FLASK_OP struct xen_flask_op op
 #define DECLARE_PLATFORM_OP struct xen_platform_op platform_op
 
+#define SUPERPAGE_2MB_SHIFT   9
+#define SUPERPAGE_2MB_NR_PFNS (1UL << SUPERPAGE_2MB_SHIFT)
+#define SUPERPAGE_1GB_SHIFT   18
+#define SUPERPAGE_1GB_NR_PFNS (1UL << SUPERPAGE_1GB_SHIFT)
+
 #undef PAGE_SHIFT
 #undef PAGE_SIZE
 #undef PAGE_MASK

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

#3 lore raw

PATCH v3 2/3 tools/libxc: add API for bitmap access for restore

In reply to Olaf Hering

Extend API for managing bitmaps. Each bitmap is now represented by a generic struct xc_sr_bitmap.
Switch the existing populated_pfns to this API.

---
 tools/libxc/xc_sr_common.c  | 41 +++++++++++++++++++++++++++
 tools/libxc/xc_sr_common.h  | 68 +++++++++++++++++++++++++++++++++++++++++++--
 tools/libxc/xc_sr_restore.c | 66 ++-----------------------------------------
 3 files changed, 110 insertions(+), 65 deletions(-)
tools/libxc/xc_sr_common.c +41 -0
index 79b9c3e940..4d221ca90c 100644
@@ -155,6 +155,47 @@ static void __attribute__((unused)) build_assertions(void)
     BUILD_BUG_ON(sizeof(struct xc_sr_rec_hvm_params)        != 8);
 }
 
+/*
+ * Expand the tracking structures as needed.
+ * To avoid realloc()ing too excessively, the size increased to the nearest power
+ * of two large enough to contain the required number of bits.
+ */
+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
tools/libxc/xc_sr_common.h +66 -2

86 lines not on this page · raw message

tools/libxc/xc_sr_restore.c +3 -63

84 lines not on this page · raw message

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

#5 lore raw

PATCH v3 3/3 tools/libxc: use superpages during restore of HVM guest

In reply to Olaf Hering

During creating of a HVM domU meminit_hvm() tries to map superpages. After save/restore or migration this mapping is lost, everything is allocated in single pages. This causes a performance degradition after migration.

Add neccessary code to preallocate a superpage for the chunk of pfns that is received. In case a pfn was not populated on the sending side it must be freed on the receiving side to avoid over-allocation.

The existing code for x86_pv is moved unmodified into its own file.

---
 tools/libxc/xc_sr_common.h          |  25 +++--
 tools/libxc/xc_sr_restore.c         |  75 ++-----------
 tools/libxc/xc_sr_restore_x86_hvm.c | 202 ++++++++++++++++++++++++++++++++++++
 tools/libxc/xc_sr_restore_x86_pv.c  |  72 ++++++++++++-
 4 files changed, 296 insertions(+), 78 deletions(-)
tools/libxc/xc_sr_common.h +17 -8
index 8901af112a..bf2758e91a 100644
@@ -139,6 +139,16 @@ struct xc_sr_restore_ops
      */
     int (*setup)(struct xc_sr_context *ctx);
 
+    /**
+     * Populate PFNs
+     *
+     * Given a set of pfns, obtain memory from Xen to fill the physmap for the
+     * unpopulated subset.
+     */
+    int (*populate_pfns)(struct xc_sr_context *ctx, unsigned count,
+                         const xen_pfn_t *original_pfns, const uint32_t *types);
+
+
     /**
      * Process an individual record from the stream.  The caller shall take
      * care of processing common records (e.g. END, PAGE_DATA).
@@ -224,6 +234,8 @@ struct xc_sr_context
 
             int send_back_fd;
             unsigned long p2m_size;
+            unsigned long max_pages;
+            unsigned long tot_pages;
             xc_hypercall_buffer_t dirty_bitmap_hbuf;
 
             /* From Image Header. */
@@ -336,6 +348,11 @@ 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;
                 } restore;
             };
         } x86_hvm;
@@ -455,14 +472,6 @@ static inline int write_record(struct xc_sr_context *ctx,
  */
 int read_record(struct xc_sr_context *ctx, int fd, struct xc_sr_record *rec);
 
-/*
- * This would ideally be private in restore.c, but is needed by
- * x86_pv_localise_page() if we receive pagetables frames ahead of the
- * contents of the frames they point at.
- */
-int populate_pfns(struct xc_sr_context *ctx, unsigned count,
-                  const xen_pfn_t *original_pfns, const uint32_t *types);
-
 #endif
 /*
  * Local variables:
tools/libxc/xc_sr_restore.c +6 -69

94 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_hvm.c +202 -0

231 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_pv.c +71 -1

90 lines not on this page · raw message

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

#6 lore raw

Re: [PATCH v3 3/3] tools/libxc: use superpages during restore of HVM guest

Quoted 11 lines · Olaf Hering

During creating of a HVM domU meminit_hvm() tries to map superpages. After save/restore or migration this mapping is lost, everything is allocated in single pages. This causes a performance degradition after migration.

Add neccessary code to preallocate a superpage for the chunk of pfns that is received. In case a pfn was not populated on the sending side it must be freed on the receiving side to avoid over-allocation.

The existing code for x86_pv is moved unmodified into its own file.

[...]

Quoted 39 lines
tools/libxc/xc_sr_restore_x86_hvm.c +39 -0
+static int x86_hvm_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];
+    unsigned i;
+    int rc = -1;
+
+    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[i] != XEN_DOMCTL_PFINFO_XTAB &&
+              types[i] != XEN_DOMCTL_PFINFO_BROKEN) &&
+             !pfn_is_populated(ctx, original_pfns[i]) )
+        {
+            rc = x86_hvm_allocate_pfn(ctx, original_pfns[i]);
+            if ( rc )
+                goto err;
+            rc = pfn_set_populated(ctx, original_pfns[i]);
+            if ( rc )
+                goto err;
+        }
+    }
+
+    while ( min_pfn < max_pfn )
+    {
+        if ( !xc_sr_bitmap_resize(&ctx->x86_hvm.restore.allocated_pfns, min_pfn) )
+        {
+            PERROR("Failed to realloc allocated_pfns %" PRI_xen_pfn, min_pfn);
+            goto err;
+        }
+        if ( !pfn_is_populated(ctx, min_pfn) &&
+            xc_sr_test_and_clear(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);

tools/libxc/xc_sr_restore_x86_hvm.c static int x86_hvm_stream_complete(struct xc_sr_context *ctx) · line 424

Quoted 10 lines
tools/libxc/xc_sr_restore_x86_hvm.c +10 -0
+            if ( rc )
+            {
+                PERROR("Failed to release pfn %" PRI_xen_pfn, min_pfn);
+                goto err;
+            }
+            ctx->restore.tot_pages--;
+        }
+        min_pfn++;
+    }
+

I'm still unconvinced this works all the time because it still needs the assumption that the stream contains contiguous pfns.

It works now probably because the mmio hole is placed right under 1G and you preemptively avoid allocating 1G page for the first GB.

Suppose we have the following memory layout:

[0...A) [mmio_start.......B) [B...C]
^1G here

A-0 < 1G
B-C < 1G
The guest can use up to 1G ram

And then you receive the following batch of pfns from remote

B,A-1

When B arrives, a 1G page is thus allocated, using up all guest's allowance. Decreasing reservation happens only after the batch has been processed, which means A will trigger over-allocation even on a 4K page. Then there is no fallback in x86_hvm_allocate_pfn, which will cause migration to fail.

Another less contrived example, without expanding the mmio hole beyond 1G:

[0...A) [mmio_start...1G) [1G...C...2G...D...E]

E < 2G
The guest can use up to 2G ram

And then the batch of pfns from remote:

C,D,A-1

So maybe you need to adjust the location of decrease reservation? Or do I misread your code?

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

Acked-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] tools/libxc: move SUPERPAGE macros to common header

In reply to Olaf Hering
Quoted 4 lines · Olaf Hering

The macros SUPERPAGE_2MB_SHIFT and SUPERPAGE_1GB_SHIFT will be used by other code in libxc. Move the macros to a header file.

Please fold in the tag if you end up reposting.

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

Acked-by on v3 2/3
#4 lore raw

Re: [PATCH v3 2/3] tools/libxc: add API for bitmap access for restore

In reply to Olaf Hering
Quoted 5 lines · Olaf Hering

Extend API for managing bitmaps. Each bitmap is now represented by a generic struct xc_sr_bitmap.
Switch the existing populated_pfns to this API.

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

#7 lore raw

Re: [PATCH v3 3/3] tools/libxc: use superpages during restore of HVM guest

In reply to Wei Liu
Quoted 2 lines · Wei Liu

I'm still unconvinced this works all the time because it still needs the assumption that the stream contains contiguous pfns.

This is how it is done today. If the pfns start to arrive in another order the format has to be changed to send a memory layout in advance.

I will check if some sort of retry logic can be added.

Olaf

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

#8 lore raw

Re: [PATCH v3 3/3] tools/libxc: use superpages during restore of HVM guest

Quoted 8 lines · Olaf Hering
Quoted 2 lines · Wei Liu

I'm still unconvinced this works all the time because it still needs the assumption that the stream contains contiguous pfns.

This is how it is done today. If the pfns start to arrive in another order the format has to be changed to send a memory layout in advance.

Maybe a middle ground is to scan the batch to see if pfns can be fit into a whole super page? I don't think you can get a batch as big as 1G but there should be a lot of 2M batches?

Quoted 2 lines

I will check if some sort of retry logic can be added.

This would be useful too.

Quoted 2 lines

Olaf

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

#9 lore raw

Re: [PATCH v3 3/3] tools/libxc: use superpages during restore of HVM guest

Quoted 3 lines · Wei Liu

Maybe a middle ground is to scan the batch to see if pfns can be fit into a whole super page? I don't think you can get a batch as big as 1G but there should be a lot of 2M batches?

I think with the new check of max_pages an overallocation can not happen anymore. If at some point the domU still has room for a superpage, it will be allocated. In case the batch does not fully fill the superpage, the holes will be freed. In the next batch no superpage can be allocated anymore, but single pages will be used.

This punching of holes might be inefficent, the win is the usage of superpages in case of contiguous pfns.

Olaf

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

#10 lore raw

Re: [PATCH v3 3/3] tools/libxc: use superpages during restore of HVM guest

Quoted 5 lines · Olaf Hering

I think with the new check of max_pages an overallocation can not happen anymore. If at some point the domU still has room for a superpage, it will be allocated. In case the batch does not fully fill the superpage, the holes will be freed. In the next batch no superpage can be allocated anymore, but single pages will be used.

There is one case where Over-allocation will happen: assume x86_hvm_populate_pfns gets a batch of pfns that fit trigger the allocation of a 1G page. All pfns will fit into that partly populated superpage. Then the guest has a hole right after the max_pfn of that batch. The next batch will start in a new superpage. As a result the freeing part of x86_hvm_populate_pfns will not consider the previous superpage anymore. Now 512MB are allocated, but unpopulated.

To handle this case the min_pfn/max_pfn have to be global so that the current batch can free allocated pfns from previous batches.

Olaf

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