mempool.c 11 KB

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