xdp: a fistful of generic changes pt. II
Subsystems (10)
cover xdp: a fistful of generic changes pt. II
XDP for idpf is currently 5.5 chapters:
* convert Rx to libeth;
* convert Tx and stats to libeth;
* generic XDP and XSk code changes;
* generic XDP and XSk code additions (you are here);
* actual XDP for idpf via new libeth_xdp;
* XSk for idpf (via ^).
Part III.2 does the following:
* allows mixing pages from several Page Pools within one XDP frame;
* optimizes &xdp_frame structure and removes no-more-used field;
* adds generic functions to build skbs from xdp_buffs (regular and XSk) and attach frags to xdp_buffs (regular and XSk);
* adds helper to optimize XSk xmit in drivers;
* makes XDP core and Page Pool a bit more netmem-friendly.
Everything is prereq for libeth_xdp, but will be useful standalone as well: faster xdp_return_frame_bulk() and xdp_frame fields access, less code in drivers, faster XSk XDP_PASS, smaller object code.
Alexander Lobakin (12):
page_pool: allow mixing PPs within one bulk
xdp: get rid of xdp_frame::mem.id
xdp: make __xdp_return() MP-agnostic
xdp: add generic xdp_buff_add_frag()
xdp: add generic xdp_build_skb_from_buff()
xsk: make xsk_buff_add_frag really add the frag via
__xdp_buff_add_frag()
xsk: add generic XSk &xdp_buff -> skb conversion
xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
page_pool: add a couple of netmem counterparts
skbuff: allow 2-4-argument skb_frag_dma_map()
jump_label: export static_key_slow_{inc,dec}_cpuslocked()
unroll: add generic loop unroll helpers
include/net/page_pool/types.h | 6 +- include/linux/skbuff.h | 47 ++- include/linux/unroll.h | 44 +++ include/net/page_pool/helpers.h | 46 ++- include/net/xdp.h | 130 +++++++- include/net/xdp_sock_drv.h | 41 ++- include/net/xsk_buff_pool.h | 8 + .../net/ethernet/freescale/dpaa/dpaa_eth.c | 2 +- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 30 +- drivers/net/ethernet/intel/ice/ice_xsk.c | 32 +- drivers/net/veth.c | 4 +- kernel/bpf/cpumap.c | 2 +- kernel/jump_label.c | 2 + net/bpf/test_run.c | 4 +- net/core/filter.c | 17 +- net/core/page_pool.c | 109 ++++--- net/core/xdp.c | 290 ++++++++++++++---- net/xdp/xsk_buff_pool.c | 40 +++ 18 files changed, 645 insertions(+), 209 deletions(-)
---
Each patch except trivial 0003 and 0009 was on the lists already.
* 0001 now includes Jakub's suggestions (pre-pass + while(count));
* 0004 doesn't leak refcounts anymore (also Jakub).
Signature · 1 line
2.47.1
PATCH 1/12 page_pool: allow mixing PPs within one bulk
The main reason for this change was to allow mixing pages from different &page_pools within one &xdp_buff/&xdp_frame. Why not? With stuff like devmem and io_uring zerocopy Rx, it's required to have separate PPs for header buffers and payload buffers.
Adjust xdp_return_frame_bulk() and page_pool_put_netmem_bulk(), so that they won't be tied to a particular pool. Let the latter create a separate bulk of pages which's PP is different from the first netmem of the bulk and process it after the main loop.
This greatly optimizes xdp_return_frame_bulk(): no more hashtable lookups and forced flushes on PP mismatch. Also make xdp_flush_frame_bulk() inline, as it's just one if + function call + one u32 read, not worth extending the call ladder.
--- include/net/page_pool/types.h | 6 +- include/net/xdp.h | 16 +++-- net/core/page_pool.c | 109 ++++++++++++++++++++++------------ net/core/xdp.c | 29 +-------- 4 files changed, 87 insertions(+), 73 deletions(-)
include/net/page_pool/types.h +2 -4
@@ -259,8 +259,7 @@ void page_pool_disable_direct_recycling(struct page_pool *pool);void page_pool_destroy (struct page_pool *pool);void page_pool_use_xdp_mem (struct page_pool *pool,void (*disconnect)(void *),const struct xdp_mem_info *mem); -void page_pool_put_netmem_bulk (struct page_pool *pool,netmem_ref *data, -u32 count); +void page_pool_put_netmem_bulk (netmem_ref *data,u32 count);#else static inline void page_pool_destroy (struct page_pool *pool) {
@@ -272,8 +271,7 @@ static inline void page_pool_use_xdp_mem(struct page_pool *pool, { } -static inline void page_pool_put_netmem_bulk (struct page_pool *pool, -netmem_ref *data,u32 count) +static inline void page_pool_put_netmem_bulk (netmem_ref *data,u32 count) { }#endif
include/net/xdp.h +12 -4
@@ -11,6 +11,8 @@#include <linux/netdevice.h> #include <linux/skbuff.h> /* skb_shared_info */ +#include <net/page_pool/types.h> +/** * DOC: XDP RX-queue information *
@@ -193,14 +195,12 @@ xdp_frame_is_frag_pfmemalloc(const struct xdp_frame *frame)#define XDP_BULK_QUEUE_SIZE 16struct xdp_frame_bulk {int count; -void *xa;netmem_ref q[XDP_BULK_QUEUE_SIZE ]; }; static __always_inline void xdp_frame_bulk_init(struct xdp_frame_bulk *bq) { -/* bq->count will be zero'ed when bq->xa gets updated */ - bq->xa =NULL ; + bq->count =0 ; } static inline struct skb_shared_info *
@@ -317,10 +317,18 @@ void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,void xdp_return_frame (struct xdp_frame *xdpf);void xdp_return_frame_rx_napi (struct xdp_frame *xdpf);void xdp_return_buff (struct xdp_buff *xdp); -void xdp_flush_frame_bulk (struct xdp_frame_bulk *bq);void xdp_return_frame_bulk (struct xdp_frame *xdpf,struct xdp_frame_bulk *bq); +static inline void xdp_flush_frame_bulk (struct xdp_frame_bulk *bq) +{ +if (unlikely (!bq->count)) +return ; + +page_pool_put_netmem_bulk (bq->q, bq->count); + bq->count =0 ; +} + static __always_inline unsigned intxdp_get_frame_len (const struct xdp_frame *xdpf) {
net/core/page_pool.c +72 -37
133 lines not on this page · raw message
net/core/xdp.c +1 -28
@@ -511,46 +511,19 @@ EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);* xdp_frame_bulk is usually stored/allocated on the function * call-stack to avoid locking penalties. */ -void xdp_flush_frame_bulk (struct xdp_frame_bulk *bq) -{ -struct xdp_mem_allocator *xa = bq->xa; - -if (unlikely (!xa || !bq->count)) -return ; - -page_pool_put_netmem_bulk (xa->page_pool, bq->q, bq->count); -/* bq->xa is not cleared to save lookup, if mem.id same in next bulk */ - bq->count =0 ; -} -EXPORT_SYMBOL_GPL (xdp_flush_frame_bulk);/* Must be called with rcu_read_lock held */ void xdp_return_frame_bulk (struct xdp_frame *xdpf,struct xdp_frame_bulk *bq) { -struct xdp_mem_info *mem = &xdpf->mem; -struct xdp_mem_allocator *xa; - -if (mem->type !=MEM_TYPE_PAGE_POOL ) { +if (xdpf->mem.type !=MEM_TYPE_PAGE_POOL ) {xdp_return_frame (xdpf);return ; } - xa = bq->xa; -if (unlikely (!xa)) { - xa =rhashtable_lookup (mem_id_ht, &mem->id, mem_id_rht_params); - bq->count =0 ; - bq->xa = xa; - } -if (bq->count ==XDP_BULK_QUEUE_SIZE )xdp_flush_frame_bulk (bq); -if (unlikely (mem->id != xa->mem.id)) { -xdp_flush_frame_bulk (bq); - bq->xa =rhashtable_lookup (mem_id_ht, &mem->id, mem_id_rht_params); - } -if (unlikely (xdp_frame_has_frags (xdpf))) {struct skb_shared_info *sinfo;int i;
Signature · 1 line
2.47.1
PATCH 2/12 xdp: get rid of xdp_frame::mem.id
In reply to Alexander LobakinInitially, xdp_frame::mem.id was used to search for the corresponding &page_pool to return the page correctly.
However, after that struct page was extended to have a direct pointer to its PP (netmem has it as well), further keeping of this field makes no sense. xdp_return_frame_bulk() still used it to do a lookup, and this leftover is now removed.
Remove xdp_frame::mem and replace it with ::mem_type, as only memory type still matters and we need to know it to be able to free the frame correctly.
As a cute side effect, we can now make every scalar field in &xdp_frame of 4 byte width, speeding up accesses to them.
--- include/net/xdp.h | 14 +++++----- .../net/ethernet/freescale/dpaa/dpaa_eth.c | 2 +- drivers/net/veth.c | 4 +-- kernel/bpf/cpumap.c | 2 +- net/bpf/test_run.c | 4 +-- net/core/filter.c | 12 ++++---- net/core/xdp.c | 28 +++++++++---------- 7 files changed, 33 insertions(+), 33 deletions(-)
include/net/xdp.h +7 -7
@@ -169,13 +169,13 @@ xdp_get_buff_len(const struct xdp_buff *xdp)struct xdp_frame {void *data; -u16 len; -u16 headroom; +u32 len; +u32 headroom;u32 metasize;/* uses lower 8-bits */ /* Lifetime of xdp_rxq_info is limited to NAPI/enqueue time, -* while mem info is valid on remote CPU. +* while mem_type is valid on remote CPU. */ -struct xdp_mem_info mem; +enum xdp_mem_type mem_type:32 ;struct net_device *dev_rx;/* used by cpumap */ u32 frame_sz;u32 flags;/* supported values defined in xdp_buff_flags */
@@ -306,13 +306,13 @@ struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)if (unlikely (xdp_update_frame_from_buff (xdp, xdp_frame) <0 ))return NULL ; -/* rxq only valid until napi_schedule ends, convert to xdp_mem_info */ - xdp_frame->mem = xdp->rxq->mem; +/* rxq only valid until napi_schedule ends, convert to xdp_mem_type */ + xdp_frame->mem_type = xdp->rxq->mem.type;return xdp_frame; } -void __xdp_return (void *data,struct xdp_mem_info *mem,bool napi_direct, +void __xdp_return (void *data,enum xdp_mem_type mem_type,bool napi_direct,struct xdp_buff *xdp);void xdp_return_frame (struct xdp_frame *xdpf);void xdp_return_frame_rx_napi (struct xdp_frame *xdpf);
drivers/net/ethernet/freescale/dpaa/dpaa_eth.c +1 -1
@@ -2281,7 +2281,7 @@ static int dpaa_a050385_wa_xdpf(struct dpaa_priv *priv, new_xdpf->len = xdpf->len; new_xdpf->headroom = priv->tx_headroom; new_xdpf->frame_sz =DPAA_BP_RAW_SIZE ; - new_xdpf->mem.type =MEM_TYPE_PAGE_ORDER0 ; + new_xdpf->mem_type =MEM_TYPE_PAGE_ORDER0 ;/* Release the initial buffer */ xdp_return_frame_rx_napi (xdpf);
drivers/net/veth.c +2 -2
@@ -634,7 +634,7 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,break ;case XDP_TX : orig_frame = *frame; - xdp->rxq->mem = frame->mem; + xdp->rxq->mem.type = frame->mem_type;if (unlikely (veth_xdp_tx (rq, xdp, bq) <0 )) {trace_xdp_exception (rq->dev, xdp_prog, act); frame = &orig_frame;
@@ -646,7 +646,7 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq, goto xdp_xmit;case XDP_REDIRECT : orig_frame = *frame; - xdp->rxq->mem = frame->mem; + xdp->rxq->mem.type = frame->mem_type;if (xdp_do_redirect (rq->dev, xdp, xdp_prog)) { frame = &orig_frame; stats->rx_drops++;
kernel/bpf/cpumap.c +1 -1
@@ -190,7 +190,7 @@ static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,int err; rxq.dev = xdpf->dev_rx; - rxq.mem = xdpf->mem; + rxq.mem.type = xdpf->mem_type;/* TODO: report queue_index to xdp_rxq_info */ xdp_convert_frame_to_buff (xdpf, &xdp);
net/bpf/test_run.c +2 -2
@@ -153,7 +153,7 @@ static void xdp_test_run_init_page(netmem_ref netmem, void *arg) new_ctx->data = new_ctx->data_meta + meta_len;xdp_update_frame_from_buff (new_ctx, frm); - frm->mem = new_ctx->rxq->mem; + frm->mem_type = new_ctx->rxq->mem.type;memcpy (&head->orig_ctx, new_ctx,sizeof (head->orig_ctx)); }
@@ -246,7 +246,7 @@ static void reset_ctx(struct xdp_page_head *head) head->ctx.data_meta = head->orig_ctx.data_meta; head->ctx.data_end = head->orig_ctx.data_end;xdp_update_frame_from_buff (&head->ctx, head->frame); - head->frame->mem = head->orig_ctx.rxq->mem; + head->frame->mem_type = head->orig_ctx.rxq->mem.type; } static int xdp_recv_frames(struct xdp_frame **frames, int nframes,
net/core/filter.c +6 -6
@@ -4119,13 +4119,13 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset) }static void bpf_xdp_shrink_data_zc (struct xdp_buff *xdp,int shrink, -struct xdp_mem_info *mem_info,bool release) +enum xdp_mem_type mem_type,bool release) {struct xdp_buff *zc_frag =xsk_buff_get_tail (xdp);if (release) {xsk_buff_del_tail (zc_frag); -__xdp_return (NULL , mem_info, false, zc_frag); +__xdp_return (NULL , mem_type, false, zc_frag); }else { zc_frag->data_end -= shrink; }
@@ -4134,18 +4134,18 @@ static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,static bool bpf_xdp_shrink_data (struct xdp_buff *xdp,skb_frag_t *frag,int shrink) { -struct xdp_mem_info *mem_info = &xdp->rxq->mem; +enum xdp_mem_type mem_type = xdp->rxq->mem.type;bool release =skb_frag_size (frag) == shrink; -if (mem_info->type ==MEM_TYPE_XSK_BUFF_POOL ) { -bpf_xdp_shrink_data_zc (xdp, shrink, mem_info, release); +if (mem_type ==MEM_TYPE_XSK_BUFF_POOL ) { +bpf_xdp_shrink_data_zc (xdp, shrink, mem_type, release); goto out; }if (release) {struct page *page =skb_frag_page (frag); -__xdp_return (page_address (page), mem_info, false,NULL ); +__xdp_return (page_address (page), mem_type, false,NULL ); } out:
net/core/xdp.c +14 -14
@@ -430,12 +430,12 @@ EXPORT_SYMBOL_GPL(xdp_rxq_info_attach_page_pool);* is used for those calls sites. Thus, allowing for faster recycling * of xdp_frames/pages in those cases. */ -void __xdp_return (void *data,struct xdp_mem_info *mem,bool napi_direct, +void __xdp_return (void *data,enum xdp_mem_type mem_type,bool napi_direct,struct xdp_buff *xdp) {struct page *page; -switch (mem->type) { +switch (mem_type) {case MEM_TYPE_PAGE_POOL : page =virt_to_head_page (data);if (napi_direct &&xdp_return_frame_no_direct ())
@@ -458,7 +458,7 @@ void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct,break ;default :/* Not possible, checked in xdp_rxq_info_reg_mem_model() */ -WARN (1 ,"Incorrect XDP memory type (%d) usage" , mem->type); +WARN (1 ,"Incorrect XDP memory type (%d) usage" , mem_type);break ; } }
@@ -475,10 +475,10 @@ void xdp_return_frame(struct xdp_frame *xdpf)for (i =0 ; i < sinfo->nr_frags; i++) {struct page *page =skb_frag_page (&sinfo->frags[i]); -__xdp_return (page_address (page), &xdpf->mem, false,NULL ); +__xdp_return (page_address (page), xdpf->mem_type, false,NULL ); } out: -__xdp_return (xdpf->data, &xdpf->mem, false,NULL ); +__xdp_return (xdpf->data, xdpf->mem_type, false,NULL ); }EXPORT_SYMBOL_GPL (xdp_return_frame);
@@ -494,10 +494,10 @@ void xdp_return_frame_rx_napi(struct xdp_frame *xdpf)for (i =0 ; i < sinfo->nr_frags; i++) {struct page *page =skb_frag_page (&sinfo->frags[i]); -__xdp_return (page_address (page), &xdpf->mem, true,NULL ); +__xdp_return (page_address (page), xdpf->mem_type, true,NULL ); } out: -__xdp_return (xdpf->data, &xdpf->mem, true,NULL ); +__xdp_return (xdpf->data, xdpf->mem_type, true,NULL ); }EXPORT_SYMBOL_GPL (xdp_return_frame_rx_napi);
@@ -516,7 +516,7 @@ EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi);void xdp_return_frame_bulk (struct xdp_frame *xdpf,struct xdp_frame_bulk *bq) { -if (xdpf->mem.type !=MEM_TYPE_PAGE_POOL ) { +if (xdpf->mem_type !=MEM_TYPE_PAGE_POOL ) {xdp_return_frame (xdpf);return ; }
@@ -553,10 +553,11 @@ void xdp_return_buff(struct xdp_buff *xdp)for (i =0 ; i < sinfo->nr_frags; i++) {struct page *page =skb_frag_page (&sinfo->frags[i]); -__xdp_return (page_address (page), &xdp->rxq->mem, true, xdp); +__xdp_return (page_address (page), xdp->rxq->mem.type, true, + xdp); } out: -__xdp_return (xdp->data, &xdp->rxq->mem, true, xdp); +__xdp_return (xdp->data, xdp->rxq->mem.type, true, xdp); }EXPORT_SYMBOL_GPL (xdp_return_buff);
@@ -602,7 +603,7 @@ struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) xdpf->headroom =0 ; xdpf->metasize = metasize; xdpf->frame_sz =PAGE_SIZE ; - xdpf->mem.type =MEM_TYPE_PAGE_ORDER0 ; + xdpf->mem_type =MEM_TYPE_PAGE_ORDER0 ;xsk_buff_free (xdp);return xdpf;
@@ -672,7 +673,7 @@ struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,* - RX ring dev queue index (skb_record_rx_queue) */ -if (xdpf->mem.type ==MEM_TYPE_PAGE_POOL ) +if (xdpf->mem_type ==MEM_TYPE_PAGE_POOL )skb_mark_for_recycle (skb);/* Allow SKB to reuse area used by xdp_frame */
@@ -719,8 +720,7 @@ struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf) nxdpf = addr; nxdpf->data = addr + headroom; nxdpf->frame_sz =PAGE_SIZE ; - nxdpf->mem.type =MEM_TYPE_PAGE_ORDER0 ; - nxdpf->mem.id =0 ; + nxdpf->mem_type =MEM_TYPE_PAGE_ORDER0 ;return nxdpf; }
Signature · 1 line
2.47.1
PATCH 3/12 xdp: make __xdp_return() MP-agnostic
In reply to Alexander LobakinCurrently, __xdp_return() takes pointer to the virtual memory to free a buffer. Apart from that this sometimes provokes redundant data <--> page conversions, taking data pointer effectively prevents lots of XDP code to support non-page-backed buffers, as there's no mapping for the non-host memory (data is always NULL).
Just convert it to always take netmem reference. For xdp_return_{buff,frame*}(), this chops off one page_address() per each frag and adds one virt_to_netmem() (same as virt_to_page()) per header buffer. For __xdp_return() itself, it removes one virt_to_page() for MEM_TYPE_PAGE_POOL and another one for MEM_TYPE_PAGE_ORDER0, adding one page_address() for [not really common nowadays] MEM_TYPE_PAGE_SHARED, but the main effect is that the abovementioned functions won't die or memleak anymore if the frame has non-host memory attached and will correctly free those.
--- include/net/xdp.h | 4 ++-- net/core/filter.c | 9 +++------ net/core/xdp.c | 47 +++++++++++++++++++---------------------------- 3 files changed, 24 insertions(+), 36 deletions(-)
include/net/xdp.h +2 -2
@@ -312,8 +312,8 @@ struct xdp_frame *xdp_convert_buff_to_frame(struct xdp_buff *xdp)return xdp_frame; } -void __xdp_return (void *data,enum xdp_mem_type mem_type,bool napi_direct, -struct xdp_buff *xdp); +void __xdp_return (netmem_ref netmem,enum xdp_mem_type mem_type, +bool napi_direct,struct xdp_buff *xdp);void xdp_return_frame (struct xdp_frame *xdpf);void xdp_return_frame_rx_napi (struct xdp_frame *xdpf);void xdp_return_buff (struct xdp_buff *xdp);
net/core/filter.c +3 -6
@@ -4125,7 +4125,7 @@ static void bpf_xdp_shrink_data_zc(struct xdp_buff *xdp, int shrink,if (release) {xsk_buff_del_tail (zc_frag); -__xdp_return (NULL , mem_type, false, zc_frag); +__xdp_return (0 , mem_type, false, zc_frag); }else { zc_frag->data_end -= shrink; }
@@ -4142,11 +4142,8 @@ static bool bpf_xdp_shrink_data(struct xdp_buff *xdp, skb_frag_t *frag, goto out; } -if (release) { -struct page *page =skb_frag_page (frag); - -__xdp_return (page_address (page), mem_type, false,NULL ); - } +if (release) +__xdp_return (skb_frag_netmem (frag), mem_type, false,NULL ); out:return release;
net/core/xdp.c +19 -28
104 lines not on this page · raw message
Signature · 1 line
2.47.1
PATCH 4/12 xdp: add generic xdp_buff_add_frag()
In reply to Alexander LobakinThe code piece which would attach a frag to &xdp_buff is almost identical across the drivers supporting XDP multi-buffer on Rx. Make it a generic elegant "oneliner".
Also, I see lots of drivers calculating frags_truesize as xdp->frame_sz * nr_frags. I can't say this is fully correct, since frags might be backed by chunks of different sizes, especially with stuff like the header split. Even page_pool_alloc() can give you two different truesizes on two subsequent requests to allocate the same buffer size. Add a field to &skb_shared_info (unionized as there's no free slot currently on x86_64) to track the "true" truesize. It can be used later when updating an skb.
--- include/linux/skbuff.h | 16 +++++-- include/net/xdp.h | 96 +++++++++++++++++++++++++++++++++++++++++- net/core/xdp.c | 11 +++++ 3 files changed, 118 insertions(+), 5 deletions(-)
include/linux/skbuff.h +12 -4
@@ -608,11 +608,19 @@ struct skb_shared_info {* Warning : all fields before dataref are cleared in __alloc_skb() */ atomic_t dataref; -unsigned int xdp_frags_size; -/* Intermediate layers must ensure that destructor_arg -* remains valid until skb destructor */ -void * destructor_arg; +union { +struct { +u32 xdp_frags_size; +u32 xdp_frags_truesize; + }; + +/* +* Intermediate layers must ensure that destructor_arg +* remains valid until skb destructor. +*/ +void *destructor_arg; + };/* must be last field, see pskb_expand_head() */ skb_frag_t frags[MAX_SKB_FRAGS ];
include/net/xdp.h +95 -1
108 lines not on this page · raw message
net/core/xdp.c +11 -0
@@ -535,6 +535,17 @@ void xdp_return_frame_bulk(struct xdp_frame *xdpf, }EXPORT_SYMBOL_GPL (xdp_return_frame_bulk); +/** +* xdp_return_frag -- free one XDP frag or decrement its refcount +* @netmem: network memory reference to release +* @xdp: &xdp_buff to release the frag for +*/ +void xdp_return_frag (netmem_ref netmem,const struct xdp_buff *xdp) +{ +__xdp_return (netmem, xdp->rxq->mem.type, true,NULL ); +} +EXPORT_SYMBOL_GPL (xdp_return_frag); +void xdp_return_buff (struct xdp_buff *xdp) {struct skb_shared_info *sinfo;
Signature · 1 line
2.47.1
PATCH 5/12 xdp: add generic xdp_build_skb_from_buff()
In reply to Alexander LobakinThe code which builds an skb from an &xdp_buff keeps multiplying itself around the drivers with almost no changes. Let's try to stop that by adding a generic function.
Unlike __xdp_build_skb_from_frame(), always allocate an skbuff head using napi_build_skb() and make use of the available xdp_rxq pointer to assign the Rx queue index. In case of PP-backed buffer, mark the skb to be recycled, as every PP user's been switched to recycle skbs.
--- include/net/xdp.h | 1 + net/core/xdp.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
include/net/xdp.h +1 -0
@@ -336,6 +336,7 @@ xdp_update_skb_shared_info(struct sk_buff *skb, u8 nr_frags,void xdp_warn (const char *msg,const char *func,const int line);#define XDP_WARN (msg) xdp_warn(msg, __func__, __LINE__) +struct sk_buff *xdp_build_skb_from_buff (const struct xdp_buff *xdp);struct xdp_frame *xdp_convert_zc_to_xdp_frame (struct xdp_buff *xdp); struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf, struct sk_buff *skb,
net/core/xdp.c +55 -0
61 lines not on this page · raw message
Signature · 1 line
2.47.1
Jakub Kicinski
maintainerRe: [PATCH net-next 05/12] xdp: add generic xdp_build_skb_from_buff()
net/core/xdp.c int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp) · line 668
Quoted 2 lines · On Wed, 11 Dec 2024 18:26:42 +0100 Alexander Lobakin wrote:
net/core/xdp.c +2
-0+if (rxq->mem.type ==MEM_TYPE_PAGE_POOL &&is_page_pool_compiled_in ()) +skb_mark_for_recycle (skb);
I feel like the check for mem.type is unnecessary. I can't think of a driver that would build a skb out of pp pages, and not own pp refs on those pages. Setting pp_recycle on non-pp skb should be safe, even if slightly wasteful.
Also:
static inline void skb_mark_for_recycle(struct sk_buff *skb) {
#ifdef CONFIG_PAGE_POOL skb->pp_recycle = 1;
#endif
}
You don't have to check if PP is complied in explicitly.
Re: [PATCH net-next 05/12] xdp: add generic xdp_build_skb_from_buff()
From: Jakub Kicinski <[email protected]>
Date: Thu, 12 Dec 2024 18:11:29 -0800
Quoted 6 lines · On Wed, 11 Dec 2024 18:26:42 +0100 Alexander Lobakin wrote:
net/core/xdp.c int xdp_alloc_skb_bulk(void **skbs, int n_skb, gfp_t gfp) · line 668
Quoted 2 lines
net/core/xdp.c +2
-0+if (rxq->mem.type ==MEM_TYPE_PAGE_POOL &&is_page_pool_compiled_in ()) +skb_mark_for_recycle (skb);I feel like the check for mem.type is unnecessary. I can't think of a driver that would build a skb out of pp pages, and not own pp
But there's no restriction that this function is limited to PP pages.
Quoted 5 lines
refs on those pages. Setting
pp_recycleon non-pp skb should be safe, even if slightly wasteful.Also:
Quoted 8 lines
static inline void
skb_mark_for_recycle(struct sk_buff *skb){#ifdef CONFIG_PAGE_POOL skb->pp_recycle = 1;#endif
}You don't have to check if PP is complied in explicitly.
Ah I see ._. My idea was to compile-out this check/branch at all when the PP is not compiled in, but I think that it would be optimized out anyway by the compiler, so correct.
Thanks,
Olek
PATCH 6/12 xsk: make xsk_buff_add_frag really add the frag via __xdp_buff_add_frag()
In reply to Alexander LobakinCurrently, xsk_buff_add_frag() only adds the frag to pool's linked list, not doing anything with the &xdp_buff. The drivers do that manually and the logic is the same.
Make it really add an skb frag, just like xdp_buff_add_frag() does that, and freeing frags on error if needed. This allows to remove repeating code from i40e and ice and not add the same code again and again.
--- include/net/xdp_sock_drv.h | 18 ++++++++++-- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 30 ++------------------ drivers/net/ethernet/intel/ice/ice_xsk.c | 32 ++-------------------- 3 files changed, 20 insertions(+), 60 deletions(-)
include/net/xdp_sock_drv.h +15 -3
@@ -136,11 +136,21 @@ static inline void xsk_buff_free(struct xdp_buff *xdp)xp_free (xskb); } -static inline void xsk_buff_add_frag (struct xdp_buff *xdp) +static inline bool xsk_buff_add_frag (struct xdp_buff *head, +struct xdp_buff *xdp) { - struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp); +const void *data = xdp->data; +struct xdp_buff_xsk *frag; + +if (!__xdp_buff_add_frag (head,virt_to_netmem (data), +offset_in_page (data), xdp->data_end - data, + xdp->frame_sz, false)) +return false; + frag = container_of(xdp, struct xdp_buff_xsk, xdp);list_add_tail (&frag->list_node, &frag->pool->xskb_list); + +return true; }static inline struct xdp_buff *xsk_buff_get_frag (const struct xdp_buff *first)
@@ -357,8 +367,10 @@ static inline void xsk_buff_free(struct xdp_buff *xdp) { } -static inline void xsk_buff_add_frag (struct xdp_buff *xdp) +static inline bool xsk_buff_add_frag (struct xdp_buff *head, +struct xdp_buff *xdp) { +return false; }static inline struct xdp_buff *xsk_buff_get_frag (const struct xdp_buff *first)
drivers/net/ethernet/intel/i40e/i40e_xsk.c +3 -27
@@ -395,32 +395,6 @@ static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring,WARN_ON_ONCE (1 ); } -static int -i40e_add_xsk_frag (struct i40e_ring *rx_ring,struct xdp_buff *first, -struct xdp_buff *xdp,const unsigned int size) -{ -struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (first); - -if (!xdp_buff_has_frags (first)) { - sinfo->nr_frags =0 ; - sinfo->xdp_frags_size =0 ; -xdp_buff_set_frags_flag (first); - } - -if (unlikely (sinfo->nr_frags ==MAX_SKB_FRAGS )) { -xsk_buff_free (first); -return -ENOMEM ; - } - -__skb_fill_page_desc_noacc (sinfo, sinfo->nr_frags++, -virt_to_page (xdp->data_hard_start), -XDP_PACKET_HEADROOM , size); - sinfo->xdp_frags_size += size; -xsk_buff_add_frag (xdp); - -return 0 ; -} -/** * i40e_clean_rx_irq_zc - Consumes Rx packets from the hardware ring * @rx_ring: Rx ring
@@ -486,8 +460,10 @@ int i40e_clean_rx_irq_zc(struct i40e_ring *rx_ring, int budget)if (!first) first = bi; -else if (i40e_add_xsk_frag (rx_ring, first, bi, size)) +else if (!xsk_buff_add_frag (first, bi)) { +xsk_buff_free (first);break ; + }if (++next_to_process == count) next_to_process =0 ;
drivers/net/ethernet/intel/ice/ice_xsk.c +2 -30
@@ -801,35 +801,6 @@ ice_run_xdp_zc(struct ice_rx_ring *rx_ring, struct xdp_buff *xdp,return result; } -static int -ice_add_xsk_frag (struct ice_rx_ring *rx_ring,struct xdp_buff *first, -struct xdp_buff *xdp,const unsigned int size) -{ -struct skb_shared_info *sinfo =xdp_get_shared_info_from_buff (first); - -if (!size) -return 0 ; - -if (!xdp_buff_has_frags (first)) { - sinfo->nr_frags =0 ; - sinfo->xdp_frags_size =0 ; -xdp_buff_set_frags_flag (first); - } - -if (unlikely (sinfo->nr_frags ==MAX_SKB_FRAGS )) { -xsk_buff_free (first); -return -ENOMEM ; - } - -__skb_fill_page_desc_noacc (sinfo, sinfo->nr_frags++, -virt_to_page (xdp->data_hard_start), -XDP_PACKET_HEADROOM , size); - sinfo->xdp_frags_size += size; -xsk_buff_add_frag (xdp); - -return 0 ; -} -/** * ice_clean_rx_irq_zc - consumes packets from the hardware ring * @rx_ring: AF_XDP Rx ring
@@ -895,7 +866,8 @@ int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring,if (!first) { first = xdp; - }else if (ice_add_xsk_frag (rx_ring, first, xdp, size)) { + }else if (likely (size) && !xsk_buff_add_frag (first, xdp)) { +xsk_buff_free (first);break ; }
Signature · 1 line
2.47.1
PATCH 7/12 xsk: add generic XSk &xdp_buff -> skb conversion
In reply to Alexander LobakinSame as with converting &xdp_buff to skb on Rx, the code which allocates a new skb and copies the XSk frame there is identical across the drivers, so make it generic. This includes copying all the frags if they are present in the original buff.
System percpu Page Pools help here a lot: when available, allocate pages from there instead of the MM layer. This greatly improves XDP_PASS performance on XSk: instead of page_alloc() + page_free(), the net core recycles the same pages, so the only overhead left is memcpy()s. Note that the passed buff gets freed if the conversion is done w/o any error, assuming you don't need this buffer after you convert it to an skb.
--- include/net/xdp.h | 1 + net/core/xdp.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+)
include/net/xdp.h +1 -0
@@ -337,6 +337,7 @@ void xdp_warn(const char *msg, const char *func, const int line);#define XDP_WARN (msg) xdp_warn(msg, __func__, __LINE__)struct sk_buff *xdp_build_skb_from_buff (const struct xdp_buff *xdp); +struct sk_buff *xdp_build_skb_from_zc (struct xdp_buff *xdp);struct xdp_frame *xdp_convert_zc_to_xdp_frame (struct xdp_buff *xdp); struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf, struct sk_buff *skb,
net/core/xdp.c +138 -0
150 lines not on this page · raw message
Signature · 1 line
2.47.1
Jakub Kicinski
maintainerRe: [PATCH net-next 07/12] xsk: add generic XSk &xdp_buff -> skb conversion
net/core/xdp.c struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp) · line 799
Quoted 13 lines · On Wed, 11 Dec 2024 18:26:44 +0100 Alexander Lobakin wrote:
net/core/xdp.c +13
-0+#else /* !CONFIG_PAGE_POOL */ +struct napi_struct *napi; + + pp =NULL ; + napi =napi_by_id (rxq->napi_id); +if (likely (napi)) + skb =napi_alloc_skb (napi, len); +else + skb =__netdev_alloc_skb_ip_align (rxq->dev, len, +GFP_ATOMIC | __GFP_NOWARN); +if (unlikely (!skb)) +return NULL ; +#endif /* !CONFIG_PAGE_POOL */
What are the chances of having a driver with AF_XDP support and without page pool support? I think it's zero :S Can we kill all the if !CONFIG_PAGE_POOL sections and hide the entire helper under if CONFIG_PAGE_POLL ?
Re: [PATCH net-next 07/12] xsk: add generic XSk &xdp_buff -> skb conversion
From: Jakub Kicinski <[email protected]>
Date: Thu, 12 Dec 2024 18:19:44 -0800
Quoted 9 lines · On Wed, 11 Dec 2024 18:26:44 +0100 Alexander Lobakin wrote:
Quoted 8 lines
net/core/xdp.c +8
-0+#else /* !CONFIG_PAGE_POOL */ +struct napi_struct *napi; + + pp =NULL ; + napi =napi_by_id (rxq->napi_id); +if (likely (napi)) + skb =napi_alloc_skb (napi, len); +else
Quoted 8 lines
net/core/xdp.c struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp) · line 799
Quoted 5 lines
net/core/xdp.c +5
-0+ skb =__netdev_alloc_skb_ip_align (rxq->dev, len, +GFP_ATOMIC | __GFP_NOWARN); +if (unlikely (!skb)) +return NULL ; +#endif /* !CONFIG_PAGE_POOL */ What are the chances of having a driver with
AF_XDPsupport and without page pool support? I think it's zero :S
Right, as CONFIG_BPF_SYSCALL selects PAGE_POOL if NET.
I think I wrote this when it wasn't true (before Lorenzo introduced generic page_pools) and then just forgot to change that ._.
Quoted 2 lines
Can we kill all the if !CONFIG_PAGE_POOL sections and hide the entire helper under if
CONFIG_PAGE_POLL?
We can. But I think I'd need to introduce a return-NULL wrapper in case of !PAGE_POOL to satisfy the linker, as lots of drivers build their XSk code unconditionally.
Thanks,
Olek
Jakub Kicinski
maintainerRe: [PATCH net-next 07/12] xsk: add generic XSk &xdp_buff -> skb conversion
Quoted 6 lines · On Fri, 13 Dec 2024 18:31:59 +0100 Alexander Lobakin wrote:
Quoted 2 lines
Can we kill all the if !CONFIG_PAGE_POOL sections and hide the entire helper under if
CONFIG_PAGE_POLL?We can. But I think I'd need to introduce a return-NULL wrapper in case of !PAGE_POOL to satisfy the linker, as lots of drivers build their XSk code unconditionally.
Oh wow, you're right. Bunch of drivers of a certain vendor still don't use page pool.. return NULL wrapped SGTM.
PATCH 8/12 xsk: add helper to get &xdp_desc's DMA and meta pointer in one go
In reply to Alexander LobakinCurrently, when you send an XSk frame with metadata, you need to do the following:
* call external xsk_buff_raw_get_dma();
* call inline xsk_buff_get_metadata(), which calls external xsk_buff_raw_get_data() and then do some inline checks.
This effectively means that the following piece:
addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
is done twice per frame, plus you have 2 external calls per frame, plus this:
meta = pool->addrs + addr - pool->tx_metadata_len; if (unlikely(!xsk_buff_valid_tx_metadata(meta)))
is always inlined, even if there's no meta or it's invalid.
Add xsk_buff_raw_get_ctx() (xp_raw_get_ctx() to be precise) to do that in one go. It returns a small structure with 2 fields: DMA address, filled unconditionally, and metadata pointer, valid only if it's present. The address correction is performed only once and you also have only 1 external call per XSk frame, which does all the calculations and checks outside of your hotpath. You only need to check if (ctx.meta) for the metadata presence.
--- include/net/xdp_sock_drv.h | 23 +++++++++++++++++++++ include/net/xsk_buff_pool.h | 8 ++++++++ net/xdp/xsk_buff_pool.c | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+)
include/net/xdp_sock_drv.h +23 -0
@@ -205,6 +205,23 @@ static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)return xp_raw_get_data (pool, addr); } +/** +* xsk_buff_raw_get_ctx - get &xdp_desc context +* @pool: XSk buff pool desc address belongs to +* @addr: desc address (from userspace) +* +* Wrapper for xp_raw_get_ctx() to be used in drivers, see its kdoc for +* details. +* +* Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata +* pointer, if it is present and valid (initialized to %NULL otherwise). +*/ +static inline struct xdp_desc_ctx +xsk_buff_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr) +{ +return xp_raw_get_ctx (pool, addr); +} +#define XDP_TXMD_FLAGS_VALID ( \ XDP_TXMD_FLAGS_TIMESTAMP | \ XDP_TXMD_FLAGS_CHECKSUM | \
@@ -402,6 +419,12 @@ static inline void *xsk_buff_raw_get_data(struct xsk_buff_pool *pool, u64 addr)return NULL ; } +static inline struct xdp_desc_ctx +xsk_buff_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr) +{ +return (struct xdp_desc_ctx ){ }; +} +static inline bool xsk_buff_valid_tx_metadata (struct xsk_tx_metadata *meta) {return false;
include/net/xsk_buff_pool.h +8 -0
@@ -141,6 +141,14 @@ u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max);bool xp_can_alloc (struct xsk_buff_pool *pool,u32 count);void *xp_raw_get_data (struct xsk_buff_pool *pool,u64 addr);dma_addr_t xp_raw_get_dma (struct xsk_buff_pool *pool,u64 addr); + +struct xdp_desc_ctx { +dma_addr_t dma; +struct xsk_tx_metadata *meta; +}; + +struct xdp_desc_ctx xp_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr); +static inline dma_addr_t xp_get_dma (struct xdp_buff_xsk *xskb) {return xskb->dma;
net/xdp/xsk_buff_pool.c +40 -0
@@ -715,3 +715,43 @@ dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) (addr & ~PAGE_MASK ); }EXPORT_SYMBOL (xp_raw_get_dma); + +/** +* xp_raw_get_ctx - get &xdp_desc context +* @pool: XSk buff pool desc address belongs to +* @addr: desc address (from userspace) +* +* Helper for getting desc's DMA address and metadata pointer, if present. +* Saves one call on hotpath, double calculation of the actual address, +* and inline checks for metadata presence and sanity. +* Please use xsk_buff_raw_get_ctx() in drivers instead. +* +* Return: new &xdp_desc_ctx struct containing desc's DMA address and metadata +* pointer, if it is present and valid (initialized to %NULL otherwise). +*/ +struct xdp_desc_ctx xp_raw_get_ctx (const struct xsk_buff_pool *pool,u64 addr) +{ +struct xsk_tx_metadata *meta; +struct xdp_desc_ctx ret; + + addr = pool->unaligned ?xp_unaligned_add_offset_to_addr (addr) : addr; + ret = (typeof (ret )){ +/* Same logic as in xp_raw_get_dma() */ + .dma = (pool->dma_pages[addr >>PAGE_SHIFT ] & + ~XSK_NEXT_PG_CONTIG_MASK ) + (addr & ~PAGE_MASK ), + }; + +if (!pool->tx_metadata_len) + goto out; + +/* Same logic as in xp_raw_get_data() + xsk_buff_get_metadata() */ + meta = pool->addrs + addr - pool->tx_metadata_len; +if (unlikely (!xsk_buff_valid_tx_metadata (meta))) + goto out; + + ret.meta = meta; + +out: +return ret; +} +EXPORT_SYMBOL (xp_raw_get_ctx);
Signature · 1 line
2.47.1
PATCH 9/12 page_pool: add a couple of netmem counterparts
In reply to Alexander LobakinAdd the following Page Pool netmem wrappers to be able to implement an MP-agnostic driver:
* page_pool{,_dev}_alloc_best_fit_netmem()
Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around the new helper (as a page is always a netmem, but not vice versa). 'page_pool_alloc_netmem' is already busy, hence '_best_fit' (which also says what the helper tries to do).
* page_pool_dma_sync_for_cpu_netmem()
Same as page_pool_dma_sync_for_cpu(). Performs DMA sync only if the netmem comes from the host.
--- include/net/page_pool/helpers.h | 46 ++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 9 deletions(-)
include/net/page_pool/helpers.h +37 -9
@@ -115,22 +115,22 @@ static inline struct page *page_pool_dev_alloc_frag(struct page_pool *pool,return page_pool_alloc_frag (pool, offset, size, gfp); } -static inline struct page *page_pool_alloc (struct page_pool *pool, -unsigned int *offset, -unsigned int *size,gfp_t gfp) +static inline netmem_ref +page_pool_alloc_best_fit_netmem (struct page_pool *pool,unsigned int *offset, +unsigned int *size,gfp_t gfp) {unsigned int max_size =PAGE_SIZE << pool->p.order; -struct page *page; +netmem_ref netmem;if ((*size <<1 ) > max_size) { *size = max_size; *offset =0 ; -return page_pool_alloc_pages (pool, gfp); +return page_pool_alloc_netmem (pool, gfp); } - page =page_pool_alloc_frag (pool, offset, *size, gfp); -if (unlikely (!page)) -return NULL ; + netmem =page_pool_alloc_frag_netmem (pool, offset, *size, gfp); +if (unlikely (!netmem)) +return 0 ;/* There is very likely not enough space for another fragment, so append * the remaining size to the current fragment to avoid truesize
@@ -141,7 +141,25 @@ static inline struct page *page_pool_alloc(struct page_pool *pool, pool->frag_offset = max_size; } -return page; +return netmem; +} + +static inline netmem_ref +page_pool_dev_alloc_best_fit_netmem (struct page_pool *pool, +unsigned int *offset, +unsigned int *size) +{ +gfp_t gfp =GFP_ATOMIC | __GFP_NOWARN; + +return page_pool_alloc_best_fit_netmem (pool, offset, size, gfp); +} + +static inline struct page *page_pool_alloc (struct page_pool *pool, +unsigned int *offset, +unsigned int *size,gfp_t gfp) +{ +return netmem_to_page (page_pool_alloc_best_fit_netmem (pool, offset, + size, gfp)); }/**
@@ -440,6 +458,16 @@ static inline void page_pool_dma_sync_for_cpu(const struct page_pool *pool, page_pool_get_dma_dir(pool)); } +static inline void +page_pool_dma_sync_for_cpu_netmem (const struct page_pool *pool, +netmem_ref netmem,u32 offset, +u32 dma_sync_size) +{ +if (!netmem_is_net_iov (netmem)) +page_pool_dma_sync_for_cpu (pool,netmem_to_page (netmem), + offset, dma_sync_size); +} +static inline bool page_pool_put (struct page_pool *pool) {return refcount_dec_and_test (&pool->user_cnt);
Signature · 1 line
2.47.1
Re: [PATCH net-next 09/12] page_pool: add a couple of netmem counterparts
Quoted 4 lines · Alexander Lobakin
Add the following Page Pool netmem wrappers to be able to implement an MP-agnostic driver:
Sorry, we raced a bit here. Jakub merged my "page_pool_alloc_netmem", which does similar to what this patch does.
Quoted 7 lines
* page_pool{,_dev}_alloc_best_fit_netmem()
Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around the new helper (as a page is always a netmem, but not vice versa). '
page_pool_alloc_netmem' is already busy, hence '_best_fit' (which also says what the helper tries to do).
I freed the page_pool_alloc_netmem name by doing a rename, and now page_pool_alloc_netmem is the netmem counterpart to page_pool_alloc. I did not however add a page_pool_dev_alloc equivalent.
Quoted 5 lines
*
page_pool_dma_sync_for_cpu_netmem()Same as
page_pool_dma_sync_for_cpu(). Performs DMA sync only if the netmem comes from the host.
My series also adds page_pool_dma_sync_netmem_for_cpu, which should be the same as your page_pool_dma_sync_for_cpu_netmem.
Re: [PATCH net-next 09/12] page_pool: add a couple of netmem counterparts
From: Mina Almasry <[email protected]>
Date: Fri, 13 Dec 2024 11:13:33 -0800
Quoted 22 lines
Quoted 4 lines · Alexander Lobakin
Add the following Page Pool netmem wrappers to be able to implement an MP-agnostic driver:
Sorry, we raced a bit here. Jakub merged my "
page_pool_alloc_netmem", which does similar to what this patch does.Quoted 7 lines
* page_pool{,_dev}_alloc_best_fit_netmem()
Same as page_pool{,_dev}_alloc(). Make the latter a wrapper around the new helper (as a page is always a netmem, but not vice versa). '
page_pool_alloc_netmem' is already busy, hence '_best_fit' (which also says what the helper tries to do).I freed the
page_pool_alloc_netmemname by doing a rename, and nowpage_pool_alloc_netmemis the netmem counterpart topage_pool_alloc. I did not however add apage_pool_dev_allocequivalent.
Quoted 8 lines
Quoted 5 lines
*
page_pool_dma_sync_for_cpu_netmem()Same as
page_pool_dma_sync_for_cpu(). Performs DMA sync only if the netmem comes from the host.My series also adds
page_pool_dma_sync_netmem_for_cpu, which should be the same as yourpage_pool_dma_sync_for_cpu_netmem.
Yep, I saw your changes, rebasing soon.
Thanks,
Olek
PATCH 10/12 skbuff: allow 2-4-argument skb_frag_dma_map()
In reply to Alexander Lobakinskb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), DMA_TO_DEVICE) is repeated across dozens of drivers and really wants a shorthand. Add a macro which will count args and handle all possible number from 2 to 5. Semantics:
skb_frag_dma_map(dev, frag) ->__skb_frag_dma_map(dev, frag, 0, skb_frag_size(frag), DMA_TO_DEVICE)
skb_frag_dma_map(dev, frag, offset) -> __skb_frag_dma_map(dev, frag, offset, skb_frag_size(frag) - offset,
DMA_TO_DEVICE)
skb_frag_dma_map(dev, frag, offset, size) -> __skb_frag_dma_map(dev, frag, offset, size, DMA_TO_DEVICE)
skb_frag_dma_map(dev, frag, offset, size, dir) ->__skb_frag_dma_map(dev, frag, offset, size, dir)
No object code size changes for the existing callers. Users passing less arguments also won't have bigger size comparing to the full equivalent call.
--- include/linux/skbuff.h | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-)
include/linux/skbuff.h +26 -5
@@ -3682,7 +3682,7 @@ static inline void skb_frag_page_copy(skb_frag_t *fragto,bool skb_page_frag_refill (unsigned int sz,struct page_frag *pfrag,gfp_t prio);/** -* skb_frag_dma_map - maps a paged fragment via the DMA API +* __skb_frag_dma_map - maps a paged fragment via the DMA API * @dev: the device to map the fragment to * @frag: the paged fragment to map * @offset: the offset within the fragment (starting at the
@@ -3692,15 +3692,36 @@ bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);* * Maps the page associated with @frag to @device. */ -static inline dma_addr_t skb_frag_dma_map (struct device *dev, -const skb_frag_t *frag, -size_t offset,size_t size, -enum dma_data_direction dir) +static inline dma_addr_t __skb_frag_dma_map (struct device *dev, +const skb_frag_t *frag, +size_t offset,size_t size, +enum dma_data_direction dir) {return dma_map_page (dev,skb_frag_page (frag),skb_frag_off (frag) + offset, size, dir); } +#define skb_frag_dma_map (dev, frag, ...) \ + CONCATENATE(_skb_frag_dma_map, \ + COUNT_ARGS(__VA_ARGS__))(dev, frag, ##__VA_ARGS__) + +#define __skb_frag_dma_map1 (dev, frag, offset, uf, uo) ({ \ + const skb_frag_t *uf = (frag); \ + size_t uo = (offset); \ + \ + __skb_frag_dma_map(dev, uf, uo, skb_frag_size(uf) - uo, \ + DMA_TO_DEVICE); \ +}) +#define _skb_frag_dma_map1 (dev, frag, offset) \ + __skb_frag_dma_map1(dev, frag, offset, __UNIQUE_ID(frag_), \ + __UNIQUE_ID(offset_)) +#define _skb_frag_dma_map0 (dev, frag) \ + _skb_frag_dma_map1(dev, frag, 0) +#define _skb_frag_dma_map2 (dev, frag, offset, size) \ + __skb_frag_dma_map(dev, frag, offset, size, DMA_TO_DEVICE) +#define _skb_frag_dma_map3 (dev, frag, offset, size, dir) \ + __skb_frag_dma_map(dev, frag, offset, size, dir) +static inline struct sk_buff *pskb_copy (struct sk_buff *skb,gfp_t gfp_mask) {
Signature · 1 line
2.47.1
PATCH 11/12 jump_label: export static_key_slow_{inc,dec}_cpuslocked()
In reply to Alexander LobakinSometimes, there's a need to modify a lot of static keys or modify the same key multiple times in a loop. In that case, it seems more optimal to lock cpu_read_lock once and then call _cpuslocked() variants. The enable/disable functions are already exported, the refcounted counterparts however are not. Fix that to allow modules to save some cycles.
--- kernel/jump_label.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/jump_label.c +2 -0
@@ -182,6 +182,7 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key) }return true; } +EXPORT_SYMBOL_GPL (static_key_slow_inc_cpuslocked);bool static_key_slow_inc (struct static_key *key) {
@@ -342,6 +343,7 @@ void static_key_slow_dec_cpuslocked(struct static_key *key)STATIC_KEY_CHECK_USE (key);__static_key_slow_dec_cpuslocked (key); } +EXPORT_SYMBOL_GPL (static_key_slow_dec_cpuslocked); void __static_key_slow_dec_deferred(struct static_key *key, struct delayed_work *work,
Signature · 1 line
2.47.1
Josh Poimboeuf
maintainerRe: [PATCH net-next 11/12] jump_label: export static_key_slow_{inc,dec}_cpuslocked()
Quoted 25 lines · Alexander Lobakin
Sometimes, there's a need to modify a lot of static keys or modify the same key multiple times in a loop. In that case, it seems more optimal to lock
cpu_read_lockonce and then call_cpuslocked()variants. The enable/disable functions are already exported, the refcounted counterparts however are not. Fix that to allow modules to save some cycles.Signed-off-by Alexander Lobakin <[email protected]>--- kernel/jump_label.c | 2 ++ 1 file changed, 2 insertions(+)kernel/jump_label.c +1
-0@@ -182,6 +182,7 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key) }return true; } +EXPORT_SYMBOL_GPL (static_key_slow_inc_cpuslocked);bool static_key_slow_inc (struct static_key *key) {
kernel/jump_label.c void static_key_slow_dec_cpuslocked(struct static_key *key) · line 346
Quoted 4 lines
kernel/jump_label.c +1
-0@@ -342,6 +343,7 @@ void static_key_slow_dec_cpuslocked(struct static_key *key)STATIC_KEY_CHECK_USE (key);__static_key_slow_dec_cpuslocked (key); } +EXPORT_SYMBOL_GPL (static_key_slow_dec_cpuslocked);
Where's the code which uses this?
Signature · 1 line
Josh
Re: [PATCH net-next 11/12] jump_label: export static_key_slow_{inc,dec}_cpuslocked()
From: Josh Poimboeuf <[email protected]>
Date: Wed, 11 Dec 2024 09:40:00 -0800
Quoted 23 lines · Alexander Lobakin
Quoted 22 lines
Sometimes, there's a need to modify a lot of static keys or modify the same key multiple times in a loop. In that case, it seems more optimal to lock
cpu_read_lockonce and then call_cpuslocked()variants. The enable/disable functions are already exported, the refcounted counterparts however are not. Fix that to allow modules to save some cycles.Signed-off-by Alexander Lobakin <[email protected]>--- kernel/jump_label.c | 2 ++ 1 file changed, 2 insertions(+)kernel/jump_label.c +1
-0@@ -182,6 +182,7 @@ bool static_key_slow_inc_cpuslocked(struct static_key *key) }return true; } +EXPORT_SYMBOL_GPL (static_key_slow_inc_cpuslocked);
Quoted 9 lines
kernel/jump_label.c void static_key_slow_dec_cpuslocked(struct static_key *key) · line 346
Quoted 7 lines
kernel/jump_label.c +1
-0bool static_key_slow_inc (struct static_key *key) {@@ -342,6 +343,7 @@ void static_key_slow_dec_cpuslocked(struct static_key *key)STATIC_KEY_CHECK_USE (key);__static_key_slow_dec_cpuslocked (key); } +EXPORT_SYMBOL_GPL (static_key_slow_dec_cpuslocked);Where's the code which uses this?
It's not in this series -- the initial one was too large, so it was split.
Thanks,
Olek
Josh Poimboeuf
maintainerRe: [PATCH net-next 11/12] jump_label: export static_key_slow_{inc,dec}_cpuslocked()
Quoted 6 lines · Alexander Lobakin
From: Josh Poimboeuf <[email protected]>
Quoted 3 lines
Quoted 1 line
+EXPORT_SYMBOL_GPL(static_key_slow_dec_cpuslocked);
Where's the code which uses this?
It's not in this series -- the initial one was too large, so it was split.
It's best to put the patch exporting the symbol adjacent to (or squashed with) the patch using it so the justification for exporting it can be reviewed at the same time.
Signature · 1 line
Josh
Re: [PATCH net-next 11/12] jump_label: export static_key_slow_{inc,dec}_cpuslocked()
From: Josh Poimboeuf <[email protected]>
Date: Fri, 13 Dec 2024 19:24:31 -0800
Quoted 11 lines · Alexander Lobakin
Quoted 6 lines
From: Josh Poimboeuf <[email protected]>
Quoted 3 lines
Quoted 1 line
+EXPORT_SYMBOL_GPL(static_key_slow_dec_cpuslocked);
Where's the code which uses this?
It's not in this series -- the initial one was too large, so it was split.
It's best to put the patch exporting the symbol adjacent to (or squashed with) the patch using it so the justification for exporting it can be reviewed at the same time.
Sure, I'll move this one to the next series.
Thanks,
Olek
PATCH 12/12 unroll: add generic loop unroll helpers
In reply to Alexander LobakinThere are cases when we need to explicitly unroll loops. For example, cache operations, filling DMA descriptors on very high speeds etc. Add compiler-specific attribute macros to give the compiler a hint that we'd like to unroll a loop.
Example usage:
#define UNROLL_BATCH 8
unrolled_count(UNROLL_BATCH) for (u32 i = 0; i < UNROLL_BATCH; i++) op(priv, i);
Note that sometimes the compilers won't unroll loops if they think this would have worse optimization and perf than without unrolling, and that unroll attributes are available only starting GCC 8. For older compiler versions, no hints/attributes will be applied.
For better unrolling/parallelization, don't have any variables that interfere between iterations except for the iterator itself.
--- include/linux/unroll.h | 44 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+)
include/linux/unroll.h +44 -0
@@ -9,6 +9,50 @@#include <linux/args.h> +#ifdef CONFIG_CC_IS_CLANG +#define __pick_unrolled (x, y) _Pragma(#x) +#elif CONFIG_GCC_VERSION >=80000 +#define __pick_unrolled (x, y) _Pragma(#y) +#else +#define __pick_unrolled (x, y)/* not supported */ +#endif + +/** +* unrolled - loop attributes to ask the compiler to unroll it +* +* Usage: +* +* #define BATCH 8 +* +* unrolled_count(BATCH) +* for (u32 i = 0; i < BATCH; i++) +* // loop body without cross-iteration dependencies +* +* This is only a hint and the compiler is free to disable unrolling if it +* thinks the count is suboptimal and may hurt performance and/or hugely +* increase object code size. +* Not having any cross-iteration dependencies (i.e. when iter x + 1 depends +* on what iter x will do with variables) is not a strict requirement, but +* provides best performance and object code size. +* Available only on Clang and GCC 8.x onwards. +*/ + +/* Ask the compiler to pick an optimal unroll count, Clang only */ +#define unrolled \ + __pick_unrolled(clang loop unroll(enable), /* nothing */) + +/* Unroll each @n iterations of a loop */ +#define unrolled_count (n) \ + __pick_unrolled(clang loop unroll_count(n), GCC unroll n) + +/* Unroll the whole loop */ +#define unrolled_full \ + __pick_unrolled(clang loop unroll(full), GCC unroll 65534) + +/* Never unroll a loop */ +#define unrolled_none \ + __pick_unrolled(clang loop unroll(disable), GCC unroll 1) + #define UNROLL(N, MACRO, args...) CONCATENATE(__UNROLL_, N)(MACRO, args) #define __UNROLL_0(MACRO, args...)
Signature · 1 line
2.47.1
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Wed, 11 Dec 2024 18:26:37 +0100 you wrote:
XDP for idpf is currently 5.5 chapters:
* convert Rx to libeth;
* convert Tx and stats to libeth;
* generic XDP and XSk code changes;
* generic XDP and XSk code additions (you are here);
* actual XDP for idpf via newlibeth_xdp;
* XSk for idpf (via ^).[...]
Here is the summary with links:
- [net-next,01/12] page_pool: allow mixing PPs within one bulk
https://git.kernel.org/netdev/net-next/c/fcc680a647ba - [net-next,02/12] xdp: get rid of xdp_frame::mem.id (no matching commit) - [net-next,03/12] xdp: make __xdp_return() MP-agnostic https://git.kernel.org/netdev/net-next/c/207ff83cecae - [net-next,04/12] xdp: add generic xdp_buff_add_frag() (no matching commit) - [net-next,05/12] xdp: add generic xdp_build_skb_from_buff() (no matching commit) - [net-next,06/12] xsk: make xsk_buff_add_frag really add the frag via __xdp_buff_add_frag() (no matching commit) - [net-next,07/12] xsk: add generic XSk &xdp_buff -> skb conversion (no matching commit) - [net-next,08/12] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go (no matching commit) - [net-next,09/12] page_pool: add a couple of netmem counterparts (no matching commit) - [net-next,10/12] skbuff: allow 2-4-argument skb_frag_dma_map() https://git.kernel.org/netdev/net-next/c/0dffdb3b3366
- [net-next,11/12] jump_label: export static_key_slow_{inc,dec}_cpuslocked() (no matching commit)
- [net-next,12/12] unroll: add generic loop unroll helpers
(no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html