mempool.c 14 KB

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