Skip to contentSkip to conversation

xsk: parse Tx batches in packet units for all shared-UMEM pools

PATCH Applied Magnus Karlsson 4 participants 4 messages Nov 24 – Dec 2

Subsystems (3)
lore
Share & tools
lore

Magnus Karlsson

author maintainer
#0 lore raw

PATCH 1/1 xsk: parse Tx batches in packet units for all shared-UMEM pools

Zero-copy drivers that use xsk_tx_peek_release_desc_batch() are meant to receive only complete packets of at most xdp_zc_max_segs descriptors. Single-socket pools and shared-UMEM pools with XDP_UMEM_SG_FLAG get this from xskq_cons_read_desc_batch(), which also drains invalid and oversized packets. Shared-UMEM pools without XDP_UMEM_SG_FLAG still use xsk_tx_peek_release_fallback(), which peeks one descriptor at a time, on the assumption that such pools never carry multi-buffer packets.

That assumption does not hold. Descriptor validation accepts XDP_PKT_CONTD whether or not the UMEM has XDP_UMEM_SG_FLAG, so descriptors from such a pool can form multi-buffer packets, as they already do on a single-socket pool. The fallback then hands the driver a batch that can end in the middle of a packet, packets longer than xdp_zc_max_segs, and, as xsk_tx_peek_desc() moves on to another socket when the per-socket budget runs out, descriptors of another socket in the middle of a packet. On ice, a packet with more than eight data descriptors triggers a malicious driver detection event.

Use xsk_tx_peek_release_shared_desc_batch() for all shared-UMEM pools, so that they are parsed like a single non-SG socket, and remove the now unused fallback.

Implemented a test for this in xskxceiver that opens two sockets in shared_umem mode and from one socket sends a packet with too many fragments in between two single-fragment packets. Without this patch, the NIC resets, but with it the two single-fragment packets are received and the test passes.

Fixes d5581966040f ("xsk: support ZC Tx multi-buffer in batch API")
---
 net/xdp/xsk.c | 17 -----------------
 1 file changed, 17 deletions(-)
net/xdp/xsk.c +0 -17
index ab2e4fa87a4f..7d0701e0e025 100644
@@ -604,18 +604,6 @@ bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
 }
 EXPORT_SYMBOL(xsk_tx_peek_desc);
 
-static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
-{
-	struct xdp_desc *descs = pool->tx_descs;
-	u32 nb_pkts = 0;
-
-	while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
-		nb_pkts++;
-
-	xsk_tx_release(pool);
-	return nb_pkts;
-}
-
 static void xsk_tx_commit_batch(struct xsk_buff_pool *pool,
 				struct xsk_tx_batch *batch)
 {
@@ -763,11 +751,6 @@ u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts)
 
 	umem_shared = !list_is_singular(&pool->xsk_tx_list);
 
