Skip to contentSkip to conversation

Refactor ioreq server for better performance.

PATCH Superseded Yu Zhang 4 participants 12 messages Aug 10, 2015 – Aug 11, 2015

v3 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

v3 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 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    | 39 +++++++++++++++++--
 tools/libxc/xc_domain.c          | 59 ++++++++++++++++++++++++++++-
 xen/arch/x86/hvm/hvm.c           | 33 ++++++++++++++--
 xen/common/rangeset.c            | 82 +++++++++++++++++++++++++++++-----------
 xen/include/asm-x86/hvm/domain.h |  4 +-
 xen/include/public/hvm/hvm_op.h  |  3 +-
 xen/include/public/hvm/ioreq.h   |  1 +
 7 files changed, 186 insertions(+), 35 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v3 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_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    | 39 +++++++++++++++++++++++---
 tools/libxc/xc_domain.c          | 59 ++++++++++++++++++++++++++++++++++++++--
 xen/arch/x86/hvm/hvm.c           | 33 +++++++++++++++++++---
 xen/include/asm-x86/hvm/domain.h |  4 +--
 xen/include/public/hvm/hvm_op.h  |  3 +-
 xen/include/public/hvm/ioreq.h   |  1 +
 6 files changed, 126 insertions(+), 13 deletions(-)
tools/libxc/include/xenctrl.h +35 -4
index de3c0ad..3e8c203 100644
@@ -1976,12 +1976,12 @@ int xc_hvm_set_ioreq_server_state(xc_interface *xch,
                                   int enabled);
 
 /**
- * This function registers a range of memory or I/O ports for emulation.
+ * This function registers a range of mmio or I/O ports 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 is_mmio is this a range of ports or memory
+ * @parm is_mmio is this a range of ports or mmio
  * @parm start start of range
  * @parm end end of range (inclusive).
  * @return 0 on success, -1 on failure.
@@ -1994,12 +1994,12 @@ int xc_hvm_map_io_range_to_ioreq_server(xc_interface *xch,
                                         uint64_t end);
 
 /**
- * This function deregisters a range of memory or I/O ports for emulation.
+ * This function deregisters a range of mmio or I/O ports 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 is_mmio is this a range of ports or memory
+ * @parm is_mmio is this a range of ports or mmio
  * @parm start start of range
  * @parm end end of range (inclusive).
  * @return 0 on success, -1 on failure.
@@ -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 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 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 +57 -2

77 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +29 -4
index c957610..d76e3ce 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_MMIO) ? "mmio" :
                       (i == HVMOP_IO_RANGE_PCI) ? "pci" :
+                      (i == HVMOP_IO_RANGE_MEMORY) ? "memory" :
                       "");
         if ( rc )
             goto fail;
@@ -1256,8 +1257,9 @@ static int hvm_map_io_range_to_ioreq_server(struct domain *d, ioservid_t id,
             switch ( type )
             {
             case HVMOP_IO_RANGE_PORT:
-            case HVMOP_IO_RANGE_MEMORY:
+            case HVMOP_IO_RANGE_MMIO:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_MEMORY:
                 r = s->range[type];
                 break;
 
@@ -1307,8 +1309,9 @@ static int hvm_unmap_io_range_from_ioreq_server(struct domain *d, ioservid_t id,
             switch ( type )
             {
             case HVMOP_IO_RANGE_PORT:
-            case HVMOP_IO_RANGE_MEMORY:
+            case HVMOP_IO_RANGE_MMIO:
             case HVMOP_IO_RANGE_PCI:
+            case HVMOP_IO_RANGE_MEMORY:
                 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,18 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
     {
         type = p->type;
         addr = p->addr;
+
+        if ( p->type == IOREQ_TYPE_COPY )
+        {
+            /* Check for paged out page */
+            ram_page = get_page_from_gfn(d, p->addr >> PAGE_SHIFT,
+                                         &p2mt, P2M_UNSHARE);
+            if ( p2mt == p2m_mmio_write_dm )
+                type = IOREQ_TYPE_MEM;
+
+            if ( ram_page )
+                put_page(ram_page);
+        }
     }
 
     list_for_each_entry ( s,
@@ -2580,8 +2597,9 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             continue;
 
         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_COPY != HVMOP_IO_RANGE_MMIO);
         BUILD_BUG_ON(IOREQ_TYPE_PCI_CONFIG != HVMOP_IO_RANGE_PCI);
