From nobody Sun Feb 8 05:59:08 2026 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD9522BEC44 for ; Wed, 8 Oct 2025 04:09:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896578; cv=none; b=OVTgFobBrPPjsmENsQUXRmJZmflr81tdXTYo/gOUd/JD+5Ovo35PHwgtFXNwOY/wOcmoDn9BVFv0F/1Fviv6OhSQG9WseHuRsGHMoay3PFnHPKmvbTJuF357TKB25MXj5ULlZbBXTLmffglngrd1LPnj9ObBTg76JPz1muFOwGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896578; c=relaxed/simple; bh=BFgmjXhlqUfV9g5aZf0Fpbp8t42vT5eiICIa6HmsPns=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=dN5elcuneUD7GAB7kRzAis/amDHau/RhQAZrflAGSw1iB9NcmGDfsiUDyHLkA5EZuk1KcGA5vDDgxo5IjBuoHf8lpQbBV7Nx/KkXPtUuc6YzriJ1aOkdhvCUmnMH7wmEK1b5vmOVBhXvesui+pzkCbI0WfRpjcZK9RZuqIbjXKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b=BtfM/lav; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b="BtfM/lav" Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5982rAJU2065805 for ; Tue, 7 Oct 2025 21:09:35 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=Zp9koOgnd/LMXr9KpmioHND3MH+op8celO7bHBPPISE=; b=BtfM/lavktxy 6ByZiGRWpmHIaO06iWfyRY6UvPGSactOPkpexKHZckBg+wgQHzwo9nXcfcqrWakG cTRH5oliHyzoqlycwf+DJSrpg6CDgUur0oIZ3YPcu29AT0mq+hfdD2fai8ovr3zj Vak1mZY4pR2+Bpr+QdKevnaJlqyB0o//JYWRS4bjHzOkbLgsgmwxrX898qanDWku w+priJpn16EOsj5dqz0ngln67WrxiheLxWjflVpLT69RrX3/qTabEAtPFYtyNS1+ zEFh6QfrxOf7NJbxMXbsgZ2B+NB3Sfx0bsdQuVPS6hf3y81w+uCsRwPqyJ/vg6Bv FTAuyaj99A== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 49n7jxuhyy-7 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 07 Oct 2025 21:09:35 -0700 (PDT) Received: from twshared76339.05.prn6.facebook.com (2620:10d:c0a8:1b::30) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Wed, 8 Oct 2025 04:09:33 +0000 Received: by devgpu015.cco6.facebook.com (Postfix, from userid 199522) id B17FAD26CCE; Tue, 7 Oct 2025 21:09:19 -0700 (PDT) From: Alex Mastro Date: Tue, 7 Oct 2025 21:08:46 -0700 Subject: [PATCH v2 1/3] vfio/type1: sanitize for overflow using check_*_overflow Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID: <20251007-fix-unmap-v2-1-759bceb9792e@fb.com> References: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> In-Reply-To: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> To: Alex Williamson CC: Jason Gunthorpe , Alejandro Jimenez , , , Alex Mastro X-Mailer: b4 0.13.0 X-FB-Internal: Safe X-Proofpoint-GUID: Ty2nKLoC4ohYlIApsS98xSffNbZpbICH X-Proofpoint-ORIG-GUID: Ty2nKLoC4ohYlIApsS98xSffNbZpbICH X-Authority-Analysis: v=2.4 cv=LJdrgZW9 c=1 sm=1 tr=0 ts=68e5e3ff cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=IkcTkHD0fZMA:10 a=x6icFKpwvdMA:10 a=FOH2dFAWAAAA:8 a=J-xk3WMZyPGmlAw7FewA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMDA4MDAyNCBTYWx0ZWRfX2l4JMp6rG8sl CSB2CGfncIWIcX3MrQHvJvV27fEI8zwaiegNdF0wVOTNLHlbGdJAR72BKqr9JxDM2Ck8JK8zgyr ByZaFaVQ4wh1C0cR6KMjFB7juT0BMNibUZb/FYoVgmOPN9tjEJ/H2IfiI4BoizKSyHrxHN2Dug/ UuVgSgFwHBtZ4xcPgc8hHlSYiA9c7huHcS2JhUAOPNE53SVKQ44FldTaFaA7FQdcCczrB7THCMM XS/uVSxQiece8GgaNCk2rjErb364gG7TokyUehSw2FsqUK2B17HlSNxS6SS2Cr3e4TpT5iwLeMz yTTKM0kgp58Vx3DiIyiKPcmxtKd3fFQJ/GwlfUdYPVHYesF9L4/Av6ZryQr1PM+2JCco+E92iZs C28aS/AHCoRjIlSrhagasT7GZjmdWw== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1117,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-10-07_02,2025-10-06_01,2025-03-28_01 Adopt check_*_overflow functions to clearly express overflow check intent. Signed-off-by: Alex Mastro --- drivers/vfio/vfio_iommu_type1.c | 54 ++++++++++++++++++++++++++++++++-----= ---- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type= 1.c index f8d68fe77b41..b510ef3f397b 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "vfio.h" =20 #define DRIVER_VERSION "0.2" @@ -825,14 +826,25 @@ static int vfio_iommu_type1_pin_pages(void *iommu_dat= a, unsigned long remote_vaddr; struct vfio_dma *dma; bool do_accounting; + dma_addr_t iova_end; + size_t iova_size; =20 - if (!iommu || !pages) + if (!iommu || !pages || npage < 0) return -EINVAL; =20 /* Supported for v2 version only */ if (!iommu->v2) return -EACCES; =20 + if (npage =3D=3D 0) + return 0; + + if (check_mul_overflow(npage, PAGE_SIZE, &iova_size)) + return -EINVAL; + + if (check_add_overflow(user_iova, iova_size - 1, &iova_end)) + return -EINVAL; + mutex_lock(&iommu->lock); =20 if (WARN_ONCE(iommu->vaddr_invalid_count, @@ -938,12 +950,23 @@ static void vfio_iommu_type1_unpin_pages(void *iommu_= data, { struct vfio_iommu *iommu =3D iommu_data; bool do_accounting; + dma_addr_t iova_end; + size_t iova_size; int i; =20 /* Supported for v2 version only */ if (WARN_ON(!iommu->v2)) return; =20 + if (WARN_ON(npage < 0) || npage =3D=3D 0) + return; + + if (WARN_ON(check_mul_overflow(npage, PAGE_SIZE, &iova_size))) + return; + + if (WARN_ON(check_add_overflow(user_iova, iova_size - 1, &iova_end))) + return; + mutex_lock(&iommu->lock); =20 do_accounting =3D list_empty(&iommu->domain_list); @@ -1304,6 +1327,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, int ret =3D -EINVAL, retries =3D 0; unsigned long pgshift; dma_addr_t iova =3D unmap->iova; + dma_addr_t iova_end; u64 size =3D unmap->size; bool unmap_all =3D unmap->flags & VFIO_DMA_UNMAP_FLAG_ALL; bool invalidate_vaddr =3D unmap->flags & VFIO_DMA_UNMAP_FLAG_VADDR; @@ -1328,7 +1352,8 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, goto unlock; size =3D U64_MAX; } else if (!size || size & (pgsize - 1) || - iova + size - 1 < iova || size > SIZE_MAX) { + check_add_overflow(iova, size - 1, &iova_end) || + size > SIZE_MAX) { goto unlock; } =20 @@ -1376,7 +1401,7 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu, if (dma && dma->iova !=3D iova) goto unlock; =20 - dma =3D vfio_find_dma(iommu, iova + size - 1, 0); + dma =3D vfio_find_dma(iommu, iova_end, 0); if (dma && dma->iova + dma->size !=3D iova + size) goto unlock; } @@ -1578,7 +1603,9 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, { bool set_vaddr =3D map->flags & VFIO_DMA_MAP_FLAG_VADDR; dma_addr_t iova =3D map->iova; + dma_addr_t iova_end; unsigned long vaddr =3D map->vaddr; + unsigned long vaddr_end; size_t size =3D map->size; int ret =3D 0, prot =3D 0; size_t pgsize; @@ -1588,6 +1615,12 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, if (map->size !=3D size || map->vaddr !=3D vaddr || map->iova !=3D iova) return -EINVAL; =20 + if (check_add_overflow(iova, size - 1, &iova_end)) + return -EINVAL; + + if (check_add_overflow(vaddr, size - 1, &vaddr_end)) + return -EINVAL; + /* READ/WRITE from device perspective */ if (map->flags & VFIO_DMA_MAP_FLAG_WRITE) prot |=3D IOMMU_WRITE; @@ -1608,12 +1641,6 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto out_unlock; } =20 - /* Don't allow IOVA or virtual address wrap */ - if (iova + size - 1 < iova || vaddr + size - 1 < vaddr) { - ret =3D -EINVAL; - goto out_unlock; - } - dma =3D vfio_find_dma(iommu, iova, size); if (set_vaddr) { if (!dma) { @@ -1640,7 +1667,7 @@ static int vfio_dma_do_map(struct vfio_iommu *iommu, goto out_unlock; } =20 - if (!vfio_iommu_iova_dma_valid(iommu, iova, iova + size - 1)) { + if (!vfio_iommu_iova_dma_valid(iommu, iova, iova_end)) { ret =3D -EINVAL; goto out_unlock; } @@ -2908,6 +2935,7 @@ static int vfio_iommu_type1_dirty_pages(struct vfio_i= ommu *iommu, unsigned long pgshift; size_t data_size =3D dirty.argsz - minsz; size_t iommu_pgsize; + dma_addr_t range_end; =20 if (!data_size || data_size < sizeof(range)) return -EINVAL; @@ -2916,8 +2944,12 @@ static int vfio_iommu_type1_dirty_pages(struct vfio_= iommu *iommu, sizeof(range))) return -EFAULT; =20 - if (range.iova + range.size < range.iova) + if (range.size =3D=3D 0) + return 0; + + if (check_add_overflow(range.iova, range.size - 1, &range_end)) return -EINVAL; + if (!access_ok((void __user *)range.bitmap.data, range.bitmap.size)) return -EINVAL; --=20 2.47.3 From nobody Sun Feb 8 05:59:08 2026 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FC782BE646 for ; Wed, 8 Oct 2025 04:09:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896567; cv=none; b=AbOFmUTg5nkZTGoGDsuUSK3n6YrvOruwQ1WjkhjqhcwDFuhWekXi3m6AEM4y2lOsyB2RX40AlnOaJlVqNq+Xi1YTMWpHukVSKBAEYEU5YMZSIHjavBqZafw6Qay6F+kgSD28iuDlXwpSniAtQmkmvc+HjEqE1fuwwa722ANY644= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896567; c=relaxed/simple; bh=Sc7RyZvHVnsiWmJJsfqHgDMcvPy6kEMQ+FShuIOWcvI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=n/kb3PGmXZexPZsXComyk0w19QPQDn7K0RtbjrEYbwxsRtLrKw7Cy0XRSg2iDDLy9t4dKSNNvpkIuhKZpY+Y7F6RlR6+s2b0s01OgeXdQ2yHYEA4g9TxpsClVlH1r/duuaeT1upMBPSSPLSQc8VuLQApiOFMjRoGRbOAiMqlYs4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b=Q8bzn0rg; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b="Q8bzn0rg" Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 597IKvDX1854453 for ; Tue, 7 Oct 2025 21:09:25 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=/DPR3MVV9m01+QHNDlVKZ/bjjUFkh+IA4QwkKhO8QSE=; b=Q8bzn0rgeH4O n55ohN99Df8Hk5AwINotNnYHCnOJu8v/J7QTglLi8KyvpQ7N2SsLVirQRoRvJQ0w KzCF8u2RnmY/yWAsXQiUHcDlrit3ooYs99t3etE4hm52yoOXl/zllThzk1npoNTz LFO51MzvMf8dIFRZ5f0d18oDjK17X/yKu8i/gfagEXwBbQW+Wy8DbnIe/I+ofFy1 7y1qZ88ZFoobu0VU03d2mImtFL0VpWdWrGJPP5x1BREZC6aOD38AmCp9MSz2UPTL aukNecOBSOtf/VMfJJVG8NapCi8yH+qotRcMuo/uDoEpmUacQQRmFsVStqskzD+a 9zWpLk5Ndw== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 49n82g394p-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 07 Oct 2025 21:09:25 -0700 (PDT) Received: from twshared28243.32.prn2.facebook.com (2620:10d:c0a8:1c::11) by mail.thefacebook.com (2620:10d:c0a9:6f::237c) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Wed, 8 Oct 2025 04:09:23 +0000 Received: by devgpu015.cco6.facebook.com (Postfix, from userid 199522) id B1A19D26CCF; Tue, 7 Oct 2025 21:09:19 -0700 (PDT) From: Alex Mastro Date: Tue, 7 Oct 2025 21:08:47 -0700 Subject: [PATCH v2 2/3] vfio/type1: move iova increment to unmap_unpin_* caller Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID: <20251007-fix-unmap-v2-2-759bceb9792e@fb.com> References: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> In-Reply-To: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> To: Alex Williamson CC: Jason Gunthorpe , Alejandro Jimenez , , , Alex Mastro X-Mailer: b4 0.13.0 X-FB-Internal: Safe X-Proofpoint-ORIG-GUID: qMj5S8sJD6nMwDErhG_HITDwSPYBA_vO X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMDA4MDAyNCBTYWx0ZWRfX9T84Qw3iQJVv TLTXRlb3K5QSNBNwIFejtngdH8NEUfApOOkfyGGE7XJhQDa+pp0JGnT4qAsVVIAUbuDaokNErT0 GFVcDw4sdPymdFKtzDUdtBiHdmijbq5ikeN334t6rOJWd2xBa2ZINBBBUXGmB9UX7JNb77VUwo+ 2BwY2ZmNGxKg6M/KAuGkEo+A+3hqV2DLhNB3veCHBo9xmxpWxZx6XYda2XYDdA/j79LQcbGrucH gxZgKGuZwBPs+xg9qoO8u2INNjeYNDLiZ2z0lwXhU03ZEiCXMeRUce+r5HGkeHBu76pgCdA/Bo3 96WXVw2sedKMnav9wckKjznxL8A6jCsN4uILvfzsl4KzqGdOCaILnMl0xzmzV+e6bAPkJvk1Kdq swgwFPShJWDDMscnKvTjj91014eZhw== X-Proofpoint-GUID: qMj5S8sJD6nMwDErhG_HITDwSPYBA_vO X-Authority-Analysis: v=2.4 cv=C5rkCAP+ c=1 sm=1 tr=0 ts=68e5e3f5 cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=IkcTkHD0fZMA:10 a=x6icFKpwvdMA:10 a=FOH2dFAWAAAA:8 a=Bkl4-CcWoNymJLHaMFkA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1117,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-10-07_02,2025-10-06_01,2025-03-28_01 Move incrementing iova to the caller of these functions as part of preparing to handle end of address space map/unmap. Signed-off-by: Alex Mastro --- drivers/vfio/vfio_iommu_type1.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type= 1.c index b510ef3f397b..f6ba2f8b1dd8 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -1020,7 +1020,7 @@ static long vfio_sync_unpin(struct vfio_dma *dma, str= uct vfio_domain *domain, #define VFIO_IOMMU_TLB_SYNC_MAX 512 =20 static size_t unmap_unpin_fast(struct vfio_domain *domain, - struct vfio_dma *dma, dma_addr_t *iova, + struct vfio_dma *dma, dma_addr_t iova, size_t len, phys_addr_t phys, long *unlocked, struct list_head *unmapped_list, int *unmapped_cnt, @@ -1030,18 +1030,17 @@ static size_t unmap_unpin_fast(struct vfio_domain *= domain, struct vfio_regions *entry =3D kzalloc(sizeof(*entry), GFP_KERNEL); =20 if (entry) { - unmapped =3D iommu_unmap_fast(domain->domain, *iova, len, + unmapped =3D iommu_unmap_fast(domain->domain, iova, len, iotlb_gather); =20 if (!unmapped) { kfree(entry); } else { - entry->iova =3D *iova; + entry->iova =3D iova; entry->phys =3D phys; entry->len =3D unmapped; list_add_tail(&entry->list, unmapped_list); =20 - *iova +=3D unmapped; (*unmapped_cnt)++; } } @@ -1060,18 +1059,17 @@ static size_t unmap_unpin_fast(struct vfio_domain *= domain, } =20 static size_t unmap_unpin_slow(struct vfio_domain *domain, - struct vfio_dma *dma, dma_addr_t *iova, + struct vfio_dma *dma, dma_addr_t iova, size_t len, phys_addr_t phys, long *unlocked) { - size_t unmapped =3D iommu_unmap(domain->domain, *iova, len); + size_t unmapped =3D iommu_unmap(domain->domain, iova, len); =20 if (unmapped) { - *unlocked +=3D vfio_unpin_pages_remote(dma, *iova, + *unlocked +=3D vfio_unpin_pages_remote(dma, iova, phys >> PAGE_SHIFT, unmapped >> PAGE_SHIFT, false); - *iova +=3D unmapped; cond_resched(); } return unmapped; @@ -1134,16 +1132,18 @@ static long vfio_unmap_unpin(struct vfio_iommu *iom= mu, struct vfio_dma *dma, * First, try to use fast unmap/unpin. In case of failure, * switch to slow unmap/unpin path. */ - unmapped =3D unmap_unpin_fast(domain, dma, &iova, len, phys, + unmapped =3D unmap_unpin_fast(domain, dma, iova, len, phys, &unlocked, &unmapped_region_list, &unmapped_region_cnt, &iotlb_gather); if (!unmapped) { - unmapped =3D unmap_unpin_slow(domain, dma, &iova, len, + unmapped =3D unmap_unpin_slow(domain, dma, iova, len, phys, &unlocked); if (WARN_ON(!unmapped)) break; } + + iova +=3D unmapped; } =20 dma->iommu_mapped =3D false; --=20 2.47.3 From nobody Sun Feb 8 05:59:08 2026 Received: from mx0b-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AEE22BE658 for ; Wed, 8 Oct 2025 04:09:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896576; cv=none; b=AO9+lPEO/Q+idczsb75x2JtQcbMMsO53d0TLej63jfDUHSIanYthbYM8dd2YfiSqHQldeWObWOEZfR4NQJVXGzZCZt0k/I1qQ2bxhZRaMihBGbQUsSJBnEb/SSOm2L5NediWeOfcM0mGwnzJiWsWU/qTrbuAlX4kHeyANnMBLko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759896576; c=relaxed/simple; bh=+2r3aolF8oHS3ozQGgTTzrAvpqn+VbWDh4dAMQglIgE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=JQkYfJ9EShKUYvaXQmcA66jWsuFCKt7cIEO78Y5lhXc2qX3u8pYUgKanQf7Be6R0MJhvZLx2XUHNS9zqMWMJbQd4Dq754VQge3ahfIhDfQHVwBzA62AXgzTR6Z56g0aIFcxorNVPYZ8LglNMOG26992KyDiPJS/jsPziFEBb9rQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b=Z00LVryI; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=fb.com header.i=@fb.com header.b="Z00LVryI" Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5982rAJQ2065805 for ; Tue, 7 Oct 2025 21:09:34 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2025-q2; bh=rZ+bhDDbOAqkVzre+Zt7TEGFheBx2O8g8tJrdNoweQ0=; b=Z00LVryIE1qM EXwR1YULXezuO+uQXY1JISsmAYLzsJzS0alOKf4LCvXvVKq9H8e90MlQf5HAIfov kKKkF3cB77RSV6rM4qUojBdGI8upfIZwl9zNaGZ5Lnf0jLfxytjca9vzmzG1Y8G+ NzxXSm77oI91fqarMmO69tPebI6GMsI04riOYlMWgPjkZe3sgda3d5yS5RHWKkGc 4vnLAeBIZkNjPI00S4VsMfSGevTCn7HcW3qXPz/5g4+wI2VCo+26p4xMJwWJkeF0 GX13/2ZxkZpITogSOe+p7UA7DDqIZTZnXSzCmBTVEt5tdg4c1BZbRBhKKPiR4VRV 17btT4nUkw== Received: from maileast.thefacebook.com ([163.114.135.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 49n7jxuhyy-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 07 Oct 2025 21:09:33 -0700 (PDT) Received: from twshared23637.05.prn5.facebook.com (2620:10d:c0a8:1b::2d) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Wed, 8 Oct 2025 04:09:31 +0000 Received: by devgpu015.cco6.facebook.com (Postfix, from userid 199522) id B1BF6D26CD0; Tue, 7 Oct 2025 21:09:19 -0700 (PDT) From: Alex Mastro Date: Tue, 7 Oct 2025 21:08:48 -0700 Subject: [PATCH v2 3/3] vfio/type1: handle DMA map/unmap up to the addressable limit Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-ID: <20251007-fix-unmap-v2-3-759bceb9792e@fb.com> References: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> In-Reply-To: <20251007-fix-unmap-v2-0-759bceb9792e@fb.com> To: Alex Williamson CC: Jason Gunthorpe , Alejandro Jimenez , , , Alex Mastro X-Mailer: b4 0.13.0 X-FB-Internal: Safe X-Proofpoint-GUID: ruONPKQcR4ulfyFPWCjW4qYYktICMtX8 X-Proofpoint-ORIG-GUID: ruONPKQcR4ulfyFPWCjW4qYYktICMtX8 X-Authority-Analysis: v=2.4 cv=LJdrgZW9 c=1 sm=1 tr=0 ts=68e5e3fd cx=c_pps a=MfjaFnPeirRr97d5FC5oHw==:117 a=MfjaFnPeirRr97d5FC5oHw==:17 a=IkcTkHD0fZMA:10 a=x6icFKpwvdMA:10 a=FOH2dFAWAAAA:8 a=P34euavMcYKjluB8mAYA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMDA4MDAyNCBTYWx0ZWRfXylwBNVy5+XSZ KQtUFBh6ToMQ+9Xejmm7LsT0J07yYFvDFH4CVv1Dy7PklW8HERUmu3ThNrUH0JvUsF/Bs3wtSrq eKzgEdiWHONfkgMEcf11ZD25n2woUznLRhvoxb0xbtflqyaCjywqojn3IBCdxRBSZMu/RXYK8DY xnxMu5hAW6rdXfRZDb/Vv/kOPmE34axpSQI23Qb0nSNft0nEOA9E/My5t1WeAEegnDQmZAKVR4x tK3EbllfQXeDlGIaTBNYMgTqJqWb0SpksXzG3NrM/w/sSUu5E5r5IINL4Oo5ZV+UWghbGcVKL9k fXc3IHn+PBYhjHswn9LKbSxVtDxgTqW88vE0UNhpdTJKFIneIptSH2XUNLM0U6GOsJkpz6fspKD KpwoY7o9nEJWFU0VSuFYamxBrsnpzA== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1117,Hydra:6.1.9,FMLib:17.12.80.40 definitions=2025-10-07_02,2025-10-06_01,2025-03-28_01 Handle DMA map/unmap operations up to the addressable limit by comparing against inclusive end-of-range limits, and changing iteration to perform relative traversals across range sizes, rather than absolute traversals across addresses. vfio_link_dma inserts a zero-sized vfio_dma into the rb-tree, and is only used for that purpose, so discard the size from consideration for the insertion point. Signed-off-by: Alex Mastro --- drivers/vfio/vfio_iommu_type1.c | 75 ++++++++++++++++++++++---------------= ---- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type= 1.c index f6ba2f8b1dd8..d0eec4b39d40 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -166,12 +166,14 @@ static struct vfio_dma *vfio_find_dma(struct vfio_iom= mu *iommu, { struct rb_node *node =3D iommu->dma_list.rb_node; =20 + WARN_ON(!size); + while (node) { struct vfio_dma *dma =3D rb_entry(node, struct vfio_dma, node); =20 - if (start + size <=3D dma->iova) + if (start + size - 1 < dma->iova) node =3D node->rb_left; - else if (start >=3D dma->iova + dma->size) + else if (start > dma->iova + dma->size - 1) node =3D node->rb_right; else return dma; @@ -181,16 +183,19 @@ static struct vfio_dma *vfio_find_dma(struct vfio_iom= mu *iommu, } =20 static struct rb_node *vfio_find_dma_first_node(struct vfio_iommu *iommu, - dma_addr_t start, u64 size) + dma_addr_t start, + dma_addr_t end) { struct rb_node *res =3D NULL; struct rb_node *node =3D iommu->dma_list.rb_node; struct vfio_dma *dma_res =3D NULL; =20 + WARN_ON(end < start); + while (node) { struct vfio_dma *dma =3D rb_entry(node, struct vfio_dma, node); =20 - if (start < dma->iova + dma->size) { + if (start <=3D dma->iova + dma->size - 1) { res =3D node; dma_res =3D dma; if (start >=3D dma->iova) @@ -200,7 +205,7 @@ static struct rb_node *vfio_find_dma_first_node(struct = vfio_iommu *iommu, node =3D node->rb_right; } } - if (res && size && dma_res->iova >=3D start + size) + if (res && dma_res->iova > end) res =3D NULL; return res; } @@ -210,11 +215,13 @@ static void vfio_link_dma(struct vfio_iommu *iommu, s= truct vfio_dma *new) struct rb_node **link =3D &iommu->dma_list.rb_node, *parent =3D NULL; struct vfio_dma *dma; =20 + WARN_ON(new->size !=3D 0); + while (*link) { parent =3D *link; dma =3D rb_entry(parent, struct vfio_dma, node); =20 - if (new->iova + new->size <=3D dma->iova) + if (new->iova <=3D dma->iova) link =3D &(*link)->rb_left; else link =3D &(*link)->rb_right; @@ -1078,12 +1085,12 @@ static size_t unmap_unpin_slow(struct vfio_domain *= domain, static long vfio_unmap_unpin(struct vfio_iommu *iommu, struct vfio_dma *dm= a, bool do_accounting) { - dma_addr_t iova =3D dma->iova, end =3D dma->iova + dma->size; struct vfio_domain *domain, *d; LIST_HEAD(unmapped_region_list); struct iommu_iotlb_gather iotlb_gather; int unmapped_region_cnt =3D 0; long unlocked =3D 0; + size_t pos =3D 0; =20 if (!dma->size) return 0; @@ -1107,13 +1114,14 @@ static long vfio_unmap_unpin(struct vfio_iommu *iom= mu, struct vfio_dma *dma, } =20 iommu_iotlb_gather_init(&iotlb_gather); - while (iova < end) { + while (pos < dma->size) { size_t unmapped, len; phys_addr_t phys, next; + dma_addr_t iova =3D dma->iova + pos; =20 phys =3D iommu_iova_to_phys(domain->domain, iova); if (WARN_ON(!phys)) { - iova +=3D PAGE_SIZE; + pos +=3D PAGE_SIZE; continue; } =20 @@ -1122,7 +1130,7 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu= , struct vfio_dma *dma, * may require hardware cache flushing, try to find the * largest contiguous physical memory chunk to unmap. */ - for (len =3D PAGE_SIZE; iova + len < end; len +=3D PAGE_SIZE) { + for (len =3D PAGE_SIZE; pos + len < dma->size; len +=3D PAGE_SIZE) { next =3D iommu_iova_to_phys(domain->domain, iova + len); if (next !=3D phys + len) break; @@ -1143,7 +1151,7 @@ static long vfio_unmap_unpin(struct vfio_iommu *iommu= , struct vfio_dma *dma, break; } =20 - iova +=3D unmapped; + pos +=3D unmapped; } =20 dma->iommu_mapped =3D false; @@ -1235,7 +1243,7 @@ static int update_user_bitmap(u64 __user *bitmap, str= uct vfio_iommu *iommu, } =20 static int vfio_iova_dirty_bitmap(u64 __user *bitmap, struct vfio_iommu *i= ommu, - dma_addr_t iova, size_t size, size_t pgsize) + dma_addr_t iova, dma_addr_t iova_end, size_t pgsize) { struct vfio_dma *dma; struct rb_node *n; @@ -1252,8 +1260,8 @@ static int vfio_iova_dirty_bitmap(u64 __user *bitmap,= struct vfio_iommu *iommu, if (dma && dma->iova !=3D iova) return -EINVAL; =20 - dma =3D vfio_find_dma(iommu, iova + size - 1, 0); - if (dma && dma->iova + dma->size !=3D iova + size) + dma =3D vfio_find_dma(iommu, iova_end, 1); + if (dma && dma->iova + dma->size - 1 !=3D iova_end) return -EINVAL; =20 for (n =3D rb_first(&iommu->dma_list); n; n =3D rb_next(n)) { @@ -1262,7 +1270,7 @@ static int vfio_iova_dirty_bitmap(u64 __user *bitmap,= struct vfio_iommu *iommu, if (dma->iova < iova) continue; =20 - if (dma->iova > iova + size - 1) + if (dma->iova > iova_end) break; =20 ret =3D update_user_bitmap(bitmap, iommu, dma, iova, pgsize); @@ -1401,17 +1409,17 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iom= mu, if (dma && dma->iova !=3D iova) goto unlock; =20 - dma =3D vfio_find_dma(iommu, iova_end, 0); - if (dma && dma->iova + dma->size !=3D iova + size) + dma =3D vfio_find_dma(iommu, iova_end, 1); + if (dma && dma->iova + dma->size - 1 !=3D iova_end) goto unlock; } =20 ret =3D 0; - n =3D first_n =3D vfio_find_dma_first_node(iommu, iova, size); + n =3D first_n =3D vfio_find_dma_first_node(iommu, iova, iova_end); =20 while (n) { dma =3D rb_entry(n, struct vfio_dma, node); - if (dma->iova >=3D iova + size) + if (dma->iova > iova_end) break; =20 if (!iommu->v2 && iova > dma->iova) @@ -1740,12 +1748,12 @@ static int vfio_iommu_replay(struct vfio_iommu *iom= mu, =20 for (; n; n =3D rb_next(n)) { struct vfio_dma *dma; - dma_addr_t iova; + size_t pos =3D 0; =20 dma =3D rb_entry(n, struct vfio_dma, node); - iova =3D dma->iova; =20 - while (iova < dma->iova + dma->size) { + while (pos < dma->size) { + dma_addr_t iova =3D dma->iova + pos; phys_addr_t phys; size_t size; =20 @@ -1761,14 +1769,14 @@ static int vfio_iommu_replay(struct vfio_iommu *iom= mu, phys =3D iommu_iova_to_phys(d->domain, iova); =20 if (WARN_ON(!phys)) { - iova +=3D PAGE_SIZE; + pos +=3D PAGE_SIZE; continue; } =20 size =3D PAGE_SIZE; p =3D phys + size; i =3D iova + size; - while (i < dma->iova + dma->size && + while (pos + size < dma->size && p =3D=3D iommu_iova_to_phys(d->domain, i)) { size +=3D PAGE_SIZE; p +=3D PAGE_SIZE; @@ -1776,9 +1784,8 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, } } else { unsigned long pfn; - unsigned long vaddr =3D dma->vaddr + - (iova - dma->iova); - size_t n =3D dma->iova + dma->size - iova; + unsigned long vaddr =3D dma->vaddr + pos; + size_t n =3D dma->size - pos; long npage; =20 npage =3D vfio_pin_pages_remote(dma, vaddr, @@ -1809,7 +1816,7 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, goto unwind; } =20 - iova +=3D size; + pos +=3D size; } } =20 @@ -1826,29 +1833,29 @@ static int vfio_iommu_replay(struct vfio_iommu *iom= mu, unwind: for (; n; n =3D rb_prev(n)) { struct vfio_dma *dma =3D rb_entry(n, struct vfio_dma, node); - dma_addr_t iova; + size_t pos =3D 0; =20 if (dma->iommu_mapped) { iommu_unmap(domain->domain, dma->iova, dma->size); continue; } =20 - iova =3D dma->iova; - while (iova < dma->iova + dma->size) { + while (pos < dma->size) { + dma_addr_t iova =3D dma->iova + pos; phys_addr_t phys, p; size_t size; dma_addr_t i; =20 phys =3D iommu_iova_to_phys(domain->domain, iova); if (!phys) { - iova +=3D PAGE_SIZE; + pos +=3D PAGE_SIZE; continue; } =20 size =3D PAGE_SIZE; p =3D phys + size; i =3D iova + size; - while (i < dma->iova + dma->size && + while (pos + size < dma->size && p =3D=3D iommu_iova_to_phys(domain->domain, i)) { size +=3D PAGE_SIZE; p +=3D PAGE_SIZE; @@ -2981,7 +2988,7 @@ static int vfio_iommu_type1_dirty_pages(struct vfio_i= ommu *iommu, if (iommu->dirty_page_tracking) ret =3D vfio_iova_dirty_bitmap(range.bitmap.data, iommu, range.iova, - range.size, + range_end, range.bitmap.pgsize); else ret =3D -EINVAL; --=20 2.47.3