Skip to contentSkip to conversation

x86: guest resource mapping

PATCH Superseded Paul Durrant 2 participants 30 messages Mar 22, 2018 – Mar 29, 2018 Arm/actions semi architecture

v18 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#0 lore raw

v18 cover x86: guest resource mapping

This series introduces support for direct mapping of guest resources. The resources are:
- IOREQ server pages
- Grant tables

v18:
- Re-base
- Use the now-reference-counted emulating domain to host ioreq pages

v17:
- Make sure ioreq page free-ing is done at domain destruction

v16:
- Fix default ioreq server code and verified with qemu trad

v15:
- Correct page ownership of ioreq pages

v14:
- Responded to more comments from Jan.

v13:
- Responded to more comments from Jan and Julien.
- Build-tested using ARM cross-compilation.

v12:
- Responded to more comments from Jan.

v11:
- Responded to more comments from Jan.

v10:
- Responded to comments from Jan.

v9:
- Change to patch #1 only.

v8:
- Re-ordered series and dropped two patches that have already been committed.

v7:
- Fixed assertion failure hit during domain destroy.

v6:
- Responded to missed comments from Roger.

v5:
- Responded to review comments from Wei.

v4:
- Responded to further review comments from Roger.

v3:
- Dropped original patch #1 since it is covered by Juergen's patch.
- Added new xenforeignmemorycleanup patch (#4).
- Replaced the patch introducing the ioreq server 'is_default' flag with one that changes the ioreq server list into an array (#8).

Paul Durrant (11):
x86/hvm/ioreq: maintain an array of ioreq servers rather than a list x86/hvm/ioreq: simplify code and use consistent naming x86/hvm/ioreq: use gfn_t in struct hvm_ioreq_page x86/hvm/ioreq: defer mapping gfns until they are actually requested x86/mm: add HYPERVISOR_memory_op to acquire guest resources x86/hvm/ioreq: add a new mappable resource type...
x86/mm: add an extra command to HYPERVISOR_mmu_update... tools/libxenforeignmemory: add support for resource mapping tools/libxenforeignmemory: reduce xenforeignmemory_restrict code
footprint
common: add a new mappable resource type: XENMEM_resource_grant_table tools/libxenctrl: use new xenforeignmemory API to seed grant table

 tools/flask/policy/modules/xen.if                  |   4 +-
 tools/include/xen-sys/Linux/privcmd.h              |  11 +
 tools/libs/devicemodel/core.c                      |   8 +
 tools/libs/devicemodel/include/xendevicemodel.h    |   6 +-
 tools/libs/foreignmemory/Makefile                  |   2 +-
 tools/libs/foreignmemory/core.c                    |  53 ++
 tools/libs/foreignmemory/freebsd.c                 |   7 -
 .../libs/foreignmemory/include/xenforeignmemory.h  |  41 +
 tools/libs/foreignmemory/libxenforeignmemory.map   |   5 +
 tools/libs/foreignmemory/linux.c                   |  45 ++
 tools/libs/foreignmemory/minios.c                  |   7 -
 tools/libs/foreignmemory/netbsd.c                  |   7 -
 tools/libs/foreignmemory/private.h                 |  43 +-
 tools/libs/foreignmemory/solaris.c                 |   7 -
 tools/libxc/include/xc_dom.h                       |   8 +-
 tools/libxc/xc_dom_boot.c                          | 114 ++-
 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/arch/x86/hvm/dm.c                              |   9 +-
 xen/arch/x86/hvm/ioreq.c                           | 884 ++++++++++++---------
 xen/arch/x86/mm.c                                  |  60 +-
 xen/arch/x86/mm/p2m.c                              |   3 +-
 xen/common/compat/memory.c                         | 100 +++
 xen/common/grant_table.c                           |  71 +-
 xen/common/memory.c                                | 137 ++++
 xen/include/asm-arm/mm.h                           |   8 +
 xen/include/asm-arm/p2m.h                          |  10 +
 xen/include/asm-x86/hvm/domain.h                   |  15 +-
 xen/include/asm-x86/hvm/ioreq.h                    |   2 +
 xen/include/asm-x86/mm.h                           |   5 +
 xen/include/asm-x86/p2m.h                          |   3 +
 xen/include/public/hvm/dm_op.h                     |  36 +-
 xen/include/public/memory.h                        |  69 +-
 xen/include/public/xen.h                           |  12 +-
 xen/include/xen/grant_table.h                      |   4 +
 xen/include/xlat.lst                               |   1 +
 xen/include/xsm/dummy.h                            |   6 +
 xen/include/xsm/xsm.h                              |   6 +
 xen/xsm/dummy.c                                    |   1 +
 xen/xsm/flask/hooks.c                              |   6 +
 xen/xsm/flask/policy/access_vectors                |   2 +
 43 files changed, 1320 insertions(+), 517 deletions(-)

---
Cc: Daniel De Graaf <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: "Marek Marczykowski-Górecki" <[email protected]>
Cc: Paul Durrant <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Chao Gao <[email protected]>

Signature · 7 lines
2.11.0


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

PATCH v18 1/11 x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

A subsequent patch will remove the current implicit limitation on creation of ioreq servers which is due to the allocation of gfns for the ioreq structures and buffered ioreq ring.

It will therefore be necessary to introduce an explicit limit and, since this limit should be small, it simplifies the code to maintain an array of that size rather than using a list.

Also, by reserving an array slot for the default server and populating array slots early in create, the need to pass an 'is_default' boolean to sub-functions can be avoided.

Some function return values are changed by this patch: Specifically, in the case where the id of the default ioreq server is passed in, -EOPNOTSUPP is now returned rather than -ENOENT.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

v18:
- non-trivial re-base.
- small modification to FOR_EACH... macro to iterate backwards, to main- tain a previous undocumented but useful semantic that secondary emulators are selected in favour of qemu.
- dropped R-b's because of change.

v10:
- modified FOR_EACH... macro as suggested by Jan.
- check for NULL in IS_DEFAULT macro as suggested by Jan.

v9:
- modified FOR_EACH... macro as requested by Andrew.

v8:
- Addressed various comments from Jan.

v7:
- Fixed assertion failure found in testing.

v6:
- Updated according to comments made by Roger on v4 that I'd missed.

v5:
- Switched GET/SET_IOREQ_SERVER() macros to get/set_ioreq_server() functions to avoid possible double-evaluation issues.

v4:
- Introduced more helper macros and relocated them to the top of the code.

v3:
- New patch (replacing "move is_default into struct hvm_ioreq_server") in response to review comments.

---
 xen/arch/x86/hvm/ioreq.c         | 539 +++++++++++++++++++--------------------
 xen/include/asm-x86/hvm/domain.h |  11 +-
 2 files changed, 265 insertions(+), 285 deletions(-)
xen/arch/x86/hvm/ioreq.c +260 -279
index 44d029499d..d8d4e96a80 100644
@@ -33,6 +33,37 @@
 
 #include <public/hvm/ioreq.h>
 
+static void set_ioreq_server(struct domain *d, unsigned int id,
+                             struct hvm_ioreq_server *s)
+{
+    ASSERT(id < MAX_NR_IOREQ_SERVERS);
+    ASSERT(!s || !d->arch.hvm_domain.ioreq_server.server[id]);
+
+    d->arch.hvm_domain.ioreq_server.server[id] = s;
+}
+
+#define GET_IOREQ_SERVER(d, id) \
+    (d)->arch.hvm_domain.ioreq_server.server[id]
+
+static struct hvm_ioreq_server *get_ioreq_server(const struct domain *d,
+                                                 unsigned int id)
+{
+    if ( id >= MAX_NR_IOREQ_SERVERS )
+        return NULL;
+
+    return GET_IOREQ_SERVER(d, id);
+}
+
+#define IS_DEFAULT(s) \
+    ((s) && (s) == GET_IOREQ_SERVER((s)->target, DEFAULT_IOSERVID))
+
+/* Iterate over all possible ioreq servers */
+#define FOR_EACH_IOREQ_SERVER(d, id, s) \
+    for ( (id) = MAX_NR_IOREQ_SERVERS; (id) != 0; ) \
+        if ( !(s = GET_IOREQ_SERVER(d, --(id))) ) \
+            continue; \
+        else
+
 static ioreq_t *get_ioreq(struct hvm_ioreq_server *s, struct vcpu *v)
 {
     shared_iopage_t *p = s->ioreq.va;
@@ -47,10 +78,9 @@ bool hvm_io_pending(struct vcpu *v)
 {
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
+    unsigned int id;
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct hvm_ioreq_vcpu *sv;
 
@@ -127,10 +157,9 @@ bool handle_hvm_io_completion(struct vcpu *v)
     struct hvm_vcpu_io *vio = &v->arch.hvm_vcpu.hvm_io;
     struct hvm_ioreq_server *s;
     enum hvm_io_completion io_completion;
+    unsigned int id;
 
-      list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct hvm_ioreq_vcpu *sv;
 
@@ -243,13 +272,12 @@ static int hvm_map_ioreq_page(
 bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
 {
     const struct hvm_ioreq_server *s;
+    unsigned int id;
     bool found = false;
 
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         if ( (s->ioreq.va && s->ioreq.page == page) ||
              (s->bufioreq.va && s->bufioreq.page == page) )
@@ -302,7 +330,7 @@ static void hvm_update_ioreq_evtchn(struct hvm_ioreq_server *s,
 }
 
 static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s,
-                                     bool is_default, struct vcpu *v)
+                                     struct vcpu *v)
 {
     struct hvm_ioreq_vcpu *sv;
     int rc;
@@ -316,7 +344,8 @@ static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s,
     spin_lock(&s->lock);
 
     rc = alloc_unbound_xen_event_channel(v->domain, v->vcpu_id,
-                                         s->emulator->domain_id, NULL);
+                                         s->emulator->domain_id,
+                                         NULL);
     if ( rc < 0 )
         goto fail2;
 
@@ -332,7 +361,7 @@ static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s,
             goto fail3;
 
         s->bufioreq_evtchn = rc;
-        if ( is_default )
+        if ( IS_DEFAULT(s) )
             d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_EVTCHN] =
                 s->bufioreq_evtchn;
     }
@@ -432,7 +461,6 @@ static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s,
 }
 
 static int hvm_ioreq_server_setup_pages(struct hvm_ioreq_server *s,
-                                        bool is_default,
                                         bool handle_bufioreq)
 {
     struct domain *d = s->target;
@@ -440,7 +468,7 @@ static int hvm_ioreq_server_setup_pages(struct hvm_ioreq_server *s,
     unsigned long bufioreq_gfn = gfn_x(INVALID_GFN);
     int rc;
 
-    if ( is_default )
+    if ( IS_DEFAULT(s) )
     {
         /*
          * The default ioreq server must handle buffered ioreqs, for
@@ -469,8 +497,7 @@ static int hvm_ioreq_server_setup_pages(struct hvm_ioreq_server *s,
     return rc;
 }
 
-static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s,
-                                         bool is_default)
+static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s)
 {
     struct domain *d = s->target;
     bool handle_bufioreq = !!s->bufioreq.va;
@@ -480,7 +507,7 @@ static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s,
 
     hvm_unmap_ioreq_page(s, false);
 
-    if ( !is_default )
+    if ( !IS_DEFAULT(s) )
     {
         if ( handle_bufioreq )
             hvm_free_ioreq_gfn(d, s->bufioreq.gfn);
@@ -489,12 +516,11 @@ static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s,
     }
 }
 
-static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s,
-                                            bool is_default)
+static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s)
 {
     unsigned int i;
 
-    if ( is_default )
+    if ( IS_DEFAULT(s) )
         return;
 
     for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
@@ -502,19 +528,21 @@ static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s,
 }
 
 static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
-                                            bool is_default)
+                                            ioservid_t id)
 {
     unsigned int i;
     int rc;
 
-    if ( is_default )
+    if ( id == DEFAULT_IOSERVID )
         goto done;
 
+    ASSERT(!IS_DEFAULT(s));
+
     for ( i = 0; i < NR_IO_RANGE_TYPES; i++ )
     {
         char *name;
 
-        rc = asprintf(&name, "ioreq_server %d %s", s->id,
+        rc = asprintf(&name, "ioreq_server %d %s", id,
                       (i == XEN_DMOP_IO_RANGE_PORT) ? "port" :
                       (i == XEN_DMOP_IO_RANGE_MEMORY) ? "memory" :
                       (i == XEN_DMOP_IO_RANGE_PCI) ? "pci" :
@@ -538,13 +566,12 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
     return 0;
 
  fail:
-    hvm_ioreq_server_free_rangesets(s, false);
+    hvm_ioreq_server_free_rangesets(s);
 
     return rc;
 }
 
-static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s,
-                                    bool is_default)
+static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s)
 {
     struct domain *d = s->target;
     struct hvm_ioreq_vcpu *sv;
@@ -555,7 +582,7 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s,
     if ( s->enabled )
         goto done;
 
-    if ( !is_default )
+    if ( !IS_DEFAULT(s) )
     {
         hvm_remove_ioreq_gfn(d, &s->ioreq);
 
@@ -574,8 +601,7 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s,
     spin_unlock(&s->lock);
 }
 
-static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s,
-                                     bool is_default)
+static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s)
 {
     struct domain *d = s->target;
     bool handle_bufioreq = !!s->bufioreq.va;
@@ -585,7 +611,7 @@ static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s,
     if ( !s->enabled )
         goto done;
 
-    if ( !is_default )
+    if ( !IS_DEFAULT(s) )
     {
         if ( handle_bufioreq )
             hvm_add_ioreq_gfn(d, &s->bufioreq);
@@ -600,14 +626,13 @@ static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s,
 }
 
 static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
-                                 struct domain *d, bool is_default,
-                                 int bufioreq_handling, ioservid_t id)
+                                 struct domain *d, int bufioreq_handling,
+                                 ioservid_t id)
 {
     struct domain *currd = current->domain;
     struct vcpu *v;
     int rc;
 
-    s->id = id;
     s->target = d;
 
     get_knownalive_domain(currd);
@@ -617,7 +642,7 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
     INIT_LIST_HEAD(&s->ioreq_vcpu_list);
     spin_lock_init(&s->bufioreq_lock);
 
-    rc = hvm_ioreq_server_alloc_rangesets(s, is_default);
+    rc = hvm_ioreq_server_alloc_rangesets(s, id);
     if ( rc )
         return rc;
 
@@ -625,13 +650,13 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
         s->bufioreq_atomic = true;
 
     rc = hvm_ioreq_server_setup_pages(
-             s, is_default, bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF);
+             s, bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF);
     if ( rc )
         goto fail_map;
 
     for_each_vcpu ( d, v )
     {
-        rc = hvm_ioreq_server_add_vcpu(s, is_default, v);
+        rc = hvm_ioreq_server_add_vcpu(s, v);
         if ( rc )
             goto fail_add;
     }
@@ -640,102 +665,87 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
 
  fail_add:
     hvm_ioreq_server_remove_all_vcpus(s);
-    hvm_ioreq_server_unmap_pages(s, is_default);
+    hvm_ioreq_server_unmap_pages(s);
 
  fail_map:
-    hvm_ioreq_server_free_rangesets(s, is_default);
+    hvm_ioreq_server_free_rangesets(s);
 
     put_domain(s->emulator);
     return rc;
 }
 
-static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s,
-                                    bool is_default)
+static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s)
 {
     ASSERT(!s->enabled);
     hvm_ioreq_server_remove_all_vcpus(s);
-    hvm_ioreq_server_unmap_pages(s, is_default);
-    hvm_ioreq_server_free_rangesets(s, is_default);
+    hvm_ioreq_server_unmap_pages(s);
+    hvm_ioreq_server_free_rangesets(s);
 
     put_domain(s->emulator);
 }
 
-static ioservid_t next_ioservid(struct domain *d)
-{
-    struct hvm_ioreq_server *s;
-    ioservid_t id;
-
-    ASSERT(spin_is_locked(&d->arch.hvm_domain.ioreq_server.lock));
-
-    id = d->arch.hvm_domain.ioreq_server.id;
-
- again:
-    id++;
-
-    /* Check for uniqueness */
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( id == s->id )
-            goto again;
-    }
-
-    d->arch.hvm_domain.ioreq_server.id = id;
-
-    return id;
-}
-
 int hvm_create_ioreq_server(struct domain *d, bool is_default,
                             int bufioreq_handling, ioservid_t *id)
 {
     struct hvm_ioreq_server *s;
+    unsigned int i;
     int rc;
 
     if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC )
         return -EINVAL;
 
-    rc = -ENOMEM;
     s = xzalloc(struct hvm_ioreq_server);
     if ( !s )
-        goto fail1;
+        return -ENOMEM;
 
     domain_pause(d);
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -EEXIST;
-    if ( is_default && d->arch.hvm_domain.default_ioreq_server != NULL )
-        goto fail2;
-
-    rc = hvm_ioreq_server_init(s, d, is_default, bufioreq_handling,
-                               next_ioservid(d));
-    if ( rc )
-        goto fail3;
-
-    list_add(&s->list_entry,
-             &d->arch.hvm_domain.ioreq_server.list);
-
     if ( is_default )
     {
-        d->arch.hvm_domain.default_ioreq_server = s;
-        hvm_ioreq_server_enable(s, true);
+        i = DEFAULT_IOSERVID;
+
+        rc = -EEXIST;
+        if ( GET_IOREQ_SERVER(d, i) )
+            goto fail;
     }
+    else
+    {
+        for ( i = 0; i < MAX_NR_IOREQ_SERVERS; i++ )
+        {
+            if ( i != DEFAULT_IOSERVID && !GET_IOREQ_SERVER(d, i) )
+                break;
+        }
+
+        rc = -ENOSPC;
+        if ( i >= MAX_NR_IOREQ_SERVERS )
+            goto fail;
+    }
+
+    set_ioreq_server(d, i, s);
+
+    rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i);
+    if ( rc )
+        goto fail;
+
+    if ( i == DEFAULT_IOSERVID )
+        hvm_ioreq_server_enable(s);
 
     if ( id )
-        *id = s->id;
+        *id = i;
 
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
     domain_unpause(d);
 
     return 0;
 
- fail3:
- fail2:
+ fail:
+    set_ioreq_server(d, i, NULL);
+
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
     domain_unpause(d);
 
     xfree(s);
- fail1:
     return rc;
 }
 
@@ -744,41 +754,38 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)
     struct hvm_ioreq_server *s;
     int rc;
 
-    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+    if ( id == DEFAULT_IOSERVID )
+        return -EPERM;
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-        if ( s->id != id )
-            continue;
+    s = get_ioreq_server(d, id);
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        domain_pause(d);
+    ASSERT(!IS_DEFAULT(s));
 
-        p2m_set_ioreq_server(d, 0, s);
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        hvm_ioreq_server_disable(s, false);
+    domain_pause(d);
 
-        list_del(&s->list_entry);
+    p2m_set_ioreq_server(d, 0, s);
 
-        hvm_ioreq_server_deinit(s, false);
+    hvm_ioreq_server_disable(s);
+    hvm_ioreq_server_deinit(s);
 
-        domain_unpause(d);
+    domain_unpause(d);
 
-        xfree(s);
+    set_ioreq_server(d, id, NULL);
+    xfree(s);
 
-        rc = 0;
-        break;
-    }
+    rc = 0;
 
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     return rc;
@@ -792,35 +799,34 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
     struct hvm_ioreq_server *s;
     int rc;
 
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    s = get_ioreq_server(d, id);
 
-        if ( s->id != id )
-            continue;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    ASSERT(!IS_DEFAULT(s));
 
-        *ioreq_gfn = s->ioreq.gfn;
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        if ( s->bufioreq.va != NULL )
-        {
-            *bufioreq_gfn = s->bufioreq.gfn;
-            *bufioreq_port = s->bufioreq_evtchn;
-        }
+    *ioreq_gfn = s->ioreq.gfn;
 
-        rc = 0;
-        break;
+    if ( s->bufioreq.va != NULL )
+    {
+        *bufioreq_gfn = s->bufioreq.gfn;
+        *bufioreq_port = s->bufioreq_evtchn;
     }
 
+    rc = 0;
+
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     return rc;
@@ -831,55 +837,53 @@ int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
                                      uint64_t end)
 {
     struct hvm_ioreq_server *s;
+    struct rangeset *r;
     int rc;
 
     if ( start > end )
         return -EINVAL;
 
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        struct rangeset *r;
+    s = get_ioreq_server(d, id);
 
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        if ( s->id != id )
-            continue;
+    ASSERT(!IS_DEFAULT(s));
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        switch ( type )
-        {
-        case XEN_DMOP_IO_RANGE_PORT:
-        case XEN_DMOP_IO_RANGE_MEMORY:
-        case XEN_DMOP_IO_RANGE_PCI:
-            r = s->range[type];
-            break;
+    switch ( type )
+    {
+    case XEN_DMOP_IO_RANGE_PORT:
+    case XEN_DMOP_IO_RANGE_MEMORY:
+    case XEN_DMOP_IO_RANGE_PCI:
+        r = s->range[type];
+        break;
 
-        default:
-            r = NULL;
-            break;
-        }
+    default:
+        r = NULL;
+        break;
+    }
 
-        rc = -EINVAL;
-        if ( !r )
-            break;
+    rc = -EINVAL;
+    if ( !r )
+        goto out;
 
-        rc = -EEXIST;
-        if ( rangeset_overlaps_range(r, start, end) )
-            break;
+    rc = -EEXIST;
+    if ( rangeset_overlaps_range(r, start, end) )
+        goto out;
 
-        rc = rangeset_add_range(r, start, end);
-        break;
-    }
+    rc = rangeset_add_range(r, start, end);
 
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     return rc;
@@ -890,55 +894,53 @@ int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
                                          uint64_t end)
 {
     struct hvm_ioreq_server *s;
+    struct rangeset *r;
     int rc;
 
     if ( start > end )
         return -EINVAL;
 
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
+    s = get_ioreq_server(d, id);
+
     rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        struct rangeset *r;
+    if ( !s )
+        goto out;
 
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    ASSERT(!IS_DEFAULT(s));
 
-        if ( s->id != id )
-            continue;
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    switch ( type )
+    {
+    case XEN_DMOP_IO_RANGE_PORT:
+    case XEN_DMOP_IO_RANGE_MEMORY:
+    case XEN_DMOP_IO_RANGE_PCI:
+        r = s->range[type];
+        break;
 
-        switch ( type )
-        {
-        case XEN_DMOP_IO_RANGE_PORT:
-        case XEN_DMOP_IO_RANGE_MEMORY:
-        case XEN_DMOP_IO_RANGE_PCI:
-            r = s->range[type];
-            break;
+    default:
+        r = NULL;
+        break;
+    }
 
-        default:
-            r = NULL;
-            break;
-        }
+    rc = -EINVAL;
+    if ( !r )
+        goto out;
 
-        rc = -EINVAL;
-        if ( !r )
-            break;
+    rc = -ENOENT;
+    if ( !rangeset_contains_range(r, start, end) )
+        goto out;
 
-        rc = -ENOENT;
-        if ( !rangeset_contains_range(r, start, end) )
-            break;
-
-        rc = rangeset_remove_range(r, start, end);
-        break;
-    }
+    rc = rangeset_remove_range(r, start, end);
 
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     return rc;
@@ -958,6 +960,9 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
     struct hvm_ioreq_server *s;
     int rc;
 
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
     if ( type != HVMMEM_ioreq_server )
         return -EINVAL;
 
@@ -966,25 +971,21 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
 
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
+    s = get_ioreq_server(d, id);
+
     rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    if ( !s )
+        goto out;
 
-        if ( s->id != id )
-            continue;
+    ASSERT(!IS_DEFAULT(s));
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        rc = p2m_set_ioreq_server(d, flags, s);
-        break;
-    }
+    rc = p2m_set_ioreq_server(d, flags, s);
 
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     if ( rc == 0 && flags == 0 )
@@ -1001,42 +1002,38 @@ int hvm_map_mem_type_to_ioreq_server(struct domain *d, ioservid_t id,
 int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id,
                                bool enabled)
 {
-    struct list_head *entry;
+    struct hvm_ioreq_server *s;
     int rc;
 
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -ENOENT;
-    list_for_each ( entry,
-                    &d->arch.hvm_domain.ioreq_server.list )
-    {
-        struct hvm_ioreq_server *s = list_entry(entry,
-                                                struct hvm_ioreq_server,
-                                                list_entry);
+    s = get_ioreq_server(d, id);
 
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        if ( s->id != id )
-            continue;
+    ASSERT(!IS_DEFAULT(s));
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        domain_pause(d);
+    domain_pause(d);
 
-        if ( enabled )
-            hvm_ioreq_server_enable(s, false);
-        else
-            hvm_ioreq_server_disable(s, false);
+    if ( enabled )
+        hvm_ioreq_server_enable(s);
+    else
+        hvm_ioreq_server_disable(s);
 
-        domain_unpause(d);
+    domain_unpause(d);
 
-        rc = 0;
-        break;
-    }
+    rc = 0;
 
+ out:
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
     return rc;
 }
@@ -1044,17 +1041,14 @@ int hvm_set_ioreq_server_state(struct domain *d, ioservid_t id,
 int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)
 {
     struct hvm_ioreq_server *s;
+    unsigned int id;
     int rc;
 
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
-        bool is_default = (s == d->arch.hvm_domain.default_ioreq_server);
-
-        rc = hvm_ioreq_server_add_vcpu(s, is_default, v);
+        rc = hvm_ioreq_server_add_vcpu(s, v);
         if ( rc )
             goto fail;
     }
@@ -1064,10 +1058,15 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)
     return 0;
 
  fail:
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    while ( id-- != 0 )
+    {
+        s = GET_IOREQ_SERVER(d, id);
+
+        if ( !s )
+            continue;
+
         hvm_ioreq_server_remove_vcpu(s, v);
+    }
 
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
@@ -1077,12 +1076,11 @@ int hvm_all_ioreq_servers_add_vcpu(struct domain *d, struct vcpu *v)
 void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v)
 {
     struct hvm_ioreq_server *s;
+    unsigned int id;
 
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
         hvm_ioreq_server_remove_vcpu(s, v);
 
     spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
@@ -1090,28 +1088,19 @@ void hvm_all_ioreq_servers_remove_vcpu(struct domain *d, struct vcpu *v)
 
 void hvm_destroy_all_ioreq_servers(struct domain *d)
 {
-    struct hvm_ioreq_server *s, *next;
+    struct hvm_ioreq_server *s;
+    unsigned int id;
 
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
     /* No need to domain_pause() as the domain is being torn down */
 
-    list_for_each_entry_safe ( s,
-                               next,
-                               &d->arch.hvm_domain.ioreq_server.list,
-                               list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
-        bool is_default = (s == d->arch.hvm_domain.default_ioreq_server);
-
-        hvm_ioreq_server_disable(s, is_default);
-
-        if ( is_default )
-            d->arch.hvm_domain.default_ioreq_server = NULL;
-
-        list_del(&s->list_entry);
-
-        hvm_ioreq_server_deinit(s, is_default);
+        hvm_ioreq_server_disable(s);
+        hvm_ioreq_server_deinit(s);
 
+        set_ioreq_server(d, id, NULL);
         xfree(s);
     }
 
@@ -1125,12 +1114,10 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
     uint32_t cf8;
     uint8_t type;
     uint64_t addr;
-
-    if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
-        return NULL;
+    unsigned int id;
 
     if ( p->type != IOREQ_TYPE_COPY && p->type != IOREQ_TYPE_PIO )
-        return d->arch.hvm_domain.default_ioreq_server;
+        return GET_IOREQ_SERVER(d, DEFAULT_IOSERVID);
 
     cf8 = d->arch.hvm_domain.pci_cf8;
 
@@ -1168,16 +1155,11 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
         addr = p->addr;
     }
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct rangeset *r;
 
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
-
-        if ( !s->enabled )
+        if ( IS_DEFAULT(s) )
             continue;
 
         r = s->range[type];
@@ -1210,7 +1192,7 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
         }
     }
 
-    return d->arch.hvm_domain.default_ioreq_server;
+    return GET_IOREQ_SERVER(d, DEFAULT_IOSERVID);
 }
 
 static int hvm_send_buffered_ioreq(struct hvm_ioreq_server *s, ioreq_t *p)
@@ -1369,13 +1351,13 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered)
 {
     struct domain *d = current->domain;
     struct hvm_ioreq_server *s;
-    unsigned int failed = 0;
+    unsigned int id, failed = 0;
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
+    {
         if ( hvm_send_ioreq(s, p, buffered) == X86EMUL_UNHANDLEABLE )
             failed++;
+    }
 
     return failed;
 }
