genalloc.c 5.3 KB

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