From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796116655; b=KgNUtE6j6RhNFEj0wPAUiGnoA4Sn/rfJmo+/RGglmmJDsxb75OVEu7eLBmWshCPC/8kO 9qS4QUqkND+SuUi78vi42pYeEloLQxOqF7QF8QfxkP+/W+GCWMCvvqra2b0QIjGZ2tTOx rkJOReiHMHWpd7dtmEPyYstgoul1BoZg5H5gnOq61diY3Airt40H8drJwpDq0EtPTHBWC Un941Oo/WaqrHMc0RYqG1XlTMiB7jPmESR3lRdDdTVWBfSMShHo3aC0tvWmN6Nz/UhLNV Ijtm+u71VvWaH18I0oqxZytOuyG1dB7KyoGyHK5KBuB/NNrl21ZqkkKsTeFTYvqIU6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796116655; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=3dq6Di7f9wSqov9zKmdrmsEPvO8fHacO/kcB02kKudY=; b=0VgsfMhUyxwDI1p21yEKOkMKJz8u6POfXwcO4BGnBxecTQyKx7jI+Ct/8kujh7L5SVsT LYAlS1QrLntBo42qrboX7hHQuGTAt/yrRNyMDFFXwRYAcZQ76Yza1cslnWNXyTqfWv+j+ njeo8D5BVdDdLnuVvj3GWaxNCvRN2bVph9wBtC7KYt0iCZ5wcY7fK72t4GFx3gsZufdRl XSzKz119nxvd31Hp7MnsruE4KSBGRz3FznWG3GCeGBlGD0wsThbuLH/Qegn1oRuKwVStT fpmJ9f5/QFLNIlzsdY+Zq79j7hICSipUjQ0XpVlGXf1hTVVxrii2k2al/AC+pefqZYA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=xiaopeng.com; spf=pass smtp.mailfrom=xiaopeng.com; dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=MzFZhppD; arc=none smtp.remote-ip=115.124.28.5 Received: from out28-5.mail.aliyun.com (out28-5.mail.aliyun.com [115.124.28.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88735606A31A; Tue, 1 Dec 2026 09:17:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 88735606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=MzFZhppD DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796116645; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=3dq6Di7f9wSqov9zKmdrmsEPvO8fHacO/kcB02kKudY=; b=MzFZhppDPp+PQvG7AGgWxKXb6wg4kfbNPpy8aC2cueLm4IonQb2zSFbESuKzbibUTNtRZtyRzEortR/psrnV6RWSYTLJVev+XaUcO4mgL1hqdpJoGk1xMtiYooBumWprswuw0pTkrsJYT1S78aN1dQd5N+eI8ChMOhZz25VzcVM= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.04478823|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.042023-0.000565291-0.957412;FP=12357548265074853235|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033037071049;MF=fangxy@xiaopeng.com;NM=1;PH=DS;RN=10;RT=10;SR=0;TI=SMTPD_---.kE3zIlV_1796116644; Received: from localhost.localdomain(mailfrom:fangxy@xiaopeng.com fp:SMTPD_---.kE3zIlV_1796116644 cluster:ay29) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 17:17:25 +0800 From: Fang Xieyan To: "Michael S . Tsirkin" , jasowangio@gmail.com, eperezma@redhat.com Cc: xiongweimin , fan.jiaming@hotmail.com, xieyongji@bytedance.com, virtualization@lists.linux.dev, kvm@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] vhost-vdpa: reject zero-size unmap Date: Tue, 1 Dec 2026 17:17:21 +0800 Message-ID: <20261201091722.46805-2-fangxy@xiaopeng.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261201091722.46805-1-fangxy@xiaopeng.com> References: <20261201091722.46805-1-fangxy@xiaopeng.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: xiongweimin 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 Signed-off-by: xiongweimin [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 --- 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