Netdev List
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn@kernel.org>
To: Magnus Karlsson <magnus.karlsson@intel.com>,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
	Alexander Lobakin <aleksander.lobakin@intel.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	netdev@vger.kernel.org, bpf@vger.kernel.org
Cc: "Björn Töpel" <bjorn@kernel.org>,
	"Simon Horman" <horms@kernel.org>,
	"Stanislav Fomichev" <sdf@fomichev.me>,
	"Martin KaFai Lau" <martin.lau@linux.dev>,
	"Song Liu" <song@kernel.org>,
	"Yonghong Song" <yonghong.song@linux.dev>,
	"Jiri Olsa" <jolsa@kernel.org>,
	"Emil Tsalapatis" <emil@etsalapatis.com>,
	"Ihor Solodrai" <ihor.solodrai@linux.dev>,
	"Tony Nguyen" <anthony.l.nguyen@intel.com>,
	"Przemek Kitszel" <przemyslaw.kitszel@intel.com>,
	intel-wired-lan@lists.osuosl.org,
	"Clément Lecigne" <clecigne@google.com>
Subject: [PATCH net v2 2/4] xdp: Size zero-copy skbs from the data, not the frame size
Date: Mon, 30 Nov 2026 13:37:33 +0100	[thread overview]
Message-ID: <20261130123737.125432-3-bjorn@kernel.org> (raw)
In-Reply-To: <20261130123737.125432-1-bjorn@kernel.org>

xdp_build_skb_from_zc() gives the skb head the frame size of the XSk
buffer, and the skb shared info takes the end of it. A device may fill
a single-buffer XSk frame to its end, so a long packet overwrites the
shared info of the new skb with packet data. For example, with 4K
chunks, MTU 3800, an xdp.frags program that returns XDP_PASS and a
socket bound without XDP_USE_SG, a 3814 byte frame needs 256 + 3814 +
320 = 4390 bytes and gets 4096. The last patch of this series lets
multi-buffer pools and bpf_xdp_adjust_tail() reach the end of the
frame as well.

The copies of the head and of the fragments also round the length up
to 16 bytes, and read and write past the end of the data.

Size the head from the data, and copy the exact length. A head that
does not fit into a page of the system page pool, such as one that
fills a whole 4K chunk, gets a kmalloc'd head from
__netdev_alloc_skb() instead.

Reported by Clément Lecigne, and found again by an AI code review
agent. Reproduced only with out-of-tree code: a test module, and the
AF_XDP page-pool RFC on fbnic in QEMU.

Reported-by: Clément Lecigne <clecigne@google.com>
Link: https://lore.kernel.org/bpf/20260629072300.1664622-1-clecigne@google.com/
Fixes: 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
Signed-off-by: Björn Töpel <bjorn@kernel.org>
---
 net/core/xdp.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/net/core/xdp.c b/net/core/xdp.c
index 1d679e8fd649..33a6740b411f 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -709,7 +709,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 		}
 
 		memcpy(page_address(page) + offset, skb_frag_address(frag),
-		       LARGEST_ALIGN(len));
+		       len);
 		__skb_fill_page_desc_noacc(sinfo, i, page, offset, len);
 
 		tsize += truesize;
@@ -723,6 +723,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 	return true;
 }
 
+static struct sk_buff *xdp_zc_alloc_head(struct page_pool *pp,
+					 struct net_device *dev, u32 size)
+{
+	u32 truesize = SKB_HEAD_ALIGN(size);
+	struct sk_buff *skb;
+	void *data;
+
+	if (truesize > PAGE_SIZE)
+		return __netdev_alloc_skb(dev, size, GFP_ATOMIC | __GFP_NOWARN);
+
+	data = page_pool_dev_alloc_va(pp, &truesize);
+	if (unlikely(!data))
+		return NULL;
+
+	skb = napi_build_skb(data, truesize);
+	if (unlikely(!skb))
+		page_pool_free_va(pp, data, true);
+
+	return skb;
+}
+
 /**
  * xdp_build_skb_from_zc - create an skb from XSk &xdp_buff
  * @xdp: source XSk buff
@@ -730,7 +751,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  * Similar to xdp_build_skb_from_buff(), but for XSk frames. Allocate an skb
  * head, new buffer for the head, copy the data and initialize the skb fields.
  * If there are frags, allocate new buffers for them and copy.
- * Buffers are allocated from the system percpu pools to try recycling them.
+ * Buffers are allocated from the system percpu pools to try recycling them,
+ * except a head that does not fit into a page.
  * If new skb was built successfully, @xdp is returned to XSk pool's freelist.
  * On error, it remains untouched and the caller must take care of this.
  *
@@ -738,33 +760,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  */
 struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
 {
+	u32 size = xdp->data_end - xdp->data_hard_start;
 	const struct xdp_rxq_info *rxq = xdp->rxq;
 	u32 len = xdp->data_end - xdp->data_meta;
-	u32 truesize = xdp->frame_sz;
 	struct sk_buff *skb = NULL;
 	struct page_pool *pp;
 	int metalen;
-	void *data;
 
 	if (!IS_ENABLED(CONFIG_PAGE_POOL))
 		return NULL;
 
 	local_lock_nested_bh(&system_page_pool.bh_lock);
 	pp = this_cpu_read(system_page_pool.pool);
-	data = page_pool_dev_alloc_va(pp, &truesize);
-	if (unlikely(!data))
-		goto out;
 
-	skb = napi_build_skb(data, truesize);
-	if (unlikely(!skb)) {
-		page_pool_free_va(pp, data, true);
+	skb = xdp_zc_alloc_head(pp, rxq->dev, size);
+	if (unlikely(!skb))
 		goto out;
-	}
 
 	skb_mark_for_recycle(skb);
 	skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
 
-	memcpy(__skb_put(skb, len), xdp->data_meta, LARGEST_ALIGN(len));
+	memcpy(__skb_put(skb, len), xdp->data_meta, len);
 
 	metalen = xdp->data - xdp->data_meta;
 	if (metalen > 0) {
-- 
2.56.1


  parent reply	other threads:[~2026-11-30 12:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 12:37 [PATCH net v2 0/4] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel
2026-11-30 12:37 ` [PATCH net v2 1/4] libeth: xsk: Count each XDP_TX buffer on its own Björn Töpel
2026-11-30 12:37 ` Björn Töpel [this message]
2026-11-30 12:37 ` [PATCH net v2 3/4] intel: Copy the exact length of XSk packets to skbs Björn Töpel
2026-11-30 12:37 ` [PATCH net v2 4/4] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel

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=20261130123737.125432-3-bjorn@kernel.org \
    --to=bjorn@kernel.org \
    --cc=aleksander.lobakin@intel.com \
    --cc=andrii@kernel.org \
    --cc=anthony.l.nguyen@intel.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=clecigne@google.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kuba@kernel.org \
    --cc=maciej.fijalkowski@intel.com \
    --cc=magnus.karlsson@intel.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=przemyslaw.kitszel@intel.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=willemb@google.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

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

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