Netdev List
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn@kernel.org>
To: "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>,
	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>,
	Nikolay Aleksandrov <razor@blackwall.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Magnus Karlsson <magnus.karlsson@intel.com>,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
	Donald Hunter <donald.hunter@gmail.com>,
	Alexander Duyck <alexanderduyck@fb.com>,
	Saeed Mahameed <saeedm@nvidia.com>,
	Tariq Toukan <tariqt@nvidia.com>, Mark Bloch <mbloch@nvidia.com>,
	Leon Romanovsky <leon@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	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>,
	"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
Subject: [PATCH net-next v2 05/15] xsk: Back a queue's page pools with UMEM chunks
Date: Fri,  4 Dec 2026 17:27:10 +0100	[thread overview]
Message-ID: <20261204162723.755897-6-bjorn@kernel.org> (raw)
In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org>

Currently, AF_XDP zero copy fills one chunk per buffer at the UMEM
headroom, so drivers carry a second RX path and cannot use their own
buffer layout.

Add XDP_UMEM_EXPLICIT_RETURN, under which the device lays out buffers
in chunks as in pages and chunks come back explicitly. Back the bound
queue's RX page pools with the chunks through an AF_XDP memory
provider, so drivers need no AF_XDP code.

A chunk on FILL twice or outside the UMEM is an invalid entry. A chunk
counts against the page pool that took it, and against none in
userspace. FILL needs no lock, as only the queue's NAPI, or an
operation that runs only while that NAPI cannot use the rings,
refills.

Binding needs:
- a native XDP program attached through netlink (-ENOENT), which
  cannot be detached, as drivers would build skbs on the buffers;
- CAP_NET_ADMIN in the initial user namespace (-EPERM), as the
  provider changes what the program does;
- XDP_USE_NEED_WAKEUP (-EINVAL), as a queue that drained FILL needs a
  wakeup.

XDP_COPY or XDP_ZEROCOPY fails with -EOPNOTSUPP. Page pools whose ring
and cache could hold every chunk fail with -ENOSPC.

Unbind restarts the queue without the provider, or closes the device
if that fails, as the old queue may still use the UMEM.

UMEM registration accepts the flag two patches later.

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

Notes (v2changes):
    Changes in v2:
    - Rename from "xsk: Lend UMEM chunks to the page pools of a queue".
    - Rename XDP_UMEM_DRV_LAYOUT to XDP_UMEM_EXPLICIT_RETURN, and the sink
      to the AF_XDP memory provider in xsk_mp.c, after what changes for
      the application (Magnus).
    - Require XDP_USE_NEED_WAKEUP here, where bind is added, and drop the
      tests of whether a socket uses it (Magnus).
    - Require CAP_NET_ADMIN in the initial user namespace, and fail
      XDP_ZEROCOPY with -EOPNOTSUPP, as XDP_COPY already did. The check of
      both now sits next to the checks of the classic modes (Sashiko).
    - Charge a FILL chunk to the page pool that takes it, so that a
      restart while bound no longer leaves the new queue without buffers
      (Sashiko).
    - Refuse page pools with an init callback, as a chunk that a page pool
      takes back from FILL keeps its page pool info and would miss the
      callback.
    - Add the page-pool netlink attribute xsk, an empty nest that
      page-pool dumps show for this provider (Sashiko).
    - Keep the bind checks apart from those of classic zero copy, as
      sharing them takes more lines than it saves (Magnus).
    - Take FILL without a lock: only the owner of the queue, its NAPI or
      an operation that runs only while that NAPI cannot use the rings,
      refills, which the driver agrees to with
      PP_FLAG_ALLOW_XDP_ONLY_NETMEM.

 Documentation/netlink/specs/netdev.yaml |   8 +
 include/linux/netdevice.h               |   1 +
 include/net/xsk_buff_pool.h             |   3 +
 include/uapi/linux/if_xdp.h             |   8 +
 include/uapi/linux/netdev.h             |   1 +
 net/core/dev.c                          |  32 ++
 net/xdp/Kconfig                         |   1 +
 net/xdp/Makefile                        |   2 +-
 net/xdp/xsk_buff_pool.c                 |  36 +-
 net/xdp/xsk_mp.c                        | 443 ++++++++++++++++++++++++
 net/xdp/xsk_mp.h                        |  14 +
 tools/include/uapi/linux/if_xdp.h       |   8 +
 tools/include/uapi/linux/netdev.h       |   1 +
 13 files changed, 551 insertions(+), 7 deletions(-)
 create mode 100644 net/xdp/xsk_mp.c
 create mode 100644 net/xdp/xsk_mp.h

