From nobody Tue Dec 2 01:27:30 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 261ED352934 for ; Fri, 21 Nov 2025 18:14:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763748889; cv=none; b=mV6HXwWI3JEOqixtJKL2QJsYJh/SmKA5McQ/GaPIYMaK60/SEMfom0tFH6+eQ8qCF4SN8774tyZrXHvIjZxlpOJk6c4JtvvgE6EosurQa4kaEEFCMIijcS0hsdmQdCrM/XaVD8hbt6MFABHHz245Xbwo+K4s8AtyOqgjZJvkrX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763748889; c=relaxed/simple; bh=JrMXo1bbH2zD+C9IAEk7RcmFaSXGprQ1nQVRbpRkObI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CiiBPeoEB+m/E/Akdo1/yV1ntmF98xuc+a9i/VgTwIf9fqqZl2IrpZRaa1/N8H8dBpfEAeKmngh80LyWGM9Dj7hAgTCefF+xqaBph1hvVvBDBz6RqcbAF1t+auPiilekxom32L8+sCxtfcrA7n3tXWXrW5KqXY180Hby3VTzKu0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ydEzlBtR; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ydEzlBtR" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-29809acd049so47184565ad.3 for ; Fri, 21 Nov 2025 10:14:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763748886; x=1764353686; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=blhLd4rrTqtEcsOjOcmqpT/d1FX49t9c289UH0z+u3s=; b=ydEzlBtRIx9xXwM+fagmXbSHYDoWqGT9apATHqsuNxl5SDb74bucN3CAfsMh1SVgUf thI8qq9kS2iGAgcrFUGCDCdihXBAeboyJGavtcH3EaFNPVclFYIxWcTQfDJSZCHQ4TkB mgme/8vfLoktNWnuDMWh5tjxLAai2NVz+2pp7Itb2PY9TV6DIIMbl6qHysJZmRIyRnUT qXU2x5hjdCdl6RraHh6VC0lA8JO05ofKpaLKRcxFSRQZ7fmcqAMxUtIfa7fsM6D6vrjc kjmUi74Y3eaF2XzOKmifbZJphZUmTHyAYBYHCkSw7moqAt2zk/ZkzmyZ2myp/goRSCzf QnHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763748886; x=1764353686; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=blhLd4rrTqtEcsOjOcmqpT/d1FX49t9c289UH0z+u3s=; b=EkcpAF6pVXlDFLGCFWEuaSLbTUNsDv3P//czmuNeTUGTXoH8z03EUkX6ojq8hdL3P8 +KzPIzxrAZlor6GUtNnB3t6X7lON9vleB+TBjI9AposqUaDQ/quzDXlMmE75hETUy8mL ii4cH2wn9ewbSFl0S9hywp6BC8WB6uMPfF4b3TZARt0MkJFqtcI9v1GHSdjy7rIBCPtm DrcI1hPaMBZ2334bV/YDtOkbwx4I2I43rhiV4AL+1Ay6AEVm/AUUnIHaVxMGufWbeCVA CDYnBnM+FbnqYVWvukivhgvLNCQFd2zMLrvZVjhmG3gxA7GvYBzPKDEkvm64Uq3Eyojn bKSQ== X-Forwarded-Encrypted: i=1; AJvYcCXVrJnMqLmHsWhj7d4bIJdLr/avf/XK7VboZQ2+ac4msPwj+boASSGe38m17zdXiStqKhZzE2pJRlWlwvE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxb9jFEvVMSGAKEJXRkqqvRfVb/wb8RqRIaZnPqYCCc312t4tBv yRuAnpEc793ys/Oy12Pqmnp0JAX6ofvgMc/c264riZ7B8DU9qjK+7r/OOtS+zhyVbhHw/WPgHXd KSPAKsejSnt4C/A== X-Google-Smtp-Source: AGHT+IFDh1Mq0Cq+V8AWErkW1uaQlm6+HEp9I9J8cTlqWejTFalLZIGvnegC1ArG8b6icswYkKT9fTxcj0NSWg== X-Received: from plmk8.prod.google.com ([2002:a17:903:1808:b0:274:6ac2:5f9f]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f609:b0:274:5030:2906 with SMTP id d9443c01a7336-29b6bf60de3mr47982835ad.46.1763748886454; Fri, 21 Nov 2025 10:14:46 -0800 (PST) Date: Fri, 21 Nov 2025 18:14:16 +0000 In-Reply-To: <20251121181429.1421717-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251121181429.1421717-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog Message-ID: <20251121181429.1421717-6-dmatlack@google.com> Subject: [PATCH v3 05/18] vfio: selftests: Introduce struct iommu From: David Matlack To: Alex Williamson Cc: Alex Mastro , David Matlack , Jason Gunthorpe , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Raghavendra Rao Ananta , Vipin Sharma Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce struct iommu, which logically represents either a VFIO container or an iommufd IOAS, depending on which IOMMU mode is used by the test. This will be used in a subsequent commit to allow devices to be added to the same container/iommufd. Reviewed-by: Alex Mastro Tested-by: Alex Mastro Signed-off-by: David Matlack --- .../selftests/vfio/lib/include/vfio_util.h | 16 ++-- .../selftests/vfio/lib/vfio_pci_device.c | 75 ++++++++++--------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/t= esting/selftests/vfio/lib/include/vfio_util.h index 2f5555138d7f..3160f2d1ea6d 100644 --- a/tools/testing/selftests/vfio/lib/include/vfio_util.h +++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h @@ -163,15 +163,19 @@ struct vfio_pci_driver { int msi; }; =20 +struct iommu { + const struct iommu_mode *mode; + int container_fd; + int iommufd; + u32 ioas_id; + struct list_head dma_regions; +}; + struct vfio_pci_device { int fd; - - const struct iommu_mode *iommu_mode; int group_fd; - int container_fd; =20 - int iommufd; - u32 ioas_id; + struct iommu *iommu; =20 struct vfio_device_info info; struct vfio_region_info config_space; @@ -180,8 +184,6 @@ struct vfio_pci_device { struct vfio_irq_info msi_info; struct vfio_irq_info msix_info; =20 - struct list_head dma_regions; - /* eventfds for MSI and MSI-x interrupts */ int msi_eventfds[PCI_MSIX_FLAGS_QSIZE + 1]; =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4a021ff4fc40..e47f3ccf6d49 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -277,7 +277,7 @@ iova_t __to_iova(struct vfio_pci_device *device, void *= vaddr) { struct vfio_dma_region *region; =20 - list_for_each_entry(region, &device->dma_regions, link) { + list_for_each_entry(region, &device->iommu->dma_regions, link) { if (vaddr < region->vaddr) continue; =20 @@ -397,7 +397,7 @@ static int vfio_iommu_dma_map(struct vfio_pci_device *d= evice, .size =3D region->size, }; =20 - if (ioctl(device->container_fd, VFIO_IOMMU_MAP_DMA, &args)) + if (ioctl(device->iommu->container_fd, VFIO_IOMMU_MAP_DMA, &args)) return -errno; =20 return 0; @@ -414,10 +414,10 @@ static int iommufd_dma_map(struct vfio_pci_device *de= vice, .user_va =3D (u64)region->vaddr, .iova =3D region->iova, .length =3D region->size, - .ioas_id =3D device->ioas_id, + .ioas_id =3D device->iommu->ioas_id, }; =20 - if (ioctl(device->iommufd, IOMMU_IOAS_MAP, &args)) + if (ioctl(device->iommu->iommufd, IOMMU_IOAS_MAP, &args)) return -errno; =20 return 0; @@ -428,7 +428,7 @@ int __vfio_pci_dma_map(struct vfio_pci_device *device, { int ret; =20 - if (device->iommufd) + if (device->iommu->iommufd) ret =3D iommufd_dma_map(device, region); else ret =3D vfio_iommu_dma_map(device, region); @@ -436,7 +436,7 @@ int __vfio_pci_dma_map(struct vfio_pci_device *device, if (ret) return ret; =20 - list_add(®ion->link, &device->dma_regions); + list_add(®ion->link, &device->iommu->dma_regions); =20 return 0; } @@ -484,13 +484,14 @@ int __vfio_pci_dma_unmap(struct vfio_pci_device *devi= ce, { int ret; =20 - if (device->iommufd) - ret =3D iommufd_dma_unmap(device->iommufd, region->iova, - region->size, device->ioas_id, + if (device->iommu->iommufd) + ret =3D iommufd_dma_unmap(device->iommu->iommufd, region->iova, + region->size, device->iommu->ioas_id, unmapped); else - ret =3D vfio_iommu_dma_unmap(device->container_fd, region->iova, - region->size, 0, unmapped); + ret =3D vfio_iommu_dma_unmap(device->iommu->container_fd, + region->iova, region->size, 0, + unmapped); =20 if (ret) return ret; @@ -505,17 +506,17 @@ int __vfio_pci_dma_unmap_all(struct vfio_pci_device *= device, u64 *unmapped) int ret; struct vfio_dma_region *curr, *next; =20 - if (device->iommufd) - ret =3D iommufd_dma_unmap(device->iommufd, 0, UINT64_MAX, - device->ioas_id, unmapped); + if (device->iommu->iommufd) + ret =3D iommufd_dma_unmap(device->iommu->iommufd, 0, UINT64_MAX, + device->iommu->ioas_id, unmapped); else - ret =3D vfio_iommu_dma_unmap(device->container_fd, 0, 0, + ret =3D vfio_iommu_dma_unmap(device->iommu->container_fd, 0, 0, VFIO_DMA_UNMAP_FLAG_ALL, unmapped); =20 if (ret) return ret; =20 - list_for_each_entry_safe(curr, next, &device->dma_regions, link) + list_for_each_entry_safe(curr, next, &device->iommu->dma_regions, link) list_del_init(&curr->link); =20 return 0; @@ -627,28 +628,28 @@ static void vfio_pci_group_setup(struct vfio_pci_devi= ce *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_GET_STATUS, &group_status); VFIO_ASSERT_TRUE(group_status.flags & VFIO_GROUP_FLAGS_VIABLE); =20 - ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->contain= er_fd); + ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) { - unsigned long iommu_type =3D device->iommu_mode->iommu_type; - const char *path =3D device->iommu_mode->container_path; + unsigned long iommu_type =3D device->iommu->mode->iommu_type; + const char *path =3D device->iommu->mode->container_path; int version; int ret; =20 - device->container_fd =3D open(path, O_RDWR); - VFIO_ASSERT_GE(device->container_fd, 0, "open(%s) failed\n", path); + device->iommu->container_fd =3D open(path, O_RDWR); + VFIO_ASSERT_GE(device->iommu->container_fd, 0, "open(%s) failed\n", path); =20 - version =3D ioctl(device->container_fd, VFIO_GET_API_VERSION); + version =3D ioctl(device->iommu->container_fd, VFIO_GET_API_VERSION); VFIO_ASSERT_EQ(version, VFIO_API_VERSION, "Unsupported version: %d\n", ve= rsion); =20 vfio_pci_group_setup(device, bdf); =20 - ret =3D ioctl(device->container_fd, VFIO_CHECK_EXTENSION, iommu_type); + ret =3D ioctl(device->iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_ty= pe); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 - ioctl_assert(device->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); + ioctl_assert(device->iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_t= ype); =20 device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); VFIO_ASSERT_GE(device->fd, 0); @@ -801,12 +802,12 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_de= vice *device, const char *b * used to check if iommufd is enabled. In practice open() will never * return 0 unless stdin is closed. */ - device->iommufd =3D open("/dev/iommu", O_RDWR); - VFIO_ASSERT_GT(device->iommufd, 0); + device->iommu->iommufd =3D open("/dev/iommu", O_RDWR); + VFIO_ASSERT_GT(device->iommu->iommufd, 0); =20 - vfio_device_bind_iommufd(device->fd, device->iommufd); - device->ioas_id =3D iommufd_ioas_alloc(device->iommufd); - vfio_device_attach_iommufd_pt(device->fd, device->ioas_id); + vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); + device->iommu->ioas_id =3D iommufd_ioas_alloc(device->iommu->iommufd); + vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, const char *= iommu_mode) @@ -816,11 +817,14 @@ struct vfio_pci_device *vfio_pci_device_init(const ch= ar *bdf, const char *iommu_ device =3D calloc(1, sizeof(*device)); VFIO_ASSERT_NOT_NULL(device); =20 - INIT_LIST_HEAD(&device->dma_regions); + device->iommu =3D calloc(1, sizeof(*device->iommu)); + VFIO_ASSERT_NOT_NULL(device->iommu); + + INIT_LIST_HEAD(&device->iommu->dma_regions); =20 - device->iommu_mode =3D lookup_iommu_mode(iommu_mode); + device->iommu->mode =3D lookup_iommu_mode(iommu_mode); =20 - if (device->iommu_mode->container_path) + if (device->iommu->mode->container_path) vfio_pci_container_setup(device, bdf); else vfio_pci_iommufd_setup(device, bdf); @@ -849,13 +853,14 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *= device) VFIO_ASSERT_EQ(close(device->msi_eventfds[i]), 0); } =20 - if (device->iommufd) { - VFIO_ASSERT_EQ(close(device->iommufd), 0); + if (device->iommu->iommufd) { + VFIO_ASSERT_EQ(close(device->iommu->iommufd), 0); } else { VFIO_ASSERT_EQ(close(device->group_fd), 0); - VFIO_ASSERT_EQ(close(device->container_fd), 0); + VFIO_ASSERT_EQ(close(device->iommu->container_fd), 0); } =20 + free(device->iommu); free(device); } =20 --=20 2.52.0.rc2.455.g230fcf2819-goog