Skip to contentSkip to conversation

xsk: don't call virt_to_netmem() on vmapped UMEM addresses

PATCH In review Maciej Fijalkowski 3 participants 3 messages Dec 4 – Dec 5

Subsystems (3)
lore
Share & tools
lore

Maciej Fijalkowski

author maintainer
#0 lore raw

PATCH 1/1 xsk: don't call virt_to_netmem() on vmapped UMEM addresses

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()")
---
 include/net/xdp_sock_drv.h |  8 ++++++--
 net/xdp/xsk_buff_pool.c    | 16 +++++++++++-----
 2 files changed, 17 insertions(+), 7 deletions(-)
include/net/xdp_sock_drv.h +6 -2
index a40ad3f90f13..0e1b9bb72b8c 100644
@@ -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;
 }
net/xdp/xsk_buff_pool.c +11 -5
index 5ec092f7cb6f..5891ac6b1f01 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw
Quoted 28 lines · 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.

Quoted 6 lines

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!

[...]

Quoted 17 lines
net/xdp/xsk_buff_pool.c +5 -1
index 5ec092f7cb6f..5891ac6b1f01 100644
@@ -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.

net/xdp/xsk_buff_pool.c void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) · line 476

Quoted 10 lines
net/xdp/xsk_buff_pool.c +5 -3
+	 */
 	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

#2 lore raw
In reply to Maciej Fijalkowski
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix's correctness argument, 'page_address() of the real page points into the linear map', is false on 32-bit CONFIG_HIGHMEM kernels.
- [Medium] xp_check_dma_contiguity() (net/xdp/xsk_buff_pool.c:462-478) now sets XSK_NEXT_PG_CONTIG_MASK only when consecutive UMEM pages are…
- [Medium] xsk: PFN check in xp_check_dma_contiguity() ignores KASAN page tags
- [Low] The commit message understates the impact of the pre-patch bug on architectures other than x86-64.

--

xsk: don't call virt_to_netmem() on vmapped UMEM addresses

In xsk_buff_add_frag(), this patch drops virt_to_netmem() on the vmapped UMEM address. The frag page is now taken from umem->pgs[], indexed by the offset into the vmap() area. It also changes xp_check_dma_contiguity() so that XSK_NEXT_PG_CONTIG_MASK is only set when consecutive pages are contiguous in both DMA address and PFN.

Quoted 4 lines

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.

[Severity: Low]

Does this hold on architectures other than x86-64? On arm64 without CONFIG_DEBUG_VIRTUAL, virt_to_page() and page_to_virt() are exact arithmetic inverses, so arm64 gets the same address back too.

On riscv64 that doesn't seem to happen. A vmalloc address is below PAGE_OFFSET, so is_linear_mapping() is false and __va_to_pa_nodebug() applies the kernel image offset:

arch/riscv/include/asm/page.h:

	is_linear_mapping(_x) ?
	linear_mapping_va_to_pa(_x) : kernel_mapping_va_to_pa(_x);

page_to_virt() then translates back through the linear map __va(). The frag therefore points at an unrelated or unmapped kernel address. On 32-bit x86, __pa() of a vmalloc address lands in the highmem or out-of-range PFN area.

On those kernels, XDP_PASS of a multi-buffer ZC frame goes through xdp_build_skb_from_zc()->xdp_copy_frags_from_zc(), which does:

	memcpy(page_address(page) + offset, skb_frag_address(frag),
	       LARGEST_ALIGN(len));

That would either oops or copy unrelated kernel memory into an skb, and remote traffic drives it. The frag paths of bpf_xdp_load_bytes() and bpf_xdp_store_bytes() have the same exposure.

Could the commit message mention this? Without it, stable and CVE triage, including the separate pre-v6.14 ice/i40e backports mentioned here, may rate the bug lower than it is.

Quoted 10 lines
include/net/xdp_sock_drv.h +1 -0
index a40ad3f90f13c..0e1b9bb72b8c9 100644
@@ -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;

include/net/xdp_sock_drv.h static inline bool xsk_buff_add_frag(struct xdp_buff *head, · line 175

Quoted 10 lines
include/net/xdp_sock_drv.h +4 -1
+	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),

[Severity: High]

The commit message says "page_address() of the real page points into the linear map". Is that true on 32-bit CONFIG_HIGHMEM kernels?

umem->pgs[] is filled by xdp_umem_pin_pages():

	npgs = pin_user_pages(address, umem->npgs,
			      gup_flags | FOLL_LONGTERM, &umem->pgs[0]);

These are user pages, so they can come from ZONE_HIGHMEM. ZONE_HIGHMEM pages are long-term pinnable, so FOLL_LONGTERM does not migrate them. The UMEM is only ever vmap()ed, never kmap()ed.

For a highmem page that is not kmapped, page_address() in mm/highmem.c returns NULL. skb_frag_address(frag) then becomes NULL + offset, and the frag consumers dereference it in NAPI context. Examples are the memcpy() in xdp_copy_frags_from_zc(), the frag paths of bpf_xdp_load_bytes() and bpf_xdp_store_bytes(), and the memset in bpf_xdp_frags_increase_tail().

I40E depends only on PCI, and ICE and IDPF only on PCI_MSI, so 32-bit HIGHMEM builds of all three drivers are possible.

The old virt_to_page() on the vmalloc address was broken in this configuration too. Still, the crash class this patch targets, a CPU access to a ZC RX frag through an invalid kernel address, looks reachable here. The new PFN check in xp_check_dma_contiguity() makes the same lowmem-only assumption.

Would it make sense to avoid page_address() for UMEM frags, or to refuse ZC multi-buffer when the UMEM contains highmem pages?

Quoted 3 lines
include/net/xdp_sock_drv.h +0 -0
 				 offset_in_page(data), xdp->data_end - data,
 				 xdp->frame_sz, false))
 		return false;

[ ... ]

Quoted 14 lines
net/xdp/xsk_buff_pool.c +3 -1
index 5ec092f7cb6f7..5891ac6b1f011 100644
@@ -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;

net/xdp/xsk_buff_pool.c void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) · line 474

Quoted 10 lines
net/xdp/xsk_buff_pool.c +6 -2
 
+	/* 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;

[Severity: Medium]
On arm64 with CONFIG_KASAN_SW_TAGS or CONFIG_KASAN_HW_TAGS, is PFN adjacency enough for the CPU to run from pages[i] into pages[i + 1] through page_address(pages[i])?

page_to_virt() on arm64 puts page_kasan_tag(page) in the top byte of the linear-map pointer. __kasan_unpoison_pages(), called from post_alloc_hook(), gives each separately allocated order-0 page its own random tag.

With SW_TAGS, should_skip_kasan_unpoison() never skips this, even for GFP_HIGHUSER_MOVABLE user pages. With HW_TAGS, the pages that FOLL_LONGTERM migrates in xdp_umem_pin_pages() are allocated by migrate_longterm_unpinnable_folios() with GFP_USER | __GFP_NOWARN. That mask has no __GFP_SKIP_KASAN, so those pages get independent tags too.

Two umem->pgs[] entries can therefore have consecutive PFNs and consecutive DMA addresses, get XSK_NEXT_PG_CONTIG_MASK here, and still carry different tags. This needs an unaligned multi-buffer UMEM (XDP_UMEM_UNALIGNED_CHUNK_FLAG with XDP_USE_SG).

xsk_buff_add_frag() stores an RX frag that starts in pages[i] and runs into pages[i + 1] as pages[i] with offset + size > PAGE_SIZE. Every consumer of skb_frag_address() then touches the second page with the first page's tag. Examples are the memcpy() in xdp_copy_frags_from_zc(), xsk_copy_xdp(), bpf_xdp_copy_buf() and bpf_xdp_pointer().

Under SW_TAGS, kasan_check_range() reports a tag mismatch and the copy is not performed. Under HW_TAGS in sync mode, it is a tag check fault in NAPI context.

Would it work to compare linear-map addresses instead of PFNs? That would mean requiring page_address(pages[i]) to be non-NULL and page_address(pages[i]) + PAGE_SIZE to equal page_address(pages[i + 1]). This checks physical contiguity and tag equality together. It still allows crossings inside a compound folio, whose subpages share one tag, and it would not mark highmem pages as contiguous.

[Severity: Medium]

The PFN requirement exists only for CPU access to RX frags through page_address(). XSK_NEXT_PG_CONTIG_MASK has a single reader, xp_desc_crosses_non_contig_pg():

	return pool->dma_pages &&
	       !(pool->dma_pages[addr >> PAGE_SHIFT] & XSK_NEXT_PG_CONTIG_MASK);

Two paths use that reader, and both only need DMA contiguity.

xp_unaligned_validate_desc() checks TX descriptors with it. ZC TX gives the device the IOVA, and the CPU-side TX helpers go through the vmap() in pool->addrs.

xp_check_unaligned()->xp_addr_crosses_non_contig_pg() checks every fill ring address, including pools that never build multi-buffer frags. There the device writes through the IOVA and the CPU reads through xdp->data, which is also the vmap().

With a translating IOMMU, or with swiotlb-xen where DMA addresses are machine addresses, consecutive mappings can be IOVA-contiguous while the pinned pages are not PFN-contiguous. For example, the IOVA rcache magazines are LIFO, so re-mapping after an earlier unmap can return ascending IOVAs.

In that case, are page-crossing TX descriptors and fill entries that the hardware handled correctly before now rejected? TX descriptors would be dropped and counted in tx_invalid_descs without a completion. Fill entries would be dropped and counted in the fq invalid_descs. Sockets without XDP_USE_SG would be affected as well.

Quoted 5 lines
net/xdp/xsk_buff_pool.c +1 -1
 		else
-			dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
+			dma[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
 	}
 }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204160411.1167875-1-maciej.fijalkowski%40intel.com