BPF List
 help / color / mirror / Atom feed
From: "Björn Töpel" <bjorn@kernel.org>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Nikolay Aleksandrov <razor@blackwall.org>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Magnus Karlsson <magnus.karlsson@intel.com>,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>,
	Donald Hunter <donald.hunter@gmail.com>,
	Alexander Duyck <alexanderduyck@fb.com>,
	Saeed Mahameed <saeedm@nvidia.com>,
	Tariq Toukan <tariqt@nvidia.com>, Mark Bloch <mbloch@nvidia.com>,
	Leon Romanovsky <leon@kernel.org>, Shuah Khan <shuah@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	netdev@vger.kernel.org, bpf@vger.kernel.org
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
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	[thread overview]
Message-ID: <20261204162723.755897-9-bjorn@kernel.org> (raw)
In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org>

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


  parent reply	other threads:[~2026-12-04 16:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:27 [PATCH net-next v2 00/15] xsk: Return UMEM chunks explicitly Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 01/15] xdp: Let memory providers expose net_iovs to XDP Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 02/15] net: Add memory providers that only feed XDP Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 03/15] page_pool: Let memory providers take back net_iovs Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 04/15] xdp: Run programs on XDP-only buffers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 05/15] xsk: Back a queue's page pools with UMEM chunks Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 06/15] xsk: Deliver packets and return chunks explicitly Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 07/15] xsk: Receive with explicit chunk return Björn Töpel
2026-12-04 16:27 ` Björn Töpel [this message]
2026-12-04 16:27 ` [PATCH net-next v2 09/15] eth: fbnic: Receive into XDP-only memory providers Björn Töpel
2026-12-04 22:34   ` Alexander Duyck
2026-12-04 16:27 ` [PATCH net-next v2 10/15] net/mlx5e: Handle restarts with XDP-only providers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 11/15] net/mlx5e: Receive into XDP-only memory providers Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 12/15] net/mlx5e: Release XDP-only pages earlier Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 13/15] net/mlx5e: Test for XDP-only pages per RQ Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 14/15] selftests: net: xsk: Test explicit chunk return Björn Töpel
2026-12-04 16:27 ` [PATCH net-next v2 15/15] Documentation: xsk: Describe " Björn Töpel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204162723.755897-9-bjorn@kernel.org \
    --to=bjorn@kernel.org \
    --cc=alexanderduyck@fb.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=corbet@lwn.net \
    --cc=cuitao@kylinos.cn \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=dw@davidwei.uk \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=emil@etsalapatis.com \
    --cc=hawk@kernel.org \
    --cc=horms@kernel.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=ilias.apalodimas@linaro.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kuba@kernel.org \
    --cc=leon@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=maciej.fijalkowski@intel.com \
    --cc=magnus.karlsson@intel.com \
    --cc=martin.lau@linux.dev \
    --cc=mbloch@nvidia.com \
    --cc=memxor@gmail.com \
    --cc=mike.marciniszyn@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=razor@blackwall.org \
    --cc=rdunlap@infradead.org \
    --cc=saeedm@nvidia.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=song@kernel.org \
    --cc=tariqt@nvidia.com \
    --cc=willemb@google.com \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox