Skip to contentSkip to conversation

xdp: fix page_pool leaks

PATCH Superseded Slawomir Stepien 4 participants 14 messages Nov 19 – Nov 20

Subsystems (7)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover xdp: fix page_pool leaks

v1:
https://lore.kernel.org/bpf/[email protected]/
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/[email protected]/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/[email protected]/
v3->v4:
- include skb->head when calculating packet length (Toke, lkp)

v4:
https://lore.kernel.org/bpf/[email protected]/
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/[email protected]/
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/[email protected]/
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

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.

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 2 and 4 and my changes are noted in their commit messages; patches 1, 3 and 5 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 4 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 3 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 2. Generic XDP runs on the rxq info of the device queue, which is shared with test_run; with patch 2 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 2, but it has to be backported together with patch 2.

Performance (QEMU/KVM, 8 vCPUs pinned 1:1 to host cores, the same kernel config without debug options for both kernels; base is bpf/master 4b4eb7f44ee0):

pktgen sends skbs into veth v0 and the XDP program runs on its peer v1, on the skb path (native veth XDP, or xdpgeneric where noted). XDP_TX sends the packets back to v0, devmap redirects them to a second veth pair and cpumap to the network stack on another CPU. The tables show packets/s that reach the destination (for XDP_DROP: packets the program saw), the median of 8 runs of 8 s over 4 boots per kernel. Changes of up to about 2 % are noise in this setup: a kernel rebuilt with an unrelated one-line change moved single results by up to 3 %.

Why it is faster: on XDP_TX and XDP_REDIRECT, veth turns the skb into an xdp_frame. On base, it takes plain page references for that (veth_xdp_get()) and the frame is released as MEM_TYPE_PAGE_SHARED, so v1's page_pool never gets its pages back: the pool hands every page of an 8000 B packet to the page allocator and allocates a new one, 4.2-6.1 M pages/s in these runs. With the series the frame is released as MEM_TYPE_PAGE_POOL, the pages go back into the pool's ring and are reused, and the pool no longer releases any page. One page holds many 64 B packets, so at 64 B base released only 20-37 k pages/s and the gain is small.

One sender, softirq NAPI:

                              64 B                      8000 B (3 frags)
  XDP_TX                      4.40 -> 4.39 Mpps (0%)    2.20 -> 2.37 (+7%)
  REDIRECT devmap -> veth     4.18 -> 4.22 (+1%)        2.14 -> 2.29 (+7%)
  REDIRECT cpumap             4.60 -> 4.61 (0%)         1.97 -> 2.12 (+8%)
  XDP_DROP (native veth)      5.74 -> 5.76 (0%)         2.77 -> 2.80 (+1%)
  XDP_DROP (xdpgeneric)       5.70 -> 5.75 (+1%)        2.97 -> 2.95 (-1%)

What it shows: the cost of one packet when nothing is batched. v1's NAPI runs on pktgen's CPU and each poll sees a single packet.

Why: 8000 B XDP_TX and the redirects are 7-8 % faster from page recycling; the per-packet work of one poll per packet and the copy of 8000 B into the page_pool still dominate. XDP_DROP does not change, as dropped pages were recycled on base too, and 64 B does not change.

Four senders, softirq NAPI:

                              64 B                      8000 B (3 frags)
  XDP_TX                     12.07 -> 12.48 Mpps (+3%)  2.65 -> 3.71 (+40%)
  REDIRECT devmap -> veth    11.69 -> 12.17 (+4%)       2.65 -> 3.66 (+38%)
  REDIRECT cpumap             5.31 -> 5.36 (+1%)        2.56 -> 3.69 (+44%)
  XDP_DROP (native veth)     14.85 -> 14.62 (-2%)       4.31 -> 4.31 (0%)

XDP_DROP (xdpgeneric) 22.32 -> 21.97 (-2%) 11.63 -> 11.42 (-2%)

What it shows: a busy veth. Four CPUs send at once, so v1 and the destination process several packets per NAPI poll, as under real load.

Why: at 8000 B the gain grows to 38-44 %: on base, the pages went through the page allocator on four CPUs at once (5.4-5.8 M pages/s). At 64 B, XDP_TX and devmap gain 3-4 % as the pages of the frames now return to the pool's ring as well. cpumap is limited by the network stack on the target CPU, about 5.3 Mpps on both kernels. The XDP_DROP differences are within the noise: a profile of 64 B generic XDP_DROP shows the extra cycles in pktgen and the senders' locks, not in code that the series changes.

Two senders, threaded NAPI (v1 on its own CPU, the bottleneck):

                              64 B                      8000 B (3 frags)
  XDP_TX                     12.76 -> 12.06 Mpps (-5%)  2.75 -> 3.91 (+43%)
  REDIRECT devmap -> veth    12.95 -> 13.58 (+5%)       2.75 -> 3.87 (+41%)
  REDIRECT cpumap             5.33 -> 5.47 (+3%)        2.70 -> 3.85 (+42%)
  XDP_DROP (native veth)     11.39 -> 11.27 (-1%)       4.50 -> 4.46 (-1%)
  XDP_DROP (xdpgeneric)      11.41 -> 11.21 (-2%)       5.93 -> 5.88 (-1%)

What it shows: the cost on v1 itself. v1's NAPI thread has a CPU of its own and runs at 100 %; v0 and the devmap target have NAPI threads on their own CPUs too.

Why: at 8000 B, XDP_TX and both redirects are 41-43 % faster from page recycling on the bottleneck CPU. At 64 B, devmap and cpumap gain 3-5 %. XDP_TX at 64 B loses 5 %, but not from more work per packet: a profile shows the same cycles per packet on v1's CPU (322 vs 320), and fewer on the CPU of v0's NAPI thread, which frees the frames (152 -> 138
cycles: page_pool recycling instead of page_frag_free() and page allocator frees). That thread then empties its ring sooner, sleeps more often and has to be woken by an IPI more often (idle and wake-up 75 -> 118 cycles per packet). In a VM each wake-up also costs exits that the guest does not see; this was not measured on bare metal.

With one sender and threaded NAPI, 64 B changes by -2 % to +3 %, and at 8000 B XDP_TX gains 42 %, devmap 37 % and cpumap 38 %.

