xref: /linux/include/net/netmem.h (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  *	Network memory
4  *
5  *	Author:	Mina Almasry <almasrymina@google.com>
6  */
7 
8 #ifndef _NET_NETMEM_H
9 #define _NET_NETMEM_H
10 
11 #include <linux/dma-mapping.h>
12 #include <linux/mm.h>
13 #include <net/net_debug.h>
14 
15 /* net_iov */
16 
17 DECLARE_STATIC_KEY_FALSE(page_pool_mem_providers);
18 
19 /*  We overload the LSB of the struct page pointer to indicate whether it's
20  *  a page or net_iov.
21  */
22 #define NET_IOV 0x01UL
23 
24 enum net_iov_type {
25 	NET_IOV_DMABUF,
26 	NET_IOV_IOURING,
27 
28 	/* Force size to unsigned long to make the NET_IOV_ASSERTS below pass.
29 	 */
30 	NET_IOV_MAX = ULONG_MAX
31 };
32 
33 struct net_iov {
34 	enum net_iov_type type;
35 	unsigned long pp_magic;
36 	struct page_pool *pp;
37 	struct net_iov_area *owner;
38 	unsigned long dma_addr;
39 	atomic_long_t pp_ref_count;
40 };
41 
42 struct net_iov_area {
43 	/* Array of net_iovs for this area. */
44 	struct net_iov *niovs;
45 	size_t num_niovs;
46 
47 	/* Offset into the dma-buf where this chunk starts.  */
48 	unsigned long base_virtual;
49 };
50 
51 /* These fields in struct page are used by the page_pool and net stack:
52  *
53  *        struct {
54  *                unsigned long pp_magic;
55  *                struct page_pool *pp;
56  *                unsigned long _pp_mapping_pad;
57  *                unsigned long dma_addr;
58  *                atomic_long_t pp_ref_count;
59  *        };
60  *
61  * We mirror the page_pool fields here so the page_pool can access these fields
62  * without worrying whether the underlying fields belong to a page or net_iov.
63  *
64  * The non-net stack fields of struct page are private to the mm stack and must
65  * never be mirrored to net_iov.
66  */
67 #define NET_IOV_ASSERT_OFFSET(pg, iov)             \
68 	static_assert(offsetof(struct page, pg) == \
69 		      offsetof(struct net_iov, iov))
70 NET_IOV_ASSERT_OFFSET(pp_magic, pp_magic);
71 NET_IOV_ASSERT_OFFSET(pp, pp);
72 NET_IOV_ASSERT_OFFSET(dma_addr, dma_addr);
73 NET_IOV_ASSERT_OFFSET(pp_ref_count, pp_ref_count);
74 #undef NET_IOV_ASSERT_OFFSET
75 
76 static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)
77 {
78 	return niov->owner;
79 }
80 
81 static inline unsigned int net_iov_idx(const struct net_iov *niov)
82 {
83 	return niov - net_iov_owner(niov)->niovs;
84 }
85 
86 /* netmem */
87 
88 /**
89  * typedef netmem_ref - a nonexistent type marking a reference to generic
90  * network memory.
91  *
92  * A netmem_ref can be a struct page* or a struct net_iov* underneath.
93  *
94  * Use the supplied helpers to obtain the underlying memory pointer and fields.
95  */
96 typedef unsigned long __bitwise netmem_ref;
97 
98 static inline bool netmem_is_net_iov(const netmem_ref netmem)
99 {
100 	return (__force unsigned long)netmem & NET_IOV;
101 }
102 
103 /**
104  * __netmem_to_page - unsafely get pointer to the &page backing @netmem
105  * @netmem: netmem reference to convert
106  *
107  * Unsafe version of netmem_to_page(). When @netmem is always page-backed,
108  * e.g. when it's a header buffer, performs faster and generates smaller
109  * object code (no check for the LSB, no WARN). When @netmem points to IOV,
110  * provokes undefined behaviour.
111  *
112  * Return: pointer to the &page (garbage if @netmem is not page-backed).
113  */
114 static inline struct page *__netmem_to_page(netmem_ref netmem)
115 {
116 	return (__force struct page *)netmem;
117 }
118 
119 static inline struct page *netmem_to_page(netmem_ref netmem)
120 {
121 	if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
122 		return NULL;
123 
124 	return __netmem_to_page(netmem);
125 }
126 
127 static inline struct net_iov *netmem_to_net_iov(netmem_ref netmem)
128 {
129 	if (netmem_is_net_iov(netmem))
130 		return (struct net_iov *)((__force unsigned long)netmem &
131 					  ~NET_IOV);
132 
133 	DEBUG_NET_WARN_ON_ONCE(true);
134 	return NULL;
135 }
136 
137 static inline netmem_ref net_iov_to_netmem(struct net_iov *niov)
138 {
139 	return (__force netmem_ref)((unsigned long)niov | NET_IOV);
140 }
141 
142 static inline netmem_ref page_to_netmem(const struct page *page)
143 {
144 	return (__force netmem_ref)page;
145 }
146 
147 /**
148  * virt_to_netmem - convert virtual memory pointer to a netmem reference
149  * @data: host memory pointer to convert
150  *
151  * Return: netmem reference to the &page backing this virtual address.
152  */
153 static inline netmem_ref virt_to_netmem(const void *data)
154 {
155 	return page_to_netmem(virt_to_page(data));
156 }
157 
158 static inline int netmem_ref_count(netmem_ref netmem)
159 {
160 	/* The non-pp refcount of net_iov is always 1. On net_iov, we only
161 	 * support pp refcounting which uses the pp_ref_count field.
162 	 */
163 	if (netmem_is_net_iov(netmem))
164 		return 1;
165 
166 	return page_ref_count(netmem_to_page(netmem));
167 }
168 
169 static inline unsigned long netmem_pfn_trace(netmem_ref netmem)
170 {
171 	if (netmem_is_net_iov(netmem))
172 		return 0;
173 
174 	return page_to_pfn(netmem_to_page(netmem));
175 }
176 
177 /* __netmem_clear_lsb - convert netmem_ref to struct net_iov * for access to
178  * common fields.
179  * @netmem: netmem reference to extract as net_iov.
180  *
181  * All the sub types of netmem_ref (page, net_iov) have the same pp, pp_magic,
182  * dma_addr, and pp_ref_count fields at the same offsets. Thus, we can access
183  * these fields without a type check to make sure that the underlying mem is
184  * net_iov or page.
185  *
186  * The resulting value of this function can only be used to access the fields
187  * that are NET_IOV_ASSERT_OFFSET'd. Accessing any other fields will result in
188  * undefined behavior.
189  *
190  * Return: the netmem_ref cast to net_iov* regardless of its underlying type.
191  */
192 static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
193 {
194 	return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV);
195 }
196 
197 /**
198  * __netmem_get_pp - unsafely get pointer to the &page_pool backing @netmem
199  * @netmem: netmem reference to get the pointer from
200  *
201  * Unsafe version of netmem_get_pp(). When @netmem is always page-backed,
202  * e.g. when it's a header buffer, performs faster and generates smaller
203  * object code (avoids clearing the LSB). When @netmem points to IOV,
204  * provokes invalid memory access.
205  *
206  * Return: pointer to the &page_pool (garbage if @netmem is not page-backed).
207  */
208 static inline struct page_pool *__netmem_get_pp(netmem_ref netmem)
209 {
210 	return __netmem_to_page(netmem)->pp;
211 }
212 
213 static inline struct page_pool *netmem_get_pp(netmem_ref netmem)
214 {
215 	return __netmem_clear_lsb(netmem)->pp;
216 }
217 
218 static inline atomic_long_t *netmem_get_pp_ref_count_ref(netmem_ref netmem)
219 {
220 	return &__netmem_clear_lsb(netmem)->pp_ref_count;
221 }
222 
223 static inline bool netmem_is_pref_nid(netmem_ref netmem, int pref_nid)
224 {
225 	/* NUMA node preference only makes sense if we're allocating
226 	 * system memory. Memory providers (which give us net_iovs)
227 	 * choose for us.
228 	 */
229 	if (netmem_is_net_iov(netmem))
230 		return true;
231 
232 	return page_to_nid(netmem_to_page(netmem)) == pref_nid;
233 }
234 
235 static inline netmem_ref netmem_compound_head(netmem_ref netmem)
236 {
237 	/* niov are never compounded */
238 	if (netmem_is_net_iov(netmem))
239 		return netmem;
240 
241 	return page_to_netmem(compound_head(netmem_to_page(netmem)));
242 }
243 
244 /**
245  * __netmem_address - unsafely get pointer to the memory backing @netmem
246  * @netmem: netmem reference to get the pointer for
247  *
248  * Unsafe version of netmem_address(). When @netmem is always page-backed,
249  * e.g. when it's a header buffer, performs faster and generates smaller
250  * object code (no check for the LSB). When @netmem points to IOV, provokes
251  * undefined behaviour.
252  *
253  * Return: pointer to the memory (garbage if @netmem is not page-backed).
254  */
255 static inline void *__netmem_address(netmem_ref netmem)
256 {
257 	return page_address(__netmem_to_page(netmem));
258 }
259 
260 static inline void *netmem_address(netmem_ref netmem)
261 {
262 	if (netmem_is_net_iov(netmem))
263 		return NULL;
264 
265 	return __netmem_address(netmem);
266 }
267 
268 /**
269  * netmem_is_pfmemalloc - check if @netmem was allocated under memory pressure
270  * @netmem: netmem reference to check
271  *
272  * Return: true if @netmem is page-backed and the page was allocated under
273  * memory pressure, false otherwise.
274  */
275 static inline bool netmem_is_pfmemalloc(netmem_ref netmem)
276 {
277 	if (netmem_is_net_iov(netmem))
278 		return false;
279 
280 	return page_is_pfmemalloc(netmem_to_page(netmem));
281 }
282 
283 static inline unsigned long netmem_get_dma_addr(netmem_ref netmem)
284 {
285 	return __netmem_clear_lsb(netmem)->dma_addr;
286 }
287 
288 void get_netmem(netmem_ref netmem);
289 void put_netmem(netmem_ref netmem);
290 
291 #define netmem_dma_unmap_addr_set(NETMEM, PTR, ADDR_NAME, VAL)   \
292 	do {                                                     \
293 		if (!netmem_is_net_iov(NETMEM))                  \
294 			dma_unmap_addr_set(PTR, ADDR_NAME, VAL); \
295 		else                                             \
296 			dma_unmap_addr_set(PTR, ADDR_NAME, 0);   \
297 	} while (0)
298 
299 static inline void netmem_dma_unmap_page_attrs(struct device *dev,
300 					       dma_addr_t addr, size_t size,
301 					       enum dma_data_direction dir,
302 					       unsigned long attrs)
303 {
304 	if (!addr)
305 		return;
306 
307 	dma_unmap_page_attrs(dev, addr, size, dir, attrs);
308 }
309 
310 #endif /* _NET_NETMEM_H */
311