Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
@ 2026-11-23 14:33 Fang Xieyan
  2026-11-24 14:33 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Fang Xieyan @ 2026-11-23 14:33 UTC (permalink / raw)
  To: Michael S . Tsirkin, Jason Wang, Eugenio Pérez
  Cc: Xie Yongji, stable, virtualization, kvm, netdev, linux-kernel

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
  2026-11-23 14:33 [PATCH] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map() Fang Xieyan
@ 2026-11-24 14:33 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24 14:33 UTC (permalink / raw)
  To: Fang Xieyan; +Cc: virtualization, kvm, Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] vhost-vdpa: inconsistent handling of unmapped gaps in
  vhost_vdpa_va_map()
- [Low] vhost-vdpa: redundant find_vma() call after gap skip in
  vhost_vdpa_va_map()
--

commit 47702b500dd411149b51bf05337d951339f690bc
Author: Fang Xieyan <fangxy@xiaopeng.com>

vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()

This commit skips unmapped virtual address gaps before vma->vm_start in
vhost_vdpa_va_map() so the file offset calculation does not underflow when
uaddr lies below vma->vm_start.

> diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
> index c912ac40ca375..2707be470a1f6 100644
> --- a/drivers/vhost/vdpa.c
> +++ b/drivers/vhost/vdpa.c
> @@ -1154,6 +1154,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;

[Severity: Medium]
Does this handle an unmapped gap at the end of the address space
differently from a gap below a higher VMA?

When the remaining range [uaddr, uaddr + size) is an unmapped gap and any
unrelated VMA exists at a higher address in dev->mm, find_vma() returns
that VMA and gap >= size breaks out of the loop with ret = 0.  If the same
unmapped gap lies above the highest VMA in dev->mm, find_vma() returns
NULL, setting ret = -EINVAL and causing vhost_vdpa_unmap() to tear down
any valid VMA mappings already created earlier in the loop:

drivers/vhost/vdpa.c:vhost_vdpa_va_map() {
	...
	while (size) {
		vma = find_vma(dev->mm, uaddr);
		if (!vma) {
			ret = -EINVAL;
			break;
		}
	...
	if (ret)
		vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
}

> +			size -= gap;
> +			uaddr = vma->vm_start;
> +			map_iova += gap;
> +			continue;
> +		}
>  		map_size = min(size, vma->vm_end - uaddr);

[Severity: Low]
Does the continue statement here cause a redundant find_vma() tree walk on
the next iteration?  Since vma already points to the VMA starting at
vma->vm_start under mmap_read_lock(dev->mm), falling through to map_size
would use vma directly without looking up vma->vm_start a second time.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123143318.73514-1-fangxy@xiaopeng.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 14:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 14:33 [PATCH] vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map() Fang Xieyan
2026-11-24 14:33 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox