mempool.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,-1);
  53. }
  54. EXPORT_SYMBOL(mempool_create);
  55. mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
  56. mempool_free_t *free_fn, void *pool_data, int node_id)
  57. {
  58. mempool_t *pool;
  59. pool = kmalloc_node(sizeof(*pool), GFP_KERNEL | __GFP_ZERO, node_id);
  60. if (!pool)
  61. return NULL;
  62. pool->elements = kmalloc_node(min_nr * sizeof(void *),
  63. GFP_KERNEL, node_id);
  64. if (!pool->elements) {
  65. kfree(pool);
  66. return NULL;
  67. }
  68. spin_lock_init(&pool->lock);
  69. pool->min_nr = min_nr;
  70. pool->pool_data = pool_data;
  71. init_waitqueue_head(&pool->wait);
  72. pool->alloc = alloc_fn;
  73. pool->free = free_fn;
  74. /*
  75. * First pre-allocate the guaranteed number of buffers.
  76. */
  77. while (pool->curr_nr < pool->min_nr) {
  78. void *element;
  79. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  80. if (unlikely(!element)) {
  81. free_pool(pool);
  82. return NULL;
  83. }
  84. add_element(pool, element);
  85. }
  86. return pool;
  87. }
  88. EXPORT_SYMBOL(mempool_create_node);
  89. /**
  90. * mempool_resize - resize an existing memory pool
  91. * @pool: pointer to the memory pool which was allocated via
  92. * mempool_create().
  93. * @new_min_nr: the new minimum number of elements guaranteed to be
  94. * allocated for this pool.
  95. * @gfp_mask: the usual allocation bitmask.
  96. *
  97. * This function shrinks/grows the pool. In the case of growing,
  98. * it cannot be guaranteed that the pool will be grown to the new
  99. * size immediately, but new mempool_free() calls will refill it.
  100. *
  101. * Note, the caller must guarantee that no mempool_destroy is called
  102. * while this function is running. mempool_alloc() & mempool_free()
  103. * might be called (eg. from IRQ contexts) while this function executes.
  104. */
  105. int mempool_resize(mempool_t *pool, int new_min_nr, gfp_t gfp_mask)
  106. {
  107. void *element;
  108. void **new_elements;
  109. unsigned long flags;
  110. BUG_ON(new_min_nr <= 0);
  111. spin_lock_irqsave(&pool->lock, flags);
  112. if (new_min_nr <= pool->min_nr) {
  113. while (new_min_nr < pool->curr_nr) {
  114. element = remove_element(pool);
  115. spin_unlock_irqrestore(&pool->lock, flags);
  116. pool->free(element, pool->pool_data);
  117. spin_lock_irqsave(&pool->lock, flags);
  118. }
  119. pool->min_nr = new_min_nr;
  120. goto out_unlock;
  121. }
  122. spin_unlock_irqrestore(&pool->lock, flags);
  123. /* Grow the pool */
  124. new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask);
  125. if (!new_elements)
  126. return -ENOMEM;
  127. spin_lock_irqsave(&pool->lock, flags);
  128. if (unlikely(new_min_nr <= pool->min_nr)) {
  129. /* Raced, other resize will do our work */
  130. spin_unlock_irqrestore(&pool->lock, flags);
  131. kfree(new_elements);
  132. goto out;
  133. }
  134. memcpy(new_elements, pool->elements,
  135. pool->curr_nr * sizeof(*new_elements));
  136. kfree(pool->elements);
  137. pool->elements = new_elements;
  138. pool->min_nr = new_min_nr;
  139. while (pool->curr_nr < pool->min_nr) {
  140. spin_unlock_irqrestore(&pool->lock, flags);
  141. element = pool->alloc(gfp_mask, pool->pool_data);
  142. if (!element)
  143. goto out;
  144. spin_lock_irqsave(&pool->lock, flags);
  145. if (pool->curr_nr < pool->min_nr) {
  146. add_element(pool, element);
  147. } else {
  148. spin_unlock_irqrestore(&pool->lock, flags);
  149. pool->free(element, pool->pool_data); /* Raced */
  150. goto out;
  151. }
  152. }
  153. out_unlock:
  154. spin_unlock_irqrestore(&pool->lock, flags);
  155. out:
  156. return 0;
  157. }
  158. EXPORT_SYMBOL(mempool_resize);
  159. /**
  160. * mempool_destroy - deallocate a memory pool
  161. * @pool: pointer to the memory pool which was allocated via
  162. * mempool_create().
  163. *
  164. * this function only sleeps if the free_fn() function sleeps. The caller
  165. * has to guarantee that all elements have been returned to the pool (ie:
  166. * freed) prior to calling mempool_destroy().
  167. */
  168. void mempool_destroy(mempool_t *pool)
  169. {
  170. /* Check for outstanding elements */
  171. BUG_ON(pool->curr_nr != pool->min_nr);
  172. free_pool(pool);
  173. }
  174. EXPORT_SYMBOL(mempool_destroy);
  175. /**
  176. * mempool_alloc - allocate an element from a specific memory pool
  177. * @pool: pointer to the memory pool which was allocated via
  178. * mempool_create().
  179. * @gfp_mask: the usual allocation bitmask.
  180. *
  181. * this function only sleeps if the alloc_fn() function sleeps or
  182. * returns NULL. Note that due to preallocation, this function
  183. * *never* fails when called from process contexts. (it might
  184. * fail if called from an IRQ context.)
  185. */
  186. void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
  187. {
  188. void *element;
  189. unsigned long flags;
  190. wait_queue_t wait;
  191. gfp_t gfp_temp;
  192. might_sleep_if(gfp_mask & __GFP_WAIT);
  193. gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
  194. gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
  195. gfp_mask |= __GFP_NOWARN; /* failures are OK */
  196. gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
  197. repeat_alloc:
  198. element = pool->alloc(gfp_temp, pool->pool_data);
  199. if (likely(element != NULL))
  200. return element;
  201. spin_lock_irqsave(&pool->lock, flags);
  202. if (likely(pool->curr_nr)) {
  203. element = remove_element(pool);
  204. spin_unlock_irqrestore(&pool->lock, flags);
  205. return element;
  206. }
  207. spin_unlock_irqrestore(&pool->lock, flags);
  208. /* We must not sleep in the GFP_ATOMIC case */
  209. if (!(gfp_mask & __GFP_WAIT))
  210. return NULL;
  211. /* Now start performing page reclaim */
  212. gfp_temp = gfp_mask;
  213. init_wait(&wait);
  214. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  215. smp_mb();
  216. if (!pool->curr_nr) {
  217. /*
  218. * FIXME: this should be io_schedule(). The timeout is there
  219. * as a workaround for some DM problems in 2.6.18.
  220. */
  221. io_schedule_timeout(5*HZ);
  222. }
  223. finish_wait(&pool->wait, &wait);
  224. goto repeat_alloc;
  225. }
  226. EXPORT_SYMBOL(mempool_alloc);
  227. /**
  228. * mempool_free - return an element to the pool.
  229. * @element: pool element pointer.
  230. * @pool: pointer to the memory pool which was allocated via
  231. * mempool_create().
  232. *
  233. * this function only sleeps if the free_fn() function sleeps.
  234. */
  235. void mempool_free(void *element, mempool_t *pool)
  236. {
  237. unsigned long flags;
  238. if (unlikely(element == NULL))
  239. return;
  240. smp_mb();
  241. if (pool->curr_nr < pool->min_nr) {
  242. spin_lock_irqsave(&pool->lock, flags);
  243. if (pool->curr_nr < pool->min_nr) {
  244. add_element(pool, element);
  245. spin_unlock_irqrestore(&pool->lock, flags);
  246. wake_up(&pool->wait);
  247. return;
  248. }
  249. spin_unlock_irqrestore(&pool->lock, flags);
  250. }
  251. pool->free(element, pool->pool_data);
  252. }
  253. EXPORT_SYMBOL(mempool_free);
  254. /*
  255. * A commonly used alloc and free fn.
  256. */
  257. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  258. {
  259. struct kmem_cache *mem = pool_data;
  260. return kmem_cache_alloc(mem, gfp_mask);
  261. }
  262. EXPORT_SYMBOL(mempool_alloc_slab);
  263. void mempool_free_slab(void *element, void *pool_data)
  264. {
  265. struct kmem_cache *mem = pool_data;
  266. kmem_cache_free(mem, element);
  267. }
  268. EXPORT_SYMBOL(mempool_free_slab);
  269. /*
  270. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  271. * specified by pool_data
  272. */
  273. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  274. {
  275. size_t size = (size_t)pool_data;
  276. return kmalloc(size, gfp_mask);
  277. }
  278. EXPORT_SYMBOL(mempool_kmalloc);
  279. void mempool_kfree(void *element, void *pool_data)
  280. {
  281. kfree(element);
  282. }
  283. EXPORT_SYMBOL(mempool_kfree);
  284. /*
  285. * A simple mempool-backed page allocator that allocates pages
  286. * of the order specified by pool_data.
  287. */
  288. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  289. {
  290. int order = (int)(long)pool_data;
  291. return alloc_pages(gfp_mask, order);
  292. }
  293. EXPORT_SYMBOL(mempool_alloc_pages);
  294. void mempool_free_pages(void *element, void *pool_data)
  295. {
  296. int order = (int)(long)pool_data;
  297. __free_pages(element, order);
  298. }
  299. EXPORT_SYMBOL(mempool_free_pages);