Skip to contentSkip to conversation

xdp: a fistful of generic changes pt. III

PATCH In review Alexander Lobakin 3 participants 13 messages Dec 18, 2024 – Dec 23, 2024

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover xdp: a fistful of generic changes pt. III

XDP for idpf is currently 5.(6) chapters:
* convert Rx to libeth;
* convert Tx and stats to libeth;
* generic XDP and XSk code changes;
* generic XDP and XSk code additions pt. 1;
* generic XDP and XSk code additions pt. 2 (you are here);
* actual XDP for idpf via new libeth_xdp;
* XSk for idpf (via ^).

Part III.3 does the following:
* 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;
* add generic loop unroll hint macros.

Everything is prereq for libeth_xdp, but will be useful standalone as well: less code in drivers, faster XSk XDP_PASS, smaller object code.

Alexander Lobakin (7):
page_pool: add page_pool_dev_alloc_netmem()
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
unroll: add generic loop unroll helpers

 include/linux/skbuff.h                     |  16 +-
 include/linux/unroll.h                     |  44 +++++
 include/net/page_pool/helpers.h            |   9 ++
 include/net/xdp.h                          |  98 +++++++++++-
 include/net/xdp_sock_drv.h                 |  41 ++++-
 include/net/xsk_buff_pool.h                |   8 +
 drivers/net/ethernet/intel/i40e/i40e_xsk.c |  30 +---
 drivers/net/ethernet/intel/ice/ice_xsk.c   |  32 +---
 net/core/xdp.c                             | 178 +++++++++++++++++++++
 net/xdp/xsk_buff_pool.c                    |  40 +++++
 10 files changed, 431 insertions(+), 65 deletions(-)

---
Each patch except trivial 0001 was on the lists already.

Since the not applied part of Chapter III.2:
* rebase on top of Mina's netmem fixes;
* 0003: remove redundant double CONFIG_PAGE_POOL check (one inside and another one outside of skb_mark_for_recycle()) (Jakub);
* 0005: remove !CONFIG_PAGE_POOL code as unreachable (eBPF always selects it) (also Jakub);
* 0005: actually check the pfmemalloc flag of newly allocated frags;
* drop exporting static_key_{inc,dec}_cpuslocked() -- were used on slowpath in very unlikely case where saving a few cycles looked worse and less convienient than the "regular" way.

Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/7 page_pool: add page_pool_dev_alloc_netmem()

Similarly to other _dev shorthands, add one for page_pool_alloc_netmem() to allocate a netmem using the default Rx GFP flags (ATOMIC | NOWARN) to make the page -> netmem transition of drivers easier.

---
 include/net/page_pool/helpers.h | 9 +++++++++
 1 file changed, 9 insertions(+)
include/net/page_pool/helpers.h +9 -0
index 776a3008ac28..543f54fa3020 100644
@@ -144,6 +144,15 @@ static inline netmem_ref page_pool_alloc_netmem(struct page_pool *pool,
 	return netmem;
 }
 
+static inline netmem_ref page_pool_dev_alloc_netmem(struct page_pool *pool,
+						    unsigned int *offset,
+						    unsigned int *size)
+{
+	gfp_t gfp = GFP_ATOMIC | __GFP_NOWARN;
+
+	return page_pool_alloc_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)
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/7 xdp: add generic xdp_buff_add_frag()

In reply to Alexander Lobakin

The 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 the 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
index b2509cd0b930..bb2b751d274a 100644
@@ -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
index f1165a35411b..a66a4e036f53 100644
@@ -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
#3 lore raw

PATCH 3/7 xdp: add generic xdp_build_skb_from_buff()

In reply to Alexander Lobakin

The 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
index 11139c210b49..aa24fa78cbe6 100644
@@ -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
#4 lore raw

PATCH 4/7 xsk: make xsk_buff_add_frag() really add the frag via __xdp_buff_add_frag()

In reply to Alexander Lobakin

Currently, 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
index f3175a5d28f7..86620c818965 100644
@@ -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
index 4e885df789ef..e28f1905a4a0 100644
@@ -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
index 334ae945d640..8975d2971bc3 100644
@@ -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
#5 lore raw

PATCH 5/7 xsk: add generic XSk &xdp_buff -> skb conversion

In reply to Alexander Lobakin

Same 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 greatly improve 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. When the Page Pool is not compiled in, the whole function is a return-NULL (but it always gets selected when eBPF is enabled).
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    | 112 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 113 insertions(+)
include/net/xdp.h +1 -0
index aa24fa78cbe6..6da0e746cf75 100644
@@ -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 +112 -0

118 lines not on this page · raw message

Signature · 1 line
2.47.1
#6 lore raw

PATCH 6/7 xsk: add helper to get &xdp_desc's DMA and meta pointer in one go

In reply to Alexander Lobakin

Currently, 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
index 86620c818965..7fd1709deef5 100644
@@ -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
index 50779406bc2d..1dcd4d71468a 100644
@@ -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
index 1f7975b49657..4ea742da304d 100644
@@ -714,3 +714,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
maintainer
#7 lore raw

Re: [PATCH net-next 6/7] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go

net/xdp/xsk_buff_pool.c dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) · line 741

Quoted 5 lines · On Wed, 18 Dec 2024 18:44:34 +0100 Alexander Lobakin wrote:
net/xdp/xsk_buff_pool.c +5 -0
+	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),
+	};

This is quite ugly IMHO

#8 lore raw

Re: [PATCH net-next 6/7] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go

From: Jakub Kicinski <[email protected]>
Date: Thu, 19 Dec 2024 19:50:58 -0800

Quoted 8 lines · On Wed, 18 Dec 2024 18:44:34 +0100 Alexander Lobakin wrote:

net/xdp/xsk_buff_pool.c dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) · line 741

Quoted 5 lines
net/xdp/xsk_buff_pool.c +5 -0
+	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),
+	};

This is quite ugly IMHO

What exactly: that the logic is copied or how that code (>> & ~ + & ~) looks like?

If the former, I already thought of making a couple internal defs to avoid copying.
If the latter, I also thought of this, just wanted to be clear that it's the same as in xp_raw_get_dma(). But it can be refactored to look more fancy anyway.

Or the compound return looks ugly? Or the struct initialization?

Thanks,
Olek

maintainer
#9 lore raw

Re: [PATCH net-next 6/7] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go

Quoted 12 lines · On Fri, 20 Dec 2024 16:58:57 +0100 Alexander Lobakin wrote:
Quoted 8 lines · On Wed, 18 Dec 2024 18:44:34 +0100 Alexander Lobakin wrote:

net/xdp/xsk_buff_pool.c dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) · line 741

Quoted 5 lines
net/xdp/xsk_buff_pool.c +5 -0
+	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),
+	};  

This is quite ugly IMHO

What exactly: that the logic is copied or how that code (>> & ~ + & ~) looks like?

Quoted 7 lines

If the former, I already thought of making a couple internal defs to avoid copying.
If the latter, I also thought of this, just wanted to be clear that it's the same as in xp_raw_get_dma(). But it can be refactored to look more fancy anyway.

Or the compound return looks ugly? Or the struct initialization?

Compound using typeof() and the fact it's multi line.

It's a two member struct, which you return by value, so unlikely to grow. Why not init the members manually?

And you could save the intermediate computations to a temp variable (addr >> PAGE_SHIFT, addr & ~PAGE_MASK) to make the line shorter.

#10 lore raw

Re: [PATCH net-next 6/7] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go

From: Jakub Kicinski <[email protected]>
Date: Fri, 20 Dec 2024 09:26:47 -0800

Quoted 19 lines · On Fri, 20 Dec 2024 16:58:57 +0100 Alexander Lobakin wrote:
Quoted 18 lines
Quoted 8 lines · On Wed, 18 Dec 2024 18:44:34 +0100 Alexander Lobakin wrote:

net/xdp/xsk_buff_pool.c dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) · line 741

Quoted 5 lines
net/xdp/xsk_buff_pool.c +5 -0
+	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),
+	};  

This is quite ugly IMHO

What exactly: that the logic is copied or how that code (>> & ~ + & ~) looks like?

If the former, I already thought of making a couple internal defs to avoid copying.
If the latter, I also thought of this, just wanted to be clear that it's the same as in xp_raw_get_dma(). But it can be refactored to look more fancy anyway.

Quoted 6 lines
Quoted 1 line

Or the compound return looks ugly? Or the struct initialization?

Compound using typeof() and the fact it's multi line.

It's a two member struct, which you return by value, so unlikely to grow. Why not init the members manually?

BTW sometimes such compound initializations are faster than member-by-member assignment. *Not* in this case, however, so sure, done already.

Quoted 3 lines

And you could save the intermediate computations to a temp variable (addr >> PAGE_SHIFT, addr & ~PAGE_MASK) to make the line shorter.

I'll just derive it into a oneliner to not copy the same stuff again between functions; also, page helpers like PHYS_PFN() and offset_in_page() can be used here instead of open-coding.

Merry holidays!
Olek

#11 lore raw

PATCH 7/7 unroll: add generic loop unroll helpers

In reply to Alexander Lobakin

There 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
index d42fd6366373..863fb69f6a7e 100644
@@ -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 the 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 the 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
#12 lore raw
In reply to Alexander Lobakin
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 10 lines · On Wed, 18 Dec 2024 18:44:28 +0100 you wrote:

XDP for idpf is currently 5.(6) chapters:
* convert Rx to libeth;
* convert Tx and stats to libeth;
* generic XDP and XSk code changes;
* generic XDP and XSk code additions pt. 1;
* generic XDP and XSk code additions pt. 2 (you are here);
* actual XDP for idpf via new libeth_xdp;
* XSk for idpf (via ^).

[...]

Here is the summary with links:
- [net-next,1/7] page_pool: add page_pool_dev_alloc_netmem()

    https://git.kernel.org/netdev/net-next/c/a19d0236f466
  - [net-next,2/7] xdp: add generic xdp_buff_add_frag()
    https://git.kernel.org/netdev/net-next/c/68ddc8ae1768
  - [net-next,3/7] xdp: add generic xdp_build_skb_from_buff()
    https://git.kernel.org/netdev/net-next/c/539c1fba1ac7
  - [net-next,4/7] xsk: make xsk_buff_add_frag() really add the frag via __xdp_buff_add_frag()
    (no matching commit)
  - [net-next,5/7] xsk: add generic XSk &xdp_buff -> skb conversion
    https://git.kernel.org/netdev/net-next/c/560d958c6c68

- [net-next,6/7] xsk: add helper to get &xdp_desc's DMA and meta pointer in one go (no matching commit)
- [net-next,7/7] 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