@@ -1395,7 +1377,6 @@ static int hvm_access_cf8(
 void hvm_ioreq_init(struct domain *d)
 {
     spin_lock_init(&d->arch.hvm_domain.ioreq_server.lock);
-    INIT_LIST_HEAD(&d->arch.hvm_domain.ioreq_server.list);
 
     register_portio_handler(d, 0xcf8, 4, hvm_access_cf8);
 }
xen/include/asm-x86/hvm/domain.h +5 -6
index 6e03d024c8..2e4d85f6fe 100644
@@ -52,13 +52,11 @@ struct hvm_ioreq_vcpu {
 #define MAX_NR_IO_RANGES  256
 
 struct hvm_ioreq_server {
-    struct list_head       list_entry;
     struct domain          *target, *emulator;
 
     /* Lock to serialize toolstack modifications */
     spinlock_t             lock;
 
-    ioservid_t             id;
     struct hvm_ioreq_page  ioreq;
     struct list_head       ioreq_vcpu_list;
     struct hvm_ioreq_page  bufioreq;
@@ -98,6 +96,9 @@ struct hvm_pi_ops {
     void (*do_resume)(struct vcpu *v);
 };
 
+#define MAX_NR_IOREQ_SERVERS 8
+#define DEFAULT_IOSERVID 0
+
 struct hvm_domain {
     /* Guest page range used for non-default ioreq servers */
     struct {
@@ -107,11 +108,9 @@ struct hvm_domain {
 
     /* Lock protects all other values in the sub-struct and the default */
     struct {
-        spinlock_t       lock;
-        ioservid_t       id;
-        struct list_head list;
+        spinlock_t              lock;
+        struct hvm_ioreq_server *server[MAX_NR_IOREQ_SERVERS];
     } ioreq_server;
-    struct hvm_ioreq_server *default_ioreq_server;
 
     /* Cached CF8 for guest PCI config cycles */
     uint32_t                pci_cf8;
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 v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:

v18:
- non-trivial re-base.
- small modification to FOR_EACH... macro to iterate backwards, to main- tain a previous undocumented but useful semantic that secondary emulators are selected in favour of qemu.

If this is intentional (and necessary), I think there should be a code comment saying why (and implicitly preventing people from wanting to change it).

I'm also having difficulty to see why that was the case before this patch: hvm_create_ioreq_server() doesn't insert the default one at the list tail. Are you perhaps basing this solely on the assumption that secondary ones would be created after the default one?

xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, · line 348

Quoted 7 lines
xen/arch/x86/hvm/ioreq.c +2 -1
@@ -316,7 +344,8 @@ static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s,
     spin_lock(&s->lock);
 
     rc = alloc_unbound_xen_event_channel(v->domain, v->vcpu_id,
-                                         s->emulator->domain_id, NULL);
+                                         s->emulator->domain_id,
+                                         NULL);

Stray change?

Quoted 47 lines
xen/arch/x86/hvm/ioreq.c +0 -0
 int hvm_create_ioreq_server(struct domain *d, bool is_default,
                             int bufioreq_handling, ioservid_t *id)
 {
     struct hvm_ioreq_server *s;
xen/arch/x86/hvm/ioreq.c +13 -16
+    unsigned int i;
     int rc;
 
     if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC )
         return -EINVAL;
 
-    rc = -ENOMEM;
     s = xzalloc(struct hvm_ioreq_server);
     if ( !s )
-        goto fail1;
+        return -ENOMEM;
 
     domain_pause(d);
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -EEXIST;
-    if ( is_default && d->arch.hvm_domain.default_ioreq_server != NULL )
-        goto fail2;
-
-    rc = hvm_ioreq_server_init(s, d, is_default, bufioreq_handling,
-                               next_ioservid(d));
-    if ( rc )
-        goto fail3;
-
-    list_add(&s->list_entry,
-             &d->arch.hvm_domain.ioreq_server.list);
-
     if ( is_default )
     {
-        d->arch.hvm_domain.default_ioreq_server = s;
-        hvm_ioreq_server_enable(s, true);
+        i = DEFAULT_IOSERVID;
+
+        rc = -EEXIST;
+        if ( GET_IOREQ_SERVER(d, i) )
+            goto fail;
     }
+    else
+    {
+        for ( i = 0; i < MAX_NR_IOREQ_SERVERS; i++ )
+        {
+            if ( i != DEFAULT_IOSERVID && !GET_IOREQ_SERVER(d, i) )
+                break;

xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_init(struct hvm_ioreq_server *s, · line 727

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+        }
+
+        rc = -ENOSPC;
+        if ( i >= MAX_NR_IOREQ_SERVERS )
+            goto fail;
+    }
+
+    set_ioreq_server(d, i, s);
+
+    rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i);

Is it safe to do the init only after the insertion? I guess all lock-less array traversals happen in context of the guest (which is paused here), but the old code did things the other way around anyway. So unless something breaks with the inverse order, I'd suggest to use that. If the order is required to be the way you have it, I'd again like to suggest to add a comment clarifying this is intentional.

Quoted 41 lines
xen/arch/x86/hvm/ioreq.c +13 -17
@@ -744,41 +754,38 @@ int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id)
     struct hvm_ioreq_server *s;
     int rc;
 
-    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+    if ( id == DEFAULT_IOSERVID )
+        return -EPERM;
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-        if ( s->id != id )
-            continue;
+    s = get_ioreq_server(d, id);
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        domain_pause(d);
+    ASSERT(!IS_DEFAULT(s));
 
-        p2m_set_ioreq_server(d, 0, s);
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        hvm_ioreq_server_disable(s, false);
+    domain_pause(d);
 
-        list_del(&s->list_entry);
+    p2m_set_ioreq_server(d, 0, s);

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 783

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4 -3
 
-        hvm_ioreq_server_deinit(s, false);
+    hvm_ioreq_server_disable(s);
+    hvm_ioreq_server_deinit(s);
 
-        domain_unpause(d);
+    domain_unpause(d);
 
-        xfree(s);
+    set_ioreq_server(d, id, NULL);

Same here then for the deinit vs list_del() / set_ioreq_server() ordering. Also (but perhaps less relevant) in hvm_destroy_all_ioreq_servers().

Quoted 7 lines
xen/arch/x86/hvm/ioreq.c +0 -3
@@ -1168,16 +1155,11 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
         addr = p->addr;
     }
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )

xen/arch/x86/hvm/ioreq.c struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, · line 1163

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +2 -4
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct rangeset *r;
 
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
-
-        if ( !s->enabled )
+        if ( IS_DEFAULT(s) )
             continue;

Do you really mean the "enabled" check to go away here? If so, why?

Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +0 -1
@@ -1369,13 +1351,13 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered)
 {
     struct domain *d = current->domain;
     struct hvm_ioreq_server *s;
-    unsigned int failed = 0;

xen/arch/x86/hvm/ioreq.c unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered) · line 1360

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4 -3
+    unsigned int id, failed = 0;
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
+    {
         if ( hvm_send_ioreq(s, p, buffered) == X86EMUL_UNHANDLEABLE )
             failed++;
+    }

Which in turn makes me wonder - should broadcasts really be sent to disabled servers?

Jan

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

#3 lore raw

Re: [PATCH v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 16 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:22
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:

v18:
- non-trivial re-base.
- small modification to FOR_EACH... macro to iterate backwards, to main- tain a previous undocumented but useful semantic that secondary emulators are selected in favour of qemu.

Quoted 4 lines

If this is intentional (and necessary), I think there should be a code comment saying why (and implicitly preventing people from wanting to change it).

Ok.

Quoted 6 lines

I'm also having difficulty to see why that was the case before this patch: hvm_create_ioreq_server() doesn't insert the default one at the list tail. Are you perhaps basing this solely on the assumption that secondary ones would be created after the default one?

It's not really about default vs. non-default. I was running my toy emulators from shell after starting up the guest paused. So I relied on (upstream) qemu being the first emulator to create its ioreq server, and then my toy emulator would be inserted ahead of it in the list.

Quoted 11 lines

Review static int hvm_ioreq_server_add_vcpu(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -316,7 +344,8 @@ static int hvm_ioreq_server_add_vcpu(struct

hvm_ioreq_server *s,

xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, · line 348

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +2 -1
     spin_lock(&s->lock);
 
     rc = alloc_unbound_xen_event_channel(v->domain, v->vcpu_id,
-                                         s->emulator->domain_id, NULL);
+                                         s->emulator->domain_id,
+                                         NULL);

Stray change?

Oh yes.

Quoted 56 lines
Quoted 56 lines
xen/arch/x86/hvm/ioreq.c +0 -0
 int hvm_create_ioreq_server(struct domain *d, bool is_default,
                             int bufioreq_handling, ioservid_t *id)
 {
     struct hvm_ioreq_server *s;
xen/arch/x86/hvm/ioreq.c +22 -16
+    unsigned int i;
     int rc;
 
     if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC )
         return -EINVAL;
 
-    rc = -ENOMEM;
     s = xzalloc(struct hvm_ioreq_server);
     if ( !s )
-        goto fail1;
+        return -ENOMEM;
 
     domain_pause(d);
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-    rc = -EEXIST;
-    if ( is_default && d->arch.hvm_domain.default_ioreq_server != NULL )
-        goto fail2;
-
-    rc = hvm_ioreq_server_init(s, d, is_default, bufioreq_handling,
-                               next_ioservid(d));
-    if ( rc )
-        goto fail3;
-
-    list_add(&s->list_entry,
-             &d->arch.hvm_domain.ioreq_server.list);
-
     if ( is_default )
     {
-        d->arch.hvm_domain.default_ioreq_server = s;
-        hvm_ioreq_server_enable(s, true);
+        i = DEFAULT_IOSERVID;
+
+        rc = -EEXIST;
+        if ( GET_IOREQ_SERVER(d, i) )
+            goto fail;
     }
+    else
+    {
+        for ( i = 0; i < MAX_NR_IOREQ_SERVERS; i++ )
+        {
+            if ( i != DEFAULT_IOSERVID && !GET_IOREQ_SERVER(d, i) )
+                break;
+        }
+
+        rc = -ENOSPC;
+        if ( i >= MAX_NR_IOREQ_SERVERS )
+            goto fail;
+    }
+
+    set_ioreq_server(d, i, s);
+
Quoted 8 lines
Quoted 1 line

+ rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i);

Is it safe to do the init only after the insertion? I guess all lock-less array traversals happen in context of the guest (which is paused here), but the old code did things the other way around anyway. So unless something breaks with the inverse order, I'd suggest to use that. If the order is required to be the way you have it, I'd again like to suggest to add a comment clarifying this is intentional.

Yes, it's safe for the reason you mention but I will switch it round since it does indeed look suspicious. I don't think there is any reason for it to be ordered this way now... it's possible I needed get_ioreq_server() to work correctly during some intermediate development step.

Quoted 51 lines

Review int hvm_destroy_ioreq_server(struct domain *d,

Quoted 2 lines
(quoted diff) +0 -0
@@ -744,41 +754,38 @@ int hvm_destroy_ioreq_server(struct domain *d,

ioservid_t id)

Quoted 48 lines
xen/arch/x86/hvm/ioreq.c +16 -19
     struct hvm_ioreq_server *s;
     int rc;
 
-    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+    if ( id == DEFAULT_IOSERVID )
+        return -EPERM;
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-        if ( s->id != id )
-            continue;
+    s = get_ioreq_server(d, id);
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        domain_pause(d);
+    ASSERT(!IS_DEFAULT(s));
 
-        p2m_set_ioreq_server(d, 0, s);
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        hvm_ioreq_server_disable(s, false);
+    domain_pause(d);
 
-        list_del(&s->list_entry);
+    p2m_set_ioreq_server(d, 0, s);
 
-        hvm_ioreq_server_deinit(s, false);
+    hvm_ioreq_server_disable(s);
+    hvm_ioreq_server_deinit(s);
 
-        domain_unpause(d);
+    domain_unpause(d);
 
Quoted 7 lines

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 783

Quoted 2 lines
xen/arch/x86/hvm/ioreq.c +1 -1
-        xfree(s);
+    set_ioreq_server(d, id, NULL);

Same here then for the deinit vs list_del() / set_ioreq_server() ordering. Also (but perhaps less relevant) in hvm_destroy_all_ioreq_servers().

I'll switch it round.

Quoted 12 lines

Review struct hvm_ioreq_server

Quoted 1 line
(quoted diff) +0 -0
@@ -1168,16 +1155,11 @@ struct hvm_ioreq_server

*hvm_select_ioreq_server(struct domain *d,

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1 -3
         addr = p->addr;
     }
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct rangeset *r;
 
Quoted 9 lines

xen/arch/x86/hvm/ioreq.c struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, · line 1163

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -4
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
-
-        if ( !s->enabled )
+        if ( IS_DEFAULT(s) )
             continue;

Do you really mean the "enabled" check to go away here? If so, why?

No, that check should still be there. I don't know how it got removed.

Quoted 17 lines

Review unsigned int hvm_broadcast_ioreq(ioreq_t *p,

Quoted 1 line
(quoted diff) +0 -0
@@ -1369,13 +1351,13 @@ unsigned int hvm_broadcast_ioreq(ioreq_t *p,

bool buffered)

Quoted 4 lines
xen/arch/x86/hvm/ioreq.c +0 -1
 {
     struct domain *d = current->domain;
     struct hvm_ioreq_server *s;
-    unsigned int failed = 0;

xen/arch/x86/hvm/ioreq.c unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered) · line 1360

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4 -3
+    unsigned int id, failed = 0;
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
+    {
         if ( hvm_send_ioreq(s, p, buffered) == X86EMUL_UNHANDLEABLE )
             failed++;
+    }
Quoted 3 lines

Which in turn makes me wonder - should broadcasts really be sent to disabled servers?

I can't think of a reason why we'd need them to be. I'll add a check there.

Paul

Quoted 1 line

Jan

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

#4 lore raw

Re: [PATCH v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 125 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 28 March 2018 15:44
To: 'Jan Beulich' <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [Xen-devel] [PATCH v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 16 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:22
To: Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; xen- [email protected]
Subject: Re: [PATCH v18 01/11] x86/hvm/ioreq: maintain an array of ioreq servers rather than a list

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:

v18:
- non-trivial re-base.
- small modification to FOR_EACH... macro to iterate backwards, to main- tain a previous undocumented but useful semantic that secondary emulators are selected in favour of qemu.

Quoted 4 lines

If this is intentional (and necessary), I think there should be a code comment saying why (and implicitly preventing people from wanting to change it).

Ok.

Quoted 6 lines

I'm also having difficulty to see why that was the case before this patch: hvm_create_ioreq_server() doesn't insert the default one at the list tail. Are you perhaps basing this solely on the assumption that secondary ones would be created after the default one?

It's not really about default vs. non-default. I was running my toy emulators from shell after starting up the guest paused. So I relied on (upstream) qemu being the first emulator to create its ioreq server, and then my toy emulator would be inserted ahead of it in the list.

Quoted 11 lines

Review static int hvm_ioreq_server_add_vcpu(struct

Quoted 1 line
(quoted diff) +0 -0
@@ -316,7 +344,8 @@ static int hvm_ioreq_server_add_vcpu(struct

hvm_ioreq_server *s,

xen/arch/x86/hvm/ioreq.c static int hvm_ioreq_server_add_vcpu(struct hvm_ioreq_server *s, · line 348

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +2 -1
     spin_lock(&s->lock);
 
     rc = alloc_unbound_xen_event_channel(v->domain, v->vcpu_id,
-                                         s->emulator->domain_id, NULL);
+                                         s->emulator->domain_id,
+                                         NULL);

Stray change?

Oh yes.

Quoted 16 lines
Quoted 16 lines
xen/arch/x86/hvm/ioreq.c +0 -0
 int hvm_create_ioreq_server(struct domain *d, bool is_default,
                             int bufioreq_handling, ioservid_t *id)
 {
     struct hvm_ioreq_server *s;
xen/arch/x86/hvm/ioreq.c +2 -2
+    unsigned int i;
     int rc;
 
     if ( bufioreq_handling > HVM_IOREQSRV_BUFIOREQ_ATOMIC )
         return -EINVAL;
 
-    rc = -ENOMEM;
     s = xzalloc(struct hvm_ioreq_server);
     if ( !s )
-        goto fail1;
+        return -ENOMEM;
Quoted 5 lines

Review

Quoted 5 lines
     domain_pause(d);
     spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
(quoted diff) +0 -2
-    rc = -EEXIST;
-    if ( is_default && d->arch.hvm_domain.default_ioreq_server != NULL

)

Quoted 35 lines
Quoted 35 lines
xen/arch/x86/hvm/ioreq.c +20 -12
-        goto fail2;
-
-    rc = hvm_ioreq_server_init(s, d, is_default, bufioreq_handling,
-                               next_ioservid(d));
-    if ( rc )
-        goto fail3;
-
-    list_add(&s->list_entry,
-             &d->arch.hvm_domain.ioreq_server.list);
-
     if ( is_default )
     {
-        d->arch.hvm_domain.default_ioreq_server = s;
-        hvm_ioreq_server_enable(s, true);
+        i = DEFAULT_IOSERVID;
+
+        rc = -EEXIST;
+        if ( GET_IOREQ_SERVER(d, i) )
+            goto fail;
     }
+    else
+    {
+        for ( i = 0; i < MAX_NR_IOREQ_SERVERS; i++ )
+        {
+            if ( i != DEFAULT_IOSERVID && !GET_IOREQ_SERVER(d, i) )
+                break;
+        }
+
+        rc = -ENOSPC;
+        if ( i >= MAX_NR_IOREQ_SERVERS )
+            goto fail;
+    }
+
+    set_ioreq_server(d, i, s);
+
Quoted 8 lines
Quoted 1 line

+ rc = hvm_ioreq_server_init(s, d, bufioreq_handling, i);

Is it safe to do the init only after the insertion? I guess all lock-less array traversals happen in context of the guest (which is paused here), but the old code did things the other way around anyway. So unless something breaks with the inverse order, I'd suggest to use that. If the order is required to be the way you have it, I'd again like to suggest to add a comment clarifying this is intentional.

Quoted 5 lines

Yes, it's safe for the reason you mention but I will switch it round since it does indeed look suspicious. I don't think there is any reason for it to be ordered this way now... it's possible I needed get_ioreq_server() to work correctly during some intermediate development step.

No, it still needs to be this way round. I need IS_DEFAULT() to work correctly in some sub-functions. I'll add comments.

Paul

Quoted 110 lines
Quoted 2 lines

Review int hvm_destroy_ioreq_server(struct domain

Quoted 2 lines
(quoted diff) +0 -0
@@ -744,41 +754,38 @@ int hvm_destroy_ioreq_server(struct domain

*d,

Quoted 49 lines

ioservid_t id)

Quoted 48 lines
xen/arch/x86/hvm/ioreq.c +16 -19
     struct hvm_ioreq_server *s;
     int rc;
 
-    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+    if ( id == DEFAULT_IOSERVID )
+        return -EPERM;
 
-    rc = -ENOENT;
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
-    {
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
+    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
 
-        if ( s->id != id )
-            continue;
+    s = get_ioreq_server(d, id);
 
-        rc = -EPERM;
-        if ( s->emulator != current->domain )
-            break;
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
 
-        domain_pause(d);
+    ASSERT(!IS_DEFAULT(s));
 
-        p2m_set_ioreq_server(d, 0, s);
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
 
-        hvm_ioreq_server_disable(s, false);
+    domain_pause(d);
 
-        list_del(&s->list_entry);
+    p2m_set_ioreq_server(d, 0, s);
 
-        hvm_ioreq_server_deinit(s, false);
+    hvm_ioreq_server_disable(s);
+    hvm_ioreq_server_deinit(s);
 
-        domain_unpause(d);
+    domain_unpause(d);
 
Quoted 7 lines

xen/arch/x86/hvm/ioreq.c int hvm_destroy_ioreq_server(struct domain *d, ioservid_t id) · line 783

Quoted 2 lines
xen/arch/x86/hvm/ioreq.c +1 -1
-        xfree(s);
+    set_ioreq_server(d, id, NULL);

Same here then for the deinit vs list_del() / set_ioreq_server() ordering. Also (but perhaps less relevant) in hvm_destroy_all_ioreq_servers().

I'll switch it round.

Quoted 12 lines

Review struct hvm_ioreq_server

Quoted 1 line
(quoted diff) +0 -0
@@ -1168,16 +1155,11 @@ struct hvm_ioreq_server

*hvm_select_ioreq_server(struct domain *d,

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +1 -3
         addr = p->addr;
     }
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct rangeset *r;
 
Quoted 9 lines

xen/arch/x86/hvm/ioreq.c struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d, · line 1163

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -4
-        if ( s == d->arch.hvm_domain.default_ioreq_server )
-            continue;
-
-        if ( !s->enabled )
+        if ( IS_DEFAULT(s) )
             continue;

Do you really mean the "enabled" check to go away here? If so, why?

No, that check should still be there. I don't know how it got removed.

Quoted 1 line

Review unsigned int hvm_broadcast_ioreq(ioreq_t

Quoted 1 line
(quoted diff) +0 -0
@@ -1369,13 +1351,13 @@ unsigned int hvm_broadcast_ioreq(ioreq_t

*p,

Quoted 16 lines

bool buffered)

Quoted 4 lines
xen/arch/x86/hvm/ioreq.c +0 -1
 {
     struct domain *d = current->domain;
     struct hvm_ioreq_server *s;
-    unsigned int failed = 0;

xen/arch/x86/hvm/ioreq.c unsigned int hvm_broadcast_ioreq(ioreq_t *p, bool buffered) · line 1360

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +4 -3
+    unsigned int id, failed = 0;
 
-    list_for_each_entry ( s,
-                          &d->arch.hvm_domain.ioreq_server.list,
-                          list_entry )
+    FOR_EACH_IOREQ_SERVER(d, id, s)
+    {
         if ( hvm_send_ioreq(s, p, buffered) == X86EMUL_UNHANDLEABLE )
             failed++;
+    }
Quoted 3 lines

Which in turn makes me wonder - should broadcasts really be sent to disabled servers?

I can't think of a reason why we'd need them to be. I'll add a check there.

Quoted 8 lines

Paul

Quoted 1 line

Jan

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

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

#5 lore raw

PATCH v18 2/11 x86/hvm/ioreq: simplify code and use consistent naming

In reply to Paul Durrant

This patch re-works much of the ioreq server initialization and teardown code:

- The hvm_map/unmap_ioreq_gfn() functions are expanded to call through to hvm_alloc/free_ioreq_gfn() rather than expecting them to be called separately by outer functions.
- Several functions now test the validity of the hvm_ioreq_page gfn value to determine whether they need to act. This means can be safely called for the bufioreq page even when it is not used.
- hvm_add/remove_ioreq_gfn() simply return in the case of the default IOREQ server so callers no longer need to test before calling.
- hvm_ioreq_server_setup_pages() is renamed to hvm_ioreq_server_map_pages() to mirror the existing hvm_ioreq_server_unmap_pages().

All of this significantly shortens the code.

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

v18:
- Trivial re-base.

v3:
- Re-based on top of 's->is_default' to 'IS_DEFAULT(s)' changes.
- Minor updates in response to review comments from Roger.

---
 xen/arch/x86/hvm/ioreq.c | 182 ++++++++++++++++++-----------------------------
 1 file changed, 69 insertions(+), 113 deletions(-)
xen/arch/x86/hvm/ioreq.c +69 -113
index d8d4e96a80..bd141db0d5 100644
@@ -210,63 +210,75 @@ bool handle_hvm_io_completion(struct vcpu *v)
     return true;
 }
 
-static int hvm_alloc_ioreq_gfn(struct domain *d, unsigned long *gfn)
+static unsigned long hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s)
 {
+    struct domain *d = s->target;
     unsigned int i;
-    int rc;
 
-    rc = -ENOMEM;
+    ASSERT(!IS_DEFAULT(s));
+
     for ( i = 0; i < sizeof(d->arch.hvm_domain.ioreq_gfn.mask) * 8; i++ )
     {
         if ( test_and_clear_bit(i, &d->arch.hvm_domain.ioreq_gfn.mask) )
-        {
-            *gfn = d->arch.hvm_domain.ioreq_gfn.base + i;
-            rc = 0;
-            break;
-        }
+            return d->arch.hvm_domain.ioreq_gfn.base + i;
     }
 
-    return rc;
+    return gfn_x(INVALID_GFN);
 }
 
-static void hvm_free_ioreq_gfn(struct domain *d, unsigned long gfn)
+static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s,
+                               unsigned long gfn)
 {
+    struct domain *d = s->target;
     unsigned int i = gfn - d->arch.hvm_domain.ioreq_gfn.base;
 
-    if ( gfn != gfn_x(INVALID_GFN) )
-        set_bit(i, &d->arch.hvm_domain.ioreq_gfn.mask);
+    ASSERT(!IS_DEFAULT(s));
+    ASSERT(gfn != gfn_x(INVALID_GFN));
+
+    set_bit(i, &d->arch.hvm_domain.ioreq_gfn.mask);
 }
 
-static void hvm_unmap_ioreq_page(struct hvm_ioreq_server *s, bool buf)
+static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
 {
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
 
+    if ( iorp->gfn == gfn_x(INVALID_GFN) )
+        return;
+
     destroy_ring_for_helper(&iorp->va, iorp->page);
+    iorp->page = NULL;
+
+    if ( !IS_DEFAULT(s) )
+        hvm_free_ioreq_gfn(s, iorp->gfn);
+
+    iorp->gfn = gfn_x(INVALID_GFN);
 }
 
-static int hvm_map_ioreq_page(
-    struct hvm_ioreq_server *s, bool buf, unsigned long gfn)
+static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
 {
     struct domain *d = s->target;
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
-    struct page_info *page;
-    void *va;
     int rc;
 
-    if ( (rc = prepare_ring_for_helper(d, gfn, &page, &va)) )
-        return rc;
-
-    if ( (iorp->va != NULL) || d->is_dying )
-    {
-        destroy_ring_for_helper(&va, page);
+    if ( d->is_dying )
         return -EINVAL;
-    }
 
-    iorp->va = va;
-    iorp->page = page;
-    iorp->gfn = gfn;
+    if ( IS_DEFAULT(s) )
+        iorp->gfn = buf ?
+                    d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_PFN] :
+                    d->arch.hvm_domain.params[HVM_PARAM_IOREQ_PFN];
+    else
+        iorp->gfn = hvm_alloc_ioreq_gfn(s);
 
-    return 0;
+    if ( iorp->gfn == gfn_x(INVALID_GFN) )
+        return -ENOMEM;
+
+    rc = prepare_ring_for_helper(d, iorp->gfn, &iorp->page, &iorp->va);
+
+    if ( rc )
+        hvm_unmap_ioreq_gfn(s, buf);
+
+    return rc;
 }
 
 bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
@@ -279,8 +291,7 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
 
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
-        if ( (s->ioreq.va && s->ioreq.page == page) ||
-             (s->bufioreq.va && s->bufioreq.page == page) )
+        if ( (s->ioreq.page == page) || (s->bufioreq.page == page) )
         {
             found = true;
             break;
@@ -292,20 +303,30 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
     return found;
 }
 
-static void hvm_remove_ioreq_gfn(
-    struct domain *d, struct hvm_ioreq_page *iorp)
+static void hvm_remove_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
+
 {
+    struct domain *d = s->target;
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+
+    if ( IS_DEFAULT(s) || iorp->gfn == gfn_x(INVALID_GFN) )
+        return;
+
     if ( guest_physmap_remove_page(d, _gfn(iorp->gfn),
                                    _mfn(page_to_mfn(iorp->page)), 0) )
         domain_crash(d);
     clear_page(iorp->va);
 }
 
-static int hvm_add_ioreq_gfn(
-    struct domain *d, struct hvm_ioreq_page *iorp)
+static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
 {
+    struct domain *d = s->target;
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
     int rc;
 
+    if ( IS_DEFAULT(s) || iorp->gfn == gfn_x(INVALID_GFN) )
+        return 0;
+
     clear_page(iorp->va);
 
     rc = guest_physmap_add_page(d, _gfn(iorp->gfn),
@@ -442,78 +463,25 @@ static void hvm_ioreq_server_remove_all_vcpus(struct hvm_ioreq_server *s)
 }
 
 static int hvm_ioreq_server_map_pages(struct hvm_ioreq_server *s,
-                                      unsigned long ioreq_gfn,
-                                      unsigned long bufioreq_gfn)
+                                      bool handle_bufioreq)
 {
     int rc;
 
-    rc = hvm_map_ioreq_page(s, false, ioreq_gfn);
-    if ( rc )
-        return rc;
-
-    if ( bufioreq_gfn != gfn_x(INVALID_GFN) )
-        rc = hvm_map_ioreq_page(s, true, bufioreq_gfn);
-
-    if ( rc )
-        hvm_unmap_ioreq_page(s, false);
-
-    return rc;
-}
-
-static int hvm_ioreq_server_setup_pages(struct hvm_ioreq_server *s,
-                                        bool handle_bufioreq)
-{
-    struct domain *d = s->target;
-    unsigned long ioreq_gfn = gfn_x(INVALID_GFN);
-    unsigned long bufioreq_gfn = gfn_x(INVALID_GFN);
-    int rc;
-
-    if ( IS_DEFAULT(s) )
-    {
-        /*
-         * The default ioreq server must handle buffered ioreqs, for
-         * backwards compatibility.
-         */
-        ASSERT(handle_bufioreq);
-        return hvm_ioreq_server_map_pages(s,
-                   d->arch.hvm_domain.params[HVM_PARAM_IOREQ_PFN],
-                   d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_PFN]);
-    }
-
-    rc = hvm_alloc_ioreq_gfn(d, &ioreq_gfn);
+    rc = hvm_map_ioreq_gfn(s, false);
 
     if ( !rc && handle_bufioreq )
-        rc = hvm_alloc_ioreq_gfn(d, &bufioreq_gfn);
-
-    if ( !rc )
-        rc = hvm_ioreq_server_map_pages(s, ioreq_gfn, bufioreq_gfn);
+        rc = hvm_map_ioreq_gfn(s, true);
 
     if ( rc )
-    {
-        hvm_free_ioreq_gfn(d, ioreq_gfn);
-        hvm_free_ioreq_gfn(d, bufioreq_gfn);
-    }
+        hvm_unmap_ioreq_gfn(s, false);
 
     return rc;
 }
 
 static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s)
 {
-    struct domain *d = s->target;
-    bool handle_bufioreq = !!s->bufioreq.va;
-
-    if ( handle_bufioreq )
-        hvm_unmap_ioreq_page(s, true);
-
-    hvm_unmap_ioreq_page(s, false);
-
-    if ( !IS_DEFAULT(s) )
-    {
-        if ( handle_bufioreq )
-            hvm_free_ioreq_gfn(d, s->bufioreq.gfn);
-
-        hvm_free_ioreq_gfn(d, s->ioreq.gfn);
-    }
+    hvm_unmap_ioreq_gfn(s, true);
+    hvm_unmap_ioreq_gfn(s, false);
 }
 
 static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s)
@@ -573,22 +541,15 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
 
 static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s)
 {
-    struct domain *d = s->target;
     struct hvm_ioreq_vcpu *sv;
-    bool handle_bufioreq = !!s->bufioreq.va;
 
     spin_lock(&s->lock);
 
     if ( s->enabled )
         goto done;
 
-    if ( !IS_DEFAULT(s) )
-    {
-        hvm_remove_ioreq_gfn(d, &s->ioreq);
-
-        if ( handle_bufioreq )
-            hvm_remove_ioreq_gfn(d, &s->bufioreq);
-    }
+    hvm_remove_ioreq_gfn(s, false);
+    hvm_remove_ioreq_gfn(s, true);
 
     s->enabled = true;
 
@@ -603,21 +564,13 @@ static void hvm_ioreq_server_enable(struct hvm_ioreq_server *s)
 
 static void hvm_ioreq_server_disable(struct hvm_ioreq_server *s)
 {
-    struct domain *d = s->target;
-    bool handle_bufioreq = !!s->bufioreq.va;
-
     spin_lock(&s->lock);
 
     if ( !s->enabled )
         goto done;
 
-    if ( !IS_DEFAULT(s) )
-    {
-        if ( handle_bufioreq )
-            hvm_add_ioreq_gfn(d, &s->bufioreq);
-
-        hvm_add_ioreq_gfn(d, &s->ioreq);
-    }
+    hvm_add_ioreq_gfn(s, true);
+    hvm_add_ioreq_gfn(s, false);
 
     s->enabled = false;
 
@@ -642,6 +595,9 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
     INIT_LIST_HEAD(&s->ioreq_vcpu_list);
     spin_lock_init(&s->bufioreq_lock);
 
+    s->ioreq.gfn = gfn_x(INVALID_GFN);
+    s->bufioreq.gfn = gfn_x(INVALID_GFN);
+
     rc = hvm_ioreq_server_alloc_rangesets(s, id);
     if ( rc )
         return rc;
@@ -649,7 +605,7 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
     if ( bufioreq_handling == HVM_IOREQSRV_BUFIOREQ_ATOMIC )
         s->bufioreq_atomic = true;
 
-    rc = hvm_ioreq_server_setup_pages(
+    rc = hvm_ioreq_server_map_pages(
              s, bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF);
     if ( rc )
         goto fail_map;
Signature · 7 lines
2.11.0


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

PATCH v18 3/11 x86/hvm/ioreq: use gfn_t in struct hvm_ioreq_page

In reply to Paul Durrant

This patch adjusts the ioreq server code to use type-safe gfn_t values where possible. No functional change.

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

v18:
- Trivial re-base.

---
 xen/arch/x86/hvm/ioreq.c         | 46 ++++++++++++++++++++--------------------
 xen/include/asm-x86/hvm/domain.h |  2 +-
 2 files changed, 24 insertions(+), 24 deletions(-)
xen/arch/x86/hvm/ioreq.c +23 -23
index bd141db0d5..d5f0e24b98 100644
@@ -210,7 +210,7 @@ bool handle_hvm_io_completion(struct vcpu *v)
     return true;
 }
 
-static unsigned long hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s)
+static gfn_t hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s)
 {
     struct domain *d = s->target;
     unsigned int i;
@@ -220,20 +220,19 @@ static unsigned long hvm_alloc_ioreq_gfn(struct hvm_ioreq_server *s)
     for ( i = 0; i < sizeof(d->arch.hvm_domain.ioreq_gfn.mask) * 8; i++ )
     {
         if ( test_and_clear_bit(i, &d->arch.hvm_domain.ioreq_gfn.mask) )
-            return d->arch.hvm_domain.ioreq_gfn.base + i;
+            return _gfn(d->arch.hvm_domain.ioreq_gfn.base + i);
     }
 
-    return gfn_x(INVALID_GFN);
+    return INVALID_GFN;
 }
 
-static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s,
-                               unsigned long gfn)
+static void hvm_free_ioreq_gfn(struct hvm_ioreq_server *s, gfn_t gfn)
 {
     struct domain *d = s->target;
-    unsigned int i = gfn - d->arch.hvm_domain.ioreq_gfn.base;
+    unsigned int i = gfn_x(gfn) - d->arch.hvm_domain.ioreq_gfn.base;
 
     ASSERT(!IS_DEFAULT(s));
-    ASSERT(gfn != gfn_x(INVALID_GFN));
+    ASSERT(!gfn_eq(gfn, INVALID_GFN));
 
     set_bit(i, &d->arch.hvm_domain.ioreq_gfn.mask);
 }
@@ -242,7 +241,7 @@ static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
 {
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
 
-    if ( iorp->gfn == gfn_x(INVALID_GFN) )
+    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
         return;
 
     destroy_ring_for_helper(&iorp->va, iorp->page);
@@ -251,7 +250,7 @@ static void hvm_unmap_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     if ( !IS_DEFAULT(s) )
         hvm_free_ioreq_gfn(s, iorp->gfn);
 
-    iorp->gfn = gfn_x(INVALID_GFN);
+    iorp->gfn = INVALID_GFN;
 }
 
 static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
@@ -264,16 +263,17 @@ static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
         return -EINVAL;
 
     if ( IS_DEFAULT(s) )
-        iorp->gfn = buf ?
-                    d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_PFN] :
-                    d->arch.hvm_domain.params[HVM_PARAM_IOREQ_PFN];
+        iorp->gfn = _gfn(buf ?
+                         d->arch.hvm_domain.params[HVM_PARAM_BUFIOREQ_PFN] :
+                         d->arch.hvm_domain.params[HVM_PARAM_IOREQ_PFN]);
     else
         iorp->gfn = hvm_alloc_ioreq_gfn(s);
 
-    if ( iorp->gfn == gfn_x(INVALID_GFN) )
+    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
         return -ENOMEM;
 
-    rc = prepare_ring_for_helper(d, iorp->gfn, &iorp->page, &iorp->va);
+    rc = prepare_ring_for_helper(d, gfn_x(iorp->gfn), &iorp->page,
+                                 &iorp->va);
 
     if ( rc )
         hvm_unmap_ioreq_gfn(s, buf);
@@ -309,10 +309,10 @@ static void hvm_remove_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     struct domain *d = s->target;
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
 
-    if ( IS_DEFAULT(s) || iorp->gfn == gfn_x(INVALID_GFN) )
+    if ( IS_DEFAULT(s) || gfn_eq(iorp->gfn, INVALID_GFN) )
         return;
 
-    if ( guest_physmap_remove_page(d, _gfn(iorp->gfn),
+    if ( guest_physmap_remove_page(d, iorp->gfn,
                                    _mfn(page_to_mfn(iorp->page)), 0) )
         domain_crash(d);
     clear_page(iorp->va);
@@ -324,15 +324,15 @@ static int hvm_add_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
     int rc;
 
-    if ( IS_DEFAULT(s) || iorp->gfn == gfn_x(INVALID_GFN) )
+    if ( IS_DEFAULT(s) || gfn_eq(iorp->gfn, INVALID_GFN) )
         return 0;
 
     clear_page(iorp->va);
 
-    rc = guest_physmap_add_page(d, _gfn(iorp->gfn),
+    rc = guest_physmap_add_page(d, iorp->gfn,
                                 _mfn(page_to_mfn(iorp->page)), 0);
     if ( rc == 0 )
-        paging_mark_pfn_dirty(d, _pfn(iorp->gfn));
+        paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn)));
 
     return rc;
 }
@@ -595,8 +595,8 @@ static int hvm_ioreq_server_init(struct hvm_ioreq_server *s,
     INIT_LIST_HEAD(&s->ioreq_vcpu_list);
     spin_lock_init(&s->bufioreq_lock);
 
-    s->ioreq.gfn = gfn_x(INVALID_GFN);
-    s->bufioreq.gfn = gfn_x(INVALID_GFN);
+    s->ioreq.gfn = INVALID_GFN;
+    s->bufioreq.gfn = INVALID_GFN;
 
     rc = hvm_ioreq_server_alloc_rangesets(s, id);
     if ( rc )
@@ -772,11 +772,11 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
     if ( s->emulator != current->domain )
         goto out;
 
-    *ioreq_gfn = s->ioreq.gfn;
+    *ioreq_gfn = gfn_x(s->ioreq.gfn);
 
     if ( s->bufioreq.va != NULL )
     {
-        *bufioreq_gfn = s->bufioreq.gfn;
+        *bufioreq_gfn = gfn_x(s->bufioreq.gfn);
         *bufioreq_port = s->bufioreq_evtchn;
     }
 
xen/include/asm-x86/hvm/domain.h +1 -1
index 2e4d85f6fe..9b73e80777 100644
@@ -36,7 +36,7 @@
 #include <public/hvm/dm_op.h>
 
 struct hvm_ioreq_page {
-    unsigned long gfn;
+    gfn_t gfn;
     struct page_info *page;
     void *va;
 };
Signature · 7 lines
2.11.0


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

PATCH v18 4/11 x86/hvm/ioreq: defer mapping gfns until they are actually requested

In reply to Paul Durrant

A subsequent patch will introduce a new scheme to allow an emulator to map ioreq server pages directly from Xen rather than the guest P2M.

This patch lays the groundwork for that change by deferring mapping of gfns until their values are requested by an emulator. To that end, the pad field of the xen_dm_op_get_ioreq_server_info structure is re-purposed to a flags field and new flag, XEN_DMOP_no_gfns, defined which modifies the behaviour of XEN_DMOP_get_ioreq_server_info to allow the caller to avoid requesting the gfn values.

---
Cc: Ian Jackson <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Chao Gao <[email protected]>

v18:
- Trivial re-base.

v17:
- Fix typo in commit comment.

v16:
- Leave call to map pages in hvm_ioreq_server_init() for default ioreq server instance, as pointed out by Chao (cc-ed). This is small and obvious change which reduces the size of the patch, so I have left existent R-bs and A-bs in place.

v8:
- For safety make all of the pointers passed to
hvm_get_ioreq_server_info() optional.
- Shrink bufioreq_handling down to a uint8_t.

v3:
- Updated in response to review comments from Wei and Roger.
- Added a HANDLE_BUFIOREQ macro to make the code neater.
- This patch no longer introduces a security vulnerability since there is now an explicit limit on the number of ioreq servers that may be created for any one domain.

---
 tools/libs/devicemodel/core.c                   |  8 ++++
 tools/libs/devicemodel/include/xendevicemodel.h |  6 +--
 xen/arch/x86/hvm/dm.c                           |  9 +++--
 xen/arch/x86/hvm/ioreq.c                        | 49 ++++++++++++++++---------
 xen/include/asm-x86/hvm/domain.h                |  2 +-
 xen/include/public/hvm/dm_op.h                  | 32 +++++++++-------
 6 files changed, 69 insertions(+), 37 deletions(-)
tools/libs/devicemodel/core.c +8 -0
index 23924e9a38..f76e3d305e 100644
@@ -204,6 +204,14 @@ int xendevicemodel_get_ioreq_server_info(
 
     data->id = id;
 
+    /*
+     * If the caller is not requesting gfn values then instruct the
+     * hypercall not to retrieve them as this may cause them to be
+     * mapped.
+     */
+    if (!ioreq_gfn && !bufioreq_gfn)
+        data->flags |= XEN_DMOP_no_gfns;
+
     rc = xendevicemodel_op(dmod, domid, 1, &op, sizeof(op));
     if (rc)
         return rc;
tools/libs/devicemodel/include/xendevicemodel.h +3 -3
index 7629c35df7..08cb0d4374 100644
@@ -61,11 +61,11 @@ int xendevicemodel_create_ioreq_server(
  * @parm domid the domain id to be serviced
  * @parm id the IOREQ Server id.
  * @parm ioreq_gfn pointer to a xen_pfn_t to receive the synchronous ioreq
- *                  gfn
+ *                  gfn. (May be NULL if not required)
  * @parm bufioreq_gfn pointer to a xen_pfn_t to receive the buffered ioreq
- *                    gfn
+ *                    gfn. (May be NULL if not required)
  * @parm bufioreq_port pointer to a evtchn_port_t to receive the buffered
- *                     ioreq event channel
+ *                     ioreq event channel. (May be NULL if not required)
  * @return 0 on success, -1 on failure.
  */
 int xendevicemodel_get_ioreq_server_info(
xen/arch/x86/hvm/dm.c +6 -3
index 96b0d13f2f..ce18754442 100644
@@ -420,16 +420,19 @@ static int dm_op(const struct dmop_args *op_args)
     {
         struct xen_dm_op_get_ioreq_server_info *data =
             &op.u.get_ioreq_server_info;
+        const uint16_t valid_flags = XEN_DMOP_no_gfns;
 
         const_op = false;
 
         rc = -EINVAL;
-        if ( data->pad )
+        if ( data->flags & ~valid_flags )
             break;
 
         rc = hvm_get_ioreq_server_info(d, data->id,
-                                       &data->ioreq_gfn,
-                                       &data->bufioreq_gfn,
+                                       (data->flags & XEN_DMOP_no_gfns) ?
+                                       NULL : &data->ioreq_gfn,
+                                       (data->flags & XEN_DMOP_no_gfns) ?
+                                       NULL : &data->bufioreq_gfn,
                                        &data->bufioreq_port);
         break;
     }
xen/arch/x86/hvm/ioreq.c +32 -17

105 lines not on this page · raw message

xen/include/asm-x86/hvm/domain.h +1 -1
index 9b73e80777..f6db8586c5 100644
@@ -66,7 +66,7 @@ struct hvm_ioreq_server {
     evtchn_port_t          bufioreq_evtchn;
     struct rangeset        *range[NR_IO_RANGE_TYPES];
     bool                   enabled;
-    bool                   bufioreq_atomic;
+    uint8_t                bufioreq_handling;
 };
 
 /*
xen/include/public/hvm/dm_op.h +19 -13
index 04c6324397..9823726d79 100644
@@ -79,28 +79,34 @@ struct xen_dm_op_create_ioreq_server {
  * XEN_DMOP_get_ioreq_server_info: Get all the information necessary to
  *                                 access IOREQ Server <id>.
  *
- * The emulator needs to map the synchronous ioreq structures and buffered
- * ioreq ring (if it exists) that Xen uses to request emulation. These are
- * hosted in the target domain's gmfns <ioreq_gfn> and <bufioreq_gfn>
- * respectively. In addition, if the IOREQ Server is handling buffered
- * emulation requests, the emulator needs to bind to event channel
- * <bufioreq_port> to listen for them. (The event channels used for
- * synchronous emulation requests are specified in the per-CPU ioreq
- * structures in <ioreq_gfn>).
- * If the IOREQ Server is not handling buffered emulation requests then the
- * values handed back in <bufioreq_gfn> and <bufioreq_port> will both be 0.
+ * If the IOREQ Server is handling buffered emulation requests, the
+ * emulator needs to bind to event channel <bufioreq_port> to listen for
+ * them. (The event channels used for synchronous emulation requests are
+ * specified in the per-CPU ioreq structures).
+ * In addition, if the XENMEM_acquire_resource memory op cannot be used,
+ * the emulator will need to map the synchronous ioreq structures and
+ * buffered ioreq ring (if it exists) from guest memory. If <flags> does
+ * not contain XEN_DMOP_no_gfns then these pages will be made available and
+ * the frame numbers passed back in gfns <ioreq_gfn> and <bufioreq_gfn>
+ * respectively. (If the IOREQ Server is not handling buffered emulation
+ * only <ioreq_gfn> will be valid).
  */
 #define XEN_DMOP_get_ioreq_server_info 2
 
 struct xen_dm_op_get_ioreq_server_info {
     /* IN - server id */
     ioservid_t id;
-    uint16_t pad;
+    /* IN - flags */
+    uint16_t flags;
+
+#define _XEN_DMOP_no_gfns 0
+#define XEN_DMOP_no_gfns (1u << _XEN_DMOP_no_gfns)
+
     /* OUT - buffered ioreq port */
     evtchn_port_t bufioreq_port;
-    /* OUT - sync ioreq gfn */
+    /* OUT - sync ioreq gfn (see block comment above) */
     uint64_aligned_t ioreq_gfn;
-    /* OUT - buffered ioreq gfn */
+    /* OUT - buffered ioreq gfn (see block comment above)*/
     uint64_aligned_t bufioreq_gfn;
 };
 
Signature · 7 lines
2.11.0


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

PATCH v18 5/11 x86/mm: add HYPERVISOR_memory_op to acquire guest resources

In reply to Paul Durrant

Certain memory resources associated with a guest are not necessarily present in the guest P2M.

This patch adds the boilerplate for new memory op to allow such a resource to be priv-mapped directly, by either a PV or HVM tools domain.

NOTE: Whilst the new op is not intrinsicly specific to the x86 architecture,
I have no means to test it on an ARM platform and so cannot verify that it functions correctly.

---
Cc: Jan Beulich <[email protected]>
Cc: George Dunlap <[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]>
Cc: Julien Grall <[email protected]>

v18:
- Allow the resource page owner to be specified by a returned flag.
- Drop Jan's R-b due to change.

v14:
- Addressed more comments from Jan.

v13:
- Use xen_pfn_t for mfn_list.
- Addressed further comments from Jan and Julien.

v12:
- Addressed more comments form Jan.
- Removed #ifdef CONFIG_X86 from common code and instead introduced a stub set_foreign_p2m_entry() in asm-arm/p2m.h returning -EOPNOTSUPP.
- Restricted mechanism for querying implementation limit on nr_frames and simplified compat code.

v11:
- Addressed more comments from Jan.

v9:
- Addressed more comments from Jan.

v8:
- Move the code into common as requested by Jan.
- Make the gmfn_list handle a 64-bit type to avoid limiting the MFN range for a 32-bit tools domain.
- Add missing pad.
- Add compat code.
- Make this patch deal with purely boilerplate.
- Drop George's A-b and Wei's R-b because the changes are non-trivial, and update Cc list now the boilerplate is common.

v5:
- Switched __copy_to/from_guest_offset() to copy_to/from_guest_offset().

---
 tools/flask/policy/modules/xen.if   |   4 +-
 xen/arch/x86/mm/p2m.c               |   3 +-
 xen/common/compat/memory.c          | 100 ++++++++++++++++++++++++++++++++++++
 xen/common/memory.c                 |  93 +++++++++++++++++++++++++++++++++
 xen/include/asm-arm/p2m.h           |  10 ++++
 xen/include/asm-x86/p2m.h           |   3 ++
 xen/include/public/memory.h         |  55 +++++++++++++++++++-
 xen/include/xlat.lst                |   1 +
 xen/include/xsm/dummy.h             |   6 +++
 xen/include/xsm/xsm.h               |   6 +++
 xen/xsm/dummy.c                     |   1 +
 xen/xsm/flask/hooks.c               |   6 +++
 xen/xsm/flask/policy/access_vectors |   2 +
 13 files changed, 286 insertions(+), 4 deletions(-)
tools/flask/policy/modules/xen.if +3 -1
index 459880bb01..7aefd0061e 100644
@@ -52,7 +52,8 @@ define(`create_domain_common', `
 			settime setdomainhandle getvcpucontext set_misc_info };
 	allow $1 $2:domain2 { set_cpuid settsc setscheduler setclaim
 			set_max_evtchn set_vnumainfo get_vnumainfo cacheflush
-			psr_cmt_op psr_alloc soft_reset set_gnttab_limits };
+			psr_cmt_op psr_alloc soft_reset set_gnttab_limits
+			resource_map };
 	allow $1 $2:security check_context;
 	allow $1 $2:shadow enable;
 	allow $1 $2:mmu { map_read map_write adjust memorymap physmap pinpage mmuext_op updatemp };
@@ -152,6 +153,7 @@ define(`device_model', `
 	allow $1 $2_target:domain { getdomaininfo shutdown };
 	allow $1 $2_target:mmu { map_read map_write adjust physmap target_hack };
 	allow $1 $2_target:hvm { getparam setparam hvmctl dm };
+	allow $1 $2_target:domain2 resource_map;
 ')
 
 # make_device_model(priv, dm_dom, hvm_dom)
xen/arch/x86/mm/p2m.c +1 -2
index 48e50fb5d8..55693eba59 100644
@@ -1132,8 +1132,7 @@ static int set_typed_p2m_entry(struct domain *d, unsigned long gfn_l,
 }
 
 /* Set foreign mfn in the given guest's p2m table. */
-static int set_foreign_p2m_entry(struct domain *d, unsigned long gfn,
-                                 mfn_t mfn)
+int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn)
 {
     return set_typed_p2m_entry(d, gfn, mfn, PAGE_ORDER_4K, p2m_map_foreign,
                                p2m_get_hostp2m(d)->default_access);
xen/common/compat/memory.c +100 -0

124 lines not on this page · raw message

xen/common/memory.c +93 -0

105 lines not on this page · raw message

xen/include/asm-arm/p2m.h +10 -0
index 45ef2cd58b..4d036b6112 100644
@@ -344,6 +344,16 @@ static inline gfn_t gfn_next_boundary(gfn_t gfn, unsigned int order)
     return gfn_add(gfn, 1UL << order);
 }
 
+static inline int set_foreign_p2m_entry(struct domain *d, unsigned long gfn,
+                                        mfn_t mfn)
+{
+    /*
+     * NOTE: If this is implemented then proper reference counting of
+     *       foreign entries will need to be impmemented.
+     */
+    return -EOPNOTSUPP;
+}
+
 #endif /* _XEN_P2M_H */
 
 /*
xen/include/asm-x86/p2m.h +3 -0
index 2e7aa8fc79..c555ccf0d9 100644
@@ -621,6 +621,9 @@ void p2m_memory_type_changed(struct domain *d);
 int p2m_is_logdirty_range(struct p2m_domain *, unsigned long start,
                           unsigned long end);
 
+/* Set foreign entry in the p2m table (for priv-mapping) */
+int set_foreign_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn);
+
 /* Set mmio addresses in the p2m table (for pass-through) */
 int set_mmio_p2m_entry(struct domain *d, unsigned long gfn, mfn_t mfn,
                        unsigned int order, p2m_access_t access);
xen/include/public/memory.h +54 -1

67 lines not on this page · raw message

xen/include/xlat.lst +1 -0
index 3690b97d5d..a727ab042e 100644
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h
 !	pod_target			memory.h
 !	remove_from_physmap		memory.h
 !	reserved_device_memory_map	memory.h
xen/include/xsm/dummy.h +6 -0
index 466bbcbde8..ff6b2dbf39 100644
@@ -740,3 +740,9 @@ static XSM_INLINE int xsm_xen_version (XSM_DEFAULT_ARG uint32_t op)
         return xsm_default_action(XSM_PRIV, current->domain, NULL);
     }
 }
+
+static XSM_INLINE int xsm_domain_resource_map(XSM_DEFAULT_ARG struct domain *d)
+{
+    XSM_ASSERT_ACTION(XSM_DM_PRIV);
+    return xsm_default_action(action, current->domain, d);
+}
xen/include/xsm/xsm.h +6 -0
index e3912bcc9d..f0c6fc7e29 100644
@@ -180,6 +180,7 @@ struct xsm_operations {
     int (*dm_op) (struct domain *d);
 #endif
     int (*xen_version) (uint32_t cmd);
+    int (*domain_resource_map) (struct domain *d);
 };
 
 #ifdef CONFIG_XSM
@@ -692,6 +693,11 @@ static inline int xsm_xen_version (xsm_default_t def, uint32_t op)
     return xsm_ops->xen_version(op);
 }
 
+static inline int xsm_domain_resource_map(xsm_default_t def, struct domain *d)
+{
+    return xsm_ops->domain_resource_map(d);
+}
+
 #endif /* XSM_NO_WRAPPERS */
 
 #ifdef CONFIG_MULTIBOOT
xen/xsm/dummy.c +1 -0
index 479b103614..6e751199ee 100644
@@ -157,4 +157,5 @@ void __init xsm_fixup_ops (struct xsm_operations *ops)
     set_to_dummy_if_null(ops, dm_op);
 #endif
     set_to_dummy_if_null(ops, xen_version);
+    set_to_dummy_if_null(ops, domain_resource_map);
 }
