mempool.c 16 KB

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