Netdev List
 help / color / mirror / Atom feed
From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: netdev@vger.kernel.org
Cc: magnus.karlsson@intel.com, stfomichev@gmail.com, kuba@kernel.org,
	pabeni@redhat.com, willemb@google.com, matttbe@kernel.org,
	bjorn@kernel.org,
	Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Subject: [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses
Date: Fri,  4 Dec 2026 17:04:11 +0100	[thread overview]
Message-ID: <20261204160411.1167875-1-maciej.fijalkowski@intel.com> (raw)

xsk_buff_add_frag() gets the page of an RX frag via
virt_to_netmem(xdp->data). The UMEM is vmap()ed, so xdp->data is a
vmalloc address and virt_to_page() is not valid for it. With
CONFIG_DEBUG_VIRTUAL, __phys_addr() catches this on the first frame
that spans more than one buffer, in NAPI context:

 kernel BUG at arch/x86/mm/physaddr.c:28!
 Oops: invalid opcode: 0000 [#1] SMP NOPTI
 RIP: 0010:__phys_addr+0x3a/0x50
 Call Trace:
  <IRQ>
  ice_clean_rx_irq_zc+0x16a/0xba0 [ice]
  ice_napi_poll+0x2b3/0xfa0 [ice]
  __napi_poll+0x2c/0x1e0
  net_rx_action+0x122/0x2a0
  handle_softirqs+0x104/0x360
  __irq_exit_rcu+0xb5/0xf0
  common_interrupt+0x81/0xa0
  </IRQ>
 Kernel panic - not syncing: Fatal exception in interrupt

ice, i40e and idpf (through libeth) are affected. This was triggered in
netdev CI, where the debug hosts hit it on E810 and X710 running
drivers/net/xsk.py, added to net-next by commit 4c810fcd484d
("selftests: drv-net: run the AF_XDP tests over veth or a NIC"). Its
rx_SEND_RECEIVE_9K_PACKETS case reproduces it on E810 with
CONFIG_DEBUG_VIRTUAL=y, without kernel changes or error injection.

Without DEBUG_VIRTUAL nothing checks the translation. The frag ends up
with a struct page that does not back the buffer, but on x86-64
page_address() of it yields the original address again, so this went
unnoticed. ice and i40e had the same virt_to_page() open-coded since
v6.6, before the commit below moved it into the helper, so kernels
older than v6.14 need a separate fix in those drivers.

Take the page from the UMEM page array instead, indexed by the offset of
the buffer in the vmap() area. Unlike vmalloc_to_page(), this does not
walk the page tables for every frag. I have not benchmarked whether the
latter would be cheaper or not when compared to proposed solution.

page_address() of the real page points into the linear map, so a frag
that runs into the next page is only accessed correctly if the two
pages are physically contiguous. Aligned chunks never cross a page.
Unaligned ones may cross only where xp_check_dma_contiguity() set
XSK_NEXT_PG_CONTIG_MASK, and it does so by comparing DMA addresses
alone. With a translating IOMMU those say nothing about physical
contiguity, so compare the PFNs as well.

Tested on E810 (ice) with CONFIG_DEBUG_VIRTUAL=y: the 9K case no longer
hits the BUG and the whole xsk.py run passes. i40e and idpf were only
build-tested, and the PFN check was not exercised with a translating
IOMMU.

Fixes: 51205f841a49 ("xsk: make xsk_buff_add_frag() really add the frag via __xdp_buff_add_frag()")
Signed-off-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
---
 include/net/xdp_sock_drv.h |  8 ++++++--
 net/xdp/xsk_buff_pool.c    | 16 +++++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h
index a40ad3f90f13..0e1b9bb72b8c 100644
--- a/include/net/xdp_sock_drv.h
+++ b/include/net/xdp_sock_drv.h
@@ -162,18 +162,22 @@ static inline bool xsk_buff_add_frag(struct xdp_buff *head,
 				     struct xdp_buff *xdp)
 {
 	struct xdp_buff_xsk *frag = container_of(xdp, struct xdp_buff_xsk, xdp);
+	struct xsk_buff_pool *pool = frag->pool;
 	const void *data = xdp->data;
+	struct page *page;
 
 	/* Duplicate fill ring entries map to the same xskb in aligned mode */
 	if (unlikely(xdp == head || !list_empty(&frag->list_node)))
 		return false;
 
-	if (!__xdp_buff_add_frag(head, virt_to_netmem(data),
+	/* data points into the vmap() of the UMEM, not the linear map */
+	page = pool->umem->pgs[(data - pool->addrs) >> PAGE_SHIFT];
+	if (!__xdp_buff_add_frag(head, page_to_netmem(page),
 				 offset_in_page(data), xdp->data_end - data,
 				 xdp->frame_sz, false))
 		return false;
 
-	list_add_tail(&frag->list_node, &frag->pool->xskb_list);
+	list_add_tail(&frag->list_node, &pool->xskb_list);
 
 	return true;
 }
diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
index 5ec092f7cb6f..5891ac6b1f01 100644
--- a/net/xdp/xsk_buff_pool.c
+++ b/net/xdp/xsk_buff_pool.c
@@ -459,15 +459,21 @@ void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
 }
 EXPORT_SYMBOL(xp_dma_unmap);
 
-static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
+static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map,
+				    struct page **pages)
 {
+	dma_addr_t *dma = dma_map->dma_pages;
 	u32 i;
 
+	/* The CPU reaches RX frags through page_address(), so pages also
+	 * need to be physically contiguous when an IOMMU translates.
+	 */
 	for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
-		if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
-			dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
+		if (dma[i] + PAGE_SIZE == dma[i + 1] &&
+		    page_to_pfn(pages[i]) + 1 == page_to_pfn(pages[i + 1]))
+			dma[i] |= XSK_NEXT_PG_CONTIG_MASK;
 		else
-			dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
+			dma[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
 	}
 }
 
@@ -531,7 +537,7 @@ int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
 	}
 
 	if (pool->unaligned)
-		xp_check_dma_contiguity(dma_map);
+		xp_check_dma_contiguity(dma_map, pages);
 
 	err = xp_init_dma_info(pool, dma_map);
 	if (err) {

base-commit: 3c8881ee98069d3c8a1a0ad1fd6efd726e466297
-- 
2.43.0


             reply	other threads:[~2026-12-04 16:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 16:04 Maciej Fijalkowski [this message]
2026-12-04 18:13 ` [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses Björn Töpel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204160411.1167875-1-maciej.fijalkowski@intel.com \
    --to=maciej.fijalkowski@intel.com \
    --cc=bjorn@kernel.org \
    --cc=kuba@kernel.org \
    --cc=magnus.karlsson@intel.com \
    --cc=matttbe@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stfomichev@gmail.com \
    --cc=willemb@google.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