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 static int page_pool_init(struct page_pool *pool,
30 const struct page_pool_params *params)
32 unsigned int ring_qsize = 1024; /* Default */
34 memcpy(&pool->p, params, sizeof(pool->p));
36 /* Validate only known flags were used */
37 if (pool->p.flags & ~(PP_FLAG_ALL))
40 if (pool->p.pool_size)
41 ring_qsize = pool->p.pool_size;
43 /* Sanity limit mem that can be pinned down */
44 if (ring_qsize > 32768)
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.
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))
57 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV) {
58 /* In order to request DMA-sync-for-device the page
61 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
67 /* pool->p.offset has to be set according to the address
68 * offset used by the DMA engine to start copying rx data
72 if (PAGE_POOL_DMA_USE_PP_FRAG_COUNT &&
73 pool->p.flags & PP_FLAG_PAGE_FRAG)
76 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0)
79 atomic_set(&pool->pages_state_release_cnt, 0);
81 /* Driver calling page_pool_create() also call page_pool_destroy() */
82 refcount_set(&pool->user_cnt, 1);
84 if (pool->p.flags & PP_FLAG_DMA_MAP)
85 get_device(pool->p.dev);
90 struct page_pool *page_pool_create(const struct page_pool_params *params)
92 struct page_pool *pool;
95 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid);
97 return ERR_PTR(-ENOMEM);
99 err = page_pool_init(pool, params);
101 pr_warn("%s() gave up with errno %d\n", __func__, err);
108 EXPORT_SYMBOL(page_pool_create);
110 static void page_pool_return_page(struct page_pool *pool, struct page *page);
113 static struct page *page_pool_refill_alloc_cache(struct page_pool *pool)
115 struct ptr_ring *r = &pool->ring;
117 int pref_nid; /* preferred NUMA node */
119 /* Quicker fallback, avoid locks when ring is empty */
120 if (__ptr_ring_empty(r))
123 /* Softirq guarantee CPU and thus NUMA node is stable. This,
124 * assumes CPU refilling driver RX-ring will also run RX-NAPI.
127 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid;
129 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */
130 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */
133 /* Slower-path: Get pages from locked ring queue */
134 spin_lock(&r->consumer_lock);
136 /* Refill alloc array, but only if NUMA match */
138 page = __ptr_ring_consume(r);
142 if (likely(page_to_nid(page) == pref_nid)) {
143 pool->alloc.cache[pool->alloc.count++] = page;
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.
150 page_pool_return_page(pool, page);
154 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
156 /* Return last page */
157 if (likely(pool->alloc.count > 0))
158 page = pool->alloc.cache[--pool->alloc.count];
160 spin_unlock(&r->consumer_lock);
165 static struct page *__page_pool_get_cached(struct page_pool *pool)
169 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
170 if (likely(pool->alloc.count)) {
172 page = pool->alloc.cache[--pool->alloc.count];
174 page = page_pool_refill_alloc_cache(pool);
180 static void page_pool_dma_sync_for_device(struct page_pool *pool,
182 unsigned int dma_sync_size)
184 dma_addr_t dma_addr = page_pool_get_dma_addr(page);
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,
192 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
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.
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))
207 page_pool_set_dma_addr(page, dma);
209 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
210 page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
215 static void page_pool_set_pp_info(struct page_pool *pool,
219 page->pp_magic |= PP_SIGNATURE;
222 static void page_pool_clear_pp_info(struct page *page)
228 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
234 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
238 if ((pool->p.flags & PP_FLAG_DMA_MAP) &&
239 unlikely(!page_pool_dma_map(pool, page))) {
244 page_pool_set_pp_info(pool, page);
246 /* Track how many pages are held 'in-flight' */
247 pool->pages_state_hold_cnt++;
248 trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
254 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
257 const int bulk = PP_ALLOC_CACHE_REFILL;
258 unsigned int pp_flags = pool->p.flags;
259 unsigned int pp_order = pool->p.order;
263 /* Don't support bulk alloc for high-order pages */
264 if (unlikely(pp_order))
265 return __page_pool_alloc_page_order(pool, gfp);
267 /* Unnecessary as alloc cache is empty, but guarantees zero count */
268 if (unlikely(pool->alloc.count > 0))
269 return pool->alloc.cache[--pool->alloc.count];
271 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
272 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
274 nr_pages = alloc_pages_bulk_array(gfp, bulk, pool->alloc.cache);
275 if (unlikely(!nr_pages))
278 /* Pages have been filled into alloc.cache array, but count is zero and
279 * page element have not been (possibly) DMA mapped.
281 for (i = 0; i < nr_pages; i++) {
282 page = pool->alloc.cache[i];
283 if ((pp_flags & PP_FLAG_DMA_MAP) &&
284 unlikely(!page_pool_dma_map(pool, page))) {
289 page_pool_set_pp_info(pool, page);
290 pool->alloc.cache[pool->alloc.count++] = page;
291 /* Track how many pages are held 'in-flight' */
292 pool->pages_state_hold_cnt++;
293 trace_page_pool_state_hold(pool, page,
294 pool->pages_state_hold_cnt);
297 /* Return last page */
298 if (likely(pool->alloc.count > 0))
299 page = pool->alloc.cache[--pool->alloc.count];
303 /* When page just alloc'ed is should/must have refcnt 1. */
307 /* For using page_pool replace: alloc_pages() API calls, but provide
308 * synchronization guarantee for allocation side.
310 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
314 /* Fast-path: Get a page from cache */
315 page = __page_pool_get_cached(pool);
319 /* Slow-path: cache empty, do real allocation */
320 page = __page_pool_alloc_pages_slow(pool, gfp);
323 EXPORT_SYMBOL(page_pool_alloc_pages);
325 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
326 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
328 #define _distance(a, b) (s32)((a) - (b))
330 static s32 page_pool_inflight(struct page_pool *pool)
332 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
333 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
336 inflight = _distance(hold_cnt, release_cnt);
338 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
339 WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
344 /* Disconnects a page (from a page_pool). API users can have a need
345 * to disconnect a page (from a page_pool), to allow it to be used as
346 * a regular page (that will eventually be returned to the normal
347 * page-allocator via put_page).
349 void page_pool_release_page(struct page_pool *pool, struct page *page)
354 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
355 /* Always account for inflight pages, even if we didn't
360 dma = page_pool_get_dma_addr(page);
362 /* When page is unmapped, it cannot be returned to our pool */
363 dma_unmap_page_attrs(pool->p.dev, dma,
364 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
365 DMA_ATTR_SKIP_CPU_SYNC);
366 page_pool_set_dma_addr(page, 0);
368 page_pool_clear_pp_info(page);
370 /* This may be the last page returned, releasing the pool, so
371 * it is not safe to reference pool afterwards.
373 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
374 trace_page_pool_state_release(pool, page, count);
376 EXPORT_SYMBOL(page_pool_release_page);
378 /* Return a page to the page allocator, cleaning up our state */
379 static void page_pool_return_page(struct page_pool *pool, struct page *page)
381 page_pool_release_page(pool, page);
384 /* An optimization would be to call __free_pages(page, pool->p.order)
385 * knowing page is not part of page-cache (thus avoiding a
386 * __page_cache_release() call).
390 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
393 /* BH protection not needed if current is serving softirq */
394 if (in_serving_softirq())
395 ret = ptr_ring_produce(&pool->ring, page);
397 ret = ptr_ring_produce_bh(&pool->ring, page);
399 return (ret == 0) ? true : false;
402 /* Only allow direct recycling in special circumstances, into the
403 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
405 * Caller must provide appropriate safe context.
407 static bool page_pool_recycle_in_cache(struct page *page,
408 struct page_pool *pool)
410 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE))
413 /* Caller MUST have verified/know (page_ref_count(page) == 1) */
414 pool->alloc.cache[pool->alloc.count++] = page;
418 /* If the page refcnt == 1, this will try to recycle the page.
419 * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for
420 * the configured size min(dma_sync_size, pool->max_len).
421 * If the page refcnt != 1, then the page will be returned to memory
424 static __always_inline struct page *
425 __page_pool_put_page(struct page_pool *pool, struct page *page,
426 unsigned int dma_sync_size, bool allow_direct)
428 /* It is not the last user for the page frag case */
429 if (pool->p.flags & PP_FLAG_PAGE_FRAG &&
430 page_pool_atomic_sub_frag_count_return(page, 1))
433 /* This allocator is optimized for the XDP mode that uses
434 * one-frame-per-page, but have fallbacks that act like the
435 * regular page allocator APIs.
437 * refcnt == 1 means page_pool owns page, and can recycle it.
439 * page is NOT reusable when allocated when system is under
440 * some pressure. (page_is_pfmemalloc)
442 if (likely(page_ref_count(page) == 1 && !page_is_pfmemalloc(page))) {
443 /* Read barrier done in page_ref_count / READ_ONCE */
445 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
446 page_pool_dma_sync_for_device(pool, page,
449 if (allow_direct && in_serving_softirq() &&
450 page_pool_recycle_in_cache(page, pool))
453 /* Page found as candidate for recycling */
456 /* Fallback/non-XDP mode: API user have elevated refcnt.
458 * Many drivers split up the page into fragments, and some
459 * want to keep doing this to save memory and do refcnt based
460 * recycling. Support this use case too, to ease drivers
461 * switching between XDP/non-XDP.
463 * In-case page_pool maintains the DMA mapping, API user must
464 * call page_pool_put_page once. In this elevated refcnt
465 * case, the DMA is unmapped/released, as driver is likely
466 * doing refcnt based recycle tricks, meaning another process
467 * will be invoking put_page.
469 /* Do not replace this with page_pool_return_page() */
470 page_pool_release_page(pool, page);
476 void page_pool_put_page(struct page_pool *pool, struct page *page,
477 unsigned int dma_sync_size, bool allow_direct)
479 page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
480 if (page && !page_pool_recycle_in_ring(pool, page)) {
481 /* Cache full, fallback to free pages */
482 page_pool_return_page(pool, page);
485 EXPORT_SYMBOL(page_pool_put_page);
487 /* Caller must not use data area after call, as this function overwrites it */
488 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
493 for (i = 0; i < count; i++) {
494 struct page *page = virt_to_head_page(data[i]);
496 page = __page_pool_put_page(pool, page, -1, false);
497 /* Approved for bulk recycling in ptr_ring cache */
499 data[bulk_len++] = page;
502 if (unlikely(!bulk_len))
505 /* Bulk producer into ptr_ring page_pool cache */
506 page_pool_ring_lock(pool);
507 for (i = 0; i < bulk_len; i++) {
508 if (__ptr_ring_produce(&pool->ring, data[i]))
509 break; /* ring full */
511 page_pool_ring_unlock(pool);
513 /* Hopefully all pages was return into ptr_ring */
514 if (likely(i == bulk_len))
517 /* ptr_ring cache full, free remaining pages outside producer lock
518 * since put_page() with refcnt == 1 can be an expensive operation
520 for (; i < bulk_len; i++)
521 page_pool_return_page(pool, data[i]);
523 EXPORT_SYMBOL(page_pool_put_page_bulk);
525 static struct page *page_pool_drain_frag(struct page_pool *pool,
528 long drain_count = BIAS_MAX - pool->frag_users;
530 /* Some user is still using the page frag */
531 if (likely(page_pool_atomic_sub_frag_count_return(page,
535 if (page_ref_count(page) == 1 && !page_is_pfmemalloc(page)) {
536 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
537 page_pool_dma_sync_for_device(pool, page, -1);
542 page_pool_return_page(pool, page);
546 static void page_pool_free_frag(struct page_pool *pool)
548 long drain_count = BIAS_MAX - pool->frag_users;
549 struct page *page = pool->frag_page;
551 pool->frag_page = NULL;
554 page_pool_atomic_sub_frag_count_return(page, drain_count))
557 page_pool_return_page(pool, page);
560 struct page *page_pool_alloc_frag(struct page_pool *pool,
561 unsigned int *offset,
562 unsigned int size, gfp_t gfp)
564 unsigned int max_size = PAGE_SIZE << pool->p.order;
565 struct page *page = pool->frag_page;
567 if (WARN_ON(!(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
571 size = ALIGN(size, dma_get_cache_alignment());
572 *offset = pool->frag_offset;
574 if (page && *offset + size > max_size) {
575 page = page_pool_drain_frag(pool, page);
581 page = page_pool_alloc_pages(pool, gfp);
582 if (unlikely(!page)) {
583 pool->frag_page = NULL;
587 pool->frag_page = page;
590 pool->frag_users = 1;
592 pool->frag_offset = size;
593 page_pool_set_frag_count(page, BIAS_MAX);
598 pool->frag_offset = *offset + size;
601 EXPORT_SYMBOL(page_pool_alloc_frag);
603 static void page_pool_empty_ring(struct page_pool *pool)
607 /* Empty recycle ring */
608 while ((page = ptr_ring_consume_bh(&pool->ring))) {
609 /* Verify the refcnt invariant of cached pages */
610 if (!(page_ref_count(page) == 1))
611 pr_crit("%s() page_pool refcnt %d violation\n",
612 __func__, page_ref_count(page));
614 page_pool_return_page(pool, page);
618 static void page_pool_free(struct page_pool *pool)
620 if (pool->disconnect)
621 pool->disconnect(pool);
623 ptr_ring_cleanup(&pool->ring, NULL);
625 if (pool->p.flags & PP_FLAG_DMA_MAP)
626 put_device(pool->p.dev);
631 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
635 if (pool->destroy_cnt)
638 /* Empty alloc cache, assume caller made sure this is
639 * no-longer in use, and page_pool_alloc_pages() cannot be
642 while (pool->alloc.count) {
643 page = pool->alloc.cache[--pool->alloc.count];
644 page_pool_return_page(pool, page);
648 static void page_pool_scrub(struct page_pool *pool)
650 page_pool_empty_alloc_cache_once(pool);
653 /* No more consumers should exist, but producers could still
656 page_pool_empty_ring(pool);
659 static int page_pool_release(struct page_pool *pool)
663 page_pool_scrub(pool);
664 inflight = page_pool_inflight(pool);
666 page_pool_free(pool);
671 static void page_pool_release_retry(struct work_struct *wq)
673 struct delayed_work *dwq = to_delayed_work(wq);
674 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
677 inflight = page_pool_release(pool);
681 /* Periodic warning */
682 if (time_after_eq(jiffies, pool->defer_warn)) {
683 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
685 pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
686 __func__, inflight, sec);
687 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
690 /* Still not ready to be disconnected, retry later */
691 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
694 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *))
696 refcount_inc(&pool->user_cnt);
697 pool->disconnect = disconnect;
700 void page_pool_destroy(struct page_pool *pool)
705 if (!page_pool_put(pool))
708 page_pool_free_frag(pool);
710 if (!page_pool_release(pool))
713 pool->defer_start = jiffies;
714 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
716 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry);
717 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
719 EXPORT_SYMBOL(page_pool_destroy);
721 /* Caller must provide appropriate safe context, e.g. NAPI. */
722 void page_pool_update_nid(struct page_pool *pool, int new_nid)
726 trace_page_pool_update_nid(pool, new_nid);
727 pool->p.nid = new_nid;
729 /* Flush pool alloc cache, as refill will check NUMA node */
730 while (pool->alloc.count) {
731 page = pool->alloc.cache[--pool->alloc.count];
732 page_pool_return_page(pool, page);
735 EXPORT_SYMBOL(page_pool_update_nid);
737 bool page_pool_return_skb_page(struct page *page)
739 struct page_pool *pp;
741 page = compound_head(page);
743 /* page->pp_magic is OR'ed with PP_SIGNATURE after the allocation
744 * in order to preserve any existing bits, such as bit 0 for the
745 * head page of compound page and bit 1 for pfmemalloc page, so
746 * mask those bits for freeing side when doing below checking,
747 * and page_is_pfmemalloc() is checked in __page_pool_put_page()
748 * to avoid recycling the pfmemalloc page.
750 if (unlikely((page->pp_magic & ~0x3UL) != PP_SIGNATURE))
755 /* Driver set this to memory recycling info. Reset it on recycle.
756 * This will *not* work for NIC using a split-page memory model.
757 * The page will be returned to the pool here regardless of the
758 * 'flipped' fragment being in use or not.
760 page_pool_put_full_page(pp, page, false);
764 EXPORT_SYMBOL(page_pool_return_skb_page);