Skip to contentSkip to conversation

guest resource mapping (reprise)

PATCH Superseded Paul Durrant 2 participants 10 messages Aug 8, 2018 – Aug 9, 2018 Arm/actions semi architecture

v22 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#0 lore raw

v22 cover guest resource mapping (reprise)

These patches are the patches from my original resource mapping series that did not make it into 4.11.

Paul Durrant (2):
common: add a new mappable resource type: XENMEM_resource_grant_table tools/libxenctrl: use new xenforeignmemory API to seed grant table

 tools/libxc/include/xc_dom.h        |  12 +--
 tools/libxc/xc_dom_boot.c           | 166 +++++++++++++++++++++++-------------
 tools/libxc/xc_sr_restore_x86_hvm.c |  10 +--
 tools/libxc/xc_sr_restore_x86_pv.c  |   2 +-
 tools/libxl/libxl_dom.c             |   1 -
 tools/python/xen/lowlevel/xc/xc.c   |   6 +-
 xen/common/grant_table.c            | 115 ++++++++++++++++++++++---
 xen/common/memory.c                 |  56 +++++++++++-
 xen/include/public/memory.h         |   6 ++
 xen/include/xen/grant_table.h       |   4 +
 10 files changed, 286 insertions(+), 92 deletions(-)
Signature · 7 lines
2.11.0


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

PATCH v22 1/2 common: add a new mappable resource type: XENMEM_resource_grant_table

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

NOTE: This patch expands the on-stack mfn_list array in acquire_resource()
but it is still small enough to remain on-stack.

NOTE: This patch also removes a bogus comment above the
grant_to_status_frames() function.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

v22:
- Remove bogus comment and update commit message accordingly.
- Add ASSERTion that an invalid MFN is not passed back to the caller of XENMEM_acquire_resource.
- Re-code the idx to nr calculation to try to make it more obvious and add explicit overflow checks.

v21:
- Prevent PVH/HVM tools domains from mapping any non-caller-owned resource unless the tools domain is also the hardware domain.
- Grow the grant table appropriately whether it is a shared frame or a status frame that is being mapped.
- Fix comment style breakage in memory.h.
- Move implicit version setting to gnttab_get_shared_frame().

v19:
- Add test to prevent PVH/HVM tools domains mapping grant status frames this way as the mapping infrastructure in Xen does not yet implement the necessary reference counting to make this safe.
- Make sure grant table version is set before any attempt to grow the table.

v18:
- Non-trivial re-base of grant table code.
- Dropped Jan's R-b because of the grant table changes.

v13:
- Re-work the internals to avoid using the XENMAPIDX_grant_table_status hack.

v12:
- Dropped limit checks as requested by Jan.

v10:
- Addressed comments from Jan.

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      | 115 +++++++++++++++++++++++++++++++++++++-----
 xen/common/memory.c           |  56 +++++++++++++++++++-
 xen/include/public/memory.h   |   6 +++
 xen/include/xen/grant_table.h |   4 ++
 4 files changed, 166 insertions(+), 15 deletions(-)
xen/common/grant_table.c +101 -14
index d9ec711c73..9aab808d44 100644
@@ -352,12 +352,17 @@ static inline void active_entry_release(struct active_grant_entry *act)
 
 #define GRANT_STATUS_PER_PAGE (PAGE_SIZE / sizeof(grant_status_t))
 #define GRANT_PER_PAGE (PAGE_SIZE / sizeof(grant_entry_v2_t))
-/* Number of grant table status entries. Caller must hold d's gr. table lock.*/
+
 static inline unsigned int grant_to_status_frames(unsigned int grant_frames)
 {
     return DIV_ROUND_UP(grant_frames * GRANT_PER_PAGE, GRANT_STATUS_PER_PAGE);
 }
 
+static inline unsigned int status_to_grant_frames(unsigned int status_frames)
+{
+    return DIV_ROUND_UP(status_frames * GRANT_STATUS_PER_PAGE, GRANT_PER_PAGE);
+}
+
 /* Check if the page has been paged out, or needs unsharing.
    If rc == GNTST_okay, *page contains the page struct with a ref taken.
    Caller must do put_page(*page).
@@ -3860,6 +3865,66 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 }
 #endif
 
+/* caller must hold write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version == 2);
+
+    if ( idx >= nr_status_frames(gt) )
+    {
+        unsigned long nr_status;
+        unsigned long nr_grant;
+
+        nr_status = idx + 1; /* sufficient frames to make idx valid */
+        nr_grant = status_to_grant_frames(nr_status);
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;
+
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);
+
+        /* check whether gnttab_grow_table() succeeded */
+        if ( idx >= nr_status_frames(gt) )
+            return -EINVAL;
+    }
+
+    *mfn = _mfn(virt_to_mfn(gt->status[idx]));
+    return 0;
+}
+
+/* caller must hold write lock */
+static int gnttab_get_shared_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version != 0);
+
+    if ( idx >= nr_grant_frames(gt) )
+    {
+        unsigned long nr_grant;
+
+        nr_grant = idx + 1; /* sufficient frames to make idx valid */
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;
+
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);
+
+        /* check whether gnttab_grow_table() succeeded */
+        if ( idx >= nr_grant_frames(gt) )
+            return -EINVAL;
+    }
+
+    *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
+    return 0;
+}
+
 int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
                      mfn_t *mfn)
 {
@@ -3877,21 +3942,11 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
     {
         idx &= ~XENMAPIDX_grant_table_status;
         status = true;
-        if ( idx < nr_status_frames(gt) )
-            *mfn = _mfn(virt_to_mfn(gt->status[idx]));
-        else
-            rc = -EINVAL;
-    }
-    else
-    {
-        if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
-            gnttab_grow_table(d, idx + 1);
 
-        if ( idx < nr_grant_frames(gt) )
-            *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
-        else
-            rc = -EINVAL;
+        rc = gnttab_get_status_frame_mfn(d, idx, mfn);
     }
+    else
+        rc = gnttab_get_shared_frame_mfn(d, idx, mfn);
 
     if ( !rc && paging_mode_translate(d) &&
          !gfn_eq(gnttab_get_frame_gfn(gt, status, idx), INVALID_GFN) )
@@ -3906,6 +3961,38 @@ int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn,
     return rc;
 }
 
+int gnttab_get_shared_frame(struct domain *d, unsigned long idx,
+                            mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_write_lock(gt);
+
+    if ( gt->gt_version == 0 )
+        gt->gt_version = 1;
+
+    rc = gnttab_get_shared_frame_mfn(d, idx, mfn);
+
+    grant_write_unlock(gt);
+
+    return rc;
+}
+
+int gnttab_get_status_frame(struct domain *d, unsigned long idx,
+                            mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_write_lock(gt);
+    rc = (gt->gt_version == 2) ?
+        gnttab_get_status_frame_mfn(d, idx, mfn) : -EINVAL;
+    grant_write_unlock(gt);
+
+    return rc;
+}
+
 static void gnttab_usage_print(struct domain *rd)
 {
     int first = 1;
xen/common/memory.c +55 -1

86 lines not on this page · raw message

xen/include/public/memory.h +6 -0
index bf2f81faae..8fc27ceeab 100644
@@ -611,14 +611,20 @@ 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
      *      unless stated otherwise.
      *
      * type == XENMEM_resource_ioreq_server -> id == ioreq server id
+     * type == XENMEM_resource_grant_table -> id defined below
      */
     uint32_t id;
+
+#define XENMEM_resource_grant_table_id_shared 0
+#define XENMEM_resource_grant_table_id_status 1
+
     /*
      * IN/OUT - As an IN parameter number of frames of the resource
      *          to be mapped. However, if the specified value is 0 and
xen/include/xen/grant_table.h +4 -0
index 0286ba33dd..c881414e5b 100644
@@ -58,6 +58,10 @@ 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_shared_frame(struct domain *d, unsigned long idx,
+                            mfn_t *mfn);
+int gnttab_get_status_frame(struct domain *d, unsigned long idx,
+                            mfn_t *mfn);
 
 unsigned int gnttab_dom0_frames(void);
 
Signature · 7 lines
2.11.0


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

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/grant_table.c +7 -0
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version == 2);
+

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, · line 3885

Quoted 10 lines
xen/common/grant_table.c +10 -0
+    if ( idx >= nr_status_frames(gt) )
+    {
+        unsigned long nr_status;
+        unsigned long nr_grant;
+
+        nr_status = idx + 1; /* sufficient frames to make idx valid */
+        nr_grant = status_to_grant_frames(nr_status);
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;

If the table is currently empty, this would always fail, wouldn't it? You haven't grown the table yet by this point.

Review

Quoted 2 lines
(quoted diff) +2 -0
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);

And here (other than originally in gnttab_map_frame()) you invoke gnttab_grow_table() perhaps pointlessly (when the table doesn't in fact need growing).

Quoted 16 lines
xen/common/memory.c +5 -0
@@ -1027,6 +1066,11 @@ static int acquire_resource(
 
     switch ( xmar.type )
     {
+    case XENMEM_resource_grant_table:
+        rc = acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames,
+                                 mfn_list);
+        break;
+
     default:
         rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame,
                                    xmar.nr_frames, mfn_list, &xmar.flags);
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

Jan

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

#3 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 26 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 09:47
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v22 1/2] common: add a new mappable resource type:
XENMEM_resource_grant_table

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/grant_table.c +12 -0
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version == 2);
+
+    if ( idx >= nr_status_frames(gt) )
+    {
+        unsigned long nr_status;
+        unsigned long nr_grant;
+
Quoted 8 lines

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, · line 3885

Quoted 5 lines
xen/common/grant_table.c +5 -0
+        nr_status = idx + 1; /* sufficient frames to make idx valid */
+        nr_grant = status_to_grant_frames(nr_status);
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;

If the table is currently empty, this would always fail, wouldn't it? You haven't grown the table yet by this point.

Good point, I should be checking nr_status not nr_grant.

Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +2 -0
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);

And here (other than originally in gnttab_map_frame()) you invoke gnttab_grow_table() perhaps pointlessly (when the table doesn't in fact need growing).

Well if idx is out of range (which is how the code got in here) then the table must need to grow.

Quoted 17 lines
Quoted 17 lines
xen/common/memory.c +5 -0
@@ -1027,6 +1066,11 @@ static int acquire_resource(
 
     switch ( xmar.type )
     {
+    case XENMEM_resource_grant_table:
+        rc = acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames,
+                                 mfn_list);
+        break;
+
     default:
         rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame,
                                    xmar.nr_frames, mfn_list, &xmar.flags);
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 
Quoted 14 lines

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

This can't live ahead of the main switch because XENMEM_rsrc_acq_caller_owned is passed-out flag, not a passed-in one. Also, the grant table would have potentially grown even though the op will fail. Is that what's concerning you?

Paul

Quoted 3 lines

Jan

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

#4 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 32 lines
Quoted 17 lines
Quoted 1 line · On 09.08.18 at 10:55, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 09:47

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/grant_table.c +12 -0
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version == 2);
+
+    if ( idx >= nr_status_frames(gt) )
+    {
+        unsigned long nr_status;
+        unsigned long nr_grant;
+
Quoted 8 lines

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, · line 3885

Quoted 5 lines
xen/common/grant_table.c +5 -0
+        nr_status = idx + 1; /* sufficient frames to make idx valid */
+        nr_grant = status_to_grant_frames(nr_status);
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;

If the table is currently empty, this would always fail, wouldn't it? You haven't grown the table yet by this point.

Good point, I should be checking nr_status not nr_grant.

Quoted 4 lines
Quoted 3 lines
(quoted diff) +2 -0
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);
Quoted 6 lines
Quoted 3 lines

