1 /* Palacios memory manager
5 #include <asm/page_64_types.h>
6 #include <linux/kernel.h>
7 #include <linux/list.h>
8 #include <linux/slab.h>
10 //static struct list_head pools;
16 #include "palacios/vmm.h"
19 static struct buddy_memzone ** memzones = NULL;
20 static uintptr_t * seed_addrs = NULL;
23 // alignment is in bytes
24 uintptr_t alloc_palacios_pgs(u64 num_pages, u32 alignment, int node_id, int constraints) {
26 int any = node_id==-1; // can allocate on any
27 int buddy_constraints=0;
29 if (constraints && constraints!=V3_ALLOC_PAGES_CONSTRAINT_4GB) {
30 ERROR("Unknown constraint mask 0x%x\n",constraints);
34 if (constraints & V3_ALLOC_PAGES_CONSTRAINT_4GB) {
35 buddy_constraints |= LWK_BUDDY_CONSTRAINT_4GB;
40 int cpu_id = get_cpu();
43 node_id = numa_cpu_to_node(cpu_id); // try first preferentially for the calling pcore
45 } else if (numa_num_nodes() == 1) {
47 } else if (node_id >= numa_num_nodes()) {
48 ERROR("Requesting memory from an invalid NUMA node. (Node: %d) (%d nodes on system)\n",
49 node_id, numa_num_nodes());
53 addr = buddy_alloc(memzones[node_id], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT, buddy_constraints);
57 // do a scan to see if we can satisfy request on any node
58 for (i=0; i< numa_num_nodes(); i++) {
60 addr = buddy_alloc(memzones[i], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT, buddy_constraints);
69 //DEBUG("Returning from alloc addr=%p, vaddr=%p\n", (void *)addr, __va(addr));
75 void free_palacios_pgs(uintptr_t pg_addr, u64 num_pages) {
76 int node_id = numa_addr_to_node(pg_addr);
78 //DEBUG("Freeing Memory page %p\n", (void *)pg_addr);
79 buddy_free(memzones[node_id], pg_addr, get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
85 unsigned long long pow2(int i)
87 unsigned long long x=1;
88 for (;i!=0;i--) { x*=2; }
92 static unsigned long long get_palacios_mem_block_size(void)
94 char *s = v3_lookup_option("mem_block_size");
97 return V3_CONFIG_MEM_BLOCK_SIZE;
99 unsigned long long temp;
101 if (strict_strtoull(s,0,&temp)) {
102 return V3_CONFIG_MEM_BLOCK_SIZE; // odd...
109 int add_palacios_memory(struct v3_mem_region *r) {
114 struct v3_mem_region *keep;
116 INFO("Palacios Memory Add Request: type=%d, node=%d, base_addr=0x%llx, num_pages=%llu\n",r->type,r->node,r->base_addr,r->num_pages);
118 // fixup request regardless of its type
119 if (r->num_pages*4096 < get_palacios_mem_block_size()) {
120 WARNING("Allocating a memory pool smaller than the Palacios block size - may not be useful\n");
123 if (pow2(get_order(r->num_pages*PAGE_SIZE)) != r->num_pages) {
124 WARNING("Allocating a memory pool that is not a power of two (is %llu) - it will be rounded down!\n", r->num_pages);
125 r->num_pages=pow2(get_order(r->num_pages*PAGE_SIZE));
126 WARNING("Rounded request is for %llu pages\n", r->num_pages);
130 if (!(keep=palacios_alloc(sizeof(struct v3_mem_region)))) {
131 ERROR("Error allocating space for tracking region\n");
136 if (r->type==REQUESTED || r->type==REQUESTED32) {
139 INFO("Attempting to allocate %llu pages of %s memory\n", r->num_pages,
140 r->type==REQUESTED ? "64 bit (unrestricted)" :
141 r->type==REQUESTED32 ? "32 bit (restricted)" : "unknown (assuming 64 bit unrestricted)");
143 pgs = alloc_pages_node(r->node,
144 r->type==REQUESTED ? GFP_KERNEL :
145 r->type==REQUESTED32 ? GFP_DMA32 : GFP_KERNEL,
146 get_order(r->num_pages*PAGE_SIZE));
148 ERROR("Unable to satisfy allocation request\n");
152 r->base_addr = page_to_pfn(pgs) << PAGE_SHIFT;
158 node_id = numa_addr_to_node(r->base_addr);
161 ERROR("Error locating node for addr %p\n", (void *)(r->base_addr));
165 if ((node_id != r->node) && (r->node!=-1)) {
166 INFO("Memory add request is for node %d, but memory is in node %d\n",r->node,node_id);
169 pool_order = get_order(r->num_pages * PAGE_SIZE) + PAGE_SHIFT;
171 if (buddy_add_pool(memzones[node_id], r->base_addr, pool_order, keep)) {
172 ERROR("ALERT ALERT ALERT Unable to add pool to buddy allocator...\n");
173 if (r->type==REQUESTED || r->type==REQUESTED32) {
174 free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
185 int remove_palacios_memory(struct v3_mem_region *req) {
186 int node_id = numa_addr_to_node(req->base_addr);
187 struct v3_mem_region *r;
189 if (buddy_remove_pool(memzones[node_id], req->base_addr, 0, (void**)(&r))) { //unforced remove
190 ERROR("Cannot remove memory at base address 0x%p\n", (void*)(req->base_addr));
195 if (r->type==REQUESTED || r->type==REQUESTED32) {
196 free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
198 // user space responsible for onlining
207 static int handle_free(void *meta)
209 struct v3_mem_region *r = (struct v3_mem_region *)meta;
212 if (r->type==REQUESTED || r->type==REQUESTED32) {
213 //INFO("Freeing %llu pages at %p\n",r->num_pages,(void*)(r->base_addr));
214 free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages*PAGE_SIZE));
216 // user space responsible for onlining
227 int palacios_deinit_mm( void ) {
232 for (i = 0; i < numa_num_nodes(); i++) {
235 INFO("Deiniting memory zone %d\n",i);
236 buddy_deinit(memzones[i],handle_free);
239 // note that the memory is not onlined here - offlining and onlining
240 // is the resposibility of the caller
243 // free the seed regions
244 INFO("Freeing seed addrs %d\n",i);
245 free_pages((uintptr_t)__va(seed_addrs[i]), MAX_ORDER - 1);
249 palacios_free(memzones);
250 palacios_free(seed_addrs);
256 int palacios_init_mm( void ) {
257 int num_nodes = numa_num_nodes();
260 INFO("memory manager init: MAX_ORDER=%d (%llu bytes)\n",MAX_ORDER, PAGE_SIZE*pow2(MAX_ORDER));
262 memzones = palacios_alloc_extended(sizeof(struct buddy_memzone *) * num_nodes, GFP_KERNEL,-1);
265 ERROR("Cannot allocate space for memory zones\n");
266 palacios_deinit_mm();
270 memset(memzones, 0, sizeof(struct buddy_memzone *) * num_nodes);
272 seed_addrs = palacios_alloc_extended(sizeof(uintptr_t) * num_nodes, GFP_KERNEL,-1);
275 ERROR("Cannot allocate space for seed addrs\n");
276 palacios_deinit_mm();
280 memset(seed_addrs, 0, sizeof(uintptr_t) * num_nodes);
282 for (node_id = 0; node_id < num_nodes; node_id++) {
283 struct buddy_memzone * zone = NULL;
285 // Seed the allocator with a small set of pages to allow initialization to complete.
286 // For now we will just grab some random pages, but in the future we will need to grab NUMA specific regions
287 // See: alloc_pages_node()
292 // attempt to first allocate below 4 GB for compatibility with
293 // 32 bit shadow paging
294 pgs = alloc_pages_node(node_id, GFP_DMA32, MAX_ORDER - 1);
297 INFO("Could not allocate initial memory block for node %d below 4GB\n", node_id);
299 pgs = alloc_pages_node(node_id, GFP_KERNEL, MAX_ORDER - 1);
302 INFO("Could not allocate initial memory block for node %d beloew 4GB\n", node_id);
304 ERROR("Could not allocate initial memory block for node %d without restrictions\n", node_id);
306 palacios_deinit_mm();
312 seed_addrs[node_id] = page_to_pfn(pgs) << PAGE_SHIFT;
315 // Initialization is done using the compile-time memory block size since
316 // at this point, we do not yet know what the run-time size is
317 zone = buddy_init(get_order(V3_CONFIG_MEM_BLOCK_SIZE) + PAGE_SHIFT, PAGE_SHIFT, node_id);
320 ERROR("Could not initialization memory management for node %d\n", node_id);
321 palacios_deinit_mm();
325 printk("Zone initialized, Adding seed region (order=%d)\n",
326 (MAX_ORDER - 1) + PAGE_SHIFT);
328 if (buddy_add_pool(zone, seed_addrs[node_id], (MAX_ORDER - 1) + PAGE_SHIFT,0)) {
329 ERROR("Could not add pool to buddy allocator\n");
330 palacios_deinit_mm();
334 memzones[node_id] = zone;