Skip to contentSkip to conversation

xsk: switch xdp_build_skb_from_zc() to napi_alloc_skb()

PATCH In review Lorenz Brun 3 participants 5 messages May 12 – May 18

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

PATCH 1/1 xsk: switch xdp_build_skb_from_zc() to napi_alloc_skb()

xdp_build_skb_from_zc() allocated xdp->frame_sz bytes from the per-cpu system_page_pool and built the skb head with napi_build_skb(). The latter places skb_shared_info at the tail of the buffer, but the helper sized the allocation as if the whole frame_sz were usable for data. Whenever the packet plus reserved headroom approached frame_sz, the head memcpy overran shinfo with packet content, corrupting ->flags (SKBFL_ZEROCOPY_ENABLE) and ->nr_frags, which then drove skb_copy_ubufs() off the end of frags[] on the RX path:

  UBSAN: array-index-out-of-bounds in include/linux/skbuff.h:2541
  index 113 is out of range for type 'skb_frag_t [17]'
   skb_copy_ubufs+0x7da/0x960
   ip_local_deliver_finish+0xcd/0x110
   ice_napi_poll+0xe4/0x2a0 [ice]

The overrun bytes come from the packet, so an on-wire sender can corrupt kernel memory remotely whenever the XDP program returns XDP_PASS.

Rather than patch the sizing math, switch to the pattern used by other in-tree AF_XDP zero-copy drivers like mlx5 and i40e which use napi_alloc_skb() sized to the actual packet plus skb_put_data(). This sizes the head exactly for the data being copied, drops the system_page_pool local_lock from this path, and removes the structural mismatch between frame_sz and the skb head buffer. Frags are allocated with alloc_page() per frag, matching the other drivers.

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
 drivers/net/ethernet/intel/ice/ice_xsk.c |  2 +-
 include/net/libeth/xsk.h                 |  2 +-
 include/net/xdp.h                        |  3 +-
 net/core/xdp.c                           | 72 ++++++++----------------
 4 files changed, 29 insertions(+), 50 deletions(-)
drivers/net/ethernet/intel/ice/ice_xsk.c +1 -1
index 0643017541c35..6c01a14fde150 100644
@@ -653,7 +653,7 @@ int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring,
 
 construct_skb:
 		/* XDP_PASS path */
-		skb = xdp_build_skb_from_zc(first);
+		skb = xdp_build_skb_from_zc(&rx_ring->q_vector->napi, first);
 		if (!skb) {
 			xsk_buff_free(first);
 			first = NULL;
include/net/libeth/xsk.h +1 -1
index 82b5d21aae878..922b4587acd3f 100644
@@ -468,7 +468,7 @@ __libeth_xsk_run_pass(struct libeth_xdp_buff *xdp,
 	if (act != LIBETH_XDP_PASS)
 		return act != LIBETH_XDP_ABORTED;
 
-	skb = xdp_build_skb_from_zc(&xdp->base);
+	skb = xdp_build_skb_from_zc(napi, &xdp->base);
 	if (unlikely(!skb)) {
 		libeth_xsk_buff_free_slow(xdp);
 		return true;
include/net/xdp.h +2 -1
index aa742f413c358..fb2452243fd36 100644
@@ -375,7 +375,8 @@ void xdp_warn(const char *msg, const char *func, const int line);
 #define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
 
 struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp);
-struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp);
+struct sk_buff *xdp_build_skb_from_zc(struct napi_struct *napi,
+				      struct xdp_buff *xdp);
 struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
 struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
 					   struct sk_buff *skb,
net/core/xdp.c +25 -47

128 lines not on this page · raw message

Signature · 1 line
2.51.2
#1 lore raw

From: Lorenz Brun <[email protected]>
Date: Tue, 12 May 2026 17:26:56 +0200

Quoted 19 lines

xdp_build_skb_from_zc() allocated xdp->frame_sz bytes from the per-cpu system_page_pool and built the skb head with napi_build_skb(). The latter places skb_shared_info at the tail of the buffer, but the helper sized the allocation as if the whole frame_sz were usable for data. Whenever the packet plus reserved headroom approached frame_sz, the head memcpy overran shinfo with packet content, corrupting ->flags (SKBFL_ZEROCOPY_ENABLE) and ->nr_frags, which then drove skb_copy_ubufs() off the end of frags[] on the RX path:

  UBSAN: array-index-out-of-bounds in include/linux/skbuff.h:2541
  index 113 is out of range for type 'skb_frag_t [17]'
   skb_copy_ubufs+0x7da/0x960
   ip_local_deliver_finish+0xcd/0x110
   ice_napi_poll+0xe4/0x2a0 [ice]

The overrun bytes come from the packet, so an on-wire sender can corrupt kernel memory remotely whenever the XDP program returns XDP_PASS.

Quoted 7 lines

Rather than patch the sizing math, switch to the pattern used by other in-tree AF_XDP zero-copy drivers like mlx5 and i40e which use napi_alloc_skb() sized to the actual packet plus skb_put_data(). This sizes the head exactly for the data being copied, drops the system_page_pool local_lock from this path, and removes the structural mismatch between frame_sz and the skb head buffer. Frags are allocated with alloc_page() per frag, matching the other drivers.

I used napi_build_skb() + system page_pool to enable PP recycling improving XSk XDP_PASS performance a lot.
Are you sure there's no other way to approach this?

napi_alloc_skb() used in other drivers works, but it's sorta old approach which is way slower.

System page_pools always allocate a full page, why can it create an skb prone to overruns?

Quoted 4 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

Thanks,
Olek

#2 lore raw
Quoted 42 lines · Alexander Lobakin

From: Lorenz Brun <[email protected]>
Date: Tue, 12 May 2026 17:26:56 +0200

Quoted 19 lines

xdp_build_skb_from_zc() allocated xdp->frame_sz bytes from the per-cpu system_page_pool and built the skb head with napi_build_skb(). The latter places skb_shared_info at the tail of the buffer, but the helper sized the allocation as if the whole frame_sz were usable for data. Whenever the packet plus reserved headroom approached frame_sz, the head memcpy overran shinfo with packet content, corrupting ->flags (SKBFL_ZEROCOPY_ENABLE) and ->nr_frags, which then drove skb_copy_ubufs() off the end of frags[] on the RX path:

  UBSAN: array-index-out-of-bounds in include/linux/skbuff.h:2541
  index 113 is out of range for type 'skb_frag_t [17]'
   skb_copy_ubufs+0x7da/0x960
   ip_local_deliver_finish+0xcd/0x110
   ice_napi_poll+0xe4/0x2a0 [ice]

The overrun bytes come from the packet, so an on-wire sender can corrupt kernel memory remotely whenever the XDP program returns XDP_PASS.

Quoted 7 lines

Rather than patch the sizing math, switch to the pattern used by other in-tree AF_XDP zero-copy drivers like mlx5 and i40e which use napi_alloc_skb() sized to the actual packet plus skb_put_data(). This sizes the head exactly for the data being copied, drops the system_page_pool local_lock from this path, and removes the structural mismatch between frame_sz and the skb head buffer. Frags are allocated with alloc_page() per frag, matching the other drivers.

I used napi_build_skb() + system page_pool to enable PP recycling improving XSk XDP_PASS performance a lot.
Are you sure there's no other way to approach this?

napi_alloc_skb() used in other drivers works, but it's sorta old approach which is way slower.

System page_pools always allocate a full page, why can it create an skb prone to overruns?

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

Thanks,
Olek

Hi Olek

I looked at the code again. While your approach is indeed faster, it is only faster for traffic bypassing AF_XDP, which is generally not that relevant for performance.

More critically, it currently corrupts kernel memory and panics the kernel very quickly when running with frame-size set to 2048, 1500 MTU, and passing received packets. To be honest, I'm not familiar enough with the XSK subsystem to know exactly what specific sizing assumption was violated here. By comparison, the approach taken by the other drivers is a lot more obviously correct and works perfectly.

If you want to preserve the current approach, I'm perfectly happy with that. However, I don't feel comfortable sending patches for it, as I don't understand exactly what the expectations of the various data blocks are.

AFAIK, reproduction should be fairly easy. You just need to run a TCP connection to the receiving node (which gets passed to the kernel) while receiving some UDP packets via AF_XDP at the same time. As mentioned, it also needs frame-size 2048 to reproduce quickly.

I checked if I could get you an easy reproducer, but xdp-tools is quite limited. If you want to keep your approach and can't reproduce the panic yourself, let me know and I can see if I can synthesize a minimal reproducer.

Regards,
Lorenz

#3 lore raw
Quoted 73 lines · Lorenz Brun
Quoted 42 lines · Alexander Lobakin

From: Lorenz Brun <[email protected]>
Date: Tue, 12 May 2026 17:26:56 +0200

Quoted 19 lines

xdp_build_skb_from_zc() allocated xdp->frame_sz bytes from the per-cpu system_page_pool and built the skb head with napi_build_skb(). The latter places skb_shared_info at the tail of the buffer, but the helper sized the allocation as if the whole frame_sz were usable for data. Whenever the packet plus reserved headroom approached frame_sz, the head memcpy overran shinfo with packet content, corrupting ->flags (SKBFL_ZEROCOPY_ENABLE) and ->nr_frags, which then drove skb_copy_ubufs() off the end of frags[] on the RX path:

  UBSAN: array-index-out-of-bounds in include/linux/skbuff.h:2541
  index 113 is out of range for type 'skb_frag_t [17]'
   skb_copy_ubufs+0x7da/0x960
   ip_local_deliver_finish+0xcd/0x110
   ice_napi_poll+0xe4/0x2a0 [ice]

The overrun bytes come from the packet, so an on-wire sender can corrupt kernel memory remotely whenever the XDP program returns XDP_PASS.

Quoted 7 lines

Rather than patch the sizing math, switch to the pattern used by other in-tree AF_XDP zero-copy drivers like mlx5 and i40e which use napi_alloc_skb() sized to the actual packet plus skb_put_data(). This sizes the head exactly for the data being copied, drops the system_page_pool local_lock from this path, and removes the structural mismatch between frame_sz and the skb head buffer. Frags are allocated with alloc_page() per frag, matching the other drivers.

I used napi_build_skb() + system page_pool to enable PP recycling improving XSk XDP_PASS performance a lot.
Are you sure there's no other way to approach this?

napi_alloc_skb() used in other drivers works, but it's sorta old approach which is way slower.

System page_pools always allocate a full page, why can it create an skb prone to overruns?

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

Thanks,
Olek

Hi Olek

I looked at the code again. While your approach is indeed faster, it is only faster for traffic bypassing AF_XDP, which is generally not that relevant for performance.

More critically, it currently corrupts kernel memory and panics the kernel very quickly when running with frame-size set to 2048, 1500 MTU, and passing received packets. To be honest, I'm not familiar enough with the XSK subsystem to know exactly what specific sizing assumption was violated here. By comparison, the approach taken by the other drivers is a lot more obviously correct and works perfectly.

If you want to preserve the current approach, I'm perfectly happy with that. However, I don't feel comfortable sending patches for it, as I don't understand exactly what the expectations of the various data blocks are.

AFAIK, reproduction should be fairly easy. You just need to run a TCP connection to the receiving node (which gets passed to the kernel) while receiving some UDP packets via AF_XDP at the same time. As mentioned, it also needs frame-size 2048 to reproduce quickly.

Quoted 4 lines

I checked if I could get you an easy reproducer, but xdp-tools is quite limited. If you want to keep your approach and can't reproduce the panic yourself, let me know and I can see if I can synthesize a minimal reproducer.

We now respect the tailroom in UMEM which is supposed to address shinfo override cases. Could you re-test this on your side with cited patchset being present on your tree?

https://lore.kernel.org/bpf/[email protected]/

Quoted 3 lines

Regards,
Lorenz

#4 lore raw

From: Maciej Fijalkowski <[email protected]>
Date: Mon, 18 May 2026 15:29:00 +0200

Quoted 79 lines · Lorenz Brun
Quoted 73 lines
Quoted 42 lines · Alexander Lobakin

From: Lorenz Brun <[email protected]>
Date: Tue, 12 May 2026 17:26:56 +0200

Quoted 19 lines

xdp_build_skb_from_zc() allocated xdp->frame_sz bytes from the per-cpu system_page_pool and built the skb head with napi_build_skb(). The latter places skb_shared_info at the tail of the buffer, but the helper sized the allocation as if the whole frame_sz were usable for data. Whenever the packet plus reserved headroom approached frame_sz, the head memcpy overran shinfo with packet content, corrupting ->flags (SKBFL_ZEROCOPY_ENABLE) and ->nr_frags, which then drove skb_copy_ubufs() off the end of frags[] on the RX path:

  UBSAN: array-index-out-of-bounds in include/linux/skbuff.h:2541
  index 113 is out of range for type 'skb_frag_t [17]'
   skb_copy_ubufs+0x7da/0x960
   ip_local_deliver_finish+0xcd/0x110
   ice_napi_poll+0xe4/0x2a0 [ice]

The overrun bytes come from the packet, so an on-wire sender can corrupt kernel memory remotely whenever the XDP program returns XDP_PASS.

Quoted 7 lines

Rather than patch the sizing math, switch to the pattern used by other in-tree AF_XDP zero-copy drivers like mlx5 and i40e which use napi_alloc_skb() sized to the actual packet plus skb_put_data(). This sizes the head exactly for the data being copied, drops the system_page_pool local_lock from this path, and removes the structural mismatch between frame_sz and the skb head buffer. Frags are allocated with alloc_page() per frag, matching the other drivers.

I used napi_build_skb() + system page_pool to enable PP recycling improving XSk XDP_PASS performance a lot.
Are you sure there's no other way to approach this?

napi_alloc_skb() used in other drivers works, but it's sorta old approach which is way slower.

System page_pools always allocate a full page, why can it create an skb prone to overruns?

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

Thanks,
Olek

Hi Olek

I looked at the code again. While your approach is indeed faster, it is only faster for traffic bypassing AF_XDP, which is generally not that relevant for performance.

More critically, it currently corrupts kernel memory and panics the kernel very quickly when running with frame-size set to 2048, 1500 MTU, and passing received packets. To be honest, I'm not familiar enough with the XSK subsystem to know exactly what specific sizing assumption was violated here. By comparison, the approach taken by the other drivers is a lot more obviously correct and works perfectly.

If you want to preserve the current approach, I'm perfectly happy with that. However, I don't feel comfortable sending patches for it, as I don't understand exactly what the expectations of the various data blocks are.

AFAIK, reproduction should be fairly easy. You just need to run a TCP connection to the receiving node (which gets passed to the kernel) while receiving some UDP packets via AF_XDP at the same time. As mentioned, it also needs frame-size 2048 to reproduce quickly.

Quoted 4 lines

I checked if I could get you an easy reproducer, but xdp-tools is quite limited. If you want to keep your approach and can't reproduce the panic yourself, let me know and I can see if I can synthesize a minimal reproducer.

Quoted 5 lines

We now respect the tailroom in UMEM which is supposed to address shinfo override cases. Could you re-test this on your side with cited patchset being present on your tree?

https://lore.kernel.org/bpf/[email protected]/

Either way and regardless of whether XSk XDP_PASS is performance-demanding or not, fixing an issue by replacing the implementation with the one from some driver "because it works" is not something I'd like to see.
If you have difficulties with root-causing the actual problem, I can take a look and fix it since it's my code.

But yeah, first make sure the series Maciej mentioned is present in your tree.

Thanks,
Olek