Intel-Wired-Lan Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 iwl-next 0/5] idpf: Avoid IOVA exhaustion under DMA-FQ IOMMU.
@ 2026-12-05 12:52 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
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

Under an IOMMU DMA-FQ domain, page_pool keeps Rx pages DMA-mapped
until they are returned to the pool.

With many concurrent TCP flows, packets waiting in receive and
out-of-order queues during retransmissions pin thousands to
millions of payload pages in sockets, exhausting the 32-bit IOVA
space and causing alloc_iova() contention and Rx drops.

When running tcp_stream with 100000 flows,

  # tcp_stream -6 --suicide-length=2000 --min-rto=5 --num-threads=256 \
    --test-length=30 --num-flows=100000 --num-ports=20 -B 65536

on the server side, Rx payload pages pinned in socket queues saturate
the 32-bit IOVA window, causing every subsequent DMA mapping (including
Tx in idpf_tx_splitq_frame()) to contend on alloc_iova(), and perf
shows heavy contention with 13.8M dropped packets:

    21.62%  switchto-defaul  [kernel.kallsyms]   [k] queued_spin_lock_slowpath
            |
            ---queued_spin_lock_slowpath
               _raw_spin_lock_irqsave
               alloc_iova
               alloc_iova_fast
               iommu_dma_alloc_iova
               __iommu_dma_map
               __iommu_dma_map_phys
               iommu_dma_map_phys
               dma_map_phys
               idpf_tx_splitq_frame
               dev_hard_start_xmit
               sch_direct_xmit
               __qdisc_run

  # ethtool -S eth1 | grep -E 'ingress_drop3|rx-hsplit'
     rx-hsplit: 92006
     rx-hsplit_hbo: 393
     ingress_drop3.packets: 488310980   # before neper
     ingress_drop3.bytes: 1989178010650

     rx-hsplit: 27779097
     rx-hsplit_hbo: 611
     ingress_drop3.packets: 502138587   # after neper
     ingress_drop3.bytes: 2046991419840

  # dmesg
  [ 1291.279869] idpf 0000:01:00.0: Using 56-bit DMA addresses

On the client side, we can see many retransmissions, and the
throughput is 161 Gbps w/ 200G NIC.

  remote_throughput=161031856462

  TcpInSegs                       19235891           0.0
  TcpOutSegs                      225122216          0.0
  TcpRetransSegs                  20767366           0.0

This series fixes the issue by using a coherent DMA buffer for
fixed-size Rx headers and allowing page_pool pages to be unmapped
early in idpf once active DMA mappings exceed a per-queue threshold:

Patch 1 refactors idpf_rx_post_buf_desc() to reuse
idpf_rx_update_bufq_desc() ahead of the DMA changes.

Patch 2 allocates a coherent DMA buffer array per buffer queue
for Rx header split when an IOMMU DMA domain is active on a
DMA-coherent device without XDP, copying headers into napi_alloc_skb()
to avoid header page pinning and per-packet IOMMU mapping churn.

Patches 3-4 annotate concurrent accesses around pp_magic and add
opt-in page_pool early DMA unmapping (page_pool_enable_dma_unmap(),
page_pool_dma_mapped_pages(), and page_pool_dma_unmap_netmem()).

Patch 5 enables early DMA unmapping for idpf Rx payload queues
without XDP on DMA-coherent devices under IOMMU_DOMAIN_DMA_FQ.
Normal page_pool DMA caching is kept while active mappings remain
at or below a per-queue threshold (splitting a 2G IOVA budget
across all vports), and pages above the threshold are unmapped once
their descriptors complete.

With this series on the same 100k-flow workload, server-side Rx
drops fall from 13.8M to ~460K (steady state), client retransmissions
drop by ~8x (from 20.8M to ~2.5M), throughput recovers from 161 Gbps
to 177 Gbps mean with zero 56-bit DMA fallback, and single-flow
same-CPU tcp_stream (1 queue) shows no regression (97.35 Gbps vs
97.35 Gbps baseline, compared to 68.49 Gbps / -29.6% with v1).

Note: Two pre-existing Rx header split overflow fixes (in
idpf_rx_hsplit_wa() and idpf_rx_rsc()) are sent separately to
iwl-net:

  https://lore.kernel.org/intel-wired-lan/20261205124319.2264197-1-kuniyu@google.com/


Changes:
  v2:
    * Replace v1's unconditional DMA unmapping (page_pool
      dma_sync_for_cpu and libeth fq.no_dma_map patches, which
      regressed single-flow throughput by ~30%) with opt-in
      page_pool early DMA unmapping (patches 3-4) and
      threshold-based unmapping in idpf (patch 5)
    * Coalesce contiguous payload frags and count dropped EOP
      frames against NAPI budget in idpf_rx_splitq_clean_skb()
      (patch 2)

  v1: https://lore.kernel.org/intel-wired-lan/20261202065737.207610-1-kuniyu@google.com/


Kuniyuki Iwashima (5):
  idpf: Reuse idpf_rx_update_bufq_desc() in idpf_rx_post_buf_desc().
  idpf: Use coherent DMA buffer for Rx header split without XDP.
  netmem: Annotate data-races around pp_magic.
  page_pool: Allow drivers to unmap DMA before returning pages.
  idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ
    IOMMU.

 drivers/net/ethernet/intel/idpf/idpf_txrx.c | 569 +++++++++++++++++---
 drivers/net/ethernet/intel/idpf/idpf_txrx.h |  29 +-
 include/linux/mm.h                          |   4 +-
 include/net/page_pool/helpers.h             |  26 +
 include/net/page_pool/types.h               |  16 +
 net/core/netmem_priv.h                      |   8 +-
 net/core/page_pool.c                        | 107 +++-
 7 files changed, 651 insertions(+), 108 deletions(-)

