Netdev List
 help / color / mirror / Atom feed
* [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses
@ 2026-12-04 16:04 Maciej Fijalkowski
  2026-12-04 18:13 ` Björn Töpel
  0 siblings, 1 reply; 2+ messages in thread
From: Maciej Fijalkowski @ 2026-12-04 16:04 UTC (permalink / raw)
  To: netdev
  Cc: magnus.karlsson, stfomichev, kuba, pabeni, willemb, matttbe,
	bjorn, Maciej Fijalkowski

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


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

* Re: [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses
  2026-12-04 16:04 [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses Maciej Fijalkowski
@ 2026-12-04 18:13 ` Björn Töpel
  0 siblings, 0 replies; 2+ messages in thread
From: Björn Töpel @ 2026-12-04 18:13 UTC (permalink / raw)
  To: Maciej Fijalkowski, netdev
  Cc: magnus.karlsson, stfomichev, kuba, pabeni, willemb, matttbe,
	Maciej Fijalkowski

Maciej Fijalkowski <maciej.fijalkowski@intel.com> writes:

> 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.

Ha! Fun times! You ended up in the direct map on the way back!

[...]

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

Hmm, aren't we being overly pessimistic for the Tx side now? Tx needs
DMA contig, but the requirement of contig pfn is only for the odd frag
behavior.

Would it be possible to do the pfn config check in the fill ring
validation, or would this have effect on the performance more than here?

I'm OK with this, but I think we might be regressing on a case that used
to work for Tx. OTOH, we'll get a weird asymmetry.

Then again, who uses unaligned! ;-)

(Let's see what Sashiko says -- I'm leaning towards being happy with
this!)


Happy weekend!
Björn

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

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

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:04 [PATCH net] xsk: don't call virt_to_netmem() on vmapped UMEM addresses Maciej Fijalkowski
2026-12-04 18:13 ` Björn Töpel

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