[PATCH v2] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
From: Fang Xieyan
Date: Thu Nov 26 2026 - 02:24:40 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Fang Xieyan <fangxy@xxxxxxxxxxxx>
---
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