page_pool.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * page_pool.c
  4. * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
  5. * Copyright (C) 2016 Red Hat, Inc.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <net/page_pool.h>
  11. #include <linux/dma-direction.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/page-flags.h>
  14. #include <linux/mm.h> /* for __put_page() */
  15. static int page_pool_init(struct page_pool *pool,
  16. const struct page_pool_params *params)
  17. {
  18. unsigned int ring_qsize = 1024; /* Default */
  19. memcpy(&pool->p, params, sizeof(pool->p));
  20. /* Validate only known flags were used */
  21. if (pool->p.flags & ~(PP_FLAG_ALL))
  22. return -EINVAL;
  23. if (pool->p.pool_size)
  24. ring_qsize = pool->p.pool_size;
  25. /* Sanity limit mem that can be pinned down */
  26. if (ring_qsize > 32768)
  27. return -E2BIG;
  28. /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
  29. * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
  30. * which is the XDP_TX use-case.
  31. */
  32. if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
  33. (pool->p.dma_dir != DMA_BIDIRECTIONAL))
  34. return -EINVAL;
  35. if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
  36. return -ENOMEM;
  37. return 0;
  38. }
  39. struct page_pool *page_pool_create(const struct page_pool_params *params)
  40. {
  41. struct page_pool *pool;
  42. int err = 0;
  43. pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
  44. if (!pool)
  45. return ERR_PTR(-ENOMEM);
  46. err = page_pool_init(pool, params);
  47. if (err < 0) {
  48. pr_warn("%s() gave up with errno %d\n", __func__, err);
  49. kfree(pool);
  50. return ERR_PTR(err);
  51. }
  52. return pool;
  53. }
  54. EXPORT_SYMBOL(page_pool_create);
  55. /* fast path */
  56. static struct page *__page_pool_get_cached(struct page_pool *pool)
  57. {
  58. struct ptr_ring *r = &pool->ring;
  59. struct page *page;
  60. /* Quicker fallback, avoid locks when ring is empty */
  61. if (__ptr_ring_empty(r))
  62. return NULL;
  63. /* Test for safe-context, caller should provide this guarantee */
  64. if (likely(in_serving_softirq())) {
  65. if (likely(pool->alloc.count)) {
  66. /* Fast-path */
  67. page = pool->alloc.cache[--pool->alloc.count];
  68. return page;
  69. }
  70. /* Slower-path: Alloc array empty, time to refill
  71. *
  72. * Open-coded bulk ptr_ring consumer.
  73. *
  74. * Discussion: the ring consumer lock is not really
  75. * needed due to the softirq/NAPI protection, but
  76. * later need the ability to reclaim pages on the
  77. * ring. Thus, keeping the locks.
  78. */
  79. spin_lock(&r->consumer_lock);
  80. while ((page = __ptr_ring_consume(r))) {
  81. if (pool->alloc.count == PP_ALLOC_CACHE_REFILL)
  82. break;
  83. pool->alloc.cache[pool->alloc.count++] = page;
  84. }
  85. spin_unlock(&r->consumer_lock);
  86. return page;
  87. }
  88. /* Slow-path: Get page from locked ring queue */
  89. page = ptr_ring_consume(&pool->ring);
  90. return page;
  91. }
  92. /* slow path */
  93. noinline
  94. static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
  95. gfp_t _gfp)
  96. {
  97. struct page *page;
  98. gfp_t gfp = _gfp;
  99. dma_addr_t dma;
  100. /* We could always set __GFP_COMP, and avoid this branch, as
  101. * prep_new_page() can handle order-0 with __GFP_COMP.
  102. */
  103. if (pool->p.order)
  104. gfp |= __GFP_COMP;
  105. /* FUTURE development:
  106. *
  107. * Current slow-path essentially falls back to single page
  108. * allocations, which doesn't improve performance. This code
  109. * need bulk allocation support from the page allocator code.
  110. */
  111. /* Cache was empty, do real allocation */
  112. page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
  113. if (!page)
  114. return NULL;
  115. if (!(pool->p.flags & PP_FLAG_DMA_MAP))
  116. goto skip_dma_map;
  117. /* Setup DMA mapping: use page->private for DMA-addr
  118. * This mapping is kept for lifetime of page, until leaving pool.
  119. */
  120. dma = dma_map_page(pool->p.dev, page, 0,
  121. (PAGE_SIZE << pool->p.order),
  122. pool->p.dma_dir);
  123. if (dma_mapping_error(pool->p.dev, dma)) {
  124. put_page(page);
  125. return NULL;
  126. }
  127. set_page_private(page, dma); /* page->private = dma; */
  128. skip_dma_map:
  129. /* When page just alloc'ed is should/must have refcnt 1. */
  130. return page;
  131. }
  132. /* For using page_pool replace: alloc_pages() API calls, but provide
  133. * synchronization guarantee for allocation side.
  134. */
  135. struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
  136. {
  137. struct page *page;
  138. /* Fast-path: Get a page from cache */
  139. page = __page_pool_get_cached(pool);
  140. if (page)
  141. return page;
  142. /* Slow-path: cache empty, do real allocation */
  143. page = __page_pool_alloc_pages_slow(pool, gfp);
  144. return page;
  145. }
  146. EXPORT_SYMBOL(page_pool_alloc_pages);
  147. /* Cleanup page_pool state from page */
  148. static void __page_pool_clean_page(struct page_pool *pool,
  149. struct page *page)
  150. {
  151. if (!(pool->p.flags & PP_FLAG_DMA_MAP))
  152. return;
  153. /* DMA unmap */
  154. dma_unmap_page(pool->p.dev, page_private(page),
  155. PAGE_SIZE << pool->p.order, pool->p.dma_dir);
  156. set_page_private(page, 0);
  157. }
  158. /* Return a page to the page allocator, cleaning up our state */
  159. static void __page_pool_return_page(struct page_pool *pool, struct page *page)
  160. {
  161. __page_pool_clean_page(pool, page);
  162. put_page(page);
  163. /* An optimization would be to call __free_pages(page, pool->p.order)
  164. * knowing page is not part of page-cache (thus avoiding a
  165. * __page_cache_release() call).
  166. */
  167. }
  168. static bool __page_pool_recycle_into_ring(struct page_pool *pool,
  169. struct page *page)
  170. {
  171. int ret;
  172. /* BH protection not needed if current is serving softirq */
  173. if (in_serving_softirq())
  174. ret = ptr_ring_produce(&pool->ring, page);
  175. else
  176. ret = ptr_ring_produce_bh(&pool->ring, page);
  177. return (ret == 0) ? true : false;
  178. }
  179. /* Only allow direct recycling in special circumstances, into the
  180. * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
  181. *
  182. * Caller must provide appropriate safe context.
  183. */
  184. static bool __page_pool_recycle_direct(struct page *page,
  185. struct page_pool *pool)
  186. {
  187. if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE))
  188. return false;
  189. /* Caller MUST have verified/know (page_ref_count(page) == 1) */
  190. pool->alloc.cache[pool->alloc.count++] = page;
  191. return true;
  192. }
  193. void __page_pool_put_page(struct page_pool *pool,
  194. struct page *page, bool allow_direct)
  195. {
  196. /* This allocator is optimized for the XDP mode that uses
  197. * one-frame-per-page, but have fallbacks that act like the
  198. * regular page allocator APIs.
  199. *
  200. * refcnt == 1 means page_pool owns page, and can recycle it.
  201. */
  202. if (likely(page_ref_count(page) == 1)) {
  203. /* Read barrier done in page_ref_count / READ_ONCE */
  204. if (allow_direct && in_serving_softirq())
  205. if (__page_pool_recycle_direct(page, pool))
  206. return;
  207. if (!__page_pool_recycle_into_ring(pool, page)) {
  208. /* Cache full, fallback to free pages */
  209. __page_pool_return_page(pool, page);
  210. }
  211. return;
  212. }
  213. /* Fallback/non-XDP mode: API user have elevated refcnt.
  214. *
  215. * Many drivers split up the page into fragments, and some
  216. * want to keep doing this to save memory and do refcnt based
  217. * recycling. Support this use case too, to ease drivers
  218. * switching between XDP/non-XDP.
  219. *
  220. * In-case page_pool maintains the DMA mapping, API user must
  221. * call page_pool_put_page once. In this elevated refcnt
  222. * case, the DMA is unmapped/released, as driver is likely
  223. * doing refcnt based recycle tricks, meaning another process
  224. * will be invoking put_page.
  225. */
  226. __page_pool_clean_page(pool, page);
  227. put_page(page);
  228. }
  229. EXPORT_SYMBOL(__page_pool_put_page);
  230. static void __page_pool_empty_ring(struct page_pool *pool)
  231. {
  232. struct page *page;
  233. /* Empty recycle ring */
  234. while ((page = ptr_ring_consume_bh(&pool->ring))) {
  235. /* Verify the refcnt invariant of cached pages */
  236. if (!(page_ref_count(page) == 1))
  237. pr_crit("%s() page_pool refcnt %d violation\n",
  238. __func__, page_ref_count(page));
  239. __page_pool_return_page(pool, page);
  240. }
  241. }
  242. static void __page_pool_destroy_rcu(struct rcu_head *rcu)
  243. {
  244. struct page_pool *pool;
  245. pool = container_of(rcu, struct page_pool, rcu);
  246. WARN(pool->alloc.count, "API usage violation");
  247. __page_pool_empty_ring(pool);
  248. ptr_ring_cleanup(&pool->ring, NULL);
  249. kfree(pool);
  250. }
  251. /* Cleanup and release resources */
  252. void page_pool_destroy(struct page_pool *pool)
  253. {
  254. struct page *page;
  255. /* Empty alloc cache, assume caller made sure this is
  256. * no-longer in use, and page_pool_alloc_pages() cannot be
  257. * call concurrently.
  258. */
  259. while (pool->alloc.count) {
  260. page = pool->alloc.cache[--pool->alloc.count];
  261. __page_pool_return_page(pool, page);
  262. }
  263. /* No more consumers should exist, but producers could still
  264. * be in-flight.
  265. */
  266. __page_pool_empty_ring(pool);
  267. /* An xdp_mem_allocator can still ref page_pool pointer */
  268. call_rcu(&pool->rcu, __page_pool_destroy_rcu);
  269. }
  270. EXPORT_SYMBOL(page_pool_destroy);