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 struct buddy_mempool * mp = NULL;
144 unsigned long flags = 0;
147 if (pool_order > zone->max_order) {
148 ERROR("Pool order size is larger than max allowable zone size (pool_order=%lu) (max_order=%lu)\n", pool_order, zone->max_order);
150 } else if (pool_order < zone->min_order) {
151 ERROR("Pool order is smaller than min allowable zone size (pool_order=%lu) (min_order=%lu)\n", pool_order, zone->min_order);
155 mp = palacios_alloc_node_extended(sizeof(struct buddy_mempool), GFP_KERNEL, zone->node_id);
158 ERROR("Could not allocate mempool\n");
162 mp->base_addr = base_addr;
163 mp->pool_order = pool_order;
165 mp->num_free_blocks = 0;
167 /* Allocate a bitmap with 1 bit per minimum-sized block */
168 mp->num_blocks = (1UL << pool_order) / (1UL << zone->min_order);
170 mp->tag_bits = palacios_alloc_node_extended(
171 BITS_TO_LONGS(mp->num_blocks) * sizeof(long), GFP_KERNEL, zone->node_id
174 /* Initially mark all minimum-sized blocks as allocated */
175 bitmap_zero(mp->tag_bits, mp->num_blocks);
177 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
178 ret = insert_mempool(zone, mp);
179 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
182 ERROR("Error: Could not insert mempool into zone\n");
183 palacios_free(mp->tag_bits);
189 buddy_free(zone, base_addr, pool_order);
191 INFO("Added memory pool (addr=%p), order=%lu\n", (void *)base_addr, pool_order);
198 * Removes a mempool from a zone,
199 * assumes the zone lock is already held
201 static int __buddy_remove_mempool(struct buddy_memzone * zone,
202 unsigned long base_addr,
203 unsigned char force) {
205 struct buddy_mempool * pool = NULL;
206 struct block * block = NULL;
208 pool = find_mempool(zone, base_addr);
211 ERROR("Could not find mempool with base address (%p)\n", (void *)base_addr);
215 if (!bitmap_empty(pool->tag_bits, pool->num_blocks)) {
216 ERROR("Trying to remove an in use memory pool\n");
220 block = (struct block *)__va(pool->base_addr);
222 list_del(&(block->link));
223 rb_erase(&(pool->tree_node), &(zone->mempools));
225 palacios_free(pool->tag_bits);
233 int buddy_remove_pool(struct buddy_memzone * zone,
234 unsigned long base_addr,
235 unsigned char force) {
236 unsigned long flags = 0;
239 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
240 ret = __buddy_remove_mempool(zone, base_addr, force);
241 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
250 * Allocates a block of memory of the requested size (2^order bytes).
253 * [IN] mp: Buddy system memory allocator object.
254 * [IN] order: Block size to allocate (2^order bytes).
257 * Success: Pointer to the start of the allocated memory block.
261 buddy_alloc(struct buddy_memzone *zone, unsigned long order)
264 struct buddy_mempool * mp = NULL;
265 struct list_head * list = NULL;
266 struct block * block = NULL;
267 struct block * buddy_block = NULL;
268 unsigned long flags = 0;
270 BUG_ON(zone == NULL);
271 BUG_ON(order > zone->max_order);
273 /* Fixup requested order to be at least the minimum supported */
274 if (order < zone->min_order) {
275 order = zone->min_order;
278 INFO("zone=%p, order=%lu\n", zone, order);
280 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
282 for (j = order; j <= zone->max_order; j++) {
284 INFO("Order iter=%lu\n", j);
286 /* Try to allocate the first block in the order j list */
287 list = &zone->avail[j];
289 if (list_empty(list))
292 block = list_entry(list->next, struct block, link);
293 list_del(&(block->link));
297 mark_allocated(mp, block);
299 INFO("pool=%p, block=%p, order=%lu, j=%lu\n", mp, block, order, j);
302 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
306 /* Trim if a higher order block than necessary was allocated */
309 buddy_block = (struct block *)((unsigned long)block + (1UL << j));
310 buddy_block->mp = mp;
311 buddy_block->order = j;
312 mark_available(mp, buddy_block);
313 list_add(&(buddy_block->link), &(zone->avail[j]));
316 mp->num_free_blocks -= (1UL << (order - zone->min_order));
318 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
323 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
325 return (uintptr_t)NULL;
330 * Returns a block of memory to the buddy system memory allocator.
334 //! Buddy system memory allocator object.
335 struct buddy_memzone * zone,
336 //! Address of memory block to free.
338 //! Size of the memory block (2^order bytes).
342 struct block * block = NULL;
343 struct buddy_mempool * pool = NULL;
344 unsigned long flags = 0;
346 BUG_ON(zone == NULL);
347 BUG_ON(order > zone->max_order);
350 if ((addr & ((1UL << zone->min_order) - 1)) != 0) {
351 ERROR("Attempting to free an invalid memory address (%p)\n", (void *)addr);
356 /* Fixup requested order to be at least the minimum supported */
357 if (order < zone->min_order) {
358 order = zone->min_order;
362 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
364 pool = find_mempool(zone, addr);
366 if ((pool == NULL) || (order > pool->pool_order)) {
367 WARNING("Attempted to free an invalid page address (%p)\n", (void *)addr);
368 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
373 /* Overlay block structure on the memory block being freed */
374 block = (struct block *) __va(addr);
376 if (is_available(pool, block)) {
377 ERROR("Error: Freeing an available block\n");
378 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
382 pool->num_free_blocks += (1UL << (order - zone->min_order));
384 /* Coalesce as much as possible with adjacent free buddy blocks */
385 while (order < pool->pool_order) {
386 /* Determine our buddy block's address */
387 struct block * buddy = find_buddy(pool, block, order);
389 /* Make sure buddy is available and has the same size as us */
390 if (!is_available(pool, buddy))
392 if (is_available(pool, buddy) && (buddy->order != order))
395 /* OK, we're good to go... buddy merge! */
396 list_del(&buddy->link);
400 block->order = order;
403 /* Add the (possibly coalesced) block to the appropriate free list */
404 block->order = order;
406 mark_available(pool, block);
407 list_add(&(block->link), &(zone->avail[order]));
409 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
416 * Dumps the state of a buddy system memory allocator object to the console.
419 zone_mem_show(struct seq_file * s, void * v) {
420 struct buddy_memzone * zone = s->private;
422 unsigned long num_blocks;
423 struct list_head * entry = NULL;
424 unsigned long flags = 0;
428 seq_printf(s, "Null Zone Pointer!!\n");
432 seq_printf(s, "DUMP OF BUDDY MEMORY ZONE:\n");
433 seq_printf(s, " Zone Max Order=%lu, Min Order=%lu\n",
434 zone->max_order, zone->min_order);
436 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
438 for (i = zone->min_order; i <= zone->max_order; i++) {
440 /* Count the number of memory blocks in the list */
442 list_for_each(entry, &zone->avail[i]) {
446 seq_printf(s, " order %2lu: %lu free blocks\n", i, num_blocks);
450 seq_printf(s, " %lu memory pools\n", zone->num_pools);
451 // list pools in zone
453 struct rb_node * node = rb_first(&(zone->mempools));
454 struct buddy_mempool * pool = NULL;
457 pool = rb_entry(node, struct buddy_mempool, tree_node);
459 seq_printf(s, " Base Addr=%p, order=%lu, size=%lu, free=%lu\n",
460 (void *)pool->base_addr, pool->pool_order, (1UL << pool->pool_order),
461 pool->num_free_blocks << zone->min_order);
464 node = rb_next(node);
468 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
474 static int zone_proc_open(struct inode * inode, struct file * filp) {
475 struct proc_dir_entry * proc_entry = PDE(inode);
476 INFO("proc_entry at %p, data at %p\n", proc_entry, proc_entry->data);
477 return single_open(filp, zone_mem_show, proc_entry->data);
481 static struct file_operations zone_proc_ops = {
482 .owner = THIS_MODULE,
483 .open = zone_proc_open,
486 .release = single_release,
490 extern struct proc_dir_entry * palacios_proc_dir;
492 void buddy_deinit(struct buddy_memzone * zone) {
495 palacios_spinlock_lock_irqsave(&(zone->lock), flags);
497 // for each pool, free it
498 #warning We really need to free the memory pools here
500 palacios_spinlock_unlock_irqrestore(&(zone->lock), flags);
503 char proc_file_name[128];
505 memset(proc_file_name, 0, 128);
506 snprintf(proc_file_name, 128, "v3-mem%d", zone->node_id);
508 remove_proc_entry(proc_file_name, palacios_proc_dir);
512 palacios_free(zone->avail);
521 * Initializes a buddy system memory allocator object.
524 * [IN] base_addr: Base address of the memory pool.
525 * [IN] pool_order: Size of the memory pool (2^pool_order bytes).
526 * [IN] min_order: Minimum allocatable block size (2^min_order bytes).
529 * Success: Pointer to an initialized buddy system memory allocator.
532 * NOTE: The min_order argument is provided as an optimization. Since one tag
533 * bit is required for each minimum-sized block, large memory pools that
534 * allow order 0 allocations will use large amounts of memory. Specifying
535 * a min_order of 5 (32 bytes), for example, reduces the number of tag
538 struct buddy_memzone *
540 unsigned long max_order,
541 unsigned long min_order,
545 struct buddy_memzone * zone = NULL;
548 DEBUG("Initializing Memory zone with up to %lu bit blocks on Node %d\n", max_order, node_id);
551 /* Smallest block size must be big enough to hold a block structure */
552 if ((1UL << min_order) < sizeof(struct block))
553 min_order = ilog2( roundup_pow_of_two(sizeof(struct block)) );
555 /* The minimum block order must be smaller than the pool order */
556 if (min_order > max_order)
559 zone = palacios_alloc_node_extended(sizeof(struct buddy_memzone), GFP_KERNEL, node_id);
561 INFO("Allocated zone at %p\n", zone);
564 ERROR("Could not allocate memzone\n");
568 memset(zone, 0, sizeof(struct buddy_memzone));
570 zone->max_order = max_order;
571 zone->min_order = min_order;
572 zone->node_id = node_id;
574 /* Allocate a list for every order up to the maximum allowed order */
575 zone->avail = palacios_alloc_node_extended((max_order + 1) * sizeof(struct list_head), GFP_KERNEL, zone->node_id);
577 INFO("Allocated free lists at %p\n", zone->avail);
579 /* Initially all lists are empty */
580 for (i = 0; i <= max_order; i++) {
581 INIT_LIST_HEAD(&zone->avail[i]);
585 palacios_spinlock_init(&(zone->lock));
587 zone->mempools.rb_node = NULL;
589 INFO("Allocated zone at %p\n", zone);
592 struct proc_dir_entry * zone_entry = NULL;
593 char proc_file_name[128];
595 memset(proc_file_name, 0, 128);
596 snprintf(proc_file_name, 128, "v3-mem%d", zone->node_id);
598 zone_entry = create_proc_entry(proc_file_name, 0444, palacios_proc_dir);
600 zone_entry->proc_fops = &zone_proc_ops;
601 zone_entry->data = zone;
603 ERROR("Error creating memory zone proc file\n");