This patch allows grant table frames to be mapped using the
XENMEM_acquire_resource memory op.

Signed-off-by: Paul Durrant <paul.durr...@citrix.com>
---
Cc: Andrew Cooper <andrew.coop...@citrix.com>
Cc: George Dunlap <george.dun...@eu.citrix.com>
Cc: Ian Jackson <ian.jack...@eu.citrix.com>
Cc: Jan Beulich <jbeul...@suse.com>
Cc: Konrad Rzeszutek Wilk <konrad.w...@oracle.com>
Cc: Stefano Stabellini <sstabell...@kernel.org>
Cc: Tim Deegan <t...@xen.org>
Cc: Wei Liu <wei.l...@citrix.com>

v8:
 - The functionality was originally incorporated into the earlier patch
   "x86/mm: add HYPERVISOR_memory_op to acquire guest resources".
---
 xen/common/grant_table.c      | 32 ++++++++++++++++++++++++++++----
 xen/common/memory.c           | 30 ++++++++++++++++++++++++++++++
 xen/include/public/memory.h   |  5 +++++
 xen/include/xen/grant_table.h |  1 +
 4 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/xen/common/grant_table.c b/xen/common/grant_table.c
index 71706f5cba..0530cf63c9 100644
--- a/xen/common/grant_table.c
+++ b/xen/common/grant_table.c
@@ -3720,14 +3720,13 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, 
grant_ref_t ref,
 }
 #endif
 
-int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
-                     mfn_t *mfn)
+/* Caller must hold write lock as version may change and table may grow */
+static int gnttab_get_frame_locked(struct domain *d, unsigned long idx,
+                                   mfn_t *mfn)
 {
     int rc = 0;
     struct grant_table *gt = d->grant_table;
 
-    grant_write_lock(gt);
-
     if ( gt->gt_version == 0 )
         gt->gt_version = 1;
 
@@ -3751,6 +3750,19 @@ int gnttab_map_frame(struct domain *d, unsigned long 
idx, gfn_t gfn,
             rc = -EINVAL;
     }
 
+    return rc;
+}
+
+int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
+                     mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_write_lock(gt);
+
+    rc = gnttab_get_frame_locked(d, idx, mfn);
+
     if ( !rc )
         gnttab_set_frame_gfn(gt, idx, gfn);
 
@@ -3759,6 +3771,18 @@ int gnttab_map_frame(struct domain *d, unsigned long 
idx, gfn_t gfn,
     return rc;
 }
 
+int gnttab_get_frame(struct domain *d, unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_write_lock(gt); /* write lock is required as table may grow */
+    rc = gnttab_get_frame_locked(d, idx, mfn);
+    grant_write_unlock(gt);
+
+    return rc;
+}
+
 static void gnttab_usage_print(struct domain *rd)
 {
     int first = 1;
diff --git a/xen/common/memory.c b/xen/common/memory.c
index a068877cf6..fb14f79218 100644
--- a/xen/common/memory.c
+++ b/xen/common/memory.c
@@ -23,6 +23,7 @@
 #include <xen/numa.h>
 #include <xen/mem_access.h>
 #include <xen/trace.h>
+#include <xen/grant_table.h>
 #include <asm/current.h>
 #include <asm/hardirq.h>
 #include <asm/p2m.h>
@@ -965,6 +966,30 @@ static long xatp_permission_check(struct domain *d, 
unsigned int space)
 }
 
 #ifdef CONFIG_X86
+static int acquire_grant_table(struct domain *d, unsigned int id,
+                               unsigned long frame,
+                               unsigned long nr_frames,
+                               unsigned long mfn_list[])
+{
+    unsigned int i = nr_frames;
+
+    if ( id != 0 )
+        return -EINVAL;
+
+    while ( i-- != 0 )
+    {
+        mfn_t mfn = INVALID_MFN;
+        int rc = gnttab_get_frame(d, frame + i, &mfn);
+
+        if ( rc )
+            return rc;
+
+        mfn_list[i] = mfn_x(mfn);
+    }
+
+    return 0;
+}
+
 static int acquire_resource(const xen_mem_acquire_resource_t *xmar)
 {
     struct domain *d, *currd = current->domain;
@@ -992,6 +1017,11 @@ static int acquire_resource(const 
xen_mem_acquire_resource_t *xmar)
                                          xmar->nr_frames, mfn_list);
         break;
 
+    case XENMEM_resource_grant_table:
+        rc = acquire_grant_table(d, xmar->id, xmar->frame,
+                                 xmar->nr_frames, mfn_list);
+        break;
+
     default:
         rc = -EOPNOTSUPP;
         break;
diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h
index e30a4d9794..2099b09cf5 100644
--- a/xen/include/public/memory.h
+++ b/xen/include/public/memory.h
@@ -611,6 +611,7 @@ struct xen_mem_acquire_resource {
     uint16_t type;
 
 #define XENMEM_resource_ioreq_server 0
+#define XENMEM_resource_grant_table 1
 
     /*
      * IN - a type-specific resource identifier, which must be zero
@@ -628,6 +629,10 @@ struct xen_mem_acquire_resource {
      *                                                       page
      *                                         frame == 1 -> ioreq
      *                                                       page
+     * type == XENMEM_resource_grant_table -> frame has same semantics
+     *                                        as idx passed to
+     *                                        XENMEM_add_to_physmap for
+     *                                        XENMAPSPACE_grant_table.
      */
     uint64_aligned_t frame;
     /* IN/OUT - If the tools domain is PV then, upon return, gmfn_list
diff --git a/xen/include/xen/grant_table.h b/xen/include/xen/grant_table.h
index d2bd2416c4..15a83df215 100644
--- a/xen/include/xen/grant_table.h
+++ b/xen/include/xen/grant_table.h
@@ -58,5 +58,6 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, 
grant_ref_t ref,
 
 int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
                      mfn_t *mfn);
+int gnttab_get_frame(struct domain *d, unsigned long idx, mfn_t *mfn);
 
 #endif /* __XEN_GRANT_TABLE_H__ */
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

Reply via email to