+        BUILD_BUG_ON(IOREQ_TYPE_MEM != HVMOP_IO_RANGE_MEMORY);
         r = s->range[type];
 
         switch ( type )
@@ -2609,6 +2627,13 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
             }
 
             break;
+
+        case IOREQ_TYPE_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..8b84aea 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_MEMORY + 1)
+#define MAX_NR_IO_RANGES  8192
 
 struct hvm_ioreq_server {
     struct list_head       list_entry;
xen/include/public/hvm/hvm_op.h +2 -1
index 014546a..9106cb9 100644
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
     ioservid_t id;               /* IN - server id */
     uint32_t type;               /* IN - type of range */
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_MEMORY 3 /* 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..1d51e60 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_MEM          3
 #define IOREQ_TYPE_TIMEOFFSET   7
 #define IOREQ_TYPE_INVALIDATE   8 /* mapcache */
 
Signature · 1 line
1.9.1
#2 lore raw

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

Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
 tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
 tools/libxc/xc_domain.c          | 59 ++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

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

Quoted 12 lines
xen/include/public/hvm/hvm_op.h +2 -1
index 014546a..9106cb9 100644
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
     ioservid_t id;               /* IN - server id */
     uint32_t type;               /* IN - type of range */
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Wei.

#3 lore raw

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

Quoted 47 lines

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

Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
 tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
 tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
     ioservid_t id;               /* IN - server id */
     uint32_t type;               /* IN - type of range */
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range

*/

Quoted 5 lines

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this would require an interface version change and some if-defery.

Paul

Quoted 1 line

Wei.

#4 lore raw

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

Quoted 47 lines · Paul Durrant
Quoted 47 lines

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

Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
 tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
 tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
     ioservid_t id;               /* IN - server id */
     uint32_t type;               /* IN - type of range */
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range

*/

Quoted 5 lines
Quoted 4 lines

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

~Andrew

#5 lore raw

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

Quoted 61 lines

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

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: 10 August 2015 09:26
To: Yu Zhang
Cc: [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; Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 28 lines
Quoted 28 lines

server

Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
 tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
 tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
     ioservid_t id;               /* IN - server id */
     uint32_t type;               /* IN - type of range */
 # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
 # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range

Quoted 2 lines
Quoted 2 lines

*/

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this

would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

If QEMU were re-built against the updated header, yes.

Paul

Quoted 2 lines

~Andrew

author
#6 lore raw

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

Quoted 64 lines · Paul Durrant
Quoted 64 lines

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

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: 10 August 2015 09:26
To: Yu Zhang
Cc: [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; Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 28 lines
Quoted 28 lines

server

Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
  tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
  tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
      ioservid_t id;               /* IN - server id */
      uint32_t type;               /* IN - type of range */
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range

Quoted 5 lines
Quoted 5 lines

*/

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Indeed, the old name is a bit problematic. Presumably re-use like this

would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

If QEMU were re-built against the updated header, yes.

Thank you, Andrew & Paul. :)
Are you referring to the xen_map/unmap_memory_section routines in QEMU? I noticed they are called by xen_region_add/del in QEMU. And I wonder, are these 2 routines used to track a memory region or to track a MMIO region? If the region to be added is a MMIO, I guess the new interface should be fine, but if it is memory region to be added into ioreq server, maybe a patch in QEMU is necessary(e.g. use some if-defery for this new interface version you suggested)?

Thanks
Yu

Quoted 6 lines

Paul

Quoted 2 lines

~Andrew

#7 lore raw

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

Quoted 88 lines

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

Quoted 7 lines · Paul Durrant
Quoted 7 lines

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

ioreq

Quoted 8 lines
Quoted 8 lines

server

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: 10 August 2015 09:26
To: Yu Zhang
Cc: [email protected]; Paul Durrant; Ian Jackson; Stefano

Stabellini;

Quoted 13 lines
Quoted 13 lines

Ian

Quoted 3 lines
Quoted 3 lines

Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper; Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 8 lines
Quoted 8 lines

server

Quoted 5 lines · Yu Zhang

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 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_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

Quoted 24 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
  tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
  tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
      ioservid_t id;               /* IN - server id */
      uint32_t type;               /* IN - type of range */
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

*/

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this

is

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this

would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

If QEMU were re-built against the updated header, yes.

Quoted 10 lines

Thank you, Andrew & Paul. :)
Are you referring to the xen_map/unmap_memory_section routines in QEMU?
I noticed they are called by xen_region_add/del in QEMU. And I wonder, are these 2 routines used to track a memory region or to track a MMIO region? If the region to be added is a MMIO, I guess the new interface should be fine, but if it is memory region to be added into ioreq server, maybe a patch in QEMU is necessary(e.g. use some if-defery for this new interface version you suggested)?

I was forgetting that QEMU uses libxenctrl so your change to xc_hvm_map_io_range_to_ioreq_server() means everything will continue to work as before. There is still the (admittedly academic) problem of some unknown emulator out there that rolls its own hypercalls and blindly updates to the new version of hvm_op.h suddenly starting to register memory ranges rather than mmio ranges though. I would leave the existing definitions as-is and come up with a new name.

Paul

Quoted 8 lines

Thanks
Yu

Quoted 6 lines

Paul

Quoted 2 lines

~Andrew

author
#8 lore raw

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

Quoted 99 lines · Paul Durrant
Quoted 88 lines

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

Quoted 7 lines · Paul Durrant
Quoted 7 lines

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

ioreq

Quoted 8 lines
Quoted 8 lines

server

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: 10 August 2015 09:26
To: Yu Zhang
Cc: [email protected]; Paul Durrant; Ian Jackson; Stefano

Stabellini;

Quoted 13 lines
Quoted 13 lines

Ian

Quoted 3 lines
Quoted 3 lines

Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew Cooper; Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 8 lines
Quoted 8 lines

server

Quoted 5 lines · Yu Zhang

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 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

This patch uses a seperate rangeset for the guest ram pages. And a new ioreq type, IOREQ_TYPE_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

Quoted 24 lines
Quoted 24 lines
Quoted 13 lines
Quoted 13 lines
Quoted 7 lines

already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
   tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
   tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
       ioservid_t id;               /* IN - server id */
       uint32_t type;               /* IN - type of range */
   # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
   # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func

range

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

*/

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this

is

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this

would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

If QEMU were re-built against the updated header, yes.

Quoted 10 lines

Thank you, Andrew & Paul. :)
Are you referring to the xen_map/unmap_memory_section routines in QEMU?
I noticed they are called by xen_region_add/del in QEMU. And I wonder, are these 2 routines used to track a memory region or to track a MMIO region? If the region to be added is a MMIO, I guess the new interface should be fine, but if it is memory region to be added into ioreq server, maybe a patch in QEMU is necessary(e.g. use some if-defery for this new interface version you suggested)?

Quoted 1 line

I was forgetting that QEMU uses libxenctrl so your change to xc_hvm_map_io_range_to_ioreq_server() means everything will continue to work as before. There is still the (admittedly academic) problem of some unknown emulator out there that rolls its own hypercalls and blindly updates to the new version of hvm_op.h suddenly starting to register memory ranges rather than mmio ranges though. I would leave the existing definitions as-is and come up with a new name.

So, how about we keep the HVMOP_IO_RANGE_MEMORY name for MMIO, and use a new one, say HVMOP_IO_RANGE_WP_MEM, for write-protected rams only? :)

Thanks
Yu

Quoted 18 lines

Paul

Quoted 8 lines

Thanks
Yu

Quoted 6 lines

Paul

Quoted 2 lines

~Andrew

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

#9 lore raw

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

Quoted 126 lines

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

Quoted 7 lines · Paul Durrant
Quoted 7 lines

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

ioreq

Quoted 10 lines
Quoted 10 lines

server

Quoted 5 lines · Paul Durrant
Quoted 5 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 10 August 2015 11:56
To: Paul Durrant; Wei Liu; Yu Zhang
Cc: [email protected]; Ian Jackson; Stefano Stabellini; Ian

Campbell;

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

Keir (Xen.org); [email protected]; Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked by

ioreq

Quoted 3 lines
Quoted 3 lines

server

On 10/08/15 09:33, Paul Durrant wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Wei Liu [mailto:[email protected]]
Sent: 10 August 2015 09:26
To: Yu Zhang
Cc: [email protected]; Paul Durrant; Ian Jackson; Stefano

Stabellini;

Quoted 2 lines
Quoted 2 lines

Ian

Quoted 1 line
Quoted 1 line

Campbell; Wei Liu; Keir (Xen.org); [email protected]; Andrew

