🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Fang Xieyan Mon, 23 Nov 2026 14:33
[PATCH] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
#21078
Author: Fang Xieyan
Date: Mon, 23 Nov 2026 14:33
79 lines
11086 bytes
find_vma() returns the first VMA with vm_end > uaddr; when uaddr lies
in an unmapped gap, vma->vm_start > uaddr and the offset arithmetic

	offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;

produces an incorrect file offset, potentially wrapping in u64
arithmetic.

The incorrect offset is stored in map_file->offset and passed to
the mapping backend. Meanwhile, map_size includes the gap, causing
the corresponding IOVA range to be mapped without valid backing.

Skip the gap: stop if the whole remaining request is gap, otherwise
shrink size, pin uaddr to vm_start and advance map_iova by the gap.
The gap is left unmapped without error.

Testing:
- make W=1 drivers/vhost/vdpa.o
- QEMU/KVM v7.4-rc1 with a probe vDPA parent; tested partial
  gap, whole-gap and contiguous IOTLB updates.

Fixes: d8945ec41120 ("vdpa: Support transferring virtual addressing during DMA mapping")
Cc: stable@vger.kernel.org
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
 drivers/vhost/vdpa.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

Reproducer: userspace model of va_map() with a mock find_vma(); shared
file-backed VMA [0x2000,0x3000) at file offset 0, unmapped gap
[0x1000,0x2000).

  UPDATE iova=0x100000 uaddr=0x1000 size=0x2000 (partial gap):
    without fix: ret=0, one map over the gap,
                 file_offset=0xfffffffffffff000
    with fix:    ret=0, map at iova=0x101000 uaddr=0x2000
                 size=0x1000 file_offset=0x0
  UPDATE uaddr=0x1000 size=0x1000 (whole request in gap):
    without fix: ret=0, bogus map with underflowed offset
    with fix:    ret=0, no mapping created

Build: make W=1 drivers/vhost/vdpa.o completed without warnings on
v7.4-rc1 (defconfig + VHOST/VDPA/VHOST_VDPA).

Runtime: On v7.4-rc1 under QEMU/KVM, a userspace test client sent
VHOST_IOTLB_UPDATE messages through the /dev/vhost-vdpa chardev to a
probe vDPA parent with use_va enabled and dma_map logging.  Without
the fix the parent receives offset=0xfffffffffffff000 for gap
requests; with the fix the gap is skipped (map_iova advanced,
offset=0), an all-gap request creates no mapping, and contiguous
requests are unchanged.
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index ff7d907..4a6a946 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -1109,6 +1109,20 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 			ret = -EINVAL;
 			break;
 		}
+		/*
+		 * uaddr may lie in an unmapped gap before vma->vm_start; skip
+		 * the gap so the offset calculation below uses an address in the VMA.
+		 */
+		if (uaddr < vma->vm_start) {
+			u64 gap = vma->vm_start - uaddr;
+
+			if (gap >= size)
+				break;
+			size -= gap;
+			uaddr = vma->vm_start;
+			map_iova += gap;
+			continue;
+		}
 		map_size = min(size, vma->vm_end - uaddr);
 		if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) &&
 			!(vma->vm_flags & (VM_IO | VM_PFNMAP))))
-- 
2.50.1
[PATCH v2] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
#22934
Author: Fang Xieyan
Date: Thu, 26 Nov 2026 07:24
86 lines
12011 bytes
find_vma() returns the first VMA with vm_end > uaddr; when uaddr lies
in an unmapped gap, vma->vm_start > uaddr and the offset arithmetic

	offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;

produces an incorrect file offset, potentially wrapping in u64
arithmetic, and map_size counts the gap as part of the mapping.

Seen at runtime on v7.4-rc1: a request whose uaddr lies 0x1000
below a shared file-backed VMA starting at file offset 0 reaches
the vDPA driver's dma_map() callback as offset=0xfffffffffffff000.
Since vhost_vdpa_map() adds the range to the vhost IOTLB before
calling the backend, a successful mapping keeps an entry covering
the gap, which has no valid backing.

Skip the gap: stop if the whole remaining request is inside the gap,
otherwise shrink size, set uaddr to vm_start and advance map_iova by
the gap.  Gaps before a later VMA are left unmapped without error;
when find_vma() returns NULL because no VMA follows uaddr, the
existing -EINVAL is kept.

