xref: /linux/include/net/netmem.h (revision bdfa82f5b8998a6311a8ef0cf89ad413f5cd9ea4)
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 currently is always a reference to a struct page. This
93  * abstraction is introduced so support for new memory types can be added.
94  *
95  * Use the supplied helpers to obtain the underlying memory pointer and fields.
96  */
97 typedef unsigned long __bitwise netmem_ref;
98 
99 static inline bool netmem_is_net_iov(const netmem_ref netmem)
100 {
101 	return (__force unsigned long)netmem & NET_IOV;
102 }
103 
104 /**
105  * __netmem_to_page - unsafely get pointer to the &page backing @netmem
106  * @netmem: netmem reference to convert
107  *
108  * Unsafe version of netmem_to_page(). When @netmem is always page-backed,
109  * e.g. when it's a header buffer, performs faster and generates smaller
110  * object code (no check for the LSB, no WARN). When @netmem points to IOV,
111  * provokes undefined behaviour.
112  *
113  * Return: pointer to the &page (garbage if @netmem is not page-backed).
114  */
115 static inline struct page *__netmem_to_page(netmem_ref netmem)
116 {
117 	return (__force struct page *)netmem;
118 }
119 
120 /* This conversion fails (returns NULL) if the netmem_ref is not struct page
121  * backed.
122  */
123 static inline struct page *netmem_to_page(netmem_ref netmem)
124 {
125 	if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
126 		return NULL;
127 
128 	return __netmem_to_page(netmem);
129 }
130 
131 static inline struct net_iov *netmem_to_net_iov(netmem_ref netmem)
132 {
133 	if (netmem_is_net_iov(netmem))
134 		return (struct net_iov *)((__force unsigned long)netmem &
135 					  ~NET_IOV);
136 
137 	DEBUG_NET_WARN_ON_ONCE(true);
138 	return NULL;
139 }
140 
141 static inline netmem_ref net_iov_to_netmem(struct net_iov *niov)
142 {
143 	return (__force netmem_ref)((unsigned long)niov | NET_IOV);
144 }
145 
146 static inline netmem_ref page_to_netmem(struct page *page)
147 {
148 	return (__force netmem_ref)page;
149 }
150 
151 /**
152  * virt_to_netmem - convert virtual memory pointer to a netmem reference
153  * @data: host memory pointer to convert
154  *
155  * Return: netmem reference to the &page backing this virtual address.
156  */
157 static inline netmem_ref virt_to_netmem(const void *data)
158 {
159 	return page_to_netmem(virt_to_page(data));
160 }
161 
162 static inline int netmem_ref_count(netmem_ref netmem)
163 {
164 	/* The non-pp refcount of net_iov is always 1. On net_iov, we only
165 	 * support pp refcounting which uses the pp_ref_count field.
166 	 */
167 	if (netmem_is_net_iov(netmem))
168 		return 1;
169 
170 	return page_ref_count(netmem_to_page(netmem));
171 }
172 
173 static inline unsigned long netmem_pfn_trace(netmem_ref netmem)
174 {
175 	if (netmem_is_net_iov(netmem))
176 		return 0;
177 
178 	return page_to_pfn(netmem_to_page(netmem));
179 }
180 
181 static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
182 {
183 	return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV);
184 }
185 
186 /**
187  * __netmem_get_pp - unsafely get pointer to the &page_pool backing @netmem
188  * @netmem: netmem reference to get the pointer from
189  *
190  * Unsafe version of netmem_get_pp(). When @netmem is always page-backed,
191  * e.g. when it's a header buffer, performs faster and generates smaller
192  * object code (avoids clearing the LSB). When @netmem points to IOV,
193  * provokes invalid memory access.
194  *
195  * Return: pointer to the &page_pool (garbage if @netmem is not page-backed).
196  */
197 static inline struct page_pool *__netmem_get_pp(netmem_ref netmem)
198 {
199 	return __netmem_to_page(netmem)->pp;
200 }
201 
202 static inline struct page_pool *netmem_get_pp(netmem_ref netmem)
203 {
204 	return __netmem_clear_lsb(netmem)->pp;
205 }
206 
207 static inline atomic_long_t *netmem_get_pp_ref_count_ref(netmem_ref netmem)
208 {
209 	return &__netmem_clear_lsb(netmem)->pp_ref_count;
210 }
211 
212 static inline bool netmem_is_pref_nid(netmem_ref netmem, int pref_nid)
213 {
214 	/* NUMA node preference only makes sense if we're allocating
215 	 * system memory. Memory providers (which give us net_iovs)
216 	 * choose for us.
217 	 */
218 	if (netmem_is_net_iov(netmem))
219 		return true;
220 
221 	return page_to_nid(netmem_to_page(netmem)) == pref_nid;
222 }
223 
224 static inline netmem_ref netmem_compound_head(netmem_ref netmem)
225 {
226 	/* niov are never compounded */
227 	if (netmem_is_net_iov(netmem))
228 		return netmem;
229 
230 	return page_to_netmem(compound_head(netmem_to_page(netmem)));
231 }
232 
233 /**
234  * __netmem_address - unsafely get pointer to the memory backing @netmem
235  * @netmem: netmem reference to get the pointer for
236  *
237  * Unsafe version of netmem_address(). When @netmem is always page-backed,
238  * e.g. when it's a header buffer, performs faster and generates smaller
239  * object code (no check for the LSB). When @netmem points to IOV, provokes
240  * undefined behaviour.
241  *
242  * Return: pointer to the memory (garbage if @netmem is not page-backed).
243  */
244 static inline void *__netmem_address(netmem_ref netmem)
245 {
246 	return page_address(__netmem_to_page(netmem));
247 }
248 
249 static inline void *netmem_address(netmem_ref netmem)
250 {
251 	if (netmem_is_net_iov(netmem))
252 		return NULL;
253 
254 	return __netmem_address(netmem);
255 }
256 
257 /**
258  * netmem_is_pfmemalloc - check if @netmem was allocated under memory pressure
259  * @netmem: netmem reference to check
260  *
261  * Return: true if @netmem is page-backed and the page was allocated under
262  * memory pressure, false otherwise.
263  */
264 static inline bool netmem_is_pfmemalloc(netmem_ref netmem)
265 {
266 	if (netmem_is_net_iov(netmem))
267 		return false;
268 
269 	return page_is_pfmemalloc(netmem_to_page(netmem));
270 }
271 
272 static inline unsigned long netmem_get_dma_addr(netmem_ref netmem)
273 {
274 	return __netmem_clear_lsb(netmem)->dma_addr;
275 }
276 
277 void get_netmem(netmem_ref netmem);
278 void put_netmem(netmem_ref netmem);
279 
280 #define netmem_dma_unmap_addr_set(NETMEM, PTR, ADDR_NAME, VAL)   \
281 	do {                                                     \
282 		if (!netmem_is_net_iov(NETMEM))                  \
283 			dma_unmap_addr_set(PTR, ADDR_NAME, VAL); \
284 		else                                             \
285 			dma_unmap_addr_set(PTR, ADDR_NAME, 0);   \
286 	} while (0)
287 
288 static inline void netmem_dma_unmap_page_attrs(struct device *dev,
289 					       dma_addr_t addr, size_t size,
290 					       enum dma_data_direction dir,
291 					       unsigned long attrs)
292 {
293 	if (!addr)
294 		return;
295 
296 	dma_unmap_page_attrs(dev, addr, size, dir, attrs);
297 }
298 
299 #endif /* _NET_NETMEM_H */
300