From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: netdev@vger.kernel.org, kuba@kernel.org, pabeni@redhat.com,
magnus.karlsson@intel.com, sdf@fomichev.me, ast@kernel.org,
daniel@iogearbox.net, hawk@kernel.org, john.fastabend@gmail.com,
jtollet@cisco.com, willemb@google.com, andrii@kernel.org,
eddyz87@gmail.com, memxor@gmail.com
Cc: bpf@vger.kernel.org, bjorn@kernel.org, kerneljasonxing@gmail.com,
marcin.knap@vecima.com, joshwash@google.com,
hramamurthy@google.com, sgoutham@marvell.com,
martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev,
toke@redhat.com, hangbin.liu@linux.dev
Subject: [PATCH net v6 2/2] xdp: Do not free ZC buffer when a frame redirect fails
Date: Fri, 4 Dec 2026 20:39:29 +0100 [thread overview]
Message-ID: <20261204193929.1168874-3-maciej.fijalkowski@intel.com> (raw)
In-Reply-To: <20261204193929.1168874-1-maciej.fijalkowski@intel.com>
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
prev parent reply other threads:[~2026-12-04 19:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204193929.1168874-3-maciej.fijalkowski@intel.com \
--to=maciej.fijalkowski@intel.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hangbin.liu@linux.dev \
--cc=hawk@kernel.org \
--cc=hramamurthy@google.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=joshwash@google.com \
--cc=jtollet@cisco.com \
--cc=kerneljasonxing@gmail.com \
--cc=kuba@kernel.org \
--cc=magnus.karlsson@intel.com \
--cc=marcin.knap@vecima.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=sgoutham@marvell.com \
--cc=song@kernel.org \
--cc=toke@redhat.com \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox