Skip to contentSkip to conversation

Refactoring mempaging code from xenpaging to libxc and few updates

PATCH In review Dushyant Behl 2 participants 7 messages Aug 7, 2014 – Aug 13, 2014

lore
Share & tools
lore
#0 lore raw

The following patches refactor memepaging code from xenpaging to libxc -

[PATCH v3 1/3] tools/libxc: refactored mempaging code from xenpaging This patch adds two routines xc_mem_paging_ring_setup and xc_mem_paging_ring_teardown to libxc at the path tools/libxc/xc_mem_paging_setup.c, which is a new file.

[PATCH v3 2/3] tools/libxc/xc_mem_paging_setup.c: replacing deprecated functions Replaces calls to deprecated function xc_map_foreign_batch with calls to xc_map_foreign_bulk.

[PATCH v3 3/3] tools/libxc/xc_mem_paging_setup.c: FIX- Race condition Fix for a known race condition bug in mempaging ring setup code, this race condition is actually simmilar to that concerned by XSA-99. This patch tries to take care of all the points mentioned in XSA-99, along with clearing the ringpage to remove any bogus input.

#1 lore raw

PATCH v3 1/3 tools/libxc: refactored mempaging code from xenpaging to libxc.

This patch moves the code to initialize and teardown mempaging from xenpaging to libxc. The code refactored from xenpaging is the code which sets up paging, initializes a shared ring and event channel to communicate with xen. This communication is done between the hypervisor and the dom0 tool which performs the activity of pager. Another routine is also added by this patch which will teardown the ring and mem paging setup. The xenpaging code is changed to use the newly created routines and is tested to properly build and work with this code. The refactored code is to be placed in libxc with LGPL license while the old xenpaging code was under GPL2+ license, hence proper permission has been taken from previous copyright holders to relicense the code.

The refactoring is done so that the tools lazy restore pager and xenpaging which rely on memory paging can use the same routines to initialize mempaging. This refactoring will also allow any future (in-tree) tools to use mempaging. The refactored code in xc_mem_paging_ring_setup is to be compiled into libxenguest.

Note:- The name of a macro in the file tools/ocaml/libs/xc/xenctrl_stubs.c is changed from RING_SIZE to BUF_RING_SIZE because the earlier name collides with xen shared ring definitions in io/ring.h

---
 tools/libxc/Makefile                |   2 +
 tools/libxc/xc_mem_paging_setup.c   | 184 ++++++++++++++++++++++++++++++++++++
 tools/libxc/xenguest.h              |  42 ++++++++
 tools/ocaml/libs/xc/xenctrl_stubs.c |   7 +-
 tools/xenpaging/Makefile            |   4 +-
 tools/xenpaging/xenpaging.c         | 110 +++++----------------
 6 files changed, 255 insertions(+), 94 deletions(-)
 create mode 100644 tools/libxc/xc_mem_paging_setup.c
tools/libxc/Makefile +2 -0
index 22eef8e..68c5f08 100644
@@ -70,6 +70,8 @@ GUEST_SRCS-$(CONFIG_ARM)     += xc_dom_armzimageloader.c
 GUEST_SRCS-y                 += xc_dom_binloader.c
 GUEST_SRCS-y                 += xc_dom_compat_linux.c
 
+GUEST_SRCS-y                 += xc_mem_paging_setup.c
+
 GUEST_SRCS-$(CONFIG_X86)     += xc_dom_x86.c
 GUEST_SRCS-$(CONFIG_X86)     += xc_cpuid_x86.c
 GUEST_SRCS-$(CONFIG_X86)     += xc_hvm_build_x86.c
tools/libxc/xc_mem_paging_setup.c new file +184 -0

184 lines not on this page · raw message

tools/libxc/xenguest.h +42 -0

52 lines not on this page · raw message

tools/ocaml/libs/xc/xenctrl_stubs.c +3 -4
index f0810eb..8ce957e 100644
@@ -525,13 +525,12 @@ CAMLprim value stub_xc_evtchn_reset(value xch, value domid)
 	CAMLreturn(Val_unit);
 }
 
-
-#define RING_SIZE 32768
-static char ring[RING_SIZE];
+#define BUF_RING_SIZE 32768
+static char ring[BUF_RING_SIZE];
 
 CAMLprim value stub_xc_readconsolering(value xch)
 {
-	unsigned int size = RING_SIZE - 1;
+	unsigned int size = BUF_RING_SIZE - 1;
 	char *ring_ptr = ring;
 	int retval;
 
tools/xenpaging/Makefile +2 -2
index 548d9dd..ea370d3 100644
@@ -1,8 +1,8 @@
 XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
-CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS)
-LDLIBS += $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
+CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS)
+LDLIBS += $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
 
 POLICY    = default
tools/xenpaging/xenpaging.c +22 -88

153 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/3 tools/libxc/xc_mem_paging_setup.c: replacing deprecated function calls.

In reply to Dushyant Behl

This patch replaces the calls to the deprecated function xc_map_foreign_batch with the calls to xc_map_foreign_bulk in tools/libxc/xc_mem_paging_ring_setup.c
The function xc_map_foreign_bulk has a cleaner error reporting interface than xc_map_foreign_batch.

---
 tools/libxc/xc_mem_paging_setup.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tools/libxc/xc_mem_paging_setup.c +7 -7
index bfb9a9b..9741dc9 100644
@@ -43,7 +43,7 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
                              uint32_t *evtchn_port,
                              mem_event_back_ring_t *back_ring)
 {
-    int rc;
+    int rc, err;
     uint64_t pfn;
     xen_pfn_t ring_pfn, mmap_pfn;
 
@@ -52,9 +52,9 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
 
     ring_pfn = pfn;
     mmap_pfn = ring_pfn;
-    ring_page = xc_map_foreign_batch(xch, domain_id,
-                                        PROT_READ | PROT_WRITE, &mmap_pfn, 1);
-    if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+    ring_page = xc_map_foreign_bulk(xch, domain_id,
+                                    PROT_READ | PROT_WRITE, &mmap_pfn, &err, 1);
+    if ( (err != 0) || (ring_page == NULL) )
     {
         /* Map failed, populate ring page */
         rc = xc_domain_populate_physmap_exact(xch, domain_id,
@@ -66,10 +66,10 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         }
 
         mmap_pfn = ring_pfn;
-        ring_page = xc_map_foreign_batch(xch, domain_id,
-                                        PROT_READ | PROT_WRITE, &mmap_pfn, 1);
+        ring_page = xc_map_foreign_bulk(xch, domain_id, PROT_READ | PROT_WRITE,
+                                        &mmap_pfn, &err, 1);
 
-        if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+        if ( (err != 0) || (ring_page == NULL) )
         {
             PERROR("Could not map the ring page\n");
             return -1;
Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 3/3 tools/libxc/xc_mem_paging_setup.c: FIX- Race condition between initializing shared ring and mempaging.

In reply to Dushyant Behl

This patch is meant to fix a known race condition bug in mempaging ring setup routines. The race condition was between initializing mem paging and initializing shared ring, earlier the code initialized mem paging before removing the ring page from guest's physical map which could enable the guest to interfere with the ring initialisation. Now the code removes the page from the guest's physical map before enabling mempaging so that the guest cannot clobber the ring after we initialise it.

---
 tools/libxc/xc_mem_paging_setup.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
tools/libxc/xc_mem_paging_setup.c +25 -4
index 9741dc9..12469b4 100644
@@ -76,6 +76,22 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         }
     }
 
+    /* Clear the ring_pfn */
+    memset(ring_page, 0, PAGE_SIZE);
+
+    rc = xc_domain_pause(xch, domain_id);
+    if ( rc != 0 )
+    {
+        PERROR("Unable to pause domain");
+        return -1;
+    }
+    DPRINTF("Domain pause successful");
+
+    /* Initialise ring */
+    SHARED_RING_INIT((mem_event_sring_t *)ring_page);
+    BACK_RING_INIT(back_ring, (mem_event_sring_t *)ring_page, PAGE_SIZE);
+    DPRINTF("ininialized shared ring");
+
     /* Initialise Xen */
     rc = xc_mem_paging_enable(xch, domain_id, evtchn_port);
     if ( rc != 0 )
@@ -99,6 +115,7 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         }
         return -1;
     }
+    DPRINTF("enabled mempaging");
 
     /* Bind event notification */
     rc = xc_evtchn_bind_interdomain(xce_handle, domain_id, *evtchn_port);
@@ -109,10 +126,6 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
     }
     *port = rc;
 
-    /* Initialise ring */
-    SHARED_RING_INIT((mem_event_sring_t *)ring_page);
-    BACK_RING_INIT(back_ring, (mem_event_sring_t *)ring_page, PAGE_SIZE);
-
     /* Now that the ring is set, remove it from the guest's physmap */
     if ( xc_domain_decrease_reservation_exact(xch, domain_id, 1, 0, &ring_pfn) )
     {
@@ -120,6 +133,14 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         return -1;
     }
 
+    rc = xc_domain_unpause(xch, domain_id);
+    if ( rc != 0 )
+    {
+        PERROR("Unable to unpause domain");
+        return -1;
+    }
+    DPRINTF("Domain unpause successful");
+
     return 0;
 }
 
Signature · 1 line
1.9.1
#4 lore raw

Re: [PATCH v3 3/3] tools/libxc/xc_mem_paging_setup.c: FIX- Race condition between initializing shared ring and mempaging.

Quoted 9 lines · Dushyant Behl

This patch is meant to fix a known race condition bug in mempaging ring setup routines. The race condition was between initializing mem paging and initializing shared ring, earlier the code initialized mem paging before removing the ring page from guest's physical map which could enable the guest to interfere with the ring initialisation. Now the code removes the page from the guest's physical map before enabling mempaging so that the guest cannot clobber the ring after we initialise it.

Quoted 4 lines

(+ IanC)

This patch has a few problems, but raises a couple interesting points about xc_mem_event_enable. See below:

---

Quoted 9 lines
 tools/libxc/xc_mem_paging_setup.c | 29 +++++++++++++++++++++++++----
 1 file changed, 25 insertions(+), 4 deletions(-)
tools/libxc/xc_mem_paging_setup.c +0 -0
index 9741dc9..12469b4 100644

tools/libxc/xc_mem_paging_setup.c int xc_mem_paging_ring_setup(xc_interface *xch, · line 81

Quoted 6 lines
tools/libxc/xc_mem_paging_setup.c +2 -0
@@ -76,6 +76,22 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         }
     }
 
+    /* Clear the ring_pfn */
+    memset(ring_page, 0, PAGE_SIZE);
 

Once xc_mem_event_enable maps the ring page, surely there is no downside to clearing it out? i.e. moving this memset there should help.

+

tools/libxc/xc_mem_paging_setup.c int xc_mem_paging_ring_setup(xc_interface *xch,

Quoted 11 lines
tools/libxc/xc_mem_paging_setup.c +10 -0
+    rc = xc_domain_pause(xch, domain_id);
+    if ( rc != 0 )
+    {
+        PERROR("Unable to pause domain");
+        return -1;
+    }
+    DPRINTF("Domain pause successful");
+
+    /* Initialise ring */
+    SHARED_RING_INIT((mem_event_sring_t *)ring_page);
 

Likewise. Once xc_mem_event_enable has mapped (and cleared) the ring page, and since it's holding the domain paused, there is no downside in applying SHARED_RING_INIT, methinks. Every consumer of xc_mem_event_enable is bound to do that. And doing it prior to enabling mem event and unpausing the domain ensures Xen will have a proper ring to place events at.

If those two make sense, then we can cut a patch against xc_mem_event_enable.

Thanks
Andres

Quoted 42 lines
tools/libxc/xc_mem_paging_setup.c +4 -4
+    BACK_RING_INIT(back_ring, (mem_event_sring_t *)ring_page, PAGE_SIZE);
+    DPRINTF("ininialized shared ring");
+
     /* Initialise Xen */
     rc = xc_mem_paging_enable(xch, domain_id, evtchn_port);
     if ( rc != 0 )
@@ -99,6 +115,7 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         }
         return -1;
     }
+    DPRINTF("enabled mempaging");
 
     /* Bind event notification */
     rc = xc_evtchn_bind_interdomain(xce_handle, domain_id, *evtchn_port);
