Skip to contentSkip to conversation

xen/arm: Support XENMEM_exchange

RFC In review Julien Grall 2 participants 7 messages Dec 17, 2015 – Dec 22, 2015

lore
Share & tools
lore
#0 lore raw

cover xen/arm: Support XENMEM_exchange

Hi all,

Currently XENMEM_exchange is not supported on ARM because the steal_page is left unimplemented.

However, even if steal_page is implemented, the hypercall can't work for ARM because:
- Direct mapped domain is not supported
- ARM doesn't have a M2P and therefore usage of mfn_to_gmfn is invalid

I'm not sure how to fix properly the latter hence the RFC. mfn_to_gmfn is used in few places in the tree, however the implementation for ARM is completely wrong (see xen/include/asm-arm/mm.h):

#define mfn_to_gmfn(_d, mfn) (mfn)

All the guest but DOM0, which is direct mapped, don't have mfn == gfn.

I think implementing a M2P for ARM is not option because it will only be used in few places and we don't have much virtual address space left for ARM32. So I would rather drop the mfn_to_gmfn call from common code.

I've looked at reworking XENMEM_exchange to avoid mfn_to_gmfn. My idea would be to allocate a temporary array to store the GFN between the two loops. However, the array would be quite large (the max default is 18 on ARM), so I don't know if it's acceptable.

Anyone have a better solution?

Regards,

Cc Ian Campbell <[email protected]>
Cc Stefano Stabellini <[email protected]>
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>

Julien Grall (3):
xen/common: memory: Introduce check_range_domain_direct_mapped xen/common: memory: Add support for direct mapped domain in
XEMEM_exchange
xen/common: memory: Move steal_page in common code

 xen/arch/arm/mm.c        |   6 --
 xen/arch/x86/mm.c        |  51 -------------
 xen/common/memory.c      | 195 +++++++++++++++++++++++++++++++----------------
 xen/common/page_alloc.c  |  51 +++++++++++++
 xen/include/asm-arm/mm.h |   4 +-
 xen/include/asm-x86/mm.h |   2 -
 xen/include/xen/mm.h     |   3 +
 7 files changed, 186 insertions(+), 126 deletions(-)
Signature · 1 line
2.1.4
#1 lore raw

RFC 1/3 xen/common: memory: Introduce check_range_domain_direct_mapped

This function will be used in various place to check a given set of contiguous MFN belongs to a direct domain guest.

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>

---
 xen/common/memory.c | 62 ++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 22 deletions(-)
xen/common/memory.c +40 -22
index 9ff1145..ac707e9 100644
@@ -89,6 +89,42 @@ static unsigned int max_order(const struct domain *d)
     return min(order, MAX_ORDER + 0U);
 }
 
