mempool.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_nowait = gfp_mask & ~(__GFP_WAIT | __GFP_IO);
  186. might_sleep_if(gfp_mask & __GFP_WAIT);
  187. repeat_alloc:
  188. element = pool->alloc(gfp_nowait|__GFP_NOWARN, pool->pool_data);
  189. if (likely(element != NULL))
  190. return element;
  191. /*
  192. * If the pool is less than 50% full and we can perform effective
  193. * page reclaim then try harder to allocate an element.
  194. */
  195. mb();
  196. if ((gfp_mask & __GFP_FS) && (gfp_mask != gfp_nowait) &&
  197. (pool->curr_nr <= pool->min_nr/2)) {
  198. element = pool->alloc(gfp_mask, pool->pool_data);
  199. if (likely(element != NULL))
  200. return element;
  201. }
  202. /*
  203. * Kick the VM at this point.
  204. */
  205. wakeup_bdflush(0);
  206. spin_lock_irqsave(&pool->lock, flags);
  207. if (likely(pool->curr_nr)) {
  208. element = remove_element(pool);
  209. spin_unlock_irqrestore(&pool->lock, flags);
  210. return element;
  211. }
  212. spin_unlock_irqrestore(&pool->lock, flags);
  213. /* We must not sleep in the GFP_ATOMIC case */
  214. if (!(gfp_mask & __GFP_WAIT))
  215. return NULL;
  216. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  217. mb();
  218. if (!pool->curr_nr)
  219. io_schedule();
  220. finish_wait(&pool->wait, &wait);
  221. goto repeat_alloc;
  222. }
  223. EXPORT_SYMBOL(mempool_alloc);
  224. /**
  225. * mempool_free - return an element to the pool.
  226. * @element: pool element pointer.
  227. * @pool: pointer to the memory pool which was allocated via
  228. * mempool_create().
  229. *
  230. * this function only sleeps if the free_fn() function sleeps.
  231. */
  232. void mempool_free(void *element, mempool_t *pool)
  233. {
  234. unsigned long flags;
  235. mb();
  236. if (pool->curr_nr < pool->min_nr) {
  237. spin_lock_irqsave(&pool->lock, flags);
  238. if (pool->curr_nr < pool->min_nr) {
  239. add_element(pool, element);
  240. spin_unlock_irqrestore(&pool->lock, flags);
  241. wake_up(&pool->wait);
  242. return;
  243. }
  244. spin_unlock_irqrestore(&pool->lock, flags);
  245. }
  246. pool->free(element, pool->pool_data);
  247. }
  248. EXPORT_SYMBOL(mempool_free);
  249. /*
  250. * A commonly used alloc and free fn.
  251. */
  252. void *mempool_alloc_slab(unsigned int __nocast gfp_mask, void *pool_data)
  253. {
  254. kmem_cache_t *mem = (kmem_cache_t *) pool_data;
  255. return kmem_cache_alloc(mem, gfp_mask);
  256. }
  257. EXPORT_SYMBOL(mempool_alloc_slab);
  258. void mempool_free_slab(void *element, void *pool_data)
  259. {
  260. kmem_cache_t *mem = (kmem_cache_t *) pool_data;
  261. kmem_cache_free(mem, element);
  262. }
  263. EXPORT_SYMBOL(mempool_free_slab);