Skip to contentSkip to conversation

x86/ioreq: Prepare spacing for upcoming patch

PATCH In review Julian Vetter 2 participants 6 messages Feb 23 – Mar 3

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/2 x86/ioreq: Prepare spacing for upcoming patch

This patch just changes indentation, to make the next patch easier to review.

---
Changes in v3:
- No changes to this patch
- Dropped patch "1/3 x86/ioreq: Add missing put_page_alloc_ref(page)"

---
 xen/arch/x86/hvm/ioreq.c | 86 ++++++++++++++++++++++------------------
 1 file changed, 47 insertions(+), 39 deletions(-)
xen/arch/x86/hvm/ioreq.c +47 -39
index a5fa97e149..5ebc48dbd4 100644
@@ -125,14 +125,16 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
 {
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
-        return;
+    {
+        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+            return;
 
-    destroy_ring_for_helper(&iorp->va, iorp->page);
-    iorp->page = NULL;
+        destroy_ring_for_helper(&iorp->va, iorp->page);
+        iorp->page = NULL;
 
-    hvm_free_ioreq_gfn(s, iorp->gfn);
-    iorp->gfn = INVALID_GFN;
+        hvm_free_ioreq_gfn(s, iorp->gfn);
+        iorp->gfn = INVALID_GFN;
+    }
 }
 
 static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
@@ -141,34 +143,36 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
     struct 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 ioreq_server_get_frame() is called), then
-         * mapping a guest frame is not permitted.
-         */
-        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
-            return -EPERM;
-
-        return 0;
-    }
+        if ( iorp->page )
+        {
+            /*
+             * If a page has already been allocated (which will happen on
+             * demand if ioreq_server_get_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;
+        if ( d->is_dying )
+            return -EINVAL;
 
-    iorp->gfn = hvm_alloc_ioreq_gfn(s);
+        iorp->gfn = hvm_alloc_ioreq_gfn(s);
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
-        return -ENOMEM;
+        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+            return -ENOMEM;
 
-    rc = prepare_ring_for_helper(d, gfn_x(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);
+        if ( rc )
+            hvm_unmap_ioreq_gfn(s, buf);
 
-    return rc;
+        return rc;
+    }
 }
 
 static void hvm_remove_ioreq_gfn(struct ioreq_server *s, bool buf)
@@ -176,12 +180,14 @@ static void hvm_remove_ioreq_gfn(struct ioreq_server *s, bool buf)
     struct domain *d = s->target;
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
-        return;
+    {
+        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+            return;
 
-    if ( p2m_remove_page(d, iorp->gfn, page_to_mfn(iorp->page), 0) )
-        domain_crash(d);
-    clear_page(iorp->va);
+        if ( p2m_remove_page(d, iorp->gfn, page_to_mfn(iorp->page), 0) )
+            domain_crash(d);
+        clear_page(iorp->va);
+    }
 }
 
 static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
@@ -190,16 +196,18 @@ static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
     struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
     int rc;
 
-    if ( gfn_eq(iorp->gfn, INVALID_GFN) )
-        return 0;
+    {
+        if ( gfn_eq(iorp->gfn, INVALID_GFN) )
+            return 0;
 
-    clear_page(iorp->va);
+        clear_page(iorp->va);
 
-    rc = p2m_add_page(d, iorp->gfn, page_to_mfn(iorp->page), 0, p2m_ram_rw);
-    if ( rc == 0 )
-        paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn)));
+        rc = p2m_add_page(d, iorp->gfn, page_to_mfn(iorp->page), 0, p2m_ram_rw);
+        if ( rc == 0 )
+            paging_mark_pfn_dirty(d, _pfn(gfn_x(iorp->gfn)));
 
-    return rc;
+        return rc;
+    }
 }
 
 int arch_ioreq_server_map_pages(struct ioreq_server *s)
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 v3 2/2 x86/ioreq: Extend ioreq server to support multiple ioreq pages

A single shared ioreq page provides PAGE_SIZE/sizeof(ioreq_t) = 128 slots, limiting HVM guests to 128 vCPUs. To support more vCPUs, extend the ioreq server to use xvzalloc_array() for allocating a contiguous virtual array of ioreq_t slots sized to d->max_vcpus, backed by potentially non-contiguous physical pages.

For the GFN-mapped path (x86), page and writable type references are obtained directly via check_get_page_from_gfn() and get_page_type() for each GFN. The pages are then combined into a single contiguous VA using vmap(). The number of ioreq pages is computed at runtime via nr_ioreq_pages(d) = DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE), so small VMs only allocate one page. All existing single-page paths (bufioreq, legacy clients) remain unchanged.

Mark the now-unused shared_iopage_t in the public header as deprecated.

---
Changes in v3:
- Replaced prepare_ring_for_helper + unmap_domain_page_global by check_get_page_from_gfn + get_page_type
---

 xen/arch/x86/hvm/ioreq.c       | 208 ++++++++++++++++++++++++++++++++-
 xen/common/ioreq.c             |  95 +++++++++++----
 xen/include/public/hvm/ioreq.h |   5 +
 xen/include/xen/ioreq.h        |  13 ++-
 4 files changed, 295 insertions(+), 26 deletions(-)
xen/arch/x86/hvm/ioreq.c +204 -4
index 5ebc48dbd4..a042866967 100644
@@ -11,10 +11,12 @@
 #include <xen/ioreq.h>
 #include <xen/irq.h>
 #include <xen/lib.h>
+#include <xen/p2m-common.h>
 #include <xen/paging.h>
 #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>
@@ -89,6 +91,39 @@ static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s)
     return hvm_alloc_legacy_ioreq_gfn(s);
 }
 
+static gfn_t hvm_alloc_ioreq_gfns(struct ioreq_server *s,
+                                  unsigned int nr_pages)
+{
+    struct domain *d = s->target;
+    unsigned long mask;
+    unsigned int i, run;
+
+    if ( nr_pages == 1 )
+        return hvm_alloc_ioreq_gfn(s);
+
+    /* Find nr_pages consecutive set bits */
+    mask = d->arch.hvm.ioreq_gfn.mask;
+
+    for ( i = 0, run = 0; i < BITS_PER_LONG; i++ )
+    {
+        if ( !test_bit(i, &mask) )
+            run = 0;
+        else if ( ++run == nr_pages )
+        {
+            /* Found a run - clear all bits and return base GFN */
+            unsigned int start = i - nr_pages + 1;
+            unsigned int j;
+
+            for ( j = start; j <= i; j++ )
+                clear_bit(j, &d->arch.hvm.ioreq_gfn.mask);
+
+            return _gfn(d->arch.hvm.ioreq_gfn.base + start);
+        }
+    }
+
+    return INVALID_GFN;
+}
+
 static bool hvm_free_legacy_ioreq_gfn(struct ioreq_server *s,
                                       gfn_t gfn)
 {
@@ -121,11 +156,23 @@ static void hvm_free_ioreq_gfn(struct ioreq_server *s, gfn_t gfn)
     }
 }
 
+static void hvm_free_ioreq_gfns(struct ioreq_server *s, gfn_t gfn,
+                                unsigned int nr_pages)
+{
+    unsigned int i;
+
+    for ( i = 0; i < nr_pages; i++ )
+        hvm_free_ioreq_gfn(s, gfn_add(gfn, i));
+}
+
 static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
 {
-    struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+    unsigned int i, nr_pages;
 
+    if ( buf )
     {
+        struct ioreq_page *iorp = &s->bufioreq;
+
         if ( gfn_eq(iorp->gfn, INVALID_GFN) )
             return;
 
@@ -134,16 +181,41 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         hvm_free_ioreq_gfn(s, iorp->gfn);
         iorp->gfn = INVALID_GFN;
+        return;
     }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return;
+
+    nr_pages = nr_ioreq_pages(s->target);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +
+                                            i * PAGE_SIZE);
+
+        put_page_and_type(pg);
+        put_page(pg);
+    }
+    vunmap(s->ioreq);
+    s->ioreq = NULL;
+
+    hvm_free_ioreq_gfns(s, s->ioreq_gfn, nr_pages);
+    s->ioreq_gfn = INVALID_GFN;
 }
 
 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;
+    unsigned int i, nr_pages;
+    gfn_t base_gfn;
+    mfn_t *mfns;
     int rc;
 
+    if ( buf )
     {
+        struct ioreq_page *iorp = &s->bufioreq;
+
         if ( iorp->page )
         {
             /*
@@ -173,30 +245,132 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         return rc;
     }
+
+    /* ioreq: multi-page with contiguous VA */
+    if ( s->ioreq )
+    {
+        if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+            return -EPERM;
+        return 0;
+    }
+
+    if ( d->is_dying )
+        return -EINVAL;
+
+    nr_pages = nr_ioreq_pages(d);
+    base_gfn = hvm_alloc_ioreq_gfns(s, nr_pages);
+
+    if ( gfn_eq(base_gfn, INVALID_GFN) )
+        return -ENOMEM;
+
+    mfns = xmalloc_array(mfn_t, nr_pages);
+    if ( !mfns )
+    {
+        hvm_free_ioreq_gfns(s, base_gfn, nr_pages);
+        return -ENOMEM;
+    }
+
+    /*
+     * Obtain a page reference and writable type reference for each GFN.
+     * No per-page VA is needed; all pages are mapped as a single contiguous
+     * VA via vmap() below.
+     */
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg;
+        p2m_type_t p2mt;
+
+        rc = check_get_page_from_gfn(d, _gfn(gfn_x(base_gfn) + i),
+                                     false, &p2mt, &pg);
+        if ( rc )
+        {
+            if ( rc == -EAGAIN )
+                rc = -ENOENT;
+            goto fail;
+        }
+
+        if ( !get_page_type(pg, PGT_writable_page) )
+        {
+            put_page(pg);
+            rc = -EINVAL;
+            goto fail;
+        }
+
+        mfns[i] = page_to_mfn(pg);
+    }
+
+    /* Map all mfns as single contiguous VA */
+    s->ioreq = vmap(mfns, nr_pages);
+    if ( !s->ioreq )
+    {
+        rc = -ENOMEM;
+        goto fail;
+    }
+
+    s->ioreq_gfn = base_gfn;
+    xfree(mfns);
+
+    return 0;
+
+ fail:
+    while ( i-- > 0 )
+    {
+        struct page_info *pg = mfn_to_page(mfns[i]);
+
+        put_page_and_type(pg);
+        put_page(pg);
+    }
+    hvm_free_ioreq_gfns(s, base_gfn, nr_pages);
+    xfree(mfns);
+
+    return rc;
 }
 
 static void hvm_remove_ioreq_gfn(struct ioreq_server *s, bool buf)
 {
     struct domain *d = s->target;
-    struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+    unsigned int i, nr_pages;
 
+    if ( buf )
     {
+        struct ioreq_page *iorp = &s->bufioreq;
+
         if ( gfn_eq(iorp->gfn, INVALID_GFN) )
             return;
 
         if ( p2m_remove_page(d, iorp->gfn, page_to_mfn(iorp->page), 0) )
             domain_crash(d);
         clear_page(iorp->va);
+        return;
+    }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return;
+
+    nr_pages = nr_ioreq_pages(d);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        gfn_t gfn = gfn_add(s->ioreq_gfn, i);
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +
+                                            i * PAGE_SIZE);
+
+        if ( p2m_remove_page(d, gfn, page_to_mfn(pg), 0) )
+            domain_crash(d);
     }
+    memset(s->ioreq, 0, nr_pages * PAGE_SIZE);
 }
 
 static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
 {
     struct domain *d = s->target;
-    struct ioreq_page *iorp = buf ? &s->bufioreq : &s->ioreq;
+    unsigned int i, nr_pages;
     int rc;
 
+    if ( buf )
     {
+        struct ioreq_page *iorp = &s->bufioreq;
+
         if ( gfn_eq(iorp->gfn, INVALID_GFN) )
             return 0;
 
@@ -208,6 +382,32 @@ static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         return rc;
     }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return 0;
+
+    nr_pages = nr_ioreq_pages(d);
+    memset(s->ioreq, 0, nr_pages * PAGE_SIZE);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        gfn_t gfn = gfn_add(s->ioreq_gfn, i);
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +
+                                            i * PAGE_SIZE);
+
+        rc = p2m_add_page(d, gfn, page_to_mfn(pg), 0, p2m_ram_rw);
+        if ( rc )
+            /*
+             * No rollback of previously added pages: The caller
+             * (arch_ioreq_server_disable) has no error handling path,
+             * and partial failure here will be cleaned up when the
+             * ioreq server is eventually destroyed.
+             */
+            return rc;
+
+        paging_mark_pfn_dirty(d, _pfn(gfn_x(gfn)));
+    }
+    return 0;
 }
 
 int arch_ioreq_server_map_pages(struct ioreq_server *s)
xen/common/ioreq.c +74 -21

167 lines not on this page · raw message

xen/include/public/hvm/ioreq.h +5 -0
index 7a6bc760d0..1c1a9e61ae 100644
@@ -49,6 +49,11 @@ struct ioreq {
 };
 typedef struct ioreq ioreq_t;
 
+/*
+ * Deprecated: shared_iopage is no longer used by Xen internally.
+ * The ioreq server now uses a dynamically sized ioreq_t array
+ * to support more than 128 vCPUs.
+ */
 struct shared_iopage {
     struct ioreq vcpu_ioreq[1];
 };
xen/include/xen/ioreq.h +12 -1
index e86f0869fa..a4c7621f3f 100644
@@ -19,9 +19,19 @@
 #ifndef __XEN_IOREQ_H__
 #define __XEN_IOREQ_H__
 
+#include <xen/macros.h>
 #include <xen/sched.h>
 
 #include <public/hvm/dm_op.h>
+#include <public/hvm/ioreq.h>
+
+/* 4096 / 32 = 128 ioreq slots per page */
+#define IOREQS_PER_PAGE  (PAGE_SIZE / sizeof(ioreq_t))
+
+static inline unsigned int nr_ioreq_pages(const struct domain *d)
+{
+    return DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE);
+}
 
 struct ioreq_page {
     gfn_t gfn;
@@ -45,7 +55,8 @@ struct ioreq_server {
     /* Lock to serialize toolstack modifications */
     spinlock_t             lock;
 
-    struct ioreq_page      ioreq;
+    ioreq_t                *ioreq;
+    gfn_t                  ioreq_gfn;
     struct list_head       ioreq_vcpu_list;
     struct ioreq_page      bufioreq;
 
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 v3 2/2] x86/ioreq: Extend ioreq server to support multiple ioreq pages

Quoted 14 lines · Julian Vetter

A single shared ioreq page provides PAGE_SIZE/sizeof(ioreq_t) = 128 slots, limiting HVM guests to 128 vCPUs. To support more vCPUs, extend the ioreq server to use xvzalloc_array() for allocating a contiguous virtual array of ioreq_t slots sized to d->max_vcpus, backed by potentially non-contiguous physical pages.

For the GFN-mapped path (x86), page and writable type references are obtained directly via check_get_page_from_gfn() and get_page_type() for each GFN. The pages are then combined into a single contiguous VA using vmap(). The number of ioreq pages is computed at runtime via nr_ioreq_pages(d) = DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE), so small VMs only allocate one page. All existing single-page paths (bufioreq, legacy clients) remain unchanged.

Quoted 1 line

Mark the now-unused shared_iopage_t in the public header as deprecated.

For this I think we need to settle on one of two options: Either it was a mistake that this was used in the hypervisor (and added to the public interface), in which case the removal of the use may want to be separate (without, imo, any need to mark the item deprecated in the public header, as the property remains). Or we deem it legitimate / useful, in which case you would want to continue using it (in struct ioreq_server).

Quoted 19 lines
xen/arch/x86/hvm/ioreq.c +15 -0
@@ -89,6 +91,39 @@ static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s)
     return hvm_alloc_legacy_ioreq_gfn(s);
 }
 