+/*
+ * Direct mapped domain has the property gpfn == mfn for RAM region.
+ *
+ * Check if a given contiguous set of MFN belongs to the guest.
+ */
+static bool_t check_range_domain_direct_mapped(struct domain *d,
+                                               xen_pfn_t gpfn,
+                                               unsigned int order)
+{
+    unsigned long i;
+    struct page_info *page;
+
+    ASSERT(is_domain_direct_mapped(d));
+
+    for ( i = 0; i < (1U << order); i++, gpfn++ )
+    {
+       if ( !mfn_valid(gpfn) )
+       {
+           gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_xen_pfn"\n", gpfn);
+           return 0;
+       }
+
+       page = mfn_to_page(gpfn);
+       if ( !get_page(page, d) )
+       {
+           gdprintk(XENLOG_INFO,
+                    "mfn %#"PRI_xen_pfn" doesn't belong to d%d\n", gpfn,
+                    d->domain_id);
+           return 0;
+        }
+        put_page(page);
+    }
+
+    return 1;
+}
+
 static void increase_reservation(struct memop_args *a)
 {
     struct page_info *page;
@@ -171,30 +207,12 @@ static void populate_physmap(struct memop_args *a)
         {
             if ( is_domain_direct_mapped(d) )
             {
-                mfn = gpfn;
-
-                for ( j = 0; j < (1U << a->extent_order); j++, mfn++ )
-                {
-                    if ( !mfn_valid(mfn) )
-                    {
-                        gdprintk(XENLOG_INFO, "Invalid mfn %#"PRI_xen_pfn"\n",
-                                 mfn);
-                        goto out;
-                    }
-
-                    page = mfn_to_page(mfn);
-                    if ( !get_page(page, d) )
-                    {
-                        gdprintk(XENLOG_INFO,
-                                 "mfn %#"PRI_xen_pfn" doesn't belong to d%d\n",
-                                  mfn, d->domain_id);
-                        goto out;
-                    }
-                    put_page(page);
-                }
+                if ( !check_range_domain_direct_mapped(d, gpfn,
+                                                       a->extent_order) )
+                    goto out;
 
+                /* For direct mapped domain mfn == gpfn */
                 mfn = gpfn;
-                page = mfn_to_page(mfn);
             }
             else
             {
Signature · 1 line
2.1.4
#2 lore raw

RFC 2/3 xen/common: memory: Add support for direct mapped domain in XEMEM_exchange

In reply to Julien Grall

Direct mapped domain needs to retrieve the exact same underlying physical page when the region is re-populated.

Therefore when memory is exchanged for direct mapped domain, we don't want to free memory of the previous region neither allocate new memory.

Note that because of that, the hypercall XENMEM_exchange can only work on memory region that has been populated with real RAM when the domain has been created.

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Tim Deegan <[email protected]>

---
 xen/common/memory.c | 133 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 90 insertions(+), 43 deletions(-)
xen/common/memory.c +90 -43
index ac707e9..94c9a78 100644
@@ -517,10 +517,19 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
 
                 page = mfn_to_page(mfn);
 
-                if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
+                if ( is_domain_direct_mapped(d) )
                 {
-                    put_gfn(d, gmfn + k);
+                    if ( !get_page(page, d) )
+                        rc = -EINVAL;
+                    else
+                        put_page(page);
+                }
+                else if ( unlikely(steal_page(d, page, MEMF_no_refcount)) )
                     rc = -EINVAL;
+
+                if ( unlikely(rc) )
+                {
+                    put_gfn(d, gmfn + k);
                     goto fail;
                 }
 
@@ -530,17 +539,20 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
         }
 
         /* Allocate a chunk's worth of anonymous output pages. */
-        for ( j = 0; j < (1UL << out_chunk_order); j++ )
+        if ( !is_domain_direct_mapped(d) )
         {
-            page = alloc_domheap_pages(d, exch.out.extent_order,
-                                       MEMF_no_owner | memflags);
-            if ( unlikely(page == NULL) )
+            for ( j = 0; j < (1UL << out_chunk_order); j++ )
             {
-                rc = -ENOMEM;
-                goto fail;
-            }
+                page = alloc_domheap_pages(d, exch.out.extent_order,
+                                           MEMF_no_owner | memflags);
+                if ( unlikely(page == NULL) )
+                {
+                    rc = -ENOMEM;
+                    goto fail;
+                }
 
-            page_list_add(page, &out_chunk_list);
+                page_list_add(page, &out_chunk_list);
+            }
         }
 
         /*
@@ -552,47 +564,26 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
         {
             unsigned long gfn;
 
-            if ( !test_and_clear_bit(_PGC_allocated, &page->count_info) )
+            if ( !is_domain_direct_mapped(d) &&
+                 !test_and_clear_bit(_PGC_allocated, &page->count_info) )
                 BUG();
             mfn = page_to_mfn(page);
             gfn = mfn_to_gmfn(d, mfn);
             /* Pages were unshared above */
             BUG_ON(SHARED_M2P(gfn));
             guest_physmap_remove_page(d, gfn, mfn, 0);
-            put_page(page);
+
+            /*
+             * For domain direct mapped, we want to be able to get
+             * the same page later, so don't deallocate it
+             */
+            if ( !is_domain_direct_mapped(d) )
+                put_page(page);
         }
 
         /* Assign each output page to the domain. */
-        for ( j = 0; (page = page_list_remove_head(&out_chunk_list)); ++j )
+        for ( j = 0; j < (1UL << out_chunk_order); j++ )
         {
-            if ( assign_pages(d, page, exch.out.extent_order,
-                              MEMF_no_refcount) )
-            {
-                unsigned long dec_count;
-                bool_t drop_dom_ref;
-
-                /*
-                 * Pages in in_chunk_list is stolen without
-                 * decreasing the tot_pages. If the domain is dying when
-                 * assign pages, we need decrease the count. For those pages
-                 * that has been assigned, it should be covered by
-                 * domain_relinquish_resources().
-                 */
-                dec_count = (((1UL << exch.in.extent_order) *
-                              (1UL << in_chunk_order)) -
-                             (j * (1UL << exch.out.extent_order)));
-
-                spin_lock(&d->page_alloc_lock);
-                drop_dom_ref = (dec_count &&
-                                !domain_adjust_tot_pages(d, -dec_count));
-                spin_unlock(&d->page_alloc_lock);
-
-                if ( drop_dom_ref )
-                    put_domain(d);
-
-                free_domheap_pages(page, exch.out.extent_order);
-                goto dying;
-            }
 
             if ( __copy_from_guest_offset(&gpfn, exch.out.extent_start,
                                           (i << out_chunk_order) + j, 1) )
@@ -601,7 +592,61 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
                 continue;
             }
 
-            mfn = page_to_mfn(page);
+            if ( is_domain_direct_mapped(d) )
+            {
+                if ( unlikely(d->is_dying) )
+                {
+                    gdprintk(XENLOG_INFO,
+                             "Cannot assign page to domain %d -- dying.\n",
+                             d->domain_id);
+                    goto dying;
+                }
+
+                if ( !check_range_domain_direct_mapped(d, gpfn,
+                                                       exch.out.extent_order) )
+                    goto dying;
+
+                mfn = gpfn;
+            }
+            else
+            {
+                page = page_list_remove_head(&out_chunk_list);
+
+                /* The outchunk list should always contain enough page */
+                BUG_ON(!page);
+
+                if ( assign_pages(d, page, exch.out.extent_order,
+                                  MEMF_no_refcount) )
+                {
+                    unsigned long dec_count;
+                    bool_t drop_dom_ref;
+
+                    /*
+                     * Pages in in_chunk_list is stolen without
+                     * decreasing the tot_pages. If the domain is dying when
+                     * assign pages, we need decrease the count. For those pages
+                     * that has been assigned, it should be covered by
+                     * domain_relinquish_resources().
+                     */
+                    dec_count = (((1UL << exch.in.extent_order) *
+                                  (1UL << in_chunk_order)) -
+                                 (j * (1UL << exch.out.extent_order)));
+
+                    spin_lock(&d->page_alloc_lock);
+                    drop_dom_ref = (dec_count &&
+                                    !domain_adjust_tot_pages(d, -dec_count));
+                    spin_unlock(&d->page_alloc_lock);
+
+                    if ( drop_dom_ref )
+                        put_domain(d);
+
+                    free_domheap_pages(page, exch.out.extent_order);
+                    goto dying;
+                }
+
+                mfn = page_to_mfn(page);
+            }
+
             guest_physmap_add_page(d, gpfn, mfn, exch.out.extent_order);
 
             if ( !paging_mode_translate(d) )
@@ -630,7 +675,8 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
  fail:
     /* Reassign any input pages we managed to steal. */
     while ( (page = page_list_remove_head(&in_chunk_list)) )
-        if ( assign_pages(d, page, 0, MEMF_no_refcount) )
+        if ( is_domain_direct_mapped(d) &&
+             assign_pages(d, page, 0, MEMF_no_refcount) )
         {
             BUG_ON(!d->is_dying);
             if ( test_and_clear_bit(_PGC_allocated, &page->count_info) )
@@ -640,6 +686,7 @@ static long memory_exchange(XEN_GUEST_HANDLE_PARAM(xen_memory_exchange_t) arg)
  dying:
     rcu_unlock_domain(d);
     /* Free any output pages we managed to allocate. */
+    BUG_ON(is_domain_direct_mapped(d) && !page_list_empty(&out_chunk_list));
     while ( (page = page_list_remove_head(&out_chunk_list)) )
         free_domheap_pages(page, exch.out.extent_order);
 
Signature · 1 line
2.1.4
#3 lore raw

Re: [RFC 2/3] xen/common: memory: Add support for direct mapped domain in XEMEM_exchange

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 17:31, <[email protected]> wrote:

Direct mapped domain needs to retrieve the exact same underlying physical page when the region is re-populated.

In which case - what sense does it make for such a domain to exchange memory (the primary purpose of which is to get _different_ memory populated at the given GFN (range))?

Jan

#4 lore raw

RFC 3/3 xen/common: memory: Move steal_page in common code

In reply to Julien Grall

The implementation of steal_page for ARM and x86 would be the same. Rather than duplicating the x86 function, move it in common code.

Also introduce missing define PRtype_info for ARM in order to compile the code.

---
Cc: Ian Campbell <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Keir Fraser <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

Note that this patch *shouldn't* be applied before the rest of the series. This is because the callers of this function are not fully fixed for ARM and could possibly introduce security issue. steal_page was always returning an error which lead the rest of the code to not be executed for ARM.

TODO:
- Check that all the callers of steal_page are properly working for ARM.

---
 xen/arch/arm/mm.c        |  6 ------
 xen/arch/x86/mm.c        | 51 ------------------------------------------------
 xen/common/page_alloc.c  | 51 ++++++++++++++++++++++++++++++++++++++++++++++++
 xen/include/asm-arm/mm.h |  4 ++--
 xen/include/asm-x86/mm.h |  2 --
 xen/include/xen/mm.h     |  3 +++
 6 files changed, 56 insertions(+), 61 deletions(-)
xen/arch/arm/mm.c +0 -6
index 47bfb27..b2e6682 100644
@@ -989,12 +989,6 @@ int donate_page(struct domain *d, struct page_info *page, unsigned int memflags)
     return -ENOSYS;
 }
 
