Skip to contentSkip to conversation

x86/HVM: Avoid cache flush operations during hvm_load

PATCH Superseded Ross Lagerwall 2 participants 2 messages Jun 1, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 when doing MTRR operations and instead do a flush at the end of hvm_load.

This reduces the time to restore an HVM guest with 32 vCPUs by about 5 seconds.

---
 xen/arch/x86/hvm/mtrr.c    |  5 +++++
 xen/common/hvm/save.c      | 21 ++++++++++++++++-----
 xen/include/asm-x86/mtrr.h |  9 +++++++++
 3 files changed, 30 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/common/hvm/save.c +16 -5
index da6e668..c6c27e4 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;
@@ -222,6 +223,8 @@ int hvm_load(struct domain *d, hvm_domain_context_t *h)
         if ( test_and_set_bit(_VPF_down, &v->pause_flags) )
             vcpu_sleep_nosync(v);
 
+    this_cpu(memory_type_changed_ignore) = 1;
+
     for ( ; ; )
     {
         if ( h->size - h->cur < sizeof(struct hvm_save_descriptor) )
@@ -230,13 +233,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;
+            goto out;
         }
         
         /* 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; 
+            goto out;
         
         /* Find the handler for this entry */
         if ( (desc->typecode > HVM_SAVE_CODE_MAX) ||
@@ -244,7 +248,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;
+            goto out;
         }
 
         /* Load the entry */
@@ -254,11 +259,17 @@ 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;
+            goto out;
         }
     }
 
-    /* Not reached */
+    ASSERT_UNREACHABLE();
+
+out:
+    this_cpu(memory_type_changed_ignore) = 0;
+    memory_type_changed(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,
Signature · 1 line
2.1.0
#1 lore raw

xen/common/hvm/save.c

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 01.06.15 at 17:26, <[email protected]> wrote:
xen/common/hvm/save.c +0 -0

This being an x86-specific problem I would think this would better be addressed in x86-specific code. If it was to stay here, a proper arch hook should be introduced (even if ARM isn't using this code right now) and ...

Quoted 28 lines
xen/common/hvm/save.c +5 -3
@@ -230,13 +233,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;
+            goto out;
         }
 
         /* 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; 
+            goto out;
 
         /* Find the handler for this entry */
         if ( (desc->typecode > HVM_SAVE_CODE_MAX) ||
@@ -244,7 +248,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;
+            goto out;
         }
 
         /* Load the entry */

xen/common/hvm/save.c int hvm_load(struct domain *d, hvm_domain_context_t *h) · line 265

Quoted 8 lines
xen/common/hvm/save.c +2 -1
@@ -254,11 +259,17 @@ 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;
+            goto out;
         }
     }

... all of the goto-s should become break-s, ...

xen/common/hvm/save.c int hvm_load(struct domain *d, hvm_domain_context_t *h) · line 269

Quoted 4 lines
xen/common/hvm/save.c +3 -1
-    /* Not reached */
+    ASSERT_UNREACHABLE();
+
+out:

... eliminating the need for both of these.

Jan