🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #27018

[PATCH net-next v2 01/15] xdp: Let memory providers expose net_iovs to XDP

#27018
From: Björn Töpel
Date: Fri, 04 Dec 2026 16:27
162 lines
14614 bytes
Currently, the address helpers give NULL for net_iovs, so XDP cannot
read them. The AF_XDP provider later in this series backs net_iovs
with UMEM chunks that XDP must read. As for io_uring zcrx, these are
user pages pinned with GUP, whose struct page fields mm owns, so they
cannot be page_pool pages.

Let a net_iov area offer an XDP view, an address per net_iov that XDP
may read and write. Add xdp_netmem_address() and xdp_frag_address()
for drivers and the BPF helpers. They give NULL for a net_iov without
a view.

The stack still treats every net_iov as unreadable, so other readers
get NULL and fail closed. Such buffers must never become skb frags or
xdp_frames.

Providers still block native XDP and need header split until the next
patch adds MP_XDP_ONLY.

Signed-off-by: Björn Töpel <bjorn@kernel.org>
---

Notes (v2changes):
    Changes in v2:
    - Rename from "xdp: Let memory providers offer XDP a view of their
      net_iovs".
    - Name the provider by what it does, AF_XDP with explicit chunk
      return, and say that MP_XDP_ONLY comes with the next patch.
    - Bring the comment on unreadable XDP frags up to date, as XDP reads
      such frags through the view of their provider (Sashiko).

 include/net/netmem.h |  8 ++++++++
 include/net/xdp.h    | 47 +++++++++++++++++++++++++++++++++++++++++---
 net/core/filter.c    |  8 ++++----
 3 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/include/net/netmem.h b/include/net/netmem.h
index fb693bc1702a..4cf4ee1cd55d 100644
--- a/include/net/netmem.h
+++ b/include/net/netmem.h
@@ -104,6 +104,14 @@ struct net_iov_area {
 	/* Array of net_iovs for this area. */
 	struct net_iov *niovs;
 	size_t num_niovs;
+	/* XDP view, see xdp_netmem_address(): XDP may read and write net_iov i
+	 * at xdp_vaddr + (i << xdp_shift), xdp_vsize bytes in all. NULL if the
+	 * provider offers none. The core and the stack take every net_iov as
+	 * unreadable either way.
+	 */
+	void *xdp_vaddr;
+	size_t xdp_vsize;
+	unsigned int xdp_shift;
 };
 
 static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)
diff --git a/include/net/xdp.h b/include/net/xdp.h
index 052402766988..da4bb98fceeb 100644
--- a/include/net/xdp.h
+++ b/include/net/xdp.h
@@ -80,9 +80,10 @@ enum xdp_buff_flags {
 	XDP_FLAGS_FRAGS_PF_MEMALLOC	= BIT(1), /* xdp paged memory is under
 						   * pressure
 						   */
-	/* frags have unreadable mem, this can't be true for real XDP packets,
-	 * but drivers may use XDP helpers to construct Rx pkt state even when
-	 * XDP program is not attached.
+	/* frags have memory that the stack cannot read, as every net_iov
+	 * has. XDP reads such frags only through the XDP view of their
+	 * provider, see xdp_netmem_address(). Drivers may use XDP helpers to
+	 * construct Rx pkt state even when XDP program is not attached.
 	 */
 	XDP_FLAGS_FRAGS_UNREADABLE	= BIT(2),
 	/* The shared info lives in &xdp_rxq_info.ext_sinfo, not at the end of
@@ -175,6 +176,46 @@ xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
 #endif
 }
 
+/* XDP view of net_iovs: a memory provider may give XDP an address for its
+ * net_iovs, see &net_iov_area.xdp_vaddr. XDP code may read and write a
+ * net_iov only through these helpers, and only if its provider offers a
+ * view; such buffers must not become skb frags or xdp_frames. For the core
+ * and the stack every net_iov stays unreadable: netmem_address() and
+ * skb_frag_address() give NULL for it. A net_iov without a view gives NULL
+ * here too, so a reader that does not use these helpers fails closed.
+ */
+static __always_inline void *xdp_net_iov_address(const struct net_iov *niov)
+{
+	const struct net_iov_area *area = net_iov_owner(niov);
+
+	if (!area->xdp_vaddr)
+		return NULL;
+	return area->xdp_vaddr +
+	       ((unsigned long)net_iov_idx(niov) << area->xdp_shift);
+}
+
+/* Address of @netmem, which a driver uses as the head of an XDP buffer */
+static __always_inline void *xdp_netmem_address(netmem_ref netmem)
+{
+	if (netmem_is_net_iov(netmem))
+		return xdp_net_iov_address(netmem_to_net_iov(netmem));
+	return __netmem_address(netmem);
+}
+
+/* Address of the bytes of @frag of an XDP buffer */
+static __always_inline void *xdp_frag_address(const skb_frag_t *frag)
+{
+	netmem_ref netmem = skb_frag_netmem(frag);
+	void *addr;
+
+	/* Only a net_iov can lack an address */
+	if (!netmem_is_net_iov(netmem))
+		return __netmem_address(netmem) + skb_frag_off(frag);
+
+	addr = xdp_net_iov_address(netmem_to_net_iov(netmem));
+	return addr ? addr + skb_frag_off(frag) : NULL;
+}
+
 static __always_inline void
 xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
 		 int headroom, int data_len, const bool meta_valid)
diff --git a/net/core/filter.c b/net/core/filter.c
index 1ebc2fab7211..90e4bf78c689 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -4311,7 +4311,7 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
 			break;
 
 		ptr_off += ptr_len;
-		ptr_buf = skb_frag_address(next_frag);
+		ptr_buf = xdp_frag_address(next_frag);
 		ptr_len = skb_frag_size(next_frag);
 		next_frag++;
 	}
@@ -4339,7 +4339,7 @@ void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
 		u32 frag_size = skb_frag_size(&sinfo->frags[i]);
 
 		if  (offset < frag_size) {
-			addr = skb_frag_address(&sinfo->frags[i]);
+			addr = xdp_frag_address(&sinfo->frags[i]);
 			size = frag_size;
 			break;
 		}
@@ -4430,7 +4430,7 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
 	if (unlikely(offset > tailroom))
 		return -EINVAL;
 
-	memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
+	memset(xdp_frag_address(frag) + skb_frag_size(frag), 0, offset);
 	skb_frag_size_add(frag, offset);
 	sinfo->xdp_frags_size += offset;
 	if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
@@ -12846,7 +12846,7 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
 		skb_frag_t *frag = &sinfo->frags[i];
 		u32 shrink = min_t(u32, delta, skb_frag_size(frag));
 
-		memcpy(xdp->data_end, skb_frag_address(frag), shrink);
+		memcpy(xdp->data_end, xdp_frag_address(frag), shrink);
 
 		xdp->data_end += shrink;
 		sinfo->xdp_frags_size -= shrink;
