Intel-Wired-Lan Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@google.com>
To: Alexander Lobakin <aleksander.lobakin@intel.com>,
	Tony Nguyen <anthony.l.nguyen@intel.com>,
	 Przemek Kitszel <przemyslaw.kitszel@intel.com>,
	Jesper Dangaard Brouer <hawk@kernel.org>,
	 Ilias Apalodimas <ilias.apalodimas@linaro.org>
Cc: Brian Vazquez <brianvv@google.com>,
	David Decotigny <decot@google.com>, Li Li <boolli@google.com>,
	 Kuniyuki Iwashima <kuniyu@google.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>,
	intel-wired-lan@lists.osuosl.org
Subject: [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages.
Date: Sat,  5 Dec 2026 21:52:22 +0900	[thread overview]
Message-ID: <20261205125237.2290932-5-kuniyu@google.com> (raw)
In-Reply-To: <20261205125237.2290932-1-kuniyu@google.com>

Drivers using PP_FLAG_DMA_MAP under an IOMMU DMA-FQ domain keep
every Rx page DMA-mapped until all skbs release their references
back to the page_pool.

When many flows pin pages in socket queues, these mappings can
exhaust the 32-bit IOVA window.

Unmapping every page unconditionally once HW Rx finishes avoids
IOVA exhaustion, but incurs per-page IOMMU map/unmap overhead even
for healthy low-flow workloads whose in-flight pages fit easily
in the 32-bit IOVA space.

Let's allow drivers to unmap DMA before returning pages.

Drivers must opt in via page_pool_enable_dma_unmap() on a
DMA-coherent pool before allocating pages or starting NAPI, which
enables page_pool_dma_early_unmap so the map, recycle, and release
checks incur no overhead when unused.

Drivers can track active DMA mappings via
page_pool_dma_mapped_pages() and unmap pages early via
page_pool_dma_unmap_netmem() once pressure rises.

page_pool_dma_unmap_netmem() calls __page_pool_release_netmem_dma()
to unmap DMA with DMA_ATTR_SKIP_CPU_SYNC, clear dma_addr, and zero
the non-zero DMA index in pp_magic.

If the netmem is still attached as pool->frag_page, it also
detaches it via page_pool_free_frag() so remaining space is not
handed out unmapped.

page_pool_netmem_is_dma_mapped() and __page_pool_release_netmem_dma()
check netmem_get_dma_index() after smp_acquire__after_ctrl_dep().

It guarantees that pp_magic is loaded after page_ref_count() or
pp_ref_count on weakly ordered architectures (just a compiler barrier
on x86) so early-unmapped pages are returned rather than recycled
without repeating unmap.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 include/net/page_pool/helpers.h |  26 ++++++++
 include/net/page_pool/types.h   |  16 +++++
 net/core/page_pool.c            | 107 ++++++++++++++++++++++++++++----
 3 files changed, 138 insertions(+), 11 deletions(-)

diff --git a/include/net/page_pool/helpers.h b/include/net/page_pool/helpers.h
index 28635ce9454e..cabb0eb7442f 100644
--- a/include/net/page_pool/helpers.h
+++ b/include/net/page_pool/helpers.h
@@ -505,6 +505,32 @@ static inline void page_pool_nid_changed(struct page_pool *pool, int new_nid)
 		page_pool_update_nid(pool, new_nid);
 }
 
+/* Calculate distance between two u32 values, valid if distance is below 2^(31)
+ *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
+ */
+static inline s32 page_pool_distance(u32 hold, u32 release)
+{
+	return (s32)(hold - release);
+}
+
+/**
+ * page_pool_dma_mapped_pages() - get approximate number of DMA-mapped pages
+ * @pool: queried page pool
+ *
+ * Counts pages that remain DMA-mapped by @pool, excluding pages already
+ * unmapped early via page_pool_dma_unmap_netmem().  Requires
+ * page_pool_enable_dma_unmap() to be enabled on @pool.
+ *
+ * Return: approximate number of DMA-mapped pages held by @pool.
+ */
+static inline u32 page_pool_dma_mapped_pages(const struct page_pool *pool)
+{
+	u32 unmap_cnt = atomic_read(&pool->dma_unmap_cnt);
+	u32 map_cnt = READ_ONCE(pool->dma_map_cnt);
+
+	return max_t(s32, 0, page_pool_distance(map_cnt, unmap_cnt));
+}
+
 /**
  * page_pool_is_unreadable() - will allocated buffers be unreadable for the CPU
  * @pool: queried page pool
diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h
index 5306e34105eb..89bde9df0872 100644
--- a/include/net/page_pool/types.h
+++ b/include/net/page_pool/types.h
@@ -188,6 +188,7 @@ struct page_pool {
 #ifdef CONFIG_PAGE_POOL_STATS
 	bool system:1;			/* This is a global percpu pool */
 #endif
+	bool dma_unmap:1;		/* Early DMA unmapping enabled */
 
 	__cacheline_group_begin_aligned(frag, PAGE_POOL_FRAG_GROUP_ALIGN);
 	long frag_users;
@@ -220,6 +221,7 @@ struct page_pool {
 	 * on a single CPU (see napi_schedule).
 	 */
 	struct pp_alloc_cache alloc ____cacheline_aligned_in_smp;
+	u32 dma_map_cnt;
 
 	/* Data structure for storing recycled pages.
 	 *
@@ -261,6 +263,9 @@ struct page_pool {
 		ktime_t detach_time;
 		u32 id;
 	} user;
+
+	/* Separated from pages_state_release_cnt by slow/user fields */
+	atomic_t dma_unmap_cnt;
 };
 
 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp);
@@ -284,6 +289,8 @@ void page_pool_destroy(struct page_pool *pool);
 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *),
 			   const struct xdp_mem_info *mem);
 void page_pool_put_netmem_bulk(netmem_ref *data, u32 count);
