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=1796401696; b=WtSNIk8R90JoymypJAm6k8BiLMLej2KY4XBbKwLAuSTudLNn25ewsZ8VoU3AspgKAZUS Oz7UJ/SzGylZfPN3fBKVwfvH6TKQlpRZ6lwDLXbIPZV3giH9UvhwNWaYa5Nb6mfzaNTAt MR46aYyPEk44q6GgpU4S2vUlQcUjq+DQ4uSo8R7LpsFIagaxY/WuexwPxp3iHijaZMlLj i47LRUF/OjmA8b+ifVci9HxZKpWm7BQhKbZsI+Qa6GhiRZXe2+86TGoIxwrEncNYhIB+R DauPdC67sXIwfseO7UI8zQfaCfTwTq6aW/3/TwQ3YCSNsVcpERE3od+CZNApxf7TCjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401696; 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=n9HYx0ccBJzTsMRzi60ol3eN3/NUzLAwUmHuiLJjE74=; b=Gpf+bVB7Kn4CWdJNOyN3Ye0wWRQpz0c2NYdJoP/lk+gQaahCezhCaFwRf8TNkHZoUO/T 9QWIqZu0LDq5yffh2QApejxytWhKpGSHtQ+iIus3Is0URGiOPndRl8CQY6EpRqNajyDOB ZqNdLmS3uINqOxWqzZVv4Gbm0LJGNUU8zhBBsJ5nkvyuarlAR9datDJaOSA20uhPg17xw EvKehQnNnULfUs1Vye2k25+Xu5mkl0t7wW3BaBmjQOVOvDCj/Pikb0EzFoaGt+aTv6Rcj VGCN69Uu3tWhLpTkL+I7UBsmw4zZjRK9MNXRkHF/7ZOW6ouleMCz0ywBmna7+fWU+fA== 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=K2eA4Z38; 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 3FDAE606A32C; Fri, 4 Dec 2026 16:28:16 +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=K2eA4Z38 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 773EA1F000C2; Fri, 4 Dec 2026 16:28:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401695; bh=n9HYx0ccBJzTsMRzi60ol3eN3/NUzLAwUmHuiLJjE74=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=K2eA4Z38FMN7uZ8rhpj6zHy/4gbZlMqPQP/sJjoRQJ+NZLLK4zHQT4qxuqwcopY4H 76/cIAvlebaU9L7VkYymHNqimGkO2+Q1nHmxi7r5zB8yNUOQuC4YIYgRXPLxW91/hj RGzNfR72CaIe3JGVKxw42evCO71gbGS0Uw2l46UlEkVJjM9mktIJ3uf12egcpX/ch3 LHyjUBIDu50RKAUuwnVZjgOLJ3RmMMdza/CYNYZ1MtJvQ7KUIXwco9jX9IBEEmxQW8 aYPACIcgX1TFLNfqABKR+tS2k2+vZfOVp5zKjGuZoL7zMmS1VEJfI1++Z37gqrGNMz lJWq5nZrnqdtg== 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 04/15] xdp: Run programs on XDP-only buffers Date: Fri, 4 Dec 2026 17:27:09 +0100 Message-ID: <20261204162723.755897-5-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: bpf@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 The AF_XDP provider needs native XDP to steer packets and for the RX metadata kfuncs. XDP reads its UMEM chunks through the kernel mapping, but userspace may write them at any time. Currently, multi-buffer shared info, XDP_TX and redirect frames, and XDP_PASS skbs live in the buffer. Run programs on such XDP-only buffers, but keep kernel structures out of them. The core tells an XDP-only head by its address, with one range check under a static key, so other buffers pay nothing while no provider is bound. Such a buffer keeps its shared info in the provider. Registering such a page pool without its RX queue fails with -EOPNOTSUPP. The core turns XDP_TX and XDP_PASS of these buffers into XDP_DROP, so drivers need no checks. Frame and skb construction refuse these buffers, so devmap and cpumap drop them. The kernel takes bounds only from its own fields, and a helper that reads bytes twice must check a copy. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - Rename from "xdp: Run programs on buffers that must not hold kernel structures". - Keep the shared info of XDP-only buffers in the provider, with the queue only pointing to it. In v1 the queue owned it, which breaks for drivers that copy their queue info. - Take the frame and skb refusals of v1 patch 2. The rest of that patch went to net as a series of its own, which covers classic zero copy too (Maciej). The frame and skb paths of classic zero copy stay as they are. - XDP_PASS and XDP_TX stay drops in this posting. A follow-up adds them as copies (Maciej). - Call the buffers XDP-only rather than sink buffers, also in the names of their flag, test and static key. include/net/netmem.h | 9 ++++- include/net/page_pool/types.h | 7 ++-- include/net/xdp.h | 70 ++++++++++++++++++++++++++++++++++- net/core/xdp.c | 55 +++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 6 deletions(-) diff --git a/include/net/netmem.h b/include/net/netmem.h index 5d3ec8554fb0..9a3994fb12ab 100644 --- a/include/net/netmem.h +++ b/include/net/netmem.h @@ -68,6 +68,7 @@ DECLARE_STATIC_KEY_FALSE(page_pool_mem_providers); enum net_iov_type { NET_IOV_DMABUF, NET_IOV_IOURING, + NET_IOV_XSK, }; /* A memory descriptor representing abstract networking I/O vectors. @@ -81,7 +82,9 @@ enum net_iov_type { * CPU-readable or unreadable. New code must not assume net_iov implies * unreadable memory (check readability via netmem_address() or * skb_frags_readable() instead) and should, as much as possible, generalize - * existing limitations to match the design principles. + * existing limitations to match the design principles. AF_XDP memory provider + * net_iovs (%NET_IOV_XSK) are unreadable for the core and the stack as well: + * only XDP reads them, through their XDP view (&net_iov_area.xdp_vaddr). * * @pp_magic: pp field, similar to the one in struct page/struct * netmem_desc. @@ -115,6 +118,10 @@ struct net_iov_area { void *xdp_vaddr; size_t xdp_vsize; unsigned int xdp_shift; + /* Shared info of the XDP buffers of an MP_XDP_ONLY provider, in its + * kernel memory, see %XDP_FLAGS_SINFO_EXT + */ + struct skb_shared_info *xdp_sinfo; }; static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov) diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h index c9b9b738e8ba..4172e9aee28f 100644 --- a/include/net/page_pool/types.h +++ b/include/net/page_pool/types.h @@ -33,9 +33,10 @@ /* * Allow an XDP-only memory provider of the queue (%MP_XDP_ONLY) to back the - * pool. The driver registers the pool as the memory model of the RX queue, - * sets up XDP buffers with xdp_prepare_buff() at xdp_netmem_address() of - * their head, and puts heads back by netmem, not by address. + * pool. The driver registers the pool as the memory model of the RX queue + * with xdp_rxq_info_reg_mem_model(), sets up XDP buffers with + * xdp_prepare_buff() at xdp_netmem_address() of their head, and puts heads + * back by netmem, not by address. * * Such a provider takes no lock, so the driver also lets one context at a * time use the buffers of the queue: its NAPI, or an operation that runs diff --git a/include/net/xdp.h b/include/net/xdp.h index da4bb98fceeb..077d4b4b2614 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -65,8 +65,13 @@ struct xdp_rxq_info { u32 reg_state; struct xdp_mem_info mem; u32 frag_size; + /* Area of the MP_XDP_ONLY provider of the page pool, see + * xdp_mark_xdp_only() + */ + struct net_iov_area *xdp_only_area; /* Shared info of buffers with %XDP_FLAGS_SINFO_EXT, in the XSk buffer - * pool, set by xsk_pool_set_rxq_info(). + * pool, set by xsk_pool_set_rxq_info(), or where an XDP-only memory + * provider keeps it, set by xdp_rxq_info_reg_mem_model() */ struct skb_shared_info *ext_sinfo; } ____cacheline_aligned; /* perf critical, avoid false-sharing */ @@ -93,6 +98,15 @@ enum xdp_buff_flags { * must copy what it needs. */ XDP_FLAGS_SINFO_EXT = BIT(3), + /* XDP-only memory, see %MP_XDP_ONLY: the buffer may only go to the + * AF_XDP socket of its queue. Userspace may write it at any time, + * also while the kernel reads it. So the kernel takes bounds only + * from its own fields, and only the XDP program and its helpers read + * the bytes. A helper that checks bytes and reads them again later + * must check a copy. Nothing builds a frame or an skb in the + * buffer. + */ + XDP_FLAGS_MP_XDP_ONLY = BIT(4), }; struct xdp_buff { @@ -148,6 +162,14 @@ static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp) xdp->flags |= XDP_FLAGS_FRAGS_UNREADABLE; } +static __always_inline bool xdp_buff_is_xdp_only(const struct xdp_buff *xdp) +{ + return !!(xdp->flags & XDP_FLAGS_MP_XDP_ONLY); +} + +/* Enabled while an AF_XDP socket receives with explicit return */ +DECLARE_STATIC_KEY_FALSE(xdp_only_mp_key); + static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp) { return xdp->flags; @@ -176,6 +198,26 @@ xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq) #endif } +/* Drivers take the address of a head buffer with xdp_netmem_address(), + * which for an AF_XDP memory provider lies in its XDP view, the UMEM, see + * &net_iov_area.xdp_vaddr. Mark such buffers before the driver adds + * fragments: their shared info lives in the queue, and they may only go to + * the AF_XDP socket of the queue. + */ +static __always_inline void xdp_mark_xdp_only(struct xdp_buff *xdp) +{ + struct net_iov_area *area = xdp->rxq->xdp_only_area; + unsigned long off; + + off = (u8 *)xdp->data_hard_start - (u8 *)area->xdp_vaddr; + /* A byte-wide update would leave the next load of the flags two + * stores to merge, which x86 cannot forward + */ + if (off < area->xdp_vsize) + WRITE_ONCE(xdp->flags, xdp->flags | XDP_FLAGS_MP_XDP_ONLY | + XDP_FLAGS_SINFO_EXT); +} + /* 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 @@ -216,6 +258,9 @@ static __always_inline void *xdp_frag_address(const skb_frag_t *frag) return addr ? addr + skb_frag_off(frag) : NULL; } +/* Marks XDP-only buffers in @xdp->flags too: call it after + * xdp_init_buff(), which clears the flags + */ static __always_inline void xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, int headroom, int data_len, const bool meta_valid) @@ -226,6 +271,10 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, xdp->data = data; xdp->data_end = data + data_len; xdp->data_meta = meta_valid ? data : data + 1; + + if (static_branch_unlikely(&xdp_only_mp_key) && xdp->rxq && + unlikely(xdp->rxq->xdp_only_area)) + xdp_mark_xdp_only(xdp); } /* Reserve memory area at end-of data area. @@ -351,8 +400,12 @@ static inline bool xdp_buff_add_frag(struct xdp_buff *xdp, netmem_ref netmem, if (unlikely(netmem_is_pfmemalloc(netmem))) xdp_buff_set_frag_pfmemalloc(xdp); - if (unlikely(netmem_is_net_iov(netmem))) + if (unlikely(netmem_is_net_iov(netmem))) { xdp_buff_set_frag_unreadable(xdp); + /* XDP-only memory may only go to the socket of its queue */ + if (netmem_to_net_iov(netmem)->type == NET_IOV_XSK) + xdp->flags |= XDP_FLAGS_MP_XDP_ONLY; + } return true; } @@ -481,6 +534,13 @@ int xdp_update_frame_from_buff(const struct xdp_buff *xdp, return -ENOSPC; } + /* XDP-only buffers go to their AF_XDP socket, or nowhere. They + * exist only while the key is on. + */ + if (static_branch_unlikely(&xdp_only_mp_key) && + xdp_buff_is_xdp_only(xdp)) + return -EFAULT; + xdp_frame->data = xdp->data; xdp_frame->len = xdp->data_end - xdp->data; xdp_frame->headroom = headroom - sizeof(*xdp_frame); @@ -761,6 +821,12 @@ static __always_inline u32 bpf_prog_run_xdp(const struct bpf_prog *prog, */ u32 act = __bpf_prog_run(prog, xdp, BPF_DISPATCHER_FUNC(xdp)); + /* XDP-only buffers go to the AF_XDP socket of their queue only */ + if (static_branch_unlikely(&xdp_only_mp_key) && + xdp_buff_is_xdp_only(xdp) && + (act == XDP_PASS || act == XDP_TX)) + act = XDP_DROP; + if (static_branch_unlikely(&bpf_master_redirect_enabled_key)) { if (act == XDP_TX && netif_is_bond_slave(xdp->rxq->dev)) act = xdp_master_redirect(xdp); diff --git a/net/core/xdp.c b/net/core/xdp.c index 33a6740b411f..56b32ef795a0 100644 --- a/net/core/xdp.c +++ b/net/core/xdp.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -142,6 +143,14 @@ void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq) } xdp_unreg_mem_model(&xdp_rxq->mem); + /* Both belong to the provider, which may go away now. The pool of a + * classic zero-copy queue owns its shared info, so leave that + * pointer to xp_set_rxq_info(). + */ + if (xdp_rxq->xdp_only_area) { + xdp_rxq->xdp_only_area = NULL; + xdp_rxq->ext_sinfo = NULL; + } } EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model); @@ -332,11 +341,23 @@ static struct xdp_mem_allocator *__xdp_reg_mem_model(struct xdp_mem_info *mem, return ERR_PTR(errno); } +/* The page pool has an MP_XDP_ONLY provider */ +static bool xdp_mem_is_xdp_only(enum xdp_mem_type type, void *allocator) +{ + struct page_pool *pp = allocator; + + return type == MEM_TYPE_PAGE_POOL && pp && pp->mp_ops && + pp->mp_ops->flags & MP_XDP_ONLY; +} + int xdp_reg_mem_model(struct xdp_mem_info *mem, enum xdp_mem_type type, void *allocator) { struct xdp_mem_allocator *xdp_alloc; + if (xdp_mem_is_xdp_only(type, allocator)) + return -EOPNOTSUPP; + xdp_alloc = __xdp_reg_mem_model(mem, type, allocator); if (IS_ERR(xdp_alloc)) return PTR_ERR(xdp_alloc); @@ -344,16 +365,30 @@ int xdp_reg_mem_model(struct xdp_mem_info *mem, } EXPORT_SYMBOL_GPL(xdp_reg_mem_model); +DEFINE_STATIC_KEY_FALSE(xdp_only_mp_key); +EXPORT_SYMBOL_GPL(xdp_only_mp_key); + int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq, enum xdp_mem_type type, void *allocator) { struct xdp_mem_allocator *xdp_alloc; + struct net_iov_area *area = NULL; if (xdp_rxq->reg_state != REG_STATE_REGISTERED) { WARN(1, "Missing register, driver bug"); return -EFAULT; } + /* MP_XDP_ONLY providers put their net_iov_area first in mp_priv. As + * userspace may write XDP-only buffers, the provider keeps their shared + * info. + */ + if (xdp_mem_is_xdp_only(type, allocator)) { + area = ((struct page_pool *)allocator)->mp_priv; + if (WARN_ON_ONCE(!area->xdp_sinfo)) + return -EINVAL; + } + xdp_alloc = __xdp_reg_mem_model(&xdp_rxq->mem, type, allocator); if (IS_ERR(xdp_alloc)) return PTR_ERR(xdp_alloc); @@ -361,6 +396,22 @@ int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq, if (type == MEM_TYPE_XSK_BUFF_POOL && allocator) xsk_pool_set_rxq_info(allocator, xdp_rxq); + /* The queue only points to both, which live as long as the page + * pool. Copies of the queue info need no care. + */ + xdp_rxq->xdp_only_area = area; + if (area) { + xdp_rxq->ext_sinfo = area->xdp_sinfo; + /* The driver publishes the queue to its buffers after this, + * as mlx5 does when it starts the new channel, and a reader + * of a buffer takes the queue with READ_ONCE(xdp->rxq), as + * xdp_get_shared_info_from_buff() does: order the shared + * info of the queue before the queue reaches a buffer, the + * way xp_set_rxq_info() does for classic zero copy. + */ + smp_wmb(); + } + if (trace_mem_connect_enabled() && xdp_alloc) trace_mem_connect(xdp_alloc, xdp_rxq); return 0; @@ -638,6 +689,10 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp) u32 nr_frags = 0; int metalen; + /* XDP-only buffers go to their AF_XDP socket, or nowhere */ + if (unlikely(xdp_buff_is_xdp_only(xdp))) + 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