From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796401687; b=ONqpUvuHkSM11i91kHm4Cif1A1FPIRsHQKeb4ennG1mNfB3xIC0umGPUtiFy2xDWN41v ChKhBfK9Kj7zqBuobbqiiy5hCIZ0LYYlfdlDtxUiiJ5gL1jhBTBBqm+c8vFvZlQFlzfI6 X2yxMSAr47cV0WyYUFOlJcFO4rbNqCZVZWEt31/1t4DPpWRbUy8wsSz5LZH4Pa+Kddgor dedQ//nBigiSYRCD6Hi4kqM6kctWIodMJQyoBzUpShprpKbtWg1UnMtV7ekuonkq+2uiy iYZwRe92XDyixv3sk7/28xjrRC+0aQ1auKCdhFHuQaLIV3G2zI85GZrpUuJMVCCCZKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796401687; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=R9b6myiXtp1AvTbN5/C9XpdxEueumqt9wkWFwywdIBk=; b=R6y7Y6uqs+iyfJvAMiihMzUnPZQ134E7MQ/5IXgAAXVPhpFfzjYGA/hDek2o2kbGt6vj UWwzhR2u6Rv2BRB+7FHvhXgf9HztCJgZVYv8qUJOCyE7iQvUnzTlkdGGGi6iG8zGpdH1p hYnl3KaBFu8Rc3KGSelBNF/maKwubwmTTUGojBtUd52J8c3Gm5HHMEdLtIw2VpJAVQWSY OYwBgJ1mAVtnlPbAzYJOClnX1v8MUZpXbN7fKgfBwOzmBKjGzufPrNJDAEMGDfQwnSj0c WwxwZEJmg91lZN4mZzRiuK0AOJpORXrCPU4sV3VChPu1RkeSeWxIlroEC6gaK6AYDew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B+R2hSft; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C0E2606A330; Fri, 4 Dec 2026 16:28:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=B+R2hSft Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E98F1F0092C; Fri, 4 Dec 2026 16:27:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796401687; bh=R9b6myiXtp1AvTbN5/C9XpdxEueumqt9wkWFwywdIBk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=B+R2hSftk5CoK4F3895gZV/BN+xqMA4XNJ5vseFczsPV3iRYcM/8tDBGz/gSldAtC 1umwV9vHclz8qmyoyRjDI35vWnUqFzWXZPiXWm58i7MS8XwfrrW/q0xBD/SfrqL6J1 j2GG7RhCI2bjmVAWx4ss0OmQ/U3V4S6Xckq5uhnwjy74CZ9lX0CNKy9NZrOJYy1Al+ wdrJDs7Qti9Z2bKsMtw78t6J9YPQf3FOGKOgPOA9cHfuIiPNuvthkFYJdQ+t6q6QOj MoBr0/F8/UVUJsItJfcJyFN65q8WjFNJaopear9TpThZZg0YPKKER+TQX3NmuX3K0G QluqPTOG3Sxvw== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Andrii Nakryiko , Eduard Zingerman , Kumar Kartikeya Dwivedi , Nikolay Aleksandrov , Andrew Lunn , Ilias Apalodimas , Magnus Karlsson , Maciej Fijalkowski , Donald Hunter , Alexander Duyck , Saeed Mahameed , Tariq Toukan , Mark Bloch , Leon Romanovsky , Shuah Khan , Jonathan Corbet , netdev@vger.kernel.org, bpf@vger.kernel.org Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , Simon Horman , Stanislav Fomichev , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Wei , Tao Cui , linux-kernel@vger.kernel.org, kernel-team@meta.com, "Mike Marciniszyn (Meta)" , linux-rdma@vger.kernel.org, linux-kselftest@vger.kernel.org, Shuah Khan , Randy Dunlap , 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 Message-ID: <20261204162723.755897-4-bjorn@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204162723.755897-1-bjorn@kernel.org> References: <20261204162723.755897-1-bjorn@kernel.org> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- 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