Skip to contentSkip to conversation

PVH: cleanup of p2m upon p2m destroy

RFC PATCH In review Mukesh Rathor 5 participants 18 messages Dec 17, 2013 – Feb 3, 2014

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 PVH: cleanup of p2m upon p2m destroy

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

---
 xen/arch/x86/domain.c     |    3 +++
 xen/arch/x86/mm/p2m.c     |   30 ++++++++++++++++++++++++++++++
 xen/include/asm-x86/p2m.h |    4 +++-
 3 files changed, 36 insertions(+), 1 deletions(-)
xen/arch/x86/domain.c +3 -0
index c0ac5d6..7f15cdd 100644
@@ -1872,6 +1872,9 @@ int domain_relinquish_resources(struct domain *d)
     case RELMEM_not_started:
         pci_release_devices(d);
 
+        if ( (ret = p2m_cleanup(d)) )
+            return ret;
+
         /* Tear down paging-assistance stuff. */
         paging_teardown(d);
 
xen/arch/x86/mm/p2m.c +30 -0
index 441d151..a91deb2 100644
@@ -499,6 +499,36 @@ void p2m_final_teardown(struct domain *d)
     p2m_teardown_hostp2m(d);
 }
 
+/* This function to do any cleanup while walking the entire p2m */
+int p2m_cleanup(struct domain *d)
+{
+    int rc = 0;
+    unsigned long gfn, count = 0;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( !is_pvh_domain(d) )
+        return 0;
+
+    for ( gfn = p2m->next_foreign_gfn_to_check;
+          gfn <= p2m->max_mapped_pfn; gfn++, count++ )
+    {
+        p2m_type_t p2mt;
+        mfn_t mfn = get_gfn_query(d, gfn, &p2mt);
+
+        if ( unlikely(p2m_is_foreign(p2mt)) )
+            put_page(mfn_to_page(mfn));
+        put_gfn(d, gfn);
+        
+        /* Preempt every 10k pages, arbritary */
+        if ( count == 10000 && hypercall_preempt_check() )
+        {
+            p2m->next_foreign_gfn_to_check = gfn + 1;
+            rc = -EAGAIN;
+            break;
+        }
+    }
+    return rc;
+}
 
 static void
 p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn, unsigned long mfn,
