Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Cleaned up deinitialization of VMM and free of VMs
[palacios.git] / linux_module / mm.c
index 01d4b75..c5ec916 100644 (file)
@@ -13,6 +13,7 @@
 #include "mm.h"
 #include "buddy.h"
 #include "numa.h"
+#include "palacios/vmm.h"
 
 
 static struct buddy_memzone ** memzones = NULL;
@@ -20,9 +21,20 @@ static uintptr_t * seed_addrs = NULL;
 
 
 // alignment is in bytes
-uintptr_t alloc_palacios_pgs(u64 num_pages, u32 alignment, int node_id) {
+uintptr_t alloc_palacios_pgs(u64 num_pages, u32 alignment, int node_id, int constraints) {
     uintptr_t addr = 0; 
     int any = node_id==-1; // can allocate on any
+    int buddy_constraints=0;
+
+    if (constraints && constraints!=V3_ALLOC_PAGES_CONSTRAINT_4GB) { 
+       ERROR("Unknown constraint mask 0x%x\n",constraints);
+       return 0;
+    }
+    
+    if (constraints & V3_ALLOC_PAGES_CONSTRAINT_4GB) { 
+       buddy_constraints |= LWK_BUDDY_CONSTRAINT_4GB;
+    }
+
 
     if (node_id == -1) {
        int cpu_id = get_cpu();
@@ -38,14 +50,14 @@ uintptr_t alloc_palacios_pgs(u64 num_pages, u32 alignment, int node_id) {
        return 0;
     }
 
-    addr = buddy_alloc(memzones[node_id], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
+    addr = buddy_alloc(memzones[node_id], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT, buddy_constraints);
 
     if (!addr && any) { 
        int i;
        // do a scan to see if we can satisfy request on any node
        for (i=0; i< numa_num_nodes(); i++) { 
            if (i!=node_id) { 
-               addr = buddy_alloc(memzones[i], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
+               addr = buddy_alloc(memzones[i], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT, buddy_constraints);
                if (addr) {
                    break;
                }
@@ -132,12 +144,16 @@ int add_palacios_memory(struct v3_mem_region *r) {
        return -1;
     }
 
+    if ((node_id != r->node) && (r->node!=-1)) { 
+       INFO("Memory add request is for node %d, but memory is in node %d\n",r->node,node_id);
+    }
+
     pool_order = get_order(r->num_pages * PAGE_SIZE) + PAGE_SHIFT;
 
     if (buddy_add_pool(memzones[node_id], r->base_addr, pool_order, keep)) {
        ERROR("ALERT ALERT ALERT Unable to add pool to buddy allocator...\n");
        if (r->type==REQUESTED || r->type==REQUESTED32) { 
-           free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages));
+           free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
        }
        palacios_free(keep);
        return -1;
@@ -148,27 +164,47 @@ int add_palacios_memory(struct v3_mem_region *r) {
 
 
 
-int palacios_remove_memory(uintptr_t base_addr) {
-    int node_id = numa_addr_to_node(base_addr);
+int remove_palacios_memory(struct v3_mem_region *req) {
+    int node_id = numa_addr_to_node(req->base_addr);
     struct v3_mem_region *r;
 
-    if (buddy_remove_pool(memzones[node_id], base_addr, 0, (void**)(&r))) { //unforced remove
-       ERROR("Cannot remove memory at base address 0x%p because it is in use\n", (void*)base_addr);
+    if (buddy_remove_pool(memzones[node_id], req->base_addr, 0, (void**)(&r))) { //unforced remove
+       ERROR("Cannot remove memory at base address 0x%p\n", (void*)(req->base_addr));
        return -1;
     }
 
-    if (r->type==REQUESTED || r->type==REQUESTED32) { 
-       free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages));
-    } else {
-       // user space resposible for onlining
+    if (r) {
+       if (r->type==REQUESTED || r->type==REQUESTED32) { 
+           free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
+       } else {
+           // user space responsible for onlining
+       }
+       palacios_free(r);
     }
-    
-    palacios_free(r);
 
     return 0;
 }
 
 
+static int handle_free(void *meta)
+{
+    struct v3_mem_region *r = (struct v3_mem_region *)meta;
+
+    if (r) { 
+       if (r->type==REQUESTED || r->type==REQUESTED32) { 
+           //INFO("Freeing %llu pages at %p\n",r->num_pages,(void*)(r->base_addr));
+           free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
+       } else {
+           // user space responsible for onlining
+       }
+       palacios_free(r);
+    }
+    
+    return 0;
+}
+
+       
+
 
 int palacios_deinit_mm( void ) {
 
@@ -178,7 +214,8 @@ int palacios_deinit_mm( void ) {
        for (i = 0; i < numa_num_nodes(); i++) {
            
            if (memzones[i]) {
-               buddy_deinit(memzones[i]);
+               INFO("Deiniting memory zone %d\n",i);
+               buddy_deinit(memzones[i],handle_free);
            }
            
            // note that the memory is not onlined here - offlining and onlining
@@ -186,6 +223,7 @@ int palacios_deinit_mm( void ) {
            
            if (seed_addrs[i]) {
                // free the seed regions
+               INFO("Freeing seed addrs %d\n",i);
                free_pages((uintptr_t)__va(seed_addrs[i]), MAX_ORDER - 1);
            }
        }
@@ -203,7 +241,7 @@ int palacios_init_mm( void ) {
 
     INFO("memory manager init: MAX_ORDER=%d (%llu bytes)\n",MAX_ORDER, PAGE_SIZE*pow2(MAX_ORDER));
 
-    memzones = palacios_alloc_extended(sizeof(struct buddy_memzone *) * num_nodes, GFP_KERNEL);
+    memzones = palacios_alloc_extended(sizeof(struct buddy_memzone *) * num_nodes, GFP_KERNEL,-1);
 
     if (!memzones) { 
        ERROR("Cannot allocate space for memory zones\n");
@@ -213,7 +251,7 @@ int palacios_init_mm( void ) {
 
     memset(memzones, 0, sizeof(struct buddy_memzone *) * num_nodes);
 
-    seed_addrs = palacios_alloc_extended(sizeof(uintptr_t) * num_nodes, GFP_KERNEL);
+    seed_addrs = palacios_alloc_extended(sizeof(uintptr_t) * num_nodes, GFP_KERNEL,-1);
 
     if (!seed_addrs) { 
        ERROR("Cannot allocate space for seed addrs\n");
@@ -238,7 +276,7 @@ int palacios_init_mm( void ) {
            pgs = alloc_pages_node(node_id, GFP_DMA32, MAX_ORDER - 1);
 
            if (!pgs) {
-               INFO("Could not allocate initial memory block for node %d beloew 4GB\n", node_id);
+               INFO("Could not allocate initial memory block for node %d below 4GB\n", node_id);
                
                pgs = alloc_pages_node(node_id, GFP_KERNEL, MAX_ORDER - 1);