Testing (x86_64, QEMU via virtme-ng, KASAN + DEBUG_VM + DEBUG_NET; the whole series on bpf/master 4b4eb7f44ee0, and before on 05441d697f7f, where the reproducers, kprobe path checks, devmap test and selftests below gave the same results; patches 2-4 also on fb7bd3a2aad7; the runs without the series on bpf/master c8a8a68d3ab4 and 05441d697f7f):
- test_run after generic XDP: send one packet through generic XDP on lo, then run xdp_adjust_tail; with patches 2-4 only:
"WARNING: include/net/page_pool/helpers.h:305 at __xdp_return" from bpf_xdp_adjust_tail() under bpf_prog_test_run_xdp(), as in BPF CI; the new xdp_adjust_frags_tail_shrink_after_generic subtest hits the same warning; with the whole series no splat
- 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 on 4b4eb7f44ee0 without the series, and rhash_timer is flaky on both kernels (it failed in 10 of 20 runs without the series and in 14 of 20 with it)
- 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
- native veth XDP_TX of multi-buffer packets, with and without trimming frags first (200 large + 200 small pings each), then veth teardown and a 75s wait for page_pool "stalled pool shutdown" or negative inflight warnings: none
- BPF selftests on bpf/master with tools/testing/selftests/bpf/config:
./test_progs -t ns_xsk,xdp: all 34 tests passed, no splats, with the series (206 subtests, with the new one from patch 5) and without it
(205)
- the standalone AF_XDP suite (test_xsk.sh) also runs the tests that test_progs skips as flaky; XDP_ADJUST_TAIL_SHRINK_MULTI_BUFF reports PASS either way, but without the series dmesg shows 60 "page_pool leak" splats from generic XDP (bpf_xdp_adjust_tail -> __xdp_return -> page_frag_free on pages allocated by skb_pp_cow_data()); 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 (IPv6 off, so no skb touches the peer's rxq first); with patches 2+4 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, kprobes show all __xdp_return() calls with MEM_TYPE_PAGE_POOL, no splat, also with IPv6 on so that skbs and frames interleave on the peer's queue; bpf/master alone passes as well since veth only sends MEM_TYPE_PAGE_SHARED frames there
- remaining veth paths, each confirmed with kprobes: skbs that skip the CoW (frames passed up by one veth and forwarded back into it: 1229 of 1260 runs without skb_pp_cow_data(), heads from the page_pool), XDP_REDIRECT failing on the skb path (40 xdp_return_buff()) and on the frame path (40 xdp_return_frame()), XDP_PASS of 20 frames after their frags were trimmed; no splat, and none of the page_pool shutdown or inflight warnings 75s after teardown
- devmap egress after patch 4: veth redirects copied skbs through a devmap whose egress program trims all frags; with the series 20/20 trimmed, all __xdp_return() calls with MEM_TYPE_PAGE_POOL, no splat, nothing 75s after teardown; with b3ca872947ef reverted on top of the
series: "page_pool leak" from page_frag_free() under dev_map_bpf_prog_run(), VM_BUG_ON_PAGE() and a panic, hence the stable prerequisite in patch 4
- W=1 builds of the touched files with LLVM=1 and with DEBUG_NET=n: no warnings
- XDP_ADJUST_TAIL_GROW fails in DRV mode with and without the series, so it is unrelated
- 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 and v6 review threads; the assistant wrote the code changes, all of patches 1, 3 and 5, 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 (3):
bpf: test_run: Use a private rxq info for XDP test runs
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

 drivers/net/veth.c                            | 56 ++++++++++++-------
 include/net/xdp.h                             | 36 ++++++++++++
 net/bpf/test_run.c                            |  9 +++
 net/core/dev.c                                | 25 ++-------
 .../bpf/prog_tests/xdp_adjust_tail.c          | 51 +++++++++++++++++
 5 files changed, 137 insertions(+), 40 deletions(-)

base-commit: 4b4eb7f44ee05754339d07e9df649809f45d14c5

Signature · 1 line
2.56.0
#1 lore raw

PATCH v7 1/5 bpf: test_run: Use a private rxq info for XDP test runs

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.

The next patch 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.

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

---
 net/bpf/test_run.c | 9 +++++++++
 1 file changed, 9 insertions(+)
net/bpf/test_run.c +9 -0
index d6c9dc62182e..676107758678 100644
@@ -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,14 @@ 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);
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH v7 bpf 1/5] bpf: test_run: Use a private rxq info for XDP test runs

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] bpf: shared lo xdp_rxq.frag_size mutation in bpf_prog_test_run_xdp() - [Low] bpf: non-standard…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] bpf: shared lo xdp_rxq.frag_size mutation in bpf_prog_test_run_xdp()
- [Low] bpf: non-standard multi-line comment style in bpf_prog_test_run_xdp()
- [Low] bpf: missing Fixes: trailer on commit marked for stable backport --

commit 56f6fcd36def90ece50bcb79a3714bded7c3b347
Author: Slawomir Stepien <[email protected]>

Quoted 35 lines

bpf: test_run: Use a private rxq info for XDP test runs

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.

The next patch 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.

Quoted 6 lines

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

[Severity: Low]
Since this commit is tagged for stable backporting and fixes freeing of alloc_page() fragments in bpf_prog_test_run_xdp(), should it include a
Fixes: tag?

Fixes 1c1949982524 ("bpf: introduce frags support to bpf_prog_test_run_xdp()")
Quoted 13 lines
net/bpf/test_run.c +1 -0
index d6c9dc62182ee..6761077586780 100644
@@ -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;

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1433

Quoted 7 lines
net/bpf/test_run.c +4 -0
@@ -1426,6 +1427,14 @@ 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.
+	 */

[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of a multi-line comment on its own line, with the comment text starting on the next line.

net/bpf/test_run.c int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr, · line 1436

Quoted 3 lines
net/bpf/test_run.c +3 -0
+	rxq = *xdp.rxq;
+	rxq.mem = (struct xdp_mem_info){ .type = MEM_TYPE_PAGE_SHARED };
+	xdp.rxq = &rxq;

[Severity: High]
Should frag_size = PAGE_SIZE also be set on the private rxq copy here instead of mutating the shared loopback queue's rxqueue->xdp_rxq.frag_size earlier in bpf_prog_test_run_xdp()?

net/bpf/test_run.c:bpf_prog_test_run_xdp() {
	...
	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_prepare_buff(&xdp, data, headroom, linear_sz, true);
	sinfo = xdp_get_shared_info_from_buff(&xdp);

	ret = xdp_convert_md_to_buff(ctx, &xdp);
	...
}

When ctx->ingress_ifindex points to a device queue other than loopback queue 0, xdp_convert_md_to_buff() switches xdp.rxq to that queue's xdp_rxq_info, so rxq = *xdp.rxq inherits the target queue's frag_size rather than PAGE_SIZE even though test_run allocates PAGE_SIZE pages via alloc_page() below. In bpf_xdp_frags_increase_tail(), a target queue with frag_size == 0 (such as veth or tun) or frag_size > PAGE_SIZE causes tail expansion to fail with -EOPNOTSUPP, while 0 < frag_size < PAGE_SIZE can compute a negative tailroom and trigger WARN_ON_ONCE(tailroom < 0).

In addition, writing PAGE_SIZE into the shared loopback queue 0 rxqueue->xdp_rxq.frag_size leaves lo with rxq->frag_size == PAGE_SIZE for later generic XDP runs; if a non-linear skb passes through generic XDP on lo, skb_pp_cow_data() can allocate sub-page fragments from a shared page_pool page while bpf_xdp_frags_increase_tail() lets bpf_xdp_adjust_tail() grow a sub-page fragment up to PAGE_SIZE and write out of bounds into adjacent fragments.

Passing PAGE_SIZE directly to xdp_init_buff(&xdp, PAGE_SIZE, &rxqueue->xdp_rxq) and setting rxq.frag_size = PAGE_SIZE on the private rxq copy avoids mutating the shared loopback queue and keeps rxq.frag_size consistent with the allocated fragment pages.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v7 2/5 xdp: introduce xdp_convert_skb_to_buff()

In reply to Slawomir Stepien

From: Maciej Fijalkowski <[email protected]>

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 and causes a page leak.

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.

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.

This problem was triggered by syzbot as well as by the AF_XDP test suite.

[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)]

---
 include/net/xdp.h | 36 ++++++++++++++++++++++++++++++++++++
 net/core/dev.c    | 25 ++++---------------------
 2 files changed, 40 insertions(+), 21 deletions(-)
include/net/xdp.h +36 -0
index 07231adfb5f8..28bd78dbef20 100644
@@ -389,6 +389,42 @@ 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
+ *
+ * The memory type set on @xdp_rxq is only used to release fragments that
+ * the XDP program consumes; the head can not be freed from under the skb.
+ * A non-linear @skb is expected to have gone through skb_pp_cow_data(), so
+ * all of its fragments are page_pool backed.
+ */
+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)
net/core/dev.c +4 -21
index 3ac700d37e59..4eeeb0182a6a 100644
@@ -5667,35 +5667,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;
Signature · 1 line
2.56.0
#4 lore raw

Re: [PATCH v7 bpf 2/5] xdp: introduce xdp_convert_skb_to_buff()

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] xdp: malformed Signed-off-by trailers with trailing comments --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] xdp: malformed Signed-off-by trailers with trailing comments --

commit b52c8a4a1dfa14f2b9eeae449bd2e813e860af3a
Author: Maciej Fijalkowski <[email protected]>

xdp: introduce xdp_convert_skb_to_buff()

This commit factors out xdp_buff initialization from bpf_prog_run_generic_xdp() into xdp_convert_skb_to_buff() and sets MEM_TYPE_PAGE_POOL on the attached xdp_rxq_info so fragments released by bpf_xdp_adjust_tail() are recycled properly.

[Severity: High]
In the commit message trailer block, the Signed-off-by trailers for Octavian Purdila and Maciej Fijalkowski have trailing hash comments appended after the closing angle bracket of the email address. Per Documentation/process/submitting-patches.rst, Signed-off-by lines must end at the closing > of the email address; any contributor attribution notes should be moved into a separate bracketed [name: description] block.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH v7 bpf 2/5] xdp: introduce xdp_convert_skb_to_buff()

