Skip to contentSkip to conversation

xsk: fix meta and publish of cq issues

PATCH Superseded Jason Xing 3 participants 22 messages May 10 – May 19

Subsystems (4)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Jason Xing

author
#0 lore raw

cover xsk: fix meta and publish of cq issues

From: Jason Xing <[email protected]>

The series is the product of previous review from sashiko[1].

1) META
patch 1: address TOCTOU around metadata.

2) PUBLISH of CQ
patch 2: make sure xsk_addr->addrs[] can be published to cq when
overflow occurs.
patch 3: keep cleaning up the continuation descs (more than 17) and
publish its address when overflow occurs.
patch 4: like patch 3, but only handles the invalid descs cases.

[1]: https://lore.kernel.org/all/[email protected]/

Jason Xing (4):
xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()
xsk: fix buffer leak in xsk_drop_skb() for AF_XDP multi-buffer Tx
xsk: drain continuation descs after overflow in xsk_build_skb()
xsk: drain continuation descs on invalid descriptor in

    __xsk_generic_xmit()
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 48 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 40 insertions(+), 9 deletions(-)
Signature · 1 line
2.41.3
author
#1 lore raw

PATCH 1/4 xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +7 -4
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 			    u32 hr)
 {
 	struct xsk_tx_metadata *meta = NULL;
+	u16 csum_start, csum_offset;
 
 	if (unlikely(pool->tx_metadata_len == 0))
 		return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 		return -EINVAL;
 
 	if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-		if (unlikely(meta->request.csum_start +
-			     meta->request.csum_offset +
+		csum_start = meta->request.csum_start;
+		csum_offset = meta->request.csum_offset;
+
+		if (unlikely(csum_start + csum_offset +
 			     sizeof(__sum16) > desc->len))
 			return -EINVAL;
 
-		skb->csum_start = hr + meta->request.csum_start;
-		skb->csum_offset = meta->request.csum_offset;
+		skb->csum_start = hr + csum_start;
+		skb->csum_offset = csum_offset;
 		skb->ip_summed = CHECKSUM_PARTIAL;
 
 		if (unlikely(pool->tx_sw_csum)) {
Signature · 1 line
2.41.3
#2 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 39 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 			    u32 hr)
 {
 	struct xsk_tx_metadata *meta = NULL;
+	u16 csum_start, csum_offset;
 
 	if (unlikely(pool->tx_metadata_len == 0))
 		return -EINVAL;

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 7 lines
net/xdp/xsk.c +2 -2
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
 		return -EINVAL;
 
 	if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-		if (unlikely(meta->request.csum_start +
-			     meta->request.csum_offset +
+		csum_start = meta->request.csum_start;
+		csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

author
#3 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 43 lines · Stanislav Fomichev
Quoted 41 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
                          u32 hr)
 {
      struct xsk_tx_metadata *meta = NULL;
+     u16 csum_start, csum_offset;
 
      if (unlikely(pool->tx_metadata_len == 0))
              return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
              return -EINVAL;
 
Quoted 7 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 5 lines
net/xdp/xsk.c +2 -2
      if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-             if (unlikely(meta->request.csum_start +
-                          meta->request.csum_offset +
+             csum_start = meta->request.csum_start;
+             csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

I still chose not to use it after reading the suggestion from local AI. The reason is there is no WRITE_ONCE pair to make sure everything is no data-race. I also checked some existing implementations around the shared buffer (between userspace and kernel) and didn't manage to see the usage of XXXX_ONCE(). Does it make any sense to you :) ?

Thanks,
Jason

#4 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 53 lines · Jason Xing
Quoted 43 lines · Stanislav Fomichev
Quoted 41 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
                          u32 hr)
 {
      struct xsk_tx_metadata *meta = NULL;
+     u16 csum_start, csum_offset;
 
      if (unlikely(pool->tx_metadata_len == 0))
              return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
              return -EINVAL;
 
Quoted 7 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 5 lines
net/xdp/xsk.c +2 -2
      if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-             if (unlikely(meta->request.csum_start +
-                          meta->request.csum_offset +
+             csum_start = meta->request.csum_start;
+             csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

Quoted 5 lines

I still chose not to use it after reading the suggestion from local AI. The reason is there is no WRITE_ONCE pair to make sure everything is no data-race. I also checked some existing implementations around the shared buffer (between userspace and kernel) and didn't manage to see the usage of XXXX_ONCE(). Does it make any sense to you :) ?

Without READ_ONCE your patch relies on the compiler honoring exactly the loads and stores as written. Which I don't think it does (hence that whole WRITE_ONCE/READ_ONCE mess). IOW, it can pretty much generate the same code (and read csum_start twice) even with your patch applied. Happy to argue with your local AI if you give me the output :-D

I grepped io_uring/ and I see similar pattern for reading user supplied entries (via READ_ONCE).

author
#5 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 61 lines · Stanislav Fomichev
Quoted 53 lines · Jason Xing
Quoted 43 lines · Stanislav Fomichev
Quoted 41 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
                          u32 hr)
 {
      struct xsk_tx_metadata *meta = NULL;
+     u16 csum_start, csum_offset;
 
      if (unlikely(pool->tx_metadata_len == 0))
              return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
              return -EINVAL;
 
Quoted 7 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 5 lines
net/xdp/xsk.c +2 -2
      if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-             if (unlikely(meta->request.csum_start +
-                          meta->request.csum_offset +
+             csum_start = meta->request.csum_start;
+             csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

Quoted 5 lines

I still chose not to use it after reading the suggestion from local AI. The reason is there is no WRITE_ONCE pair to make sure everything is no data-race. I also checked some existing implementations around the shared buffer (between userspace and kernel) and didn't manage to see the usage of XXXX_ONCE(). Does it make any sense to you :) ?

Quoted 8 lines

Without READ_ONCE your patch relies on the compiler honoring exactly the loads and stores as written. Which I don't think it does (hence that whole WRITE_ONCE/READ_ONCE mess). IOW, it can pretty much generate the same code (and read csum_start twice) even with your patch applied. Happy to argue with your local AI if you give me the output :-D

I grepped io_uring/ and I see similar pattern for reading user supplied entries (via READ_ONCE).

I roughly understand what your meaning is here. My thought is the data-race condition in this case still happens even with the READ_ONCE() protection (because no such corresponding operation is performed on the writer side).

Actually the local AI said use READ_ONCE instead.

I can change this as you suggested for sure. Maybe related fields can be protected in the same way in another patch.

Thanks,
Jason

#6 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 76 lines · Jason Xing
Quoted 61 lines · Stanislav Fomichev
Quoted 53 lines · Jason Xing
Quoted 43 lines · Stanislav Fomichev
Quoted 41 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
                          u32 hr)
 {
      struct xsk_tx_metadata *meta = NULL;
+     u16 csum_start, csum_offset;
 
      if (unlikely(pool->tx_metadata_len == 0))
              return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
              return -EINVAL;
 
Quoted 7 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 5 lines
net/xdp/xsk.c +2 -2
      if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-             if (unlikely(meta->request.csum_start +
-                          meta->request.csum_offset +
+             csum_start = meta->request.csum_start;
+             csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

Quoted 5 lines

I still chose not to use it after reading the suggestion from local AI. The reason is there is no WRITE_ONCE pair to make sure everything is no data-race. I also checked some existing implementations around the shared buffer (between userspace and kernel) and didn't manage to see the usage of XXXX_ONCE(). Does it make any sense to you :) ?

Quoted 8 lines

Without READ_ONCE your patch relies on the compiler honoring exactly the loads and stores as written. Which I don't think it does (hence that whole WRITE_ONCE/READ_ONCE mess). IOW, it can pretty much generate the same code (and read csum_start twice) even with your patch applied. Happy to argue with your local AI if you give me the output :-D

I grepped io_uring/ and I see similar pattern for reading user supplied entries (via READ_ONCE).

I roughly understand what your meaning is here. My thought is the data-race condition in this case still happens even with the READ_ONCE() protection (because no such corresponding operation is performed on the writer side).

Quoted 4 lines

Actually the local AI said use READ_ONCE instead.

I can change this as you suggested for sure. Maybe related fields can be protected in the same way in another patch.

In general, feels like we should be doing READ_ONCE on all user-supplied descriptors (to make it more apparent that those can be concurrently changed by the userspace). In practice, probably too late and too much churn to update everything at this point, so let's only fix the ones that can lead to TOCTOU.

author
#7 lore raw

Re: [PATCH net 1/4] xsk: cache csum_start/csum_offset to fix TOCTOU in xsk_skb_metadata()

Quoted 80 lines · Stanislav Fomichev
Quoted 78 lines · Jason Xing
Quoted 61 lines · Stanislav Fomichev
Quoted 53 lines · Jason Xing
Quoted 43 lines · Stanislav Fomichev
Quoted 41 lines · Jason Xing

From: Jason Xing <[email protected]>

The TX metadata area resides in the UMEM buffer which is memory-mapped and concurrently writable by userspace. In xsk_skb_metadata(), csum_start and csum_offset are read from shared memory for bounds validation, then read again for skb assignment. A malicious userspace application can race to overwrite these values between the two reads, bypassing the bounds check and causing out-of-bounds memory access during checksum computation in the transmit path.

Fix this by reading csum_start and csum_offset into local variables once, then using the local copies for both validation and assignment.

Note that other metadata fields (flags, launch_time) and the cached csum fields may be mutually inconsistent due to concurrent userspace writes, but this is benign: the only security-critical invariant is that each field's validated value is the same one used, which local caching guarantees.

Fixes 48eb03dd2630 ("xsk: Add TX timestamp and TX checksum offload support")
---
 net/xdp/xsk.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/xdp/xsk.c +1 -0
index 6bcd77068e52..cd039e397018 100644
@@ -722,6 +722,7 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
                          u32 hr)
 {
      struct xsk_tx_metadata *meta = NULL;
+     u16 csum_start, csum_offset;
 
      if (unlikely(pool->tx_metadata_len == 0))
              return -EINVAL;
@@ -731,13 +732,15 @@ static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
              return -EINVAL;
 
Quoted 7 lines

net/xdp/xsk.c static int xsk_skb_metadata(struct sk_buff *skb, void *buffer, · line 736

Quoted 5 lines
net/xdp/xsk.c +2 -2
      if (meta->flags & XDP_TXMD_FLAGS_CHECKSUM) {
-             if (unlikely(meta->request.csum_start +
-                          meta->request.csum_offset +
+             csum_start = meta->request.csum_start;
+             csum_offset = meta->request.csum_offset;

Wondering if it's better to READ_ONCE(x) these?

Quoted 5 lines

I still chose not to use it after reading the suggestion from local AI. The reason is there is no WRITE_ONCE pair to make sure everything is no data-race. I also checked some existing implementations around the shared buffer (between userspace and kernel) and didn't manage to see the usage of XXXX_ONCE(). Does it make any sense to you :) ?

Quoted 8 lines

Without READ_ONCE your patch relies on the compiler honoring exactly the loads and stores as written. Which I don't think it does (hence that whole WRITE_ONCE/READ_ONCE mess). IOW, it can pretty much generate the same code (and read csum_start twice) even with your patch applied. Happy to argue with your local AI if you give me the output :-D

I grepped io_uring/ and I see similar pattern for reading user supplied entries (via READ_ONCE).

I roughly understand what your meaning is here. My thought is the data-race condition in this case still happens even with the READ_ONCE() protection (because no such corresponding operation is performed on the writer side).

Actually the local AI said use READ_ONCE instead.

Quoted 8 lines
Quoted 2 lines

I can change this as you suggested for sure. Maybe related fields can be protected in the same way in another patch.

In general, feels like we should be doing READ_ONCE on all user-supplied descriptors (to make it more apparent that those can be concurrently changed by the userspace). In practice, probably too late and too much churn to update everything at this point, so let's only fix the ones that can lead to TOCTOU.

Agreed. Will update it then :)

Thanks,
Jason

author
#8 lore raw

PATCH 2/4 xsk: fix buffer leak in xsk_drop_skb() for AF_XDP multi-buffer Tx

In reply to Jason Xing

From: Jason Xing <[email protected]>

This patch is inspired by the check[1] from sashiko. It says when overflow happens, the address of cq to be published is invalid. Actually the severer thing is the whole process of publishing the address of cq in this particular case is not right: it should truely publish the address and advance the cached_prod in cq as long as it reads descriptors from txq.

The following is the full analysis.
xsk_drop_skb() is called in three places, which all discard a partially built multi-buffer skb:
1) xsk_build_skb() -EOVERFLOW error path: packet exceeds MAX_SKB_FRAGS
2) __xsk_generic_xmit() post-loop cleanup: an invalid descriptor in the TX ring prevents the partial packet from completing
3) xsk_release(): socket close while xs->skb holds an incomplete packet

In all three cases, the TX descriptors for the already-processed frags have been consumed from the TX ring (xskq_cons_release), and CQ slots have been reserved. However, xsk_drop_skb() calls xsk_consume_skb() which cancels the CQ reservations via xsk_cq_cancel_locked(). Since the buffer addresses never appear in the completion queue, userspace permanently loses track of these buffers.

Fix this by letting consume_skb() trigger the existing xsk_destruct_skb destructor, which already submits buffer addresses to the CQ via xsk_cq_submit_addr_locked().

Note that cancelling the descriptors back to the TX ring (via xskq_cons_cancel_n) is not a appropriate option because an oversized packet that always exceeds MAX_SKB_FRAGS would be retried indefinitely, which is an obviously deadlock bug in the TX path.

Also move the desc->addr assignment in xsk_build_skb() above the overflow check so that the current descriptor's address is recorded before a potential -EOVERFLOW jump to free_err, consistent with the zerocopy path in xsk_build_skb_zerocopy().

[1]: https://lore.kernel.org/all/[email protected]/

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 net/xdp/xsk.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
net/xdp/xsk.c +8 -5
index cd039e397018..3f1e590c855d 100644
@@ -713,8 +713,11 @@ static void xsk_consume_skb(struct sk_buff *skb)
 
 static void xsk_drop_skb(struct sk_buff *skb)
 {
-	xdp_sk(skb->sk)->tx->invalid_descs += xsk_get_num_desc(skb);
-	xsk_consume_skb(skb);
+	struct xdp_sock *xs = xdp_sk(skb->sk);
+
+	xs->tx->invalid_descs += xsk_get_num_desc(skb);
+	consume_skb(skb);
+	xs->skb = NULL;
 }
 
 static int xsk_skb_metadata(struct sk_buff *skb, void *buffer,
@@ -796,7 +799,7 @@ static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
 			return ERR_PTR(-ENOMEM);
 
 		/* in case of -EOVERFLOW that could happen below,
-		 * xsk_consume_skb() will release this node as whole skb
+		 * xsk_drop_skb() will release this node as whole skb
 		 * would be dropped, which implies freeing all list elements
 		 */
 		xsk_addr->addrs[xsk_addr->num_descs] = desc->addr;
@@ -888,6 +891,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
 				goto free_err;
 			}
 
+			xsk_addr->addrs[xsk_addr->num_descs] = desc->addr;
+
 			if (unlikely(nr_frags == (MAX_SKB_FRAGS - 1) && xp_mb_desc(desc))) {
 				err = -EOVERFLOW;
 				goto free_err;
@@ -905,8 +910,6 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
 
 			skb_add_rx_frag(skb, nr_frags, page, 0, len, PAGE_SIZE);
 			refcount_add(PAGE_SIZE, &xs->sk.sk_wmem_alloc);
-
-			xsk_addr->addrs[xsk_addr->num_descs] = desc->addr;
 		}
 	}
 
Signature · 1 line
2.41.3
author
#9 lore raw

PATCH 3/4 xsk: drain continuation descs after overflow in xsk_build_skb()

In reply to Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
 	 * call of __xsk_generic_xmit().
 	 */
 	struct sk_buff *skb;
+	bool drain_cont;
 
 	struct list_head map_list;
 	/* Protects map_list */
net/xdp/xsk.c +22 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
 			xs->tx->invalid_descs++;
 		}
 		xskq_cons_release(xs->tx);
+		if (xp_mb_desc(desc))
+			xs->drain_cont = true;
 	} else {
 		/* Let application retry */
 		xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
 			goto out;
 		}
 
+		if (unlikely(xs->drain_cont)) {
+			unsigned long flags;
+			u32 idx;
+
+			spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+			idx = xskq_get_prod(xs->pool->cq);
+			xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+			xskq_prod_submit_n(xs->pool->cq, 1);
+			spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
+
+			xs->tx->invalid_descs++;
+			xskq_cons_release(xs->tx);
+			if (!xp_mb_desc(&desc)) {
+				xs->drain_cont = false;
+				err = -EOVERFLOW;
+				goto out;
+			}
+			continue;
+		}
+
 		skb = xsk_build_skb(xs, &desc);
 		if (IS_ERR(skb)) {
 			err = PTR_ERR(skb);
Signature · 1 line
2.41.3
#10 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
 	 * call of __xsk_generic_xmit().
 	 */
 	struct sk_buff *skb;
+	bool drain_cont;
 
 	struct list_head map_list;
 	/* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
 			xs->tx->invalid_descs++;
 		}
 		xskq_cons_release(xs->tx);
+		if (xp_mb_desc(desc))
+			xs->drain_cont = true;
 	} else {
 		/* Let application retry */
 		xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
 			goto out;
 		}
 
+		if (unlikely(xs->drain_cont)) {
+			unsigned long flags;
+			u32 idx;
+

[..]

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 995

Quoted 5 lines
net/xdp/xsk.c +5 -0
+			spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+			idx = xskq_get_prod(xs->pool->cq);
+			xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+			xskq_prod_submit_n(xs->pool->cq, 1);
+			spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);

Not sure I understand why you want this if you're still marking the desc invalid.

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 1003

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+			xs->tx->invalid_descs++;
+			xskq_cons_release(xs->tx);
+			if (!xp_mb_desc(&desc)) {
+				xs->drain_cont = false;
+				err = -EOVERFLOW;
+				goto out;
+			}

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Also, have you considered doing this drain in xsk_build_skb error path? Is it more messy? Carrying this drain_cont across the calls seems a bit too complicated (but idk, maybe the alternative drain in xsk_build_skb is even messier).

author
#11 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 69 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 8 lines

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 995

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Quoted 10 lines

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 1003

Quoted 9 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }
Quoted 3 lines

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Quoted 5 lines

Also, have you considered doing this drain in xsk_build_skb error path? Is it more messy? Carrying this drain_cont across the calls seems a bit too complicated (but idk, maybe the alternative drain in xsk_build_skb is even messier).

Sure, it was my first version actually. One of the primary reasons is how to handle it gracefully when CQ is full? Then those descriptors can not be processed in this loop at this time and will be delayed until CQ has available slots. Another important reason is that I want to put the whole read process under the previous clear logic in __xsk_generic_xmit(). Some protections like budget/batch still take effect in this case. In this way, there is no strange/exceptional read process in copy mode :)

Thanks,
Jason

#12 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }
Quoted 6 lines
Quoted 3 lines

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

Quoted 6 lines
Quoted 5 lines

Also, have you considered doing this drain in xsk_build_skb error path? Is it more messy? Carrying this drain_cont across the calls seems a bit too complicated (but idk, maybe the alternative drain in xsk_build_skb is even messier).

Quoted 8 lines

Sure, it was my first version actually. One of the primary reasons is how to handle it gracefully when CQ is full? Then those descriptors can not be processed in this loop at this time and will be delayed until CQ has available slots. Another important reason is that I want to put the whole read process under the previous clear logic in __xsk_generic_xmit(). Some protections like budget/batch still take effect in this case. In this way, there is no strange/exceptional read process in copy mode :)

Agreed, given the fact that we need to handle prod backpressure, your approach is good (but see above comment, we should probably clarify what's the expectation on the user side).

author
#13 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 1003

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Thanks,
Jason

Quoted 19 lines
Quoted 7 lines
Quoted 6 lines

Also, have you considered doing this drain in xsk_build_skb error path? Is it more messy? Carrying this drain_cont across the calls seems a bit too complicated (but idk, maybe the alternative drain in xsk_build_skb is even messier).

Quoted 8 lines

Sure, it was my first version actually. One of the primary reasons is how to handle it gracefully when CQ is full? Then those descriptors can not be processed in this loop at this time and will be delayed until CQ has available slots. Another important reason is that I want to put the whole read process under the previous clear logic in __xsk_generic_xmit(). Some protections like budget/batch still take effect in this case. In this way, there is no strange/exceptional read process in copy mode :)

Agreed, given the fact that we need to handle prod backpressure, your approach is good (but see above comment, we should probably clarify what's the expectation on the user side).

#14 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 126 lines · Jason Xing
Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c static int __xsk_generic_xmit(struct sock *sk) · line 1003

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

Quoted 4 lines

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Let's say the user puts a packet with too many descriptors. We find that mid-way and return -EOVERFLOW. What is user supposed to do with that error? In my mind, the user should drain TX ring completely and start posting shorter packets. You seem to be trying to handle the case where the user leaves the remainder of the previously EOVERFLOW'd packet in place, why?

author
#15 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 133 lines · Stanislav Fomichev
Quoted 126 lines · Jason Xing
Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

Quoted 4 lines

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Quoted 5 lines

Let's say the user puts a packet with too many descriptors. We find that mid-way and return -EOVERFLOW. What is user supposed to do with that error? In my mind, the user should drain TX ring completely and start posting shorter packets. You seem to be trying to handle the case where the user leaves the remainder of the previously EOVERFLOW'd packet in place, why?

Good point. I think now I understand what your opinion is here:

net/xdp/xsk.c +1 -4
index 3b604776f09f..72d05a1fb0b5 100644
@@ -996,11 +996,8 @@ static int __xsk_generic_xmit(struct sock *sk)
 
                        xs->tx->invalid_descs++;
                        xskq_cons_release(xs->tx);
-                       if (!xp_mb_desc(&desc)) {
+                       if (!xp_mb_desc(&desc))
                                xs->drain_cont = false;
-                               err = -EOVERFLOW;
-                               goto out;
-                       }
                        continue;
                }
 

Then as long as we have enough budget, we silently handle the whole big skb and then continue with the rest of the skbs in txq. Users will be alerted by the counter of invalid_descs. I think that is what you're asking, right?

Thanks,
Jason

#16 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 159 lines · Jason Xing
Quoted 133 lines · Stanislav Fomichev
Quoted 126 lines · Jason Xing
Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

Quoted 4 lines

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Quoted 5 lines

Let's say the user puts a packet with too many descriptors. We find that mid-way and return -EOVERFLOW. What is user supposed to do with that error? In my mind, the user should drain TX ring completely and start posting shorter packets. You seem to be trying to handle the case where the user leaves the remainder of the previously EOVERFLOW'd packet in place, why?

Good point. I think now I understand what your opinion is here:

net/xdp/xsk.c +1 -4
index 3b604776f09f..72d05a1fb0b5 100644
@@ -996,11 +996,8 @@ static int __xsk_generic_xmit(struct sock *sk)
 
                        xs->tx->invalid_descs++;
                        xskq_cons_release(xs->tx);
-                       if (!xp_mb_desc(&desc)) {
+                       if (!xp_mb_desc(&desc))
                                xs->drain_cont = false;
-                               err = -EOVERFLOW;
-                               goto out;
-                       }
                        continue;
                }
 
Quoted 4 lines

Then as long as we have enough budget, we silently handle the whole big skb and then continue with the rest of the skbs in txq. Users will be alerted by the counter of invalid_descs. I think that is what you're asking, right?

Yes, this resolves one part. But the larger question still stands:
why do this tracking at all? Why can't we clarify in the documentation that the user is responsible for flushing tx queue completely after EOVERFLOW? What am I missing?

author
#17 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 161 lines · Stanislav Fomichev
Quoted 159 lines · Jason Xing
Quoted 133 lines · Stanislav Fomichev
Quoted 126 lines · Jason Xing
Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

Quoted 4 lines

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Quoted 5 lines

Let's say the user puts a packet with too many descriptors. We find that mid-way and return -EOVERFLOW. What is user supposed to do with that error? In my mind, the user should drain TX ring completely and start posting shorter packets. You seem to be trying to handle the case where the user leaves the remainder of the previously EOVERFLOW'd packet in place, why?

Good point. I think now I understand what your opinion is here:

net/xdp/xsk.c +1 -4
index 3b604776f09f..72d05a1fb0b5 100644
@@ -996,11 +996,8 @@ static int __xsk_generic_xmit(struct sock *sk)
 
                        xs->tx->invalid_descs++;
                        xskq_cons_release(xs->tx);
-                       if (!xp_mb_desc(&desc)) {
+                       if (!xp_mb_desc(&desc))
                                xs->drain_cont = false;
-                               err = -EOVERFLOW;
-                               goto out;
-                       }
                        continue;
                }
 
Quoted 9 lines
Quoted 4 lines

Then as long as we have enough budget, we silently handle the whole big skb and then continue with the rest of the skbs in txq. Users will be alerted by the counter of invalid_descs. I think that is what you're asking, right?

Yes, this resolves one part. But the larger question still stands:
why do this tracking at all? Why can't we clarify in the documentation that the user is responsible for flushing tx queue completely after EOVERFLOW? What am I missing?

Previous and current (v3 as you may notice) impl always silently handle the overflow packet which means kernel never returns -EOVERFLOW back to the userspace and users have no way to receive this signal and stop sending more overflow packet.

Another point is kernel should be capable of tackling this overflow issue separately without considering how users would do after realizing the problem.

The v3 patch doesn't change any pre-set behavior of xmit path but only publish the desc into the cq.

Thanks,
Jason

#18 lore raw

Re: [PATCH net 3/4] xsk: drain continuation descs after overflow in xsk_build_skb()

Quoted 178 lines · Jason Xing
Quoted 161 lines · Stanislav Fomichev
Quoted 159 lines · Jason Xing
Quoted 133 lines · Stanislav Fomichev
Quoted 126 lines · Jason Xing
Quoted 103 lines · Stanislav Fomichev
Quoted 77 lines · Jason Xing
Quoted 75 lines · Stanislav Fomichev
Quoted 64 lines · Jason Xing

From: Jason Xing <[email protected]>

When a multi-buffer packet exceeds MAX_SKB_FRAGS and triggers -EOVERFLOW, only the current descriptor is released from the TX ring. The remaining continuation descriptors of the same packet stay in the ring. Since xs->skb is set to NULL after the drop, the TX loop picks up these leftover frags and misinterprets each one as the beginning of a new packet, corrupting the packet stream.

Fix this by adding a drain_cont flag to xdp_sock. When overflow occurs and the dropped descriptor has XDP_PKT_CONTD set, the flag is raised. The main TX loop in __xsk_generic_xmit() then handles continuation descriptors one at a time: each gets a normal CQ reservation (with backpressure), its address is submitted to the completion queue, and the descriptor is released from the TX ring. When the last fragment (without XDP_PKT_CONTD) is processed, the flag is cleared and the function returns -EOVERFLOW so the next call starts with a fresh budget for normal packets.

This reuses the existing CQ backpressure and budget mechanisms, so if the CQ is full the function returns -EAGAIN and userspace drains the CQ before retrying. Zero buffer leakage, zero packet stream corruption.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 include/net/xdp_sock.h |  1 +
 net/xdp/xsk.c          | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/xdp_sock.h +1 -0
index 23e8861e8b25..1958d19d9925 100644
@@ -80,6 +80,7 @@ struct xdp_sock {
       * call of __xsk_generic_xmit().
       */
      struct sk_buff *skb;
+     bool drain_cont;
 
      struct list_head map_list;
      /* Protects map_list */
net/xdp/xsk.c +6 -0
index 3f1e590c855d..232dd7126905 100644
@@ -936,6 +936,8 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
                      xs->tx->invalid_descs++;
              }
              xskq_cons_release(xs->tx);
+             if (xp_mb_desc(desc))
+                     xs->drain_cont = true;
      } else {
              /* Let application retry */
              xsk_cq_cancel_locked(xs->pool, 1);
@@ -982,6 +984,26 @@ static int __xsk_generic_xmit(struct sock *sk)
                      goto out;
              }
 
+             if (unlikely(xs->drain_cont)) {
+                     unsigned long flags;
+                     u32 idx;
+

[..]

Quoted 5 lines
net/xdp/xsk.c +5 -0
+                     spin_lock_irqsave(&xs->pool->cq_prod_lock, flags);
+                     idx = xskq_get_prod(xs->pool->cq);
+                     xskq_prod_write_addr(xs->pool->cq, idx, desc.addr);
+                     xskq_prod_submit_n(xs->pool->cq, 1);
+                     spin_unlock_irqrestore(&xs->pool->cq_prod_lock, flags);
Quoted 8 lines
Quoted 2 lines

Not sure I understand why you want this if you're still marking the desc invalid.

The key point is that as long as we read the desc from txq, the desc should be either put back to txq again or publish it in the cq (for application to keep track of this) at this point. Or else, the application will lose track of this desc, which breaks the whole logic.

Yes, makes sense!

Quoted 13 lines
Quoted 12 lines

net/xdp/xsk.c

Quoted 8 lines
net/xdp/xsk.c +8 -0
+
+                     xs->tx->invalid_descs++;
+                     xskq_cons_release(xs->tx);
+                     if (!xp_mb_desc(&desc)) {
+                             xs->drain_cont = false;
+                             err = -EOVERFLOW;
+                             goto out;
+                     }

I also don't understand why you want to return -EOVERFLOW again? Why not (quietly) swallow these invalid xp_mb_desc from the previous packet and move on?

Quoted 6 lines
Quoted 2 lines

This particular desc is really one of overflow cases, right? We should warn users to handle this with this error code.

Right, but didn't we already return -EOVERFLOW to the user once? The part where you set drain_count=true will -EOVERFLOW. Then this remainder will also return -EOVERFLOW?

Right, my intention is to alert users twice under this kind of circumstance. We silently drain the remaining portion of the skb the second time, which looks a bit strange, doesn't it?

Quoted 5 lines

But let's maybe step back, what's the expectation on the user when we return -EOVERFLOW? In theory, the user can re-submit new shorter packet (at the current prod index), right? And then your !xp_mb_desc logic will break/swallow/-EOVERFLOW it?

People would be aware of the skb that is too big to handle when receiving two times of overflow warning. To put it in a simpler way, the second time in xmit path only handling the remaining is enough, no more descs that belong to another skb should be taken care of.

Quoted 4 lines

If the user is able to quickly react to this case, I think the whole skb should be re-put into the txq again instead of adding the remaining part. I'm not so sure if I interpret the "break" correctly here.

Quoted 5 lines

Let's say the user puts a packet with too many descriptors. We find that mid-way and return -EOVERFLOW. What is user supposed to do with that error? In my mind, the user should drain TX ring completely and start posting shorter packets. You seem to be trying to handle the case where the user leaves the remainder of the previously EOVERFLOW'd packet in place, why?

Good point. I think now I understand what your opinion is here:

net/xdp/xsk.c +1 -4
index 3b604776f09f..72d05a1fb0b5 100644
@@ -996,11 +996,8 @@ static int __xsk_generic_xmit(struct sock *sk)
 
                        xs->tx->invalid_descs++;
                        xskq_cons_release(xs->tx);
-                       if (!xp_mb_desc(&desc)) {
+                       if (!xp_mb_desc(&desc))
                                xs->drain_cont = false;
-                               err = -EOVERFLOW;
-                               goto out;
-                       }
                        continue;
                }
 
Quoted 9 lines
Quoted 4 lines

Then as long as we have enough budget, we silently handle the whole big skb and then continue with the rest of the skbs in txq. Users will be alerted by the counter of invalid_descs. I think that is what you're asking, right?

Yes, this resolves one part. But the larger question still stands:
why do this tracking at all? Why can't we clarify in the documentation that the user is responsible for flushing tx queue completely after EOVERFLOW? What am I missing?

Previous and current (v3 as you may notice) impl always silently handle the overflow packet which means kernel never returns -EOVERFLOW back to the userspace and users have no way to receive this signal and stop sending more overflow packet.

Quoted 6 lines

Another point is kernel should be capable of tackling this overflow issue separately without considering how users would do after realizing the problem.

The v3 patch doesn't change any pre-set behavior of xmit path but only publish the desc into the cq.

SG, will take a look at that, thanks!

author
#19 lore raw

PATCH 4/4 xsk: drain continuation descs on invalid descriptor in __xsk_generic_xmit()

In reply to Jason Xing

From: Jason Xing <[email protected]>

When the TX loop in __xsk_generic_xmit() encounters an invalid descriptor mid-packet (e.g. an out-of-bounds address), the partial skb is dropped and the offending descriptor is released. However, remaining continuation descriptors belonging to the same multi-buffer packet still sit in the TX ring. Since xs->skb becomes NULL after the drop, the next iteration treats the leftover continuation fragment as a brand-new packet, corrupting the packet stream.

Fix this by setting the drain_cont flag when the released descriptor has XDP_PKT_CONTD set. On the next call to __xsk_generic_xmit(), the drain logic introduced in the previous patch handles the remaining fragments with normal CQ backpressure.

Fixes cf24f5a5feea ("xsk: add support for AF_XDP multi-buffer on Tx path")
---
 net/xdp/xsk.c | 2 ++
 1 file changed, 2 insertions(+)
net/xdp/xsk.c +2 -0
index 232dd7126905..b41ed44e3192 100644
@@ -1045,6 +1045,8 @@ static int __xsk_generic_xmit(struct sock *sk)
 		if (xs->skb)
 			xsk_drop_skb(xs->skb);
 		xskq_cons_release(xs->tx);
+		if (xp_mb_desc(&desc))
+			xs->drain_cont = true;
 	}
 
 out:
Signature · 1 line
2.41.3
maintainer
#20 lore raw
In reply to Jason Xing
Quoted 1 line · On Sun, 10 May 2026 09:23:06 +0800 Jason Xing wrote:

The series is the product of previous review from sashiko[1].

This series breaks the BPF selftests, it seems:

Error: #277 ns_xsk_drv
Error: #277/21 ns_xsk_drv/ALIGNED_INV_DESC_MULTI_BUFF
Error: #277/22 ns_xsk_drv/TOO_MANY_FRAGS
Error: #278 ns_xsk_skb
Error: #278/21 ns_xsk_skb/ALIGNED_INV_DESC_MULTI_BUFF
Error: #278/22 ns_xsk_skb/TOO_MANY_FRAGS

author
#21 lore raw
Quoted 4 lines · Jakub Kicinski
Quoted 1 line · On Sun, 10 May 2026 09:23:06 +0800 Jason Xing wrote:

The series is the product of previous review from sashiko[1].

Quoted 8 lines

This series breaks the BPF selftests, it seems:

Error: #277 ns_xsk_drv
Error: #277/21 ns_xsk_drv/ALIGNED_INV_DESC_MULTI_BUFF
Error: #277/22 ns_xsk_drv/TOO_MANY_FRAGS
Error: #278 ns_xsk_skb
Error: #278/21 ns_xsk_skb/ALIGNED_INV_DESC_MULTI_BUFF
Error: #278/22 ns_xsk_skb/TOO_MANY_FRAGS

Thanks. I should've updated the selftest as well.

Thanks,
Jason