Skip to contentSkip to conversation

x86/HVM: Avoid cache flush operations during hvm_load

PATCH In review Ross Lagerwall 3 participants 3 messages Jun 2, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/HVM: Avoid cache flush operations during hvm_load

An MTRR record is processed for each vCPU during hvm_load. Each MTRR record sets several mtrrs, each of which flushes the cache on all pCPUs. This can take some time and trip the watchdog for HVM guests with many CPUs.

To fix this, introduce a flag which prevents flushing the cache on x86 while loading the restore records and instead does a single cache flush at the end of hvm_load.

This reduces the time to restore an HVM guest with 32 vCPUs by about 5 seconds on an Intel Xeon CPU E7-2870.

---

In v2: Code moved into arch hooks since it's x86 specific.

 xen/arch/x86/hvm/mtrr.c    |  5 +++++
 xen/arch/x86/hvm/save.c    | 10 ++++++++++
 xen/common/hvm/save.c      | 15 ++++++++++-----
 xen/include/asm-x86/mtrr.h |  9 +++++++++
 xen/include/xen/hvm/save.h |  1 +
 5 files changed, 35 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/mtrr.c +5 -0
index a69ee62..f21b367 100644
@@ -65,6 +65,8 @@ static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] = {
 #undef RS
 };
 
+DEFINE_PER_CPU(bool_t, memory_type_changed_ignore);
+
 /*
  * Reverse lookup table, to find a pat type according to MTRR and effective
  * memory type. This table is dynamically generated.
@@ -789,6 +791,9 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,
 
 void memory_type_changed(struct domain *d)
 {
+    if ( this_cpu(memory_type_changed_ignore) )
+        return;
+
     if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
xen/arch/x86/hvm/save.c +10 -0
index 61f780d..b6325d0 100644
@@ -76,9 +76,19 @@ int arch_hvm_load(struct domain *d, struct hvm_save_header *hdr)
     /* VGA state is not saved/restored, so we nobble the cache. */
     d->arch.hvm_domain.stdvga.cache = 0;
 
+    /* Prevent cache flushes until after all restore records. */
+    this_cpu(memory_type_changed_ignore) = 1;
+
     return 0;
 }
 
+void arch_hvm_load_post(struct domain *d)
+{
+    /* Re-enable cache flushes and flush the cache. */
+    this_cpu(memory_type_changed_ignore) = 0;
+    memory_type_changed(d);
+}
+
 /*
  * Local variables:
  * mode: C
xen/common/hvm/save.c +10 -5
index da6e668..40ab9c0 100644
@@ -206,6 +206,7 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h)
     struct hvm_save_descriptor *desc;
     hvm_load_handler handler;
     struct vcpu *v;
+    int ret = 0;
     
     if ( d->is_dying )
         return -EINVAL;
@@ -230,13 +231,14 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h)
             printk(XENLOG_G_ERR
                    "HVM%d restore: save did not end with a null entry\n",
                    d->domain_id);
-            return -1;
+            ret = -1;
+            break;
         }
         
         /* Read the typecode of the next entry  and check for the end-marker */
         desc = (struct hvm_save_descriptor *)(&h->data[h->cur]);
         if ( desc->typecode == 0 )
-            return 0; 
+            break;
         
         /* Find the handler for this entry */
         if ( (desc->typecode > HVM_SAVE_CODE_MAX) ||
@@ -244,7 +246,8 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h)
         {
             printk(XENLOG_G_ERR "HVM%d restore: unknown entry typecode %u\n",
                    d->domain_id, desc->typecode);
-            return -1;
+            ret = -1;
+            break;
         }
 
         /* Load the entry */
@@ -254,11 +257,13 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h)
         {
             printk(XENLOG_G_ERR "HVM%d restore: failed to load entry %u/%u\n",
                    d->domain_id, desc->typecode, desc->instance);
-            return -1;
+            ret = -1;
+            break;
         }
     }
 