+void page_pool_enable_dma_unmap(struct page_pool *pool);
+void page_pool_dma_unmap_netmem(struct page_pool *pool, netmem_ref netmem);
 #else
 static inline void page_pool_destroy(struct page_pool *pool)
 {
@@ -298,6 +305,15 @@ static inline void page_pool_use_xdp_mem(struct page_pool *pool,
 static inline void page_pool_put_netmem_bulk(netmem_ref *data, u32 count)
 {
 }
+
+static inline void page_pool_enable_dma_unmap(struct page_pool *pool)
+{
+}
+
+static inline void page_pool_dma_unmap_netmem(struct page_pool *pool,
+					      netmem_ref netmem)
+{
+}
 #endif
 
 void page_pool_put_unrefed_netmem(struct page_pool *pool, netmem_ref netmem,
diff --git a/net/core/page_pool.c b/net/core/page_pool.c
index d7c88c0b67e5..cf2afa295282 100644
--- a/net/core/page_pool.c
+++ b/net/core/page_pool.c
@@ -33,6 +33,7 @@
 #include "page_pool_priv.h"
 
 DEFINE_STATIC_KEY_FALSE(page_pool_mem_providers);
+static DEFINE_STATIC_KEY_FALSE(page_pool_dma_early_unmap);
 
 #define DEFER_TIME (msecs_to_jiffies(1000))
 #define DEFER_WARN_INTERVAL (60 * HZ)
@@ -330,6 +331,9 @@ static void page_pool_uninit(struct page_pool *pool)
 		pool->mp_ops->destroy(pool);
 		static_branch_dec(&page_pool_mem_providers);
 	}
+
+	if (pool->dma_unmap)
+		static_branch_dec(&page_pool_dma_early_unmap);
 }
 
 /**
@@ -571,6 +575,10 @@ static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t g
 
 	page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len);
 
+	if (static_branch_unlikely(&page_pool_dma_early_unmap) &&
+	    pool->dma_unmap)
+		WRITE_ONCE(pool->dma_map_cnt, pool->dma_map_cnt + 1);
+
 	return true;
 
 unset_failed:
@@ -697,18 +705,13 @@ struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp)
 }
 EXPORT_SYMBOL(page_pool_alloc_pages);
 
-/* Calculate distance between two u32 values, valid if distance is below 2^(31)
- *  https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution
- */
-#define _distance(a, b)	(s32)((a) - (b))
-
 s32 page_pool_inflight(const struct page_pool *pool, bool strict)
 {
 	u32 release_cnt = atomic_read(&pool->pages_state_release_cnt);
 	u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt);
 	s32 inflight;
 
-	inflight = _distance(hold_cnt, release_cnt);
+	inflight = page_pool_distance(hold_cnt, release_cnt);
 
 	if (strict) {
 		trace_page_pool_release(pool, inflight, hold_cnt, release_cnt);
@@ -752,7 +755,23 @@ static __always_inline void __page_pool_release_netmem_dma(struct page_pool *poo
 	if (!pool->dma_map)
 		return;
 
-	__page_pool_unmap_netmem_dma(pool, netmem);
+	if (static_branch_unlikely(&page_pool_dma_early_unmap) &&
+	    pool->dma_unmap) {
+		/* Pair with page_pool_dma_unmap_netmem() / releasing CPU
+		 * before reading pp_magic in netmem_get_dma_index().
+		 */
+		smp_acquire__after_ctrl_dep();
+
+		if (PP_DMA_INDEX_BITS && !netmem_get_dma_index(netmem))
+			return;
+
+		__page_pool_unmap_netmem_dma(pool, netmem);
+
+		atomic_inc(&pool->dma_unmap_cnt);
+	} else {
+		__page_pool_unmap_netmem_dma(pool, netmem);
+	}
+
 	page_pool_set_dma_addr_netmem(netmem, 0);
 	if (likely(PP_DMA_INDEX_BITS))
 		netmem_set_dma_index(netmem, 0);
@@ -823,11 +842,28 @@ static bool page_pool_recycle_in_cache(netmem_ref netmem,
 	return true;
 }
 
-static bool __page_pool_page_can_be_recycled(netmem_ref netmem)
+static bool page_pool_netmem_is_dma_mapped(struct page_pool *pool,
+					   netmem_ref netmem)
+{
+	if (!static_branch_unlikely(&page_pool_dma_early_unmap) ||
+	    !pool->dma_unmap)
+		return true;
+
+	/* Pair with page_pool_dma_unmap_netmem() / releasing CPU
+	 * before reading pp_magic in netmem_get_dma_index().
+	 */
+	smp_acquire__after_ctrl_dep();
+
+	return netmem_get_dma_index(netmem) != 0;
+}
+
+static bool __page_pool_page_can_be_recycled(struct page_pool *pool,
+					     netmem_ref netmem)
 {
 	return netmem_is_net_iov(netmem) ||
 	       (page_ref_count(netmem_to_page(netmem)) == 1 &&
-		!page_is_pfmemalloc(netmem_to_page(netmem)));
+		!page_is_pfmemalloc(netmem_to_page(netmem)) &&
+		page_pool_netmem_is_dma_mapped(pool, netmem));
 }
 
 /* If the page refcnt == 1, this will try to recycle the page.
@@ -851,7 +887,7 @@ __page_pool_put_page(struct page_pool *pool, netmem_ref netmem,
 	 * page is NOT reusable when allocated when system is under
 	 * some pressure. (page_is_pfmemalloc)
 	 */
-	if (likely(__page_pool_page_can_be_recycled(netmem))) {
+	if (likely(__page_pool_page_can_be_recycled(pool, netmem))) {
 		/* Read barrier done in page_ref_count / READ_ONCE */
 
 		page_pool_dma_sync_for_device(pool, netmem, dma_sync_size);
@@ -1041,7 +1077,7 @@ 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_page_can_be_recycled(netmem)) {
+	if (__page_pool_page_can_be_recycled(pool, netmem)) {
 		page_pool_dma_sync_for_device(pool, netmem, -1);
 		return netmem;
 	}
@@ -1063,6 +1099,55 @@ static void page_pool_free_frag(struct page_pool *pool)
 	page_pool_return_netmem(pool, netmem);
 }
 
+/**
+ * page_pool_enable_dma_unmap() - enable early DMA unmapping for a pool
+ * @pool: page pool that may call page_pool_dma_unmap_netmem()
+ *
+ * Must be called on a DMA-coherent pool before allocating pages or starting
+ * NAPI polling.
+ */
+void page_pool_enable_dma_unmap(struct page_pool *pool)
+{
+	if (unlikely(!PP_DMA_INDEX_BITS || !pool->dma_map || pool->mp_ops ||
+		     dma_dev_need_sync(pool->p.dev) ||
+		     READ_ONCE(pool->pages_state_hold_cnt)))
+		return;
+
+	if (pool->dma_unmap)
+		return;
+
+	pool->dma_unmap = true;
+	static_branch_inc(&page_pool_dma_early_unmap);
+}
+EXPORT_SYMBOL(page_pool_enable_dma_unmap);
+
+/**
+ * page_pool_dma_unmap_netmem() - unmap a DMA-mapped netmem early
+ * @pool: page pool owning @netmem
+ * @netmem: network memory reference to unmap
+ *
+ * Unmaps @netmem with %DMA_ATTR_SKIP_CPU_SYNC and clears the stored DMA
+ * address to 0 before it is returned to @pool so long-lived skb queues do
+ * not pin IOMMU IOVAs.  If @netmem is still attached as @pool->frag_page,
+ * clears @pool->frag_page so remaining space is not handed out unmapped.
+ * No-op if dma_dev_need_sync() is true.  Caller must have enabled early
+ * unmapping via page_pool_enable_dma_unmap(), ensure no hardware DMA remains
+ * pending on @netmem (including other fragments of the same page), hold a
+ * reference on @netmem, and run in NAPI/control context.
+ */
+void page_pool_dma_unmap_netmem(struct page_pool *pool, netmem_ref netmem)
+{
+	if (unlikely(!pool->dma_unmap || dma_dev_need_sync(pool->p.dev) ||
+		     netmem_is_net_iov(netmem)))
+		return;
+
+	__page_pool_release_netmem_dma(pool, netmem);
+
+	if (unlikely(pool->frag_page == netmem))
+		page_pool_free_frag(pool);
+}
+EXPORT_SYMBOL(page_pool_dma_unmap_netmem);
+
 netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,
 				       unsigned int *offset, unsigned int size,
 				       gfp_t gfp)
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-05 12:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 12:52 [PATCH v2 iwl-next 0/5] idpf: Avoid IOVA exhaustion under DMA-FQ IOMMU Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 1/5] idpf: Reuse idpf_rx_update_bufq_desc() in idpf_rx_post_buf_desc() Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 2/5] idpf: Use coherent DMA buffer for Rx header split without XDP Kuniyuki Iwashima
2026-12-05 12:52 ` [PATCH v2 iwl-next 3/5] netmem: Annotate data-races around pp_magic Kuniyuki Iwashima
2026-12-05 12:52 ` Kuniyuki Iwashima [this message]
2026-12-05 13:15   ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages sashiko-bot
2026-12-05 12:52 ` [PATCH v2 iwl-next 5/5] idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ IOMMU Kuniyuki Iwashima
2026-12-05 13:16   ` sashiko-bot

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=20261205125237.2290932-5-kuniyu@google.com \
    --to=kuniyu@google.com \
    --cc=aleksander.lobakin@intel.com \
    --cc=anthony.l.nguyen@intel.com \
    --cc=boolli@google.com \
    --cc=brianvv@google.com \
    --cc=decot@google.com \
    --cc=hawk@kernel.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=intel-wired-lan@lists.osuosl.org \
    --cc=kuni1840@gmail.com \
    --cc=przemyslaw.kitszel@intel.com \
    /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