xen/xsm/flask/hooks.c +6 -0
index 1802d8dfe6..033caa5328 100644
@@ -1712,6 +1712,11 @@ static int flask_xen_version (uint32_t op)
     }
 }
 
+static int flask_domain_resource_map(struct domain *d)
+{
+    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__RESOURCE_MAP);
+}
+
 long do_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
 int compat_flask_op(XEN_GUEST_HANDLE_PARAM(xsm_op_t) u_flask_op);
 
@@ -1845,6 +1850,7 @@ static struct xsm_operations flask_ops = {
     .dm_op = flask_dm_op,
 #endif
     .xen_version = flask_xen_version,
+    .domain_resource_map = flask_domain_resource_map,
 };
 
 void __init flask_init(const void *policy_buffer, size_t policy_size)
xen/xsm/flask/policy/access_vectors +2 -0
index 89b99966bb..3ce2a51706 100644
@@ -250,6 +250,8 @@ class domain2
     psr_alloc
 # XEN_DOMCTL_set_gnttab_limits
     set_gnttab_limits
+# XENMEM_resource_map
+    resource_map
 }
 
 # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 7 lines
2.11.0


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

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 73 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/memory.c +66 -0
@@ -967,6 +967,94 @@ static long xatp_permission_check(struct domain *d, unsigned int space)
     return xsm_add_to_physmap(XSM_TARGET, current->domain, d);
 }
 
