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.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>
22 #include <trace/events/page_pool.h>
24 #define DEFER_TIME (msecs_to_jiffies(1000))
25 #define DEFER_WARN_INTERVAL (60 * HZ)
27 #define BIAS_MAX LONG_MAX
29 #ifdef CONFIG_PAGE_POOL_STATS
30 /* alloc_stat_inc is intended to be used in softirq context */
31 #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++)
32 /* recycle_stat_inc is safe to use when preemption is possible. */
33 #define recycle_stat_inc(pool, __stat) \
35 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \
36 this_cpu_inc(s->__stat); \
39 bool page_pool_get_stats(struct page_pool *pool,
40 struct page_pool_stats *stats)
47 memcpy(&stats->alloc_stats, &pool->alloc_stats, sizeof(pool->alloc_stats));
49 for_each_possible_cpu(cpu) {
50 const struct page_pool_recycle_stats *pcpu =
51 per_cpu_ptr(pool->recycle_stats, cpu);
53 stats->recycle_stats.cached += pcpu->cached;
54 stats->recycle_stats.cache_full += pcpu->cache_full;
55 stats->recycle_stats.ring += pcpu->ring;
56 stats->recycle_stats.ring_full += pcpu->ring_full;
57 stats->recycle_stats.released_refcnt += pcpu->released_refcnt;
62 EXPORT_SYMBOL(page_pool_get_stats);
64 #define alloc_stat_inc(pool, __stat)
65 #define recycle_stat_inc(pool, __stat)
68 static int page_pool_init(struct page_pool *pool,
69 const struct page_pool_params *params)
71 unsigned int ring_qsize = 1024; /* Default */
73 memcpy(&pool->p, params, sizeof(pool->p));
75 /* Validate only known flags were used */
76 if (pool->p.flags & ~(PP_FLAG_ALL))
79 if (pool->p.pool_size)
80 ring_qsize = pool->p.pool_size;
82 /* Sanity limit mem that can be pinned down */
83 if (ring_qsize > 32768)
86 /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL.
87 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending,
88 * which is the XDP_TX use-case.
90 if (pool->p.flags & PP_FLAG_DMA_MAP) {
91 if ((pool->p.dma_dir != DMA_FROM_DEVICE) &&
92 (pool->p.dma_dir != DMA_BIDIRECTIONAL))
96 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV) {
97 /* In order to request DMA-sync-for-device the page
100 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
103 if (!pool->p.max_len)
106 /* pool->p.offset has to be set according to the address
107 * offset used by the DMA engine to start copying rx data
111 if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT &&
112 pool->p.flags & PP_FLAG_PAGE_FRAG)
115 #ifdef CONFIG_PAGE_POOL_STATS
116 pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats);
117 if (!pool->recycle_stats)
121 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
124 atomic_set(&pool->pages_state_release_cnt, 0);
126 /* Driver calling page_pool_create() also call page_pool_destroy() */
127 refcount_set(&pool->user_cnt, 1);
129 if (pool->p.flags & PP_FLAG_DMA_MAP)
130 get_device(pool->p.dev);
135 struct page_pool *page_pool_create(const struct page_pool_params *params)
137 struct page_pool *pool;
140 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
142 return ERR_PTR(-ENOMEM);
144 err = page_pool_init(pool, params);
146 pr_warn("%s() gave up with errno %d\n", __func__, err);
153 EXPORT_SYMBOL(page_pool_create);
155 static void page_pool_return_page(struct page_pool *pool, struct page *page);
158 static struct page *page_pool_refill_alloc_cache(struct page_pool *pool)
160 struct ptr_ring *r = &pool->ring;
162 int pref_nid; /* preferred NUMA node */
164 /* Quicker fallback, avoid locks when ring is empty */
165 if (__ptr_ring_empty(r)) {
166 alloc_stat_inc(pool, empty);
170 /* Softirq guarantee CPU and thus NUMA node is stable. This,
171 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
174 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
176 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
177 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
180 /* Refill alloc array, but only if NUMA match */
182 page = __ptr_ring_consume(r);
186 if (likely(page_to_nid(page) == pref_nid)) {
187 pool->alloc.cache[pool->alloc.count++] = page;
190 * (1) release 1 page to page-allocator and
191 * (2) break out to fallthrough to alloc_pages_node.
192 * This limit stress on page buddy alloactor.
194 page_pool_return_page(pool, page);
195 alloc_stat_inc(pool, waive);
199 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
201 /* Return last page */
202 if (likely(pool->alloc.count > 0)) {
203 page = pool->alloc.cache[--pool->alloc.count];
204 alloc_stat_inc(pool, refill);
211 static struct page *__page_pool_get_cached(struct page_pool *pool)
215 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
216 if (likely(pool->alloc.count)) {
218 page = pool->alloc.cache[--pool->alloc.count];
219 alloc_stat_inc(pool, fast);
221 page = page_pool_refill_alloc_cache(pool);
227 static void page_pool_dma_sync_for_device(struct page_pool *pool,
229 unsigned int dma_sync_size)
231 dma_addr_t dma_addr = page_pool_get_dma_addr(page);
233 dma_sync_size = min(dma_sync_size, pool->p.max_len);
234 dma_sync_single_range_for_device(pool->p.dev, dma_addr,
235 pool->p.offset, dma_sync_size,
239 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
243 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr
244 * since dma_addr_t can be either 32 or 64 bits and does not always fit
245 * into page private data (i.e 32bit cpu with 64bit DMA caps)
246 * This mapping is kept for lifetime of page, until leaving pool.
248 dma = dma_map_page_attrs(pool->p.dev, page, 0,
249 (PAGE_SIZE << pool->p.order),
250 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
251 if (dma_mapping_error(pool->p.dev, dma))
254 page_pool_set_dma_addr(page, dma);
256 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
257 page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
262 static void page_pool_set_pp_info(struct page_pool *pool,
266 page->pp_magic |= PP_SIGNATURE;
267 if (pool->p.init_callback)
268 pool->p.init_callback(page, pool->p.init_arg);
271 static void page_pool_clear_pp_info(struct page *page)
277 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
283 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
287 if ((pool->p.flags & PP_FLAG_DMA_MAP) &&
288 unlikely(!page_pool_dma_map(pool, page))) {
293 alloc_stat_inc(pool, slow_high_order);
294 page_pool_set_pp_info(pool, page);
296 /* Track how many pages are held 'in-flight' */
297 pool->pages_state_hold_cnt++;
298 trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
304 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
307 const int bulk = PP_ALLOC_CACHE_REFILL;
308 unsigned int pp_flags = pool->p.flags;
309 unsigned int pp_order = pool->p.order;
313 /* Don't support bulk alloc for high-order pages */
314 if (unlikely(pp_order))
315 return __page_pool_alloc_page_order(pool, gfp);
317 /* Unnecessary as alloc cache is empty, but guarantees zero count */
318 if (unlikely(pool->alloc.count > 0))
319 return pool->alloc.cache[--pool->alloc.count];
321 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
322 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
324 nr_pages = alloc_pages_bulk_array(gfp, bulk, pool->alloc.cache);
325 if (unlikely(!nr_pages))
328 /* Pages have been filled into alloc.cache array, but count is zero and
329 * page element have not been (possibly) DMA mapped.
331 for (i = 0; i < nr_pages; i++) {
332 page = pool->alloc.cache[i];
333 if ((pp_flags & PP_FLAG_DMA_MAP) &&
334 unlikely(!page_pool_dma_map(pool, page))) {
339 page_pool_set_pp_info(pool, page);
340 pool->alloc.cache[pool->alloc.count++] = page;
341 /* Track how many pages are held 'in-flight' */
342 pool->pages_state_hold_cnt++;
343 trace_page_pool_state_hold(pool, page,
344 pool->pages_state_hold_cnt);
347 /* Return last page */
348 if (likely(pool->alloc.count > 0)) {
349 page = pool->alloc.cache[--pool->alloc.count];
350 alloc_stat_inc(pool, slow);
355 /* When page just alloc'ed is should/must have refcnt 1. */
359 /* For using page_pool replace: alloc_pages() API calls, but provide
360 * synchronization guarantee for allocation side.
362 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
366 /* Fast-path: Get a page from cache */
367 page = __page_pool_get_cached(pool);
371 /* Slow-path: cache empty, do real allocation */
372 page = __page_pool_alloc_pages_slow(pool, gfp);
375 EXPORT_SYMBOL(page_pool_alloc_pages);
377 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
378 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
380 #define _distance(a, b) (s32)((a) - (b))
382 static s32 page_pool_inflight(struct page_pool *pool)
384 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
385 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
388 inflight = _distance(hold_cnt, release_cnt);
390 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
391 WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
396 /* Disconnects a page (from a page_pool). API users can have a need
397 * to disconnect a page (from a page_pool), to allow it to be used as
398 * a regular page (that will eventually be returned to the normal
399 * page-allocator via put_page).
401 void page_pool_release_page(struct page_pool *pool, struct page *page)
406 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
407 /* Always account for inflight pages, even if we didn't
412 dma = page_pool_get_dma_addr(page);
414 /* When page is unmapped, it cannot be returned to our pool */
415 dma_unmap_page_attrs(pool->p.dev, dma,
416 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
417 DMA_ATTR_SKIP_CPU_SYNC);
418 page_pool_set_dma_addr(page, 0);
420 page_pool_clear_pp_info(page);
422 /* This may be the last page returned, releasing the pool, so
423 * it is not safe to reference pool afterwards.
425 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
426 trace_page_pool_state_release(pool, page, count);
428 EXPORT_SYMBOL(page_pool_release_page);
430 /* Return a page to the page allocator, cleaning up our state */
431 static void page_pool_return_page(struct page_pool *pool, struct page *page)
433 page_pool_release_page(pool, page);
436 /* An optimization would be to call __free_pages(page, pool->p.order)
437 * knowing page is not part of page-cache (thus avoiding a
438 * __page_cache_release() call).
442 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
445 /* BH protection not needed if current is serving softirq */
446 if (in_serving_softirq())
447 ret = ptr_ring_produce(&pool->ring, page);
449 ret = ptr_ring_produce_bh(&pool->ring, page);
452 recycle_stat_inc(pool, ring);
459 /* Only allow direct recycling in special circumstances, into the
460 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
462 * Caller must provide appropriate safe context.
464 static bool page_pool_recycle_in_cache(struct page *page,
465 struct page_pool *pool)
467 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) {
468 recycle_stat_inc(pool, cache_full);
472 /* Caller MUST have verified/know (page_ref_count(page) == 1) */
473 pool->alloc.cache[pool->alloc.count++] = page;
474 recycle_stat_inc(pool, cached);
478 /* If the page refcnt == 1, this will try to recycle the page.
479 * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for
480 * the configured size min(dma_sync_size, pool->max_len).
481 * If the page refcnt != 1, then the page will be returned to memory
484 static __always_inline struct page *
485 __page_pool_put_page(struct page_pool *pool, struct page *page,
486 unsigned int dma_sync_size, bool allow_direct)
488 /* This allocator is optimized for the XDP mode that uses
489 * one-frame-per-page, but have fallbacks that act like the
490 * regular page allocator APIs.
492 * refcnt == 1 means page_pool owns page, and can recycle it.
494 * page is NOT reusable when allocated when system is under
495 * some pressure. (page_is_pfmemalloc)
497 if (likely(page_ref_count(page) == 1 && !page_is_pfmemalloc(page))) {
498 /* Read barrier done in page_ref_count / READ_ONCE */
500 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
501 page_pool_dma_sync_for_device(pool, page,
504 if (allow_direct && in_serving_softirq() &&
505 page_pool_recycle_in_cache(page, pool))
508 /* Page found as candidate for recycling */
511 /* Fallback/non-XDP mode: API user have elevated refcnt.
513 * Many drivers split up the page into fragments, and some
514 * want to keep doing this to save memory and do refcnt based
515 * recycling. Support this use case too, to ease drivers
516 * switching between XDP/non-XDP.
518 * In-case page_pool maintains the DMA mapping, API user must
519 * call page_pool_put_page once. In this elevated refcnt
520 * case, the DMA is unmapped/released, as driver is likely
521 * doing refcnt based recycle tricks, meaning another process
522 * will be invoking put_page.
524 recycle_stat_inc(pool, released_refcnt);
525 /* Do not replace this with page_pool_return_page() */
526 page_pool_release_page(pool, page);
532 void page_pool_put_defragged_page(struct page_pool *pool, struct page *page,
533 unsigned int dma_sync_size, bool allow_direct)
535 page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
536 if (page && !page_pool_recycle_in_ring(pool, page)) {
537 /* Cache full, fallback to free pages */
538 recycle_stat_inc(pool, ring_full);
539 page_pool_return_page(pool, page);
542 EXPORT_SYMBOL(page_pool_put_defragged_page);
544 /* Caller must not use data area after call, as this function overwrites it */
545 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
550 for (i = 0; i < count; i++) {
551 struct page *page = virt_to_head_page(data[i]);
553 /* It is not the last user for the page frag case */
554 if (!page_pool_is_last_frag(pool, page))
557 page = __page_pool_put_page(pool, page, -1, false);
558 /* Approved for bulk recycling in ptr_ring cache */
560 data[bulk_len++] = page;
563 if (unlikely(!bulk_len))
566 /* Bulk producer into ptr_ring page_pool cache */
567 page_pool_ring_lock(pool);
568 for (i = 0; i < bulk_len; i++) {
569 if (__ptr_ring_produce(&pool->ring, data[i]))
570 break; /* ring full */
572 page_pool_ring_unlock(pool);
574 /* Hopefully all pages was return into ptr_ring */
575 if (likely(i == bulk_len))
578 /* ptr_ring cache full, free remaining pages outside producer lock
579 * since put_page() with refcnt == 1 can be an expensive operation
581 for (; i < bulk_len; i++)
582 page_pool_return_page(pool, data[i]);
584 EXPORT_SYMBOL(page_pool_put_page_bulk);
586 static struct page *page_pool_drain_frag(struct page_pool *pool,
589 long drain_count = BIAS_MAX - pool->frag_users;
591 /* Some user is still using the page frag */
592 if (likely(page_pool_defrag_page(page, drain_count)))
595 if (page_ref_count(page) == 1 && !page_is_pfmemalloc(page)) {
596 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
597 page_pool_dma_sync_for_device(pool, page, -1);
602 page_pool_return_page(pool, page);
606 static void page_pool_free_frag(struct page_pool *pool)
608 long drain_count = BIAS_MAX - pool->frag_users;
609 struct page *page = pool->frag_page;
611 pool->frag_page = NULL;
613 if (!page || page_pool_defrag_page(page, drain_count))
616 page_pool_return_page(pool, page);
619 struct page *page_pool_alloc_frag(struct page_pool *pool,
620 unsigned int *offset,
621 unsigned int size, gfp_t gfp)
623 unsigned int max_size = PAGE_SIZE << pool->p.order;
624 struct page *page = pool->frag_page;
626 if (WARN_ON(!(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
630 size = ALIGN(size, dma_get_cache_alignment());
631 *offset = pool->frag_offset;
633 if (page && *offset + size > max_size) {
634 page = page_pool_drain_frag(pool, page);
640 page = page_pool_alloc_pages(pool, gfp);
641 if (unlikely(!page)) {
642 pool->frag_page = NULL;
646 pool->frag_page = page;
649 pool->frag_users = 1;
651 pool->frag_offset = size;
652 page_pool_fragment_page(page, BIAS_MAX);
657 pool->frag_offset = *offset + size;
660 EXPORT_SYMBOL(page_pool_alloc_frag);
662 static void page_pool_empty_ring(struct page_pool *pool)
666 /* Empty recycle ring */
667 while ((page = ptr_ring_consume_bh(&pool->ring))) {
668 /* Verify the refcnt invariant of cached pages */
669 if (!(page_ref_count(page) == 1))
670 pr_crit("%s() page_pool refcnt %d violation\n",
671 __func__, page_ref_count(page));
673 page_pool_return_page(pool, page);
677 static void page_pool_free(struct page_pool *pool)
679 if (pool->disconnect)
680 pool->disconnect(pool);
682 ptr_ring_cleanup(&pool->ring, NULL);
684 if (pool->p.flags & PP_FLAG_DMA_MAP)
685 put_device(pool->p.dev);
687 #ifdef CONFIG_PAGE_POOL_STATS
688 free_percpu(pool->recycle_stats);
693 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
697 if (pool->destroy_cnt)
700 /* Empty alloc cache, assume caller made sure this is
701 * no-longer in use, and page_pool_alloc_pages() cannot be
704 while (pool->alloc.count) {
705 page = pool->alloc.cache[--pool->alloc.count];
706 page_pool_return_page(pool, page);
710 static void page_pool_scrub(struct page_pool *pool)
712 page_pool_empty_alloc_cache_once(pool);
715 /* No more consumers should exist, but producers could still
718 page_pool_empty_ring(pool);
721 static int page_pool_release(struct page_pool *pool)
725 page_pool_scrub(pool);
726 inflight = page_pool_inflight(pool);
728 page_pool_free(pool);
733 static void page_pool_release_retry(struct work_struct *wq)
735 struct delayed_work *dwq = to_delayed_work(wq);
736 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
739 inflight = page_pool_release(pool);
743 /* Periodic warning */
744 if (time_after_eq(jiffies, pool->defer_warn)) {
745 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
747 pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
748 __func__, inflight, sec);
749 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
752 /* Still not ready to be disconnected, retry later */
753 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
756 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
757 struct xdp_mem_info *mem)
759 refcount_inc(&pool->user_cnt);
760 pool->disconnect = disconnect;
761 pool->xdp_mem_id = mem->id;
764 void page_pool_destroy(struct page_pool *pool)
769 if (!page_pool_put(pool))
772 page_pool_free_frag(pool);
774 if (!page_pool_release(pool))
777 pool->defer_start = jiffies;
778 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
780 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
781 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
783 EXPORT_SYMBOL(page_pool_destroy);
785 /* Caller must provide appropriate safe context, e.g. NAPI. */
786 void page_pool_update_nid(struct page_pool *pool, int new_nid)
790 trace_page_pool_update_nid(pool, new_nid);
791 pool->p.nid = new_nid;
793 /* Flush pool alloc cache, as refill will check NUMA node */
794 while (pool->alloc.count) {
795 page = pool->alloc.cache[--pool->alloc.count];
796 page_pool_return_page(pool, page);
799 EXPORT_SYMBOL(page_pool_update_nid);
801 bool page_pool_return_skb_page(struct page *page)
803 struct page_pool *pp;
805 page = compound_head(page);
807 /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
808 * in order to preserve any existing bits, such as bit 0 for the
809 * head page of compound page and bit 1 for pfmemalloc page, so
810 * mask those bits for freeing side when doing below checking,
811 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
812 * to avoid recycling the pfmemalloc page.
814 if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
819 /* Driver set this to memory recycling info. Reset it on recycle.
820 * This will *not* work for NIC using a split-page memory model.
821 * The page will be returned to the pool here regardless of the
822 * 'flipped' fragment being in use or not.
824 page_pool_put_full_page(pp, page, false);
828 EXPORT_SYMBOL(page_pool_return_skb_page);