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