Skip to contentSkip to conversation

Fix VGA logdirty related display freezes with altp2m

PATCH Superseded Razvan Cojocaru 3 participants 13 messages Nov 22, 2018 – Nov 28, 2018

v9 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v9 cover Fix VGA logdirty related display freezes with altp2m

This series aims to prevent the display from freezing when enabling altp2m and switching to a new view (and assorted problems when resizing the display).

The series introduces p2m_{init,free}_logdirty(), allocates a new logdirty rangeset for each new altp2m, and propagates (under lock) changes to all p2ms.

[PATCH V9 1/7] x86/mm: introduce p2m_{init,free}_logdirty()
[PATCH V9 2/7] x86/p2m: switch global_logdirty from bool_t to bool [PATCH V9 3/7] x86/p2m: allocate logdirty_ranges for altp2ms
[PATCH V9 4/7] x86/p2m: refactor p2m_reset_altp2m()
[PATCH V9 5/7] x86/altp2m: fix display frozen when switching to a new view early [PATCH V9 6/7] p2m: Always use hostp2m when clipping rangesets [PATCH V9 7/7] p2m: change_range_type: Only invalidate mapped gfns

Thanks,
Razvan

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

#1 lore raw

PATCH v9 1/7 x86/mm: introduce p2m_{init, free}_logdirty()

Add logdirty_ranges allocator / deallocator helpers. p2m_init_logdirty() will not re-allocate if p2m->logdirty ranges has already been allocated.

Move the rangeset deallocation call from p2m_teardown_hostp2m() to p2m_free_one() - we will want this to apply to altp2ms as well.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- None.

---
 xen/arch/x86/mm/p2m.c | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)
xen/arch/x86/mm/p2m.c +34 -12
index b5a59d6..fea4497 100644
@@ -59,6 +59,28 @@ static void p2m_nestedp2m_init(struct p2m_domain *p2m)
 #endif
 }
 
