mempool.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * memory buffer pool support. Such pools are mostly used
  4. * for guaranteed, deadlock-free memory allocations during
  5. * extreme VM load.
  6. *
  7. * started by Ingo Molnar, Copyright (C) 2001
  8. * debugging by David Rientjes, Copyright (C) 2015
  9. */
  10. #include <linux/fault-inject.h>
  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/writeback.h>
  19. #include "slab.h"
  20. static DECLARE_FAULT_ATTR(fail_mempool_alloc);
  21. static DECLARE_FAULT_ATTR(fail_mempool_alloc_bulk);
  22. static int __init mempool_faul_inject_init(void)
  23. {
  24. int error;
  25. error = PTR_ERR_OR_ZERO(fault_create_debugfs_attr("fail_mempool_alloc",
  26. NULL, &fail_mempool_alloc));
  27. if (error)
  28. return error;
  29. /* booting will fail on error return here, don't bother to cleanup */
  30. return PTR_ERR_OR_ZERO(
  31. fault_create_debugfs_attr("fail_mempool_alloc_bulk", NULL,
  32. &fail_mempool_alloc_bulk));
  33. }
  34. late_initcall(mempool_faul_inject_init);
  35. #ifdef CONFIG_SLUB_DEBUG_ON
  36. static void poison_error(struct mempool *pool, void *element, size_t size,
  37. size_t byte)
  38. {
  39. const int nr = pool->curr_nr;
  40. const int start = max_t(int, byte - (BITS_PER_LONG / 8), 0);
  41. const int end = min_t(int, byte + (BITS_PER_LONG / 8), size);
  42. int i;
  43. pr_err("BUG: mempool element poison mismatch\n");
  44. pr_err("Mempool %p size %zu\n", pool, size);
  45. pr_err(" nr=%d @ %p: %s0x", nr, element, start > 0 ? "... " : "");
  46. for (i = start; i < end; i++)
  47. pr_cont("%x ", *(u8 *)(element + i));
  48. pr_cont("%s\n", end < size ? "..." : "");
  49. dump_stack();
  50. }
  51. static void __check_element(struct mempool *pool, void *element, size_t size)
  52. {
  53. u8 *obj = element;
  54. size_t i;
  55. for (i = 0; i < size; i++) {
  56. u8 exp = (i < size - 1) ? POISON_FREE : POISON_END;
  57. if (obj[i] != exp) {
  58. poison_error(pool, element, size, i);
  59. return;
  60. }
  61. }
  62. memset(obj, POISON_INUSE, size);
  63. }
  64. static void check_element(struct mempool *pool, void *element)
  65. {
  66. /* Skip checking: KASAN might save its metadata in the element. */
  67. if (kasan_enabled())
  68. return;
  69. /* Mempools backed by slab allocator */
  70. if (pool->free == mempool_kfree) {
  71. __check_element(pool, element, (size_t)pool->pool_data);
  72. } else if (pool->free == mempool_free_slab) {
  73. __check_element(pool, element, kmem_cache_size(pool->pool_data));
  74. } else if (pool->free == mempool_free_pages) {
  75. /* Mempools backed by page allocator */
  76. int order = (int)(long)pool->pool_data;
  77. #ifdef CONFIG_HIGHMEM
  78. for (int i = 0; i < (1 << order); i++) {
  79. struct page *page = (struct page *)element;
  80. void *addr = kmap_local_page(page + i);
  81. __check_element(pool, addr, PAGE_SIZE);
  82. kunmap_local(addr);
  83. }
  84. #else
  85. void *addr = page_address((struct page *)element);
  86. __check_element(pool, addr, PAGE_SIZE << order);
  87. #endif
  88. }
  89. }
  90. static void __poison_element(void *element, size_t size)
  91. {
  92. u8 *obj = element;
  93. memset(obj, POISON_FREE, size - 1);
  94. obj[size - 1] = POISON_END;
  95. }
  96. static void poison_element(struct mempool *pool, void *element)
  97. {
  98. /* Skip poisoning: KASAN might save its metadata in the element. */
  99. if (kasan_enabled())
  100. return;
  101. /* Mempools backed by slab allocator */
  102. if (pool->alloc == mempool_kmalloc) {
  103. __poison_element(element, (size_t)pool->pool_data);
  104. } else if (pool->alloc == mempool_alloc_slab) {
  105. __poison_element(element, kmem_cache_size(pool->pool_data));
  106. } else if (pool->alloc == mempool_alloc_pages) {
  107. /* Mempools backed by page allocator */
  108. int order = (int)(long)pool->pool_data;
  109. #ifdef CONFIG_HIGHMEM
  110. for (int i = 0; i < (1 << order); i++) {
  111. struct page *page = (struct page *)element;
  112. void *addr = kmap_local_page(page + i);
  113. __poison_element(addr, PAGE_SIZE);
  114. kunmap_local(addr);
  115. }
  116. #else
  117. void *addr = page_address((struct page *)element);
  118. __poison_element(addr, PAGE_SIZE << order);
  119. #endif
  120. }
  121. }
  122. #else /* CONFIG_SLUB_DEBUG_ON */
  123. static inline void check_element(struct mempool *pool, void *element)
  124. {
  125. }
  126. static inline void poison_element(struct mempool *pool, void *element)
  127. {
  128. }
  129. #endif /* CONFIG_SLUB_DEBUG_ON */
  130. static __always_inline bool kasan_poison_element(struct mempool *pool,
  131. void *element)
  132. {
  133. if (pool->alloc == mempool_alloc_slab || pool->alloc == mempool_kmalloc)
  134. return kasan_mempool_poison_object(element);
  135. else if (pool->alloc == mempool_alloc_pages)
  136. return kasan_mempool_poison_pages(element,
  137. (unsigned long)pool->pool_data);
  138. return true;
  139. }
  140. static void kasan_unpoison_element(struct mempool *pool, void *element)
  141. {
  142. if (pool->alloc == mempool_kmalloc)
  143. kasan_mempool_unpoison_object(element, (size_t)pool->pool_data);
  144. else if (pool->alloc == mempool_alloc_slab)
  145. kasan_mempool_unpoison_object(element,
  146. kmem_cache_size(pool->pool_data));
  147. else if (pool->alloc == mempool_alloc_pages)
  148. kasan_mempool_unpoison_pages(element,
  149. (unsigned long)pool->pool_data);
  150. }
  151. static __always_inline void add_element(struct mempool *pool, void *element)
  152. {
  153. BUG_ON(pool->min_nr != 0 && pool->curr_nr >= pool->min_nr);
  154. poison_element(pool, element);
  155. if (kasan_poison_element(pool, element))
  156. pool->elements[pool->curr_nr++] = element;
  157. }
  158. static void *remove_element(struct mempool *pool)
  159. {
  160. void *element = pool->elements[--pool->curr_nr];
  161. BUG_ON(pool->curr_nr < 0);
  162. kasan_unpoison_element(pool, element);
  163. check_element(pool, element);
  164. return element;
  165. }
  166. /**
  167. * mempool_exit - exit a mempool initialized with mempool_init()
  168. * @pool: pointer to the memory pool which was initialized with
  169. * mempool_init().
  170. *
  171. * Free all reserved elements in @pool and @pool itself. This function
  172. * only sleeps if the free_fn() function sleeps.
  173. *
  174. * May be called on a zeroed but uninitialized mempool (i.e. allocated with
  175. * kzalloc()).
  176. */
  177. void mempool_exit(struct mempool *pool)
  178. {
  179. while (pool->curr_nr) {
  180. void *element = remove_element(pool);
  181. pool->free(element, pool->pool_data);
  182. }
  183. kfree(pool->elements);
  184. pool->elements = NULL;
  185. }
  186. EXPORT_SYMBOL(mempool_exit);
  187. /**
  188. * mempool_destroy - deallocate a memory pool
  189. * @pool: pointer to the memory pool which was allocated via
  190. * mempool_create().
  191. *
  192. * Free all reserved elements in @pool and @pool itself. This function
  193. * only sleeps if the free_fn() function sleeps.
  194. */
  195. void mempool_destroy(struct mempool *pool)
  196. {
  197. if (unlikely(!pool))
  198. return;
  199. mempool_exit(pool);
  200. kfree(pool);
  201. }
  202. EXPORT_SYMBOL(mempool_destroy);
  203. int mempool_init_node(struct mempool *pool, int min_nr,
  204. mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
  205. void *pool_data, gfp_t gfp_mask, int node_id)
  206. {
  207. spin_lock_init(&pool->lock);
  208. pool->min_nr = min_nr;
  209. pool->pool_data = pool_data;
  210. pool->alloc = alloc_fn;
  211. pool->free = free_fn;
  212. init_waitqueue_head(&pool->wait);
  213. /*
  214. * max() used here to ensure storage for at least 1 element to support
  215. * zero minimum pool
  216. */
  217. pool->elements = kmalloc_array_node(max(1, min_nr), sizeof(void *),
  218. gfp_mask, node_id);
  219. if (!pool->elements)
  220. return -ENOMEM;
  221. /*
  222. * First pre-allocate the guaranteed number of buffers,
  223. * also pre-allocate 1 element for zero minimum pool.
  224. */
  225. while (pool->curr_nr < max(1, pool->min_nr)) {
  226. void *element;
  227. element = pool->alloc(gfp_mask, pool->pool_data);
  228. if (unlikely(!element)) {
  229. mempool_exit(pool);
  230. return -ENOMEM;
  231. }
  232. add_element(pool, element);
  233. }
  234. return 0;
  235. }
  236. EXPORT_SYMBOL(mempool_init_node);
  237. /**
  238. * mempool_init - initialize a memory pool
  239. * @pool: pointer to the memory pool that should be initialized
  240. * @min_nr: the minimum number of elements guaranteed to be
  241. * allocated for this pool.
  242. * @alloc_fn: user-defined element-allocation function.
  243. * @free_fn: user-defined element-freeing function.
  244. * @pool_data: optional private data available to the user-defined functions.
  245. *
  246. * Like mempool_create(), but initializes the pool in (i.e. embedded in another
  247. * structure).
  248. *
  249. * Return: %0 on success, negative error code otherwise.
  250. */
  251. int mempool_init_noprof(struct mempool *pool, int min_nr,
  252. mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
  253. void *pool_data)
  254. {
  255. return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
  256. pool_data, GFP_KERNEL, NUMA_NO_NODE);
  257. }
  258. EXPORT_SYMBOL(mempool_init_noprof);
  259. /**
  260. * mempool_create_node - create a memory pool
  261. * @min_nr: the minimum number of elements guaranteed to be
  262. * allocated for this pool.
  263. * @alloc_fn: user-defined element-allocation function.
  264. * @free_fn: user-defined element-freeing function.
  265. * @pool_data: optional private data available to the user-defined functions.
  266. * @gfp_mask: memory allocation flags
  267. * @node_id: numa node to allocate on
  268. *
  269. * this function creates and allocates a guaranteed size, preallocated
  270. * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
  271. * functions. This function might sleep. Both the alloc_fn() and the free_fn()
  272. * functions might sleep - as long as the mempool_alloc() function is not called
  273. * from IRQ contexts.
  274. *
  275. * Return: pointer to the created memory pool object or %NULL on error.
  276. */
  277. struct mempool *mempool_create_node_noprof(int min_nr,
  278. mempool_alloc_t *alloc_fn, mempool_free_t *free_fn,
  279. void *pool_data, gfp_t gfp_mask, int node_id)
  280. {
  281. struct mempool *pool;
  282. pool = kmalloc_node_noprof(sizeof(*pool), gfp_mask | __GFP_ZERO, node_id);
  283. if (!pool)
  284. return NULL;
  285. if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
  286. gfp_mask, node_id)) {
  287. kfree(pool);
  288. return NULL;
  289. }
  290. return pool;
  291. }
  292. EXPORT_SYMBOL(mempool_create_node_noprof);
  293. /**
  294. * mempool_resize - resize an existing memory pool
  295. * @pool: pointer to the memory pool which was allocated via
  296. * mempool_create().
  297. * @new_min_nr: the new minimum number of elements guaranteed to be
  298. * allocated for this pool.
  299. *
  300. * This function shrinks/grows the pool. In the case of growing,
  301. * it cannot be guaranteed that the pool will be grown to the new
  302. * size immediately, but new mempool_free() calls will refill it.
  303. * This function may sleep.
  304. *
  305. * Note, the caller must guarantee that no mempool_destroy is called
  306. * while this function is running. mempool_alloc() & mempool_free()
  307. * might be called (eg. from IRQ contexts) while this function executes.
  308. *
  309. * Return: %0 on success, negative error code otherwise.
  310. */
  311. int mempool_resize(struct mempool *pool, int new_min_nr)
  312. {
  313. void *element;
  314. void **new_elements;
  315. unsigned long flags;
  316. BUG_ON(new_min_nr <= 0);
  317. might_sleep();
  318. spin_lock_irqsave(&pool->lock, flags);
  319. if (new_min_nr <= pool->min_nr) {
  320. while (new_min_nr < pool->curr_nr) {
  321. element = remove_element(pool);
  322. spin_unlock_irqrestore(&pool->lock, flags);
  323. pool->free(element, pool->pool_data);
  324. spin_lock_irqsave(&pool->lock, flags);
  325. }
  326. pool->min_nr = new_min_nr;
  327. goto out_unlock;
  328. }
  329. spin_unlock_irqrestore(&pool->lock, flags);
  330. /* Grow the pool */
  331. new_elements = kmalloc_objs(*new_elements, new_min_nr);
  332. if (!new_elements)
  333. return -ENOMEM;
  334. spin_lock_irqsave(&pool->lock, flags);
  335. if (unlikely(new_min_nr <= pool->min_nr)) {
  336. /* Raced, other resize will do our work */
  337. spin_unlock_irqrestore(&pool->lock, flags);
  338. kfree(new_elements);
  339. goto out;
  340. }
  341. memcpy(new_elements, pool->elements,
  342. pool->curr_nr * sizeof(*new_elements));
  343. kfree(pool->elements);
  344. pool->elements = new_elements;
  345. pool->min_nr = new_min_nr;
  346. while (pool->curr_nr < pool->min_nr) {
  347. spin_unlock_irqrestore(&pool->lock, flags);
  348. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  349. if (!element)
  350. goto out;
  351. spin_lock_irqsave(&pool->lock, flags);
  352. if (pool->curr_nr < pool->min_nr) {
  353. add_element(pool, element);
  354. } else {
  355. spin_unlock_irqrestore(&pool->lock, flags);
  356. pool->free(element, pool->pool_data); /* Raced */
  357. goto out;
  358. }
  359. }
  360. out_unlock:
  361. spin_unlock_irqrestore(&pool->lock, flags);
  362. out:
  363. return 0;
  364. }
  365. EXPORT_SYMBOL(mempool_resize);
  366. static unsigned int mempool_alloc_from_pool(struct mempool *pool, void **elems,
  367. unsigned int count, unsigned int allocated,
  368. gfp_t gfp_mask)
  369. {
  370. unsigned long flags;
  371. unsigned int i;
  372. spin_lock_irqsave(&pool->lock, flags);
  373. if (unlikely(pool->curr_nr < count - allocated))
  374. goto fail;
  375. for (i = 0; i < count; i++) {
  376. if (!elems[i]) {
  377. elems[i] = remove_element(pool);
  378. allocated++;
  379. }
  380. }
  381. spin_unlock_irqrestore(&pool->lock, flags);
  382. /* Paired with rmb in mempool_free(), read comment there. */
  383. smp_wmb();
  384. /*
  385. * Update the allocation stack trace as this is more useful for
  386. * debugging.
  387. */
  388. for (i = 0; i < count; i++)
  389. kmemleak_update_trace(elems[i]);
  390. return allocated;
  391. fail:
  392. if (gfp_mask & __GFP_DIRECT_RECLAIM) {
  393. DEFINE_WAIT(wait);
  394. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  395. spin_unlock_irqrestore(&pool->lock, flags);
  396. /*
  397. * Wait for someone else to return an element to @pool, but wake
  398. * up occasionally as memory pressure might have reduced even
  399. * and the normal allocation in alloc_fn could succeed even if
  400. * no element was returned.
  401. */
  402. io_schedule_timeout(5 * HZ);
  403. finish_wait(&pool->wait, &wait);
  404. } else {
  405. /* We must not sleep if __GFP_DIRECT_RECLAIM is not set. */
  406. spin_unlock_irqrestore(&pool->lock, flags);
  407. }
  408. return allocated;
  409. }
  410. /*
  411. * Adjust the gfp flags for mempool allocations, as we never want to dip into
  412. * the global emergency reserves or retry in the page allocator.
  413. *
  414. * The first pass also doesn't want to go reclaim, but the next passes do, so
  415. * return a separate subset for that first iteration.
  416. */
  417. static inline gfp_t mempool_adjust_gfp(gfp_t *gfp_mask)
  418. {
  419. *gfp_mask |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN;
  420. return *gfp_mask & ~(__GFP_DIRECT_RECLAIM | __GFP_IO);
  421. }
  422. /**
  423. * mempool_alloc_bulk - allocate multiple elements from a memory pool
  424. * @pool: pointer to the memory pool
  425. * @elems: partially or fully populated elements array
  426. * @count: number of entries in @elem that need to be allocated
  427. * @allocated: number of entries in @elem already allocated
  428. *
  429. * Allocate elements for each slot in @elem that is non-%NULL. This is done by
  430. * first calling into the alloc_fn supplied at pool initialization time, and
  431. * dipping into the reserved pool when alloc_fn fails to allocate an element.
  432. *
  433. * On return all @count elements in @elems will be populated.
  434. *
  435. * Return: Always 0. If it wasn't for %$#^$ alloc tags, it would return void.
  436. */
  437. int mempool_alloc_bulk_noprof(struct mempool *pool, void **elems,
  438. unsigned int count, unsigned int allocated)
  439. {
  440. gfp_t gfp_mask = GFP_KERNEL;
  441. gfp_t gfp_temp = mempool_adjust_gfp(&gfp_mask);
  442. unsigned int i = 0;
  443. VM_WARN_ON_ONCE(count > pool->min_nr);
  444. might_alloc(gfp_mask);
  445. /*
  446. * If an error is injected, fail all elements in a bulk allocation so
  447. * that we stress the multiple elements missing path.
  448. */
  449. if (should_fail_ex(&fail_mempool_alloc_bulk, 1, FAULT_NOWARN)) {
  450. pr_info("forcing mempool usage for %pS\n",
  451. (void *)_RET_IP_);
  452. goto use_pool;
  453. }
  454. repeat_alloc:
  455. /*
  456. * Try to allocate the elements using the allocation callback first as
  457. * that might succeed even when the caller's bulk allocation did not.
  458. */
  459. for (i = 0; i < count; i++) {
  460. if (elems[i])
  461. continue;
  462. elems[i] = pool->alloc(gfp_temp, pool->pool_data);
  463. if (unlikely(!elems[i]))
  464. goto use_pool;
  465. allocated++;
  466. }
  467. return 0;
  468. use_pool:
  469. allocated = mempool_alloc_from_pool(pool, elems, count, allocated,
  470. gfp_temp);
  471. gfp_temp = gfp_mask;
  472. goto repeat_alloc;
  473. }
  474. EXPORT_SYMBOL_GPL(mempool_alloc_bulk_noprof);
  475. /**
  476. * mempool_alloc - allocate an element from a memory pool
  477. * @pool: pointer to the memory pool
  478. * @gfp_mask: GFP_* flags. %__GFP_ZERO is not supported.
  479. *
  480. * Allocate an element from @pool. This is done by first calling into the
  481. * alloc_fn supplied at pool initialization time, and dipping into the reserved
  482. * pool when alloc_fn fails to allocate an element.
  483. *
  484. * This function only sleeps if the alloc_fn callback sleeps, or when waiting
  485. * for elements to become available in the pool.
  486. *
  487. * Return: pointer to the allocated element or %NULL when failing to allocate
  488. * an element. Allocation failure can only happen when @gfp_mask does not
  489. * include %__GFP_DIRECT_RECLAIM.
  490. */
  491. void *mempool_alloc_noprof(struct mempool *pool, gfp_t gfp_mask)
  492. {
  493. gfp_t gfp_temp = mempool_adjust_gfp(&gfp_mask);
  494. void *element;
  495. VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
  496. might_alloc(gfp_mask);
  497. repeat_alloc:
  498. if (should_fail_ex(&fail_mempool_alloc, 1, FAULT_NOWARN)) {
  499. pr_info("forcing mempool usage for %pS\n",
  500. (void *)_RET_IP_);
  501. element = NULL;
  502. } else {
  503. element = pool->alloc(gfp_temp, pool->pool_data);
  504. }
  505. if (unlikely(!element)) {
  506. /*
  507. * Try to allocate an element from the pool.
  508. *
  509. * The first pass won't have __GFP_DIRECT_RECLAIM and won't
  510. * sleep in mempool_alloc_from_pool. Retry the allocation
  511. * with all flags set in that case.
  512. */
  513. if (!mempool_alloc_from_pool(pool, &element, 1, 0, gfp_temp)) {
  514. if (gfp_temp != gfp_mask) {
  515. gfp_temp = gfp_mask;
  516. goto repeat_alloc;
  517. }
  518. if (gfp_mask & __GFP_DIRECT_RECLAIM) {
  519. goto repeat_alloc;
  520. }
  521. }
  522. }
  523. return element;
  524. }
  525. EXPORT_SYMBOL(mempool_alloc_noprof);
  526. /**
  527. * mempool_alloc_preallocated - allocate an element from preallocated elements
  528. * belonging to a memory pool
  529. * @pool: pointer to the memory pool
  530. *
  531. * This function is similar to mempool_alloc(), but it only attempts allocating
  532. * an element from the preallocated elements. It only takes a single spinlock_t
  533. * and immediately returns if no preallocated elements are available.
  534. *
  535. * Return: pointer to the allocated element or %NULL if no elements are
  536. * available.
  537. */
  538. void *mempool_alloc_preallocated(struct mempool *pool)
  539. {
  540. void *element = NULL;
  541. mempool_alloc_from_pool(pool, &element, 1, 0, GFP_NOWAIT);
  542. return element;
  543. }
  544. EXPORT_SYMBOL(mempool_alloc_preallocated);
  545. /**
  546. * mempool_free_bulk - return elements to a mempool
  547. * @pool: pointer to the memory pool
  548. * @elems: elements to return
  549. * @count: number of elements to return
  550. *
  551. * Returns a number of elements from the start of @elem to @pool if @pool needs
  552. * replenishing and sets their slots in @elem to NULL. Other elements are left
  553. * in @elem.
  554. *
  555. * Return: number of elements transferred to @pool. Elements are always
  556. * transferred from the beginning of @elem, so the return value can be used as
  557. * an offset into @elem for the freeing the remaining elements in the caller.
  558. */
  559. unsigned int mempool_free_bulk(struct mempool *pool, void **elems,
  560. unsigned int count)
  561. {
  562. unsigned long flags;
  563. unsigned int freed = 0;
  564. bool added = false;
  565. /*
  566. * Paired with the wmb in mempool_alloc(). The preceding read is
  567. * for @element and the following @pool->curr_nr. This ensures
  568. * that the visible value of @pool->curr_nr is from after the
  569. * allocation of @element. This is necessary for fringe cases
  570. * where @element was passed to this task without going through
  571. * barriers.
  572. *
  573. * For example, assume @p is %NULL at the beginning and one task
  574. * performs "p = mempool_alloc(...);" while another task is doing
  575. * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
  576. * may end up using curr_nr value which is from before allocation
  577. * of @p without the following rmb.
  578. */
  579. smp_rmb();
  580. /*
  581. * For correctness, we need a test which is guaranteed to trigger
  582. * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
  583. * without locking achieves that and refilling as soon as possible
  584. * is desirable.
  585. *
  586. * Because curr_nr visible here is always a value after the
  587. * allocation of @element, any task which decremented curr_nr below
  588. * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
  589. * incremented to min_nr afterwards. If curr_nr gets incremented
  590. * to min_nr after the allocation of @element, the elements
  591. * allocated after that are subject to the same guarantee.
  592. *
  593. * Waiters happen iff curr_nr is 0 and the above guarantee also
  594. * ensures that there will be frees which return elements to the
  595. * pool waking up the waiters.
  596. *
  597. * For zero-minimum pools, curr_nr < min_nr (0 < 0) never succeeds,
  598. * so waiters sleeping on pool->wait would never be woken by the
  599. * wake-up path of previous test. This explicit check ensures the
  600. * allocation of element when both min_nr and curr_nr are 0, and
  601. * any active waiters are properly awakened.
  602. */
  603. if (unlikely(READ_ONCE(pool->curr_nr) < pool->min_nr)) {
  604. spin_lock_irqsave(&pool->lock, flags);
  605. while (pool->curr_nr < pool->min_nr && freed < count) {
  606. add_element(pool, elems[freed++]);
  607. added = true;
  608. }
  609. spin_unlock_irqrestore(&pool->lock, flags);
  610. } else if (unlikely(pool->min_nr == 0 &&
  611. READ_ONCE(pool->curr_nr) == 0)) {
  612. /* Handle the min_nr = 0 edge case: */
  613. spin_lock_irqsave(&pool->lock, flags);
  614. if (likely(pool->curr_nr == 0)) {
  615. add_element(pool, elems[freed++]);
  616. added = true;
  617. }
  618. spin_unlock_irqrestore(&pool->lock, flags);
  619. }
  620. if (unlikely(added) && wq_has_sleeper(&pool->wait))
  621. wake_up(&pool->wait);
  622. return freed;
  623. }
  624. EXPORT_SYMBOL_GPL(mempool_free_bulk);
  625. /**
  626. * mempool_free - return an element to the pool.
  627. * @element: element to return
  628. * @pool: pointer to the memory pool
  629. *
  630. * Returns @element to @pool if it needs replenishing, else frees it using
  631. * the free_fn callback in @pool.
  632. *
  633. * This function only sleeps if the free_fn callback sleeps.
  634. */
  635. void mempool_free(void *element, struct mempool *pool)
  636. {
  637. if (likely(element) && !mempool_free_bulk(pool, &element, 1))
  638. pool->free(element, pool->pool_data);
  639. }
  640. EXPORT_SYMBOL(mempool_free);
  641. /*
  642. * A commonly used alloc and free fn.
  643. */
  644. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  645. {
  646. struct kmem_cache *mem = pool_data;
  647. VM_BUG_ON(mem->ctor);
  648. return kmem_cache_alloc_noprof(mem, gfp_mask);
  649. }
  650. EXPORT_SYMBOL(mempool_alloc_slab);
  651. void mempool_free_slab(void *element, void *pool_data)
  652. {
  653. struct kmem_cache *mem = pool_data;
  654. kmem_cache_free(mem, element);
  655. }
  656. EXPORT_SYMBOL(mempool_free_slab);
  657. /*
  658. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  659. * specified by pool_data
  660. */
  661. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  662. {
  663. size_t size = (size_t)pool_data;
  664. return kmalloc_noprof(size, gfp_mask);
  665. }
  666. EXPORT_SYMBOL(mempool_kmalloc);
  667. void mempool_kfree(void *element, void *pool_data)
  668. {
  669. kfree(element);
  670. }
  671. EXPORT_SYMBOL(mempool_kfree);
  672. /*
  673. * A simple mempool-backed page allocator that allocates pages
  674. * of the order specified by pool_data.
  675. */
  676. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  677. {
  678. int order = (int)(long)pool_data;
  679. return alloc_pages_noprof(gfp_mask, order);
  680. }
  681. EXPORT_SYMBOL(mempool_alloc_pages);
  682. void mempool_free_pages(void *element, void *pool_data)
  683. {
  684. int order = (int)(long)pool_data;
  685. __free_pages(element, order);
  686. }
  687. EXPORT_SYMBOL(mempool_free_pages);