From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F305606A31C for ; Mon, 30 Nov 2026 12:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=osuosl.org header.i=@osuosl.org header.a=rsa-sha256 header.s=default header.b=5mT71siU; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S3BiWOlJ Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 20D3A605E4; Mon, 30 Nov 2026 12:38:16 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id UNMxXwhwIKmk; Mon, 30 Nov 2026 12:38:15 +0000 (UTC) ARC-Filter: OpenARC Filter v1.3.2 smtp3.osuosl.org 367AD60708 Authentication-Results: smtp3.osuosl.org; arc=none smtp.remote-ip=140.211.166.142 ARC-Seal: i=1; d=osuosl.org; s=arc; a=rsa-sha256; cv=none; t=1796042295; b=mqo7sySnmSMMK//Cel0HUZeQZY9xhZ0CgyaTBcCt8InXCqzup2YQ3B//bAXojXUSDyRf aX7uhLwnlMAyKSuRYzFl6Qvx2z2Cc3Pnr3szL0E9L1U/27ZkOEaD6YuxWJS9qiwY9xlrK HZK+foNKCoaiDcEUE0zSRflu09e8vuwY1lM0R3UbnMmJXqx72EPjBnv8TqK2C2EqMiF81 Xe0w3Y+vz9/JwGSVMxnvnhUj6KaOTP0AZxjeg9kVg/OtgCl1L8wpSwlg6VaIdsAK7quao lOIfm//LSczL9Gt8TAmv52HAfvdgcjegLyvkYT6nZqwQxEQOhDkXo1Wfu/iN/ccXEZg== ARC-Message-Signature: i=1; d=osuosl.org; s=arc; a=rsa-sha256; c=relaxed/relaxed; t=1796042295; h=X-Comment:DKIM-Signature:X-Original-To:Delivered-To:Received: Received:X-Virus-Scanned:X-Spam-Flag:X-Spam-Score:X-Spam-Level: X-Spam-Status:Received:ARC-Filter:Received-SPF:Received:Received: Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding:X-BeenThere:X-Mailman-Version:Precedence: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help: List-Subscribe:Errors-To; bh=uDyiJ1iFm/JYSSNCJmxGMlTFg2OqeLVDj1kDhMzJWFM=; b=j1lHQyKyLCVPBmI/mPrXm+oBKo3TJmHxfshHIkmhZQ7UXqZmvuD+yW5EG0bcgd3EvcQL uGoHWrpkyJd6Fy+R8zyUGiQG2Bj804UJHnAikSZNt48XfYbskENVSrXtpPdg/3AOBcy3C XYeRwsjd8VaSfh/TAPmWvsF9vLvQFUq3LyIz9Qqou3HamAfaPV8payPH9Dx12kWyQkok4 90QD/3hIJe3GkN3h+vuTAJMKC6zx0sTNcguFhHfAJysWAf69Eh0pJ2NKDX6fg+4yZCO3/ HaExCXtinoxKQV/DkdlmHi68u/laZ8csRHlmrLJndlbUdzFm78hkja4/B8pZhmGlR9A== ARC-Authentication-Results: i=1; smtp3.osuosl.org; dmarc=pass header.from=kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S3BiWOlJ; arc=none smtp.remote-ip=140.211.166.142 X-Comment: SPF check N/A for local connections - client-ip=140.211.166.142; helo=lists1.osuosl.org; envelope-from=intel-wired-lan-bounces@osuosl.org; receiver= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osuosl.org; s=default; t=1796042295; bh=uDyiJ1iFm/JYSSNCJmxGMlTFg2OqeLVDj1kDhMzJWFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=5mT71siUncJfnwpIm5OK65tRbsdkdHEKFmmAxtfWl21ndKAq6YyYjbqLGW2oGlnJA VKpF/qUeEnbq5pxAsSpdZWJgiTobAur6KTR2oKtuWAi9jErsUEKwxnf85f6yYbWK/F fUfg8e9a+yb390E3C4WYhW7QtCWo2V6TvO1PkRy/eWmWohQ0VFcOOC8KH9FXM+NDHJ jxLLQiHli+m+DxMM3dZ4sAF8PsrGIViOOsCLtXP/djavoThmOVTSpBc3jr69MnKViX bEmZvFX7BTHpkJbRi/Ek5fieuUn3uxZ3qClceJ8sSdI6EoZEYSrfPGQQVnmnf9KTwy GCgZMvsfEGuQQ== Received: from lists1.osuosl.org (lists1.osuosl.org [140.211.166.142]) by smtp3.osuosl.org (Postfix) with ESMTP id 367AD60708; Mon, 30 Nov 2026 12:38:15 +0000 (UTC) Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists1.osuosl.org (Postfix) with ESMTP id EB78949D for ; Mon, 30 Nov 2026 12:38:13 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id E91FC60708 for ; Mon, 30 Nov 2026 12:38:13 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id hSGxIE0MYE_c for ; Mon, 30 Nov 2026 12:38:13 +0000 (UTC) ARC-Filter: OpenARC Filter v1.3.2 smtp3.osuosl.org E26E7605E4 Authentication-Results: smtp3.osuosl.org; arc=none smtp.remote-ip="2600:3c04:e001:324:0:1991:8:25" Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=2600:3c04:e001:324:0:1991:8:25; helo=tor.source.kernel.org; envelope-from=bjorn@kernel.org; receiver= Authentication-Results: smtp3.osuosl.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: smtp3.osuosl.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S3BiWOlJ Received: from tor.source.kernel.org (tor.source.kernel.org [IPv6:2600:3c04:e001:324:0:1991:8:25]) by smtp3.osuosl.org (Postfix) with ESMTPS id E26E7605E4 for ; Mon, 30 Nov 2026 12:38:12 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 3C5AF60CCA; Mon, 30 Nov 2026 12:38:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DC081F00897; Mon, 30 Nov 2026 12:38:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796042290; bh=uDyiJ1iFm/JYSSNCJmxGMlTFg2OqeLVDj1kDhMzJWFM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=S3BiWOlJD4mql77iqIHB2Rc3XmDgHVMxEZexV9pDywZ2IdLTl8g370cKQHW0Dqc9y XIRbzwzkAViTqp33GYzQXK7dCQCE2ft7NJz8/mja0wyrZeHQ4oB9V2wR+aScLqJTuk 4Fb/igsnhsrqXtlsGYMKjKSncKjzbIsqfs8wc5iZqYEgSEcWrjIMMw1bpbfNeOAhH/ dvBP5SuJ9g0X0djUkkI6tdSdjuYNJauK/M1Txqp9SgQkRM3kSTc0EgGz6ljVaqvK8J o8l8Ag8yinikdEEeJeKuQW7ckZsw9O4dTk9yh5yOhm+4mKrzseR1wKUFD1blb46utu LhNgg7tEMqPvw== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Magnus Karlsson , Maciej Fijalkowski , Alexander Lobakin , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , 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 , Tony Nguyen , Przemek Kitszel , intel-wired-lan@lists.osuosl.org, =?UTF-8?q?Cl=C3=A9ment=20Lecigne?= Subject: [PATCH net v2 4/4] xsk: Keep the shared info of zero-copy buffers in kernel memory Date: Mon, 30 Nov 2026 13:37:35 +0100 Message-ID: <20261130123737.125432-5-bjorn@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261130123737.125432-1-bjorn@kernel.org> References: <20261130123737.125432-1-bjorn@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: intel-wired-lan@osuosl.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Intel Wired Ethernet Linux Kernel Driver Development List-Archive: List-Post: List-Help: List-Subscribe: , X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 A packet that a driver receives in zero-copy mode over several buffers keeps its shared info, the fragment list and its count, in the tailroom of the UMEM chunk of its head. Userspace may write the UMEM at any time, also while the kernel walks the fragments, and a rewritten list or count makes the kernel read and write out of bounds. Keep the shared info in the buffer pool, which outlives its buffers and the queues that point to it, and let the device write up to the end of the chunk. One shared info per pool is enough, as a pool builds one multi-buffer packet at a time. libeth read it later, at an XDP_TX bulk flush, which an earlier patch of this series fixes. Every buffer of a pool carries the flag from the start, and xp_alloc() and xsk_buff_set_size() set it again, as a driver may clear the flags of the buffers it receives into. Multi-buffer pools now give the device the whole chunk, as single-buffer pools already do. Every UMEM accepts a larger headroom, also in copy mode, and bpf_xdp_adjust_tail() can grow a zero-copy packet up to the end of its chunk. Found by an AI code review agent, in its review of another series. Not triggered at runtime. Suggested-by: Maciej Fijalkowski Fixes: 24ea50127ecf ("xsk: support mbuf on ZC RX") Signed-off-by: Björn Töpel --- include/net/xdp.h | 32 ++++++++++++++++++++++++++------ include/net/xdp_sock_drv.h | 26 ++++++-------------------- include/net/xsk_buff_pool.h | 1 + net/core/filter.c | 2 +- net/xdp/xdp_umem.c | 3 +-- net/xdp/xsk_buff_pool.c | 20 ++++++++++++-------- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/include/net/xdp.h b/include/net/xdp.h index 07231adfb5f8..052402766988 100644 --- a/include/net/xdp.h +++ b/include/net/xdp.h @@ -65,6 +65,10 @@ struct xdp_rxq_info { u32 reg_state; struct xdp_mem_info mem; u32 frag_size; + /* Shared info of buffers with %XDP_FLAGS_SINFO_EXT, in the XSk buffer + * pool, set by xsk_pool_set_rxq_info(). + */ + struct skb_shared_info *ext_sinfo; } ____cacheline_aligned; /* perf critical, avoid false-sharing */ struct xdp_txq_info { @@ -81,6 +85,13 @@ enum xdp_buff_flags { * XDP program is not attached. */ XDP_FLAGS_FRAGS_UNREADABLE = BIT(2), + /* The shared info lives in &xdp_rxq_info.ext_sinfo, not at the end of + * the frame, as userspace may write the frame. The frame reserves no + * tailroom, so the data may reach its end. The pool builds one such + * buffer at a time, so a consumer that uses the shared info later + * must copy what it needs. + */ + XDP_FLAGS_SINFO_EXT = BIT(3), }; struct xdp_buff { @@ -178,17 +189,26 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, /* Reserve memory area at end-of data area. * - * This macro reserves tailroom in the XDP buffer by limiting the - * XDP/BPF data access to data_hard_end. Notice same area (and size) - * is used for XDP_PASS, when constructing the SKB via build_skb(). + * This reserves tailroom in the XDP buffer by limiting the XDP/BPF data + * access to data_hard_end. Notice same area (and size) is used for + * XDP_PASS, when constructing the SKB via build_skb(). Buffers with + * %XDP_FLAGS_SINFO_EXT keep the shared info elsewhere and reserve none. */ -#define xdp_data_hard_end(xdp) \ - ((xdp)->data_hard_start + (xdp)->frame_sz - \ - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) +static __always_inline void *xdp_data_hard_end(const struct xdp_buff *xdp) +{ + u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); + + if (xdp->flags & XDP_FLAGS_SINFO_EXT) + tailroom = 0; + return xdp->data_hard_start + xdp->frame_sz - tailroom; +} static inline struct skb_shared_info * xdp_get_shared_info_from_buff(const struct xdp_buff *xdp) { + if (xdp->flags & XDP_FLAGS_SINFO_EXT) + return READ_ONCE(xdp->rxq)->ext_sinfo; + return (struct skb_shared_info *)xdp_data_hard_end(xdp); } diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h index a40ad3f90f13..ec74bf3ffa51 100644 --- a/include/net/xdp_sock_drv.h +++ b/include/net/xdp_sock_drv.h @@ -41,11 +41,6 @@ static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool) return XDP_PACKET_HEADROOM + pool->headroom; } -static inline u32 xsk_pool_get_tailroom(bool mbuf) -{ - return mbuf ? SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) : 0; -} - static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool) { return pool->chunk_size; @@ -58,18 +53,7 @@ static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) { - u32 frame_size = __xsk_pool_get_rx_frame_size(pool); - struct xdp_umem *umem = pool->umem; - bool mbuf; - - /* Reserve tailroom only for zero-copy pools that opted into - * multi-buffer. The reserved area is used for skb_shared_info, - * matching the XDP core's xdp_data_hard_end() layout. - */ - mbuf = pool->dev && (umem->flags & XDP_UMEM_SG_FLAG); - frame_size -= xsk_pool_get_tailroom(mbuf); - - return ALIGN_DOWN(frame_size, 128); + return ALIGN_DOWN(__xsk_pool_get_rx_frame_size(pool), 128); } static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool) @@ -152,7 +136,6 @@ static inline void xsk_buff_free(struct xdp_buff *xdp) xp_free(pos); } - xdp_get_shared_info_from_buff(xdp)->nr_frags = 0; xdp_buff_clear_frags_flag(xdp); out: xp_free(xskb); @@ -226,8 +209,11 @@ static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size) xdp->data = xdp->data_hard_start + XDP_PACKET_HEADROOM; xdp->data_meta = xdp->data; xdp->data_end = xdp->data + size; - /* Frags flag is cleared when the frags of a head are released */ - xdp->flags &= XDP_FLAGS_HAS_FRAGS; + /* Frags flag is cleared when the frags of a head are released. The + * shared info stays in the pool, see xp_set_rxq_info(), also for a + * buffer whose flags a driver cleared. + */ + xdp->flags = (xdp->flags & XDP_FLAGS_HAS_FRAGS) | XDP_FLAGS_SINFO_EXT; } static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool, diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h index 02e89350faf9..3d0e065cc8a7 100644 --- a/include/net/xsk_buff_pool.h +++ b/include/net/xsk_buff_pool.h @@ -59,6 +59,7 @@ struct xsk_buff_pool { spinlock_t rx_lock; struct list_head free_list; struct list_head xskb_list; + struct skb_shared_info sinfo; u32 heads_cnt; u16 queue_id; diff --git a/net/core/filter.c b/net/core/filter.c index 53c042b267ab..3820c4edc14d 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4510,7 +4510,7 @@ BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset) return bpf_xdp_frags_increase_tail(xdp, offset); } - /* Notice that xdp_data_hard_end have reserved some tailroom */ + /* Notice that xdp_data_hard_end may have reserved some tailroom */ if (unlikely(data_end > data_hard_end)) return -EINVAL; diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c index cd8643360eb3..e78a43c3c3e9 100644 --- a/net/xdp/xdp_umem.c +++ b/net/xdp/xdp_umem.c @@ -203,8 +203,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) if (!unaligned_chunks && chunks_rem) return -EINVAL; - if (headroom > chunk_size - XDP_PACKET_HEADROOM - - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - 128) + if (headroom > chunk_size - XDP_PACKET_HEADROOM - 128) return -EINVAL; if (mr->flags & XDP_UMEM_TX_METADATA_LEN) { diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c index 5ec092f7cb6f..28bdc63551bf 100644 --- a/net/xdp/xsk_buff_pool.c +++ b/net/xdp/xsk_buff_pool.c @@ -107,6 +107,7 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, xskb = &pool->heads[i]; xskb->pool = pool; xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; + xskb->xdp.flags = XDP_FLAGS_SINFO_EXT; INIT_LIST_HEAD(&xskb->list_node); if (pool->unaligned) pool->free_heads[i] = xskb; @@ -125,8 +126,15 @@ void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) { u32 i; + /* Userspace may write the UMEM. Keep the shared info in the pool, + * and let the device write up to the end of each chunk. A NAPI may + * still run on the buffers, so publish the shared info first. + */ + rxq->ext_sinfo = &pool->sinfo; + /* Pairs with READ_ONCE(xdp->rxq) in xdp_get_shared_info_from_buff() */ + smp_wmb(); for (i = 0; i < pool->heads_cnt; i++) - pool->heads[i].xdp.rxq = rxq; + WRITE_ONCE(pool->heads[i].xdp.rxq, rxq); } EXPORT_SYMBOL(xp_set_rxq_info); @@ -229,12 +237,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool, segs = 1; } - /* open-code xsk_pool_get_rx_frame_size() as pool->dev is not - * set yet at this point; we are before getting down to driver - */ - frame_size = __xsk_pool_get_rx_frame_size(pool) - - xsk_pool_get_tailroom(mbuf); - frame_size = ALIGN_DOWN(frame_size, 128); + frame_size = xsk_pool_get_rx_frame_size(pool); if (needed > frame_size * segs) { err = -EINVAL; @@ -629,7 +632,8 @@ struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; xskb->xdp.data_meta = xskb->xdp.data; - xskb->xdp.flags &= XDP_FLAGS_HAS_FRAGS; + xskb->xdp.flags = (xskb->xdp.flags & XDP_FLAGS_HAS_FRAGS) | + XDP_FLAGS_SINFO_EXT; if (pool->dev) xp_dma_sync_for_device(pool, xskb->dma, pool->frame_len); -- 2.56.1