mempool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. /*
  203. * We use gfp mask w/o __GFP_WAIT or IO for the first round. If
  204. * alloc failed with that and @pool was empty, retry immediately.
  205. */
  206. if (gfp_temp != gfp_mask) {
  207. spin_unlock_irqrestore(&pool->lock, flags);
  208. gfp_temp = gfp_mask;
  209. goto repeat_alloc;
  210. }
  211. /* We must not sleep if !__GFP_WAIT */
  212. if (!(gfp_mask & __GFP_WAIT)) {
  213. spin_unlock_irqrestore(&pool->lock, flags);
  214. return NULL;
  215. }
  216. /* Let's wait for someone else to return an element to @pool */
  217. init_wait(&wait);
  218. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  219. spin_unlock_irqrestore(&pool->lock, flags);
  220. /*
  221. * FIXME: this should be io_schedule(). The timeout is there as a
  222. * workaround for some DM problems in 2.6.18.
  223. */
  224. io_schedule_timeout(5*HZ);
  225. finish_wait(&pool->wait, &wait);
  226. goto repeat_alloc;
  227. }
  228. EXPORT_SYMBOL(mempool_alloc);
  229. /**
  230. * mempool_free - return an element to the pool.
  231. * @element: pool element pointer.
  232. * @pool: pointer to the memory pool which was allocated via
  233. * mempool_create().
  234. *
  235. * this function only sleeps if the free_fn() function sleeps.
  236. */
  237. void mempool_free(void *element, mempool_t *pool)
  238. {
  239. unsigned long flags;
  240. if (unlikely(element == NULL))
  241. return;
  242. /*
  243. * Paired with the wmb in mempool_alloc(). The preceding read is
  244. * for @element and the following @pool->curr_nr. This ensures
  245. * that the visible value of @pool->curr_nr is from after the
  246. * allocation of @element. This is necessary for fringe cases
  247. * where @element was passed to this task without going through
  248. * barriers.
  249. *
  250. * For example, assume @p is %NULL at the beginning and one task
  251. * performs "p = mempool_alloc(...);" while another task is doing
  252. * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
  253. * may end up using curr_nr value which is from before allocation
  254. * of @p without the following rmb.
  255. */
  256. smp_rmb();
  257. /*
  258. * For correctness, we need a test which is guaranteed to trigger
  259. * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
  260. * without locking achieves that and refilling as soon as possible
  261. * is desirable.
  262. *
  263. * Because curr_nr visible here is always a value after the
  264. * allocation of @element, any task which decremented curr_nr below
  265. * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
  266. * incremented to min_nr afterwards. If curr_nr gets incremented
  267. * to min_nr after the allocation of @element, the elements
  268. * allocated after that are subject to the same guarantee.
  269. *
  270. * Waiters happen iff curr_nr is 0 and the above guarantee also
  271. * ensures that there will be frees which return elements to the
  272. * pool waking up the waiters.
  273. */
  274. if (pool->curr_nr < pool->min_nr) {
  275. spin_lock_irqsave(&pool->lock, flags);
  276. if (pool->curr_nr < pool->min_nr) {
  277. add_element(pool, element);
  278. spin_unlock_irqrestore(&pool->lock, flags);
  279. wake_up(&pool->wait);
  280. return;
  281. }
  282. spin_unlock_irqrestore(&pool->lock, flags);
  283. }
  284. pool->free(element, pool->pool_data);
  285. }
  286. EXPORT_SYMBOL(mempool_free);
  287. /*
  288. * A commonly used alloc and free fn.
  289. */
  290. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  291. {
  292. struct kmem_cache *mem = pool_data;
  293. return kmem_cache_alloc(mem, gfp_mask);
  294. }
  295. EXPORT_SYMBOL(mempool_alloc_slab);
  296. void mempool_free_slab(void *element, void *pool_data)
  297. {
  298. struct kmem_cache *mem = pool_data;
  299. kmem_cache_free(mem, element);
  300. }
  301. EXPORT_SYMBOL(mempool_free_slab);
  302. /*
  303. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  304. * specified by pool_data
  305. */
  306. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  307. {
  308. size_t size = (size_t)pool_data;
  309. return kmalloc(size, gfp_mask);
  310. }
  311. EXPORT_SYMBOL(mempool_kmalloc);
  312. void mempool_kfree(void *element, void *pool_data)
  313. {
  314. kfree(element);
  315. }
  316. EXPORT_SYMBOL(mempool_kfree);
  317. /*
  318. * A simple mempool-backed page allocator that allocates pages
  319. * of the order specified by pool_data.
  320. */
  321. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  322. {
  323. int order = (int)(long)pool_data;
  324. return alloc_pages(gfp_mask, order);
  325. }
  326. EXPORT_SYMBOL(mempool_alloc_pages);
  327. void mempool_free_pages(void *element, void *pool_data)
  328. {
  329. int order = (int)(long)pool_data;
  330. __free_pages(element, order);
  331. }
  332. EXPORT_SYMBOL(mempool_free_pages);