* [PATCH v3 1/2] vhost-vdpa: reject zero-size unmap
2026-12-01 9:17 [PATCH v3 0/2] vhost-vdpa: reject mappings over unmapped VA gaps Fang Xieyan
@ 2026-12-01 9:17 ` Fang Xieyan
2026-12-01 9:17 ` [PATCH v3 2/2] vhost-vdpa: use vma_lookup() in vhost_vdpa_va_map() Fang Xieyan
1 sibling, 0 replies; 3+ messages in thread
From: Fang Xieyan @ 2026-12-01 9:17 UTC (permalink / raw)
To: Michael S . Tsirkin, jasowangio, eperezma
Cc: xiongweimin, fan.jiaming, xieyongji, virtualization, kvm, netdev,
linux-kernel
From: xiongweimin <xiongweimin@kylinos.cn>
Reject unmap requests with size == 0 to prevent iova + size - 1
from underflowing to U64_MAX, which would incorrectly unmap the
entire IOTLB range.
This fix also covers the error rollback path in vhost_vdpa_va_map:
when the first VMA lookup fails, map_iova equals iova, resulting
in a zero-size unmap that would otherwise clear the whole IOTLB.
Fixes: 4c8cf31885f6 ("vhost: introduce vDPA-based backend")
Link: https://lore.kernel.org/all/20260716030238.124368-1-xiongwm2026@163.com/
Acked-by: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: xiongweimin <xiongweimin@kylinos.cn>
[Fang Xieyan: acbf36deb53d ("vdpa: invalidate DMA mappings before
releasing memory") made vhost_vdpa_unmap() return an int and
moved the set_map branch below the range calculation, so the
guard returns 0 now and sits after the declarations. Added the
Fixes tag. No other change.]
Signed-off-by: Fang Xieyan <fangxy@xiaopeng.com>
---
Base: v7.4-rc5.
Resend of Weimin Xiong's 2026-07-16 patch with his authorship and
Eugenio's Acked-by kept, so that patch 2 has something to build on.
The v3 numbers the series; the change itself is posted for the first
time since July. acbf36deb53d ("vdpa: invalidate DMA mappings before
releasing memory", v7.4-rc2) made vhost_vdpa_unmap() return int, so
the July hunk no longer applies, and dropped into v7.4-rc5 unchanged
it does not compile:
drivers/vhost/vdpa.c:1122:3: error: 'return' with no value, in
function returning non-void [-Werror=return-type]
Still open on v7.4-rc5: the userspace entry point is covered,
vhost_vdpa_process_iotlb_msg() rejects a zero-size INVALIDATE at
vdpa.c:1385, but the error rollback of vhost_vdpa_va_map() reaches
the sink at vdpa.c:1183 and passes size 0 whenever the first lookup
fails, which is also what a VDUSE-shaped set_map-only parent sees.
No Cc stable: ca50ec377c2e9 ("vhost: Allow null msg.size on
VHOST_IOTLB_INVALIDATE", v6.6, Cc stable # v5.17+) restored the null
size as the supported way to invalidate a whole range, and until
acbf36deb53d the INVALIDATE case hands msg->size to
vhost_vdpa_unmap() unchecked, so the wrap to U64_MAX is what
qemu/smmuv3/vhost relies on there. A guard at the sink would take
that away from v6.6 and the trees ca50ec377c2e9 reached, where the
check belongs at the rollback call site instead. From v7.4-rc2 on no
caller has a use for size 0 here: the INVALIDATE case rejects it
before the call and vhost_vdpa_remove_as() asks for the whole range.
Testing:
- QEMU/KVM on v7.4-rc5: one kernel image, two probe vdpa devices,
four vhost_vdpa.ko states built from the shipped .patch files
(pristine, this patch, patch 2, this patch + patch 2).
- A one page UPDATE with iova 0 whose first VMA lookup fails is
rejected with -EINVAL and, without this patch, removes every
mapping of the address space: two mappings the request does not
name are gone (re-adding them succeeds), the parent gets two
dma_unmap calls for them, and the set_map-only parent is
reprogrammed with an empty IOTLB (set_map, nmaps 0). With this
patch the same request only fails.
- An INVALIDATE with a real size still unmaps what it names.
- The wipe is silent: the states without this patch lose those
mappings with no WARNING, BUG or Oops, and the fix adds none.
drivers/vhost/vdpa.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/vhost/vdpa.c b/drivers/vhost/vdpa.c
index c912ac4..6cddd50 100644
--- a/drivers/vhost/vdpa.c
+++ b/drivers/vhost/vdpa.c
@@ -1113,6 +1113,9 @@ static int vhost_vdpa_unmap(struct vhost_vdpa *v,
u64 last = iova + size - 1;
int ret;
+ if (!size)
+ return 0;
+
if (ops->set_map && !ops->dma_map) {
vhost_iotlb_init(&new_iotlb, iotlb->limit, iotlb->flags);
list_for_each_entry(map, &iotlb->list, link) {
--
2.50.1
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH v3 2/2] vhost-vdpa: use vma_lookup() in vhost_vdpa_va_map()
2026-12-01 9:17 [PATCH v3 0/2] vhost-vdpa: reject mappings over unmapped VA gaps Fang Xieyan
2026-12-01 9:17 ` [PATCH v3 1/2] vhost-vdpa: reject zero-size unmap Fang Xieyan
@ 2026-12-01 9:17 ` Fang Xieyan
1 sibling, 0 replies; 3+ messages in thread
From: Fang Xieyan @ 2026-12-01 9:17 UTC (permalink / raw)
To: Michael S . Tsirkin, jasowangio, eperezma
Cc: stable, fan.jiaming, xiongweimin, xieyongji, virtualization, kvm,
netdev, linux-kernel
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: stable@vger.kernel.org
Link: https://lore.kernel.org/all/20261126072414.97199-1-fangxy@xiaopeng.com/
Suggested-by: Jiaming Fan <fan.jiaming@hotmail.com>
Signed-off-by: Fang Xieyan <fangxy@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
^ permalink raw reply [flat|nested] 3+ messages in thread