-int steal_page(
-    struct domain *d, struct page_info *page, unsigned int memflags)
-{
-    return -1;
-}
-
 int page_is_ram_type(unsigned long mfn, unsigned long mem_type)
 {
     ASSERT(0);
xen/arch/x86/mm.c +0 -51

57 lines not on this page · raw message

xen/common/page_alloc.c +51 -0

57 lines not on this page · raw message

xen/include/asm-arm/mm.h +2 -2
index 2e9d0b2..d212d39 100644
@@ -73,6 +73,8 @@ struct page_info
     u64 pad;
 };
 
+#define PRtype_info "016lx"/* should only be used for printk's */
+
 #define PG_shift(idx)   (BITS_PER_LONG - (idx))
 #define PG_mask(x, idx) (x ## UL << PG_shift(idx))
 
@@ -319,8 +321,6 @@ static inline int relinquish_shared_pages(struct domain *d)
 /* Arch-specific portion of memory_op hypercall. */
 long arch_memory_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg);
 
-int steal_page(
-    struct domain *d, struct page_info *page, unsigned int memflags);
 int donate_page(
     struct domain *d, struct page_info *page, unsigned int memflags);
 
xen/include/asm-x86/mm.h +0 -2
index 67b34c6..769552e 100644
@@ -531,8 +531,6 @@ long subarch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
 int compat_arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void));
 int compat_subarch_memory_op(int op, XEN_GUEST_HANDLE_PARAM(void));
 
-int steal_page(
-    struct domain *d, struct page_info *page, unsigned int memflags);
 int donate_page(
     struct domain *d, struct page_info *page, unsigned int memflags);
 
xen/include/xen/mm.h +3 -0
index 5d4b64b..4fafd88 100644
@@ -139,6 +139,9 @@ int assign_pages(
     unsigned int order,
     unsigned int memflags);
 
+int steal_page(
+    struct domain *d, struct page_info *page, unsigned int memflags);
+
 /* Dump info to serial console */
 void arch_dump_shared_mem_info(void);
 
Signature · 1 line
2.1.4
#5 lore raw

Re: [RFC 3/3] xen/common: memory: Move steal_page in common code

xen/common/page_alloc.c int assign_pages( · line 1791

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 17:31, <[email protected]> wrote:
xen/common/page_alloc.c +3 -0
@@ -1786,6 +1786,57 @@ int assign_pages(
     return -1;
 }
 
+int steal_page(
+    struct domain *d, struct page_info *page, unsigned int memflags)
+{

I think it would better go into common/memory.c - there's no allocation involved here.

Jan

#6 lore raw
In reply to Julien Grall
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.12.15 at 17:31, <[email protected]> wrote:

I've looked at reworking XENMEM_exchange to avoid mfn_to_gmfn. My idea would be to allocate a temporary array to store the GFN between the two loops. However, the array would be quite large (the max default is 18 on ARM), so I don't know if it's acceptable.

This would quite clearly not be acceptable, yet I don't have an alternative suggestion other then implementing M2P on ARM, which you have already named as not an option.

Otoh I'm not sure what two loops you talk about - I find exactly one use of mfn_to_gmfn() in memory_exchange(), and latching the GFN earlier would be wrong (as it might change in parallel to your processing).

Jan