Article View: linux.kernel.netdev
Article #27030[PATCH net-next v2 08/15] xsk: Drop references of a run of buffers at once
From: Björn Töpel
Date: Fri, 04 Dec 2026 16:27
Date: Fri, 04 Dec 2026 16:27
237 lines
17561 bytes
17561 bytes
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 <bjorn@kernel.org>
---
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
Technical Details
Message-ID:
<20261204162723.755897-9-bjorn@kernel.org>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377490-linux-kernel-netdev=dewy.ruReferences:
<20261204162723.755897-1-bjorn@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377490-linux-kernel-netdev=dewy.ru From: bjorn@kernel.org (Björn Töpel) Newsgroups: linux.kernel.netdev 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 Organization: Usenet Network Message-ID: <20261204162723.755897-9-bjorn@kernel.org> References: <20261204162723.755897-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="2807014"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:9NTlXrLPyz+dbn8GkG4AQVUJ5HI= sha256:T2OeDdHd+rlQrzuJoeMAR77TWp8Uoa6gi+MKZRLPbpw= sha1:V0NbU1ZyBaAy90WbiNcCNC2+AL4= sha256:PX+0QUO9NuEuvJPH7b6H3QcOjkZuH2pHJ4jBMMt8UVw= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id BDEF4117C6C; Fri, 4 Dec 2026 19:30:18 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.542 X-Spam-Status: No, score=-3.542 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.142, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id GbXKbkeIyBdX; Fri, 4 Dec 2026 19:30:16 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-377490-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id E8495117C5C; Fri, 4 Dec 2026 19:30:07 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id F40C9A7744; Fri, 4 Dec 2026 16:29:02 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796401742; b=unxTor/hv2EEdSETRBeqlXJs7UwPZ5aXXrWYSW61pwCS3cZnsx5O5BWwT1OIvw+tK8dJ qpeXea+BlPiz8nNlWpdXc+knfy8aP4sy18oFb6vHflLZ27yp1Vpnh3kAjVyrnC2+cRtMI EBgrlrwdeEuOzvTdeKFjj9Dz5+ICnJOFe6eyTiBq26LtAWcBqA57ItAhn4Bp2QyinB3Ji qmw26s0Cj60Mm9csNkph5trm679lk2X4fnx99GEe2r6QuwaKw7+puCc7vOq+aq7+uOsHI ivcz+wKqXSKl14MwZ6Q9/8qdPq4J/INClmgJCJPjOKUj/mA7455kxFVPdwBDGuV+V+A== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401742; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=4eT4AJfBiOEC78eXDiKba4QRC1we8TmgQRYWvWtit2M=; b=WOtcsgESHtsZac9B7LIrWjHv2+rkBH1KjhFDScfxzg73D467K53dUzFyE9xTIo82u1lr 7b9NaDUR41BlZjGegS8mXRtBjJIEPCipmn3TGPcr8E+m9lQaSWL3PT8gjsbn6RcQ0tPWq VJ1EaNncMTFVGyzTqGMP6O4PIJDiQq+MDwZcQpxMNy7fbC4X3YRIZsgmAwswJhtZWVenI nnPRZ/YCKq5mLTwgJtrs/Hot1GTtFvxbsi5GfCu+PDBPEQLxuajdJhj878Ne+0Ia38/17 dlLl/fpmztuqEdyAyzc1tunDltWAFOGnFCIr6e4KNAkyA8DtUlTiC9z0oodM2XX9KIQ== X-ARC-Authentication-Results: i=2; 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=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CB59D606A333; Fri, 4 Dec 2026 16:29:02 +0000 (UTC) X-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 X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-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== X-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== X-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 X-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) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1648A1F00A08; Fri, 4 Dec 2026 16:28:42 +0000 (UTC) X-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== X-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 X-Mailer: git-send-email 2.56.1 X-In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org> X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list