[PATCH net-next v2 08/15] xsk: Drop references of a run of buffers at once

From: Björn Töpel

Date: Fri Dec 04 2026 - 11:29:50 EST


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@xxxxxxxxxx>
---

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