Skip to contentSkip to conversation

xen/grant_table: introduce GNTTABOP_unmap_and_duplicate

PATCH In review Roger Pau Monne 2 participants 2 messages Nov 4, 2013 – Nov 5, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/grant_table: introduce GNTTABOP_unmap_and_duplicate

GNTTABOP_unmap_and_replace has two issues:
- it unconditionally replaces the mapping passed in new_addr with 0;
- it doesn't support GNTMAP_contains_pte mappings on x86, returning a general error instead of some form of ENOSYS.

Introduce a new operation called GNTTABOP_unmap_and_duplicate that behaves like GNTTABOP_unmap_and_replace without zeroing the mapping passed in new_addr. Also return GNTST_enosys instead of GNTST_general_error if GNTMAP_contains_pte is specified.

---
 xen/arch/arm/mm.c                 |    2 +-
 xen/arch/x86/mm.c                 |   36 ++++++++++++----
 xen/common/compat/grant_table.c   |    8 ++++
 xen/common/grant_table.c          |   83 ++++++++++++++++++++++++++++++++++++-
 xen/include/asm-arm/grant_table.h |    2 +-
 xen/include/asm-x86/grant_table.h |    3 +-
 xen/include/public/grant_table.h  |   23 ++++++++++
 xen/include/xlat.lst              |    1 +
 8 files changed, 145 insertions(+), 13 deletions(-)
xen/arch/arm/mm.c +1 -1
index 123280e..39e6071 100644
@@ -1289,7 +1289,7 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
 }
 
 int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
-        unsigned long new_addr, unsigned int flags)
+        unsigned long new_addr, unsigned int flags, bool_t zero_new_addr)
 {
     unsigned long gfn = (unsigned long)(addr >> PAGE_SHIFT);
     struct domain *d = current->domain;
xen/arch/x86/mm.c +27 -9
index 43aaceb..89e1b43 100644
@@ -4047,7 +4047,8 @@ static int replace_grant_p2m_mapping(
 }
 
 int replace_grant_host_mapping(
-    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags)
+    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags,
+    bool_t zero_new_addr)
 {
     struct vcpu *curr = current;
     l1_pgentry_t *pl1e, ol1e;
@@ -4064,7 +4065,7 @@ int replace_grant_host_mapping(
             return destroy_grant_pte_mapping(addr, frame, curr->domain);
         
         MEM_LOG("Unsupported grant table operation");
-        return GNTST_general_error;
+        return GNTST_enosys;
     }
 
     if ( !new_addr )
@@ -4102,14 +4103,31 @@ int replace_grant_host_mapping(
 
     ol1e = *pl1e;
 
-    if ( unlikely(!UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(),
-                                gl1mfn, curr, 0)) )
+    if ( zero_new_addr )
     {
-        page_unlock(l1pg);
-        put_page(l1pg);
-        MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
-        guest_unmap_l1e(curr, pl1e);
-        return GNTST_general_error;
+        if ( unlikely(!UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(),
+                                    gl1mfn, curr, 0)) )
+        {
+            page_unlock(l1pg);
+            put_page(l1pg);
+            MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
+            guest_unmap_l1e(curr, pl1e);
+            return GNTST_general_error;
+        }
+    }
+    else
+    {
+        /* Increase refcount */
+        if ( unlikely(!get_page_and_type(l1e_get_page(ol1e), curr->domain,
+                                         PGT_writable_page)) )
+        {
+            page_unlock(l1pg);
+            put_page(l1pg);
+            guest_unmap_l1e(curr, pl1e);
+            MEM_LOG("Cannot increase refcount of frame at %"PRI_mfn,
+                    page_to_mfn(l1e_get_page(ol1e)));
+            return GNTST_general_error;
+        }
     }
 
     page_unlock(l1pg);
xen/common/compat/grant_table.c +8 -0
index 7ebbbc1..8ab5e78 100644
@@ -51,6 +51,10 @@ CHECK_gnttab_get_version;
 CHECK_gnttab_swap_grant_ref;
 #undef xen_gnttab_swap_grant_ref
 
+#define xen_gnttab_unmap_and_duplicate gnttab_unmap_and_duplicate
+CHECK_gnttab_unmap_and_duplicate;
+#undef xen_gnttab_unmap_and_duplicate
+
 int compat_grant_table_op(unsigned int cmd,
                           XEN_GUEST_HANDLE_PARAM(void) cmp_uop,
                           unsigned int count)
@@ -106,6 +110,10 @@ int compat_grant_table_op(unsigned int cmd,
     CASE(swap_grant_ref);
 #endif
 
+#ifndef CHECK_gnttab_unmap_and_duplicate
+    CASE(unmap_and_duplicate);
+#endif
+
 #undef CASE
     default:
         return do_grant_table_op(cmd, cmp_uop, count);
xen/common/grant_table.c +82 -1

113 lines not on this page · raw message

xen/include/asm-arm/grant_table.h +1 -1
index 6e0cc59..0410355 100644
@@ -12,7 +12,7 @@ int create_grant_host_mapping(unsigned long gpaddr,
         cache_flags);
 #define gnttab_host_mapping_get_page_type(op, d, rd) (0)
 int replace_grant_host_mapping(unsigned long gpaddr, unsigned long mfn,
-        unsigned long new_gpaddr, unsigned int flags);
+        unsigned long new_gpaddr, unsigned int flags, bool_t zero_new_addr);
 void gnttab_mark_dirty(struct domain *d, unsigned long l);
 #define gnttab_create_status_page(d, t, i) do {} while (0)
 #define gnttab_status_gmfn(d, t, i) (0)
