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;
18 static struct buddy_memzone ** memzones = NULL;
19 static uintptr_t * seed_addrs = NULL;
22 // alignment is in bytes
23 uintptr_t alloc_palacios_pgs(u64 num_pages, u32 alignment, int node_id) {
25 int any = node_id==-1; // can allocate on any
28 int cpu_id = get_cpu();
31 node_id = numa_cpu_to_node(cpu_id); // try first preferentially for the calling pcore
33 } else if (numa_num_nodes() == 1) {
35 } else if (node_id >= numa_num_nodes()) {
36 ERROR("Requesting memory from an invalid NUMA node. (Node: %d) (%d nodes on system)\n",
37 node_id, numa_num_nodes());
41 addr = buddy_alloc(memzones[node_id], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
45 // do a scan to see if we can satisfy request on any node
46 for (i=0; i< numa_num_nodes(); i++) {
48 addr = buddy_alloc(memzones[i], get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
57 //DEBUG("Returning from alloc addr=%p, vaddr=%p\n", (void *)addr, __va(addr));
63 void free_palacios_pgs(uintptr_t pg_addr, u64 num_pages) {
64 int node_id = numa_addr_to_node(pg_addr);
66 //DEBUG("Freeing Memory page %p\n", (void *)pg_addr);
67 buddy_free(memzones[node_id], pg_addr, get_order(num_pages * PAGE_SIZE) + PAGE_SHIFT);
73 unsigned long long pow2(int i)
75 unsigned long long x=1;
76 for (;i!=0;i--) { x*=2; }
80 int add_palacios_memory(struct v3_mem_region *r) {
84 struct v3_mem_region *keep;
86 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);
88 // fixup request regardless of its type
89 if (r->num_pages*4096 < V3_CONFIG_MEM_BLOCK_SIZE) {
90 WARNING("Allocating a memory pool smaller than the Palacios block size - may not be useful\n");
93 if (pow2(get_order(r->num_pages*PAGE_SIZE)) != r->num_pages) {
94 WARNING("Allocating a memory pool that is not a power of two (is %llu) - it will be rounded down!\n", r->num_pages);
95 r->num_pages=pow2(get_order(r->num_pages*PAGE_SIZE));
96 WARNING("Rounded request is for %llu pages\n", r->num_pages);
100 if (!(keep=palacios_alloc(sizeof(struct v3_mem_region)))) {
101 ERROR("Error allocating space for tracking region\n");
106 if (r->type==REQUESTED || r->type==REQUESTED32) {
109 INFO("Attempting to allocate %llu pages of %s memory\n", r->num_pages,
110 r->type==REQUESTED ? "64 bit (unrestricted)" :
111 r->type==REQUESTED32 ? "32 bit (restricted)" : "unknown (assuming 64 bit unrestricted)");
113 pgs = alloc_pages_node(r->node,
114 r->type==REQUESTED ? GFP_KERNEL :
115 r->type==REQUESTED32 ? GFP_DMA32 : GFP_KERNEL,
116 get_order(r->num_pages*PAGE_SIZE));
118 ERROR("Unable to satisfy allocation request\n");
122 r->base_addr = page_to_pfn(pgs) << PAGE_SHIFT;
128 node_id = numa_addr_to_node(r->base_addr);
131 ERROR("Error locating node for addr %p\n", (void *)(r->base_addr));
135 pool_order = get_order(r->num_pages * PAGE_SIZE) + PAGE_SHIFT;
137 if (buddy_add_pool(memzones[node_id], r->base_addr, pool_order, keep)) {
138 ERROR("ALERT ALERT ALERT Unable to add pool to buddy allocator...\n");
139 if (r->type==REQUESTED || r->type==REQUESTED32) {
140 free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages));
151 int palacios_remove_memory(uintptr_t base_addr) {
152 int node_id = numa_addr_to_node(base_addr);
153 struct v3_mem_region *r;
155 if (buddy_remove_pool(memzones[node_id], base_addr, 0, (void**)(&r))) { //unforced remove
156 ERROR("Cannot remove memory at base address 0x%p because it is in use\n", (void*)base_addr);
160 if (r->type==REQUESTED || r->type==REQUESTED32) {
161 free_pages((uintptr_t)__va(r->base_addr), get_order(r->num_pages));
163 // user space resposible for onlining
173 int palacios_deinit_mm( void ) {
178 for (i = 0; i < numa_num_nodes(); i++) {
181 buddy_deinit(memzones[i]);
184 // note that the memory is not onlined here - offlining and onlining
185 // is the resposibility of the caller
188 // free the seed regions
189 free_pages((uintptr_t)__va(seed_addrs[i]), MAX_ORDER - 1);
193 palacios_free(memzones);
194 palacios_free(seed_addrs);
200 int palacios_init_mm( void ) {
201 int num_nodes = numa_num_nodes();
204 INFO("memory manager init: MAX_ORDER=%d (%llu bytes)\n",MAX_ORDER, PAGE_SIZE*pow2(MAX_ORDER));
206 memzones = palacios_alloc_extended(sizeof(struct buddy_memzone *) * num_nodes, GFP_KERNEL);
209 ERROR("Cannot allocate space for memory zones\n");
210 palacios_deinit_mm();
214 memset(memzones, 0, sizeof(struct buddy_memzone *) * num_nodes);
216 seed_addrs = palacios_alloc_extended(sizeof(uintptr_t) * num_nodes, GFP_KERNEL);
219 ERROR("Cannot allocate space for seed addrs\n");
220 palacios_deinit_mm();
224 memset(seed_addrs, 0, sizeof(uintptr_t) * num_nodes);
226 for (node_id = 0; node_id < num_nodes; node_id++) {
227 struct buddy_memzone * zone = NULL;
229 // Seed the allocator with a small set of pages to allow initialization to complete.
230 // For now we will just grab some random pages, but in the future we will need to grab NUMA specific regions
231 // See: alloc_pages_node()
236 // attempt to first allocate below 4 GB for compatibility with
237 // 32 bit shadow paging
238 pgs = alloc_pages_node(node_id, GFP_DMA32, MAX_ORDER - 1);
241 INFO("Could not allocate initial memory block for node %d beloew 4GB\n", node_id);
243 pgs = alloc_pages_node(node_id, GFP_KERNEL, MAX_ORDER - 1);
246 INFO("Could not allocate initial memory block for node %d beloew 4GB\n", node_id);
248 ERROR("Could not allocate initial memory block for node %d without restrictions\n", node_id);
250 palacios_deinit_mm();
256 seed_addrs[node_id] = page_to_pfn(pgs) << PAGE_SHIFT;
259 zone = buddy_init(get_order(V3_CONFIG_MEM_BLOCK_SIZE) + PAGE_SHIFT, PAGE_SHIFT, node_id);
262 ERROR("Could not initialization memory management for node %d\n", node_id);
263 palacios_deinit_mm();
267 printk("Zone initialized, Adding seed region (order=%d)\n",
268 (MAX_ORDER - 1) + PAGE_SHIFT);
270 if (buddy_add_pool(zone, seed_addrs[node_id], (MAX_ORDER - 1) + PAGE_SHIFT,0)) {
271 ERROR("Could not add pool to buddy allocator\n");
272 palacios_deinit_mm();
276 memzones[node_id] = zone;