mempool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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/export.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. /**
  27. * mempool_destroy - deallocate a memory pool
  28. * @pool: pointer to the memory pool which was allocated via
  29. * mempool_create().
  30. *
  31. * Free all reserved elements in @pool and @pool itself. This function
  32. * only sleeps if the free_fn() function sleeps.
  33. */
  34. void mempool_destroy(mempool_t *pool)
  35. {
  36. while (pool->curr_nr) {
  37. void *element = remove_element(pool);
  38. pool->free(element, pool->pool_data);
  39. }
  40. kfree(pool->elements);
  41. kfree(pool);
  42. }
  43. EXPORT_SYMBOL(mempool_destroy);
  44. /**
  45. * mempool_create - create a memory pool
  46. * @min_nr: the minimum number of elements guaranteed to be
  47. * allocated for this pool.
  48. * @alloc_fn: user-defined element-allocation function.
  49. * @free_fn: user-defined element-freeing function.
  50. * @pool_data: optional private data available to the user-defined functions.
  51. *
  52. * this function creates and allocates a guaranteed size, preallocated
  53. * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
  54. * functions. This function might sleep. Both the alloc_fn() and the free_fn()
  55. * functions might sleep - as long as the mempool_alloc() function is not called
  56. * from IRQ contexts.
  57. */
  58. mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
  59. mempool_free_t *free_fn, void *pool_data)
  60. {
  61. return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,-1);
  62. }
  63. EXPORT_SYMBOL(mempool_create);
  64. mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
  65. mempool_free_t *free_fn, void *pool_data, int node_id)
  66. {
  67. mempool_t *pool;
  68. pool = kmalloc_node(sizeof(*pool), GFP_KERNEL | __GFP_ZERO, node_id);
  69. if (!pool)
  70. return NULL;
  71. pool->elements = kmalloc_node(min_nr * sizeof(void *),
  72. GFP_KERNEL, node_id);
  73. if (!pool->elements) {
  74. kfree(pool);
  75. return NULL;
  76. }
  77. spin_lock_init(&pool->lock);
  78. pool->min_nr = min_nr;
  79. pool->pool_data = pool_data;
  80. init_waitqueue_head(&pool->wait);
  81. pool->alloc = alloc_fn;
  82. pool->free = free_fn;
  83. /*
  84. * First pre-allocate the guaranteed number of buffers.
  85. */
  86. while (pool->curr_nr < pool->min_nr) {
  87. void *element;
  88. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  89. if (unlikely(!element)) {
  90. mempool_destroy(pool);
  91. return NULL;
  92. }
  93. add_element(pool, element);
  94. }
  95. return pool;
  96. }
  97. EXPORT_SYMBOL(mempool_create_node);
  98. /**
  99. * mempool_resize - resize an existing memory pool
  100. * @pool: pointer to the memory pool which was allocated via
  101. * mempool_create().
  102. * @new_min_nr: the new minimum number of elements guaranteed to be
  103. * allocated for this pool.
  104. * @gfp_mask: the usual allocation bitmask.
  105. *
  106. * This function shrinks/grows the pool. In the case of growing,
  107. * it cannot be guaranteed that the pool will be grown to the new
  108. * size immediately, but new mempool_free() calls will refill it.
  109. *
  110. * Note, the caller must guarantee that no mempool_destroy is called
  111. * while this function is running. mempool_alloc() & mempool_free()
  112. * might be called (eg. from IRQ contexts) while this function executes.
  113. */
  114. int mempool_resize(mempool_t *pool, int new_min_nr, gfp_t gfp_mask)
  115. {
  116. void *element;
  117. void **new_elements;
  118. unsigned long flags;
  119. BUG_ON(new_min_nr <= 0);
  120. spin_lock_irqsave(&pool->lock, flags);
  121. if (new_min_nr <= pool->min_nr) {
  122. while (new_min_nr < pool->curr_nr) {
  123. element = remove_element(pool);
  124. spin_unlock_irqrestore(&pool->lock, flags);
  125. pool->free(element, pool->pool_data);
  126. spin_lock_irqsave(&pool->lock, flags);
  127. }
  128. pool->min_nr = new_min_nr;
  129. goto out_unlock;
  130. }
  131. spin_unlock_irqrestore(&pool->lock, flags);
  132. /* Grow the pool */
  133. new_elements = kmalloc(new_min_nr * sizeof(*new_elements), gfp_mask);
  134. if (!new_elements)
  135. return -ENOMEM;
  136. spin_lock_irqsave(&pool->lock, flags);
  137. if (unlikely(new_min_nr <= pool->min_nr)) {
  138. /* Raced, other resize will do our work */
  139. spin_unlock_irqrestore(&pool->lock, flags);
  140. kfree(new_elements);
  141. goto out;
  142. }
  143. memcpy(new_elements, pool->elements,
  144. pool->curr_nr * sizeof(*new_elements));
  145. kfree(pool->elements);
  146. pool->elements = new_elements;
  147. pool->min_nr = new_min_nr;
  148. while (pool->curr_nr < pool->min_nr) {
  149. spin_unlock_irqrestore(&pool->lock, flags);
  150. element = pool->alloc(gfp_mask, pool->pool_data);
  151. if (!element)
  152. goto out;
  153. spin_lock_irqsave(&pool->lock, flags);
  154. if (pool->curr_nr < pool->min_nr) {
  155. add_element(pool, element);
  156. } else {
  157. spin_unlock_irqrestore(&pool->lock, flags);
  158. pool->free(element, pool->pool_data); /* Raced */
  159. goto out;
  160. }
  161. }
  162. out_unlock:
  163. spin_unlock_irqrestore(&pool->lock, flags);
  164. out:
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(mempool_resize);
  168. /**
  169. * mempool_alloc - allocate an element from a specific memory pool
  170. * @pool: pointer to the memory pool which was allocated via
  171. * mempool_create().
  172. * @gfp_mask: the usual allocation bitmask.
  173. *
  174. * this function only sleeps if the alloc_fn() function sleeps or
  175. * returns NULL. Note that due to preallocation, this function
  176. * *never* fails when called from process contexts. (it might
  177. * fail if called from an IRQ context.)
  178. */
  179. void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
  180. {
  181. void *element;
  182. unsigned long flags;
  183. wait_queue_t wait;
  184. gfp_t gfp_temp;
  185. might_sleep_if(gfp_mask & __GFP_WAIT);
  186. gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
  187. gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
  188. gfp_mask |= __GFP_NOWARN; /* failures are OK */
  189. gfp_temp = gfp_mask & ~(__GFP_WAIT|__GFP_IO);
  190. repeat_alloc:
  191. element = pool->alloc(gfp_temp, pool->pool_data);
  192. if (likely(element != NULL))
  193. return element;
  194. spin_lock_irqsave(&pool->lock, flags);
  195. if (likely(pool->curr_nr)) {
  196. element = remove_element(pool);
  197. spin_unlock_irqrestore(&pool->lock, flags);
  198. /* paired with rmb in mempool_free(), read comment there */
  199. smp_wmb();
  200. return element;
  201. }
  202. /* We must not sleep in the GFP_ATOMIC case */
  203. if (!(gfp_mask & __GFP_WAIT)) {
  204. spin_unlock_irqrestore(&pool->lock, flags);
  205. return NULL;
  206. }
  207. /* Let's wait for someone else to return an element to @pool */
  208. gfp_temp = gfp_mask;
  209. init_wait(&wait);
  210. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  211. spin_unlock_irqrestore(&pool->lock, flags);
  212. /*
  213. * FIXME: this should be io_schedule(). The timeout is there as a
  214. * workaround for some DM problems in 2.6.18.
  215. */
  216. io_schedule_timeout(5*HZ);
  217. finish_wait(&pool->wait, &wait);
  218. goto repeat_alloc;
  219. }
  220. EXPORT_SYMBOL(mempool_alloc);
  221. /**
  222. * mempool_free - return an element to the pool.
  223. * @element: pool element pointer.
  224. * @pool: pointer to the memory pool which was allocated via
  225. * mempool_create().
  226. *
  227. * this function only sleeps if the free_fn() function sleeps.
  228. */
  229. void mempool_free(void *element, mempool_t *pool)
  230. {
  231. unsigned long flags;
  232. if (unlikely(element == NULL))
  233. return;
  234. /*
  235. * Paired with the wmb in mempool_alloc(). The preceding read is
  236. * for @element and the following @pool->curr_nr. This ensures
  237. * that the visible value of @pool->curr_nr is from after the
  238. * allocation of @element. This is necessary for fringe cases
  239. * where @element was passed to this task without going through
  240. * barriers.
  241. *
  242. * For example, assume @p is %NULL at the beginning and one task
  243. * performs "p = mempool_alloc(...);" while another task is doing
  244. * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
  245. * may end up using curr_nr value which is from before allocation
  246. * of @p without the following rmb.
  247. */
  248. smp_rmb();
  249. /*
  250. * For correctness, we need a test which is guaranteed to trigger
  251. * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
  252. * without locking achieves that and refilling as soon as possible
  253. * is desirable.
  254. *
  255. * Because curr_nr visible here is always a value after the
  256. * allocation of @element, any task which decremented curr_nr below
  257. * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
  258. * incremented to min_nr afterwards. If curr_nr gets incremented
  259. * to min_nr after the allocation of @element, the elements
  260. * allocated after that are subject to the same guarantee.
  261. *
  262. * Waiters happen iff curr_nr is 0 and the above guarantee also
  263. * ensures that there will be frees which return elements to the
  264. * pool waking up the waiters.
  265. */
  266. if (pool->curr_nr < pool->min_nr) {
  267. spin_lock_irqsave(&pool->lock, flags);
  268. if (pool->curr_nr < pool->min_nr) {
  269. add_element(pool, element);
  270. spin_unlock_irqrestore(&pool->lock, flags);
  271. wake_up(&pool->wait);
  272. return;
  273. }
  274. spin_unlock_irqrestore(&pool->lock, flags);
  275. }
  276. pool->free(element, pool->pool_data);
  277. }
  278. EXPORT_SYMBOL(mempool_free);
  279. /*
  280. * A commonly used alloc and free fn.
  281. */
  282. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  283. {
  284. struct kmem_cache *mem = pool_data;
  285. return kmem_cache_alloc(mem, gfp_mask);
  286. }
  287. EXPORT_SYMBOL(mempool_alloc_slab);
  288. void mempool_free_slab(void *element, void *pool_data)
  289. {
  290. struct kmem_cache *mem = pool_data;
  291. kmem_cache_free(mem, element);
  292. }
  293. EXPORT_SYMBOL(mempool_free_slab);
  294. /*
  295. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  296. * specified by pool_data
  297. */
  298. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  299. {
  300. size_t size = (size_t)pool_data;
  301. return kmalloc(size, gfp_mask);
  302. }
  303. EXPORT_SYMBOL(mempool_kmalloc);
  304. void mempool_kfree(void *element, void *pool_data)
  305. {
  306. kfree(element);
  307. }
  308. EXPORT_SYMBOL(mempool_kfree);
  309. /*
  310. * A simple mempool-backed page allocator that allocates pages
  311. * of the order specified by pool_data.
  312. */
  313. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  314. {
  315. int order = (int)(long)pool_data;
  316. return alloc_pages(gfp_mask, order);
  317. }
  318. EXPORT_SYMBOL(mempool_alloc_pages);
  319. void mempool_free_pages(void *element, void *pool_data)
  320. {
  321. int order = (int)(long)pool_data;
  322. __free_pages(element, order);
  323. }
  324. EXPORT_SYMBOL(mempool_free_pages);