xref: /openbmc/linux/net/core/page_pool.c (revision 64693ec7)
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 	/* Slower-path: Get pages from locked ring queue */
134 	spin_lock(&r->consumer_lock);
135 
136 	/* Refill alloc array, but only if NUMA match */
137 	do {
138 		page = __ptr_ring_consume(r);
139 		if (unlikely(!page))
140 			break;
141 
142 		if (likely(page_to_nid(page) == pref_nid)) {
143 			pool->alloc.cache[pool->alloc.count++] = page;
144 		} else {
145 			/* NUMA mismatch;
146 			 * (1) release 1 page to page-allocator and
147 			 * (2) break out to fallthrough to alloc_pages_node.
148 			 * This limit stress on page buddy alloactor.
149 			 */
150 			page_pool_return_page(pool, page);
151 			page = NULL;
152 			break;
153 		}
154 	} while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
155 
156 	/* Return last page */
157 	if (likely(pool->alloc.count > 0))
158 		page = pool->alloc.cache[--pool->alloc.count];
159 
160 	spin_unlock(&r->consumer_lock);
161 	return page;
162 }
163 
164 /* fast path */
165 static struct page *__page_pool_get_cached(struct page_pool *pool)
166 {
167 	struct page *page;
168 
169 	/* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
170 	if (likely(pool->alloc.count)) {
171 		/* Fast-path */
172 		page = pool->alloc.cache[--pool->alloc.count];
173 	} else {
174 		page = page_pool_refill_alloc_cache(pool);
175 	}
176 
177 	return page;
178 }
179 
180 static void page_pool_dma_sync_for_device(struct page_pool *pool,
181 					  struct page *page,
182 					  unsigned int dma_sync_size)
183 {
184 	dma_addr_t dma_addr = page_pool_get_dma_addr(page);
185 
186 	dma_sync_size = min(dma_sync_size, pool->p.max_len);
187 	dma_sync_single_range_for_device(pool->p.dev, dma_addr,
188 					 pool->p.offset, dma_sync_size,
189 					 pool->p.dma_dir);
190 }
191 
192 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
193 {
194 	dma_addr_t dma;
195 
196 	/* Setup DMA mapping: use 'struct page' area for storing DMA-addr
197 	 * since dma_addr_t can be either 32 or 64 bits and does not always fit
198 	 * into page private data (i.e 32bit cpu with 64bit DMA caps)
199 	 * This mapping is kept for lifetime of page, until leaving pool.
200 	 */
201 	dma = dma_map_page_attrs(pool->p.dev, page, 0,
202 				 (PAGE_SIZE << pool->p.order),
203 				 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
204 	if (dma_mapping_error(pool->p.dev, dma))
205 		return false;
206 
207 	page_pool_set_dma_addr(page, dma);
208 
209 	if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
210 		page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
211 
212 	return true;
213 }
214 
215 static void page_pool_set_pp_info(struct page_pool *pool,
216 				  struct page *page)
217 {
218 	page->pp = pool;
219 	page->pp_magic |= PP_SIGNATURE;
220 	if (pool->p.init_callback)
221 		pool->p.init_callback(page, pool->p.init_arg);
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 			   struct xdp_mem_info *mem)
698 {
699 	refcount_inc(&pool->user_cnt);
700 	pool->disconnect = disconnect;
701 	pool->xdp_mem_id = mem->id;
702 }
703 
704 void page_pool_destroy(struct page_pool *pool)
705 {
706 	if (!pool)
707 		return;
708 
709 	if (!page_pool_put(pool))
710 		return;
711 
712 	page_pool_free_frag(pool);
713 
714 	if (!page_pool_release(pool))
715 		return;
716 
717 	pool->defer_start = jiffies;
718 	pool->defer_warn  = jiffies + DEFER_WARN_INTERVAL;
719 
720 	INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
721 	schedule_delayed_work(&pool->release_dw, DEFER_TIME);
722 }
723 EXPORT_SYMBOL(page_pool_destroy);
724 
725 /* Caller must provide appropriate safe context, e.g. NAPI. */
726 void page_pool_update_nid(struct page_pool *pool, int new_nid)
727 {
728 	struct page *page;
729 
730 	trace_page_pool_update_nid(pool, new_nid);
731 	pool->p.nid = new_nid;
732 
733 	/* Flush pool alloc cache, as refill will check NUMA node */
734 	while (pool->alloc.count) {
735 		page = pool->alloc.cache[--pool->alloc.count];
736 		page_pool_return_page(pool, page);
737 	}
738 }
739 EXPORT_SYMBOL(page_pool_update_nid);
740 
741 bool page_pool_return_skb_page(struct page *page)
742 {
743 	struct page_pool *pp;
744 
745 	page = compound_head(page);
746 
747 	/* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
748 	 * in order to preserve any existing bits, such as bit 0 for the
749 	 * head page of compound page and bit 1 for pfmemalloc page, so
750 	 * mask those bits for freeing side when doing below checking,
751 	 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
752 	 * to avoid recycling the pfmemalloc page.
753 	 */
754 	if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
755 		return false;
756 
757 	pp = page->pp;
758 
759 	/* Driver set this to memory recycling info. Reset it on recycle.
760 	 * This will *not* work for NIC using a split-page memory model.
761 	 * The page will be returned to the pool here regardless of the
762 	 * 'flipped' fragment being in use or not.
763 	 */
764 	page_pool_put_full_page(pp, page, false);
765 
766 	return true;
767 }
768 EXPORT_SYMBOL(page_pool_return_skb_page);
769