From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401705; b=HdO0HE0hwa7234j2n9sVzuNXrBEXgTgva+5/BYLDto57Kkk546tf6zAR53GkT8T4Yfaz 7hH9AAutBzw6ju7Eh3RgR6oWHg/ayb0pRZ4wuErgM/NXuiJgYTUyk6V/xXMAMll61/TiT 6r443kAJmFqN8YUcLoGytiNU5xk4cZ/2fA5NG2x1oyZB0930oUHO38zDLXhmkeii8Z5XU rlOCdU6t2439EfzQ8IQknozZcIjR4dYY/XVhmR9WNJwEE12rrpXXkdT2j8xD+RXlp6528 wDD/09LW4fhIdtu4lmzmo3wVjP93FD9tjDH6Ga+BYqWIwdbEy6sJmVujFuD+8pmVpmw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401705; 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=8vFFqsnKFFTS2Vc4TC84nTNMXmB0BnazU6lihkO9hBw=; b=hoPnkVnxD29yj70no9pH6ry3qQsmD+wkJZ0m4ak7g3NUd/GGmSG9xgDOidf9QNCvhLhY tjXseJ+Ag1/XiVlYr1/v/eAwgWq8ycun7F241S1/wGVqxw3z8QztsKv6U9tESMXpuvAC3 xZpJ6M7qGBea6bTUOM8UdDgZI4CN3kWlJ9zp59VIQrVPLBix339U3chS+xWLclB6epbab x6Hzy26bOr2qEiBh8bcK6jI6fvWxKpZ7WsA2ScJN4WrJu97Zqq5yn8XcTRTdkc11fHxVq c6i2Y41BVh5XSLd/HSl0QrnLGDR28r8ZMI3S/A3Jq0j5GRGhv6VBfU4kilR+ydeg+Kw== 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=knhqIzac; arc=none smtp.remote-ip=100.103.45.18 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 0FE68606A335; Fri, 4 Dec 2026 16:28:25 +0000 (UTC) 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=knhqIzac Received: by smtp.kernel.org (Postfix) with ESMTPSA id 510461F00933; Fri, 4 Dec 2026 16:28:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401704; bh=8vFFqsnKFFTS2Vc4TC84nTNMXmB0BnazU6lihkO9hBw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=knhqIzacwkRHOAl4jSip9sqEs7a1dwA57WaupVpg1/PmSDPwknRrxpFxYw1154pQX 6/P8c7TCTYor+iq5FmACrnx05Ah1dvSlhTWsNXKRHaZpkDf4LhR9QlTxo7FF3YhxIU ugs6Z0RX/Nc8ZGyD1lmvM0vizEw0CaehYXq3jHrto+lT+8WDIlFCAd1w8RtVFGVWts pAyb/5CxJ/aC9DTqPspbR2wd/W5SKBCR65Rn7HD6H8hMQvSKuUpeBtUgFLlVbC2clG Wa5/08AQ6GQ0SG7vT+jcwxD8IxFpeKeNVTAfXQWeQzlu7ch0nIymAUeDWOz1kKDnbo DtBXbLUPvD0jQ== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Nikolay Aleksandrov , Andrew Lunn , Ilias Apalodimas , Magnus Karlsson , Maciej Fijalkowski , Donald Hunter , Alexander Duyck , Saeed Mahameed , Tariq Toukan , Mark Bloch , Leon Romanovsky , Shuah Khan , Jonathan Corbet , netdev@vger.kernel.org, bpf@vger.kernel.org Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Simon Horman , Stanislav Fomichev , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Wei , Tao Cui , linux-kernel@vger.kernel.org, kernel-team@meta.com, "Mike Marciniszyn (Meta)" , linux-rdma@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan , Randy Dunlap , 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 Message-ID: <20261204162723.755897-6-bjorn@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org> References: <20261204162723.755897-1-bjorn@kernel.org> X-Mailing-List: linux-rdma@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- 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 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 #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 +#include +#include +#include +#include +#include + +#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