And here (other than originally in gnttab_map_frame()) you invoke gnttab_grow_table() perhaps pointlessly (when the table doesn't in fact need growing).

Well if idx is out of range (which is how the code got in here) then the table must need to grow.

Oh, sorry - I've ignored the outer if().

Quoted 31 lines
Quoted 16 lines
Quoted 16 lines
xen/common/memory.c +5 -0
@@ -1027,6 +1066,11 @@ static int acquire_resource(
 
     switch ( xmar.type )
     {
+    case XENMEM_resource_grant_table:
+        rc = acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames,
+                                 mfn_list);
+        break;
+
     default:
         rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame,
                                    xmar.nr_frames, mfn_list, &xmar.flags);
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 
Quoted 14 lines

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

Quoted 2 lines

This can't live ahead of the main switch because XENMEM_rsrc_acq_caller_owned is passed-out flag, not a passed-in one.

Oh, right. Except that only arch_acquire_resource() could currently set the flag, and hence from this patch's perspective it's not visible that this is an "out" flag. I guess you mean to set the flag in acquire_grant_table() or next to the call to it.

Quoted 2 lines

Also, the grant table would have potentially grown even though the op will fail. Is that what's concerning you?

But a failure post-grow other than the one above is relatively unlikely, isn't it?

Jan

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

#5 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 92 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 10:07
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; xen- devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v22 1/2] common: add a new mappable resource type:
XENMEM_resource_grant_table

