Skip to contentSkip to conversation

x86/ioreq: Extend ioreq server to support multiple ioreq pages

PATCH Superseded Julian Vetter 2 participants 5 messages Feb 16

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/ioreq: Extend ioreq server to support multiple ioreq pages

Hello Jan and Andrew,
I have adressed your feedback and replaced the for loop with xvzalloc_array() in ioreq_server_alloc_mfn. This indeed simplifies the code in that function. I have also addressed the other points (indentation patch, blank lines, etc.). I just couldn't come up with a better solution for the struct ioreq_server. We now have a new member ioreq_gfn, to track the guest frame number. Any thoughts on this?

Julian Vetter (3):
x86/ioreq: Add missing put_page_alloc_ref(page) x86/ioreq: Prepare spacing for upcoming patch x86/ioreq: Extend ioreq server to support multiple ioreq pages

 xen/arch/x86/hvm/ioreq.c       | 264 ++++++++++++++++++++++++++++-----
 xen/common/ioreq.c             |  96 +++++++++---
 xen/include/public/hvm/ioreq.h |   5 +
 xen/include/xen/ioreq.h        |  13 +-
 4 files changed, 323 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 v2 1/3 x86/ioreq: Add missing put_page_alloc_ref(page)

The page was allocated with MEMF_no_refcount. The MEMF_no_refcount flag means the page is allocated without a regular reference, but it still has the allocation reference. If get_page_and_type() fails, we still need to release the allocation reference. Otherwise the page would leak. domain_crash() doesn't free individual pages; it just marks the domain for destruction. The domain teardown will eventually free domain heap pages, but only those it can find. A page with a dangling alloc ref would prevent the page from being fully freed during domain cleanup.

---
Changes in v2:
- New patch

---
 xen/common/ioreq.c | 1 +
 1 file changed, 1 insertion(+)
xen/common/ioreq.c +1 -0
index f5fd30ce12..5d722c8d4e 100644
@@ -287,6 +287,7 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
          * The domain can't possibly know about this page yet, so failure
          * here is a clear indication of something fishy going on.
          */
+        put_page_alloc_ref(page);
         domain_crash(s->emulator);
         return -ENODATA;
     }
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 v2 1/3] x86/ioreq: Add missing put_page_alloc_ref(page)

Quoted 10 lines · Julian Vetter

The page was allocated with MEMF_no_refcount. The MEMF_no_refcount flag means the page is allocated without a regular reference, but it still has the allocation reference. If get_page_and_type() fails, we still need to release the allocation reference. Otherwise the page would leak. domain_crash() doesn't free individual pages; it just marks the domain for destruction. The domain teardown will eventually free domain heap pages, but only those it can find. A page with a dangling alloc ref would prevent the page from being fully freed during domain cleanup.

Yes, it would be nice if this could be done, but have you read the big comment in put_page_alloc_ref()? We have no choice here, ...

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

Quoted 10 lines
xen/common/ioreq.c +1 -0
@@ -287,6 +287,7 @@ static int ioreq_server_alloc_mfn(struct ioreq_server *s, bool buf)
          * The domain can't possibly know about this page yet, so failure
          * here is a clear indication of something fishy going on.
          */
+        put_page_alloc_ref(page);
         domain_crash(s->emulator);
         return -ENODATA;
     }

... we need to leak the page. (Perhaps the comment could have done with saying so explicitly.) Hence why the domain is being crashed, so the leak cannot easily accumulate. Otherwise simply returning an error here would be quite fine.

Jan

#3 lore raw

PATCH v2 2/3 x86/ioreq: Prepare spacing for upcoming patch

In reply to Julian Vetter

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

---
Changes in v2
- New patch

---
 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
#4 lore raw

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

In reply to 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), individual pages are mapped via prepare_ring_for_helper() and 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 v2
- Use xvalloc_array to allocate the contigeous region
- Removed unncessary includes
- nr_ioreq_pages is now based on d->max_vcpus and not the HVM_MAX_VCPUS define
- Reduced indentation by 1 level in hvm_alloc_ioreq_gfns
- Added blank lines between declarations and statements
- Added comment why we can just return in hvm_add_ioreq_gfn without rollback

---
 xen/arch/x86/hvm/ioreq.c       | 198 ++++++++++++++++++++++++++++++++-
 xen/common/ioreq.c             |  95 ++++++++++++----
 xen/include/public/hvm/ioreq.h |   5 +
 xen/include/xen/ioreq.h        |  13 ++-
 4 files changed, 285 insertions(+), 26 deletions(-)
xen/arch/x86/hvm/ioreq.c +194 -4
index 5ebc48dbd4..a77f00dd96 100644
@@ -6,6 +6,7 @@
  */
 
 #include <xen/domain.h>
+#include <xen/domain_page.h>
 #include <xen/event.h>
 #include <xen/init.h>
 #include <xen/ioreq.h>
@@ -15,6 +16,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>
@@ -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,122 @@ 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;
+    }
+
+    /*
+     * Use prepare_ring_for_helper() to obtain page and type references
+     * for each GFN. Discard its per-page VA immediately, as all pages
+     * will be combined into a single contiguous VA via vmap() below.
+     */
+    for ( i = 0; i < nr_pages; i++ )
+    {
+        struct page_info *pg;
+        void *va;
+
+        rc = prepare_ring_for_helper(d, gfn_x(base_gfn) + i, &pg, &va);
+        if ( rc )
+            goto fail;
+
+        /* Discard per-page VA */
+        unmap_domain_page_global(va);
+        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 +372,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