]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/net/page_pool.h
mlx5: more strict use of page_pool API
[mirror_ubuntu-jammy-kernel.git] / include / net / page_pool.h
CommitLineData
ff7d6b27
JDB
1/* SPDX-License-Identifier: GPL-2.0
2 *
3 * page_pool.h
4 * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com>
5 * Copyright (C) 2016 Red Hat, Inc.
6 */
7
8/**
9 * DOC: page_pool allocator
10 *
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.
14 *
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().
18 *
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().
23 *
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.
27 */
28#ifndef _NET_PAGE_POOL_H
29#define _NET_PAGE_POOL_H
30
31#include <linux/mm.h> /* Needed by ptr_ring */
32#include <linux/ptr_ring.h>
33#include <linux/dma-direction.h>
34
35#define PP_FLAG_DMA_MAP 1 /* Should page_pool do the DMA map/unmap */
36#define PP_FLAG_ALL PP_FLAG_DMA_MAP
37
38/*
39 * Fast allocation side cache array/stack
40 *
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.
45 *
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.
51 */
52#define PP_ALLOC_CACHE_SIZE 128
53#define PP_ALLOC_CACHE_REFILL 64
54struct pp_alloc_cache {
55 u32 count;
56 void *cache[PP_ALLOC_CACHE_SIZE];
57};
58
59struct page_pool_params {
60 unsigned int flags;
61 unsigned int order;
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 */
66};
67
68struct page_pool {
69 struct rcu_head rcu;
70 struct page_pool_params p;
71
72 /*
73 * Data structure for allocation side
74 *
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.
78 *
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).
84 */
85 struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
86
87 /* Data structure for storing recycled pages.
88 *
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.
92 *
93 * Use ptr_ring, as it separates consumer and producer
94 * effeciently, it a way that doesn't bounce cache-lines.
95 *
96 * TODO: Implement bulk return pages into this structure.
97 */
98 struct ptr_ring ring;
99};
100
101struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
102
103static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool)
104{
105 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN);
106
107 return page_pool_alloc_pages(pool, gfp);
108}
109
110struct page_pool *page_pool_create(const struct page_pool_params *params);
111
112void page_pool_destroy(struct page_pool *pool);
113
e54cfd7e
JDB
114void __page_pool_free(struct page_pool *pool);
115static inline void page_pool_free(struct page_pool *pool)
116{
117 /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
118 * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
119 */
120#ifdef CONFIG_PAGE_POOL
121 __page_pool_free(pool);
122#endif
123}
124
ff7d6b27
JDB
125/* Never call this directly, use helpers below */
126void __page_pool_put_page(struct page_pool *pool,
127 struct page *page, bool allow_direct);
128
389ab7f0
JDB
129static inline void page_pool_put_page(struct page_pool *pool,
130 struct page *page, bool allow_direct)
ff7d6b27 131{
57d0a1c1
JDB
132 /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
133 * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
134 */
135#ifdef CONFIG_PAGE_POOL
389ab7f0 136 __page_pool_put_page(pool, page, allow_direct);
57d0a1c1 137#endif
ff7d6b27
JDB
138}
139/* Very limited use-cases allow recycle direct */
140static inline void page_pool_recycle_direct(struct page_pool *pool,
141 struct page *page)
142{
143 __page_pool_put_page(pool, page, true);
144}
145
6bf071bf
JDB
146/* Disconnects a page (from a page_pool). API users can have a need
147 * to disconnect a page (from a page_pool), to allow it to be used as
148 * a regular page (that will eventually be returned to the normal
149 * page-allocator via put_page).
150 */
151void page_pool_unmap_page(struct page_pool *pool, struct page *page);
152static inline void page_pool_release_page(struct page_pool *pool,
153 struct page *page)
154{
155#ifdef CONFIG_PAGE_POOL
156 page_pool_unmap_page(pool, page);
157#endif
158}
159
0afdeeed
IA
160static inline dma_addr_t page_pool_get_dma_addr(struct page *page)
161{
162 return page->dma_addr;
163}
164
57d0a1c1
JDB
165static inline bool is_page_pool_compiled_in(void)
166{
167#ifdef CONFIG_PAGE_POOL
168 return true;
169#else
170 return false;
171#endif
172}
173
ff7d6b27 174#endif /* _NET_PAGE_POOL_H */