xen/include/asm-x86/grant_table.h +2 -1
index 3013869..e985968 100644
@@ -16,7 +16,8 @@
 int create_grant_host_mapping(uint64_t addr, unsigned long frame,
 			      unsigned int flags, unsigned int cache_flags);
 int replace_grant_host_mapping(
-    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags);
+    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags,
+    bool_t zero_new_addr);
 
 #define gnttab_create_shared_page(d, t, i)                               \
     do {                                                                 \
xen/include/public/grant_table.h +23 -0
index b8a3d6c..a4ad98d 100644
@@ -309,6 +309,7 @@ typedef uint16_t grant_status_t;
 #define GNTTABOP_get_status_frames    9
 #define GNTTABOP_get_version          10
 #define GNTTABOP_swap_grant_ref	      11
+#define GNTTABOP_unmap_and_duplicate  12
 #endif /* __XEN_INTERFACE_VERSION__ */
 /* ` } */
 
@@ -574,6 +575,27 @@ struct gnttab_swap_grant_ref {
 typedef struct gnttab_swap_grant_ref gnttab_swap_grant_ref_t;
 DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t);
 
+/*
+ * GNTTABOP_unmap_and_duplicate: Destroy one or more grant-reference mappings
+ * tracked by <handle> but atomically replace the page table entry with one
+ * pointing to the machine address under <new_addr>.
+ * NOTES:
+ *  1. The call may fail in an undefined manner if either mapping is not
+ *     tracked by <handle>.
+ *  2. After executing a batch of unmaps, it is guaranteed that no stale
+ *     mappings will remain in the device or host TLBs.
+ */
+struct gnttab_unmap_and_duplicate {
+    /* IN parameters. */
+    uint64_t host_addr;
+    uint64_t new_addr;
+    grant_handle_t handle;
+    /* OUT parameters. */
+    int16_t  status;              /* => enum grant_status */
+};
+typedef struct gnttab_unmap_and_duplicate gnttab_unmap_and_duplicate_t;
+DEFINE_XEN_GUEST_HANDLE(gnttab_unmap_and_duplicate_t);
+
 #endif /* __XEN_INTERFACE_VERSION__ */
 
 /*
@@ -631,6 +653,7 @@ DEFINE_XEN_GUEST_HANDLE(gnttab_swap_grant_ref_t);
 #define GNTST_bad_copy_arg    (-10) /* copy arguments cross page boundary.   */
 #define GNTST_address_too_big (-11) /* transfer page address too large.      */
 #define GNTST_eagain          (-12) /* Operation not done; try again.        */
+#define GNTST_enosys          (-13) /* Operation not implemented.            */
 /* ` } */
 
 #define GNTTABOP_error_msgs {                   \
xen/include/xlat.lst +1 -0
index f00cef3..8b967f1 100644
@@ -51,6 +51,7 @@
 ?       grant_entry_header              grant_table.h
 ?	grant_entry_v2			grant_table.h
 ?	gnttab_swap_grant_ref		grant_table.h
+?	gnttab_unmap_and_duplicate	grant_table.h
 ?	kexec_exec			kexec.h
 !	kexec_image			kexec.h
 !	kexec_range			kexec.h
Signature · 7 lines
1.7.7.5 (Apple Git-26)


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

xen/arch/x86/mm.c static int replace_grant_p2m_mapping( · line 4051

Quoted 5 lines
Quoted 1 line
Quoted 1 line · Roger Pau Monne

int replace_grant_host_mapping(

xen/arch/x86/mm.c +2 -1
-    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags)
+    uint64_t addr, unsigned long frame, uint64_t new_addr, unsigned int flags,
+    bool_t zero_new_addr)

So why can this flag not be merged into "flags"?

Quoted 22 lines
xen/arch/x86/mm.c +14 -7
-    if ( unlikely(!UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(),
-                                gl1mfn, curr, 0)) )
+    if ( zero_new_addr )
     {
-        page_unlock(l1pg);
-        put_page(l1pg);
-        MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
-        guest_unmap_l1e(curr, pl1e);
-        return GNTST_general_error;
+        if ( unlikely(!UPDATE_ENTRY(l1, pl1e, ol1e, l1e_empty(),
+                                    gl1mfn, curr, 0)) )
+        {
+            page_unlock(l1pg);
+            put_page(l1pg);
+            MEM_LOG("Cannot delete PTE entry at %p", (unsigned long *)pl1e);
+            guest_unmap_l1e(curr, pl1e);
+            return GNTST_general_error;
+        }
+    }
+    else
+    {
+        /* Increase refcount */

xen/arch/x86/mm.c int replace_grant_host_mapping( · line 4130

Quoted 10 lines
xen/arch/x86/mm.c +10 -0
+        if ( unlikely(!get_page_and_type(l1e_get_page(ol1e), curr->domain,
+                                         PGT_writable_page)) )
+        {
+            page_unlock(l1pg);
+            put_page(l1pg);
+            guest_unmap_l1e(curr, pl1e);
+            MEM_LOG("Cannot increase refcount of frame at %"PRI_mfn,
+                    page_to_mfn(l1e_get_page(ol1e)));
+            return GNTST_general_error;
+        }

The two error paths are identical except for the messages they log. Please avoid unnecessary code duplication like this. That'll also reduce the churn the patch causes due to the indentation change. (Moving the guest_unmap_l1e() ahead of the issuing of the message is, btw, appreciated.)

Quoted 8 lines
xen/include/public/grant_table.h +8 -0
+/*
+ * GNTTABOP_unmap_and_duplicate: Destroy one or more grant-reference mappings
+ * tracked by <handle> but atomically replace the page table entry with one
+ * pointing to the machine address under <new_addr>.
+ * NOTES:
+ *  1. The call may fail in an undefined manner if either mapping is not
+ *     tracked by <handle>.
+ *  2. After executing a batch of unmaps, it is guaranteed that no stale

xen/include/public/grant_table.h struct gnttab_swap_grant_ref { · line 595

Quoted 10 lines
xen/include/public/grant_table.h +10 -0
+ *     mappings will remain in the device or host TLBs.
+ */
+struct gnttab_unmap_and_duplicate {
+    /* IN parameters. */
+    uint64_t host_addr;
+    uint64_t new_addr;
+    grant_handle_t handle;
+    /* OUT parameters. */
+    int16_t  status;              /* => enum grant_status */
+};

If you re-used the identical in layout gnttab_unmap_and_replace here (perhaps nevertheless creating aliases with the right name), wouldn't it be possible to also reduce the amount of added code duplication in xen/common/grant_table.c?

Jan