[PATCH net-next v2 03/15] page_pool: Let memory providers take back net_iovs
From: Björn Töpel
Date: Fri Dec 04 2026 - 11:28:18 EST
Currently, a provider learns of the last put of a net_iov only through
its release hook, which takes the net_iov out of its page pool. The
AF_XDP provider later in this series must see that put, as it returns
a chunk to userspace only once the device is done with it too.
Let a provider mark a net_iov so that its last put goes to the
provider's new hand_off() instead of the page pool cache. It may
decline, never runs in hard IRQ context, and for an XDP-only pool runs
in the one context that uses the queue's buffers. The cached net_iovs
of a pool that goes away are unmarked and never reach the provider.
Let a provider also count net_iovs off their page pool and back on in
bulk, keeping their page pool info, so that net_iovs in userspace pin
no pool.
Signed-off-by: Björn Töpel <bjorn@xxxxxxxxxx>
---
Notes (v2changes):
Changes in v2:
- Let a provider count net_iovs that it took back off their page
pool, and back on, in bulk (Sashiko).
- Add a helper that tests the mark (Sashiko).
- Say in the message that hand_off() may decline, that it never runs
in hard IRQ context, and that for an XDP-only pool it runs in the
one context that uses the buffers of the queue (Sashiko).
- Make the mark a u8.
include/net/netmem.h | 14 +++++++
include/net/page_pool/memory_provider.h | 3 ++
net/core/page_pool.c | 55 ++++++++++++++++++++++++-
3 files changed, 70 insertions(+), 2 deletions(-)
diff --git a/include/net/netmem.h b/include/net/netmem.h
index 4cf4ee1cd55d..5d3ec8554fb0 100644
--- a/include/net/netmem.h
+++ b/include/net/netmem.h
@@ -97,6 +97,9 @@ enum net_iov_type {
struct net_iov {
struct netmem_desc desc;
enum net_iov_type type;
+ u8 mp_hand_off;
+ /* Owned by the memory provider */
+ u8 mp_state;
struct net_iov_area *owner;
};
@@ -132,6 +135,8 @@ static inline void net_iov_init(struct net_iov *niov,
{
niov->owner = owner;
niov->type = type;
+ niov->mp_hand_off = 0;
+ niov->mp_state = 0;
}
/* netmem */
@@ -194,6 +199,15 @@ static inline struct net_iov *netmem_to_net_iov(netmem_ref netmem)
return NULL;
}
+/* A provider takes the netmem back at its last put, see
+ * &memory_provider_ops.hand_off. Never true for pages.
+ */
+static inline bool netmem_mp_hand_off(netmem_ref netmem)
+{
+ return netmem_is_net_iov(netmem) &&
+ netmem_to_net_iov(netmem)->mp_hand_off;
+}
+
static inline netmem_ref net_iov_to_netmem(struct net_iov *niov)
{
return (__force netmem_ref)((unsigned long)niov | NET_IOV);
diff --git a/include/net/page_pool/memory_provider.h b/include/net/page_pool/memory_provider.h
index ae300f9504d0..38bf292b8505 100644
--- a/include/net/page_pool/memory_provider.h
+++ b/include/net/page_pool/memory_provider.h
@@ -40,6 +40,7 @@ enum mp_flags {
struct memory_provider_ops {
netmem_ref (*alloc_netmems)(struct page_pool *pool, gfp_t gfp);
bool (*release_netmem)(struct page_pool *pool, netmem_ref netmem);
+ bool (*hand_off)(struct page_pool *pool, netmem_ref netmem);
int (*init)(struct page_pool *pool);
void (*destroy)(struct page_pool *pool);
int (*nl_fill)(void *mp_priv, struct sk_buff *rsp,
@@ -51,6 +52,8 @@ struct memory_provider_ops {
bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr);
void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov);
void net_mp_niov_clear_page_pool(struct net_iov *niov);
+void net_mp_release_page_pool(struct page_pool *pool, u32 nr);
+void net_mp_hold_page_pool(struct page_pool *pool, u32 nr);
int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
const struct pp_memory_provider_params *p,
diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index 8a1ea21ccdcd..03a03074e49c 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -840,6 +840,15 @@ static bool __page_pool_page_can_be_recycled(netmem_ref netmem)
!page_is_pfmemalloc(netmem_to_page(netmem)));
}
+/* A memory provider takes back a free net_iov it marked, see
+ * &memory_provider_ops.hand_off
+ */
+static bool page_pool_hand_off(struct page_pool *pool, netmem_ref netmem)
+{
+ return unlikely(netmem_mp_hand_off(netmem)) &&
+ pool->mp_ops->hand_off(pool, netmem);
+}
+
/* If the page refcnt == 1, this will try to recycle the page.
* If pool->dma_sync is set, we'll try to sync the DMA area for
* the configured size min(dma_sync_size, pool->max_len).
@@ -852,6 +861,10 @@ __page_pool_put_page(struct page_pool *pool, netmem_ref netmem,
{
lockdep_assert_no_hardirq();
+ /* The provider takes the net_iovs it marked */
+ if (page_pool_hand_off(pool, netmem))
+ return 0;
+
/* This allocator is optimized for the XDP mode that uses
* one-frame-per-page, but have fallbacks that act like the
* regular page allocator APIs.
@@ -1051,6 +1064,9 @@ static netmem_ref page_pool_drain_frag(struct page_pool *pool,
if (likely(page_pool_unref_netmem(netmem, drain_count)))
return 0;
+ if (page_pool_hand_off(pool, netmem))
+ return 0;
+
if (__page_pool_page_can_be_recycled(netmem)) {
page_pool_dma_sync_for_device(pool, netmem, -1);
return netmem;
@@ -1067,7 +1083,8 @@ static void page_pool_free_frag(struct page_pool *pool)
pool->frag_page = 0;
- if (!netmem || page_pool_unref_netmem(netmem, drain_count))
+ if (!netmem || page_pool_unref_netmem(netmem, drain_count) ||
+ page_pool_hand_off(pool, netmem))
return;
page_pool_return_netmem(pool, netmem);
@@ -1351,7 +1368,8 @@ bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr)
}
/* Associate a niov with a page pool. Should follow with a matching
- * net_mp_niov_clear_page_pool()
+ * net_mp_niov_clear_page_pool(), or net_mp_release_page_pool() for a niov
+ * that its provider takes back, see &memory_provider_ops.hand_off
*/
void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov)
{
@@ -1372,3 +1390,36 @@ void net_mp_niov_clear_page_pool(struct net_iov *niov)
page_pool_clear_pp_info(netmem);
}
+
+/**
+ * net_mp_release_page_pool() - account niovs that left their page pool
+ * @pool: page pool that the niovs were taken from
+ * @nr: number of niovs
+ *
+ * Accounts @nr free niovs that only the provider holds, as a hand-off
+ * leaves them, as released from @pool, as page_pool_return_netmem() would,
+ * without calling the provider. They keep their page pool info: the
+ * provider gives them to a page pool again with net_mp_niov_set_page_pool(),
+ * or to @pool with net_mp_hold_page_pool(). Not traced per niov.
+ *
+ * @pool may go away at once, as the niovs may have been its last.
+ */
+void net_mp_release_page_pool(struct page_pool *pool, u32 nr)
+{
+ atomic_add(nr, &pool->pages_state_release_cnt);
+}
+
+/**
+ * net_mp_hold_page_pool() - account niovs that their page pool takes again
+ * @pool: page pool whose info the niovs still have
+ * @nr: number of niovs
+ *
+ * Accounts @nr niovs that net_mp_release_page_pool() released from @pool as
+ * held by @pool again, as net_mp_niov_set_page_pool() would. Called where
+ * @pool allocates, from &memory_provider_ops.alloc_netmems. Not traced per
+ * niov.
+ */
+void net_mp_hold_page_pool(struct page_pool *pool, u32 nr)
+{
+ pool->pages_state_hold_cnt += nr;
+}
--
2.56.1