+static gfn_t hvm_alloc_ioreq_gfns(struct ioreq_server *s,
+                                  unsigned int nr_pages)
+{
+    struct domain *d = s->target;
+    unsigned long mask;
+    unsigned int i, run;
+
+    if ( nr_pages == 1 )
+        return hvm_alloc_ioreq_gfn(s);
+
+    /* Find nr_pages consecutive set bits */
+    mask = d->arch.hvm.ioreq_gfn.mask;
+
+    for ( i = 0, run = 0; i < BITS_PER_LONG; i++ )
+    {

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 118

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+        if ( !test_bit(i, &mask) )
+            run = 0;
+        else if ( ++run == nr_pages )
+        {
+            /* Found a run - clear all bits and return base GFN */
+            unsigned int start = i - nr_pages + 1;
+            unsigned int j;
+
+            for ( j = start; j <= i; j++ )
+                clear_bit(j, &d->arch.hvm.ioreq_gfn.mask);

You using clear_bit() here doesn't make the while operation atomic. There will need to be synchronization (also with hvm_alloc_ioreq_gfn()), and once that's there (or if things are suitably synchronized already) __clear_bit() ought to suffice here.

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 125

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+            return _gfn(d->arch.hvm.ioreq_gfn.base + start);
+        }
+    }
+
+    return INVALID_GFN;
+}

Did you consider whether fragmentation could get in the way here, as is usually the case when doing mixed-size allocations from a single pool? In how far is it necessary for the GFNs used to be consecutive?

Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -134,16 +181,41 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         hvm_free_ioreq_gfn(s, iorp->gfn);
         iorp->gfn = INVALID_GFN;
+        return;

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +9 -0
     }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return;
+
+    nr_pages = nr_ioreq_pages(s->target);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +

Please can you preferably cast to const void * (or void *)? But perhaps you would better latch the pointer into a local variable anyway, for ...

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

Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +7 -0
+                                            i * PAGE_SIZE);
+
+        put_page_and_type(pg);
+        put_page(pg);
 
+    }
+    vunmap(s->ioreq);
+    s->ioreq = NULL;

... this clearing to move up ahead of any teardown done.

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

Quoted 2 lines
xen/arch/x86/hvm/ioreq.c +2 -0
+    hvm_free_ioreq_gfns(s, s->ioreq_gfn, nr_pages);
+    s->ioreq_gfn = INVALID_GFN;

This similarly may want moving up.

As to the loop body, destroy_ring_for_helper() has put_page_and_type(), but no put_page(). Why is this different here?

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +6 -0
@@ -173,30 +245,132 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         return rc;
     }
+
+    /* ioreq: multi-page with contiguous VA */
+    if ( s->ioreq )
+    {
+        if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+            return -EPERM;

The comment on the other path at least wants referencing here.

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

Quoted 13 lines
xen/arch/x86/hvm/ioreq.c +13 -0
+        return 0;
+    }
+
+    if ( d->is_dying )
+        return -EINVAL;
+
+    nr_pages = nr_ioreq_pages(d);
+    base_gfn = hvm_alloc_ioreq_gfns(s, nr_pages);
+
+    if ( gfn_eq(base_gfn, INVALID_GFN) )
+        return -ENOMEM;
+
+    mfns = xmalloc_array(mfn_t, nr_pages);

Especially when there are only a few MFNs, this dynamic allocation would be nice to avoid. If there was a not overly larger upper bound, using a per-CPU array might also be an option.

If the dynamic allocation stays, new code wants to use xvmalloc() and friends.

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

Quoted 11 lines
xen/arch/x86/hvm/ioreq.c +11 -0
+    if ( !mfns )
+    {
+        hvm_free_ioreq_gfns(s, base_gfn, nr_pages);
+        return -ENOMEM;
+    }
+
+    /*
+     * Obtain a page reference and writable type reference for each GFN.
+     * No per-page VA is needed; all pages are mapped as a single contiguous
+     * VA via vmap() below.
+     */

I think this comment wants to mention prepare_ring_for_helper(), so that if updates are done there, there's at least a fair chance that people might spot that changes need doing here as well.

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

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg;
+        p2m_type_t p2mt;
+
+        rc = check_get_page_from_gfn(d, _gfn(gfn_x(base_gfn) + i),

gfn_add() please

Quoted 28 lines
xen/arch/x86/hvm/ioreq.c +28 -0
+                                     false, &p2mt, &pg);
+        if ( rc )
+        {
+            if ( rc == -EAGAIN )
+                rc = -ENOENT;
+            goto fail;
+        }
+
+        if ( !get_page_type(pg, PGT_writable_page) )
+        {
+            put_page(pg);
+            rc = -EINVAL;
+            goto fail;
+        }
+
+        mfns[i] = page_to_mfn(pg);
+    }
+
+    /* Map all mfns as single contiguous VA */
+    s->ioreq = vmap(mfns, nr_pages);
+    if ( !s->ioreq )
+    {
+        rc = -ENOMEM;
+        goto fail;
+    }
+
+    s->ioreq_gfn = base_gfn;
+    xfree(mfns);

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+
+    return 0;
+
+ fail:
+    while ( i-- > 0 )
+    {
+        struct page_info *pg = mfn_to_page(mfns[i]);
+
+        put_page_and_type(pg);
+        put_page(pg);

Same issue here - I can't spot which page reference you're dropping. You obtained only one above.

Quoted 16 lines
xen/arch/x86/hvm/ioreq.c +12 -0
@@ -208,6 +382,32 @@ static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
 
         return rc;
     }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return 0;
+
+    nr_pages = nr_ioreq_pages(d);
+    memset(s->ioreq, 0, nr_pages * PAGE_SIZE);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        gfn_t gfn = gfn_add(s->ioreq_gfn, i);
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +
+                                            i * PAGE_SIZE);

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+
+        rc = p2m_add_page(d, gfn, page_to_mfn(pg), 0, p2m_ram_rw);
+        if ( rc )
+            /*
+             * No rollback of previously added pages: The caller
+             * (arch_ioreq_server_disable) has no error handling path,
+             * and partial failure here will be cleaned up when the
+             * ioreq server is eventually destroyed.
+             */
+            return rc;

Shouldn't you continue the loop, to try to add back as many pages as you can, so the domain encountering problems later is as unlikely as possible (albeit a single missing page is already bad enough)?

Quoted 16 lines
xen/common/ioreq.c +9 -1
@@ -260,9 +259,32 @@ 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 ioreq_page *iorp;
     struct page_info *page;
 
+    if ( !buf )
+    {
+        if ( s->ioreq )
+        {
+            /*
+             * If a guest frame has already been mapped (which may happen
+             * on demand if ioreq_server_get_info() is called), then
+             * allocating a page is not permitted.

xen/common/ioreq.c bool vcpu_ioreq_handle_completion(struct vcpu *v) · line 282

Quoted 10 lines
xen/common/ioreq.c +10 -0
+             */
+            if ( !gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+                return -EPERM;
+
+            return 0;
+        }
+
+        s->ioreq = xvzalloc_array(ioreq_t, s->target->max_vcpus);
+
+        return s->ioreq ? 0 : -ENOMEM;

At this point you haven't fulfilled what the function is supposed to be doing. The pages you allocate also aren't associated with the domain, and you haven't obtained writable references.

Quoted 20 lines
xen/common/ioreq.c +1 -8
@@ -812,26 +861,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;
 
         *mfn = page_to_mfn(s->bufioreq.page);
         rc = 0;
-        break;
-
-    case XENMEM_resource_ioreq_server_frame_ioreq(0):
-        *mfn = page_to_mfn(s->ioreq.page);
-        rc = 0;
-        break;

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 -2
+    }
+    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);
 
-    default:
         rc = -EINVAL;
-        break;
+        if ( idx >= XENMEM_resource_ioreq_server_frame_ioreq(0) &&

You checked this above already, didn't you?

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

Quoted 1 line
xen/common/ioreq.c +1 -0
+             page_idx < nr_ioreq_pages(d) && s->ioreq )

The former part of this check also looks redundant with the earlier one.

Quoted 4 lines
xen/include/xen/ioreq.h +0 -0
@@ -19,9 +19,19 @@
 #ifndef __XEN_IOREQ_H__

xen/include/xen/ioreq.h line 29

Quoted 10 lines
xen/include/xen/ioreq.h +5 -0
 #define __XEN_IOREQ_H__
 
+#include <xen/macros.h>
 #include <xen/sched.h>
 
 #include <public/hvm/dm_op.h>
+#include <public/hvm/ioreq.h>
+
+/* 4096 / 32 = 128 ioreq slots per page */
+#define IOREQS_PER_PAGE  (PAGE_SIZE / sizeof(ioreq_t))

I think the comment wants dropping, as it would end up wrong / useless for page size different from 4k. I'm also not quite sure the #define is overly useful, as ...

xen/include/xen/ioreq.h line 33

Quoted 3 lines
xen/include/xen/ioreq.h +3 -0
+static inline unsigned int nr_ioreq_pages(const struct domain *d)
+{
+    return DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE);

... this is its only use.

Jan

#3 lore raw

Re: [PATCH v3 2/2] x86/ioreq: Extend ioreq server to support multiple ioreq pages

Quoted 36 lines · Jan Beulich
Quoted 19 lines · Julian Vetter
xen/arch/x86/hvm/ioreq.c +15 -0
@@ -89,6 +91,39 @@ static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s)
     return hvm_alloc_legacy_ioreq_gfn(s);
 }
 
+static gfn_t hvm_alloc_ioreq_gfns(struct ioreq_server *s,
+                                  unsigned int nr_pages)
+{
+    struct domain *d = s->target;
+    unsigned long mask;
+    unsigned int i, run;
+
+    if ( nr_pages == 1 )
+        return hvm_alloc_ioreq_gfn(s);
+
+    /* Find nr_pages consecutive set bits */
+    mask = d->arch.hvm.ioreq_gfn.mask;
+
+    for ( i = 0, run = 0; i < BITS_PER_LONG; i++ )
+    {

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 118

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+        if ( !test_bit(i, &mask) )
+            run = 0;
+        else if ( ++run == nr_pages )
+        {
+            /* Found a run - clear all bits and return base GFN */
+            unsigned int start = i - nr_pages + 1;
+            unsigned int j;
+
+            for ( j = start; j <= i; j++ )
+                clear_bit(j, &d->arch.hvm.ioreq_gfn.mask);

You using clear_bit() here doesn't make the while operation atomic. There will need to be synchronization (also with hvm_alloc_ioreq_gfn()), and once that's there (or if things are suitably synchronized already) __clear_bit() ought to suffice here.

Quoted 10 lines

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 125

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+            return _gfn(d->arch.hvm.ioreq_gfn.base + start);
+        }
+    }
+
+    return INVALID_GFN;
+}

Did you consider whether fragmentation could get in the way here, as is usually the case when doing mixed-size allocations from a single pool? In how far is it necessary for the GFNs used to be consecutive?

Thinking about it - isn't this GFN based approach the legacy one? Can't we demand use of the resource mapping approach to support bigger guests?

Jan

#4 lore raw

Re: [PATCH v3 2/2] x86/ioreq: Extend ioreq server to support multiple ioreq pages

In reply to Jan Beulich
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julian Vetter

A single shared ioreq page provides PAGE_SIZE/sizeof(ioreq_t) = 128 slots, limiting HVM guests to 128 vCPUs. To support more vCPUs, extend the ioreq server to use xvzalloc_array() for allocating a contiguous virtual array of ioreq_t slots sized to d->max_vcpus, backed by potentially non-contiguous physical pages.

For the GFN-mapped path (x86), page and writable type references are obtained directly via check_get_page_from_gfn() and get_page_type() for each GFN. The pages are then combined into a single contiguous VA using vmap(). The number of ioreq pages is computed at runtime via nr_ioreq_pages(d) = DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE), so small VMs only allocate one page. All existing single-page paths (bufioreq, legacy clients) remain unchanged.

Quoted 9 lines
Quoted 1 line

Mark the now-unused shared_iopage_t in the public header as deprecated.

For this I think we need to settle on one of two options: Either it was a mistake that this was used in the hypervisor (and added to the public interface), in which case the removal of the use may want to be separate (without, imo, any need to mark the item deprecated in the public header, as the property remains). Or we deem it legitimate / useful, in which case you would want to continue using it (in struct ioreq_server).

Thank you Jan for you feedback! It's very appreciated! You're right. But I'm wondering how would dropping the struct work? I looked into QEMU and varstored, and they both use this struct at the moment. But modifications to both of them would be minimal if we decide to drop the struct. And if they want to support multiple ioreq pages we would need to modify this struct anyway to not contain a single struct ioreq, but a pointer or [].

Quoted 26 lines
Quoted 26 lines
xen/arch/x86/hvm/ioreq.c +22 -0
@@ -89,6 +91,39 @@ static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s)
      return hvm_alloc_legacy_ioreq_gfn(s);
  }
 
+static gfn_t hvm_alloc_ioreq_gfns(struct ioreq_server *s,
+                                  unsigned int nr_pages)
+{
+    struct domain *d = s->target;
+    unsigned long mask;
+    unsigned int i, run;
+
+    if ( nr_pages == 1 )
+        return hvm_alloc_ioreq_gfn(s);
+
+    /* Find nr_pages consecutive set bits */
+    mask = d->arch.hvm.ioreq_gfn.mask;
+
+    for ( i = 0, run = 0; i < BITS_PER_LONG; i++ )
+    {
+        if ( !test_bit(i, &mask) )
+            run = 0;
+        else if ( ++run == nr_pages )
+        {
+            /* Found a run - clear all bits and return base GFN */
+            unsigned int start = i - nr_pages + 1;
+            unsigned int j;
Quoted 8 lines

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 118

Quoted 3 lines
xen/arch/x86/hvm/ioreq.c +3 -0
+
+            for ( j = start; j <= i; j++ )
+                clear_bit(j, &d->arch.hvm.ioreq_gfn.mask);

You using clear_bit() here doesn't make the while operation atomic. There will need to be synchronization (also with hvm_alloc_ioreq_gfn()), and once that's there (or if things are suitably synchronized already) __clear_bit() ought to suffice here.

Yes, sorry. I will drop the "legacy gfn multi page" part entirely. In your other reply you wrote: "Thinking about it - isn't this GFN based approach the legacy one? Can't we demand use of the resource mapping approach to support bigger guests?"
I didn't fully understand this path is the legacy path. So, I adapted it as well. Now, it's clear to me and I agree with your assement. I will remove the part for the legacy path, and will just check that if we take it, in 'hvm_map_ioreq_gfn' I will do:

if ( nr_ioreq_pages(d) > 1 )

     return -EOPNOTSUPP;
Quoted 11 lines

xen/arch/x86/hvm/ioreq.c static gfn_t hvm_alloc_ioreq_gfn(struct ioreq_server *s) · line 125

Quoted 7 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+            return _gfn(d->arch.hvm.ioreq_gfn.base + start);
+        }
+    }
+
+    return INVALID_GFN;
+}

Did you consider whether fragmentation could get in the way here, as is usually the case when doing mixed-size allocations from a single pool? In how far is it necessary for the GFNs used to be consecutive?

Same as above. I will remove this part.

Quoted 31 lines
Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +1 -0
@@ -134,16 +181,41 @@ static void hvm_unmap_ioreq_gfn(struct ioreq_server *s, bool buf)
 
          hvm_free_ioreq_gfn(s, iorp->gfn);
          iorp->gfn = INVALID_GFN;
+        return;

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +9 -0
      }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return;
+
+    nr_pages = nr_ioreq_pages(s->target);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +

Please can you preferably cast to const void * (or void *)? But perhaps you would better latch the pointer into a local variable anyway, for ...

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

Quoted 8 lines
xen/arch/x86/hvm/ioreq.c +7 -0
+                                            i * PAGE_SIZE);
+
+        put_page_and_type(pg);
+        put_page(pg);
 
+    }
+    vunmap(s->ioreq);
+    s->ioreq = NULL;

... this clearing to move up ahead of any teardown done.

Quoted 7 lines

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

Quoted 2 lines
xen/arch/x86/hvm/ioreq.c +2 -0
+    hvm_free_ioreq_gfns(s, s->ioreq_gfn, nr_pages);
+    s->ioreq_gfn = INVALID_GFN;

This similarly may want moving up.

As to the loop body, destroy_ring_for_helper() has put_page_and_type(), but no put_page(). Why is this different here?

Yes, sorry. We don't need it here. I dropped it. I now do the same as 'destroy_ring_for_helper'.

Quoted 32 lines

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

Quoted 11 lines
xen/arch/x86/hvm/ioreq.c +6 -0
@@ -173,30 +245,132 @@ static int hvm_map_ioreq_gfn(struct ioreq_server *s, bool buf)
 
          return rc;
      }
+
+    /* ioreq: multi-page with contiguous VA */
+    if ( s->ioreq )
+    {
+        if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+            return -EPERM;

The comment on the other path at least wants referencing here.

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

Quoted 13 lines
xen/arch/x86/hvm/ioreq.c +13 -0
+        return 0;
+    }
+
+    if ( d->is_dying )
+        return -EINVAL;
+
+    nr_pages = nr_ioreq_pages(d);
+    base_gfn = hvm_alloc_ioreq_gfns(s, nr_pages);
+
+    if ( gfn_eq(base_gfn, INVALID_GFN) )
+        return -ENOMEM;
+
+    mfns = xmalloc_array(mfn_t, nr_pages);

Especially when there are only a few MFNs, this dynamic allocation would be nice to avoid. If there was a not overly larger upper bound, using a per-CPU array might also be an option.

Quoted 2 lines

If the dynamic allocation stays, new code wants to use xvmalloc() and friends.

This all goes away for the legacy path we only have a single page so no xmalloc_array anymore.

Quoted 59 lines

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

Quoted 12 lines
xen/arch/x86/hvm/ioreq.c +11 -0
+    if ( !mfns )
+    {
+        hvm_free_ioreq_gfns(s, base_gfn, nr_pages);
+        return -ENOMEM;
+    }
+
+    /*
+     * Obtain a page reference and writable type reference for each GFN.
+     * No per-page VA is needed; all pages are mapped as a single contiguous
+     * VA via vmap() below.
+     */

I think this comment wants to mention prepare_ring_for_helper(), so that if updates are done there, there's at least a fair chance that people might spot that changes need doing here as well.

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

Quoted 6 lines
xen/arch/x86/hvm/ioreq.c +6 -0
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg;
+        p2m_type_t p2mt;
+
+        rc = check_get_page_from_gfn(d, _gfn(gfn_x(base_gfn) + i),

gfn_add() please

Quoted 33 lines
xen/arch/x86/hvm/ioreq.c +33 -0
+                                     false, &p2mt, &pg);
+        if ( rc )
+        {
+            if ( rc == -EAGAIN )
+                rc = -ENOENT;
+            goto fail;
+        }
+
+        if ( !get_page_type(pg, PGT_writable_page) )
+        {
+            put_page(pg);
+            rc = -EINVAL;
+            goto fail;
+        }
+
+        mfns[i] = page_to_mfn(pg);
+    }
+
+    /* Map all mfns as single contiguous VA */
+    s->ioreq = vmap(mfns, nr_pages);
+    if ( !s->ioreq )
+    {
+        rc = -ENOMEM;
+        goto fail;
+    }
+
+    s->ioreq_gfn = base_gfn;
+    xfree(mfns);
+
+    return 0;
+
+ fail:
+    while ( i-- > 0 )
Quoted 8 lines

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

Quoted 5 lines
xen/arch/x86/hvm/ioreq.c +5 -0
+    {
+        struct page_info *pg = mfn_to_page(mfns[i]);
+
+        put_page_and_type(pg);
+        put_page(pg);

Same issue here - I can't spot which page reference you're dropping. You obtained only one above.

yes, I forgot to call the alloc_domheap_page.

Quoted 54 lines
Quoted 17 lines
xen/arch/x86/hvm/ioreq.c +12 -0
@@ -208,6 +382,32 @@ static int hvm_add_ioreq_gfn(struct ioreq_server *s, bool buf)
 
          return rc;
      }
+
+    if ( gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+        return 0;
+
+    nr_pages = nr_ioreq_pages(d);
+    memset(s->ioreq, 0, nr_pages * PAGE_SIZE);
+
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        gfn_t gfn = gfn_add(s->ioreq_gfn, i);
+        struct page_info *pg = vmap_to_page((char *)s->ioreq +
+                                            i * PAGE_SIZE);

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

Quoted 10 lines
xen/arch/x86/hvm/ioreq.c +10 -0
+
+        rc = p2m_add_page(d, gfn, page_to_mfn(pg), 0, p2m_ram_rw);
+        if ( rc )
+            /*
+             * No rollback of previously added pages: The caller
+             * (arch_ioreq_server_disable) has no error handling path,
+             * and partial failure here will be cleaned up when the
+             * ioreq server is eventually destroyed.
+             */
+            return rc;

Shouldn't you continue the loop, to try to add back as many pages as you can, so the domain encountering problems later is as unlikely as possible (albeit a single missing page is already bad enough)?

Quoted 22 lines
xen/common/ioreq.c +15 -1
@@ -260,9 +259,32 @@ 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 ioreq_page *iorp;
      struct page_info *page;
 
+    if ( !buf )
+    {
+        if ( s->ioreq )
+        {
+            /*
+             * If a guest frame has already been mapped (which may happen
+             * on demand if ioreq_server_get_info() is called), then
+             * allocating a page is not permitted.
+             */
+            if ( !gfn_eq(s->ioreq_gfn, INVALID_GFN) )
+                return -EPERM;
+
+            return 0;
+        }
Quoted 8 lines

xen/common/ioreq.c bool vcpu_ioreq_handle_completion(struct vcpu *v) · line 282

Quoted 4 lines
xen/common/ioreq.c +4 -0
+
+        s->ioreq = xvzalloc_array(ioreq_t, s->target->max_vcpus);
+
+        return s->ioreq ? 0 : -ENOMEM;

At this point you haven't fulfilled what the function is supposed to be doing. The pages you allocate also aren't associated with the domain, and you haven't obtained writable references.

yes, sorry, same as above I forgot the call to alloc_domheap_page.

Quoted 57 lines
Quoted 21 lines
xen/common/ioreq.c +1 -8
@@ -812,26 +861,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;
 
          *mfn = page_to_mfn(s->bufioreq.page);
          rc = 0;
-        break;
-
-    case XENMEM_resource_ioreq_server_frame_ioreq(0):
-        *mfn = page_to_mfn(s->ioreq.page);
-        rc = 0;
-        break;

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 -2
+    }
+    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);
 
-    default:
          rc = -EINVAL;
-        break;
+        if ( idx >= XENMEM_resource_ioreq_server_frame_ioreq(0) &&

You checked this above already, didn't you?

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

Quoted 1 line
xen/common/ioreq.c +1 -0
+             page_idx < nr_ioreq_pages(d) && s->ioreq )

The former part of this check also looks redundant with the earlier one.

Quoted 4 lines
xen/include/xen/ioreq.h +0 -0
@@ -19,9 +19,19 @@
  #ifndef __XEN_IOREQ_H__

xen/include/xen/ioreq.h line 29

Quoted 10 lines
xen/include/xen/ioreq.h +5 -0
  #define __XEN_IOREQ_H__
 
+#include <xen/macros.h>
  #include <xen/sched.h>
 
  #include <public/hvm/dm_op.h>
+#include <public/hvm/ioreq.h>
+
+/* 4096 / 32 = 128 ioreq slots per page */
+#define IOREQS_PER_PAGE  (PAGE_SIZE / sizeof(ioreq_t))

I think the comment wants dropping, as it would end up wrong / useless for page size different from 4k. I'm also not quite sure the #define is overly useful, as ...

Quoted 7 lines

xen/include/xen/ioreq.h line 33

Quoted 3 lines
xen/include/xen/ioreq.h +3 -0
+static inline unsigned int nr_ioreq_pages(const struct domain *d)
+{
+    return DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE);

... this is its only use.

Jan

--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#5 lore raw

Re: [PATCH v3 2/2] x86/ioreq: Extend ioreq server to support multiple ioreq pages

Quoted 25 lines · Julian Vetter
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Julian Vetter

A single shared ioreq page provides PAGE_SIZE/sizeof(ioreq_t) = 128 slots, limiting HVM guests to 128 vCPUs. To support more vCPUs, extend the ioreq server to use xvzalloc_array() for allocating a contiguous virtual array of ioreq_t slots sized to d->max_vcpus, backed by potentially non-contiguous physical pages.

For the GFN-mapped path (x86), page and writable type references are obtained directly via check_get_page_from_gfn() and get_page_type() for each GFN. The pages are then combined into a single contiguous VA using vmap(). The number of ioreq pages is computed at runtime via nr_ioreq_pages(d) = DIV_ROUND_UP(d->max_vcpus, IOREQS_PER_PAGE), so small VMs only allocate one page. All existing single-page paths (bufioreq, legacy clients) remain unchanged.

Quoted 8 lines
Quoted 1 line

Mark the now-unused shared_iopage_t in the public header as deprecated.

For this I think we need to settle on one of two options: Either it was a mistake that this was used in the hypervisor (and added to the public interface), in which case the removal of the use may want to be separate (without, imo, any need to mark the item deprecated in the public header, as the property remains). Or we deem it legitimate / useful, in which case you would want to continue using it (in struct ioreq_server).

Quoted 7 lines

Thank you Jan for you feedback! It's very appreciated! You're right. But I'm wondering how would dropping the struct work? I looked into QEMU and varstored, and they both use this struct at the moment. But modifications to both of them would be minimal if we decide to drop the struct. And if they want to support multiple ioreq pages we would need to modify this struct anyway to not contain a single struct ioreq, but a pointer or [].

There may be a misunderstanding here: I said "drop the use" (in the hypervisor, that is), not "drop the struct". There's also no strong need for a pointer or [], as it looks: It being [1] right now is, aiui, a poor man's flexible array. For one, for general use the public headers need to be C89 compatible, i.e. no flexibly arrays unconditionally. While we do have some conditional uses (see XEN_FLEX_ARRAY_DIM), that may not be usable here, as the array is the sole field of the struct.

Jan