vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
Subsystems (6)
PATCH 1/1 vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
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.
--- 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.
drivers/vhost/vdpa.c +14 -0
@@ -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 ))))
Signature · 1 line
2.50.1
[email protected]
AI reviewThank 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…
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 <[email protected]>
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.
Quoted 7 lines
drivers/vhost/vdpa.c +0
-0@@ -1154,6 +1154,20 @@ static int vhost_vdpa_va_map(struct vhost_vdpa *v, ret = -EINVAL ;break ;
drivers/vhost/vdpa.c static int vhost_vdpa_va_map(struct vhost_vdpa *v, · line 1165
Quoted 10 lines
drivers/vhost/vdpa.c +9
-0} +/* +* 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);
}drivers/vhost/vdpa.c static int vhost_vdpa_va_map(struct vhost_vdpa *v, · line 1126
Quoted 6 lines
drivers/vhost/vdpa.c +5
-0+ 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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
In reply to Fang Xieyanfind_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.
--- 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.
drivers/vhost/vdpa.c +13 -0
@@ -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 ))))
Signature · 1 line
2.50.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] vhost-vdpa: inconsistent unmapped gap handling in vhost_vdpa_va_map() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] vhost-vdpa: inconsistent unmapped gap handling in vhost_vdpa_va_map()
--
commit 32b0d078e53a97a709c1be4706495eeda3a12b09
Author: Fang Xieyan <[email protected]>
vhost-vdpa: skip unmapped gap in vhost_vdpa_va_map()
This commit skips unmapped virtual address gaps before a VMA in vhost_vdpa_va_map() so the file offset calculation does not underflow when uaddr lies below vma->vm_start.
Quoted 14 lines
drivers/vhost/vdpa.c +6
-0@@ -1154,6 +1154,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;
drivers/vhost/vdpa.c static int vhost_vdpa_va_map(struct vhost_vdpa *v, · line 1172
Quoted 10 lines
drivers/vhost/vdpa.c +7
-0+ +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 ))))
[Severity: Medium]
Should an unmapped gap at the end of [uaddr, uaddr + size) be handled the same way regardless of whether an unrelated VMA exists at a higher address in dev->mm?
find_vma(dev->mm, uaddr) searches up to ULONG_MAX rather than uaddr + size. If an unmapped gap (either the whole request or a trailing portion after valid VMAs) has any unrelated VMA above it in dev->mm, such as the stack or vDSO, find_vma() returns that VMA with gap >= size and the loop exits with ret = 0, keeping any mappings created in earlier iterations. If no VMA exists above uaddr in dev->mm, find_vma() returns NULL, setting ret = -EINVAL and calling vhost_vdpa_unmap() to tear down all valid VMA mappings already established earlier in the same request.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
drivers/vhost/vdpa.c static int vhost_vdpa_va_map(struct vhost_vdpa *v, · line 1120
Quoted 5 lines
drivers/vhost/vdpa.c +5
-0+if (uaddr < vma->vm_start) { +u64 gap = vma->vm_start - uaddr; + +if (gap >= size) +break ;
So if the whole request sits in a hole we just bail out and return 0 with nothing mapped? Userspace has no way to know. Same with a hole in the middle, it quietly ends up with a sparse mapping.
A hole here just means userspace gave us an address with nothing behind it. Why not just use vma_lookup() instead and fail it?
- vma = find_vma(dev->mm, uaddr);
+ vma = vma_lookup(dev->mm, uaddr);
That also gets rid of the "depends on what's mapped above" thing sashiko pointed out, and it's what the PA path does anyway, pin_user_pages() just fails there.
There's another bug here though, in the rollback at the end of vhost_vdpa_va_map():
if (ret) vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
If the very first chunk fails, that's a zero-size unmap, and with iova == 0 vhost_vdpa_unmap() computes last = U64_MAX and wipes every mapping in the AS. This is already broken with find_vma() (a uaddr above all VMAs gets there), vma_lookup() just makes it easier to hit. Reproduced with vdpa_sim under QEMU.
So I'd fix the rollback first, then switch to vma_lookup(). Are you planning a v3? If not I'm happy to pick it up and keep you credited.
Quoted 1 line
So I'd fix the rollback first, then switch to
vma_lookup().
Aha :P
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.
Hi Jiaming,
Quoted 2 lines
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.
Quoted 1 line
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.
Quoted 2 lines
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/[email protected]/
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