[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Minios-devel] [UNIKRAFT PATCH 5/5] lib/ukallocbbuddy: Allocator Ballooning Use



From: Cason Schindler <cason.j.schindler@xxxxxxxxx>

Modify ukallocbbuddy to use memory ballooning API.

Signed-off-by: Jack Raney <raney.jack99@xxxxxxxxx>
Signed-off-by: Cason Schindler <cason.j.schindler@xxxxxxxxx>
---
 lib/ukallocbbuddy/bbuddy.c                 | 104 ++++++++++++++++++++-
 lib/ukallocbbuddy/include/uk/allocbbuddy.h |   2 +
 2 files changed, 104 insertions(+), 2 deletions(-)

diff --git a/lib/ukallocbbuddy/bbuddy.c b/lib/ukallocbbuddy/bbuddy.c
index 7de3b84..4a3641e 100644
--- a/lib/ukallocbbuddy/bbuddy.c
+++ b/lib/ukallocbbuddy/bbuddy.c
@@ -48,6 +48,10 @@
 #include <uk/assert.h>
 #include <uk/page.h>
 
+#include <uk/plat/memory.h>
+
+int using_balloon = 0;
+
 typedef struct chunk_head_st chunk_head_t;
 typedef struct chunk_tail_st chunk_tail_t;
 
@@ -224,6 +228,40 @@ static ssize_t bbuddy_availmem(struct uk_alloc *a)
 }
 #endif
 
+/*********************
+ * BALLOON SUPPORT
+ */
+
+/**
+ * Initializes the balloon once it is ready. Immediate for Xen,
+ * but later for KVM driver
+*/
+void balloon_init(struct uk_alloc *a)
+{
+       struct uk_bbpalloc *b;
+       size_t i;
+       int order;
+       chunk_head_t *chunk;
+       b = (struct uk_bbpalloc *)&a->priv;
+       if (using_balloon != 0) {
+               return;
+       }
+
+       for (i = 0; i < FREELIST_SIZE; i++) {
+               if (!FREELIST_EMPTY(b->free_head[i])) {
+                       chunk = b->free_head[i];
+                       order = chunk->level;
+               int r = ukplat_inflate((void*)chunk, order );
+               if (r < 0) {
+                               /* The balloon is ready but failed for another 
reason. */
+                               return;
+                       }
+               }
+
+       }
+       using_balloon = 1;
+}
+
 /*********************
  * BINARY BUDDY PAGE ALLOCATOR
  */
@@ -271,6 +309,24 @@ static void *bbuddy_palloc(struct uk_alloc *a, size_t 
order)
        }
        map_alloc(b, (uintptr_t)alloc_ch, 1UL << order);
 
+       /* Remove the chunk from the balloon */
+       int r = ukplat_deflate((void*)alloc_ch, (int)order);
+       if (r < 0) {
+               if (r == -ENXIO) {
+                       /* The balloon isnt ready yet! We don't need to do 
anything */
+               } else if (r == -ENOSYS) {
+                       /* deflation not implemented */
+               } else {
+                       /* The balloon is ready but failed for another reason. 
*/
+                       return r;
+               }
+       } else if (using_balloon == 0) {
+               /* Deflate succeeded for the first time. The balloon driver is 
now ready!
+                * We need to move all of the freelist data to the balloon
+                */
+               balloon_init(a);
+       }
+
        return ((void *)alloc_ch);
 
 no_memory:
@@ -296,6 +352,25 @@ static void bbuddy_pfree(struct uk_alloc *a, void *obj, 
size_t order)
        /* First free the chunk */
        map_free(b, (uintptr_t)obj, 1UL << order);
 
+       /* Add the free chunk to the balloon */
+       int r = ukplat_inflate((void*)obj, (int)order);
+       if (r < 0) {
+               if (r == -ENXIO) {
+                       /* The balloon isnt ready yet! We don't need to do 
anything */
+               } else if (r == -ENOSYS) {
+                       /* inflateion not implemented */
+               } else {
+                       /* The balloon is ready but failed for another reason. 
*/
+                       return r;
+               }       
+
+       } else if (using_balloon == 0) {
+               /* Inflate succeeded for the first time. The balloon driver is 
now ready!
+                * We need to move all of the free list data to the balloon 
+                */
+               balloon_init(a);
+       }
+
        /* Create free chunk */
        freed_ch = (chunk_head_t *)obj;
        freed_ct = (chunk_tail_t *)((char *)obj
@@ -409,8 +484,8 @@ static int bbuddy_addmem(struct uk_alloc *a, void *base, 
size_t len)
        memset(memr->mm_alloc_bitmap, (unsigned char) ~0,
                        memr->mm_alloc_bitmap_size);
 
-       /* free up the memory we've been given to play with */
-       map_free(b, min, memr->nr_pages);
+
+       /* Do not call map_free; instead inflate: */
 
        count = 0;
        while (range != 0) {
@@ -426,6 +501,31 @@ static int bbuddy_addmem(struct uk_alloc *a, void *base, 
size_t len)
                            (uintptr_t)a, min, (uintptr_t)(min + (1UL << i)),
                            (i - __PAGE_SHIFT));
 
+               /*
+                * For each free block we should attempt to add it to the 
balloon! 
+                * Afterwards, if successful, all of our usable memory will be 
in the balloon. 
+                * It will become accessible by calling deflate in ukpalloc!
+                *
+                * If the balloon device has not yet been added then the 
allocator will proceed 
+                * without ballooning suport until the device has been added 
(KVM)
+                */
+               int r = ukplat_inflate((void*)min, (int)(i - __PAGE_SHIFT));
+               if (r < 0) {
+                       if (r == -ENXIO) {
+                               /* The balloon isnt ready yet! We don't need to 
do anything */
+                       } else if (r == -ENOSYS) {
+                               /* inflation not implemented */
+                       } else {
+                               /* The balloon is ready but failed for another 
reason. */
+                               return r;
+                       }
+               } else if (using_balloon == 0) {
+                       /* Inflate succeeded for the first time. The balloon 
driver is now ready!
+                        * We need to move all of the free list data to the 
balloon
+                        */
+                       balloon_init(a);
+               }
+
                ch = (chunk_head_t *)min;
                min += 1UL << i;
                range -= 1UL << i;
diff --git a/lib/ukallocbbuddy/include/uk/allocbbuddy.h 
b/lib/ukallocbbuddy/include/uk/allocbbuddy.h
index d7afef4..a3371c8 100644
--- a/lib/ukallocbbuddy/include/uk/allocbbuddy.h
+++ b/lib/ukallocbbuddy/include/uk/allocbbuddy.h
@@ -42,6 +42,8 @@
 extern "C" {
 #endif
 
+void balloon_init(struct uk_alloc *a);
+
 struct uk_alloc *uk_allocbbuddy_init(void *base, size_t len);
 
 #ifdef __cplusplus
-- 
2.24.0


_______________________________________________
Minios-devel mailing list
Minios-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/minios-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.