BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Slawomir Stepien" <sst@poczta.fm>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v7 bpf 1/5] bpf: test_run: Use a private rxq info for XDP test runs
Date: Thu, 19 Nov 2026 13:49:38 +0000	[thread overview]
Message-ID: <sashiko-outbox-223211@kernel.org> (raw)
In-Reply-To: <20261119133032.2015656-2-sst@poczta.fm>

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 <sst@poczta.fm>

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

[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()")

> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> index d6c9dc62182ee..6761077586780 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,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.

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119133032.2015656-1-sst@poczta.fm?part=1

  reply	other threads:[~2026-11-19 13:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 13:30 [PATCH v7 bpf 0/5] xdp: fix page_pool leaks Slawomir Stepien
2026-11-19 13:30 ` [PATCH v7 bpf 1/5] bpf: test_run: Use a private rxq info for XDP test runs Slawomir Stepien
2026-11-19 13:49   ` sashiko-bot [this message]
2026-11-19 13:30 ` [PATCH v7 bpf 2/5] xdp: introduce xdp_convert_skb_to_buff() Slawomir Stepien
2026-11-19 13:41   ` sashiko-bot
2026-11-19 13:30 ` [PATCH v7 bpf 3/5] veth: use the frame mem type when running XDP on an xdp_frame Slawomir Stepien
2026-11-19 13:54   ` bot+bpf-ci
2026-11-19 13:30 ` [PATCH v7 bpf 4/5] veth: update mem type in xdp_buff Slawomir Stepien
2026-11-19 13:30 ` [PATCH v7 bpf 5/5] selftests/bpf: Shrink test_run frags after generic XDP ran on lo Slawomir Stepien
2026-11-19 13:37   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-223211@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sst@poczta.fm \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox