Skip to content

Commit 6a5bcd8

Browse files
apalosdavem330
authored andcommitted
page_pool: Allow drivers to hint on SKB recycling
Up to now several high speed NICs have custom mechanisms of recycling the allocated memory they use for their payloads. Our page_pool API already has recycling capabilities that are always used when we are running in 'XDP mode'. So let's tweak the API and the kernel network stack slightly and allow the recycling to happen even during the standard operation. The API doesn't take into account 'split page' policies used by those drivers currently, but can be extended once we have users for that. The idea is to be able to intercept the packet on skb_release_data(). If it's a buffer coming from our page_pool API recycle it back to the pool for further usage or just release the packet entirely. To achieve that we introduce a bit in struct sk_buff (pp_recycle:1) and a field in struct page (page->pp) to store the page_pool pointer. Storing the information in page->pp allows us to recycle both SKBs and their fragments. We could have skipped the skb bit entirely, since identical information can bederived from struct page. However, in an effort to affect the free path as less as possible, reading a single bit in the skb which is already in cache, is better that trying to derive identical information for the page stored data. The driver or page_pool has to take care of the sync operations on it's own during the buffer recycling since the buffer is, after opting-in to the recycling, never unmapped. Since the gain on the drivers depends on the architecture, we are not enabling recycling by default if the page_pool API is used on a driver. In order to enable recycling the driver must call skb_mark_for_recycle() to store the information we need for recycling in page->pp and enabling the recycling bit, or page_pool_store_mem_info() for a fragment. Co-developed-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Co-developed-by: Matteo Croce <mcroce@microsoft.com> Signed-off-by: Matteo Croce <mcroce@microsoft.com> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c420c98 commit 6a5bcd8

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed

include/linux/skbuff.h

+30-3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/in6.h>
3838
#include <linux/if_packet.h>
3939
#include <net/flow.h>
40+
#include <net/page_pool.h>
4041
#if IS_ENABLED(CONFIG_NF_CONNTRACK)
4142
#include <linux/netfilter/nf_conntrack_common.h>
4243
#endif
@@ -667,6 +668,8 @@ typedef unsigned char *sk_buff_data_t;
667668
* @head_frag: skb was allocated from page fragments,
668669
* not allocated by kmalloc() or vmalloc().
669670
* @pfmemalloc: skbuff was allocated from PFMEMALLOC reserves
671+
* @pp_recycle: mark the packet for recycling instead of freeing (implies
672+
* page_pool support on driver)
670673
* @active_extensions: active extensions (skb_ext_id types)
671674
* @ndisc_nodetype: router type (from link layer)
672675
* @ooo_okay: allow the mapping of a socket to a queue to be changed
@@ -791,10 +794,12 @@ struct sk_buff {
791794
fclone:2,
792795
peeked:1,
793796
head_frag:1,
794-
pfmemalloc:1;
797+
pfmemalloc:1,
798+
pp_recycle:1; /* page_pool recycle indicator */
795799
#ifdef CONFIG_SKB_EXTENSIONS
796800
__u8 active_extensions;
797801
#endif
802+
798803
/* fields enclosed in headers_start/headers_end are copied
799804
* using a single memcpy() in __copy_skb_header()
800805
*/
@@ -3088,7 +3093,13 @@ static inline void skb_frag_ref(struct sk_buff *skb, int f)
30883093
*/
30893094
static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle)
30903095
{
3091-
put_page(skb_frag_page(frag));
3096+
struct page *page = skb_frag_page(frag);
3097+
3098+
#ifdef CONFIG_PAGE_POOL
3099+
if (recycle && page_pool_return_skb_page(page))
3100+
return;
3101+
#endif
3102+
put_page(page);
30923103
}
30933104

30943105
/**
@@ -3100,7 +3111,7 @@ static inline void __skb_frag_unref(skb_frag_t *frag, bool recycle)
31003111
*/
31013112
static inline void skb_frag_unref(struct sk_buff *skb, int f)
31023113
{
3103-
__skb_frag_unref(&skb_shinfo(skb)->frags[f], false);
3114+
__skb_frag_unref(&skb_shinfo(skb)->frags[f], skb->pp_recycle);
31043115
}
31053116

31063117
/**
@@ -4699,5 +4710,21 @@ static inline u64 skb_get_kcov_handle(struct sk_buff *skb)
46994710
#endif
47004711
}
47014712

4713+
#ifdef CONFIG_PAGE_POOL
4714+
static inline void skb_mark_for_recycle(struct sk_buff *skb, struct page *page,
4715+
struct page_pool *pp)
4716+
{
4717+
skb->pp_recycle = 1;
4718+
page_pool_store_mem_info(page, pp);
4719+
}
4720+
#endif
4721+
4722+
static inline bool skb_pp_recycle(struct sk_buff *skb, void *data)
4723+
{
4724+
if (!IS_ENABLED(CONFIG_PAGE_POOL) || !skb->pp_recycle)
4725+
return false;
4726+
return page_pool_return_skb_page(virt_to_page(data));
4727+
}
4728+
47024729
#endif /* __KERNEL__ */
47034730
#endif /* _LINUX_SKBUFF_H */

include/net/page_pool.h

+9
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ inline enum dma_data_direction page_pool_get_dma_dir(struct page_pool *pool)
146146
return pool->p.dma_dir;
147147
}
148148

149+
bool page_pool_return_skb_page(struct page *page);
150+
149151
struct page_pool *page_pool_create(const struct page_pool_params *params);
150152

151153
#ifdef CONFIG_PAGE_POOL
@@ -251,4 +253,11 @@ static inline void page_pool_ring_unlock(struct page_pool *pool)
251253
spin_unlock_bh(&pool->ring.producer_lock);
252254
}
253255

256+
/* Store mem_info on struct page and use it while recycling skb frags */
257+
static inline
258+
void page_pool_store_mem_info(struct page *page, struct page_pool *pp)
259+
{
260+
page->pp = pp;
261+
}
262+
254263
#endif /* _NET_PAGE_POOL_H */

net/core/page_pool.c

+22
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,25 @@ void page_pool_update_nid(struct page_pool *pool, int new_nid)
628628
}
629629
}
630630
EXPORT_SYMBOL(page_pool_update_nid);
631+
632+
bool page_pool_return_skb_page(struct page *page)
633+
{
634+
struct page_pool *pp;
635+
636+
page = compound_head(page);
637+
if (unlikely(page->pp_magic != PP_SIGNATURE))
638+
return false;
639+
640+
pp = page->pp;
641+
642+
/* Driver set this to memory recycling info. Reset it on recycle.
643+
* This will *not* work for NIC using a split-page memory model.
644+
* The page will be returned to the pool here regardless of the
645+
* 'flipped' fragment being in use or not.
646+
*/
647+
page->pp = NULL;
648+
page_pool_put_full_page(pp, page, false);
649+
650+
return true;
651+
}
652+
EXPORT_SYMBOL(page_pool_return_skb_page);

net/core/skbuff.c

+16-4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include <net/xfrm.h>
7171
#include <net/mpls.h>
7272
#include <net/mptcp.h>
73+
#include <net/page_pool.h>
7374

7475
#include <linux/uaccess.h>
7576
#include <trace/events/skb.h>
@@ -645,10 +646,13 @@ static void skb_free_head(struct sk_buff *skb)
645646
{
646647
unsigned char *head = skb->head;
647648

648-
if (skb->head_frag)
649+
if (skb->head_frag) {
650+
if (skb_pp_recycle(skb, head))
651+
return;
649652
skb_free_frag(head);
650-
else
653+
} else {
651654
kfree(head);
655+
}
652656
}
653657

654658
static void skb_release_data(struct sk_buff *skb)
@@ -664,7 +668,7 @@ static void skb_release_data(struct sk_buff *skb)
664668
skb_zcopy_clear(skb, true);
665669

666670
for (i = 0; i < shinfo->nr_frags; i++)
667-
__skb_frag_unref(&shinfo->frags[i], false);
671+
__skb_frag_unref(&shinfo->frags[i], skb->pp_recycle);
668672

669673
if (shinfo->frag_list)
670674
kfree_skb_list(shinfo->frag_list);
@@ -1046,6 +1050,7 @@ static struct sk_buff *__skb_clone(struct sk_buff *n, struct sk_buff *skb)
10461050
n->nohdr = 0;
10471051
n->peeked = 0;
10481052
C(pfmemalloc);
1053+
C(pp_recycle);
10491054
n->destructor = NULL;
10501055
C(tail);
10511056
C(end);
@@ -3495,7 +3500,7 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
34953500
fragto = &skb_shinfo(tgt)->frags[merge];
34963501

34973502
skb_frag_size_add(fragto, skb_frag_size(fragfrom));
3498-
__skb_frag_unref(fragfrom, false);
3503+
__skb_frag_unref(fragfrom, skb->pp_recycle);
34993504
}
35003505

35013506
/* Reposition in the original skb */
@@ -5285,6 +5290,13 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
52855290
if (skb_cloned(to))
52865291
return false;
52875292

5293+
/* The page pool signature of struct page will eventually figure out
5294+
* which pages can be recycled or not but for now let's prohibit slab
5295+
* allocated and page_pool allocated SKBs from being coalesced.
5296+
*/
5297+
if (to->pp_recycle != from->pp_recycle)
5298+
return false;
5299+
52885300
if (len <= skb_tailroom(to)) {
52895301
if (len)
52905302
BUG_ON(skb_copy_bits(from, 0, skb_put(to, len), len));

0 commit comments

Comments
 (0)