Cooper;

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Kevin Tian; [email protected]
Subject: Re: [PATCH v3 1/2] Differentiate IO/mem resources tracked

by

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

ioreq

Quoted 15 lines
Quoted 15 lines

server

Quoted 12 lines · Yu Zhang

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

Quoted 7 lines
Quoted 7 lines

there's

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

already a type parameter in this hypercall. So no new hypercall defined, only a new type is introduced.

---
   tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++-

--

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line
   tools/libxc/xc_domain.c          | 59

++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Review

Quoted 1 line
(quoted diff) +0 -0

b/xen/include/public/hvm/hvm_op.h

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

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

Quoted 10 lines

index 014546a..9106cb9 100644

xen/include/public/hvm/hvm_op.h +1 -1
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
       ioservid_t id;               /* IN - server id */
       uint32_t type;               /* IN - type of range */
   # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
   # define HVMOP_IO_RANGE_PCI    2 /* PCI

segment/bus/dev/func

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

range

Quoted 3 lines
Quoted 3 lines

*/

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

Quoted 1 line
xen/include/public/hvm/hvm_op.h +1 -0
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because

this

Quoted 22 lines
Quoted 11 lines

is

Quoted 9 lines
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

a toolstack-only interface?

Indeed, the old name is a bit problematic. Presumably re-use like this

would require an interface version change and some if-defery.

I assume it is an interface used by qemu, so this patch in its currently state will break things.

If QEMU were re-built against the updated header, yes.

Quoted 10 lines

Thank you, Andrew & Paul. :)
Are you referring to the xen_map/unmap_memory_section routines in QEMU?
I noticed they are called by xen_region_add/del in QEMU. And I wonder, are these 2 routines used to track a memory region or to track a MMIO region? If the region to be added is a MMIO, I guess the new interface should be fine, but if it is memory region to be added into ioreq server, maybe a patch in QEMU is necessary(e.g. use some if-defery for this new interface version you suggested)?

Quoted 1 line

I was forgetting that QEMU uses libxenctrl so your change to

xc_hvm_map_io_range_to_ioreq_server() means everything will continue to work as before. There is still the (admittedly academic) problem of some unknown emulator out there that rolls its own hypercalls and blindly updates to the new version of hvm_op.h suddenly starting to register memory ranges rather than mmio ranges though. I would leave the existing definitions as-is and come up with a new name.

Quoted 4 lines

So, how about we keep the HVMOP_IO_RANGE_MEMORY name for MMIO, and use
a new one, say HVMOP_IO_RANGE_WP_MEM, for write-protected rams only? :)

That works for me :-)

Paul

Quoted 22 lines

Thanks
Yu

Quoted 18 lines

Paul

Quoted 8 lines

Thanks
Yu

Quoted 6 lines

Paul

Quoted 2 lines

~Andrew

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

author
#10 lore raw

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

In reply to Wei Liu
Quoted 23 lines · Wei Liu
Quoted 15 lines · Yu Zhang

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_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.

Quoted 4 lines
---
  tools/libxc/include/xenctrl.h    | 39 +++++++++++++++++++++++---
  tools/libxc/xc_domain.c          | 59 ++++++++++++++++++++++++++++++++++++++--

FWIW the hypercall wrappers look correct to me.

Quoted 16 lines

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

Quoted 12 lines
xen/include/public/hvm/hvm_op.h +2 -1
index 014546a..9106cb9 100644
@@ -329,8 +329,9 @@ struct xen_hvm_io_range {
      ioservid_t id;               /* IN - server id */
      uint32_t type;               /* IN - type of range */
  # define HVMOP_IO_RANGE_PORT   0 /* I/O port range */
-# define HVMOP_IO_RANGE_MEMORY 1 /* MMIO range */
+# define HVMOP_IO_RANGE_MMIO   1 /* MMIO range */
  # define HVMOP_IO_RANGE_PCI    2 /* PCI segment/bus/dev/func range */
+# define HVMOP_IO_RANGE_MEMORY 3 /* MEMORY range */

This looks problematic. Maybe you can get away with this because this is a toolstack-only interface?

Thanks Wei.
Well, I believe this interface could be used both by the backend device driver and qemu as well(which I neglected). :-)

Yu

Quoted 8 lines

Wei.

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

author
#11 lore raw

PATCH v3 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