Skip to contentSkip to conversation

Extend ioreq server to support multiple ioreq pages

PATCH In review Julian Vetter 2 participants 6 messages Mar 16 – Mar 24

lore
Share & tools
lore
#0 lore raw

v5 cover Extend ioreq server to support multiple ioreq pages

Thank you again Jan for you feedback on the series! As you suggested I have kept the struct ioreq_page fully intakt and just dropped the struct page_info *page, because I have replaced the __map_domain_page_global() by an explicit vmap(). So, the page can be recovered via vmap_to_page(). My only concern with my patch series is that in non-debug builds on x86, map_domain_page_global() avoids vmap overhead for low MFNs by directly returning a directmap address. But when switching to vmap() unconditionally this optimization is gone. But this is required so that vmap_to_page() works correctly and can recover the struct page_info * uniformly at teardown. What do you think?

Julian Vetter (3):
ioreq: Unify buf and non-buf ioreq page management
ioreq: Indent ioreq_server_{alloc,free}_mfn() body one level deeper x86/ioreq: Extend ioreq server to support multiple ioreq pages

 xen/arch/x86/hvm/ioreq.c |  65 +++++++++++++++---
 xen/common/ioreq.c       | 141 +++++++++++++++++++++++++++------------
 xen/include/xen/ioreq.h  |   7 +-
 3 files changed, 158 insertions(+), 55 deletions(-)
Signature · 10 lines
2.51.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

PATCH v5 1/3 ioreq: Unify buf and non-buf ioreq page management

Switch the ioreq page mapping in hvm_map_ioreq_gfn() from prepare_ring_for_helper() / __map_domain_page_global() to explicit vmap(), aligning it with ioreq_server_alloc_mfn() which already allocates domain-heap pages and will now also map them via vmap().

With both paths using vmap(), vmap_to_page() can recover the struct page_info * uniformly during teardown, removing the need to cache the page pointer in struct ioreq_page. So, drop the 'page' field from struct ioreq_page and update all callers accordingly.

---
Changes in v5:
- New patch that unforms the buf and non-buf code path

---
 xen/arch/x86/hvm/ioreq.c | 57 ++++++++++++++++++++++++++++++++--------
 xen/common/ioreq.c       | 36 +++++++++++++------------
 xen/include/xen/ioreq.h  |  1 -
 3 files changed, 65 insertions(+), 29 deletions(-)
xen/arch/x86/hvm/ioreq.c +46 -11
index a5fa97e149..145dcba5c1 100644
@@ -15,6 +15,7 @@
 #include <xen/sched.h>
 #include <xen/softirq.h>
 #include <xen/trace.h>
+#include <xen/vmap.h>
 #include <xen/vpci.h>
 
 #include <asm/hvm/emulate.h>
@@ -128,8 +129,9 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( gfn_eq(iorp->gfn, INVALID_GFN) )
         return;
 
-    destroy_ring_for_helper(&iorp->va, iorp->page);
-    iorp->page = NULL;
+    put_page_and_type(vmap_to_page(iorp->va));
+    vunmap(iorp->va);
+    iorp->va = NULL;
 
     hvm_free_ioreq_gfn(s, iorp->gfn);
     iorp->gfn = INVALID_GFN;
@@ -139,9 +141,13 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
 {
     struct domain *d = s->target;
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+    struct page_info *page;
+    p2m_type_t p2mt;
+    gfn_t base_gfn;
+    mfn_t mfn;
     int rc;
 
-    if ( iorp->page )
+    if ( iorp->va )
     {
         /*
          * If a page has already been allocated (which will happen on
@@ -157,17 +163,45 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( d->is_dying )
         return -EINVAL;
 
-    iorp->gfn = hvm_alloc_ioreq_gfn(s);
+    base_gfn = hvm_alloc_ioreq_gfn(s);
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+    if ( gfn_eq(base_gfn, INVALID_GFN) )
         return -ENOMEM;
 
-    rc = prepare_ring_for_helper(d, gfn_x(iorp->gfn), &iorp->page,
-                                 &iorp->va);
-
+    /*
+     * vmap() is used for the Xen-side mapping so that vmap_to_page() can
+     * recover the struct page_info * during teardown, consistent with
+     * ioreq_server_alloc_mfn().
+     */
+    rc = check_get_page_from_gfn(d, base_gfn, false, &p2mt, &page);
     if ( rc )
-        hvm_unmap_ioreq_gfn(s, buf);
+    {
+        if ( rc == -EAGAIN )
+            rc = -ENOENT;
+        goto fail;
+    }
+
+    if ( !get_page_type(page, PGT_writable_page) )
+    {
+        put_page(page);
+        rc = -EINVAL;
+        goto fail;
+    }
+
+    mfn = page_to_mfn(page);
+    iorp->va = vmap(&mfn, 1);
+    if ( !iorp->va )
+    {
+        put_page_and_type(page);
+        rc = -ENOMEM;
+        goto fail;
+    }
+
+    iorp->gfn = base_gfn;
+    return 0;
 
+ fail:
+    hvm_free_ioreq_gfn(s, base_gfn);
     return rc;
 }
 
@@ -179,7 +213,7 @@ static void hvm_remove_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( gfn_eq(iorp->gfn, INVALID_GFN) )
         return;
 
-    if ( p2m_remove_page(d, iorp->gfn, page_to_mfn(iorp->page), 0) )
+    if ( p2m_remove_page(d, iorp->gfn, page_to_mfn(vmap_to_page(iorp->va)), 0) )
         domain_crash(d);
     clear_page(iorp->va);
 }
@@ -195,7 +229,8 @@ static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
 
     clear_page(iorp->va);
 
-    rc = p2m_add_page(d, iorp->gfn, page_to_mfn(iorp->page), 0, p2m_ram_rw);
+    rc = p2m_add_page(d, iorp->gfn, page_to_mfn(vmap_to_page(iorp->va)), 0,
+                      p2m_ram_rw);
     if ( rc == 0 )
         paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn)));
 
xen/common/ioreq.c +19 -17
index f5fd30ce12..5b026fc1b2 100644
@@ -17,11 +17,11 @@
  */
 
 #include <xen/domain.h>
-#include <xen/domain_page.h>
 #include <xen/event.h>
 #include <xen/init.h>
 #include <xen/ioreq.h>
 #include <xen/irq.h>
+#include <xen/vmap.h>
 #include <xen/lib.h>
 #include <xen/paging.h>
 #include <xen/sched.h>
@@ -262,8 +262,9 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
     struct page_info *page;
+    mfn_t mfn;
 
-    if ( iorp->page )
+    if ( iorp->va )
     {
         /*
          * If a guest frame has already been mapped (which may happen
@@ -291,11 +292,11 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
         return -ENODATA;
     }
 
-    iorp->va = __map_domain_page_global(page);
+    mfn = page_to_mfn(page);
+    iorp->va = vmap(&mfn, 1);
     if ( !iorp->va )
         goto fail;
 
-    iorp->page = page;
     clear_page(iorp->va);
     return 0;
 
@@ -309,14 +310,13 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
 static void ioreq_server_free_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
-    struct page_info *page = iorp->page;
+    struct page_info *page;
 
-    if ( !page )
+    if ( !iorp->va )
         return;
 
-    iorp->page = NULL;
-
-    unmap_domain_page_global(iorp->va);
+    page = vmap_to_page(iorp->va);
+    vunmap(iorp->va);
     iorp->va = NULL;
 
     put_page_alloc_ref(page);
@@ -333,7 +333,8 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
 
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
-        if ( (s->ioreq.page == page) || (s->bufioreq.page == page) )
+        if ( (s->ioreq.va && vmap_to_page(s->ioreq.va) == page) ||
+             (s->bufioreq.va && vmap_to_page(s->bufioreq.va) == page) )
         {
             found = true;
             break;
@@ -626,11 +627,12 @@ static void ioreq_server_deinit(struct ioreq_server *s)
     /*
      * NOTE: It is safe to call both arch_ioreq_server_unmap_pages() and
      *       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.
+     *       arch_ioreq_server_unmap_pages() handles the GFN-mapped path
+     *       (iorp->gfn != INVALID_GFN) and clears iorp->va on completion,
+     *       so ioreq_server_free_pages() will find iorp->va == NULL and
+     *       do nothing. Conversely, pages allocated via the resource path
+     *       have iorp->gfn == INVALID_GFN, so arch_ioreq_server_unmap_pages()
+     *       is a no-op and ioreq_server_free_pages() handles the teardown.
      */
     arch_ioreq_server_unmap_pages(s);
     ioreq_server_free_pages(s);
@@ -819,12 +821,12 @@ int ioreq_server_get_frame(struct domain *d, ioservid_t id,
         if ( !HANDLE_BUFIOREQ(s) )
             goto out;
 
-        *mfn = page_to_mfn(s->bufioreq.page);
+        *mfn = page_to_mfn(vmap_to_page(s->bufioreq.va));
         rc = 0;
         break;
 
     case XENMEM_resource_ioreq_server_frame_ioreq(0):
-        *mfn = page_to_mfn(s->ioreq.page);
+        *mfn = page_to_mfn(vmap_to_page(s->ioreq.va));
         rc = 0;
         break;
 
xen/include/xen/ioreq.h +0 -1
index e86f0869fa..d63fa4729e 100644
@@ -25,7 +25,6 @@
 
 struct ioreq_page {
     gfn_t gfn;
-    struct page_info *page;
     void *va;
 };
 
Signature · 10 lines
2.51.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#2 lore raw

Re: [PATCH v5 1/3] ioreq: Unify buf and non-buf ioreq page management

Quoted 4 lines · Julian Vetter

Switch the ioreq page mapping in hvm_map_ioreq_gfn() from prepare_ring_for_helper() / __map_domain_page_global() to explicit vmap(), aligning it with ioreq_server_alloc_mfn() which already allocates domain-heap pages and will now also map them via vmap().

In debug builds it did so before already, just indirectly through map_domain_page_global(). You may want to adjust the wording slightly.

Quoted 6 lines

With both paths using vmap(), vmap_to_page() can recover the struct page_info * uniformly during teardown, removing the need to cache the page pointer in struct ioreq_page. So, drop the 'page' field from struct ioreq_page and update all callers accordingly.

What's missing is _why_ you actually want to make this change. Without that info, one may want to reject the change for adding overhead for no gain. This would then also help with naming choices like "base_gfn".

xen/arch/x86/hvm/ioreq.c static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf) · line 134

Quoted 9 lines
xen/arch/x86/hvm/ioreq.c +3 -2
@@ -128,8 +129,9 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( gfn_eq(iorp->gfn, INVALID_GFN) )
         return;
 
-    destroy_ring_for_helper(&iorp->va, iorp->page);
-    iorp->page = NULL;
+    put_page_and_type(vmap_to_page(iorp->va));
+    vunmap(iorp->va);
+    iorp->va = NULL;

In ioreq_server_deinit() you alter a comment regarding arch_ioreq_server_unmap_pages(), which calls the function here. The property described there looks to be lost.

Here (and in the counterpart function below) I think you also want to leave a comment that this is effectively {destroy,prepare}_ring_for_helper(), merely using vmap(). That'll increase the chance of noticing a change is needed here as well in case those functions are modified.

Quoted 12 lines
xen/arch/x86/hvm/ioreq.c +2 -3
@@ -157,17 +163,45 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( d->is_dying )
         return -EINVAL;
 
-    iorp->gfn = hvm_alloc_ioreq_gfn(s);
+    base_gfn = hvm_alloc_ioreq_gfn(s);
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+    if ( gfn_eq(base_gfn, INVALID_GFN) )
         return -ENOMEM;
 
-    rc = prepare_ring_for_helper(d, gfn_x(iorp->gfn), &iorp->page,

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +6 -3
-                                 &iorp->va);
-
+    /*
+     * vmap() is used for the Xen-side mapping so that vmap_to_page() can
+     * recover the struct page_info * during teardown, consistent with
+     * ioreq_server_alloc_mfn().
+     */
+    rc = check_get_page_from_gfn(d, base_gfn, false, &p2mt, &page);
     if ( rc )
-        hvm_unmap_ioreq_gfn(s, buf);

With the comment above addressed, I think this should be possible to keep. (FTAOD the same isn't true for the other error paths further down.)

Review static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 263

Quoted 3 lines
(quoted diff) +0 -0
@@ -262,8 +262,9 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;

This is a good place to comment on the patch title: Here we're dealing (in unified manner) with both buffered and non-buffered ioreq-s. There's nothing being further unified in this regard. I think you must mean something else.

Quoted 9 lines
xen/common/ioreq.c +2 -2
@@ -309,14 +310,13 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
 static void ioreq_server_free_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
-    struct page_info *page = iorp->page;
+    struct page_info *page;
 
-    if ( !page )
+    if ( !iorp->va )

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 322

Quoted 10 lines
xen/common/ioreq.c +2 -3
         return;
 
-    iorp->page = NULL;
-
-    unmap_domain_page_global(iorp->va);
+    page = vmap_to_page(iorp->va);
+    vunmap(iorp->va);
     iorp->va = NULL;
 
     put_page_alloc_ref(page);

Operations want re-ordering a little, to retain the prior property of what the if() at the top is checking for getting cleared _before_ freeing / unmapping anything.

xen/common/ioreq.c int ioreq_server_get_frame(struct domain *d, ioservid_t id, · line 824

Quoted 6 lines
xen/common/ioreq.c +1 -1
@@ -819,12 +821,12 @@ int ioreq_server_get_frame(struct domain *d, ioservid_t id,
         if ( !HANDLE_BUFIOREQ(s) )
             goto out;
 
-        *mfn = page_to_mfn(s->bufioreq.page);
+        *mfn = page_to_mfn(vmap_to_page(s->bufioreq.va));

You did look at what vmap_to_page() expands to, didn't you? If you did, didn't it occur to you to use vmap_to_mfn() directly? (Applies elsewhere as well.)

Jan

#3 lore raw

PATCH v5 2/3 ioreq: Indent ioreq_server_{alloc,free}_mfn() body one level deeper

In reply to Julian Vetter

No functional change. It adds a wrapping block to prepare for the loop that the subsequent patch introduces to handle multiple ioreq pages.

---
Changes in v5
- Added proper commit message and fixed commit title

---
 xen/common/ioreq.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
xen/common/ioreq.c +22 -18
index 5b026fc1b2..b22f656701 100644
@@ -277,22 +277,24 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
         return 0;
     }
 
-    page = alloc_domheap_page(s->target, MEMF_no_refcount);
+    {
+        page = alloc_domheap_page(s->target, MEMF_no_refcount);
 
-    if ( !page )
-        return -ENOMEM;
+        if ( !page )
+            return -ENOMEM;
 
-    if ( !get_page_and_type(page, s->target, PGT_writable_page) )
-    {
-        /*
-         * The domain can't possibly know about this page yet, so failure
-         * here is a clear indication of something fishy going on.
-         */
-        domain_crash(s->emulator);
-        return -ENODATA;
-    }
+        if ( !get_page_and_type(page, s->target, PGT_writable_page) )
+        {
+            /*
+             * The domain can't possibly know about this page yet, so failure
+             * here is a clear indication of something fishy going on.
+             */
+            domain_crash(s->emulator);
+            return -ENODATA;
+        }
 
-    mfn = page_to_mfn(page);
+        mfn = page_to_mfn(page);
+    }
     iorp->va = vmap(&mfn, 1);
     if ( !iorp->va )
         goto fail;
@@ -315,12 +317,14 @@ static void ioreq_server_free_mfn(struct ioreq_server *s, bool buf)
     if ( !iorp->va )
         return;
 
-    page = vmap_to_page(iorp->va);
-    vunmap(iorp->va);
-    iorp->va = NULL;
+    {
+        page = vmap_to_page(iorp->va);
+        vunmap(iorp->va);
+        iorp->va = NULL;
 
-    put_page_alloc_ref(page);
-    put_page_and_type(page);
+        put_page_alloc_ref(page);
+        put_page_and_type(page);
+    }
 }
 
 bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
Signature · 10 lines
2.51.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#4 lore raw

PATCH v5 3/3 x86/ioreq: Extend ioreq server to support multiple ioreq pages

In reply to Julian Vetter

A domain with more than (PAGE_SIZE / sizeof(ioreq_t)) vCPUs needs more than one ioreq page to hold all per-vCPU ioreq slots. In order to support this a number of changes have been made:

1. Add nr_ioreq_pages() to compute the required number of pages, defined as DIV_ROUND_UP(d->max_vcpus, PAGE_SIZE / sizeof(ioreq_t))
2. ioreq_server_alloc_mfn() now allocates nr_ioreq_pages() pages for the non-buf case, builds an mfn_t array, and calls vmap() to map them contiguously. The buf path remains single-page.
3. ioreq_server_free_mfn() uses vmap_size() to determine how many pages to release.
4. is_ioreq_server_page() loops over all mapped ioreq pages using vmap_size() and vmap_to_page() with per-page offsets
5. ioreq_server_get_frame() now handles idx values in the range [XENMEM_resource_ioreq_server_frame_ioreq(0), XENMEM_resource_ioreq_server_frame_ioreq(nr_pages - 1)], returning the MFN via vmap_to_mfn() with the appropriate page offset.

The legacy GFN path (hvm_map_ioreq_gfn) is restricted to single-page. Domains with more vCPUs must use XENMEM_acquire_resource!

---
Changes in v5:
- Reduced complexity a lot because there is no distinction between buf and !buf case
- Directly use va and gfn from struct ioreq_page, dropped additional members in struct ioreq_server

---
 xen/arch/x86/hvm/ioreq.c |   8 +++
 xen/common/ioreq.c       | 103 +++++++++++++++++++++++++++++----------
 xen/include/xen/ioreq.h  |   6 +++
 3 files changed, 90 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/ioreq.c +8 -0
index 145dcba5c1..872247e300 100644
@@ -163,6 +163,14 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( d->is_dying )
         return -EINVAL;
 
+    /*
+     * The legacy GFN path supports only a single ioreq page. Guests requiring
+     * more ioreq slots must use the resource mapping interface
+     * (XENMEM_acquire_resource).
+     */
+    if ( !buf && nr_ioreq_pages(d) > 1 )
+        return -EOPNOTSUPP;
+
     base_gfn = hvm_alloc_ioreq_gfn(s);
 
     if ( gfn_eq(base_gfn, INVALID_GFN) )
xen/common/ioreq.c +76 -27

174 lines not on this page · raw message

xen/include/xen/ioreq.h +6 -0
index d63fa4729e..c12480472d 100644
@@ -19,6 +19,7 @@
 #ifndef __XEN_IOREQ_H__
 #define __XEN_IOREQ_H__
 
+#include <xen/macros.h>
 #include <xen/sched.h>
 
 #include <public/hvm/dm_op.h>
@@ -82,6 +83,11 @@ static inline bool ioreq_needs_completion(const ioreq_t *ioreq)
 #define HANDLE_BUFIOREQ(s) \
     ((s)->bufioreq_handling != HVM_IOREQSRV_BUFIOREQ_OFF)
 
+static inline unsigned int nr_ioreq_pages(const struct domain *d)
+{
+    return DIV_ROUND_UP(d->max_vcpus, PAGE_SIZE / sizeof(ioreq_t));
+}
+
 bool domain_has_ioreq_server(const struct domain *d);
 
 bool vcpu_ioreq_pending(struct vcpu *v);
Signature · 10 lines
2.51.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#5 lore raw

Re: [PATCH v5 3/3] x86/ioreq: Extend ioreq server to support multiple ioreq pages

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

Quoted 13 lines · Julian Vetter
xen/arch/x86/hvm/ioreq.c +7 -0
@@ -163,6 +163,14 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
     if ( d->is_dying )
         return -EINVAL;
 
+    /*
+     * The legacy GFN path supports only a single ioreq page. Guests requiring
+     * more ioreq slots must use the resource mapping interface
+     * (XENMEM_acquire_resource).
+     */
+    if ( !buf && nr_ioreq_pages(d) > 1 )
+        return -EOPNOTSUPP;

Nit towards the comment: It's not guests using this themselves, but their device models. This wants wording accurately.

Quoted 15 lines
xen/common/ioreq.c +3 -2
@@ -261,8 +261,9 @@ bool vcpu_ioreq_handle_completion(struct vcpu *v)
 static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
-    struct page_info *page;
-    mfn_t mfn;
+    unsigned int i, nr_pages = buf ? 1 : nr_ioreq_pages(s->target);
+    mfn_t *mfns;
+    int rc;
 
     if ( iorp->va )
     {

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 283

Quoted 6 lines
xen/common/ioreq.c +3 -0
@@ -277,11 +278,20 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
         return 0;
     }
 
+    mfns = xmalloc_array(mfn_t, nr_pages);
+    if ( !mfns )
+        return -ENOMEM;

It would be nice to avoid this allocation for guests requiring only a single page, perhaps even for ones requiring only a few. (Ideally we'd avoid such a runtime allocation altogether, as they hinder certification aiui.)

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 288

Quoted 5 lines
xen/common/ioreq.c +3 -1
+    for ( i = 0; i < nr_pages; i++ )
     {
-        page = alloc_domheap_page(s->target, MEMF_no_refcount);
+        struct page_info *page = alloc_domheap_page(s->target,
+                                                    MEMF_no_refcount);

To limit churn in this patch as much as possible, this scope reduction of "page" may want to also move into the earlier, purely mechanical change.

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 294

Quoted 6 lines
xen/common/ioreq.c +4 -1
         if ( !page )
-            return -ENOMEM;
+        {
+            rc = -ENOMEM;
+            goto fail;
+        }

This and ...

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 305

Quoted 10 lines
xen/common/ioreq.c +2 -1
         if ( !get_page_and_type(page, s->target, PGT_writable_page) )
         {
@@ -290,41 +300,60 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
              * here is a clear indication of something fishy going on.
              */
             domain_crash(s->emulator);
-            return -ENODATA;
+            rc = -ENODATA;
+            goto fail;
         }

... this error patch have different cleanup needs, yet they both simply "goto fail".

Quoted 6 lines
xen/common/ioreq.c +3 -2
-        mfn = page_to_mfn(page);
+        mfns[i] = page_to_mfn(page);
     }
-    iorp->va = vmap(&mfn, 1);
+
+    iorp->va = vmap(mfns, nr_pages);

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 320

Quoted 10 lines
xen/common/ioreq.c +8 -0
     if ( !iorp->va )
+    {
+        rc = -ENOMEM;
         goto fail;
+    }
+
+    xfree(mfns);
+
+    for ( i = 0; i < nr_pages; i++ )
+        clear_page((char *)iorp->va + i * PAGE_SIZE);

As before - please prefer casts to void * when no particular type is needed. Plus you don't need a cast here at all, do you?

I wonder though - might this not better be a single memset()? (Else we may want to introduce clear_pages().)

Quoted 19 lines
xen/common/ioreq.c +9 -4
-    clear_page(iorp->va);
     return 0;
 
  fail:
-    put_page_alloc_ref(page);
-    put_page_and_type(page);
+    while ( i-- )
+    {
+        struct page_info *page = mfn_to_page(mfns[i]);
+
+        put_page_alloc_ref(page);
+        put_page_and_type(page);
+    }
+    xfree(mfns);
 
-    return -ENOMEM;
+    return rc;
 }
 

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 345

Quoted 10 lines
xen/common/ioreq.c +2 -1
 static void ioreq_server_free_mfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
-    struct page_info *page;
+    unsigned int i, nr_pages;
 
     if ( !iorp->va )
         return;
 
+    nr_pages = vmap_size(iorp->va);

Why would you need to query this? You should be able to calculate this the same as ioreq_server_alloc_mfn() does, I suppose.

Quoted 4 lines
xen/common/ioreq.c +1 -2
+    for ( i = 0; i < nr_pages; i++ )
     {
-        page = vmap_to_page(iorp->va);
-        vunmap(iorp->va);

xen/common/ioreq.c static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf) · line 357

Quoted 10 lines
xen/common/ioreq.c +4 -1
-        iorp->va = NULL;
+        struct page_info *page = vmap_to_page(iorp->va + i * PAGE_SIZE);
 
         put_page_alloc_ref(page);
         put_page_and_type(page);
     }
+
+    vunmap(iorp->va);
+    iorp->va = NULL;
 }

This re-ordering isn't nice. At least iorp->va ought to be cleared before the references on the pages are dropped. I fear, though, that even the mapping needs dropping ahead of time: By (likely) freeing the pages, they can be reused for another purpose. Speculative accesses through the mapping would better be excluded as a possibile source of a security issue.

Quoted 14 lines
xen/common/ioreq.c +4 -2
@@ -337,12 +366,28 @@ bool is_ioreq_server_page(struct domain *d, const struct page_info *page)
 
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
-        if ( (s->ioreq.va && vmap_to_page(s->ioreq.va) == page) ||
-             (s->bufioreq.va && vmap_to_page(s->bufioreq.va) == page) )
+        if ( s->bufioreq.va && vmap_to_page(s->bufioreq.va) == page )
         {
             found = true;
             break;
         }
+
+        if ( s->ioreq.va )
+        {

xen/common/ioreq.c bool is_ioreq_server_page(struct domain *d, const struct page_info *page) · line 386

Quoted 10 lines
xen/common/ioreq.c +10 -0
+            unsigned int i;
+
+            for ( i = 0; i < vmap_size(s->ioreq.va); i++ )
+            {
+                if ( vmap_to_page(s->ioreq.va + i * PAGE_SIZE) == page )
+                {
+                    found = true;
+                    break;
+                }
+            }

This raises the question whether storing the array of MFNs permanently wouldn't be better. This would then also eliminate other concerns voiced above.

Quoted 11 lines
xen/common/ioreq.c +1 -2
@@ -818,26 +863,30 @@ int ioreq_server_get_frame(struct domain *d, ioservid_t id,
     if ( rc )
         goto out;
 
-    switch ( idx )
+    if ( idx == XENMEM_resource_ioreq_server_frame_bufioreq )
     {
-    case XENMEM_resource_ioreq_server_frame_bufioreq:
         rc = -ENOENT;
         if ( !HANDLE_BUFIOREQ(s) )
             goto out;

xen/common/ioreq.c int ioreq_server_get_frame(struct domain *d, ioservid_t id, · line 879

Quoted 10 lines
xen/common/ioreq.c +6 -1
 
         *mfn = page_to_mfn(vmap_to_page(s->bufioreq.va));
         rc = 0;
-        break;
+    }
+    else if ( idx >= XENMEM_resource_ioreq_server_frame_ioreq(0) &&
+              idx < XENMEM_resource_ioreq_server_frame_ioreq(nr_ioreq_pages(d)) )
+    {
+        unsigned int page_idx = idx - XENMEM_resource_ioreq_server_frame_ioreq(0);
+        if ( page_idx >= vmap_size(s->ioreq.va) )

This ought to be impossible, so may at best want to be an ASSERT().

Also, nit: Blank line please between declaration(s) and statement(s).

Jan