From: Slawomir Stepien <sst@poczta.fm>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
"David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Jesper Dangaard Brouer <hawk@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Stanislav Fomichev <sdf@fomichev.me>
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
"Maciej Fijalkowski" <maciej.fijalkowski@intel.com>,
"Tianyi Gao" <tianyi@cloudflare.com>,
"Octavian Purdila" <tavip@google.com>,
"Toke Høiland-Jørgensen" <toke@redhat.com>,
"Alexander Lobakin" <aleksander.lobakin@intel.com>,
"Ilias Apalodimas" <ilias.apalodimas@linaro.org>,
"Lorenzo Bianconi" <lorenzo@kernel.org>,
"Lorenzo Bianconi" <lorenzo.bianconi@oss.qualcomm.com>,
"Magnus Karlsson" <magnus.karlsson@intel.com>,
"Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Eric Dumazet" <edumazet@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"Simon Horman" <horms@kernel.org>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Emil Tsalapatis" <emil@etsalapatis.com>,
"Shuah Khan" <shuah@kernel.org>,
linux-kselftest@vger.kernel.org,
syzbot+ff145014d6b0ce64a173@syzkaller.appspotmail.com,
stable@vger.kernel.org, "Slawomir Stepien" <sst@poczta.fm>
Subject: [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff()
Date: Tue, 24 Nov 2026 11:40:12 +0100 [thread overview]
Message-ID: <20261124104016.532341-4-sst@poczta.fm> (raw)
In-Reply-To: <20261124104016.532341-1-sst@poczta.fm>
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Currently, generic XDP hook uses xdp_rxq_info from netstack Rx queues
which do not have its XDP memory model registered. There is a case when
XDP program calls bpf_xdp_adjust_tail() BPF helper, which in turn
releases underlying memory. This happens when it consumes enough amount
of bytes and when XDP buffer has fragments. For this action the memory
model knowledge passed to XDP program is crucial so that core can call
suitable function for freeing/recycling the page.
For netstack queues it defaults to MEM_TYPE_PAGE_SHARED (0) due to lack
of mem model registration. The problem we're fixing here is when kernel
copied the skb to new buffer backed by system's page_pool and XDP buffer
is built around it. Then when bpf_xdp_adjust_tail() calls
__xdp_return(), it acts incorrectly due to mem type not being set to
MEM_TYPE_PAGE_POOL: page_frag_free() drops the page reference that
page_pool holds, while page_pool and other fragments of the same page
may still use it. With page checks enabled (e.g. CONFIG_DEBUG_VM), the
page allocator reports this as a "page_pool leak" and does not free the
page. Without them, the page is freed while still in use, which can lead
to a use-after-free.
Pull out the existing code from bpf_prog_run_generic_xdp() that
init/prepares xdp_buff onto new helper xdp_convert_skb_to_buff() and
set there the mem type of the rxq info that the xdp_buff points to.
Make it agnostic to current skb->data position.
For generic XDP, the mem type is only consulted when the program
releases fragments; the head can not be freed from under the skb. All
current users pass skbs with fragments through skb_pp_cow_data() first,
and when the CoW fails the skb is linearized, so fragments seen here are
always page_pool backed. Hence set MEM_TYPE_PAGE_POOL unconditionally
and warn under CONFIG_DEBUG_NET if a non-linear skb shows up without
pp_recycle.
Callers that turn the xdp_buff into an xdp_frame, like veth on XDP_TX
and XDP_REDIRECT later in this series, release the head through the
mem type too, so they have to set the type that matches the head
after calling this helper.
This problem was triggered by syzbot as well as by the AF_XDP test
suite.
The problem exists since v6.9, see Fixes:. The stable range starts at
6.4.x, because the veth fix later in this series, which goes back to
v6.4, uses this helper. Before v6.9 generic XDP linearizes all skbs, so
there the mem type set here is never consulted.
Reported-by: syzbot+ff145014d6b0ce64a173@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/netdev/6756c37b.050a0220.a30f1.019a.GAE@google.com/
Fixes: e6d5dbdd20aa ("xdp: add multi-buff support for xdp running in generic mode")
Cc: <stable@vger.kernel.org> # 6.4.x
Suggested-by: Jakub Kicinski <kuba@kernel.org>
Co-developed-by: Octavian Purdila <tavip@google.com>
Signed-off-by: Octavian Purdila <tavip@google.com>
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
[sst: rebased onto bpf/master; set MEM_TYPE_PAGE_POOL unconditionally, add
DEBUG_NET_WARN_ON_ONCE() and kdoc as agreed in v4/v5 review; dropped
Tested-by/Reviewed-by as the code changed; reworded the description
of the mem type and dropped the stale BPF CI remark (BPF CI); reworded
the kdoc, described the use-after-free, the stable range and the
xdp_frame callers (BPF CI); dropped the "# ..." notes after the
Signed-off-by lines (checkpatch)]
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
include/net/xdp.h | 38 ++++++++++++++++++++++++++++++++++++++
net/core/dev.c | 25 ++++---------------------
2 files changed, 42 insertions(+), 21 deletions(-)
diff --git a/include/net/xdp.h b/include/net/xdp.h
index 07231adfb5f8..96ff5067fdaa 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -389,6 +389,44 @@ struct sk_buff *xdp_build_skb_from_frame(struct xdp_frame *xdpf,
struct net_device *dev);
struct xdp_frame *xdpf_clone(struct xdp_frame *xdpf);
+/**
+ * xdp_convert_skb_to_buff - build an xdp_buff on top of an skb
+ * @skb: skb to describe, packet data is taken from its MAC header onwards
+ * @xdp: xdp_buff to initialize
+ * @xdp_rxq: Rx queue info to attach to @xdp
+ *
+ * Sets the memory type of @xdp_rxq to MEM_TYPE_PAGE_POOL; the change stays
+ * on @xdp_rxq after the call. A non-linear @skb is expected to have gone
+ * through skb_pp_cow_data(), so all of its fragments are page_pool backed.
+ * The head is page_pool backed only if the whole skb was copied that way:
+ * a caller that may release the head through the memory type, e.g. by
+ * converting @xdp to an xdp_frame, has to set the type that matches it.
+ */
+static inline
+void xdp_convert_skb_to_buff(struct sk_buff *skb, struct xdp_buff *xdp,
+ struct xdp_rxq_info *xdp_rxq)
+{
+ u32 frame_sz, pkt_len;
+
+ /* SKB "head" area always have tailroom for skb_shared_info */
+ frame_sz = skb_end_pointer(skb) - skb->head;
+ frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
+ pkt_len = skb_tail_pointer(skb) - skb_mac_header(skb);
+
+ xdp_init_buff(xdp, frame_sz, xdp_rxq);
+ xdp_prepare_buff(xdp, skb->head, skb->mac_header, pkt_len, true);
+
+ if (skb_is_nonlinear(skb)) {
+ skb_shinfo(skb)->xdp_frags_size = skb->data_len;
+ xdp_buff_set_frags_flag(xdp);
+ } else {
+ xdp_buff_clear_frags_flag(xdp);
+ }
+
+ DEBUG_NET_WARN_ON_ONCE(skb_is_nonlinear(skb) && !skb->pp_recycle);
+ xdp->rxq->mem.type = MEM_TYPE_PAGE_POOL;
+}
+
static inline
void xdp_convert_frame_to_buff(const struct xdp_frame *frame,
struct xdp_buff *xdp)
diff --git a/net/core/dev.c b/net/core/dev.c
index 6140fd9d11cd..46f5fdd7f855 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -5681,35 +5681,18 @@ static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
const struct bpf_prog *xdp_prog)
{
- void *orig_data, *orig_data_end, *hard_start;
+ void *orig_data, *orig_data_end;
struct netdev_rx_queue *rxqueue;
bool orig_bcast, orig_host;
- u32 mac_len, frame_sz;
+ u32 metalen, act, mac_len;
__be16 orig_eth_type;
struct ethhdr *eth;
- u32 metalen, act;
int off;
- /* The XDP program wants to see the packet starting at the MAC
- * header.
- */
+ rxqueue = netif_get_rxqueue(skb);
mac_len = skb->data - skb_mac_header(skb);
- hard_start = skb->data - skb_headroom(skb);
-
- /* SKB "head" area always have tailroom for skb_shared_info */
- frame_sz = (void *)skb_end_pointer(skb) - hard_start;
- frame_sz += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
- rxqueue = netif_get_rxqueue(skb);
- xdp_init_buff(xdp, frame_sz, &rxqueue->xdp_rxq);
- xdp_prepare_buff(xdp, hard_start, skb_headroom(skb) - mac_len,
- skb_headlen(skb) + mac_len, true);
- if (skb_is_nonlinear(skb)) {
- skb_shinfo(skb)->xdp_frags_size = skb->data_len;
- xdp_buff_set_frags_flag(xdp);
- } else {
- xdp_buff_clear_frags_flag(xdp);
- }
+ xdp_convert_skb_to_buff(skb, xdp, &rxqueue->xdp_rxq);
orig_data_end = xdp->data_end;
orig_data = xdp->data;
--
2.56.0
next prev parent reply other threads:[~2026-11-24 10:40 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
2026-11-24 10:40 ` [PATCH v8 bpf 1/7] bpf: test_run: Use a private rxq info for XDP test runs Slawomir Stepien
2026-11-24 10:40 ` [PATCH v8 bpf 2/7] bpf: test_run: Keep frag_size on the private rxq info Slawomir Stepien
2026-11-24 10:40 ` Slawomir Stepien [this message]
2026-11-24 11:32 ` [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff() bot+bpf-ci
2026-11-24 10:40 ` [PATCH v8 bpf 4/7] veth: use the frame mem type when running XDP on an xdp_frame Slawomir Stepien
2026-11-24 10:40 ` [PATCH v8 bpf 5/7] veth: update mem type in xdp_buff Slawomir Stepien
2026-11-24 10:40 ` [PATCH v8 bpf 6/7] selftests/bpf: Shrink test_run frags after generic XDP ran on lo Slawomir Stepien
2026-11-24 10:40 ` [PATCH v8 bpf 7/7] selftests/bpf: Grow test_run and generic XDP frags after a test run Slawomir Stepien
2026-11-24 21:04 ` [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Alexei Starovoitov
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=20261124104016.532341-4-sst@poczta.fm \
--to=sst@poczta.fm \
--cc=aleksander.lobakin@intel.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=ilias.apalodimas@linaro.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lorenzo.bianconi@oss.qualcomm.com \
--cc=lorenzo@kernel.org \
--cc=maciej.fijalkowski@intel.com \
--cc=magnus.karlsson@intel.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=stable@vger.kernel.org \
--cc=syzbot+ff145014d6b0ce64a173@syzkaller.appspotmail.com \
--cc=tavip@google.com \
--cc=tianyi@cloudflare.com \
--cc=toke@redhat.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