diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-06 03:09:16 +0900 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-06 03:09:16 +0900 |
commit | 11126c611e10abb18b6f1ed0300c0548c3906b54 (patch) | |
tree | 246227ad730c1e68f5a9c03db57cd4592abe7687 /lib/genalloc.c | |
parent | 67966e088b0c7dc926f4ce19565ebf208e18d058 (diff) | |
parent | 33e2a4227ddff7c18921ac175fae3ab0e3ff8a76 (diff) | |
download | linux-11126c611e10abb18b6f1ed0300c0548c3906b54.tar.bz2 |
Merge branch 'akpm' (Andrew's patch-bomb)
Merge misc patches from Andrew Morton:
"The MM tree is rather stuck while I wait to find out what the heck is
happening with sched/numa. Probably I'll need to route around all the
code which was added to -next, sigh.
So this is "everything else", or at least most of it - other small
bits are still awaiting resolutions of various kinds."
* emailed patches from Andrew Morton <akpm@linux-foundation.org>: (180 commits)
lib/decompress.c add __init to decompress_method and data
kernel/resource.c: fix stack overflow in __reserve_region_with_split()
omfs: convert to use beXX_add_cpu()
taskstats: cgroupstats_user_cmd() may leak on error
aoe: update aoe-internal version number to 50
aoe: update documentation to better reflect aoe-plus-udev usage
aoe: remove unused code
aoe: make dynamic block minor numbers the default
aoe: update and specify AoE address guards and error messages
aoe: retain static block device numbers for backwards compatibility
aoe: support more AoE addresses with dynamic block device minor numbers
aoe: update documentation with new URL and VM settings reference
aoe: update copyright year in touched files
aoe: update internal version number to 49
aoe: remove unused code and add cosmetic improvements
aoe: increase net_device reference count while using it
aoe: associate frames with the AoE storage target
aoe: disallow unsupported AoE minor addresses
aoe: do revalidation steps in order
aoe: failover remote interface based on aoe_deadsecs parameter
...
Diffstat (limited to 'lib/genalloc.c')
-rw-r--r-- | lib/genalloc.c | 88 |
1 files changed, 84 insertions, 4 deletions
diff --git a/lib/genalloc.c b/lib/genalloc.c index 6bc04aab6ec7..ca208a92628c 100644 --- a/lib/genalloc.c +++ b/lib/genalloc.c @@ -152,6 +152,8 @@ struct gen_pool *gen_pool_create(int min_alloc_order, int nid) spin_lock_init(&pool->lock); INIT_LIST_HEAD(&pool->chunks); pool->min_alloc_order = min_alloc_order; + pool->algo = gen_pool_first_fit; + pool->data = NULL; } return pool; } @@ -255,8 +257,9 @@ EXPORT_SYMBOL(gen_pool_destroy); * @size: number of bytes to allocate from the pool * * Allocate the requested number of bytes from the specified pool. - * Uses a first-fit algorithm. Can not be used in NMI handler on - * architectures without NMI-safe cmpxchg implementation. + * Uses the pool allocation function (with first-fit algorithm by default). + * Can not be used in NMI handler on architectures without + * NMI-safe cmpxchg implementation. */ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size) { @@ -280,8 +283,8 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size) end_bit = (chunk->end_addr - chunk->start_addr) >> order; retry: - start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, - start_bit, nbits, 0); + start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits, + pool->data); if (start_bit >= end_bit) continue; remain = bitmap_set_ll(chunk->bits, start_bit, nbits); @@ -400,3 +403,80 @@ size_t gen_pool_size(struct gen_pool *pool) return size; } EXPORT_SYMBOL_GPL(gen_pool_size); + +/** + * gen_pool_set_algo - set the allocation algorithm + * @pool: pool to change allocation algorithm + * @algo: custom algorithm function + * @data: additional data used by @algo + * + * Call @algo for each memory allocation in the pool. + * If @algo is NULL use gen_pool_first_fit as default + * memory allocation function. + */ +void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data) +{ + rcu_read_lock(); + + pool->algo = algo; + if (!pool->algo) + pool->algo = gen_pool_first_fit; + + pool->data = data; + + rcu_read_unlock(); +} +EXPORT_SYMBOL(gen_pool_set_algo); + +/** + * gen_pool_first_fit - find the first available region + * of memory matching the size requirement (no alignment constraint) + * @map: The address to base the search on + * @size: The bitmap size in bits + * @start: The bitnumber to start searching at + * @nr: The number of zeroed bits we're looking for + * @data: additional data - unused + */ +unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, + unsigned long start, unsigned int nr, void *data) +{ + return bitmap_find_next_zero_area(map, size, start, nr, 0); +} +EXPORT_SYMBOL(gen_pool_first_fit); + +/** + * gen_pool_best_fit - find the best fitting region of memory + * macthing the size requirement (no alignment constraint) + * @map: The address to base the search on + * @size: The bitmap size in bits + * @start: The bitnumber to start searching at + * @nr: The number of zeroed bits we're looking for + * @data: additional data - unused + * + * Iterate over the bitmap to find the smallest free region + * which we can allocate the memory. + */ +unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, + unsigned long start, unsigned int nr, void *data) +{ + unsigned long start_bit = size; + unsigned long len = size + 1; + unsigned long index; + + index = bitmap_find_next_zero_area(map, size, start, nr, 0); + + while (index < size) { + int next_bit = find_next_bit(map, size, index + nr); + if ((next_bit - index) < len) { + len = next_bit - index; + start_bit = index; + if (len == nr) + return start_bit; + } + index = bitmap_find_next_zero_area(map, size, + next_bit + 1, nr, 0); + } + + return start_bit; +} +EXPORT_SYMBOL(gen_pool_best_fit); |