1 /* SPDX-License-Identifier: GPL-2.0
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 /* Refill alloc array, but only if NUMA match */
135 page = __ptr_ring_consume(r);
139 if (likely(page_to_nid(page) == pref_nid)) {
140 pool->alloc.cache[pool->alloc.count++] = page;
143 * (1) release 1 page to page-allocator and
144 * (2) break out to fallthrough to alloc_pages_node.
145 * This limit stress on page buddy alloactor.
147 page_pool_return_page(pool, page);
151 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL);
153 /* Return last page */
154 if (likely(pool->alloc.count > 0))
155 page = pool->alloc.cache[--pool->alloc.count];
161 static struct page *__page_pool_get_cached(struct page_pool *pool)
165 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */
166 if (likely(pool->alloc.count)) {
168 page = pool->alloc.cache[--pool->alloc.count];
170 page = page_pool_refill_alloc_cache(pool);
176 static void page_pool_dma_sync_for_device(struct page_pool *pool,
178 unsigned int dma_sync_size)
180 dma_addr_t dma_addr = page_pool_get_dma_addr(page);
182 dma_sync_size = min(dma_sync_size, pool->p.max_len);
183 dma_sync_single_range_for_device(pool->p.dev, dma_addr,
184 pool->p.offset, dma_sync_size,
188 static bool page_pool_dma_map(struct page_pool *pool, struct page *page)
192 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr
193 * since dma_addr_t can be either 32 or 64 bits and does not always fit
194 * into page private data (i.e 32bit cpu with 64bit DMA caps)
195 * This mapping is kept for lifetime of page, until leaving pool.
197 dma = dma_map_page_attrs(pool->p.dev, page, 0,
198 (PAGE_SIZE << pool->p.order),
199 pool->p.dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
200 if (dma_mapping_error(pool->p.dev, dma))
203 page_pool_set_dma_addr(page, dma);
205 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
206 page_pool_dma_sync_for_device(pool, page, pool->p.max_len);
211 static void page_pool_set_pp_info(struct page_pool *pool,
215 page->pp_magic |= PP_SIGNATURE;
216 if (pool->p.init_callback)
217 pool->p.init_callback(page, pool->p.init_arg);
220 static void page_pool_clear_pp_info(struct page *page)
226 static struct page *__page_pool_alloc_page_order(struct page_pool *pool,
232 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order);
236 if ((pool->p.flags & PP_FLAG_DMA_MAP) &&
237 unlikely(!page_pool_dma_map(pool, page))) {
242 page_pool_set_pp_info(pool, page);
244 /* Track how many pages are held 'in-flight' */
245 pool->pages_state_hold_cnt++;
246 trace_page_pool_state_hold(pool, page, pool->pages_state_hold_cnt);
252 static struct page *__page_pool_alloc_pages_slow(struct page_pool *pool,
255 const int bulk = PP_ALLOC_CACHE_REFILL;
256 unsigned int pp_flags = pool->p.flags;
257 unsigned int pp_order = pool->p.order;
261 /* Don't support bulk alloc for high-order pages */
262 if (unlikely(pp_order))
263 return __page_pool_alloc_page_order(pool, gfp);
265 /* Unnecessary as alloc cache is empty, but guarantees zero count */
266 if (unlikely(pool->alloc.count > 0))
267 return pool->alloc.cache[--pool->alloc.count];
269 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */
270 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk);
272 nr_pages = alloc_pages_bulk_array(gfp, bulk, pool->alloc.cache);
273 if (unlikely(!nr_pages))
276 /* Pages have been filled into alloc.cache array, but count is zero and
277 * page element have not been (possibly) DMA mapped.
279 for (i = 0; i < nr_pages; i++) {
280 page = pool->alloc.cache[i];
281 if ((pp_flags & PP_FLAG_DMA_MAP) &&
282 unlikely(!page_pool_dma_map(pool, page))) {
287 page_pool_set_pp_info(pool, page);
288 pool->alloc.cache[pool->alloc.count++] = page;
289 /* Track how many pages are held 'in-flight' */
290 pool->pages_state_hold_cnt++;
291 trace_page_pool_state_hold(pool, page,
292 pool->pages_state_hold_cnt);
295 /* Return last page */
296 if (likely(pool->alloc.count > 0))
297 page = pool->alloc.cache[--pool->alloc.count];
301 /* When page just alloc'ed is should/must have refcnt 1. */
305 /* For using page_pool replace: alloc_pages() API calls, but provide
306 * synchronization guarantee for allocation side.
308 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
312 /* Fast-path: Get a page from cache */
313 page = __page_pool_get_cached(pool);
317 /* Slow-path: cache empty, do real allocation */
318 page = __page_pool_alloc_pages_slow(pool, gfp);
321 EXPORT_SYMBOL(page_pool_alloc_pages);
323 /* Calculate distance between two u32 values, valid if distance is below 2^(31)
324 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
326 #define _distance(a, b) (s32)((a) - (b))
328 static s32 page_pool_inflight(struct page_pool *pool)
330 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
331 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
334 inflight = _distance(hold_cnt, release_cnt);
336 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
337 WARN(inflight < 0, "Negative(%d) inflight packet-pages", inflight);
342 /* Disconnects a page (from a page_pool). API users can have a need
343 * to disconnect a page (from a page_pool), to allow it to be used as
344 * a regular page (that will eventually be returned to the normal
345 * page-allocator via put_page).
347 void page_pool_release_page(struct page_pool *pool, struct page *page)
352 if (!(pool->p.flags & PP_FLAG_DMA_MAP))
353 /* Always account for inflight pages, even if we didn't
358 dma = page_pool_get_dma_addr(page);
360 /* When page is unmapped, it cannot be returned to our pool */
361 dma_unmap_page_attrs(pool->p.dev, dma,
362 PAGE_SIZE << pool->p.order, pool->p.dma_dir,
363 DMA_ATTR_SKIP_CPU_SYNC);
364 page_pool_set_dma_addr(page, 0);
366 page_pool_clear_pp_info(page);
368 /* This may be the last page returned, releasing the pool, so
369 * it is not safe to reference pool afterwards.
371 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt);
372 trace_page_pool_state_release(pool, page, count);
374 EXPORT_SYMBOL(page_pool_release_page);
376 /* Return a page to the page allocator, cleaning up our state */
377 static void page_pool_return_page(struct page_pool *pool, struct page *page)
379 page_pool_release_page(pool, page);
382 /* An optimization would be to call __free_pages(page, pool->p.order)
383 * knowing page is not part of page-cache (thus avoiding a
384 * __page_cache_release() call).
388 static bool page_pool_recycle_in_ring(struct page_pool *pool, struct page *page)
391 /* BH protection not needed if current is serving softirq */
392 if (in_serving_softirq())
393 ret = ptr_ring_produce(&pool->ring, page);
395 ret = ptr_ring_produce_bh(&pool->ring, page);
397 return (ret == 0) ? true : false;
400 /* Only allow direct recycling in special circumstances, into the
401 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case.
403 * Caller must provide appropriate safe context.
405 static bool page_pool_recycle_in_cache(struct page *page,
406 struct page_pool *pool)
408 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE))
411 /* Caller MUST have verified/know (page_ref_count(page) == 1) */
412 pool->alloc.cache[pool->alloc.count++] = page;
416 /* If the page refcnt == 1, this will try to recycle the page.
417 * if PP_FLAG_DMA_SYNC_DEV is set, we'll try to sync the DMA area for
418 * the configured size min(dma_sync_size, pool->max_len).
419 * If the page refcnt != 1, then the page will be returned to memory
422 static __always_inline struct page *
423 __page_pool_put_page(struct page_pool *pool, struct page *page,
424 unsigned int dma_sync_size, bool allow_direct)
426 /* It is not the last user for the page frag case */
427 if (pool->p.flags & PP_FLAG_PAGE_FRAG &&
428 page_pool_atomic_sub_frag_count_return(page, 1))
431 /* This allocator is optimized for the XDP mode that uses
432 * one-frame-per-page, but have fallbacks that act like the
433 * regular page allocator APIs.
435 * refcnt == 1 means page_pool owns page, and can recycle it.
437 * page is NOT reusable when allocated when system is under
438 * some pressure. (page_is_pfmemalloc)
440 if (likely(page_ref_count(page) == 1 && !page_is_pfmemalloc(page))) {
441 /* Read barrier done in page_ref_count / READ_ONCE */
443 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
444 page_pool_dma_sync_for_device(pool, page,
447 if (allow_direct && in_serving_softirq() &&
448 page_pool_recycle_in_cache(page, pool))
451 /* Page found as candidate for recycling */
454 /* Fallback/non-XDP mode: API user have elevated refcnt.
456 * Many drivers split up the page into fragments, and some
457 * want to keep doing this to save memory and do refcnt based
458 * recycling. Support this use case too, to ease drivers
459 * switching between XDP/non-XDP.
461 * In-case page_pool maintains the DMA mapping, API user must
462 * call page_pool_put_page once. In this elevated refcnt
463 * case, the DMA is unmapped/released, as driver is likely
464 * doing refcnt based recycle tricks, meaning another process
465 * will be invoking put_page.
467 /* Do not replace this with page_pool_return_page() */
468 page_pool_release_page(pool, page);
474 void page_pool_put_page(struct page_pool *pool, struct page *page,
475 unsigned int dma_sync_size, bool allow_direct)
477 page = __page_pool_put_page(pool, page, dma_sync_size, allow_direct);
478 if (page && !page_pool_recycle_in_ring(pool, page)) {
479 /* Cache full, fallback to free pages */
480 page_pool_return_page(pool, page);
483 EXPORT_SYMBOL(page_pool_put_page);
485 /* Caller must not use data area after call, as this function overwrites it */
486 void page_pool_put_page_bulk(struct page_pool *pool, void **data,
491 for (i = 0; i < count; i++) {
492 struct page *page = virt_to_head_page(data[i]);
494 page = __page_pool_put_page(pool, page, -1, false);
495 /* Approved for bulk recycling in ptr_ring cache */
497 data[bulk_len++] = page;
500 if (unlikely(!bulk_len))
503 /* Bulk producer into ptr_ring page_pool cache */
504 page_pool_ring_lock(pool);
505 for (i = 0; i < bulk_len; i++) {
506 if (__ptr_ring_produce(&pool->ring, data[i]))
507 break; /* ring full */
509 page_pool_ring_unlock(pool);
511 /* Hopefully all pages was return into ptr_ring */
512 if (likely(i == bulk_len))
515 /* ptr_ring cache full, free remaining pages outside producer lock
516 * since put_page() with refcnt == 1 can be an expensive operation
518 for (; i < bulk_len; i++)
519 page_pool_return_page(pool, data[i]);
521 EXPORT_SYMBOL(page_pool_put_page_bulk);
523 static struct page *page_pool_drain_frag(struct page_pool *pool,
526 long drain_count = BIAS_MAX - pool->frag_users;
528 /* Some user is still using the page frag */
529 if (likely(page_pool_atomic_sub_frag_count_return(page,
533 if (page_ref_count(page) == 1 && !page_is_pfmemalloc(page)) {
534 if (pool->p.flags & PP_FLAG_DMA_SYNC_DEV)
535 page_pool_dma_sync_for_device(pool, page, -1);
540 page_pool_return_page(pool, page);
544 static void page_pool_free_frag(struct page_pool *pool)
546 long drain_count = BIAS_MAX - pool->frag_users;
547 struct page *page = pool->frag_page;
549 pool->frag_page = NULL;
552 page_pool_atomic_sub_frag_count_return(page, drain_count))
555 page_pool_return_page(pool, page);
558 struct page *page_pool_alloc_frag(struct page_pool *pool,
559 unsigned int *offset,
560 unsigned int size, gfp_t gfp)
562 unsigned int max_size = PAGE_SIZE << pool->p.order;
563 struct page *page = pool->frag_page;
565 if (WARN_ON(!(pool->p.flags & PP_FLAG_PAGE_FRAG) ||
569 size = ALIGN(size, dma_get_cache_alignment());
570 *offset = pool->frag_offset;
572 if (page && *offset + size > max_size) {
573 page = page_pool_drain_frag(pool, page);
579 page = page_pool_alloc_pages(pool, gfp);
580 if (unlikely(!page)) {
581 pool->frag_page = NULL;
585 pool->frag_page = page;
588 pool->frag_users = 1;
590 pool->frag_offset = size;
591 page_pool_set_frag_count(page, BIAS_MAX);
596 pool->frag_offset = *offset + size;
599 EXPORT_SYMBOL(page_pool_alloc_frag);
601 static void page_pool_empty_ring(struct page_pool *pool)
605 /* Empty recycle ring */
606 while ((page = ptr_ring_consume_bh(&pool->ring))) {
607 /* Verify the refcnt invariant of cached pages */
608 if (!(page_ref_count(page) == 1))
609 pr_crit("%s() page_pool refcnt %d violation\n",
610 __func__, page_ref_count(page));
612 page_pool_return_page(pool, page);
616 static void page_pool_free(struct page_pool *pool)
618 if (pool->disconnect)
619 pool->disconnect(pool);
621 ptr_ring_cleanup(&pool->ring, NULL);
623 if (pool->p.flags & PP_FLAG_DMA_MAP)
624 put_device(pool->p.dev);
629 static void page_pool_empty_alloc_cache_once(struct page_pool *pool)
633 if (pool->destroy_cnt)
636 /* Empty alloc cache, assume caller made sure this is
637 * no-longer in use, and page_pool_alloc_pages() cannot be
640 while (pool->alloc.count) {
641 page = pool->alloc.cache[--pool->alloc.count];
642 page_pool_return_page(pool, page);
646 static void page_pool_scrub(struct page_pool *pool)
648 page_pool_empty_alloc_cache_once(pool);
651 /* No more consumers should exist, but producers could still
654 page_pool_empty_ring(pool);
657 static int page_pool_release(struct page_pool *pool)
661 page_pool_scrub(pool);
662 inflight = page_pool_inflight(pool);
664 page_pool_free(pool);
669 static void page_pool_release_retry(struct work_struct *wq)
671 struct delayed_work *dwq = to_delayed_work(wq);
672 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw);
675 inflight = page_pool_release(pool);
679 /* Periodic warning */
680 if (time_after_eq(jiffies, pool->defer_warn)) {
681 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ;
683 pr_warn("%s() stalled pool shutdown %d inflight %d sec\n",
684 __func__, inflight, sec);
685 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL;
688 /* Still not ready to be disconnected, retry later */
689 schedule_delayed_work(&pool->release_dw, DEFER_TIME);
692 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
693 struct xdp_mem_info *mem)
695 refcount_inc(&pool->user_cnt);
696 pool->disconnect = disconnect;
697 pool->xdp_mem_id = mem->id;
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);