linux/include/net/page_pool.h
<<
>>
Prefs
   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
 114/* Never call this directly, use helpers below */
 115void __page_pool_put_page(struct page_pool *pool,
 116                          struct page *page, bool allow_direct);
 117
 118static inline void page_pool_put_page(struct page_pool *pool,
 119                                      struct page *page, bool allow_direct)
 120{
 121        /* When page_pool isn't compiled-in, net/core/xdp.c doesn't
 122         * allow registering MEM_TYPE_PAGE_POOL, but shield linker.
 123         */
 124#ifdef CONFIG_PAGE_POOL
 125        __page_pool_put_page(pool, page, allow_direct);
 126#endif
 127}
 128/* Very limited use-cases allow recycle direct */
 129static inline void page_pool_recycle_direct(struct page_pool *pool,
 130                                            struct page *page)
 131{
 132        __page_pool_put_page(pool, page, true);
 133}
 134
 135static inline bool is_page_pool_compiled_in(void)
 136{
 137#ifdef CONFIG_PAGE_POOL
 138        return true;
 139#else
 140        return false;
 141#endif
 142}
 143
 144#endif /* _NET_PAGE_POOL_H */
 145