diff --git a/Documentation/netlink/specs/netdev.yaml b/Documentation/netlink/specs/netdev.yaml
index 0de7f93f0c4c..cf07ea7f0727 100644
--- a/Documentation/netlink/specs/netdev.yaml
+++ b/Documentation/netlink/specs/netdev.yaml
@@ -206,6 +206,13 @@ attribute-sets:
         doc: io-uring memory provider information.
         type: nest
         nested-attributes: io-uring-provider-info
+      -
+        name: xsk
+        doc: |
+          The page pool takes its buffers from the UMEM of an AF_XDP
+          socket with explicit chunk return.
+        type: nest
+        nested-attributes: xsk-info
   -
     name: page-pool-info
     subset-of: page-pool
@@ -678,6 +685,7 @@ operations:
             - detach-time
             - dmabuf
             - io-uring
+            - xsk
       dump:
         request:
           attributes:
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 9e542d793df7..c1fde6420940 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -4460,6 +4460,7 @@ u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode);
 
 u32 dev_get_min_mp_channel_count(const struct net_device *dev);
 bool dev_mp_blocks_xdp(const struct net_device *dev);
+bool dev_xdp_drv_prog_held(struct net_device *dev);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h
index ef4bbf7f66b9..e26162f956c4 100644
--- a/include/net/xsk_buff_pool.h
+++ b/include/net/xsk_buff_pool.h
@@ -11,6 +11,7 @@
 #include <net/xdp.h>
 
 struct xsk_buff_pool;
+struct xsk_mp;
 struct xdp_rxq_info;
 struct xsk_cb_desc;
 struct xsk_queue;
@@ -54,6 +55,8 @@ struct xsk_buff_pool {
 	spinlock_t xsk_tx_list_lock;
 	refcount_t users;
 	struct xdp_umem *umem;
+	/* RX with explicit return, see net/xdp/xsk_mp.c */
+	struct xsk_mp *mp;
 	struct work_struct work;
 	/* Protects generic receive in shared and non-shared umem mode. */
 	spinlock_t rx_lock;
diff --git a/include/uapi/linux/if_xdp.h b/include/uapi/linux/if_xdp.h
index 50d67df78911..60f01759a1d4 100644
--- a/include/uapi/linux/if_xdp.h
+++ b/include/uapi/linux/if_xdp.h
@@ -45,6 +45,14 @@
  */
 #define XDP_UMEM_TX_METADATA_LEN	(1 << 2)
 
+/* With explicit return, the kernel and the driver decide where packets
+ * land in a chunk, how many share it and the headroom: userspace only
+ * supplies chunks on FILL. An RX descriptor does not give its chunk
+ * back, and a packet may span chunks. Needs aligned page-sized chunks
+ * and no headroom.
+ */
+#define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
+
 struct sockaddr_xdp {
 	__u16 sxdp_family;
 	__u16 sxdp_flags;
diff --git a/include/uapi/linux/netdev.h b/include/uapi/linux/netdev.h
index 9026e0414fbe..0e2b4cdf1ef4 100644
--- a/include/uapi/linux/netdev.h
+++ b/include/uapi/linux/netdev.h
@@ -112,6 +112,7 @@ enum {
 	NETDEV_A_PAGE_POOL_DETACH_TIME,
 	NETDEV_A_PAGE_POOL_DMABUF,
 	NETDEV_A_PAGE_POOL_IO_URING,
+	NETDEV_A_PAGE_POOL_XSK,
 
 	__NETDEV_A_PAGE_POOL_MAX,
 	NETDEV_A_PAGE_POOL_MAX = (__NETDEV_A_PAGE_POOL_MAX - 1)
diff --git a/net/core/dev.c b/net/core/dev.c
index 9ca2bfedcab5..12e852cc3855 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10813,6 +10813,22 @@ static void dev_xdp_uninstall(struct net_device *dev)
 	dev->xdp_from_upper = 0;
 }
 
+/* A queue of @dev has an XDP-only memory provider */
+static bool dev_has_xdp_only_mp(const struct net_device *dev)
+{
+	const struct memory_provider_ops *ops;
+	unsigned int i;
+
+	netdev_assert_locked_ops(dev);
+	for (i = 0; i < dev->real_num_rx_queues; i++) {
+		ops = dev->_rx[i].mp_params.mp_ops;
+		if (ops && ops->flags & MP_XDP_ONLY)
+			return true;
+	}
+
+	return false;
+}
+
 static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
 			  struct bpf_xdp_link *link, struct bpf_prog *new_prog,
 			  struct bpf_prog *old_prog, u32 flags)
@@ -10881,6 +10897,12 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 		NL_SET_ERR_MSG(extack, "Active program does not match expected");
 		return -EEXIST;
 	}
+	if (cur_prog && !new_prog && !link && mode == XDP_MODE_DRV &&
+	    dev_has_xdp_only_mp(dev)) {
+		NL_SET_ERR_MSG(extack,
+			       "An XDP-only provider needs the program");
+		return -EBUSY;
+	}
 
 	/* put effective new program into new_prog */
 	if (link)
@@ -11190,6 +11212,16 @@ bool dev_mp_blocks_xdp(const struct net_device *dev)
 	return false;
 }
 
+/* A native program is attached, and dev_xdp_attach() keeps it while an
+ * AF_XDP socket receives with explicit return on @dev. A BPF link could go
+ * without asking, and so could a program that an upper device propagated.
+ */
+bool dev_xdp_drv_prog_held(struct net_device *dev)
+{
+	return dev_xdp_prog_id(dev, XDP_MODE_DRV) &&
+	       !dev_xdp_link(dev, XDP_MODE_DRV) && !dev->xdp_from_upper;
+}
+
 u32 dev_get_min_mp_channel_count(const struct net_device *dev)
 {
 	int i;
diff --git a/net/xdp/Kconfig b/net/xdp/Kconfig
index 71af2febe72a..c9c68d3b3712 100644
--- a/net/xdp/Kconfig
+++ b/net/xdp/Kconfig
@@ -2,6 +2,7 @@
 config XDP_SOCKETS
 	bool "XDP sockets"
 	depends on BPF_SYSCALL
+	select PAGE_POOL
 	default n
 	help
 	  XDP sockets allows a channel between XDP programs and
diff --git a/net/xdp/Makefile b/net/xdp/Makefile
index 30cdc4315f42..add6c7a7dec4 100644
--- a/net/xdp/Makefile
+++ b/net/xdp/Makefile
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o xskmap.o
-obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o
+obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o xsk_mp.o
 obj-$(CONFIG_XDP_SOCKETS_DIAG) += xsk_diag.o
diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
index d707fb308001..2cfe069e9959 100644
--- a/net/xdp/xsk_buff_pool.c
+++ b/net/xdp/xsk_buff_pool.c
@@ -7,6 +7,7 @@
 #include <net/xdp_sock_drv.h>
 
 #include "xsk_queue.h"
+#include "xsk_mp.h"
 #include "xdp_umem.h"
 #include "xsk.h"
 
@@ -69,9 +70,14 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
 	if (!pool)
 		goto out;
 
-	pool->heads = kvzalloc_objs(*pool->heads, umem->chunks);
-	if (!pool->heads)
-		goto out;
+	/* Receiving with explicit return and copy TX take none */
+	if (!(umem->flags & XDP_UMEM_EXPLICIT_RETURN)) {
+		pool->heads = kvzalloc_objs(*pool->heads, umem->chunks);
+		if (!pool->heads)
+			goto out;
+		pool->heads_cnt = umem->chunks;
+		pool->free_heads_cnt = umem->chunks;
+	}
 
 	if (xs->tx)
 		if (xp_alloc_tx_descs(pool, xs, max_segs))
@@ -79,8 +85,6 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
 
 	pool->chunk_mask = ~((u64)umem->chunk_size - 1);
 	pool->addrs_cnt = umem->size;
-	pool->heads_cnt = umem->chunks;
-	pool->free_heads_cnt = umem->chunks;
 	pool->headroom = umem->headroom;
 	pool->chunk_size = umem->chunk_size;
 	pool->chunk_shift = ffs(umem->chunk_size) - 1;
@@ -189,6 +193,12 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 	if (force_zc && force_copy)
 		return -EINVAL;
 
+	/* Explicit return lets the kernel place the packets, which is
+	 * neither of the classic modes
+	 */
+	if (umem_flags & XDP_UMEM_EXPLICIT_RETURN && (force_zc || force_copy))
+		return -EOPNOTSUPP;
+
 	if (pool->tx_sw_csum && (netdev->priv_flags & IFF_TX_SKB_NO_LINEAR))
 		return -EOPNOTSUPP;
 
@@ -219,6 +229,13 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 	 */
 	hlist_add_head(&pool->dev_node, &netdev->xsk_pools);
 
+	if (pool->umem->flags & XDP_UMEM_EXPLICIT_RETURN) {
+		err = xsk_mp_bind(pool, netdev, queue_id, flags);
+		if (err)
+			goto err_unbind;
+		return 0;
+	}
+
 	if (force_copy)
 		/* For copy-mode, we are done. */
 		return 0;
@@ -271,6 +288,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 err_unreg_pool:
 	if (!force_zc)
 		err = 0; /* fallback to copy mode */
+err_unbind:
 	if (err) {
 		pool->umem->flags = umem_flags;
 		xsk_clear_pool_at_qid(netdev, queue_id);
@@ -288,6 +306,9 @@ int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
 	struct xdp_umem *umem = umem_xs->umem;
 
 	flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
+	/* Explicit return has no copy mode */
+	if (umem->flags & XDP_UMEM_EXPLICIT_RETURN)
+		flags = 0;
 
 	if (umem->flags & XDP_UMEM_SG_FLAG)
 		flags |= XDP_USE_SG;
@@ -303,7 +324,10 @@ static void __xp_clear_dev(struct xsk_buff_pool *pool)
 	struct net_device *netdev = pool->netdev;
 
 	netdev_assert_locked_ops_compat(netdev);
-	xp_disable_drv_zc(pool);
+	if (pool->mp)
+		xsk_mp_unbind(pool);
+	else
+		xp_disable_drv_zc(pool);
 	xsk_clear_pool_at_qid(netdev, pool->queue_id);
 	hlist_del(&pool->dev_node);
 	pool->netdev = NULL;
diff --git a/net/xdp/xsk_mp.c b/net/xdp/xsk_mp.c
new file mode 100644
index 000000000000..0e33fd4d9fd5
--- /dev/null
+++ b/net/xdp/xsk_mp.c
@@ -0,0 +1,443 @@
+// SPDX-License-Identifier: GPL-2.0
+/* AF_XDP RX with explicit return
+ *
+ * Userspace hands page-sized UMEM chunks to the kernel through the FILL
+ * ring, and only XDP_RX_CHUNK_RETURNED gives them back. Each chunk
+ * becomes a net_iov of a page pool memory provider, and the device splits it
+ * as it splits pages: a chunk may hold several RX buffers, and a packet may
+ * span chunks.
+ *
+ * One context at a time owns the provider state of a queue: the queue's
+ * NAPI, in softirq, busy polling or its thread, or a driver operation that
+ * runs only while that NAPI cannot use the rings, as a queue restart or an
+ * error recovery does. A driver that lets the provider in agrees, see
+ * PP_FLAG_ALLOW_XDP_ONLY_NETMEM. It fills and drains its rings only while
+ * the queue's NAPI cannot use them, so the owner reads FILL and changes the
+ * chunk states without a lock. The exception is a page pool that goes
+ * away: it releases the chunks it never handed out from any context, onto
+ * a lock-free list.
+ *
+ * The kernel maps the chunks through the UMEM mapping, so XDP programs read
+ * and write the packets in place. The kernel stores none of its own data in
+ * the chunks: the shared info of a packet lives in the buffer pool, see
+ * %XDP_FLAGS_SINFO_EXT, and the core turns every verdict that would build an
+ * skb or a frame in the buffers into a drop.
+ */
+
+#include <linux/nospec.h>
+#include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
+#include <net/page_pool/helpers.h>
+#include <net/page_pool/memory_provider.h>
+#include <net/xdp_sock_drv.h>
+
+#include "../core/dev.h"
+#include "xsk_queue.h"
+#include "xsk_mp.h"
+
+#define XSK_MP_DMA_ATTR (DMA_ATTR_WEAK_ORDERING | DMA_ATTR_SKIP_CPU_SYNC)
+/* Chunks a refill tops up to with released ones */
+#define XSK_MP_RESERVE	32
+
+/* &net_iov.mp_state: whether userspace or the kernel has the chunk */
+enum {
+	XSK_MP_USER,
+	XSK_MP_LENT,
+};
+
+struct xsk_mp {
+	struct net_iov_area area;
+	struct device *dev;
+	struct xsk_buff_pool *pool;
+	/* NULL once no queue uses the provider */
+	struct netdev_rx_queue *rxq;
+	refcount_t refs;
+	/* NULL once no queue refills from the provider. Its consumer is the
+	 * owner of the queue, see the top of this file.
+	 */
+	struct xsk_queue *fq;
+	/* One node per chunk: chunks that page pools released, from any
+	 * context
+	 */
+	struct llist_node *nodes;
+	struct llist_head released;
+};
+
+static void xsk_mp_free(struct xsk_mp *mp)
+{
+	kvfree(mp->nodes);
+	kvfree(mp->area.niovs);
+	kfree(mp);
+}
+
+static void xsk_mp_put(struct xsk_mp *mp)
+{
+	if (refcount_dec_and_test(&mp->refs))
+		xsk_mp_free(mp);
+}
+
+static int xsk_mp_init(struct page_pool *pp)
+{
+	struct xsk_mp *mp = pp->mp_priv;
+
+	/* Each net_iov is one chunk, mapped for one device */
+	if (pp->p.order)
+		return -E2BIG;
+	if (pp->p.dev != mp->dev || pp->p.offset + pp->p.max_len > PAGE_SIZE)
+		return -EINVAL;
+	/* A chunk that its page pool takes back from FILL keeps the page
+	 * pool info, see xsk_mp_take_fill(), and would miss the callback
+	 */
+	if (pp->slow.init_callback)
+		return -EOPNOTSUPP;
+	/* Rings that keep every chunk posted leave none to replace the ones
+	 * the device filled: mlx5 posts about 16K pages per queue at MTU
+	 * 9000.
+	 */
+	if (pp->p.pool_size + PP_ALLOC_CACHE_SIZE >= mp->area.num_niovs)
+		return -ENOSPC;
+
+	refcount_inc(&mp->refs);
+	return 0;
+}
+
+static void xsk_mp_destroy(struct page_pool *pp)
+{
+	xsk_mp_put(pp->mp_priv);
+}
+
+/* Takes the chunks of FILL entries into the cache of @pp, which holds them
+ * from now on: a chunk that userspace has counts against no page pool. It
+ * keeps the info of the page pool it went back from, which takes it back
+ * cheaply. Returns the number of chunks taken. The owner of the queue is
+ * the only consumer of FILL.
+ */
+static u32 xsk_mp_take_fill(struct xsk_mp *mp, struct page_pool *pp,
+			    struct xsk_queue *fq)
+{
+	u32 i, entries, cons, mask, nr, n = 0, held = 0;
+	struct net_iov *niov, *niovs = mp->area.niovs;
+	struct xdp_umem_ring *ring;
+	netmem_ref netmem;
+	bool lent;
+	u64 addr;
+
+	/* One cache worth, bounding NAPI work */
+	entries = fq ? xskq_cons_nb_entries(fq, PP_ALLOC_CACHE_SIZE) : 0;
+	if (!entries)
+		return 0;
+
+	nr = mp->area.num_niovs;
+	ring = (struct xdp_umem_ring *)fq->ring;
+	mask = fq->ring_mask;
+	cons = fq->cached_cons;
+	for (i = 0; i < entries; i++) {
+		addr = ring->desc[(cons + i) & mask];
+		niov = &niovs[array_index_nospec((u32)(addr >> PAGE_SHIFT),
+						 nr)];
+		/* A chunk on FILL twice would be in two places at once. This
+		 * pairs with the release that gave it to userspace.
+		 */
+		lent = smp_load_acquire(&niov->mp_state) != XSK_MP_USER;
+		if (unlikely(addr >= (u64)nr << PAGE_SHIFT || lent)) {
+			fq->invalid_descs++;
+			continue;
+		}
+
+		WRITE_ONCE(niov->mp_state, XSK_MP_LENT);
+		netmem = net_iov_to_netmem(niov);
+		if (netmem_get_pp(netmem) == pp)
+			held++;
+		else
+			net_mp_niov_set_page_pool(pp, niov);
+		pp->alloc.cache[n++] = netmem;
+	}
+	net_mp_hold_page_pool(pp, held);
+
+	xskq_cons_release_n(fq, entries);
+	__xskq_cons_release(fq);
+	return n;
+}
+
+/* Tops up the cache of @pp, which holds @n chunks, with chunks that page
+ * pools released. A reserve, not a source. Each chunk it adds stays in
+ * circulation, so take only what FILL could not give. The owner of the
+ * queue is the only consumer of the list, as llist_del_first() needs.
+ */
+static u32 xsk_mp_take_released(struct xsk_mp *mp, struct page_pool *pp,
+				u32 n)
+{
+	struct llist_node *node;
+	struct net_iov *niov;
+
+	/* The page pool cache has 8 entries with 64K pages */
+	while (n < min(XSK_MP_RESERVE, PP_ALLOC_CACHE_SIZE)) {
+		node = llist_del_first(&mp->released);
+		if (!node)
+			break;
+		niov = &mp->area.niovs[node - mp->nodes];
+		net_mp_niov_set_page_pool(pp, niov);
+		pp->alloc.cache[n++] = net_iov_to_netmem(niov);
+	}
+	return n;
+}
+
+static netmem_ref xsk_mp_alloc_netmems(struct page_pool *pp, gfp_t gfp)
+{
+	struct xsk_mp *mp = pp->mp_priv;
+	/* The NAPI owns the need_wakeup flags */
+	bool napi = in_softirq() && !in_hardirq();
+	struct xsk_queue *fq;
+	dma_addr_t dma;
+	u32 i, n;
+
+	/* An owner outside the NAPI holds the instance lock. The pool keeps
+	 * its device for as long as the provider, while stop clears @rxq.
+	 */
+	lockdep_assert_once(napi || lockdep_is_held(&mp->pool->netdev->lock));
+
+	/* See xsk_mp_stop() */
+	fq = READ_ONCE(mp->fq);
+	/* The page pool asks only once its cache is empty */
+	n = xsk_mp_take_fill(mp, pp, fq);
+	n = xsk_mp_take_released(mp, pp, n);
+	/* Ask for a wakeup only while FILL runs dry, as zero copy drivers do:
+	 * a wakeup schedules the NAPI, which a busy poller would rather run
+	 * itself. Process context refills new queues only and leaves the
+	 * flag to bind and to their NAPI, which the driver runs once after a
+	 * start.
+	 */
+	if (napi && fq) {
+		if (n)
+			xsk_clear_rx_need_wakeup(mp->pool);
+		else
+			xsk_set_rx_need_wakeup(mp->pool);
+	}
+	if (fq && !n)
+		fq->queue_empty_descs++;
+	if (!n)
+		return 0;
+
+	for (i = 0; i < n; i++) {
+		dma = page_pool_get_dma_addr_netmem(pp->alloc.cache[i]);
+		dma_sync_single_range_for_device(pp->p.dev, dma, pp->p.offset,
+						 pp->p.max_len, pp->p.dma_dir);
+	}
+
+	pp->alloc.count = n - 1;
+	return pp->alloc.cache[n - 1];
+}
+
+static bool xsk_mp_release_netmem(struct page_pool *pp, netmem_ref netmem)
+{
+	struct net_iov *niov = netmem_to_net_iov(netmem);
+	struct xsk_mp *mp = pp->mp_priv;
+
+	net_mp_niov_clear_page_pool(niov);
+	llist_add(&mp->nodes[net_iov_idx(niov)], &mp->released);
+	return false;
+}
+
+static int xsk_mp_nl_fill(void *mp_priv, struct sk_buff *rsp,
+			  struct netdev_rx_queue *rxq)
+{
+	/* Queue dumps show NETDEV_A_QUEUE_XSK through the queue's pool */
+	if (rxq)
+		return 0;
+	return nla_put_empty_nest(rsp, NETDEV_A_PAGE_POOL_XSK);
+}
+
+/* No queue DMAs to the UMEM any longer: the queue restarted without the
+ * provider, or the device went away, so nothing refills from it
+ */
+static void xsk_mp_stop(struct xsk_mp *mp)
+{
+	struct xsk_queue *fq = mp->fq;
+
+	WRITE_ONCE(mp->rxq, NULL);
+	WRITE_ONCE(mp->fq, NULL);
+	/* Wait for wakeups that found the provider or the queue */
+	synchronize_net();
+	if (fq)
+		xp_dma_unmap(mp->pool, XSK_MP_DMA_ATTR);
+}
+
+/* Device unregister, after the queue stopped */
+static void xsk_mp_uninstall(void *mp_priv, struct netdev_rx_queue *rxq)
+{
+	memset(&rxq->mp_params, 0, sizeof(rxq->mp_params));
+	xsk_mp_stop(mp_priv);
+}
+
+static const struct memory_provider_ops xsk_mp_ops = {
+	.alloc_netmems	= xsk_mp_alloc_netmems,
+	.release_netmem	= xsk_mp_release_netmem,
+	.init		= xsk_mp_init,
+	.destroy	= xsk_mp_destroy,
+	.nl_fill	= xsk_mp_nl_fill,
+	.uninstall	= xsk_mp_uninstall,
+	.flags		= MP_XDP_ONLY,
+};
+
+static struct xsk_mp *xsk_mp_create(struct xsk_buff_pool *pool,
+				    struct device *dev)
+{
+	u32 i, nr = pool->umem->chunks;
+	struct net_iov *niov;
+	struct xsk_mp *mp;
+	dma_addr_t dma;
+
+	mp = kzalloc_obj(*mp, GFP_KERNEL_ACCOUNT);
+	if (!mp)
+		return ERR_PTR(-ENOMEM);
+
+	/* See &xdp_rxq_info.xdp_only_area */
+	BUILD_BUG_ON(offsetof(struct xsk_mp, area));
+
+	mp->area.niovs = kvcalloc(nr, sizeof(*niov), GFP_KERNEL_ACCOUNT);
+	mp->nodes = kvcalloc(nr, sizeof(*mp->nodes), GFP_KERNEL_ACCOUNT);
+	if (!mp->area.niovs || !mp->nodes) {
+		xsk_mp_free(mp);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	mp->area.num_niovs = nr;
+	/* Chunks are pages, see XDP_UMEM_EXPLICIT_RETURN */
+	mp->area.xdp_vaddr = pool->addrs;
+	mp->area.xdp_vsize = (size_t)nr << PAGE_SHIFT;
+	mp->area.xdp_shift = PAGE_SHIFT;
+	/* XDP runs on the queue only while the pool is bound, see
+	 * xsk_mp_unbind()
+	 */
+	mp->area.xdp_sinfo = &pool->sinfo;
+	mp->dev = dev;
+	mp->pool = pool;
+	mp->fq = pool->fq;
+	init_llist_head(&mp->released);
+	refcount_set(&mp->refs, 1);
+
+	/* Chunks are pages, see XDP_UMEM_EXPLICIT_RETURN */
+	for (i = 0; i < nr; i++) {
+		niov = &mp->area.niovs[i];
+		dma = pool->dma_pages[i] & ~XSK_NEXT_PG_CONTIG_MASK;
+		net_iov_init(niov, &mp->area, NET_IOV_XSK);
+		if (net_mp_niov_set_dma_addr(niov, dma)) {
+			xsk_mp_free(mp);
+			return ERR_PTR(-ERANGE);
+		}
+	}
+
+	return mp;
+}
+
+/* Sockets sharing the pool find no provider after the grace period of
+ * xsk_mp_stop(), see xsk_wakeup()
+ */
+static void xsk_mp_detach(struct xsk_buff_pool *pool)
+{
+	struct xsk_mp *mp = pool->mp;
+
+	WRITE_ONCE(pool->mp, NULL);
+	xsk_mp_stop(mp);
+	xsk_mp_put(mp);
+	static_branch_dec(&xdp_only_mp_key);
+}
+
+/**
+ * xsk_mp_bind - install the UMEM of @pool as the provider of an RX queue
+ * @pool: buffer pool with an XDP_UMEM_EXPLICIT_RETURN UMEM
+ * @dev: device of the queue
+ * @queue_id: RX queue
+ * @flags: bind flags
+ *
+ * Context: RTNL and the instance lock of @dev.
+ * Return: 0 on success. No copy mode takes over on error.
+ */
+int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
+		u16 queue_id, u16 flags)
+{
+	struct pp_memory_provider_params p = { .mp_ops = &xsk_mp_ops };
+	u32 needed = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
+	struct device *dma_dev;
+	struct xsk_mp *mp;
+	int err;
+
+	/* The provider changes what the program of the queue does to every
+	 * packet, and keeps the program from going
+	 */
+	if (!capable(CAP_NET_ADMIN))
+		return -EPERM;
+
+	/* Without a wakeup, a queue that used up the FILL ring stays dry */
+	if (!(flags & XDP_USE_NEED_WAKEUP))
+		return -EINVAL;
+
+	if (flags & XDP_USE_SG)
+		needed |= NETDEV_XDP_ACT_RX_SG;
+	/* The queue must receive, and the device must take the queue back
+	 * and run programs on what it gives the socket
+	 */
+	if (!dev->queue_mgmt_ops || (dev->xdp_features & needed) != needed ||
+	    queue_id >= dev->real_num_rx_queues)
+		return -EOPNOTSUPP;
+
+	/* The program of another device would see a leased queue's packets */
+	if (netif_rxq_is_leased(dev, queue_id))
+		return -EOPNOTSUPP;
+
+	/* The device of the queue's page pools, which may differ per queue */
+	dma_dev = netdev_queue_get_dma_dev(dev, queue_id, NETDEV_QUEUE_TYPE_RX);
+	if (!dma_dev)
+		return -EOPNOTSUPP;
+
+	/* Without a program, drivers would build skbs from the buffers */
+	if (!dev_xdp_drv_prog_held(dev))
+		return -ENOENT;
+
+	err = xp_dma_map(pool, dma_dev, XSK_MP_DMA_ATTR, pool->umem->pgs,
+			 pool->umem->npgs);
+	if (err)
+		return err;
+
+	mp = xsk_mp_create(pool, dma_dev);
+	if (IS_ERR(mp)) {
+		xp_dma_unmap(pool, XSK_MP_DMA_ATTR);
+		return PTR_ERR(mp);
+	}
+
+	/* Until the first refill of the NAPI: the queue may need FILL to
+	 * start. Set before the NAPI runs, which owns the flags after that.
+	 */
+	xsk_set_rx_need_wakeup(pool);
+
+	/* The core keeps XDP-only buffers to the socket once the queue runs */
+	static_branch_inc(&xdp_only_mp_key);
+	mp->rxq = __netif_get_rx_queue(dev, queue_id);
+	pool->mp = mp;
+	p.mp_priv = mp;
+	err = netif_mp_open_rxq(dev, queue_id, &p, NULL);
+	if (err) {
+		xsk_mp_detach(pool);
+		return err;
+	}
+
+	return 0;
+}
+
+/* Context: RTNL and the instance lock of the device */
+void xsk_mp_unbind(struct xsk_buff_pool *pool)
+{
+	struct pp_memory_provider_params p = {
+		.mp_ops = &xsk_mp_ops,
+		.mp_priv = pool->mp,
+	};
+	struct xsk_mp *mp = pool->mp;
+
+	/* Unregister may have uninstalled the provider already. A queue that
+	 * cannot restart without it may still use the UMEM: stop the device.
+	 */
+	if (READ_ONCE(mp->rxq) &&
+	    netif_mp_close_rxq(pool->netdev, pool->queue_id, &p))
+		netif_close(pool->netdev);
+	xsk_mp_detach(pool);
+}
diff --git a/net/xdp/xsk_mp.h b/net/xdp/xsk_mp.h
new file mode 100644
index 000000000000..e6c965bd3b2c
--- /dev/null
+++ b/net/xdp/xsk_mp.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* AF_XDP RX with explicit return, see xsk_mp.c */
+
+#ifndef XSK_MP_H_
+#define XSK_MP_H_
+
+struct net_device;
+struct xsk_buff_pool;
+
+int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
+		u16 queue_id, u16 flags);
+void xsk_mp_unbind(struct xsk_buff_pool *pool);
+
+#endif /* XSK_MP_H_ */
diff --git a/tools/include/uapi/linux/if_xdp.h b/tools/include/uapi/linux/if_xdp.h
index 50d67df78911..60f01759a1d4 100644
--- a/tools/include/uapi/linux/if_xdp.h
+++ b/tools/include/uapi/linux/if_xdp.h
@@ -45,6 +45,14 @@
  */
 #define XDP_UMEM_TX_METADATA_LEN	(1 << 2)
 
+/* With explicit return, the kernel and the driver decide where packets
+ * land in a chunk, how many share it and the headroom: userspace only
+ * supplies chunks on FILL. An RX descriptor does not give its chunk
+ * back, and a packet may span chunks. Needs aligned page-sized chunks
+ * and no headroom.
+ */
+#define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
+
 struct sockaddr_xdp {
 	__u16 sxdp_family;
 	__u16 sxdp_flags;
diff --git a/tools/include/uapi/linux/netdev.h b/tools/include/uapi/linux/netdev.h
index 9026e0414fbe..0e2b4cdf1ef4 100644
--- a/tools/include/uapi/linux/netdev.h
+++ b/tools/include/uapi/linux/netdev.h
@@ -112,6 +112,7 @@ enum {
 	NETDEV_A_PAGE_POOL_DETACH_TIME,
 	NETDEV_A_PAGE_POOL_DMABUF,
 	NETDEV_A_PAGE_POOL_IO_URING,
+	NETDEV_A_PAGE_POOL_XSK,
 
 	__NETDEV_A_PAGE_POOL_MAX,
 	NETDEV_A_PAGE_POOL_MAX = (__NETDEV_A_PAGE_POOL_MAX - 1)
-- 
2.56.1


  parent reply	other threads:[~2026-12-04 16:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:27 [PATCH net-next v2 00/15] xsk: Return UMEM chunks explicitly Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 01/15] xdp: Let memory providers expose net_iovs to XDP Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 02/15] net: Add memory providers that only feed XDP Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 03/15] page_pool: Let memory providers take back net_iovs Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 04/15] xdp: Run programs on XDP-only buffers Björn Töpel
2026-12-04 16:27 ` Björn Töpel [this message]
2026-12-04 16:27 ` [PATCH net-next v2 06/15] xsk: Deliver packets and return chunks explicitly Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 07/15] xsk: Receive with explicit chunk return Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 08/15] xsk: Drop references of a run of buffers at once Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 09/15] eth: fbnic: Receive into XDP-only memory providers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 10/15] net/mlx5e: Handle restarts with XDP-only providers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 11/15] net/mlx5e: Receive into XDP-only memory providers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 12/15] net/mlx5e: Release XDP-only pages earlier Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 13/15] net/mlx5e: Test for XDP-only pages per RQ Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 14/15] selftests: net: xsk: Test explicit chunk return Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 15/15] Documentation: xsk: Describe " 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=20261204162723.755897-6-bjorn@kernel.org \
    --to=bjorn@kernel.org \
    --cc=alexanderduyck@fb.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=cuitao@kylinos.cn \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=dw@davidwei.uk \
    --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=ilias.apalodimas@linaro.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=maciej.fijalkowski@intel.com \
    --cc=magnus.karlsson@intel.com \
    --cc=martin.lau@linux.dev \
    --cc=mbloch@nvidia.com \
    --cc=memxor@gmail.com \
    --cc=mike.marciniszyn@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=razor@blackwall.org \
    --cc=rdunlap@infradead.org \
    --cc=saeedm@nvidia.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=song@kernel.org \
    --cc=tariqt@nvidia.com \
    --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