page_pool.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * page_pool.h
  4. * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
  5. * Copyright (C) 2016 Red Hat, Inc.
  6. */
  7. /**
  8. * DOC: page_pool allocator
  9. *
  10. * This page_pool allocator is optimized for the XDP mode that
  11. * uses one-frame-per-page, but have fallbacks that act like the
  12. * regular page allocator APIs.
  13. *
  14. * Basic use involve replacing alloc_pages() calls with the
  15. * page_pool_alloc_pages() call. Drivers should likely use
  16. * page_pool_dev_alloc_pages() replacing dev_alloc_pages().
  17. *
  18. * If page_pool handles DMA mapping (use page->private), then API user
  19. * is responsible for invoking page_pool_put_page() once. In-case of
  20. * elevated refcnt, the DMA state is released, assuming other users of
  21. * the page will eventually call put_page().
  22. *
  23. * If no DMA mapping is done, then it can act as shim-layer that
  24. * fall-through to alloc_page. As no state is kept on the page, the
  25. * regular put_page() call is sufficient.
  26. */
  27. #ifndef _NET_PAGE_POOL_H
  28. #define _NET_PAGE_POOL_H
  29. #include <linux/mm.h> /* Needed by ptr_ring */
  30. #include <linux/ptr_ring.h>
  31. #include <linux/dma-direction.h>
  32. #define PP_FLAG_DMA_MAP 1 /* Should page_pool do the DMA map/unmap */
  33. #define PP_FLAG_ALL PP_FLAG_DMA_MAP
  34. /*
  35. * Fast allocation side cache array/stack
  36. *
  37. * The cache size and refill watermark is related to the network
  38. * use-case. The NAPI budget is 64 packets. After a NAPI poll the RX
  39. * ring is usually refilled and the max consumed elements will be 64,
  40. * thus a natural max size of objects needed in the cache.
  41. *
  42. * Keeping room for more objects, is due to XDP_DROP use-case. As
  43. * XDP_DROP allows the opportunity to recycle objects directly into
  44. * this array, as it shares the same softirq/NAPI protection. If
  45. * cache is already full (or partly full) then the XDP_DROP recycles
  46. * would have to take a slower code path.
  47. */
  48. #define PP_ALLOC_CACHE_SIZE 128
  49. #define PP_ALLOC_CACHE_REFILL 64
  50. struct pp_alloc_cache {
  51. u32 count;
  52. void *cache[PP_ALLOC_CACHE_SIZE];
  53. };
  54. struct page_pool_params {
  55. unsigned int flags;
  56. unsigned int order;
  57. unsigned int pool_size;
  58. int nid; /* Numa node id to allocate from pages from */
  59. struct device *dev; /* device, for DMA pre-mapping purposes */
  60. enum dma_data_direction dma_dir; /* DMA mapping direction */
  61. };
  62. struct page_pool {
  63. struct rcu_head rcu;
  64. struct page_pool_params p;
  65. /*
  66. * Data structure for allocation side
  67. *
  68. * Drivers allocation side usually already perform some kind
  69. * of resource protection. Piggyback on this protection, and
  70. * require driver to protect allocation side.
  71. *
  72. * For NIC drivers this means, allocate a page_pool per
  73. * RX-queue. As the RX-queue is already protected by
  74. * Softirq/BH scheduling and napi_schedule. NAPI schedule
  75. * guarantee that a single napi_struct will only be scheduled
  76. * on a single CPU (see napi_schedule).
  77. */
  78. struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
  79. /* Data structure for storing recycled pages.
  80. *
  81. * Returning/freeing pages is more complicated synchronization
  82. * wise, because free's can happen on remote CPUs, with no
  83. * association with allocation resource.
  84. *
  85. * Use ptr_ring, as it separates consumer and producer
  86. * effeciently, it a way that doesn't bounce cache-lines.
  87. *
  88. * TODO: Implement bulk return pages into this structure.
  89. */
  90. struct ptr_ring ring;
  91. };
  92. struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
  93. static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
  94. {
  95. gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
  96. return page_pool_alloc_pages(pool, gfp);
  97. }
  98. struct page_pool *page_pool_create(const struct page_pool_params *params);
  99. void page_pool_destroy(struct page_pool *pool);
  100. /* Never call this directly, use helpers below */
  101. void __page_pool_put_page(struct page_pool *pool,
  102. struct page *page, bool allow_direct);
  103. static inline void page_pool_put_page(struct page_pool *pool,
  104. struct page *page, bool allow_direct)
  105. {
  106. /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
  107. * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
  108. */
  109. #ifdef CONFIG_PAGE_POOL
  110. __page_pool_put_page(pool, page, allow_direct);
  111. #endif
  112. }
  113. /* Very limited use-cases allow recycle direct */
  114. static inline void page_pool_recycle_direct(struct page_pool *pool,
  115. struct page *page)
  116. {
  117. __page_pool_put_page(pool, page, true);
  118. }
  119. static inline bool is_page_pool_compiled_in(void)
  120. {
  121. #ifdef CONFIG_PAGE_POOL
  122. return true;
  123. #else
  124. return false;
  125. #endif
  126. }
  127. #endif /* _NET_PAGE_POOL_H */