mempool.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 __always_inline 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, _RET_IP_);
  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)
  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 __always_inline 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)
  113. {
  114. void *element = pool->elements[--pool->curr_nr];
  115. BUG_ON(pool->curr_nr < 0);
  116. kasan_unpoison_element(pool, element);
  117. check_element(pool, element);
  118. return element;
  119. }
  120. /**
  121. * mempool_exit - exit a mempool initialized with mempool_init()
  122. * @pool: pointer to the memory pool which was initialized with
  123. * mempool_init().
  124. *
  125. * Free all reserved elements in @pool and @pool itself. This function
  126. * only sleeps if the free_fn() function sleeps.
  127. *
  128. * May be called on a zeroed but uninitialized mempool (i.e. allocated with
  129. * kzalloc()).
  130. */
  131. void mempool_exit(mempool_t *pool)
  132. {
  133. while (pool->curr_nr) {
  134. void *element = remove_element(pool);
  135. pool->free(element, pool->pool_data);
  136. }
  137. kfree(pool->elements);
  138. pool->elements = NULL;
  139. }
  140. EXPORT_SYMBOL(mempool_exit);
  141. /**
  142. * mempool_destroy - deallocate a memory pool
  143. * @pool: pointer to the memory pool which was allocated via
  144. * mempool_create().
  145. *
  146. * Free all reserved elements in @pool and @pool itself. This function
  147. * only sleeps if the free_fn() function sleeps.
  148. */
  149. void mempool_destroy(mempool_t *pool)
  150. {
  151. if (unlikely(!pool))
  152. return;
  153. mempool_exit(pool);
  154. kfree(pool);
  155. }
  156. EXPORT_SYMBOL(mempool_destroy);
  157. int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
  158. mempool_free_t *free_fn, void *pool_data,
  159. gfp_t gfp_mask, int node_id)
  160. {
  161. spin_lock_init(&pool->lock);
  162. pool->min_nr = min_nr;
  163. pool->pool_data = pool_data;
  164. pool->alloc = alloc_fn;
  165. pool->free = free_fn;
  166. init_waitqueue_head(&pool->wait);
  167. pool->elements = kmalloc_array_node(min_nr, sizeof(void *),
  168. gfp_mask, node_id);
  169. if (!pool->elements)
  170. return -ENOMEM;
  171. /*
  172. * First pre-allocate the guaranteed number of buffers.
  173. */
  174. while (pool->curr_nr < pool->min_nr) {
  175. void *element;
  176. element = pool->alloc(gfp_mask, pool->pool_data);
  177. if (unlikely(!element)) {
  178. mempool_exit(pool);
  179. return -ENOMEM;
  180. }
  181. add_element(pool, element);
  182. }
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(mempool_init_node);
  186. /**
  187. * mempool_init - initialize a memory pool
  188. * @pool: pointer to the memory pool that should be initialized
  189. * @min_nr: the minimum number of elements guaranteed to be
  190. * allocated for this pool.
  191. * @alloc_fn: user-defined element-allocation function.
  192. * @free_fn: user-defined element-freeing function.
  193. * @pool_data: optional private data available to the user-defined functions.
  194. *
  195. * Like mempool_create(), but initializes the pool in (i.e. embedded in another
  196. * structure).
  197. */
  198. int mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
  199. mempool_free_t *free_fn, void *pool_data)
  200. {
  201. return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
  202. pool_data, GFP_KERNEL, NUMA_NO_NODE);
  203. }
  204. EXPORT_SYMBOL(mempool_init);
  205. /**
  206. * mempool_create - create a memory pool
  207. * @min_nr: the minimum number of elements guaranteed to be
  208. * allocated for this pool.
  209. * @alloc_fn: user-defined element-allocation function.
  210. * @free_fn: user-defined element-freeing function.
  211. * @pool_data: optional private data available to the user-defined functions.
  212. *
  213. * this function creates and allocates a guaranteed size, preallocated
  214. * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
  215. * functions. This function might sleep. Both the alloc_fn() and the free_fn()
  216. * functions might sleep - as long as the mempool_alloc() function is not called
  217. * from IRQ contexts.
  218. */
  219. mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
  220. mempool_free_t *free_fn, void *pool_data)
  221. {
  222. return mempool_create_node(min_nr,alloc_fn,free_fn, pool_data,
  223. GFP_KERNEL, NUMA_NO_NODE);
  224. }
  225. EXPORT_SYMBOL(mempool_create);
  226. mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
  227. mempool_free_t *free_fn, void *pool_data,
  228. gfp_t gfp_mask, int node_id)
  229. {
  230. mempool_t *pool;
  231. pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
  232. if (!pool)
  233. return NULL;
  234. if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
  235. gfp_mask, node_id)) {
  236. kfree(pool);
  237. return NULL;
  238. }
  239. return pool;
  240. }
  241. EXPORT_SYMBOL(mempool_create_node);
  242. /**
  243. * mempool_resize - resize an existing memory pool
  244. * @pool: pointer to the memory pool which was allocated via
  245. * mempool_create().
  246. * @new_min_nr: the new minimum number of elements guaranteed to be
  247. * allocated for this pool.
  248. *
  249. * This function shrinks/grows the pool. In the case of growing,
  250. * it cannot be guaranteed that the pool will be grown to the new
  251. * size immediately, but new mempool_free() calls will refill it.
  252. * This function may sleep.
  253. *
  254. * Note, the caller must guarantee that no mempool_destroy is called
  255. * while this function is running. mempool_alloc() & mempool_free()
  256. * might be called (eg. from IRQ contexts) while this function executes.
  257. */
  258. int mempool_resize(mempool_t *pool, int new_min_nr)
  259. {
  260. void *element;
  261. void **new_elements;
  262. unsigned long flags;
  263. BUG_ON(new_min_nr <= 0);
  264. might_sleep();
  265. spin_lock_irqsave(&pool->lock, flags);
  266. if (new_min_nr <= pool->min_nr) {
  267. while (new_min_nr < pool->curr_nr) {
  268. element = remove_element(pool);
  269. spin_unlock_irqrestore(&pool->lock, flags);
  270. pool->free(element, pool->pool_data);
  271. spin_lock_irqsave(&pool->lock, flags);
  272. }
  273. pool->min_nr = new_min_nr;
  274. goto out_unlock;
  275. }
  276. spin_unlock_irqrestore(&pool->lock, flags);
  277. /* Grow the pool */
  278. new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
  279. GFP_KERNEL);
  280. if (!new_elements)
  281. return -ENOMEM;
  282. spin_lock_irqsave(&pool->lock, flags);
  283. if (unlikely(new_min_nr <= pool->min_nr)) {
  284. /* Raced, other resize will do our work */
  285. spin_unlock_irqrestore(&pool->lock, flags);
  286. kfree(new_elements);
  287. goto out;
  288. }
  289. memcpy(new_elements, pool->elements,
  290. pool->curr_nr * sizeof(*new_elements));
  291. kfree(pool->elements);
  292. pool->elements = new_elements;
  293. pool->min_nr = new_min_nr;
  294. while (pool->curr_nr < pool->min_nr) {
  295. spin_unlock_irqrestore(&pool->lock, flags);
  296. element = pool->alloc(GFP_KERNEL, pool->pool_data);
  297. if (!element)
  298. goto out;
  299. spin_lock_irqsave(&pool->lock, flags);
  300. if (pool->curr_nr < pool->min_nr) {
  301. add_element(pool, element);
  302. } else {
  303. spin_unlock_irqrestore(&pool->lock, flags);
  304. pool->free(element, pool->pool_data); /* Raced */
  305. goto out;
  306. }
  307. }
  308. out_unlock:
  309. spin_unlock_irqrestore(&pool->lock, flags);
  310. out:
  311. return 0;
  312. }
  313. EXPORT_SYMBOL(mempool_resize);
  314. /**
  315. * mempool_alloc - allocate an element from a specific memory pool
  316. * @pool: pointer to the memory pool which was allocated via
  317. * mempool_create().
  318. * @gfp_mask: the usual allocation bitmask.
  319. *
  320. * this function only sleeps if the alloc_fn() function sleeps or
  321. * returns NULL. Note that due to preallocation, this function
  322. * *never* fails when called from process contexts. (it might
  323. * fail if called from an IRQ context.)
  324. * Note: using __GFP_ZERO is not supported.
  325. */
  326. void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)
  327. {
  328. void *element;
  329. unsigned long flags;
  330. wait_queue_entry_t wait;
  331. gfp_t gfp_temp;
  332. VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
  333. might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
  334. gfp_mask |= __GFP_NOMEMALLOC; /* don't allocate emergency reserves */
  335. gfp_mask |= __GFP_NORETRY; /* don't loop in __alloc_pages */
  336. gfp_mask |= __GFP_NOWARN; /* failures are OK */
  337. gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
  338. repeat_alloc:
  339. element = pool->alloc(gfp_temp, pool->pool_data);
  340. if (likely(element != NULL))
  341. return element;
  342. spin_lock_irqsave(&pool->lock, flags);
  343. if (likely(pool->curr_nr)) {
  344. element = remove_element(pool);
  345. spin_unlock_irqrestore(&pool->lock, flags);
  346. /* paired with rmb in mempool_free(), read comment there */
  347. smp_wmb();
  348. /*
  349. * Update the allocation stack trace as this is more useful
  350. * for debugging.
  351. */
  352. kmemleak_update_trace(element);
  353. return element;
  354. }
  355. /*
  356. * We use gfp mask w/o direct reclaim or IO for the first round. If
  357. * alloc failed with that and @pool was empty, retry immediately.
  358. */
  359. if (gfp_temp != gfp_mask) {
  360. spin_unlock_irqrestore(&pool->lock, flags);
  361. gfp_temp = gfp_mask;
  362. goto repeat_alloc;
  363. }
  364. /* We must not sleep if !__GFP_DIRECT_RECLAIM */
  365. if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
  366. spin_unlock_irqrestore(&pool->lock, flags);
  367. return NULL;
  368. }
  369. /* Let's wait for someone else to return an element to @pool */
  370. init_wait(&wait);
  371. prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
  372. spin_unlock_irqrestore(&pool->lock, flags);
  373. /*
  374. * FIXME: this should be io_schedule(). The timeout is there as a
  375. * workaround for some DM problems in 2.6.18.
  376. */
  377. io_schedule_timeout(5*HZ);
  378. finish_wait(&pool->wait, &wait);
  379. goto repeat_alloc;
  380. }
  381. EXPORT_SYMBOL(mempool_alloc);
  382. /**
  383. * mempool_free - return an element to the pool.
  384. * @element: pool element pointer.
  385. * @pool: pointer to the memory pool which was allocated via
  386. * mempool_create().
  387. *
  388. * this function only sleeps if the free_fn() function sleeps.
  389. */
  390. void mempool_free(void *element, mempool_t *pool)
  391. {
  392. unsigned long flags;
  393. if (unlikely(element == NULL))
  394. return;
  395. /*
  396. * Paired with the wmb in mempool_alloc(). The preceding read is
  397. * for @element and the following @pool->curr_nr. This ensures
  398. * that the visible value of @pool->curr_nr is from after the
  399. * allocation of @element. This is necessary for fringe cases
  400. * where @element was passed to this task without going through
  401. * barriers.
  402. *
  403. * For example, assume @p is %NULL at the beginning and one task
  404. * performs "p = mempool_alloc(...);" while another task is doing
  405. * "while (!p) cpu_relax(); mempool_free(p, ...);". This function
  406. * may end up using curr_nr value which is from before allocation
  407. * of @p without the following rmb.
  408. */
  409. smp_rmb();
  410. /*
  411. * For correctness, we need a test which is guaranteed to trigger
  412. * if curr_nr + #allocated == min_nr. Testing curr_nr < min_nr
  413. * without locking achieves that and refilling as soon as possible
  414. * is desirable.
  415. *
  416. * Because curr_nr visible here is always a value after the
  417. * allocation of @element, any task which decremented curr_nr below
  418. * min_nr is guaranteed to see curr_nr < min_nr unless curr_nr gets
  419. * incremented to min_nr afterwards. If curr_nr gets incremented
  420. * to min_nr after the allocation of @element, the elements
  421. * allocated after that are subject to the same guarantee.
  422. *
  423. * Waiters happen iff curr_nr is 0 and the above guarantee also
  424. * ensures that there will be frees which return elements to the
  425. * pool waking up the waiters.
  426. */
  427. if (unlikely(pool->curr_nr < pool->min_nr)) {
  428. spin_lock_irqsave(&pool->lock, flags);
  429. if (likely(pool->curr_nr < pool->min_nr)) {
  430. add_element(pool, element);
  431. spin_unlock_irqrestore(&pool->lock, flags);
  432. wake_up(&pool->wait);
  433. return;
  434. }
  435. spin_unlock_irqrestore(&pool->lock, flags);
  436. }
  437. pool->free(element, pool->pool_data);
  438. }
  439. EXPORT_SYMBOL(mempool_free);
  440. /*
  441. * A commonly used alloc and free fn.
  442. */
  443. void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data)
  444. {
  445. struct kmem_cache *mem = pool_data;
  446. VM_BUG_ON(mem->ctor);
  447. return kmem_cache_alloc(mem, gfp_mask);
  448. }
  449. EXPORT_SYMBOL(mempool_alloc_slab);
  450. void mempool_free_slab(void *element, void *pool_data)
  451. {
  452. struct kmem_cache *mem = pool_data;
  453. kmem_cache_free(mem, element);
  454. }
  455. EXPORT_SYMBOL(mempool_free_slab);
  456. /*
  457. * A commonly used alloc and free fn that kmalloc/kfrees the amount of memory
  458. * specified by pool_data
  459. */
  460. void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data)
  461. {
  462. size_t size = (size_t)pool_data;
  463. return kmalloc(size, gfp_mask);
  464. }
  465. EXPORT_SYMBOL(mempool_kmalloc);
  466. void mempool_kfree(void *element, void *pool_data)
  467. {
  468. kfree(element);
  469. }
  470. EXPORT_SYMBOL(mempool_kfree);
  471. /*
  472. * A simple mempool-backed page allocator that allocates pages
  473. * of the order specified by pool_data.
  474. */
  475. void *mempool_alloc_pages(gfp_t gfp_mask, void *pool_data)
  476. {
  477. int order = (int)(long)pool_data;
  478. return alloc_pages(gfp_mask, order);
  479. }
  480. EXPORT_SYMBOL(mempool_alloc_pages);
  481. void mempool_free_pages(void *element, void *pool_data)
  482. {
  483. int order = (int)(long)pool_data;
  484. __free_pages(element, order);
  485. }
  486. EXPORT_SYMBOL(mempool_free_pages);