1 /* SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2016 Red Hat, Inc.
8 #include <linux/error-injection.h>
9 #include <linux/types.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
14 #include <net/page_pool/helpers.h>
17 #include <linux/dma-direction.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/page-flags.h>
20 #include <linux/mm.h> /* for put_page() */
21 #include <linux/poison.h>
22 #include <linux/ethtool.h>
23 #include <linux/netdevice.h>
25 #include <trace/events/page_pool.h>
27 #include "page_pool_priv.h"
29 #define DEFER_TIME (msecs_to_jiffies(1000))
30 #define DEFER_WARN_INTERVAL (60 * HZ)
32 #define BIAS_MAX (LONG_MAX >> 1)
34 #ifdef CONFIG_PAGE_POOL_STATS
35 static DEFINE_PER_CPU(struct page_pool_recycle_stats, pp_system_recycle_stats);
37 /* alloc_stat_inc is intended to be used in softirq context */
38 #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++)
39 /* recycle_stat_inc is safe to use when preemption is possible. */
40 #define recycle_stat_inc(pool, __stat) \
42 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
43 this_cpu_inc(s->__stat); \
46 #define recycle_stat_add(pool, __stat, val) \
48 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
49 this_cpu_add(s->__stat, val); \
52 static const char pp_stats[][ETH_GSTRING_LEN] = {
55 "rx_pp_alloc_slow_ho",
59 "rx_pp_recycle_cached",
60 "rx_pp_recycle_cache_full",
62 "rx_pp_recycle_ring_full",
63 "rx_pp_recycle_released_ref",
67 * page_pool_get_stats() - fetch page pool stats
68 * @pool: pool from which page was allocated
69 * @stats: struct page_pool_stats to fill in
71 * Retrieve statistics about the page_pool. This API is only available
72 * if the kernel has been configured with ``CONFIG_PAGE_POOL_STATS=y``.
73 * A pointer to a caller allocated struct page_pool_stats structure
74 * is passed to this API which is filled in. The caller can then report
75 * those stats to the user (perhaps via ethtool, debugfs, etc.).
77 bool page_pool_get_stats(const struct page_pool *pool,
78 struct page_pool_stats *stats)
85 /* The caller is responsible to initialize stats. */
86 stats->alloc_stats.fast += pool->alloc_stats.fast;
87 stats->alloc_stats.slow += pool->alloc_stats.slow;
88 stats->alloc_stats.slow_high_order += pool->alloc_stats.slow_high_order;
89 stats->alloc_stats.empty += pool->alloc_stats.empty;
90 stats->alloc_stats.refill += pool->alloc_stats.refill;
91 stats->alloc_stats.waive += pool->alloc_stats.waive;
93 for_each_possible_cpu(cpu) {
94 const struct page_pool_recycle_stats *pcpu =
95 per_cpu_ptr(pool->recycle_stats, cpu);
97 stats->recycle_stats.cached += pcpu->cached;
98 stats->recycle_stats.cache_full += pcpu->cache_full;
99 stats->recycle_stats.ring += pcpu->ring;
100 stats->recycle_stats.ring_full += pcpu->ring_full;
101 stats->recycle_stats.released_refcnt += pcpu->released_refcnt;
106 EXPORT_SYMBOL(page_pool_get_stats);
108 u8 *page_pool_ethtool_stats_get_strings(u8 *data)
112 for (i = 0; i < ARRAY_SIZE(pp_stats); i++) {
113 memcpy(data, pp_stats[i], ETH_GSTRING_LEN);
114 data += ETH_GSTRING_LEN;
119 EXPORT_SYMBOL(page_pool_ethtool_stats_get_strings);
121 int page_pool_ethtool_stats_get_count(void)
123 return ARRAY_SIZE(pp_stats);
125 EXPORT_SYMBOL(page_pool_ethtool_stats_get_count);
127 u64 *page_pool_ethtool_stats_get(u64 *data, const void *stats)
129 const struct page_pool_stats *pool_stats = stats;
131 *data++ = pool_stats->alloc_stats.fast;
132 *data++ = pool_stats->alloc_stats.slow;
133 *data++ = pool_stats->alloc_stats.slow_high_order;
134 *data++ = pool_stats->alloc_stats.empty;
135 *data++ = pool_stats->alloc_stats.refill;
136 *data++ = pool_stats->alloc_stats.waive;
137 *data++ = pool_stats->recycle_stats.cached;
138 *data++ = pool_stats->recycle_stats.cache_full;
139 *data++ = pool_stats->recycle_stats.ring;
140 *data++ = pool_stats->recycle_stats.ring_full;
141 *data++ = pool_stats->recycle_stats.released_refcnt;
145 EXPORT_SYMBOL(page_pool_ethtool_stats_get);
148 #define alloc_stat_inc(pool, __stat)
149 #define recycle_stat_inc(pool, __stat)
150 #define recycle_stat_add(pool, __stat, val)
153 static bool page_pool_producer_lock(struct page_pool *pool)
154 __acquires(&pool->ring.producer_lock)
156 bool in_softirq = in_softirq();
159 spin_lock(&pool->ring.producer_lock);
161 spin_lock_bh(&pool->ring.producer_lock);
166 static void page_pool_producer_unlock(struct page_pool *pool,
168 __releases(&pool->ring.producer_lock)
171 spin_unlock(&pool->ring.producer_lock);
173 spin_unlock_bh(&pool->ring.producer_lock);
176 static void page_pool_struct_check(void)
178 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_users);
179 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_page);
180 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_offset);
181 CACHELINE_ASSERT_GROUP_SIZE(struct page_pool, frag, 4 * sizeof(long));
184 static int page_pool_init(struct page_pool *pool,
185 const struct page_pool_params *params,
188 unsigned int ring_qsize = 1024; /* Default */
190 page_pool_struct_check();
192 memcpy(&pool->p, ¶ms->fast, sizeof(pool->p));
193 memcpy(&pool->slow, ¶ms->slow, sizeof(pool->slow));
197 /* Validate only known flags were used */
198 if (pool->slow.flags & ~PP_FLAG_ALL)
201 if (pool->p.pool_size)
202 ring_qsize = pool->p.pool_size;
204 /* Sanity limit mem that can be pinned down */
205 if (ring_qsize > 32768)
208 /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
209 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
210 * which is the XDP_TX use-case.
212 if (pool->slow.flags & PP_FLAG_DMA_MAP) {
213 if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
214 (pool->p.dma_dir != DMA_BIDIRECTIONAL))
217 pool->dma_map = true;
220 if (pool->slow.flags & PP_FLAG_DMA_SYNC_DEV) {
221 /* In order to request DMA-sync-for-device the page
224 if (!(pool->slow.flags & PP_FLAG_DMA_MAP))
227 if (!pool->p.max_len)
230 pool->dma_sync = true;
232 /* pool->p.offset has to be set according to the address
233 * offset used by the DMA engine to start copying rx data
237 pool->has_init_callback = !!pool->slow.init_callback;
239 #ifdef CONFIG_PAGE_POOL_STATS
240 if (!(pool->slow.flags & PP_FLAG_SYSTEM_POOL)) {
241 pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats);
242 if (!pool->recycle_stats)
245 /* For system page pool instance we use a singular stats object
246 * instead of allocating a separate percpu variable for each
247 * (also percpu) page pool instance.
249 pool->recycle_stats = &pp_system_recycle_stats;
254 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0) {
255 #ifdef CONFIG_PAGE_POOL_STATS
257 free_percpu(pool->recycle_stats);
262 atomic_set(&pool->pages_state_release_cnt, 0);
264 /* Driver calling page_pool_create() also call page_pool_destroy() */
265 refcount_set(&pool->user_cnt, 1);
268 get_device(pool->p.dev);
273 static void page_pool_uninit(struct page_pool *pool)
275 ptr_ring_cleanup(&pool->ring, NULL);
278 put_device(pool->p.dev);
280 #ifdef CONFIG_PAGE_POOL_STATS
282 free_percpu(pool->recycle_stats);
287 * page_pool_create_percpu() - create a page pool for a given cpu.
288 * @params: parameters, see struct page_pool_params
289 * @cpuid: cpu identifier
292 page_pool_create_percpu(const struct page_pool_params *params, int cpuid)
294 struct page_pool *pool;
297 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
299 return ERR_PTR(-ENOMEM);
301 err = page_pool_init(pool, params, cpuid);
305 err = page_pool_list(pool);
312 page_pool_uninit(pool);
314 pr_warn("%s() gave up with errno %d\n", __func__, err);
318 EXPORT_SYMBOL(page_pool_create_percpu);
321 * page_pool_create() - create a page pool
322 * @params: parameters, see struct page_pool_params
324 struct page_pool *page_pool_create(const struct page_pool_params *params)
326 return page_pool_create_percpu(params, -1);
328 EXPORT_SYMBOL(page_pool_create);
330 static void page_pool_return_page(struct page_pool *pool, struct page *page);
333 static struct page *page_pool_refill_alloc_cache(struct page_pool *pool)
335 struct ptr_ring *r = &pool->ring;
337 int pref_nid; /* preferred NUMA node */
339 /* Quicker fallback, avoid locks when ring is empty */
340 if (__ptr_ring_empty(r)) {
341 alloc_stat_inc(pool, empty);
345 /* Softirq guarantee CPU and thus NUMA node is stable. This,
346 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
349 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
351 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
352 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
355 /* Refill alloc array, but only if NUMA match */
357 page = __ptr_ring_consume(r);
361 if (likely(page_to_nid(page) == pref_nid)) {
362 pool->alloc.cache[pool->alloc.count++] = page;
365 * (1) release 1 page to page-allocator and
366 * (2) break out to fallthrough to alloc_pages_node.
367 * This limit stress on page buddy alloactor.
369 page_pool_return_page(pool, page);
370 alloc_stat_inc(pool, waive);
374 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
376 /* Return last page */
377 if (likely(pool->alloc.count > 0)) {
378 page = pool->alloc.cache[--pool->alloc.count];
379 alloc_stat_inc(pool, refill);
386 static struct page *__page_pool_get_cached(struct page_pool *pool)
390 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
391 if (likely(pool->alloc.count)) {
393 page = pool->alloc.cache[--pool->alloc.count];
394 alloc_stat_inc(pool, fast);
396 page = page_pool_refill_alloc_cache(pool);
402 static void __page_pool_dma_sync_for_device(const struct page_pool *pool,
403 const struct page *page,
406 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC)
407 dma_addr_t dma_addr = page_pool_get_dma_addr(page);
409 dma_sync_size = min(dma_sync_size, pool->p.max_len);
410 __dma_sync_single_for_device(pool->p.dev, dma_addr + pool->p.offset,
411 dma_sync_size, pool->p.dma_dir);
415 static __always_inline void
416 page_pool_dma_sync_for_device(const struct page_pool *pool,
417 const struct page *page,
420 if (pool->dma_sync && dma_dev_need_sync(pool->p.dev))
421 __page_pool_dma_sync_for_device(pool, page, dma_sync_size);
424 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
428 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr
429 * since dma_addr_t can be either 32 or 64 bits and does not always fit
430 * into page private data (i.e 32bit cpu with 64bit DMA caps)
431 * This mapping is kept for lifetime of page, until leaving pool.
433 dma = dma_map_page_attrs(pool->p.dev, page, 0,
434 (PAGE_SIZE << pool->p.order),
435 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC |
436 DMA_ATTR_WEAK_ORDERING);
437 if (dma_mapping_error(pool->p.dev, dma))
440 if (page_pool_set_dma_addr(page, dma))
443 page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
448 WARN_ON_ONCE("unexpected DMA address, please report to netdev@");
449 dma_unmap_page_attrs(pool->p.dev, dma,
450 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
451 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
455 static void page_pool_set_pp_info(struct page_pool *pool,
459 page->pp_magic |= PP_SIGNATURE;
461 /* Ensuring all pages have been split into one fragment initially:
462 * page_pool_set_pp_info() is only called once for every page when it
463 * is allocated from the page allocator and page_pool_fragment_page()
464 * is dirtying the same cache line as the page->pp_magic above, so
465 * the overhead is negligible.
467 page_pool_fragment_page(page, 1);
468 if (pool->has_init_callback)
469 pool->slow.init_callback(page, pool->slow.init_arg);
472 static void page_pool_clear_pp_info(struct page *page)
478 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
484 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
488 if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page))) {
493 alloc_stat_inc(pool, slow_high_order);
494 page_pool_set_pp_info(pool, page);
496 /* Track how many pages are held 'in-flight' */
497 pool->pages_state_hold_cnt++;
498 trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
504 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
507 const int bulk = PP_ALLOC_CACHE_REFILL;
508 unsigned int pp_order = pool->p.order;
509 bool dma_map = pool->dma_map;
513 /* Don't support bulk alloc for high-order pages */
514 if (unlikely(pp_order))
515 return __page_pool_alloc_page_order(pool, gfp);
517 /* Unnecessary as alloc cache is empty, but guarantees zero count */
518 if (unlikely(pool->alloc.count > 0))
519 return pool->alloc.cache[--pool->alloc.count];
521 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
522 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
524 nr_pages = alloc_pages_bulk_array_node(gfp, pool->p.nid, bulk,
526 if (unlikely(!nr_pages))
529 /* Pages have been filled into alloc.cache array, but count is zero and
530 * page element have not been (possibly) DMA mapped.
532 for (i = 0; i < nr_pages; i++) {
533 page = pool->alloc.cache[i];
534 if (dma_map && unlikely(!page_pool_dma_map(pool, page))) {
539 page_pool_set_pp_info(pool, page);
540 pool->alloc.cache[pool->alloc.count++] = page;
541 /* Track how many pages are held 'in-flight' */
542 pool->pages_state_hold_cnt++;
543 trace_page_pool_state_hold(pool, page,
544 pool->pages_state_hold_cnt);
547 /* Return last page */
548 if (likely(pool->alloc.count > 0)) {
549 page = pool->alloc.cache[--pool->alloc.count];
550 alloc_stat_inc(pool, slow);
555 /* When page just alloc'ed is should/must have refcnt 1. */
559 /* For using page_pool replace: alloc_pages() API calls, but provide
560 * synchronization guarantee for allocation side.
562 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
566 /* Fast-path: Get a page from cache */
567 page = __page_pool_get_cached(pool);
571 /* Slow-path: cache empty, do real allocation */
572 page = __page_pool_alloc_pages_slow(pool, gfp);
575 EXPORT_SYMBOL(page_pool_alloc_pages);
576 ALLOW_ERROR_INJECTION(page_pool_alloc_pages, NULL);
578 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
579 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
581 #define _distance(a, b) (s32)((a) - (b))
583 s32 page_pool_inflight(const struct page_pool *pool, bool strict)
585 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
586 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
589 inflight = _distance(hold_cnt, release_cnt);
592 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
593 WARN(inflight < 0, "Negative(%d) inflight packet-pages",
596 inflight = max(0, inflight);
602 static __always_inline
603 void __page_pool_release_page_dma(struct page_pool *pool, struct page *page)
608 /* Always account for inflight pages, even if we didn't
613 dma = page_pool_get_dma_addr(page);
615 /* When page is unmapped, it cannot be returned to our pool */
616 dma_unmap_page_attrs(pool->p.dev, dma,
617 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
618 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING);
619 page_pool_set_dma_addr(page, 0);
622 /* Disconnects a page (from a page_pool). API users can have a need
623 * to disconnect a page (from a page_pool), to allow it to be used as
624 * a regular page (that will eventually be returned to the normal
625 * page-allocator via put_page).
627 void page_pool_return_page(struct page_pool *pool, struct page *page)
631 __page_pool_release_page_dma(pool, page);
633 page_pool_clear_pp_info(page);
635 /* This may be the last page returned, releasing the pool, so
636 * it is not safe to reference pool afterwards.
638 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
639 trace_page_pool_state_release(pool, page, count);
642 /* An optimization would be to call __free_pages(page, pool->p.order)
643 * knowing page is not part of page-cache (thus avoiding a
644 * __page_cache_release() call).
648 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
651 /* BH protection not needed if current is softirq */
653 ret = ptr_ring_produce(&pool->ring, page);
655 ret = ptr_ring_produce_bh(&pool->ring, page);
658 recycle_stat_inc(pool, ring);
665 /* Only allow direct recycling in special circumstances, into the
666 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
668 * Caller must provide appropriate safe context.
670 static bool page_pool_recycle_in_cache(struct page *page,
671 struct page_pool *pool)
673 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) {
674 recycle_stat_inc(pool, cache_full);
678 /* Caller MUST have verified/know (page_ref_count(page) == 1) */
679 pool->alloc.cache[pool->alloc.count++] = page;
680 recycle_stat_inc(pool, cached);
684 static bool __page_pool_page_can_be_recycled(const struct page *page)
686 return page_ref_count(page) == 1 && !page_is_pfmemalloc(page);
689 /* If the page refcnt == 1, this will try to recycle the page.
690 * If pool->dma_sync is set, we'll try to sync the DMA area for
691 * the configured size min(dma_sync_size, pool->max_len).
692 * If the page refcnt != 1, then the page will be returned to memory
695 static __always_inline struct page *
696 __page_pool_put_page(struct page_pool *pool, struct page *page,
697 unsigned int dma_sync_size, bool allow_direct)
699 lockdep_assert_no_hardirq();
701 /* This allocator is optimized for the XDP mode that uses
702 * one-frame-per-page, but have fallbacks that act like the
703 * regular page allocator APIs.
705 * refcnt == 1 means page_pool owns page, and can recycle it.
707 * page is NOT reusable when allocated when system is under
708 * some pressure. (page_is_pfmemalloc)
710 if (likely(__page_pool_page_can_be_recycled(page))) {
711 /* Read barrier done in page_ref_count / READ_ONCE */
713 page_pool_dma_sync_for_device(pool, page, dma_sync_size);
715 if (allow_direct && page_pool_recycle_in_cache(page, pool))
718 /* Page found as candidate for recycling */
721 /* Fallback/non-XDP mode: API user have elevated refcnt.
723 * Many drivers split up the page into fragments, and some
724 * want to keep doing this to save memory and do refcnt based
725 * recycling. Support this use case too, to ease drivers
726 * switching between XDP/non-XDP.
728 * In-case page_pool maintains the DMA mapping, API user must
729 * call page_pool_put_page once. In this elevated refcnt
730 * case, the DMA is unmapped/released, as driver is likely
731 * doing refcnt based recycle tricks, meaning another process
732 * will be invoking put_page.
734 recycle_stat_inc(pool, released_refcnt);
735 page_pool_return_page(pool, page);
740 static bool page_pool_napi_local(const struct page_pool *pool)
742 const struct napi_struct *napi;
745 if (unlikely(!in_softirq()))
748 /* Allow direct recycle if we have reasons to believe that we are
749 * in the same context as the consumer would run, so there's
751 * __page_pool_put_page() makes sure we're not in hardirq context
752 * and interrupts are enabled prior to accessing the cache.
754 cpuid = smp_processor_id();
755 if (READ_ONCE(pool->cpuid) == cpuid)
758 napi = READ_ONCE(pool->p.napi);
760 return napi && READ_ONCE(napi->list_owner) == cpuid;
763 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page,
764 unsigned int dma_sync_size, bool allow_direct)
767 allow_direct = page_pool_napi_local(pool);
769 page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
770 if (page && !page_pool_recycle_in_ring(pool, page)) {
771 /* Cache full, fallback to free pages */
772 recycle_stat_inc(pool, ring_full);
773 page_pool_return_page(pool, page);
776 EXPORT_SYMBOL(page_pool_put_unrefed_page);
779 * page_pool_put_page_bulk() - release references on multiple pages
780 * @pool: pool from which pages were allocated
781 * @data: array holding page pointers
782 * @count: number of pages in @data
784 * Tries to refill a number of pages into the ptr_ring cache holding ptr_ring
785 * producer lock. If the ptr_ring is full, page_pool_put_page_bulk()
786 * will release leftover pages to the page allocator.
787 * page_pool_put_page_bulk() is suitable to be run inside the driver NAPI tx
788 * completion loop for the XDP_REDIRECT use case.
790 * Please note the caller must not use data area after running
791 * page_pool_put_page_bulk(), as this function overwrites it.
793 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
800 allow_direct = page_pool_napi_local(pool);
802 for (i = 0; i < count; i++) {
803 struct page *page = virt_to_head_page(data[i]);
805 /* It is not the last user for the page frag case */
806 if (!page_pool_is_last_ref(page))
809 page = __page_pool_put_page(pool, page, -1, allow_direct);
810 /* Approved for bulk recycling in ptr_ring cache */
812 data[bulk_len++] = page;
818 /* Bulk producer into ptr_ring page_pool cache */
819 in_softirq = page_pool_producer_lock(pool);
820 for (i = 0; i < bulk_len; i++) {
821 if (__ptr_ring_produce(&pool->ring, data[i])) {
823 recycle_stat_inc(pool, ring_full);
827 recycle_stat_add(pool, ring, i);
828 page_pool_producer_unlock(pool, in_softirq);
830 /* Hopefully all pages was return into ptr_ring */
831 if (likely(i == bulk_len))
834 /* ptr_ring cache full, free remaining pages outside producer lock
835 * since put_page() with refcnt == 1 can be an expensive operation
837 for (; i < bulk_len; i++)
838 page_pool_return_page(pool, data[i]);
840 EXPORT_SYMBOL(page_pool_put_page_bulk);
842 static struct page *page_pool_drain_frag(struct page_pool *pool,
845 long drain_count = BIAS_MAX - pool->frag_users;
847 /* Some user is still using the page frag */
848 if (likely(page_pool_unref_page(page, drain_count)))
851 if (__page_pool_page_can_be_recycled(page)) {
852 page_pool_dma_sync_for_device(pool, page, -1);
856 page_pool_return_page(pool, page);
860 static void page_pool_free_frag(struct page_pool *pool)
862 long drain_count = BIAS_MAX - pool->frag_users;
863 struct page *page = pool->frag_page;
865 pool->frag_page = NULL;
867 if (!page || page_pool_unref_page(page, drain_count))
870 page_pool_return_page(pool, page);
873 struct page *page_pool_alloc_frag(struct page_pool *pool,
874 unsigned int *offset,
875 unsigned int size, gfp_t gfp)
877 unsigned int max_size = PAGE_SIZE << pool->p.order;
878 struct page *page = pool->frag_page;
880 if (WARN_ON(size > max_size))
883 size = ALIGN(size, dma_get_cache_alignment());
884 *offset = pool->frag_offset;
886 if (page && *offset + size > max_size) {
887 page = page_pool_drain_frag(pool, page);
889 alloc_stat_inc(pool, fast);
895 page = page_pool_alloc_pages(pool, gfp);
896 if (unlikely(!page)) {
897 pool->frag_page = NULL;
901 pool->frag_page = page;
904 pool->frag_users = 1;
906 pool->frag_offset = size;
907 page_pool_fragment_page(page, BIAS_MAX);
912 pool->frag_offset = *offset + size;
913 alloc_stat_inc(pool, fast);
916 EXPORT_SYMBOL(page_pool_alloc_frag);
918 static void page_pool_empty_ring(struct page_pool *pool)
922 /* Empty recycle ring */
923 while ((page = ptr_ring_consume_bh(&pool->ring))) {
924 /* Verify the refcnt invariant of cached pages */
925 if (!(page_ref_count(page) == 1))
926 pr_crit("%s() page_pool refcnt %d violation\n",
927 __func__, page_ref_count(page));
929 page_pool_return_page(pool, page);
933 static void __page_pool_destroy(struct page_pool *pool)
935 if (pool->disconnect)
936 pool->disconnect(pool);
938 page_pool_unlist(pool);
939 page_pool_uninit(pool);
943 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
947 if (pool->destroy_cnt)
950 /* Empty alloc cache, assume caller made sure this is
951 * no-longer in use, and page_pool_alloc_pages() cannot be
954 while (pool->alloc.count) {
955 page = pool->alloc.cache[--pool->alloc.count];
956 page_pool_return_page(pool, page);
960 static void page_pool_scrub(struct page_pool *pool)
962 page_pool_empty_alloc_cache_once(pool);
965 /* No more consumers should exist, but producers could still
968 page_pool_empty_ring(pool);
971 static int page_pool_release(struct page_pool *pool)
975 page_pool_scrub(pool);
976 inflight = page_pool_inflight(pool, true);
978 __page_pool_destroy(pool);
983 static void page_pool_release_retry(struct work_struct *wq)
985 struct delayed_work *dwq = to_delayed_work(wq);
986 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
990 inflight = page_pool_release(pool);
994 /* Periodic warning for page pools the user can't see */
995 netdev = READ_ONCE(pool->slow.netdev);
996 if (time_after_eq(jiffies, pool->defer_warn) &&
997 (!netdev || netdev == NET_PTR_POISON)) {
998 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
1000 pr_warn("%s() stalled pool shutdown: id %u, %d inflight %d sec\n",
1001 __func__, pool->user.id, inflight, sec);
1002 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
1005 /* Still not ready to be disconnected, retry later */
1006 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
1009 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
1010 const struct xdp_mem_info *mem)
1012 refcount_inc(&pool->user_cnt);
1013 pool->disconnect = disconnect;
1014 pool->xdp_mem_id = mem->id;
1017 static void page_pool_disable_direct_recycling(struct page_pool *pool)
1019 /* Disable direct recycling based on pool->cpuid.
1020 * Paired with READ_ONCE() in page_pool_napi_local().
1022 WRITE_ONCE(pool->cpuid, -1);
1027 /* To avoid races with recycling and additional barriers make sure
1028 * pool and NAPI are unlinked when NAPI is disabled.
1030 WARN_ON(!test_bit(NAPI_STATE_SCHED, &pool->p.napi->state) ||
1031 READ_ONCE(pool->p.napi->list_owner) != -1);
1033 WRITE_ONCE(pool->p.napi, NULL);
1036 void page_pool_destroy(struct page_pool *pool)
1041 if (!page_pool_put(pool))
1044 page_pool_disable_direct_recycling(pool);
1045 page_pool_free_frag(pool);
1047 if (!page_pool_release(pool))
1050 page_pool_detached(pool);
1051 pool->defer_start = jiffies;
1052 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
1054 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
1055 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
1057 EXPORT_SYMBOL(page_pool_destroy);
1059 /* Caller must provide appropriate safe context, e.g. NAPI. */
1060 void page_pool_update_nid(struct page_pool *pool, int new_nid)
1064 trace_page_pool_update_nid(pool, new_nid);
1065 pool->p.nid = new_nid;
1067 /* Flush pool alloc cache, as refill will check NUMA node */
1068 while (pool->alloc.count) {
1069 page = pool->alloc.cache[--pool->alloc.count];
1070 page_pool_return_page(pool, page);
1073 EXPORT_SYMBOL(page_pool_update_nid);