1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef _NET_PAGE_POOL_TYPES_H
4 #define _NET_PAGE_POOL_TYPES_H
6 #include <linux/dma-direction.h>
7 #include <linux/ptr_ring.h>
8 #include <linux/types.h>
10 #define PP_FLAG_DMA_MAP BIT(0) /* Should page_pool do the DMA
13 #define PP_FLAG_DMA_SYNC_DEV BIT(1) /* If set all pages that the driver gets
14 * from page_pool will be
15 * DMA-synced-for-device according to
16 * the length provided by the device
18 * Please note DMA-sync-for-CPU is still
19 * device driver responsibility
21 #define PP_FLAG_SYSTEM_POOL BIT(2) /* Global system page_pool */
22 #define PP_FLAG_ALL (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | \
26 * Fast allocation side cache array/stack
28 * The cache size and refill watermark is related to the network
29 * use-case. The NAPI budget is 64 packets. After a NAPI poll the RX
30 * ring is usually refilled and the max consumed elements will be 64,
31 * thus a natural max size of objects needed in the cache.
33 * Keeping room for more objects, is due to XDP_DROP use-case. As
34 * XDP_DROP allows the opportunity to recycle objects directly into
35 * this array, as it shares the same softirq/NAPI protection. If
36 * cache is already full (or partly full) then the XDP_DROP recycles
37 * would have to take a slower code path.
39 #define PP_ALLOC_CACHE_SIZE 128
40 #define PP_ALLOC_CACHE_REFILL 64
41 struct pp_alloc_cache {
43 struct page *cache[PP_ALLOC_CACHE_SIZE];
47 * struct page_pool_params - page pool parameters
48 * @fast: params accessed frequently on hotpath
49 * @order: 2^order pages on allocation
50 * @pool_size: size of the ptr_ring
51 * @nid: NUMA node id to allocate from pages from
52 * @dev: device, for DMA pre-mapping purposes
53 * @napi: NAPI which is the sole consumer of pages, otherwise NULL
54 * @dma_dir: DMA mapping direction
55 * @max_len: max DMA sync memory size for PP_FLAG_DMA_SYNC_DEV
56 * @offset: DMA sync address offset for PP_FLAG_DMA_SYNC_DEV
57 * @slow: params with slowpath access only (initialization and Netlink)
58 * @netdev: netdev this pool will serve (leave as NULL if none or multiple)
59 * @flags: PP_FLAG_DMA_MAP, PP_FLAG_DMA_SYNC_DEV, PP_FLAG_SYSTEM_POOL
61 struct page_pool_params {
62 struct_group_tagged(page_pool_params_fast, fast,
64 unsigned int pool_size;
67 struct napi_struct *napi;
68 enum dma_data_direction dma_dir;
72 struct_group_tagged(page_pool_params_slow, slow,
73 struct net_device *netdev;
75 /* private: used by test code only */
76 void (*init_callback)(struct page *page, void *arg);
81 #ifdef CONFIG_PAGE_POOL_STATS
83 * struct page_pool_alloc_stats - allocation statistics
84 * @fast: successful fast path allocations
85 * @slow: slow path order-0 allocations
86 * @slow_high_order: slow path high order allocations
87 * @empty: ptr ring is empty, so a slow path allocation was forced
88 * @refill: an allocation which triggered a refill of the cache
89 * @waive: pages obtained from the ptr ring that cannot be added to
90 * the cache due to a NUMA mismatch
92 struct page_pool_alloc_stats {
102 * struct page_pool_recycle_stats - recycling (freeing) statistics
103 * @cached: recycling placed page in the page pool cache
104 * @cache_full: page pool cache was full
105 * @ring: page placed into the ptr ring
106 * @ring_full: page released from page pool because the ptr ring was full
107 * @released_refcnt: page released (and not recycled) because refcnt > 1
109 struct page_pool_recycle_stats {
118 * struct page_pool_stats - combined page pool use statistics
119 * @alloc_stats: see struct page_pool_alloc_stats
120 * @recycle_stats: see struct page_pool_recycle_stats
122 * Wrapper struct for combining page pool stats with different storage
125 struct page_pool_stats {
126 struct page_pool_alloc_stats alloc_stats;
127 struct page_pool_recycle_stats recycle_stats;
132 struct page_pool_params_fast p;
135 u32 pages_state_hold_cnt;
137 bool has_init_callback:1; /* slow::init_callback is set */
138 bool dma_map:1; /* Perform DMA mapping */
139 bool dma_sync:1; /* Perform DMA sync */
140 #ifdef CONFIG_PAGE_POOL_STATS
141 bool system:1; /* This is a global percpu pool */
144 /* The following block must stay within one cacheline. On 32-bit
145 * systems, sizeof(long) == sizeof(int), so that the block size is
146 * ``3 * sizeof(long)``. On 64-bit systems, the actual size is
147 * ``2 * sizeof(long) + sizeof(int)``. The closest pow-2 to both of
148 * them is ``4 * sizeof(long)``, so just use that one for simplicity.
149 * Having it aligned to a cacheline boundary may be excessive and
150 * doesn't bring any good.
152 __cacheline_group_begin(frag) __aligned(4 * sizeof(long));
154 struct page *frag_page;
155 unsigned int frag_offset;
156 __cacheline_group_end(frag);
158 struct delayed_work release_dw;
159 void (*disconnect)(void *pool);
160 unsigned long defer_start;
161 unsigned long defer_warn;
163 #ifdef CONFIG_PAGE_POOL_STATS
164 /* these stats are incremented while in softirq context */
165 struct page_pool_alloc_stats alloc_stats;
170 * Data structure for allocation side
172 * Drivers allocation side usually already perform some kind
173 * of resource protection. Piggyback on this protection, and
174 * require driver to protect allocation side.
176 * For NIC drivers this means, allocate a page_pool per
177 * RX-queue. As the RX-queue is already protected by
178 * Softirq/BH scheduling and napi_schedule. NAPI schedule
179 * guarantee that a single napi_struct will only be scheduled
180 * on a single CPU (see napi_schedule).
182 struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
184 /* Data structure for storing recycled pages.
186 * Returning/freeing pages is more complicated synchronization
187 * wise, because free's can happen on remote CPUs, with no
188 * association with allocation resource.
190 * Use ptr_ring, as it separates consumer and producer
191 * efficiently, it a way that doesn't bounce cache-lines.
193 * TODO: Implement bulk return pages into this structure.
195 struct ptr_ring ring;
197 #ifdef CONFIG_PAGE_POOL_STATS
198 /* recycle stats are per-cpu to avoid locking */
199 struct page_pool_recycle_stats __percpu *recycle_stats;
201 atomic_t pages_state_release_cnt;
203 /* A page_pool is strictly tied to a single RX-queue being
204 * protected by NAPI, due to above pp_alloc_cache. This
205 * refcnt serves purpose is to simplify drivers error handling.
211 /* Slow/Control-path information follows */
212 struct page_pool_params_slow slow;
213 /* User-facing fields, protected by page_pools_lock */
215 struct hlist_node list;
222 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
223 struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset,
224 unsigned int size, gfp_t gfp);
225 struct page_pool *page_pool_create(const struct page_pool_params *params);
226 struct page_pool *page_pool_create_percpu(const struct page_pool_params *params,
231 #ifdef CONFIG_PAGE_POOL
232 void page_pool_destroy(struct page_pool *pool);
233 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
234 const struct xdp_mem_info *mem);
235 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
238 static inline void page_pool_destroy(struct page_pool *pool)
242 static inline void page_pool_use_xdp_mem(struct page_pool *pool,
243 void (*disconnect)(void *),
244 const struct xdp_mem_info *mem)
248 static inline void page_pool_put_page_bulk(struct page_pool *pool, void **data,
254 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page,
255 unsigned int dma_sync_size,
258 static inline bool is_page_pool_compiled_in(void)
260 #ifdef CONFIG_PAGE_POOL
267 /* Caller must provide appropriate safe context, e.g. NAPI. */
268 void page_pool_update_nid(struct page_pool *pool, int new_nid);
270 #endif /* _NET_PAGE_POOL_H */