mempool.c 10 KB

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