xref: /linux/net/core/page_pool.c (revision 06b9cce42634a50f2840777a66553b02320db5ef)
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/types.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 
13 #include <net/page_pool.h>
14 #include <net/xdp.h>
15 
16 #include <linux/dma-direction.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/page-flags.h>
19 #include <linux/mm.h> /* for __put_page() */
20 #include <linux/poison.h>
21 
22 #include <trace/events/page_pool.h>
23 
24 #define DEFER_TIME (msecs_to_jiffies(1000))
25 #define DEFER_WARN_INTERVAL (60 * HZ)
26 
27 #define BIAS_MAX	LONG_MAX
28 
29 static int page_pool_init(struct page_pool *pool,
30 			  const struct page_pool_params *params)
31 {
32 	unsigned int ring_qsize = 1024; /* Default */
33 
34 	memcpy(&pool->p, params, sizeof(pool->p));
35 
36 	/* Validate only known flags were used */
37 	if (pool->p.flags & ~(PP_FLAG_ALL))
38 		return -EINVAL;
39 
40 	if (pool->p.pool_size)
41 		ring_qsize = pool->p.pool_size;
42 
43 	/* Sanity limit mem that can be pinned down */
44 	if (ring_qsize > 32768)
45 		return -E2BIG;
46 
47 	/* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
48 	 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
49 	 * which is the XDP_TX use-case.
50 	 */
51 	if (pool->p.flags & PP_FLAG_DMA_MAP) {
52 		if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
53 		    (pool->p.dma_dir != DMA_BIDIRECTIONAL))
54 			return -EINVAL;
55 	}
56 
57 	if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV) {
58 		/* In order to request DMA-sync-for-device the page
59 		 * needs to be mapped
60 		 */
61 		if (!(pool->p.flags & PP_FLAG_DMA_MAP))
62 			return -EINVAL;
63 
64 		if (!pool->p.max_len)
65 			return -EINVAL;
66 
67 		/* pool->p.offset has to be set according to the address
68 		 * offset used by the DMA engine to start copying rx data
69 		 */
70 	}
71 
72 	if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT &&
73 	    pool->p.flags & PP_FLAG_PAGE_FRAG)
74 		return -EINVAL;
75 
76 	if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
77 		return -ENOMEM;
78 
79 	atomic_set(&pool->pages_state_release_cnt, 0);
80 
81 	/* Driver calling page_pool_create() also call page_pool_destroy() */
82 	refcount_set(&pool->user_cnt, 1);
83 
84 	if (pool->p.flags & PP_FLAG_DMA_MAP)
85 		get_device(pool->p.dev);
86 
87 	return 0;
88 }
89 
90 struct page_pool *page_pool_create(const struct page_pool_params *params)
91 {
92 	struct page_pool *pool;
93 	int err;
94 
95 	pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
96 	if (!pool)
97 		return ERR_PTR(-ENOMEM);
98 
99 	err = page_pool_init(pool, params);
100 	if (err < 0) {
101 		pr_warn("%s() gave up with errno %d\n", __func__, err);
102 		kfree(pool);
103 		return ERR_PTR(err);
104 	}
105 
106 	return pool;
107 }
108 EXPORT_SYMBOL(page_pool_create);
109 
110 static void page_pool_return_page(struct page_pool *pool, struct page *page);
111 
112 noinline
113 static struct page *page_pool_refill_alloc_cache(struct page_pool *pool)
114 {
115 	struct ptr_ring *r = &pool->ring;
116 	struct page *page;
117 	int pref_nid; /* preferred NUMA node */
118 
119 	/* Quicker fallback, avoid locks when ring is empty */
120 	if (__ptr_ring_empty(r))
121 		return NULL;
122 
123 	/* Softirq guarantee CPU and thus NUMA node is stable. This,
124 	 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
125 	 */
126 #ifdef CONFIG_NUMA
127 	pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
128 #else
129 	/* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
130 	pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
131 #endif
132 
133 	/* Refill alloc array, but only if NUMA match */
134 	do {
135 		page = __ptr_ring_consume(r);
136 		if (unlikely(!page))
137 			break;
138 
139 		if (likely(page_to_nid(page) == pref_nid)) {
140 			pool->alloc.cache[pool->alloc.count++] = page;
141 		} else {
142 			/* NUMA mismatch;
143 			 * (1) release 1 page to page-allocator and
144 			 * (2) break out to fallthrough to alloc_pages_node.
145 			 * This limit stress on page buddy alloactor.
146 			 */
147 			page_pool_return_page(pool, page);
148 			page = NULL;
149 			break;
150 		}
151 	} while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
152 
153 	/* Return last page */
154 	if (likely(pool->alloc.count > 0))
155 		page = pool->alloc.cache[--pool->alloc.count];
156 
157 	return page;
158 }
159 
160 /* fast path */
161 static struct page *__page_pool_get_cached(struct page_pool *pool)
162 {
163 	struct page *page;
164 
165 	/* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
166 	if (likely(pool->alloc.count)) {
167 		/* Fast-path */
168 		page = pool->alloc.cache[--pool->alloc.count];
169 	} else {
170 		page = page_pool_refill_alloc_cache(pool);
171 	}
172 
173 	return page;
174 }
175 
176 static void page_pool_dma_sync_for_device(struct page_pool *pool,
177 					  struct page *page,
178 					  unsigned int dma_sync_size)
179 {
180 	dma_addr_t dma_addr = page_pool_get_dma_addr(page);
181 
182 	dma_sync_size = min(dma_sync_size, pool->p.max_len);
183 	dma_sync_single_range_for_device(pool->p.dev, dma_addr,
184 					 pool->p.offset, dma_sync_size,
185 					 pool->p.dma_dir);
186 }
187 
188 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
189 {
190 	dma_addr_t dma;
191 
192 	/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
193 	 * since dma_addr_t can be either 32 or 64 bits and does not always fit
194 	 * into page private data (i.e 32bit cpu with 64bit DMA caps)
195 	 * This mapping is kept for lifetime of page, until leaving pool.
196 	 */
197 	dma = dma_map_page_attrs(pool->p.dev, page, 0,
198 				 (PAGE_SIZE << pool->p.order),
199 				 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
200 	if (dma_mapping_error(pool->p.dev, dma))
201 		return false;
202 
203 	page_pool_set_dma_addr(page, dma);
204 
205 	if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
206 		page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
207 
208 	return true;
209 }
210 
211 static void page_pool_set_pp_info(struct page_pool *pool,
212 				  struct page *page)
213 {
214 	page->pp = pool;
215 	page->pp_magic |= PP_SIGNATURE;
216 	if (pool->p.init_callback)
217 		pool->p.init_callback(page, pool->p.init_arg);
218 }
219 
220 static void page_pool_clear_pp_info(struct page *page)
221 {
222 	page->pp_magic = 0;
223 	page->pp = NULL;
224 }
225 
226 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
227 						 gfp_t gfp)
228 {
229 	struct page *page;
230 
231 	gfp |= __GFP_COMP;
232 	page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
233 	if (unlikely(!page))
234 		return NULL;
235 
236 	if ((pool->p.flags & PP_FLAG_DMA_MAP) &&
237 	    unlikely(!page_pool_dma_map(pool, page))) {
238 		put_page(page);
239 		return NULL;
240 	}
241 
242 	page_pool_set_pp_info(pool, page);
243 
244 	/* Track how many pages are held 'in-flight' */
245 	pool->pages_state_hold_cnt++;
246 	trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
247 	return page;
248 }
249 
250 /* slow path */
251 noinline
252 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
253 						 gfp_t gfp)
254 {
255 	const int bulk = PP_ALLOC_CACHE_REFILL;
256 	unsigned int pp_flags = pool->p.flags;
257 	unsigned int pp_order = pool->p.order;
258 	struct page *page;
259 	int i, nr_pages;
260 
261 	/* Don't support bulk alloc for high-order pages */
262 	if (unlikely(pp_order))
263 		return __page_pool_alloc_page_order(pool, gfp);
264 
265 	/* Unnecessary as alloc cache is empty, but guarantees zero count */
266 	if (unlikely(pool->alloc.count > 0))
267 		return pool->alloc.cache[--pool->alloc.count];
268 
269 	/* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
270 	memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
271 
272 	nr_pages = alloc_pages_bulk_array(gfp, bulk, pool->alloc.cache);
273 	if (unlikely(!nr_pages))
274 		return NULL;
275 
276 	/* Pages have been filled into alloc.cache array, but count is zero and
277 	 * page element have not been (possibly) DMA mapped.
278 	 */
279 	for (i = 0; i < nr_pages; i++) {
280 		page = pool->alloc.cache[i];
281 		if ((pp_flags & PP_FLAG_DMA_MAP) &&
282 		    unlikely(!page_pool_dma_map(pool, page))) {
283 			put_page(page);
284 			continue;
285 		}
286 
287 		page_pool_set_pp_info(pool, page);
288 		pool->alloc.cache[pool->alloc.count++] = page;
289 		/* Track how many pages are held 'in-flight' */
290 		pool->pages_state_hold_cnt++;
291 		trace_page_pool_state_hold(pool, page,
292 					   pool->pages_state_hold_cnt);
293 	}
294 
295 	/* Return last page */
296 	if (likely(pool->alloc.count > 0))
297 		page = pool->alloc.cache[--pool->alloc.count];
298 	else
299 		page = NULL;
300 
301 	/* When page just alloc'ed is should/must have refcnt 1. */
302 	return page;
303 }
304 
305 /* For using page_pool replace: alloc_pages() API calls, but provide
306  * synchronization guarantee for allocation side.
307  */
308 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
309 {
310 	struct page *page;
311 
312 	/* Fast-path: Get a page from cache */
313 	page = __page_pool_get_cached(pool);
314 	if (page)
315 		return page;
316 
317 	/* Slow-path: cache empty, do real allocation */
318 	page = __page_pool_alloc_pages_slow(pool, gfp);
319 	return page;
320 }
321 EXPORT_SYMBOL(page_pool_alloc_pages);
322 
323 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
324  *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
325  */
326 #define _distance(a, b)	(s32)((a) - (b))
327 
328 static s32 page_pool_inflight(struct page_pool *pool)
329 {
330 	u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
331 	u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
332 	s32 inflight;
333 
334 	inflight = _distance(hold_cnt, release_cnt);
335 
336 	trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
337 	WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
338 
339 	return inflight;
340 }
341 
342 /* Disconnects a page (from a page_pool).  API users can have a need
343  * to disconnect a page (from a page_pool), to allow it to be used as
344  * a regular page (that will eventually be returned to the normal
345  * page-allocator via put_page).
346  */
347 void page_pool_release_page(struct page_pool *pool, struct page *page)
348 {
349 	dma_addr_t dma;
350 	int count;
351 
352 	if (!(pool->p.flags & PP_FLAG_DMA_MAP))
353 		/* Always account for inflight pages, even if we didn't
354 		 * map them
355 		 */
356 		goto skip_dma_unmap;
357 
358 	dma = page_pool_get_dma_addr(page);
359 
360 	/* When page is unmapped, it cannot be returned to our pool */
361 	dma_unmap_page_attrs(pool->p.dev, dma,
362 			     PAGE_SIZE << pool->p.order, pool->p.dma_dir,
363 			     DMA_ATTR_SKIP_CPU_SYNC);
364 	page_pool_set_dma_addr(page, 0);
365 skip_dma_unmap:
366 	page_pool_clear_pp_info(page);
367 
368 	/* This may be the last page returned, releasing the pool, so
369 	 * it is not safe to reference pool afterwards.
370 	 */
371 	count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
372 	trace_page_pool_state_release(pool, page, count);
373 }
374 EXPORT_SYMBOL(page_pool_release_page);
375 
376 /* Return a page to the page allocator, cleaning up our state */
377 static void page_pool_return_page(struct page_pool *pool, struct page *page)
378 {
379 	page_pool_release_page(pool, page);
380 
381 	put_page(page);
382 	/* An optimization would be to call __free_pages(page, pool->p.order)
383 	 * knowing page is not part of page-cache (thus avoiding a
384 	 * __page_cache_release() call).
385 	 */
386 }
387 
388 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
389 {
390 	int ret;
391 	/* BH protection not needed if current is serving softirq */
392 	if (in_serving_softirq())
393 		ret = ptr_ring_produce(&pool->ring, page);
394 	else
395 		ret = ptr_ring_produce_bh(&pool->ring, page);
396 
397 	return (ret == 0) ? true : false;
398 }
399 
400 /* Only allow direct recycling in special circumstances, into the
401  * alloc side cache.  E.g. during RX-NAPI processing for XDP_DROP use-case.
402  *
403  * Caller must provide appropriate safe context.
404  */
405 static bool page_pool_recycle_in_cache(struct page *page,
406 				       struct page_pool *pool)
407 {
408 	if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE))
409 		return false;
410 
411 	/* Caller MUST have verified/know (page_ref_count(page) == 1) */
412 	pool->alloc.cache[pool->alloc.count++] = page;
413 	return true;
414 }
415 
416 /* If the page refcnt == 1, this will try to recycle the page.
417  * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for
418  * the configured size min(dma_sync_size, pool->max_len).
419  * If the page refcnt != 1, then the page will be returned to memory
420  * subsystem.
421  */
422 static __always_inline struct page *
423 __page_pool_put_page(struct page_pool *pool, struct page *page,
424 		     unsigned int dma_sync_size, bool allow_direct)
425 {
426 	/* This allocator is optimized for the XDP mode that uses
427 	 * one-frame-per-page, but have fallbacks that act like the
428 	 * regular page allocator APIs.
429 	 *
430 	 * refcnt == 1 means page_pool owns page, and can recycle it.
431 	 *
432 	 * page is NOT reusable when allocated when system is under
433 	 * some pressure. (page_is_pfmemalloc)
434 	 */
435 	if (likely(page_ref_count(page) == 1 && !page_is_pfmemalloc(page))) {
436 		/* Read barrier done in page_ref_count / READ_ONCE */
437 
438 		if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
439 			page_pool_dma_sync_for_device(pool, page,
440 						      dma_sync_size);
441 
442 		if (allow_direct && in_serving_softirq() &&
443 		    page_pool_recycle_in_cache(page, pool))
444 			return NULL;
445 
446 		/* Page found as candidate for recycling */
447 		return page;
448 	}
449 	/* Fallback/non-XDP mode: API user have elevated refcnt.
450 	 *
451 	 * Many drivers split up the page into fragments, and some
452 	 * want to keep doing this to save memory and do refcnt based
453 	 * recycling. Support this use case too, to ease drivers
454 	 * switching between XDP/non-XDP.
455 	 *
456 	 * In-case page_pool maintains the DMA mapping, API user must
457 	 * call page_pool_put_page once.  In this elevated refcnt
458 	 * case, the DMA is unmapped/released, as driver is likely
459 	 * doing refcnt based recycle tricks, meaning another process
460 	 * will be invoking put_page.
461 	 */
462 	/* Do not replace this with page_pool_return_page() */
463 	page_pool_release_page(pool, page);
464 	put_page(page);
465 
466 	return NULL;
467 }
468 
469 void page_pool_put_defragged_page(struct page_pool *pool, struct page *page,
470 				  unsigned int dma_sync_size, bool allow_direct)
471 {
472 	page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
473 	if (page && !page_pool_recycle_in_ring(pool, page)) {
474 		/* Cache full, fallback to free pages */
475 		page_pool_return_page(pool, page);
476 	}
477 }
478 EXPORT_SYMBOL(page_pool_put_defragged_page);
479 
480 /* Caller must not use data area after call, as this function overwrites it */
481 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
482 			     int count)
483 {
484 	int i, bulk_len = 0;
485 
486 	for (i = 0; i < count; i++) {
487 		struct page *page = virt_to_head_page(data[i]);
488 
489 		/* It is not the last user for the page frag case */
490 		if (!page_pool_is_last_frag(pool, page))
491 			continue;
492 
493 		page = __page_pool_put_page(pool, page, -1, false);
494 		/* Approved for bulk recycling in ptr_ring cache */
495 		if (page)
496 			data[bulk_len++] = page;
497 	}
498 
499 	if (unlikely(!bulk_len))
500 		return;
501 
502 	/* Bulk producer into ptr_ring page_pool cache */
503 	page_pool_ring_lock(pool);
504 	for (i = 0; i < bulk_len; i++) {
505 		if (__ptr_ring_produce(&pool->ring, data[i]))
506 			break; /* ring full */
507 	}
508 	page_pool_ring_unlock(pool);
509 
510 	/* Hopefully all pages was return into ptr_ring */
511 	if (likely(i == bulk_len))
512 		return;
513 
514 	/* ptr_ring cache full, free remaining pages outside producer lock
515 	 * since put_page() with refcnt == 1 can be an expensive operation
516 	 */
517 	for (; i < bulk_len; i++)
518 		page_pool_return_page(pool, data[i]);
519 }
520 EXPORT_SYMBOL(page_pool_put_page_bulk);
521 
522 static struct page *page_pool_drain_frag(struct page_pool *pool,
523 					 struct page *page)
524 {
525 	long drain_count = BIAS_MAX - pool->frag_users;
526 
527 	/* Some user is still using the page frag */
528 	if (likely(page_pool_defrag_page(page, drain_count)))
529 		return NULL;
530 
531 	if (page_ref_count(page) == 1 && !page_is_pfmemalloc(page)) {
532 		if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
533 			page_pool_dma_sync_for_device(pool, page, -1);
534 
535 		return page;
536 	}
537 
538 	page_pool_return_page(pool, page);
539 	return NULL;
540 }
541 
542 static void page_pool_free_frag(struct page_pool *pool)
543 {
544 	long drain_count = BIAS_MAX - pool->frag_users;
545 	struct page *page = pool->frag_page;
546 
547 	pool->frag_page = NULL;
548 
549 	if (!page || page_pool_defrag_page(page, drain_count))
550 		return;
551 
552 	page_pool_return_page(pool, page);
553 }
554 
555 struct page *page_pool_alloc_frag(struct page_pool *pool,
556 				  unsigned int *offset,
557 				  unsigned int size, gfp_t gfp)
558 {
559 	unsigned int max_size = PAGE_SIZE << pool->p.order;
560 	struct page *page = pool->frag_page;
561 
562 	if (WARN_ON(!(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
563 		    size > max_size))
564 		return NULL;
565 
566 	size = ALIGN(size, dma_get_cache_alignment());
567 	*offset = pool->frag_offset;
568 
569 	if (page && *offset + size > max_size) {
570 		page = page_pool_drain_frag(pool, page);
571 		if (page)
572 			goto frag_reset;
573 	}
574 
575 	if (!page) {
576 		page = page_pool_alloc_pages(pool, gfp);
577 		if (unlikely(!page)) {
578 			pool->frag_page = NULL;
579 			return NULL;
580 		}
581 
582 		pool->frag_page = page;
583 
584 frag_reset:
585 		pool->frag_users = 1;
586 		*offset = 0;
587 		pool->frag_offset = size;
588 		page_pool_fragment_page(page, BIAS_MAX);
589 		return page;
590 	}
591 
592 	pool->frag_users++;
593 	pool->frag_offset = *offset + size;
594 	return page;
595 }
596 EXPORT_SYMBOL(page_pool_alloc_frag);
597 
598 static void page_pool_empty_ring(struct page_pool *pool)
599 {
600 	struct page *page;
601 
602 	/* Empty recycle ring */
603 	while ((page = ptr_ring_consume_bh(&pool->ring))) {
604 		/* Verify the refcnt invariant of cached pages */
605 		if (!(page_ref_count(page) == 1))
606 			pr_crit("%s() page_pool refcnt %d violation\n",
607 				__func__, page_ref_count(page));
608 
609 		page_pool_return_page(pool, page);
610 	}
611 }
612 
613 static void page_pool_free(struct page_pool *pool)
614 {
615 	if (pool->disconnect)
616 		pool->disconnect(pool);
617 
618 	ptr_ring_cleanup(&pool->ring, NULL);
619 
620 	if (pool->p.flags & PP_FLAG_DMA_MAP)
621 		put_device(pool->p.dev);
622 
623 	kfree(pool);
624 }
625 
626 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
627 {
628 	struct page *page;
629 
630 	if (pool->destroy_cnt)
631 		return;
632 
633 	/* Empty alloc cache, assume caller made sure this is
634 	 * no-longer in use, and page_pool_alloc_pages() cannot be
635 	 * call concurrently.
636 	 */
637 	while (pool->alloc.count) {
638 		page = pool->alloc.cache[--pool->alloc.count];
639 		page_pool_return_page(pool, page);
640 	}
641 }
642 
643 static void page_pool_scrub(struct page_pool *pool)
644 {
645 	page_pool_empty_alloc_cache_once(pool);
646 	pool->destroy_cnt++;
647 
648 	/* No more consumers should exist, but producers could still
649 	 * be in-flight.
650 	 */
651 	page_pool_empty_ring(pool);
652 }
653 
654 static int page_pool_release(struct page_pool *pool)
655 {
656 	int inflight;
657 
658 	page_pool_scrub(pool);
659 	inflight = page_pool_inflight(pool);
660 	if (!inflight)
661 		page_pool_free(pool);
662 
663 	return inflight;
664 }
665 
666 static void page_pool_release_retry(struct work_struct *wq)
667 {
668 	struct delayed_work *dwq = to_delayed_work(wq);
669 	struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
670 	int inflight;
671 
672 	inflight = page_pool_release(pool);
673 	if (!inflight)
674 		return;
675 
676 	/* Periodic warning */
677 	if (time_after_eq(jiffies, pool->defer_warn)) {
678 		int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
679 
680 		pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
681 			__func__, inflight, sec);
682 		pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
683 	}
684 
685 	/* Still not ready to be disconnected, retry later */
686 	schedule_delayed_work(&pool->release_dw, DEFER_TIME);
687 }
688 
689 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
690 			   struct xdp_mem_info *mem)
691 {
692 	refcount_inc(&pool->user_cnt);
693 	pool->disconnect = disconnect;
694 	pool->xdp_mem_id = mem->id;
695 }
696 
697 void page_pool_destroy(struct page_pool *pool)
698 {
699 	if (!pool)
700 		return;
701 
702 	if (!page_pool_put(pool))
703 		return;
704 
705 	page_pool_free_frag(pool);
706 
707 	if (!page_pool_release(pool))
708 		return;
709 
710 	pool->defer_start = jiffies;
711 	pool->defer_warn  = jiffies + DEFER_WARN_INTERVAL;
712 
713 	INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
714 	schedule_delayed_work(&pool->release_dw, DEFER_TIME);
715 }
716 EXPORT_SYMBOL(page_pool_destroy);
717 
718 /* Caller must provide appropriate safe context, e.g. NAPI. */
719 void page_pool_update_nid(struct page_pool *pool, int new_nid)
720 {
721 	struct page *page;
722 
723 	trace_page_pool_update_nid(pool, new_nid);
724 	pool->p.nid = new_nid;
725 
726 	/* Flush pool alloc cache, as refill will check NUMA node */
727 	while (pool->alloc.count) {
728 		page = pool->alloc.cache[--pool->alloc.count];
729 		page_pool_return_page(pool, page);
730 	}
731 }
732 EXPORT_SYMBOL(page_pool_update_nid);
733 
734 bool page_pool_return_skb_page(struct page *page)
735 {
736 	struct page_pool *pp;
737 
738 	page = compound_head(page);
739 
740 	/* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
741 	 * in order to preserve any existing bits, such as bit 0 for the
742 	 * head page of compound page and bit 1 for pfmemalloc page, so
743 	 * mask those bits for freeing side when doing below checking,
744 	 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
745 	 * to avoid recycling the pfmemalloc page.
746 	 */
747 	if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
748 		return false;
749 
750 	pp = page->pp;
751 
752 	/* Driver set this to memory recycling info. Reset it on recycle.
753 	 * This will *not* work for NIC using a split-page memory model.
754 	 * The page will be returned to the pool here regardless of the
755 	 * 'flipped' fragment being in use or not.
756 	 */
757 	page_pool_put_full_page(pp, page, false);
758 
759 	return true;
760 }
761 EXPORT_SYMBOL(page_pool_return_skb_page);
762