* [PATCH net v6 0/2] xsk, xdp: fix ZC buffer double free on failed redirect
@ 2026-12-04 19:39 Maciej Fijalkowski
2026-12-04 19:39 ` [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full Maciej Fijalkowski
2026-12-04 19:39 ` [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails Maciej Fijalkowski
0 siblings, 2 replies; 5+ messages in thread
From: Maciej Fijalkowski @ 2026-12-04 19:39 UTC (permalink / raw)
To: netdev, kuba, pabeni, magnus.karlsson, sdf, ast, daniel, hawk,
john.fastabend, jtollet, willemb, andrii, eddyz87, memxor
Cc: bpf, bjorn, kerneljasonxing, marcin.knap, joshwash, hramamurthy,
sgoutham, martin.lau, song, yonghong.song, jolsa, emil,
ihor.solodrai, toke, hangbin.liu
Changes in v6:
v5: https://lore.kernel.org/all/20261113-xsk-zc-rx-double-free-v5-1-6ead3af4d131@vecima.com/
- Add patch 2 for the devmap, cpumap and ifindex paths (Jakub).
- Patch 1: update the changelog paragraph about the devmap path, which
patch 2 now fixes. No code changes, rebased on current net.
Hi,
A failed xdp_do_redirect() leaves a zero-copy buffer with the driver,
which then frees it. Two paths in the core free the buffer themselves
before failing:
- xsk_rcv_zc(), when the Rx ring of the target XSK is full;
- xdp_convert_zc_to_xdp_frame(), which copies the buffer for a devmap,
cpumap or ifindex target and frees it before the redirect of the
copy can still fail. The copy then leaks as well.
The second free is harmless when the driver frees the buffer right
away, as xp_free() skips a buffer that is already on the pool free
list. It corrupts the pool when the driver frees it later, after the
buffer may have been handed out again, as mlx5e does on Rx WQE refill.
Marcin's patch 1 fixes the first path, and its v1 to v5 were posted on
their own. Jakub asked for all redirect targets to be fixed in one
series [0], and with Marcin away I took it over [1].
Patch 1 is Marcin's v5 without code changes. I only updated the
paragraph about the devmap path in its changelog, as patch 2 fixes it.
Patch 2 makes xdp_do_redirect() copy the buffer without freeing it,
and free it only once the copy has been redirected. If the redirect
fails, the copy is freed and the buffer is left to the driver. With
both patches, a failed xdp_do_redirect() never frees a zero-copy
buffer, whatever the target, so this complements original intent from
patch 1.
I asked LLM to test it with virtio_net in zero-copy mode under QEMU,
20000 packets per case. The XDP program redirects either to lo through a
devmap or an ifindex, which fails as lo has no ndo_xdp_xmit; to another
virtio_net device through a devmap, which succeeds; or to an XSKMAP
whose socket's Rx ring is full. xp_free() calls were counted with a
kprobe and leaked pages derived from nr_free_pages (rounded, as it is
noisy), both per packet:
xp_free() calls pages leaked
base p1 p1+p2 base p1 p1+p2
devmap, fails 2 2 1 1 1 0
ifindex, fails 2 2 1 1 1 0
XSKMAP, Rx ring full 2 1 1 0 0 0
devmap, succeeds 1 1 1 0 0 0
XDP_TX 1 1 1 0 0 0
[0] https://lore.kernel.org/r/20261111173715.75bccc04@kernel.org
[1] https://lore.kernel.org/r/av3tAd5kTyxfmo1S@boxer
Maciej Fijalkowski (1):
xdp: Do not free ZC buffer when a frame redirect fails
Marcin Knap (1):
xsk: Do not free ZC Rx buffer when the Rx ring is full
drivers/net/ethernet/google/gve/gve_rx_dqo.c | 4 +++-
drivers/net/ethernet/intel/i40e/i40e_xsk.c | 1 +
drivers/net/ethernet/intel/ice/ice_xsk.c | 1 +
.../marvell/octeontx2/nic/otx2_txrx.c | 3 ++-
include/net/xdp.h | 2 ++
net/core/filter.c | 23 +++++++++++++++++--
net/core/xdp.c | 19 +++++++++++++--
net/xdp/xsk.c | 15 +++---------
8 files changed, 50 insertions(+), 18 deletions(-)
base-commit: e9d78fc6741ac06d438cf64e314c28e57014432a
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full 2026-12-04 19:39 [PATCH net v6 0/2] xsk, xdp: fix ZC buffer double free on failed redirect Maciej Fijalkowski @ 2026-12-04 19:39 ` Maciej Fijalkowski 2026-12-05 19:42 ` netdev-bot+sashiko 2026-12-04 19:39 ` [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails Maciej Fijalkowski 1 sibling, 1 reply; 5+ messages in thread From: Maciej Fijalkowski @ 2026-12-04 19:39 UTC (permalink / raw) To: netdev, kuba, pabeni, magnus.karlsson, sdf, ast, daniel, hawk, john.fastabend, jtollet, willemb, andrii, eddyz87, memxor Cc: bpf, bjorn, kerneljasonxing, marcin.knap, joshwash, hramamurthy, sgoutham, martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai, toke, hangbin.liu From: Marcin Knap <marcin.knap@vecima.com> When an XDP program redirects a zero-copy buffer to an XSK and the socket's Rx ring is full, xsk_rcv_zc() frees the buffer and returns -ENOBUFS to xdp_do_redirect(). A failed xdp_do_redirect() leaves the buffer owned by the caller, so the driver releases it as well. That is harmless when the driver frees it right away, as i40e, ice, igc, stmmac, idpf and most others do: xp_free() ignores a buffer that is already on the pool free list. It breaks when the driver releases or uses the buffer later, after it may have been reallocated: - mlx5e treats the failure as XDP_DROP and releases the buffer when the Rx WQE is refilled. By then the buffer can be in use for another Rx descriptor, so an in-use buffer goes back on the free list and the same UMEM frame is later handed to user space twice. With striding RQ this also ended in a NULL pointer dereference in mlx5e_post_rx_wqes(). - ixgbe keeps the buffer in the ring on its need_wakeup -ENOBUFS (XDP_EXIT) path and runs XDP on it again on the next poll. - dpaa2 hands the buffer back to its hardware pool while it is also on the pool free list. - octeontx2 returns false on a failed redirect, so the caller goes on to build an skb from the freed buffer. Leave the buffer with the caller on both the single-buffer and the multi-buffer -ENOBUFS paths, as xsk_rcv() and xsk_rcv_check() already do. Make the drivers that relied on the core freeing the buffer free it themselves: - i40e and ice consume the descriptor on XDP_EXIT without freeing it. - gve DQO counts the redirect error without freeing the buffer. - octeontx2 now frees the buffer and reports it as consumed. This covers redirects to an XSKMAP. A zero-copy buffer redirected to a devmap, a cpumap or an ifindex is copied by xdp_convert_zc_to_xdp_frame(), which frees it before __xdp_do_redirect_frame() can still fail. The driver then releases it a second time, and the copied page leaks. That path is not changed here and is fixed separately in xdp_do_redirect(). Seen on Mellanox ConnectX-6 DX MT2892 (FW 22.47.1026) with AF_XDP zero-copy on 7.3.0-rc6 when the user-space consumer could not keep up with the Rx ring. With this change no duplicate frames were seen with striding RQ on or off. Fixes: 269009893146 ("xsk: recycle buffer in case Rx queue was full") Signed-off-by: Marcin Knap <marcin.knap@vecima.com> Reviewed-by: Björn Töpel <bjorn@kernel.org> Reviewed-by: Jason Xing <kerneljasonxing@gmail.com> Reviewed-by: Nitin Shetty J <nshettyj@marvell.com> Tested-by: Jerome Tollet <jtollet@cisco.com> Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> --- drivers/net/ethernet/google/gve/gve_rx_dqo.c | 4 +++- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 1 + drivers/net/ethernet/intel/ice/ice_xsk.c | 1 + .../ethernet/marvell/octeontx2/nic/otx2_txrx.c | 3 ++- net/xdp/xsk.c | 15 +++------------ 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/drivers/net/ethernet/google/gve/gve_rx_dqo.c b/drivers/net/ethernet/google/gve/gve_rx_dqo.c index b4dbfb1ba39b..349b42d60fc2 100644 --- a/drivers/net/ethernet/google/gve/gve_rx_dqo.c +++ b/drivers/net/ethernet/google/gve/gve_rx_dqo.c @@ -687,8 +687,10 @@ static void gve_xsk_done_dqo(struct gve_priv *priv, struct gve_rx_ring *rx, goto err; break; case XDP_REDIRECT: - if (unlikely(xdp_do_redirect(priv->dev, xdp, xprog))) + if (unlikely(xdp_do_redirect(priv->dev, xdp, xprog))) { + xsk_buff_free(xdp); goto err; + } break; } diff --git a/drivers/net/ethernet/intel/i40e/i40e_xsk.c b/drivers/net/ethernet/intel/i40e/i40e_xsk.c index 5f5f33b2a097..8760a493767c 100644 --- a/drivers/net/ethernet/intel/i40e/i40e_xsk.c +++ b/drivers/net/ethernet/intel/i40e/i40e_xsk.c @@ -356,6 +356,7 @@ static void i40e_handle_xdp_result_zc(struct i40e_ring *rx_ring, return; if (xdp_res == I40E_XDP_EXIT) { + xsk_buff_free(xdp_buff); *failure = true; return; } diff --git a/drivers/net/ethernet/intel/ice/ice_xsk.c b/drivers/net/ethernet/intel/ice/ice_xsk.c index 0c1ce3e86a45..6e53387818a7 100644 --- a/drivers/net/ethernet/intel/ice/ice_xsk.c +++ b/drivers/net/ethernet/intel/ice/ice_xsk.c @@ -646,6 +646,7 @@ int ice_clean_rx_irq_zc(struct ice_rx_ring *rx_ring, if (likely(xdp_res & (ICE_XDP_TX | ICE_XDP_REDIR))) { xdp_xmit |= xdp_res; } else if (xdp_res == ICE_XDP_EXIT) { + xsk_buff_free(first); failure = true; first = NULL; break; diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c index 90af9b9044aa..6d10c132f61c 100644 --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c @@ -1555,7 +1555,8 @@ static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, *need_xdp_flush = true; return true; } - return false; + xsk_buff_free(xsk_buff); + return true; } err = xdp_do_redirect(pfvf->netdev, &xdp, prog); diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 48a835e53518..933a13a19a78 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -224,21 +224,15 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) struct list_head *xskb_list; u32 contd = 0; u32 num_desc; - int err; - if (likely(!frags)) { - err = __xsk_rcv_zc(xs, xskb, len, contd); - if (err) - goto err; - return 0; - } + if (likely(!frags)) + return __xsk_rcv_zc(xs, xskb, len, contd); contd = XDP_PKT_CONTD; num_desc = xdp_get_shared_info_from_buff(xdp)->nr_frags + 1; if (xskq_prod_nb_free(xs->rx, num_desc) < num_desc) { xs->rx_queue_full++; - err = -ENOBUFS; - goto err; + return -ENOBUFS; } xdp_buff_clear_frags_flag(xdp); @@ -253,9 +247,6 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) } return 0; -err: - xsk_buff_free(xdp); - return err; } static void *xsk_copy_xdp_start(struct xdp_buff *from) -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full 2026-12-04 19:39 ` [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full Maciej Fijalkowski @ 2026-12-05 19:42 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 19:42 UTC (permalink / raw) To: maciej.fijalkowski Cc: netdev, kuba, pabeni, magnus.karlsson, sdf, ast, daniel, hawk, john.fastabend, jtollet, willemb, andrii, eddyz87, memxor, bpf, bjorn, kerneljasonxing, marcin.knap, joshwash, hramamurthy, sgoutham, martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai, toke, hangbin.liu Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 (1) · High: 1 (1) · Medium: 0 · Low: 1 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] The patch relies on ixgbe's XDP_EXIT retry path to handle the retained buffer correctly, but that retry reuses stale xdp_buff state. - [Low] The commit message lists the drivers that break because xsk_rcv_zc() freed the buffer on -ENOBUFS, and names ixgbe as the one… Pre-existing issues: - [Critical] In otx2_xdp_rcv_pkt_handler(), the non-XSK (page pool) XDP_REDIRECT failure path releases the receive page and then returns… - [High] In otx2_xdp_rcv_pkt_handler(), when pool->xsk_pool is set, the code runs the program on xsk_buff and does `goto handle_xdp_verdict`… -- xsk: Do not free ZC Rx buffer when the Rx ring is full This patch stops xsk_rcv_zc() from freeing the zero-copy buffer when the XSK Rx ring is full. The buffer now stays with the driver that called xdp_do_redirect(). It also updates i40e, ice, gve DQO and octeontx2, which relied on the core freeing the buffer, so that they free it themselves. > A failed xdp_do_redirect() leaves the buffer owned by the caller, so > the driver releases it as well. That is harmless when the driver frees > it right away, as i40e, ice, igc, stmmac, idpf and most others do: > xp_free() ignores a buffer that is already on the pool free list. It > breaks when the driver releases or uses the buffer later, after it may > have been reallocated: [ ... ] > - ixgbe keeps the buffer in the ring on its need_wakeup -ENOBUFS > (XDP_EXIT) path and runs XDP on it again on the next poll. [Severity: Low] Should igb be listed next to ixgbe here? igb_run_xdp_zc() returns IGB_XDP_EXIT on -ENOBUFS when need_wakeup is enabled. igb_clean_rx_irq_zc() then handles it the same way ixgbe does: drivers/net/ethernet/intel/igb/igb_xsk.c:igb_clean_rx_irq_zc() { ... } else if (xdp_res == IGB_XDP_EXIT) { failure = true; break; ... } ntc is not advanced and rx_buffer_info_zc[ntc] is not cleared, so the same xdp_buff goes through XDP again on the next poll. Before this patch, that buffer was already on the pool free list. The core change in xsk_rcv_zc() fixes igb without any driver change. The changelog doesn't say so, though, and it suggests igb is one of "most others" that free the buffer right away. Could igb be added to the list of affected drivers? [ ... ] > diff --git a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c > index 90af9b9044aa0..6d10c132f61cb 100644 > --- a/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c > +++ b/drivers/net/ethernet/marvell/octeontx2/nic/otx2_txrx.c > @@ -1555,7 +1555,8 @@ static bool otx2_xdp_rcv_pkt_handler(struct otx2_nic *pfvf, > *need_xdp_flush = true; > return true; > } > - return false; > + xsk_buff_free(xsk_buff); > + return true; > } [Severity: High] This is a pre-existing issue, but this patch is fixing the XSK verdict handling in otx2_xdp_rcv_pkt_handler(). What happens with XDP_TX and XDP_PASS on the XSK path? The XSK branch jumps to the verdict without initializing the local struct xdp_buff xdp: if (pool->xsk_pool) { xsk_buff = pool->xdp[--cq->rbpool->xdp_top]; ... xsk_buff->data_end = xsk_buff->data + cqe->sg.seg_size; act = bpf_prog_run_xdp(prog, xsk_buff); goto handle_xdp_verdict; } The verdict cases still use xdp, though: case XDP_PASS: *metasize = xdp.data - xdp.data_meta; break; case XDP_TX: ... xdpf = xdp_convert_buff_to_frame(&xdp); xdp_convert_buff_to_frame() dereferences xdp->rxq->mem.type immediately. xdp.rxq is uninitialized stack memory, or NULL with INIT_STACK_ALL_ZERO. Can an XDP_TX verdict on an AF_XDP zero-copy queue oops in NAPI context here? xsk_buff is also never converted or released on that path. For XDP_PASS, metasize comes from uninitialized stack memory, and the caller passes it to skb_metadata_set(). > > err = xdp_do_redirect(pfvf->netdev, &xdp, prog); [Severity: Critical] This isn't a bug introduced by this patch, but the non-XSK branch just below seems to have the same problem the commit message describes for octeontx2 ("returns false on a failed redirect, so the caller goes on to build an skb from the freed buffer"): err = xdp_do_redirect(pfvf->netdev, &xdp, prog); if (!err) { *need_xdp_flush = true; return true; } otx2_dma_unmap_page(pfvf, iova, pfvf->rbsize, DMA_FROM_DEVICE); xdpf = xdp_convert_buff_to_frame(&xdp); xdp_return_frame(xdpf); break; ... return false; The page is unmapped and returned through xdp_return_frame(), and then the function returns false. otx2_rcv_pkt_handler() treats false as XDP_PASS: if (pfvf->xdp_prog) if (otx2_xdp_rcv_pkt_handler(pfvf, pfvf->xdp_prog, cqe, cq, &metasize, need_xdp_flush)) return; skb = napi_get_frags(napi); ... if (otx2_skb_add_frag(pfvf, skb, *seg_addr, seg_size[seg], parse, cq->cq_idx)) cq->pool_ptrs++; Does this attach the already freed page to an skb and pass it to GRO? cq->pool_ptrs also gets incremented a second time. This looks reachable whenever __xdp_do_redirect_frame() fails, for example when a devmap or ifindex target is down or has no ndo_xdp_xmit. Should this branch also return true after releasing the page? The later patch in this series doesn't touch this driver either. > diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c > index 48a835e535188..933a13a19a781 100644 > --- a/net/xdp/xsk.c > +++ b/net/xdp/xsk.c > @@ -224,21 +224,15 @@ static int xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) > struct list_head *xskb_list; > u32 contd = 0; > u32 num_desc; > - int err; > > - if (likely(!frags)) { > - err = __xsk_rcv_zc(xs, xskb, len, contd); > - if (err) > - goto err; > - return 0; > - } > + if (likely(!frags)) > + return __xsk_rcv_zc(xs, xskb, len, contd); [Severity: High] Now that the buffer stays with the driver, ixgbe's XDP_EXIT retry becomes the normal path whenever the XSK Rx ring is full. Does that retry start from a clean xdp_buff? drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c:ixgbe_clean_rx_irq_zc() { ... bi->xdp->data_end = bi->xdp->data + size; xsk_buff_dma_sync_for_cpu(bi->xdp); xdp_res = ixgbe_run_xdp_zc(adapter, rx_ring, bi->xdp); if (likely(xdp_res & (IXGBE_XDP_TX | IXGBE_XDP_REDIR))) { xdp_xmit |= xdp_res; } else if (xdp_res == IXGBE_XDP_EXIT) { failure = true; break; ... } On the next poll only data_end is recomputed. data and data_meta keep whatever the previous program run left in them. igb resets them with xsk_buff_set_size(). Suppose the program calls bpf_xdp_adjust_head() with a positive offset N before redirecting. data then moves forward by N on every retry, and data_end moves with it, so this check in bpf_xdp_adjust_head() keeps passing: if (unlikely(data < data_start || data > xdp->data_end - ETH_HLEN)) return -EINVAL; After a single retry, user space would get the packet shifted by N bytes, with N trailing bytes the hardware never wrote. If the Rx ring stays full for enough polls, can [data, data_end) move past the end of the UMEM chunk? The program could then read and write the neighbouring chunk. When the redirect finally succeeds, in aligned mode xp_get_handle() takes the descriptor address from xskb->xdp.data - pool->addrs. That address could then point into another chunk, so the same frame would be handed to user space twice. Would ixgbe need an xsk_buff_set_size() style reset before it runs XDP again, as igb does? The later patch in this series ("xdp: Do not free ZC buffer when a frame redirect fails") doesn't seem to touch ixgbe. [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204193929.1168874-1-maciej.fijalkowski%40intel.com ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails 2026-12-04 19:39 [PATCH net v6 0/2] xsk, xdp: fix ZC buffer double free on failed redirect Maciej Fijalkowski 2026-12-04 19:39 ` [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full Maciej Fijalkowski @ 2026-12-04 19:39 ` Maciej Fijalkowski 2026-12-05 19:42 ` netdev-bot+sashiko 1 sibling, 1 reply; 5+ messages in thread From: Maciej Fijalkowski @ 2026-12-04 19:39 UTC (permalink / raw) To: netdev, kuba, pabeni, magnus.karlsson, sdf, ast, daniel, hawk, john.fastabend, jtollet, willemb, andrii, eddyz87, memxor Cc: bpf, bjorn, kerneljasonxing, marcin.knap, joshwash, hramamurthy, sgoutham, martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai, toke, hangbin.liu When an XDP program redirects a zero-copy buffer to a devmap, a cpumap or map-less target, xdp_do_redirect() copies it into a new frame with xdp_convert_zc_to_xdp_frame(), which also frees the zero-copy buffer. The redirect of the copy can still fail after that, e.g. with -EOPNOTSUPP, -ENETDOWN or -EMSGSIZE from __xdp_enqueue(), -EINVAL for an unknown ifindex or -ENOMEM when dev_map_enqueue_multi() fails to clone the frame. Then: - Nothing frees the copied frame, so its page leaks. This happens with every zero-copy driver. - The driver sees the error and frees the zero-copy buffer once more. That is harmless when the driver frees it right away, as i40e, ice and most others do: xp_free() ignores a buffer that is already on the pool free list. It breaks when the driver does not give the buffer back to the pool right away, the same way as on the XSKMAP path: mlx5e releases it when the Rx WQE is refilled, by which time it can be in use again, and dpaa2 recycles it into its hardware pool while it is also on the pool free list. Add __xdp_convert_zc_to_xdp_frame(), which frees the zero-copy buffer after copying it only when asked to, and use it in xdp_do_redirect() to copy the buffer without freeing it. Free the buffer only once the copy has been redirected. If the redirect fails, free the copy and leave the zero-copy buffer to the caller, as for a failed redirect to an XSKMAP. Zero-copy drivers already free the buffer when xdp_do_redirect() fails, as they have to when the copy itself fails, so they need no changes. Other memory types are not copied: their frame is built in place in the buffer, which the caller already releases when xdp_do_redirect() fails, so that path is unchanged. xdp_convert_zc_to_xdp_frame() still frees the buffer on success, for the zero-copy XDP_TX paths that rely on it, such as those of i40e, mlx5e and stmmac. Fixes: e624d4ed4aa8 ("xdp: Extend xdp_redirect_map with broadcast support") Fixes: d53ad5d8b218 ("xdp: Move conversion to xdp_frame out of map functions") Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> --- include/net/xdp.h | 2 ++ net/core/filter.c | 23 +++++++++++++++++++++-- net/core/xdp.c | 19 +++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/include/net/xdp.h b/include/net/xdp.h index 07231adfb5f8..918867d6644f 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -381,6 +381,8 @@ void xdp_warn(const char *msg, const char *func, const int line); struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp); struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp); +struct xdp_frame *__xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp, + bool release); 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, diff --git a/net/core/filter.c b/net/core/filter.c index b7d38b075ef3..98484ee96ad8 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4775,12 +4775,31 @@ int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp, { struct bpf_redirect_info *ri = bpf_net_ctx_get_ri(); enum bpf_map_type map_type = ri->map_type; + struct xdp_frame *xdpf; + bool zc; + int err; if (map_type == BPF_MAP_TYPE_XSKMAP) return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog); - return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp), - xdp_prog); + zc = xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL; + if (unlikely(zc)) + xdpf = __xdp_convert_zc_to_xdp_frame(xdp, false); + else + xdpf = xdp_convert_buff_to_frame(xdp); + err = __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog); + if (unlikely(zc)) { + /* + * On error, the XSk buffer is still owned and freed by the + * driver, only its copy has to be released here. + */ + if (!err) + xsk_buff_free(xdp); + else if (xdpf) + xdp_return_frame(xdpf); + } + + return err; } EXPORT_SYMBOL_GPL(xdp_do_redirect); diff --git a/net/core/xdp.c b/net/core/xdp.c index 1d679e8fd649..0466b1d62231 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -573,7 +573,8 @@ void xdp_attachment_setup(struct xdp_attachment_info *info, } EXPORT_SYMBOL_GPL(xdp_attachment_setup); -struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) +struct xdp_frame * +__xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp, bool release) { unsigned int metasize, totsize; void *addr, *data_to_copy; @@ -607,9 +608,23 @@ struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) xdpf->frame_sz = PAGE_SIZE; xdpf->mem_type = MEM_TYPE_PAGE_ORDER0; - xsk_buff_free(xdp); + if (release) + xsk_buff_free(xdp); + return xdpf; } + +/** + * xdp_convert_zc_to_xdp_frame - copy an XSk buffer into a new &xdp_frame + * @xdp: XSk buffer to copy, freed if the copy succeeds + * + * Return: new %MEM_TYPE_PAGE_ORDER0 frame, or %NULL if the data does not + * fit into a page or the allocation fails. + */ +struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) +{ + return __xdp_convert_zc_to_xdp_frame(xdp, true); +} EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame); /* Used by XDP_WARN macro, to avoid inlining WARN() in fast-path */ -- 2.43.0 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails 2026-12-04 19:39 ` [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails Maciej Fijalkowski @ 2026-12-05 19:42 ` netdev-bot+sashiko 0 siblings, 0 replies; 5+ messages in thread From: netdev-bot+sashiko @ 2026-12-05 19:42 UTC (permalink / raw) To: maciej.fijalkowski Cc: netdev, kuba, pabeni, magnus.karlsson, sdf, ast, daniel, hawk, john.fastabend, jtollet, willemb, andrii, eddyz87, memxor, bpf, bjorn, kerneljasonxing, marcin.knap, joshwash, hramamurthy, sgoutham, martin.lau, song, yonghong.song, jolsa, emil, ihor.solodrai, toke, hangbin.liu Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider. Critical: 0 · High: 1 · Medium: 0 · Low: 0 If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough). - [High] On the zero-copy XDP_TX paths of i40e, ixgbe and icssg, the bug this patch fixes for xdp_do_redirect() is still there: a copied frame… -- xdp: Do not free ZC buffer when a frame redirect fails This patch adds __xdp_convert_zc_to_xdp_frame(), which takes a release flag. xdp_do_redirect() now uses it so that the zero-copy buffer is freed only after the copied frame has been redirected. If the redirect fails, the copy is returned with xdp_return_frame() and the XSk buffer is left to the driver. xdp_convert_zc_to_xdp_frame() still frees on success, for the zero-copy XDP_TX users. > diff --git a/net/core/xdp.c b/net/core/xdp.c > index 1d679e8fd6493..0466b1d62231e 100644 > --- a/net/core/xdp.c > +++ b/net/core/xdp.c [ ... ] > @@ -607,9 +608,23 @@ struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) > xdpf->frame_sz = PAGE_SIZE; > xdpf->mem_type = MEM_TYPE_PAGE_ORDER0; > > - xsk_buff_free(xdp); > + if (release) > + xsk_buff_free(xdp); > + > return xdpf; > } > + > +/** > + * xdp_convert_zc_to_xdp_frame - copy an XSk buffer into a new &xdp_frame > + * @xdp: XSk buffer to copy, freed if the copy succeeds > + * > + * Return: new %MEM_TYPE_PAGE_ORDER0 frame, or %NULL if the data does not > + * fit into a page or the allocation fails. > + */ > +struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) > +{ > + return __xdp_convert_zc_to_xdp_frame(xdp, true); > +} [Severity: High] The commit message says free-on-success is kept "for the zero-copy XDP_TX paths that rely on it, such as those of i40e, mlx5e and stmmac". The zero-copy XDP_TX paths in i40e, ixgbe and icssg seem to have the same leak of the copied page, and the same second xsk_buff_free(), that this patch fixes for xdp_do_redirect(). Is that right? In i40e the conversion frees the XSk buffer right away: drivers/net/ethernet/intel/i40e/i40e_txrx.c:i40e_xmit_xdp_tx_ring() { struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp); if (unlikely(!xdpf)) return I40E_XDP_CONSUMED; return i40e_xmit_xdp_ring(xdpf, xdp_ring); } i40e_xmit_xdp_ring() can then fail without freeing or storing xdpf. One case is a full ring, and the DMA mapping error path is another: if (unlikely(I40E_DESC_UNUSED(xdp_ring) < 1 + nr_frags)) { xdp_ring->tx_stats.tx_busy++; return I40E_XDP_CONSUMED; } i40e_run_xdp_zc() passes the CONSUMED result to out_failure. Then i40e_handle_xdp_result_zc() frees the XSk buffer a second time: if (xdp_res == I40E_XDP_CONSUMED) { xsk_buff_free(xdp_buff); return; } Would this leak the dev_alloc_page() page from __xdp_convert_zc_to_xdp_frame() for every XDP_TX packet that is dropped this way? A full XDP Tx ring is a normal runtime condition, and remote senders can cause it. ixgbe looks the same. In ixgbe_run_xdp_zc()->ixgbe_xmit_xdp_ring(), a full ring or a DMA map error returns IXGBE_XDP_CONSUMED without freeing xdpf. ixgbe_clean_rx_irq_zc() then calls xsk_buff_free(bi->xdp) again. icssg looks the same too. In emac_run_xdp()->emac_xmit_xdp_frame(), a failure to allocate a descriptor, map the buffer or push it returns ICSSG_XDP_CONSUMED. prueth_xmit_free() does not free xdpf, and emac_rx_packet_zc() then calls xsk_buff_free(xdp) again. The second free is harmless only because xp_free() ignores a buffer that is already on the free list. The commit message makes the same argument for the redirect case. mlx5e_xmit_xdp_buff() and stmmac_xdp_xmit_back() call xdp_return_frame(xdpf) on failure and skip the second XSk free. So this looks specific to these three drivers. There seem to be two ways to fix these XDP_TX paths: - convert with __xdp_convert_zc_to_xdp_frame(xdp, false) and free the XSk buffer only after a successful transmit, as xdp_do_redirect() now does - call xdp_return_frame(xdpf) when the transmit fails and skip the caller's xsk_buff_free() Should one of these be applied? Also, should the commit message and the new kernel-doc stop presenting i40e as a correct user of the free-on-success contract? > EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame); > > /* Used by XDP_WARN macro, to avoid inlining WARN() in fast-path */ -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204193929.1168874-1-maciej.fijalkowski%40intel.com ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-05 19:42 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-04 19:39 [PATCH net v6 0/2] xsk, xdp: fix ZC buffer double free on failed redirect Maciej Fijalkowski 2026-12-04 19:39 ` [PATCH net v6 1/2] xsk: Do not free ZC Rx buffer when the Rx ring is full Maciej Fijalkowski 2026-12-05 19:42 ` netdev-bot+sashiko 2026-12-04 19:39 ` [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails Maciej Fijalkowski 2026-12-05 19:42 ` netdev-bot+sashiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox