Article View: linux.kernel.netdev
Article #24610[PATCH net v2 2/4] xdp: Size zero-copy skbs from the data, not the frame size
From: Björn Töpel
Date: Mon, 30 Nov 2026 12:37
Date: Mon, 30 Nov 2026 12:37
122 lines
12535 bytes
12535 bytes
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
Technical Details
Message-ID:
<20261130123737.125432-3-bjorn@kernel.org>Path:
mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374172-linux-kernel-netdev=dewy.ruReferences:
<20261130123737.125432-1-bjorn@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lon.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder1.feed.ams11.usenet.farm!feed.usenet.farm!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374172-linux-kernel-netdev=dewy.ru From: bjorn@kernel.org (Björn Töpel) Newsgroups: linux.kernel.netdev 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 Organization: Usenet Network Message-ID: <20261130123737.125432-3-bjorn@kernel.org> References: <20261130123737.125432-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1424864"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:oYiwArZ6QVxMrXh78VDAPmCQKT8= sha256:E+VJt0iM0Ov0KYaZaxxiY8ljeU+edHOSLUb0pV6dUE8= sha1:iNr7ZQA8kGclw21i2m+RQP8J6ME= sha256:FIf2DKj4991BKXsO0yJpBI4qjQ82CieVlweBJ9HfN5A= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 435B6125FE3; Mon, 30 Nov 2026 15:38:15 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.401 X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dUzhDEdzLh7l; Mon, 30 Nov 2026 15:38:11 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-374172-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 8285B125FDE; Mon, 30 Nov 2026 15:38:03 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 54BD42D263; Mon, 30 Nov 2026 12:38:00 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796042279; b=VgD7t0Cgh3IAvozxHk7aX4zayD4xsuYrIyGhZ7rIAuZGdlv6Io83jrs5vD/xxfr5h5es /FIqeOeeSzyXZMKxL5D9V7KRaul78EKRQq3Og4oEKPoaK4jxncdz/Z+S4j4A+rzOwySb+ sHxYRWlnY/Rr6iHrPPnFKEXH0da9bI1okBwjbyNFuQgJMCikZtLouOoO2i0shFsJwcI1j tOOkGyH74ozViAGQu4mSHuSfWTioT2O92UzdC/2sRtLmU/mAbl2XAV8K/epgKw1uUIrt6 0ZLdUmxY9o5Uh9SMaCaiDlzh1kIMoMDOyPz2fMTB1DKwRRLe6RQs/tX5db722XCdHvg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796042279; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=NDz/XFLS7is7xTvXkt8FpdsgSqL0b4z/Pc+pKdjj6sA=; b=l+f5kdAdzVhOGuweW6WHcTbwMnY+Od03RsEIhhEfNhUY8utQXszDvM0Wesw+HjKiAzB7 cdoZb1JLR8iUDljTgZxd5Yq5Kp6xm8NL6i5bxt2ijVDZiXplej1bNvkmWBE06vDITvTIe LkOvpra5eEqYTj+zEM/1AMjsxMbJStmHZDlXQFEnVPgI7c67Lvi9B92XXBo9AGcdaQggE hkl3jF69+St8IrL9g7NWcdj9ZblRuHWzW8+F0yRdtLjez2kFoGn5ADDx4C76AWWV6h9vt 6QJN2ytaki2bx0aNslPuQhmIjOw9YNG3H/UjKg1kgF8sOBDzOEloWU6byVK8W2735Kg== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=d2piCtSU; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B67BE606A331; Mon, 30 Nov 2026 12:37:59 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=d2piCtSU X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796042278; b=3kWpm/vUEuGlJgaqLYLebmsk4a78ciQs1p/g6tDPxcLlELXv+xqj+MnBJhfjlfj1AyRd LoHsJn301e1hffRGo6ibS8i7aXhKPEqnW2eZhPHca1vMWdBwP1u7Zf3cmsOO4qLwsyzWp CBGzdRPkS5KxJfAl1fhLpw93p9crOQtTrCI7T2+YXVd0sRGFQsKwvPmFaa19FIVbi5OFb 3RKsvf+UNBYZIiuD6laKAUynp18QxzcXG3wlvKRLhMeKnFBOy0jljPsKW2BCcOmlfIil2 xahOTvzmhW5oO7Uwyi2x5lKQWkBwzd/9znv65Ja/13HeGtBA/Xxky82RHPhIeEeF3Ig== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796042278; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=NDz/XFLS7is7xTvXkt8FpdsgSqL0b4z/Pc+pKdjj6sA=; b=ddLLfX/ML/ybgEYZK7zyWERpwPugScr5SXZhEd89uAXHFaRWhGsagH2GeWdGsX+x76vS fXJb7PkFjcrMWX5hsiyGyvFt9USs3FnTvpuWyy5IFAuOldBjyKhmBUaIBT+az6wB9L2kS B4+xqa1TUmbRlCYXK4wtH0FJMtutikKS01z77q9ogLTCqPPHtKK2z/vj7rTY7MGYEWaeH /kCkTRGQ/t44TgDT0tuNPBqrn+/7timcbvwGYIElHWUo3XMjdcVkg050KBkvBn9cTMEfw 5/oWqhaTxu2WLHqw1A5/tj95npnxwNB1lrDbTemwnWfs7wTHpHSabzZHY93o0BLeyew== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=d2piCtSU; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D66FE606A31C; Mon, 30 Nov 2026 12:37:58 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76FA31F00897; Mon, 30 Nov 2026 12:37:52 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796042278; bh=NDz/XFLS7is7xTvXkt8FpdsgSqL0b4z/Pc+pKdjj6sA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=d2piCtSUUZPpJhYFnQb6XRuxjZApf1IgfAfBT1Uk51y9yLXOADNRi9WVS7GqHVRts Fr8jRn4QjclE5ymY11ymNEug8+gpZSZjujd4AzLs+g4dKQ0NfIjiUx7JnEalRWnpo+ 1EVJLEce/SLGLaqZecnS/1dvU1JHARDvMv+wbWDVyhD3ndJC0AvvPyRdBp+u6CV9Lj JeqsvlIZVLr7YejMvEKdpLf68OdFoBDWHwkZi9kmJ6mperDwtEFw71wwiAQcqIgxp0 zDcsc8c89wrBzxQ7ckOX5kOAqgV+UX0DfVK1vucCbE5s1neWr1RNisFqF8bfvmW0uy JcEwwJFd9E5rQ== X-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> X-Mailer: git-send-email 2.56.1 X-In-Reply-To: <20261130123737.125432-1-bjorn@kernel.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-Received-Bytes: 12249