-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 iwl-next 1/5] idpf: Reuse idpf_rx_update_bufq_desc() in idpf_rx_post_buf_desc().
  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 ` 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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

idpf_rx_post_buf_desc() and idpf_rx_update_bufq_desc() duplicate
the same header and payload buffer allocation and descriptor setup
logic, except that idpf_rx_post_buf_desc() allocates with
GFP_KERNEL for the initial ring fill while idpf_rx_clean_refillq()
allocates with GFP_ATOMIC from NAPI.

Also, in later commits we will extend buffer allocation to support
coherent header buffers and early DMA unmapping.

Let's add a gfp_t gfp parameter to idpf_rx_update_bufq_desc(), move
it up, and reuse it in idpf_rx_post_buf_desc().

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 drivers/net/ethernet/intel/idpf/idpf_txrx.c | 123 ++++++++------------
 1 file changed, 50 insertions(+), 73 deletions(-)

diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
index 7d926d13e5e6..d6601272c9ba 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
@@ -596,48 +596,68 @@ static void idpf_post_buf_refill(struct idpf_sw_queue *refillq, u16 buf_id)
 }
 
 /**
- * idpf_rx_post_buf_desc - Post buffer to bufq descriptor ring
- * @bufq: buffer queue to post to
- * @buf_id: buffer id to post
- *
- * Only used for the initial fill, from process context, so it allocates
- * with GFP_KERNEL.
+ * idpf_rx_update_bufq_desc - Update buffer queue descriptor
+ * @bufq: Pointer to the buffer queue
+ * @buf_id: buffer ID
+ * @buf_desc: Buffer queue descriptor
+ * @gfp: allocation flags (GFP_KERNEL for initial fill, GFP_ATOMIC from NAPI)
  *
- * Return: %false if buffer could not be allocated, %true otherwise.
+ * Return: 0 on success and negative on failure.
  */
-static bool idpf_rx_post_buf_desc(struct idpf_buf_queue *bufq, u16 buf_id)
+static int idpf_rx_update_bufq_desc(struct idpf_buf_queue *bufq, u32 buf_id,
+				    struct virtchnl2_splitq_rx_buf_desc *buf_desc,
+				    gfp_t gfp)
 {
-	struct virtchnl2_splitq_rx_buf_desc *splitq_rx_desc = NULL;
 	struct libeth_fq_fp fq = {
-		.count	= bufq->desc_count,
+		.pp		= bufq->pp,
+		.fqes		= bufq->buf,
+		.truesize	= bufq->truesize,
+		.count		= bufq->desc_count,
 	};
-	u16 nta = bufq->next_to_alloc;
 	dma_addr_t addr;
 
-	splitq_rx_desc = &bufq->split_buf[nta];
+	addr = __libeth_rx_alloc(&fq, buf_id, gfp);
+	if (addr == DMA_MAPPING_ERROR)
+		return -ENOMEM;
+
+	buf_desc->pkt_addr = cpu_to_le64(addr);
+	buf_desc->qword0.buf_id = cpu_to_le16(buf_id);
 
-	if (idpf_queue_has(HSPLIT_EN, bufq)) {
-		fq.pp = bufq->hdr_pp;
-		fq.fqes = bufq->hdr_buf;
-		fq.truesize = bufq->hdr_truesize;
+	if (!idpf_queue_has(HSPLIT_EN, bufq))
+		return 0;
 
-		addr = __libeth_rx_alloc(&fq, buf_id, GFP_KERNEL);
-		if (addr == DMA_MAPPING_ERROR)
-			return false;
+	fq.pp = bufq->hdr_pp;
+	fq.fqes = bufq->hdr_buf;
+	fq.truesize = bufq->hdr_truesize;
 
-		splitq_rx_desc->hdr_addr = cpu_to_le64(addr);
+	addr = __libeth_rx_alloc(&fq, buf_id, gfp);
+	if (addr == DMA_MAPPING_ERROR) {
+		idpf_rx_page_rel(&bufq->buf[buf_id]);
+		return -ENOMEM;
 	}
 
-	fq.pp = bufq->pp;
-	fq.fqes = bufq->buf;
-	fq.truesize = bufq->truesize;
+	buf_desc->hdr_addr = cpu_to_le64(addr);
 
-	addr = __libeth_rx_alloc(&fq, buf_id, GFP_KERNEL);
-	if (addr == DMA_MAPPING_ERROR)
-		return false;
+	return 0;
+}
 
-	splitq_rx_desc->pkt_addr = cpu_to_le64(addr);
-	splitq_rx_desc->qword0.buf_id = cpu_to_le16(buf_id);
+/**
+ * idpf_rx_post_buf_desc - Post buffer to bufq descriptor ring
+ * @bufq: buffer queue to post to
+ * @buf_id: buffer id to post
+ *
+ * Only used for the initial fill, from process context, so it allocates
+ * with GFP_KERNEL.
+ *
+ * Return: %false if buffer could not be allocated, %true otherwise.
+ */
+static bool idpf_rx_post_buf_desc(struct idpf_buf_queue *bufq, u16 buf_id)
+{
+	u16 nta = bufq->next_to_alloc;
+
+	if (idpf_rx_update_bufq_desc(bufq, buf_id, &bufq->split_buf[nta],
+				     GFP_KERNEL))
+		return false;
 
 	nta++;
 	if (unlikely(nta == bufq->desc_count))
@@ -3770,50 +3790,6 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 	return rs.packets;
 }
 
-/**
- * idpf_rx_update_bufq_desc - Update buffer queue descriptor
- * @bufq: Pointer to the buffer queue
- * @buf_id: buffer ID
- * @buf_desc: Buffer queue descriptor
- *
- * Return: 0 on success and negative on failure.
- */
-static int idpf_rx_update_bufq_desc(struct idpf_buf_queue *bufq, u32 buf_id,
-				    struct virtchnl2_splitq_rx_buf_desc *buf_desc)
-{
-	struct libeth_fq_fp fq = {
-		.pp		= bufq->pp,
-		.fqes		= bufq->buf,
-		.truesize	= bufq->truesize,
-		.count		= bufq->desc_count,
-	};
-	dma_addr_t addr;
-
-	addr = libeth_rx_alloc(&fq, buf_id);
-	if (addr == DMA_MAPPING_ERROR)
-		return -ENOMEM;
-
-	buf_desc->pkt_addr = cpu_to_le64(addr);
-	buf_desc->qword0.buf_id = cpu_to_le16(buf_id);
-
-	if (!idpf_queue_has(HSPLIT_EN, bufq))
-		return 0;
-
-	fq.pp = bufq->hdr_pp;
-	fq.fqes = bufq->hdr_buf;
-	fq.truesize = bufq->hdr_truesize;
-
-	addr = libeth_rx_alloc(&fq, buf_id);
-	if (addr == DMA_MAPPING_ERROR) {
-		idpf_rx_page_rel(&bufq->buf[buf_id]);
-		return -ENOMEM;
-	}
-
-	buf_desc->hdr_addr = cpu_to_le64(addr);
-
-	return 0;
-}
-
 /**
  * idpf_rx_clean_refillq - Clean refill queue buffers
  * @bufq: buffer queue to post buffers back to
@@ -3841,7 +3817,8 @@ static void idpf_rx_clean_refillq(struct idpf_buf_queue *bufq,
 			break;
 
 		buf_id = FIELD_GET(IDPF_RFL_BI_BUFID_M, refill_desc);
-		failure = idpf_rx_update_bufq_desc(bufq, buf_id, buf_desc);
+		failure = idpf_rx_update_bufq_desc(bufq, buf_id, buf_desc,
+						   GFP_ATOMIC);
 		if (failure)
 			break;
 
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 iwl-next 2/5] idpf: Use coherent DMA buffer for Rx header split without XDP.
  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 ` Kuniyuki Iwashima
  2026-12-05 12:52 ` [PATCH v2 iwl-next 3/5] netmem: Annotate data-races around pp_magic Kuniyuki Iwashima
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

With header split enabled under an IOMMU DMA domain, idpf uses
page_pool backed by 4K pages for Rx header buffers.

Since a single 4K page is sliced across multiple header buffers,
queued skbs in socket receive and out-of-order queues pin these
pages and retain their DMA mappings, exhausting the 32-bit IOVA
window and causing alloc_iova() contention and Rx drops.

Commit 90912f9f4f2d ("idpf: convert header split mode to libeth +
napi_build_skb()") removed coherent header allocation in favour
of page_pool.

Let's allocate a single coherent DMA buffer array per buffer
queue when header split is enabled on a DMA-coherent device under
an IOMMU DMA domain without XDP.

If dma_alloc_coherent_node() fails, the queue group falls back to
the existing page_pool path.

Rx headers are small and fixed-size, so copying headers into
napi_alloc_skb() from a pre-allocated coherent array eliminates all
per-packet IOMMU mapping churn and page_pool overhead for header
buffers.

On Rx, idpf_rx_splitq_clean_skb() copies headers from the coherent
array (or the full frame when pkt_len <= L1_CACHE_BYTES, or
ETH_HLEN otherwise on header buffer overflow) into napi_alloc_skb()
and attaches or coalesces payload fragments directly.

If an allocation or bounds check fails mid-frame, rxq->rx_drop
drops remaining descriptors of a multi-descriptor frame across
NAPI polls.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
v2:
  * Coalesce contiguous payload frags in idpf_rx_splitq_clean_skb()
  * Bound NAPI loop by dropped EOP frames as well
---
 drivers/net/ethernet/intel/idpf/idpf_txrx.c | 270 ++++++++++++++++++--
 drivers/net/ethernet/intel/idpf/idpf_txrx.h |  22 +-
 2 files changed, 272 insertions(+), 20 deletions(-)

diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
index d6601272c9ba..fb6a2ea25dc5 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
@@ -1,12 +1,17 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (C) 2023 Intel Corporation */
 
+#include <linux/dma-mapping.h>
+#include <linux/iommu.h>
+
 #include "idpf.h"
 #include "idpf_ptp.h"
 #include "idpf_virtchnl.h"
 #include "xdp.h"
 #include "xsk.h"
 
+#define IDPF_RX_HDR_BUF_SIZE		ALIGN(LIBETH_MAX_HEAD, LIBETH_RX_BUF_STRIDE)
+
 #define idpf_tx_buf_next(buf)		(*(u32 *)&(buf)->priv)
 LIBETH_SQE_CHECK_PRIV(u32);
 
@@ -362,10 +367,25 @@ static void idpf_rx_page_rel(struct libeth_fqe *rx_buf)
  */
 static void idpf_rx_hdr_buf_rel_all(struct idpf_buf_queue *bufq)
 {
-	struct libeth_fq fq = {
-		.fqes	= bufq->hdr_buf,
-		.pp	= bufq->hdr_pp,
-	};
+	struct libeth_fq fq;
+
+	if (idpf_queue_has(HSPLIT_COHERENT, bufq)) {
+		if (!bufq->hdr_buf_va)
+			return;
+
+		dma_free_coherent(&bufq->q_vector->vport->adapter->pdev->dev,
+				  bufq->desc_count * IDPF_RX_HDR_BUF_SIZE,
+				  bufq->hdr_buf_va, bufq->hdr_buf_pa);
+		bufq->hdr_buf_va = NULL;
+		bufq->hdr_buf_pa = 0;
+		return;
+	}
+
+	if (!bufq->hdr_pp)
+		return;
+
+	fq.fqes = bufq->hdr_buf;
+	fq.pp = bufq->hdr_pp;
 
 	for (u32 i = 0; i < bufq->desc_count; i++)
 		idpf_rx_page_rel(&bufq->hdr_buf[i]);
@@ -386,6 +406,9 @@ static void idpf_rx_buf_rel_bufq(struct idpf_buf_queue *bufq)
 		.pp	= bufq->pp,
 	};
 
+	if (idpf_queue_has(HSPLIT_COHERENT, bufq))
+		idpf_rx_hdr_buf_rel_all(bufq);
+
 	/* queue already cleared, nothing to do */
 	if (!bufq->buf)
 		return;
@@ -443,8 +466,15 @@ static void idpf_rx_desc_rel(struct idpf_rx_queue *rxq, struct device *dev,
 	if (!rxq)
 		return;
 
-	if (!idpf_queue_has(XSK, rxq))
-		libeth_xdp_return_stash(&rxq->xdp);
+	if (!idpf_queue_has(XSK, rxq)) {
+		if (idpf_queue_has(HSPLIT_COHERENT, rxq)) {
+			dev_kfree_skb_any(rxq->skb);
+			rxq->skb = NULL;
+			rxq->rx_drop = false;
+		} else {
+			libeth_xdp_return_stash(&rxq->xdp);
+		}
+	}
 
 	if (!idpf_is_queue_model_split(model))
 		idpf_rx_buf_rel_all(rxq);
@@ -623,6 +653,12 @@ static int idpf_rx_update_bufq_desc(struct idpf_buf_queue *bufq, u32 buf_id,
 	buf_desc->pkt_addr = cpu_to_le64(addr);
 	buf_desc->qword0.buf_id = cpu_to_le16(buf_id);
 
+	if (idpf_queue_has(HSPLIT_COHERENT, bufq)) {
+		buf_desc->hdr_addr = cpu_to_le64(bufq->hdr_buf_pa +
+						 (dma_addr_t)buf_id * IDPF_RX_HDR_BUF_SIZE);
+		return 0;
+	}
+
 	if (!idpf_queue_has(HSPLIT_EN, bufq))
 		return 0;
 
@@ -747,7 +783,8 @@ static int idpf_rx_buf_alloc_all(struct idpf_buf_queue *rxbufq)
 {
 	int err = 0;
 
-	if (idpf_queue_has(HSPLIT_EN, rxbufq)) {
+	if (idpf_queue_has(HSPLIT_EN, rxbufq) &&
+	    !idpf_queue_has(HSPLIT_COHERENT, rxbufq)) {
 		err = idpf_rx_hdr_buf_alloc_all(rxbufq);
 		if (err)
 			goto rx_buf_alloc_all_out;
@@ -764,6 +801,46 @@ static int idpf_rx_buf_alloc_all(struct idpf_buf_queue *rxbufq)
 	return err;
 }
 
+static void idpf_rx_hdr_buf_alloc_coherent(struct idpf_rxq_group *rx_qgrp,
+					   u16 num_bufqs)
+{
+	struct idpf_buf_queue *bufq = &rx_qgrp->splitq.bufq_sets[0].bufq;
+	struct device *dev = &bufq->q_vector->vport->adapter->pdev->dev;
+
+	if (!idpf_queue_has(HSPLIT_COHERENT, bufq))
+		return;
+
+	for (unsigned int j = 0; j < num_bufqs; j++) {
+		dma_addr_t pa;
+		void *va;
+
+		bufq = &rx_qgrp->splitq.bufq_sets[j].bufq;
+		va = dma_alloc_coherent_node(dev,
+					     bufq->desc_count * IDPF_RX_HDR_BUF_SIZE,
+					     &pa, GFP_KERNEL | __GFP_NOWARN,
+					     idpf_q_vector_to_mem(bufq->q_vector));
+		if (!va)
+			goto fallback;
+
+		bufq->hdr_buf_va = va;
+		bufq->hdr_buf_pa = pa;
+		bufq->rx_hbuf_size = IDPF_RX_HDR_BUF_SIZE;
+	}
+
+	return;
+
+fallback:
+	for (unsigned int j = 0; j < num_bufqs; j++) {
+		bufq = &rx_qgrp->splitq.bufq_sets[j].bufq;
+		idpf_rx_hdr_buf_rel_all(bufq);
+		idpf_queue_clear(HSPLIT_COHERENT, bufq);
+	}
+
+	for (unsigned int j = 0; j < rx_qgrp->splitq.num_rxq_sets; j++)
+		idpf_queue_clear(HSPLIT_COHERENT,
+				 &rx_qgrp->splitq.rxq_sets[j]->rxq);
+}
+
 /**
  * idpf_rx_bufs_init - Initialize page pool, allocate rx bufs, and post to HW
  * @bufq: buffer queue to create page pool for
@@ -835,6 +912,9 @@ int idpf_rx_bufs_init_all(struct idpf_vport *vport,
 			continue;
 		}
 
+		idpf_rx_hdr_buf_alloc_coherent(rx_qgrp,
+					       rsrc->num_bufqs_per_qgrp);
+
 		/* Otherwise, allocate bufs for the buffer queues */
 		for (unsigned int j = 0; j < rsrc->num_bufqs_per_qgrp; j++) {
 			enum libeth_fqe_type type;
@@ -1861,8 +1941,10 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport,
 				u16 num_rxq)
 {
 	struct idpf_adapter *adapter = vport->adapter;
+	struct device *dev = &adapter->pdev->dev;
 	struct idpf_rxq_group *rxq_grps;
-	bool hs, rsc;
+	struct iommu_domain *domain;
+	bool hs, rsc, coherent_hdr;
 	int err;
 
 	rxq_grps = kzalloc_objs(struct idpf_rxq_group, rsrc->num_rxq_grp);
@@ -1872,6 +1954,11 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport,
 	hs = idpf_vport_get_hsplit(vport) == ETHTOOL_TCP_DATA_SPLIT_ENABLED;
 	rsc = idpf_is_feature_ena(vport, NETIF_F_GRO_HW);
 
+	domain = iommu_get_domain_for_dev(dev);
+	coherent_hdr = hs && !idpf_xdp_enabled(vport) &&
+		domain && iommu_is_dma_domain(domain) &&
+		!dma_dev_need_sync(dev);
+
 	for (unsigned int i = 0; i < rsrc->num_rxq_grp; i++) {
 		struct idpf_rxq_group *rx_qgrp = &rxq_grps[i];
 
@@ -1917,6 +2004,7 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport,
 			q->rx_buffer_low_watermark = IDPF_LOW_WATERMARK;
 
 			idpf_queue_assign(HSPLIT_EN, q, hs);
+			idpf_queue_assign(HSPLIT_COHERENT, q, coherent_hdr);
 			idpf_queue_assign(RSC_EN, q, rsc);
 
 			bufq_set->refillqs = kcalloc(num_rxq, swq_size,
@@ -1960,6 +2048,7 @@ static int idpf_rxq_group_alloc(struct idpf_vport *vport,
 				      &rx_qgrp->splitq.bufq_sets[1].refillqs[j];
 
 			idpf_queue_assign(HSPLIT_EN, q, hs);
+			idpf_queue_assign(HSPLIT_COHERENT, q, coherent_hdr);
 			idpf_queue_assign(RSC_EN, q, rsc);
 			q->rx_ptype_lkup = adapter->splitq_pt_lkup;
 
@@ -3649,13 +3738,139 @@ static bool idpf_rx_splitq_test_staterr(const u8 stat_err_field,
  * Return: %true if the buffer is an EOP buffer, %false otherwise, indicating
  * that this is in fact a non-EOP buffer.
  */
-static bool idpf_rx_splitq_is_eop(struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc)
+static bool idpf_rx_splitq_is_eop(const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc)
 {
 	/* if we are the last buffer then there is nothing else to do */
 	return likely(idpf_rx_splitq_test_staterr(rx_desc->status_err0_qw1,
 						  IDPF_RXD_EOF_SPLITQ));
 }
 
+static void idpf_rx_splitq_clean_skb(struct idpf_rx_queue *rxq,
+				     const struct idpf_buf_queue *rx_bufq,
+				     const struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc,
+				     struct libeth_fqe *rx_buf, u16 buf_id, u32 hdr_len,
+				     u32 pkt_len, struct sk_buff **skb_p,
+				     struct libeth_rq_napi_stats *rs, u32 *drops)
+{
+	bool eop = idpf_rx_splitq_is_eop(rx_desc);
+	struct sk_buff *skb = *skb_p;
+	bool sync;
+	u32 len;
+
+	if (unlikely(rxq->rx_drop)) {
+		libeth_rx_recycle_slow(rx_buf->netmem);
+		if (eop) {
+			rxq->rx_drop = false;
+			(*drops)++;
+		}
+		return;
+	}
+
+	sync = libeth_rx_sync_for_cpu(rx_buf, pkt_len);
+
+	if (!skb) {
+		const void *va;
+		u32 copy;
+
+		if (hdr_len) {
+			if (unlikely(hdr_len > IDPF_RX_HDR_BUF_SIZE))
+				goto drop_sync;
+
+			va = rx_bufq->hdr_buf_va +
+			     (size_t)buf_id * IDPF_RX_HDR_BUF_SIZE;
+			copy = hdr_len;
+
+			rs->hsplit++;
+		} else {
+			u64_stats_update_begin(&rxq->stats_sync);
+			u64_stats_inc(&rxq->q_stats.hsplit_buf_ovf);
+			u64_stats_update_end(&rxq->stats_sync);
+
+			if (!sync)
+				goto drop;
+
+			if (unlikely(netmem_is_net_iov(rx_buf->netmem)))
+				goto drop_sync;
+
+			copy = pkt_len <= L1_CACHE_BYTES ? pkt_len : ETH_HLEN;
+			va = page_address(__netmem_to_page(rx_buf->netmem)) +
+			     rx_buf->offset + rx_bufq->pp->p.offset;
+			rx_buf->offset += copy;
+			pkt_len -= copy;
+		}
+
+		skb = napi_alloc_skb(rxq->napi, copy);
+		if (unlikely(!skb))
+			goto drop_sync;
+
+		skb_put_data(skb, va, copy);
+		if (sync && !pkt_len) {
+			libeth_rx_recycle_slow(rx_buf->netmem);
+			sync = false;
+		}
+	}
+
+	if (sync) {
+		int nr_frags = skb_shinfo(skb)->nr_frags;
+		u32 off = rx_buf->offset + rx_bufq->pp->p.offset;
+
+		if (nr_frags) {
+			skb_frag_t *prev = &skb_shinfo(skb)->frags[nr_frags - 1];
+
+			if (rx_buf->netmem == skb_frag_netmem(prev) &&
+			    off == skb_frag_off(prev) + skb_frag_size(prev)) {
+				skb_coalesce_rx_frag(skb, nr_frags - 1,
+						     pkt_len, rx_buf->truesize);
+				libeth_rx_recycle_slow(rx_buf->netmem);
+				goto check_eop;
+			}
+		}
+
+		if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
+			dev_kfree_skb_any(skb);
+			*skb_p = NULL;
+			goto drop_sync;
+		}
+
+		skb_mark_for_recycle(skb);
+		skb_add_rx_frag_netmem(skb, nr_frags, rx_buf->netmem, off,
+				       pkt_len, rx_buf->truesize);
+	}
+
+check_eop:
+	if (!eop) {
+		*skb_p = skb;
+		return;
+	}
+
+	skb_record_rx_queue(skb, rxq->idx);
+	len = skb->len;
+	skb->protocol = eth_type_trans(skb, rxq->napi->dev);
+
+	if (unlikely(__idpf_rx_process_skb_fields(rxq, skb, rx_desc))) {
+		napi_consume_skb(skb, true);
+		*skb_p = NULL;
+		(*drops)++;
+		return;
+	}
+
+	rs->packets++;
+	rs->bytes += len;
+
+	napi_gro_receive(rxq->napi, skb);
+	*skb_p = NULL;
+	return;
+
+drop_sync:
+	if (sync)
+		libeth_rx_recycle_slow(rx_buf->netmem);
+drop:
+	if (!eop)
+		rxq->rx_drop = true;
+	else
+		(*drops)++;
+}
+
 /**
  * idpf_rx_splitq_clean - Clean completed descriptors from Rx queue
  * @rxq: Rx descriptor queue to retrieve receive buffer queue
@@ -3670,18 +3885,23 @@ static bool idpf_rx_splitq_is_eop(struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_de
  */
 static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 {
+	bool coherent_hdr = idpf_queue_has(HSPLIT_COHERENT, rxq);
+	struct sk_buff *skb = coherent_hdr ? rxq->skb : NULL;
 	struct idpf_buf_queue *rx_bufq = NULL;
 	struct libeth_rq_napi_stats rs = { };
 	u16 ntc = rxq->next_to_clean;
 	LIBETH_XDP_ONSTACK_BUFF(xdp);
 	LIBETH_XDP_ONSTACK_BULK(bq);
+	u32 drops = 0;
 
-	libeth_xdp_tx_init_bulk(&bq, rxq->xdp_prog, rxq->xdp_rxq.dev,
-				rxq->xdpsqs, rxq->num_xdp_txq);
-	libeth_xdp_init_buff(xdp, &rxq->xdp, &rxq->xdp_rxq);
+	if (!coherent_hdr) {
+		libeth_xdp_tx_init_bulk(&bq, rxq->xdp_prog, rxq->xdp_rxq.dev,
+					rxq->xdpsqs, rxq->num_xdp_txq);
+		libeth_xdp_init_buff(xdp, &rxq->xdp, &rxq->xdp_rxq);
+	}
 
 	/* Process Rx packets bounded by budget */
-	while (likely(rs.packets < budget)) {
+	while (likely(rs.packets + drops < budget)) {
 		struct virtchnl2_rx_flex_desc_adv_nic_3 *rx_desc;
 		struct libeth_fqe *hdr, *rx_buf = NULL;
 		struct idpf_sw_queue *refillq = NULL;
@@ -3729,7 +3949,7 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 
 		rx_buf = &rx_bufq->buf[buf_id];
 
-		if (!rx_bufq->hdr_pp)
+		if (!coherent_hdr && !rx_bufq->hdr_pp)
 			goto payload;
 
 #define __HBO_BIT	VIRTCHNL2_RX_FLEX_DESC_ADV_STATUS0_HBO_M
@@ -3745,6 +3965,16 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 #undef __HDR_LEN_MASK
 #undef __HBO_BIT
 
+		if (coherent_hdr) {
+			idpf_rx_splitq_clean_skb(rxq, rx_bufq, rx_desc, rx_buf,
+						 buf_id, hdr_len, pkt_len, &skb,
+						 &rs, &drops);
+			rx_buf->netmem = 0;
+			idpf_post_buf_refill(refillq, buf_id);
+			IDPF_RX_BUMP_NTC(rxq, ntc);
+			continue;
+		}
+
 		hdr = &rx_bufq->hdr_buf[buf_id];
 
 		if (unlikely(!hdr_len && !xdp->data)) {
@@ -3776,10 +4006,14 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 		idpf_xdp_run_pass(xdp, &bq, rxq->napi, &rs, rx_desc);
 	}
 
-	idpf_xdp_finalize_rx(&bq);
+	if (coherent_hdr) {
+		rxq->skb = skb;
+	} else {
+		idpf_xdp_finalize_rx(&bq);
+		libeth_xdp_save_buff(&rxq->xdp, xdp);
+	}
 
 	rxq->next_to_clean = ntc;
-	libeth_xdp_save_buff(&rxq->xdp, xdp);
 
 	u64_stats_update_begin(&rxq->stats_sync);
 	u64_stats_add(&rxq->q_stats.packets, rs.packets);
@@ -3787,7 +4021,7 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 	u64_stats_add(&rxq->q_stats.hsplit_pkts, rs.hsplit);
 	u64_stats_update_end(&rxq->stats_sync);
 
-	return rs.packets;
+	return min_t(int, rs.packets + drops, budget);
 }
 
 /**
@@ -3869,7 +4103,7 @@ static void idpf_rx_clean_refillq_all(struct idpf_buf_queue *bufq, int nid)
 	int i;
 
 	page_pool_nid_changed(bufq->pp, nid);
-	if (bufq->hdr_pp)
+	if (!idpf_queue_has(HSPLIT_COHERENT, bufq) && bufq->hdr_pp)
 		page_pool_nid_changed(bufq->hdr_pp, nid);
 
 	bufq_set = container_of(bufq, struct idpf_bufq_set, bufq);
diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.h b/drivers/net/ethernet/intel/idpf/idpf_txrx.h
index 0f16f9a376f4..3fad569d27b8 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_txrx.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.h
@@ -288,6 +288,7 @@ struct idpf_ptype_state {
  * @__IDPF_Q_CRC_EN: enable CRC offload in singleq mode
  * @__IDPF_Q_RSC_EN: enable Receive Side Coalescing on Rx (splitq)
  * @__IDPF_Q_HSPLIT_EN: enable header split on Rx (splitq)
+ * @__IDPF_Q_HSPLIT_COHERENT: use coherent DMA buffer for Rx header split
  * @__IDPF_Q_PTP: indicates whether the Rx timestamping is enabled for the
  *		  queue
  * @__IDPF_Q_NOIRQ: queue is polling-driven and has no interrupt
@@ -303,6 +304,7 @@ enum idpf_queue_flags_t {
 	__IDPF_Q_CRC_EN,
 	__IDPF_Q_RSC_EN,
 	__IDPF_Q_HSPLIT_EN,
+	__IDPF_Q_HSPLIT_COHERENT,
 	__IDPF_Q_PTP,
 	__IDPF_Q_NOIRQ,
 	__IDPF_Q_XDP,
@@ -491,6 +493,8 @@ struct idpf_tx_queue_stats {
  * @xdp: XDP buffer with the current frame
  * @xsk: current XDP buffer in XSk mode
  * @pool: XSk pool if installed
+ * @skb: partially built skb in coherent header split mode
+ * @rx_drop: drop remaining descriptors of the current Rx packet
  * @cached_phc_time: Cached PHC time for the Rx queue
  * @stats_sync: See struct u64_stats_sync
  * @q_stats: See union idpf_rx_queue_stats
@@ -552,6 +556,10 @@ struct idpf_rx_queue {
 			struct libeth_xdp_buff *xsk;
 			struct xsk_buff_pool *pool;
 		};
+		struct {
+			struct sk_buff *skb;
+			bool rx_drop;
+		};
 	};
 	u64 cached_phc_time;
 
@@ -731,6 +739,8 @@ libeth_cacheline_set_assert(struct idpf_tx_queue, 64,
  * @pool: &xsk_buff_pool on XSk queues
  * @hdr_buf: &libeth_fqe for header buffers
  * @hdr_pp: &page_pool for header buffers
+ * @hdr_buf_va: virtual address of coherent header buffer array
+ * @hdr_buf_pa: DMA address of coherent header buffer array
  * @tail: Tail offset
  * @flags: See enum idpf_queue_flags_t
  * @desc_count: Number of descriptors
@@ -762,8 +772,16 @@ struct idpf_buf_queue {
 			struct xsk_buff_pool *pool;
 		};
 	};
-	struct libeth_fqe *hdr_buf;
-	struct page_pool *hdr_pp;
+	union {
+		struct {
+			struct libeth_fqe *hdr_buf;
+			struct page_pool *hdr_pp;
+		};
+		struct {
+			void *hdr_buf_va;
+			dma_addr_t hdr_buf_pa;
+		};
+	};
 	void __iomem *tail;
 
 	DECLARE_BITMAP(flags, __IDPF_Q_FLAGS_NBITS);
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 iwl-next 3/5] netmem: Annotate data-races around pp_magic.
  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 ` Kuniyuki Iwashima
  2026-12-05 12:52 ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages Kuniyuki Iwashima
  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
  4 siblings, 0 replies; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

page->pp_magic shares storage with page->lru.next, which also holds
the pfmemalloc bit (BIT(1)).

During page_pool_scrub(), a reader can also call
netmem_get_dma_index() concurrently with netmem_clear_pp_magic()
and netmem_set_dma_index().

Furthermore, we will allow unmapping DMA early in page_pool while
earlier fragments of the same page may still be in flight on remote
CPUs, allowing netmem_set_dma_index() to run concurrently with
readers of pp_magic and page_is_pfmemalloc().

Let's annotate accesses to page->pp_magic and page->lru.next with
READ_ONCE() and WRITE_ONCE().

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
 include/linux/mm.h     | 4 ++--
 net/core/netmem_priv.h | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381b..b4bcd0e389c4 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3354,7 +3354,7 @@ static inline bool page_is_pfmemalloc(const struct page *page)
 	 * pfmemalloc reserves.  Callers may simply overwrite it if
 	 * they do not need to preserve that information.
 	 */
-	return (uintptr_t)page->lru.next & BIT(1);
+	return (uintptr_t)READ_ONCE(page->lru.next) & BIT(1);
 }
 
 /*
@@ -5764,7 +5764,7 @@ int arch_lock_shadow_stack_status(struct task_struct *t, unsigned long status);
 #ifdef CONFIG_PAGE_POOL
 static inline bool page_pool_page_is_pp(const struct page *page)
 {
-	return (page->pp_magic & PP_MAGIC_MASK) == PP_SIGNATURE;
+	return (READ_ONCE(page->pp_magic) & PP_MAGIC_MASK) == PP_SIGNATURE;
 }
 #else
 static inline bool page_pool_page_is_pp(const struct page *page)
diff --git a/net/core/netmem_priv.h b/net/core/netmem_priv.h
index 23175cb2bd86..7bf216b85eec 100644
--- a/net/core/netmem_priv.h
+++ b/net/core/netmem_priv.h
@@ -5,7 +5,7 @@
 
 static inline unsigned long netmem_get_pp_magic(netmem_ref netmem)
 {
-	return netmem_to_nmdesc(netmem)->pp_magic & ~PP_DMA_INDEX_MASK;
+	return READ_ONCE(netmem_to_nmdesc(netmem)->pp_magic) & ~PP_DMA_INDEX_MASK;
 }
 
 static inline void netmem_or_pp_magic(netmem_ref netmem, unsigned long pp_magic)
@@ -17,7 +17,7 @@ static inline void netmem_clear_pp_magic(netmem_ref netmem)
 {
 	WARN_ON_ONCE(netmem_to_nmdesc(netmem)->pp_magic & PP_DMA_INDEX_MASK);
 
-	netmem_to_nmdesc(netmem)->pp_magic = 0;
+	WRITE_ONCE(netmem_to_nmdesc(netmem)->pp_magic, 0);
 }
 
 static inline bool netmem_is_pp(netmem_ref netmem)
@@ -43,7 +43,7 @@ static inline unsigned long netmem_get_dma_index(netmem_ref netmem)
 	if (WARN_ON_ONCE(netmem_is_net_iov(netmem)))
 		return 0;
 
-	magic = netmem_to_nmdesc(netmem)->pp_magic;
+	magic = READ_ONCE(netmem_to_nmdesc(netmem)->pp_magic);
 
 	return (magic & PP_DMA_INDEX_MASK) >> PP_DMA_INDEX_SHIFT;
 }
@@ -57,6 +57,6 @@ static inline void netmem_set_dma_index(netmem_ref netmem,
 		return;
 
 	magic = netmem_get_pp_magic(netmem) | (id << PP_DMA_INDEX_SHIFT);
-	netmem_to_nmdesc(netmem)->pp_magic = magic;
+	WRITE_ONCE(netmem_to_nmdesc(netmem)->pp_magic, magic);
 }
 #endif
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages.
  2026-12-05 12:52 [PATCH v2 iwl-next 0/5] idpf: Avoid IOVA exhaustion under DMA-FQ IOMMU Kuniyuki Iwashima
                   ` (2 preceding siblings ...)
  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
  2026-12-05 13:15   ` 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
  4 siblings, 1 reply; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v2 iwl-next 5/5] idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ IOMMU.
  2026-12-05 12:52 [PATCH v2 iwl-next 0/5] idpf: Avoid IOVA exhaustion under DMA-FQ IOMMU Kuniyuki Iwashima
                   ` (3 preceding siblings ...)
  2026-12-05 12:52 ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages Kuniyuki Iwashima
@ 2026-12-05 12:52 ` Kuniyuki Iwashima
  2026-12-05 13:16   ` sashiko-bot
  4 siblings, 1 reply; 8+ messages in thread
From: Kuniyuki Iwashima @ 2026-12-05 12:52 UTC (permalink / raw)
  To: Alexander Lobakin, Tony Nguyen, Przemek Kitszel,
	Jesper Dangaard Brouer, Ilias Apalodimas
  Cc: Brian Vazquez, David Decotigny, Li Li, Kuniyuki Iwashima,
	Kuniyuki Iwashima, intel-wired-lan

In splitq mode under an IOMMU DMA-FQ domain, page_pool keeps
every Rx payload page DMA-mapped until it is released back to the
pool.

When running tcp_stream with 100000 flows,

  # tcp_stream -6 --suicide-length=2000 --min-rto=5 --num-threads=256 \
    --test-length=30 --num-flows=100000 --num-ports=20 -B 65536

on the server side, Rx payload pages pinned in socket queues
saturate the 32-bit IOVA window, causing every subsequent DMA
mapping (including Tx in idpf_tx_splitq_frame()) to contend on
alloc_iova(), and perf shows heavy contention with 13.8M dropped
packets:

    21.62%  switchto-defaul  [kernel.kallsyms]   [k] queued_spin_lock_slowpath
            |
            ---queued_spin_lock_slowpath
               _raw_spin_lock_irqsave
               alloc_iova
               alloc_iova_fast
               iommu_dma_alloc_iova
               __iommu_dma_map
               __iommu_dma_map_phys
               iommu_dma_map_phys
               dma_map_phys
               idpf_tx_splitq_frame
               dev_hard_start_xmit
               sch_direct_xmit
               __qdisc_run

  # ethtool -S eth1 | grep -E 'ingress_drop3|rx-hsplit'
     rx-hsplit: 92006
     rx-hsplit_hbo: 393
     ingress_drop3.packets: 488310980   # before neper
     ingress_drop3.bytes: 1989178010650

     rx-hsplit: 27779097
     rx-hsplit_hbo: 611
     ingress_drop3.packets: 502138587   # after neper
     ingress_drop3.bytes: 2046991419840

  # dmesg
  ...
  [ 1291.279869] idpf 0000:01:00.0: Using 56-bit DMA addresses

On the client side, we can see many retransmissions, and the
throughput is 161 Gbps w/ 200G NIC.

  remote_throughput=161031856462

  TcpInSegs                       19235891           0.0
  TcpOutSegs                      225122216          0.0
  TcpRetransSegs                  20767366           0.0

Even with iommu.forcedac=1, IOVA nodes continue accumulating across
the IOMMU rbtree because iova_depot_work_func() drains only one
magazine (127 PFNs) every 100 ms, which still leads to alloc_iova()
soft lockup under heavy churn.

Let's unmap Rx payload pages early in splitq mode once active DMA
mappings exceed a per-queue threshold when XDP is disabled and the
device is DMA-coherent under IOMMU_DOMAIN_DMA_FQ.

Under those conditions, we opt in via page_pool_enable_dma_unmap(),
allocate a per-bufq tracking ring (bufq->buf_dma), and set a new
IDPF_Q_DMA_UNMAP flag on the buffer queue.

Instead of unmapping every page unconditionally, normal page_pool
DMA caching is kept while page_pool_dma_mapped_pages() stays at or
below ring->thresh, avoiding per-page IOMMU map/unmap overhead for
healthy low-flow workloads.

ring->thresh splits a 2G IOVA budget evenly across all buffer queues
of all vports on the adapter, floored above the pages needed to fill
the HW ring and page_pool alloc cache so large multi-queue rings
stay in normal recycling mode while idle.

Once page_pool_dma_mapped_pages() exceeds ring->thresh, new pages
(at offset 0) are tracked with active descriptor refcounts across
sub-page fragments and unmapped via page_pool_dma_unmap_netmem()
once all of their descriptors complete in idpf_rx_splitq_clean().

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
v2:
  * Switch from unconditional DMA unmapping (fq.no_dma_map) to
    threshold-based early unmapping via page_pool_dma_unmap_netmem()
  * Fall back to normal page_pool caching if ring allocation fails
---
 drivers/net/ethernet/intel/idpf/idpf_txrx.c | 212 ++++++++++++++++++--
 drivers/net/ethernet/intel/idpf/idpf_txrx.h |   7 +-
 2 files changed, 203 insertions(+), 16 deletions(-)

diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
index fb6a2ea25dc5..a21f21cf66ec 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c
+++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
@@ -3,6 +3,7 @@
 
 #include <linux/dma-mapping.h>
 #include <linux/iommu.h>
+#include <linux/sizes.h>
 
 #include "idpf.h"
 #include "idpf_ptp.h"
@@ -11,6 +12,37 @@
 #include "xsk.h"
 
 #define IDPF_RX_HDR_BUF_SIZE		ALIGN(LIBETH_MAX_HEAD, LIBETH_RX_BUF_STRIDE)
+#define IDPF_RX_DMA_INVALID_ID		U16_MAX
+
+/**
+ * struct idpf_rx_dma_entry - Early-unmap tracker slot for an Rx payload page
+ * @refs: number of active HW descriptors referencing this page
+ * @next_free: index of the next free entry, or %IDPF_RX_DMA_INVALID_ID
+ */
+struct idpf_rx_dma_entry {
+	u16			refs;
+	u16			next_free;
+};
+
+/**
+ * struct idpf_rx_dma_ring - Per-bufq early-unmap tracker for Rx payload pages
+ * @cur_page: most recently tracked netmem page for sub-page frag sharing
+ * @page_ids: per-buf_id tracker slot index, or %IDPF_RX_DMA_INVALID_ID
+ * @cur_id: entry index corresponding to @cur_page, or %IDPF_RX_DMA_INVALID_ID
+ * @free_head: head of the free entry list, or %IDPF_RX_DMA_INVALID_ID
+ * @active: number of tracked entries currently in use
+ * @thresh: page_pool DMA-mapped page threshold above which pages are unmapped
+ * @entries: flexible array of early-unmap slots indexed by tracker ID
+ */
+struct idpf_rx_dma_ring {
+	netmem_ref		cur_page;
+	u16			*page_ids;
+	u16			cur_id;
+	u16			free_head;
+	u16			active;
+	u32			thresh;
+	struct idpf_rx_dma_entry entries[];
+};
 
 #define idpf_tx_buf_next(buf)		(*(u32 *)&(buf)->priv)
 LIBETH_SQE_CHECK_PRIV(u32);
@@ -395,6 +427,49 @@ static void idpf_rx_hdr_buf_rel_all(struct idpf_buf_queue *bufq)
 	bufq->hdr_pp = NULL;
 }
 
+/**
+ * idpf_rx_buf_dma_unmap - Unmap tracked Rx payload page when all descriptors finish
+ * @bufq: buffer queue owning the descriptor
+ * @buf_id: buffer index in @bufq
+ *
+ * Drops the HW descriptor refcount for a tracked page and asks page_pool to
+ * unmap it early once @refs reaches 0.  Must be called from the owning NAPI
+ * poll.
+ */
+static void idpf_rx_buf_dma_unmap(struct idpf_buf_queue *bufq, u32 buf_id)
+{
+	struct idpf_rx_dma_entry *entry;
+	struct idpf_rx_dma_ring *ring;
+	u16 id;
+
+	if (!idpf_queue_has(DMA_UNMAP, bufq))
+		return;
+
+	ring = bufq->buf_dma;
+	if (!ring->active)
+		return;
+
+	id = ring->page_ids[buf_id];
+	if (id == IDPF_RX_DMA_INVALID_ID)
+		return;
+
+	ring->page_ids[buf_id] = IDPF_RX_DMA_INVALID_ID;
+	entry = &ring->entries[id];
+	if (--entry->refs)
+		return;
+
+	page_pool_dma_unmap_netmem(bufq->pp, bufq->buf[buf_id].netmem);
+
+	if (ring->cur_id == id) {
+		ring->cur_id = IDPF_RX_DMA_INVALID_ID;
+		ring->cur_page = 0;
+	}
+
+	entry->next_free = ring->free_head;
+	ring->free_head = id;
+	ring->active--;
+}
+
 /**
  * idpf_rx_buf_rel_bufq - Free all Rx buffer resources for a buffer queue
  * @bufq: queue to be cleaned
@@ -426,6 +501,9 @@ static void idpf_rx_buf_rel_bufq(struct idpf_buf_queue *bufq)
 		idpf_rx_hdr_buf_rel_all(bufq);
 
 	libeth_rx_fq_destroy(&fq);
+	kvfree(bufq->buf_dma);
+	bufq->buf_dma = NULL;
+	idpf_queue_clear(DMA_UNMAP, bufq);
 	bufq->buf = NULL;
 	bufq->pp = NULL;
 }
@@ -625,6 +703,56 @@ static void idpf_post_buf_refill(struct idpf_sw_queue *refillq, u16 buf_id)
 	refillq->next_to_use = nta;
 }
 
+/**
+ * idpf_rx_buf_dma_track - Track Rx payload page for early unmap above threshold
+ * @bufq: buffer queue owning the descriptor
+ * @rx_buf: Rx buffer containing the allocated netmem
+ * @buf_id: buffer index in @bufq
+ *
+ * Increments the descriptor refcount for consecutive frags on an already-tracked
+ * page, or starts tracking a fresh page when page_pool DMA-mapped pages exceed
+ * @ring->thresh.  Must be called from the owning NAPI poll or control path.
+ */
+static void idpf_rx_buf_dma_track(struct idpf_buf_queue *bufq,
+				  const struct libeth_fqe *rx_buf, u32 buf_id)
+{
+	struct idpf_rx_dma_ring *ring = bufq->buf_dma;
+	struct idpf_rx_dma_entry *entry;
+	u16 id;
+
+	if (ring->cur_id != IDPF_RX_DMA_INVALID_ID &&
+	    ring->cur_page == rx_buf->netmem) {
+		id = ring->cur_id;
+		ring->entries[id].refs++;
+		ring->page_ids[buf_id] = id;
+		return;
+	}
+
+	if (rx_buf->offset || page_pool_dma_mapped_pages(bufq->pp) <= ring->thresh) {
+		if (unlikely(ring->cur_id != IDPF_RX_DMA_INVALID_ID)) {
+			ring->cur_id = IDPF_RX_DMA_INVALID_ID;
+			ring->cur_page = 0;
+		}
+		return;
+	}
+
+	id = ring->free_head;
+	if (WARN_ON_ONCE(id == IDPF_RX_DMA_INVALID_ID)) {
+		ring->cur_id = IDPF_RX_DMA_INVALID_ID;
+		ring->cur_page = 0;
+		return;
+	}
+
+	entry = &ring->entries[id];
+	ring->free_head = entry->next_free;
+	ring->active++;
+	entry->refs = 1;
+
+	ring->cur_id = id;
+	ring->cur_page = rx_buf->netmem;
+	ring->page_ids[buf_id] = id;
+}
+
 /**
  * idpf_rx_update_bufq_desc - Update buffer queue descriptor
  * @bufq: Pointer to the buffer queue
@@ -656,23 +784,22 @@ static int idpf_rx_update_bufq_desc(struct idpf_buf_queue *bufq, u32 buf_id,
 	if (idpf_queue_has(HSPLIT_COHERENT, bufq)) {
 		buf_desc->hdr_addr = cpu_to_le64(bufq->hdr_buf_pa +
 						 (dma_addr_t)buf_id * IDPF_RX_HDR_BUF_SIZE);
-		return 0;
-	}
-
-	if (!idpf_queue_has(HSPLIT_EN, bufq))
-		return 0;
-
-	fq.pp = bufq->hdr_pp;
-	fq.fqes = bufq->hdr_buf;
-	fq.truesize = bufq->hdr_truesize;
+	} else if (idpf_queue_has(HSPLIT_EN, bufq)) {
+		fq.pp = bufq->hdr_pp;
+		fq.fqes = bufq->hdr_buf;
+		fq.truesize = bufq->hdr_truesize;
+
+		addr = __libeth_rx_alloc(&fq, buf_id, gfp);
+		if (addr == DMA_MAPPING_ERROR) {
+			idpf_rx_page_rel(&bufq->buf[buf_id]);
+			return -ENOMEM;
+		}
 
-	addr = __libeth_rx_alloc(&fq, buf_id, gfp);
-	if (addr == DMA_MAPPING_ERROR) {
-		idpf_rx_page_rel(&bufq->buf[buf_id]);
-		return -ENOMEM;
+		buf_desc->hdr_addr = cpu_to_le64(addr);
 	}
 
-	buf_desc->hdr_addr = cpu_to_le64(addr);
+	if (idpf_queue_has(DMA_UNMAP, bufq))
+		idpf_rx_buf_dma_track(bufq, &bufq->buf[buf_id], buf_id);
 
 	return 0;
 }
@@ -851,13 +978,14 @@ static void idpf_rx_hdr_buf_alloc_coherent(struct idpf_rxq_group *rx_qgrp,
 static int idpf_rx_bufs_init(struct idpf_buf_queue *bufq,
 			     enum libeth_fqe_type type)
 {
+	struct idpf_vport *vport = bufq->q_vector->vport;
 	struct libeth_fq fq = {
 		.truesize	= bufq->truesize,
 		.count		= bufq->desc_count,
 		.type		= type,
 		.buf_len	= IDPF_RX_MAX_BUF_SZ,
 		.hsplit		= idpf_queue_has(HSPLIT_EN, bufq),
-		.xdp		= idpf_xdp_enabled(bufq->q_vector->vport),
+		.xdp		= idpf_xdp_enabled(vport),
 		.nid		= idpf_q_vector_to_mem(bufq->q_vector),
 	};
 	int ret;
@@ -869,6 +997,59 @@ static int idpf_rx_bufs_init(struct idpf_buf_queue *bufq,
 	if (ret)
 		return ret;
 
+	if (PP_DMA_INDEX_BITS && !fq.xdp &&
+	    !dma_dev_need_sync(&vport->adapter->pdev->dev)) {
+		struct iommu_domain *domain;
+
+		domain = iommu_get_domain_for_dev(&vport->adapter->pdev->dev);
+		if (domain && domain->type == IOMMU_DOMAIN_DMA_FQ) {
+			struct idpf_rx_dma_ring *ring;
+			u32 min_thresh, num_bufqs = 0;
+			size_t size;
+
+			size = size_add(struct_size(ring, entries, bufq->desc_count),
+					array_size(sizeof(*ring->page_ids),
+						   bufq->desc_count));
+			ring = kvzalloc_node(size, GFP_KERNEL, fq.nid);
+			if (ring) {
+				ring->page_ids =
+					(u16 *)&ring->entries[bufq->desc_count];
+				ring->cur_id = IDPF_RX_DMA_INVALID_ID;
+				ring->free_head = 0;
+				for (u32 i = 0; i < bufq->desc_count; i++) {
+					ring->entries[i].next_free = i + 1;
+					ring->page_ids[i] =
+						IDPF_RX_DMA_INVALID_ID;
+				}
+				ring->entries[bufq->desc_count - 1].next_free =
+					IDPF_RX_DMA_INVALID_ID;
+
+				idpf_for_each_vport(vport->adapter, v)
+					num_bufqs +=
+						v->dflt_qv_rsrc.num_rxq_grp *
+						v->dflt_qv_rsrc.num_bufqs_per_qgrp;
+				num_bufqs = max_t(u32, 1, num_bufqs);
+
+				min_thresh = DIV_ROUND_UP_ULL((u64)bufq->desc_count *
+							      fq.truesize,
+							      PAGE_SIZE << fq.pp->p.order) +
+					     PP_ALLOC_CACHE_SIZE;
+				/* Leave 2G headroom in the 32-bit window
+				 * across all vports, while keeping normal
+				 * caching for pages posted to HW rings.
+				 */
+				ring->thresh = max_t(u32, min_thresh,
+						     (SZ_2G >>
+						      (PAGE_SHIFT + fq.pp->p.order)) /
+						     num_bufqs);
+
+				page_pool_enable_dma_unmap(fq.pp);
+				bufq->buf_dma = ring;
+				idpf_queue_set(DMA_UNMAP, bufq);
+			}
+		}
+	}
+
 	bufq->pp = fq.pp;
 	bufq->buf = fq.fqes;
 	bufq->truesize = fq.truesize;
@@ -3948,6 +4129,7 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
 		buf_id = le16_to_cpu(rx_desc->buf_id);
 
 		rx_buf = &rx_bufq->buf[buf_id];
+		idpf_rx_buf_dma_unmap(rx_bufq, buf_id);
 
 		if (!coherent_hdr && !rx_bufq->hdr_pp)
 			goto payload;
diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.h b/drivers/net/ethernet/intel/idpf/idpf_txrx.h
index 3fad569d27b8..01ccd04dc003 100644
--- a/drivers/net/ethernet/intel/idpf/idpf_txrx.h
+++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.h
@@ -289,6 +289,8 @@ struct idpf_ptype_state {
  * @__IDPF_Q_RSC_EN: enable Receive Side Coalescing on Rx (splitq)
  * @__IDPF_Q_HSPLIT_EN: enable header split on Rx (splitq)
  * @__IDPF_Q_HSPLIT_COHERENT: use coherent DMA buffer for Rx header split
+ * @__IDPF_Q_DMA_UNMAP: unmap Rx payload pages above mapped threshold under
+ *			DMA-FQ IOMMU
  * @__IDPF_Q_PTP: indicates whether the Rx timestamping is enabled for the
  *		  queue
  * @__IDPF_Q_NOIRQ: queue is polling-driven and has no interrupt
@@ -305,6 +307,7 @@ enum idpf_queue_flags_t {
 	__IDPF_Q_RSC_EN,
 	__IDPF_Q_HSPLIT_EN,
 	__IDPF_Q_HSPLIT_COHERENT,
+	__IDPF_Q_DMA_UNMAP,
 	__IDPF_Q_PTP,
 	__IDPF_Q_NOIRQ,
 	__IDPF_Q_XDP,
@@ -751,6 +754,7 @@ libeth_cacheline_set_assert(struct idpf_tx_queue, 64,
  * @pending: number of buffers to refill (Xsk)
  * @hdr_truesize: truesize for buffer headers
  * @truesize: truesize for data buffers
+ * @buf_dma: early-unmap tracker for payload pages under DMA-FQ IOMMU
  * @q_id: Queue id
  * @size: Length of descriptor ring in bytes
  * @dma: Physical address of ring
@@ -798,6 +802,7 @@ struct idpf_buf_queue {
 	u32 pending;
 	u32 hdr_truesize;
 	u32 truesize;
+	struct idpf_rx_dma_ring *buf_dma;
 	__cacheline_group_end_aligned(read_write);
 
 	__cacheline_group_begin_aligned(cold);
@@ -812,7 +817,7 @@ struct idpf_buf_queue {
 	u16 rx_buf_size;
 	__cacheline_group_end_aligned(cold);
 };
-libeth_cacheline_set_assert(struct idpf_buf_queue, 64, 24, 32);
+libeth_cacheline_set_assert(struct idpf_buf_queue, 64, 32, 32);
 
 /**
  * struct idpf_compl_queue - software structure representing a completion queue
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages.
  2026-12-05 12:52 ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages Kuniyuki Iwashima
@ 2026-12-05 13:15   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05 13:15 UTC (permalink / raw)
  To: Kuniyuki Iwashima; +Cc: intel-wired-lan

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] page_pool: unordered dma_unmap_cnt increment risks UAF on
  pool destroy
- [Medium] page_pool: dma_unmap_cnt shares cacheline on 128B/256B SMP
  architectures
--

commit 478e1772cca7ebcaee62ee863cf1ca0ac47a1dc8
Author: Kuniyuki Iwashima <kuniyu@google.com>

page_pool: Allow drivers to unmap DMA before returning pages.

This commit adds opt-in early DMA unmapping to page_pool so drivers can
track active DMA mappings and unmap in-flight pages before they are
returned to the pool.

> diff --git a/include/net/page_pool/types.h b/include/net/page_pool/types.h
> index 5306e34105eb6..89bde9df0872c 100644
> --- a/include/net/page_pool/types.h
> +++ b/include/net/page_pool/types.h

[ ... ]

> @@ -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;
>  };

[Severity: Medium]
Can dma_unmap_cnt still share a cacheline with pages_state_release_cnt on
architectures with 128-byte or 256-byte cachelines?

struct ptr_ring is SMP_CACHE_BYTES-aligned, so mp_priv begins at cacheline
offset 0.  On 64-bit kernels, pages_state_release_cnt sits at +40 (+32
without CONFIG_PAGE_POOL_STATS) and dma_unmap_cnt sits at +120 (+112), so
both share a cacheline on powerpc64 (128B) and s390 (256B).

When early DMA unmapping is active, the NAPI CPU updates dma_unmap_cnt in
page_pool_dma_unmap_netmem() while remote CPUs freeing skbs update
pages_state_release_cnt in page_pool_return_netmem(), causing cross-CPU
false sharing.

Marking dma_unmap_cnt with ____cacheline_aligned_in_smp would guarantee
separation across all SMP_CACHE_BYTES values.

[ ... ]

> diff --git a/net/core/page_pool.c b/net/core/page_pool.c
> index d7c88c0b67e53..cf2afa2952827 100644
> --- a/net/core/page_pool.c
> +++ b/net/core/page_pool.c

[ ... ]

> @@ -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);

[Severity: Medium]
Can this unordered atomic_inc(&pool->dma_unmap_cnt) write to freed memory
when the last page is returned concurrently with pool teardown?

In page_pool_return_netmem(), __page_pool_release_netmem_dma() increments
dma_unmap_cnt after xa_cmpxchg() unlocks xa_lock, and the caller then
updates pages_state_release_cnt with atomic_inc_return_relaxed().  Both
atomics are unordered and sit on separate cachelines.

CPU0 (last page return)           CPU1 (teardown)
page_pool_return_netmem()
  __page_pool_release_netmem_dma()
    atomic_inc(&pool->dma_unmap_cnt)
      (delayed / reordered)
  atomic_inc_return_relaxed(
      &pool->pages_state_release_cnt)
                                  page_pool_release()
                                    page_pool_inflight()
                                      atomic_read(&pages_state_release_cnt)
                                      /* sees inflight == 0 */
                                    __page_pool_destroy(pool)
                                      kfree(pool)
    dma_unmap_cnt write commits
      <-- use-after-free

On weakly ordered architectures such as arm64, the pages_state_release_cnt
increment can become visible before atomic_inc(&pool->dma_unmap_cnt)
completes.

Because page_pool_enable_dma_unmap() requires !dma_dev_need_sync(),
page_pool_scrub() skips synchronize_net().  When another pool still has
dma_unmap enabled, static_branch_dec() takes the lockless fast path,
leaving nothing to order the write before kfree(pool).

Using release ordering on pages_state_release_cnt (such as
atomic_inc_return_release()) paired with acquire ordering when
page_pool_release() observes inflight == 0 would close this race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205125237.2290932-1-kuniyu@google.com?part=4

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 iwl-next 5/5] idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ IOMMU.
  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
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05 13:16 UTC (permalink / raw)
  To: Kuniyuki Iwashima; +Cc: intel-wired-lan

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] idpf: use-after-free iterating adapter->vports in
  idpf_rx_bufs_init()
- [Medium] idpf: early DMA unmap before sync in idpf_rx_splitq_clean()
--

commit 7eb204337944ee9347e5afd898d0150f3523d291
Author: Kuniyuki Iwashima <kuniyu@google.com>

idpf: Unmap Rx payload pages above mapped threshold under DMA-FQ IOMMU.

This commit adds threshold-based early DMA unmapping for splitq Rx payload
pages in idpf when XDP is disabled on DMA-coherent devices under an
IOMMU_DOMAIN_DMA_FQ domain.  It tracks active descriptor references per page
in a per-bufq ring and calls page_pool_dma_unmap_netmem() once all
descriptors referencing a tracked page complete.

> diff --git a/drivers/net/ethernet/intel/idpf/idpf_txrx.c b/drivers/net/ethernet/intel/idpf/idpf_txrx.c
> index fb6a2ea25dc5d..a21f21cf66ec7 100644
> --- a/drivers/net/ethernet/intel/idpf/idpf_txrx.c
> +++ b/drivers/net/ethernet/intel/idpf/idpf_txrx.c

[ ... ]

> @@ -869,6 +997,59 @@ static int idpf_rx_bufs_init(struct idpf_buf_queue *bufq,
>  	if (ret)
>  		return ret;
>  
> +	if (PP_DMA_INDEX_BITS && !fq.xdp &&
> +	    !dma_dev_need_sync(&vport->adapter->pdev->dev)) {
> +		struct iommu_domain *domain;
> +
> +		domain = iommu_get_domain_for_dev(&vport->adapter->pdev->dev);
> +		if (domain && domain->type == IOMMU_DOMAIN_DMA_FQ) {

[ ... ]

> +				idpf_for_each_vport(vport->adapter, v)
> +					num_bufqs +=
> +						v->dflt_qv_rsrc.num_rxq_grp *
> +						v->dflt_qv_rsrc.num_bufqs_per_qgrp;
> +				num_bufqs = max_t(u32, 1, num_bufqs);

[Severity: Medium]
Can iterating adapter->vports here race with vport teardown on a multi-vport
adapter and trigger a use-after-free?

idpf_open() reaches idpf_rx_bufs_init() holding rtnl_lock and
adapter->vport_ctrl_lock.  On device removal, idpf_remove() ->
idpf_vc_core_deinit() -> idpf_deinit_task() tears down vports sequentially
via idpf_vport_dealloc() without holding either lock across vport
destruction, while sibling netdevs are still registered.

In idpf_vport_dealloc(), idpf_vport_rel() calls kfree(vport) before
adapter->vports[i] is cleared to NULL:

CPU0                              CPU1
idpf_remove()
  idpf_vc_core_deinit()
    idpf_deinit_task()
      idpf_vport_dealloc(...)
        idpf_vport_rel(vports[0])
          kfree(vports[0])
                                  idpf_open(netdevs[1])
                                    ...
                                      idpf_rx_bufs_init()
                                        v->dflt_qv_rsrc...
                                          <-- use-after-free
        adapter->vports[0] = NULL

Dereferencing v->dflt_qv_rsrc on the freed sibling vport causes a slab
use-after-free read.

[ ... ]

> @@ -3948,6 +4129,7 @@ static int idpf_rx_splitq_clean(struct idpf_rx_queue *rxq, int budget)
>  		buf_id = le16_to_cpu(rx_desc->buf_id);
>  
>  		rx_buf = &rx_bufq->buf[buf_id];
> +		idpf_rx_buf_dma_unmap(rx_bufq, buf_id);
>  
>  		if (!coherent_hdr && !rx_bufq->hdr_pp)
>  			goto payload;

[Severity: Medium]
Should idpf_rx_buf_dma_unmap() run after libeth_rx_sync_for_cpu() instead of
before it?

When entry->refs reaches 0, page_pool_dma_unmap_netmem() unmaps the page and
clears netmem->dma_addr to 0.  On an untrusted DMA-coherent PCIe device
switched to IOMMU_DOMAIN_DMA_FQ via sysfs, dma_dev_need_sync() starts false
and flips to true on the first unaligned streaming mapping that uses
swiotlb:

CPU0                              CPU1
idpf_rx_splitq_clean()
  idpf_rx_buf_dma_unmap()
    page_pool_dma_unmap_netmem()
      dma_dev_need_sync() == false
      dma_addr = 0
                                  idpf_tx_splitq_map()
                                    dma_map_single()
                                      swiotlb_tbl_map_single()
                                        dma_reset_need_sync(dev)
  idpf_rx_splitq_clean_skb()
    libeth_rx_sync_for_cpu()
      iommu_dma_sync_single_for_cpu(0 + offset)
        swiotlb_bounce()  <-- corrupts unrelated memory

When host PAGE_SIZE exceeds the IOMMU granule (for example, 64K pages with a
4K granule), sub-page offsets >= 4K land in allocatable IOVA PFNs rather
than the reserved PFN 0.  If that PFN holds an active swiotlb bounce buffer,
swiotlb_bounce() overwrites unrelated kernel memory.

Moving idpf_rx_buf_dma_unmap() after libeth_rx_sync_for_cpu() and before
rx_buf->netmem is cleared avoids syncing a cleared DMA address.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205125237.2290932-1-kuniyu@google.com?part=5

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-05 13:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH v2 iwl-next 4/5] page_pool: Allow drivers to unmap DMA before returning pages Kuniyuki Iwashima
2026-12-05 13:15   ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox