Skip to contentSkip to conversation

Refactor ioreq server for better performance.

PATCH Superseded Yu Zhang 2 participants 11 messages Aug 13, 2015 – Aug 14, 2015

v4 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

v4 cover Refactor ioreq server for better performance.

XenGT leverages ioreq server to track and forward the accesses to GPU I/O resources, e.g. the PPGTT(per-process graphic translation tables). Currently, ioreq server uses rangeset to track the BDF/ PIO/MMIO ranges to be emulated. To select an ioreq server, the rangeset is searched to see if the I/O range is recorded. However, traversing the link list inside rangeset could be time consuming when number of ranges is too high. On HSW platform, number of PPGTTs for each vGPU could be several hundred. On BDW, this value could be several thousand. This patch series refactored rangeset to base it on red-back tree, so that the searching would be more efficient.

Besides, this patchset also splits the tracking of MMIO and guest ram ranges into different rangesets. And to accommodate more ranges, limitation of the number of ranges in an ioreq server, MAX_NR_IO_RANGES is changed - future patches might be provided to tune this with other approaches.

Changes in v4:
Keep the name HVMOP_IO_RANGE_MEMORY for MMIO resources, and add a new one, HVMOP_IO_RANGE_WP_MEM, for write-protected memory.

Changes in v3:
1> Use a seperate rangeset for guest ram pages in ioreq server; 2> Refactor rangeset, instead of introduce a new data structure.

Changes in v2:
1> Split the original patch into 2;
2> Take Paul Durrant's comments:
a> Add a name member in the struct rb_rangeset, and use the 'q'
debug key to dump the ranges in ioreq server;
b> Keep original routine names for hvm ioreq server; c> Commit message changes - mention that a future patch to change
the maximum ranges inside ioreq server.

Yu Zhang (2):
Differentiate IO/mem resources tracked by ioreq server Refactor rangeset structure for better performance.

 tools/libxc/include/xenctrl.h    | 31 +++++++++++++++
 tools/libxc/xc_domain.c          | 55 +++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++-
 xen/common/rangeset.c            | 82 +++++++++++++++++++++++++++++-----------
 xen/include/asm-x86/hvm/domain.h |  4 +-
 xen/include/public/hvm/hvm_op.h  |  1 +
 xen/include/public/hvm/ioreq.h   |  1 +
 7 files changed, 175 insertions(+), 25 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v4 1/2 Differentiate IO/mem resources tracked by ioreq server

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
 tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
 tools/libxc/xc_domain.c          | 55 ++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
 xen/include/asm-x86/hvm/domain.h |  4 +--
 xen/include/public/hvm/hvm_op.h  |  1 +
 xen/include/public/hvm/ioreq.h   |  1 +
 6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +31 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                             int is_mmio,
                                             uint64_t start,
                                             uint64_t end);
+/**
+ * This function registers a range of write-protected memory for emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
 
 /**
  * This function registers a PCI device for config space emulation.
tools/libxc/xc_domain.c +55 -0

61 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +25 -1
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int hvm_ioreq_server_alloc_rangesets(struct hvm_ioreq_server *s,
 
         rc = asprintf(&name, "ioreq_server %d %s", s->id,
                       (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                       (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                       "");
         if ( rc )
             goto fail;
@@ -1258,6 +1259,7 @@ static int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
             case HVMOP_IO_RANGE_PORT:
             case HVMOP_IO_RANGE_MEMORY:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                 r = s->range[type];
                 break;
 
@@ -1309,6 +1311,7 @@ static int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
             case HVMOP_IO_RANGE_PORT:
             case HVMOP_IO_RANGE_MEMORY:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                 r = s->range[type];
                 break;
 
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
     uint32_t cf8;
     uint8_t type;
     uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
     if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
         return NULL;
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
     {
         type = p->type;
         addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
     }
 
     list_for_each_entry ( s,
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
         BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
         BUILD_BUG_ON(IOREQ_TYPE_COPY != HVMOP_IO_RANGE_MEMORY);
         BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG != HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM != HVMOP_IO_RANGE_WP_MEM);
         r = s->range[type];
 
         switch ( type )
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             }
 
             break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
         }
     }
 
xen/include/asm-x86/hvm/domain.h +2 -2
index 992d5d1..b2e4234 100644
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
     bool_t           pending;
 };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
 struct hvm_ioreq_server {
     struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +1 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
 # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory range */
     uint64_aligned_t start, end; /* IN - inclusive start and end of range */
 };
 typedef struct xen_hvm_io_range xen_hvm_io_range_t;
xen/include/public/hvm/ioreq.h +1 -0
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
 #define IOREQ_TYPE_PIO          0 /* pio */
 #define IOREQ_TYPE_COPY         1 /* mmio ops */
 #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
 #define IOREQ_TYPE_TIMEOFFSET   7
 #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
Signature · 1 line
1.9.1
author
#7 lore raw

PATCH v4 2/2 Refactor rangeset structure for better performance.

In reply to Yu Zhang

This patch refactors struct rangeset to base it on the red-black tree structure, instead of on the current doubly linked list. By now, ioreq leverages rangeset to keep track of the IO/memory resources to be emulated. Yet when number of ranges inside one ioreq server is very high, traversing a doubly linked list could be time consuming. With this patch, the time complexity for searching a rangeset can be improved from O(n) to O(log(n)). Interfaces of rangeset still remain the same, and no new APIs introduced.

---
 xen/common/rangeset.c | 82 +++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 60 insertions(+), 22 deletions(-)
xen/common/rangeset.c +60 -22
index 6c6293c..87b6aab 100644
@@ -10,11 +10,12 @@
 #include <xen/sched.h>
 #include <xen/errno.h>
 #include <xen/rangeset.h>
+#include <xen/rbtree.h>
 #include <xsm/xsm.h>
 
 /* An inclusive range [s,e] and pointer to next range in ascending order. */
 struct range {
-    struct list_head list;
+    struct rb_node node;
     unsigned long s, e;
 };
 
@@ -24,7 +25,7 @@ struct rangeset {
     struct domain   *domain;
 
     /* Ordered list of ranges contained in this set, and protecting lock. */
-    struct list_head range_list;
+    struct rb_root   range_tree;
 
     /* Number of ranges that can be allocated */
     long             nr_ranges;
@@ -45,41 +46,78 @@ struct rangeset {
 static struct range *find_range(
     struct rangeset *r, unsigned long s)
 {
-    struct range *x = NULL, *y;
+    struct rb_node *node;
+    struct range   *x;
+    struct range   *prev = NULL;
 
-    list_for_each_entry ( y, &r->range_list, list )
+    node = r->range_tree.rb_node;
+    while ( node )
     {
-        if ( y->s > s )
-            break;
-        x = y;
+        x = container_of(node, struct range, node);
+        if ( (s >= x->s) && (s <= x->e) )
+            return x;
+        if ( s < x->s )
+            node = node->rb_left;
+        else if ( s > x->s )
+        {
+            prev = x;
+            node = node->rb_right;
+        }
     }
 
-    return x;
+    return prev;
 }
 
 /* Return the lowest range in the set r, or NULL if r is empty. */
 static struct range *first_range(
     struct rangeset *r)
 {
-    if ( list_empty(&r->range_list) )
-        return NULL;
-    return list_entry(r->range_list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_first(&r->range_tree);
+    if ( node != NULL )
+        return container_of(node, struct range, node);
+
+    return NULL;
 }
 
 /* Return range following x in ascending order, or NULL if x is the highest. */
 static struct range *next_range(
     struct rangeset *r, struct range *x)
 {
-    if ( x->list.next == &r->range_list )
-        return NULL;
-    return list_entry(x->list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_next(&x->node);
+    if ( node )
+        return container_of(node, struct range, node);
+
+    return NULL;
 }
 
 /* Insert range y after range x in r. Insert as first range if x is NULL. */
 static void insert_range(
     struct rangeset *r, struct range *x, struct range *y)
 {
-    list_add(&y->list, (x != NULL) ? &x->list : &r->range_list);
+    struct rb_node **node;
+    struct rb_node *parent = NULL;
+
+    if ( x == NULL )
+        node = &r->range_tree.rb_node;
+    else
+    {
+        node = &x->node.rb_right;
+        parent = &x->node;
+    }
+
+    while ( *node )
+    {
+        parent = *node;
+        node = &parent->rb_left;
+    }
+
+    /* Add new node and rebalance the red-black tree. */
+    rb_link_node(&y->node, parent, node);
+    rb_insert_color(&y->node, &r->range_tree);
 }
 
 /* Remove a range from its list and free it. */
@@ -88,7 +126,7 @@ static void destroy_range(
 {
     r->nr_ranges++;
 
-    list_del(&x->list);
+    rb_erase(&x->node, &r->range_tree);
     xfree(x);
 }
 
@@ -319,7 +357,7 @@ bool_t rangeset_contains_singleton(
 bool_t rangeset_is_empty(
     const struct rangeset *r)
 {
-    return ((r == NULL) || list_empty(&r->range_list));
+    return ((r == NULL) || RB_EMPTY_ROOT(&r->range_tree));
 }
 
 struct rangeset *rangeset_new(
@@ -332,7 +370,7 @@ struct rangeset *rangeset_new(
         return NULL;
 
     rwlock_init(&r->lock);
-    INIT_LIST_HEAD(&r->range_list);
+    r->range_tree = RB_ROOT;
     r->nr_ranges = -1;
 
     BUG_ON(flags & ~RANGESETF_prettyprint_hex);
@@ -410,7 +448,7 @@ void rangeset_domain_destroy(
 
 void rangeset_swap(struct rangeset *a, struct rangeset *b)
 {
-    LIST_HEAD(tmp);
+    struct rb_node* tmp;
 
     if ( a < b )
     {
@@ -423,9 +461,9 @@ void rangeset_swap(struct rangeset *a, struct rangeset *b)
         write_lock(&a->lock);
     }
 
-    list_splice_init(&a->range_list, &tmp);
-    list_splice_init(&b->range_list, &a->range_list);
-    list_splice(&tmp, &b->range_list);
+    tmp = a->range_tree.rb_node;
+    a->range_tree.rb_node = b->range_tree.rb_node;
+    b->range_tree.rb_node = tmp;
 
     write_unlock(&a->lock);
     write_unlock(&b->lock);
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

In reply to Yu Zhang
Quoted 9 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Quoted 4 lines

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

Would the interfaces be better using xen_pfn_t rather than using uint64_t to pass addresses round where the bottom 12 bits will always be zero?

Paul

Quoted 271 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
 tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
 tools/libxc/xc_domain.c          | 55
++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
 xen/include/asm-x86/hvm/domain.h |  4 +--
 xen/include/public/hvm/hvm_op.h  |  1 +
 xen/include/public/hvm/ioreq.h   |  1 +
 6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +18 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                             int is_mmio,
                                             uint64_t start,
                                             uint64_t end);
+/**
+ * This function registers a range of write-protected memory for emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +5 -0
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
 
 /**
  * This function registers a PCI device for config space emulation.
tools/libxc/xc_domain.c +0 -0
index 2ee26fb..42aeba9 100644
@@ -1552,6 +1552,61 @@ int

xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,

     return rc;

}

+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch, domid_t domid,

tools/libxc/xc_domain.c +27 -0
+                                        ioservid_t id, uint64_t start, uint64_t end)
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+}
+
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,

domid_t domid,

tools/libxc/xc_domain.c +27 -0
+                                            ioservid_t id, uint64_t start, uint64_t end)
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+
+}
+
 int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t

domid,
ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device,

xen/arch/x86/hvm/hvm.c +2 -1
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int hvm_ioreq_server_alloc_rangesets(struct
 hvm_ioreq_server *s,
 
         rc = asprintf(&name, "ioreq_server %d %s", s->id,
                       (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                       (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                       "");
         if ( rc )
             goto fail;
xen/arch/x86/hvm/hvm.c +4 -0
@@ -1258,6 +1259,7 @@ static int
 hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
             case HVMOP_IO_RANGE_PORT:
             case HVMOP_IO_RANGE_MEMORY:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                 r = s->range[type];
                 break;
@@ -1309,6 +1311,7 @@ static int
 hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
             case HVMOP_IO_RANGE_PORT:
             case HVMOP_IO_RANGE_MEMORY:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                 r = s->range[type];
                 break;
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
     uint32_t cf8;
     uint8_t type;
     uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
     if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
         return NULL;
xen/arch/x86/hvm/hvm.c +11 -0
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
     {
         type = p->type;
         addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
     }
 

list_for_each_entry ( s,

xen/arch/x86/hvm/hvm.c +1 -0
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
         BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
         BUILD_BUG_ON(IOREQ_TYPE_COPY != HVMOP_IO_RANGE_MEMORY);
         BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG !=
 HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM !=
 HVMOP_IO_RANGE_WP_MEM);

r = s->range[type];

switch ( type )

xen/arch/x86/hvm/hvm.c +7 -0
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
             }
 
             break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
         }
     }
xen/include/asm- renamed from xen/include/asm-x86/hvm/domain.h +0 -0

x86/hvm/domain.h
index 992d5d1..b2e4234 100644

xen/include/asm-x86/hvm/domain.h +2 -2
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
     bool_t           pending;
 };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
 struct hvm_ioreq_server {
     struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +1 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
 # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory

range */
uint64_aligned_t start, end; /* IN - inclusive start and end of range */
};
typedef struct xen_hvm_io_range xen_hvm_io_range_t;

xen/include/public/hvm/ioreq.h +1 -1
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
 #define IOREQ_TYPE_PIO          0 /* pio */
 #define IOREQ_TYPE_COPY         1 /* mmio ops */
 #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
 #define IOREQ_TYPE_TIMEOFFSET   7
 #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
--

1.9.1

#8 lore raw

Re: [PATCH v4 2/2] Refactor rangeset structure for better performance.

In reply to Yu Zhang
Quoted 63 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 2/2] Refactor rangeset structure for better performance.

This patch refactors struct rangeset to base it on the red-black tree structure, instead of on the current doubly linked list. By now, ioreq leverages rangeset to keep track of the IO/memory resources to be emulated. Yet when number of ranges inside one ioreq server is very high, traversing a doubly linked list could be time consuming. With this patch, the time complexity for searching a rangeset can be improved from O(n) to O(log(n)). Interfaces of rangeset still remain the same, and no new APIs introduced.

---
 xen/common/rangeset.c | 82
+++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 60 insertions(+), 22 deletions(-)
xen/common/rangeset.c +8 -4
index 6c6293c..87b6aab 100644
@@ -10,11 +10,12 @@
 #include <xen/sched.h>
 #include <xen/errno.h>
 #include <xen/rangeset.h>
+#include <xen/rbtree.h>
 #include <xsm/xsm.h>
 
 /* An inclusive range [s,e] and pointer to next range in ascending order. */
 struct range {
-    struct list_head list;
+    struct rb_node node;
     unsigned long s, e;
 };
 
@@ -24,7 +25,7 @@ struct rangeset {
     struct domain   *domain;
 
     /* Ordered list of ranges contained in this set, and protecting lock. */
-    struct list_head range_list;
+    struct rb_root   range_tree;
 
     /* Number of ranges that can be allocated */
     long             nr_ranges;
@@ -45,41 +46,78 @@ struct rangeset {
 static struct range *find_range(
     struct rangeset *r, unsigned long s)
 {
-    struct range *x = NULL, *y;
+    struct rb_node *node;
+    struct range   *x;
+    struct range   *prev = NULL;
 
-    list_for_each_entry ( y, &r->range_list, list )
+    node = r->range_tree.rb_node;
+    while ( node )

xen/common/rangeset.c struct rangeset { · line 61

Quoted 10 lines
xen/common/rangeset.c +6 -3
     {
-        if ( y->s > s )
-            break;
-        x = y;
+        x = container_of(node, struct range, node);
+        if ( (s >= x->s) && (s <= x->e) )
+            return x;
+        if ( s < x->s )
+            node = node->rb_left;
+        else if ( s > x->s )

Do you need else if there? I think else would do. s either has to be < x->s or > x->e to get to this point.

xen/common/rangeset.c struct rangeset { · line 68

Quoted 8 lines
xen/common/rangeset.c +5 -1
+        {
+            prev = x;
+            node = node->rb_right;
+        }
     }
 
-    return x;
+    return prev;

Do you not want to return NULL here? It looks like you'd only get here if there was no matching range.

Paul

Quoted 114 lines
xen/common/rangeset.c +41 -14
 }
 
 /* Return the lowest range in the set r, or NULL if r is empty. */
 static struct range *first_range(
     struct rangeset *r)
 {
-    if ( list_empty(&r->range_list) )
-        return NULL;
-    return list_entry(r->range_list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_first(&r->range_tree);
+    if ( node != NULL )
+        return container_of(node, struct range, node);
+
+    return NULL;
 }
 
 /* Return range following x in ascending order, or NULL if x is the highest. */
 static struct range *next_range(
     struct rangeset *r, struct range *x)
 {
-    if ( x->list.next == &r->range_list )
-        return NULL;
-    return list_entry(x->list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_next(&x->node);
+    if ( node )
+        return container_of(node, struct range, node);
+
+    return NULL;
 }
 
 /* Insert range y after range x in r. Insert as first range if x is NULL. */
 static void insert_range(
     struct rangeset *r, struct range *x, struct range *y)
 {
-    list_add(&y->list, (x != NULL) ? &x->list : &r->range_list);
+    struct rb_node **node;
+    struct rb_node *parent = NULL;
+
+    if ( x == NULL )
+        node = &r->range_tree.rb_node;
+    else
+    {
+        node = &x->node.rb_right;
+        parent = &x->node;
+    }
+
+    while ( *node )
+    {
+        parent = *node;
+        node = &parent->rb_left;
+    }
+
+    /* Add new node and rebalance the red-black tree. */
+    rb_link_node(&y->node, parent, node);
+    rb_insert_color(&y->node, &r->range_tree);
 }
 
 /* Remove a range from its list and free it. */
@@ -88,7 +126,7 @@ static void destroy_range(
 {
     r->nr_ranges++;
 
-    list_del(&x->list);
+    rb_erase(&x->node, &r->range_tree);
     xfree(x);
 }
 
@@ -319,7 +357,7 @@ bool_t rangeset_contains_singleton(
 bool_t rangeset_is_empty(
     const struct rangeset *r)
 {
-    return ((r == NULL) || list_empty(&r->range_list));
+    return ((r == NULL) || RB_EMPTY_ROOT(&r->range_tree));
 }
 
 struct rangeset *rangeset_new(
@@ -332,7 +370,7 @@ struct rangeset *rangeset_new(
         return NULL;
 
     rwlock_init(&r->lock);
-    INIT_LIST_HEAD(&r->range_list);
+    r->range_tree = RB_ROOT;
     r->nr_ranges = -1;
 
     BUG_ON(flags & ~RANGESETF_prettyprint_hex);
@@ -410,7 +448,7 @@ void rangeset_domain_destroy(
 
 void rangeset_swap(struct rangeset *a, struct rangeset *b)
 {
-    LIST_HEAD(tmp);
+    struct rb_node* tmp;
 
     if ( a < b )
     {
@@ -423,9 +461,9 @@ void rangeset_swap(struct rangeset *a, struct
 rangeset *b)
         write_lock(&a->lock);
     }
 
-    list_splice_init(&a->range_list, &tmp);
-    list_splice_init(&b->range_list, &a->range_list);
-    list_splice(&tmp, &b->range_list);
+    tmp = a->range_tree.rb_node;
+    a->range_tree.rb_node = b->range_tree.rb_node;
+    b->range_tree.rb_node = tmp;
 
     write_unlock(&a->lock);
     write_unlock(&b->lock);

--
1.9.1

author
#3 lore raw

Re: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

In reply to Paul Durrant
Quoted 9 lines · Paul Durrant
Quoted 9 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Quoted 8 lines
Quoted 4 lines

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

Would the interfaces be better using xen_pfn_t rather than using uint64_t to pass addresses round where the bottom 12 bits will always be zero?

Paul

Thank you, Paul.
Well, I'm not quite sure if the bottom 12 bits of the address are zero. I had thought these addresses are just guest physical ones causing the ept violation, and not aligned down to its page address, like the MMIO. So, if our handler code has already done that, using xen_pfn_t could be better(my developing machine encountered some hardware problem, will check the code tomorrow) :)

Yu

Quoted 280 lines
Quoted 271 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
  tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
  tools/libxc/xc_domain.c          | 55
++++++++++++++++++++++++++++++++++++++++
  xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
  xen/include/asm-x86/hvm/domain.h |  4 +--
  xen/include/public/hvm/hvm_op.h  |  1 +
  xen/include/public/hvm/ioreq.h   |  1 +
  6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +18 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                              int is_mmio,
                                              uint64_t start,
                                              uint64_t end);
+/**
+ * This function registers a range of write-protected memory for emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +5 -0
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
 
  /**
   * This function registers a PCI device for config space emulation.
tools/libxc/xc_domain.c +0 -0
index 2ee26fb..42aeba9 100644
@@ -1552,6 +1552,61 @@ int

xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,

      return rc;

}

+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch, domid_t domid,

tools/libxc/xc_domain.c +27 -0
+                                        ioservid_t id, uint64_t start, uint64_t end)
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+}
+
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,

domid_t domid,

tools/libxc/xc_domain.c +27 -0
+                                            ioservid_t id, uint64_t start, uint64_t end)
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+
+}
+
  int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t

domid,
ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device,

xen/arch/x86/hvm/hvm.c +2 -1
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int hvm_ioreq_server_alloc_rangesets(struct
 hvm_ioreq_server *s,
 
          rc = asprintf(&name, "ioreq_server %d %s", s->id,
                        (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                        (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                        "");
          if ( rc )
              goto fail;
xen/arch/x86/hvm/hvm.c +4 -0
@@ -1258,6 +1259,7 @@ static int
 hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
              case HVMOP_IO_RANGE_PORT:
              case HVMOP_IO_RANGE_MEMORY:
              case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
@@ -1309,6 +1311,7 @@ static int
 hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
              case HVMOP_IO_RANGE_PORT:
              case HVMOP_IO_RANGE_MEMORY:
              case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      uint32_t cf8;
      uint8_t type;
      uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
      if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
          return NULL;
xen/arch/x86/hvm/hvm.c +11 -0
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      {
          type = p->type;
          addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
      }
 

list_for_each_entry ( s,

xen/arch/x86/hvm/hvm.c +1 -0
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
          BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
          BUILD_BUG_ON(IOREQ_TYPE_COPY != HVMOP_IO_RANGE_MEMORY);
          BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG !=
 HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM !=
 HVMOP_IO_RANGE_WP_MEM);

r = s->range[type];

switch ( type )

xen/arch/x86/hvm/hvm.c +7 -0
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
              }
 
              break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
          }
      }
xen/include/asm- renamed from xen/include/asm-x86/hvm/domain.h +0 -0

x86/hvm/domain.h
index 992d5d1..b2e4234 100644

xen/include/asm-x86/hvm/domain.h +2 -2
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
      bool_t           pending;
  };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
  struct hvm_ioreq_server {
      struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +1 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
  # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory

range */
uint64_aligned_t start, end; /* IN - inclusive start and end of range */
};
typedef struct xen_hvm_io_range xen_hvm_io_range_t;

xen/include/public/hvm/ioreq.h +1 -1
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
  #define IOREQ_TYPE_PIO          0 /* pio */
  #define IOREQ_TYPE_COPY         1 /* mmio ops */
  #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
  #define IOREQ_TYPE_TIMEOFFSET   7
  #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
--
Quoted 1 line

1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#9 lore raw

Re: [PATCH v4 2/2] Refactor rangeset structure for better performance.

In reply to Paul Durrant
Quoted 60 lines · Paul Durrant
Quoted 60 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 2/2] Refactor rangeset structure for better performance.

This patch refactors struct rangeset to base it on the red-black tree structure, instead of on the current doubly linked list. By now, ioreq leverages rangeset to keep track of the IO/memory resources to be emulated. Yet when number of ranges inside one ioreq server is very high, traversing a doubly linked list could be time consuming. With this patch, the time complexity for searching a rangeset can be improved from O(n) to O(log(n)). Interfaces of rangeset still remain the same, and no new APIs introduced.

---
  xen/common/rangeset.c | 82
+++++++++++++++++++++++++++++++++++++--------------
  1 file changed, 60 insertions(+), 22 deletions(-)
xen/common/rangeset.c +6 -3
index 6c6293c..87b6aab 100644
@@ -10,11 +10,12 @@
  #include <xen/sched.h>
  #include <xen/errno.h>
  #include <xen/rangeset.h>
+#include <xen/rbtree.h>
  #include <xsm/xsm.h>
 
  /* An inclusive range [s,e] and pointer to next range in ascending order. */
  struct range {
-    struct list_head list;
+    struct rb_node node;
      unsigned long s, e;
  };
 
@@ -24,7 +25,7 @@ struct rangeset {
      struct domain   *domain;
 
      /* Ordered list of ranges contained in this set, and protecting lock. */
-    struct list_head range_list;
+    struct rb_root   range_tree;
 
      /* Number of ranges that can be allocated */
      long             nr_ranges;
@@ -45,41 +46,78 @@ struct rangeset {
  static struct range *find_range(
      struct rangeset *r, unsigned long s)
  {
-    struct range *x = NULL, *y;
+    struct rb_node *node;
+    struct range   *x;
+    struct range   *prev = NULL;
 
Quoted 15 lines

xen/common/rangeset.c struct rangeset { · line 61

Quoted 13 lines
xen/common/rangeset.c +8 -4
-    list_for_each_entry ( y, &r->range_list, list )
+    node = r->range_tree.rb_node;
+    while ( node )
      {
-        if ( y->s > s )
-            break;
-        x = y;
+        x = container_of(node, struct range, node);
+        if ( (s >= x->s) && (s <= x->e) )
+            return x;
+        if ( s < x->s )
+            node = node->rb_left;
+        else if ( s > x->s )

Do you need else if there? I think else would do. s either has to be < x->s or > x->e to get to this point.

Thanks. You are right. :)

Quoted 11 lines

xen/common/rangeset.c struct rangeset { · line 68

Quoted 9 lines
xen/common/rangeset.c +5 -1
+        {
+            prev = x;
+            node = node->rb_right;
+        }
      }
 
-    return x;
+    return prev;

Do you not want to return NULL here? It looks like you'd only get here if there was no matching range.

Well, prev can either be NULL(like its initial value), or pointing to the previous node of the range. It's to keep the original intention of routine find_range() - to return the range if found, or return the previous one, or NULL.

B.R.
Yu

Quoted 125 lines

Paul

Quoted 112 lines
xen/common/rangeset.c +41 -14
  }
 
  /* Return the lowest range in the set r, or NULL if r is empty. */
  static struct range *first_range(
      struct rangeset *r)
  {
-    if ( list_empty(&r->range_list) )
-        return NULL;
-    return list_entry(r->range_list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_first(&r->range_tree);
+    if ( node != NULL )
+        return container_of(node, struct range, node);
+
+    return NULL;
  }
 
  /* Return range following x in ascending order, or NULL if x is the highest. */
  static struct range *next_range(
      struct rangeset *r, struct range *x)
  {
-    if ( x->list.next == &r->range_list )
-        return NULL;
-    return list_entry(x->list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_next(&x->node);
+    if ( node )
+        return container_of(node, struct range, node);
+
+    return NULL;
  }
 
  /* Insert range y after range x in r. Insert as first range if x is NULL. */
  static void insert_range(
      struct rangeset *r, struct range *x, struct range *y)
  {
-    list_add(&y->list, (x != NULL) ? &x->list : &r->range_list);
+    struct rb_node **node;
+    struct rb_node *parent = NULL;
+
+    if ( x == NULL )
+        node = &r->range_tree.rb_node;
+    else
+    {
+        node = &x->node.rb_right;
+        parent = &x->node;
+    }
+
+    while ( *node )
+    {
+        parent = *node;
+        node = &parent->rb_left;
+    }
+
+    /* Add new node and rebalance the red-black tree. */
+    rb_link_node(&y->node, parent, node);
+    rb_insert_color(&y->node, &r->range_tree);
  }
 
  /* Remove a range from its list and free it. */
@@ -88,7 +126,7 @@ static void destroy_range(
  {
      r->nr_ranges++;
 
-    list_del(&x->list);
+    rb_erase(&x->node, &r->range_tree);
      xfree(x);
  }
 
@@ -319,7 +357,7 @@ bool_t rangeset_contains_singleton(
  bool_t rangeset_is_empty(
      const struct rangeset *r)
  {
-    return ((r == NULL) || list_empty(&r->range_list));
+    return ((r == NULL) || RB_EMPTY_ROOT(&r->range_tree));
  }
 
  struct rangeset *rangeset_new(
@@ -332,7 +370,7 @@ struct rangeset *rangeset_new(
          return NULL;
 
      rwlock_init(&r->lock);
-    INIT_LIST_HEAD(&r->range_list);
+    r->range_tree = RB_ROOT;
      r->nr_ranges = -1;
 
      BUG_ON(flags & ~RANGESETF_prettyprint_hex);
@@ -410,7 +448,7 @@ void rangeset_domain_destroy(
 
  void rangeset_swap(struct rangeset *a, struct rangeset *b)
  {
-    LIST_HEAD(tmp);
+    struct rb_node* tmp;
 
      if ( a < b )
      {
@@ -423,9 +461,9 @@ void rangeset_swap(struct rangeset *a, struct
 rangeset *b)
          write_lock(&a->lock);
      }
 
-    list_splice_init(&a->range_list, &tmp);
-    list_splice_init(&b->range_list, &a->range_list);
-    list_splice(&tmp, &b->range_list);
+    tmp = a->range_tree.rb_node;
+    a->range_tree.rb_node = b->range_tree.rb_node;
+    b->range_tree.rb_node = tmp;
 
      write_unlock(&a->lock);
      write_unlock(&b->lock);
Quoted 2 lines

--
1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

Re: [PATCH v4 2/2] Refactor rangeset structure for better performance.

Quoted 102 lines

-----Original Message-----
From: Yu, Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:47
To: Paul Durrant; [email protected]; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: Re: [Xen-devel] [PATCH v4 2/2] Refactor rangeset structure for better performance.

Quoted 4 lines · Paul Durrant
Quoted 4 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini;

Ian

Quoted 3 lines
Quoted 3 lines

Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 2/2] Refactor rangeset structure for better

performance.

Quoted 28 lines
Quoted 28 lines

This patch refactors struct rangeset to base it on the red-black tree structure, instead of on the current doubly linked list. By now, ioreq leverages rangeset to keep track of the IO/memory resources to be emulated. Yet when number of ranges inside one ioreq server is very high, traversing a doubly linked list could be time consuming. With this patch, the time complexity for searching a rangeset can be improved from O(n) to O(log(n)). Interfaces of rangeset still remain the same, and no new APIs introduced.

---
  xen/common/rangeset.c | 82
+++++++++++++++++++++++++++++++++++++--------------
  1 file changed, 60 insertions(+), 22 deletions(-)
xen/common/rangeset.c +1 -0
index 6c6293c..87b6aab 100644
@@ -10,11 +10,12 @@
  #include <xen/sched.h>
  #include <xen/errno.h>
  #include <xen/rangeset.h>
+#include <xen/rbtree.h>
  #include <xsm/xsm.h>
 
Quoted 1 line

xen/common/rangeset.c line 16

Quoted 1 line
xen/common/rangeset.c +0 -0
  /* An inclusive range [s,e] and pointer to next range in ascending order.

*/

Quoted 24 lines
Quoted 24 lines
xen/common/rangeset.c +5 -3
  struct range {
-    struct list_head list;
+    struct rb_node node;
      unsigned long s, e;
  };
 
@@ -24,7 +25,7 @@ struct rangeset {
      struct domain   *domain;
 
      /* Ordered list of ranges contained in this set, and protecting lock. */
-    struct list_head range_list;
+    struct rb_root   range_tree;
 
      /* Number of ranges that can be allocated */
      long             nr_ranges;
@@ -45,41 +46,78 @@ struct rangeset {
  static struct range *find_range(
      struct rangeset *r, unsigned long s)
  {
-    struct range *x = NULL, *y;
+    struct rb_node *node;
+    struct range   *x;
+    struct range   *prev = NULL;
 
Quoted 16 lines

xen/common/rangeset.c struct rangeset { · line 61

Quoted 13 lines
xen/common/rangeset.c +8 -4
-    list_for_each_entry ( y, &r->range_list, list )
+    node = r->range_tree.rb_node;
+    while ( node )
      {
-        if ( y->s > s )
-            break;
-        x = y;
+        x = container_of(node, struct range, node);
+        if ( (s >= x->s) && (s <= x->e) )
+            return x;
+        if ( s < x->s )
+            node = node->rb_left;
+        else if ( s > x->s )

Do you need else if there? I think else would do. s either has to be < x->s or x->e to get to this point.

Thanks. You are right. :)

Quoted 10 lines
Quoted 9 lines
xen/common/rangeset.c +5 -1
+        {
+            prev = x;
+            node = node->rb_right;
+        }
      }
 
-    return x;
+    return prev;
Quoted 6 lines
Quoted 1 line

Do you not want to return NULL here? It looks like you'd only get here if

there was no matching range.
Well, prev can either be NULL(like its initial value), or pointing to the previous node of the range. It's to keep the original intention of routine find_range() - to return the range if found, or return the previous one, or NULL.

If that's the correct semantic then that's fine.

Paul

Quoted 129 lines

B.R.
Yu

Quoted 21 lines

Paul

Quoted 18 lines
xen/common/rangeset.c +7 -3
  }
 
  /* Return the lowest range in the set r, or NULL if r is empty. */
  static struct range *first_range(
      struct rangeset *r)
  {
-    if ( list_empty(&r->range_list) )
-        return NULL;
-    return list_entry(r->range_list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_first(&r->range_tree);
+    if ( node != NULL )
+        return container_of(node, struct range, node);
+
+    return NULL;
  }
 
Quoted 1 line

xen/common/rangeset.c struct rangeset { · line 84

Quoted 1 line
xen/common/rangeset.c +0 -0
  /* Return range following x in ascending order, or NULL if x is the highest.

*/

Quoted 103 lines
Quoted 93 lines
xen/common/rangeset.c +34 -11
  static struct range *next_range(
      struct rangeset *r, struct range *x)
  {
-    if ( x->list.next == &r->range_list )
-        return NULL;
-    return list_entry(x->list.next, struct range, list);
+    struct rb_node *node;
+
+    node = rb_next(&x->node);
+    if ( node )
+        return container_of(node, struct range, node);
+
+    return NULL;
  }
 
  /* Insert range y after range x in r. Insert as first range if x is NULL. */
  static void insert_range(
      struct rangeset *r, struct range *x, struct range *y)
  {
-    list_add(&y->list, (x != NULL) ? &x->list : &r->range_list);
+    struct rb_node **node;
+    struct rb_node *parent = NULL;
+
+    if ( x == NULL )
+        node = &r->range_tree.rb_node;
+    else
+    {
+        node = &x->node.rb_right;
+        parent = &x->node;
+    }
+
+    while ( *node )
+    {
+        parent = *node;
+        node = &parent->rb_left;
+    }
+
+    /* Add new node and rebalance the red-black tree. */
+    rb_link_node(&y->node, parent, node);
+    rb_insert_color(&y->node, &r->range_tree);
  }
 
  /* Remove a range from its list and free it. */
@@ -88,7 +126,7 @@ static void destroy_range(
  {
      r->nr_ranges++;
 
-    list_del(&x->list);
+    rb_erase(&x->node, &r->range_tree);
      xfree(x);
  }
 
@@ -319,7 +357,7 @@ bool_t rangeset_contains_singleton(
  bool_t rangeset_is_empty(
      const struct rangeset *r)
  {
-    return ((r == NULL) || list_empty(&r->range_list));
+    return ((r == NULL) || RB_EMPTY_ROOT(&r->range_tree));
  }
 
  struct rangeset *rangeset_new(
@@ -332,7 +370,7 @@ struct rangeset *rangeset_new(
          return NULL;
 
      rwlock_init(&r->lock);
-    INIT_LIST_HEAD(&r->range_list);
+    r->range_tree = RB_ROOT;
      r->nr_ranges = -1;
 
      BUG_ON(flags & ~RANGESETF_prettyprint_hex);
@@ -410,7 +448,7 @@ void rangeset_domain_destroy(
 
  void rangeset_swap(struct rangeset *a, struct rangeset *b)
  {
-    LIST_HEAD(tmp);
+    struct rb_node* tmp;
 
      if ( a < b )
      {
@@ -423,9 +461,9 @@ void rangeset_swap(struct rangeset *a, struct
 rangeset *b)
          write_lock(&a->lock);
      }
 
-    list_splice_init(&a->range_list, &tmp);
-    list_splice_init(&b->range_list, &a->range_list);
-    list_splice(&tmp, &b->range_list);
+    tmp = a->range_tree.rb_node;
+    a->range_tree.rb_node = b->range_tree.rb_node;
+    b->range_tree.rb_node = tmp;
 
      write_unlock(&a->lock);
      write_unlock(&b->lock);
Quoted 2 lines

--
1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#4 lore raw

Re: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

In reply to Yu, Zhang

Hi Paul,

Quoted 32 lines · Zhang
Quoted 15 lines · Paul Durrant
Quoted 10 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian
Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Quoted 4 lines

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

Quoted 5 lines

Would the interfaces be better using xen_pfn_t rather than using uint64_t to pass addresses round where the bottom 12 bits will always be zero?

Paul

Thank you, Paul.
Well, I'm not quite sure if the bottom 12 bits of the address are zero. I had thought these addresses are just guest physical ones causing the ept violation, and not aligned down to its page address, like the MMIO. So, if our handler code has already done that, using xen_pfn_t could be better(my developing machine encountered some hardware problem, will check the code tomorrow) :)

Quoted 1 line

Yu

I just checked the code in hvm_select_ioreq_server(), and found value of address is not aligned down, and the lower 12 bits are not zero. So I wonder, is it necessary to do the shift to get a gfn, and what's the benefit?

Thanks
Yu

Quoted 292 lines
Quoted 275 lines
Quoted 275 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
  tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
  tools/libxc/xc_domain.c          | 55
++++++++++++++++++++++++++++++++++++++++
  xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
  xen/include/asm-x86/hvm/domain.h |  4 +--
  xen/include/public/hvm/hvm_op.h  |  1 +
  xen/include/public/hvm/ioreq.h   |  1 +
  6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                              int is_mmio,
                                              uint64_t start,
                                              uint64_t end);
+/**
+ * This function registers a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +8 -0
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +5 -0
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
 
  /**
   * This function registers a PCI device for config space emulation.
tools/libxc/xc_domain.c +0 -0
index 2ee26fb..42aeba9 100644
@@ -1552,6 +1552,61 @@ int

xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,

      return rc;

}

+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch, domid_t domid,
+ ioservid_t id, uint64_t
start, uint64_t end)

tools/libxc/xc_domain.c +26 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+}
+
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface *xch,

domid_t domid,
+ ioservid_t id, uint64_t
start, uint64_t end)

tools/libxc/xc_domain.c +26 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+
+}
+
  int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t

domid,
ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device,

xen/arch/x86/hvm/hvm.c +2 -1
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int hvm_ioreq_server_alloc_rangesets(struct
 hvm_ioreq_server *s,
 
          rc = asprintf(&name, "ioreq_server %d %s", s->id,
                        (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                        (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                        "");
          if ( rc )
              goto fail;
xen/arch/x86/hvm/hvm.c +4 -0
@@ -1258,6 +1259,7 @@ static int
 hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
              case HVMOP_IO_RANGE_PORT:
              case HVMOP_IO_RANGE_MEMORY:
              case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
@@ -1309,6 +1311,7 @@ static int
 hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
              case HVMOP_IO_RANGE_PORT:
              case HVMOP_IO_RANGE_MEMORY:
              case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      uint32_t cf8;
      uint8_t type;
      uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
      if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
          return NULL;
xen/arch/x86/hvm/hvm.c +11 -0
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      {
          type = p->type;
          addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
      }
 

list_for_each_entry ( s,

xen/arch/x86/hvm/hvm.c +1 -0
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
          BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
          BUILD_BUG_ON(IOREQ_TYPE_COPY != HVMOP_IO_RANGE_MEMORY);
          BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG !=
 HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM !=
 HVMOP_IO_RANGE_WP_MEM);

r = s->range[type];

switch ( type )

xen/arch/x86/hvm/hvm.c +7 -0
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
              }
 
              break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
          }
      }
xen/include/asm- renamed from xen/include/asm-x86/hvm/domain.h +0 -0

x86/hvm/domain.h
index 992d5d1..b2e4234 100644

xen/include/asm-x86/hvm/domain.h +2 -2
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
      bool_t           pending;
  };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
  struct hvm_ioreq_server {
      struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +1 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
  # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory

range */
uint64_aligned_t start, end; /* IN - inclusive start and end of
range */
};
typedef struct xen_hvm_io_range xen_hvm_io_range_t;

xen/include/public/hvm/ioreq.h +1 -0
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
  #define IOREQ_TYPE_PIO          0 /* pio */
  #define IOREQ_TYPE_COPY         1 /* mmio ops */
  #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
  #define IOREQ_TYPE_TIMEOFFSET   7
  #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
Quoted 10 lines
Quoted 2 lines

--
1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Re: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Quoted 45 lines

-----Original Message-----
From: Yu, Zhang [mailto:[email protected]]
Sent: 14 August 2015 08:41
To: Paul Durrant; [email protected]; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: Re: [Xen-devel] [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Hi Paul,

Quoted 11 lines · Zhang
Quoted 8 lines · Paul Durrant
Quoted 8 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian
Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 21 lines
Quoted 7 lines
Quoted 6 lines

server

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

Quoted 5 lines

Would the interfaces be better using xen_pfn_t rather than using uint64_t to pass addresses round where the bottom 12 bits will always be zero?

Paul

Thank you, Paul.
Well, I'm not quite sure if the bottom 12 bits of the address are zero. I had thought these addresses are just guest physical ones causing the ept violation, and not aligned down to its page address, like the MMIO. So, if our handler code has already done that, using xen_pfn_t could be better(my developing machine encountered some hardware problem, will check the code tomorrow) :)

Quoted 7 lines
Quoted 1 line

Yu

I just checked the code in hvm_select_ioreq_server(), and found value of address is not aligned down, and the lower 12 bits are not zero. So I wonder, is it necessary to do the shift to get a gfn, and what's the benefit?

Well, you can only make whole pages mmio_dm_write and I was assuming an emulator that did so would be interested in writes anywhere in the page. Maybe that assumption is wrong?

Paul

Quoted 301 lines

Thanks
Yu

Quoted 54 lines
Quoted 54 lines
Quoted 54 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
  tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
  tools/libxc/xc_domain.c          | 55
++++++++++++++++++++++++++++++++++++++++
  xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
  xen/include/asm-x86/hvm/domain.h |  4 +--
  xen/include/public/hvm/hvm_op.h  |  1 +
  xen/include/public/hvm/ioreq.h   |  1 +
  6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                              int is_mmio,
                                              uint64_t start,
                                              uint64_t end);
+/**
+ * This function registers a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +8 -0
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for

emulation.
+ *

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface

*xch,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/libxc/include/xenctrl.h +4 -0
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
Quoted 8 lines
Quoted 8 lines

tools/libxc/xc_domain.c int · line 1552

Quoted 8 lines

/**
* This function registers a PCI device for config space emulation.

tools/libxc/xc_domain.c +0 -0
index 2ee26fb..42aeba9 100644
@@ -1552,6 +1552,61 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,

domid_t

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
tools/libxc/xc_domain.c +2 -0
 domid,
      return rc;
  }
 
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
 domid_t domid,
+                                        ioservid_t id, uint64_t

start, uint64_t end)

tools/libxc/xc_domain.c +18 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
Quoted 8 lines
Quoted 8 lines

tools/libxc/xc_domain.c int xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,

Quoted 8 lines
tools/libxc/xc_domain.c +8 -0
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+}
+
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface

*xch,

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

domid_t domid,
+ ioservid_t id, uint64_t
start, uint64_t end)

tools/libxc/xc_domain.c +26 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+
+}
+
  int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static int

Quoted 8 lines

domid,
ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device,

xen/arch/x86/hvm/hvm.c +0 -0
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int

hvm_ioreq_server_alloc_rangesets(struct

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
xen/arch/x86/hvm/hvm.c +2 -1
 hvm_ioreq_server *s,
 
          rc = asprintf(&name, "ioreq_server %d %s", s->id,
                        (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                        (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                        "");
          if ( rc )
              goto fail;
xen/arch/x86/hvm/hvm.c +1 -0
@@ -1258,6 +1259,7 @@ static int
 hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
              case HVMOP_IO_RANGE_PORT:
              case HVMOP_IO_RANGE_MEMORY:
              case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1309,6 +1311,7 @@ static int

hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t

id,

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

case HVMOP_IO_RANGE_PORT:
case HVMOP_IO_RANGE_MEMORY:
case HVMOP_IO_RANGE_PCI:

xen/arch/x86/hvm/hvm.c +3 -0
+            case HVMOP_IO_RANGE_WP_MEM:
                  r = s->range[type];
                  break;
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      uint32_t cf8;
      uint8_t type;
      uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
      if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
          return NULL;
xen/arch/x86/hvm/hvm.c +11 -0
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
      {
          type = p->type;
          addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
      }
 
Quoted 5 lines
Quoted 5 lines

xen/arch/x86/hvm/hvm.c struct hvm_ioreq_server · line 2600

Quoted 5 lines

list_for_each_entry ( s,

xen/arch/x86/hvm/hvm.c +0 -0
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
          BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
          BUILD_BUG_ON(IOREQ_TYPE_COPY !=

HVMOP_IO_RANGE_MEMORY);

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/hvm.c +1 -0
          BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG !=
 HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM !=
 HVMOP_IO_RANGE_WP_MEM);

r = s->range[type];

switch ( type )

xen/arch/x86/hvm/hvm.c +7 -0
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
              }
 
              break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
          }
      }
xen/include/asm- renamed from xen/include/asm-x86/hvm/domain.h +0 -0

x86/hvm/domain.h
index 992d5d1..b2e4234 100644

xen/include/asm-x86/hvm/domain.h +2 -2
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
      bool_t           pending;
  };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
Quoted 11 lines
Quoted 11 lines

xen/include/public/hvm/hvm_op.h struct xen_hvm_io_range { · line 333

Quoted 11 lines
xen/include/asm-x86/hvm/domain.h +0 -0
  struct hvm_ioreq_server {
      struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +0 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
  # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range */

Quoted 36 lines
Quoted 19 lines
Quoted 19 lines
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory

range */
uint64_aligned_t start, end; /* IN - inclusive start and end of
range */
};
typedef struct xen_hvm_io_range xen_hvm_io_range_t;

xen/include/public/hvm/ioreq.h +1 -0
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
  #define IOREQ_TYPE_PIO          0 /* pio */
  #define IOREQ_TYPE_COPY         1 /* mmio ops */
  #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
  #define IOREQ_TYPE_TIMEOFFSET   7
  #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
Quoted 10 lines
Quoted 2 lines

--
1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#6 lore raw

Re: [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Quoted 53 lines · Paul Durrant
Quoted 45 lines

-----Original Message-----
From: Yu, Zhang [mailto:[email protected]]
Sent: 14 August 2015 08:41
To: Paul Durrant; [email protected]; Ian Jackson; Stefano Stabellini; Ian Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: Re: [Xen-devel] [PATCH v4 1/2] Differentiate IO/mem resources tracked by ioreq server

Hi Paul,

Quoted 11 lines · Zhang
Quoted 8 lines · Paul Durrant
Quoted 8 lines

-----Original Message-----
From: Yu Zhang [mailto:[email protected]]
Sent: 13 August 2015 11:06
To: [email protected]; Paul Durrant; Ian Jackson; Stefano Stabellini; Ian
Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper
Cc: Kevin Tian; [email protected]
Subject: [PATCH v4 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 21 lines
Quoted 7 lines
Quoted 6 lines

server

Currently in ioreq server, guest write-protected ram pages are tracked in the same rangeset with device mmio resources. Yet unlike device mmio, which can be in big chunks, the guest write- protected pages may be discrete ranges with 4K bytes each.

Quoted 5 lines

Would the interfaces be better using xen_pfn_t rather than using uint64_t to pass addresses round where the bottom 12 bits will always be zero?

Paul

Thank you, Paul.
Well, I'm not quite sure if the bottom 12 bits of the address are zero. I had thought these addresses are just guest physical ones causing the ept violation, and not aligned down to its page address, like the MMIO. So, if our handler code has already done that, using xen_pfn_t could be better(my developing machine encountered some hardware problem, will check the code tomorrow) :)

Quoted 7 lines
Quoted 1 line

Yu

I just checked the code in hvm_select_ioreq_server(), and found value of address is not aligned down, and the lower 12 bits are not zero. So I wonder, is it necessary to do the shift to get a gfn, and what's the benefit?

Quoted 3 lines

Well, you can only make whole pages mmio_dm_write and I was assuming an emulator that did so would be interested in writes anywhere in the page. Maybe that assumption is wrong?

Paul

Thank you, Paul. That makes sense. I'll try use the xen_pfn_t. :-)

Yu

Quoted 309 lines
Quoted 254 lines

Thanks
Yu

Quoted 54 lines
Quoted 54 lines
Quoted 54 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_WP_MEM, is defined.

Note: Previously, a new hypercall or subop was suggested to map write-protected pages into ioreq server. However, it turned out handler of this new hypercall would be almost the same with the existing pair - HVMOP_[un]map_io_range_to_ioreq_server, and there's already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
   tools/libxc/include/xenctrl.h    | 31 ++++++++++++++++++++++
   tools/libxc/xc_domain.c          | 55
++++++++++++++++++++++++++++++++++++++++
   xen/arch/x86/hvm/hvm.c           | 26 ++++++++++++++++++-
   xen/include/asm-x86/hvm/domain.h |  4 +--
   xen/include/public/hvm/hvm_op.h  |  1 +
   xen/include/public/hvm/ioreq.h   |  1 +
   6 files changed, 115 insertions(+), 3 deletions(-)
tools/libxc/include/xenctrl.h +2 -0
index de3c0ad..53f196d 100644
@@ -2010,6 +2010,37 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,
                                               int is_mmio,
                                               uint64_t start,
                                               uint64_t end);
+/**
+ * This function registers a range of write-protected memory for

emulation.
+ *
+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */

tools/libxc/include/xenctrl.h +8 -0
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
+                                        domid_t domid,
+                                        ioservid_t id,
+                                        uint64_t start,
+                                        uint64_t end);
+
+/**
+ * This function deregisters a range of write-protected memory for

emulation.
+ *

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+ * @parm xch a handle to an open hypervisor interface.
+ * @parm domid the domain id to be serviced
+ * @parm id the IOREQ Server id.
+ * @parm start start of range
+ * @parm end end of range (inclusive).
+ * @return 0 on success, -1 on failure.
+ */
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface

*xch,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
tools/libxc/include/xenctrl.h +4 -0
+                                            domid_t domid,
+                                            ioservid_t id,
+                                            uint64_t start,
+                                            uint64_t end);
Quoted 8 lines
Quoted 8 lines

tools/libxc/xc_domain.c int · line 1552

Quoted 8 lines

/**
* This function registers a PCI device for config space emulation.

tools/libxc/xc_domain.c +0 -0
index 2ee26fb..42aeba9 100644
@@ -1552,6 +1552,61 @@ int
 xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch,

domid_t

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
tools/libxc/xc_domain.c +2 -0
 domid,
       return rc;
   }
 
+int xc_hvm_map_mem_range_to_ioreq_server(xc_interface *xch,
 domid_t domid,
+                                        ioservid_t id, uint64_t

start, uint64_t end)

tools/libxc/xc_domain.c +18 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
Quoted 8 lines
Quoted 8 lines

tools/libxc/xc_domain.c int xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,

Quoted 8 lines
tools/libxc/xc_domain.c +8 -0
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+}
+
+int xc_hvm_unmap_mem_range_from_ioreq_server(xc_interface

*xch,

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines

domid_t domid,
+ ioservid_t id, uint64_t
start, uint64_t end)

tools/libxc/xc_domain.c +26 -0
+{
+    DECLARE_HYPERCALL;
+    DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
+    int rc;
+
+    arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
+    if ( arg == NULL )
+        return -1;
+
+    hypercall.op     = __HYPERVISOR_hvm_op;
+    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
+    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
+
+    arg->domid = domid;
+    arg->id = id;
+    arg->type = HVMOP_IO_RANGE_WP_MEM;
+    arg->start = start;
+    arg->end = end;
+
+    rc = do_xen_hypercall(xch, &hypercall);
+
+    xc_hypercall_buffer_free(xch, arg);
+    return rc;
+
+}
+
   int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static int

Quoted 8 lines

domid,
ioservid_t id, uint16_t segment, uint8_t bus, uint8_t device,

xen/arch/x86/hvm/hvm.c +0 -0
index c957610..0389c0a 100644
@@ -938,8 +938,9 @@ static int

hvm_ioreq_server_alloc_rangesets(struct

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
xen/arch/x86/hvm/hvm.c +2 -1
 hvm_ioreq_server *s,
 
           rc = asprintf(&name, "ioreq_server %d %s", s->id,
                         (i == HVMOP_IO_RANGE_PORT) ? "port" :
-                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "mmio" :
                         (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_WP_MEM) ? "wp-ed memory" :
                         "");
           if ( rc )
               goto fail;
xen/arch/x86/hvm/hvm.c +1 -0
@@ -1258,6 +1259,7 @@ static int
 hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
               case HVMOP_IO_RANGE_PORT:
               case HVMOP_IO_RANGE_MEMORY:
               case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_WP_MEM:
                   r = s->range[type];
                   break;
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
xen/arch/x86/hvm/hvm.c +0 -0
@@ -1309,6 +1311,7 @@ static int

hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t

id,

Quoted 35 lines
Quoted 35 lines
Quoted 35 lines

case HVMOP_IO_RANGE_PORT:
case HVMOP_IO_RANGE_MEMORY:
case HVMOP_IO_RANGE_PCI:

xen/arch/x86/hvm/hvm.c +3 -0
+            case HVMOP_IO_RANGE_WP_MEM:
                   r = s->range[type];
                   break;
@@ -2523,6 +2526,8 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
       uint32_t cf8;
       uint8_t type;
       uint64_t addr;
+    p2m_type_t p2mt;
+    struct page_info *ram_page;
 
       if ( list_empty(&d->arch.hvm_domain.ioreq_server.list) )
           return NULL;
xen/arch/x86/hvm/hvm.c +11 -0
@@ -2565,6 +2570,17 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
       {
           type = p->type;
           addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_WP_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
       }
 
Quoted 5 lines
Quoted 5 lines

xen/arch/x86/hvm/hvm.c struct hvm_ioreq_server · line 2600

Quoted 5 lines

list_for_each_entry ( s,

xen/arch/x86/hvm/hvm.c +0 -0
@@ -2582,6 +2598,7 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
           BUILD_BUG_ON(IOREQ_TYPE_PIO != HVMOP_IO_RANGE_PORT);
           BUILD_BUG_ON(IOREQ_TYPE_COPY !=

HVMOP_IO_RANGE_MEMORY);

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/hvm.c +1 -0
           BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG !=
 HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_WP_MEM !=
 HVMOP_IO_RANGE_WP_MEM);

r = s->range[type];

switch ( type )

xen/arch/x86/hvm/hvm.c +7 -0
@@ -2609,6 +2626,13 @@ struct hvm_ioreq_server
 *hvm_select_ioreq_server(struct domain *d,
               }
 
               break;
+
+        case IOREQ_TYPE_WP_MEM:
+            end = addr + (p->size * p->count) - 1;
+            if ( rangeset_contains_range(r, addr, end) )
+                return s;
+
+            break;
           }
       }
xen/include/asm- renamed from xen/include/asm-x86/hvm/domain.h +0 -0

x86/hvm/domain.h
index 992d5d1..b2e4234 100644

xen/include/asm-x86/hvm/domain.h +2 -2
@@ -48,8 +48,8 @@ struct hvm_ioreq_vcpu {
       bool_t           pending;
   };
 
-#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_PCI + 1)
-#define MAX_NR_IO_RANGES  256
+#define NR_IO_RANGE_TYPES (HVMOP_IO_RANGE_WP_MEM + 1)
+#define MAX_NR_IO_RANGES  8192
 
Quoted 48 lines
Quoted 11 lines
Quoted 11 lines

xen/include/public/hvm/hvm_op.h struct xen_hvm_io_range { · line 333

Quoted 11 lines
xen/include/asm-x86/hvm/domain.h +0 -0
   struct hvm_ioreq_server {
       struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +0 -0
index 014546a..c02c91a 100644
@@ -331,6 +331,7 @@ struct xen_hvm_io_range {
   # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
   # define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
   # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range */

Quoted 36 lines
Quoted 19 lines
Quoted 19 lines
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_WP_MEM 3 /* Write-protected memory

range */
uint64_aligned_t start, end; /* IN - inclusive start and end of
range */

   };
   typedef struct xen_hvm_io_range xen_hvm_io_range_t;
xen/include/public/hvm/ioreq.h +1 -0
index 2e5809b..2b712ac 100644
@@ -35,6 +35,7 @@
   #define IOREQ_TYPE_PIO          0 /* pio */
   #define IOREQ_TYPE_COPY         1 /* mmio ops */
   #define IOREQ_TYPE_PCI_CONFIG   2
+#define IOREQ_TYPE_WP_MEM       3
   #define IOREQ_TYPE_TIMEOFFSET   7
   #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
Quoted 10 lines
Quoted 2 lines

--
1.9.1

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel