genalloc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. end_bit -= nbits + 1;
  113. spin_lock_irqsave(&chunk->lock, flags);
  114. start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, 0,
  115. nbits, 0);
  116. if (start_bit >= end_bit) {
  117. spin_unlock_irqrestore(&chunk->lock, flags);
  118. continue;
  119. }
  120. addr = chunk->start_addr + ((unsigned long)start_bit << order);
  121. bitmap_set(chunk->bits, start_bit, nbits);
  122. spin_unlock_irqrestore(&chunk->lock, flags);
  123. read_unlock(&pool->lock);
  124. return addr;
  125. }
  126. read_unlock(&pool->lock);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(gen_pool_alloc);
  130. /**
  131. * gen_pool_free - free allocated special memory back to the pool
  132. * @pool: pool to free to
  133. * @addr: starting address of memory to free back to pool
  134. * @size: size in bytes of memory to free
  135. *
  136. * Free previously allocated special memory back to the specified pool.
  137. */
  138. void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
  139. {
  140. struct list_head *_chunk;
  141. struct gen_pool_chunk *chunk;
  142. unsigned long flags;
  143. int order = pool->min_alloc_order;
  144. int bit, nbits;
  145. nbits = (size + (1UL << order) - 1) >> order;
  146. read_lock(&pool->lock);
  147. list_for_each(_chunk, &pool->chunks) {
  148. chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
  149. if (addr >= chunk->start_addr && addr < chunk->end_addr) {
  150. BUG_ON(addr + size > chunk->end_addr);
  151. spin_lock_irqsave(&chunk->lock, flags);
  152. bit = (addr - chunk->start_addr) >> order;
  153. while (nbits--)
  154. __clear_bit(bit++, chunk->bits);
  155. spin_unlock_irqrestore(&chunk->lock, flags);
  156. break;
  157. }
  158. }
  159. BUG_ON(nbits > 0);
  160. read_unlock(&pool->lock);
  161. }
  162. EXPORT_SYMBOL(gen_pool_free);