[PATCH net-next 04/15] page_pool: Let memory providers take back net_iovs
From: Björn Töpel
Date: Tue Nov 24 2026 - 07:58:05 EST
Currently, the only provider hook on the last put of a net_iov is
release_netmem(), which takes it out of its page pool. The AF_XDP
sink, later in this series, gives a chunk back to userspace only once
the device is done with it too, so it must see that last put. With
release_netmem(), the pool would take the chunk in again each time
userspace lends it back.
Let a provider mark a net_iov so that its last put goes to the
provider's hand_off() instead of the page pool cache, still charged to
the pool, for the provider to put back later.
The hand-off also covers the fragment allocator draining a marked
net_iov. Providers that mark net_iovs must implement hand_off(), and
nothing changes for others, as the mark is clear by default. Also give
providers a byte of their own in the net_iov, in the cache line they
touch anyway.
Signed-off-by: Björn Töpel <bjorn@xxxxxxxxxx>
---
include/net/netmem.h | 8 ++++++++
include/net/page_pool/memory_provider.h | 6 ++++++
net/core/page_pool.c | 20 +++++++++++++++++++-
3 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/include/net/netmem.h b/include/net/netmem.h
index 4cf4ee1cd55d..1be7298ac6d9 100644
--- a/include/net/netmem.h
+++ b/include/net/netmem.h
@@ -97,6 +97,12 @@ enum net_iov_type {
struct net_iov {
struct netmem_desc desc;
enum net_iov_type type;
+ /* The last put hands it to its memory provider instead of the page
+ * pool cache, see &memory_provider_ops.hand_off
+ */
+ bool mp_hand_off;
+ /* Owned by the memory provider */
+ u8 mp_state;
struct net_iov_area *owner;
};
@@ -132,6 +138,8 @@ static inline void net_iov_init(struct net_iov *niov,
{
niov->owner = owner;
niov->type = type;
+ niov->mp_hand_off = false;
+ niov->mp_state = 0;
}
/* netmem */
diff --git a/include/net/page_pool/memory_provider.h b/include/net/page_pool/memory_provider.h
index d52c7a450c68..bfc284158222 100644
--- a/include/net/page_pool/memory_provider.h
+++ b/include/net/page_pool/memory_provider.h
@@ -36,6 +36,12 @@ 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);
+ /* Takes a free net_iov marked mp_hand_off that stays charged to the
+ * pool; the provider puts it back later. Returns false to have the
+ * pool recycle it instead. Called where the pool puts netmem, outside
+ * hard IRQ context. Needed by providers that mark net_iovs.
+ */
+ 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,
diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index 8a1ea21ccdcd..200b0569b9fe 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -840,6 +840,16 @@ 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 netmem_is_net_iov(netmem) &&
+ unlikely(netmem_to_net_iov(netmem)->mp_hand_off) &&
+ 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 +862,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 +1065,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 +1084,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);
--
2.56.1