Quoted 32 lines
Quoted 17 lines
Quoted 1 line · On 09.08.18 at 10:55, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 09:47

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/grant_table.c +12 -0
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    const struct grant_table *gt = d->grant_table;
+
+    ASSERT(gt->gt_version == 2);
+
+    if ( idx >= nr_status_frames(gt) )
+    {
+        unsigned long nr_status;
+        unsigned long nr_grant;
+
Quoted 8 lines

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref, · line 3885

Quoted 5 lines
xen/common/grant_table.c +5 -0
+        nr_status = idx + 1; /* sufficient frames to make idx valid */
+        nr_grant = status_to_grant_frames(nr_status);
+
+        if ( nr_grant <= nr_grant_frames(gt) ) /* overflow check */
+            return -EINVAL;

If the table is currently empty, this would always fail, wouldn't it? You haven't grown the table yet by this point.

Good point, I should be checking nr_status not nr_grant.

Quoted 4 lines
Quoted 3 lines
(quoted diff) +2 -0
+        if ( nr_grant <= gt->max_grant_frames )
+            gnttab_grow_table(d, nr_grant);
Quoted 6 lines
Quoted 3 lines

And here (other than originally in gnttab_map_frame()) you invoke gnttab_grow_table() perhaps pointlessly (when the table doesn't in fact need growing).

Well if idx is out of range (which is how the code got in here) then the table must need to grow.

Oh, sorry - I've ignored the outer if().

Quoted 31 lines
Quoted 16 lines
Quoted 16 lines
xen/common/memory.c +5 -0
@@ -1027,6 +1066,11 @@ static int acquire_resource(
 
     switch ( xmar.type )
     {
+    case XENMEM_resource_grant_table:
+        rc = acquire_grant_table(d, xmar.id, xmar.frame, xmar.nr_frames,
+                                 mfn_list);
+        break;
+
     default:
         rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame,
                                    xmar.nr_frames, mfn_list, &xmar.flags);
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 
Quoted 14 lines

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

Quoted 2 lines

This can't live ahead of the main switch because XENMEM_rsrc_acq_caller_owned is passed-out flag, not a passed-in one.

Oh, right. Except that only arch_acquire_resource() could currently set the flag, and hence from this patch's perspective it's not visible that this is an "out" flag. I guess you mean to set the flag in acquire_grant_table() or next to the call to it.

Quoted 5 lines
Quoted 2 lines

Also, the grant table would have potentially grown even though the op will fail. Is that what's concerning you?

But a failure post-grow other than the one above is relatively unlikely, isn't it?

Yes, this failure or a copy to guest can derail things at this point. I was just wondering whether you had an objection to something returning EOPNOTSUPP having side effects. I could return something like EACCES instead to make this situation stand out.

Paul

Quoted 3 lines

Jan

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

#6 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 8 lines
Quoted 8 lines
Quoted 1 line · On 09.08.18 at 11:13, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 10:07

Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 09.08.18 at 10:55, <[email protected]> wrote:

Also, the grant table would have potentially grown even though the op will fail. Is that what's concerning you?

Quoted 7 lines
Quoted 2 lines

But a failure post-grow other than the one above is relatively unlikely, isn't it?

Yes, this failure or a copy to guest can derail things at this point. I was just wondering whether you had an objection to something returning EOPNOTSUPP having side effects. I could return something like EACCES instead to make this situation stand out.

The error code is secondary (but indeed EACCES may be more to the point here).

Jan

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

#7 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

In reply to Paul Durrant
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines · On 09.08.18 at 11:07, wrote:
Quoted 1 line · On 09.08.18 at 10:55, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 09:47

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/memory.c +0 -0
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

Quoted 7 lines
Quoted 2 lines

This can't live ahead of the main switch because XENMEM_rsrc_acq_caller_owned is passed-out flag, not a passed-in one.

Oh, right. Except that only arch_acquire_resource() could currently set the flag, and hence from this patch's perspective it's not visible that this is an "out" flag. I guess you mean to set the flag in acquire_grant_table() or next to the call to it.

Rubbish - the flag is supposed to be clear here. I'm sorry for the noise.

Jan

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

#8 lore raw

Re: [PATCH v22 1/2] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 40 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 10:09
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; Stefano Stabellini <[email protected]>; xen- devel <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v22 1/2] common: add a new mappable resource type:
XENMEM_resource_grant_table

Quoted 28 lines
Quoted 27 lines
Quoted 10 lines · On 09.08.18 at 11:07, wrote:
Quoted 1 line · On 09.08.18 at 10:55, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 09 August 2018 09:47

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 08.08.18 at 16:16, <[email protected]> wrote:
xen/common/memory.c +0 -0
@@ -1046,6 +1090,16 @@ static int acquire_resource(
         xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
         unsigned int i;
 
Quoted 14 lines

xen/common/memory.c static int acquire_resource( · line 1102

Quoted 10 lines
xen/common/memory.c +10 -0
+        /*
+         * FIXME: Until foreign pages inserted into the P2M are properly
+         *        reference counted, it is unsafe to allow mapping of
+         *        non-caller-owned resource pages unless the caller is
+         *        the hardware domain.
+         */
+        if ( !(xmar.flags & XENMEM_rsrc_acq_caller_owned) &&
+             !is_hardware_domain(currd) )
+            return -EOPNOTSUPP;
+

Now that I look at this again - wouldn't this check better live ahead of the main switch()? I find it odd, for example, that in this case the grant table would still have got grown.

This can't live ahead of the main switch because XENMEM_rsrc_acq_caller_owned is passed-out flag, not a passed-in one.

Quoted 7 lines
Quoted 4 lines

Oh, right. Except that only arch_acquire_resource() could currently set the flag, and hence from this patch's perspective it's not visible that this is an "out" flag. I guess you mean to set the flag in acquire_grant_table() or next to the call to it.

Rubbish - the flag is supposed to be clear here. I'm sorry for the noise.

I was just poised to say that :-)

Paul

Quoted 2 lines

Jan

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

#9 lore raw

PATCH v22 2/2 tools/libxenctrl: use new xenforeignmemory API to seed grant table

In reply to Paul Durrant

A previous patch added support for priv-mapping guest resources directly (rather than having to foreign-map, which requires P2M modification for HVM guests).

This patch makes use of the new API to seed the guest grant table unless the underlying infrastructure (i.e. privcmd) doesn't support it, in which case the old scheme is used.

NOTE: The call to xc_dom_gnttab_hvm_seed() in hvm_build_set_params() was
actually unnecessary, as the grant table has already been seeded by a prior call to xc_dom_gnttab_init() made by libxl__build_dom().

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>

v22:
- Addressed comments from Andrew (cosmetic changes only).

v18:
- Trivial re-base.

v13:
- Re-base.

v10:
- Use new id constant for grant table.

v4:
- Minor cosmetic fix suggested by Roger.

v3:
- Introduced xc_dom_set_gnttab_entry() to avoid duplicated code.

---
 tools/libxc/include/xc_dom.h        |  12 +--
 tools/libxc/xc_dom_boot.c           | 166 +++++++++++++++++++++++-------------
 tools/libxc/xc_sr_restore_x86_hvm.c |  10 +--
 tools/libxc/xc_sr_restore_x86_pv.c  |   2 +-
 tools/libxl/libxl_dom.c             |   1 -
 tools/python/xen/lowlevel/xc/xc.c   |   6 +-
 6 files changed, 120 insertions(+), 77 deletions(-)
tools/libxc/include/xc_dom.h +4 -8
index 8a66889c68..0b5a632d3c 100644
@@ -337,14 +337,10 @@ void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn,
 int xc_dom_boot_image(struct xc_dom_image *dom);
 int xc_dom_compat_check(struct xc_dom_image *dom);
 int xc_dom_gnttab_init(struct xc_dom_image *dom);
-int xc_dom_gnttab_hvm_seed(xc_interface *xch, uint32_t domid,
-                           xen_pfn_t console_gmfn,
-                           xen_pfn_t xenstore_gmfn,
-                           uint32_t console_domid,
-                           uint32_t xenstore_domid);
-int xc_dom_gnttab_seed(xc_interface *xch, uint32_t domid,
-                       xen_pfn_t console_gmfn,
-                       xen_pfn_t xenstore_gmfn,
+int xc_dom_gnttab_seed(xc_interface *xch, uint32_t guest_domid,
+                       bool is_hvm,
+                       xen_pfn_t console_gfn,
+                       xen_pfn_t xenstore_gfn,
                        uint32_t console_domid,
                        uint32_t xenstore_domid);
 bool xc_dom_translated(const struct xc_dom_image *dom);
tools/libxc/xc_dom_boot.c +107 -59

246 lines not on this page · raw message

tools/libxc/xc_sr_restore_x86_hvm.c +5 -5
index 227c48553e..4765a52f33 100644
@@ -216,11 +216,11 @@ static int x86_hvm_stream_complete(struct xc_sr_context *ctx)
         return rc;
     }
 
-    rc = xc_dom_gnttab_hvm_seed(xch, ctx->domid,
-                                ctx->restore.console_gfn,
-                                ctx->restore.xenstore_gfn,
-                                ctx->restore.console_domid,
-                                ctx->restore.xenstore_domid);
+    rc = xc_dom_gnttab_seed(xch, ctx->domid, true,
+                            ctx->restore.console_gfn,
+                            ctx->restore.xenstore_gfn,
+                            ctx->restore.console_domid,
+                            ctx->restore.xenstore_domid);
     if ( rc )
     {
         PERROR("Failed to seed grant table");
tools/libxc/xc_sr_restore_x86_pv.c +1 -1
index d81dfdcca6..a2dbf85157 100644
@@ -1105,7 +1105,7 @@ static int x86_pv_stream_complete(struct xc_sr_context *ctx)
     if ( rc )
         return rc;
 
-    rc = xc_dom_gnttab_seed(xch, ctx->domid,
+    rc = xc_dom_gnttab_seed(xch, ctx->domid, false,
                             ctx->restore.console_gfn,
                             ctx->restore.xenstore_gfn,
                             ctx->restore.console_domid,
tools/libxl/libxl_dom.c +0 -1
index 3cfe0d4808..c8a1dc7fd5 100644
@@ -905,7 +905,6 @@ static int hvm_build_set_params(xc_interface *handle, uint32_t domid,
     *store_mfn = str_mfn;
     *console_mfn = cons_mfn;
 
-    xc_dom_gnttab_hvm_seed(handle, domid, *console_mfn, *store_mfn, console_domid, store_domid);
     return 0;
 }
 
tools/python/xen/lowlevel/xc/xc.c +3 -3
index fc19ee0741..5ade12762a 100644
@@ -800,9 +800,9 @@ static PyObject *pyxc_gnttab_hvm_seed(XcObject *self,
 				      &console_domid, &xenstore_domid) )
         return NULL;
 
-    if ( xc_dom_gnttab_hvm_seed(self->xc_handle, dom,
-				console_gmfn, xenstore_gmfn,
-				console_domid, xenstore_domid) != 0 )
+    if ( xc_dom_gnttab_seed(self->xc_handle, dom, true,
+                            console_gmfn, xenstore_gmfn,
+                            console_domid, xenstore_domid) != 0 )
         return pyxc_error_to_exception(self->xc_handle);
 
     return Py_None;
Signature · 7 lines
2.11.0


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