xen/include/asm-x86/p2m.h +3 -1
index 6371705..d32d103 100644
@@ -254,9 +254,10 @@ struct p2m_domain {
     /* Highest guest frame that's ever been mapped in the p2m */
     unsigned long max_mapped_pfn;
 
-    /* When releasing shared gfn's in a preemptible manner, recall where
+    /* When releasing gfn's in a preemptible manner, recall where
      * to resume the search */
     unsigned long next_shared_gfn_to_relinquish;
+    unsigned long next_foreign_gfn_to_check;
 
     /* Populate-on-demand variables
      * All variables are protected with the pod lock. We cannot rely on
@@ -471,6 +472,7 @@ int p2m_alloc_table(struct p2m_domain *p2m);
 /* Return all the p2m resources to Xen. */
 void p2m_teardown(struct p2m_domain *p2m);
 void p2m_final_teardown(struct domain *d);
+int p2m_cleanup(struct domain *d);
 
 /* Add a page to a domain's p2m table */
 int guest_physmap_add_entry(struct domain *d, unsigned long gfn,
Signature · 1 line
1.7.2.3
#1 lore raw
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Mukesh Rathor

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

First of all, you leave open what the relationship of this is with your 7-patch v7 series. Why is this not part of it? Obviously any necessary cleanup would logically have to come no later than where the first creation of pages of this type happens...

Quoted 17 lines
xen/arch/x86/mm/p2m.c +17 -0
+/* This function to do any cleanup while walking the entire p2m */
+int p2m_cleanup(struct domain *d)
+{
+    int rc = 0;
+    unsigned long gfn, count = 0;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( !is_pvh_domain(d) )
+        return 0;
+
+    for ( gfn = p2m->next_foreign_gfn_to_check;
+          gfn <= p2m->max_mapped_pfn; gfn++, count++ )
+    {
+        p2m_type_t p2mt;
+        mfn_t mfn = get_gfn_query(d, gfn, &p2mt);
+
+        if ( unlikely(p2m_is_foreign(p2mt)) )

xen/arch/x86/mm/p2m.c void p2m_final_teardown(struct domain *d) · line 528

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+            put_page(mfn_to_page(mfn));
+        put_gfn(d, gfn);
+        
+        /* Preempt every 10k pages, arbritary */
+        if ( count == 10000 && hypercall_preempt_check() )
+        {
+            p2m->next_foreign_gfn_to_check = gfn + 1;
+            rc = -EAGAIN;
+            break;
+        }

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

As to the copied bug: Should hypercall_preempt_check() return false, you'd never again try to preempt.

Jan

Quoted 3 lines
xen/arch/x86/mm/p2m.c +3 -0
+    }
+    return rc;
+}
#2 lore raw
Quoted 44 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · Mukesh Rathor

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

First of all, you leave open what the relationship of this is with your 7-patch v7 series. Why is this not part of it? Obviously any necessary cleanup would logically have to come no later than where the first creation of pages of this type happens...

Quoted 17 lines
xen/arch/x86/mm/p2m.c +17 -0
+/* This function to do any cleanup while walking the entire p2m */
+int p2m_cleanup(struct domain *d)
+{
+    int rc = 0;
+    unsigned long gfn, count = 0;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( !is_pvh_domain(d) )
+        return 0;
+
+    for ( gfn = p2m->next_foreign_gfn_to_check;
+          gfn <= p2m->max_mapped_pfn; gfn++, count++ )
+    {
+        p2m_type_t p2mt;
+        mfn_t mfn = get_gfn_query(d, gfn, &p2mt);
+
+        if ( unlikely(p2m_is_foreign(p2mt)) )

xen/arch/x86/mm/p2m.c void p2m_final_teardown(struct domain *d) · line 528

Quoted 10 lines
xen/arch/x86/mm/p2m.c +10 -0
+            put_page(mfn_to_page(mfn));
+        put_gfn(d, gfn);
+        
+        /* Preempt every 10k pages, arbritary */
+        if ( count == 10000 && hypercall_preempt_check() )
+        {
+            p2m->next_foreign_gfn_to_check = gfn + 1;
+            rc = -EAGAIN;
+            break;
+        }

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Quoted 2 lines

As to the copied bug: Should hypercall_preempt_check() return false, you'd never again try to preempt.

Further, looping from 0 to max_mapped_pfn, even preemptibly, is not a good way to do this: the guest can set its own max_mapped_pfn, and we don't want Xen to spend its time counting to 2^63.

Further further, it occurs to me that the refcounting change might interact badly with nested EPT, which creates and destroys p2m tables quite regularly.

Tim.

#3 lore raw
Quoted 8 lines · On Tue, 17 Dec 2013 11:19:57 +0100 Tim Deegan <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

On 17.12.13 at 02:47, Mukesh Rathor <[email protected]> wrote:

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

......

Quoted 6 lines
Quoted 6 lines

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Quoted 6 lines
Quoted 2 lines

As to the copied bug: Should hypercall_preempt_check() return false, you'd never again try to preempt.

Further, looping from 0 to max_mapped_pfn, even preemptibly, is not a good way to do this: the guest can set its own max_mapped_pfn, and we don't want Xen to spend its time counting to 2^63.

Ok, some p2m code is setting a bad precedent.

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

Or better, if I add a count of foreign mappings and hang it off the p2m_domain, then this code would only execute in case of control domain going away... it seems in that case walking the p2m would be tolerable. That should be acceptable?

Quoted 3 lines

Further further, it occurs to me that the refcounting change might interact badly with nested EPT, which creates and destroys p2m tables quite regularly.

nested ept is not supported on pvh.

thanks
mukesh

#4 lore raw
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Mukesh Rathor

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small,

That's according to the usage you're aware of. I think Fujitsu's tools to control BS2000 guests map huge parts of guest memory (if not all of it).

Quoted 9 lines

so the overhead of
16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

Or better, if I add a count of foreign mappings and hang it off the p2m_domain, then this code would only execute in case of control domain going away... it seems in that case walking the p2m would be tolerable. That should be acceptable?

That would at least be a first step (following the shared page count maintained for a domain). I don't think this is going to be good enough though considering disaggregation.

Jan

#5 lore raw

Am Mittwoch 18 Dezember 2013, 10:03:00 schrieb Jan Beulich:

Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line · Mukesh Rathor

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small,

That's according to the usage you're aware of. I think Fujitsu's tools to control BS2000 guests map huge parts of guest memory (if not all of it).

Yes, that's true, think of dimensions in GB.

Dietmar.

Quoted 23 lines
Quoted 10 lines

so the overhead of
16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

Or better, if I add a count of foreign mappings and hang it off the p2m_domain, then this code would only execute in case of control domain going away... it seems in that case walking the p2m would be tolerable. That should be acceptable?

That would at least be a first step (following the shared page count maintained for a domain). I don't think this is going to be good enough though considering disaggregation.

Jan

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

Signature · 1 line
Company details: http://ts.fujitsu.com/imprint.html
#6 lore raw
In reply to Mukesh Rathor
Quoted 18 lines · Mukesh Rathor
Quoted 8 lines · On Tue, 17 Dec 2013 11:19:57 +0100 Tim Deegan <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

On 17.12.13 at 02:47, Mukesh Rathor <[email protected]> wrote:

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

......

Quoted 6 lines
Quoted 6 lines

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

As to the copied bug: Should hypercall_preempt_check() return false, you'd never again try to preempt.

Further, looping from 0 to max_mapped_pfn, even preemptibly, is not a good way to do this: the guest can set its own max_mapped_pfn, and we don't want Xen to spend its time counting to 2^63.

Ok, some p2m code is setting a bad precedent.

Yes, indeed. :( As I pointed out before, and has been the subject of several XSAs.

Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

Quoted 3 lines

Or better, if I add a count of foreign mappings and hang it off the p2m_domain, then this code would only execute in case of control domain going away...

That's a good idea as an optimisation, but it doesn't prevent the bad
case: this teardown has to execute for any domain that has a p2m and at least one foreign mapping. Since that domain chooses the GFN of the mapping, it can still cause the hypervisor to to a lot of work.

Quoted 5 lines
Quoted 3 lines

Further further, it occurs to me that the refcounting change might interact badly with nested EPT, which creates and destroys p2m tables quite regularly.

nested ept is not supported on pvh.

What, never? Or as a 'fimxe' for later? We should avoid painting ourselves into any more corners here if we can.

Tim.

#7 lore raw
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

Cheers,

Tim.

#8 lore raw
Quoted 12 lines · On Wed, 18 Dec 2013 17:51:52 +0100 Tim Deegan <[email protected]> wrote:
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

Quoted 5 lines

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

No, not quite, because I need to know which mfns are foreign and do put_page on them.

By changing things around a bit for change ept type, I came up with following:

xen/arch/x86/mm/p2m-ept.c +74 -0
index 0ba2365..c996aac 100644
@@ -838,6 +838,80 @@ void setup_ept_dump(void)
     register_keyhandler('D', &ept_p2m_table);
 }
 
+typedef int (ept_walk_entry_callback_f)(ept_entry_t *, unsigned long);
+
+static int ept_change_entry_type(ept_entry_t *entry, unsigned long data)
+{
+    p2m_type_t new = (p2m_type_t)data;
+
+    entry->sa_p2mt = new;
+    ept_p2m_type_to_flags(entry, new, entry->access);
+    return 1;
+}
+
+static int ept_put_foreign_mfn(ept_entry_t *entry, unsigned long data)
+{
+    put_page(mfn_to_page(entry->mfn));
+    return 0;
+}
+
+static void ept_walk_entry_callback_recurse(
+    mfn_t ept_page_mfn, int ept_page_level, p2m_type_t p2mt, 
+    ept_walk_entry_callback_f *fp, unsigned long data)
+{
+    ept_entry_t e, *epte = map_domain_page(mfn_x(ept_page_mfn));
+
+    for ( int i = 0; i < EPT_PAGETABLE_ENTRIES; i++ )
+    {
+        if ( !is_epte_valid(epte + i) )
+            continue;
+
+        if ( (ept_page_level > 0) && !is_epte_superpage(epte + i) )
+            ept_walk_entry_callback_recurse(_mfn(epte[i].mfn),
+                                            ept_page_level - 1, p2mt, fp, data);
+        else
+        {
+            e = atomic_read_ept_entry(&epte[i]);
+
+            if ( e.sa_p2mt == p2mt && fp(&e, data) )
+                    atomic_write_ept_entry(&epte[i], &e);
+        }
+    }
+
+    unmap_domain_page(epte);
+}
+
+static void ept_walk_entry_callback(struct p2m_domain *p2m, p2m_type_t p2mt,
+                                    ept_walk_entry_callback_f *fp, 
+                                    unsigned long data, int do_flush)
+{
+    struct ept_data *ept = &p2m->ept;
+
+    if ( ept_get_asr(ept) == 0 )
+        return;
+
+    ept_walk_entry_callback_recurse(_mfn(ept_get_asr(ept)), ept_get_wl(ept),
+                                    p2mt, fp, data);
+    if ( do_flush )
+        ept_sync_domain(p2m);
+}
+
+int ept_release_foreign_pages(struct p2m_domain *p2m)
+{
+    ept_walk_entry_callback(p2m, p2m_map_foreign, ept_put_foreign_mfn, 0, 0);
+    return 0;
+}
+
+int ept_change_entry_type_global_mine(struct p2m_domain *p2m,
+                                      p2m_type_t ot, p2m_type_t nt)
+{
+    BUG_ON(p2m_is_grant(ot) || p2m_is_grant(nt));
+    BUG_ON(ot != nt && (ot == p2m_mmio_direct || nt == p2m_mmio_direct));
+
+    ept_walk_entry_callback(p2m, ot, ept_change_entry_type, nt, 1);
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C

I can further make this pre-emptible by returning some "handle" that the caller must stash somewhere, say, p2m_domain, so that at ept level, synchronization doesn't need to be concerned about.

what do you think?

thanks
mukesh

#9 lore raw
Quoted 15 lines · Mukesh Rathor
Quoted 12 lines · On Wed, 18 Dec 2013 17:51:52 +0100 Tim Deegan <[email protected]> wrote:
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

Quoted 8 lines
Quoted 5 lines

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

No, not quite, because I need to know which mfns are foreign and do put_page on them.

Oh right, but you need to do that anyway -- once you've got refcounts held by ept entries, you need to do _all_ entry writes through the function that preserves the refcount invariant. So p2m_change_entry_type_global() has to use that function too, in case either of the types it's called with is p2m_foreign. And once that's fixed, a call to s/foreign/invalid/ DTRT.

Also, this:

xen/arch/x86/mm/p2m-ept.c void setup_ept_dump(void) · line 856

Quoted 5 lines
xen/arch/x86/mm/p2m-ept.c +5 -0
+static int ept_put_foreign_mfn(ept_entry_t *entry, unsigned long data)
+{
+    put_page(mfn_to_page(entry->mfn));
+    return 0;
+}

gives me the heebie-jeebies, because it explcitly _breaks_ the invariant, leaving the foreign mapping in place without its refcount.

Tim.

#10 lore raw
Quoted 25 lines · On Thu, 19 Dec 2013 11:50:58 +0100 Tim Deegan <[email protected]> wrote:
Quoted 15 lines · Mukesh Rathor
Quoted 12 lines · On Wed, 18 Dec 2013 17:51:52 +0100 Tim Deegan <[email protected]> wrote:
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

Quoted 8 lines
Quoted 5 lines

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

No, not quite, because I need to know which mfns are foreign and do put_page on them.

Quoted 6 lines

Oh right, but you need to do that anyway -- once you've got refcounts held by ept entries, you need to do _all_ entry writes through the function that preserves the refcount invariant. So p2m_change_entry_type_global() has to use that function too, in case either of the types it's called with is p2m_foreign. And once that's fixed, a call to s/foreign/invalid/ DTRT.

Right, right... i keep not realizing, it's there now and it's use will scope beyond the current PVH usage. So rather than deferring to when someone actually finds a use case for p2m_change_entry_type_global using foreign, i should be handled now.

I'll have next, un-rushed this time, patch prob first week of jan given the upcoming holidays.

Quoted 10 lines

Also, this:

xen/arch/x86/mm/p2m-ept.c void setup_ept_dump(void) · line 856

Quoted 5 lines

+static int ept_put_foreign_mfn(ept_entry_t *entry, unsigned long data) +{

xen/arch/x86/mm/p2m-ept.c +3 -0
+    put_page(mfn_to_page(entry->mfn));
+    return 0;
+}

gives me the heebie-jeebies, because it explcitly _breaks_ the invariant, leaving the foreign mapping in place without its refcount.

I was thinking the case of multiple mappings, but re-thinking about it, multiple mappings should just not be allowed IMO, so as soon as put_page is done, the entry be invalidated. I'll redo it.

thanks a lot,
mukesh

#11 lore raw

Hi,

Quoted 12 lines · Mukesh Rathor
Quoted 10 lines · On Thu, 19 Dec 2013 11:50:58 +0100 Tim Deegan <[email protected]> wrote:
Quoted 2 lines · Mukesh Rathor

No, not quite, because I need to know which mfns are foreign and do put_page on them.

Oh right, but you need to do that anyway -- once you've got refcounts held by ept entries, you need to do _all_ entry writes through the function that preserves the refcount invariant. So p2m_change_entry_type_global() has to use that function too, in case either of the types it's called with is p2m_foreign. And once that's fixed, a call to s/foreign/invalid/ DTRT.

Quoted 7 lines

Right, right... i keep not realizing, it's there now and it's use will scope beyond the current PVH usage. So rather than deferring to when someone actually finds a use case for p2m_change_entry_type_global using foreign, i should be handled now.

I'll have next, un-rushed this time, patch prob first week of jan given the upcoming holidays.

Great! Looking forward to it. Happy holidays in the meantime. :)

Tim.

#12 lore raw
In reply to Tim Deegan
Quoted 18 lines · On Wed, 18 Dec 2013 17:51:52 +0100 Tim Deegan <[email protected]> wrote:
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

Quoted 3 lines

Cheers,

Tim.

Finally, coming back to this, the answer is yes. Looks like all I need to do is:

xen/arch/x86/mm/p2m.c +4 -0
index 9faa663..268a8a2 100644
@@ -470,6 +470,10 @@ void p2m_teardown(struct p2m_domain *p2m)
 
     p2m_lock(p2m);
 
+    /* pvh: we must release refcnt on all foreign pages */
+    if ( is_pvh_domain(d) )
+        p2m_change_entry_type_global(d, p2m_map_foreign, p2m_invalid);
+
     /* Try to unshare any remaining shared p2m entries. Safeguard
      * Since relinquish_shared_pages should have done the work. */ 
     for ( gfn=0; gfn < p2m->max_mapped_pfn; gfn++ )

In this call, the new atomic_write_ept_entry() will DTRT:

static inline void atomic_write_ept_entry(ept_entry_t *entryptr,
const ept_entry_t *new)

{
    if ( p2m_is_foreign(new->sa_p2mt) )
    {
        struct page_info *page;
        struct domain *fdom;

        ASSERT(mfn_valid(new->mfn));
        page = mfn_to_page(new->mfn);
        fdom = page_get_owner(page);
        get_page(page, fdom);
    }
    if ( p2m_is_foreign(entryptr->sa_p2mt) )
        put_page(mfn_to_page(entryptr->mfn));

    write_atomic(&entryptr->epte, new->epte);
}

If that sounds ok, I'm ready to submit dom0 PVH patches. Pl lmk.

thanks
Mukesh

#13 lore raw
Quoted 33 lines · Mukesh Rathor
Quoted 18 lines · On Wed, 18 Dec 2013 17:51:52 +0100 Tim Deegan <[email protected]> wrote:
Quoted 10 lines · Tim Deegan
Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

What I've ended up with is making p2m_change_entry_type_global() preemptible (which is a bigger task but will be needed as domains get bigger). Do you think that using that function to switch all mappings from p2m_foreign to p2m_invalid, appropriately late in the teardown, will be good enough for what you need?

Quoted 3 lines

Cheers,

Tim.

Finally, coming back to this, the answer is yes. Looks like all I need to do is:

xen/arch/x86/mm/p2m.c +0 -0
index 9faa663..268a8a2 100644

xen/arch/x86/mm/p2m.c void p2m_teardown(struct p2m_domain *p2m) · line 479

Quoted 10 lines
xen/arch/x86/mm/p2m.c +4 -0
@@ -470,6 +470,10 @@ void p2m_teardown(struct p2m_domain *p2m)
 
     p2m_lock(p2m);
 
+    /* pvh: we must release refcnt on all foreign pages */
+    if ( is_pvh_domain(d) )
+        p2m_change_entry_type_global(d, p2m_map_foreign, p2m_invalid);
+
     /* Try to unshare any remaining shared p2m entries. Safeguard
      * Since relinquish_shared_pages should have done the work. */ 
     for ( gfn=0; gfn < p2m->max_mapped_pfn; gfn++ )

That looks right. Sorting out how to make it restartable is on my TODO list, along with other similar code.

Quoted 4 lines

In this call, the new atomic_write_ept_entry() will DTRT:

static inline void atomic_write_ept_entry(ept_entry_t *entryptr,
const ept_entry_t *new)

Quoted 16 lines
{
    if ( p2m_is_foreign(new->sa_p2mt) )
    {
        struct page_info *page;
        struct domain *fdom;

        ASSERT(mfn_valid(new->mfn));
        page = mfn_to_page(new->mfn);
        fdom = page_get_owner(page);
        get_page(page, fdom);
    }
    if ( p2m_is_foreign(entryptr->sa_p2mt) )
        put_page(mfn_to_page(entryptr->mfn));

    write_atomic(&entryptr->epte, new->epte);
}

Yep. The write_atomic() should happen before the put_page(), so we don't need to think about race conditions (see, e.g. shadow_set_l1e() for the idiom), but otherwise that looks fine.

Cheers,

Tim.

#14 lore raw
In reply to Tim Deegan
Quoted 51 lines · Tim Deegan
Quoted 18 lines · Mukesh Rathor
Quoted 8 lines · On Tue, 17 Dec 2013 11:19:57 +0100 Tim Deegan <[email protected]> wrote:
Quoted 7 lines · Jan Beulich
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

On 17.12.13 at 02:47, Mukesh Rathor <[email protected]> wrote:

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

......

Quoted 6 lines
Quoted 6 lines

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

As to the copied bug: Should hypercall_preempt_check() return false, you'd never again try to preempt.

Further, looping from 0 to max_mapped_pfn, even preemptibly, is not a good way to do this: the guest can set its own max_mapped_pfn, and we don't want Xen to spend its time counting to 2^63.

Ok, some p2m code is setting a bad precedent.

Yes, indeed. :( As I pointed out before, and has been the subject of several XSAs.

Quoted 5 lines

An alternative might be to just create a link list then and walk it. In general, foreign mappings should be very small, so the overhead of 16 bytes per page for the link list might not be too bad. I will code it if there is no disagreement from any maintainer... everyone has different ideas :)...

I think it would be best to walk the p2m trie (i.e. bounded by amount of RAM, rather than max GFN) and do it preemptably. I'll look into something like that for the mem_sharing loop today, and foreign mapping code can reuse it.

Quoted 3 lines

Or better, if I add a count of foreign mappings and hang it off the p2m_domain, then this code would only execute in case of control domain going away...

That's a good idea as an optimisation, but it doesn't prevent the bad
case: this teardown has to execute for any domain that has a p2m and at least one foreign mapping. Since that domain chooses the GFN of the mapping, it can still cause the hypervisor to to a lot of work.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Further further, it occurs to me that the refcounting change might interact badly with nested EPT, which creates and destroys p2m tables quite regularly.

nested ept is not supported on pvh.

What, never? Or as a 'fimxe' for later? We should avoid painting ourselves into any more corners here if we can.

I think most uses of nested virt would want a fully virtualized environment to run on, wouldn't they?

-George

#15 lore raw
Quoted 9 lines · George Dunlap
Quoted 5 lines · Tim Deegan
Quoted 1 line · Mukesh Rathor

nested ept is not supported on pvh.

What, never? Or as a 'fimxe' for later? We should avoid painting ourselves into any more corners here if we can.

I think most uses of nested virt would want a fully virtualized environment to run on, wouldn't they?

If, as I hope, we eventually make PVH a special case of HVM with fewer emulated devices, that line will be blurred. And maybe a PVH guest would want to run KVM or VirtualBox?

Tim.

#16 lore raw
In reply to Jan Beulich
Quoted 11 lines · On Tue, 17 Dec 2013 08:42:32 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

On 17.12.13 at 02:47, Mukesh Rathor <[email protected]> wrote:

When a controlling domain is destroyed, any p2m_is_foreign pages must release the refcnt gotten when the page was added to the p2m.

First of all, you leave open what the relationship of this is with your 7-patch v7 series. Why is this not part of it? Obviously any necessary cleanup would logically have to come no later than where the first creation of pages of this type happens...

I was intending this for 4.5, and the series for 4.4, but now i can combine it since the series is out of 4.4.

Quoted 22 lines
Quoted 22 lines
xen/arch/x86/mm/p2m.c +22 -0
+/* This function to do any cleanup while walking the entire p2m */
+int p2m_cleanup(struct domain *d)
+{
+    int rc = 0;
+    unsigned long gfn, count = 0;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( !is_pvh_domain(d) )
+        return 0;
+
+    for ( gfn = p2m->next_foreign_gfn_to_check;
+          gfn <= p2m->max_mapped_pfn; gfn++, count++ )
+    {
+        p2m_type_t p2mt;
+        mfn_t mfn = get_gfn_query(d, gfn, &p2mt);
+
+        if ( unlikely(p2m_is_foreign(p2mt)) )
+            put_page(mfn_to_page(mfn));
+        put_gfn(d, gfn);
+        
+        /* Preempt every 10k pages, arbritary */
+        if ( count == 10000 && hypercall_preempt_check() )
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_final_teardown(struct domain *d) · line 528

Quoted 5 lines
xen/arch/x86/mm/p2m.c +5 -0
+        {
+            p2m->next_foreign_gfn_to_check = gfn + 1;
+            rc = -EAGAIN;
+            break;
+        }

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point

LOL...

Quoted 2 lines

by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Because the foreign pages are very small amount, so the loop is mostly not doing anything. ok, will reduce it to 1k.

thanks
Mukesh

#17 lore raw
Quoted 33 lines
Quoted 1 line
Quoted 1 line · Mukesh Rathor
Quoted 22 lines · On Tue, 17 Dec 2013 08:42:32 +0000 "Jan Beulich" <[email protected]> wrote:
Quoted 22 lines
xen/arch/x86/mm/p2m.c +22 -0
+/* This function to do any cleanup while walking the entire p2m */
+int p2m_cleanup(struct domain *d)
+{
+    int rc = 0;
+    unsigned long gfn, count = 0;
+    struct p2m_domain *p2m = p2m_get_hostp2m(d);
+
+    if ( !is_pvh_domain(d) )
+        return 0;
+
+    for ( gfn = p2m->next_foreign_gfn_to_check;
+          gfn <= p2m->max_mapped_pfn; gfn++, count++ )
+    {
+        p2m_type_t p2mt;
+        mfn_t mfn = get_gfn_query(d, gfn, &p2mt);
+
+        if ( unlikely(p2m_is_foreign(p2mt)) )
+            put_page(mfn_to_page(mfn));
+        put_gfn(d, gfn);
+        
+        /* Preempt every 10k pages, arbritary */
+        if ( count == 10000 && hypercall_preempt_check() )
Quoted 8 lines

xen/arch/x86/mm/p2m.c void p2m_final_teardown(struct domain *d) · line 528

Quoted 5 lines
xen/arch/x86/mm/p2m.c +5 -0
+        {
+            p2m->next_foreign_gfn_to_check = gfn + 1;
+            rc = -EAGAIN;
+            break;
+        }

So it looks like you copied one of the two obvious bugs from relinquish_shared_pages() _and_ deferred the preemption point

Quoted 1 line

LOL...

If that makes you laugh, I would imply you should have seen the (as said quite obvious) bugs and not only hyve avoided copying one of them, but also submit a fix to the original code.

You did neither, and hence I assume you didn't pay too much attention to how what you copy actually works...

Quoted 5 lines
Quoted 2 lines

by quite a bit - 10,000 pages is quite a lot, the 512 used there seems much more reasonable.

Because the foreign pages are very small amount, so the loop is mostly not doing anything. ok, will reduce it to 1k.

Right - in which case biasing just like done in the fix to the original code is probably the right approach here too.

Jan