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: [email protected]
Signed-off-by: Fang Xieyan <[email protected]>
---
 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


Reply via email to