From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796116656; b=on++G5MIylydTpc2SU3b34spP8tQLK4ucX2KFWh7bFwvDY9hb0pMv5Q5RDUWdZS0yZnb w5BFsVjGijd3FJoAkwtk5MN/AFayd06truAkKmmt8fRBYQRwjZdf+eCM5NAiI3uBaLDED Id04t3YGzXr8bepW2v1EuIJDM/FAwpy+pvS+RTliPNgbTM33sVC4sfIk1lUGTwky46iEN IWSXgoVtkL+c0Q/b4NtL5kBzMv0+YovZl2nt7Y1T//LUnnOHxgTWFvt510JCISQ98S8/b vkZj7vO0zghf2G5JpSRIL+OWp/5W/210BVO3JTEcFFRoLoh6wfp9DQ5C8Xz5PjUNWXw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796116656; 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-Transfer-Encoding; bh=RA5cRfvLgjxyN8MOK5tPDXfq4Uk3r/GIQo4k9uPncH8=; b=2Uu5tOtG9TM+yvChKuQkSvvGKTsT/TQwK1oys+kNLdWb0ThBzlhyE/THGpuMUFJBeJJR pfvDakVN8Nxhtfs/kbz4s2dN0iqJuxjNcVuORWtGGRRgpEzRBfrFuhwxMNWKMTTs0jjjx 0Vct/C/X/nuqVO6by8/CgUQGsb/elFXXP7PUgEQJGBe4vF6Di1ZYmkaQdl/qORaaz6tG8 m0qAQNIKY3CkqIDc70gSrqm8LRCd+UGF8Xp+tdXf34ThekIe1hFE6CHVpQxSRjxad5mZD P6cLSOqUu/mYy0tp3RV6c6MpVYDUMv590ODSKPyz+6cVb+9f1MlImLc4dKNwPtWfPRQ== 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="U4/TUEpU"; arc=none smtp.remote-ip=115.124.28.4 Received: from out28-4.mail.aliyun.com (out28-4.mail.aliyun.com [115.124.28.4]) (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 0D2B6606A329; Tue, 1 Dec 2026 09:17:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0D2B6606A329 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=U4/TUEpU DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796116646; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=RA5cRfvLgjxyN8MOK5tPDXfq4Uk3r/GIQo4k9uPncH8=; b=U4/TUEpUqlf8TtHTzJxnUAjB3nmg8Wqt5/8STNT067LlL6AFU1HsIGTommasScL3Rrk27J2fratTkB/7EEzC1BonVk4JbNSuNkj2ZLDXfDPjgNxNeVG1MRKP/B6Ow7F3Vaik/d/FoUgOMPL8e7uw2kJTUbM7GaVS8PxQ3cS5ak0= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.04534032|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.00746372-0.000417409-0.992119;FP=3725831658881942771|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033068002237;MF=fangxy@xiaopeng.com;NM=1;PH=DS;RN=11;RT=11;SR=0;TI=SMTPD_---.kE3zImN_1796116645; Received: from localhost.localdomain(mailfrom:fangxy@xiaopeng.com fp:SMTPD_---.kE3zImN_1796116645 cluster:ay29) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 17:17:26 +0800 From: Fang Xieyan To: "Michael S . Tsirkin" , jasowangio@gmail.com, eperezma@redhat.com Cc: stable@vger.kernel.org, fan.jiaming@hotmail.com, xiongweimin@kylinos.cn, xieyongji@bytedance.com, virtualization@lists.linux.dev, kvm@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] vhost-vdpa: use vma_lookup() in vhost_vdpa_va_map() Date: Tue, 1 Dec 2026 17:17:22 +0800 Message-ID: <20261201091722.46805-3-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-Transfer-Encoding: 8bit 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 Signed-off-by: Fang Xieyan --- 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