genalloc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Basic general purpose allocator for managing special purpose memory
  3. * not managed by the regular kmalloc/kfree interface.
  4. * Uses for this includes on-device special memory, uncached memory
  5. * etc.
  6. *
  7. * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
  8. *
  9. * This source code is licensed under the GNU General Public License,
  10. * Version 2. See the file COPYING for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/genalloc.h>
  16. /**
  17. * gen_pool_create - create a new special memory pool
  18. * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
  19. * @nid: node id of the node the pool structure should be allocated on, or -1
  20. *
  21. * Create a new special memory pool that can be used to manage special purpose
  22. * memory not managed by the regular kmalloc/kfree interface.
  23. */
  24. struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
  25. {
  26. struct gen_pool *pool;
  27. pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
  28. if (pool != NULL) {
  29. rwlock_init(&pool->lock);
  30. INIT_LIST_HEAD(&pool->chunks);
  31. pool->min_alloc_order = min_alloc_order;
  32. }
  33. return pool;
  34. }
  35. EXPORT_SYMBOL(gen_pool_create);
  36. /**
  37. * gen_pool_add - add a new chunk of special memory to the pool
  38. * @pool: pool to add new memory chunk to
  39. * @addr: starting address of memory chunk to add to pool
  40. * @size: size in bytes of the memory chunk to add to pool
  41. * @nid: node id of the node the chunk structure and bitmap should be
  42. * allocated on, or -1
  43. *
  44. * Add a new chunk of special memory to the specified pool.
  45. */
  46. int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
  47. int nid)
  48. {
  49. struct gen_pool_chunk *chunk;
  50. int nbits = size >> pool->min_alloc_order;
  51. int nbytes = sizeof(struct gen_pool_chunk) +
  52. (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
  53. chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
  54. if (unlikely(chunk == NULL))
  55. return -1;
  56. spin_lock_init(&chunk->lock);
  57. chunk->start_addr = addr;
  58. chunk->end_addr = addr + size;
  59. write_lock(&pool->lock);
  60. list_add(&chunk->next_chunk, &pool->chunks);
  61. write_unlock(&pool->lock);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL(gen_pool_add);
  65. /**
  66. * gen_pool_destroy - destroy a special memory pool
  67. * @pool: pool to destroy
  68. *
  69. * Destroy the specified special memory pool. Verifies that there are no
  70. * outstanding allocations.
  71. */
  72. void gen_pool_destroy(struct gen_pool *pool)
  73. {
  74. struct list_head *_chunk, *_next_chunk;
  75. struct gen_pool_chunk *chunk;
  76. int order = pool->min_alloc_order;
  77. int bit, end_bit;
  78. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  79. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  80. list_del(&chunk->next_chunk);
  81. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  82. bit = find_next_bit(chunk->bits, end_bit, 0);
  83. BUG_ON(bit < end_bit);
  84. kfree(chunk);
  85. }
  86. kfree(pool);
  87. return;
  88. }
  89. EXPORT_SYMBOL(gen_pool_destroy);
  90. /**
  91. * gen_pool_alloc - allocate special memory from the pool
  92. * @pool: pool to allocate from
  93. * @size: number of bytes to allocate from the pool
  94. *
  95. * Allocate the requested number of bytes from the specified pool.
  96. * Uses a first-fit algorithm.
  97. */
  98. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  99. {
  100. struct list_head *_chunk;
  101. struct gen_pool_chunk *chunk;
  102. unsigned long addr, flags;
  103. int order = pool->min_alloc_order;
  104. int nbits, start_bit, end_bit;
  105. if (size == 0)
  106. return 0;
  107. nbits = (size + (1UL << order) - 1) >> order;
  108. read_lock(&pool->lock);
  109. list_for_each(_chunk, &pool->chunks) {
  110. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  111. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  112. spin_lock_irqsave(&chunk->lock, flags);
  113. start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, 0,
  114. nbits, 0);
  115. if (start_bit >= end_bit) {
  116. spin_unlock_irqrestore(&chunk->lock, flags);
  117. continue;
  118. }
  119. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  120. bitmap_set(chunk->bits, start_bit, nbits);
  121. spin_unlock_irqrestore(&chunk->lock, flags);
  122. read_unlock(&pool->lock);
  123. return addr;
  124. }
  125. read_unlock(&pool->lock);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(gen_pool_alloc);
  129. /**
  130. * gen_pool_free - free allocated special memory back to the pool
  131. * @pool: pool to free to
  132. * @addr: starting address of memory to free back to pool
  133. * @size: size in bytes of memory to free
  134. *
  135. * Free previously allocated special memory back to the specified pool.
  136. */
  137. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  138. {
  139. struct list_head *_chunk;
  140. struct gen_pool_chunk *chunk;
  141. unsigned long flags;
  142. int order = pool->min_alloc_order;
  143. int bit, nbits;
  144. nbits = (size + (1UL << order) - 1) >> order;
  145. read_lock(&pool->lock);
  146. list_for_each(_chunk, &pool->chunks) {
  147. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  148. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  149. BUG_ON(addr + size > chunk->end_addr);
  150. spin_lock_irqsave(&chunk->lock, flags);
  151. bit = (addr - chunk->start_addr) >> order;
  152. while (nbits--)
  153. __clear_bit(bit++, chunk->bits);
  154. spin_unlock_irqrestore(&chunk->lock, flags);
  155. break;
  156. }
  157. }
  158. BUG_ON(nbits > 0);
  159. read_unlock(&pool->lock);
  160. }
  161. EXPORT_SYMBOL(gen_pool_free);