+static int p2m_init_logdirty(struct p2m_domain *p2m)
+{
+    if ( p2m->logdirty_ranges )
+        return 0;
+
+    p2m->logdirty_ranges = rangeset_new(p2m->domain, "log-dirty",
+                                        RANGESETF_prettyprint_hex);
+    if ( !p2m->logdirty_ranges )
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void p2m_free_logdirty(struct p2m_domain *p2m)
+{
+    if ( !p2m->logdirty_ranges )
+        return;
+
+    rangeset_destroy(p2m->logdirty_ranges);
+    p2m->logdirty_ranges = NULL;
+}
+
 /* Init the datastructures for later use by the p2m code */
 static int p2m_initialise(struct domain *d, struct p2m_domain *p2m)
 {
@@ -107,6 +129,7 @@ free_p2m:
 
 static void p2m_free_one(struct p2m_domain *p2m)
 {
+    p2m_free_logdirty(p2m);
     if ( hap_enabled(p2m->domain) && cpu_has_vmx )
         ept_p2m_uninit(p2m);
     free_cpumask_var(p2m->dirty_cpumask);
@@ -116,19 +139,19 @@ static void p2m_free_one(struct p2m_domain *p2m)
 static int p2m_init_hostp2m(struct domain *d)
 {
     struct p2m_domain *p2m = p2m_init_one(d);
+    int rc;
 
-    if ( p2m )
-    {
-        p2m->logdirty_ranges = rangeset_new(d, "log-dirty",
-                                            RANGESETF_prettyprint_hex);
-        if ( p2m->logdirty_ranges )
-        {
-            d->arch.p2m = p2m;
-            return 0;
-        }
+    if ( !p2m )
+        return -ENOMEM;
+
+    rc = p2m_init_logdirty(p2m);
+
+    if ( !rc )
+        d->arch.p2m = p2m;
+    else
         p2m_free_one(p2m);
-    }
-    return -ENOMEM;
+
+    return rc;
 }
 
 static void p2m_teardown_hostp2m(struct domain *d)
@@ -138,7 +161,6 @@ static void p2m_teardown_hostp2m(struct domain *d)
 
     if ( p2m )
     {
-        rangeset_destroy(p2m->logdirty_ranges);
         p2m_free_one(p2m);
         d->arch.p2m = NULL;
     }
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

PATCH v9 2/7 x86/p2m: switch global_logdirty from bool_t to bool

In reply to Razvan Cojocaru

---
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- None.

---
 xen/include/asm-x86/p2m.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/asm-x86/p2m.h +1 -1
index 6d849a5..b52fefd 100644
@@ -222,7 +222,7 @@ struct p2m_domain {
     struct rangeset   *logdirty_ranges;
 
     /* Host p2m: Global log-dirty mode enabled for the domain. */
-    bool_t             global_logdirty;
+    bool               global_logdirty;
 
     /* Host p2m: when this flag is set, don't flush all the nested-p2m 
      * tables on every host-p2m change.  The setter of this flag 
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#4 lore raw

PATCH v9 3/7 x86/p2m: allocate logdirty_ranges for altp2ms

In reply to Razvan Cojocaru

For now, only do allocation/deallocation; keeping them in sync will be done in subsequent patches.

Logdirty synchronization will only be done for active altp2ms; so allocate logdirty rangesets (copying the host logdirty rangeset) when an altp2m is activated, and free it when deactivated.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- Indented the "out:" label from p2m_activate_altp2m() by one blank.

---
 xen/arch/x86/mm/p2m.c | 46 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 7 deletions(-)
xen/arch/x86/mm/p2m.c +39 -7
index fea4497..96a6d3e 100644
@@ -2265,6 +2265,40 @@ void p2m_flush_altp2m(struct domain *d)
     altp2m_list_unlock(d);
 }
 
+static int p2m_activate_altp2m(struct domain *d, unsigned int idx)
+{
+    struct p2m_domain *hostp2m, *p2m;
+    int rc;
+
+    ASSERT(idx < MAX_ALTP2M);
+
+    p2m = d->arch.altp2m_p2m[idx];
+    hostp2m = p2m_get_hostp2m(d);
+
+    p2m_lock(p2m);
+
+    rc = p2m_init_logdirty(p2m);
+
+    if ( rc )
+        goto out;
+
+    /* The following is really just a rangeset copy. */
+    rc = rangeset_merge(p2m->logdirty_ranges, hostp2m->logdirty_ranges);
+
+    if ( rc )
+    {
+        p2m_free_logdirty(p2m);
+        goto out;
+    }
+
+    p2m_init_altp2m_ept(d, idx);
+
+ out:
+    p2m_unlock(p2m);
+
+    return rc;
+}
+
 int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
 {
     int rc = -EINVAL;
@@ -2275,10 +2309,7 @@ int p2m_init_altp2m_by_id(struct domain *d, unsigned int idx)
     altp2m_list_lock(d);
 
     if ( d->arch.altp2m_eptp[idx] == mfn_x(INVALID_MFN) )
-    {
-        p2m_init_altp2m_ept(d, idx);
-        rc = 0;
-    }
+        rc = p2m_activate_altp2m(d, idx);
 
     altp2m_list_unlock(d);
     return rc;
@@ -2296,9 +2327,10 @@ int p2m_init_next_altp2m(struct domain *d, uint16_t *idx)
         if ( d->arch.altp2m_eptp[i] != mfn_x(INVALID_MFN) )
             continue;
 
-        p2m_init_altp2m_ept(d, i);
-        *idx = i;
-        rc = 0;
+        rc = p2m_activate_altp2m(d, i);
+
+        if ( !rc )
+            *idx = i;
 
         break;
     }
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

PATCH v9 4/7 x86/p2m: refactor p2m_reset_altp2m()

In reply to Razvan Cojocaru

Refactor p2m_reset_altp2m() so that it can be used to remove redundant codepaths, fixing the locking while we're at it.

The previous code now replaced by p2m_reset_altp2m(d, i, ALTP2M_DEACTIVATE) calls did not set p2m->min_remapped_gfn and p2m->max_remapped_gfn because in those cases the altp2m idx was disabled; so before getting used again, p2m_init_altp2m_ept() would get called, which resets them. Always setting them in p2m_reset_altp2m(), while redundant, is preferable to an extra conditional.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- Updated the patch description to mention the p2m->{min,max}_remapped_gfn changes.

---
 xen/arch/x86/mm/p2m.c | 57 ++++++++++++++++++++++++++++++---------------------
 1 file changed, 34 insertions(+), 23 deletions(-)
xen/arch/x86/mm/p2m.c +34 -23
index 96a6d3e..7c6aae7 100644
@@ -2247,6 +2247,36 @@ bool_t p2m_altp2m_lazy_copy(struct vcpu *v, paddr_t gpa,
     return 1;
 }
 
+enum altp2m_reset_type {
+    ALTP2M_RESET,
+    ALTP2M_DEACTIVATE
+};
+
+static void p2m_reset_altp2m(struct domain *d, unsigned int idx,
+                             enum altp2m_reset_type reset_type)
+{
+    struct p2m_domain *p2m;
+
+    ASSERT(idx < MAX_ALTP2M);
+    p2m = d->arch.altp2m_p2m[idx];
+
+    p2m_lock(p2m);
+
+    p2m_flush_table_locked(p2m);
+
+    if ( reset_type == ALTP2M_DEACTIVATE )
+        p2m_free_logdirty(p2m);
+
+    /* Uninit and reinit ept to force TLB shootdown */
+    ept_p2m_uninit(p2m);
+    ept_p2m_init(p2m);
+
+    p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
+    p2m->max_remapped_gfn = 0;
+
+    p2m_unlock(p2m);
+}
+
 void p2m_flush_altp2m(struct domain *d)
 {
     unsigned int i;
@@ -2255,10 +2285,7 @@ void p2m_flush_altp2m(struct domain *d)
 
     for ( i = 0; i < MAX_ALTP2M; i++ )
     {
-        p2m_flush_table(d->arch.altp2m_p2m[i]);
-        /* Uninit and reinit ept to force TLB shootdown */
-        ept_p2m_uninit(d->arch.altp2m_p2m[i]);
-        ept_p2m_init(d->arch.altp2m_p2m[i]);
+        p2m_reset_altp2m(d, i, ALTP2M_DEACTIVATE);
         d->arch.altp2m_eptp[i] = mfn_x(INVALID_MFN);
     }
 
@@ -2357,10 +2384,7 @@ int p2m_destroy_altp2m_by_id(struct domain *d, unsigned int idx)
 
         if ( !_atomic_read(p2m->active_vcpus) )
         {
-            p2m_flush_table(d->arch.altp2m_p2m[idx]);
-            /* Uninit and reinit ept to force TLB shootdown */
-            ept_p2m_uninit(d->arch.altp2m_p2m[idx]);
-            ept_p2m_init(d->arch.altp2m_p2m[idx]);
+            p2m_reset_altp2m(d, idx, ALTP2M_DEACTIVATE);
             d->arch.altp2m_eptp[idx] = mfn_x(INVALID_MFN);
             rc = 0;
         }
@@ -2485,16 +2509,6 @@ int p2m_change_altp2m_gfn(struct domain *d, unsigned int idx,
     return rc;
 }
 
-static void p2m_reset_altp2m(struct p2m_domain *p2m)
-{
-    p2m_flush_table(p2m);
-    /* Uninit and reinit ept to force TLB shootdown */
-    ept_p2m_uninit(p2m);
-    ept_p2m_init(p2m);
-    p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
-    p2m->max_remapped_gfn = 0;
-}
-
 int p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
                                 mfn_t mfn, unsigned int page_order,
                                 p2m_type_t p2mt, p2m_access_t p2ma)
@@ -2528,7 +2542,7 @@ int p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
         {
             if ( !reset_count++ )
             {
-                p2m_reset_altp2m(p2m);
+                p2m_reset_altp2m(d, i, ALTP2M_RESET);
                 last_reset_idx = i;
             }
             else
@@ -2542,10 +2556,7 @@ int p2m_altp2m_propagate_change(struct domain *d, gfn_t gfn,
                          d->arch.altp2m_eptp[i] == mfn_x(INVALID_MFN) )
                         continue;
 
-                    p2m = d->arch.altp2m_p2m[i];
-                    p2m_lock(p2m);
-                    p2m_reset_altp2m(p2m);
-                    p2m_unlock(p2m);
+                    p2m_reset_altp2m(d, i, ALTP2M_RESET);
                 }
 
                 ret = 0;
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

PATCH v9 5/7 x86/altp2m: fix display frozen when switching to a new view early

In reply to Razvan Cojocaru

When an new altp2m view is created very early on guest boot, the display will freeze (although the guest will run normally). This may also happen on resizing the display. The reason is the way Xen currently (mis)handles logdirty VGA: it intentionally misconfigures VGA pages so that they will fault.

The problem is that it only does this in the host p2m. Once we switch to a new altp2m, the misconfigured entries will no longer fault, so the display will not be updated.

This patch:
* updates ept_handle_misconfig() to use the active altp2m instead of the hostp2m;
* modifies p2m_change_entry_type_global(), p2m_memory_type_changed(), p2m_change_type_range() and p2m_finish_type_change() to propagate their changes to all valid altp2ms.

With the introduction of altp2m fields in p2m_memory_type_changed() the whole function has been put under CONFIG_HVM.

---
CC: Jun Nakajima <[email protected]>
CC: Kevin Tian <[email protected]>
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- None.

---
 xen/arch/x86/mm/p2m-ept.c |   9 ++-
 xen/arch/x86/mm/p2m-pt.c  |   8 +++
 xen/arch/x86/mm/p2m.c     | 165 ++++++++++++++++++++++++++++++++++++++--------
 xen/include/asm-x86/p2m.h |   6 +-
 4 files changed, 157 insertions(+), 31 deletions(-)
xen/arch/x86/mm/p2m-ept.c +8 -1
index 6e4e375..00fb82d 100644
@@ -657,6 +657,9 @@ bool_t ept_handle_misconfig(uint64_t gpa)
     bool_t spurious;
     int rc;
 
+    if ( altp2m_active(curr->domain) )
+        p2m = p2m_get_altp2m(curr);
+
     p2m_lock(p2m);
 
     spurious = curr->arch.hvm.vmx.ept_spurious_misconfig;
@@ -1416,9 +1419,13 @@ void p2m_init_altp2m_ept(struct domain *d, unsigned int i)
     struct p2m_domain *hostp2m = p2m_get_hostp2m(d);
     struct ept_data *ept;
 
+    p2m->default_access = hostp2m->default_access;
+    p2m->domain = hostp2m->domain;
+
+    p2m->global_logdirty = hostp2m->global_logdirty;
     p2m->ept.ad = hostp2m->ept.ad;
     p2m->min_remapped_gfn = gfn_x(INVALID_GFN);
-    p2m->max_remapped_gfn = 0;
+    p2m->max_mapped_pfn = p2m->max_remapped_gfn = 0;
     ept = &p2m->ept;
     ept->mfn = pagetable_get_pfn(p2m_get_pagetable(p2m));
     d->arch.altp2m_eptp[i] = ept->eptp;
xen/arch/x86/mm/p2m-pt.c +8 -0
index 17a6b61..b5c19df 100644
@@ -29,6 +29,7 @@
 #include <xen/event.h>
 #include <xen/trace.h>
 #include <public/vm_event.h>
+#include <asm/altp2m.h>
 #include <asm/domain.h>
 #include <asm/page.h>
 #include <asm/paging.h>
@@ -464,6 +465,13 @@ int p2m_pt_handle_deferred_changes(uint64_t gpa)
     struct p2m_domain *p2m = p2m_get_hostp2m(current->domain);
     int rc;
 
+    /*
+     * Should altp2m ever be enabled for NPT / shadow use, this code
+     * should be updated to make use of the active altp2m, like
+     * ept_handle_misconfig().
+     */
+    ASSERT(!altp2m_active(current->domain));
+
     p2m_lock(p2m);
     rc = do_recalc(p2m, PFN_DOWN(gpa));
     p2m_unlock(p2m);
xen/arch/x86/mm/p2m.c +138 -27

231 lines not on this page · raw message

xen/include/asm-x86/p2m.h +3 -3
index b52fefd..f353dcd 100644
@@ -627,9 +627,6 @@ int p2m_finish_type_change(struct domain *d,
                            gfn_t first_gfn,
                            unsigned long max_nr);
 
-/* Report a change affecting memory types. */
-void p2m_memory_type_changed(struct domain *d);
-
 int p2m_is_logdirty_range(struct p2m_domain *, unsigned long start,
                           unsigned long end);
 
@@ -660,6 +657,9 @@ void p2m_pod_dump_data(struct domain *d);
 
 #ifdef CONFIG_HVM
 
+/* Report a change affecting memory types. */
+void p2m_memory_type_changed(struct domain *d);
+
 /* Called by p2m code when demand-populating a PoD page */
 bool
 p2m_pod_demand_populate(struct p2m_domain *p2m, gfn_t gfn, unsigned int order);
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [PATCH V9 5/7] x86/altp2m: fix display frozen when switching to a new view early

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 12:40, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +3 -3
@@ -956,18 +1003,14 @@ int p2m_change_type_one(struct domain *d, unsigned long gfn_l,
 }
 
 /* Modify the p2m type of a range of gfns from ot to nt. */
-void p2m_change_type_range(struct domain *d, 
-                           unsigned long start, unsigned long end,
-                           p2m_type_t ot, p2m_type_t nt)
+static void change_type_range(struct p2m_domain *p2m,
+                              unsigned long start, unsigned long end,
+                              p2m_type_t ot, p2m_type_t nt)
 {

xen/arch/x86/mm/p2m.c int p2m_change_type_one(struct domain *d, unsigned long gfn_l, · line 1014

Quoted 10 lines
xen/arch/x86/mm/p2m.c +1 -5
     unsigned long gfn = start;
-    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct domain *d = p2m->domain;
     int rc = 0;
 
-    ASSERT(ot != nt);
-    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
-
-    p2m_lock(p2m);
     p2m->defer_nested_flush = 1;

While reviewing the first of George's patches I started wondering why you keep this and ...

xen/arch/x86/mm/p2m.c void p2m_change_type_range(struct domain *d, · line 1056

Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -1011,23 +1054,54 @@ void p2m_change_type_range(struct domain *d,
     p2m->defer_nested_flush = 0;
     if ( nestedhvm_enabled(d) )
         p2m_flush_nestedp2m(d);

... this here, such that it would be called multiple times, despite p2m_flush_nestedp2m() taking a struct domain, not a struct p2m as input.

Jan

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

#8 lore raw

Re: [PATCH V9 5/7] x86/altp2m: fix display frozen when switching to a new view early

Quoted 26 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 12:40, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +3 -3
@@ -956,18 +1003,14 @@ int p2m_change_type_one(struct domain *d, unsigned long gfn_l,
 }
 
 /* Modify the p2m type of a range of gfns from ot to nt. */
-void p2m_change_type_range(struct domain *d, 
-                           unsigned long start, unsigned long end,
-                           p2m_type_t ot, p2m_type_t nt)
+static void change_type_range(struct p2m_domain *p2m,
+                              unsigned long start, unsigned long end,
+                              p2m_type_t ot, p2m_type_t nt)
 {

xen/arch/x86/mm/p2m.c int p2m_change_type_one(struct domain *d, unsigned long gfn_l, · line 1014

Quoted 10 lines
xen/arch/x86/mm/p2m.c +1 -5
     unsigned long gfn = start;
-    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+    struct domain *d = p2m->domain;
     int rc = 0;
 
-    ASSERT(ot != nt);
-    ASSERT(p2m_is_changeable(ot) && p2m_is_changeable(nt));
-
-    p2m_lock(p2m);
     p2m->defer_nested_flush = 1;

While reviewing the first of George's patches I started wondering why you keep this and ...

Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_change_type_range(struct domain *d, · line 1056

Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
@@ -1011,23 +1054,54 @@ void p2m_change_type_range(struct domain *d,
     p2m->defer_nested_flush = 0;
     if ( nestedhvm_enabled(d) )
         p2m_flush_nestedp2m(d);

... this here, such that it would be called multiple times, despite p2m_flush_nestedp2m() taking a struct domain, not a struct p2m as input.

Good point, I'll use p2m->defer_nested_flush only for the hostp2m (the only explicit check I see in the code is "&& !p2m_get_hostp2m(d)->defer_nested_flush ) {" in hap.c) and call p2m_flush_nestedp2m(d) only in p2m_change_type_range().

Thanks,
Razvan

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

#9 lore raw

PATCH v9 6/7 p2m: Always use hostp2m when clipping rangesets

In reply to Razvan Cojocaru

The logdirty rangesets of the altp2ms need to be kept in sync with the hostp2m. This means when iterating through the altp2ms, we need to use the host p2m to clip the rangeset, not the indiviual altp2m's value.

This change also:

- Documents that the end is non-inclusive

- Calculates an "inclusive" value for the end once, rather than open-coding the modification, and (worse) back-modifying updates so that the calculation ends up correct

- Clarifies the logic deciding whether to call change_entry_type_global() or change_entry_type_range()

- Handles the case where start >= hostp2m->max_mapped_pfn

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
RFC: Wasn't sure what the best thing was to do if start >= host_max_pfn. We silently clip the logdirty rangeset to max_mapped_pfn, and the chosen behavior seems consistent with that. But it seems like such a request would almost certainly be a bug somewhere that people might like to find out about.

---
Changes since V8:
- None.

---
 xen/arch/x86/mm/p2m.c | 44 ++++++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 16 deletions(-)
xen/arch/x86/mm/p2m.c +28 -16
index 73721ee..f6494ab 100644
@@ -1002,32 +1002,44 @@ int p2m_change_type_one(struct domain *d, unsigned long gfn_l,
     return rc;
 }
 
-/* Modify the p2m type of a range of gfns from ot to nt. */
+/* Modify the p2m type of [start, end) from ot to nt. */
 static void change_type_range(struct p2m_domain *p2m,
                               unsigned long start, unsigned long end,
                               p2m_type_t ot, p2m_type_t nt)
 {
-    unsigned long gfn = start;
+    unsigned long rangeset_start, rangeset_end;
     struct domain *d = p2m->domain;
+    unsigned long host_max_pfn = p2m_get_hostp2m(d)->max_mapped_pfn;
     int rc = 0;
 
+    rangeset_start = start;
+    rangeset_end   = end - 1;
+
+    /* Always clip the rangeset down to the host p2m */
+    if ( unlikely(rangeset_end > host_max_pfn) )
+        rangeset_end = host_max_pfn;
+
+    /* If the requested range is out of scope, return doing nothing */
+    if ( rangeset_start > rangeset_end )
+        return;
+
     p2m->defer_nested_flush = 1;
 
-    if ( unlikely(end > p2m->max_mapped_pfn) )
-    {
-        if ( !gfn )
-        {
-            p2m->change_entry_type_global(p2m, ot, nt);
-            gfn = end;
-        }
-        end = p2m->max_mapped_pfn + 1;
-    }
-    if ( gfn < end )
-        rc = p2m->change_entry_type_range(p2m, ot, nt, gfn, end - 1);
+    /*
+     * If all valid gfns are in the invalidation range, just do a
+     * global type change.  Otherwise, invalidate only the range we
+     * need.
+     */
+    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 )
     {
         printk(XENLOG_G_ERR "Error %d changing Dom%d GFNs [%lx,%lx] from %d to %d\n",
-               rc, d->domain_id, start, end - 1, ot, nt);
+               rc, d->domain_id, rangeset_start, rangeset_end, ot, nt);
         domain_crash(d);
     }
 
@@ -1035,11 +1047,11 @@ static void change_type_range(struct p2m_domain *p2m,
     {
     case p2m_ram_rw:
         if ( ot == p2m_ram_logdirty )
-            rc = rangeset_remove_range(p2m->logdirty_ranges, start, end - 1);
+            rc = rangeset_remove_range(p2m->logdirty_ranges, rangeset_start, rangeset_end);
         break;
     case p2m_ram_logdirty:
         if ( ot == p2m_ram_rw )
-            rc = rangeset_add_range(p2m->logdirty_ranges, start, end - 1);
+            rc = rangeset_add_range(p2m->logdirty_ranges, rangeset_start, rangeset_end);
         break;
     default:
         break;
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

Re: [PATCH V9 6/7] p2m: Always use hostp2m when clipping rangesets

Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 12:40, <[email protected]> wrote:

The logdirty rangesets of the altp2ms need to be kept in sync with the hostp2m. This means when iterating through the altp2ms, we need to use the host p2m to clip the rangeset, not the indiviual altp2m's value.

This change also:

- Documents that the end is non-inclusive

- Calculates an "inclusive" value for the end once, rather than open-coding the modification, and (worse) back-modifying updates so that the calculation ends up correct

- Clarifies the logic deciding whether to call change_entry_type_global() or change_entry_type_range()

- Handles the case where start >= hostp2m->max_mapped_pfn

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

Quoted 6 lines

---
RFC: Wasn't sure what the best thing was to do if start >= host_max_pfn. We silently clip the logdirty rangeset to max_mapped_pfn, and the chosen behavior seems consistent with that. But it seems like such a request would almost certainly be a bug somewhere that people might like to find out about.

Warning about this might be worthwhile as a first step, but I don't think we should converted this to some form of error right away, unless the "almost" could be dropped from your explanation.

Quoted 9 lines
xen/arch/x86/mm/p2m.c +1 -1
@@ -1002,32 +1002,44 @@ int p2m_change_type_one(struct domain *d, unsigned long gfn_l,
     return rc;
 }
 
-/* Modify the p2m type of a range of gfns from ot to nt. */
+/* Modify the p2m type of [start, end) from ot to nt. */
 static void change_type_range(struct p2m_domain *p2m,

xen/arch/x86/mm/p2m.c int p2m_change_type_one(struct domain *d, unsigned long gfn_l, · line 1015

Quoted 10 lines
xen/arch/x86/mm/p2m.c +3 -1
                               unsigned long start, unsigned long end,
                               p2m_type_t ot, p2m_type_t nt)
 {
-    unsigned long gfn = start;
+    unsigned long rangeset_start, rangeset_end;
     struct domain *d = p2m->domain;
+    unsigned long host_max_pfn = p2m_get_hostp2m(d)->max_mapped_pfn;
     int rc = 0;
 
+    rangeset_start = start;

You never change this value - do you really want to introduce this redundant variable? I also don't see why you couldn't simply re-purpose "end".

xen/arch/x86/mm/p2m.c int p2m_change_type_one(struct domain *d, unsigned long gfn_l, · line 1024

Quoted 9 lines
xen/arch/x86/mm/p2m.c +9 -0
+    rangeset_end   = end - 1;
+
+    /* Always clip the rangeset down to the host p2m */
+    if ( unlikely(rangeset_end > host_max_pfn) )
+        rangeset_end = host_max_pfn;
+
+    /* If the requested range is out of scope, return doing nothing */
+    if ( rangeset_start > rangeset_end )
+        return;

The original idea behind not using such an early return was to make sure the subsequent rangeset manipulations would not be skipped. If you're convinced this is unnecessary, could you please reason about this in the description? After all the logdirty rangeset also needs to cope with subsequently increasing ->max_mapped_pfn.

Jan

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

#11 lore raw

PATCH v9 7/7 p2m: change_range_type: Only invalidate mapped gfns

In reply to Razvan Cojocaru

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 the current p2m's max_mapped_pfn to further clip the invalidation range for alternate p2ms.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Andrew Cooper <[email protected]>
CC: Wei Liu <[email protected]>
CC: "Roger Pau Monné" <[email protected]>

---
Changes since V8:
- None.

---
 xen/arch/x86/mm/p2m.c | 57 ++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 17 deletions(-)
xen/arch/x86/mm/p2m.c +40 -17
index f6494ab..69cf4da 100644
@@ -1008,39 +1008,62 @@ 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 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) )
+        invalidate_end = min(invalidate_end, max_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.
+         *
+         * NB that invalidate_end can't logically be >max_pfn at this
+         * point.  If this changes, the == will need to be changed to
+         * >=.
+         */
+        ASSERT(invalidate_end <= max_pfn);
+        if ( !invalidate_start && 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 )
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v9 7/7
#12 lore raw

Re: [PATCH V9 7/7] p2m: change_range_type: Only invalidate mapped gfns

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 22.11.18 at 12:40, <[email protected]> wrote:

change_range_type() invalidates gfn ranges to lazily change the type

Would you mind correcting the function name here?

Quoted 7 lines

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.

Quoted 6 lines

Separate out the calculation of the two ranges. Keep using the hostp2m's max_mapped_pfn to clip the logdirty ranges, but use the current p2m's max_mapped_pfn to further clip the invalidation range for alternate p2ms.

Subject to possible adjustments due to changes to patch 6

Jan

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