-	if (umem_shared && !(pool->umem->flags & XDP_UMEM_SG_FLAG)) {
-		rcu_read_unlock();
-		return xsk_tx_peek_release_fallback(pool, nb_pkts);
-	}
-
 	/* This is the backpressure mechanism for the Tx path. Try to
 	 * reserve space in the completion queue for all packets, but
 	 * if there are fewer slots available, just process that many
Signature · 1 line
2.38.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Magnus Karlsson

Zero-copy drivers that use xsk_tx_peek_release_desc_batch() are meant to receive only complete packets of at most xdp_zc_max_segs descriptors. Single-socket pools and shared-UMEM pools with XDP_UMEM_SG_FLAG get this from xskq_cons_read_desc_batch(), which also drains invalid and oversized packets. Shared-UMEM pools without XDP_UMEM_SG_FLAG still use xsk_tx_peek_release_fallback(), which peeks one descriptor at a time, on the assumption that such pools never carry multi-buffer packets.

That assumption does not hold. Descriptor validation accepts XDP_PKT_CONTD whether or not the UMEM has XDP_UMEM_SG_FLAG, so descriptors from such a pool can form multi-buffer packets, as they already do on a single-socket pool. The fallback then hands the driver a batch that can end in the middle of a packet, packets longer than xdp_zc_max_segs, and, as xsk_tx_peek_desc() moves on to another socket when the per-socket budget runs out, descriptors of another socket in the middle of a packet. On ice, a packet with more than eight data descriptors triggers a malicious driver detection event.

Quoted 3 lines

Use xsk_tx_peek_release_shared_desc_batch() for all shared-UMEM pools, so that they are parsed like a single non-SG socket, and remove the now unused fallback.

Should have done it from day 1 - thanks!

Quoted 6 lines

Implemented a test for this in xskxceiver that opens two sockets in shared_umem mode and from one socket sends a packet with too many fragments in between two single-fragment packets. Without this patch, the NIC resets, but with it the two single-fragment packets are received and the test passes.

I don't see it anywhere so this reads a bit weird. I would appreciate the follow-up with this new test case.

Quoted 45 lines
Fixes d5581966040f ("xsk: support ZC Tx multi-buffer in batch API")
---
 net/xdp/xsk.c | 17 -----------------
 1 file changed, 17 deletions(-)
net/xdp/xsk.c +0 -17
index ab2e4fa87a4f..7d0701e0e025 100644
@@ -604,18 +604,6 @@ bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
 }
 EXPORT_SYMBOL(xsk_tx_peek_desc);
 
-static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
-{
-	struct xdp_desc *descs = pool->tx_descs;
-	u32 nb_pkts = 0;
-
-	while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
-		nb_pkts++;
-
-	xsk_tx_release(pool);
-	return nb_pkts;
-}
-
 static void xsk_tx_commit_batch(struct xsk_buff_pool *pool,
 				struct xsk_tx_batch *batch)
 {
@@ -763,11 +751,6 @@ u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts)
 
 	umem_shared = !list_is_singular(&pool->xsk_tx_list);
 
-	if (umem_shared && !(pool->umem->flags & XDP_UMEM_SG_FLAG)) {
-		rcu_read_unlock();
-		return xsk_tx_peek_release_fallback(pool, nb_pkts);
-	}
-
 	/* This is the backpressure mechanism for the Tx path. Try to
 	 * reserve space in the completion queue for all packets, but
 	 * if there are fewer slots available, just process that many
Signature · 1 line
2.38.1
#2 lore raw
Quoted 27 lines · Maciej Fijalkowski
Quoted 18 lines · Magnus Karlsson

Zero-copy drivers that use xsk_tx_peek_release_desc_batch() are meant to receive only complete packets of at most xdp_zc_max_segs descriptors. Single-socket pools and shared-UMEM pools with XDP_UMEM_SG_FLAG get this from xskq_cons_read_desc_batch(), which also drains invalid and oversized packets. Shared-UMEM pools without XDP_UMEM_SG_FLAG still use xsk_tx_peek_release_fallback(), which peeks one descriptor at a time, on the assumption that such pools never carry multi-buffer packets.

That assumption does not hold. Descriptor validation accepts XDP_PKT_CONTD whether or not the UMEM has XDP_UMEM_SG_FLAG, so descriptors from such a pool can form multi-buffer packets, as they already do on a single-socket pool. The fallback then hands the driver a batch that can end in the middle of a packet, packets longer than xdp_zc_max_segs, and, as xsk_tx_peek_desc() moves on to another socket when the per-socket budget runs out, descriptors of another socket in the middle of a packet. On ice, a packet with more than eight data descriptors triggers a malicious driver detection event.

Quoted 3 lines

Use xsk_tx_peek_release_shared_desc_batch() for all shared-UMEM pools, so that they are parsed like a single non-SG socket, and remove the now unused fallback.

Should have done it from day 1 - thanks!

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

Implemented a test for this in xskxceiver that opens two sockets in shared_umem mode and from one socket sends a packet with too many fragments in between two single-fragment packets. Without this patch, the NIC resets, but with it the two single-fragment packets are received and the test passes.

I don't see it anywhere so this reads a bit weird. I would appreciate the follow-up with this new test case.

Should have said explicitly that it is not included here as it has to go to net-next. I will contribute it as soon as your stuff is in.

Quoted 47 lines
Quoted 45 lines
Fixes d5581966040f ("xsk: support ZC Tx multi-buffer in batch API")
---
 net/xdp/xsk.c | 17 -----------------
 1 file changed, 17 deletions(-)
net/xdp/xsk.c +0 -18
index ab2e4fa87a4f..7d0701e0e025 100644
@@ -604,18 +604,6 @@ bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
 }
 EXPORT_SYMBOL(xsk_tx_peek_desc);
 
-static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, u32 max_entries)
-{
-     struct xdp_desc *descs = pool->tx_descs;
-     u32 nb_pkts = 0;
-
-     while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
-             nb_pkts++;
-
-     xsk_tx_release(pool);
-     return nb_pkts;
-}
-
 static void xsk_tx_commit_batch(struct xsk_buff_pool *pool,
                              struct xsk_tx_batch *batch)
 {
@@ -763,11 +751,6 @@ u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 nb_pkts)
 
      umem_shared = !list_is_singular(&pool->xsk_tx_list);
 
-     if (umem_shared && !(pool->umem->flags & XDP_UMEM_SG_FLAG)) {
-             rcu_read_unlock();
-             return xsk_tx_peek_release_fallback(pool, nb_pkts);
-     }
-
      /* This is the backpressure mechanism for the Tx path. Try to
       * reserve space in the completion queue for all packets, but
       * if there are fewer slots available, just process that many
--

2.38.1

#3 lore raw
In reply to Magnus Karlsson
This patch was applied to netdev/net.git (main) by Willem de Bruijn <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Willem de Bruijn <[email protected]>:

Quoted 9 lines · On Tue, 24 Nov 2026 14:48:08 +0100 you wrote:

Zero-copy drivers that use xsk_tx_peek_release_desc_batch() are meant to receive only complete packets of at most xdp_zc_max_segs descriptors. Single-socket pools and shared-UMEM pools with XDP_UMEM_SG_FLAG get this from xskq_cons_read_desc_batch(), which also drains invalid and oversized packets. Shared-UMEM pools without XDP_UMEM_SG_FLAG still use xsk_tx_peek_release_fallback(), which peeks one descriptor at a time, on the assumption that such pools never carry multi-buffer packets.

[...]

Here is the summary with links:
- [net] xsk: parse Tx batches in packet units for all shared-UMEM pools

    https://git.kernel.org/netdev/net/c/0d546d385c30

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html