From nobody Sun Feb 8 06:55:47 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 CEC23331223; Wed, 14 Jan 2026 18:57:30 +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=1768417052; cv=none; b=TXgDu4QrmQSRPcNxVgQc+F8WnIrgLIWT4/ieBDE0z/B0P217LfIrNKQzZu6RiMADsTpFm4Gai5A9ksbe+2cRM8AfSii54kEOzAEjcdauKBZpXD+Pu+T9HNZyRN+TZoe9wkIlEwui2y/xOGu3ZEvb/8iG3+PWI390+XZ/oc4cUNg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768417052; c=relaxed/simple; bh=fkrdBctk1aST+sSHe3JoGU8KujzMzEzuEICfMKThj7k=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=i8sfw27ZgFZzwMeIlM1QhWOEObOvu2+FnEP/bDKGbWmNCgAECE3yZETt7GFsTwgTy4+DOjEEo9zat4zGCtcLatfX2F2mOFD6TS9mvV/75t6objanhm3+mhJzmfiWWAmEtOeLrVsiBe/FoZBRB6Kc5fI5rXFdQLWCZyzg7bcp8dk= 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=I2YO4yOU; 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="I2YO4yOU" Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 60EHQReH2373504; Wed, 14 Jan 2026 10:57:23 -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=OyC5pNyK4lUwaTgpChdW1KmmFya8dvnjMMlIoImdkRQ=; b=I2YO4yOUfCR8 9Dm6LVot3h3tXIReBUpphdLOtrK1k4RqcXBGJ6U4F0p8WolNfVvzi9O4U0958B+w gCVMCUG/nMhWI7f7KH/ynZVteDm0enwnAl/gD2ihoc/Z1xHvpFP9PjIMIT2zhwQG ZGImeRpOgmsDD9poCXqtm22S87mfwXm3j1ULgYj9sSpgfKpjEDPz1zakMBDovna6 3EGJaUbpihMCWEtSi066tbDPRFnk4DLku6DmaR8wTnbyfQNyyMCak1kEG7R0WyR8 g/4JGSrnr2lYiZaIypezfGfcr6+ffru4vq8ZO+L2B3+BJcTr6ITU+VwFVkL8J5vi KRmVB5RLTw== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4bpactbmre-12 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 14 Jan 2026 10:57:23 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:208::7cb7) by mail.thefacebook.com (2620:10d:c08b:78::c78f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.29; Wed, 14 Jan 2026 18:57:22 +0000 From: Alex Mastro Date: Wed, 14 Jan 2026 10:57:16 -0800 Subject: [PATCH v3 1/3] vfio: selftests: Centralize IOMMU mode name definitions 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: <20260114-map-mmio-test-v3-1-44e036d95e64@fb.com> References: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> In-Reply-To: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> To: Alex Williamson , David Matlack , Shuah Khan CC: Peter Xu , , , , Jason Gunthorpe , Alex Mastro X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: _vmoxHnfhHZHBnnyiYvn8mu-woVfge1C X-Authority-Analysis: v=2.4 cv=d5f4CBjE c=1 sm=1 tr=0 ts=6967e713 cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=ZEimqIeFnaV32_7obd8A:9 a=QEXdDO2ut3YA:10 X-Proofpoint-GUID: _vmoxHnfhHZHBnnyiYvn8mu-woVfge1C X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE0MDE1NiBTYWx0ZWRfX7mDnEzNWGAgR S0W67gfBVbYe+Xhp5wOC2xyuvIPBTVzH1tJklMo6IdJuDMgUhiPk8z7VqYxg07oSHqKwUDBhBhp tQYOpRHY6JIDoNXJbxLPA6kftXpo6QDtuYPjz4MeapEZljZjsLYeW6yvIs0F/Eu8ynvJAud9Ltm VwEwnF3XEI4W2ZZBkzj/0jBVpRxEWf+TsrMnL5iIDmT2+kbn/w8WiRzmWi9qVbv2LhTP6zyFocf 2Lr1wSvxmqs9kigITeoCDr3eqV39q2CsNVrpPTlfKPO1r3Lj+A4V3jzzCpKCXiYK4CZWlsw1bde nTN5v8/q81r35iDKDmUIzOXhrzO6eMojF32jrEyVYChegg8qnSbIvUFXJno8E1oweIrgITZIv3q ot+gN1/orUMV8LvKkGWIDXSPFwq1MLuRfBj5RpcPxZIyPTHnMtgJGb0Tfh6mRBhdGzyVChDSgh/ bxsczuZSJ339sq1KyCQ== 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=2026-01-14_06,2026-01-14_01,2025-10-01_01 Replace scattered string literals with MODE_* macros in iommu.h. This provides a single source of truth for IOMMU mode name strings. Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- tools/testing/selftests/vfio/lib/include/libvfio/iommu.h | 6 ++++++ tools/testing/selftests/vfio/lib/iommu.c | 12 ++++++------ tools/testing/selftests/vfio/vfio_dma_mapping_test.c | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/iommu.h index 5c9b9dc6d993..e9a3386a4719 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h @@ -61,6 +61,12 @@ iova_t iommu_hva2iova(struct iommu *iommu, void *vaddr); =20 struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nrang= es); =20 +#define MODE_VFIO_TYPE1_IOMMU "vfio_type1_iommu" +#define MODE_VFIO_TYPE1V2_IOMMU "vfio_type1v2_iommu" +#define MODE_IOMMUFD_COMPAT_TYPE1 "iommufd_compat_type1" +#define MODE_IOMMUFD_COMPAT_TYPE1V2 "iommufd_compat_type1v2" +#define MODE_IOMMUFD "iommufd" + /* * Generator for VFIO selftests fixture variants that replicate across all * possible IOMMU modes. Tests must define FIXTURE_VARIANT_ADD_IOMMU_MODE() diff --git a/tools/testing/selftests/vfio/lib/iommu.c b/tools/testing/selft= ests/vfio/lib/iommu.c index 8079d43523f3..27d1d13abfeb 100644 --- a/tools/testing/selftests/vfio/lib/iommu.c +++ b/tools/testing/selftests/vfio/lib/iommu.c @@ -21,32 +21,32 @@ #include "../../../kselftest.h" #include =20 -const char *default_iommu_mode =3D "iommufd"; +const char *default_iommu_mode =3D MODE_IOMMUFD; =20 /* Reminder: Keep in sync with FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(). */ static const struct iommu_mode iommu_modes[] =3D { { - .name =3D "vfio_type1_iommu", + .name =3D MODE_VFIO_TYPE1_IOMMU, .container_path =3D "/dev/vfio/vfio", .iommu_type =3D VFIO_TYPE1_IOMMU, }, { - .name =3D "vfio_type1v2_iommu", + .name =3D MODE_VFIO_TYPE1V2_IOMMU, .container_path =3D "/dev/vfio/vfio", .iommu_type =3D VFIO_TYPE1v2_IOMMU, }, { - .name =3D "iommufd_compat_type1", + .name =3D MODE_IOMMUFD_COMPAT_TYPE1, .container_path =3D "/dev/iommu", .iommu_type =3D VFIO_TYPE1_IOMMU, }, { - .name =3D "iommufd_compat_type1v2", + .name =3D MODE_IOMMUFD_COMPAT_TYPE1V2, .container_path =3D "/dev/iommu", .iommu_type =3D VFIO_TYPE1v2_IOMMU, }, { - .name =3D "iommufd", + .name =3D MODE_IOMMUFD, }, }; =20 diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index 5397822c3dd4..7cd396aa205c 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -166,7 +166,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) * IOMMUFD compatibility-mode does not support huge mappings when * using VFIO_TYPE1_IOMMU. */ - if (!strcmp(variant->iommu_mode, "iommufd_compat_type1")) + if (!strcmp(variant->iommu_mode, MODE_IOMMUFD_COMPAT_TYPE1)) mapping_size =3D SZ_4K; =20 ASSERT_EQ(0, rc); --=20 2.47.3 From nobody Sun Feb 8 06:55:47 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 658053358B5; Wed, 14 Jan 2026 18:57:31 +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=1768417052; cv=none; b=NYhpCVtMcZ4jl33teb4lLsZIXMEfPqytDPPJ/1lxL1SUyJl+CiE/5pQfJgCMs6kRHoIPRNKyGFqJiuENpVGIifKoKp8I608BAgyvrOcYmYJniIeQITTK89+8hu6cVYPnNEyJi9XhIPNxA2rQgaeL0teSXNqufTOWTXQoZWcE7U4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768417052; c=relaxed/simple; bh=P+ylyyobdRfpqyKGD4M7CxJZEK93xTbybC+YMGYjj08=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=M6qnHpDDC18tRYl3N6bYs2Ld8vQeIeI+KKAq4qsOtaDe7IZadAPE1S6M6sF86canrLbpFQIf7o/PLou6+aNv/RRPL4lhtZfF0Kism4VlFJy/hgrF+NHSyYX0HI4Kso6G4Fe45XnpljkPbATW2k/OF7bs+krCR7nseTTH/OJsasM= 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=xrXzEiHR; 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="xrXzEiHR" Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 60EHQReI2373504; Wed, 14 Jan 2026 10:57:23 -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=kPgcrMq0L8Gnl5U3jocAvSuj28CbtC+KeunoTaPsmDc=; b=xrXzEiHRZhXk Rla8WQal/Y53GTcUzWOOF4+B1elkz7eVVxw64LnkfUpCbWOsr/+bxbx7P6QHgIH3 rjpngutidc/Np4oqN8o2IwWV54wee+SAOtTw69Kj3CZ08tpi5S4WF57M7Lkm3xBN j5f+4LjwTINaWkFmT8KM9st2q0idVNYerkaKA59AHRYWWD9EPfXSa0EmYt+E+K5p IMi99EX15WN1Aft2S/585XqUUREraF5s0HBcCcQ6r4kfa9dkHfStdYOEVwvxdFc5 /xzH7trk3M23IHNej1UJ3nyAXYj7/C+TjhVtEKeI9ezuJwTtfmC5+Nb3oQ0slAiC JXJ7p8HXww== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4bpactbmre-13 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 14 Jan 2026 10:57:23 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:208::7cb7) by mail.thefacebook.com (2620:10d:c08b:78::c78f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.29; Wed, 14 Jan 2026 18:57:22 +0000 From: Alex Mastro Date: Wed, 14 Jan 2026 10:57:17 -0800 Subject: [PATCH v3 2/3] vfio: selftests: Align BAR mmaps for efficient IOMMU mapping 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: <20260114-map-mmio-test-v3-2-44e036d95e64@fb.com> References: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> In-Reply-To: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> To: Alex Williamson , David Matlack , Shuah Khan CC: Peter Xu , , , , Jason Gunthorpe , Alex Mastro X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: TPP7iQMnzfIvC12AA245AjY7hERbzSFB X-Authority-Analysis: v=2.4 cv=d5f4CBjE c=1 sm=1 tr=0 ts=6967e713 cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=TV4XJVjupk0IgnZMQ3QA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-GUID: TPP7iQMnzfIvC12AA245AjY7hERbzSFB X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE0MDE1NiBTYWx0ZWRfXyA6sn1ky1TnX fXF5tQj642WLXuG7d9OAVubJ3zcqrVXkc1RPC86aaLdvZnfWUphksnWE75xz4Dv0397lNsvalDd cb54vOxWy2LVef1MhgmXdbSjRw8dDm8so9+yIlA60IvDyhv/HyJeiWgT6qmdKc9C5pQl+Ny4Kv6 bFgt2slyONvPORoNQE9H9qlRxbpd5l1jPnQST4+QzkoDG2azML6jsB3LbUp1k1cV6Cypy0VQuBe f6C1QiJWYJG6jQGWSjcX92CdcPDvF/OV7aJ7ouJpjp7OY3i2ae01ApT4NyIjmjm7lYlgHBqLTLq o3Yq/wyF+fjBXQTnfykDm6yBxcHN0strYnNPhIGtGJxXWQfGGKXo5RLTOEsSAOGY6FzrtPP06ZV VvxqLupSfeFXAf1jRfKpINuV+1pQOKQcdjtqboQ3ljZ570tWWt2T4tURznpbRl9KzWCUo9TPjl2 oiJEFj1Ql0623mZb5Lw== 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=2026-01-14_06,2026-01-14_01,2025-10-01_01 Update vfio_pci_bar_map() to align BAR mmaps for efficient huge page mappings. The manual mmap alignment can be removed once mmap(!MAP_FIXED) on vfio device fds improves to automatically return well-aligned addresses. Also add MADV_HUGEPAGE, which encourages the kernel to use huge pages (e.g. when /sys/kernel/mm/transparent_hugepage/enabled is set to "madvise"). Drop MAP_FILE from mmap(). It is an ignored compatibility flag. Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- tools/testing/selftests/vfio/lib/include/libvfio.h | 9 ++++++++ tools/testing/selftests/vfio/lib/libvfio.c | 25 ++++++++++++++++++= ++++ tools/testing/selftests/vfio/lib/vfio_pci_device.c | 24 ++++++++++++++++++= ++- 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/tes= ting/selftests/vfio/lib/include/libvfio.h index 279ddcd70194..1b6da54cc2cb 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio.h @@ -23,4 +23,13 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv[]); char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs); =20 +/* + * Reserve virtual address space of size at an address satisfying + * (vaddr % align) =3D=3D offset. + * + * Returns the reserved vaddr. The caller is responsible for unmapping + * the returned region. + */ +void *mmap_reserve(size_t size, size_t align, size_t offset); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_H */ diff --git a/tools/testing/selftests/vfio/lib/libvfio.c b/tools/testing/sel= ftests/vfio/lib/libvfio.c index a23a3cc5be69..3a3d1ed635c1 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.c +++ b/tools/testing/selftests/vfio/lib/libvfio.c @@ -2,6 +2,9 @@ =20 #include #include +#include + +#include =20 #include "../../../kselftest.h" #include @@ -76,3 +79,25 @@ const char *vfio_selftests_get_bdf(int *argc, char *argv= []) =20 return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0]; } + +void *mmap_reserve(size_t size, size_t align, size_t offset) +{ + void *map_base, *map_align; + size_t delta; + + VFIO_ASSERT_GT(align, offset); + delta =3D align - offset; + + map_base =3D mmap(NULL, size + align, PROT_NONE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + VFIO_ASSERT_NE(map_base, MAP_FAILED); + + map_align =3D (void *)(ALIGN((uintptr_t)map_base + delta, align) - delta); + + if (map_align > map_base) + VFIO_ASSERT_EQ(munmap(map_base, map_align - map_base), 0); + + VFIO_ASSERT_EQ(munmap(map_align + size, map_base + align - map_align), 0); + + return map_align; +} diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 13fdb4b0b10f..2e0721d58078 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -12,10 +12,14 @@ #include =20 #include +#include #include +#include #include +#include #include #include +#include #include #include =20 @@ -124,20 +128,38 @@ static void vfio_pci_region_get(struct vfio_pci_devic= e *device, int index, static void vfio_pci_bar_map(struct vfio_pci_device *device, int index) { struct vfio_pci_bar *bar =3D &device->bars[index]; + size_t align, size; int prot =3D 0; + void *vaddr; =20 VFIO_ASSERT_LT(index, PCI_STD_NUM_BARS); VFIO_ASSERT_NULL(bar->vaddr); VFIO_ASSERT_TRUE(bar->info.flags & VFIO_REGION_INFO_FLAG_MMAP); + VFIO_ASSERT_TRUE(is_power_of_2(bar->info.size)); =20 if (bar->info.flags & VFIO_REGION_INFO_FLAG_READ) prot |=3D PROT_READ; if (bar->info.flags & VFIO_REGION_INFO_FLAG_WRITE) prot |=3D PROT_WRITE; =20 - bar->vaddr =3D mmap(NULL, bar->info.size, prot, MAP_FILE | MAP_SHARED, + size =3D bar->info.size; + + /* + * Align BAR mmaps to improve page fault granularity during potential + * subsequent IOMMU mapping of these BAR vaddr. 1G for x86 is the + * largest hugepage size across any architecture, so no benefit from + * larger alignment. BARs smaller than 1G will be aligned by their + * power-of-two size, guaranteeing sufficient alignment for smaller + * hugepages, if present. + */ + align =3D min_t(size_t, size, SZ_1G); + + vaddr =3D mmap_reserve(size, align, 0); + bar->vaddr =3D mmap(vaddr, size, prot, MAP_SHARED | MAP_FIXED, device->fd, bar->info.offset); VFIO_ASSERT_NE(bar->vaddr, MAP_FAILED); + + madvise(bar->vaddr, size, MADV_HUGEPAGE); } =20 static void vfio_pci_bar_unmap(struct vfio_pci_device *device, int index) --=20 2.47.3 From nobody Sun Feb 8 06:55:47 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 6C20433BBA2; Wed, 14 Jan 2026 18:57:32 +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=1768417053; cv=none; b=aKdb079W+JcICo6wXTT9wnSiNMiZCuumoFZJkEE2BFwDsZbAm2Ptkoa0ArIRPuJG+PHaRCZApkwAIglg3iSvSQrmLtn89lvhegMiShA9YMrqTasaIeL1ob/83oj291bmVltyNlWMZxgtdfb4qEopioQYFt6I4T9Z9PmQH4qw1Og= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768417053; c=relaxed/simple; bh=qq3ABdRRAf3UMfh0KNPmif3pDMNMB+3JTB8aQ1mC3Pw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-ID:References: In-Reply-To:To:CC; b=a0SM0T8PtcOHGmZm83ktoo6AzzqElg2MuolayNwaPWgtpBe1AYElIYnfyvk8sS8OEetNF0alC+ZzxBCSfGWMNbPDPeHryepmZ33CKyJH6S53ixfGbZUTAxrTYgUgbf3PAqJkdHj3jsxUvSlHhLNXO9fNZkFx61cSm+l7T7d3cVg= 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=oE40xB4n; 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="oE40xB4n" Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 60EHQReJ2373504; Wed, 14 Jan 2026 10:57:24 -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=OoGfnppMNxMZ1o1XcxdaXDrCrbXqUXsRS4QpCZFQ0vs=; b=oE40xB4nAyWa 9RRsAAIYTmdi7b1Li6Gw2wmtHP/P/jK0Jf2F1MvN9yeGTcRAscISaHC0Y/iULxqV lFxyCqqob6EWQ/wqVQtYXzzWphbvdxDeiyADK9W+bW7jsQE20367zUHUPME8Zfzv kh6yC9qPuKXbv2ePyv7y2o4l9J1RQqnY9eExvrCNFa3OjD1YLZx8nO1rZoK97E5i OQBBVADpR9daJS+DKoUNALEugbgSmi2QukdEy9vXHiHwjpGG4YE0Yc7SG/zERS80 hV3QSjWrHxpfOjyryw4dJf5LBv9jp9GU0xLdqVD8q9964QTspromQcu++5AoBdIr ZL1rgeJ1uw== Received: from mail.thefacebook.com ([163.114.134.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 4bpactbmre-14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 14 Jan 2026 10:57:24 -0800 (PST) Received: from devgpu015.cco6.facebook.com (2620:10d:c085:208::7cb7) by mail.thefacebook.com (2620:10d:c08b:78::c78f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.2562.29; Wed, 14 Jan 2026 18:57:22 +0000 From: Alex Mastro Date: Wed, 14 Jan 2026 10:57:18 -0800 Subject: [PATCH v3 3/3] vfio: selftests: Add vfio_dma_mapping_mmio_test 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: <20260114-map-mmio-test-v3-3-44e036d95e64@fb.com> References: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> In-Reply-To: <20260114-map-mmio-test-v3-0-44e036d95e64@fb.com> To: Alex Williamson , David Matlack , Shuah Khan CC: Peter Xu , , , , Jason Gunthorpe , Alex Mastro X-Mailer: b4 0.13.0 X-Proofpoint-ORIG-GUID: vvasFR4N2RaAG4aoW_dVVaCbAdy_oc19 X-Authority-Analysis: v=2.4 cv=d5f4CBjE c=1 sm=1 tr=0 ts=6967e714 cx=c_pps a=CB4LiSf2rd0gKozIdrpkBw==:117 a=CB4LiSf2rd0gKozIdrpkBw==:17 a=IkcTkHD0fZMA:10 a=vUbySO9Y5rIA:10 a=VkNPw1HP01LnGYTKEx00:22 a=FOH2dFAWAAAA:8 a=FzPllQ_cktK6OXWCqgAA:9 a=QEXdDO2ut3YA:10 X-Proofpoint-GUID: vvasFR4N2RaAG4aoW_dVVaCbAdy_oc19 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTE0MDE1NiBTYWx0ZWRfX2cETnFHpQd0J ISgfZtmz/DNq43fzY6wa2sIQwhI5WqT8qdys3Kq8vieoQr/w3TtPwZ9UmRzgogCuvCfb9ztu+Qr msLFPdATpK4Ug4pZvZvMcLrPY60eVUC5DdEUHsxuClf8nqirqx8g3+/RFPMheWLSdwUsYxzoX4g pkqMyehaC/uDiGAwZfhRbLOTgt4chXIv9YjlsuiyUK2lw5LrRubE3imptff+X2iJ74+EnYe8RZJ 2gRY06cf1Z3MTM9RH5Z1lsPfOalp34sEDq8GNH07au+trd52O9QRKinSrA2HrESHEfhi8JALv2M 8qmw62XbvU/Cf+RHWpPExr9C5blS1v2ltdZqxUeBg2fBxEFkOhkddoan5oYnG+oog7/9uexsexx 5W/mQpyw4+Skue5FtkXxhEIZAaislfx2xQwxyy1CF9Ysx+/wyHjni53+Rpb+Bk2kbJQ6NSyR26m o7aKgxvxyqIRYfdodsw== 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=2026-01-14_06,2026-01-14_01,2025-10-01_01 Test IOMMU mapping the BAR mmaps created during vfio_pci_device_setup(). All IOMMU modes are tested: vfio_type1 variants are expected to succeed, while non-type1 modes are expected to fail. iommufd compat mode can be updated to expect success once kernel support lands. Native iommufd will not support mapping vaddrs backed by MMIO (it will support dma-buf based MMIO mapping instead). Signed-off-by: Alex Mastro Reviewed-by: David Matlack Tested-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_dma_mapping_mmio_test.c | 143 +++++++++++++++++= ++++ 2 files changed, 144 insertions(+) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 3c796ca99a50..ead27892ab65 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -1,5 +1,6 @@ CFLAGS =3D $(KHDR_INCLUDES) TEST_GEN_PROGS +=3D vfio_dma_mapping_test +TEST_GEN_PROGS +=3D vfio_dma_mapping_mmio_test TEST_GEN_PROGS +=3D vfio_iommufd_setup_test TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c b/to= ols/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c new file mode 100644 index 000000000000..957a89ce7b3a --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_mmio_test.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "../kselftest_harness.h" + +static const char *device_bdf; + +static struct vfio_pci_bar *largest_mapped_bar(struct vfio_pci_device *dev= ice) +{ + u32 flags =3D VFIO_REGION_INFO_FLAG_READ | VFIO_REGION_INFO_FLAG_WRITE; + struct vfio_pci_bar *largest =3D NULL; + u64 bar_size =3D 0; + + for (int i =3D 0; i < PCI_STD_NUM_BARS; i++) { + struct vfio_pci_bar *bar =3D &device->bars[i]; + + if (!bar->vaddr) + continue; + + /* + * iommu_map() maps with READ|WRITE, so require the same + * abilities for the underlying VFIO region. + */ + if ((bar->info.flags & flags) !=3D flags) + continue; + + if (bar->info.size > bar_size) { + bar_size =3D bar->info.size; + largest =3D bar; + } + } + + return largest; +} + +FIXTURE(vfio_dma_mapping_mmio_test) { + struct iommu *iommu; + struct vfio_pci_device *device; + struct iova_allocator *iova_allocator; + struct vfio_pci_bar *bar; +}; + +FIXTURE_VARIANT(vfio_dma_mapping_mmio_test) { + const char *iommu_mode; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode) \ +FIXTURE_VARIANT_ADD(vfio_dma_mapping_mmio_test, _iommu_mode) { \ + .iommu_mode =3D #_iommu_mode, \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(); + +#undef FIXTURE_VARIANT_ADD_IOMMU_MODE + +FIXTURE_SETUP(vfio_dma_mapping_mmio_test) +{ + self->iommu =3D iommu_init(variant->iommu_mode); + self->device =3D vfio_pci_device_init(device_bdf, self->iommu); + self->iova_allocator =3D iova_allocator_init(self->iommu); + self->bar =3D largest_mapped_bar(self->device); + + if (!self->bar) + SKIP(return, "No mappable BAR found on device %s", device_bdf); +} + +FIXTURE_TEARDOWN(vfio_dma_mapping_mmio_test) +{ + iova_allocator_cleanup(self->iova_allocator); + vfio_pci_device_cleanup(self->device); + iommu_cleanup(self->iommu); +} + +static void do_mmio_map_test(struct iommu *iommu, + struct iova_allocator *iova_allocator, + void *vaddr, size_t size) +{ + struct dma_region region =3D { + .vaddr =3D vaddr, + .size =3D size, + .iova =3D iova_allocator_alloc(iova_allocator, size), + }; + + /* + * NOTE: Check for iommufd compat success once it lands. Native iommufd + * will never support this. + */ + if (!strcmp(iommu->mode->name, MODE_VFIO_TYPE1V2_IOMMU) || + !strcmp(iommu->mode->name, MODE_VFIO_TYPE1_IOMMU)) { + iommu_map(iommu, ®ion); + iommu_unmap(iommu, ®ion); + } else { + VFIO_ASSERT_NE(__iommu_map(iommu, ®ion), 0); + VFIO_ASSERT_NE(__iommu_unmap(iommu, ®ion, NULL), 0); + } +} + +TEST_F(vfio_dma_mapping_mmio_test, map_full_bar) +{ + do_mmio_map_test(self->iommu, self->iova_allocator, + self->bar->vaddr, self->bar->info.size); +} + +TEST_F(vfio_dma_mapping_mmio_test, map_partial_bar) +{ + if (self->bar->info.size < 2 * getpagesize()) + SKIP(return, "BAR too small (size=3D0x%llx)", self->bar->info.size); + + do_mmio_map_test(self->iommu, self->iova_allocator, + self->bar->vaddr, getpagesize()); +} + +/* Test IOMMU mapping of BAR mmap with intentionally poor vaddr alignment.= */ +TEST_F(vfio_dma_mapping_mmio_test, map_bar_misaligned) +{ + /* Limit size to bound test time for large BARs */ + size_t size =3D min_t(size_t, self->bar->info.size, SZ_1G); + void *vaddr; + + vaddr =3D mmap_reserve(size, SZ_1G, getpagesize()); + vaddr =3D mmap(vaddr, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXE= D, + self->device->fd, self->bar->info.offset); + VFIO_ASSERT_NE(vaddr, MAP_FAILED); + + do_mmio_map_test(self->iommu, self->iova_allocator, vaddr, size); + + VFIO_ASSERT_EQ(munmap(vaddr, size), 0); +} + +int main(int argc, char *argv[]) +{ + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + return test_harness_run(argc, argv); +} --=20 2.47.3