mempool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. * debugging by David Rientjes, Copyright (C) 2015
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/slab.h>
  13. #include <linux/highmem.h>
  14. #include <linux/kasan.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/export.h>
  17. #include <linux/mempool.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/writeback.h>
  20. #include "slab.h"
  21. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_SLUB_DEBUG_ON)
  22. static void poison_error(mempool_t *pool, void *element, size_t size,
  23. size_t byte)
  24. {
  25. const int nr = pool->curr_nr;
  26. const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
  27. const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
  28. int i;
  29. pr_err("BUG: mempool element poison mismatch\n");
  30. pr_err("Mempool %p size %zu\n", pool, size);
  31. pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
  32. for (i = start; i < end; i++)
  33. pr_cont("%x ", *(u8 *)(element + i));
  34. pr_cont("%s\n", end < size ? "..." : "");
  35. dump_stack();
  36. }
  37. static void __check_element(mempool_t *pool, void *element, size_t size)
  38. {
  39. u8 *obj = element;
  40. size_t i;
  41. for (i = 0; i < size; i++) {
  42. u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
  43. if (obj[i] != exp) {
  44. poison_error(pool, element, size, i);
  45. return;
  46. }
  47. }
  48. memset(obj, POISON_INUSE, size);
  49. }
  50. static void check_element(mempool_t *pool, void *element)
  51. {
  52. /* Mempools backed by slab allocator */
  53. if (pool->free == mempool_free_slab || pool->free == mempool_kfree)
  54. __check_element(pool, element, ksize(element));
  55. /* Mempools backed by page allocator */
  56. if (pool->free == mempool_free_pages) {
  57. int order = (int)(long)pool->pool_data;
  58. void *addr = kmap_atomic((struct page *)element);
  59. __check_element(pool, addr, 1UL << (PAGE_SHIFT + order));
  60. kunmap_atomic(addr);
  61. }
  62. }
  63. static void __poison_element(void *element, size_t size)
  64. {
  65. u8 *obj = element;
  66. memset(obj, POISON_FREE, size - 1);
  67. obj[size - 1] = POISON_END;
  68. }
  69. static void poison_element(mempool_t *pool, void *element)
  70. {
  71. /* Mempools backed by slab allocator */
  72. if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
  73. __poison_element(element, ksize(element));
  74. /* Mempools backed by page allocator */
  75. if (pool->alloc == mempool_alloc_pages) {
  76. int order = (int)(long)pool->pool_data;
  77. void *addr = kmap_atomic((struct page *)element);
  78. __poison_element(addr, 1UL << (PAGE_SHIFT + order));
  79. kunmap_atomic(addr);
  80. }
  81. }
  82. #else /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
  83. static inline void check_element(mempool_t *pool, void *element)
  84. {
  85. }
  86. static inline void poison_element(mempool_t *pool, void *element)
  87. {
  88. }
  89. #endif /* CONFIG_DEBUG_SLAB || CONFIG_SLUB_DEBUG_ON */
  90. static void kasan_poison_element(mempool_t *pool, void *element)
  91. {
  92. if (pool->alloc == mempool_alloc_slab)
  93. kasan_slab_free(pool->pool_data, element);
  94. if (pool->alloc == mempool_kmalloc)
  95. kasan_kfree(element);
  96. if (pool->alloc == mempool_alloc_pages)
  97. kasan_free_pages(element, (unsigned long)pool->pool_data);
  98. }
  99. static void kasan_unpoison_element(mempool_t *pool, void *element, gfp_t flags)
  100. {
  101. if (pool->alloc == mempool_alloc_slab)
  102. kasan_slab_alloc(pool->pool_data, element, flags);
  103. if (pool->alloc == mempool_kmalloc)
  104. kasan_krealloc(element, (size_t)pool->pool_data, flags);
  105. if (pool->alloc == mempool_alloc_pages)
  106. kasan_alloc_pages(element, (unsigned long)pool->pool_data);
  107. }
  108. static void add_element(mempool_t *pool, void *element)
  109. {
  110. BUG_ON(pool->curr_nr >= pool->min_nr);
  111. poison_element(pool, element);
  112. kasan_poison_element(pool, element);
  113. pool->elements[pool->curr_nr++] = element;
  114. }
  115. static void *remove_element(mempool_t *pool, gfp_t flags)
  116. {
  117. void *element = pool->elements[--pool->curr_nr];
  118. BUG_ON(pool->curr_nr < 0);
  119. kasan_unpoison_element(pool, element, flags);
  120. check_element(pool, element);
  121. return element;
  122. }
  123. /**
  124. * mempool_destroy - deallocate a memory pool
  125. * @pool: pointer to the memory pool which was allocated via
  126. * mempool_create().
  127. *
  128. * Free all reserved elements in @pool and @pool itself. This function
  129. * only sleeps if the free_fn() function sleeps.
  130. */
  131. void mempool_destroy(mempool_t *pool)
  132. {
  133. if (unlikely(!pool))
  134. return;
  135. while (pool->curr_nr) {
  136. void *element = remove_element(pool, GFP_KERNEL);
  137. pool->free(element, pool->pool_data);
  138. }
  139. kfree(pool->elements);
  140. kfree(pool);
  141. }
  142. EXPORT_SYMBOL(mempool_destroy);
  143. /**
  144. * mempool_create - create a memory pool
  145. * @min_nr: the minimum number of elements guaranteed to be
  146. * allocated for this pool.
  147. * @alloc_fn: user-defined element-allocation function.
  148. * @free_fn: user-defined element-freeing function.
  149. * @pool_data: optional private data available to the user-defined functions.
  150. *
  151. * this function creates and allocates a guaranteed size, preallocated
  152. * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
  153. * functions. This function might sleep. Both the alloc_fn() and the free_fn()
  154. * functions might sleep - as long as the mempool_alloc() function is not called
  155. * from IRQ contexts.
  156. */
  157. mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
  158. mempool_free_t *free_fn, void *pool_data)
  159. {
  160. return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
  161. GFP_KERNEL, NUMA_NO_NODE);
  162. }
  163. EXPORT_SYMBOL(mempool_create);
  164. mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
  165. mempool_free_t *free_fn, void *pool_data,
  166. gfp_t gfp_mask, int node_id)
  167. {
  168. mempool_t *pool;
  169. pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
  170. if (!pool)
  171. return NULL;
  172. pool->elements = kmalloc_node(min_nr * sizeof(void *),
  173. gfp_mask, node_id);
  174. if (!pool->elements) {
  175. kfree(pool);
  176. return NULL;
  177. }
  178. spin_lock_init(&pool->lock);
  179. pool->min_nr = min_nr;
  180. pool->pool_data = pool_data;
  181. init_waitqueue_head(&pool->wait);
  182. pool->alloc = alloc_fn;
  183. pool->free = free_fn;
  184. /*
  185. * First pre-allocate the guaranteed number of buffers.
  186. */
  187. while (pool->curr_nr < pool->min_nr) {
  188. void *element;
  189. element = pool->alloc(gfp_mask, pool->pool_data);
  190. if (unlikely(!element)) {
  191. mempool_destroy(pool);
  192. return NULL;
  193. }
  194. add_element(pool, element);
  195. }
  196. return pool;
  197. }
  198. EXPORT_SYMBOL(mempool_create_node);
  199. /**
  200. * mempool_resize - resize an existing memory pool
  201. * @pool: pointer to the memory pool which was allocated via
  202. * mempool_create().
  203. * @new_min_nr: the new minimum number of elements guaranteed to be
  204. * allocated for this pool.
  205. *
  206. * This function shrinks/grows the pool. In the case of growing,
  207. * it cannot be guaranteed that the pool will be grown to the new
  208. * size immediately, but new mempool_free() calls will refill it.
  209. * This function may sleep.
  210. *
  211. * Note, the caller must guarantee that no mempool_destroy is called
  212. * while this function is running. mempool_alloc() & mempool_free()
  213. * might be called (eg. from IRQ contexts) while this function executes.
  214. */
  215. int mempool_resize(mempool_t *pool, int new_min_nr)
  216. {
  217. void *element;
  218. void **new_elements;
  219. unsigned long flags;
  220. BUG_ON(new_min_nr <= 0);
  221. might_sleep();
  222. spin_lock_irqsave(&pool->lock, flags);
  223. if (new_min_nr <= pool->min_nr) {
  224. while (new_min_nr < pool->curr_nr) {
  225. element = remove_element(pool, GFP_KERNEL);
  226. spin_unlock_irqrestore(&pool->lock, flags);
  227. pool->free(element, pool->pool_data);
  228. spin_lock_irqsave(&pool->lock, flags);
  229. }
  230. pool->min_nr = new_min_nr;
  231. goto out_unlock;
  232. }
  233. spin_unlock_irqrestore(&pool->lock, flags);
  234. /* Grow the pool */
  235. new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
  236. GFP_KERNEL);
  237. if (!new_elements)
  238. return -ENOMEM;
  239. spin_lock_irqsave(&pool->lock, flags);
  240. if (unlikely(new_min_nr <= pool->min_nr)) {
  241. /* Raced, other resize will do our work */
  242. spin_unlock_irqrestore(&pool->lock, flags);
  243. kfree(new_elements);
  244. goto out;
  245. }
  246. memcpy(new_elements, pool->elements,
  247. pool->curr_nr * sizeof(*new_elements));
  248. kfree(pool->elements);
  249. pool->elements = new_elements;
  250. pool->min_nr = new_min_nr;
  251. while (pool->curr_nr < pool->min_nr) {
  252. spin_unlock_irqrestore(&pool->lock, flags);
  253. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  254. if (!element)
  255. goto out;
  256. spin_lock_irqsave(&pool->lock, flags);
  257. if (pool->curr_nr < pool->min_nr) {
  258. add_element(pool, element);
  259. } else {
  260. spin_unlock_irqrestore(&pool->lock, flags);
  261. pool->free(element, pool->pool_data); /* Raced */
  262. goto out;
  263. }
  264. }
  265. out_unlock:
  266. spin_unlock_irqrestore(&pool->lock, flags);
  267. out:
  268. return 0;
  269. }
  270. EXPORT_SYMBOL(mempool_resize);
  271. /**
  272. * mempool_alloc - allocate an element from a specific memory pool
  273. * @pool: pointer to the memory pool which was allocated via
  274. * mempool_create().
  275. * @gfp_mask: the usual allocation bitmask.
  276. *
  277. * this function only sleeps if the alloc_fn() function sleeps or
  278. * returns NULL. Note that due to preallocation, this function
  279. * *never* fails when called from process contexts. (it might
  280. * fail if called from an IRQ context.)
  281. * Note: neither __GFP_NOMEMALLOC nor __GFP_ZERO are supported.
  282. */
  283. void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
  284. {
  285. void *element;
  286. unsigned long flags;
  287. wait_queue_t wait;
  288. gfp_t gfp_temp;
  289. /* If oom killed, memory reserves are essential to prevent livelock */
  290. VM_WARN_ON_ONCE(gfp_mask & __GFP_NOMEMALLOC);
  291. /* No element size to zero on allocation */
  292. VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
  293. might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
  294. gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
  295. gfp_mask |= __GFP_NOWARN; /* failures are OK */
  296. gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
  297. repeat_alloc:
  298. if (likely(pool->curr_nr)) {
  299. /*
  300. * Don't allocate from emergency reserves if there are
  301. * elements available. This check is racy, but it will
  302. * be rechecked each loop.
  303. */
  304. gfp_temp |= __GFP_NOMEMALLOC;
  305. }
  306. element = pool->alloc(gfp_temp, pool->pool_data);
  307. if (likely(element != NULL))
  308. return element;
  309. spin_lock_irqsave(&pool->lock, flags);
  310. if (likely(pool->curr_nr)) {
  311. element = remove_element(pool, gfp_temp);
  312. spin_unlock_irqrestore(&pool->lock, flags);
  313. /* paired with rmb in mempool_free(), read comment there */
  314. smp_wmb();
  315. /*
  316. * Update the allocation stack trace as this is more useful
  317. * for debugging.
  318. */
  319. kmemleak_update_trace(element);
  320. return element;
  321. }
  322. /*
  323. * We use gfp mask w/o direct reclaim or IO for the first round. If
  324. * alloc failed with that and @pool was empty, retry immediately.
  325. */
  326. if ((gfp_temp & ~__GFP_NOMEMALLOC) != gfp_mask) {
  327. spin_unlock_irqrestore(&pool->lock, flags);
  328. gfp_temp = gfp_mask;
  329. goto repeat_alloc;
  330. }
  331. gfp_temp = gfp_mask;
  332. /* We must not sleep if !__GFP_DIRECT_RECLAIM */
  333. if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
  334. spin_unlock_irqrestore(&pool->lock, flags);
  335. return NULL;
  336. }
  337. /* Let's wait for someone else to return an element to @pool */
  338. init_wait(&wait);
  339. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  340. spin_unlock_irqrestore(&pool->lock, flags);
  341. /*
  342. * FIXME: this should be io_schedule(). The timeout is there as a
  343. * workaround for some DM problems in 2.6.18.
  344. */
  345. io_schedule_timeout(5*HZ);
  346. finish_wait(&pool->wait, &wait);
  347. goto repeat_alloc;
  348. }
  349. EXPORT_SYMBOL(mempool_alloc);
  350. /**
  351. * mempool_free - return an element to the pool.
  352. * @element: pool element pointer.
  353. * @pool: pointer to the memory pool which was allocated via
  354. * mempool_create().
  355. *
  356. * this function only sleeps if the free_fn() function sleeps.
  357. */
  358. void mempool_free(void *element, mempool_t *pool)
  359. {
  360. unsigned long flags;
  361. if (unlikely(element == NULL))
  362. return;
  363. /*
  364. * Paired with the wmb in mempool_alloc(). The preceding read is
  365. * for @element and the following @pool->curr_nr. This ensures
  366. * that the visible value of @pool->curr_nr is from after the
  367. * allocation of @element. This is necessary for fringe cases
  368. * where @element was passed to this task without going through
  369. * barriers.
  370. *
  371. * For example, assume @p is %NULL at the beginning and one task
  372. * performs "p = mempool_alloc(...);" while another task is doing
  373. * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
  374. * may end up using curr_nr value which is from before allocation
  375. * of @p without the following rmb.
  376. */
  377. smp_rmb();
  378. /*
  379. * For correctness, we need a test which is guaranteed to trigger
  380. * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
  381. * without locking achieves that and refilling as soon as possible
  382. * is desirable.
  383. *
  384. * Because curr_nr visible here is always a value after the
  385. * allocation of @element, any task which decremented curr_nr below
  386. * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
  387. * incremented to min_nr afterwards. If curr_nr gets incremented
  388. * to min_nr after the allocation of @element, the elements
  389. * allocated after that are subject to the same guarantee.
  390. *
  391. * Waiters happen iff curr_nr is 0 and the above guarantee also
  392. * ensures that there will be frees which return elements to the
  393. * pool waking up the waiters.
  394. */
  395. if (unlikely(pool->curr_nr < pool->min_nr)) {
  396. spin_lock_irqsave(&pool->lock, flags);
  397. if (likely(pool->curr_nr < pool->min_nr)) {
  398. add_element(pool, element);
  399. spin_unlock_irqrestore(&pool->lock, flags);
  400. wake_up(&pool->wait);
  401. return;
  402. }
  403. spin_unlock_irqrestore(&pool->lock, flags);
  404. }
  405. pool->free(element, pool->pool_data);
  406. }
  407. EXPORT_SYMBOL(mempool_free);
  408. /*
  409. * A commonly used alloc and free fn.
  410. */
  411. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  412. {
  413. struct kmem_cache *mem = pool_data;
  414. VM_BUG_ON(mem->ctor);
  415. return kmem_cache_alloc(mem, gfp_mask);
  416. }
  417. EXPORT_SYMBOL(mempool_alloc_slab);
  418. void mempool_free_slab(void *element, void *pool_data)
  419. {
  420. struct kmem_cache *mem = pool_data;
  421. kmem_cache_free(mem, element);
  422. }
  423. EXPORT_SYMBOL(mempool_free_slab);
  424. /*
  425. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  426. * specified by pool_data
  427. */
  428. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  429. {
  430. size_t size = (size_t)pool_data;
  431. return kmalloc(size, gfp_mask);
  432. }
  433. EXPORT_SYMBOL(mempool_kmalloc);
  434. void mempool_kfree(void *element, void *pool_data)
  435. {
  436. kfree(element);
  437. }
  438. EXPORT_SYMBOL(mempool_kfree);
  439. /*
  440. * A simple mempool-backed page allocator that allocates pages
  441. * of the order specified by pool_data.
  442. */
  443. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  444. {
  445. int order = (int)(long)pool_data;
  446. return alloc_pages(gfp_mask, order);
  447. }
  448. EXPORT_SYMBOL(mempool_alloc_pages);
  449. void mempool_free_pages(void *element, void *pool_data)
  450. {
  451. int order = (int)(long)pool_data;
  452. __free_pages(element, order);
  453. }
  454. EXPORT_SYMBOL(mempool_free_pages);