1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef _NET_PAGE_POOL_TYPES_H
4 #define _NET_PAGE_POOL_TYPES_H
5
6 #include <linux/dma-direction.h>
7 #include <linux/ptr_ring.h>
8 #include <linux/types.h>
9 #include <net/netmem.h>
10 #include <linux/android_kabi.h>
11
12 #define PP_FLAG_DMA_MAP BIT(0) /* Should page_pool do the DMA
13 * map/unmap
14 */
15 #define PP_FLAG_DMA_SYNC_DEV BIT(1) /* If set all pages that the driver gets
16 * from page_pool will be
17 * DMA-synced-for-device according to
18 * the length provided by the device
19 * driver.
20 * Please note DMA-sync-for-CPU is still
21 * device driver responsibility
22 */
23 #define PP_FLAG_SYSTEM_POOL BIT(2) /* Global system page_pool */
24
25 /* Allow unreadable (net_iov backed) netmem in this page_pool. Drivers setting
26 * this must be able to support unreadable netmem, where netmem_address() would
27 * return NULL. This flag should not be set for header page_pools.
28 *
29 * If the driver sets PP_FLAG_ALLOW_UNREADABLE_NETMEM, it should also set
30 * page_pool_params.slow.queue_idx.
31 */
32 #define PP_FLAG_ALLOW_UNREADABLE_NETMEM BIT(3)
33
34 #define PP_FLAG_ALL (PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | \
35 PP_FLAG_SYSTEM_POOL | PP_FLAG_ALLOW_UNREADABLE_NETMEM)
36
37 /*
38 * Fast allocation side cache array/stack
39 *
40 * The cache size and refill watermark is related to the network
41 * use-case. The NAPI budget is 64 packets. After a NAPI poll the RX
42 * ring is usually refilled and the max consumed elements will be 64,
43 * thus a natural max size of objects needed in the cache.
44 *
45 * Keeping room for more objects, is due to XDP_DROP use-case. As
46 * XDP_DROP allows the opportunity to recycle objects directly into
47 * this array, as it shares the same softirq/NAPI protection. If
48 * cache is already full (or partly full) then the XDP_DROP recycles
49 * would have to take a slower code path.
50 */
51 #define PP_ALLOC_CACHE_SIZE 128
52 #define PP_ALLOC_CACHE_REFILL 64
53 struct pp_alloc_cache {
54 u32 count;
55 netmem_ref cache[PP_ALLOC_CACHE_SIZE];
56 };
57
58 /**
59 * struct page_pool_params - page pool parameters
60 * @fast: params accessed frequently on hotpath
61 * @order: 2^order pages on allocation
62 * @pool_size: size of the ptr_ring
63 * @nid: NUMA node id to allocate from pages from
64 * @dev: device, for DMA pre-mapping purposes
65 * @napi: NAPI which is the sole consumer of pages, otherwise NULL
66 * @dma_dir: DMA mapping direction
67 * @max_len: max DMA sync memory size for PP_FLAG_DMA_SYNC_DEV
68 * @offset: DMA sync address offset for PP_FLAG_DMA_SYNC_DEV
69 * @slow: params with slowpath access only (initialization and Netlink)
70 * @netdev: netdev this pool will serve (leave as NULL if none or multiple)
71 * @queue_idx: queue idx this page_pool is being created for.
72 * @flags: PP_FLAG_DMA_MAP, PP_FLAG_DMA_SYNC_DEV, PP_FLAG_SYSTEM_POOL,
73 * PP_FLAG_ALLOW_UNREADABLE_NETMEM.
74 */
75 struct page_pool_params {
76 struct_group_tagged(page_pool_params_fast, fast,
77 unsigned int order;
78 unsigned int pool_size;
79 int nid;
80 struct device *dev;
81 struct napi_struct *napi;
82 enum dma_data_direction dma_dir;
83 unsigned int max_len;
84 unsigned int offset;
85 );
86 struct_group_tagged(page_pool_params_slow, slow,
87 struct net_device *netdev;
88 unsigned int queue_idx;
89 unsigned int flags;
90 /* private: used by test code only */
91 void (*init_callback)(netmem_ref netmem, void *arg);
92 void *init_arg;
93 );
94 };
95
96 #ifdef CONFIG_PAGE_POOL_STATS
97 /**
98 * struct page_pool_alloc_stats - allocation statistics
99 * @fast: successful fast path allocations
100 * @slow: slow path order-0 allocations
101 * @slow_high_order: slow path high order allocations
102 * @empty: ptr ring is empty, so a slow path allocation was forced
103 * @refill: an allocation which triggered a refill of the cache
104 * @waive: pages obtained from the ptr ring that cannot be added to
105 * the cache due to a NUMA mismatch
106 */
107 struct page_pool_alloc_stats {
108 u64 fast;
109 u64 slow;
110 u64 slow_high_order;
111 u64 empty;
112 u64 refill;
113 u64 waive;
114 };
115
116 /**
117 * struct page_pool_recycle_stats - recycling (freeing) statistics
118 * @cached: recycling placed page in the page pool cache
119 * @cache_full: page pool cache was full
120 * @ring: page placed into the ptr ring
121 * @ring_full: page released from page pool because the ptr ring was full
122 * @released_refcnt: page released (and not recycled) because refcnt > 1
123 */
124 struct page_pool_recycle_stats {
125 u64 cached;
126 u64 cache_full;
127 u64 ring;
128 u64 ring_full;
129 u64 released_refcnt;
130 };
131
132 /**
133 * struct page_pool_stats - combined page pool use statistics
134 * @alloc_stats: see struct page_pool_alloc_stats
135 * @recycle_stats: see struct page_pool_recycle_stats
136 *
137 * Wrapper struct for combining page pool stats with different storage
138 * requirements.
139 */
140 struct page_pool_stats {
141 struct page_pool_alloc_stats alloc_stats;
142 struct page_pool_recycle_stats recycle_stats;
143 };
144 #endif
145
146 /* The whole frag API block must stay within one cacheline. On 32-bit systems,
147 * sizeof(long) == sizeof(int), so that the block size is ``3 * sizeof(long)``.
148 * On 64-bit systems, the actual size is ``2 * sizeof(long) + sizeof(int)``.
149 * The closest pow-2 to both of them is ``4 * sizeof(long)``, so just use that
150 * one for simplicity.
151 * Having it aligned to a cacheline boundary may be excessive and doesn't bring
152 * any good.
153 */
154 #define PAGE_POOL_FRAG_GROUP_ALIGN (4 * sizeof(long))
155
156 struct pp_memory_provider_params {
157 void *mp_priv;
158 };
159
160 struct page_pool {
161 struct page_pool_params_fast p;
162
163 int cpuid;
164 u32 pages_state_hold_cnt;
165
166 bool has_init_callback:1; /* slow::init_callback is set */
167 bool dma_map:1; /* Perform DMA mapping */
168 bool dma_sync:1; /* Perform DMA sync */
169 #ifdef CONFIG_PAGE_POOL_STATS
170 bool system:1; /* This is a global percpu pool */
171 #endif
172
173 __cacheline_group_begin_aligned(frag, PAGE_POOL_FRAG_GROUP_ALIGN);
174 long frag_users;
175 netmem_ref frag_page;
176 unsigned int frag_offset;
177 __cacheline_group_end_aligned(frag, PAGE_POOL_FRAG_GROUP_ALIGN);
178
179 struct delayed_work release_dw;
180 void (*disconnect)(void *pool);
181 unsigned long defer_start;
182 unsigned long defer_warn;
183
184 #ifdef CONFIG_PAGE_POOL_STATS
185 /* these stats are incremented while in softirq context */
186 struct page_pool_alloc_stats alloc_stats;
187 #endif
188 u32 xdp_mem_id;
189
190 /*
191 * Data structure for allocation side
192 *
193 * Drivers allocation side usually already perform some kind
194 * of resource protection. Piggyback on this protection, and
195 * require driver to protect allocation side.
196 *
197 * For NIC drivers this means, allocate a page_pool per
198 * RX-queue. As the RX-queue is already protected by
199 * Softirq/BH scheduling and napi_schedule. NAPI schedule
200 * guarantee that a single napi_struct will only be scheduled
201 * on a single CPU (see napi_schedule).
202 */
203 struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
204
205 /* Data structure for storing recycled pages.
206 *
207 * Returning/freeing pages is more complicated synchronization
208 * wise, because free's can happen on remote CPUs, with no
209 * association with allocation resource.
210 *
211 * Use ptr_ring, as it separates consumer and producer
212 * efficiently, it a way that doesn't bounce cache-lines.
213 *
214 * TODO: Implement bulk return pages into this structure.
215 */
216 struct ptr_ring ring;
217
218 void *mp_priv;
219
220 #ifdef CONFIG_PAGE_POOL_STATS
221 /* recycle stats are per-cpu to avoid locking */
222 struct page_pool_recycle_stats __percpu *recycle_stats;
223 #endif
224 atomic_t pages_state_release_cnt;
225
226 /* A page_pool is strictly tied to a single RX-queue being
227 * protected by NAPI, due to above pp_alloc_cache. This
228 * refcnt serves purpose is to simplify drivers error handling.
229 */
230 refcount_t user_cnt;
231
232 u64 destroy_cnt;
233
234 /* Slow/Control-path information follows */
235 struct page_pool_params_slow slow;
236 /* User-facing fields, protected by page_pools_lock */
237 struct {
238 struct hlist_node list;
239 u64 detach_time;
240 u32 napi_id;
241 u32 id;
242 } user;
243 ANDROID_KABI_RESERVE(1);
244 };
245
246 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
247 netmem_ref page_pool_alloc_netmem(struct page_pool *pool, gfp_t gfp);
248 struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset,
249 unsigned int size, gfp_t gfp);
250 netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,
251 unsigned int *offset, unsigned int size,
252 gfp_t gfp);
253 struct page_pool *page_pool_create(const struct page_pool_params *params);
254 struct page_pool *page_pool_create_percpu(const struct page_pool_params *params,
255 int cpuid);
256
257 struct xdp_mem_info;
258
259 #ifdef CONFIG_PAGE_POOL
260 void page_pool_disable_direct_recycling(struct page_pool *pool);
261 void page_pool_destroy(struct page_pool *pool);
262 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
263 const struct xdp_mem_info *mem);
264 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
265 int count);
266 #else
page_pool_destroy(struct page_pool * pool)267 static inline void page_pool_destroy(struct page_pool *pool)
268 {
269 }
270
page_pool_use_xdp_mem(struct page_pool * pool,void (* disconnect)(void *),const struct xdp_mem_info * mem)271 static inline void page_pool_use_xdp_mem(struct page_pool *pool,
272 void (*disconnect)(void *),
273 const struct xdp_mem_info *mem)
274 {
275 }
276
page_pool_put_page_bulk(struct page_pool * pool,void ** data,int count)277 static inline void page_pool_put_page_bulk(struct page_pool *pool, void **data,
278 int count)
279 {
280 }
281 #endif
282
283 void page_pool_put_unrefed_netmem(struct page_pool *pool, netmem_ref netmem,
284 unsigned int dma_sync_size,
285 bool allow_direct);
286 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page,
287 unsigned int dma_sync_size,
288 bool allow_direct);
289
is_page_pool_compiled_in(void)290 static inline bool is_page_pool_compiled_in(void)
291 {
292 #ifdef CONFIG_PAGE_POOL
293 return true;
294 #else
295 return false;
296 #endif
297 }
298
299 /* Caller must provide appropriate safe context, e.g. NAPI. */
300 void page_pool_update_nid(struct page_pool *pool, int new_nid);
301
302 #endif /* _NET_PAGE_POOL_H */
303