In reply to Slawomir Stepien
TL;DR: - Commit message calls the bug a page leak; it may be a use-after-free of page_pool pages - Stable Cc 6.4.x predates the Fixes: tag (v6.9) - kdoc may overstate mem type usage (nit)
Quoted 1 line

xdp: introduce xdp_convert_skb_to_buff()

TL;DR:
- Commit message calls the bug a page leak; it may be a use-after-free of page_pool pages
- Stable Cc 6.4.x predates the Fixes: tag (v6.9)
- kdoc may overstate mem type usage (nit)

Quoted 3 lines

Then when bpf_xdp_adjust_tail() calls
__xdp_return(), it acts incorrectly due to mem type not being set to MEM_TYPE_PAGE_POOL and causes a page leak.

Is "causes a page leak" the full impact?

The mechanism is bpf_xdp_adjust_tail() -> bpf_xdp_frags_shrink_tail() -> bpf_xdp_shrink_data() -> __xdp_return(netmem, MEM_TYPE_PAGE_SHARED) -> page_frag_free(). That ends in put_page_testzero() on a page_pool page.

With page checks enabled this is reported as "page_pool leak" and the page is not freed. Without them, page_pool keeps _refcount at 1 and counts fragment users in pp_ref_count, and skb_pp_cow_data() allocates fragment-sized chunks. A single page_frag_free() can therefore hand back to the buddy allocator a page that other skbs or pool->frag_page still use, which looks like a use-after-free rather than only a leak.

Could the commit message describe that?

Quoted 1 line
Cc <[email protected]> # 6.4.x

The Fixes: tag points at e6d5dbdd20aa, which first landed in v6.9. Before that, generic XDP linearized every skb and never set the frags flag, so this bug cannot occur on 6.4 through 6.8.

The later veth patch in this series (1d3541dbf827) appears to be why 6.4.x is listed, since it uses this helper and needs it as a backport prerequisite. Could the commit message either say that, or align the stable range with the Fixes: tag?

Quoted 3 lines
include/net/xdp.h +0 -0

[ ... ]

Quoted 11 lines

+/**
+ * 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
+ *
+ * The memory type set on @xdp_rxq is only used to release fragments that
+ * the XDP program consumes; the head can not be freed from under the skb.
+ * A non-linear @skb is expected to have gone through skb_pp_cow_data(), so
+ * all of its fragments are page_pool backed.
+ */

This isn't a bug, but is the "only used to release fragments" statement a property of this helper, or of its current caller?

It holds for generic XDP. However, the veth patch later in this series (1d3541dbf827) converts the xdp_buff to an xdp_frame on XDP_TX and XDP_REDIRECT, where the head is released through the same mem type, and it has to overwrite rq->xdp_rxq.mem after calling this helper.

The kdoc also does not mention that the mem type stays set on the caller's possibly shared @xdp_rxq after the call. Should the kdoc say that these properties depend on the caller?

---
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/44535627869

#6 lore raw

Re: [PATCH v7 bpf 2/5] xdp: introduce xdp_convert_skb_to_buff()

Quoted 18 lines · On lis 19, 2026 14:11, [email protected] wrote:
Quoted 1 line

xdp: introduce xdp_convert_skb_to_buff()

TL;DR:
- Commit message calls the bug a page leak; it may be a use-after-free of page_pool pages
- Stable Cc 6.4.x predates the Fixes: tag (v6.9)
- kdoc may overstate mem type usage (nit)

Quoted 3 lines

Then when bpf_xdp_adjust_tail() calls
__xdp_return(), it acts incorrectly due to mem type not being set to MEM_TYPE_PAGE_POOL and causes a page leak.

Is "causes a page leak" the full impact?

The mechanism is bpf_xdp_adjust_tail() -> bpf_xdp_frags_shrink_tail() -> bpf_xdp_shrink_data() -> __xdp_return(netmem, MEM_TYPE_PAGE_SHARED) -> page_frag_free(). That ends in put_page_testzero() on a page_pool page.

Quoted 8 lines

