find_vma() returns the first VMA whose vm_end is past the address and says nothing about vm_start. When uaddr lies in an unmapped gap, vhost_vdpa_va_map() uses that VMA anyway: uaddr - vma->vm_start wraps in u64 arithmetic, so map_file->offset becomes a file offset that belongs to no VMA, and map_size counts the gap towards it. Measured on v7.4-rc5: a request whose uaddr lies in a gap below a file-backed VMA reaches the parent as dma_map with offset=0xfffffffffffff000, and vhost_vdpa_map() has already added the range to the IOTLB at that point, so the address space ends up mapping bytes that no VMA backs. Use vma_lookup(), which returns a VMA only when it holds the address: unmapped bytes then fail the request with -EINVAL, as they do on the physical address path, where pin_user_pages() cannot pin what no VMA covers. When the first lookup fails, map_iova is still iova, so the rollback calls vhost_vdpa_unmap() with size 0: that is what the previous patch of this series closes. Fixes: d8945ec41120 ("vdpa: Support transferring virtual addressing during DMA mapping") Cc: stab…@vger.kernel.org Link: https://lore.kernel.org/all/2026…@xiaopeng.com/ Suggested-by: Jiaming Fan <fan…@hotmail.com> Signed-off-by: Fang Xieyan <fang…@xiaopeng.com> --- Base: v7.4-rc5. Changes since v2: - use vma_lookup() instead of skipping the gap, following Jiaming Fan: skipping leaves a request with a hole in the middle partly mapped, and userspace has no way to tell that from a request that was mapped in full. - drop the check for a gap covering the whole request, since vma_lookup() cannot return a VMA starting above uaddr. - the zero-size rollback is no longer left to another thread: it is patch 1 of this series and it has to come first. Boundary analysis: - vma_lookup() returns a VMA only for vm_start <= uaddr < vm_end, so uaddr - vm_start stays in [0, vm_end - vm_start) and cannot wrap, and map_size = min(size, vm_end - uaddr) is between 1 and size: the loop still advances on every iteration. - a VMA that begins exactly at uaddr is returned, so a request spanning two adjacent VMAs is still mapped as two segments, each with the file offset of its own VMA. - the lookup runs under the mmap_read_lock that the function already takes. - for a request that lies inside one VMA nothing changes at all. - behaviour change: a request whose remaining bytes fall in a gap now fails as a whole with -EINVAL instead of succeeding with the gap left out. Measured on v7.4-rc5 with patch 1 applied: a one page UPDATE with uaddr 0x1000 below a shared file-backed VMA was mapped at file offset 0xfffffffffffff000, and a three page UPDATE spanning VMA-gap-VMA was mapped in two segments, the second one at that offset; both now return -EINVAL, and the segment mapped before the gap is rolled back with its real size. Testing: - QEMU/KVM, the same four vhost_vdpa.ko states as in patch 1: the underflowed file offset (offset=0xfffffffffffff000) shows up in the parent's dma_map calls twice without this patch and not at all with it. - a request that spans two adjacent VMAs is still mapped as two segments, each with the offset of its own VMA, unchanged in all four states. - patch 1 has to precede this one: with this patch alone, the request that fails on a gap takes every mapping of the address space with it, which is what patch 1 prevents. - the defect is silent: the states without this patch show it with no WARNING, BUG or Oops, and the fix adds none. drivers/vhost/vdpa.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c index 6cddd50..ac67e7e 100644 --- a/drivers/vhost/vdpa.c +++ b/drivers/vhost/vdpa.c @@ -1152,7 +1152,7 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v, mmap_read_lock(dev->mm); while (size) { - vma = find_vma(dev->mm, uaddr); + vma = vma_lookup(dev->mm, uaddr); if (!vma) { ret = -EINVAL; break;
-- 2.50.1