-- 
2.56.1
Technical Details
Message-ID: <20261204162723.755897-2-bjorn@kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377483-linux-kernel-netdev=dewy.ru
References: <20261204162723.755897-1-bjorn@kernel.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377483-linux-kernel-netdev=dewy.ru
From: bjorn@kernel.org (Björn Töpel)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net-next v2 01/15] xdp: Let memory providers expose net_iovs to XDP
Date: Fri,  4 Dec 2026 17:27:06 +0100
Organization: Usenet Network
Message-ID: <20261204162723.755897-2-bjorn@kernel.org>
References: <20261204162723.755897-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="2805911"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:hhGua/bFieM0mmfgbjcFnFLTOxQ= sha256:fhuFfjy/KaijPnAovTyJdHgt2iRpgjU+iC48sn4ZncA=
	sha1:TVhzvE943EbpUQsH7/IsfGRQ2s0= sha256:qnkCONBgVfLbgE4z79muTucQYUVHk4LoVSlBSVK6M1Y=
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 BA462117C61; Fri,  4 Dec 2026 19:28:10 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.542
X-Spam-Status: No, score=-3.542 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.142, 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 btjT6f4MPxei;
	Fri,  4 Dec 2026 19:28:08 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-377483-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 A89EA117C5D; Fri,  4 Dec 2026 19:27:58 +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 788E73FDF1; Fri,  4 Dec 2026 16:27:51 +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=1796401670;
	b=1x+Ms/kZCXHwOpSei+U3nXcmlSINezEWnSd9AnBkITXIUpHzhwx01usC9dxaE3IyqXth
	 S2eil6W2OIXqmF1c6OSQjP1QDdUt/snOo/RSN6kB3vvg+QAdDYACMFcrZXcxd5CFOuE7R
	 c0TWki2d3MrTkMddCUfaX/qlOSJzN4994YUh1oXye8ORfijw9oU1V9UFMZfjejNPG1Zxi
	 vvuVhgBv5LYCcbDHbehYEthQ7FvoaOpaxpK/+dMmOs0hP6FLCcjCeHAaZrFsp4Y1nuSdx
	 QwbtmhwyB9caOmdinKkI365Xupv0tnMylGNKGceJSwjUdAdklFM9lRjsejgteyTVZhg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796401670;
	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=B90dl92Q6oxlQ8EoAjvaPv119jXUayFy3mIScaSSNko=;
	b=pas6b6LV1Ctyp8Y9FG9h1J/MBG5SjWlAxhrcS5Ro5oUmaAf4exuSP5Z1MxXkdRgJ+wOQ
	 bKU+Ag2IPMDfHy3tWmPsr7soNcvzczUFveeRrquv6Jak4WK6ttGTeKpVphCYU4kx6FjFp
	 PUwB/bk7IF1qO9uJU9QAf8/oTrfjo0mnR0E0dTig4ebQQDy/Hr6vdX82IvrGjohPORLiE
	 MtxIcAFQmFFg23d/Lq9H8ONfy+nZvcmQXJe4MiPJtm0mMwAZdCBJMgkioOhPwfgqqzlH5
	 ty2TWGc58x+jbd0PiJttNTng4F2S2eTCrHmh4jYfDAwv6sXfOIOANthbxDqq8NcB15Q==
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=aRIQ3tgW;
	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 91A15606A686;
	Fri,  4 Dec 2026 16:27:50 +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=aRIQ3tgW
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=1796401669;
	b=NDUiebZXwMaS4BaDUKyyVbojguGVIypaIgkNXclYb4s87XTo3R2mAqc2goYo66Ow5gCH
	 jOoJJqpZZNi3uCfKZXBukjXBlZCx47zF+O5+pec+gbGc/czfF/y9WnVuRVbGrHAK9mTbR
	 0Mduks25r6+GQYMkaDPSEiM8WUWCzJd+dd1V10OGLZTtOp0oeE+LKhrbePpWng4xE5liM
	 JUdMsubzdOf7AsK3+6ZYL9IiO9Ql3msScQ9VkMH4xYJFUpMHWkZ1XVao4e+xy1GxZYcst
	 bpi4v3pKNTC1EAsbb4myE57Jmc4e/Cg2qGCZGlDD72lFSOYhprS1pmA1DSZj1PvlU1w==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796401669;
	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=B90dl92Q6oxlQ8EoAjvaPv119jXUayFy3mIScaSSNko=;
	b=U6W5zZ47YlprrpZvhygBFe38m/Tpra4kVZrewUUtsQnFGpTq4lUoQkCja7XMs3strWP6
	 4fviuvLV/yNDXj8fDyOH7++LqAlWCk8JdIp585z/CnWpgtEozzhQ1efmjeAJpPh9C21hv
	 gPcLoRn7hIpJ/sUEIHGlFBjHgSzWtSLO3dwak5pbBtdos9w4HTHQ933XGUWZsBWvuZUqS
	 rewKtn5JRoh8sUZ9P/yum/ZF7PCEm/KiQOk0hrSfAmg0Z+1+9Vvx/RKfJPsZTBqYySXbM
	 uV8vLlqBYTm+in5ES/SEK7Au8pwKHfpCTh3Tq2ilx6Pch+NB43iiCHPqIwV82M7VYAg==
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=aRIQ3tgW;
	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 B2233606A330;
	Fri,  4 Dec 2026 16:27:49 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB3491F0092C;
	Fri,  4 Dec 2026 16:27:40 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796401669;
	bh=B90dl92Q6oxlQ8EoAjvaPv119jXUayFy3mIScaSSNko=;
	h=From:To:Cc:Subject:Date:In-Reply-To:References;
	b=aRIQ3tgWrI1g0qG/6SbsAI7Gnq4RtsoIzGx5ZGcGRF/o8Bhgq/lpfUA1IiJdA21jc
	 o43NJI49M5VF5A7TC9p/VMVn3POr5mmsPyvcRILOlE5GGe6pkzRav0oF1Y2pkaw5OF
	 D9MCnrmjPk4NZx6fFG5X25Jk5noTHEFV9qOaW3tcN/pe0+papY3TPElTxuwfgFwKo/
	 bqXmemq+45gL8bRI/53aFmhJmFbiUGfOlLXcicgaoJHZfSHtmge0Lb2psaYhibAwNz
	 xDgKInM873Fai6KDdQKfKa44bGZ/mT5ZBTEKfXeqwcmpdVjiAtJ4sEyceUmULTwE27
	 j8XciwNQhp2RQ==
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>,
	David Wei <dw@davidwei.uk>,
	Tao Cui <cuitao@kylinos.cn>,
	linux-kernel@vger.kernel.org,
	kernel-team@meta.com,
	"Mike Marciniszyn (Meta)" <mike.marciniszyn@gmail.com>,
	linux-rdma@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	Shuah Khan <skhan@linuxfoundation.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-doc@vger.kernel.org
X-Mailer: git-send-email 2.56.1
X-In-Reply-To: <20261204162723.755897-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