Linux RDMA and InfiniBand development
 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 03/15] page_pool: Let memory providers take back net_iovs
Date: Fri,  4 Dec 2026 17:27:08 +0100	[thread overview]
Message-ID: <20261204162723.755897-4-bjorn@kernel.org> (raw)
In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org>

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@kernel.org>
---

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


  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 ` Björn Töpel [this message]
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 ` [PATCH net-next v2 08/15] xsk: Drop references of a run of buffers at once Björn Töpel
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-4-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