BPF List
 help / color / mirror / Atom feed
* [PATCH v8 bpf 0/7] xdp: fix page_pool leaks
@ 2026-11-24 10:40 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
                   ` (7 more replies)
  0 siblings, 8 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien

v1:
https://lore.kernel.org/bpf/20251003140243.2534865-1-maciej.fijalkowski@intel.com/
v1->v2:
- add skb to xdp_buff converter (Jakub)
- postpone restoration of veth's xdp_rxq_info mem model (AI)

v2:
https://lore.kernel.org/bpf/20251017143103.2620164-1-maciej.fijalkowski@intel.com/T/
v2->v3:
- make skb->xdp_buff conversion agnostic of current skb->data position
  (Toke)
- do not pull mac header in veth (Toke)
- use virt_to_head_page() when checking if xdp->data comes from
  page_pool (Olek)
- add ack from Toke

v3:
https://lore.kernel.org/bpf/20251022125209.2649287-1-maciej.fijalkowski@intel.com/
v3->v4:
- include skb->head when calculating packet length (Toke, lkp)

v4:
https://lore.kernel.org/bpf/20251027121318.2679226-1-maciej.fijalkowski@intel.com/
v4->v5:
- rely again on sk_buff::pp_recycle when setting mem type on rxq
  (Jesper, Jakub)
- add ack from Toke on patch 2 and restore it on patch 1

v5:
https://lore.kernel.org/bpf/20251029221315.2694841-1-maciej.fijalkowski@intel.com/
v5->v6:
- rebase onto bpf/master
- patch 1: set MEM_TYPE_PAGE_POOL unconditionally in
  xdp_convert_skb_to_buff(); mem type only matters for frags consumed
  by the program, and those always come from skb_pp_cow_data() (Jakub)
- patch 1: add DEBUG_NET_WARN_ON_ONCE() for a non-linear skb without
  pp_recycle and document the expectation in kdoc (Jakub); the warning
  is limited to non-linear skbs since linear ones legitimately skip
  the CoW
- new patch 2: veth_xdp_rcv_one() ran the program on an xdp_frame with
  whatever mem type the previous packet left on rq->xdp_rxq; use the
  frame's mem type instead, as cpumap and devmap do. With patch 3 alone
  veth sends page_pool frames to its peer and the peer's program
  trimming frags crashed the kernel, so this comes first
- patch 3 (was 2): veth turns the buff into an xdp_frame on
  XDP_{TX,REDIRECT}, so the head is released through mem type too;
  keep the queue's original model unless the buffer was copied into
  rq->page_pool (Jakub)
- patch 3: take page_pool refs on compound heads via netmem helpers
- patch 3: do not restore the rxq mem model after XDP_{TX,REDIRECT}
  and on the error path; with patch 2 every path sets its own mem type
- patch 3: veth can now redirect MEM_TYPE_PAGE_POOL frames, which needs
  commit b3ca872947ef ("bpf: Use the frame mem type in devmap egress
  XDP") in devmap; list it as a stable prerequisite
- drop Reviewed-by/Tested-by tags as patches 1 and 3 changed

v6:
https://lore.kernel.org/bpf/20261104121316.1155680-1-sst@poczta.fm/
v6->v7:
- rebase onto bpf/master
- new patch 1: bpf_prog_test_run_xdp() borrowed the rxq info of lo's
  queue 0 (or of the ingress_ifindex queue) for its alloc_page() frags;
  once patch 2 makes generic XDP set MEM_TYPE_PAGE_POOL on that queue,
  a later test run freed the frags as page_pool pages (WARN in
  page_pool_unref_netmem(), reported by Sashiko and BPF CI); use a
  private copy of the rxq info with MEM_TYPE_PAGE_SHARED (BPF CI,
  Tianyi)
- new patch 5: selftest that runs generic XDP on lo before shrinking
  test_run frags, so the ordering behind patch 1 no longer depends on
  which tests ran before
- patch 2: reword the description of the mem type and drop the stale
  BPF CI remark in the commit message; Cc stable, as patch 4 needs it
  (BPF CI)
- patch 3: add Reviewed-by from Tianyi
- patch 4: list the backport prerequisites in the commit message
  (BPF CI)
- new performance numbers; the ones in the v6 cover letter were wrong:
  the XDP_REDIRECT run was broken (the devmap entry pointed to a device
  in the wrong netns, so no packet reached the target) and the kernels
  still had debug options (KCOV, page_owner, INIT_ON_ALLOC) enabled

v7:
https://lore.kernel.org/bpf/20261119133032.2015656-1-sst@poczta.fm/
v7->v8:
- rebase onto bpf/master
- new patch 2: test_run wrote frag_size = PAGE_SIZE into the rxq info
  of lo's queue 0, where it stays after the test run. Generic XDP on lo
  then let bpf_xdp_adjust_tail() grow a frag up to the end of its page,
  but frags from skb_pp_cow_data() can be sub-page page_pool chunks, so
  the program could write into the next chunk. With ingress_ifindex the
  private copy from patch 1 took frag_size of the target queue instead,
  usually 0, and growing frags failed. Set frag_size = PAGE_SIZE on the
  private copy only (Sashiko)
- new patch 7: selftests for both cases of patch 2
- patch 1: say why there is no Fixes: tag and why it goes to stable
  together with patch 3 (Sashiko, checkpatch)
- patch 3 (was 2): describe that the wrong mem type can lead to a
  use-after-free, not only to a "page_pool leak" report; explain the
  6.4.x stable range; reword the kdoc: the mem type stays set on the
  rxq info, and a caller that may release the head through it, e.g. by
  converting to an xdp_frame, has to set the type that matches the
  head; describe the xdp_frame callers in the commit message (BPF CI)
- patch 3: drop the "# ..." notes after Octavian's and Maciej's
  Signed-off-by lines (Sashiko, checkpatch); Co-developed-by stays
- patch 4 (was 3): keep Fixes: 718a18a0c8a6, frames with frags reach
  veth_xdp_rcv_one() only since that commit (BPF CI, Tianyi)
- patches 1, 5, 6, 7: put the opening /* of multi-line comments on its
  own line (Sashiko)
- patch 6 (was 5): wrap lines at 80 columns

Hi,

here we fix page_pool leaks which happen when fragment is released
within XDP program and memory type is set incorrectly. The reports come
from syzbot and AF_XDP test suite. With page checks enabled (e.g.
CONFIG_DEBUG_VM) the wrong release shows up as a "page_pool leak";
without them, a page that page_pool still uses can be freed, which can
lead to a use-after-free.

Most of the changes are about pulling out the common code for
init/prepare xdp_buff based on existing skb and supplying it with
correct xdp_rxq_info's mem_type that is assigned to xdp_buff. A bit more
stuff, page_pool related, had to be done on veth side.

Originally, this work was started by Octavian Purdila and continued by
Maciej up to v5. v6 picked up the v5 series, with Maciej's agreement, as
the minimal fix for bpf and stable, and addressed the v5 review. Maciej
stays the author of patches 3 and 5 and my changes are noted in their
commit messages; patches 1, 2, 4, 6 and 7 are new.

For veth, Jakub listed three options: stop turning skbs into xdp_frames
in veth, decide the head's mem type when the CoW was skipped, or CoW the
head on the Tx/Redirect path. The first one changes veth's XDP handling
beyond a fix, so patch 5 takes the second: veth knows whether it just
copied the buffer into rq->page_pool, and only then advertises
MEM_TYPE_PAGE_POOL. An skb that skipped the CoW is linear and keeps the
queue's original model, as before this series. This keeps the series a
minimal fix that is easy to reason about and to backport.

Patch 4 fixes the same problem for frames in veth_xdp_rcv_one() that
commit b3ca872947ef ("bpf: Use the frame mem type in devmap egress
XDP") fixed for devmap; cpumap already handled it.

Patch 1 prepares bpf_prog_test_run_xdp() for patch 3. Generic XDP runs
on the rxq info of the device queue, which is shared with test_run; with
patch 3 alone, any packet through generic XDP on lo makes later
multi-buffer test runs free their frags as page_pool pages. Restoring
the mem type after generic XDP would still race with test runs on other
CPUs, so test_run gets its own copy, like devmap and cpumap. Patch 1
has no Fixes: tag since nothing changes the shared mem type before
patch 3, but it has to be backported together with patch 3.

Patch 2 fixes the other field test_run wrote into the shared rxq info,
frag_size; it is an existing bug, so it has its own Fixes: tag. It is a
separate patch to keep patch 1 a pure prerequisite of patch 3.

Performance: compared to v7, only test_run, comments and selftests
changed; the veth and generic XDP code is the same. The numbers and
their explanation are in the v7 cover letter:
https://lore.kernel.org/bpf/20261119133032.2015656-1-sst@poczta.fm/

Testing of v8 (x86_64, QEMU via virtme-ng, KASAN + DEBUG_VM + DEBUG_NET,
bpf/master cbaaeff649b7):
- patch 7 without patch 2 (on v7): the lo subtest grows a frag of a
  generic XDP packet on lo after a test run (bpf_xdp_adjust_tail()
  returns -EINVAL for the too large offset instead of -EOPNOTSUPP), and
  the ingress_ifindex subtest cannot grow the test_run frags (XDP_DROP
  instead of XDP_TX, size 9000 instead of 9010); with the whole series
  both pass
- ./test_progs -t ns_xsk,xdp: all 34 tests passed, no splats, 208
  subtests (206 in v7)
- full ./test_progs run (with panic_on_warn=0): all xdp, xsk and veth
  tests pass and there are no page_pool or __xdp_return warnings;
  fs_kfuncs, kfree_skb, kprobe_multi_bench_attach,
  memcg_proactive_reclaim, test_ima, uprobe_syscall and
  verifier_live_stack fail the same way without the series, and
  rhash_timer is flaky on both kernels, as in v7;
  timer_stress_nmi_update hung in one of two runs; it triggers a
  lockdep report about the obj_hash lock taken in NMI (debugobjects),
  outside the code this series touches, with v7 as well

Testing of v7 (base 4b4eb7f44ee0), still valid for the unchanged code of
patches 3-5; see the v7 cover letter for the details:
- reproducer: an xdp.frags program that trims all frags with
  bpf_xdp_adjust_tail() on 8000-byte pings over a 9000 MTU veth pair;
  without the series both generic XDP and native veth XDP hit
  "BUG: Bad page state ... page_pool leak" followed by
  VM_BUG_ON_PAGE(page_ref_count(page) == 0) and a panic; with the
  series 20/20 packets are trimmed with no splat
- the standalone AF_XDP suite (test_xsk.sh): without the series dmesg
  shows 60 "page_pool leak" splats from generic XDP; with the series
  there are none
- frame path: one veth bounces 8000-byte pings with XDP_TX, its peer
  trims all frags of the resulting frames; with patches 3+5 only:
  "page_pool leak", VM_BUG_ON_PAGE() and a panic in veth_xdp_rcv_one();
  with the whole series 50/50 frames are trimmed and no splat
- the remaining veth paths and devmap egress after patch 5, each
  confirmed with kprobes; no splat, and none of the page_pool shutdown
  or inflight warnings 75s after teardown
- not tested: page_pool frames from hardware drivers redirected into
  veth, bpf_xdp_pull_data(), multi-queue load

This series was prepared with an AI coding assistant (Claude) from the
v5, v6 and v7 review threads; the assistant wrote the code changes, all
of patches 1, 2, 4, 6 and 7, the changelog updates, and ran the tests
listed above.

Maciej Fijalkowski (2):
  xdp: introduce xdp_convert_skb_to_buff()
  veth: update mem type in xdp_buff

Slawomir Stepien (5):
  bpf: test_run: Use a private rxq info for XDP test runs
  bpf: test_run: Keep frag_size on the private rxq info
  veth: use the frame mem type when running XDP on an xdp_frame
  selftests/bpf: Shrink test_run frags after generic XDP ran on lo
  selftests/bpf: Grow test_run and generic XDP frags after a test run

 drivers/net/veth.c                            |  57 ++++--
 include/net/xdp.h                             |  38 ++++
 net/bpf/test_run.c                            |  15 +-
 net/core/dev.c                                |  25 +--
 .../bpf/prog_tests/xdp_adjust_tail.c          | 191 ++++++++++++++++++
 .../test_xdp_adjust_frags_tail_grow_lo.c      |  25 +++
 6 files changed, 309 insertions(+), 42 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c


base-commit: cbaaeff649b7aa09170cb31db001e5468c2726c0
-- 
2.56.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 1/7] bpf: test_run: Use a private rxq info for XDP test runs
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
@ 2026-11-24 10:40 ` 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
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien, stable

bpf_prog_test_run_xdp() runs the program with xdp->rxq pointing to the
xdp_rxq_info of a real device queue: queue 0 of the loopback device, or
the queue given by ingress_ifindex and rx_queue_index in the context.
The frags of a multi-buffer test packet come from alloc_page(), and when
the program releases one with bpf_xdp_adjust_tail() or
bpf_xdp_pull_data(), __xdp_return() frees it according to
xdp->rxq->mem.type. This only works as long as nobody else changes the
mem type of that shared queue.

A later patch in this series adds xdp_convert_skb_to_buff(), which makes
generic XDP set the mem type of the queue it runs on to
MEM_TYPE_PAGE_POOL, since frags it hands to the program come from
skb_pp_cow_data(). After any packet went through generic XDP on lo, a
later test run would free its alloc_page() frags as page_pool pages:

  WARNING: include/net/page_pool/helpers.h:305 at __xdp_return+0x828/0x9c0
  Call Trace:
   bpf_xdp_adjust_tail+0x8a0/0xd10
   bpf_prog_..._xdp_adjust_tail_shrink+0xe1/0x101
   bpf_test_run+0x31a/0xc30
   bpf_prog_test_run_xdp+0x948/0x1740

and leak the page. Restoring the mem type after generic XDP would not
be enough, as generic XDP may run on the same queue on another CPU at
the same time.

Run the program on a private copy of the rxq info instead, with
MEM_TYPE_PAGE_SHARED, which describes the memory test_run allocates.
devmap and cpumap already do the same for the programs they run. The
device and queue index stay the same, so the program sees the same
ingress_ifindex and rx_queue_index, and xdp_convert_buff_to_md() still
drops the device reference. Live frames mode does not allow frags and
uses its own rxq info for the page_pool frames, so it is unaffected.

No Fixes: tag, as nothing is broken until xdp_convert_skb_to_buff()
changes the mem type; this goes to stable together with that patch.

The problem was found by Sashiko AI review and BPF CI in the review of
v6 of this series.

Link: https://lore.kernel.org/all/sashiko-outbox-197255@kernel.org/
Cc: <stable@vger.kernel.org> # 6.4.x
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 net/bpf/test_run.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index d6c9dc62182e..9bbd83ece67b 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -1348,6 +1348,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	u32 repeat = kattr->test.repeat;
 	struct netdev_rx_queue *rxqueue;
 	struct skb_shared_info *sinfo;
+	struct xdp_rxq_info rxq;
 	struct xdp_buff xdp = {};
 	int i, ret = -EINVAL;
 	struct xdp_md *ctx;
@@ -1426,6 +1427,15 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	if (ret)
 		goto free_data;
 
+	/*
+	 * xdp.rxq points to the rxq info of a real device queue, which
+	 * generic XDP updates for every packet it runs on. Use a private
+	 * copy with the mem type of the frags allocated below.
+	 */
+	rxq = *xdp.rxq;
+	rxq.mem = (struct xdp_mem_info){ .type = MEM_TYPE_PAGE_SHARED };
+	xdp.rxq = &rxq;
+
 	size = linear_sz;
 	if (unlikely(kattr->test.data_size_in > size)) {
 		void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 2/7] bpf: test_run: Keep frag_size on the private rxq info
  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 ` Slawomir Stepien
  2026-11-24 10:40 ` [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff() Slawomir Stepien
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien, stable

bpf_prog_test_run_xdp() sets frag_size of the rxq info of lo's queue 0
to PAGE_SIZE, for the PAGE_SIZE frags it allocates, before
ingress_ifindex may switch the program to another queue. This has two
effects:

The value stays on lo after the test run. Generic XDP on lo runs with
this rxq info, and bpf_xdp_frags_increase_tail() then lets a program
grow the last frag up to the end of its page. The frags generic XDP
gets from skb_pp_cow_data() can be parts of a page_pool page, so the
program overwrites what follows the frag in that page, possibly the
data of another packet. Without a test run, frag_size of lo is 0 and
the grow fails with -EOPNOTSUPP, as for generic XDP on any other
device. Generic XDP runs programs on frags since commit e6d5dbdd20aa
("xdp: add multi-buff support for xdp running in generic mode").

With ingress_ifindex, the program runs with the rxq info of that device
queue, whose frag_size is 0, so growing a frag of the test packet fails
although the frags are full pages.

The previous patch made test_run run the program on a private copy of
the rxq info. Set frag_size there and leave the device's rxq info
alone.

The problem was found by Sashiko AI review in the review of v7 of this
series.

Link: https://lore.kernel.org/all/sashiko-outbox-223211@kernel.org/
Fixes: 1c1949982524 ("bpf: introduce frags support to bpf_prog_test_run_xdp()")
Cc: <stable@vger.kernel.org> # 6.4.x
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 net/bpf/test_run.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index 9bbd83ece67b..8e9543f98211 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -1418,8 +1418,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 
 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
-	rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
-	xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
+	xdp_init_buff(&xdp, PAGE_SIZE, &rxqueue->xdp_rxq);
 	xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
 	sinfo = xdp_get_shared_info_from_buff(&xdp);
 
@@ -1430,10 +1429,12 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	/*
 	 * xdp.rxq points to the rxq info of a real device queue, which
 	 * generic XDP updates for every packet it runs on. Use a private
-	 * copy with the mem type of the frags allocated below.
+	 * copy with the mem type and the frag size of the frags allocated
+	 * below.
 	 */
 	rxq = *xdp.rxq;
 	rxq.mem = (struct xdp_mem_info){ .type = MEM_TYPE_PAGE_SHARED };
+	rxq.frag_size = PAGE_SIZE;
 	xdp.rxq = &rxq;
 
 	size = linear_sz;
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff()
  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
  2026-11-24 11:32   ` 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
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, syzbot+ff145014d6b0ce64a173, stable,
	Slawomir Stepien

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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 4/7] veth: use the frame mem type when running XDP on an xdp_frame
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
                   ` (2 preceding siblings ...)
  2026-11-24 10:40 ` [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff() Slawomir Stepien
@ 2026-11-24 10:40 ` Slawomir Stepien
  2026-11-24 10:40 ` [PATCH v8 bpf 5/7] veth: update mem type in xdp_buff Slawomir Stepien
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien, stable

veth_xdp_rcv_one() runs the XDP program on frames queued by
ndo_xdp_xmit(), i.e. redirected to veth or sent back by the peer with
XDP_TX. The xdp_buff points at rq->xdp_rxq, whose mem type is whatever
the previous packet left there: the queue's MEM_TYPE_PAGE_SHARED, or
the type of an earlier frame or skb. frame->mem_type is copied in only
for XDP_TX and XDP_REDIRECT, after the program has run.

bpf_xdp_adjust_tail() and bpf_xdp_pull_data() release whole frags with
the rxq mem type. For a MEM_TYPE_PAGE_POOL frame and a stale
MEM_TYPE_PAGE_SHARED rxq, page_frag_free() drops the reference owned by
the page_pool:

  BUG: Bad page state in process ping  pfn:2244a
  page dumped because: page_pool leak
   bad_page.cold+0x58/0x6b
   __free_frozen_pages+0x837/0x1280
   page_frag_free+0x1a4/0x200
   __xdp_return+0x3a8/0x9c0
   bpf_xdp_adjust_tail+0x8a0/0xd10
   bpf_prog_007aa02d10594afa_xdp_trim_drop+0x8f/0xd8
   veth_xdp_rcv_one+0x389/0x10e0
   veth_xdp_rcv+0x1c5/0xb20
   veth_poll+0x33d/0xe00
  ...
  kernel BUG at ./include/linux/mm.h:1820!

The reverse, a MEM_TYPE_PAGE_SHARED frame after a page_pool one, hands
pages that are not page_pool backed to page_pool_put_full_netmem().

Set the rxq mem type from the frame before running the program, as
cpumap and devmap do, and drop the now redundant updates in the XDP_TX
and XDP_REDIRECT paths.

The splat above is from two veth peers with the next patch applied:
one bounces multi-buffer packets with XDP_TX, the other trims all frags
of the resulting frames. Page pool backed frames from other drivers
can hit the same path today.

Fixes: 718a18a0c8a6 ("veth: Rework veth_xdp_rcv_skb in order to accept non-linear skb")
Cc: <stable@vger.kernel.org>
Reviewed-by: Tianyi Gao <tianyi@cloudflare.com>
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 drivers/net/veth.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index faa837acea7b..80d3c116e757 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -658,6 +658,8 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 
 		xdp_convert_frame_to_buff(frame, xdp);
 		xdp->rxq = &rq->xdp_rxq;
+		/* The program may release frags, do it the frame's way */
+		xdp->rxq->mem.type = frame->mem_type;
 		vxbuf.skb = NULL;
 
 		act = bpf_prog_run_xdp(xdp_prog, xdp);
@@ -669,7 +671,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 			break;
 		case XDP_TX:
 			orig_frame = *frame;
-			xdp->rxq->mem.type = frame->mem_type;
 			if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 				trace_xdp_exception(rq->dev, xdp_prog, act);
 				frame = &orig_frame;
@@ -681,7 +682,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 			goto xdp_xmit;
 		case XDP_REDIRECT:
 			orig_frame = *frame;
-			xdp->rxq->mem.type = frame->mem_type;
 			if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 				frame = &orig_frame;
 				stats->rx_drops++;
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 5/7] veth: update mem type in xdp_buff
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
                   ` (3 preceding siblings ...)
  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 ` 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
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, stable, Slawomir Stepien

From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>

When skb's headroom is not sufficient for XDP purposes,
skb_pp_cow_data() returns new skb with requested headroom space. This
skb was provided by page_pool.

For CONFIG_DEBUG_VM=y and XDP program that uses bpf_xdp_adjust_tail()
against a skb with frags, and mentioned helper consumed enough amount of
bytes that in turn released the page, following splat was observed:

[   32.204881] BUG: Bad page state in process test_progs  pfn:11c98b
[   32.207167] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x11c98b
[   32.210084] flags: 0x1fffe0000000000(node=0|zone=1|lastcpupid=0x7fff)
[   32.212493] raw: 01fffe0000000000 dead000000000040 ff11000123c9b000 0000000000000000
[   32.218056] raw: 0000000000000000 0000000000000001 00000000ffffffff 0000000000000000
[   32.220900] page dumped because: page_pool leak
[   32.222636] Modules linked in: bpf_testmod(O) bpf_preload
[   32.224632] CPU: 6 UID: 0 PID: 3612 Comm: test_progs Tainted: G O        6.17.0-rc5-gfec474d29325 #6969 PREEMPT
[   32.224638] Tainted: [O]=OOT_MODULE
[   32.224639] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.14.0-0-g155821a1990b-prebuilt.qemu.org 04/01/2014
[   32.224641] Call Trace:
[   32.224644]  <IRQ>
[   32.224646]  dump_stack_lvl+0x4b/0x70
[   32.224653]  bad_page.cold+0xbd/0xe0
[   32.224657]  __free_frozen_pages+0x838/0x10b0
[   32.224660]  ? skb_pp_cow_data+0x782/0xc30
[   32.224665]  bpf_xdp_shrink_data+0x221/0x530
[   32.224668]  ? skb_pp_cow_data+0x6d1/0xc30
[   32.224671]  bpf_xdp_adjust_tail+0x598/0x810
[   32.224673]  ? xsk_destruct_skb+0x321/0x800
[   32.224678]  bpf_prog_004ac6bb21de57a7_xsk_xdp_adjust_tail+0x52/0xd6
[   32.224681]  veth_xdp_rcv_skb+0x45d/0x15a0
[   32.224684]  ? get_stack_info_noinstr+0x16/0xe0
[   32.224688]  ? veth_set_channels+0x920/0x920
[   32.224691]  ? get_stack_info+0x2f/0x80
[   32.224693]  ? unwind_next_frame+0x3af/0x1df0
[   32.224697]  veth_xdp_rcv.constprop.0+0x38a/0xbe0
[   32.224700]  ? common_startup_64+0x13e/0x148
[   32.224703]  ? veth_xdp_rcv_one+0xcd0/0xcd0
[   32.224706]  ? stack_trace_save+0x84/0xa0
[   32.224709]  ? stack_depot_save_flags+0x28/0x820
[   32.224713]  ? __resched_curr.constprop.0+0x332/0x3b0
[   32.224716]  ? timerqueue_add+0x217/0x320
[   32.224719]  veth_poll+0x115/0x5e0
[   32.224722]  ? veth_xdp_rcv.constprop.0+0xbe0/0xbe0
[   32.224726]  ? update_load_avg+0x1cb/0x12d0
[   32.224730]  ? update_cfs_group+0x121/0x2c0
[   32.224733]  __napi_poll+0xa0/0x420
[   32.224736]  net_rx_action+0x901/0xe90
[   32.224740]  ? run_backlog_napi+0x50/0x50
[   32.224743]  ? clockevents_program_event+0x1cc/0x280
[   32.224746]  ? hrtimer_interrupt+0x31e/0x7c0
[   32.224749]  handle_softirqs+0x151/0x430
[   32.224752]  do_softirq+0x3f/0x60
[   32.224755]  </IRQ>

It's because xdp_rxq with mem model set to MEM_TYPE_PAGE_SHARED was used
when initializing xdp_buff.

Fix this by using new helper xdp_convert_skb_to_buff(). Unlike generic
XDP, veth turns the xdp_buff into an xdp_frame on XDP_{TX,REDIRECT}, so
the head is later released through the mem type as well and the
helper's frags-only reasoning is not enough here. Advertise
MEM_TYPE_PAGE_POOL only when the whole buffer was just copied into
rq->page_pool by skb_pp_cow_data(); otherwise keep the queue's original
MEM_TYPE_PAGE_SHARED model. An skb that skips the CoW has no frags, so
nothing can be released through the mem type while the program runs.

XDP_{TX,REDIRECT} no longer reset the mem type to the queue's original
model before the buff is converted to a frame, as the frame has to
carry the type chosen above. Nothing needs to be restored afterwards
either: the previous patch makes the frame path set its own mem type
and every skb sets it here. Add a page_pool variant of veth_xdp_get()
so that we avoid refcount underflow when draining page frag.

Frames redirected out of veth can now be MEM_TYPE_PAGE_POOL. A devmap
egress program only releases their frags correctly since commit
b3ca872947ef ("bpf: Use the frame mem type in devmap egress XDP"), so
that commit has to come first when backporting, as do the previous
three patches of this series: xdp_convert_skb_to_buff() is used here,
dropping the restores relies on the frame path setting its own mem
type, and test_run needs its own rxq info once the helper sets it.
Before v6.9, generic XDP has no frags and xdp_convert_skb_to_buff()
has to be adapted to the older bpf_prog_run_generic_xdp().

Fixes: 0ebab78cbcbf ("net: veth: add page_pool for page recycling")
Reported-by: Alexei Starovoitov <ast@kernel.org>
Closes: https://lore.kernel.org/bpf/CAADnVQ+bBofJDfieyOYzSmSujSfJwDTQhiz3aJw7hE+4E2_iPA@mail.gmail.com/
Cc: <stable@vger.kernel.org> # 6.4.x: b3ca872947ef: bpf: Use the frame mem type in devmap egress XDP
Cc: <stable@vger.kernel.org> # 6.4.x
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
[sst: rebased onto bpf/master; claim MEM_TYPE_PAGE_POOL only after CoW into
 rq->page_pool; take page_pool refs on compound heads via netmem helpers;
 drop the rq mem model restores as every path sets its own mem type now;
 dropped Reviewed-by as the code changed; list the backport
 prerequisites (BPF CI)]
Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 drivers/net/veth.c | 53 +++++++++++++++++++++++++++++++---------------
 1 file changed, 36 insertions(+), 17 deletions(-)

diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index 80d3c116e757..1a14ce77e4a7 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -741,7 +741,7 @@ static void veth_xdp_rcv_bulk_skb(struct veth_rq *rq, void **frames,
 	}
 }
 
-static void veth_xdp_get(struct xdp_buff *xdp)
+static void veth_xdp_get_shared(struct xdp_buff *xdp)
 {
 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
 	int i;
@@ -754,12 +754,36 @@ static void veth_xdp_get(struct xdp_buff *xdp)
 		netmem_inc_ref(skb_frag_netmem(&sinfo->frags[i]));
 }
 
+static void veth_xdp_get_pp(struct xdp_buff *xdp)
+{
+	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
+	int i;
+
+	page_pool_ref_netmem(netmem_compound_head(virt_to_netmem(xdp->data)));
+	if (likely(!xdp_buff_has_frags(xdp)))
+		return;
+
+	for (i = 0; i < sinfo->nr_frags; i++) {
+		netmem_ref netmem = skb_frag_netmem(&sinfo->frags[i]);
+
+		page_pool_ref_netmem(netmem_compound_head(netmem));
+	}
+}
+
+static void veth_xdp_get(struct xdp_buff *xdp)
+{
+	if (xdp->rxq->mem.type == MEM_TYPE_PAGE_POOL)
+		veth_xdp_get_pp(xdp);
+	else
+		veth_xdp_get_shared(xdp);
+}
+
 static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
 					struct xdp_buff *xdp,
 					struct sk_buff **pskb)
 {
 	struct sk_buff *skb = *pskb;
-	u32 frame_sz;
+	bool pp_cow = false;
 
 	if (skb_shared(skb) || skb_head_is_locked(skb) ||
 	    skb_is_nonlinear(skb) ||
@@ -768,21 +792,18 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
 			goto drop;
 
 		skb = *pskb;
+		pp_cow = true;
 	}
 
-	/* 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));
-	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
-	xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
-			 skb_headlen(skb), true);
-
-	if (skb_shinfo(skb)->nr_frags) {
-		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, &rq->xdp_rxq);
+	/*
+	 * Unlike generic XDP, XDP_{TX,REDIRECT} turn the buff into an
+	 * xdp_frame, which later releases the head via mem.type as well.
+	 * Only claim page_pool memory when the whole buffer was copied into
+	 * rq->page_pool above; an skb that skipped the CoW is linear.
+	 */
+	if (!pp_cow)
+		rq->xdp_rxq.mem = rq->xdp_mem;
 	*pskb = skb;
 
 	return 0;
@@ -830,7 +851,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	case XDP_TX:
 		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp->rxq->mem = rq->xdp_mem;
 		if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 			trace_xdp_exception(rq->dev, xdp_prog, act);
 			stats->rx_drops++;
@@ -842,7 +862,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	case XDP_REDIRECT:
 		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp->rxq->mem = rq->xdp_mem;
 		if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 			stats->rx_drops++;
 			goto err_xdp;
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 6/7] selftests/bpf: Shrink test_run frags after generic XDP ran on lo
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
                   ` (4 preceding siblings ...)
  2026-11-24 10:40 ` [PATCH v8 bpf 5/7] veth: update mem type in xdp_buff Slawomir Stepien
@ 2026-11-24 10:40 ` 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
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien

bpf_prog_test_run_xdp() used the rxq info of the loopback device's
queue 0, and generic XDP sets the mem type of that rxq info for the
packets it handles. Run a packet through generic XDP on lo in a new
netns, then repeat xdp_adjust_frags_tail_shrink there, which frees
test_run frags from the program.

Without the test_run fix this hits WARN_ON() in page_pool_unref_netmem()
and leaks the frag pages; the test itself still passes, so the failure
shows up in the kernel log (and as a panic with panic_on_warn).

Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 .../bpf/prog_tests/xdp_adjust_tail.c          | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
index 43264347e7d7..be0c36871911 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
@@ -1,6 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 #include <network_helpers.h>
+#include <linux/if_link.h>
+#include <net/if.h>
+#include "xdp_dummy.skel.h"
 
 static void test_xdp_adjust_tail_shrink(void)
 {
@@ -355,6 +358,54 @@ static void test_xdp_adjust_frags_tail_grow_64k(void)
 	bpf_object__close(obj);
 }
 
+/*
+ * bpf_prog_test_run_xdp() runs on the rxq of lo's queue 0, where generic
+ * XDP sets the mem type of the frags it hands to its program. Let generic
+ * XDP see a packet on lo first, then free test_run frags from the program.
+ */
+static void test_xdp_adjust_frags_tail_shrink_after_generic(void)
+{
+	struct sockaddr_in addr = {
+		.sin_family = AF_INET,
+		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+		.sin_port = htons(9),
+	};
+	struct netns_obj *ns = NULL;
+	struct xdp_dummy *skel;
+	int fd = -1, lo, prog_fd;
+
+	skel = xdp_dummy__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "xdp_dummy__open_and_load"))
+		return;
+
+	ns = netns_new("xdp_adjust_tail_ns", true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto out;
+
+	SYS(out, "ip link set dev lo up");
+	lo = if_nametoindex("lo");
+	if (!ASSERT_GT(lo, 0, "lo ifindex"))
+		goto out;
+
+	prog_fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
+	if (!ASSERT_OK(bpf_xdp_attach(lo, prog_fd, XDP_FLAGS_SKB_MODE, NULL),
+		       "attach generic XDP"))
+		goto out;
+
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (ASSERT_GE(fd, 0, "socket"))
+		ASSERT_EQ(sendto(fd, "x", 1, 0, (struct sockaddr *)&addr,
+				 sizeof(addr)), 1, "sendto");
+	bpf_xdp_detach(lo, XDP_FLAGS_SKB_MODE, NULL);
+
+	test_xdp_adjust_frags_tail_shrink();
+out:
+	if (fd >= 0)
+		close(fd);
+	netns_free(ns);
+	xdp_dummy__destroy(skel);
+}
+
 void test_xdp_adjust_tail(void)
 {
 	int page_size = getpagesize();
@@ -367,6 +418,8 @@ void test_xdp_adjust_tail(void)
 		test_xdp_adjust_tail_grow2();
 	if (test__start_subtest("xdp_adjust_frags_tail_shrink"))
 		test_xdp_adjust_frags_tail_shrink();
+	if (test__start_subtest("xdp_adjust_frags_tail_shrink_after_generic"))
+		test_xdp_adjust_frags_tail_shrink_after_generic();
 	if (test__start_subtest("xdp_adjust_frags_tail_grow")) {
 		if (page_size == 65536)
 			test_xdp_adjust_frags_tail_grow_64k();
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH v8 bpf 7/7] selftests/bpf: Grow test_run and generic XDP frags after a test run
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
                   ` (5 preceding siblings ...)
  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 ` Slawomir Stepien
  2026-11-24 21:04 ` [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Alexei Starovoitov
  7 siblings, 0 replies; 10+ messages in thread
From: Slawomir Stepien @ 2026-11-24 10:40 UTC (permalink / raw)
  To: Alexei Starovoitov, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest, Slawomir Stepien

Add two xdp_adjust_tail subtests for frag_size of the rxq info a
program runs with:

- xdp_adjust_frags_tail_grow_ingress_ifindex grows the last frag of a
  9000 byte test_run packet with ingress_ifindex set to a veth device.
  test_run allocates PAGE_SIZE frags, so the grow must succeed.

- xdp_adjust_frags_tail_grow_lo_after_test_run does a test run, then
  sends a UDP packet a bit larger than a page over lo with a generic XDP
  frags program attached, or is skipped with 64K pages. Generic XDP
  copies the packet into a page_pool head page and a frag, which can be
  a part of a page, so growing the frag must fail with -EOPNOTSUPP. The
  program asks for more than any tailroom, so nothing is written even
  when the grow is allowed.

Signed-off-by: Slawomir Stepien <sst@poczta.fm>
---
 .../bpf/prog_tests/xdp_adjust_tail.c          | 138 ++++++++++++++++++
 .../test_xdp_adjust_frags_tail_grow_lo.c      |  25 ++++
 2 files changed, 163 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c

diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
index be0c36871911..ead6c0be960e 100644
--- a/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
+++ b/tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c
@@ -4,6 +4,7 @@
 #include <linux/if_link.h>
 #include <net/if.h>
 #include "xdp_dummy.skel.h"
+#include "test_xdp_adjust_frags_tail_grow_lo.skel.h"
 
 static void test_xdp_adjust_tail_shrink(void)
 {
@@ -406,6 +407,139 @@ static void test_xdp_adjust_frags_tail_shrink_after_generic(void)
 	xdp_dummy__destroy(skel);
 }
 
+/*
+ * test_run allocates PAGE_SIZE frags, so a program may grow the last one up
+ * to the end of its page, also when ingress_ifindex picks the rxq info of
+ * another device.
+ */
+static void test_xdp_adjust_frags_tail_grow_ingress_ifindex(void)
+{
+	const char *file = "./test_xdp_adjust_tail_grow.bpf.o";
+	struct xdp_md ctx = { .data_end = 9000 };
+	struct netns_obj *ns = NULL;
+	struct bpf_program *prog;
+	struct bpf_object *obj;
+	__u8 *buf = NULL;
+	int err, ifindex;
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.ctx_in = &ctx,
+		.ctx_size_in = sizeof(ctx),
+	);
+
+	if (getpagesize() != 4096) {
+		test__skip();
+		return;
+	}
+
+	obj = bpf_object__open(file);
+	if (!ASSERT_OK_PTR(obj, "open"))
+		return;
+
+	prog = bpf_object__next_program(obj, NULL);
+	if (!ASSERT_OK(bpf_object__load(obj), "load"))
+		goto out;
+
+	ns = netns_new("xdp_adjust_tail_ns", true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto out;
+
+	SYS(out, "ip link add veth_xat0 type veth peer name veth_xat1");
+	ifindex = if_nametoindex("veth_xat0");
+	if (!ASSERT_GT(ifindex, 0, "veth ifindex"))
+		goto out;
+	ctx.ingress_ifindex = ifindex;
+
+	buf = malloc(16384);
+	if (!ASSERT_OK_PTR(buf, "alloc buf 16Kb"))
+		goto out;
+	memset(buf, 1, 16384);
+
+	/* Test case add 10 bytes to last frag */
+	topts.data_in = buf;
+	topts.data_out = buf;
+	topts.data_size_in = 9000;
+	topts.data_size_out = 16384;
+	err = bpf_prog_test_run_opts(bpf_program__fd(prog), &topts);
+
+	ASSERT_OK(err, "9Kb+10b");
+	ASSERT_EQ(topts.retval, XDP_TX, "9Kb+10b retval");
+	ASSERT_EQ(topts.data_size_out, 9000 + 10, "9Kb+10b size");
+out:
+	free(buf);
+	netns_free(ns);
+	bpf_object__close(obj);
+}
+
+/*
+ * The frags generic XDP gets from skb_pp_cow_data() can be parts of a
+ * page_pool page, so its rxq info must not allow growing them. test_run
+ * must not change that for lo.
+ */
+static void test_xdp_adjust_frags_tail_grow_lo_after_test_run(void)
+{
+	struct sockaddr_in addr = {
+		.sin_family = AF_INET,
+		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
+		.sin_port = htons(9),
+	};
+	struct test_xdp_adjust_frags_tail_grow_lo *skel;
+	struct netns_obj *ns = NULL;
+	static char payload[60000];
+	int fd = -1, lo, i, len, prog_fd;
+	LIBBPF_OPTS(bpf_test_run_opts, topts,
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.repeat = 1,
+	);
+
+	/* larger than the head generic XDP copies the packet into */
+	len = getpagesize() + 500;
+	if (len > (int)sizeof(payload)) {
+		test__skip();
+		return;
+	}
+
+	skel = test_xdp_adjust_frags_tail_grow_lo__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
+		return;
+
+	ns = netns_new("xdp_adjust_tail_ns", true);
+	if (!ASSERT_OK_PTR(ns, "netns_new"))
+		goto out;
+
+	SYS(out, "ip link set dev lo up");
+	lo = if_nametoindex("lo");
+	if (!ASSERT_GT(lo, 0, "lo ifindex"))
+		goto out;
+
+	/* a test run in this netns, before generic XDP sees a packet on lo */
+	prog_fd = bpf_program__fd(skel->progs.xdp_grow_frags);
+	ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "test_run");
+
+	if (!ASSERT_OK(bpf_xdp_attach(lo, prog_fd, XDP_FLAGS_SKB_MODE, NULL),
+		       "attach generic XDP"))
+		goto out;
+
+	/*
+	 * a linear skb with little headroom: generic XDP copies it into a
+	 * page_pool head page and a frag
+	 */
+	fd = socket(AF_INET, SOCK_DGRAM, 0);
+	if (ASSERT_GE(fd, 0, "socket"))
+		ASSERT_EQ(sendto(fd, payload, len, 0, (struct sockaddr *)&addr,
+				 sizeof(addr)), len, "sendto");
+	for (i = 0; i < 100 && skel->data->grow_ret == 1; i++)
+		usleep(10000);
+	bpf_xdp_detach(lo, XDP_FLAGS_SKB_MODE, NULL);
+
+	ASSERT_EQ(skel->data->grow_ret, -EOPNOTSUPP, "grow frag on lo");
+out:
+	if (fd >= 0)
+		close(fd);
+	netns_free(ns);
+	test_xdp_adjust_frags_tail_grow_lo__destroy(skel);
+}
+
 void test_xdp_adjust_tail(void)
 {
 	int page_size = getpagesize();
@@ -426,4 +560,8 @@ void test_xdp_adjust_tail(void)
 		else
 			test_xdp_adjust_frags_tail_grow_4k();
 	}
+	if (test__start_subtest("xdp_adjust_frags_tail_grow_ingress_ifindex"))
+		test_xdp_adjust_frags_tail_grow_ingress_ifindex();
+	if (test__start_subtest("xdp_adjust_frags_tail_grow_lo_after_test_run"))
+		test_xdp_adjust_frags_tail_grow_lo_after_test_run();
 }
diff --git a/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c b/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c
new file mode 100644
index 000000000000..207346b14ad8
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_xdp_adjust_frags_tail_grow_lo.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+/* 1 until the program saw a packet with frags */
+int grow_ret = 1;
+
+SEC("xdp.frags")
+int xdp_grow_frags(struct xdp_md *xdp)
+{
+	void *data_end = (void *)(long)xdp->data_end;
+	void *data = (void *)(long)xdp->data;
+
+	if (grow_ret != 1 || bpf_xdp_get_buff_len(xdp) <= data_end - data)
+		return XDP_PASS;
+
+	/*
+	 * More than the tailroom of any frag, so nothing is written: the
+	 * result only tells if frag_size of the rxq allows a grow at all.
+	 */
+	grow_ret = bpf_xdp_adjust_tail(xdp, 0x10000);
+	return XDP_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff()
  2026-11-24 10:40 ` [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff() Slawomir Stepien
@ 2026-11-24 11:32   ` bot+bpf-ci
  0 siblings, 0 replies; 10+ messages in thread
From: bot+bpf-ci @ 2026-11-24 11:32 UTC (permalink / raw)
  To: sst, ast, daniel, davem, kuba, hawk, john.fastabend, sdf
  Cc: bpf, netdev, maciej.fijalkowski, tianyi, tavip, toke,
	aleksander.lobakin, ilias.apalodimas, lorenzo, lorenzo.bianconi,
	magnus.karlsson, ihor.solodrai, edumazet, pabeni, andrew+netdev,
	horms, andrii, martin.lau, eddyz87, song, yonghong.song, jolsa,
	memxor, emil, shuah, linux-kselftest, syzbot+ff145014d6b0ce64a173,
	stable, sst, ast, andrii, daniel, martin.lau, eddyz87,
	yonghong.song, mason, ihor.solodrai

[-- Attachment #1: Type: text/plain, Size: 3631 bytes --]

> xdp: introduce xdp_convert_skb_to_buff()

TL;DR:
- Kernel-doc may wrongly say the head is page_pool backed only if copied; a
  future caller could free a page_pool page via page_frag_free()
- Commit message may omit a stable backport prerequisite (e6d90293fd16)

> diff --git a/include/net/xdp.h b/include/net/xdp.h
> --- a/include/net/xdp.h
> +++ b/include/net/xdp.h

[ ... ]

> +/**
> + * 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.
> + */

Is the statement "The head is page_pool backed only if the whole skb was
copied that way" accurate?

A driver can build an skb on a page_pool page and call
skb_mark_for_recycle() without any copy.  For example,
mvneta_swbm_build_skb() does:

    skb = napi_build_skb(xdp->data_hard_start, PAGE_SIZE);
    ...
    skb_mark_for_recycle(skb);
    skb_reserve(skb, xdp->data - xdp->data_hard_start);

and reserves MVNETA_SKB_HEADROOM, which is at least XDP_PACKET_HEADROOM.

netif_receive_generic_xdp() only calls netif_skb_check_for_xdp() when:

    if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
        skb_headroom(skb) < XDP_PACKET_HEADROOM) {

so a linear, uncloned skb like that reaches bpf_prog_run_generic_xdp() ->
xdp_convert_skb_to_buff() with a page_pool head that was never copied.

Nothing breaks today, since generic XDP never releases the head through the
mem type, and veth takes its own page references before consume_skb().

But a future caller that turns @xdp into an xdp_frame and relies on this
sentence to pick MEM_TYPE_PAGE_SHARED for an uncopied head would free a
page_pool page with page_frag_free().

Could the wording say that the head is only known to be page_pool backed
after the copy, or that it may be page_pool backed for other reasons, so
that callers check rather than assume?

[ ... ]

Separately, the commit message has this paragraph:

> 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.

Is the last sentence accurate for stable kernels between v6.4 and v6.9?

The mem type is written to the shared netdev rxq, and the write stays
there.  In those kernels bpf_prog_test_run_xdp() runs multi-buffer test
packets with alloc_page() frags on
__netif_get_rx_queue(loopback_dev, 0)->xdp_rxq, and reads that mem type
when the test program shrinks frags.

On a kernel that has this patch but not e6d90293fd16 ("bpf: test_run: Use a
private rxq info for XDP test runs"), those alloc_page() frags would be
freed as page_pool pages.

Should the commit message record e6d90293fd16 as a backport prerequisite,
the way the veth commit lists its prerequisites?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45379686209

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH v8 bpf 0/7] xdp: fix page_pool leaks
  2026-11-24 10:40 [PATCH v8 bpf 0/7] xdp: fix page_pool leaks Slawomir Stepien
                   ` (6 preceding siblings ...)
  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 ` Alexei Starovoitov
  7 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 21:04 UTC (permalink / raw)
  To: Slawomir Stepien, Daniel Borkmann, David S . Miller,
	Jakub Kicinski, Jesper Dangaard Brouer, John Fastabend,
	Stanislav Fomichev
  Cc: bpf, netdev, Maciej Fijalkowski, Tianyi Gao, Octavian Purdila,
	Toke Høiland-Jørgensen, Alexander Lobakin,
	Ilias Apalodimas, Lorenzo Bianconi, Lorenzo Bianconi,
	Magnus Karlsson, Ihor Solodrai, Eric Dumazet, Paolo Abeni,
	Andrew Lunn, Simon Horman, Andrii Nakryiko, Martin KaFai Lau,
	Eduard Zingerman, Song Liu, Yonghong Song, Jiri Olsa,
	Kumar Kartikeya Dwivedi, Emil Tsalapatis, Shuah Khan,
	linux-kselftest

On Tue, Nov 24, 2026 at 02:40 AM Slawomir Stepien <sst@poczta.fm> wrote:
> Patch 1 prepares bpf_prog_test_run_xdp() for patch 3. Generic XDP runs
> on the rxq info of the device queue, which is shared with test_run; with
> patch 3 alone, any packet through generic XDP on lo makes later
> multi-buffer test runs free their frags as page_pool pages. Restoring

The store into the shared rxq is the problem.
Patch 3 does it for every packet on every CPU, for linear skbs too.
The comment in include/net/xdp.h says that rxq must not be updated
during NAPI poll.
On Dudu's patch I proposed to put rxq on stack:
https://lore.kernel.org/bpf/DMAP8LNBJRPN.2THUHLKURP25G@gmail.com/
It works for veth too.
An skb leaves veth as a MEM_TYPE_PAGE_SHARED frame, like today.
No need for xdp_convert_skb_to_buff(), veth_xdp_get_pp(), pp_cow,
rq->xdp_mem and patch 6.

Patches 6 and 7 never free a page_pool frag. Jiayuan's test does:
https://lore.kernel.org/bpf/20260911135711.109338-3-jiayuan.chen@linux.dev/

Below replaces patches 1-5.

diff --git a/drivers/net/veth.c b/drivers/net/veth.c
index faa837a..8bb015c 100644
--- a/drivers/net/veth.c
+++ b/drivers/net/veth.c
@@ -63,7 +63,6 @@ struct veth_rq {
 	struct napi_struct __rcu *napi; /* points to xdp_napi when the latter is initialized */
 	struct net_device	*dev;
 	struct bpf_prog __rcu	*xdp_prog;
-	struct xdp_mem_info	xdp_mem;
 	struct veth_rq_stats	stats;
 	bool			rx_notify_masked;
 	struct ptr_ring		xdp_ring;
@@ -642,6 +641,7 @@ static int veth_xdp_tx(struct veth_rq *rq, struct xdp_buff *xdp,
 }
 
 static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
+					  struct xdp_rxq_info *rxq,
 					  struct xdp_frame *frame,
 					  struct veth_xdp_tx_bq *bq,
 					  struct veth_stats *stats)
@@ -657,7 +657,8 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 		u32 act;
 
 		xdp_convert_frame_to_buff(frame, xdp);
-		xdp->rxq = &rq->xdp_rxq;
+		rxq->mem.type = frame->mem_type;
+		xdp->rxq = rxq;
 		vxbuf.skb = NULL;
 
 		act = bpf_prog_run_xdp(xdp_prog, xdp);
@@ -669,7 +670,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 			break;
 		case XDP_TX:
 			orig_frame = *frame;
-			xdp->rxq->mem.type = frame->mem_type;
 			if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 				trace_xdp_exception(rq->dev, xdp_prog, act);
 				frame = &orig_frame;
@@ -681,7 +681,6 @@ static struct xdp_frame *veth_xdp_rcv_one(struct veth_rq *rq,
 			goto xdp_xmit;
 		case XDP_REDIRECT:
 			orig_frame = *frame;
-			xdp->rxq->mem.type = frame->mem_type;
 			if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 				frame = &orig_frame;
 				stats->rx_drops++;
@@ -746,6 +745,9 @@ static void veth_xdp_get(struct xdp_buff *xdp)
 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
 	int i;
 
+	/* the buff holds plain page references from here on */
+	xdp->rxq->mem.type = MEM_TYPE_PAGE_SHARED;
+
 	get_page(virt_to_page(xdp->data));
 	if (likely(!xdp_buff_has_frags(xdp)))
 		return;
@@ -755,6 +757,7 @@ static void veth_xdp_get(struct xdp_buff *xdp)
 }
 
 static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
+					struct xdp_rxq_info *rxq,
 					struct xdp_buff *xdp,
 					struct sk_buff **pskb)
 {
@@ -773,7 +776,9 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,
 	/* 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));
-	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
+	/* the prog can free only frags and they came from skb_pp_cow_data() */
+	rxq->mem.type = MEM_TYPE_PAGE_POOL;
+	xdp_init_buff(xdp, frame_sz, rxq);
 	xdp_prepare_buff(xdp, skb->head, skb_headroom(skb),
 			 skb_headlen(skb), true);
 
@@ -794,6 +799,7 @@ drop:
 }
 
 static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
+					struct xdp_rxq_info *rxq,
 					struct sk_buff *skb,
 					struct veth_xdp_tx_bq *bq,
 					struct veth_stats *stats)
@@ -815,7 +821,7 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	}
 
 	__skb_push(skb, skb->data - skb_mac_header(skb));
-	if (veth_convert_skb_to_xdp_buff(rq, xdp, &skb))
+	if (veth_convert_skb_to_xdp_buff(rq, rxq, xdp, &skb))
 		goto drop;
 	vxbuf.skb = skb;
 
@@ -830,7 +836,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	case XDP_TX:
 		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp->rxq->mem = rq->xdp_mem;
 		if (unlikely(veth_xdp_tx(rq, xdp, bq) < 0)) {
 			trace_xdp_exception(rq->dev, xdp_prog, act);
 			stats->rx_drops++;
@@ -842,7 +847,6 @@ static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq,
 	case XDP_REDIRECT:
 		veth_xdp_get(xdp);
 		consume_skb(skb);
-		xdp->rxq->mem = rq->xdp_mem;
 		if (xdp_do_redirect(rq->dev, xdp, xdp_prog)) {
 			stats->rx_drops++;
 			goto err_xdp;
@@ -914,6 +918,11 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
 			struct veth_xdp_tx_bq *bq,
 			struct veth_stats *stats)
 {
+	struct veth_priv *priv = netdev_priv(rq->dev);
+	struct xdp_rxq_info rxq = {
+		.dev = rq->dev,
+		.queue_index = rq - priv->rq,
+	};
 	int i, done = 0, n_xdpf = 0;
 	void *xdpf[VETH_XDP_BATCH];
 
@@ -928,7 +937,7 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
 			struct xdp_frame *frame = veth_ptr_to_xdp(ptr);
 
 			stats->xdp_bytes += xdp_get_frame_len(frame);
-			frame = veth_xdp_rcv_one(rq, frame, bq, stats);
+			frame = veth_xdp_rcv_one(rq, &rxq, frame, bq, stats);
 			if (frame) {
 				/* XDP_PASS */
 				xdpf[n_xdpf++] = frame;
@@ -943,7 +952,7 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,
 			struct sk_buff *skb = ptr;
 
 			stats->xdp_bytes += skb->len;
-			skb = veth_xdp_rcv_skb(rq, skb, bq, stats);
+			skb = veth_xdp_rcv_skb(rq, &rxq, skb, bq, stats);
 			if (skb) {
 				if (skb_shared(skb) || skb_unclone(skb, GFP_ATOMIC))
 					netif_receive_skb(skb);
@@ -1149,9 +1158,6 @@ static int veth_enable_xdp_range(struct net_device *dev, int start, int end,
 						 NULL);
 		if (err < 0)
 			goto err_reg_mem;
-
-		/* Save original mem info as it can be overwritten */
-		rq->xdp_mem = rq->xdp_rxq.mem;
 	}
 	return 0;
 
@@ -1180,7 +1186,6 @@ static void veth_disable_xdp_range(struct net_device *dev, int start, int end,
 	for (i = start; i < end; i++) {
 		struct veth_rq *rq = &priv->rq[i];
 
-		rq->xdp_rxq.mem = rq->xdp_mem;
 		xdp_rxq_info_unreg(&rq->xdp_rxq);
 
 		if (delete_napi)
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index d6c9dc6..1d89b6f 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -1349,6 +1349,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	struct netdev_rx_queue *rxqueue;
 	struct skb_shared_info *sinfo;
 	struct xdp_buff xdp = {};
+	struct xdp_rxq_info rxq;
 	int i, ret = -EINVAL;
 	struct xdp_md *ctx;
 	void *data;
@@ -1417,8 +1418,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	}
 
 	rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
-	rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
-	xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
+	xdp_init_buff(&xdp, PAGE_SIZE, &rxqueue->xdp_rxq);
 	xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
 	sinfo = xdp_get_shared_info_from_buff(&xdp);
 
@@ -1426,6 +1426,11 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 	if (ret)
 		goto free_data;
 
+	/* generic XDP uses the same rxq and its frags can be smaller */
+	rxq = *xdp.rxq;
+	rxq.frag_size = PAGE_SIZE;
+	xdp.rxq = &rxq;
+
 	size = linear_sz;
 	if (unlikely(kattr->test.data_size_in > size)) {
 		void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
diff --git a/net/core/dev.c b/net/core/dev.c
index 6140fd9..fe9f222 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -5684,6 +5684,7 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
 	void *orig_data, *orig_data_end, *hard_start;
 	struct netdev_rx_queue *rxqueue;
 	bool orig_bcast, orig_host;
+	struct xdp_rxq_info rxq;
 	u32 mac_len, frame_sz;
 	__be16 orig_eth_type;
 	struct ethhdr *eth;
@@ -5707,6 +5708,13 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
 	if (skb_is_nonlinear(skb)) {
 		skb_shinfo(skb)->xdp_frags_size = skb->data_len;
 		xdp_buff_set_frags_flag(xdp);
+		/*
+		 * The frags came from skb_pp_cow_data(). xdp_rxq is shared
+		 * with other CPUs and with BPF_PROG_TEST_RUN, so use a copy.
+		 */
+		rxq = rxqueue->xdp_rxq;
+		rxq.mem.type = MEM_TYPE_PAGE_POOL;
+		xdp->rxq = &rxq;
 	} else {
 		xdp_buff_clear_frags_flag(xdp);
 	}
@@ -5719,6 +5727,7 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
 	orig_eth_type = eth->h_proto;
 
 	act = bpf_prog_run_xdp(xdp_prog, xdp);
+	xdp->rxq = &rxqueue->xdp_rxq;
 
 	/* check if bpf_xdp_adjust_head was used */
 	off = xdp->data - orig_data;

pw-bot: cr

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-24 21:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v8 bpf 3/7] xdp: introduce xdp_convert_skb_to_buff() Slawomir Stepien
2026-11-24 11:32   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox