From nobody Mon Feb 9 12:23:59 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 0EE713396E6 for ; Wed, 7 Jan 2026 20:18:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767817091; cv=none; b=j0AwFDqEy1UrSlOAwnObaVqRaTv7a5Nj6p3klDScl77NN/QGnQF2XkBMjMoUUJbOx6YRdO96ayrjUvuyRTL9O9S91p03hyZBjVAnIRmYa0DMcqrPYQLvNIhHAzH4B0yOd1/1tsl4ds2yt7o1N0fCW0KIX2GInjU0ngP2w4zq1LU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767817091; c=relaxed/simple; bh=4Pd02XCOO1b0RU5MmE2Uwo/GFJB5CoMMUmyDrSB5Hn0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rCoss+d8Fy5QeaIRKdZltehdR0YNnXTBghO0FEfI8U9yqCCNslNIeYOy7ibHob+MoTwnKQGpVCiXtTFatK0NabRrPXkBpyH14Ys+sTreUu4805hnVqd8nhfVD6lA1m/xVUNAE3ni8C/3bj/dhko3Axy/wEfOt1xKoEejxo90ugM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aIoRH2Wj; arc=none smtp.client-ip=209.85.214.201 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--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aIoRH2Wj" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2a0dabc192eso33841735ad.0 for ; Wed, 07 Jan 2026 12:18:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767817088; x=1768421888; 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=YbIgDevabE4WtwYsX6fHSQEhrlUTqPdnsQVPiMzIU6I=; b=aIoRH2WjHIDGeXHNv5++w9KWHjf3ppnHYlnOJ9kv0WcX2PPirM8tAcwjmoxeNX/X7y NxwSWoM23sNLaisokd/iriHBrGQMxV2QXf6BDsliSafO55MmGoIPIXsrqStwVIUj4QNX lK+mwiYhzyEGKJmT6dANzzc8TT/yrtf0Kegi48hG2OZbLIgmgv2mgcfh9EhZtz4zeXXW LfCmHHAHtGeyhFNewT53TNAlaEozzOSZd78QvaeITS0dgSvQCcGh/R6YIKYmct39pFGO W7SENbrFD9iGt0eI5Q46biADU6vf1sH32LTxBXDuvLHzcuY/fIhcJI19xayR1fahJn1n TKRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767817088; x=1768421888; 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=YbIgDevabE4WtwYsX6fHSQEhrlUTqPdnsQVPiMzIU6I=; b=uBZlZhtgfxvL4xvgt7j6xMDhUr75/cs0G0VNO3fq6m+MM52qj+X/9U4ggt8tfkz8Fj 12FsjBh+ROQN7zuqpiULTdHCovXlzQXaub7h+S79C+LJ1c+HctGA137f0MoZqurs8Q+h GV36qptSYSZyOZ0QABc67ySD7Zv3GFWHyfR3yZHo2AWFuwwhMpMDXHMFk2gjSk5cCeOg 94Jj02UhmP1J2FbW9PUUi94Wb5Lr37xfYFwGV16dT0R6pr83Yww3bvG6RMVTHFWHYue3 KECPxCEDeqwGWNew0LInbRR7nQnaqKjOffwxsyhlGb0JQ9dlCThrcWzWMZqE3PtG2OEU 79Xw== X-Forwarded-Encrypted: i=1; AJvYcCVdBb9QDWTW6W9/CyQMYWxEDqXZdZZw6DFhm3NIhQIpFBYcwv9TBiNan8Dqb0jHPi5ZkH/Ydk/xnWFXCyo=@vger.kernel.org X-Gm-Message-State: AOJu0Yyz1PmHqqz+ZQzZWaYzwkNifs5u/XiR3qhhxrAiFpZa0fW1RWkD nmMjVA5pKiAPeDpdzUZS4h4qhbx9JHgYpZX+jlcKRKut9hwpFhIl5jpQ2TZBrK9VVYEq7q5n0Ye xqQLdvZ61kavbmA== X-Google-Smtp-Source: AGHT+IE1DFCnmny/nHtC+I3vQMTODQGJxSrSd6kNwp+xLBMyWXqHQwoP9SAPWVjXbrVNZ0Jk6FS+jUxa+UC6xw== X-Received: from plyw19.prod.google.com ([2002:a17:902:d713:b0:2a0:f83c:bb4b]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e391:b0:2a0:9d0f:b1ed with SMTP id d9443c01a7336-2a3ee45b3b5mr21123465ad.24.1767817088271; Wed, 07 Jan 2026 12:18:08 -0800 (PST) Date: Wed, 7 Jan 2026 20:18:00 +0000 In-Reply-To: <20260107201800.2486137-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260107201800.2486137-1-skhawaja@google.com> X-Mailer: git-send-email 2.52.0.351.gbe84eed79e-goog Message-ID: <20260107201800.2486137-4-skhawaja@google.com> Subject: [PATCH 3/3] vfio: selftests: Add iommufd hwpt replace test From: Samiullah Khawaja To: David Woodhouse , Lu Baolu , Joerg Roedel , Will Deacon , Pasha Tatashin , Jason Gunthorpe , David Matlack Cc: Samiullah Khawaja , Robin Murphy , Pratyush Yadav , Kevin Tian , Alex Williamson , Shuah Khan , iommu@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Saeed Mahameed , Adithya Jayachandran , Parav Pandit , Leon Romanovsky , William Tu Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test that does iommufd hwpt replace while a DMA is ongoing. This verifies the hitless replace of IOMMU domain without disrupting the DMA. Note that the new domain is attached after mapping the required DMA memory at the same IOVA in the new domain. Signed-off-by: Samiullah Khawaja --- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_hwpt_replace_test.c | 151 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_= test.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 3c796ca99a50..09a1e57cc77d 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_iommufd_hwpt_replace_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_iommufd_hwpt_replace_test.c = b/tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file mode 100644 index 000000000000..efef3233494f --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +#include +#include + +#include + +#include "kselftest_harness.h" + +static const char *device_bdf; + +static void region_setup(struct iommu *iommu, + struct iova_allocator *iova_allocator, + struct dma_region *region, u64 size) +{ + const int flags =3D MAP_SHARED | MAP_ANONYMOUS; + const int prot =3D PROT_READ | PROT_WRITE; + void *vaddr; + + vaddr =3D mmap(NULL, size, prot, flags, -1, 0); + VFIO_ASSERT_NE(vaddr, MAP_FAILED); + + region->vaddr =3D vaddr; + region->iova =3D iova_allocator_alloc(iova_allocator, size); + region->size =3D size; + + iommu_map(iommu, region); +} + +static void region_teardown(struct iommu *iommu, struct dma_region *region) +{ + iommu_unmap(iommu, region); + VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0); +} + +FIXTURE(vfio_iommufd_replace_hwpt_test) { + struct iommu *iommu; + struct vfio_pci_device *device; + struct iova_allocator *iova_allocator; + struct dma_region memcpy_region; + void *vaddr; + + u64 size; + void *src; + void *dst; + iova_t src_iova; + iova_t dst_iova; +}; + +FIXTURE_SETUP(vfio_iommufd_replace_hwpt_test) +{ + struct vfio_pci_driver *driver; + + self->iommu =3D iommu_init("iommufd"); + self->device =3D vfio_pci_device_init(device_bdf, self->iommu); + self->iova_allocator =3D iova_allocator_init(self->iommu); + + driver =3D &self->device->driver; + + region_setup(self->iommu, self->iova_allocator, &self->memcpy_region, SZ_= 1G); + region_setup(self->iommu, self->iova_allocator, &driver->region, SZ_2M); + + if (driver->ops) + vfio_pci_driver_init(self->device); + + self->size =3D self->memcpy_region.size / 2; + self->src =3D self->memcpy_region.vaddr; + self->dst =3D self->src + self->size; + + self->src_iova =3D to_iova(self->device, self->src); + self->dst_iova =3D to_iova(self->device, self->dst); +} + +FIXTURE_TEARDOWN(vfio_iommufd_replace_hwpt_test) +{ + struct vfio_pci_driver *driver =3D &self->device->driver; + + if (driver->ops) + vfio_pci_driver_remove(self->device); + + region_teardown(self->iommu, &self->memcpy_region); + region_teardown(self->iommu, &driver->region); + + iova_allocator_cleanup(self->iova_allocator); + vfio_pci_device_cleanup(self->device); + iommu_cleanup(self->iommu); +} + +FIXTURE_VARIANT(vfio_iommufd_replace_hwpt_test) { + bool replace_hwpt; +}; + +FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, domain_replace) { + .replace_hwpt =3D true, +}; + +FIXTURE_VARIANT_ADD(vfio_iommufd_replace_hwpt_test, noreplace) { + .replace_hwpt =3D false, +}; + +TEST_F(vfio_iommufd_replace_hwpt_test, memcpy) +{ + struct dma_region memcpy_region, driver_region; + struct iommu *iommu2; + + if (self->device->driver.ops) { + memset(self->src, 'x', self->size); + memset(self->dst, 'y', self->size); + + vfio_pci_driver_memcpy_start(self->device, + self->src_iova, + self->dst_iova, + self->size, + 100); + } + + if (variant->replace_hwpt) { + iommu2 =3D iommufd_iommu_init(self->iommu->iommufd, + self->device->dev_id); + + memcpy_region =3D self->memcpy_region; + driver_region =3D self->device->driver.region; + + iommu_map(iommu2, &memcpy_region); + iommu_map(iommu2, &driver_region); + + vfio_pci_device_attach_iommu(self->device, iommu2); + } + + if (self->device->driver.ops) { + ASSERT_EQ(0, vfio_pci_driver_memcpy_wait(self->device)); + ASSERT_EQ(0, memcmp(self->src, self->dst, self->size)); + } + + if (variant->replace_hwpt) { + vfio_pci_device_attach_iommu(self->device, self->iommu); + + iommu_unmap(iommu2, &memcpy_region); + iommu_unmap(iommu2, &driver_region); + iommu_cleanup(iommu2); + } +} + +int main(int argc, char *argv[]) +{ + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + + return test_harness_run(argc, argv); +} --=20 2.52.0.351.gbe84eed79e-goog