Lines Matching +full:page +full:- +full:size
1 /* SPDX-License-Identifier: GPL-2.0
11 * The page_pool allocator is optimized for recycling page or page fragment used
15 * which allocate memory with or without page splitting depending on the
16 * requested memory size.
19 * always smaller than half a page, it can use one of the more specific API
22 * 1. page_pool_alloc_pages(): allocate memory without page splitting when
23 * driver knows that the memory it need is always bigger than half of the page
24 * allocated from page pool. There is no cache line dirtying for 'struct page'
25 * when a page is recycled back to the page pool.
27 * 2. page_pool_alloc_frag(): allocate memory with page splitting when driver
29 * page allocated from page pool. Page splitting enables memory saving and thus
31 * implement page splitting, mainly some cache line dirtying/bouncing for
32 * 'struct page' and atomic operation for page->pp_ref_count.
34 * The API keeps track of in-flight pages, in order to let API users know when
37 * attach the page_pool object to a page_pool-aware object like skbs marked with
40 * page_pool_put_page() may be called multiple times on the same page if a page
42 * recycle the page, or in case of page->_refcount > 1, it will release the DMA
43 * mapping and in-flight state accounting.
48 * the same page when a page is split. The API user must setup pool->p.max_len
49 * and pool->p.offset correctly and ensure that page_pool_put_page() is called
50 * with dma_sync_size being -1 for fragment API.
55 #include <linux/dma-mapping.h>
62 /* Deprecated driver-facing API, use netlink instead */
87 * page_pool_dev_alloc_pages() - allocate a page.
90 * Get a page from the page allocator or page_pool caches.
92 static inline struct page *page_pool_dev_alloc_pages(struct page_pool *pool) in page_pool_dev_alloc_pages()
100 * page_pool_dev_alloc_frag() - allocate a page fragment.
102 * @offset: offset to the allocated page
103 * @size: requested size
105 * Get a page fragment from the page allocator or page_pool caches.
107 * Return: allocated page fragment, otherwise return NULL.
109 static inline struct page *page_pool_dev_alloc_frag(struct page_pool *pool, in page_pool_dev_alloc_frag()
111 unsigned int size) in page_pool_dev_alloc_frag() argument
115 return page_pool_alloc_frag(pool, offset, size, gfp); in page_pool_dev_alloc_frag()
120 unsigned int *size, gfp_t gfp) in page_pool_alloc_netmem() argument
122 unsigned int max_size = PAGE_SIZE << pool->p.order; in page_pool_alloc_netmem()
125 if ((*size << 1) > max_size) { in page_pool_alloc_netmem()
126 *size = max_size; in page_pool_alloc_netmem()
131 netmem = page_pool_alloc_frag_netmem(pool, offset, *size, gfp); in page_pool_alloc_netmem()
136 * the remaining size to the current fragment to avoid truesize in page_pool_alloc_netmem()
139 if (pool->frag_offset + *size > max_size) { in page_pool_alloc_netmem()
140 *size = max_size - *offset; in page_pool_alloc_netmem()
141 pool->frag_offset = max_size; in page_pool_alloc_netmem()
149 unsigned int *size) in page_pool_dev_alloc_netmem() argument
153 return page_pool_alloc_netmem(pool, offset, size, gfp); in page_pool_dev_alloc_netmem()
156 static inline struct page *page_pool_alloc(struct page_pool *pool, in page_pool_alloc()
158 unsigned int *size, gfp_t gfp) in page_pool_alloc() argument
160 return netmem_to_page(page_pool_alloc_netmem(pool, offset, size, gfp)); in page_pool_alloc()
164 * page_pool_dev_alloc() - allocate a page or a page fragment.
166 * @offset: offset to the allocated page
167 * @size: in as the requested size, out as the allocated size
169 * Get a page or a page fragment from the page allocator or page_pool caches
170 * depending on the requested size in order to allocate memory with least memory
173 * Return: allocated page or page fragment, otherwise return NULL.
175 static inline struct page *page_pool_dev_alloc(struct page_pool *pool, in page_pool_dev_alloc()
177 unsigned int *size) in page_pool_dev_alloc() argument
181 return page_pool_alloc(pool, offset, size, gfp); in page_pool_dev_alloc()
185 unsigned int *size, gfp_t gfp) in page_pool_alloc_va() argument
188 struct page *page; in page_pool_alloc_va() local
191 page = page_pool_alloc(pool, &offset, size, gfp & ~__GFP_HIGHMEM); in page_pool_alloc_va()
192 if (unlikely(!page)) in page_pool_alloc_va()
195 return page_address(page) + offset; in page_pool_alloc_va()
199 * page_pool_dev_alloc_va() - allocate a page or a page fragment and return its
202 * @size: in as the requested size, out as the allocated size
205 * it returns va of the allocated page or page fragment.
207 * Return: the va for the allocated page or page fragment, otherwise return NULL.
210 unsigned int *size) in page_pool_dev_alloc_va() argument
214 return page_pool_alloc_va(pool, size, gfp); in page_pool_dev_alloc_va()
218 * page_pool_get_dma_dir() - Retrieve the stored DMA direction.
219 * @pool: pool from which page was allocated
227 return pool->p.dma_dir; in page_pool_get_dma_dir()
236 * page_pool_fragment_page() - split a fresh page into fragments
237 * @page: page to split
240 * pp_ref_count represents the number of outstanding references to the page,
241 * which will be freed using page_pool APIs (rather than page allocator APIs
242 * like put_page()). Such references are usually held by page_pool-aware
243 * objects like skbs marked for page pool recycling.
246 * freshly allocated page. The page must be freshly allocated (have a
248 * "fragment allocators" to save atomic operations - either when they know
253 static inline void page_pool_fragment_page(struct page *page, long nr) in page_pool_fragment_page() argument
255 page_pool_fragment_netmem(page_to_netmem(page), nr); in page_pool_fragment_page()
264 * references to the page: in page_pool_unref_netmem()
271 * an atomic update, especially when dealing with a page that may be in page_pool_unref_netmem()
274 * initially, and only overwrite it when the page is partitioned into in page_pool_unref_netmem()
279 * the BUILD_BUG_ON(), only need to handle the non-constant case in page_pool_unref_netmem()
303 static inline long page_pool_unref_page(struct page *page, long nr) in page_pool_unref_page() argument
305 return page_pool_unref_netmem(page_to_netmem(page), nr); in page_pool_unref_page()
313 static inline void page_pool_ref_page(struct page *page) in page_pool_ref_page() argument
315 page_pool_ref_netmem(page_to_netmem(page)); in page_pool_ref_page()
329 /* When page_pool isn't compiled-in, net/core/xdp.c doesn't in page_pool_put_netmem()
341 * page_pool_put_page() - release a reference to a page pool page
342 * @pool: pool from which page was allocated
343 * @page: page to release a reference on
344 * @dma_sync_size: how much of the page may have been touched by the device
347 * The outcome of this depends on the page refcnt. If the driver bumps
348 * the refcnt > 1 this will unmap the page. If the page refcnt is 1
349 * the allocator owns the page and will try to recycle it in one of the pool
350 * caches. If PP_FLAG_DMA_SYNC_DEV is set, the page will be synced for_device
354 struct page *page, in page_pool_put_page() argument
358 page_pool_put_netmem(pool, page_to_netmem(page), dma_sync_size, in page_pool_put_page()
366 page_pool_put_netmem(pool, netmem, -1, allow_direct); in page_pool_put_full_netmem()
370 * page_pool_put_full_page() - release a reference on a page pool page
371 * @pool: pool from which page was allocated
372 * @page: page to release a reference on
379 struct page *page, bool allow_direct) in page_pool_put_full_page() argument
381 page_pool_put_netmem(pool, page_to_netmem(page), -1, allow_direct); in page_pool_put_full_page()
385 * page_pool_recycle_direct() - release a reference on a page pool page
386 * @pool: pool from which page was allocated
387 * @page: page to release a reference on
390 * (e.g NAPI), since it will recycle the page directly into the pool fast cache.
393 struct page *page) in page_pool_recycle_direct() argument
395 page_pool_put_full_page(pool, page, true); in page_pool_recycle_direct()
402 * page_pool_free_va() - free a va into the page_pool
412 page_pool_put_page(pool, virt_to_head_page(va), -1, allow_direct); in page_pool_free_va()
426 * page_pool_get_dma_addr() - Retrieve the stored DMA address.
427 * @page: page allocated from a page pool
429 * Fetch the DMA address of the page. The page pool to which the page belongs
432 static inline dma_addr_t page_pool_get_dma_addr(const struct page *page) in page_pool_get_dma_addr() argument
434 dma_addr_t ret = page->dma_addr; in page_pool_get_dma_addr()
446 dma_sync_single_range_for_cpu(pool->p.dev, dma_addr, in __page_pool_dma_sync_for_cpu()
447 offset + pool->p.offset, dma_sync_size, in __page_pool_dma_sync_for_cpu()
452 * page_pool_dma_sync_for_cpu - sync Rx page for CPU after it's written by HW
453 * @pool: &page_pool the @page belongs to
454 * @page: page to sync
455 * @offset: offset from page start to "hard" start if using PP frags
456 * @dma_sync_size: size of the data written to the page
461 * associated PP doesn't perform sync-for-device.
464 const struct page *page, in page_pool_dma_sync_for_cpu() argument
467 __page_pool_dma_sync_for_cpu(pool, page_pool_get_dma_addr(page), offset, in page_pool_dma_sync_for_cpu()
476 if (!pool->dma_sync_for_cpu) in page_pool_dma_sync_netmem_for_cpu()
486 return refcount_dec_and_test(&pool->user_cnt); in page_pool_put()
491 if (unlikely(pool->p.nid != new_nid)) in page_pool_nid_changed()