Article View: linux.kernel.netdev
Article #21764[PATCH net-next 02/15] xdp: Keep the shared info of a buffer in kernel memory
From: Björn Töpel
Date: Tue, 24 Nov 2026 12:56
Date: Tue, 24 Nov 2026 12:56
175 lines
15594 bytes
15594 bytes
Currently, the shared info of an XDP buffer, with the fragment list of
a multi-buffer packet, lives at the end of the buffer's frame, in
memory that the device writes. Helpers follow its fragments, drivers
free them through it, and frame and skb conversion copy it, so memory
that others may write while the kernel uses it must not hold it.
Classic AF_XDP zero copy keeps it in the tail of a UMEM chunk, where
userspace can rewrite it.
Let an RX queue keep the shared info of its buffers in kernel memory.
The queue gets one from xdp_rxq_info_reg_ext_sinfo(), and
xdp_rxq_info_unreg_mem_model() frees it. A buffer with the new
XDP_FLAGS_SINFO_EXT finds its shared info there.
Whoever prepares a buffer sets the flag before adding fragments. The
queue NAPI builds one buffer at a time, so one shared info per queue
is enough. It is valid until the buffer is redirected, dropped or
copied. Frame conversion and skb construction refuse buffers with the
flag, as a frame or an skb would keep its shared info in the buffer.
Nothing sets the flag until later in this series.
xdp_data_hard_end() reserves no shared-info tailroom for such a
buffer, so its data may reach the end of the frame. frame_sz then
bounds every write past the data, such as tail growth and
bpf_xdp_pull_data(), so the frame must not cover memory of other
buffers. Drivers may then pack such buffers tighter.
Signed-off-by: Björn Töpel <bjorn@kernel.org>
---
include/net/xdp.h | 41 +++++++++++++++++++++++++++++++++++------
net/core/xdp.c | 30 ++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 6 deletions(-)
diff --git a/include/net/xdp.h b/include/net/xdp.h
index 1629e347b71f..944418111a86 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -65,6 +65,11 @@ struct xdp_rxq_info {
u32 reg_state;
struct xdp_mem_info mem;
u32 frag_size;
+ /* Shared info of buffers with %XDP_FLAGS_SINFO_EXT, in kernel
+ * memory, see xdp_rxq_info_reg_ext_sinfo(). The queue builds one
+ * such buffer at a time.
+ */
+ struct skb_shared_info *ext_sinfo;
} ____cacheline_aligned; /* perf critical, avoid false-sharing */
struct xdp_txq_info {
@@ -81,6 +86,15 @@ enum xdp_buff_flags {
* XDP program is not attached.
*/
XDP_FLAGS_FRAGS_UNREADABLE = BIT(2),
+ /* The shared info lives in &xdp_rxq_info.ext_sinfo, in kernel memory,
+ * not at the end of the frame. For memory that someone outside the
+ * kernel may write. Whoever prepares the buffer sets it before adding
+ * fragments, on a queue that has ext_sinfo. The queue NAPI builds one
+ * such buffer at a time: its shared info is valid until the buffer
+ * is redirected, dropped or copied. The data may reach the end of the
+ * frame, see xdp_data_hard_end(): the frame must hold nothing else.
+ */
+ XDP_FLAGS_SINFO_EXT = BIT(3),
};
struct xdp_buff {
@@ -212,17 +226,27 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
/* Reserve memory area at end-of data area.
*
- * This macro reserves tailroom in the XDP buffer by limiting the
- * XDP/BPF data access to data_hard_end. Notice same area (and size)
- * is used for XDP_PASS, when constructing the SKB via build_skb().
+ * This reserves tailroom in the XDP buffer by limiting the XDP/BPF data
+ * access to data_hard_end. Notice same area (and size) is used for
+ * XDP_PASS, when constructing the SKB via build_skb(). Buffers with
+ * %XDP_FLAGS_SINFO_EXT keep the shared info elsewhere and reserve none.
*/
-#define xdp_data_hard_end(xdp) \
- ((xdp)->data_hard_start + (xdp)->frame_sz - \
- SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
+static __always_inline void *xdp_data_hard_end(const struct xdp_buff *xdp)
+{
+ u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
+
+ if (xdp->flags & XDP_FLAGS_SINFO_EXT)
+ tailroom = 0;
+ return xdp->data_hard_start + xdp->frame_sz - tailroom;
+}
static inline struct skb_shared_info *
xdp_get_shared_info_from_buff(const struct xdp_buff *xdp)
{
+ /* Kernel structures stay out of memory that userspace may write */
+ if (unlikely(xdp->flags & XDP_FLAGS_SINFO_EXT))
+ return xdp->rxq->ext_sinfo;
+
return (struct skb_shared_info *)xdp_data_hard_end(xdp);
}
@@ -454,6 +478,10 @@ int xdp_update_frame_from_buff(const struct xdp_buff *xdp,
return -ENOSPC;
}
+ /* A frame keeps its shared info in the buffer */
+ if (unlikely(xdp->flags & XDP_FLAGS_SINFO_EXT))
+ return -EFAULT;
+
xdp_frame->data = xdp->data;
xdp_frame->len = xdp->data_end - xdp->data;
xdp_frame->headroom = headroom - sizeof(*xdp_frame);
@@ -533,6 +561,7 @@ bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq);
int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
enum xdp_mem_type type, void *allocator);
void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq);
+int xdp_rxq_info_reg_ext_sinfo(struct xdp_rxq_info *xdp_rxq);
int xdp_reg_mem_model(struct xdp_mem_info *mem,
enum xdp_mem_type type, void *allocator);
void xdp_unreg_mem_model(struct xdp_mem_info *mem);
diff --git a/net/core/xdp.c b/net/core/xdp.c
index 1d679e8fd649..3472b03ee661 100644
--- a/net/core/xdp.c
+++ b/net/core/xdp.c
@@ -142,6 +142,8 @@ void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq)
}
xdp_unreg_mem_model(&xdp_rxq->mem);
+ kfree(xdp_rxq->ext_sinfo);
+ xdp_rxq->ext_sinfo = NULL;
}
EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model);
@@ -368,6 +370,30 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq,
EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model);
+/**
+ * xdp_rxq_info_reg_ext_sinfo - keep the shared info of buffers in the queue
+ * @xdp_rxq: registered RX queue
+ *
+ * For buffers in memory that someone outside the kernel may write: those
+ * with %XDP_FLAGS_SINFO_EXT keep their shared info in kernel memory of
+ * @xdp_rxq instead of at the end of their frame.
+ * xdp_rxq_info_unreg_mem_model() frees it.
+ *
+ * Return: 0 on success, or a negative errno.
+ */
+int xdp_rxq_info_reg_ext_sinfo(struct xdp_rxq_info *xdp_rxq)
+{
+ if (xdp_rxq->reg_state != REG_STATE_REGISTERED) {
+ WARN(1, "Missing register, driver bug");
+ return -EFAULT;
+ }
+
+ if (!xdp_rxq->ext_sinfo)
+ xdp_rxq->ext_sinfo = kzalloc_obj(*xdp_rxq->ext_sinfo);
+ return xdp_rxq->ext_sinfo ? 0 : -ENOMEM;
+}
+EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_ext_sinfo);
+
/**
* xdp_reg_page_pool - register &page_pool as a memory provider for XDP
* @pool: &page_pool to register
@@ -638,6 +664,10 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
u32 nr_frags = 0;
int metalen;
+ /* An skb keeps its shared info in the buffer */
+ if (unlikely(xdp->flags & XDP_FLAGS_SINFO_EXT))
+ return NULL;
+
if (unlikely(xdp_buff_has_frags(xdp))) {
sinfo = xdp_get_shared_info_from_buff(xdp);
nr_frags = sinfo->nr_frags;
--
2.56.1
Technical Details
Message-ID:
<20261124125654.183091-2-bjorn@kernel.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370347-linux-kernel-netdev=dewy.ruReferences:
<20261124112707.118678-1-bjorn@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder2.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370347-linux-kernel-netdev=dewy.ru From: bjorn@kernel.org (Björn Töpel) Newsgroups: linux.kernel.netdev Subject: [PATCH net-next 02/15] xdp: Keep the shared info of a buffer in kernel memory Date: Tue, 24 Nov 2026 13:56:37 +0100 Organization: Usenet Network Message-ID: <20261124125654.183091-2-bjorn@kernel.org> References: <20261124112707.118678-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="4137626"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:QhukcyNehq68U27gHYsKTPr85PI= sha256:lAre9Xji5XmFqqcwuE5blZNQ5CJLU+vSSmR7ZY72n54= sha1:x5jDPOwlDC41zbsbEuFnsj/e7y8= sha256:9QNLU9gctr6kgVxP9uHmMlp1N8xdXjoVriKREbe4itg= 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 7BC30125E15; Tue, 24 Nov 2026 15:57:50 +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 tYInnAM9vYvO; Tue, 24 Nov 2026 15:57:47 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-370347-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 B37F3125E0A; Tue, 24 Nov 2026 15:57:36 +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 E3AC52D04F; Tue, 24 Nov 2026 12:57:34 +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=1795525054; b=P9gb813nxfe1hrcnxO0lp3kZbsEw2BIJ1ETIRXnvuGkvWs/dYvJXsnbxHrAejUjIEC3q CLRlXZ89rw+Ou4FfQNuTjT8nfGifQeMMg4DLUCfrlNaoEdhqjkL0RsSsO9WCtF1OjqhW5 Sy29jHVp6x9b9mJWf+u/F+iB1P28do+aIr5Iw9PyvOC6tlK8AbV2WswXHr0pjfuVxsSDs GNRYsrajjZ0R+XCoLxZJMyBG4PXQo37YUvuqbsSlfVbnw2Wdt5IWlkrdxxG/qeWBvKaou mH5XqX04Icn1yiTvSbmbHJsBru57Y+6MOB50pZ9nDOWlm0a/Ur5wcPFR/eOGx+4A6uw== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795525054; 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=dNNwywQJbTsZXg/w5rJJ/5JqisBmDi3sRrbjcHK3wFI=; b=YdERXkjAmYI4gNQPVZr+s9DebQzpWJvX9z8G5xvH7joecJqhT120wGyed4wg484eoVv8 Bxe8HwALauxkLfb/zeOqJpR+UJISVtUdDW2CXqT2aAjkCE8zhfBum/Miets5EY6apVQCA wsd0XpErOo2oBQl9SpH1qXpsQCziXmuiXGz5MG6QpNxLN0rMkhgfyVriIiRb4MKUJbO2q 4EcyZ+1W0aTbDCFpS8NaMx6nYbymVpaGUChZNy9gSwyGx/Y1DeFiJz8UCZ2vO6IlCF7xv RF/JsfO96OalXsgRJvrvjZtZ3kj+XvDt+SPb/GZQlJUMjQknQeGAxNc93bTsitZ32TQ== 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=RITCRP94; 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 11316606A337; Tue, 24 Nov 2026 12:57:34 +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=RITCRP94 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=1795525048; b=ZFmqVCI5hyQd1LY6mM9rYdJKEtmLq20urIBVAraSI7qB1mebDXWqD2LVjYFkZfGPRSB+ ngWLyy7WZW0ZvimahwG6JUbHHHWLIAqMIUGQV2XW2PH0ZpkqFwqpvwy5DGE4fMsRd+Tgy jdA8y91HoT34Vp5O+pJSt+WOSg1rVHUb9LpCa9iknUbsXyhvDAXrSCLuOQ5ZzzHcOpQEh t6T7eor+JzUnukL7LKF4/OJ8CSLWHoX+RTnVK65fK2nppJddU+Js1cmbhuCPcApxAMmq+ lGMB6KKJTjycCIqj5F5933Y4rHN/Ba/zTxYB8Rwrv71B2GcMaKsOkILicczS0uuvp8g== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795525048; 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=dNNwywQJbTsZXg/w5rJJ/5JqisBmDi3sRrbjcHK3wFI=; b=d5cFrg/rmRCEIE7UtKOda7XXXK07l9QzFAhzZEepxJhfIKK9TrehRj3zXlXBDTTbMgwL K4qLxUzx7U0nbMttpGiAmqdyQXhHUVPAgSR7RpYvg4RotdVAwOJPWoec/xc88PLyUfAsY Tmg8zWfnyzXi/vE6tJFn3NPypLQ0ALTXoRB4njqpLyVYZC7sv2w4XDEHf2wZXkwng6O2O 82oYDHOolqDRjL+cMjrPKYs0SKKnwZOtfCZ/tWfJSHX1stkk3mBI6yqfu2vxzBUs/lXLP 1iwhlxnw1xgDPwOrsEOsZw1hXzsbf5ft0mSwE/ptroOj/Z+pXmDk1RjkNEUQG/vPauQ== 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=RITCRP94; 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 2D3CF606A31E; Tue, 24 Nov 2026 12:57:28 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7FEA51F000D8; Tue, 24 Nov 2026 12:57:16 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795525047; bh=dNNwywQJbTsZXg/w5rJJ/5JqisBmDi3sRrbjcHK3wFI=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RITCRP94VvGO4yKZt0Mc2EcdoIJbRPKkygZKiYEXUC62oX65zzxJGW+g0nf5HePbL PGeUarp0OhFoqURnm96f7IledVu7c4FlRPUkYORRD3SQ9yS9k0dq3kwFensDxqQr3f SDlANCSzwy5mB+SKo/8YPyNKTUtsO81jpD5EqhVUuVyGfqywVmwdhwneN2iSUmEepp mOuwN82PRrFLy96Hcb7HvR3fUB9ZOdQljO5g44VyYiqdvxEPIIX8AEbOOdgKmSQoh0 ludlwWiniWUwo8QmedTZYseZiYZkp2QXqOmg7JT0xCm7YqXvoFpPzEFF+uKlK+Hh+T Rc/m9v+KjiV7Q== X-Cc: Stuart Clark <stuclar@meta.com>, Björn Töpel <bjorn@kernel.org>, Stanislav Fomichev <sdf@fomichev.me>, Simon Horman <horms@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, Mina Almasry <almasrymina@google.com>, 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>, Randy Dunlap <rdunlap@infradead.org>, kernel-team@meta.com, Dimitri Daskalakis <dimitri.daskalakis1@gmail.com>, Bobby Eshleman <bobbyeshleman@meta.com>, Mohsin Bashir <mohsin.bashr@gmail.com>, Kees Cook <kees@kernel.org>, Breno Leitao <leitao@debian.org>, Maxime Chevallier <maxime.chevallier@bootlin.com>, Tao Cui <cuitao@kylinos.cn>, Vadim Fedorenko <vadim.fedorenko@linux.dev>, "Mike Marciniszyn (Meta)" <mike.marciniszyn@gmail.com>, Kuniyuki Iwashima <kuniyu@google.com>, Jiayuan Chen <jiayuan.chen@linux.dev>, Martin Karsten <mkarsten@uwaterloo.ca>, Nikolay Aleksandrov <razor@blackwall.org>, David Wei <dw@davidwei.uk>, Pavel Begunkov <asml.silence@gmail.com>, linux-rdma@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org X-Mailer: git-send-email 2.56.1 X-In-Reply-To: <20261124112707.118678-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