Skip to contentSkip to conversation

libxc: refactor memory allocation functions

PATCH Applied Wei Liu 3 participants 9 messages Dec 1, 2015

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 libxc: refactor memory allocation functions

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

We don't have INVALID_PFN, maybe we need one?

Tested with Jessie with 64 bit pvgrub, Wheezy with 64 bit pvgrub, Wheezy pv guest, Wheezy HVM guest, Wheezy HVM with qemu stubdom.

 tools/libxc/include/xc_dom.h |  2 +-
 tools/libxc/xc_dom_core.c    | 16 +++++++---------
 2 files changed, 8 insertions(+), 10 deletions(-)
tools/libxc/include/xc_dom.h +1 -1
index 2176216..2d0de8c 100644
@@ -350,7 +350,7 @@ char *xc_dom_strdup(struct xc_dom_image *dom, const char *str);
 
 /* --- alloc memory pool ------------------------------------------- */
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
 int xc_dom_alloc_segment(struct xc_dom_image *dom,
                          struct xc_dom_seg *seg, char *name,
                          xen_vaddr_t start, xen_vaddr_t size);
tools/libxc/xc_dom_core.c +7 -9
index 5d6c3ba..8967970 100644
@@ -555,6 +555,9 @@ static int xc_dom_chk_alloc_pages(struct xc_dom_image *dom, char *name,
     dom->pfn_alloc_end += pages;
     dom->virt_alloc_end += pages * page_size;
 
+    if ( dom->allocate )
+        dom->allocate(dom);
+
     return 0;
 }
 
@@ -602,9 +605,6 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     if ( xc_dom_chk_alloc_pages(dom, name, pages) )
         return -1;
 
-    if (dom->allocate)
-        dom->allocate(dom);
-
     /* map and clear pages */
     ptr = xc_dom_seg_to_ptr(dom, seg);
     if ( ptr == NULL )
@@ -621,18 +621,16 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     return 0;
 }
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
 {
-    unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
     xen_vaddr_t start;
     xen_pfn_t pfn;
 
     start = dom->virt_alloc_end;
     pfn = dom->pfn_alloc_end - dom->rambase_pfn;
-    dom->virt_alloc_end += page_size;
-    dom->pfn_alloc_end++;
-    if ( dom->allocate )
-        dom->allocate(dom);
+
+    if ( xc_dom_chk_alloc_pages(dom, name, 1) )
+        return (xen_pfn_t)-1;
 
     DOMPRINTF("%-20s:   %-12s : 0x%" PRIx64 " (pfn 0x%" PRIpfn ")",
               __FUNCTION__, name, start, pfn);
Signature · 1 line
2.1.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

Quoted 7 lines

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Quoted 75 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

We don't have INVALID_PFN, maybe we need one?

Tested with Jessie with 64 bit pvgrub, Wheezy with 64 bit pvgrub, Wheezy pv guest, Wheezy HVM guest, Wheezy HVM with qemu stubdom.

 tools/libxc/include/xc_dom.h |  2 +-
 tools/libxc/xc_dom_core.c    | 16 +++++++---------
 2 files changed, 8 insertions(+), 10 deletions(-)
tools/libxc/include/xc_dom.h +1 -1
index 2176216..2d0de8c 100644
@@ -350,7 +350,7 @@ char *xc_dom_strdup(struct xc_dom_image *dom, const char *str);
 
 /* --- alloc memory pool ------------------------------------------- */
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
 int xc_dom_alloc_segment(struct xc_dom_image *dom,
                          struct xc_dom_seg *seg, char *name,
                          xen_vaddr_t start, xen_vaddr_t size);
tools/libxc/xc_dom_core.c +7 -9
index 5d6c3ba..8967970 100644
@@ -555,6 +555,9 @@ static int xc_dom_chk_alloc_pages(struct xc_dom_image *dom, char *name,
     dom->pfn_alloc_end += pages;
     dom->virt_alloc_end += pages * page_size;
 
+    if ( dom->allocate )
+        dom->allocate(dom);
+
     return 0;
 }
 
@@ -602,9 +605,6 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     if ( xc_dom_chk_alloc_pages(dom, name, pages) )
         return -1;
 
-    if (dom->allocate)
-        dom->allocate(dom);
-
     /* map and clear pages */
     ptr = xc_dom_seg_to_ptr(dom, seg);
     if ( ptr == NULL )
@@ -621,18 +621,16 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
     return 0;
 }
 
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
 {
-    unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
     xen_vaddr_t start;
     xen_pfn_t pfn;
 
     start = dom->virt_alloc_end;
     pfn = dom->pfn_alloc_end - dom->rambase_pfn;
-    dom->virt_alloc_end += page_size;
-    dom->pfn_alloc_end++;
-    if ( dom->allocate )
-        dom->allocate(dom);
+
+    if ( xc_dom_chk_alloc_pages(dom, name, 1) )
+        return (xen_pfn_t)-1;
 
     DOMPRINTF("%-20s:   %-12s : 0x%" PRIx64 " (pfn 0x%" PRIpfn ")",
               __FUNCTION__, name, start, pfn);
Applied
#2 lore raw
Quoted 15 lines · Juergen Gross
Quoted 14 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
   xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Quoted 7 lines
Quoted 5 lines

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return
an invalid pfn when xc_dom_chk_alloc_pages fails.

Applied, thanks.

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

Acked-by Tested-by on v1 1/1
#3 lore raw
In reply to Wei Liu
Quoted 10 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
   xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

Quoted 5 lines

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Given this presumably the handful of callers ought to gain some error handling in a followup patch?

xc_dom_chk_alloc_pages does log, so at least the callers needn't bother with that.

Quoted 1 line
Quoted 6 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

We don't have INVALID_PFN, maybe we need one?

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

Quoted 4 lines

Tested with Jessie with 64 bit pvgrub, Wheezy with 64 bit pvgrub, Wheezy pv
guest, Wheezy HVM guest, Wheezy HVM with qemu stubdom.

Also by me in my previously failing environment =>

I think there are two more fixes in this space:
libxc: correct domain builder for 64 bit guest with 32 bit tools
libxc: use correct return type for do_memory_op()

I intend to pick up all three for commit in a moment, prod me if there are others.

Quoted 59 lines

 tools/libxc/include/xc_dom.h |  2 +- tools/libxc/xc_dom_core.c    | 16 +++++++---------

 2 files changed, 8 insertions(+), 10 deletions(-)
tools/libxc/include/xc_dom.h +0 -0
index 2176216..2d0de8c 100644
@@ -350,7 +350,7 @@ char *xc_dom_strdup(struct xc_dom_image *dom, const
 char *str);


 /* --- alloc memory pool ------------------------------------------- */

tools/libxc/include/xc_dom.h +1 -1
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name);
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name);

 int xc_dom_alloc_segment(struct xc_dom_image *dom, struct xc_dom_seg *seg, char *name, xen_vaddr_t start, xen_vaddr_t size);

