From: xiongweimin <xion…@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/2026…@163.com/ Acked-by: Eugenio Pérez <eper…@redhat.com> Signed-off-by: xiongweimin <xion…@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 <fang…@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