From nobody Mon Dec 1 22:34:42 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 BBF8D34887C for ; Wed, 26 Nov 2025 23:18:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764199084; cv=none; b=jQe7EGwJfG2l02SdWtxN/wgMFiCgXD8tomieoivgdfuMTe4MBoMBTfxs3/lCvbt0nPN8h/PMtzJfx8nP6I/kJhlz1E6rS6HWgUI7+s3ZCWidtonolOR4og7v9qbbk22fte0Vc2FwlCD9mVZGrYhbQkuFQD/zJ/1BUdm78f1f+ek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764199084; c=relaxed/simple; bh=kLSpk5VFHeMFBJ+GwTtilOXQsKSv5ho2z8c5GeFW23k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RGrsOJdRfLDMwWdgr4WPkn7m62mhcaGRGkAF/fE9PUFmmXcW++v74e7kMlIoJLWma7nDplx+a6Yn5joM+bttTHm0iNl3C6+y0GrD8xz5XDSs7qEpTzjx6AaPfgJtq9m4Yd/V/nfzHzLgn5DxfN8XKyOk+0bKyLMb0dP7S4AUnlg= 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=qGETBYWL; arc=none smtp.client-ip=209.85.210.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="qGETBYWL" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7be3d08f863so340600b3a.2 for ; Wed, 26 Nov 2025 15:18:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764199082; x=1764803882; 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=x8JEo4qwFAgC4mqRGiFhhIpCUvh9YEL7505XJDWuM1w=; b=qGETBYWLggrmbA5mvGvCwXCLOS1Utiiuteuynz99e9WBwQhiLT8LFRQqQWohKURhSg mYl3b55xSuxt/JSKnbrX3jClX8hiqj2vpD+YQ1zt66ksXU0RRGwQeup0ztGDsz5ZoeA6 it4v6lg4y7FaiZ2CmAEumZzbb+Idmf3jRMdQJG/keShEWcAWesuuFsdcE50ayd7uixSS 9oWEsq2IgHAu2ILfO5Hn4o84dX74Sw16oY2vN9bWCNwpWvwMQ9Y+rhY+ZJp4Y9brjfRT 7JO6QlD/2yYHQbCsLjEK9RrcD3rMMUocNjd6fe83LpW+ygCx4czuUWDc4me9PQaeafSU 61DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764199082; x=1764803882; 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=x8JEo4qwFAgC4mqRGiFhhIpCUvh9YEL7505XJDWuM1w=; b=J+T7wPdbV7l6jUhhb6ZPpWbaIRB0IwL8EfdroG7UPFJEejK4m/xsRJKi4amWVUYXnJ KZYI7NSOlVHbb9ch7FNTLZI/20atB9xCEmaYmnHmYgWU4M+5nahrd7I1+D4xW6F46CJI sKCIvpzvvlzVtKSiX4xaVuAm/357OuZTdOQTZHiXYtaGrdeEUj4ANkdGYLWAXRcZaKXV rQdvr7SglmDk67NUu+T4p/HExvqAR/goy9HiOU8Zzfj9OkdRpK/pb1e20apPn+vYPQfZ 6DSye7mJ6pe+ytmX8Dpif2VLEubv4UV8ylanY0SZ6jAfWMroDvBvg7KNq7nSyooquf9b zANw== X-Forwarded-Encrypted: i=1; AJvYcCVyRTqZG40bBJ8SM8fDNrCdUmAQg7TsBF+ixVpS0VahIpXWx4GA114IPMYspdwRpxsKBJ56JL/w1yB9zxc=@vger.kernel.org X-Gm-Message-State: AOJu0YzJ82K8b1yDiH77E2Vr1FBh4PQ071+1deyVnDHwwHT/KsCDYx+8 1b5N6fF9QcqIyyOmpKLUxf3lZoKUAvchj8qN2j9cKbKUZqTJbACSluMcb3EBxUD7gY4pO10GVQk shaxD0xzjvhHwOQ== X-Google-Smtp-Source: AGHT+IFl22NbQBZgbgr/TKTkVvLlEFJJlyBFNSyJf/VAq6Sml92UZTLp/bHAj/FesXj2d1quZCUi9YxULJg6Aw== X-Received: from pfht15.prod.google.com ([2002:a62:ea0f:0:b0:77f:68a4:6770]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2789:b0:7bf:1a4b:1675 with SMTP id d2e1a72fcca58-7c58e700286mr23608102b3a.28.1764199082281; Wed, 26 Nov 2025 15:18:02 -0800 (PST) Date: Wed, 26 Nov 2025 23:17:28 +0000 In-Reply-To: <20251126231733.3302983-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: <20251126231733.3302983-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.487.g5c8c507ade-goog Message-ID: <20251126231733.3302983-14-dmatlack@google.com> Subject: [PATCH v4 13/18] vfio: selftests: Stop passing device for IOMMU operations 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" Drop the struct vfio_pci_device wrappers for IOMMU map/unmap functions and require tests to directly call iommu_map(), iommu_unmap(), etc. This results in more concise code, and also makes it clear the map operations are happening on a struct iommu, not necessarily on a specific device, especially when multi-device tests are introduced. Do the same for iova_allocator_init() as that function only needs the struct iommu, not struct vfio_pci_device. Reviewed-by: Alex Mastro Tested-by: Alex Mastro Reviewed-by: Raghavendra Rao Ananta Signed-off-by: David Matlack --- .../selftests/vfio/lib/include/vfio_util.h | 44 +------------------ .../selftests/vfio/lib/iova_allocator.c | 4 +- .../selftests/vfio/vfio_dma_mapping_test.c | 20 ++++----- .../selftests/vfio/vfio_pci_driver_test.c | 19 ++++---- 4 files changed, 22 insertions(+), 65 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 f67915d9443e..5224808201fe 100644 --- a/tools/testing/selftests/vfio/lib/include/vfio_util.h +++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h @@ -260,52 +260,10 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *= device); =20 void vfio_pci_device_reset(struct vfio_pci_device *device); =20 -static inline struct iommu_iova_range *vfio_pci_iova_ranges(struct vfio_pc= i_device *device, - u32 *nranges) -{ - return iommu_iova_ranges(device->iommu, nranges); -} - -struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device); +struct iova_allocator *iova_allocator_init(struct iommu *iommu); void iova_allocator_cleanup(struct iova_allocator *allocator); iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size); =20 -static inline int __vfio_pci_dma_map(struct vfio_pci_device *device, - struct dma_region *region) -{ - return __iommu_map(device->iommu, region); -} - -static inline void vfio_pci_dma_map(struct vfio_pci_device *device, - struct dma_region *region) -{ - VFIO_ASSERT_EQ(__vfio_pci_dma_map(device, region), 0); -} - -static inline int __vfio_pci_dma_unmap(struct vfio_pci_device *device, - struct dma_region *region, - u64 *unmapped) -{ - return __iommu_unmap(device->iommu, region, unmapped); -} - -static inline void vfio_pci_dma_unmap(struct vfio_pci_device *device, - struct dma_region *region) -{ - VFIO_ASSERT_EQ(__vfio_pci_dma_unmap(device, region, NULL), 0); -} - -static inline int __vfio_pci_dma_unmap_all(struct vfio_pci_device *device, - u64 *unmapped) -{ - return __iommu_unmap_all(device->iommu, unmapped); -} - -static inline void vfio_pci_dma_unmap_all(struct vfio_pci_device *device) -{ - VFIO_ASSERT_EQ(__vfio_pci_dma_unmap_all(device, NULL), 0); -} - void vfio_pci_config_access(struct vfio_pci_device *device, bool write, size_t config, size_t size, void *data); =20 diff --git a/tools/testing/selftests/vfio/lib/iova_allocator.c b/tools/test= ing/selftests/vfio/lib/iova_allocator.c index f03648361ba2..b3b6b27f5d1e 100644 --- a/tools/testing/selftests/vfio/lib/iova_allocator.c +++ b/tools/testing/selftests/vfio/lib/iova_allocator.c @@ -21,13 +21,13 @@ =20 #include =20 -struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device) +struct iova_allocator *iova_allocator_init(struct iommu *iommu) { struct iova_allocator *allocator; struct iommu_iova_range *ranges; u32 nranges; =20 - ranges =3D vfio_pci_iova_ranges(device, &nranges); + ranges =3D iommu_iova_ranges(iommu, &nranges); VFIO_ASSERT_NOT_NULL(ranges); =20 allocator =3D malloc(sizeof(*allocator)); diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index 289af4665803..c4c2fc36c7b3 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -122,7 +122,7 @@ FIXTURE_SETUP(vfio_dma_mapping_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->device); + self->iova_allocator =3D iova_allocator_init(self->iommu); } =20 FIXTURE_TEARDOWN(vfio_dma_mapping_test) @@ -153,7 +153,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) region.iova =3D iova_allocator_alloc(self->iova_allocator, size); region.size =3D size; =20 - vfio_pci_dma_map(self->device, ®ion); + iommu_map(self->iommu, ®ion); printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", region.vaddr, size, = region.iova); =20 ASSERT_EQ(region.iova, to_iova(self->device, region.vaddr)); @@ -195,7 +195,7 @@ TEST_F(vfio_dma_mapping_test, dma_map_unmap) } =20 unmap: - rc =3D __vfio_pci_dma_unmap(self->device, ®ion, &unmapped); + rc =3D __iommu_unmap(self->iommu, ®ion, &unmapped); ASSERT_EQ(rc, 0); ASSERT_EQ(unmapped, region.size); printf("Unmapped IOVA 0x%lx\n", region.iova); @@ -245,7 +245,7 @@ FIXTURE_SETUP(vfio_dma_map_limit_test) MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); ASSERT_NE(region->vaddr, MAP_FAILED); =20 - ranges =3D vfio_pci_iova_ranges(self->device, &nranges); + ranges =3D iommu_iova_ranges(self->iommu, &nranges); VFIO_ASSERT_NOT_NULL(ranges); last_iova =3D ranges[nranges - 1].last; free(ranges); @@ -268,10 +268,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_range) u64 unmapped; int rc; =20 - vfio_pci_dma_map(self->device, region); + iommu_map(self->iommu, region); ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr)); =20 - rc =3D __vfio_pci_dma_unmap(self->device, region, &unmapped); + rc =3D __iommu_unmap(self->iommu, region, &unmapped); ASSERT_EQ(rc, 0); ASSERT_EQ(unmapped, region->size); } @@ -282,10 +282,10 @@ TEST_F(vfio_dma_map_limit_test, unmap_all) u64 unmapped; int rc; =20 - vfio_pci_dma_map(self->device, region); + iommu_map(self->iommu, region); ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr)); =20 - rc =3D __vfio_pci_dma_unmap_all(self->device, &unmapped); + rc =3D __iommu_unmap_all(self->iommu, &unmapped); ASSERT_EQ(rc, 0); ASSERT_EQ(unmapped, region->size); } @@ -298,10 +298,10 @@ TEST_F(vfio_dma_map_limit_test, overflow) 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); + rc =3D __iommu_map(self->iommu, region); ASSERT_EQ(rc, -EOVERFLOW); =20 - rc =3D __vfio_pci_dma_unmap(self->device, region, NULL); + rc =3D __iommu_unmap(self->iommu, region, NULL); ASSERT_EQ(rc, -EOVERFLOW); } =20 diff --git a/tools/testing/selftests/vfio/vfio_pci_driver_test.c b/tools/te= sting/selftests/vfio/vfio_pci_driver_test.c index 057aa9bbe13e..229e932a06f8 100644 --- a/tools/testing/selftests/vfio/vfio_pci_driver_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_driver_test.c @@ -18,7 +18,7 @@ static const char *device_bdf; ASSERT_EQ(EAGAIN, errno); \ } while (0) =20 -static void region_setup(struct vfio_pci_device *device, +static void region_setup(struct iommu *iommu, struct iova_allocator *iova_allocator, struct dma_region *region, u64 size) { @@ -33,13 +33,12 @@ static void region_setup(struct vfio_pci_device *device, region->iova =3D iova_allocator_alloc(iova_allocator, size); region->size =3D size; =20 - vfio_pci_dma_map(device, region); + iommu_map(iommu, region); } =20 -static void region_teardown(struct vfio_pci_device *device, - struct dma_region *region) +static void region_teardown(struct iommu *iommu, struct dma_region *region) { - vfio_pci_dma_unmap(device, region); + iommu_unmap(iommu, region); VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0); } =20 @@ -76,12 +75,12 @@ FIXTURE_SETUP(vfio_pci_driver_test) =20 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->device); + self->iova_allocator =3D iova_allocator_init(self->iommu); =20 driver =3D &self->device->driver; =20 - region_setup(self->device, self->iova_allocator, &self->memcpy_region, SZ= _1G); - region_setup(self->device, self->iova_allocator, &driver->region, SZ_2M); + region_setup(self->iommu, self->iova_allocator, &self->memcpy_region, SZ_= 1G); + region_setup(self->iommu, self->iova_allocator, &driver->region, SZ_2M); =20 /* Any IOVA that doesn't overlap memcpy_region and driver->region. */ self->unmapped_iova =3D iova_allocator_alloc(self->iova_allocator, SZ_1G); @@ -110,8 +109,8 @@ FIXTURE_TEARDOWN(vfio_pci_driver_test) =20 vfio_pci_driver_remove(self->device); =20 - region_teardown(self->device, &self->memcpy_region); - region_teardown(self->device, &driver->region); + region_teardown(self->iommu, &self->memcpy_region); + region_teardown(self->iommu, &driver->region); =20 iova_allocator_cleanup(self->iova_allocator); vfio_pci_device_cleanup(self->device); --=20 2.52.0.487.g5c8c507ade-goog