tools/libxc/xc_dom_core.c +0 -0
index 5d6c3ba..8967970 100644
@@ -555,6 +555,9 @@ static int xc_dom_chk_alloc_pages(struct xc_dom_image
 *dom, char *name,
      dom->pfn_alloc_end += pages;
      dom->virt_alloc_end += pages * page_size;


+    if ( dom->allocate )
+        dom->allocate(dom);
+
     return 0;
 }

tools/libxc/xc_dom_core.c +0 -0
@@ -602,9 +605,6 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
      if ( xc_dom_chk_alloc_pages(dom, name, pages) )
          return -1;


-    if (dom->allocate)
-        dom->allocate(dom);
-
     /* map and clear pages */
     ptr = xc_dom_seg_to_ptr(dom, seg);
     if ( ptr == NULL )

tools/libxc/xc_dom_core.c +0 -0
@@ -621,18 +621,16 @@ int xc_dom_alloc_segment(struct xc_dom_image *dom,
      return 0;
  }

tools/libxc/xc_dom_core.c +1 -1
-int xc_dom_alloc_page(struct xc_dom_image *dom, char *name)
+xen_pfn_t xc_dom_alloc_page(struct xc_dom_image *dom, char *name)

 {
-    unsigned int page_size = XC_DOM_PAGE_SIZE(dom); xen_vaddr_t start;
     xen_pfn_t pfn;

     start = dom->virt_alloc_end;
     pfn = dom->pfn_alloc_end - dom->rambase_pfn; -    dom->virt_alloc_end += page_size;
-    dom->pfn_alloc_end++;

Quoted 8 lines

-    if ( dom->allocate )
-        dom->allocate(dom);
+
+    if ( xc_dom_chk_alloc_pages(dom, name, 1) )
+        return (xen_pfn_t)-1;

     DOMPRINTF("%-20s:   %-12s : 0x%" PRIx64 " (pfn 0x%" PRIpfn ")", __FUNCTION__, name, start, pfn);

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

author
#4 lore raw
Quoted 11 lines · Ian Campbell
Quoted 10 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
   xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

Quoted 9 lines
Quoted 5 lines

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Given this presumably the handful of callers ought to gain some error handling in a followup patch?

Yes, that's for sure.

Quoted 12 lines

xc_dom_chk_alloc_pages does log, so at least the callers needn't bother with that.

Quoted 1 line
Quoted 5 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

Quoted 5 lines
Quoted 1 line

We don't have INVALID_PFN, maybe we need one?

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

Yeah, I'm aware of those but they don't seem to fit because they are used in MFN space.

Quoted 7 lines
Quoted 4 lines

Tested with Jessie with 64 bit pvgrub, Wheezy with 64 bit pvgrub, Wheezy pv
guest, Wheezy HVM guest, Wheezy HVM with qemu stubdom.

Also by me in my previously failing environment =>

Quoted 9 lines

I think there are two more fixes in this space:
libxc: correct domain builder for 64 bit guest with 32 bit tools
libxc: use correct return type for do_memory_op()

I intend to pick up all three for commit in a moment, prod me if there are others.

This one and the other two are all that I'm aware of at the moment.

Wei.

#5 lore raw
Quoted 9 lines · Wei Liu

Quoted 5 lines
Quoted 1 line

We don't have INVALID_PFN, maybe we need one?

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

Yeah, I'm aware of those but they don't seem to fit because they are used in MFN space.

Right, I wasn't sure about the latter, since given a P2M_ENTRY is an MFN I then don't see how/why it is distinct from INVALID_MFN. Perhaps I'm missing something (guest p2m width perhaps?), or maybe it's just historical baggage.

Ian.

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

#6 lore raw
In reply to Ian Campbell
Quoted 11 lines · Ian Campbell
Quoted 10 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

Quoted 8 lines
Quoted 5 lines

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Given this presumably the handful of callers ought to gain some error handling in a followup patch?

I could do that. Wei?

Quoted 13 lines

xc_dom_chk_alloc_pages does log, so at least the callers needn't bother with that.

Quoted 1 line
Quoted 5 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

Quoted 4 lines
Quoted 1 line

We don't have INVALID_PFN, maybe we need one?

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

What about merging all of them into INVALID_FRAME?

I can create a patch(-series).

Juergen

author
#7 lore raw
Quoted 15 lines · Juergen Gross
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Given this presumably the handful of callers ought to gain some error handling in a followup patch?

I could do that. Wei?

You're welcome to pick that up.

Quoted 13 lines
Quoted 13 lines

xc_dom_chk_alloc_pages does log, so at least the callers needn't bother with that.

Quoted 1 line
Quoted 5 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

We don't have INVALID_PFN, maybe we need one?

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

What about merging all of them into INVALID_FRAME?

Note that INVALID_MFN is in public header (xenctrl.h) while INVALID_P2M_ENTRY is not.

In fact (xen_pfn_t)-1 is part of guest ABI so we should properly export and document it. The current situation is not ideal. Not sure how much yak shaving is required though.

Wei.

Quoted 5 lines

I can create a patch(-series).

Juergen

#8 lore raw
Quoted 42 lines · Wei Liu
Quoted 15 lines · Juergen Gross
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Wei Liu

There were some problems with the original memory allocation functions:
1. xc_dom_alloc_segment and xc_dom_alloc_pad ended up calling xc_dom_chk_alloc_pages while xc_dom_alloc_page open-coded everything.
2. xc_dom_alloc_pad didn't call dom->allocate.

Refactor the code so that:
1. xc_dom_alloc_{segment,pad,page} end up calling
xc_dom_chk_alloc_pages.
2. xc_dom_chk_alloc_pages calls dom->allocate.

This way we avoid scattering dom->allocate over multiple locations and open-coding.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Also change the return type of xc_dom_alloc_page to xen_pfn_t and return an invalid pfn when xc_dom_chk_alloc_pages fails.

Given this presumably the handful of callers ought to gain some error handling in a followup patch?

I could do that. Wei?

You're welcome to pick that up.

Quoted 15 lines
Quoted 15 lines

xc_dom_chk_alloc_pages does log, so at least the callers needn't bother with that.

Quoted 1 line
Quoted 6 lines

---
Cc: Ian Campbell <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Juergen Gross <[email protected]>

We don't have INVALID_PFN, maybe we need one?

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

We have INVALID_MFN and INVALID_P2M_ENTRY, not sure if the latter fits the bill (or if not how it is distinct from the former).

What about merging all of them into INVALID_FRAME?

Note that INVALID_MFN is in public header (xenctrl.h) while INVALID_P2M_ENTRY is not.

INVALID_P2M_ENTRY in libxc is defined as (xen_pfn_t)-1. I think it would make sense to modify that to INVALID_PFN.

Quoted 3 lines

In fact (xen_pfn_t)-1 is part of guest ABI so we should properly export and document it. The current situation is not ideal. Not sure how much yak shaving is required though.

As this relates to the return value checking of the domain builder memory allocating I'll have a try, too.

Juergen