@@ -109,10 +126,6 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
     }
     *port = rc;
 
-    /* Initialise ring */
-    SHARED_RING_INIT((mem_event_sring_t *)ring_page);
-    BACK_RING_INIT(back_ring, (mem_event_sring_t *)ring_page, PAGE_SIZE);
-
     /* Now that the ring is set, remove it from the guest's physmap */
     if ( xc_domain_decrease_reservation_exact(xch, domain_id, 1, 0,
 &ring_pfn) )
     {
tools/libxc/xc_mem_paging_setup.c +8 -1
@@ -120,6 +133,14 @@ int xc_mem_paging_ring_setup(xc_interface *xch,
         return -1;
     }
 
+    rc = xc_domain_unpause(xch, domain_id);
+    if ( rc != 0 )
+    {
+        PERROR("Unable to unpause domain");
+        return -1;
+    }
+    DPRINTF("Domain unpause successful");
+
     return 0;
 }
 
--
Quoted 3 lines

1.9.1

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

#5 lore raw
In reply to Dushyant Behl

Hi Everyone,

I am extremely sorry that I didn't notice these patches had most of the code which did exactly what xc_mem_event_enable (added by XSA-99) did. I request you to kindly ignore this patch series, I have prepared a new patch series which takes some necessary bits and pieces from this patch series and adds them to xc_mem_event_enable.
I'll be sending the new patch series, Please have a look at those.

I am really sorry for the inconvenience.

Thanks and Regards,
Dushyant Behl

Quoted 20 lines · Dushyant Behl

The following patches refactor memepaging code from xenpaging to libxc -

[PATCH v3 1/3] tools/libxc: refactored mempaging code from xenpaging This patch adds two routines xc_mem_paging_ring_setup and xc_mem_paging_ring_teardown to libxc at the path tools/libxc/xc_mem_paging_setup.c, which is a new file.

[PATCH v3 2/3] tools/libxc/xc_mem_paging_setup.c: replacing deprecated functions
Replaces calls to deprecated function xc_map_foreign_batch with calls to xc_map_foreign_bulk.

[PATCH v3 3/3] tools/libxc/xc_mem_paging_setup.c: FIX- Race condition Fix for a known race condition bug in mempaging ring setup code, this race condition is actually simmilar to that concerned by XSA-99. This patch tries to
take care of all the points mentioned in XSA-99, along with clearing the ringpage
to remove any bogus input.

Quoted 2 lines

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

#6 lore raw

The updated patch series is present at the following thread -
http://lists.xenproject.org/archives/html/xen-devel/2014-08/msg01609.html.

Thanks and Regards,
Dushyant Behl

On Wed, Aug 13, 2014 at 9:58 PM, Dushyant Behl <[email protected]

Quoted 11 lines

wrote:

Hi Everyone,

I am extremely sorry that I didn't notice these patches had most of the code which did exactly what xc_mem_event_enable (added by XSA-99) did. I request you to kindly ignore this patch series, I have prepared a new patch series which takes some necessary bits and pieces from this patch series and adds them to xc_mem_event_enable.
I'll be sending the new patch series, Please have a look at those.

Quoted 33 lines

I am really sorry for the inconvenience.

Thanks and Regards,
Dushyant Behl

Quoted 22 lines · Dushyant Behl

The following patches refactor memepaging code from xenpaging to libxc -

[PATCH v3 1/3] tools/libxc: refactored mempaging code from xenpaging This patch adds two routines xc_mem_paging_ring_setup and xc_mem_paging_ring_teardown to libxc at the path tools/libxc/xc_mem_paging_setup.c, which is a new file.

[PATCH v3 2/3] tools/libxc/xc_mem_paging_setup.c: replacing deprecated functions
Replaces calls to deprecated function xc_map_foreign_batch with calls to xc_map_foreign_bulk.

[PATCH v3 3/3] tools/libxc/xc_mem_paging_setup.c: FIX- Race condition Fix for a known race condition bug in mempaging ring setup code, this race condition is actually simmilar to that concerned by XSA-99. This patch tries to
take care of all the points mentioned in XSA-99, along with clearing the ringpage
to remove any bogus input.

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