xdp: recycle Page Pool backed skbs built from XDP frames
Subsystems (6)
v3 cover xdp: recycle Page Pool backed skbs built from XDP frames
Yeah, I still remember that "Who needs cpumap nowadays" (c), but anyway.
__xdp_build_skb_from_frame() missed the moment when the networking stack became able to recycle skb pages backed by a page_pool. This was making e.g. cpumap redirect even less effective than simple %XDP_PASS. veth was also affected in some scenarios.
A lot of drivers use skb_mark_for_recycle() already, it's been almost two years and seems like there are no issues in using it in the generic code too. {__,}xdp_release_frame() can be then removed as it losts its last user.
Page Pool becomes then zero-alloc (or almost) in the abovementioned cases, too. Other memory type models (who needs them at this point) have no changes.
Some numbers on 1 Xeon Platinum core bombed with 27 Mpps of 64-byte IPv6 UDP, iavf w/XDP[0] (CONFIG_PAGE_POOL_STATS is enabled):
Plain %XDP_PASS on baseline, Page Pool driver:
src cpu Rx drops dst cpu Rx
2.1 Mpps N/A 2.1 Mpps
cpumap redirect (cross-core, w/o leaving its NUMA node) on baseline:
6.8 Mpps 5.0 Mpps 1.8 Mpps
cpumap redirect with skb PP recycling:
7.9 Mpps 5.7 Mpps 2.2 Mpps
+22% (from cpumap redir on baseline)
[0] https://github.com/alobakin/linux/commits/iavf-xdp
Alexander Lobakin (4):
selftests/bpf: robustify test_xdp_do_redirect with more payload magics
net: page_pool, skbuff: make skb_mark_for_recycle() always available
xdp: recycle Page Pool backed skbs built from XDP frames
xdp: remove unused {__,}xdp_release_frame()
include/linux/skbuff.h | 4 +-- include/net/xdp.h | 29 --------------- net/core/xdp.c | 19 ++-------- .../bpf/progs/test_xdp_do_redirect.c | 36 +++++++++++++------ 4 files changed, 30 insertions(+), 58 deletions(-)
---
From v2[1]:
* fix the test_xdp_do_redirect selftest failing after the series: it was relying on that %XDP_PASS frames can't be recycled on veth (BPF CI, Alexei);
* explain "w/o leaving its node" in the cover letter (Jesper).
From v1[2]:
* make skb_mark_for_recycle() always available, otherwise there are build failures on non-PP systems (kbuild bot);
* 'Page Pool' -> 'page_pool' when it's about a page_pool instance, not API (Jesper);
* expanded test system info a bit in the cover letter (Jesper).
[1] https://lore.kernel.org/bpf/[email protected]
[2] https://lore.kernel.org/bpf/[email protected]
Signature · 1 line
2.39.2
PATCH v3 1/4 selftests/bpf: robustify test_xdp_do_redirect with more payload magics
Currently, the test relies on that only dropped ("xmitted") frames will be recycled and if a frame became an skb, it will be freed later by the stack and never come back to its page_pool.
So, it easily gets broken by trying to recycle skbs[0]:
test_xdp_do_redirect:PASS:pkt_count_xdp 0 nsec test_xdp_do_redirect:FAIL:pkt_count_zero unexpected pkt_count_zero:
actual 9936 != expected 2
test_xdp_do_redirect:PASS:pkt_count_tc 0 nsec
That huge mismatch happened because after the TC ingress hook zeroes the magic, the page gets recycled when skb is freed, not returned to the MM layer. "Live frames" mode initializes only new pages and keeps the recycled ones as is by design, so they appear with zeroed magic on the Rx path again.
Expand the possible magic values from two: 0 (was "xmitted"/dropped or did hit the TC hook) and 0x42 (hit the input XDP prog) to three: the new one will mark frames hit the TC hook, so that they will elide both @pkt_count_zero and @pkt_count_xdp. They can then be recycled to their page_pool or returned to the page allocator, this won't affect the counters anyhow. Just make sure to mark them as "input" (0x42) when they appear on the Rx path again.
Also make an enum from those magics, so that they will be always visible and can be changed in just one place anytime. This also eases adding any new marks later on.
--- .../bpf/progs/test_xdp_do_redirect.c | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-)
tools/testing/selftests/bpf/progs/test_xdp_do_redirect.c +26 -10
@@ -4,6 +4,19 @@#define ETH_ALEN 6#define HDR_SZ (sizeof(struct ethhdr) + sizeof(struct ipv6hdr) + sizeof(struct udphdr)) + +/** +* enum frame_mark - magics to distinguish page/packet paths +* @MARK_XMIT: page was recycled due to the frame being "xmitted" by the NIC. +* @MARK_IN: frame is being processed by the input XDP prog. +* @MARK_SKB: frame did hit the TC ingress hook as an skb. +*/ +enum frame_mark { +MARK_XMIT =0U , +MARK_IN =0x42 , +MARK_SKB =0x45 , +}; +const volatile int ifindex_out;const volatile int ifindex_in;const volatile __u8 expect_dst[ETH_ALEN ];
@@ -34,10 +47,10 @@ int xdp_redirect(struct xdp_md *xdp)if (*metadata !=0x42 )return XDP_ABORTED ; -if (*payload ==0 ) { - *payload =0x42 ; +if (*payload ==MARK_XMIT ) pkts_seen_zero++; - } + + *payload =MARK_IN ;if (bpf_xdp_adjust_meta (xdp,4 ))return XDP_ABORTED ;
@@ -51,7 +64,7 @@ int xdp_redirect(struct xdp_md *xdp)return ret; } -static bool check_pkt (void *data,void *data_end) +static bool check_pkt (void *data,void *data_end,const __u32 mark) {struct ipv6hdr *iph = data +sizeof (struct ethhdr );__u8 *payload = data +HDR_SZ ;
@@ -59,13 +72,13 @@ static bool check_pkt(void *data, void *data_end)if (payload +1 > data_end)return false; -if (iph->nexthdr !=IPPROTO_UDP || *payload !=0x42 ) +if (iph->nexthdr !=IPPROTO_UDP || *payload !=MARK_IN )return false;/* reset the payload so the same packet doesn't get counted twice when * it cycles back through the kernel path and out the dst veth */ - *payload =0 ; + *payload = mark;return true; }
@@ -75,11 +88,11 @@ int xdp_count_pkts(struct xdp_md *xdp)void *data = (void *)(long )xdp->data;void *data_end = (void *)(long )xdp->data_end; -if (check_pkt (data, data_end)) +if (check_pkt (data, data_end,MARK_XMIT )) pkts_seen_xdp++; -/* Return XDP_DROP to make sure the data page is recycled, like when it -* exits a physical NIC. Recycled pages will be counted in the +/* Return %XDP_DROP to recycle the data page with %MARK_XMIT, like +* it exited a physical NIC. Those pages will be counted in the * pkts_seen_zero counter above. */ return XDP_DROP ;
@@ -91,9 +104,12 @@ int tc_count_pkts(struct __sk_buff *skb)void *data = (void *)(long )skb->data;void *data_end = (void *)(long )skb->data_end; -if (check_pkt (data, data_end)) +if (check_pkt (data, data_end,MARK_SKB )) pkts_seen_tc++; +/* Will be either recycled or freed, %MARK_SKB makes sure it won't +* hit any of the counters above. +*/ return 0 ; }
Signature · 1 line
2.39.2
PATCH v3 2/4 net: page_pool, skbuff: make skb_mark_for_recycle() always available
In reply to Alexander Lobakinskb_mark_for_recycle() is guarded with CONFIG_PAGE_POOL, this creates unneeded complication when using it in the generic code. For now, it's only used in the drivers always selecting Page Pool, so this works. Move the guards so that preprocessor will cut out only the operation itself and the function will still be a noop on !PAGE_POOL systems, but available there as well.
No functional changes.
--- include/linux/skbuff.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/skbuff.h +2 -2
@@ -5069,12 +5069,12 @@ static inline u64 skb_get_kcov_handle(struct sk_buff *skb)#endif } -#ifdef CONFIG_PAGE_POOL static inline void skb_mark_for_recycle (struct sk_buff *skb) { +#ifdef CONFIG_PAGE_POOL skb->pp_recycle =1 ; -}#endif +}#endif /* __KERNEL__ */ #endif /* _LINUX_SKBUFF_H */
Signature · 1 line
2.39.2
PATCH v3 3/4 xdp: recycle Page Pool backed skbs built from XDP frames
In reply to Alexander Lobakin__xdp_build_skb_from_frame() state(d):
/* Until page_pool get SKB return path, release DMA here */
Page Pool got skb pages recycling in April 2021, but missed this function.
xdp_release_frame() is relevant only for Page Pool backed frames and it detaches the page from the corresponding page_pool in order to make it freeable via page_frag_free(). It can instead just mark the output skb as eligible for recycling if the frame is backed by a pp. No change for other memory model types (the same condition check as before). cpumap redirect and veth on Page Pool drivers now become zero-alloc (or almost).
--- net/core/xdp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/xdp.c +2 -2
@@ -658,8 +658,8 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,* - RX ring dev queue index (skb_record_rx_queue) */ -/* Until page_pool get SKB return path, release DMA here */ -xdp_release_frame (xdpf); +if (xdpf->mem.type ==MEM_TYPE_PAGE_POOL ) +skb_mark_for_recycle (skb);/* Allow SKB to reuse area used by xdp_frame */ xdp_scrub_frame (xdpf);
Signature · 1 line
2.39.2
PATCH v3 4/4 xdp: remove unused {__,}xdp_release_frame()
In reply to Alexander Lobakin__xdp_build_skb_from_frame() was the last user of {__,}xdp_release_frame(), which detaches pages from the page_pool. All the consumers now recycle Page Pool skbs and page, except mlx5, stmmac and tsnep drivers, which use page_pool_release_page() directly (might change one day). It's safe to assume this functionality is not needed anymore and can be removed (in favor of recycling).
--- include/net/xdp.h | 29 ----------------------------- net/core/xdp.c | 15 --------------- 2 files changed, 44 deletions(-)
include/net/xdp.h +0 -29
@@ -317,35 +317,6 @@ void xdp_flush_frame_bulk(struct xdp_frame_bulk *bq);void xdp_return_frame_bulk (struct xdp_frame *xdpf,struct xdp_frame_bulk *bq); -/* When sending xdp_frame into the network stack, then there is no -* return point callback, which is needed to release e.g. DMA-mapping -* resources with page_pool. Thus, have explicit function to release -* frame resources. -*/ -void __xdp_release_frame (void *data,struct xdp_mem_info *mem); -static inline void xdp_release_frame (struct xdp_frame *xdpf) -{ -struct xdp_mem_info *mem = &xdpf->mem; -struct skb_shared_info *sinfo; -int i; - -/* Curr only page_pool needs this */ -if (mem->type !=MEM_TYPE_PAGE_POOL ) -return ; - -if (likely (!xdp_frame_has_frags (xdpf))) - goto out; - - sinfo =xdp_get_shared_info_from_frame (xdpf); -for (i =0 ; i < sinfo->nr_frags; i++) { -struct page *page =skb_frag_page (&sinfo->frags[i]); - -__xdp_release_frame (page_address (page), mem); - } -out: -__xdp_release_frame (xdpf->data, mem); -} - static __always_inline unsigned int xdp_get_frame_len(struct xdp_frame *xdpf) {struct skb_shared_info *sinfo;
net/core/xdp.c +0 -15
@@ -531,21 +531,6 @@ void xdp_return_buff(struct xdp_buff *xdp) }EXPORT_SYMBOL_GPL (xdp_return_buff); -/* Only called for MEM_TYPE_PAGE_POOL see xdp.h */ -void __xdp_release_frame (void *data,struct xdp_mem_info *mem) -{ -struct xdp_mem_allocator *xa; -struct page *page; - -rcu_read_lock (); - xa =rhashtable_lookup (mem_id_ht, &mem->id, mem_id_rht_params); - page =virt_to_head_page (data); -if (xa) -page_pool_release_page (xa->page_pool, page); -rcu_read_unlock (); -} -EXPORT_SYMBOL_GPL (__xdp_release_frame); -void xdp_attachment_setup (struct xdp_attachment_info *info,struct netdev_bpf *bpf) {
Signature · 1 line
2.39.2
From: Alexander Lobakin <[email protected]>
Date: Mon, 13 Mar 2023 22:42:56 +0100
Quoted 13 lines
Yeah, I still remember that "Who needs cpumap nowadays" (c), but anyway.
__xdp_build_skb_from_frame()missed the moment when the networking stack became able to recycle skb pages backed by apage_pool. This was making e.g. cpumap redirect even less effective than simple %XDP_PASS. veth was also affected in some scenarios.
A lot of drivers useskb_mark_for_recycle()already, it's been almost two years and seems like there are no issues in using it in the generic code too. {__,}xdp_release_frame() can be then removed as it losts its last user.
Page Pool becomes then zero-alloc (or almost) in the abovementioned cases, too. Other memory type models (who needs them at this point) have no changes.
Sorry, our SMTP proxy went crazy and resent several times all my messages sent via git-send-email during the last couple days. Please ignore this.
Quoted 46 lines
Some numbers on 1 Xeon Platinum core bombed with 27 Mpps of 64-byte IPv6 UDP, iavf w/XDP[0] (
CONFIG_PAGE_POOL_STATSis enabled):Plain %XDP_PASS on baseline, Page Pool driver:
src cpu Rx drops dst cpu Rx
2.1 Mpps N/A 2.1 Mppscpumap redirect (cross-core, w/o leaving its NUMA node) on baseline:
6.8 Mpps 5.0 Mpps 1.8 Mpps
cpumap redirect with skb PP recycling:
7.9 Mpps 5.7 Mpps 2.2 Mpps
+22% (from cpumap redir on baseline)[0] https://github.com/alobakin/linux/commits/iavf-xdp
Alexander Lobakin (4):
selftests/bpf: robustifytest_xdp_do_redirectwith more payload magics
net:page_pool, skbuff: makeskb_mark_for_recycle()always available
xdp: recycle Page Pool backed skbs built from XDP frames
xdp: remove unused {__,}xdp_release_frame()include/linux/skbuff.h | 4 +-- include/net/xdp.h | 29 --------------- net/core/xdp.c | 19 ++-------- .../bpf/progs/test_xdp_do_redirect.c | 36 +++++++++++++------ 4 files changed, 30 insertions(+), 58 deletions(-)---
From v2[1]:
* fix thetest_xdp_do_redirectselftest failing after the series: it was relying on that %XDP_PASS frames can't be recycled on veth (BPF CI, Alexei);
* explain "w/o leaving its node" in the cover letter (Jesper).From v1[2]:
* makeskb_mark_for_recycle()always available, otherwise there are build failures on non-PP systems (kbuild bot);
* 'Page Pool' -> 'page_pool' when it's about apage_poolinstance, not API (Jesper);
* expanded test system info a bit in the cover letter (Jesper).
Quoted 2 lines
[1] https://lore.kernel.org/bpf/[email protected]
[2] https://lore.kernel.org/bpf/[email protected]
Thanks,
Olek