-    /* Not reached */
+    arch_hvm_load_post(d);
+    return ret;
 }
 
 int _hvm_init_entry(struct hvm_domain_context *h,
xen/include/asm-x86/mtrr.h +9 -0
index 0569db6..9df87cd 100644
@@ -60,6 +60,15 @@ struct mtrr_state {
 };
 extern struct mtrr_state mtrr_state;
 
+/*
+ * The purpose of the memory_type_changed_ignore cpu flag is to
+ * avoid unecessary cache flushes when doing multiple memory type
+ * operations that may flush the cache. Code can set this flag, do
+ * several memory type operations, clear the flag and then call
+ * memory_type_changed() to flush the cache at the end.
+ */
+DECLARE_PER_CPU(bool_t, memory_type_changed_ignore);
+
 extern void mtrr_save_fixed_ranges(void *);
 extern void mtrr_save_state(void);
 extern int mtrr_add(unsigned long base, unsigned long size,
xen/include/xen/hvm/save.h +1 -0
index ae6f0bb..815780b 100644
@@ -134,5 +134,6 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h);
 struct hvm_save_header;
 void arch_hvm_save(struct domain *d, struct hvm_save_header *hdr);
 int arch_hvm_load(struct domain *d, struct hvm_save_header *hdr);
+void arch_hvm_load_post(struct domain *d);
 
 #endif /* __XEN_HVM_SAVE_H__ */
Signature · 1 line
2.1.0
#1 lore raw
Quoted 29 lines · Ross Lagerwall

An MTRR record is processed for each vCPU during hvm_load. Each MTRR record sets several mtrrs, each of which flushes the cache on all pCPUs. This can take some time and trip the watchdog for HVM guests with many CPUs.

To fix this, introduce a flag which prevents flushing the cache on x86 while loading the restore records and instead does a single cache flush at the end of hvm_load.

This reduces the time to restore an HVM guest with 32 vCPUs by about 5 seconds on an Intel Xeon CPU E7-2870.

---

In v2: Code moved into arch hooks since it's x86 specific.

 xen/arch/x86/hvm/mtrr.c    |  5 +++++
 xen/arch/x86/hvm/save.c    | 10 ++++++++++
 xen/common/hvm/save.c      | 15 ++++++++++-----
 xen/include/asm-x86/mtrr.h |  9 +++++++++
 xen/include/xen/hvm/save.h |  1 +
 5 files changed, 35 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/mtrr.c +0 -0
index a69ee62..f21b367 100644

xen/arch/x86/hvm/mtrr.c static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] = { · line 68

Quoted 4 lines
xen/arch/x86/hvm/mtrr.c +1 -0
@@ -65,6 +65,8 @@ static const uint8_t mm_type_tbl[MTRR_NUM_TYPES][PAT_TYPE_NUMS] = {
 #undef RS
 };
 
+DEFINE_PER_CPU(bool_t, memory_type_changed_ignore);

Thinking about this, this should be "memory_type_changed_defer" and there should be a second bool "memory_type_changed_wanted".

This way, we don't suffer a memory_type_changed() in arch_hvm_load_post() if there were no MTRR records loaded; we support partial loads of state via this path as well.

~Andrew

#2 lore raw
In reply to Ross Lagerwall

xen/arch/x86/hvm/save.c int arch_hvm_load(struct domain *d, struct hvm_save_header *hdr) · line 90

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 02.06.15 at 14:47, <[email protected]> wrote:
xen/arch/x86/hvm/save.c +6 -0
+void arch_hvm_load_post(struct domain *d)
+{
+    /* Re-enable cache flushes and flush the cache. */
+    this_cpu(memory_type_changed_ignore) = 0;
+    memory_type_changed(d);
+}

Does this really need to be done unconditionally? I.e. couldn't this be a tristate, with memory_type_changed() e.g. flipping its sign when it gets actually called?

Also, are we certain that memory_type_changed() will never be called from asynchronous code (i.e. this doesn't introduce a latent, hard to debug issue)?

Jan