Fixes: d8945ec41120 ("vdpa: Support transferring virtual addressing during DMA mapping")
Cc: stable@vger.kernel.org
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
 drivers/vhost/vdpa.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Changes since v1:
- drop the redundant "continue": the already-found VMA is processed
  directly instead of being looked up again; the mappings, the
  return value and the unmap span are unchanged.
- commit message only: quote the offset measured at runtime, note
  that a uaddr no VMA follows keeps -EINVAL, move test records here.

Testing: make W=1 drivers/vhost/vdpa.o completed without warnings on
v7.4-rc1 (defconfig + VHOST/VDPA/VHOST_VDPA).  Under QEMU/KVM a
userspace client sent VHOST_IOTLB_UPDATE through /dev/vhost-vdpa to
a probe vDPA parent with use_va and dma_map logging; its memory is
file-backed VMAs separated by a 0x1000 gap, the upper one at offset
0.  The dma_map log then reads:

  iova=0x100000 size=0x3000 uaddr=0x20001000 (request into the gap):
    unpatched: dma_map iova=0x100000 size=0x3000 offset=0xfffffffffffff000
    patched:   dma_map iova=0x101000 size=0x2000 offset=0x0
  iova=0x400000 size=0x4000 (spans the gap), second segment:
    unpatched: dma_map iova=0x401000 size=0x3000 offset=0xfffffffffffff000
    patched:   dma_map iova=0x402000 size=0x2000 offset=0x0
  iova=0x200000 size=0x1000 (whole request in the gap): no dma_map
    once patched, write() still succeeds
  iova=0x300000 size=0x1000 (contiguous): identical maps in both runs
  iova=0x600000 size=0x3000 (tail past the last VMA): dma_map and
    dma_unmap of the same 0x2000, write() = -1 EFAULT in both runs
  iova=0x500000 size=0x1000 (uaddr above every VMA): no dma_map,
    write() = -1 EFAULT in both runs

vhost_chr_write_iter() turns a non-zero handler return into EFAULT,
so the last two cases fail the same way with and without the patch.

diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index ff7d907..623c2da 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -1109,6 +1109,19 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v,
 			ret = -EINVAL;
 			break;
 		}
+		/*
+		 * uaddr may lie in an unmapped gap before vma->vm_start; skip
+		 * the gap so the offset calculation below uses an address in the VMA.
+		 */
+		if (uaddr < vma->vm_start) {
+			u64 gap = vma->vm_start - uaddr;
+
+			if (gap >= size)
+				break;
+			size -= gap;
+			uaddr = vma->vm_start;
+			map_iova += gap;
+		}
 		map_size = min(size, vma->vm_end - uaddr);
 		if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) &&
 			!(vma->vm_flags & (VM_IO | VM_PFNMAP))))
-- 
2.50.1
Re: [PATCH v2] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
#25135
Author: Fang Xieyan
Date: Tue, 01 Dec 2026 09:41
33 lines
9843 bytes
Hi Jiaming,

> Weimin already sent the rollback fix in July [1]. I've asked him for a
> rebased v2, vma_lookup() can go on top of that.

Done, in that order: patch 1 is Weimin's July change rebased onto
v7.4-rc5 with his authorship and Eugenio's Acked-by kept, patch 2 is
your line, with Suggested-by on it.  The gap-skipping is gone.

> vma_lookup() just makes it easier to hit.

Right, and that is what puts patch 1 in front: with patch 2 alone, the
request that fails on a gap takes every mapping of the address space
with it.  The four-state numbers are in the notes of patch 1.

> Are you planning a v3? If not I'm happy to pick it up and keep you
> credited.

Posted just before this reply, as its own thread so that the cover
carries the defect:

https://lore.kernel.org/all/20261201091722.46805-1-fangxy@xiaopeng.com/

Nothing to pick up, thanks - Weimin has the choice to resend patch 1
himself, which I offered him in the cover.  Patch 1 deliberately
carries no Cc stable, for the reason in its notes; patch 2 does.

Thanks again for the pointer and the repro.

Fang Xieyan

[1] https://lore.kernel.org/all/20260716030238.124368-1-xiongwm2026@163.com/
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads