X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=linux_module%2Fbuddy.c;h=f21c95dae3ef547fcf7f6d9d19bee5bd72f6a30e;hb=d0aa5bd27c22a33f04f47363910f9773fdeab7eb;hp=ea5b705d505a764548d69e9e02f0a9d5b8b4faef;hpb=28dcbfda8061d2785301784d27694d1e02f54fff;p=palacios.git diff --git a/linux_module/buddy.c b/linux_module/buddy.c index ea5b705..f21c95d 100644 --- a/linux_module/buddy.c +++ b/linux_module/buddy.c @@ -155,7 +155,7 @@ int buddy_add_pool(struct buddy_memzone * zone, mp = palacios_alloc_extended(sizeof(struct buddy_mempool), GFP_KERNEL, zone->node_id); - if (IS_ERR(mp)) { + if (!mp) { ERROR("Could not allocate mempool\n"); return -1; } @@ -174,6 +174,13 @@ int buddy_add_pool(struct buddy_memzone * zone, BITS_TO_LONGS(mp->num_blocks) * sizeof(long), GFP_KERNEL, zone->node_id ); + if (!(mp->tag_bits)) { + ERROR("Could not allocate tag_bits\n"); + palacios_free(mp); + return -1; + } + + /* Initially mark all minimum-sized blocks as allocated */ bitmap_zero(mp->tag_bits, mp->num_blocks); @@ -235,7 +242,13 @@ static int __buddy_remove_mempool(struct buddy_memzone * zone, *user_metadata = pool->user_metadata; - list_del(&(block->link)); + if (is_available(pool,block)) { + list_del(&(block->link)); + } else { + // we may not be on the free list if we are being + // forcibly removed before all allocations are freed + } + rb_erase(&(pool->tree_node), &(zone->mempools)); palacios_free(pool->tag_bits); @@ -254,6 +267,7 @@ int buddy_remove_pool(struct buddy_memzone * zone, unsigned long flags = 0; int ret = 0; + palacios_spinlock_lock_irqsave(&(zone->lock), flags); ret = __buddy_remove_mempool(zone, base_addr, force, user_metadata); palacios_spinlock_unlock_irqrestore(&(zone->lock), flags); @@ -271,13 +285,14 @@ int buddy_remove_pool(struct buddy_memzone * zone, * Arguments: * [IN] mp: Buddy system memory allocator object. * [IN] order: Block size to allocate (2^order bytes). - * [IN] constraints: bitmmask showing restrictions for scan. currently: 0=none, or LWK_BUDDY_CONSTRAINT_4GB + * [IN] filter_func: returns nonzero if given paddr is OK to use + * [IN] filter_state: opaque argument to filter_func * Returns: * Success: Pointer to the start of the allocated memory block. * Failure: NULL */ uintptr_t -buddy_alloc(struct buddy_memzone *zone, unsigned long order, int constraints) +buddy_alloc(struct buddy_memzone *zone, unsigned long order, int (*filter_func)(void *paddr, void *filter_state), void *filter_state) { unsigned long j; struct buddy_mempool * mp = NULL; @@ -287,11 +302,6 @@ buddy_alloc(struct buddy_memzone *zone, unsigned long order, int constraints) struct block * buddy_block = NULL; unsigned long flags = 0; - if (constraints && constraints!=LWK_BUDDY_CONSTRAINT_4GB) { - ERROR("Do not know how to satisfy constraint mask 0x%x\n", constraints); - return (uintptr_t) NULL; - } - BUG_ON(zone == NULL); BUG_ON(order > zone->max_order); @@ -319,23 +329,22 @@ buddy_alloc(struct buddy_memzone *zone, unsigned long order, int constraints) list_for_each(cur, list) { block = list_entry(cur, struct block, link); - if (!constraints) { - // without a constraint, we just want the first one + if (!filter_func) { + // without a filter, we just want the first one break; - } - - if (constraints & LWK_BUDDY_CONSTRAINT_4GB) { - // under this constraint, we will only use if the entirity - // of the allocation within the block will be below 4 GB + } else { + void *block_pa = (void*)__pa(block); - if ((block_pa + (1ULL< pool->pool_order)) { - WARNING("Attempted to free an invalid page address (%p)\n", (void *)addr); + WARNING("Attempted to free an invalid page address (%p) - pool=%p order=%lu\n", (void *)addr,pool,order); palacios_spinlock_unlock_irqrestore(&(zone->lock), flags); - return; + return -1; } @@ -427,7 +436,7 @@ buddy_free( if (is_available(pool, block)) { ERROR("Error: Freeing an available block\n"); palacios_spinlock_unlock_irqrestore(&(zone->lock), flags); - return; + return -1; } pool->num_free_blocks += (1UL << (order - zone->min_order)); @@ -458,6 +467,8 @@ buddy_free( list_add(&(block->link), &(zone->avail[order])); palacios_spinlock_unlock_irqrestore(&(zone->lock), flags); + + return 0; }