1 /* SPDX-License-Identifier: GPL-2.0
4 * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
5 * Copyright (C) 2016 Red Hat, Inc.
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
13 #include <net/page_pool/helpers.h>
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 #include <linux/ethtool.h>
22 #include <linux/netdevice.h>
24 #include <trace/events/page_pool.h>
26 #define DEFER_TIME (msecs_to_jiffies(1000))
27 #define DEFER_WARN_INTERVAL (60 * HZ)
29 #define BIAS_MAX LONG_MAX
31 #ifdef CONFIG_PAGE_POOL_STATS
32 /* alloc_stat_inc is intended to be used in softirq context */
33 #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++)
34 /* recycle_stat_inc is safe to use when preemption is possible. */
35 #define recycle_stat_inc(pool, __stat) \
37 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
38 this_cpu_inc(s->__stat); \
41 #define recycle_stat_add(pool, __stat, val) \
43 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
44 this_cpu_add(s->__stat, val); \
47 static const char pp_stats[][ETH_GSTRING_LEN] = {
50 "rx_pp_alloc_slow_ho",
54 "rx_pp_recycle_cached",
55 "rx_pp_recycle_cache_full",
57 "rx_pp_recycle_ring_full",
58 "rx_pp_recycle_released_ref",
62 * page_pool_get_stats() - fetch page pool stats
63 * @pool: pool from which page was allocated
64 * @stats: struct page_pool_stats to fill in
66 * Retrieve statistics about the page_pool. This API is only available
67 * if the kernel has been configured with ``CONFIG_PAGE_POOL_STATS=y``.
68 * A pointer to a caller allocated struct page_pool_stats structure
69 * is passed to this API which is filled in. The caller can then report
70 * those stats to the user (perhaps via ethtool, debugfs, etc.).
72 bool page_pool_get_stats(struct page_pool *pool,
73 struct page_pool_stats *stats)
80 /* The caller is responsible to initialize stats. */
81 stats->alloc_stats.fast += pool->alloc_stats.fast;
82 stats->alloc_stats.slow += pool->alloc_stats.slow;
83 stats->alloc_stats.slow_high_order += pool->alloc_stats.slow_high_order;
84 stats->alloc_stats.empty += pool->alloc_stats.empty;
85 stats->alloc_stats.refill += pool->alloc_stats.refill;
86 stats->alloc_stats.waive += pool->alloc_stats.waive;
88 for_each_possible_cpu(cpu) {
89 const struct page_pool_recycle_stats *pcpu =
90 per_cpu_ptr(pool->recycle_stats, cpu);
92 stats->recycle_stats.cached += pcpu->cached;
93 stats->recycle_stats.cache_full += pcpu->cache_full;
94 stats->recycle_stats.ring += pcpu->ring;
95 stats->recycle_stats.ring_full += pcpu->ring_full;
96 stats->recycle_stats.released_refcnt += pcpu->released_refcnt;
101 EXPORT_SYMBOL(page_pool_get_stats);
103 u8 *page_pool_ethtool_stats_get_strings(u8 *data)
107 for (i = 0; i < ARRAY_SIZE(pp_stats); i++) {
108 memcpy(data, pp_stats[i], ETH_GSTRING_LEN);
109 data += ETH_GSTRING_LEN;
114 EXPORT_SYMBOL(page_pool_ethtool_stats_get_strings);
116 int page_pool_ethtool_stats_get_count(void)
118 return ARRAY_SIZE(pp_stats);
120 EXPORT_SYMBOL(page_pool_ethtool_stats_get_count);
122 u64 *page_pool_ethtool_stats_get(u64 *data, void *stats)
124 struct page_pool_stats *pool_stats = stats;
126 *data++ = pool_stats->alloc_stats.fast;
127 *data++ = pool_stats->alloc_stats.slow;
128 *data++ = pool_stats->alloc_stats.slow_high_order;
129 *data++ = pool_stats->alloc_stats.empty;
130 *data++ = pool_stats->alloc_stats.refill;
131 *data++ = pool_stats->alloc_stats.waive;
132 *data++ = pool_stats->recycle_stats.cached;
133 *data++ = pool_stats->recycle_stats.cache_full;
134 *data++ = pool_stats->recycle_stats.ring;
135 *data++ = pool_stats->recycle_stats.ring_full;
136 *data++ = pool_stats->recycle_stats.released_refcnt;
140 EXPORT_SYMBOL(page_pool_ethtool_stats_get);
143 #define alloc_stat_inc(pool, __stat)
144 #define recycle_stat_inc(pool, __stat)
145 #define recycle_stat_add(pool, __stat, val)
148 static bool page_pool_producer_lock(struct page_pool *pool)
149 __acquires(&pool->ring.producer_lock)
151 bool in_softirq = in_softirq();
154 spin_lock(&pool->ring.producer_lock);
156 spin_lock_bh(&pool->ring.producer_lock);
161 static void page_pool_producer_unlock(struct page_pool *pool,
163 __releases(&pool->ring.producer_lock)
166 spin_unlock(&pool->ring.producer_lock);
168 spin_unlock_bh(&pool->ring.producer_lock);
171 static int page_pool_init(struct page_pool *pool,
172 const struct page_pool_params *params)
174 unsigned int ring_qsize = 1024; /* Default */
176 memcpy(&pool->p, params, sizeof(pool->p));
178 /* Validate only known flags were used */
179 if (pool->p.flags & ~(PP_FLAG_ALL))
182 if (pool->p.pool_size)
183 ring_qsize = pool->p.pool_size;
185 /* Sanity limit mem that can be pinned down */
186 if (ring_qsize > 32768)
189 /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
190 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
191 * which is the XDP_TX use-case.
193 if (pool->p.flags & PP_FLAG_DMA_MAP) {
194 if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
195 (pool->p.dma_dir != DMA_BIDIRECTIONAL))
199 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV) {
200 /* In order to request DMA-sync-for-device the page
203 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
206 if (!pool->p.max_len)
209 /* pool->p.offset has to be set according to the address
210 * offset used by the DMA engine to start copying rx data
214 if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT &&
215 pool->p.flags & PP_FLAG_PAGE_FRAG)
218 #ifdef CONFIG_PAGE_POOL_STATS
219 pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats);
220 if (!pool->recycle_stats)
224 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
227 atomic_set(&pool->pages_state_release_cnt, 0);
229 /* Driver calling page_pool_create() also call page_pool_destroy() */
230 refcount_set(&pool->user_cnt, 1);
232 if (pool->p.flags & PP_FLAG_DMA_MAP)
233 get_device(pool->p.dev);
239 * page_pool_create() - create a page pool.
240 * @params: parameters, see struct page_pool_params
242 struct page_pool *page_pool_create(const struct page_pool_params *params)
244 struct page_pool *pool;
247 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
249 return ERR_PTR(-ENOMEM);
251 err = page_pool_init(pool, params);
253 pr_warn("%s() gave up with errno %d\n", __func__, err);
260 EXPORT_SYMBOL(page_pool_create);
262 static void page_pool_return_page(struct page_pool *pool, struct page *page);
265 static struct page *page_pool_refill_alloc_cache(struct page_pool *pool)
267 struct ptr_ring *r = &pool->ring;
269 int pref_nid; /* preferred NUMA node */
271 /* Quicker fallback, avoid locks when ring is empty */
272 if (__ptr_ring_empty(r)) {
273 alloc_stat_inc(pool, empty);
277 /* Softirq guarantee CPU and thus NUMA node is stable. This,
278 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
281 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
283 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
284 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
287 /* Refill alloc array, but only if NUMA match */
289 page = __ptr_ring_consume(r);
293 if (likely(page_to_nid(page) == pref_nid)) {
294 pool->alloc.cache[pool->alloc.count++] = page;
297 * (1) release 1 page to page-allocator and
298 * (2) break out to fallthrough to alloc_pages_node.
299 * This limit stress on page buddy alloactor.
301 page_pool_return_page(pool, page);
302 alloc_stat_inc(pool, waive);
306 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
308 /* Return last page */
309 if (likely(pool->alloc.count > 0)) {
310 page = pool->alloc.cache[--pool->alloc.count];
311 alloc_stat_inc(pool, refill);
318 static struct page *__page_pool_get_cached(struct page_pool *pool)
322 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
323 if (likely(pool->alloc.count)) {
325 page = pool->alloc.cache[--pool->alloc.count];
326 alloc_stat_inc(pool, fast);
328 page = page_pool_refill_alloc_cache(pool);
334 static void page_pool_dma_sync_for_device(struct page_pool *pool,
336 unsigned int dma_sync_size)
338 dma_addr_t dma_addr = page_pool_get_dma_addr(page);
340 dma_sync_size = min(dma_sync_size, pool->p.max_len);
341 dma_sync_single_range_for_device(pool->p.dev, dma_addr,
342 pool->p.offset, dma_sync_size,
346 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
350 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr
351 * since dma_addr_t can be either 32 or 64 bits and does not always fit
352 * into page private data (i.e 32bit cpu with 64bit DMA caps)
353 * This mapping is kept for lifetime of page, until leaving pool.
355 dma = dma_map_page_attrs(pool->p.dev, page, 0,
356 (PAGE_SIZE << pool->p.order),
357 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC |
358 DMA_ATTR_WEAK_ORDERING);
359 if (dma_mapping_error(pool->p.dev, dma))
362 page_pool_set_dma_addr(page, dma);
364 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
365 page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
370 static void page_pool_set_pp_info(struct page_pool *pool,
374 page->pp_magic |= PP_SIGNATURE;
375 if (pool->p.init_callback)
376 pool->p.init_callback(page, pool->p.init_arg);
379 static void page_pool_clear_pp_info(struct page *page)
385 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
391 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
395 if ((pool->p.flags & PP_FLAG_DMA_MAP) &&
396 unlikely(!page_pool_dma_map(pool, page))) {
401 alloc_stat_inc(pool, slow_high_order);
402 page_pool_set_pp_info(pool, page);
404 /* Track how many pages are held 'in-flight' */
405 pool->pages_state_hold_cnt++;
406 trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
412 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
415 const int bulk = PP_ALLOC_CACHE_REFILL;
416 unsigned int pp_flags = pool->p.flags;
417 unsigned int pp_order = pool->p.order;
421 /* Don't support bulk alloc for high-order pages */
422 if (unlikely(pp_order))
423 return __page_pool_alloc_page_order(pool, gfp);
425 /* Unnecessary as alloc cache is empty, but guarantees zero count */
426 if (unlikely(pool->alloc.count > 0))
427 return pool->alloc.cache[--pool->alloc.count];
429 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
430 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
432 nr_pages = alloc_pages_bulk_array_node(gfp, pool->p.nid, bulk,
434 if (unlikely(!nr_pages))
437 /* Pages have been filled into alloc.cache array, but count is zero and
438 * page element have not been (possibly) DMA mapped.
440 for (i = 0; i < nr_pages; i++) {
441 page = pool->alloc.cache[i];
442 if ((pp_flags & PP_FLAG_DMA_MAP) &&
443 unlikely(!page_pool_dma_map(pool, page))) {
448 page_pool_set_pp_info(pool, page);
449 pool->alloc.cache[pool->alloc.count++] = page;
450 /* Track how many pages are held 'in-flight' */
451 pool->pages_state_hold_cnt++;
452 trace_page_pool_state_hold(pool, page,
453 pool->pages_state_hold_cnt);
456 /* Return last page */
457 if (likely(pool->alloc.count > 0)) {
458 page = pool->alloc.cache[--pool->alloc.count];
459 alloc_stat_inc(pool, slow);
464 /* When page just alloc'ed is should/must have refcnt 1. */
468 /* For using page_pool replace: alloc_pages() API calls, but provide
469 * synchronization guarantee for allocation side.
471 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
475 /* Fast-path: Get a page from cache */
476 page = __page_pool_get_cached(pool);
480 /* Slow-path: cache empty, do real allocation */
481 page = __page_pool_alloc_pages_slow(pool, gfp);
484 EXPORT_SYMBOL(page_pool_alloc_pages);
486 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
487 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
489 #define _distance(a, b) (s32)((a) - (b))
491 static s32 page_pool_inflight(struct page_pool *pool)
493 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
494 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
497 inflight = _distance(hold_cnt, release_cnt);
499 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
500 WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
505 /* Disconnects a page (from a page_pool). API users can have a need
506 * to disconnect a page (from a page_pool), to allow it to be used as
507 * a regular page (that will eventually be returned to the normal
508 * page-allocator via put_page).
510 static void page_pool_return_page(struct page_pool *pool, struct page *page)
515 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
516 /* Always account for inflight pages, even if we didn't
521 dma = page_pool_get_dma_addr(page);
523 /* When page is unmapped, it cannot be returned to our pool */
524 dma_unmap_page_attrs(pool->p.dev, dma,
525 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
526 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
527 page_pool_set_dma_addr(page, 0);
529 page_pool_clear_pp_info(page);
531 /* This may be the last page returned, releasing the pool, so
532 * it is not safe to reference pool afterwards.
534 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
535 trace_page_pool_state_release(pool, page, count);
538 /* An optimization would be to call __free_pages(page, pool->p.order)
539 * knowing page is not part of page-cache (thus avoiding a
540 * __page_cache_release() call).
544 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
547 /* BH protection not needed if current is softirq */
549 ret = ptr_ring_produce(&pool->ring, page);
551 ret = ptr_ring_produce_bh(&pool->ring, page);
554 recycle_stat_inc(pool, ring);
561 /* Only allow direct recycling in special circumstances, into the
562 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
564 * Caller must provide appropriate safe context.
566 static bool page_pool_recycle_in_cache(struct page *page,
567 struct page_pool *pool)
569 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) {
570 recycle_stat_inc(pool, cache_full);
574 /* Caller MUST have verified/know (page_ref_count(page) == 1) */
575 pool->alloc.cache[pool->alloc.count++] = page;
576 recycle_stat_inc(pool, cached);
580 /* If the page refcnt == 1, this will try to recycle the page.
581 * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for
582 * the configured size min(dma_sync_size, pool->max_len).
583 * If the page refcnt != 1, then the page will be returned to memory
586 static __always_inline struct page *
587 __page_pool_put_page(struct page_pool *pool, struct page *page,
588 unsigned int dma_sync_size, bool allow_direct)
590 lockdep_assert_no_hardirq();
592 /* This allocator is optimized for the XDP mode that uses
593 * one-frame-per-page, but have fallbacks that act like the
594 * regular page allocator APIs.
596 * refcnt == 1 means page_pool owns page, and can recycle it.
598 * page is NOT reusable when allocated when system is under
599 * some pressure. (page_is_pfmemalloc)
601 if (likely(page_ref_count(page) == 1 && !page_is_pfmemalloc(page))) {
602 /* Read barrier done in page_ref_count / READ_ONCE */
604 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
605 page_pool_dma_sync_for_device(pool, page,
608 if (allow_direct && in_softirq() &&
609 page_pool_recycle_in_cache(page, pool))
612 /* Page found as candidate for recycling */
615 /* Fallback/non-XDP mode: API user have elevated refcnt.
617 * Many drivers split up the page into fragments, and some
618 * want to keep doing this to save memory and do refcnt based
619 * recycling. Support this use case too, to ease drivers
620 * switching between XDP/non-XDP.
622 * In-case page_pool maintains the DMA mapping, API user must
623 * call page_pool_put_page once. In this elevated refcnt
624 * case, the DMA is unmapped/released, as driver is likely
625 * doing refcnt based recycle tricks, meaning another process
626 * will be invoking put_page.
628 recycle_stat_inc(pool, released_refcnt);
629 page_pool_return_page(pool, page);
634 void page_pool_put_defragged_page(struct page_pool *pool, struct page *page,
635 unsigned int dma_sync_size, bool allow_direct)
637 page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
638 if (page && !page_pool_recycle_in_ring(pool, page)) {
639 /* Cache full, fallback to free pages */
640 recycle_stat_inc(pool, ring_full);
641 page_pool_return_page(pool, page);
644 EXPORT_SYMBOL(page_pool_put_defragged_page);
647 * page_pool_put_page_bulk() - release references on multiple pages
648 * @pool: pool from which pages were allocated
649 * @data: array holding page pointers
650 * @count: number of pages in @data
652 * Tries to refill a number of pages into the ptr_ring cache holding ptr_ring
653 * producer lock. If the ptr_ring is full, page_pool_put_page_bulk()
654 * will release leftover pages to the page allocator.
655 * page_pool_put_page_bulk() is suitable to be run inside the driver NAPI tx
656 * completion loop for the XDP_REDIRECT use case.
658 * Please note the caller must not use data area after running
659 * page_pool_put_page_bulk(), as this function overwrites it.
661 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
667 for (i = 0; i < count; i++) {
668 struct page *page = virt_to_head_page(data[i]);
670 /* It is not the last user for the page frag case */
671 if (!page_pool_is_last_frag(pool, page))
674 page = __page_pool_put_page(pool, page, -1, false);
675 /* Approved for bulk recycling in ptr_ring cache */
677 data[bulk_len++] = page;
680 if (unlikely(!bulk_len))
683 /* Bulk producer into ptr_ring page_pool cache */
684 in_softirq = page_pool_producer_lock(pool);
685 for (i = 0; i < bulk_len; i++) {
686 if (__ptr_ring_produce(&pool->ring, data[i])) {
688 recycle_stat_inc(pool, ring_full);
692 recycle_stat_add(pool, ring, i);
693 page_pool_producer_unlock(pool, in_softirq);
695 /* Hopefully all pages was return into ptr_ring */
696 if (likely(i == bulk_len))
699 /* ptr_ring cache full, free remaining pages outside producer lock
700 * since put_page() with refcnt == 1 can be an expensive operation
702 for (; i < bulk_len; i++)
703 page_pool_return_page(pool, data[i]);
705 EXPORT_SYMBOL(page_pool_put_page_bulk);
707 static struct page *page_pool_drain_frag(struct page_pool *pool,
710 long drain_count = BIAS_MAX - pool->frag_users;
712 /* Some user is still using the page frag */
713 if (likely(page_pool_defrag_page(page, drain_count)))
716 if (page_ref_count(page) == 1 && !page_is_pfmemalloc(page)) {
717 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
718 page_pool_dma_sync_for_device(pool, page, -1);
723 page_pool_return_page(pool, page);
727 static void page_pool_free_frag(struct page_pool *pool)
729 long drain_count = BIAS_MAX - pool->frag_users;
730 struct page *page = pool->frag_page;
732 pool->frag_page = NULL;
734 if (!page || page_pool_defrag_page(page, drain_count))
737 page_pool_return_page(pool, page);
740 struct page *page_pool_alloc_frag(struct page_pool *pool,
741 unsigned int *offset,
742 unsigned int size, gfp_t gfp)
744 unsigned int max_size = PAGE_SIZE << pool->p.order;
745 struct page *page = pool->frag_page;
747 if (WARN_ON(!(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
751 size = ALIGN(size, dma_get_cache_alignment());
752 *offset = pool->frag_offset;
754 if (page && *offset + size > max_size) {
755 page = page_pool_drain_frag(pool, page);
757 alloc_stat_inc(pool, fast);
763 page = page_pool_alloc_pages(pool, gfp);
764 if (unlikely(!page)) {
765 pool->frag_page = NULL;
769 pool->frag_page = page;
772 pool->frag_users = 1;
774 pool->frag_offset = size;
775 page_pool_fragment_page(page, BIAS_MAX);
780 pool->frag_offset = *offset + size;
781 alloc_stat_inc(pool, fast);
784 EXPORT_SYMBOL(page_pool_alloc_frag);
786 static void page_pool_empty_ring(struct page_pool *pool)
790 /* Empty recycle ring */
791 while ((page = ptr_ring_consume_bh(&pool->ring))) {
792 /* Verify the refcnt invariant of cached pages */
793 if (!(page_ref_count(page) == 1))
794 pr_crit("%s() page_pool refcnt %d violation\n",
795 __func__, page_ref_count(page));
797 page_pool_return_page(pool, page);
801 static void page_pool_free(struct page_pool *pool)
803 if (pool->disconnect)
804 pool->disconnect(pool);
806 ptr_ring_cleanup(&pool->ring, NULL);
808 if (pool->p.flags & PP_FLAG_DMA_MAP)
809 put_device(pool->p.dev);
811 #ifdef CONFIG_PAGE_POOL_STATS
812 free_percpu(pool->recycle_stats);
817 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
821 if (pool->destroy_cnt)
824 /* Empty alloc cache, assume caller made sure this is
825 * no-longer in use, and page_pool_alloc_pages() cannot be
828 while (pool->alloc.count) {
829 page = pool->alloc.cache[--pool->alloc.count];
830 page_pool_return_page(pool, page);
834 static void page_pool_scrub(struct page_pool *pool)
836 page_pool_empty_alloc_cache_once(pool);
839 /* No more consumers should exist, but producers could still
842 page_pool_empty_ring(pool);
845 static int page_pool_release(struct page_pool *pool)
849 page_pool_scrub(pool);
850 inflight = page_pool_inflight(pool);
852 page_pool_free(pool);
857 static void page_pool_release_retry(struct work_struct *wq)
859 struct delayed_work *dwq = to_delayed_work(wq);
860 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
863 inflight = page_pool_release(pool);
867 /* Periodic warning */
868 if (time_after_eq(jiffies, pool->defer_warn)) {
869 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
871 pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
872 __func__, inflight, sec);
873 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
876 /* Still not ready to be disconnected, retry later */
877 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
880 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
881 struct xdp_mem_info *mem)
883 refcount_inc(&pool->user_cnt);
884 pool->disconnect = disconnect;
885 pool->xdp_mem_id = mem->id;
888 void page_pool_unlink_napi(struct page_pool *pool)
893 /* To avoid races with recycling and additional barriers make sure
894 * pool and NAPI are unlinked when NAPI is disabled.
896 WARN_ON(!test_bit(NAPI_STATE_SCHED, &pool->p.napi->state) ||
897 READ_ONCE(pool->p.napi->list_owner) != -1);
899 WRITE_ONCE(pool->p.napi, NULL);
901 EXPORT_SYMBOL(page_pool_unlink_napi);
903 void page_pool_destroy(struct page_pool *pool)
908 if (!page_pool_put(pool))
911 page_pool_unlink_napi(pool);
912 page_pool_free_frag(pool);
914 if (!page_pool_release(pool))
917 pool->defer_start = jiffies;
918 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
920 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
921 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
923 EXPORT_SYMBOL(page_pool_destroy);
925 /* Caller must provide appropriate safe context, e.g. NAPI. */
926 void page_pool_update_nid(struct page_pool *pool, int new_nid)
930 trace_page_pool_update_nid(pool, new_nid);
931 pool->p.nid = new_nid;
933 /* Flush pool alloc cache, as refill will check NUMA node */
934 while (pool->alloc.count) {
935 page = pool->alloc.cache[--pool->alloc.count];
936 page_pool_return_page(pool, page);
939 EXPORT_SYMBOL(page_pool_update_nid);