mempool.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * linux/mm/mempool.c
  3. *
  4. * memory buffer pool support. Such pools are mostly used
  5. * for guaranteed, deadlock-free memory allocations during
  6. * extreme VM load.
  7. *
  8. * started by Ingo Molnar, Copyright (C) 2001
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/mempool.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/writeback.h>
  16. static void add_element(mempool_t *pool, void *element)
  17. {
  18. BUG_ON(pool->curr_nr >= pool->min_nr);
  19. pool->elements[pool->curr_nr++] = element;
  20. }
  21. static void *remove_element(mempool_t *pool)
  22. {
  23. BUG_ON(pool->curr_nr <= 0);
  24. return pool->elements[--pool->curr_nr];
  25. }
  26. static void free_pool(mempool_t *pool)
  27. {
  28. while (pool->curr_nr) {
  29. void *element = remove_element(pool);
  30. pool->free(element, pool->pool_data);
  31. }
  32. kfree(pool->elements);
  33. kfree(pool);
  34. }
  35. /**
  36. * mempool_create - create a memory pool
  37. * @min_nr: the minimum number of elements guaranteed to be
  38. * allocated for this pool.
  39. * @alloc_fn: user-defined element-allocation function.
  40. * @free_fn: user-defined element-freeing function.
  41. * @pool_data: optional private data available to the user-defined functions.
  42. *
  43. * this function creates and allocates a guaranteed size, preallocated
  44. * memory pool. The pool can be used from the mempool_alloc and mempool_free
  45. * functions. This function might sleep. Both the alloc_fn() and the free_fn()
  46. * functions might sleep - as long as the mempool_alloc function is not called
  47. * from IRQ contexts.
  48. */
  49. mempool_t * mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
  50. mempool_free_t *free_fn, void *pool_data)
  51. {
  52. mempool_t *pool;
  53. pool = kmalloc(sizeof(*pool), GFP_KERNEL);
  54. if (!pool)
  55. return NULL;
  56. memset(pool, 0, sizeof(*pool));
  57. pool->elements = kmalloc(min_nr * sizeof(void *), GFP_KERNEL);
  58. if (!pool->elements) {
  59. kfree(pool);
  60. return NULL;
  61. }
  62. spin_lock_init(&pool->lock);
  63. pool->min_nr = min_nr;
  64. pool->pool_data = pool_data;
  65. init_waitqueue_head(&pool->wait);
  66. pool->alloc = alloc_fn;
  67. pool->free = free_fn;
  68. /*
  69. * First pre-allocate the guaranteed number of buffers.
  70. */
  71. while (pool->curr_nr < pool->min_nr) {
  72. void *element;
  73. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  74. if (unlikely(!element)) {
  75. free_pool(pool);
  76. return NULL;
  77. }
  78. add_element(pool, element);
  79. }
  80. return pool;
  81. }
  82. EXPORT_SYMBOL(mempool_create);
  83. /**
  84. * mempool_resize - resize an existing memory pool
  85. * @pool: pointer to the memory pool which was allocated via
  86. * mempool_create().
  87. * @new_min_nr: the new minimum number of elements guaranteed to be
  88. * allocated for this pool.
  89. * @gfp_mask: the usual allocation bitmask.
  90. *
  91. * This function shrinks/grows the pool. In the case of growing,
  92. * it cannot be guaranteed that the pool will be grown to the new
  93. * size immediately, but new mempool_free() calls will refill it.
  94. *
  95. * Note, the caller must guarantee that no mempool_destroy is called
  96. * while this function is running. mempool_alloc() & mempool_free()
  97. * might be called (eg. from IRQ contexts) while this function executes.
  98. */
  99. int mempool_resize(mempool_t *pool, int new_min_nr, unsigned int __nocast gfp_mask)
  100. {
  101. void *element;
  102. void **new_elements;
  103. unsigned long flags;
  104. BUG_ON(new_min_nr <= 0);
  105. spin_lock_irqsave(&pool->lock, flags);
  106. if (new_min_nr <= pool->min_nr) {
  107. while (new_min_nr < pool->curr_nr) {
  108. element = remove_element(pool);
  109. spin_unlock_irqrestore(&pool->lock, flags);
  110. pool->free(element, pool->pool_data);
  111. spin_lock_irqsave(&pool->lock, flags);
  112. }
  113. pool->min_nr = new_min_nr;
  114. goto out_unlock;
  115. }
  116. spin_unlock_irqrestore(&pool->lock, flags);
  117. /* Grow the pool */
  118. new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask);
  119. if (!new_elements)
  120. return -ENOMEM;
  121. spin_lock_irqsave(&pool->lock, flags);
  122. if (unlikely(new_min_nr <= pool->min_nr)) {
  123. /* Raced, other resize will do our work */
  124. spin_unlock_irqrestore(&pool->lock, flags);
  125. kfree(new_elements);
  126. goto out;
  127. }
  128. memcpy(new_elements, pool->elements,
  129. pool->curr_nr * sizeof(*new_elements));
  130. kfree(pool->elements);
  131. pool->elements = new_elements;
  132. pool->min_nr = new_min_nr;
  133. while (pool->curr_nr < pool->min_nr) {
  134. spin_unlock_irqrestore(&pool->lock, flags);
  135. element = pool->alloc(gfp_mask, pool->pool_data);
  136. if (!element)
  137. goto out;
  138. spin_lock_irqsave(&pool->lock, flags);
  139. if (pool->curr_nr < pool->min_nr) {
  140. add_element(pool, element);
  141. } else {
  142. spin_unlock_irqrestore(&pool->lock, flags);
  143. pool->free(element, pool->pool_data); /* Raced */
  144. goto out;
  145. }
  146. }
  147. out_unlock:
  148. spin_unlock_irqrestore(&pool->lock, flags);
  149. out:
  150. return 0;
  151. }
  152. EXPORT_SYMBOL(mempool_resize);
  153. /**
  154. * mempool_destroy - deallocate a memory pool
  155. * @pool: pointer to the memory pool which was allocated via
  156. * mempool_create().
  157. *
  158. * this function only sleeps if the free_fn() function sleeps. The caller
  159. * has to guarantee that all elements have been returned to the pool (ie:
  160. * freed) prior to calling mempool_destroy().
  161. */
  162. void mempool_destroy(mempool_t *pool)
  163. {
  164. if (pool->curr_nr != pool->min_nr)
  165. BUG(); /* There were outstanding elements */
  166. free_pool(pool);
  167. }
  168. EXPORT_SYMBOL(mempool_destroy);
  169. /**
  170. * mempool_alloc - allocate an element from a specific memory pool
  171. * @pool: pointer to the memory pool which was allocated via
  172. * mempool_create().
  173. * @gfp_mask: the usual allocation bitmask.
  174. *
  175. * this function only sleeps if the alloc_fn function sleeps or
  176. * returns NULL. Note that due to preallocation, this function
  177. * *never* fails when called from process contexts. (it might
  178. * fail if called from an IRQ context.)
  179. */
  180. void * mempool_alloc(mempool_t *pool, unsigned int __nocast gfp_mask)
  181. {
  182. void *element;
  183. unsigned long flags;
  184. DEFINE_WAIT(wait);
  185. int gfp_temp;
  186. might_sleep_if(gfp_mask & __GFP_WAIT);
  187. gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
  188. gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
  189. gfp_mask |= __GFP_NOWARN; /* failures are OK */
  190. gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
  191. repeat_alloc:
  192. element = pool->alloc(gfp_temp, pool->pool_data);
  193. if (likely(element != NULL))
  194. return element;
  195. spin_lock_irqsave(&pool->lock, flags);
  196. if (likely(pool->curr_nr)) {
  197. element = remove_element(pool);
  198. spin_unlock_irqrestore(&pool->lock, flags);
  199. return element;
  200. }
  201. spin_unlock_irqrestore(&pool->lock, flags);
  202. /* We must not sleep in the GFP_ATOMIC case */
  203. if (!(gfp_mask & __GFP_WAIT))
  204. return NULL;
  205. /* Now start performing page reclaim */
  206. gfp_temp = gfp_mask;
  207. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  208. smp_mb();
  209. if (!pool->curr_nr)
  210. io_schedule();
  211. finish_wait(&pool->wait, &wait);
  212. goto repeat_alloc;
  213. }
  214. EXPORT_SYMBOL(mempool_alloc);
  215. /**
  216. * mempool_free - return an element to the pool.
  217. * @element: pool element pointer.
  218. * @pool: pointer to the memory pool which was allocated via
  219. * mempool_create().
  220. *
  221. * this function only sleeps if the free_fn() function sleeps.
  222. */
  223. void mempool_free(void *element, mempool_t *pool)
  224. {
  225. unsigned long flags;
  226. smp_mb();
  227. if (pool->curr_nr < pool->min_nr) {
  228. spin_lock_irqsave(&pool->lock, flags);
  229. if (pool->curr_nr < pool->min_nr) {
  230. add_element(pool, element);
  231. spin_unlock_irqrestore(&pool->lock, flags);
  232. wake_up(&pool->wait);
  233. return;
  234. }
  235. spin_unlock_irqrestore(&pool->lock, flags);
  236. }
  237. pool->free(element, pool->pool_data);
  238. }
  239. EXPORT_SYMBOL(mempool_free);
  240. /*
  241. * A commonly used alloc and free fn.
  242. */
  243. void *mempool_alloc_slab(unsigned int __nocast gfp_mask, void *pool_data)
  244. {
  245. kmem_cache_t *mem = (kmem_cache_t *) pool_data;
  246. return kmem_cache_alloc(mem, gfp_mask);
  247. }
  248. EXPORT_SYMBOL(mempool_alloc_slab);
  249. void mempool_free_slab(void *element, void *pool_data)
  250. {
  251. kmem_cache_t *mem = (kmem_cache_t *) pool_data;
  252. kmem_cache_free(mem, element);
  253. }
  254. EXPORT_SYMBOL(mempool_free_slab);