Skip to content

Commit ff7d6b2

Browse files
netoptimizerdavem330
authored andcommitted
page_pool: refurbish version of page_pool code
Need a fast page recycle mechanism for ndo_xdp_xmit API for returning pages on DMA-TX completion time, which have good cross CPU performance, given DMA-TX completion time can happen on a remote CPU. Refurbish my page_pool code, that was presented[1] at MM-summit 2016. Adapted page_pool code to not depend the page allocator and integration into struct page. The DMA mapping feature is kept, even-though it will not be activated/used in this patchset. [1] http://people.netfilter.org/hawk/presentations/MM-summit2016/generic_page_pool_mm_summit2016.pdf V2: Adjustments requested by Tariq - Changed page_pool_create return codes, don't return NULL, only ERR_PTR, as this simplifies err handling in drivers. V4: many small improvements and cleanups - Add DOC comment section, that can be used by kernel-doc - Improve fallback mode, to work better with refcnt based recycling e.g. remove a WARN as pointed out by Tariq e.g. quicker fallback if ptr_ring is empty. V5: Fixed SPDX license as pointed out by Alexei V6: Adjustments requested by Eric Dumazet - Adjust ____cacheline_aligned_in_smp usage/placement - Move rcu_head in struct page_pool - Free pages quicker on destroy, minimize resources delayed an RCU period - Remove code for forward/backward compat ABI interface V8: Issues found by kbuild test robot - Address sparse should be static warnings - Only compile+link when a driver use/select page_pool, mlx5 selects CONFIG_PAGE_POOL, although its first used in two patches Signed-off-by: Jesper Dangaard Brouer <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8d5d885 commit ff7d6b2

File tree

5 files changed

+451
-0
lines changed

5 files changed

+451
-0
lines changed

drivers/net/ethernet/mellanox/mlx5/core/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config MLX5_CORE_EN
3030
bool "Mellanox Technologies ConnectX-4 Ethernet support"
3131
depends on NETDEVICES && ETHERNET && INET && PCI && MLX5_CORE
3232
depends on IPV6=y || IPV6=n || MLX5_CORE=m
33+
select PAGE_POOL
3334
default n
3435
---help---
3536
Ethernet support in Mellanox Technologies ConnectX-4 NIC.

include/net/page_pool.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* SPDX-License-Identifier: GPL-2.0
2+
*
3+
* page_pool.h
4+
* Author: Jesper Dangaard Brouer <[email protected]>
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
54+
struct pp_alloc_cache {
55+
u32 count;
56+
void *cache[PP_ALLOC_CACHE_SIZE];
57+
};
58+
59+
struct 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+
68+
struct 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+
101+
struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
102+
103+
static 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+
110+
struct page_pool *page_pool_create(const struct page_pool_params *params);
111+
112+
void page_pool_destroy(struct page_pool *pool);
113+
114+
/* Never call this directly, use helpers below */
115+
void __page_pool_put_page(struct page_pool *pool,
116+
struct page *page, bool allow_direct);
117+
118+
static inline void page_pool_put_page(struct page_pool *pool, struct page *page)
119+
{
120+
__page_pool_put_page(pool, page, false);
121+
}
122+
/* Very limited use-cases allow recycle direct */
123+
static inline void page_pool_recycle_direct(struct page_pool *pool,
124+
struct page *page)
125+
{
126+
__page_pool_put_page(pool, page, true);
127+
}
128+
129+
#endif /* _NET_PAGE_POOL_H */

net/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ config MAY_USE_DEVLINK
423423
on MAY_USE_DEVLINK to ensure they do not cause link errors when
424424
devlink is a loadable module and the driver using it is built-in.
425425

426+
config PAGE_POOL
427+
bool
428+
426429
endif # if NET
427430

428431
# Used by archs to tell that they support BPF JIT compiler plus which flavour.

net/core/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ obj-y += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \
1414
fib_notifier.o xdp.o
1515

1616
obj-y += net-sysfs.o
17+
obj-$(CONFIG_PAGE_POOL) += page_pool.o
1718
obj-$(CONFIG_PROC_FS) += net-procfs.o
1819
obj-$(CONFIG_NET_PKTGEN) += pktgen.o
1920
obj-$(CONFIG_NETPOLL) += netpoll.o

0 commit comments

Comments
 (0)