xref: /linux/net/core/page_pool.c (revision 334fbe734e687404f346eba7d5d96ed2b44d35ab)
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 
8 #include <linux/error-injection.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 
14 #include <net/netdev_lock.h>
15 #include <net/netdev_rx_queue.h>
16 #include <net/page_pool/helpers.h>
17 #include <net/page_pool/memory_provider.h>
18 #include <net/xdp.h>
19 
20 #include <linux/dma-direction.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/page-flags.h>
23 #include <linux/mm.h> /* for put_page() */
24 #include <linux/poison.h>
25 #include <linux/ethtool.h>
26 #include <linux/netdevice.h>
27 
28 #include <trace/events/page_pool.h>
29 
30 #include "dev.h"
31 #include "mp_dmabuf_devmem.h"
32 #include "netmem_priv.h"
33 #include "page_pool_priv.h"
34 
35 DEFINE_STATIC_KEY_FALSE(page_pool_mem_providers);
36 
37 #define DEFER_TIME (msecs_to_jiffies(1000))
38 #define DEFER_WARN_INTERVAL (60 * HZ)
39 
40 #define BIAS_MAX	(LONG_MAX >> 1)
41 
42 #ifdef CONFIG_PAGE_POOL_STATS
43 static DEFINE_PER_CPU(struct page_pool_recycle_stats, pp_system_recycle_stats);
44 
45 /* alloc_stat_inc is intended to be used in softirq context */
46 #define alloc_stat_inc(pool, __stat)	(pool->alloc_stats.__stat++)
47 /* recycle_stat_inc is safe to use when preemption is possible. */
48 #define recycle_stat_inc(pool, __stat)							\
49 	do {										\
50 		struct page_pool_recycle_stats __percpu *s = pool->recycle_stats;	\
51 		this_cpu_inc(s->__stat);						\
52 	} while (0)
53 
54 #define recycle_stat_add(pool, __stat, val)						\
55 	do {										\
56 		struct page_pool_recycle_stats __percpu *s = pool->recycle_stats;	\
57 		this_cpu_add(s->__stat, val);						\
58 	} while (0)
59 
60 static const char pp_stats[][ETH_GSTRING_LEN] = {
61 	"rx_pp_alloc_fast",
62 	"rx_pp_alloc_slow",
63 	"rx_pp_alloc_slow_ho",
64 	"rx_pp_alloc_empty",
65 	"rx_pp_alloc_refill",
66 	"rx_pp_alloc_waive",
67 	"rx_pp_recycle_cached",
68 	"rx_pp_recycle_cache_full",
69 	"rx_pp_recycle_ring",
70 	"rx_pp_recycle_ring_full",
71 	"rx_pp_recycle_released_ref",
72 };
73 
74 /**
75  * page_pool_get_stats() - fetch page pool stats
76  * @pool:	pool from which page was allocated
77  * @stats:	struct page_pool_stats to fill in
78  *
79  * Retrieve statistics about the page_pool. This API is only available
80  * if the kernel has been configured with ``CONFIG_PAGE_POOL_STATS=y``.
81  * A pointer to a caller allocated struct page_pool_stats structure
82  * is passed to this API which is filled in. The caller can then report
83  * those stats to the user (perhaps via ethtool, debugfs, etc.).
84  */
page_pool_get_stats(const struct page_pool * pool,struct page_pool_stats * stats)85 bool page_pool_get_stats(const struct page_pool *pool,
86 			 struct page_pool_stats *stats)
87 {
88 	int cpu = 0;
89 
90 	if (!stats)
91 		return false;
92 
93 	/* The caller is responsible to initialize stats. */
94 	stats->alloc_stats.fast += pool->alloc_stats.fast;
95 	stats->alloc_stats.slow += pool->alloc_stats.slow;
96 	stats->alloc_stats.slow_high_order += pool->alloc_stats.slow_high_order;
97 	stats->alloc_stats.empty += pool->alloc_stats.empty;
98 	stats->alloc_stats.refill += pool->alloc_stats.refill;
99 	stats->alloc_stats.waive += pool->alloc_stats.waive;
100 
101 	for_each_possible_cpu(cpu) {
102 		const struct page_pool_recycle_stats *pcpu =
103 			per_cpu_ptr(pool->recycle_stats, cpu);
104 
105 		stats->recycle_stats.cached += pcpu->cached;
106 		stats->recycle_stats.cache_full += pcpu->cache_full;
107 		stats->recycle_stats.ring += pcpu->ring;
108 		stats->recycle_stats.ring_full += pcpu->ring_full;
109 		stats->recycle_stats.released_refcnt += pcpu->released_refcnt;
110 	}
111 
112 	return true;
113 }
114 EXPORT_SYMBOL(page_pool_get_stats);
115 
page_pool_ethtool_stats_get_strings(u8 * data)116 u8 *page_pool_ethtool_stats_get_strings(u8 *data)
117 {
118 	int i;
119 
120 	for (i = 0; i < ARRAY_SIZE(pp_stats); i++) {
121 		memcpy(data, pp_stats[i], ETH_GSTRING_LEN);
122 		data += ETH_GSTRING_LEN;
123 	}
124 
125 	return data;
126 }
127 EXPORT_SYMBOL(page_pool_ethtool_stats_get_strings);
128 
page_pool_ethtool_stats_get_count(void)129 int page_pool_ethtool_stats_get_count(void)
130 {
131 	return ARRAY_SIZE(pp_stats);
132 }
133 EXPORT_SYMBOL(page_pool_ethtool_stats_get_count);
134 
page_pool_ethtool_stats_get(u64 * data,const void * stats)135 u64 *page_pool_ethtool_stats_get(u64 *data, const void *stats)
136 {
137 	const struct page_pool_stats *pool_stats = stats;
138 
139 	*data++ = pool_stats->alloc_stats.fast;
140 	*data++ = pool_stats->alloc_stats.slow;
141 	*data++ = pool_stats->alloc_stats.slow_high_order;
142 	*data++ = pool_stats->alloc_stats.empty;
143 	*data++ = pool_stats->alloc_stats.refill;
144 	*data++ = pool_stats->alloc_stats.waive;
145 	*data++ = pool_stats->recycle_stats.cached;
146 	*data++ = pool_stats->recycle_stats.cache_full;
147 	*data++ = pool_stats->recycle_stats.ring;
148 	*data++ = pool_stats->recycle_stats.ring_full;
149 	*data++ = pool_stats->recycle_stats.released_refcnt;
150 
151 	return data;
152 }
153 EXPORT_SYMBOL(page_pool_ethtool_stats_get);
154 
155 #else
156 #define alloc_stat_inc(...)	do { } while (0)
157 #define recycle_stat_inc(...)	do { } while (0)
158 #define recycle_stat_add(...)	do { } while (0)
159 #endif
160 
page_pool_producer_lock(struct page_pool * pool)161 static bool page_pool_producer_lock(struct page_pool *pool)
162 	__acquires(&pool->ring.producer_lock)
163 {
164 	bool in_softirq = in_softirq();
165 
166 	if (in_softirq)
167 		spin_lock(&pool->ring.producer_lock);
168 	else
169 		spin_lock_bh(&pool->ring.producer_lock);
170 
171 	return in_softirq;
172 }
173 
page_pool_producer_unlock(struct page_pool * pool,bool in_softirq)174 static void page_pool_producer_unlock(struct page_pool *pool,
175 				      bool in_softirq)
176 	__releases(&pool->ring.producer_lock)
177 {
178 	if (in_softirq)
179 		spin_unlock(&pool->ring.producer_lock);
180 	else
181 		spin_unlock_bh(&pool->ring.producer_lock);
182 }
183 
page_pool_struct_check(void)184 static void page_pool_struct_check(void)
185 {
186 	CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_users);
187 	CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_page);
188 	CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_offset);
189 	CACHELINE_ASSERT_GROUP_SIZE(struct page_pool, frag,
190 				    PAGE_POOL_FRAG_GROUP_ALIGN);
191 }
192 
page_pool_init(struct page_pool * pool,const struct page_pool_params * params,int cpuid)193 static int page_pool_init(struct page_pool *pool,
194 			  const struct page_pool_params *params,
195 			  int cpuid)
196 {
197 	unsigned int ring_qsize = 1024; /* Default */
198 	struct netdev_rx_queue *rxq;
199 	int err;
200 
201 	page_pool_struct_check();
202 
203 	memcpy(&pool->p, &params->fast, sizeof(pool->p));
204 	memcpy(&pool->slow, &params->slow, sizeof(pool->slow));
205 
206 	pool->cpuid = cpuid;
207 	pool->dma_sync_for_cpu = true;
208 
209 	/* Validate only known flags were used */
210 	if (pool->slow.flags & ~PP_FLAG_ALL)
211 		return -EINVAL;
212 
213 	if (pool->p.pool_size)
214 		ring_qsize = min(pool->p.pool_size, 16384);
215 
216 	/* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
217 	 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
218 	 * which is the XDP_TX use-case.
219 	 */
220 	if (pool->slow.flags & PP_FLAG_DMA_MAP) {
221 		if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
222 		    (pool->p.dma_dir != DMA_BIDIRECTIONAL))
223 			return -EINVAL;
224 
225 		pool->dma_map = true;
226 	}
227 
228 	if (pool->slow.flags & PP_FLAG_DMA_SYNC_DEV) {
229 		/* In order to request DMA-sync-for-device the page
230 		 * needs to be mapped
231 		 */
232 		if (!(pool->slow.flags & PP_FLAG_DMA_MAP))
233 			return -EINVAL;
234 
235 		if (!pool->p.max_len)
236 			return -EINVAL;
237 
238 		pool->dma_sync = true;
239 
240 		/* pool->p.offset has to be set according to the address
241 		 * offset used by the DMA engine to start copying rx data
242 		 */
243 	}
244 
245 	pool->has_init_callback = !!pool->slow.init_callback;
246 
247 #ifdef CONFIG_PAGE_POOL_STATS
248 	if (!(pool->slow.flags & PP_FLAG_SYSTEM_POOL)) {
249 		pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats);
250 		if (!pool->recycle_stats)
251 			return -ENOMEM;
252 	} else {
253 		/* For system page pool instance we use a singular stats object
254 		 * instead of allocating a separate percpu variable for each
255 		 * (also percpu) page pool instance.
256 		 */
257 		pool->recycle_stats = &pp_system_recycle_stats;
258 		pool->system = true;
259 	}
260 #endif
261 
262 	if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0) {
263 #ifdef CONFIG_PAGE_POOL_STATS
264 		if (!pool->system)
265 			free_percpu(pool->recycle_stats);
266 #endif
267 		return -ENOMEM;
268 	}
269 
270 	atomic_set(&pool->pages_state_release_cnt, 0);
271 
272 	/* Driver calling page_pool_create() also call page_pool_destroy() */
273 	refcount_set(&pool->user_cnt, 1);
274 
275 	xa_init_flags(&pool->dma_mapped, XA_FLAGS_ALLOC1);
276 
277 	if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) {
278 		netdev_assert_locked(pool->slow.netdev);
279 		rxq = __netif_get_rx_queue(pool->slow.netdev,
280 					   pool->slow.queue_idx);
281 		pool->mp_priv = rxq->mp_params.mp_priv;
282 		pool->mp_ops = rxq->mp_params.mp_ops;
283 	}
284 
285 	if (pool->mp_ops) {
286 		if (!pool->dma_map || !pool->dma_sync) {
287 			err = -EOPNOTSUPP;
288 			goto free_ptr_ring;
289 		}
290 
291 		if (WARN_ON(!is_kernel_rodata((unsigned long)pool->mp_ops))) {
292 			err = -EFAULT;
293 			goto free_ptr_ring;
294 		}
295 
296 		err = pool->mp_ops->init(pool);
297 		if (err) {
298 			pr_warn("%s() mem-provider init failed %d\n", __func__,
299 				err);
300 			goto free_ptr_ring;
301 		}
302 
303 		static_branch_inc(&page_pool_mem_providers);
304 	} else if (pool->p.order > MAX_PAGE_ORDER) {
305 		err = -EINVAL;
306 		goto free_ptr_ring;
307 	}
308 
309 	return 0;
310 
311 free_ptr_ring:
312 	ptr_ring_cleanup(&pool->ring, NULL);
313 	xa_destroy(&pool->dma_mapped);
314 #ifdef CONFIG_PAGE_POOL_STATS
315 	if (!pool->system)
316 		free_percpu(pool->recycle_stats);
317 #endif
318 	return err;
319 }
320 
page_pool_uninit(struct page_pool * pool)321 static void page_pool_uninit(struct page_pool *pool)
322 {
323 	ptr_ring_cleanup(&pool->ring, NULL);
324 	xa_destroy(&pool->dma_mapped);
325 
326 #ifdef CONFIG_PAGE_POOL_STATS
327 	if (!pool->system)
328 		free_percpu(pool->recycle_stats);
329 #endif
330 }
331 
332 /**
333  * page_pool_create_percpu() - create a page pool for a given cpu.
334  * @params: parameters, see struct page_pool_params
335  * @cpuid: cpu identifier
336  */
337 struct page_pool *
page_pool_create_percpu(const struct page_pool_params * params,int cpuid)338 page_pool_create_percpu(const struct page_pool_params *params, int cpuid)
339 {
340 	struct page_pool *pool;
341 	int err;
342 
343 	pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
344 	if (!pool)
345 		return ERR_PTR(-ENOMEM);
346 
347 	err = page_pool_init(pool, params, cpuid);
348 	if (err < 0)
349 		goto err_free;
350 
351 	err = page_pool_list(pool);
352 	if (err)
353 		goto err_uninit;
354 
355 	return pool;
356 
357 err_uninit:
358 	page_pool_uninit(pool);
359 err_free:
360 	pr_warn("%s() gave up with errno %d\n", __func__, err);
361 	kfree(pool);
362 	return ERR_PTR(err);
363 }
364 EXPORT_SYMBOL(page_pool_create_percpu);
365 
366 /**
367  * page_pool_create() - create a page pool
368  * @params: parameters, see struct page_pool_params
369  */
page_pool_create(const struct page_pool_params * params)370 struct page_pool *page_pool_create(const struct page_pool_params *params)
371 {
372 	return page_pool_create_percpu(params, -1);
373 }
374 EXPORT_SYMBOL(page_pool_create);
375 
376 static void page_pool_return_netmem(struct page_pool *pool, netmem_ref netmem);
377 
page_pool_refill_alloc_cache(struct page_pool * pool)378 static noinline netmem_ref page_pool_refill_alloc_cache(struct page_pool *pool)
379 {
380 	struct ptr_ring *r = &pool->ring;
381 	netmem_ref netmem;
382 	int pref_nid; /* preferred NUMA node */
383 
384 	/* Quicker fallback, avoid locks when ring is empty */
385 	if (__ptr_ring_empty(r)) {
386 		alloc_stat_inc(pool, empty);
387 		return 0;
388 	}
389 
390 	/* Softirq guarantee CPU and thus NUMA node is stable. This,
391 	 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
392 	 */
393 #ifdef CONFIG_NUMA
394 	pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
395 #else
396 	/* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
397 	pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
398 #endif
399 
400 	/* Refill alloc array, but only if NUMA match */
401 	do {
402 		netmem = (__force netmem_ref)__ptr_ring_consume(r);
403 		if (unlikely(!netmem))
404 			break;
405 
406 		if (likely(netmem_is_pref_nid(netmem, pref_nid))) {
407 			pool->alloc.cache[pool->alloc.count++] = netmem;
408 		} else {
409 			/* NUMA mismatch;
410 			 * (1) release 1 page to page-allocator and
411 			 * (2) break out to fallthrough to alloc_pages_node.
412 			 * This limit stress on page buddy alloactor.
413 			 */
414 			page_pool_return_netmem(pool, netmem);
415 			alloc_stat_inc(pool, waive);
416 			netmem = 0;
417 			break;
418 		}
419 	} while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
420 
421 	/* Return last page */
422 	if (likely(pool->alloc.count > 0)) {
423 		netmem = pool->alloc.cache[--pool->alloc.count];
424 		alloc_stat_inc(pool, refill);
425 	}
426 
427 	return netmem;
428 }
429 
430 /* fast path */
__page_pool_get_cached(struct page_pool * pool)431 static netmem_ref __page_pool_get_cached(struct page_pool *pool)
432 {
433 	netmem_ref netmem;
434 
435 	/* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
436 	if (likely(pool->alloc.count)) {
437 		/* Fast-path */
438 		netmem = pool->alloc.cache[--pool->alloc.count];
439 		alloc_stat_inc(pool, fast);
440 	} else {
441 		netmem = page_pool_refill_alloc_cache(pool);
442 	}
443 
444 	return netmem;
445 }
446 
__page_pool_dma_sync_for_device(const struct page_pool * pool,netmem_ref netmem,u32 dma_sync_size)447 static void __page_pool_dma_sync_for_device(const struct page_pool *pool,
448 					    netmem_ref netmem,
449 					    u32 dma_sync_size)
450 {
451 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC)
452 	dma_addr_t dma_addr = page_pool_get_dma_addr_netmem(netmem);
453 
454 	dma_sync_size = min(dma_sync_size, pool->p.max_len);
455 	__dma_sync_single_for_device(pool->p.dev, dma_addr + pool->p.offset,
456 				     dma_sync_size, pool->p.dma_dir);
457 #endif
458 }
459 
460 static __always_inline void
page_pool_dma_sync_for_device(const struct page_pool * pool,netmem_ref netmem,u32 dma_sync_size)461 page_pool_dma_sync_for_device(const struct page_pool *pool,
462 			      netmem_ref netmem,
463 			      u32 dma_sync_size)
464 {
465 	if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) {
466 		rcu_read_lock();
467 		/* re-check under rcu_read_lock() to sync with page_pool_scrub() */
468 		if (pool->dma_sync)
469 			__page_pool_dma_sync_for_device(pool, netmem,
470 							dma_sync_size);
471 		rcu_read_unlock();
472 	}
473 }
474 
page_pool_register_dma_index(struct page_pool * pool,netmem_ref netmem,gfp_t gfp)475 static int page_pool_register_dma_index(struct page_pool *pool,
476 					netmem_ref netmem, gfp_t gfp)
477 {
478 	int err = 0;
479 	u32 id;
480 
481 	if (unlikely(!PP_DMA_INDEX_BITS))
482 		goto out;
483 
484 	if (in_softirq())
485 		err = xa_alloc(&pool->dma_mapped, &id, netmem_to_page(netmem),
486 			       PP_DMA_INDEX_LIMIT, gfp);
487 	else
488 		err = xa_alloc_bh(&pool->dma_mapped, &id, netmem_to_page(netmem),
489 				  PP_DMA_INDEX_LIMIT, gfp);
490 	if (err) {
491 		WARN_ONCE(err != -ENOMEM, "couldn't track DMA mapping, please report to netdev@");
492 		goto out;
493 	}
494 
495 	netmem_set_dma_index(netmem, id);
496 out:
497 	return err;
498 }
499 
page_pool_release_dma_index(struct page_pool * pool,netmem_ref netmem)500 static int page_pool_release_dma_index(struct page_pool *pool,
501 				       netmem_ref netmem)
502 {
503 	struct page *old, *page = netmem_to_page(netmem);
504 	unsigned long id;
505 
506 	if (unlikely(!PP_DMA_INDEX_BITS))
507 		return 0;
508 
509 	id = netmem_get_dma_index(netmem);
510 	if (!id)
511 		return -1;
512 
513 	if (in_softirq())
514 		old = xa_cmpxchg(&pool->dma_mapped, id, page, NULL, 0);
515 	else
516 		old = xa_cmpxchg_bh(&pool->dma_mapped, id, page, NULL, 0);
517 	if (old != page)
518 		return -1;
519 
520 	netmem_set_dma_index(netmem, 0);
521 
522 	return 0;
523 }
524 
page_pool_dma_map(struct page_pool * pool,netmem_ref netmem,gfp_t gfp)525 static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t gfp)
526 {
527 	dma_addr_t dma;
528 	int err;
529 
530 	/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
531 	 * since dma_addr_t can be either 32 or 64 bits and does not always fit
532 	 * into page private data (i.e 32bit cpu with 64bit DMA caps)
533 	 * This mapping is kept for lifetime of page, until leaving pool.
534 	 */
535 	dma = dma_map_page_attrs(pool->p.dev, netmem_to_page(netmem), 0,
536 				 (PAGE_SIZE << pool->p.order), pool->p.dma_dir,
537 				 DMA_ATTR_SKIP_CPU_SYNC |
538 					 DMA_ATTR_WEAK_ORDERING);
539 	if (dma_mapping_error(pool->p.dev, dma))
540 		return false;
541 
542 	if (page_pool_set_dma_addr_netmem(netmem, dma)) {
543 		WARN_ONCE(1, "unexpected DMA address, please report to netdev@");
544 		goto unmap_failed;
545 	}
546 
547 	err = page_pool_register_dma_index(pool, netmem, gfp);
548 	if (err)
549 		goto unset_failed;
550 
551 	page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len);
552 
553 	return true;
554 
555 unset_failed:
556 	page_pool_set_dma_addr_netmem(netmem, 0);
557 unmap_failed:
558 	dma_unmap_page_attrs(pool->p.dev, dma,
559 			     PAGE_SIZE << pool->p.order, pool->p.dma_dir,
560 			     DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
561 	return false;
562 }
563 
__page_pool_alloc_page_order(struct page_pool * pool,gfp_t gfp)564 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
565 						 gfp_t gfp)
566 {
567 	struct page *page;
568 
569 	gfp |= __GFP_COMP;
570 	page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
571 	if (unlikely(!page))
572 		return NULL;
573 
574 	if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page), gfp))) {
575 		put_page(page);
576 		return NULL;
577 	}
578 
579 	alloc_stat_inc(pool, slow_high_order);
580 	page_pool_set_pp_info(pool, page_to_netmem(page));
581 
582 	/* Track how many pages are held 'in-flight' */
583 	pool->pages_state_hold_cnt++;
584 	trace_page_pool_state_hold(pool, page_to_netmem(page),
585 				   pool->pages_state_hold_cnt);
586 	return page;
587 }
588 
589 /* slow path */
__page_pool_alloc_netmems_slow(struct page_pool * pool,gfp_t gfp)590 static noinline netmem_ref __page_pool_alloc_netmems_slow(struct page_pool *pool,
591 							  gfp_t gfp)
592 {
593 	const int bulk = PP_ALLOC_CACHE_REFILL;
594 	unsigned int pp_order = pool->p.order;
595 	bool dma_map = pool->dma_map;
596 	netmem_ref netmem;
597 	int i, nr_pages;
598 
599 	/* Unconditionally set NOWARN if allocating from NAPI.
600 	 * Drivers forget to set it, and OOM reports on packet Rx are useless.
601 	 */
602 	if ((gfp & GFP_ATOMIC) == GFP_ATOMIC)
603 		gfp |= __GFP_NOWARN;
604 
605 	/* Don't support bulk alloc for high-order pages */
606 	if (unlikely(pp_order))
607 		return page_to_netmem(__page_pool_alloc_page_order(pool, gfp));
608 
609 	/* Unnecessary as alloc cache is empty, but guarantees zero count */
610 	if (unlikely(pool->alloc.count > 0))
611 		return pool->alloc.cache[--pool->alloc.count];
612 
613 	/* Mark empty alloc.cache slots "empty" for alloc_pages_bulk */
614 	memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
615 
616 	nr_pages = alloc_pages_bulk_node(gfp, pool->p.nid, bulk,
617 					 (struct page **)pool->alloc.cache);
618 	if (unlikely(!nr_pages))
619 		return 0;
620 
621 	/* Pages have been filled into alloc.cache array, but count is zero and
622 	 * page element have not been (possibly) DMA mapped.
623 	 */
624 	for (i = 0; i < nr_pages; i++) {
625 		netmem = pool->alloc.cache[i];
626 		if (dma_map && unlikely(!page_pool_dma_map(pool, netmem, gfp))) {
627 			put_page(netmem_to_page(netmem));
628 			continue;
629 		}
630 
631 		page_pool_set_pp_info(pool, netmem);
632 		pool->alloc.cache[pool->alloc.count++] = netmem;
633 		/* Track how many pages are held 'in-flight' */
634 		pool->pages_state_hold_cnt++;
635 		trace_page_pool_state_hold(pool, netmem,
636 					   pool->pages_state_hold_cnt);
637 	}
638 
639 	/* Return last page */
640 	if (likely(pool->alloc.count > 0)) {
641 		netmem = pool->alloc.cache[--pool->alloc.count];
642 		alloc_stat_inc(pool, slow);
643 	} else {
644 		netmem = 0;
645 	}
646 
647 	/* When page just alloc'ed is should/must have refcnt 1. */
648 	return netmem;
649 }
650 
651 /* For using page_pool replace: alloc_pages() API calls, but provide
652  * synchronization guarantee for allocation side.
653  */
page_pool_alloc_netmems(struct page_pool * pool,gfp_t gfp)654 netmem_ref page_pool_alloc_netmems(struct page_pool *pool, gfp_t gfp)
655 {
656 	netmem_ref netmem;
657 
658 	/* Fast-path: Get a page from cache */
659 	netmem = __page_pool_get_cached(pool);
660 	if (netmem)
661 		return netmem;
662 
663 	/* Slow-path: cache empty, do real allocation */
664 	if (static_branch_unlikely(&page_pool_mem_providers) && pool->mp_ops)
665 		netmem = pool->mp_ops->alloc_netmems(pool, gfp);
666 	else
667 		netmem = __page_pool_alloc_netmems_slow(pool, gfp);
668 	return netmem;
669 }
670 EXPORT_SYMBOL(page_pool_alloc_netmems);
671 ALLOW_ERROR_INJECTION(page_pool_alloc_netmems, NULL);
672 
page_pool_alloc_pages(struct page_pool * pool,gfp_t gfp)673 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
674 {
675 	return netmem_to_page(page_pool_alloc_netmems(pool, gfp));
676 }
677 EXPORT_SYMBOL(page_pool_alloc_pages);
678 
679 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
680  *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
681  */
682 #define _distance(a, b)	(s32)((a) - (b))
683 
page_pool_inflight(const struct page_pool * pool,bool strict)684 s32 page_pool_inflight(const struct page_pool *pool, bool strict)
685 {
686 	u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
687 	u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
688 	s32 inflight;
689 
690 	inflight = _distance(hold_cnt, release_cnt);
691 
692 	if (strict) {
693 		trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
694 		WARN(inflight < 0, "Negative(%d) inflight packet-pages",
695 		     inflight);
696 	} else {
697 		inflight = max(0, inflight);
698 	}
699 
700 	return inflight;
701 }
702 
page_pool_set_pp_info(struct page_pool * pool,netmem_ref netmem)703 void page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem)
704 {
705 	struct page *page;
706 
707 	netmem_set_pp(netmem, pool);
708 
709 	/* XXX: Now that the offset of page_type is shared between
710 	 * struct page and net_iov, just cast the netmem to struct page
711 	 * unconditionally by clearing NET_IOV if any, no matter whether
712 	 * it comes from struct net_iov or struct page.  This should be
713 	 * adjusted once the offset is no longer shared.
714 	 */
715 	page = (struct page *)((__force unsigned long)netmem & ~NET_IOV);
716 	__SetPageNetpp(page);
717 
718 	/* Ensuring all pages have been split into one fragment initially:
719 	 * page_pool_set_pp_info() is only called once for every page when it
720 	 * is allocated from the page allocator and page_pool_fragment_page()
721 	 * is dirtying the same cache line as the page->pp_magic above, so
722 	 * the overhead is negligible.
723 	 */
724 	page_pool_fragment_netmem(netmem, 1);
725 	if (pool->has_init_callback)
726 		pool->slow.init_callback(netmem, pool->slow.init_arg);
727 }
728 
page_pool_clear_pp_info(netmem_ref netmem)729 void page_pool_clear_pp_info(netmem_ref netmem)
730 {
731 	struct page *page;
732 
733 	/* XXX: Now that the offset of page_type is shared between
734 	 * struct page and net_iov, just cast the netmem to struct page
735 	 * unconditionally by clearing NET_IOV if any, no matter whether
736 	 * it comes from struct net_iov or struct page.  This should be
737 	 * adjusted once the offset is no longer shared.
738 	 */
739 	page = (struct page *)((__force unsigned long)netmem & ~NET_IOV);
740 	__ClearPageNetpp(page);
741 
742 	netmem_set_pp(netmem, NULL);
743 }
744 
__page_pool_release_netmem_dma(struct page_pool * pool,netmem_ref netmem)745 static __always_inline void __page_pool_release_netmem_dma(struct page_pool *pool,
746 							   netmem_ref netmem)
747 {
748 	dma_addr_t dma;
749 
750 	if (!pool->dma_map)
751 		/* Always account for inflight pages, even if we didn't
752 		 * map them
753 		 */
754 		return;
755 
756 	if (page_pool_release_dma_index(pool, netmem))
757 		return;
758 
759 	dma = page_pool_get_dma_addr_netmem(netmem);
760 
761 	/* When page is unmapped, it cannot be returned to our pool */
762 	dma_unmap_page_attrs(pool->p.dev, dma,
763 			     PAGE_SIZE << pool->p.order, pool->p.dma_dir,
764 			     DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
765 	page_pool_set_dma_addr_netmem(netmem, 0);
766 }
767 
768 /* Disconnects a page (from a page_pool).  API users can have a need
769  * to disconnect a page (from a page_pool), to allow it to be used as
770  * a regular page (that will eventually be returned to the normal
771  * page-allocator via put_page).
772  */
page_pool_return_netmem(struct page_pool * pool,netmem_ref netmem)773 static void page_pool_return_netmem(struct page_pool *pool, netmem_ref netmem)
774 {
775 	int count;
776 	bool put;
777 
778 	put = true;
779 	if (static_branch_unlikely(&page_pool_mem_providers) && pool->mp_ops)
780 		put = pool->mp_ops->release_netmem(pool, netmem);
781 	else
782 		__page_pool_release_netmem_dma(pool, netmem);
783 
784 	/* This may be the last page returned, releasing the pool, so
785 	 * it is not safe to reference pool afterwards.
786 	 */
787 	count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
788 	trace_page_pool_state_release(pool, netmem, count);
789 
790 	if (put) {
791 		page_pool_clear_pp_info(netmem);
792 		put_page(netmem_to_page(netmem));
793 	}
794 	/* An optimization would be to call __free_pages(page, pool->p.order)
795 	 * knowing page is not part of page-cache (thus avoiding a
796 	 * __page_cache_release() call).
797 	 */
798 }
799 
page_pool_recycle_in_ring(struct page_pool * pool,netmem_ref netmem)800 static bool page_pool_recycle_in_ring(struct page_pool *pool, netmem_ref netmem)
801 {
802 	bool in_softirq, ret;
803 
804 	/* BH protection not needed if current is softirq */
805 	in_softirq = page_pool_producer_lock(pool);
806 	ret = !__ptr_ring_produce(&pool->ring, (__force void *)netmem);
807 	if (ret)
808 		recycle_stat_inc(pool, ring);
809 	page_pool_producer_unlock(pool, in_softirq);
810 
811 	return ret;
812 }
813 
814 /* Only allow direct recycling in special circumstances, into the
815  * alloc side cache.  E.g. during RX-NAPI processing for XDP_DROP use-case.
816  *
817  * Caller must provide appropriate safe context.
818  */
page_pool_recycle_in_cache(netmem_ref netmem,struct page_pool * pool)819 static bool page_pool_recycle_in_cache(netmem_ref netmem,
820 				       struct page_pool *pool)
821 {
822 	if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) {
823 		recycle_stat_inc(pool, cache_full);
824 		return false;
825 	}
826 
827 	/* Caller MUST have verified/know (page_ref_count(page) == 1) */
828 	pool->alloc.cache[pool->alloc.count++] = netmem;
829 	recycle_stat_inc(pool, cached);
830 	return true;
831 }
832 
__page_pool_page_can_be_recycled(netmem_ref netmem)833 static bool __page_pool_page_can_be_recycled(netmem_ref netmem)
834 {
835 	return netmem_is_net_iov(netmem) ||
836 	       (page_ref_count(netmem_to_page(netmem)) == 1 &&
837 		!page_is_pfmemalloc(netmem_to_page(netmem)));
838 }
839 
840 /* If the page refcnt == 1, this will try to recycle the page.
841  * If pool->dma_sync is set, we'll try to sync the DMA area for
842  * the configured size min(dma_sync_size, pool->max_len).
843  * If the page refcnt != 1, then the page will be returned to memory
844  * subsystem.
845  */
846 static __always_inline netmem_ref
__page_pool_put_page(struct page_pool * pool,netmem_ref netmem,unsigned int dma_sync_size,bool allow_direct)847 __page_pool_put_page(struct page_pool *pool, netmem_ref netmem,
848 		     unsigned int dma_sync_size, bool allow_direct)
849 {
850 	lockdep_assert_no_hardirq();
851 
852 	/* This allocator is optimized for the XDP mode that uses
853 	 * one-frame-per-page, but have fallbacks that act like the
854 	 * regular page allocator APIs.
855 	 *
856 	 * refcnt == 1 means page_pool owns page, and can recycle it.
857 	 *
858 	 * page is NOT reusable when allocated when system is under
859 	 * some pressure. (page_is_pfmemalloc)
860 	 */
861 	if (likely(__page_pool_page_can_be_recycled(netmem))) {
862 		/* Read barrier done in page_ref_count / READ_ONCE */
863 
864 		page_pool_dma_sync_for_device(pool, netmem, dma_sync_size);
865 
866 		if (allow_direct && page_pool_recycle_in_cache(netmem, pool))
867 			return 0;
868 
869 		/* Page found as candidate for recycling */
870 		return netmem;
871 	}
872 
873 	/* Fallback/non-XDP mode: API user have elevated refcnt.
874 	 *
875 	 * Many drivers split up the page into fragments, and some
876 	 * want to keep doing this to save memory and do refcnt based
877 	 * recycling. Support this use case too, to ease drivers
878 	 * switching between XDP/non-XDP.
879 	 *
880 	 * In-case page_pool maintains the DMA mapping, API user must
881 	 * call page_pool_put_page once.  In this elevated refcnt
882 	 * case, the DMA is unmapped/released, as driver is likely
883 	 * doing refcnt based recycle tricks, meaning another process
884 	 * will be invoking put_page.
885 	 */
886 	recycle_stat_inc(pool, released_refcnt);
887 	page_pool_return_netmem(pool, netmem);
888 
889 	return 0;
890 }
891 
page_pool_napi_local(const struct page_pool * pool)892 static bool page_pool_napi_local(const struct page_pool *pool)
893 {
894 	const struct napi_struct *napi;
895 	u32 cpuid;
896 
897 	/* On PREEMPT_RT the softirq can be preempted by the consumer */
898 	if (IS_ENABLED(CONFIG_PREEMPT_RT))
899 		return false;
900 
901 	if (unlikely(!in_softirq()))
902 		return false;
903 
904 	/* Allow direct recycle if we have reasons to believe that we are
905 	 * in the same context as the consumer would run, so there's
906 	 * no possible race.
907 	 * __page_pool_put_page() makes sure we're not in hardirq context
908 	 * and interrupts are enabled prior to accessing the cache.
909 	 */
910 	cpuid = smp_processor_id();
911 	if (READ_ONCE(pool->cpuid) == cpuid)
912 		return true;
913 
914 	napi = READ_ONCE(pool->p.napi);
915 
916 	return napi && READ_ONCE(napi->list_owner) == cpuid;
917 }
918 
page_pool_put_unrefed_netmem(struct page_pool * pool,netmem_ref netmem,unsigned int dma_sync_size,bool allow_direct)919 void page_pool_put_unrefed_netmem(struct page_pool *pool, netmem_ref netmem,
920 				  unsigned int dma_sync_size, bool allow_direct)
921 {
922 	if (!allow_direct)
923 		allow_direct = page_pool_napi_local(pool);
924 
925 	netmem = __page_pool_put_page(pool, netmem, dma_sync_size,
926 				      allow_direct);
927 	if (netmem && !page_pool_recycle_in_ring(pool, netmem)) {
928 		/* Cache full, fallback to free pages */
929 		recycle_stat_inc(pool, ring_full);
930 		page_pool_return_netmem(pool, netmem);
931 	}
932 }
933 EXPORT_SYMBOL(page_pool_put_unrefed_netmem);
934 
page_pool_put_unrefed_page(struct page_pool * pool,struct page * page,unsigned int dma_sync_size,bool allow_direct)935 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page,
936 				unsigned int dma_sync_size, bool allow_direct)
937 {
938 	page_pool_put_unrefed_netmem(pool, page_to_netmem(page), dma_sync_size,
939 				     allow_direct);
940 }
941 EXPORT_SYMBOL(page_pool_put_unrefed_page);
942 
page_pool_recycle_ring_bulk(struct page_pool * pool,netmem_ref * bulk,u32 bulk_len)943 static void page_pool_recycle_ring_bulk(struct page_pool *pool,
944 					netmem_ref *bulk,
945 					u32 bulk_len)
946 {
947 	bool in_softirq;
948 	u32 i;
949 
950 	/* Bulk produce into ptr_ring page_pool cache */
951 	in_softirq = page_pool_producer_lock(pool);
952 
953 	for (i = 0; i < bulk_len; i++) {
954 		if (__ptr_ring_produce(&pool->ring, (__force void *)bulk[i])) {
955 			/* ring full */
956 			recycle_stat_inc(pool, ring_full);
957 			break;
958 		}
959 	}
960 
961 	page_pool_producer_unlock(pool, in_softirq);
962 	recycle_stat_add(pool, ring, i);
963 
964 	/* Hopefully all pages were returned into ptr_ring */
965 	if (likely(i == bulk_len))
966 		return;
967 
968 	/*
969 	 * ptr_ring cache is full, free remaining pages outside producer lock
970 	 * since put_page() with refcnt == 1 can be an expensive operation.
971 	 */
972 	for (; i < bulk_len; i++)
973 		page_pool_return_netmem(pool, bulk[i]);
974 }
975 
976 /**
977  * page_pool_put_netmem_bulk() - release references on multiple netmems
978  * @data:	array holding netmem references
979  * @count:	number of entries in @data
980  *
981  * Tries to refill a number of netmems into the ptr_ring cache holding ptr_ring
982  * producer lock. If the ptr_ring is full, page_pool_put_netmem_bulk()
983  * will release leftover netmems to the memory provider.
984  * page_pool_put_netmem_bulk() is suitable to be run inside the driver NAPI tx
985  * completion loop for the XDP_REDIRECT use case.
986  *
987  * Please note the caller must not use data area after running
988  * page_pool_put_netmem_bulk(), as this function overwrites it.
989  */
page_pool_put_netmem_bulk(netmem_ref * data,u32 count)990 void page_pool_put_netmem_bulk(netmem_ref *data, u32 count)
991 {
992 	u32 bulk_len = 0;
993 
994 	for (u32 i = 0; i < count; i++) {
995 		netmem_ref netmem = netmem_compound_head(data[i]);
996 
997 		if (page_pool_unref_and_test(netmem))
998 			data[bulk_len++] = netmem;
999 	}
1000 
1001 	count = bulk_len;
1002 	while (count) {
1003 		netmem_ref bulk[XDP_BULK_QUEUE_SIZE];
1004 		struct page_pool *pool = NULL;
1005 		bool allow_direct;
1006 		u32 foreign = 0;
1007 
1008 		bulk_len = 0;
1009 
1010 		for (u32 i = 0; i < count; i++) {
1011 			struct page_pool *netmem_pp;
1012 			netmem_ref netmem = data[i];
1013 
1014 			netmem_pp = netmem_get_pp(netmem);
1015 			if (unlikely(!pool)) {
1016 				pool = netmem_pp;
1017 				allow_direct = page_pool_napi_local(pool);
1018 			} else if (netmem_pp != pool) {
1019 				/*
1020 				 * If the netmem belongs to a different
1021 				 * page_pool, save it for another round.
1022 				 */
1023 				data[foreign++] = netmem;
1024 				continue;
1025 			}
1026 
1027 			netmem = __page_pool_put_page(pool, netmem, -1,
1028 						      allow_direct);
1029 			/* Approved for bulk recycling in ptr_ring cache */
1030 			if (netmem)
1031 				bulk[bulk_len++] = netmem;
1032 		}
1033 
1034 		if (bulk_len)
1035 			page_pool_recycle_ring_bulk(pool, bulk, bulk_len);
1036 
1037 		count = foreign;
1038 	}
1039 }
1040 EXPORT_SYMBOL(page_pool_put_netmem_bulk);
1041 
page_pool_drain_frag(struct page_pool * pool,netmem_ref netmem)1042 static netmem_ref page_pool_drain_frag(struct page_pool *pool,
1043 				       netmem_ref netmem)
1044 {
1045 	long drain_count = BIAS_MAX - pool->frag_users;
1046 
1047 	/* Some user is still using the page frag */
1048 	if (likely(page_pool_unref_netmem(netmem, drain_count)))
1049 		return 0;
1050 
1051 	if (__page_pool_page_can_be_recycled(netmem)) {
1052 		page_pool_dma_sync_for_device(pool, netmem, -1);
1053 		return netmem;
1054 	}
1055 
1056 	page_pool_return_netmem(pool, netmem);
1057 	return 0;
1058 }
1059 
page_pool_free_frag(struct page_pool * pool)1060 static void page_pool_free_frag(struct page_pool *pool)
1061 {
1062 	long drain_count = BIAS_MAX - pool->frag_users;
1063 	netmem_ref netmem = pool->frag_page;
1064 
1065 	pool->frag_page = 0;
1066 
1067 	if (!netmem || page_pool_unref_netmem(netmem, drain_count))
1068 		return;
1069 
1070 	page_pool_return_netmem(pool, netmem);
1071 }
1072 
page_pool_alloc_frag_netmem(struct page_pool * pool,unsigned int * offset,unsigned int size,gfp_t gfp)1073 netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,
1074 				       unsigned int *offset, unsigned int size,
1075 				       gfp_t gfp)
1076 {
1077 	unsigned int max_size = PAGE_SIZE << pool->p.order;
1078 	netmem_ref netmem = pool->frag_page;
1079 
1080 	if (WARN_ON(size > max_size))
1081 		return 0;
1082 
1083 	size = ALIGN(size, dma_get_cache_alignment());
1084 	*offset = pool->frag_offset;
1085 
1086 	if (netmem && *offset + size > max_size) {
1087 		netmem = page_pool_drain_frag(pool, netmem);
1088 		if (netmem) {
1089 			recycle_stat_inc(pool, cached);
1090 			alloc_stat_inc(pool, fast);
1091 			goto frag_reset;
1092 		}
1093 	}
1094 
1095 	if (!netmem) {
1096 		netmem = page_pool_alloc_netmems(pool, gfp);
1097 		if (unlikely(!netmem)) {
1098 			pool->frag_page = 0;
1099 			return 0;
1100 		}
1101 
1102 		pool->frag_page = netmem;
1103 
1104 frag_reset:
1105 		pool->frag_users = 1;
1106 		*offset = 0;
1107 		pool->frag_offset = size;
1108 		page_pool_fragment_netmem(netmem, BIAS_MAX);
1109 		return netmem;
1110 	}
1111 
1112 	pool->frag_users++;
1113 	pool->frag_offset = *offset + size;
1114 	return netmem;
1115 }
1116 EXPORT_SYMBOL(page_pool_alloc_frag_netmem);
1117 
page_pool_alloc_frag(struct page_pool * pool,unsigned int * offset,unsigned int size,gfp_t gfp)1118 struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset,
1119 				  unsigned int size, gfp_t gfp)
1120 {
1121 	return netmem_to_page(page_pool_alloc_frag_netmem(pool, offset, size,
1122 							  gfp));
1123 }
1124 EXPORT_SYMBOL(page_pool_alloc_frag);
1125 
page_pool_empty_ring(struct page_pool * pool)1126 static void page_pool_empty_ring(struct page_pool *pool)
1127 {
1128 	netmem_ref netmem;
1129 
1130 	/* Empty recycle ring */
1131 	while ((netmem = (__force netmem_ref)ptr_ring_consume_bh(&pool->ring))) {
1132 		/* Verify the refcnt invariant of cached pages */
1133 		if (!(netmem_ref_count(netmem) == 1))
1134 			pr_crit("%s() page_pool refcnt %d violation\n",
1135 				__func__, netmem_ref_count(netmem));
1136 
1137 		page_pool_return_netmem(pool, netmem);
1138 	}
1139 }
1140 
__page_pool_destroy(struct page_pool * pool)1141 static void __page_pool_destroy(struct page_pool *pool)
1142 {
1143 	if (pool->disconnect)
1144 		pool->disconnect(pool);
1145 
1146 	page_pool_unlist(pool);
1147 	page_pool_uninit(pool);
1148 
1149 	if (pool->mp_ops) {
1150 		pool->mp_ops->destroy(pool);
1151 		static_branch_dec(&page_pool_mem_providers);
1152 	}
1153 
1154 	kfree(pool);
1155 }
1156 
page_pool_empty_alloc_cache_once(struct page_pool * pool)1157 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
1158 {
1159 	netmem_ref netmem;
1160 
1161 	if (pool->destroy_cnt)
1162 		return;
1163 
1164 	/* Empty alloc cache, assume caller made sure this is
1165 	 * no-longer in use, and page_pool_alloc_pages() cannot be
1166 	 * call concurrently.
1167 	 */
1168 	while (pool->alloc.count) {
1169 		netmem = pool->alloc.cache[--pool->alloc.count];
1170 		page_pool_return_netmem(pool, netmem);
1171 	}
1172 }
1173 
page_pool_scrub(struct page_pool * pool)1174 static void page_pool_scrub(struct page_pool *pool)
1175 {
1176 	unsigned long id;
1177 	void *ptr;
1178 
1179 	page_pool_empty_alloc_cache_once(pool);
1180 	if (!pool->destroy_cnt++ && pool->dma_map) {
1181 		if (pool->dma_sync) {
1182 			/* Disable page_pool_dma_sync_for_device() */
1183 			pool->dma_sync = false;
1184 
1185 			/* Make sure all concurrent returns that may see the old
1186 			 * value of dma_sync (and thus perform a sync) have
1187 			 * finished before doing the unmapping below. Skip the
1188 			 * wait if the device doesn't actually need syncing, or
1189 			 * if there are no outstanding mapped pages.
1190 			 */
1191 			if (dma_dev_need_sync(pool->p.dev) &&
1192 			    !xa_empty(&pool->dma_mapped))
1193 				synchronize_net();
1194 		}
1195 
1196 		xa_for_each(&pool->dma_mapped, id, ptr)
1197 			__page_pool_release_netmem_dma(pool, page_to_netmem((struct page *)ptr));
1198 	}
1199 
1200 	/* No more consumers should exist, but producers could still
1201 	 * be in-flight.
1202 	 */
1203 	page_pool_empty_ring(pool);
1204 }
1205 
page_pool_release(struct page_pool * pool)1206 static int page_pool_release(struct page_pool *pool)
1207 {
1208 	bool in_softirq;
1209 	int inflight;
1210 
1211 	page_pool_scrub(pool);
1212 	inflight = page_pool_inflight(pool, true);
1213 	/* Acquire producer lock to make sure producers have exited. */
1214 	in_softirq = page_pool_producer_lock(pool);
1215 	page_pool_producer_unlock(pool, in_softirq);
1216 	if (!inflight)
1217 		__page_pool_destroy(pool);
1218 
1219 	return inflight;
1220 }
1221 
page_pool_release_retry(struct work_struct * wq)1222 static void page_pool_release_retry(struct work_struct *wq)
1223 {
1224 	struct delayed_work *dwq = to_delayed_work(wq);
1225 	struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
1226 	void *netdev;
1227 	int inflight;
1228 
1229 	inflight = page_pool_release(pool);
1230 	/* In rare cases, a driver bug may cause inflight to go negative.
1231 	 * Don't reschedule release if inflight is 0 or negative.
1232 	 * - If 0, the page_pool has been destroyed
1233 	 * - if negative, we will never recover
1234 	 * in both cases no reschedule is necessary.
1235 	 */
1236 	if (inflight <= 0)
1237 		return;
1238 
1239 	/* Periodic warning for page pools the user can't see */
1240 	netdev = READ_ONCE(pool->slow.netdev);
1241 	if (time_after_eq(jiffies, pool->defer_warn) &&
1242 	    (!netdev || netdev == NET_PTR_POISON)) {
1243 		int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
1244 
1245 		pr_warn("%s() stalled pool shutdown: id %u, %d inflight %d sec\n",
1246 			__func__, pool->user.id, inflight, sec);
1247 		pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
1248 	}
1249 
1250 	/* Still not ready to be disconnected, retry later */
1251 	schedule_delayed_work(&pool->release_dw, DEFER_TIME);
1252 }
1253 
page_pool_use_xdp_mem(struct page_pool * pool,void (* disconnect)(void *),const struct xdp_mem_info * mem)1254 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
1255 			   const struct xdp_mem_info *mem)
1256 {
1257 	refcount_inc(&pool->user_cnt);
1258 	pool->disconnect = disconnect;
1259 	pool->xdp_mem_id = mem->id;
1260 }
1261 
1262 /**
1263  * page_pool_enable_direct_recycling() - mark page pool as owned by NAPI
1264  * @pool: page pool to modify
1265  * @napi: NAPI instance to associate the page pool with
1266  *
1267  * Associate a page pool with a NAPI instance for lockless page recycling.
1268  * This is useful when a new page pool has to be added to a NAPI instance
1269  * without disabling that NAPI instance, to mark the point at which control
1270  * path "hands over" the page pool to the NAPI instance. In most cases driver
1271  * can simply set the @napi field in struct page_pool_params, and does not
1272  * have to call this helper.
1273  *
1274  * The function is idempotent, but does not implement any refcounting.
1275  * Single page_pool_disable_direct_recycling() will disable recycling,
1276  * no matter how many times enable was called.
1277  */
page_pool_enable_direct_recycling(struct page_pool * pool,struct napi_struct * napi)1278 void page_pool_enable_direct_recycling(struct page_pool *pool,
1279 				       struct napi_struct *napi)
1280 {
1281 	if (READ_ONCE(pool->p.napi) == napi)
1282 		return;
1283 	WARN_ON(!napi || pool->p.napi);
1284 
1285 	mutex_lock(&page_pools_lock);
1286 	WRITE_ONCE(pool->p.napi, napi);
1287 	mutex_unlock(&page_pools_lock);
1288 }
1289 EXPORT_SYMBOL(page_pool_enable_direct_recycling);
1290 
page_pool_disable_direct_recycling(struct page_pool * pool)1291 void page_pool_disable_direct_recycling(struct page_pool *pool)
1292 {
1293 	/* Disable direct recycling based on pool->cpuid.
1294 	 * Paired with READ_ONCE() in page_pool_napi_local().
1295 	 */
1296 	WRITE_ONCE(pool->cpuid, -1);
1297 
1298 	if (!pool->p.napi)
1299 		return;
1300 
1301 	napi_assert_will_not_race(pool->p.napi);
1302 
1303 	mutex_lock(&page_pools_lock);
1304 	WRITE_ONCE(pool->p.napi, NULL);
1305 	mutex_unlock(&page_pools_lock);
1306 }
1307 EXPORT_SYMBOL(page_pool_disable_direct_recycling);
1308 
page_pool_destroy(struct page_pool * pool)1309 void page_pool_destroy(struct page_pool *pool)
1310 {
1311 	if (!pool)
1312 		return;
1313 
1314 	if (!page_pool_put(pool))
1315 		return;
1316 
1317 	page_pool_disable_direct_recycling(pool);
1318 	page_pool_free_frag(pool);
1319 
1320 	if (!page_pool_release(pool))
1321 		return;
1322 
1323 	page_pool_detached(pool);
1324 	pool->defer_start = jiffies;
1325 	pool->defer_warn  = jiffies + DEFER_WARN_INTERVAL;
1326 
1327 	INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
1328 	schedule_delayed_work(&pool->release_dw, DEFER_TIME);
1329 }
1330 EXPORT_SYMBOL(page_pool_destroy);
1331 
1332 /* Caller must provide appropriate safe context, e.g. NAPI. */
page_pool_update_nid(struct page_pool * pool,int new_nid)1333 void page_pool_update_nid(struct page_pool *pool, int new_nid)
1334 {
1335 	netmem_ref netmem;
1336 
1337 	trace_page_pool_update_nid(pool, new_nid);
1338 	pool->p.nid = new_nid;
1339 
1340 	/* Flush pool alloc cache, as refill will check NUMA node */
1341 	while (pool->alloc.count) {
1342 		netmem = pool->alloc.cache[--pool->alloc.count];
1343 		page_pool_return_netmem(pool, netmem);
1344 	}
1345 }
1346 EXPORT_SYMBOL(page_pool_update_nid);
1347 
net_mp_niov_set_dma_addr(struct net_iov * niov,dma_addr_t addr)1348 bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr)
1349 {
1350 	return page_pool_set_dma_addr_netmem(net_iov_to_netmem(niov), addr);
1351 }
1352 
1353 /* Associate a niov with a page pool. Should follow with a matching
1354  * net_mp_niov_clear_page_pool()
1355  */
net_mp_niov_set_page_pool(struct page_pool * pool,struct net_iov * niov)1356 void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov)
1357 {
1358 	netmem_ref netmem = net_iov_to_netmem(niov);
1359 
1360 	page_pool_set_pp_info(pool, netmem);
1361 
1362 	pool->pages_state_hold_cnt++;
1363 	trace_page_pool_state_hold(pool, netmem, pool->pages_state_hold_cnt);
1364 }
1365 
1366 /* Disassociate a niov from a page pool. Should only be used in the
1367  * ->release_netmem() path.
1368  */
net_mp_niov_clear_page_pool(struct net_iov * niov)1369 void net_mp_niov_clear_page_pool(struct net_iov *niov)
1370 {
1371 	netmem_ref netmem = net_iov_to_netmem(niov);
1372 
1373 	page_pool_clear_pp_info(netmem);
1374 }
1375