+static int acquire_resource(
+    XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg)
+{
+    struct domain *d, *currd = current->domain;
+    xen_mem_acquire_resource_t xmar;
+    /*
+     * The mfn_list and gfn_list (below) arrays are ok on stack for the
+     * moment since they are small, but if they need to grow in future
+     * use-cases then per-CPU arrays or heap allocations may be required.
+     */
+    xen_pfn_t mfn_list[2];
+    int rc;
+
+    if ( copy_from_guest(&xmar, arg, 1) )
+        return -EFAULT;
+
+    if ( xmar.flags != 0 )
+        return -EINVAL;
+
+    if ( guest_handle_is_null(xmar.frame_list) )
+    {
+        if ( xmar.nr_frames )
+            return -EINVAL;
+
+        xmar.nr_frames = ARRAY_SIZE(mfn_list);
+
+        if ( __copy_field_to_guest(arg, &xmar, nr_frames) )
+            return -EFAULT;
+
+        return 0;
+    }
+
+    if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) )
+        return -E2BIG;
+
+    rc = rcu_lock_remote_domain_by_id(xmar.domid, &d);
+    if ( rc )
+        return rc;
+
+    rc = xsm_domain_resource_map(XSM_DM_PRIV, d);
+    if ( rc )
+        goto out;
+
+    switch ( xmar.type )
+    {
+    default:
+        rc = -EOPNOTSUPP;
+        break;
+    }
+
+    if ( rc )
+        goto out;
+
+    if ( !paging_mode_translate(currd) )
+    {
+        if ( copy_to_guest(xmar.frame_list, mfn_list, xmar.nr_frames) )
+            rc = -EFAULT;
+    }
+    else
+    {
+        xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
+        unsigned int i;
+
+        if ( copy_from_guest(gfn_list, xmar.frame_list, xmar.nr_frames) )
+            rc = -EFAULT;
+

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1045

Quoted 10 lines
xen/common/memory.c +10 -0
+        for ( i = 0; !rc && i < xmar.nr_frames; i++ )
+        {
+            rc = set_foreign_p2m_entry(currd, gfn_list[i],
+                                       _mfn(mfn_list[i]));
+            if ( rc )
+                /*
+                 * Make sure rc is -EIO for any iteration other than
+                 * the first.
+                 */
+                rc = i ? -EIO : rc;

Perhaps easier as

            /*
             * Make sure rc is -EIO for any iteration other than
             * the first.
             */
            if ( rc && i )
                rc = -EIO;

? Looks like the comment could then also be a single line one.

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1056

Quoted 11 lines
xen/common/memory.c +11 -0
+        }
+    }
+
+    if ( xmar.flags != 0 &&
+         __copy_field_to_guest(arg, &xmar, flags) )
+        rc = -EFAULT;
+
+ out:
+    rcu_unlock_domain(d);
+    return rc;
+}

Blank line please ahead of main "return".

Quoted 28 lines
xen/include/public/memory.h +21 -0
@@ -599,6 +599,59 @@ struct xen_reserved_device_memory_map {
 typedef struct xen_reserved_device_memory_map 
 xen_reserved_device_memory_map_t;
 DEFINE_XEN_GUEST_HANDLE(xen_reserved_device_memory_map_t);
 
+/*
+ * Get the pages for a particular guest resource, so that they can be
+ * mapped directly by a tools domain.
+ */
+#define XENMEM_acquire_resource 28
+struct xen_mem_acquire_resource {
+    /* IN - The domain whose resource is to be mapped */
+    domid_t domid;
+    /* IN - the type of resource */
+    uint16_t type;
+    /*
+     * IN - a type-specific resource identifier, which must be zero
+     *      unless stated otherwise.
+     */
+    uint32_t id;
+    /*
+     * IN/OUT - As an IN parameter number of frames of the resource
+     *          to be mapped. However, if the specified value is 0 and
+     *          frame_list is NULL then this field will be set to the
+     *          maximum value supported by the implementation on return.
+     */

xen/include/public/memory.h struct xen_reserved_device_memory_map { · line 633

Quoted 10 lines
xen/include/public/memory.h +10 -0
+    uint32_t nr_frames;
+    /*
+     * OUT - Must be zero on entry. On return this may contain a bitwise
+     *       OR of the following values.
+     */
+    uint32_t flags;
+
+    /* The resource pages have been assigned to the tools domain */
+#define _XENMEM_resource_flag_tools_owned 0
+#define XENMEM_resource_flag_tools_owned (1u << _XENMEM_resource_flag_tools_owned)

Is "tools" really an appropriate (and "flag" a necessary) name component here? How about e.g. XENMEM_res_acq_caller_owned?

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Jan

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

#10 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 42 lines
Quoted 30 lines
Quoted 2 lines · On 26.03.18 at 13:41, <[email protected]> wrote:
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/include/public/memory.h +21 -0
@@ -599,6 +599,59 @@ struct xen_reserved_device_memory_map {
 typedef struct xen_reserved_device_memory_map 
 xen_reserved_device_memory_map_t;
 DEFINE_XEN_GUEST_HANDLE(xen_reserved_device_memory_map_t);
 
+/*
+ * Get the pages for a particular guest resource, so that they can be
+ * mapped directly by a tools domain.
+ */
+#define XENMEM_acquire_resource 28
+struct xen_mem_acquire_resource {
+    /* IN - The domain whose resource is to be mapped */
+    domid_t domid;
+    /* IN - the type of resource */
+    uint16_t type;
+    /*
+     * IN - a type-specific resource identifier, which must be zero
+     *      unless stated otherwise.
+     */
+    uint32_t id;
+    /*
+     * IN/OUT - As an IN parameter number of frames of the resource
+     *          to be mapped. However, if the specified value is 0 and
+     *          frame_list is NULL then this field will be set to the
+     *          maximum value supported by the implementation on return.
+     */

xen/include/public/memory.h struct xen_reserved_device_memory_map { · line 633

Quoted 10 lines
xen/include/public/memory.h +10 -0
+    uint32_t nr_frames;
+    /*
+     * OUT - Must be zero on entry. On return this may contain a bitwise
+     *       OR of the following values.
+     */
+    uint32_t flags;
+
+    /* The resource pages have been assigned to the tools domain */
+#define _XENMEM_resource_flag_tools_owned 0
+#define XENMEM_resource_flag_tools_owned (1u << 

_XENMEM_resource_flag_tools_owned)

Quoted 2 lines

Is "tools" really an appropriate (and "flag" a necessary) name component here? How about e.g. XENMEM_res_acq_caller_owned?

Or maybe XENMEM_rsrc_acq_caller_owned.

Jan

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

#11 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 54 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 13:51
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 42 lines
Quoted 30 lines
Quoted 2 lines · On 26.03.18 at 13:41, <[email protected]> wrote:
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/include/public/memory.h +21 -0
@@ -599,6 +599,59 @@ struct xen_reserved_device_memory_map {
 typedef struct xen_reserved_device_memory_map
 xen_reserved_device_memory_map_t;
 DEFINE_XEN_GUEST_HANDLE(xen_reserved_device_memory_map_t);
 
+/*
+ * Get the pages for a particular guest resource, so that they can be
+ * mapped directly by a tools domain.
+ */
+#define XENMEM_acquire_resource 28
+struct xen_mem_acquire_resource {
+    /* IN - The domain whose resource is to be mapped */
+    domid_t domid;
+    /* IN - the type of resource */
+    uint16_t type;
+    /*
+     * IN - a type-specific resource identifier, which must be zero
+     *      unless stated otherwise.
+     */
+    uint32_t id;
+    /*
+     * IN/OUT - As an IN parameter number of frames of the resource
+     *          to be mapped. However, if the specified value is 0 and
+     *          frame_list is NULL then this field will be set to the
+     *          maximum value supported by the implementation on return.
+     */

xen/include/public/memory.h struct xen_reserved_device_memory_map { · line 633

Quoted 10 lines
xen/include/public/memory.h +10 -0
+    uint32_t nr_frames;
+    /*
+     * OUT - Must be zero on entry. On return this may contain a bitwise
+     *       OR of the following values.
+     */
+    uint32_t flags;
+
+    /* The resource pages have been assigned to the tools domain */
+#define _XENMEM_resource_flag_tools_owned 0
+#define XENMEM_resource_flag_tools_owned (1u <<

_XENMEM_resource_flag_tools_owned)

Quoted 5 lines
Quoted 2 lines

Is "tools" really an appropriate (and "flag" a necessary) name component here? How about e.g. XENMEM_res_acq_caller_owned?

Or maybe XENMEM_rsrc_acq_caller_owned.

Yes, I'm fine with that. I'll make the change.

Paul

Quoted 1 line

Jan

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

#12 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

In reply to Jan Beulich
Quoted 100 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

xen/common/memory.c static long xatp_permission_check(struct domain

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/memory.c +0 -0
@@ -967,6 +967,94 @@ static long xatp_permission_check(struct domain

*d, unsigned int space)

Quoted 69 lines
xen/common/memory.c +66 -0
     return xsm_add_to_physmap(XSM_TARGET, current->domain, d);
 }
 
+static int acquire_resource(
+    XEN_GUEST_HANDLE_PARAM(xen_mem_acquire_resource_t) arg)
+{
+    struct domain *d, *currd = current->domain;
+    xen_mem_acquire_resource_t xmar;
+    /*
+     * The mfn_list and gfn_list (below) arrays are ok on stack for the
+     * moment since they are small, but if they need to grow in future
+     * use-cases then per-CPU arrays or heap allocations may be required.
+     */
+    xen_pfn_t mfn_list[2];
+    int rc;
+
+    if ( copy_from_guest(&xmar, arg, 1) )
+        return -EFAULT;
+
+    if ( xmar.flags != 0 )
+        return -EINVAL;
+
+    if ( guest_handle_is_null(xmar.frame_list) )
+    {
+        if ( xmar.nr_frames )
+            return -EINVAL;
+
+        xmar.nr_frames = ARRAY_SIZE(mfn_list);
+
+        if ( __copy_field_to_guest(arg, &xmar, nr_frames) )
+            return -EFAULT;
+
+        return 0;
+    }
+
+    if ( xmar.nr_frames > ARRAY_SIZE(mfn_list) )
+        return -E2BIG;
+
+    rc = rcu_lock_remote_domain_by_id(xmar.domid, &d);
+    if ( rc )
+        return rc;
+
+    rc = xsm_domain_resource_map(XSM_DM_PRIV, d);
+    if ( rc )
+        goto out;
+
+    switch ( xmar.type )
+    {
+    default:
+        rc = -EOPNOTSUPP;
+        break;
+    }
+
+    if ( rc )
+        goto out;
+
+    if ( !paging_mode_translate(currd) )
+    {
+        if ( copy_to_guest(xmar.frame_list, mfn_list, xmar.nr_frames) )
+            rc = -EFAULT;
+    }
+    else
+    {
+        xen_pfn_t gfn_list[ARRAY_SIZE(mfn_list)];
+        unsigned int i;
+
+        if ( copy_from_guest(gfn_list, xmar.frame_list, xmar.nr_frames) )
+            rc = -EFAULT;
+

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1045

Quoted 10 lines
xen/common/memory.c +10 -0
+        for ( i = 0; !rc && i < xmar.nr_frames; i++ )
+        {
+            rc = set_foreign_p2m_entry(currd, gfn_list[i],
+                                       _mfn(mfn_list[i]));
+            if ( rc )
+                /*
+                 * Make sure rc is -EIO for any iteration other than
+                 * the first.
+                 */
+                rc = i ? -EIO : rc;

Perhaps easier as

Quoted 9 lines
            /*
             * Make sure rc is -EIO for any iteration other than
             * the first.
             */
            if ( rc && i )
                rc = -EIO;

? Looks like the comment could then also be a single line one.

Ok.

Quoted 5 lines
Quoted 5 lines
xen/common/memory.c +5 -0
+        }
+    }
+
+    if ( xmar.flags != 0 &&
+         __copy_field_to_guest(arg, &xmar, flags) )
Quoted 9 lines

xen/common/memory.c static long xatp_permission_check(struct domain *d, unsigned int space) · line 1056

Quoted 6 lines
xen/common/memory.c +6 -0
+        rc = -EFAULT;
+
+ out:
+    rcu_unlock_domain(d);
+    return rc;
+}

Blank line please ahead of main "return".

Ok.

Quoted 43 lines
Quoted 28 lines
xen/include/public/memory.h +21 -0
@@ -599,6 +599,59 @@ struct xen_reserved_device_memory_map {
 typedef struct xen_reserved_device_memory_map
 xen_reserved_device_memory_map_t;
 DEFINE_XEN_GUEST_HANDLE(xen_reserved_device_memory_map_t);
 
+/*
+ * Get the pages for a particular guest resource, so that they can be
+ * mapped directly by a tools domain.
+ */
+#define XENMEM_acquire_resource 28
+struct xen_mem_acquire_resource {
+    /* IN - The domain whose resource is to be mapped */
+    domid_t domid;
+    /* IN - the type of resource */
+    uint16_t type;
+    /*
+     * IN - a type-specific resource identifier, which must be zero
+     *      unless stated otherwise.
+     */
+    uint32_t id;
+    /*
+     * IN/OUT - As an IN parameter number of frames of the resource
+     *          to be mapped. However, if the specified value is 0 and
+     *          frame_list is NULL then this field will be set to the
+     *          maximum value supported by the implementation on return.
+     */

xen/include/public/memory.h struct xen_reserved_device_memory_map { · line 633

Quoted 10 lines
xen/include/public/memory.h +10 -0
+    uint32_t nr_frames;
+    /*
+     * OUT - Must be zero on entry. On return this may contain a bitwise
+     *       OR of the following values.
+     */
+    uint32_t flags;
+
+    /* The resource pages have been assigned to the tools domain */
+#define _XENMEM_resource_flag_tools_owned 0
+#define XENMEM_resource_flag_tools_owned (1u <<

_XENMEM_resource_flag_tools_owned)

Is "tools" really an appropriate (and "flag" a necessary) name component here? How about e.g. XENMEM_res_acq_caller_owned?

Quoted 13 lines

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

It's been XEN_GUEST_HANDLE throughout all but the earliest revisions of the patch and I have not modified the compat code massively since you gave your R-b anyway... the only thing that changed was copying back the new flags value.

Paul

Quoted 1 line

Jan

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

#13 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 18 lines
Quoted 5 lines
Quoted 1 line · On 29.03.18 at 11:53, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
Quoted 12 lines

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Quoted 4 lines

It's been XEN_GUEST_HANDLE throughout all but the earliest revisions of the patch and I have not modified the compat code massively since you gave your R-b anyway... the only thing that changed was copying back the new flags value.

Granted I could/should have noticed this earlier, but being able to get away without compat translation would certainly be a win, and we have that option since this is a tools-only interface.

Jan

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

#14 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 33 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 March 2018 13:29
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; StefanoStabellini <[email protected]>; xen- [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 18 lines
Quoted 5 lines
Quoted 1 line · On 29.03.18 at 11:53, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
Quoted 12 lines

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Quoted 1 line

It's been XEN_GUEST_HANDLE throughout all but the earliest revisions of

the

Quoted 9 lines
Quoted 1 line

patch and I have not modified the compat code massively since you gave

your

Quoted 2 lines

R-b anyway... the only thing that changed was copying back the new flags value.

Granted I could/should have noticed this earlier, but being able to get away without compat translation would certainly be a win, and we have that option since this is a tools-only interface.

Ok. I'll see if I can get this done today then.

Paul

Quoted 1 line

Jan

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

#15 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 52 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 29 March 2018 13:43
To: 'Jan Beulich' <[email protected]>
Cc: StefanoStabellini <[email protected]>; Wei Liu <[email protected]>; Andrew Cooper <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; [email protected]; Ian Jackson <[email protected]>
Subject: Re: [Xen-devel] [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 7 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 March 2018 13:29
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei

Liu

Quoted 31 lines

<[email protected]>; StefanoStabellini <[email protected]>; xen- [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 18 lines
Quoted 5 lines
Quoted 1 line · On 29.03.18 at 11:53, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
Quoted 12 lines

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Quoted 1 line

It's been XEN_GUEST_HANDLE throughout all but the earliest revisions of

the

Quoted 1 line

patch and I have not modified the compat code massively since you gave

your

Quoted 2 lines

R-b anyway... the only thing that changed was copying back the new flags value.

Quoted 8 lines
Quoted 4 lines

Granted I could/should have noticed this earlier, but being able to get away without compat translation would certainly be a win, and we have that option since this is a tools-only interface.

Ok. I'll see if I can get this done today then.

Paul

Actually, I'm getting confused by all this... The handle is for an array of xen_pfn_t, which means they are going to be 32-bits wide for a 32-bit tools domain. Doesn't this mean I'm going to need compat code to iterate and translate the array anyway?

Paul

Quoted 8 lines
Quoted 2 lines

Jan

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

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

Reviewed-by
#16 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 58 lines
Quoted 58 lines
Quoted 1 line · On 29.03.18 at 15:17, <[email protected]> wrote:

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 29 March 2018 13:43
To: 'Jan Beulich' <[email protected]>
Cc: StefanoStabellini <[email protected]>; Wei Liu <[email protected]>; Andrew Cooper <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; [email protected]; Ian Jackson <[email protected]>
Subject: Re: [Xen-devel] [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 7 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 March 2018 13:29
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei

Liu

Quoted 35 lines

<[email protected]>; StefanoStabellini <[email protected]>; xen- [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 18 lines
Quoted 5 lines
Quoted 1 line · On 29.03.18 at 11:53, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41

Quoted 1 line
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
Quoted 12 lines

xen/include/xlat.lst line 89

Quoted 7 lines
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Quoted 1 line

It's been XEN_GUEST_HANDLE throughout all but the earliest revisions of

the

Quoted 1 line

patch and I have not modified the compat code massively since you gave

your

Quoted 2 lines

R-b anyway... the only thing that changed was copying back the new flags value.

Granted I could/should have noticed this earlier, but being able to get away without compat translation would certainly be a win, and we have that option since this is a tools-only interface.

Quoted 8 lines
Quoted 3 lines

Ok. I'll see if I can get this done today then.

Paul

Actually, I'm getting confused by all this... The handle is for an array of xen_pfn_t, which means they are going to be 32-bits wide for a 32-bit tools domain. Doesn't this mean I'm going to need compat code to iterate and translate the array anyway?

Oh, yes, indeed. I'm sorry for the confusion. With the other remarks addressed feel free to add

Jan

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

#17 lore raw

Re: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 82 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 29 March 2018 15:25
To: Paul Durrant <[email protected]>
Cc: StefanoStabellini <[email protected]>; Wei Liu <[email protected]>; Andrew Cooper <[email protected]>; Tim (Xen.org) <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; [email protected]; Ian Jackson <[email protected]>
Subject: Re: [Xen-devel] [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

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

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On

Behalf

Quoted 5 lines
Quoted 5 lines

Of Paul Durrant
Sent: 29 March 2018 13:43
To: 'Jan Beulich' <[email protected]>
Cc: StefanoStabellini <[email protected]>; Wei Liu <[email protected]>; Andrew Cooper <[email protected]>;

Tim

Quoted 6 lines
Quoted 6 lines

(Xen.org) <[email protected]>; George Dunlap <[email protected]>; Julien Grall <[email protected]>; [email protected]; Ian Jackson <[email protected]>
Subject: Re: [Xen-devel] [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op to acquire guest resources

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 29 March 2018 13:29
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>;

Wei

Quoted 5 lines
Quoted 5 lines

Liu

Quoted 4 lines

<[email protected]>; StefanoStabellini <[email protected]>; xen- [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: RE: [PATCH v18 05/11] x86/mm: add HYPERVISOR_memory_op

to

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines

acquire guest resources

Quoted 18 lines
Quoted 17 lines
Quoted 1 line · On 29.03.18 at 11:53, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:41

xen/include/xlat.lst line 89

Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/include/xlat.lst +1 -0
@@ -86,6 +86,7 @@
 !	memory_map			memory.h
 !	memory_reservation		memory.h
 !	mem_access_op			memory.h
+!	mem_acquire_resource		memory.h

Why ! ? The layout doesn't appear to differ between native and compat. Or wait, the handle does, but why is that not XEN_GUEST_HANDLE_64()? (I've skipped the compat layer code in this round of review for that reason.)

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

It's been XEN_GUEST_HANDLE throughout all but the earliest

revisions of

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the

Quoted 1 line

patch and I have not modified the compat code massively since you

gave

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

your

Quoted 1 line

R-b anyway... the only thing that changed was copying back the new

flags

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 1 line

value.

Granted I could/should have noticed this earlier, but being able to get away without compat translation would certainly be a win, and we have that option since this is a tools-only interface.

Ok. I'll see if I can get this done today then.

Paul

Quoted 8 lines
Quoted 4 lines

Actually, I'm getting confused by all this... The handle is for an array of xen_pfn_t, which means they are going to be 32-bits wide for a 32-bit tools domain. Doesn't this mean I'm going to need compat code to iterate and translate the array anyway?

Oh, yes, indeed. I'm sorry for the confusion. With the other remarks addressed feel free to add

Ok, thanks :-)

Paul

Quoted 8 lines

Jan

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

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

#18 lore raw

PATCH v18 6/11 x86/hvm/ioreq: add a new mappable resource type...

In reply to Paul Durrant

... XENMEM_resource_ioreq_server

This patch adds support for a new resource type that can be mapped using the XENMEM_acquire_resource memory op.

If an emulator makes use of this resource type then, instead of mapping gfns, the IOREQ server will allocate pages from the emulating domain's heap. These pages will never be present in the P2M of the guest at any point (and are not even shared with the guest) and so are not vulnerable to any direct attack by the guest.

NOTE: Use of the new resource type is not compatible with use of
XEN_DMOP_get_ioreq_server_info unless the XEN_DMOP_no_gfns flag is set.

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

v18:
- Revert largely back to v14, but use ioreq server emulator rather than current->domain.
- Add missing checks spotted by Jan.
- Re-base.

v17:
- The use of xenheap pages means that freeing needs to be deferred until domain destruction. Add an explanatory paragraph to the commit comment.

v15:
- Use xenheap pages rather than domheap pages and assign ownership to target domain.

v14:
- Addressed more comments from Jan.

v13:
- Introduce an arch_acquire_resource() as suggested by Julien (and have the ARM varient simply return -EOPNOTSUPP).
- Check for ioreq server id truncation as requested by Jan.
- Not added Jan's R-b due to substantive change from v12.

v12:
- Addressed more comments from Jan.
- Dropped George's A-b and Wei's R-b because of material change.

v11:
- Addressed more comments from Jan.

v10:
- Addressed comments from Jan.

v8:
- Re-base on new boilerplate.
- Adjust function signature of hvm_get_ioreq_server_frame(), and test whether the bufioreq page is present.

v5:
- Use get_ioreq_server() function rather than indexing array directly.
- Add more explanation into comments to state than mapping guest frames and allocation of pages for ioreq servers are not simultaneously permitted.
- Add a comment into asm/ioreq.h stating the meaning of the index value passed to hvm_get_ioreq_server_frame().

---
 xen/arch/x86/hvm/ioreq.c        | 164 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/mm.c               |  47 ++++++++++++
 xen/common/memory.c             |   3 +-
 xen/include/asm-arm/mm.h        |   8 ++
 xen/include/asm-x86/hvm/ioreq.h |   2 +
 xen/include/asm-x86/mm.h        |   5 ++
 xen/include/public/hvm/dm_op.h  |   4 +
 xen/include/public/memory.h     |   9 +++
 8 files changed, 241 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/ioreq.c +164 -0
index ce53d883e9..ca02e6da10 100644
@@ -259,6 +259,19 @@ static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
     int rc;
 
+    if ( iorp->page )
+    {
+        /*
+         * If a page has already been allocated (which will happen on
+         * demand if hvm_get_ioreq_server_frame() is called), then
+         * mapping a guest frame is not permitted.
+         */
+        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+            return -EPERM;
+
+        return 0;
+    }
+
     if ( d->is_dying )
         return -EINVAL;
 
@@ -281,6 +294,67 @@ static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf)
     return rc;
 }
 
+static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
+{
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+
+    if ( iorp->page )
+    {
+        /*
+         * If a guest frame has already been mapped (which may happen
+         * on demand if hvm_get_ioreq_server_info() is called), then
+         * allocating a page is not permitted.
+         */
+        if ( !gfn_eq(iorp->gfn, INVALID_GFN) )
+            return -EPERM;
+
+        return 0;
+    }
+
+    /*
+     * Allocated IOREQ server pages are assigned to the emulating
+     * domain, not the target domain. This is safe because the emulating
+     * domain cannot be destroyed until the ioreq server is destroyed.
+     * Also we must use MEMF_no_refcount otherwise page allocation
+     * could fail if the emulating domain has already reached its
+     * maximum allocation.
+     */
+    iorp->page = alloc_domheap_page(s->emulator, MEMF_no_refcount);
+
+    if ( !iorp->page )
+        return -ENOMEM;
+
+    if ( !get_page_type(iorp->page, PGT_writable_page) )
+        goto fail;
+
+    iorp->va = __map_domain_page_global(iorp->page);
+    if ( !iorp->va )
+        goto fail;
+
+    clear_page(iorp->va);
+    return 0;
+
+ fail:
+    put_page_and_type(iorp->page);
+    iorp->page = NULL;
+
+    return -ENOMEM;
+}
+
+static void hvm_free_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
+{
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+
+    if ( !iorp->page )
+        return;
+
+    unmap_domain_page_global(iorp->va);
+    iorp->va = NULL;
+
+    put_page_and_type(iorp->page);
+    iorp->page = NULL;
+}
+
 bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
 {
     const struct hvm_ioreq_server *s;
@@ -486,6 +560,27 @@ static void hvm_ioreq_server_unmap_pages(struct hvm_ioreq_server *s)
     hvm_unmap_ioreq_gfn(s, false);
 }
 
+static int hvm_ioreq_server_alloc_pages(struct hvm_ioreq_server *s)
+{
+    int rc;
+
+    rc = hvm_alloc_ioreq_mfn(s, false);
+
+    if ( !rc && (s->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF) )
+        rc = hvm_alloc_ioreq_mfn(s, true);
+
+    if ( rc )
+        hvm_free_ioreq_mfn(s, false);
+
+    return rc;
+}
+
+static void hvm_ioreq_server_free_pages(struct hvm_ioreq_server *s)
+{
+    hvm_free_ioreq_mfn(s, true);
+    hvm_free_ioreq_mfn(s, false);
+}
+
 static void hvm_ioreq_server_free_rangesets(struct hvm_ioreq_server *s)
 {
     unsigned int i;
@@ -637,7 +732,19 @@ static void hvm_ioreq_server_deinit(struct hvm_ioreq_server *s)
 {
     ASSERT(!s->enabled);
     hvm_ioreq_server_remove_all_vcpus(s);
+
+    /*
+     * NOTE: It is safe to call both hvm_ioreq_server_unmap_pages() and
+     *       hvm_ioreq_server_free_pages() in that order.
+     *       This is because the former will do nothing if the pages
+     *       are not mapped, leaving the page to be freed by the latter.
+     *       However if the pages are mapped then the former will set
+     *       the page_info pointer to NULL, meaning the latter will do
+     *       nothing.
+     */
     hvm_ioreq_server_unmap_pages(s);
+    hvm_ioreq_server_free_pages(s);
+
     hvm_ioreq_server_free_rangesets(s);
 
     put_domain(s->emulator);
@@ -802,6 +909,63 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
     return rc;
 }
 
+int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id,
+                               unsigned long idx, mfn_t *mfn)
+{
+    struct hvm_ioreq_server *s;
+    int rc;
+
+    if ( id == DEFAULT_IOSERVID )
+        return -EOPNOTSUPP;
+
+    if ( !is_hvm_domain(d) )
+        return -EINVAL;
+
+    spin_lock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+
+    s = get_ioreq_server(d, id);
+
+    rc = -ENOENT;
+    if ( !s )
+        goto out;
+
+    ASSERT(!IS_DEFAULT(s));
+
+    rc = -EPERM;
+    if ( s->emulator != current->domain )
+        goto out;
+
+    rc = hvm_ioreq_server_alloc_pages(s);
+    if ( rc )
+        goto out;
+
+    switch ( idx )
+    {
+    case XENMEM_resource_ioreq_server_frame_bufioreq:
+        rc = -ENOENT;
+        if ( !HANDLE_BUFIOREQ(s) )
+            goto out;
+
+        *mfn = _mfn(page_to_mfn(s->bufioreq.page));
+        rc = 0;
+        break;
+
+    case XENMEM_resource_ioreq_server_frame_ioreq(0):
+        *mfn = _mfn(page_to_mfn(s->ioreq.page));
+        rc = 0;
+        break;
+
+    default:
+        rc = -EINVAL;
+        break;
+    }
+
+ out:
+    spin_unlock_recursive(&d->arch.hvm_domain.ioreq_server.lock);
+
+    return rc;
+}
+
 int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
                                      uint32_t type, uint64_t start,
                                      uint64_t end)
xen/arch/x86/mm.c +47 -0

59 lines not on this page · raw message

xen/common/memory.c +2 -1
index d56cea1590..c09ef179e8 100644
@@ -1013,7 +1013,8 @@ static int acquire_resource(
     switch ( xmar.type )
     {
     default:
-        rc = -EOPNOTSUPP;
+        rc = arch_acquire_resource(d, xmar.type, xmar.id, xmar.frame,
+                                   xmar.nr_frames, mfn_list, &xmar.flags);
         break;
     }
 
xen/include/asm-arm/mm.h +8 -0
index a0e922f360..5ddc287b6f 100644
@@ -374,6 +374,14 @@ static inline void put_page_and_type(struct page_info *page)
 
 void clear_and_clean_page(struct page_info *page);
 
+static inline int arch_acquire_resource(
+    struct domain *d, unsigned int type, unsigned int id,
+    unsigned long frame,unsigned int nr_frames, xen_pfn_t mfn_list[],
+    unsigned int *flags)
+{
+    return -EOPNOTSUPP;
+}
+
 #endif /*  __ARCH_ARM_MM__ */
 /*
  * Local variables:
xen/include/asm-x86/hvm/ioreq.h +2 -0
index 1bd1a02f23..bab473cf44 100644
@@ -30,6 +30,8 @@ int hvm_get_ioreq_server_info(struct domain *d, ioservid_t id,
                               unsigned long *ioreq_gfn,
                               unsigned long *bufioreq_gfn,
                               evtchn_port_t *bufioreq_port);
+int hvm_get_ioreq_server_frame(struct domain *d, ioservid_t id,
+                               unsigned long idx, mfn_t *mfn);
 int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
                                      uint32_t type, uint64_t start,
                                      uint64_t end);
xen/include/asm-x86/mm.h +5 -0
index c115661837..d3cd5b9b22 100644
@@ -623,4 +623,9 @@ static inline bool arch_mfn_in_directmap(unsigned long mfn)
     return mfn <= (virt_to_mfn(eva - 1) + 1);
 }
 
+int arch_acquire_resource(struct domain *d, unsigned int type,
+                          unsigned int id, unsigned long frame,
+                          unsigned int nr_frames, xen_pfn_t mfn_list[],
+                          unsigned int *flags);
+
 #endif /* __ASM_X86_MM_H__ */
xen/include/public/hvm/dm_op.h +4 -0
index 9823726d79..d3b554d019 100644
@@ -90,6 +90,10 @@ struct xen_dm_op_create_ioreq_server {
  * the frame numbers passed back in gfns <ioreq_gfn> and <bufioreq_gfn>
  * respectively. (If the IOREQ Server is not handling buffered emulation
  * only <ioreq_gfn> will be valid).
+ *
+ * NOTE: To access the synchronous ioreq structures and buffered ioreq
+ *       ring, it is preferable to use the XENMEM_acquire_resource memory
+ *       op specifying resource type XENMEM_resource_ioreq_server.
  */
 #define XEN_DMOP_get_ioreq_server_info 2
 
xen/include/public/memory.h +9 -0
index 53d258fd92..c5cd2ba1a2 100644
@@ -609,9 +609,14 @@ struct xen_mem_acquire_resource {
     domid_t domid;
     /* IN - the type of resource */
     uint16_t type;
+
+#define XENMEM_resource_ioreq_server 0
+
     /*
      * IN - a type-specific resource identifier, which must be zero
      *      unless stated otherwise.
+     *
+     * type == XENMEM_resource_ioreq_server -> id == ioreq server id
      */
     uint32_t id;
     /*
@@ -636,6 +641,10 @@ struct xen_mem_acquire_resource {
      *      is ignored if nr_frames is 0.
      */
     uint64_aligned_t frame;
+
+#define XENMEM_resource_ioreq_server_frame_bufioreq 0
+#define XENMEM_resource_ioreq_server_frame_ioreq(n) (1 + (n))
+
     /*
      * IN/OUT - If the tools domain is PV then, upon return, frame_list
      *          will be populated with the MFNs of the resource.
Signature · 7 lines
2.11.0


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

Re: [PATCH v18 06/11] x86/hvm/ioreq: add a new mappable resource type...

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:

... XENMEM_resource_ioreq_server

This patch adds support for a new resource type that can be mapped using the XENMEM_acquire_resource memory op.

If an emulator makes use of this resource type then, instead of mapping gfns, the IOREQ server will allocate pages from the emulating domain's heap. These pages will never be present in the P2M of the guest at any point (and are not even shared with the guest) and so are not vulnerable to any direct attack by the guest.

"allocate pages from the emulating domain's heap" is a sub-optimal (at least slightly misleading) description, due to your use of MEMF_no_refcount together with the fact that domain's don't really have their own heaps.

Quoted 32 lines
xen/arch/x86/hvm/ioreq.c +32 -0
+static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
+{
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+
+    if ( iorp->page )
+    {
+        /*
+         * If a guest frame has already been mapped (which may happen
+         * on demand if hvm_get_ioreq_server_info() is called), then
+         * allocating a page is not permitted.
+         */
+        if ( !gfn_eq(iorp->gfn, INVALID_GFN) )
+            return -EPERM;
+
+        return 0;
+    }
+
+    /*
+     * Allocated IOREQ server pages are assigned to the emulating
+     * domain, not the target domain. This is safe because the emulating
+     * domain cannot be destroyed until the ioreq server is destroyed.
+     * Also we must use MEMF_no_refcount otherwise page allocation
+     * could fail if the emulating domain has already reached its
+     * maximum allocation.
+     */
+    iorp->page = alloc_domheap_page(s->emulator, MEMF_no_refcount);
+
+    if ( !iorp->page )
+        return -ENOMEM;
+
+    if ( !get_page_type(iorp->page, PGT_writable_page) )
+        goto fail;

xen/arch/x86/hvm/ioreq.c static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) · line 338

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+
+    iorp->va = __map_domain_page_global(iorp->page);
+    if ( !iorp->va )
+        goto fail;
+
+    clear_page(iorp->va);
+    return 0;
+
+ fail:
+    put_page_and_type(iorp->page);

This is wrong in case it's the get_page_type() which failed.

Quoted 24 lines
xen/arch/x86/mm.c +24 -0
+int arch_acquire_resource(struct domain *d, unsigned int type,
+                          unsigned int id, unsigned long frame,
+                          unsigned int nr_frames, xen_pfn_t mfn_list[],
+                          unsigned int *flags)
+{
+    int rc;
+
+    switch ( type )
+    {
+    case XENMEM_resource_ioreq_server:
+    {
+        ioservid_t ioservid = id;
+        unsigned int i;
+
+        rc = -EINVAL;
+        if ( id != (unsigned int)ioservid )
+            break;
+
+        rc = 0;
+        for ( i = 0; i < nr_frames; i++ )
+        {
+            mfn_t mfn;
+
+            rc = hvm_get_ioreq_server_frame(d, id, frame + i, &mfn);

xen/arch/x86/mm.c int xenmem_add_to_physmap_one( · line 4265

Quoted 10 lines
xen/arch/x86/mm.c +10 -0
+            if ( rc )
+                break;
+
+            mfn_list[i] = mfn_x(mfn);
+        }
+
+        /*
+         * The frames will be assigned to the tools domain that created
+         * the ioreq server.
+         */

s/will be/have been/ and perhaps drop "tools"?

xen/include/asm-arm/mm.h static inline void put_page_and_type(struct page_info *page) · line 379

Quoted 9 lines
xen/include/asm-arm/mm.h +3 -0
@@ -374,6 +374,14 @@ static inline void put_page_and_type(struct page_info *page)
 
 void clear_and_clean_page(struct page_info *page);
 
+static inline int arch_acquire_resource(
+    struct domain *d, unsigned int type, unsigned int id,
+    unsigned long frame,unsigned int nr_frames, xen_pfn_t mfn_list[],

Missing blank.

Jan

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

#20 lore raw

Re: [PATCH v18 06/11] x86/hvm/ioreq: add a new mappable resource type...

Quoted 25 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 12:55
To: Paul Durrant <[email protected]>
Cc: JulienGrall <[email protected]>; Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Stefano Stabellini <[email protected]>; [email protected]; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v18 06/11] x86/hvm/ioreq: add a new mappable resource type...

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:

... XENMEM_resource_ioreq_server

This patch adds support for a new resource type that can be mapped using the XENMEM_acquire_resource memory op.

If an emulator makes use of this resource type then, instead of mapping gfns, the IOREQ server will allocate pages from the emulating domain's heap. These pages will never be present in the P2M of the guest at any point (and are not even shared with the guest) and so are not vulnerable to any direct attack by the guest.

Quoted 5 lines

"allocate pages from the emulating domain's heap" is a sub-optimal (at least slightly misleading) description, due to your use of MEMF_no_refcount together with the fact that domain's don't really have their own heaps.

Ok, I'll say 'allocate pages which are assigned to the emulating domain' instead.

Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/ioreq.c +36 -0
+static int hvm_alloc_ioreq_mfn(struct hvm_ioreq_server *s, bool buf)
+{
+    struct hvm_ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+
+    if ( iorp->page )
+    {
+        /*
+         * If a guest frame has already been mapped (which may happen
+         * on demand if hvm_get_ioreq_server_info() is called), then
+         * allocating a page is not permitted.
+         */
+        if ( !gfn_eq(iorp->gfn, INVALID_GFN) )
+            return -EPERM;
+
+        return 0;
+    }
+
+    /*
+     * Allocated IOREQ server pages are assigned to the emulating
+     * domain, not the target domain. This is safe because the emulating
+     * domain cannot be destroyed until the ioreq server is destroyed.
+     * Also we must use MEMF_no_refcount otherwise page allocation
+     * could fail if the emulating domain has already reached its
+     * maximum allocation.
+     */
+    iorp->page = alloc_domheap_page(s->emulator, MEMF_no_refcount);
+
+    if ( !iorp->page )
+        return -ENOMEM;
+
+    if ( !get_page_type(iorp->page, PGT_writable_page) )
+        goto fail;
+
+    iorp->va = __map_domain_page_global(iorp->page);
+    if ( !iorp->va )
+        goto fail;
Quoted 9 lines

xen/arch/x86/hvm/ioreq.c static int hvm_map_ioreq_gfn(struct hvm_ioreq_server *s, bool buf) · line 338

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+
+    clear_page(iorp->va);
+    return 0;
+
+ fail:
+    put_page_and_type(iorp->page);

This is wrong in case it's the get_page_type() which failed.

Oh, I thought it was safe. I'll re-work the error path.

Quoted 28 lines
Quoted 28 lines
xen/arch/x86/mm.c +28 -0
+int arch_acquire_resource(struct domain *d, unsigned int type,
+                          unsigned int id, unsigned long frame,
+                          unsigned int nr_frames, xen_pfn_t mfn_list[],
+                          unsigned int *flags)
+{
+    int rc;
+
+    switch ( type )
+    {
+    case XENMEM_resource_ioreq_server:
+    {
+        ioservid_t ioservid = id;
+        unsigned int i;
+
+        rc = -EINVAL;
+        if ( id != (unsigned int)ioservid )
+            break;
+
+        rc = 0;
+        for ( i = 0; i < nr_frames; i++ )
+        {
+            mfn_t mfn;
+
+            rc = hvm_get_ioreq_server_frame(d, id, frame + i, &mfn);
+            if ( rc )
+                break;
+
+            mfn_list[i] = mfn_x(mfn);
Quoted 9 lines

xen/arch/x86/mm.c int xenmem_add_to_physmap_one( · line 4265

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        }
+
+        /*
+         * The frames will be assigned to the tools domain that created
+         * the ioreq server.
+         */

s/will be/have been/ and perhaps drop "tools"?

Ok.

Quoted 5 lines

xen/include/asm-arm/mm.h static inline void put_page_and_type(struct

Quoted 3 lines
xen/include/asm-arm/mm.h +0 -0
@@ -374,6 +374,14 @@ static inline void put_page_and_type(struct

page_info *page)

Quoted 1 line
Quoted 8 lines

xen/include/asm-arm/mm.h static inline void put_page_and_type(struct page_info *page) · line 379

Quoted 5 lines

void clear_and_clean_page(struct page_info *page);

xen/include/asm-arm/mm.h +3 -0
+static inline int arch_acquire_resource(
+    struct domain *d, unsigned int type, unsigned int id,
+    unsigned long frame,unsigned int nr_frames, xen_pfn_t mfn_list[],

Missing blank.

Ok.

Paul

Quoted 1 line

Jan

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

#21 lore raw

PATCH v18 7/11 x86/mm: add an extra command to HYPERVISOR_mmu_update...

In reply to Paul Durrant

...to allow the calling domain to prevent translation of specified l1e value.

Despite what the comment in public/xen.h might imply, specifying a command value of MMU_NORMAL_PT_UPDATE will not simply update an l1e with the specified value. Instead, mod_l1_entry() tests whether foreign_dom has PG_translate set in its paging mode and, if it does, assumes that the the pfn value in the l1e is a gfn rather than an mfn.

To allow PV tools domain to map mfn values from a previously issued HYPERVISOR_memory_op:XENMEM_acquire_resource, there needs to be a way to tell HYPERVISOR_mmu_update that the specific l1e value does not require translation regardless of the paging mode of foreign_dom. This patch therefore defines a new command value, MMU_PT_UPDATE_NO_TRANSLATE, which has the same semantics as MMU_NORMAL_PT_UPDATE except that the paging mode of foreign_dom is ignored and the l1e value is used verbatim.

---
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]>

v13:
- Re-base.

v8:
- New in this version, replacing "allow a privileged PV domain to map guest mfns".

---
 xen/arch/x86/mm.c        | 13 ++++++++-----
 xen/include/public/xen.h | 12 +++++++++---
 2 files changed, 17 insertions(+), 8 deletions(-)
xen/arch/x86/mm.c +8 -5
index 846cc61935..8e3be1f263 100644
@@ -1901,9 +1901,10 @@ void page_unlock(struct page_info *page)
 
 /* Update the L1 entry at pl1e to new value nl1e. */
 static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
-                        unsigned long gl1mfn, int preserve_ad,
+                        unsigned long gl1mfn, unsigned int cmd,
                         struct vcpu *pt_vcpu, struct domain *pg_dom)
 {
+    bool preserve_ad = (cmd == MMU_PT_UPDATE_PRESERVE_AD);
     l1_pgentry_t ol1e;
     struct domain *pt_dom = pt_vcpu->domain;
     int rc = 0;
@@ -1925,7 +1926,8 @@ static int mod_l1_entry(l1_pgentry_t *pl1e, l1_pgentry_t nl1e,
         }
 
         /* Translate foreign guest address. */
-        if ( paging_mode_translate(pg_dom) )
+        if ( cmd != MMU_PT_UPDATE_NO_TRANSLATE &&
+             paging_mode_translate(pg_dom) )
         {
             p2m_type_t p2mt;
             p2m_query_t q = l1e_get_flags(nl1e) & _PAGE_RW ?
@@ -3617,6 +3619,7 @@ long do_mmu_update(
              */
         case MMU_NORMAL_PT_UPDATE:
         case MMU_PT_UPDATE_PRESERVE_AD:
+        case MMU_PT_UPDATE_NO_TRANSLATE:
         {
             p2m_type_t p2mt;
 
@@ -3676,8 +3679,7 @@ long do_mmu_update(
                 {
                 case PGT_l1_page_table:
                     rc = mod_l1_entry(va, l1e_from_intpte(req.val), mfn,
-                                      cmd == MMU_PT_UPDATE_PRESERVE_AD, v,
-                                      pg_owner);
+                                      cmd, v, pg_owner);
                     break;
 
                 case PGT_l2_page_table:
@@ -3988,7 +3990,8 @@ static int __do_update_va_mapping(
         goto out;
     }
 
-    rc = mod_l1_entry(pl1e, val, mfn_x(gl1mfn), 0, v, pg_owner);
+    rc = mod_l1_entry(pl1e, val, mfn_x(gl1mfn), MMU_NORMAL_PT_UPDATE, v,
+                      pg_owner);
 
     page_unlock(gl1pg);
     put_page(gl1pg);
xen/include/public/xen.h +9 -3
index 308109f176..fb1df8f293 100644
@@ -268,6 +268,10 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
  * As MMU_NORMAL_PT_UPDATE above, but A/D bits currently in the PTE are ORed
  * with those in @val.
  *
+ * ptr[1:0] == MMU_PT_UPDATE_NO_TRANSLATE:
+ * As MMU_NORMAL_PT_UPDATE above, but @val is not translated though FD
+ * page tables.
+ *
  * @val is usually the machine frame number along with some attributes.
  * The attributes by default follow the architecture defined bits. Meaning that
  * if this is a X86_64 machine and four page table layout is used, the layout
@@ -334,9 +338,11 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
  *
  * PAT (bit 7 on) --> PWT (bit 3 on) and clear bit 7.
  */
-#define MMU_NORMAL_PT_UPDATE      0 /* checked '*ptr = val'. ptr is MA.      */
-#define MMU_MACHPHYS_UPDATE       1 /* ptr = MA of frame to modify entry for */
-#define MMU_PT_UPDATE_PRESERVE_AD 2 /* atomically: *ptr = val | (*ptr&(A|D)) */
+#define MMU_NORMAL_PT_UPDATE       0 /* checked '*ptr = val'. ptr is MA.      */
+#define MMU_MACHPHYS_UPDATE        1 /* ptr = MA of frame to modify entry for */
+#define MMU_PT_UPDATE_PRESERVE_AD  2 /* atomically: *ptr = val | (*ptr&(A|D)) */
+#define MMU_PT_UPDATE_NO_TRANSLATE 3 /* checked '*ptr = val'. ptr is MA.      */
+                                     /* val never translated.                 */
 
 /*
  * MMU EXTENDED OPERATIONS
Signature · 7 lines
2.11.0


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

PATCH v18 8/11 tools/libxenforeignmemory: add support for resource mapping

In reply to Paul Durrant

A previous patch introduced a new HYPERVISOR_memory_op to acquire guest resources for direct priv-mapping.

This patch adds new functionality into libxenforeignmemory to make use of a new privcmd ioctl [1] that uses the new memory op to make such resources available via mmap(2).

[1] http://xenbits.xen.org/gitweb/?p=people/pauldu/linux.git;a=commit;h=ce59a05e6712

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

v4:
- Fixed errno and removed single-use label
- The unmap call now returns a status
- Use C99 initialization for ioctl struct

v2:
- Bump minor version up to 3.

---
 tools/include/xen-sys/Linux/privcmd.h              | 11 +++++
 tools/libs/foreignmemory/Makefile                  |  2 +-
 tools/libs/foreignmemory/core.c                    | 53 ++++++++++++++++++++++
 .../libs/foreignmemory/include/xenforeignmemory.h  | 41 +++++++++++++++++
 tools/libs/foreignmemory/libxenforeignmemory.map   |  5 ++
 tools/libs/foreignmemory/linux.c                   | 45 ++++++++++++++++++
 tools/libs/foreignmemory/private.h                 | 31 +++++++++++++
 7 files changed, 187 insertions(+), 1 deletion(-)
tools/include/xen-sys/Linux/privcmd.h +11 -0
index 732ff7c15a..9531b728f9 100644
@@ -86,6 +86,15 @@ typedef struct privcmd_dm_op {
 	const privcmd_dm_op_buf_t __user *ubufs;
 } privcmd_dm_op_t;
 
+typedef struct privcmd_mmap_resource {
+	domid_t dom;
+	__u32 type;
+	__u32 id;
+	__u32 idx;
+	__u64 num;
+	__u64 addr;
+} privcmd_mmap_resource_t;
+
 /*
  * @cmd: IOCTL_PRIVCMD_HYPERCALL
  * @arg: &privcmd_hypercall_t
@@ -103,5 +112,7 @@ typedef struct privcmd_dm_op {
 	_IOC(_IOC_NONE, 'P', 5, sizeof(privcmd_dm_op_t))
 #define IOCTL_PRIVCMD_RESTRICT					\
 	_IOC(_IOC_NONE, 'P', 6, sizeof(domid_t))
+#define IOCTL_PRIVCMD_MMAP_RESOURCE				\
+	_IOC(_IOC_NONE, 'P', 7, sizeof(privcmd_mmap_resource_t))
 
 #endif /* __LINUX_PUBLIC_PRIVCMD_H__ */
tools/libs/foreignmemory/Makefile +1 -1
index cbe815fce8..ee5c3fd67e 100644
@@ -2,7 +2,7 @@ XEN_ROOT = $(CURDIR)/../../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 MAJOR    = 1
-MINOR    = 2
+MINOR    = 3
 SHLIB_LDFLAGS += -Wl,--version-script=libxenforeignmemory.map
 
 CFLAGS   += -Werror -Wmissing-prototypes
tools/libs/foreignmemory/core.c +53 -0

65 lines not on this page · raw message

tools/libs/foreignmemory/include/xenforeignmemory.h +41 -0

47 lines not on this page · raw message

tools/libs/foreignmemory/libxenforeignmemory.map +5 -0
index 716ecaf15c..d5323c87d9 100644
@@ -14,3 +14,8 @@ VERS_1.2 {
 	global:
 		xenforeignmemory_map2;
 } VERS_1.1;
+VERS_1.3 {
+	global:
+		xenforeignmemory_map_resource;
+		xenforeignmemory_unmap_resource;
+} VERS_1.2;
tools/libs/foreignmemory/linux.c +45 -0

51 lines not on this page · raw message

tools/libs/foreignmemory/private.h +31 -0
index 2470f3c46c..b191000b49 100644
@@ -45,6 +45,37 @@ void *compat_mapforeign_batch(xenforeignmem_handle *fmem, uint32_t dom,
                               xen_pfn_t *arr, int num);
 #endif
 
+struct xenforeignmemory_resource_handle {
+    domid_t domid;
+    unsigned int type;
+    unsigned int id;
+    unsigned long frame;
+    unsigned long nr_frames;
+    void *addr;
+    int prot;
+    int flags;
+};
+
+#ifndef __linux__
+static inline int osdep_xenforeignmemory_map_resource(
+    xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres)
+{
+    errno = EOPNOTSUPP;
+    return -1;
+}
+
+static inline int osdep_xenforeignmemory_unmap_resource(
+    xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres)
+{
+    return 0;
+}
+#else
+int osdep_xenforeignmemory_map_resource(
+    xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres);
+int osdep_xenforeignmemory_unmap_resource(
+    xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres);
+#endif
+
 #define PERROR(_f...) \
     xtl_log(fmem->logger, XTL_ERROR, errno, "xenforeignmemory", _f)
 
Signature · 7 lines
2.11.0


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

PATCH v18 9/11 tools/libxenforeignmemory: reduce xenforeignmemory_restrict code footprint

In reply to Paul Durrant

By using a static inline stub in private.h for OS where this functionality is not implemented, the various duplicate stubs in the OS-specific source modules can be avoided.

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

v4:
- Removed extraneous freebsd code.

v3:
- Patch added in response to review comments.

---
 tools/libs/foreignmemory/freebsd.c |  7 -------
 tools/libs/foreignmemory/minios.c  |  7 -------
 tools/libs/foreignmemory/netbsd.c  |  7 -------
 tools/libs/foreignmemory/private.h | 12 +++++++++---
 tools/libs/foreignmemory/solaris.c |  7 -------
 5 files changed, 9 insertions(+), 31 deletions(-)
tools/libs/foreignmemory/freebsd.c +0 -7
index dec447485a..6e6bc4b11f 100644
@@ -95,13 +95,6 @@ int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
     return munmap(addr, num << PAGE_SHIFT);
 }
 
-int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
-                                    domid_t domid)
-{
-    errno = -EOPNOTSUPP;
-    return -1;
-}
-
 /*
  * Local variables:
  * mode: C
tools/libs/foreignmemory/minios.c +0 -7
index 75f340122e..43341ca301 100644
@@ -58,13 +58,6 @@ int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
     return munmap(addr, num << PAGE_SHIFT);
 }
 
-int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
-                                    domid_t domid)
-{
-    errno = -EOPNOTSUPP;
-    return -1;
-}
-
 /*
  * Local variables:
  * mode: C
tools/libs/foreignmemory/netbsd.c +0 -7
index 9bf95ef4f0..54a418ebd6 100644
@@ -100,13 +100,6 @@ int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
     return munmap(addr, num*XC_PAGE_SIZE);
 }
 
-int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
-                                    domid_t domid)
-{
-    errno = -EOPNOTSUPP;
-    return -1;
-}
-
 /*
  * Local variables:
  * mode: C
tools/libs/foreignmemory/private.h +9 -3
index b191000b49..b06ce12583 100644
@@ -35,9 +35,6 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num);
 
-int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
-                                    domid_t domid);
-
 #if defined(__NetBSD__) || defined(__sun__)
 /* Strictly compat for those two only only */
 void *compat_mapforeign_batch(xenforeignmem_handle *fmem, uint32_t dom,
@@ -57,6 +54,13 @@ struct xenforeignmemory_resource_handle {
 };
 
 #ifndef __linux__
+static inline int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
+                                                  domid_t domid)
+{
+    errno = EOPNOTSUPP;
+    return -1;
+}
+
 static inline int osdep_xenforeignmemory_map_resource(
     xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres)
 {
@@ -70,6 +74,8 @@ static inline int osdep_xenforeignmemory_unmap_resource(
     return 0;
 }
 #else
+int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
+                                    domid_t domid);
 int osdep_xenforeignmemory_map_resource(
     xenforeignmemory_handle *fmem, xenforeignmemory_resource_handle *fres);
 int osdep_xenforeignmemory_unmap_resource(
tools/libs/foreignmemory/solaris.c +0 -7
index a33decb4ae..ee8aae4fbd 100644
@@ -97,13 +97,6 @@ int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
     return munmap(addr, num*XC_PAGE_SIZE);
 }
 
-int osdep_xenforeignmemory_restrict(xenforeignmemory_handle *fmem,
-                                    domid_t domid)
-{
-    errno = -EOPNOTSUPP;
-    return -1;
-}
-
 /*
  * Local variables:
  * mode: C
Signature · 7 lines
2.11.0


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

PATCH v18 10/11 common: add a new mappable resource type: XENMEM_resource_grant_table

In reply to Paul Durrant

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.

---
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]>

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      | 71 +++++++++++++++++++++++++++++++++++--------
 xen/common/memory.c           | 45 ++++++++++++++++++++++++++-
 xen/include/public/memory.h   |  9 ++++--
 xen/include/xen/grant_table.h |  4 +++
 4 files changed, 113 insertions(+), 16 deletions(-)
xen/common/grant_table.c +58 -13
index 18201912e4..c8c3661b19 100644
@@ -3863,6 +3863,35 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 }
 #endif
 
+/* caller must hold read or write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+
+    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)
+{
+    struct grant_table *gt = d->grant_table;
+
+    if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
+        gnttab_grow_table(d, idx + 1);
+
+    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)
 {
@@ -3880,21 +3909,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) )
@@ -3909,6 +3928,32 @@ 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);
+    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_read_lock(gt);
+    rc = gnttab_get_status_frame_mfn(d, idx, mfn);
+    grant_read_unlock(gt);
+
+    return rc;
+}
+
 static void gnttab_usage_print(struct domain *rd)
 {
     int first = 1;
xen/common/memory.c +44 -1

69 lines not on this page · raw message

xen/include/public/memory.h +7 -2
index c5cd2ba1a2..c653772333 100644
@@ -611,16 +611,21 @@ 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;
-    /*
-     * IN/OUT - As an IN parameter number of frames of the resource
+
+#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
      *          frame_list is NULL then this field will be set to the
      *          maximum value supported by the implementation on return.
xen/include/xen/grant_table.h +4 -0
index b3a95fda58..144d2cde35 100644
@@ -55,6 +55,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
Reviewed-by on v18 10/11
#25 lore raw

Re: [PATCH v18 10/11] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 25 lines
Quoted 1 line
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/grant_table.c +18 -0
@@ -3863,6 +3863,35 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 }
 #endif
 
+/* caller must hold read or write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+
+    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)
+{
+    struct grant_table *gt = d->grant_table;

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

Quoted 10 lines
xen/common/grant_table.c +10 -0
+
+    if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
+        gnttab_grow_table(d, idx + 1);
+
+    if ( idx >= nr_grant_frames(gt) )
+        return -EINVAL;
+
+    *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
+    return 0;
+}

I realize the anomaly was there already before, but imo it becomes more pronounced with the two functions differing in more than just the shared vs status naming (IOW I find it strange that one grows the grant table while the other doesn't). This extends to ...

Quoted 15 lines
xen/common/grant_table.c +15 -0
+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);
+    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)

xen/common/grant_table.c int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, · line 3955

Quoted 10 lines
xen/common/grant_table.c +10 -0
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_read_lock(gt);
+    rc = gnttab_get_status_frame_mfn(d, idx, mfn);
+    grant_read_unlock(gt);
+
+    return rc;
+}

... these two acquiring the lock in different ways.

And then I'm completely missing the interaction with gnttab_unpopulate_status_frames(). While this might not be a practical problem at this point in time, we're liable to forget to address this later on if there's no stop gap measure. A PV guest mapping the obtained MFNs is going to be fine, but a HVM/PVH one isn't, since neither x86 nor ARM refcount pages inserted into or removed from a domain's p2m. I therefore think you need to add a is_hvm_domain() check to acquire_grant_table(), with a suitable fixme comment attached to it.

With that (and despite the noticed anomaly)

Jan

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

#26 lore raw

Re: [PATCH v18 10/11] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 70 lines
Quoted 26 lines
Quoted 2 lines · On 26.03.18 at 14:16, <[email protected]> wrote:
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/grant_table.c +18 -0
@@ -3863,6 +3863,35 @@ int mem_sharing_gref_to_gfn(struct grant_table *gt, grant_ref_t ref,
 }
 #endif
 
+/* caller must hold read or write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+
+    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)
+{
+    struct grant_table *gt = d->grant_table;

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

Quoted 10 lines
xen/common/grant_table.c +10 -0
+
+    if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
+        gnttab_grow_table(d, idx + 1);
+
+    if ( idx >= nr_grant_frames(gt) )
+        return -EINVAL;
+
+    *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
+    return 0;
+}

I realize the anomaly was there already before, but imo it becomes more pronounced with the two functions differing in more than just the shared vs status naming (IOW I find it strange that one grows the grant table while the other doesn't). This extends to ...

Quoted 15 lines
xen/common/grant_table.c +15 -0
+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);
+    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)

xen/common/grant_table.c int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, · line 3955

Quoted 10 lines
xen/common/grant_table.c +10 -0
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_read_lock(gt);
+    rc = gnttab_get_status_frame_mfn(d, idx, mfn);
+    grant_read_unlock(gt);
+
+    return rc;
+}

... these two acquiring the lock in different ways.

Quoted 9 lines

And then I'm completely missing the interaction with gnttab_unpopulate_status_frames(). While this might not be a practical problem at this point in time, we're liable to forget to address this later on if there's no stop gap measure. A PV guest mapping the obtained MFNs is going to be fine, but a HVM/PVH one isn't, since neither x86 nor ARM refcount pages inserted into or removed from a domain's p2m. I therefore think you need to add a is_hvm_domain() check to acquire_grant_table(), with a suitable fixme comment attached to it.

Or perhaps better paging_mode_translate(current->domain).

Jan

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

#27 lore raw

Re: [PATCH v18 10/11] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 12 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 13:55
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]>; [email protected]; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH v18 10/11] common: add a new mappable resource
type: XENMEM_resource_grant_table

Quoted 70 lines
Quoted 5 lines

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct

Quoted 5 lines
Quoted 2 lines · On 26.03.18 at 14:16, <[email protected]> wrote:
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/grant_table.c +0 -0
@@ -3863,6 +3863,35 @@ int mem_sharing_gref_to_gfn(struct

grant_table *gt, grant_ref_t ref,

Quoted 64 lines
Quoted 21 lines
xen/common/grant_table.c +18 -0
 }
 #endif
 
+/* caller must hold read or write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+
+    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)
+{
+    struct grant_table *gt = d->grant_table;

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

Quoted 10 lines
xen/common/grant_table.c +10 -0
+
+    if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
+        gnttab_grow_table(d, idx + 1);
+
+    if ( idx >= nr_grant_frames(gt) )
+        return -EINVAL;
+
+    *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
+    return 0;
+}

I realize the anomaly was there already before, but imo it becomes more pronounced with the two functions differing in more than just the shared vs status naming (IOW I find it strange that one grows the grant table while the other doesn't). This extends to ...

Quoted 15 lines
xen/common/grant_table.c +15 -0
+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);
+    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)

xen/common/grant_table.c int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, · line 3955

Quoted 10 lines
xen/common/grant_table.c +10 -0
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_read_lock(gt);
+    rc = gnttab_get_status_frame_mfn(d, idx, mfn);
+    grant_read_unlock(gt);
+
+    return rc;
+}

... these two acquiring the lock in different ways.

So, you want me to have gnttab_get_status_frame() grow the table accordingly? I'd really rather not do that at v19 of the series when it's never been part of the scope before.

Quoted 11 lines
Quoted 10 lines

And then I'm completely missing the interaction with gnttab_unpopulate_status_frames(). While this might not be a practical problem at this point in time, we're liable to forget to address this later on if there's no stop gap measure. A PV guest mapping the obtained MFNs is going to be fine, but a HVM/PVH one isn't, since neither x86 nor ARM refcount pages inserted into or removed from a domain's p2m. I therefore think you need to add a is_hvm_domain() check to acquire_grant_table(), with a suitable fixme comment attached to it.

Quoted 2 lines

Or perhaps better paging_mode_translate(current->domain).

Ok. I'll add the safety check and comment.

Paul

Quoted 1 line

Jan

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

#28 lore raw

Re: [PATCH v18 10/11] common: add a new mappable resource type: XENMEM_resource_grant_table

Quoted 75 lines
Quoted 4 lines
Quoted 1 line · On 29.03.18 at 13:02, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 March 2018 13:55

Quoted 70 lines
Quoted 5 lines

xen/common/grant_table.c int mem_sharing_gref_to_gfn(struct

Quoted 5 lines
Quoted 2 lines · On 26.03.18 at 14:16, <[email protected]> wrote:
Quoted 1 line · On 22.03.18 at 12:55, <[email protected]> wrote:
xen/common/grant_table.c +0 -0
@@ -3863,6 +3863,35 @@ int mem_sharing_gref_to_gfn(struct

grant_table *gt, grant_ref_t ref,

Quoted 64 lines
Quoted 21 lines
xen/common/grant_table.c +18 -0
 }
 #endif
 
+/* caller must hold read or write lock */
+static int gnttab_get_status_frame_mfn(struct domain *d,
+                                       unsigned long idx, mfn_t *mfn)
+{
+    struct grant_table *gt = d->grant_table;
+
+    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)
+{
+    struct grant_table *gt = d->grant_table;

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

Quoted 10 lines
xen/common/grant_table.c +10 -0
+
+    if ( (idx >= nr_grant_frames(gt)) && (idx < gt->max_grant_frames) )
+        gnttab_grow_table(d, idx + 1);
+
+    if ( idx >= nr_grant_frames(gt) )
+        return -EINVAL;
+
+    *mfn = _mfn(virt_to_mfn(gt->shared_raw[idx]));
+    return 0;
+}

I realize the anomaly was there already before, but imo it becomes more pronounced with the two functions differing in more than just the shared vs status naming (IOW I find it strange that one grows the grant table while the other doesn't). This extends to ...

Quoted 15 lines
xen/common/grant_table.c +15 -0
+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);
+    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)

xen/common/grant_table.c int gnttab_map_frame(struct domain *d, unsigned long idx, gfn_t gfn, · line 3955

Quoted 10 lines
xen/common/grant_table.c +10 -0
+{
+    struct grant_table *gt = d->grant_table;
+    int rc;
+
+    grant_read_lock(gt);
+    rc = gnttab_get_status_frame_mfn(d, idx, mfn);
+    grant_read_unlock(gt);
+
+    return rc;
+}

... these two acquiring the lock in different ways.

Quoted 3 lines

So, you want me to have gnttab_get_status_frame() grow the table accordingly? I'd really rather not do that at v19 of the series when it's never been part of the scope before.

Indeed, please only take this as a remark at this point in time. It just so happens that when you look at something again after a fair amount of time, you see things you haven't noticed before.

Jan

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

#29 lore raw

PATCH v18 11/11 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]>

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        |   8 +--
 tools/libxc/xc_dom_boot.c           | 114 +++++++++++++++++++++++++-----------
 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, 92 insertions(+), 49 deletions(-)
tools/libxc/include/xc_dom.h +2 -6
index 491cad8114..cee2ac9901 100644
@@ -332,12 +332,8 @@ 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,
+int xc_dom_gnttab_seed(xc_interface *xch, uint32_t guest_domid,
+                       bool is_hvm,
                        xen_pfn_t console_gmfn,
                        xen_pfn_t xenstore_gmfn,
                        uint32_t console_domid,
tools/libxc/xc_dom_boot.c +81 -33

146 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 2e29b52166..5610f0ae89 100644
@@ -903,7 +903,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 f501764100..51dc2afb1e 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