From nobody Sun Feb 8 02:26:33 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 121822F8BEE; Tue, 11 Nov 2025 14:52:35 +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=1762872757; cv=none; b=WjQYIw+zKt1STEE4jiHi0Rf9himPWNJ2uIwyZoeAuYAH7L5v5gmJAClM8HfGFD5lwOQHVw23WJEuVi0OzpzJVvu2Ug3zJCBscxc5AZGl4gKnHx5vk99MhZzNVE6qvxs0vRaTDTXrHJmMUG66Wi70B1n47pA/eAtIWZ0Igc9GvLU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762872757; c=relaxed/simple; bh=cveI6e+XRZtUA4ELFU9FZTPydsO9z748yMb6+miUQ2A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=ZxoysltmYnBZifeXpvkMe8Dci5vHtO4x84d/CX2vX+DasuJC0TsFY7oRYbqELCx6IJPczjEpsMrmXHM1q/S7WvKLHgN/+WHrsJLO4U0lhO9YzwoSvtfbVkK/ICW05t2KqFxnx4nqg5DIsK55kM8upZ4/pVumFp9/zHddmkACBFE= 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=EbjVct4i; 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="EbjVct4i" Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5AB7SXNH1282474; Tue, 11 Nov 2025 06:52:30 -0800 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=sEOiaSvVzfpd+hSpNRMwujtxgU5Idw1uQYgrA454INM=; b=EbjVct4iIdCX k37YehMnhype00xF14c1i3oC9G5fkj+ITC39M0W8Qwt3wgaCqdrLMTCr5AwcdIE9 dQPm67HyLRMftrJszGffgMCN2U8hBs/hIriw/A8JrCHdV4H6Gwvo0zJ7q+HnZDQc +viHzS2r8csn2BpHZokfaBspZppWROPCMzDve87hhQElq40+d0jfq/Oj2bZrfGD9 r5Pesxqos9DinavZZc87Wx+ghUiFsDaCz/lmvTr8s+H3s6rsrI2vW5MWTG/MdRD+ MaPsIECWOGAhCCCZDu4qwkIWRdGRtAcxFRnj8pvN+alTIGmyyRkT84o+ASpT0v0Q lSpcHm8C2A== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4ac0shadt9-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 11 Nov 2025 06:52:30 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Tue, 11 Nov 2025 14:52:29 +0000 From: Alex Mastro Date: Tue, 11 Nov 2025 06:52:02 -0800 Subject: [PATCH v2 1/4] vfio: selftests: add iova range query helpers 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: <20251111-iova-ranges-v2-1-0fa267ff9b78@fb.com> References: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> In-Reply-To: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> To: Alex Williamson , David Matlack , Shuah Khan , Jason Gunthorpe CC: , , , Alex Mastro X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=Pf/yRyhd c=1 sm=1 tr=0 ts=69134dae cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=GlSvj1RGuABl-Q5FckAA:9 a=QEXdDO2ut3YA:10 a=cPQSjfK2_nFv0Q5t_7PE:22 X-Proofpoint-GUID: rCxQo8KyXoD9PzuHyPS0ZRC5NC24Aww6 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTExMDExOSBTYWx0ZWRfX6q7+DxqGGUNn h06a/uEPDL/maGKfhrs/xkKiebUkePj8zaTdxBR3g2V6ScNuFBlJcZz9rBondCddILcxiYBOkNB 8eWgmPNNyn7ucFc6tf8O9RbenFR1LxWBWyCbf7hvlg2y75sbaUJdd1iwnp2DRIhdtgc/a2ccArD Gfg6fIFQFQEaybuelWgKktmx5Q4OSMohQj3HvMkdf5U14njM9PdZsyIwQo1Wu6K7v6kGaG1IIcI e0SFD0OmikXnRjNFLkgTrwxIIY6B3hqIW1gWim9IqKae0ME/tY8JsHXcyi+oFxWOWDnHjpKwXEv kFw0wsoceTtM/1fb/6JUsnUybHVOa2WpTiQBjbw4Bre7twYnhEk2XT5idDprwLTZTG27IYazjEM lnph3lwCLgMaW/kDypxK0nTwaJNz3Q== X-Proofpoint-ORIG-GUID: rCxQo8KyXoD9PzuHyPS0ZRC5NC24Aww6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-11_02,2025-11-11_02,2025-10-01_01 VFIO selftests need to map IOVAs from legally accessible ranges, which could vary between hardware. Tests in vfio_dma_mapping_test.c are making excessively strong assumptions about which IOVAs can be mapped. Add vfio_iommu_iova_ranges(), which queries IOVA ranges from the IOMMUFD or VFIO container associated with the device. The queried ranges are normalized to IOMMUFD's iommu_iova_range representation so that handling of IOVA ranges up the stack can be implementation-agnostic. iommu_iova_range and vfio_iova_range are equivalent, so bias to using the new interface's struct. Query IOMMUFD's ranges with IOMMU_IOAS_IOVA_RANGES. Query VFIO container's ranges with VFIO_IOMMU_GET_INFO and VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGE. The underlying vfio_iommu_type1_info buffer-related functionality has been kept generic so the same helpers can be used to query other capability chain information, if needed. Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- .../testing/selftests/vfio/lib/include/vfio_util.h | 8 +- tools/testing/selftests/vfio/lib/vfio_pci_device.c | 167 +++++++++++++++++= ++++ 2 files changed, 174 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/t= esting/selftests/vfio/lib/include/vfio_util.h index 240409bf5f8a..ef8f06ef0c13 100644 --- a/tools/testing/selftests/vfio/lib/include/vfio_util.h +++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h @@ -4,9 +4,12 @@ =20 #include #include -#include + +#include +#include #include #include +#include =20 #include "../../../kselftest.h" =20 @@ -206,6 +209,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char= *bdf, const char *iommu_ void vfio_pci_device_cleanup(struct vfio_pci_device *device); void vfio_pci_device_reset(struct vfio_pci_device *device); =20 +struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pci_device *devi= ce, + u32 *nranges); + int __vfio_pci_dma_map(struct vfio_pci_device *device, struct vfio_dma_region *region); int __vfio_pci_dma_unmap(struct vfio_pci_device *device, diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index a381fd253aa7..7a523e3f2dce 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -29,6 +29,173 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #= _arg, __ret); \ } while (0) =20 +static struct vfio_info_cap_header *next_cap_hdr(void *buf, size_t bufsz, + size_t *cap_offset) +{ + struct vfio_info_cap_header *hdr; + + if (!*cap_offset) + return NULL; + + VFIO_ASSERT_LT(*cap_offset, bufsz); + VFIO_ASSERT_GE(bufsz - *cap_offset, sizeof(*hdr)); + + hdr =3D (struct vfio_info_cap_header *)((u8 *)buf + *cap_offset); + + if (hdr->next) + VFIO_ASSERT_GT(hdr->next, *cap_offset); + + *cap_offset =3D hdr->next; + + return hdr; +} + +static struct vfio_info_cap_header *vfio_iommu_info_cap_hdr(struct vfio_io= mmu_type1_info *info, + u16 cap_id) +{ + struct vfio_info_cap_header *hdr; + size_t cap_offset =3D info->cap_offset; + + if (!(info->flags & VFIO_IOMMU_INFO_CAPS)) + return NULL; + + if (cap_offset) + VFIO_ASSERT_GE(cap_offset, sizeof(struct vfio_iommu_type1_info)); + + while ((hdr =3D next_cap_hdr(info, info->argsz, &cap_offset))) { + if (hdr->id =3D=3D cap_id) + return hdr; + } + + return NULL; +} + +/* Return buffer including capability chain, if present. Free with free() = */ +static struct vfio_iommu_type1_info *vfio_iommu_get_info(struct vfio_pci_d= evice *device) +{ + struct vfio_iommu_type1_info *info; + + info =3D malloc(sizeof(*info)); + VFIO_ASSERT_NOT_NULL(info); + + *info =3D (struct vfio_iommu_type1_info) { + .argsz =3D sizeof(*info), + }; + + ioctl_assert(device->container_fd, VFIO_IOMMU_GET_INFO, info); + + info =3D realloc(info, info->argsz); + VFIO_ASSERT_NOT_NULL(info); + + ioctl_assert(device->container_fd, VFIO_IOMMU_GET_INFO, info); + + return info; +} + +/* + * Return iova ranges for the device's container. Normalize vfio_iommu_typ= e1 to + * report iommufd's iommu_iova_range. Free with free(). + */ +static struct iommu_iova_range *vfio_iommu_iova_ranges(struct vfio_pci_dev= ice *device, + u32 *nranges) +{ + struct vfio_iommu_type1_info_cap_iova_range *cap_range; + struct vfio_iommu_type1_info *info; + struct vfio_info_cap_header *hdr; + struct iommu_iova_range *ranges =3D NULL; + + info =3D vfio_iommu_get_info(device); + hdr =3D vfio_iommu_info_cap_hdr(info, VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANG= E); + VFIO_ASSERT_NOT_NULL(hdr); + + cap_range =3D container_of(hdr, struct vfio_iommu_type1_info_cap_iova_ran= ge, header); + VFIO_ASSERT_GT(cap_range->nr_iovas, 0); + + ranges =3D calloc(cap_range->nr_iovas, sizeof(*ranges)); + VFIO_ASSERT_NOT_NULL(ranges); + + for (u32 i =3D 0; i < cap_range->nr_iovas; i++) { + ranges[i] =3D (struct iommu_iova_range){ + .start =3D cap_range->iova_ranges[i].start, + .last =3D cap_range->iova_ranges[i].end, + }; + } + + *nranges =3D cap_range->nr_iovas; + + free(info); + return ranges; +} + +/* Return iova ranges of the device's IOAS. Free with free() */ +static struct iommu_iova_range *iommufd_iova_ranges(struct vfio_pci_device= *device, + u32 *nranges) +{ + struct iommu_iova_range *ranges; + int ret; + + struct iommu_ioas_iova_ranges query =3D { + .size =3D sizeof(query), + .ioas_id =3D device->ioas_id, + }; + + ret =3D ioctl(device->iommufd, IOMMU_IOAS_IOVA_RANGES, &query); + VFIO_ASSERT_EQ(ret, -1); + VFIO_ASSERT_EQ(errno, EMSGSIZE); + VFIO_ASSERT_GT(query.num_iovas, 0); + + ranges =3D calloc(query.num_iovas, sizeof(*ranges)); + VFIO_ASSERT_NOT_NULL(ranges); + + query.allowed_iovas =3D (uintptr_t)ranges; + + ioctl_assert(device->iommufd, IOMMU_IOAS_IOVA_RANGES, &query); + *nranges =3D query.num_iovas; + + return ranges; +} + +static int iova_range_comp(const void *a, const void *b) +{ + const struct iommu_iova_range *ra =3D a, *rb =3D b; + + if (ra->start < rb->start) + return -1; + + if (ra->start > rb->start) + return 1; + + return 0; +} + +/* Return sorted IOVA ranges of the device. Free with free(). */ +struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pci_device *devi= ce, + u32 *nranges) +{ + struct iommu_iova_range *ranges; + + if (device->iommufd) + ranges =3D iommufd_iova_ranges(device, nranges); + else + ranges =3D vfio_iommu_iova_ranges(device, nranges); + + if (!ranges) + return NULL; + + VFIO_ASSERT_GT(*nranges, 0); + + /* Sort and check that ranges are sane and non-overlapping */ + qsort(ranges, *nranges, sizeof(*ranges), iova_range_comp); + VFIO_ASSERT_LT(ranges[0].start, ranges[0].last); + + for (u32 i =3D 1; i < *nranges; i++) { + VFIO_ASSERT_LT(ranges[i].start, ranges[i].last); + VFIO_ASSERT_LT(ranges[i - 1].last, ranges[i].start); + } + + return ranges; +} + iova_t __to_iova(struct vfio_pci_device *device, void *vaddr) { struct vfio_dma_region *region; --=20 2.47.3 From nobody Sun Feb 8 02:26:33 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 974A32DCC04; Tue, 11 Nov 2025 14:52:36 +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=1762872758; cv=none; b=azkFqdpGl3WAW1VqDj1KpbOnY1gN8QNRuDlYAuozLb/QPg3c1mnjRYGCSPsT68M/5Gghi4Fvkr19Tt7r402ZxpGj+DntBTi75c+qkFYQGUVYHF/dRjg7zbZyTz69xoO6aBSKNQYGmjL5rZFE2WwUXwmW1NeU48R2UEpeRN7Zn70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762872758; c=relaxed/simple; bh=2gk6Lgwb9ciOn6mgd/GeO4X6ZmYktuz1kT1SX9M7haM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=Ltt84lWfJEXqUWZICpdjG6B2dnPvJPMJzmGtAaXy1J0MLaBdbX0zfNjAyy+DoJi+3ePE1QsZZtPX//RB3urgr/psVe0lo3+RXjPE064XXO+OLK9POeni1pM61BifcF9wYWZYSk8Fq5cI9KGIE5PLZb+QAT9aPlsBlldhKcsRCSM= 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=QpPymCyv; 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="QpPymCyv" Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5AB7SXNI1282474; Tue, 11 Nov 2025 06:52:30 -0800 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=V+eeGZWAGviVYfF/KZNUFNM+nih9w63Wwi9agXNGmio=; b=QpPymCyvwXn3 7wEWBSt78czivy+ETfIkfoB/et+UCkNo+FUEtUU3G6PNB241ChPzd2c3WqVZwaAp XXCbT+8ndTpYGBSFHI5OAb6LKO51wdPp/CYxj2a4geHFHZR1qm5+oi5xQ9xUcRGL NQyIa1WJMBeVYmC1/2zcQA9d/fKm7t8W692/1g9xGQzm4A1XpzKzBBetqdtKHRcc YFg/Tj3jwQb1DtqDB744uw50X1Sww6a/Cx3O3LgUdYuvgP2odLpliR7WcnCeeyUN G3hl3sg60WyF9q2oR2J45wlOJ1PBQL7wkpQbUdRyLR7GRKA/kYeVLg7cioBvTkJr 9yiH38rXzw== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4ac0shadt9-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 11 Nov 2025 06:52:30 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Tue, 11 Nov 2025 14:52:29 +0000 From: Alex Mastro Date: Tue, 11 Nov 2025 06:52:03 -0800 Subject: [PATCH v2 2/4] vfio: selftests: fix map limit tests to use last available iova 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: <20251111-iova-ranges-v2-2-0fa267ff9b78@fb.com> References: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> In-Reply-To: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> To: Alex Williamson , David Matlack , Shuah Khan , Jason Gunthorpe CC: , , , Alex Mastro X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=Pf/yRyhd c=1 sm=1 tr=0 ts=69134dae cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=VoiLvOrikwrOPNCf7twA:9 a=QEXdDO2ut3YA:10 a=cPQSjfK2_nFv0Q5t_7PE:22 X-Proofpoint-GUID: ybIUioFS8mDL99FNbWxCBBn06QskcB0N X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTExMDExOSBTYWx0ZWRfX51J8btF0AG84 1qTOOQkX43d+h0YA8Ar/FHcELzJ4xCOrqMJbZ5SjgW6dWMun9tWZuvWIExHprhjmyi8ibXbErHR SHXb/xq0FIPHK+mOzqUGF3g/jy3sALKqFIZq/x0eRQQkcL+922CZOGTmj7o7eZc5uFtrzsmEvvT pOStYji7AoMYOiP5dZ0YzPGUJg2BYHqiAzIyNxZjKedfsbp/jx9imNjXxZ36o6VOFy+rtu6bsxW q+E+khvDOFbdlfP7K16M+Sbw/9rtcUPg1vNxGNlGBhFiVNvjLqtSUUQIVyQfw1QXptbk2dw8TJi eHKgi81mu5oKgwssW7gAk84yjVqvHnpRbMnlp+fc2Zks5QgfUxs+HA9ERblpT8wWOyAtGxIZOvm AQMO9L9apk2SoLkokG0uZ4d0gXQaQg== X-Proofpoint-ORIG-GUID: ybIUioFS8mDL99FNbWxCBBn06QskcB0N X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-11_02,2025-11-11_02,2025-10-01_01 Use the newly available vfio_pci_iova_ranges() to determine the last legal IOVA, and use this as the basis for vfio_dma_map_limit_test tests. Fixes: de8d1f2fd5a5 ("vfio: selftests: add end of address space DMA map/unm= ap tests") Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index 4f1ea79a200c..e1374aab96bd 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -3,6 +3,8 @@ #include #include =20 +#include +#include #include #include #include @@ -219,7 +221,10 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(); FIXTURE_SETUP(vfio_dma_map_limit_test) { struct vfio_dma_region *region =3D &self->region; + struct iommu_iova_range *ranges; u64 region_size =3D getpagesize(); + iova_t last_iova; + u32 nranges; =20 /* * Over-allocate mmap by double the size to provide enough backing vaddr @@ -232,8 +237,13 @@ FIXTURE_SETUP(vfio_dma_map_limit_test) MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); ASSERT_NE(region->vaddr, MAP_FAILED); =20 - /* One page prior to the end of address space */ - region->iova =3D ~(iova_t)0 & ~(region_size - 1); + ranges =3D vfio_pci_iova_ranges(self->device, &nranges); + VFIO_ASSERT_NOT_NULL(ranges); + last_iova =3D ranges[nranges - 1].last; + free(ranges); + + /* One page prior to the last iova */ + region->iova =3D last_iova & ~(region_size - 1); region->size =3D region_size; } =20 @@ -276,6 +286,7 @@ TEST_F(vfio_dma_map_limit_test, overflow) struct vfio_dma_region *region =3D &self->region; int rc; =20 + region->iova =3D ~(iova_t)0 & ~(region->size - 1); region->size =3D self->mmap_size; =20 rc =3D __vfio_pci_dma_map(self->device, region); --=20 2.47.3 From nobody Sun Feb 8 02:26:33 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 CFF6728D829; Tue, 11 Nov 2025 14:52:36 +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=1762872758; cv=none; b=iaxGpY0bgccrz3nIOAz8CdX9/EcX3Dtq90TcObSxOpV/9Phwbmj5BdpMUNiOgygm4kqZvqfuTgk+oLgefKfEkkPcbYn3DYo7HBpGakSeV3sDUUbbvu6ZcxUBVZNtSI0WJHgz7+/Vbma1VgsknfgfDTKlsyYH/lmo90nYYUCnaeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762872758; c=relaxed/simple; bh=VhDxEgPasP8lYcHW4kqVTS8L3eb6Y4YGcg+MMM89DkM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=aqdzpqV749Q3cw+O72km+xqUFqSISie7kqQN0T0Q3REEmI2LfeHnk7bsKKnJ+TORA8lyFg9okPx/85AKsf+wxdiTcS6fo0E30r4w4IxLMPIf4sE80Xd4wHTmC52F6WY/1MnsnGswQRqG2f0f/xXyEO7T1FysHUZzN7SzqbhFdRk= 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=3Z2hhjTl; 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="3Z2hhjTl" Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5AB7SXNJ1282474; Tue, 11 Nov 2025 06:52:31 -0800 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=4ZaUVck6S6J48bKwbMsq4gVF+BpB0wOt3hBiWKgOrPw=; b=3Z2hhjTl4oIW 46KZEOTc7kT9U/ZKxSRsFFqAWcauL2nq+ARfDhwq6birPXMz5rEStPfA0hqptcWQ Xqezqg16hhvlJ+B2irkRyRbgox0zpq9DxYpl9TDk8HrwfJgY0oM02ptWiED8BLNP FGq4/aoetdKgCotuxS2gqAP6mmy2mFRdl1kVsCTO3wUKUHO00H4HypHV4hohIQcx UtoQCGThPGmpG6yPorCFxHkp5pylLKGYBPk0RnhpPL+tJXiOkh2EEWwe+3+GtDYM tGyeeY70PAwMGX1zM7Mge4HD9zEZLyvl8jmYssC9dfip04iQtvuqf7AXnXaiWIQ5 UPNrIr87/w== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4ac0shadt9-4 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 11 Nov 2025 06:52:30 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Tue, 11 Nov 2025 14:52:29 +0000 From: Alex Mastro Date: Tue, 11 Nov 2025 06:52:04 -0800 Subject: [PATCH v2 3/4] vfio: selftests: add iova allocator 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: <20251111-iova-ranges-v2-3-0fa267ff9b78@fb.com> References: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> In-Reply-To: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> To: Alex Williamson , David Matlack , Shuah Khan , Jason Gunthorpe CC: , , , Alex Mastro X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=Pf/yRyhd c=1 sm=1 tr=0 ts=69134dae cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=VwQbUJbxAAAA:8 a=r1p2_3pzAAAA:8 a=FOH2dFAWAAAA:8 a=jdgmLsG8W1E4BNsLh0EA:9 a=QEXdDO2ut3YA:10 a=r_pkcD-q9-ctt7trBg_g:22 a=cPQSjfK2_nFv0Q5t_7PE:22 X-Proofpoint-GUID: YEFXIK-Mia_QKM5caj7scJhaAcB7zsiE X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTExMDExOSBTYWx0ZWRfX5qdHqNwDnwzu n6prwavDGeRM0Hvx2U9d1uqmUudd6ykFlI3UUZzMJ/Pu8pImNKctfL4oFBjQUzmeZaQYzUFvGqE wHZBJivHJc2M14J6MRuCrBI0BLNAOJL0p/UUX7PzlWlaHlu7l6VLwJ1/L3zsgqfcoQ9CHRpX95O PxA9DUB265VbbcPihLEAznX3N4x0e70rAN91o5t1T2GYKj8yep3np1Kh2Q0aeszcEMqC6Lbs6f1 tk4Jg9ezrYoyXMoI0kay5Ffi6oPmGVH0X32DzY9DDIcLFNrGiwrxj6aIboc3swB0UdJKEuGj/Hm k5xdt8c1OqtXwZ0O5xT3tMlUr0opBJbWiUaRsuGZzPXbQH5SuJ4W+H0NYxmvWDth9Qv/XClkg7h cldegDLMQBl9T/7g3LGvULmYl4A8og== X-Proofpoint-ORIG-GUID: YEFXIK-Mia_QKM5caj7scJhaAcB7zsiE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-11_02,2025-11-11_02,2025-10-01_01 Add struct iova_allocator, which gives tests a convenient way to generate legally-accessible IOVAs to map. This allocator traverses the sorted available IOVA ranges linearly, requires power-of-two size allocations, and does not support freeing iova allocations. The assumption is that tests are not IOVA space-bounded, and will not need to recycle IOVAs. This is based on Alex Williamson's patch series for adding an IOVA allocator [1]. [1] https://lore.kernel.org/all/20251108212954.26477-1-alex@shazbot.org/ Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- The unfolded code uses David's range_offset suggestion because it makes assignment of the updated state simpler when crossing a range boundary. No more ALIGN(). The insight is that the initial check for sufficient space (pre-alignment) requires computing last =3D iova + size - 1 which is already half of what ALIGN() does. Just masking is left. --- --- .../testing/selftests/vfio/lib/include/vfio_util.h | 11 ++++ tools/testing/selftests/vfio/lib/vfio_pci_device.c | 74 ++++++++++++++++++= +++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/t= esting/selftests/vfio/lib/include/vfio_util.h index ef8f06ef0c13..69ec0c856481 100644 --- a/tools/testing/selftests/vfio/lib/include/vfio_util.h +++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h @@ -188,6 +188,13 @@ struct vfio_pci_device { struct vfio_pci_driver driver; }; =20 +struct iova_allocator { + struct iommu_iova_range *ranges; + u32 nranges; + u32 range_idx; + u64 range_offset; +}; + /* * Return the BDF string of the device that the test should use. * @@ -212,6 +219,10 @@ void vfio_pci_device_reset(struct vfio_pci_device *dev= ice); struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pci_device *devi= ce, u32 *nranges); =20 +struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device); +void iova_allocator_cleanup(struct iova_allocator *allocator); +iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size); + int __vfio_pci_dma_map(struct vfio_pci_device *device, struct vfio_dma_region *region); int __vfio_pci_dma_unmap(struct vfio_pci_device *device, diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 7a523e3f2dce..ff1edb11747a 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -12,11 +12,12 @@ #include =20 #include +#include #include #include +#include #include #include -#include =20 #include "../../../kselftest.h" #include @@ -196,6 +197,77 @@ struct iommu_iova_range *vfio_pci_iova_ranges(struct v= fio_pci_device *device, return ranges; } =20 +struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device) +{ + struct iova_allocator *allocator; + struct iommu_iova_range *ranges; + u32 nranges; + + ranges =3D vfio_pci_iova_ranges(device, &nranges); + VFIO_ASSERT_NOT_NULL(ranges); + + allocator =3D malloc(sizeof(*allocator)); + VFIO_ASSERT_NOT_NULL(allocator); + + *allocator =3D (struct iova_allocator){ + .ranges =3D ranges, + .nranges =3D nranges, + .range_idx =3D 0, + .range_offset =3D 0, + }; + + return allocator; +} + +void iova_allocator_cleanup(struct iova_allocator *allocator) +{ + free(allocator->ranges); + free(allocator); +} + +iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size) +{ + VFIO_ASSERT_GT(size, 0, "Invalid size arg, zero\n"); + VFIO_ASSERT_EQ(size & (size - 1), 0, "Invalid size arg, non-power-of-2\n"= ); + + for (;;) { + struct iommu_iova_range *range; + iova_t iova, last; + + VFIO_ASSERT_LT(allocator->range_idx, allocator->nranges, + "IOVA allocator out of space\n"); + + range =3D &allocator->ranges[allocator->range_idx]; + iova =3D range->start + allocator->range_offset; + + /* Check for sufficient space at the current offset */ + if (check_add_overflow(iova, size - 1, &last) || + last > range->last) + goto next_range; + + /* Align iova to size */ + iova =3D last & ~(size - 1); + + /* Check for sufficient space at the aligned iova */ + if (check_add_overflow(iova, size - 1, &last) || + last > range->last) + goto next_range; + + if (last =3D=3D range->last) { + allocator->range_idx++; + allocator->range_offset =3D 0; + } else { + allocator->range_offset =3D last - range->start + 1; + } + + return iova; + +next_range: + allocator->range_idx++; + allocator->range_offset =3D 0; + } +} + iova_t __to_iova(struct vfio_pci_device *device, void *vaddr) { struct vfio_dma_region *region; --=20 2.47.3 From nobody Sun Feb 8 02:26:33 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 63B4A3054D7; Tue, 11 Nov 2025 14:52:38 +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=1762872760; cv=none; b=ld1tu+O64KUuqKsgSv9vek9GwzdbjNcs0UwTZcMcipWWkCORi2cw792fIO4io2gRILE/sVYeh812sjMOUSKyscUiJiwaAH7ai0bNEk0f+4etZE2mj5mzlq8ENQT4SdGLbedoFZcUyvgppeMXGkjUdgYqvU5WGLNtEZXctG6oJhg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762872760; c=relaxed/simple; bh=IgaI2VllyJmFtyZAxQUSXs0BSulng0Rn09jr4Zjxryo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=DDErdvxz0Vukqg4BVSP+wrVr8rFheJuYT/xQAso6F9MfDr54qkuAbOJR2unCXGXKOg2W0DrKXe1sU90rrDFw1cxTJpVxlAR8wuSxvBMD+hGIOKcKpzTvo5JY+3UsFRH1WZf+wB2a0plaaI8Pu2FWO4SzXwxqJtni19ruT1xjOP4= 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=iJtLqPsv; 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="iJtLqPsv" Received: from pps.filterd (m0148461.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 5AB7SXNK1282474; Tue, 11 Nov 2025 06:52:31 -0800 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=n/YAbbC01iMLaoR4MVMLequGIlh29d4NC8TUXJnvea8=; b=iJtLqPsvM7we hCmm9C8NC7asUW22Wm3LP6ZTwXV49aBCBF6tFy+3nLTo9uWgBlXNgp5lBR3gddeQ SDubhk0vmh8TDMkrDc73OSouYCy6MoQoo2n6Mkowaximu4xNV6Z2P2NPcWs+jqla No2t9ZqN7b1oyf19rSkYCWH16xHKQaaqgQOEtImB7FxdBUeh60FHi9zH8K5yJD6q z0JnXosDmLG6zlPRzRG35+HqoibHk9DjHHNTR4zlE1rf/jTiF5to2vl5tunSbgox u+nSOEujKxCAxkr5v2vn0ZexghgZN3GUBlYTEKCBR6DJVxx+MeBvDvCAIzQ6JUCc GkcF7SM+1Q== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4ac0shadt9-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Tue, 11 Nov 2025 06:52:31 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:108::150d) by mail.thefacebook.com (2620:10d:c08b:78::2ac9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.20; Tue, 11 Nov 2025 14:52:30 +0000 From: Alex Mastro Date: Tue, 11 Nov 2025 06:52:05 -0800 Subject: [PATCH v2 4/4] vfio: selftests: replace iova=vaddr with allocated iovas 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: <20251111-iova-ranges-v2-4-0fa267ff9b78@fb.com> References: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> In-Reply-To: <20251111-iova-ranges-v2-0-0fa267ff9b78@fb.com> To: Alex Williamson , David Matlack , Shuah Khan , Jason Gunthorpe CC: , , , Alex Mastro X-Mailer: b4 0.13.0 X-Authority-Analysis: v=2.4 cv=Pf/yRyhd c=1 sm=1 tr=0 ts=69134daf cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=6UeiqGixMTsA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=JmSn-CL03WZ9larzvTIA:9 a=QEXdDO2ut3YA:10 a=cPQSjfK2_nFv0Q5t_7PE:22 X-Proofpoint-GUID: 36w6tK_ieGWNvpPkS5b1RVrFa4IObO8E X-Proofpoint-Spam-Details-Enc: AW1haW4tMjUxMTExMDExOSBTYWx0ZWRfXwi2Ly+j3ekBJ GEmBFHkoZUHfqtiXLqPqJ+GeReZWXkizy331Mevu57bIcuqMhWYSjYB8CWpk3PMeN5CMOiahOQR m61tbWIFqE3N9BsRZeGp4xzcF4w2LC804vpkXet/GMTAj2z86MtYcQcGbja1uy2LyFQvV+Ia/xS SQ8jagBAOk4CNFXIgNREbt9yNdEP8DsAokqha9Wa25aUfCex+rLJLLfSj+/ae1pKwdKx6b5d33R VBe0YJ1BRPswQqhSa32G8CGSRYoq8MVgzbh0VFKEfzRqNyEk6hYKsJNigCsjk7CDiKZANQTvm3A Uc/nDJMGaJrPCdaiEFCJ1HL5pF3hcvMN7CHx/EXw7pWieaNJ/vT4AGtBKyNsuX3wDRA+AI7583z 126l8miA9Kn+CISSD0M8ETM8wMwkOQ== X-Proofpoint-ORIG-GUID: 36w6tK_ieGWNvpPkS5b1RVrFa4IObO8E X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.9,FMLib:17.12.100.49 definitions=2025-11-11_02,2025-11-11_02,2025-10-01_01 vfio_dma_mapping_test and vfio_pci_driver_test currently use iova=3Dvaddr as part of DMA mapping operations. The assumption that these IOVAs are legal has held up on all the hardware we've tested so far, but is not guaranteed. Make the tests more robust by using iova_allocator to vend IOVAs, which queries legally accessible IOVAs from the underlying IOMMUFD or VFIO container. Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 5 ++++- tools/testing/selftests/vfio/vfio_pci_driver_test.c | 12 ++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index e1374aab96bd..102603d4407d 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -95,6 +95,7 @@ static int iommu_mapping_get(const char *bdf, u64 iova, =20 FIXTURE(vfio_dma_mapping_test) { struct vfio_pci_device *device; + struct iova_allocator *iova_allocator; }; =20 FIXTURE_VARIANT(vfio_dma_mapping_test) { @@ -119,10 +120,12 @@ FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb= _1gb, SZ_1G, MAP_HUGETLB | FIXTURE_SETUP(vfio_dma_mapping_test) { self->device =3D vfio_pci_device_init(device_bdf, variant->iommu_mode); + self->iova_allocator =3D iova_allocator_init(self->device); } =20 FIXTURE_TEARDOWN(vfio_dma_mapping_test) { + iova_allocator_cleanup(self->iova_allocator); vfio_pci_device_cleanup(self->device); } =20 @@ -144,7 +147,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) else ASSERT_NE(region.vaddr, MAP_FAILED); =20 - region.iova =3D (u64)region.vaddr; + region.iova =3D iova_allocator_alloc(self->iova_allocator, size); region.size =3D size; =20 vfio_pci_dma_map(self->device, ®ion); diff --git a/tools/testing/selftests/vfio/vfio_pci_driver_test.c b/tools/te= sting/selftests/vfio/vfio_pci_driver_test.c index 2dbd70b7db62..f69eec8b928d 100644 --- a/tools/testing/selftests/vfio/vfio_pci_driver_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_driver_test.c @@ -19,6 +19,7 @@ static const char *device_bdf; } while (0) =20 static void region_setup(struct vfio_pci_device *device, + struct iova_allocator *iova_allocator, struct vfio_dma_region *region, u64 size) { const int flags =3D MAP_SHARED | MAP_ANONYMOUS; @@ -29,7 +30,7 @@ static void region_setup(struct vfio_pci_device *device, VFIO_ASSERT_NE(vaddr, MAP_FAILED); =20 region->vaddr =3D vaddr; - region->iova =3D (u64)vaddr; + region->iova =3D iova_allocator_alloc(iova_allocator, size); region->size =3D size; =20 vfio_pci_dma_map(device, region); @@ -44,6 +45,7 @@ static void region_teardown(struct vfio_pci_device *devic= e, =20 FIXTURE(vfio_pci_driver_test) { struct vfio_pci_device *device; + struct iova_allocator *iova_allocator; struct vfio_dma_region memcpy_region; void *vaddr; int msi_fd; @@ -72,14 +74,15 @@ FIXTURE_SETUP(vfio_pci_driver_test) struct vfio_pci_driver *driver; =20 self->device =3D vfio_pci_device_init(device_bdf, variant->iommu_mode); + self->iova_allocator =3D iova_allocator_init(self->device); =20 driver =3D &self->device->driver; =20 - region_setup(self->device, &self->memcpy_region, SZ_1G); - region_setup(self->device, &driver->region, SZ_2M); + region_setup(self->device, self->iova_allocator, &self->memcpy_region, SZ= _1G); + region_setup(self->device, self->iova_allocator, &driver->region, SZ_2M); =20 /* Any IOVA that doesn't overlap memcpy_region and driver->region. */ - self->unmapped_iova =3D 8UL * SZ_1G; + self->unmapped_iova =3D iova_allocator_alloc(self->iova_allocator, SZ_1G); =20 vfio_pci_driver_init(self->device); self->msi_fd =3D self->device->msi_eventfds[driver->msi]; @@ -108,6 +111,7 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test) region_teardown(self->device, &self->memcpy_region); region_teardown(self->device, &driver->region); =20 + iova_allocator_cleanup(self->iova_allocator); vfio_pci_device_cleanup(self->device); } =20 --=20 2.47.3