1 /* Copyright (c) 2007, Sandia National Laboratories */
2 /* Modified by Jack Lange, 2012 */
4 #include <linux/module.h>
5 #include <linux/slab.h>
6 #include <linux/log2.h>
10 #include <linux/proc_fs.h>
11 #include <linux/seq_file.h>
15 * Converts a block address to its block index in the specified buddy allocator.
16 * A block's index is used to find the block's tag bit, mp->tag_bits[block_id].
19 block_to_id(struct buddy_mempool *mp, struct block *block)
21 unsigned long block_id =
22 ((unsigned long)__pa(block) - mp->base_addr) >> mp->zone->min_order;
23 BUG_ON(block_id >= mp->num_blocks);
29 * Marks a block as free by setting its tag bit to one.
32 mark_available(struct buddy_mempool *mp, struct block *block)
34 __set_bit(block_to_id(mp, block), mp->tag_bits);
39 * Marks a block as allocated by setting its tag bit to zero.
42 mark_allocated(struct buddy_mempool *mp, struct block *block)
44 __clear_bit(block_to_id(mp, block), mp->tag_bits);
49 * Returns true if block is free, false if it is allocated.
52 is_available(struct buddy_mempool *mp, struct block *block)
54 return test_bit(block_to_id(mp, block), mp->tag_bits);
59 * Returns the address of the block's buddy block.
62 find_buddy(struct buddy_mempool *mp, struct block *block, unsigned long order)
67 BUG_ON((unsigned long)__pa(block) < mp->base_addr);
69 /* Fixup block address to be zero-relative */
70 _block = (unsigned long)__pa(block) - mp->base_addr;
72 /* Calculate buddy in zero-relative space */
73 _buddy = _block ^ (1UL << order);
75 /* Return the buddy's address */
76 return (void *)(_buddy + __va(mp->base_addr));
80 static inline uintptr_t pool_end_addr(struct buddy_mempool * pool) {
81 return pool->base_addr + (1UL << pool->pool_order);
85 static struct buddy_mempool *
86 find_mempool(struct buddy_memzone * zone, uintptr_t addr) {
87 struct rb_node * n = zone->mempools.rb_node;
88 struct buddy_mempool * pool = NULL;
91 pool = rb_entry(n, struct buddy_mempool, tree_node);
93 if (addr < pool->base_addr) {
95 } else if (addr >= pool_end_addr(pool)) {
108 insert_mempool(struct buddy_memzone * zone,
109 struct buddy_mempool * pool) {
110 struct rb_node ** p = &(zone->mempools.rb_node);
111 struct rb_node * parent = NULL;
112 struct buddy_mempool * tmp_pool;
116 tmp_pool = rb_entry(parent, struct buddy_mempool, tree_node);
118 if (pool_end_addr(pool) <= tmp_pool->base_addr) {
120 } else if (pool->base_addr >= pool_end_addr(tmp_pool)) {
127 rb_link_node(&(pool->tree_node), parent, p);
128 rb_insert_color(&(pool->tree_node), &(zone->mempools));
137 /* This adds a pool of a given size to a buddy allocated zone
140 int buddy_add_pool(struct buddy_memzone * zone,
141 unsigned long base_addr,
142 unsigned long pool_order,
143 void *user_metadata) {
144 struct buddy_mempool * mp = NULL;
145 unsigned long flags = 0;
148 if (pool_order > zone->max_order) {
149 ERROR("Pool order size is larger than max allowable zone size (pool_order=%lu) (max_order=%lu)\n", pool_order, zone->max_order);
151 } else if (pool_order < zone->min_order) {
152 ERROR("Pool order is smaller than min allowable zone size (pool_order=%lu) (min_order=%lu)\n", pool_order, zone->min_order);
156 mp = palacios_alloc_extended(sizeof(struct buddy_mempool), GFP_KERNEL, zone->node_id);
159 ERROR("Could not allocate mempool\n");
163 mp->base_addr = base_addr;
164 mp->pool_order = pool_order;
166 mp->num_free_blocks = 0;
168 mp->user_metadata = user_metadata;
170 /* Allocate a bitmap with 1 bit per minimum-sized block */
171 mp->num_blocks = (1UL << pool_order) / (1UL << zone->min_order);
173 mp->tag_bits = palacios_alloc_extended(
174 BITS_TO_LONGS(mp->num_blocks) * sizeof(long), GFP_KERNEL, zone->node_id
177 /* Initially mark all minimum-sized blocks as allocated */
178 bitmap_zero(mp->tag_bits, mp->num_blocks);
180 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
181 ret = insert_mempool(zone, mp);
182 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
185 ERROR("Error: Could not insert mempool into zone\n");
186 palacios_free(mp->tag_bits);
192 buddy_free(zone, base_addr, pool_order);
194 INFO("Added memory pool (addr=%p), order=%lu\n", (void *)base_addr, pool_order);
201 * Removes a mempool from a zone,
202 * assumes the zone lock is already held
204 static int __buddy_remove_mempool(struct buddy_memzone * zone,
205 unsigned long base_addr,
207 void **user_metadata)
210 struct buddy_mempool * pool = NULL;
211 struct block * block = NULL;
214 pool = find_mempool(zone, base_addr);
217 ERROR("Could not find mempool with base address (%p)\n", (void *)base_addr);
221 block = (struct block *)__va(pool->base_addr);
223 INFO("Removing Mempool %p, base=%p\n",pool,block);
225 // The largest order block in the memory pool must be free
226 if (!is_available(pool, block)) {
228 ERROR("Trying to remove an in use memory pool\n");
232 WARNING("Forcefully removing in use memory pool\n");
236 *user_metadata = pool->user_metadata;
238 list_del(&(block->link));
239 rb_erase(&(pool->tree_node), &(zone->mempools));
241 palacios_free(pool->tag_bits);
249 int buddy_remove_pool(struct buddy_memzone * zone,
250 unsigned long base_addr,
252 void **user_metadata)
254 unsigned long flags = 0;
257 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
258 ret = __buddy_remove_mempool(zone, base_addr, force, user_metadata);
259 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
269 * Allocates a block of memory of the requested size (2^order bytes).
272 * [IN] mp: Buddy system memory allocator object.
273 * [IN] order: Block size to allocate (2^order bytes).
274 * [IN] constraints: bitmmask showing restrictions for scan. currently: 0=none, or LWK_BUDDY_CONSTRAINT_4GB
276 * Success: Pointer to the start of the allocated memory block.
280 buddy_alloc(struct buddy_memzone *zone, unsigned long order, int constraints)
283 struct buddy_mempool * mp = NULL;
284 struct list_head * list = NULL;
285 struct list_head * cur = NULL;
286 struct block * block = NULL;
287 struct block * buddy_block = NULL;
288 unsigned long flags = 0;
290 if (constraints && constraints!=LWK_BUDDY_CONSTRAINT_4GB) {
291 ERROR("Do not know how to satisfy constraint mask 0x%x\n", constraints);
292 return (uintptr_t) NULL;
295 BUG_ON(zone == NULL);
296 BUG_ON(order > zone->max_order);
298 /* Fixup requested order to be at least the minimum supported */
299 if (order < zone->min_order) {
300 order = zone->min_order;
303 INFO("zone=%p, order=%lu\n", zone, order);
305 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
307 for (j = order; j <= zone->max_order; j++) {
309 INFO("Order iter=%lu\n", j);
313 list = &(zone->avail[j]);
315 if (list_empty(list)) {
319 list_for_each(cur, list) {
320 block = list_entry(cur, struct block, link);
323 // without a constraint, we just want the first one
327 if (constraints & LWK_BUDDY_CONSTRAINT_4GB) {
328 // under this constraint, we will only use if the entirity
329 // of the allocation within the block will be below 4 GB
330 void *block_pa = (void*)__pa(block);
331 if ((block_pa + (1ULL<<order)) <= (void*)(0x100000000ULL)) {
332 // this block will work
335 // look for the next block
343 // uh oh, no block, look to next order
347 // have appropriate block, will allocate
349 list_del(&(block->link));
353 mark_allocated(mp, block);
355 INFO("pool=%p, block=%p, order=%lu, j=%lu\n", mp, block, order, j);
357 /* Trim if a higher order block than necessary was allocated */
360 buddy_block = (struct block *)((unsigned long)block + (1UL << j));
361 buddy_block->mp = mp;
362 buddy_block->order = j;
363 mark_available(mp, buddy_block);
364 list_add(&(buddy_block->link), &(zone->avail[j]));
367 mp->num_free_blocks -= (1UL << (order - zone->min_order));
369 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
374 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
376 return (uintptr_t)NULL;
381 * Returns a block of memory to the buddy system memory allocator.
385 //! Buddy system memory allocator object.
386 struct buddy_memzone * zone,
387 //! Address of memory block to free.
389 //! Size of the memory block (2^order bytes).
393 struct block * block = NULL;
394 struct buddy_mempool * pool = NULL;
395 unsigned long flags = 0;
397 BUG_ON(zone == NULL);
398 BUG_ON(order > zone->max_order);
401 if ((addr & ((1UL << zone->min_order) - 1)) != 0) {
402 ERROR("Attempting to free an invalid memory address (%p)\n", (void *)addr);
407 /* Fixup requested order to be at least the minimum supported */
408 if (order < zone->min_order) {
409 order = zone->min_order;
413 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
415 pool = find_mempool(zone, addr);
417 if ((pool == NULL) || (order > pool->pool_order)) {
418 WARNING("Attempted to free an invalid page address (%p)\n", (void *)addr);
419 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
424 /* Overlay block structure on the memory block being freed */
425 block = (struct block *) __va(addr);
427 if (is_available(pool, block)) {
428 ERROR("Error: Freeing an available block\n");
429 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
433 pool->num_free_blocks += (1UL << (order - zone->min_order));
435 /* Coalesce as much as possible with adjacent free buddy blocks */
436 while (order < pool->pool_order) {
437 /* Determine our buddy block's address */
438 struct block * buddy = find_buddy(pool, block, order);
440 /* Make sure buddy is available and has the same size as us */
441 if (!is_available(pool, buddy))
443 if (is_available(pool, buddy) && (buddy->order != order))
446 /* OK, we're good to go... buddy merge! */
447 list_del(&buddy->link);
451 block->order = order;
454 /* Add the (possibly coalesced) block to the appropriate free list */
455 block->order = order;
457 mark_available(pool, block);
458 list_add(&(block->link), &(zone->avail[order]));
460 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
467 * Dumps the state of a buddy system memory allocator object to the console.
470 zone_mem_show(struct seq_file * s, void * v) {
471 struct buddy_memzone * zone = s->private;
473 unsigned long num_blocks;
474 struct list_head * entry = NULL;
475 unsigned long flags = 0;
479 seq_printf(s, "Null Zone Pointer!!\n");
483 seq_printf(s, "DUMP OF BUDDY MEMORY ZONE:\n");
484 seq_printf(s, " Zone Max Order=%lu, Min Order=%lu\n",
485 zone->max_order, zone->min_order);
487 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
489 for (i = zone->min_order; i <= zone->max_order; i++) {
491 /* Count the number of memory blocks in the list */
493 list_for_each(entry, &zone->avail[i]) {
497 seq_printf(s, " order %2lu: %lu free blocks\n", i, num_blocks);
501 seq_printf(s, " %lu memory pools\n", zone->num_pools);
502 // list pools in zone
504 struct rb_node * node = rb_first(&(zone->mempools));
505 struct buddy_mempool * pool = NULL;
508 pool = rb_entry(node, struct buddy_mempool, tree_node);
510 seq_printf(s, " Base Addr=%p, order=%lu, size=%lu, free=%lu\n",
511 (void *)pool->base_addr, pool->pool_order, (1UL << pool->pool_order),
512 pool->num_free_blocks << zone->min_order);
515 node = rb_next(node);
519 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
525 static int zone_proc_open(struct inode * inode, struct file * filp) {
526 struct proc_dir_entry * proc_entry = PDE(inode);
527 INFO("proc_entry at %p, data at %p\n", proc_entry, proc_entry->data);
528 return single_open(filp, zone_mem_show, proc_entry->data);
532 static struct file_operations zone_proc_ops = {
533 .owner = THIS_MODULE,
534 .open = zone_proc_open,
537 .release = single_release,
541 void buddy_deinit(struct buddy_memzone * zone, int (*free_callback)(void *user_metadata)) {
543 struct rb_node *node;
544 struct buddy_mempool **pools;
545 unsigned long long base_addr;
548 unsigned long num_in_tree;
550 pools = (struct buddy_mempool **) palacios_alloc(sizeof(struct buddy_mempool *)*zone->num_pools);
552 ERROR("Cannot allocate space for doing deinit of memory zone\n");
556 // We will lock only to build up the memory pool list
557 // when we free memory, we need to be able to support free callbacks
558 // that could block. This does leave a race with adds, allocs, and frees, however
559 // In Palacios, we expect a deinit will only really happen on the module unload
560 // so this should not present a problem
561 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
563 // because it does not appear possible to erase while iterating
564 // over the rb tree, we do the following contorted mess
566 for (num_in_tree=0, node=rb_first(&(zone->mempools));
567 node && num_in_tree<zone->num_pools;
568 node=rb_next(node), num_in_tree++) {
570 pools[num_in_tree]=rb_entry(node,struct buddy_mempool, tree_node);
573 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
575 if (num_in_tree != zone->num_pools) {
576 WARNING("Odd, the number of pools in the tree is %lu, but the zone reports %lu\n",
577 num_in_tree, zone->num_pools);
580 // now we'll free the memory
581 // note that buddy_remove_mempool also removes them
582 // from the rb tree, and frees them
583 for (i=0;i<num_in_tree;i++) {
584 base_addr = pools[i]->base_addr;
586 if (buddy_remove_pool(zone, base_addr, 1, &meta)) {
587 WARNING("Cannot remove memory pool at %p during zone deinit...\n",(void*)(base_addr));
591 // pool and node are now gone...
593 // invoke the callback to free the actual memory, if any
600 // get rid of /proc entry
602 char proc_file_name[128];
604 memset(proc_file_name, 0, 128);
605 snprintf(proc_file_name, 128, "v3-mem%d", zone->node_id);
607 remove_proc_entry(proc_file_name, palacios_get_procdir());
611 palacios_free(pools);
612 palacios_free(zone->avail);
621 * Initializes a buddy system memory allocator object.
624 * [IN] base_addr: Base address of the memory pool.
625 * [IN] pool_order: Size of the memory pool (2^pool_order bytes).
626 * [IN] min_order: Minimum allocatable block size (2^min_order bytes).
629 * Success: Pointer to an initialized buddy system memory allocator.
632 * NOTE: The min_order argument is provided as an optimization. Since one tag
633 * bit is required for each minimum-sized block, large memory pools that
634 * allow order 0 allocations will use large amounts of memory. Specifying
635 * a min_order of 5 (32 bytes), for example, reduces the number of tag
638 struct buddy_memzone *
640 unsigned long max_order,
641 unsigned long min_order,
645 struct buddy_memzone * zone = NULL;
648 DEBUG("Initializing Memory zone with up to %lu bit blocks on Node %d\n", max_order, node_id);
651 /* Smallest block size must be big enough to hold a block structure */
652 if ((1UL << min_order) < sizeof(struct block))
653 min_order = ilog2( roundup_pow_of_two(sizeof(struct block)) );
655 /* The minimum block order must be smaller than the pool order */
656 if (min_order > max_order)
659 zone = palacios_alloc_extended(sizeof(struct buddy_memzone), GFP_KERNEL, node_id);
661 INFO("Allocated zone at %p\n", zone);
664 ERROR("Could not allocate memzone\n");
668 memset(zone, 0, sizeof(struct buddy_memzone));
670 zone->max_order = max_order;
671 zone->min_order = min_order;
672 zone->node_id = node_id;
674 /* Allocate a list for every order up to the maximum allowed order */
675 zone->avail = palacios_alloc_extended((max_order + 1) * sizeof(struct list_head), GFP_KERNEL, zone->node_id);
677 if (!(zone->avail)) {
678 ERROR("Unable to allocate space for zone list\n");
683 INFO("Allocated free lists at %p\n", zone->avail);
685 /* Initially all lists are empty */
686 for (i = 0; i <= max_order; i++) {
687 INIT_LIST_HEAD(&zone->avail[i]);
691 palacios_spinlock_init(&(zone->lock));
693 zone->mempools.rb_node = NULL;
695 INFO("Allocated zone at %p\n", zone);
698 struct proc_dir_entry * zone_entry = NULL;
699 char proc_file_name[128];
701 memset(proc_file_name, 0, 128);
702 snprintf(proc_file_name, 128, "v3-mem%u", zone->node_id);
704 zone_entry = create_proc_entry(proc_file_name, 0444, palacios_get_procdir());
706 zone_entry->proc_fops = &zone_proc_ops;
707 zone_entry->data = zone;
708 INFO("Successfully created /proc/v3vee/v3-mem%d\n", zone->node_id);
710 ERROR("Cannot create /proc/v3vee/v3-mem%d\n", zone->node_id);