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=1796401732; b=y/ZlcFRcMp9QxccN0qBW5tYtmO2d8rBr/0x8jinaF/rJ9iwbQ9Buh1kpSkMFvjIcjFFZ P04IRivWX1KFtlxDYvVeE2Ta8YfNv1/95KZYex6u/PMYCwWc3tg90HXZhEsUAj/eaEyzh lgLIjcLZwwK+/tHseDpS6eZnfFYielFVVUQlcKN5u2hr4KyEr/fg1LaEb+VjOig75zfLn 4rqgECVsDtbLnUQqtHB5jN+QO+ekwF+cLuNOSaYlT8+wXHDyS31TP7Z0Ot5QIQWcQvV0a VzoyS/wMfuI0pNE0gtLeN72ZLxtT9PZts9Edyo9gWIUQjfHT8WpeMudiEiChphdefQA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401732; 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=4eT4AJfBiOEC78eXDiKba4QRC1we8TmgQRYWvWtit2M=; b=cyh0JbTk+TKCOdziRFcexyhRQ36O5wUn/aqIPf7UUSonejiFDzl+5KeGmyi5jbVQtRTQ accO3woCeEUn/bQnUoBWd7LOjHekUGToBTX1R9M+j3vwXlCQ1Efjx8Pv707oPWjhNPQi/ tMjIqf4XoUFilcJ0Iz+UwKxm/cRNt5jvnHAzEzSg4Cev//985b+gx+bgND/r+Qle0CJqK bAEaOWZBn7yael7ZH+Htv1yCIPQX4Qwe1jvax2/WbD+sPrsdhbfqqO7N/X7aRXnnAud6B 5D0xz4eB3axNPs07R1f2EiTMfm6AtsnuiiuV3e3QGyF44Zb88ItCkP9cPusTZxn9aVQ== 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=f9+xj9cx; 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 E92CC606A338; Fri, 4 Dec 2026 16:28:51 +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=f9+xj9cx Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1648A1F00A08; Fri, 4 Dec 2026 16:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401731; bh=4eT4AJfBiOEC78eXDiKba4QRC1we8TmgQRYWvWtit2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=f9+xj9cx+35LBtRYZXA7wX/+xGm2nyQ7QwufZWAUqbd3WipqmS6Q3WgeYpbK62McB Yn8jjSbJFJHJsn9GbXCzsd2hWiPdeVpU4lLgPqLVSet0fNeVPdZO8c3twE3fpG6f+7 hkET70tvgQJgAFoj8vw3MamNlxwOyZL2I5M3TcUaZA2i/OvvHJGUIAROMkcqWLeXu2 4xldqWSdYn/xxmC3GLt5KzoNGf+U3edUMKcsGY3y1fUxZgt0hXGG8GC/jwTETsRNJN d6FqGHhJxEFXofJCpI4VqjwRpY+58lCjmoNh3C6NCV/am+oFVgmvgqnnA+ok5c8wIJ 3nb/kihhmTR/g== 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 08/15] xsk: Drop references of a run of buffers at once Date: Fri, 4 Dec 2026 17:27:13 +0100 Message-ID: <20261204162723.755897-9-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: netdev@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, the AF_XDP provider drops the page pool reference of a buffer inside the redirect, before the driver drops its own. A driver that lets go of a page after its last buffer still holds the chunk then, so the chunk returns later, on an extra empty descriptor. A return in the middle of a poll also shows the RX ring and wakes the reader. Drop the references of a run of buffers from one chunk together, when a buffer comes from another chunk or at the NAPI flush. A chunk that this frees returns on the descriptor of the last buffer of the run. A return while a run is pending ends the run first. Returns wait for the flush of a poll that delivered to the socket, and go out at once otherwise. Only the queue NAPI delivers, so a run needs no lock and never outlives its poll. A receiver that goes during the poll still gets its run dropped before its RX ring goes. Signed-off-by: Björn Töpel --- Notes (v2changes): Changes in v2: - New in v2, reworked from "xsk: Drop the references of a run of buffers at once" of the v1 full series. The socket dropped its reference to a buffer inside the redirect, before the driver was done with the page, so a chunk that the driver lets go of after its last buffer still came back on an extra empty descriptor. A return while a run is pending ends the run first, so the run's last descriptor is the last one reserved, and returns of a poll that delivered to the socket wait for its flush. net/xdp/xsk.c | 2 +- net/xdp/xsk_mp.c | 81 +++++++++++++++++++++++++++++++++++++----------- net/xdp/xsk_mp.h | 2 +- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/net/xdp/xsk.c b/net/xdp/xsk.c index 33d4cbee658d..2e37f3b2f82f 100644 --- a/net/xdp/xsk.c +++ b/net/xdp/xsk.c @@ -468,7 +468,7 @@ static void xsk_flush(struct xdp_sock *xs) struct xsk_mp *mp = xs->pool->mp; if (mp) - xsk_mp_flush(mp); + xsk_mp_flush(mp, xs); xskq_prod_submit(xs->rx); /* The provider releases FILL as it takes it, see xsk_mp_take_fill() */ if (!mp) diff --git a/net/xdp/xsk_mp.c b/net/xdp/xsk_mp.c index fa5e715233ad..4cf205fbe2e6 100644 --- a/net/xdp/xsk_mp.c +++ b/net/xdp/xsk_mp.c @@ -8,9 +8,11 @@ * span chunks. Each buffer of a packet that XDP redirects to the socket * becomes one RX descriptor, at the address the device chose. * - * The socket drops the page pool reference of each buffer it takes. A chunk - * goes back to userspace once nothing in the kernel uses it, if a buffer of - * it reached the socket: the descriptor of the buffer that freed it carries + * The socket drops the page pool references of the buffers it takes, those + * of a run of buffers of one chunk together, once a buffer of another chunk + * comes or the NAPI poll ends. A chunk goes back to userspace once nothing + * in the kernel uses it, if a buffer of it reached the socket: the last + * descriptor of the run that freed it carries * XDP_RX_CHUNK_RETURNED, or, when the device lets go of the chunk later, an * empty descriptor at the chunk does: * @@ -84,6 +86,13 @@ struct xsk_mp { * file. */ struct xsk_queue *fq; + /* Chunk of the last buffers the socket took in this NAPI poll, and + * their number. Their references are dropped together, see + * xsk_mp_drop_pend(). Set only from the socket taking a buffer until + * the flush of that poll, or a return in between. + */ + struct net_iov *pend; + u32 pend_nr; /* Page pool of the chunks that went back to userspace and still count * against it, and their number, see xsk_mp_returned() */ @@ -282,19 +291,24 @@ static void xsk_mp_publish(struct xsk_mp *mp, struct xdp_sock *xs) xs->sk.sk_data_ready(&xs->sk); } -/* Drops the reference of the buffer of @niov that the socket took at the - * last RX descriptor. Only the owner of the queue changes the count of a - * chunk that delivers to the socket, so this needs no atomic. +/* Drops the references of the buffers the socket took from one chunk. Only + * the owner of the queue changes the count of a chunk that delivers to the + * socket, so this needs no atomic. */ -static void xsk_mp_drop_buf(struct xsk_mp *mp, struct xdp_sock *xs, - struct net_iov *niov) +static void xsk_mp_drop_pend(struct xsk_mp *mp, struct xdp_sock *xs) { + struct net_iov *niov = mp->pend; struct xdp_rxtx_ring *ring; atomic_long_t *count; long left; + if (!niov) + return; + + mp->pend = NULL; count = netmem_get_pp_ref_count_ref(net_iov_to_netmem(niov)); - left = atomic_long_read(count) - 1; + left = atomic_long_read(count) - mp->pend_nr; + mp->pend_nr = 0; if (left > 0) { /* The device still uses it: its last put hands it off, onto * a descriptor that the RX ring keeps room for @@ -307,8 +321,9 @@ static void xsk_mp_drop_buf(struct xsk_mp *mp, struct xdp_sock *xs, return; } - /* Free: back to userspace with this descriptor, with the count of a - * free netmem + /* Free: back to userspace with the last descriptor of the run, with + * the count of a free netmem. No return comes between a run and its + * end, see xsk_mp_hand_off(), so that is the last one reserved. */ atomic_long_set(count, 1); mp->owed -= niov->mp_hand_off; @@ -321,6 +336,17 @@ static void xsk_mp_drop_buf(struct xsk_mp *mp, struct xdp_sock *xs, XDP_RX_CHUNK_RETURNED; } +/* The socket takes a buffer of @niov at the next RX descriptor */ +static void xsk_mp_take_buf(struct xsk_mp *mp, struct xdp_sock *xs, + struct net_iov *niov) +{ + if (mp->pend != niov) { + xsk_mp_drop_pend(mp, xs); + mp->pend = niov; + } + mp->pend_nr++; +} + static netmem_ref xsk_mp_alloc_netmems(struct page_pool *pp, gfp_t gfp) { struct xsk_mp *mp = pp->mp_priv; @@ -384,6 +410,7 @@ static bool xsk_mp_hand_off(struct page_pool *pp, netmem_ref netmem) { struct net_iov *niov = netmem_to_net_iov(netmem); struct xsk_mp *mp = pp->mp_priv; + struct net_iov *pending; struct xdp_sock *xs; niov->mp_hand_off = 0; @@ -405,8 +432,19 @@ static bool xsk_mp_hand_off(struct page_pool *pp, netmem_ref netmem) return true; } + /* A NAPI poll that delivered to the socket shows the descriptor with + * its flush, as when fbnic cleans its rings after its packets or a + * program drops a packet. A pending run ends first, so that its last + * descriptor stays the last of the run. A NAPI after its flush, as + * mlx5 when it reposts, shows the descriptor at once, and so does an + * owner outside the NAPI, which runs only while the NAPI cannot. + * Only the NAPI of the queue puts the socket on its flush list. + */ + pending = mp->pend; + xsk_mp_drop_pend(mp, xs); xsk_mp_return(mp, xs, niov); - xsk_mp_publish(mp, xs); + if (!pending && !xs->flush_node.prev) + xsk_mp_publish(mp, xs); rcu_read_unlock(); return true; } @@ -760,10 +798,11 @@ static int xsk_mp_rcv(struct xsk_mp *mp, struct xdp_sock *xs, goto drop; /* Room for the packet, and for a return of every chunk that the - * kernel owes after it, so returns never wait: those it owes, and one - * per buffer, as the device may keep the chunk of each + * kernel owes after it, so returns never wait: those it owes, the + * pending one, and one per buffer, as each may start a run whose + * chunk the device keeps */ - k = 2 * n + mp->owed; + k = 2 * n + 1 + mp->owed; if (xskq_prod_nb_free(xs->rx, k) < k) { xs->rx_queue_full++; return -ENOBUFS; @@ -782,9 +821,9 @@ static int xsk_mp_rcv(struct xsk_mp *mp, struct xdp_sock *xs, skb_frag_off(frag); len = skb_frag_size(frag); } + xsk_mp_take_buf(mp, xs, niov); __xskq_prod_reserve_desc(xs->rx, addr, len, i + 1 < n ? XDP_PKT_CONTD : 0); - xsk_mp_drop_buf(mp, xs, niov); } if (!head_desc) { @@ -800,13 +839,19 @@ static int xsk_mp_rcv(struct xsk_mp *mp, struct xdp_sock *xs, /** * xsk_mp_flush - give back chunks at the end of a NAPI poll - * @mp: provider of a socket that received in this poll + * @mp: provider of @xs + * @xs: socket that received in this poll + * + * @xs comes from the flush list, as the provider may have dropped it as + * its receiving socket during the poll, see xsk_mp_del_sock(). Its RX + * ring stays until the poll ends. * * Context: the queue NAPI, from xdp_do_flush(), before the RX ring is * submitted. */ -void xsk_mp_flush(struct xsk_mp *mp) +void xsk_mp_flush(struct xsk_mp *mp, struct xdp_sock *xs) { + xsk_mp_drop_pend(mp, xs); xsk_mp_flush_returned(mp); } diff --git a/net/xdp/xsk_mp.h b/net/xdp/xsk_mp.h index b2e3909f282f..3a83b615396b 100644 --- a/net/xdp/xsk_mp.h +++ b/net/xdp/xsk_mp.h @@ -13,7 +13,7 @@ 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); int xsk_mp_wakeup(struct xsk_mp *mp); -void xsk_mp_flush(struct xsk_mp *mp); +void xsk_mp_flush(struct xsk_mp *mp, struct xdp_sock *xs); void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs); void xsk_mp_del_sock(struct xsk_mp *mp, struct xdp_sock *xs); bool xsk_mp_rx_taken(struct xsk_mp *mp); -- 2.56.1