1 /* SPDX-License-Identifier: GPL-2.0
4 * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
5 * Copyright (C) 2016 Red Hat, Inc.
9 * DOC: page_pool allocator
11 * This page_pool allocator is optimized for the XDP mode that
12 * uses one-frame-per-page, but have fallbacks that act like the
13 * regular page allocator APIs.
15 * Basic use involve replacing alloc_pages() calls with the
16 * page_pool_alloc_pages() call. Drivers should likely use
17 * page_pool_dev_alloc_pages() replacing dev_alloc_pages().
19 * If page_pool handles DMA mapping (use page->private), then API user
20 * is responsible for invoking page_pool_put_page() once. In-case of
21 * elevated refcnt, the DMA state is released, assuming other users of
22 * the page will eventually call put_page().
24 * If no DMA mapping is done, then it can act as shim-layer that
25 * fall-through to alloc_page. As no state is kept on the page, the
26 * regular put_page() call is sufficient.
28 #ifndef _NET_PAGE_POOL_H
29 #define _NET_PAGE_POOL_H
31 #include <linux/mm.h> /* Needed by ptr_ring */
32 #include <linux/ptr_ring.h>
33 #include <linux/dma-direction.h>
35 #define PP_FLAG_DMA_MAP 1 /* Should page_pool do the DMA map/unmap */
36 #define PP_FLAG_ALL PP_FLAG_DMA_MAP
39 * Fast allocation side cache array/stack
41 * The cache size and refill watermark is related to the network
42 * use-case. The NAPI budget is 64 packets. After a NAPI poll the RX
43 * ring is usually refilled and the max consumed elements will be 64,
44 * thus a natural max size of objects needed in the cache.
46 * Keeping room for more objects, is due to XDP_DROP use-case. As
47 * XDP_DROP allows the opportunity to recycle objects directly into
48 * this array, as it shares the same softirq/NAPI protection. If
49 * cache is already full (or partly full) then the XDP_DROP recycles
50 * would have to take a slower code path.
52 #define PP_ALLOC_CACHE_SIZE 128
53 #define PP_ALLOC_CACHE_REFILL 64
54 struct pp_alloc_cache {
56 void *cache[PP_ALLOC_CACHE_SIZE];
59 struct page_pool_params {
62 unsigned int pool_size;
63 int nid; /* Numa node id to allocate from pages from */
64 struct device *dev; /* device, for DMA pre-mapping purposes */
65 enum dma_data_direction dma_dir; /* DMA mapping direction */
70 struct page_pool_params p;
73 * Data structure for allocation side
75 * Drivers allocation side usually already perform some kind
76 * of resource protection. Piggyback on this protection, and
77 * require driver to protect allocation side.
79 * For NIC drivers this means, allocate a page_pool per
80 * RX-queue. As the RX-queue is already protected by
81 * Softirq/BH scheduling and napi_schedule. NAPI schedule
82 * guarantee that a single napi_struct will only be scheduled
83 * on a single CPU (see napi_schedule).
85 struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
87 /* Data structure for storing recycled pages.
89 * Returning/freeing pages is more complicated synchronization
90 * wise, because free's can happen on remote CPUs, with no
91 * association with allocation resource.
93 * Use ptr_ring, as it separates consumer and producer
94 * effeciently, it a way that doesn't bounce cache-lines.
96 * TODO: Implement bulk return pages into this structure.
101 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
103 static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
105 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
107 return page_pool_alloc_pages(pool, gfp);
110 struct page_pool *page_pool_create(const struct page_pool_params *params);
112 void page_pool_destroy(struct page_pool *pool);
114 /* Never call this directly, use helpers below */
115 void __page_pool_put_page(struct page_pool *pool,
116 struct page *page, bool allow_direct);
118 static inline void page_pool_put_page(struct page_pool *pool,
119 struct page *page, bool allow_direct)
121 /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
122 * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
124 #ifdef CONFIG_PAGE_POOL
125 __page_pool_put_page(pool, page, allow_direct);
128 /* Very limited use-cases allow recycle direct */
129 static inline void page_pool_recycle_direct(struct page_pool *pool,
132 __page_pool_put_page(pool, page, true);
135 static inline bool is_page_pool_compiled_in(void)
137 #ifdef CONFIG_PAGE_POOL
144 #endif /* _NET_PAGE_POOL_H */