genalloc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
  77. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  78. list_del(&chunk->next_chunk);
  79. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  80. bit = find_next_bit(chunk->bits, end_bit, 0);
  81. BUG_ON(bit < end_bit);
  82. kfree(chunk);
  83. }
  84. kfree(pool);
  85. return;
  86. }
  87. EXPORT_SYMBOL(gen_pool_destroy);
  88. /**
  89. * gen_pool_alloc - allocate special memory from the pool
  90. * @pool: pool to allocate from
  91. * @size: number of bytes to allocate from the pool
  92. *
  93. * Allocate the requested number of bytes from the specified pool.
  94. * Uses a first-fit algorithm.
  95. */
  96. unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
  97. {
  98. struct list_head *_chunk;
  99. struct gen_pool_chunk *chunk;
  100. unsigned long addr, flags;
  101. int order = pool->min_alloc_order;
  102. int nbits, bit, start_bit, end_bit;
  103. if (size == 0)
  104. return 0;
  105. nbits = (size + (1UL << order) - 1) >> order;
  106. read_lock(&pool->lock);
  107. list_for_each(_chunk, &pool->chunks) {
  108. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  109. end_bit = (chunk->end_addr - chunk->start_addr) >> order;
  110. end_bit -= nbits + 1;
  111. spin_lock_irqsave(&chunk->lock, flags);
  112. bit = -1;
  113. while (bit + 1 < end_bit) {
  114. bit = find_next_zero_bit(chunk->bits, end_bit, bit + 1);
  115. if (bit >= end_bit)
  116. break;
  117. start_bit = bit;
  118. if (nbits > 1) {
  119. bit = find_next_bit(chunk->bits, bit + nbits,
  120. bit + 1);
  121. if (bit - start_bit < nbits)
  122. continue;
  123. }
  124. addr = chunk->start_addr +
  125. ((unsigned long)start_bit << order);
  126. while (nbits--)
  127. __set_bit(start_bit++, chunk->bits);
  128. spin_unlock_irqrestore(&chunk->lock, flags);
  129. read_unlock(&pool->lock);
  130. return addr;
  131. }
  132. spin_unlock_irqrestore(&chunk->lock, flags);
  133. }
  134. read_unlock(&pool->lock);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(gen_pool_alloc);
  138. /**
  139. * gen_pool_free - free allocated special memory back to the pool
  140. * @pool: pool to free to
  141. * @addr: starting address of memory to free back to pool
  142. * @size: size in bytes of memory to free
  143. *
  144. * Free previously allocated special memory back to the specified pool.
  145. */
  146. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  147. {
  148. struct list_head *_chunk;
  149. struct gen_pool_chunk *chunk;
  150. unsigned long flags;
  151. int order = pool->min_alloc_order;
  152. int bit, nbits;
  153. nbits = (size + (1UL << order) - 1) >> order;
  154. read_lock(&pool->lock);
  155. list_for_each(_chunk, &pool->chunks) {
  156. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  157. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  158. BUG_ON(addr + size > chunk->end_addr);
  159. spin_lock_irqsave(&chunk->lock, flags);
  160. bit = (addr - chunk->start_addr) >> order;
  161. while (nbits--)
  162. __clear_bit(bit++, chunk->bits);
  163. spin_unlock_irqrestore(&chunk->lock, flags);
  164. break;
  165. }
  166. }
  167. BUG_ON(nbits > 0);
  168. read_unlock(&pool->lock);
  169. }
  170. EXPORT_SYMBOL(gen_pool_free);