1 /* SPDX-License-Identifier: GPL-2.0
5 * Author: Mina Almasry <almasrymina@google.com>
12 #include <net/net_debug.h>
16 DECLARE_STATIC_KEY_FALSE(page_pool_mem_providers);
18 /* We overload the LSB of the struct page pointer to indicate whether it's
21 #define NET_IOV 0x01UL
24 unsigned long __unused_padding;
25 unsigned long pp_magic;
27 struct dmabuf_genpool_chunk_owner *owner;
28 unsigned long dma_addr;
29 atomic_long_t pp_ref_count;
32 /* These fields in struct page are used by the page_pool and net stack:
35 * unsigned long pp_magic;
36 * struct page_pool *pp;
37 * unsigned long _pp_mapping_pad;
38 * unsigned long dma_addr;
39 * atomic_long_t pp_ref_count;
42 * We mirror the page_pool fields here so the page_pool can access these fields
43 * without worrying whether the underlying fields belong to a page or net_iov.
45 * The non-net stack fields of struct page are private to the mm stack and must
46 * never be mirrored to net_iov.
48 #define NET_IOV_ASSERT_OFFSET(pg, iov) \
49 static_assert(offsetof(struct page, pg) == \
50 offsetof(struct net_iov, iov))
51 NET_IOV_ASSERT_OFFSET(pp_magic, pp_magic);
52 NET_IOV_ASSERT_OFFSET(pp, pp);
53 NET_IOV_ASSERT_OFFSET(dma_addr, dma_addr);
54 NET_IOV_ASSERT_OFFSET(pp_ref_count, pp_ref_count);
55 #undef NET_IOV_ASSERT_OFFSET
60 * typedef netmem_ref - a nonexistent type marking a reference to generic
63 * A netmem_ref currently is always a reference to a struct page. This
64 * abstraction is introduced so support for new memory types can be added.
66 * Use the supplied helpers to obtain the underlying memory pointer and fields.
68 typedef unsigned long __bitwise netmem_ref;
70 static inline bool netmem_is_net_iov(const netmem_ref netmem)
72 return (__force unsigned long)netmem & NET_IOV;
76 * __netmem_to_page - unsafely get pointer to the &page backing @netmem
77 * @netmem: netmem reference to convert
79 * Unsafe version of netmem_to_page(). When @netmem is always page-backed,
80 * e.g. when it's a header buffer, performs faster and generates smaller
81 * object code (no check for the LSB, no WARN). When @netmem points to IOV,
82 * provokes undefined behaviour.
84 * Return: pointer to the &page (garbage if @netmem is not page-backed).
86 static inline struct page *__netmem_to_page(netmem_ref netmem)
88 return (__force struct page *)netmem;
91 /* This conversion fails (returns NULL) if the netmem_ref is not struct page
94 static inline struct page *netmem_to_page(netmem_ref netmem)
96 if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
99 return __netmem_to_page(netmem);
102 static inline struct net_iov *netmem_to_net_iov(netmem_ref netmem)
104 if (netmem_is_net_iov(netmem))
105 return (struct net_iov *)((__force unsigned long)netmem &
108 DEBUG_NET_WARN_ON_ONCE(true);
112 static inline netmem_ref net_iov_to_netmem(struct net_iov *niov)
114 return (__force netmem_ref)((unsigned long)niov | NET_IOV);
117 static inline netmem_ref page_to_netmem(struct page *page)
119 return (__force netmem_ref)page;
123 * virt_to_netmem - convert virtual memory pointer to a netmem reference
124 * @data: host memory pointer to convert
126 * Return: netmem reference to the &page backing this virtual address.
128 static inline netmem_ref virt_to_netmem(const void *data)
130 return page_to_netmem(virt_to_page(data));
133 static inline int netmem_ref_count(netmem_ref netmem)
135 /* The non-pp refcount of net_iov is always 1. On net_iov, we only
136 * support pp refcounting which uses the pp_ref_count field.
138 if (netmem_is_net_iov(netmem))
141 return page_ref_count(netmem_to_page(netmem));
144 static inline unsigned long netmem_pfn_trace(netmem_ref netmem)
146 if (netmem_is_net_iov(netmem))
149 return page_to_pfn(netmem_to_page(netmem));
152 static inline struct net_iov *__netmem_clear_lsb(netmem_ref netmem)
154 return (struct net_iov *)((__force unsigned long)netmem & ~NET_IOV);
158 * __netmem_get_pp - unsafely get pointer to the &page_pool backing @netmem
159 * @netmem: netmem reference to get the pointer from
161 * Unsafe version of netmem_get_pp(). When @netmem is always page-backed,
162 * e.g. when it's a header buffer, performs faster and generates smaller
163 * object code (avoids clearing the LSB). When @netmem points to IOV,
164 * provokes invalid memory access.
166 * Return: pointer to the &page_pool (garbage if @netmem is not page-backed).
168 static inline struct page_pool *__netmem_get_pp(netmem_ref netmem)
170 return __netmem_to_page(netmem)->pp;
173 static inline struct page_pool *netmem_get_pp(netmem_ref netmem)
175 return __netmem_clear_lsb(netmem)->pp;
178 static inline atomic_long_t *netmem_get_pp_ref_count_ref(netmem_ref netmem)
180 return &__netmem_clear_lsb(netmem)->pp_ref_count;
183 static inline bool netmem_is_pref_nid(netmem_ref netmem, int pref_nid)
185 /* NUMA node preference only makes sense if we're allocating
186 * system memory. Memory providers (which give us net_iovs)
189 if (netmem_is_net_iov(netmem))
192 return page_to_nid(netmem_to_page(netmem)) == pref_nid;
195 static inline netmem_ref netmem_compound_head(netmem_ref netmem)
197 /* niov are never compounded */
198 if (netmem_is_net_iov(netmem))
201 return page_to_netmem(compound_head(netmem_to_page(netmem)));
205 * __netmem_address - unsafely get pointer to the memory backing @netmem
206 * @netmem: netmem reference to get the pointer for
208 * Unsafe version of netmem_address(). When @netmem is always page-backed,
209 * e.g. when it's a header buffer, performs faster and generates smaller
210 * object code (no check for the LSB). When @netmem points to IOV, provokes
211 * undefined behaviour.
213 * Return: pointer to the memory (garbage if @netmem is not page-backed).
215 static inline void *__netmem_address(netmem_ref netmem)
217 return page_address(__netmem_to_page(netmem));
220 static inline void *netmem_address(netmem_ref netmem)
222 if (netmem_is_net_iov(netmem))
225 return __netmem_address(netmem);
229 * netmem_is_pfmemalloc - check if @netmem was allocated under memory pressure
230 * @netmem: netmem reference to check
232 * Return: true if @netmem is page-backed and the page was allocated under
233 * memory pressure, false otherwise.
235 static inline bool netmem_is_pfmemalloc(netmem_ref netmem)
237 if (netmem_is_net_iov(netmem))
240 return page_is_pfmemalloc(netmem_to_page(netmem));
243 static inline unsigned long netmem_get_dma_addr(netmem_ref netmem)
245 return __netmem_clear_lsb(netmem)->dma_addr;
248 #endif /* _NET_NETMEM_H */