page_pool.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * page_pool.h
  4. * Author: Jesper Dangaard Brouer <[email protected]>
  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. * API keeps track of in-flight pages, in-order to let API user know
  19. * when it is safe to dealloactor page_pool object. Thus, API users
  20. * must make sure to call page_pool_release_page() when a page is
  21. * "leaving" the page_pool. Or call page_pool_put_page() where
  22. * appropiate. For maintaining correct accounting.
  23. *
  24. * API user must only call page_pool_put_page() once on a page, as it
  25. * will either recycle the page, or in case of elevated refcnt, it
  26. * will release the DMA mapping and in-flight state accounting. We
  27. * hope to lift this requirement in the future.
  28. */
  29. #ifndef _NET_PAGE_POOL_H
  30. #define _NET_PAGE_POOL_H
  31. #include <linux/mm.h> /* Needed by ptr_ring */
  32. #include <linux/ptr_ring.h>
  33. #include <linux/dma-direction.h>
  34. #include <linux/android_kabi.h>
  35. #define PP_FLAG_DMA_MAP BIT(0) /* Should page_pool do the DMA
  36. * map/unmap
  37. */
  38. #define PP_FLAG_DMA_SYNC_DEV BIT(1) /* If set all pages that the driver gets
  39. * from page_pool will be
  40. * DMA-synced-for-device according to
  41. * the length provided by the device
  42. * driver.
  43. * Please note DMA-sync-for-CPU is still
  44. * device driver responsibility
  45. */
  46. #define PP_FLAG_PAGE_FRAG BIT(2) /* for page frag feature */
  47. #define PP_FLAG_ALL (PP_FLAG_DMA_MAP |\
  48. PP_FLAG_DMA_SYNC_DEV |\
  49. PP_FLAG_PAGE_FRAG)
  50. /*
  51. * Fast allocation side cache array/stack
  52. *
  53. * The cache size and refill watermark is related to the network
  54. * use-case. The NAPI budget is 64 packets. After a NAPI poll the RX
  55. * ring is usually refilled and the max consumed elements will be 64,
  56. * thus a natural max size of objects needed in the cache.
  57. *
  58. * Keeping room for more objects, is due to XDP_DROP use-case. As
  59. * XDP_DROP allows the opportunity to recycle objects directly into
  60. * this array, as it shares the same softirq/NAPI protection. If
  61. * cache is already full (or partly full) then the XDP_DROP recycles
  62. * would have to take a slower code path.
  63. */
  64. #define PP_ALLOC_CACHE_SIZE 128
  65. #define PP_ALLOC_CACHE_REFILL 64
  66. struct pp_alloc_cache {
  67. u32 count;
  68. struct page *cache[PP_ALLOC_CACHE_SIZE];
  69. };
  70. struct page_pool_params {
  71. unsigned int flags;
  72. unsigned int order;
  73. unsigned int pool_size;
  74. int nid; /* Numa node id to allocate from pages from */
  75. struct device *dev; /* device, for DMA pre-mapping purposes */
  76. enum dma_data_direction dma_dir; /* DMA mapping direction */
  77. unsigned int max_len; /* max DMA sync memory size */
  78. unsigned int offset; /* DMA addr offset */
  79. void (*init_callback)(struct page *page, void *arg);
  80. void *init_arg;
  81. };
  82. #ifdef CONFIG_PAGE_POOL_STATS
  83. struct page_pool_alloc_stats {
  84. u64 fast; /* fast path allocations */
  85. u64 slow; /* slow-path order 0 allocations */
  86. u64 slow_high_order; /* slow-path high order allocations */
  87. u64 empty; /* failed refills due to empty ptr ring, forcing
  88. * slow path allocation
  89. */
  90. u64 refill; /* allocations via successful refill */
  91. u64 waive; /* failed refills due to numa zone mismatch */
  92. };
  93. struct page_pool_recycle_stats {
  94. u64 cached; /* recycling placed page in the cache. */
  95. u64 cache_full; /* cache was full */
  96. u64 ring; /* recycling placed page back into ptr ring */
  97. u64 ring_full; /* page was released from page-pool because
  98. * PTR ring was full.
  99. */
  100. u64 released_refcnt; /* page released because of elevated
  101. * refcnt
  102. */
  103. };
  104. /* This struct wraps the above stats structs so users of the
  105. * page_pool_get_stats API can pass a single argument when requesting the
  106. * stats for the page pool.
  107. */
  108. struct page_pool_stats {
  109. struct page_pool_alloc_stats alloc_stats;
  110. struct page_pool_recycle_stats recycle_stats;
  111. };
  112. int page_pool_ethtool_stats_get_count(void);
  113. u8 *page_pool_ethtool_stats_get_strings(u8 *data);
  114. u64 *page_pool_ethtool_stats_get(u64 *data, void *stats);
  115. /*
  116. * Drivers that wish to harvest page pool stats and report them to users
  117. * (perhaps via ethtool, debugfs, or another mechanism) can allocate a
  118. * struct page_pool_stats call page_pool_get_stats to get stats for the specified pool.
  119. */
  120. bool page_pool_get_stats(struct page_pool *pool,
  121. struct page_pool_stats *stats);
  122. #else
  123. static inline int page_pool_ethtool_stats_get_count(void)
  124. {
  125. return 0;
  126. }
  127. static inline u8 *page_pool_ethtool_stats_get_strings(u8 *data)
  128. {
  129. return data;
  130. }
  131. static inline u64 *page_pool_ethtool_stats_get(u64 *data, void *stats)
  132. {
  133. return data;
  134. }
  135. #endif
  136. struct page_pool {
  137. struct page_pool_params p;
  138. struct delayed_work release_dw;
  139. void (*disconnect)(void *);
  140. unsigned long defer_start;
  141. unsigned long defer_warn;
  142. u32 pages_state_hold_cnt;
  143. unsigned int frag_offset;
  144. struct page *frag_page;
  145. long frag_users;
  146. #ifdef CONFIG_PAGE_POOL_STATS
  147. /* these stats are incremented while in softirq context */
  148. struct page_pool_alloc_stats alloc_stats;
  149. #endif
  150. u32 xdp_mem_id;
  151. /*
  152. * Data structure for allocation side
  153. *
  154. * Drivers allocation side usually already perform some kind
  155. * of resource protection. Piggyback on this protection, and
  156. * require driver to protect allocation side.
  157. *
  158. * For NIC drivers this means, allocate a page_pool per
  159. * RX-queue. As the RX-queue is already protected by
  160. * Softirq/BH scheduling and napi_schedule. NAPI schedule
  161. * guarantee that a single napi_struct will only be scheduled
  162. * on a single CPU (see napi_schedule).
  163. */
  164. struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
  165. /* Data structure for storing recycled pages.
  166. *
  167. * Returning/freeing pages is more complicated synchronization
  168. * wise, because free's can happen on remote CPUs, with no
  169. * association with allocation resource.
  170. *
  171. * Use ptr_ring, as it separates consumer and producer
  172. * effeciently, it a way that doesn't bounce cache-lines.
  173. *
  174. * TODO: Implement bulk return pages into this structure.
  175. */
  176. struct ptr_ring ring;
  177. #ifdef CONFIG_PAGE_POOL_STATS
  178. /* recycle stats are per-cpu to avoid locking */
  179. struct page_pool_recycle_stats __percpu *recycle_stats;
  180. #endif
  181. atomic_t pages_state_release_cnt;
  182. /* A page_pool is strictly tied to a single RX-queue being
  183. * protected by NAPI, due to above pp_alloc_cache. This
  184. * refcnt serves purpose is to simplify drivers error handling.
  185. */
  186. refcount_t user_cnt;
  187. u64 destroy_cnt;
  188. ANDROID_KABI_RESERVE(1);
  189. };
  190. struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
  191. static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
  192. {
  193. gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
  194. return page_pool_alloc_pages(pool, gfp);
  195. }
  196. struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset,
  197. unsigned int size, gfp_t gfp);
  198. static inline struct page *page_pool_dev_alloc_frag(struct page_pool *pool,
  199. unsigned int *offset,
  200. unsigned int size)
  201. {
  202. gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
  203. return page_pool_alloc_frag(pool, offset, size, gfp);
  204. }
  205. /* get the stored dma direction. A driver might decide to treat this locally and
  206. * avoid the extra cache line from page_pool to determine the direction
  207. */
  208. static
  209. inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool)
  210. {
  211. return pool->p.dma_dir;
  212. }
  213. bool page_pool_return_skb_page(struct page *page);
  214. struct page_pool *page_pool_create(const struct page_pool_params *params);
  215. struct xdp_mem_info;
  216. #ifdef CONFIG_PAGE_POOL
  217. void page_pool_destroy(struct page_pool *pool);
  218. void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
  219. struct xdp_mem_info *mem);
  220. void page_pool_release_page(struct page_pool *pool, struct page *page);
  221. void page_pool_put_page_bulk(struct page_pool *pool, void **data,
  222. int count);
  223. #else
  224. static inline void page_pool_destroy(struct page_pool *pool)
  225. {
  226. }
  227. static inline void page_pool_use_xdp_mem(struct page_pool *pool,
  228. void (*disconnect)(void *),
  229. struct xdp_mem_info *mem)
  230. {
  231. }
  232. static inline void page_pool_release_page(struct page_pool *pool,
  233. struct page *page)
  234. {
  235. }
  236. static inline void page_pool_put_page_bulk(struct page_pool *pool, void **data,
  237. int count)
  238. {
  239. }
  240. #endif
  241. void page_pool_put_defragged_page(struct page_pool *pool, struct page *page,
  242. unsigned int dma_sync_size,
  243. bool allow_direct);
  244. static inline void page_pool_fragment_page(struct page *page, long nr)
  245. {
  246. atomic_long_set(&page->pp_frag_count, nr);
  247. }
  248. static inline long page_pool_defrag_page(struct page *page, long nr)
  249. {
  250. long ret;
  251. /* If nr == pp_frag_count then we have cleared all remaining
  252. * references to the page. No need to actually overwrite it, instead
  253. * we can leave this to be overwritten by the calling function.
  254. *
  255. * The main advantage to doing this is that an atomic_read is
  256. * generally a much cheaper operation than an atomic update,
  257. * especially when dealing with a page that may be partitioned
  258. * into only 2 or 3 pieces.
  259. */
  260. if (atomic_long_read(&page->pp_frag_count) == nr)
  261. return 0;
  262. ret = atomic_long_sub_return(nr, &page->pp_frag_count);
  263. WARN_ON(ret < 0);
  264. return ret;
  265. }
  266. static inline bool page_pool_is_last_frag(struct page_pool *pool,
  267. struct page *page)
  268. {
  269. /* If fragments aren't enabled or count is 0 we were the last user */
  270. return !(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
  271. (page_pool_defrag_page(page, 1) == 0);
  272. }
  273. static inline void page_pool_put_page(struct page_pool *pool,
  274. struct page *page,
  275. unsigned int dma_sync_size,
  276. bool allow_direct)
  277. {
  278. /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
  279. * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
  280. */
  281. #ifdef CONFIG_PAGE_POOL
  282. if (!page_pool_is_last_frag(pool, page))
  283. return;
  284. page_pool_put_defragged_page(pool, page, dma_sync_size, allow_direct);
  285. #endif
  286. }
  287. /* Same as above but will try to sync the entire area pool->max_len */
  288. static inline void page_pool_put_full_page(struct page_pool *pool,
  289. struct page *page, bool allow_direct)
  290. {
  291. page_pool_put_page(pool, page, -1, allow_direct);
  292. }
  293. /* Same as above but the caller must guarantee safe context. e.g NAPI */
  294. static inline void page_pool_recycle_direct(struct page_pool *pool,
  295. struct page *page)
  296. {
  297. page_pool_put_full_page(pool, page, true);
  298. }
  299. #define PAGE_POOL_DMA_USE_PP_FRAG_COUNT \
  300. (sizeof(dma_addr_t) > sizeof(unsigned long))
  301. static inline dma_addr_t page_pool_get_dma_addr(struct page *page)
  302. {
  303. dma_addr_t ret = page->dma_addr;
  304. if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT)
  305. ret |= (dma_addr_t)page->dma_addr_upper << 16 << 16;
  306. return ret;
  307. }
  308. static inline void page_pool_set_dma_addr(struct page *page, dma_addr_t addr)
  309. {
  310. page->dma_addr = addr;
  311. if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT)
  312. page->dma_addr_upper = upper_32_bits(addr);
  313. }
  314. static inline bool is_page_pool_compiled_in(void)
  315. {
  316. #ifdef CONFIG_PAGE_POOL
  317. return true;
  318. #else
  319. return false;
  320. #endif
  321. }
  322. static inline bool page_pool_put(struct page_pool *pool)
  323. {
  324. return refcount_dec_and_test(&pool->user_cnt);
  325. }
  326. /* Caller must provide appropriate safe context, e.g. NAPI. */
  327. void page_pool_update_nid(struct page_pool *pool, int new_nid);
  328. static inline void page_pool_nid_changed(struct page_pool *pool, int new_nid)
  329. {
  330. if (unlikely(pool->p.nid != new_nid))
  331. page_pool_update_nid(pool, new_nid);
  332. }
  333. #endif /* _NET_PAGE_POOL_H */