With page checks enabled this is reported as "page_pool leak" and the page is not freed. Without them, page_pool keeps _refcount at 1 and counts fragment users in pp_ref_count, and skb_pp_cow_data() allocates fragment-sized chunks. A single page_frag_free() can therefore hand back to the buddy allocator a page that other skbs or pool->frag_page still use, which looks like a use-after-free rather than only a leak.

Could the commit message describe that?

Yes, I will add that do commit message in v8.

Quoted 10 lines
Quoted 1 line
Cc <[email protected]> # 6.4.x

The Fixes: tag points at e6d5dbdd20aa, which first landed in v6.9. Before that, generic XDP linearized every skb and never set the frags flag, so this bug cannot occur on 6.4 through 6.8.

The later veth patch in this series (1d3541dbf827) appears to be why 6.4.x is listed, since it uses this helper and needs it as a backport prerequisite. Could the commit message either say that, or align the stable range with the Fixes: tag?

Sure, v8 will describe that in v8.

Quoted 18 lines
Quoted 3 lines
include/net/xdp.h +0 -0

[ ... ]

Quoted 11 lines

+/**
+ * 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
+ *
+ * The memory type set on @xdp_rxq is only used to release fragments that
+ * the XDP program consumes; the head can not be freed from under the skb.
+ * A non-linear @skb is expected to have gone through skb_pp_cow_data(), so
+ * all of its fragments are page_pool backed.
+ */

Quoted 7 lines

This isn't a bug, but is the "only used to release fragments" statement a property of this helper, or of its current caller?

It holds for generic XDP. However, the veth patch later in this series (1d3541dbf827) converts the xdp_buff to an xdp_frame on XDP_TX and XDP_REDIRECT, where the head is released through the same mem type, and it has to overwrite rq->xdp_rxq.mem after calling this helper.

True, will add that info to commit message in v8.

Quoted 3 lines

The kdoc also does not mention that the mem type stays set on the caller's possibly shared @xdp_rxq after the call. Should the kdoc say that these properties depend on the caller?

Yes I agree. Will add in v8.

Signature · 1 line
Slawomir Stepien
#7 lore raw

PATCH v7 3/5 veth: use the frame mem type when running XDP on an xdp_frame

In reply to Slawomir Stepien

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")
---
 drivers/net/veth.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/net/veth.c +2 -2
index 3eca92d3dc60..afae2c6533b9 100644
@@ -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++;
Signature · 1 line
2.56.0
#8 lore raw

Re: [PATCH v7 bpf 3/5] veth: use the frame mem type when running XDP on an xdp_frame

TL;DR: Commit message only: the Fixes: tag (718a18a0c8a6) looks wrong, since it only touches the skb path; the review suggests naming the commit that introduced the stale rxq mem type problem.
Quoted 1 line

veth: use the frame mem type when running XDP on an xdp_frame

TL;DR: Commit message only: the Fixes: tag (718a18a0c8a6) looks wrong, since it only touches the skb path; the review suggests naming the commit that introduced the stale rxq mem type problem.

Quoted 10 lines

veth: use the frame mem type when running XDP on an xdp_frame

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.

[ ... ]

Quoted 3 lines

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.

[ ... ]

Quoted 4 lines
Fixes 718a18a0c8a6 ("veth: Rework veth_xdp_rcv_skb in order to accept non-linear skb")

Is this Fixes: tag pointing at the right commit?

718a18a0c8a6 only reworks the skb path (veth_xdp_rcv_skb() and veth_convert_skb_to_xdp_buff()), and does not touch veth_xdp_rcv_one(), which is the only function this patch changes.

The stale rxq mem type on the xdp_frame path comes from the XDP_TX/XDP_REDIRECT-only "xdp->rxq->mem = frame->mem" assignments, which predate that commit and were added by d1396004dd86 ("veth: Add XDP TX and REDIRECT").

The stale type only becomes harmful once frag release uses the rxq mem type, which happened in bf25146a5595 for bpf_xdp_adjust_tail() and in 4dce1a0d7cf3 for bpf_xdp_pull_data().

Could the tag be changed to name the commit that actually introduced the problem?

---
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/44535627869

#9 lore raw

Re: [PATCH v7 bpf 3/5] veth: use the frame mem type when running XDP on an xdp_frame

Quoted 5 lines · On Thu, 19 Nov 2026 13:54:58 +0000, [email protected] wrote:

Is this Fixes: tag pointing at the right commit?

718a18a0c8a6 only reworks the skb path (veth_xdp_rcv_skb() and veth_convert_skb_to_xdp_buff()), and does not touch veth_xdp_rcv_one(), which is the only function this patch changes.

I think the AI is wrong here. Before 718a18a0c8a6, veth copied a non-linear skb into one linear page, so no frame with frags could reach veth_xdp_rcv_one(). Either way, 718a18a0c8a6 and bf25146a5595 are both in v5.18-rc1, so the choice makes no difference for stable.

Cheers,
Tianyi

#10 lore raw

Re: [PATCH v7 bpf 3/5] veth: use the frame mem type when running XDP on an xdp_frame

Quoted 11 lines · Tianyi Gao
Quoted 5 lines · On Thu, 19 Nov 2026 13:54:58 +0000, [email protected] wrote:

Is this Fixes: tag pointing at the right commit?

718a18a0c8a6 only reworks the skb path (veth_xdp_rcv_skb() and veth_convert_skb_to_xdp_buff()), and does not touch veth_xdp_rcv_one(), which is the only function this patch changes.

I think the AI is wrong here. Before 718a18a0c8a6, veth copied a non-linear skb into one linear page, so no frame with frags could reach veth_xdp_rcv_one(). Either way, 718a18a0c8a6 and bf25146a5595 are both in v5.18-rc1, so the choice makes no difference for stable.

Thanks! I will keep 718a18a0c8a6.

Signature · 1 line
Slawomir Stepien
#11 lore raw

PATCH v7 4/5 veth: update mem type in xdp_buff

In reply to Slawomir Stepien

From: Maciej Fijalkowski <[email protected]>

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")
Cc <[email protected]> # 6.4.x: b3ca872947ef: bpf: Use the frame mem type in devmap egress XDP
Cc <[email protected]> # 6.4.x

[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)]

---
 drivers/net/veth.c | 52 +++++++++++++++++++++++++++++++---------------
 1 file changed, 35 insertions(+), 17 deletions(-)
drivers/net/veth.c +35 -17
index afae2c6533b9..e159f0e00ddb 100644
@@ -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,17 @@ 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 +850,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 +861,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;
Signature · 1 line
2.56.0
#12 lore raw

PATCH v7 5/5 selftests/bpf: Shrink test_run frags after generic XDP ran on lo

In reply to 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).

---
 .../bpf/prog_tests/xdp_adjust_tail.c          | 51 +++++++++++++++++++
 1 file changed, 51 insertions(+)
tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c +51 -0
index 43264347e7d7..c0d54cf421d8 100644
@@ -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,52 @@ 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;
+
+	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;
+
+	if (!ASSERT_OK(bpf_xdp_attach(lo, bpf_program__fd(skel->progs.xdp_dummy_prog),
+				      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 +416,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();
Signature · 1 line
2.56.0
#13 lore raw

Re: [PATCH v7 bpf 5/5] selftests/bpf: Shrink test_run frags after generic XDP ran on lo

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] selftests/bpf: multi-line comment style violation in xdp_adjust_tail.c --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] selftests/bpf: multi-line comment style violation in xdp_adjust_tail.c
--

commit 9a3d1edafa68cea574abb6c7a0a6330bded5c6c7
Author: Slawomir Stepien <[email protected]>

selftests/bpf: Shrink test_run frags after generic XDP ran on lo

This commit adds a BPF selftest that sends a packet through generic XDP on the loopback interface in a new network namespace before running test_xdp_adjust_frags_tail_shrink().

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c +0 -0
index 43264347e7d77..c0d54cf421d8c 100644

[ ... ]

tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c static void test_xdp_adjust_frags_tail_grow_64k(void) · line 364

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/xdp_adjust_tail.c +4 -0
@@ -355,6 +358,52 @@ 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.
+ */

[Severity: Low]
This isn't a bug, but BPF coding style places the opening /* of a multi-line comment on its own line with the comment text starting on the next line.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5