Skip to contentSkip to conversation

iommu/vt-d: Add support to hitless replace IOMMU domain

PATCH In review Samiullah Khawaja 5 participants 12 messages Jan 7 – Jan 12

Subsystems (6)
KVM Kselftest IOMMU VFIO driver +2 more subsystems
lore
Share & tools
lore
#0 lore raw

cover iommu/vt-d: Add support to hitless replace IOMMU domain

Intel IOMMU Driver already supports replacing IOMMU domain hitlessly in scalable mode. The support is not available in legacy mode and for NO_PASID in scalable mode. This patch series adds the support for legacy mode and NO_PASID scalable mode.

This is needed for the Live update IOMMU persistence to hotswap the iommu domain after liveupdate:
https://lore.kernel.org/all/[email protected]/

The patch adds the support for scalable mode NO_PASID mode by using the existing replace semantics. This works since in scalable mode the context entries are not updated and only the pasid entries are updated.

The patch series also contains a vfio selftests for the iommu domain replace using iommufd hwpt replace functionality.

Tested on a Host with scalable mode and on Qemu with legacy mode:

tools/testing/selftests/vfio/scripts/setup.sh <dsa_device_bdf> tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test <dsa_device_bdf>

TAP version 13
1..2
# Starting 2 tests from 2 test cases.
# RUN vfio_iommufd_replace_hwpt_test.domain_replace.memcpy ...
# OK vfio_iommufd_replace_hwpt_test.domain_replace.memcpy
ok 1 vfio_iommufd_replace_hwpt_test.domain_replace.memcpy
# RUN vfio_iommufd_replace_hwpt_test.noreplace.memcpy ...
# OK vfio_iommufd_replace_hwpt_test.noreplace.memcpy
ok 2 vfio_iommufd_replace_hwpt_test.noreplace.memcpy # PASSED: 2 / 2 tests passed.
# Totals: pass:2 fail:0 xfail:0 xpass:0 skip:0 error:0

Samiullah Khawaja (3):
iommu/vt-d: Allow replacing no_pasid iommu_domain
vfio: selftests: Add support of creating iommus from iommufd
vfio: selftests: Add iommufd hwpt replace test

 drivers/iommu/intel/iommu.c                   | 107 +++++++++----
 tools/testing/selftests/vfio/Makefile         |   1 +
 .../vfio/lib/include/libvfio/iommu.h          |   2 +
 .../lib/include/libvfio/vfio_pci_device.h     |   2 +
 tools/testing/selftests/vfio/lib/iommu.c      |  60 ++++++-
 .../selftests/vfio/lib/vfio_pci_device.c      |  16 +-
 .../vfio/vfio_iommufd_hwpt_replace_test.c     | 151 ++++++++++++++++++
 7 files changed, 303 insertions(+), 36 deletions(-)
 create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c

base-commit: 6cd6c12031130a349a098dbeb19d8c3070d2dfbe

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#1 lore raw

PATCH 1/3 iommu/vt-d: Allow replacing no_pasid iommu_domain

Intel IOMMU driver already supports replacing IOMMU domains attachments with PASIDs. Add support for replacing a domain attached with no_pasid. This includes replacing domains in legacy mode.

---
 drivers/iommu/intel/iommu.c | 107 ++++++++++++++++++++++++++----------
 1 file changed, 77 insertions(+), 30 deletions(-)
drivers/iommu/intel/iommu.c +77 -30
index 134302fbcd92..c0e359fd3ee1 100644
@@ -1140,6 +1140,7 @@ static void context_present_cache_flush(struct intel_iommu *iommu, u16 did,
 }
 
 static int domain_context_mapping_one(struct dmar_domain *domain,
+				      struct dmar_domain *old_domain,
 				      struct intel_iommu *iommu,
 				      u8 bus, u8 devfn)
 {
@@ -1148,7 +1149,8 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
 	u16 did = domain_id_iommu(domain, iommu);
 	int translation = CONTEXT_TT_MULTI_LEVEL;
 	struct pt_iommu_vtdss_hw_info pt_info;
-	struct context_entry *context;
+	struct context_entry *context, new_context;
+	u16 did_old;
 	int ret;
 
 	if (WARN_ON(!intel_domain_is_ss_paging(domain)))
@@ -1166,26 +1168,44 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
 		goto out_unlock;
 
 	ret = 0;
-	if (context_present(context) && !context_copied(iommu, bus, devfn))
+	if (!old_domain && (context_present(context) && !context_copied(iommu, bus, devfn)))
 		goto out_unlock;
 
+	if (old_domain) {
+		did_old = context_domain_id(context);
+		WARN_ON(did_old != domain_id_iommu(old_domain, iommu));
+	}
+
 	copied_context_tear_down(iommu, context, bus, devfn);
-	context_clear_entry(context);
-	context_set_domain_id(context, did);
+	context_set_domain_id(&new_context, did);
 
 	if (info && info->ats_supported)
 		translation = CONTEXT_TT_DEV_IOTLB;
 	else
 		translation = CONTEXT_TT_MULTI_LEVEL;
 
-	context_set_address_root(context, pt_info.ssptptr);
-	context_set_address_width(context, pt_info.aw);
-	context_set_translation_type(context, translation);
-	context_set_fault_enable(context);
-	context_set_present(context);
+	context_set_address_root(&new_context, pt_info.ssptptr);
+	context_set_address_width(&new_context, pt_info.aw);
+	context_set_translation_type(&new_context, translation);
+	context_set_fault_enable(&new_context);
+	context_set_present(&new_context);
+
+	*context = new_context;
 	if (!ecap_coherent(iommu->ecap))
 		clflush_cache_range(context, sizeof(*context));
-	context_present_cache_flush(iommu, did, bus, devfn);
+
+	/*
+	 * Spec 6.5.3.3, changing a present context entry requires,
+	 * - IOTLB invalidation for each effected Domain.
+	 * - Issue Device IOTLB invalidation for function.
+	 */
+	if (old_domain) {
+		intel_context_flush_no_pasid(info, context, did);
+		intel_context_flush_no_pasid(info, context, did_old);
+	} else {
+		context_present_cache_flush(iommu, did, bus, devfn);
+	}
+
 	ret = 0;
 
 out_unlock:
@@ -1194,30 +1214,39 @@ static int domain_context_mapping_one(struct dmar_domain *domain,
 	return ret;
 }
 
+struct domain_context_mapping_data {
+	struct dmar_domain *domain;
+	struct dmar_domain *old_domain;
+};
+
 static int domain_context_mapping_cb(struct pci_dev *pdev,
 				     u16 alias, void *opaque)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(&pdev->dev);
 	struct intel_iommu *iommu = info->iommu;
-	struct dmar_domain *domain = opaque;
+	struct domain_context_mapping_data *data = opaque;
 
-	return domain_context_mapping_one(domain, iommu,
+	return domain_context_mapping_one(data->domain, data->old_domain, iommu,
 					  PCI_BUS_NUM(alias), alias & 0xff);
 }
 
 static int
-domain_context_mapping(struct dmar_domain *domain, struct device *dev)
+domain_context_mapping(struct dmar_domain *domain,
+		       struct dmar_domain *old_domain, struct device *dev)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct intel_iommu *iommu = info->iommu;
 	u8 bus = info->bus, devfn = info->devfn;
+	struct domain_context_mapping_data data;
 	int ret;
 
 	if (!dev_is_pci(dev))
-		return domain_context_mapping_one(domain, iommu, bus, devfn);
+		return domain_context_mapping_one(domain, old_domain, iommu, bus, devfn);
 
+	data.domain = domain;
+	data.old_domain = old_domain;
 	ret = pci_for_each_dma_alias(to_pci_dev(dev),
-				     domain_context_mapping_cb, domain);
+				     domain_context_mapping_cb, &data);
 	if (ret)
 		return ret;
 
@@ -1309,18 +1338,28 @@ static int domain_setup_first_level(struct intel_iommu *iommu,
 					  pt_info.gcr3_pt, flags, old);
 }
 
-static int dmar_domain_attach_device(struct dmar_domain *domain,
-				     struct device *dev)
+static int device_replace_dmar_domain(struct dmar_domain *domain,
+				      struct dmar_domain *old_domain,
+				      struct device *dev)
 {
 	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	struct intel_iommu *iommu = info->iommu;
 	unsigned long flags;
 	int ret;
 
+	if (old_domain && dev_is_real_dma_subdevice(dev))
+		return -EOPNOTSUPP;
+
 	ret = domain_attach_iommu(domain, iommu);
 	if (ret)
 		return ret;
 
+	if (old_domain) {
+		spin_lock_irqsave(&info->domain->lock, flags);
+		list_del(&info->link);
+		spin_unlock_irqrestore(&info->domain->lock, flags);
+	}
+
 	info->domain = domain;
 	info->domain_attached = true;
 	spin_lock_irqsave(&domain->lock, flags);
@@ -1331,27 +1370,27 @@ static int dmar_domain_attach_device(struct dmar_domain *domain,
 		return 0;
 
 	if (!sm_supported(iommu))
-		ret = domain_context_mapping(domain, dev);
+		ret = domain_context_mapping(domain, old_domain, dev);
 	else if (intel_domain_is_fs_paging(domain))
 		ret = domain_setup_first_level(iommu, domain, dev,
-					       IOMMU_NO_PASID, NULL);
+					       IOMMU_NO_PASID, &old_domain->domain);
 	else if (intel_domain_is_ss_paging(domain))
 		ret = domain_setup_second_level(iommu, domain, dev,
-						IOMMU_NO_PASID, NULL);
+						IOMMU_NO_PASID, &old_domain->domain);
 	else if (WARN_ON(true))
 		ret = -EINVAL;
 
-	if (ret)
-		goto out_block_translation;
+	if (!ret)
+		ret = cache_tag_assign_domain(domain, dev, IOMMU_NO_PASID);
 
-	ret = cache_tag_assign_domain(domain, dev, IOMMU_NO_PASID);
 	if (ret)
-		goto out_block_translation;
+		device_block_translation(dev);
 
-	return 0;
+	if (old_domain) {
+		cache_tag_unassign_domain(old_domain, dev, IOMMU_NO_PASID);
+		domain_detach_iommu(old_domain, iommu);
+	}
 
-out_block_translation:
-	device_block_translation(dev);
 	return ret;
 }
 
@@ -3127,19 +3166,27 @@ static int intel_iommu_attach_device(struct iommu_domain *domain,
 				     struct device *dev,
 				     struct iommu_domain *old)
 {
+	struct device_domain_info *info = dev_iommu_priv_get(dev);
 	int ret;
 
-	device_block_translation(dev);
+	if (dev_is_real_dma_subdevice(dev) ||
+	    domain->type != __IOMMU_DOMAIN_PAGING ||
+	    !info->domain || &info->domain->domain != old)
+		old = NULL;
+
+	if (!old)
+		device_block_translation(dev);
 
 	ret = paging_domain_compatible(domain, dev);
 	if (ret)
 		return ret;
 
-	ret = iopf_for_domain_set(domain, dev);
+	ret = iopf_for_domain_replace(domain, old, dev);
 	if (ret)
 		return ret;
 
-	ret = dmar_domain_attach_device(to_dmar_domain(domain), dev);
+	ret = device_replace_dmar_domain(to_dmar_domain(domain),
+					 old ? to_dmar_domain(old) : NULL, dev);
 	if (ret)
 		iopf_for_domain_remove(domain, dev);
 
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#2 lore raw

PATCH 2/3 vfio: selftests: Add support of creating iommus from iommufd

In reply to Samiullah Khawaja

Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.

---
 .../vfio/lib/include/libvfio/iommu.h          |  2 +
 .../lib/include/libvfio/vfio_pci_device.h     |  2 +
 tools/testing/selftests/vfio/lib/iommu.c      | 60 +++++++++++++++++--
 .../selftests/vfio/lib/vfio_pci_device.c      | 16 ++++-
 4 files changed, 74 insertions(+), 6 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
index 5c9b9dc6d993..9e96da1e6fd3 100644
@@ -29,10 +29,12 @@ struct iommu {
 	int container_fd;
 	int iommufd;
 	u32 ioas_id;
+	u32 hwpt_id;
 	struct list_head dma_regions;
 };
 
 struct iommu *iommu_init(const char *iommu_mode);
+struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id);
 void iommu_cleanup(struct iommu *iommu);
 
 int __iommu_map(struct iommu *iommu, struct dma_region *region);
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2 -0
index 2858885a89bb..1143ceb6a9b8 100644
@@ -19,6 +19,7 @@ struct vfio_pci_device {
 	const char *bdf;
 	int fd;
 	int group_fd;
+	u32 dev_id;
 
 	struct iommu *iommu;
 
@@ -65,6 +66,7 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
 #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)
 #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32)
 
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu);
 void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,
 			 u32 vector, int count);
 void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index);
tools/testing/selftests/vfio/lib/iommu.c +56 -4

89 lines not on this page · raw message

tools/testing/selftests/vfio/lib/vfio_pci_device.c +14 -2
index fac4c0ecadef..9bc1f5ade5c4 100644
@@ -298,7 +298,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
 	return cdev_path;
 }
 
-static void vfio_device_bind_iommufd(int device_fd, int iommufd)
+static int vfio_device_bind_iommufd(int device_fd, int iommufd)
 {
 	struct vfio_device_bind_iommufd args = {
 		.argsz = sizeof(args),
@@ -306,6 +306,7 @@ static void vfio_device_bind_iommufd(int device_fd, int iommufd)
 	};
 
 	ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
+	return args.out_devid;
 }
 
 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
@@ -326,10 +327,21 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
 	VFIO_ASSERT_GE(device->fd, 0);
 	free((void *)cdev_path);
 
-	vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
+	device->dev_id = vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
 	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu)
+{
+	u32 pt_id = iommu->ioas_id;
+
+	if (iommu->hwpt_id)
+		pt_id = iommu->hwpt_id;
+
+	VFIO_ASSERT_NE(pt_id, 0);
+	vfio_device_attach_iommufd_pt(device->fd, pt_id);
+}
+
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
 {
 	struct vfio_pci_device *device;
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
maintainer
#3 lore raw

Re: [PATCH 2/3] vfio: selftests: Add support of creating iommus from iommufd

Quoted 102 lines · Samiullah Khawaja

Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.

---
 .../vfio/lib/include/libvfio/iommu.h          |  2 +
 .../lib/include/libvfio/vfio_pci_device.h     |  2 +
 tools/testing/selftests/vfio/lib/iommu.c      | 60 +++++++++++++++++--
 .../selftests/vfio/lib/vfio_pci_device.c      | 16 ++++-
 4 files changed, 74 insertions(+), 6 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
index 5c9b9dc6d993..9e96da1e6fd3 100644
@@ -29,10 +29,12 @@ struct iommu {
 	int container_fd;
 	int iommufd;
 	u32 ioas_id;
+	u32 hwpt_id;
 	struct list_head dma_regions;
 };
 
 struct iommu *iommu_init(const char *iommu_mode);
+struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id);
 void iommu_cleanup(struct iommu *iommu);
 
 int __iommu_map(struct iommu *iommu, struct dma_region *region);
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2 -0
index 2858885a89bb..1143ceb6a9b8 100644
@@ -19,6 +19,7 @@ struct vfio_pci_device {
 	const char *bdf;
 	int fd;
 	int group_fd;
+	u32 dev_id;
 
 	struct iommu *iommu;
 
@@ -65,6 +66,7 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
 #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)
 #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32)
 
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu);
 void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,
 			 u32 vector, int count);
 void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index);
tools/testing/selftests/vfio/lib/iommu.c +23 -3
index 58b7fb7430d4..2c67d7e24d0c 100644
@@ -408,6 +408,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges)
 	return ranges;
 }
 
+static u32 iommufd_hwpt_alloc(struct iommu *iommu, u32 dev_id)
+{
+	struct iommu_hwpt_alloc args = {
+		.size = sizeof(args),
+		.pt_id = iommu->ioas_id,
+		.dev_id = dev_id,
+	};
+
+	ioctl_assert(iommu->iommufd, IOMMU_HWPT_ALLOC, &args);
+	return args.out_hwpt_id;
+}
+
 static u32 iommufd_ioas_alloc(int iommufd)
 {
 	struct iommu_ioas_alloc args = {
@@ -418,11 +430,9 @@ static u32 iommufd_ioas_alloc(int iommufd)
 	return args.out_ioas_id;
 }
 
-struct iommu *iommu_init(const char *iommu_mode)
+static struct iommu *iommu_alloc(const char *iommu_mode)
 {
-	const char *container_path;
 	struct iommu *iommu;
-	int version;
 
 	iommu = calloc(1, sizeof(*iommu));
 	VFIO_ASSERT_NOT_NULL(iommu);
@@ -430,6 +440,16 @@ struct iommu *iommu_init(const char *iommu_mode)
 	INIT_LIST_HEAD(&iommu->dma_regions);
 
 	iommu->mode = lookup_iommu_mode(iommu_mode);
+	return iommu;
+}
+
+struct iommu *iommu_init(const char *iommu_mode)
+{
+	const char *container_path;
+	struct iommu *iommu;
+	int version;
+
+	iommu = iommu_alloc(iommu_mode);
 
 	container_path = iommu->mode->container_path;
 	if (container_path) {

tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 476

Quoted 4 lines
tools/testing/selftests/vfio/lib/iommu.c +1 -0
@@ -453,10 +473,42 @@ struct iommu *iommu_init(const char *iommu_mode)
 	return iommu;
 }
 
+struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id)

I don't think the name really captures what this routine is doing. How about iommufd_dup()?

Quoted 5 lines
tools/testing/selftests/vfio/lib/iommu.c +5 -0
+{
+	struct iommu *iommu;
+
+	iommu = iommu_alloc("iommufd");
+

tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 491

Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +10 -0
+	iommu->iommufd = dup(iommufd);
+	VFIO_ASSERT_GT(iommu->iommufd, 0);
+
+	iommu->ioas_id = iommufd_ioas_alloc(iommu->iommufd);
+	iommu->hwpt_id = iommufd_hwpt_alloc(iommu, dev_id);
+
+	return iommu;
+}
+
+static void iommufd_iommu_cleanup(struct iommu *iommu)

nit: iommufd_cleanup()

Quoted 47 lines
tools/testing/selftests/vfio/lib/iommu.c +17 -1
+{
+	struct iommu_destroy args = {
+		.size = sizeof(args),
+	};
+
+	if (iommu->hwpt_id) {
+		args.id = iommu->hwpt_id;
+		ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
+	}
+
+	args.id = iommu->ioas_id;
+	ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
+
+	VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
+}
+
 void iommu_cleanup(struct iommu *iommu)
 {
 	if (iommu->iommufd)
-		VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
+		iommufd_iommu_cleanup(iommu);
 	else
 		VFIO_ASSERT_EQ(close(iommu->container_fd), 0);
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -1
index fac4c0ecadef..9bc1f5ade5c4 100644
@@ -298,7 +298,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
 	return cdev_path;
 }
 
-static void vfio_device_bind_iommufd(int device_fd, int iommufd)
+static int vfio_device_bind_iommufd(int device_fd, int iommufd)
 {
 	struct vfio_device_bind_iommufd args = {
 		.argsz = sizeof(args),
@@ -306,6 +306,7 @@ static void vfio_device_bind_iommufd(int device_fd, int iommufd)
 	};
 
 	ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
+	return args.out_devid;
 }
 
 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
@@ -326,10 +327,21 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
 	VFIO_ASSERT_GE(device->fd, 0);

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 336

Quoted 10 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +4 -1
 	free((void *)cdev_path);
 
-	vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
+	device->dev_id = vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
 	vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu)
+{
+	u32 pt_id = iommu->ioas_id;

/* Only iommufd supports changing struct iommu attachments */ VFIO_ASSERT_TRUE(iommu->iommufd);

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 342

Quoted 6 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6 -0
+
+	if (iommu->hwpt_id)
+		pt_id = iommu->hwpt_id;
+
+	VFIO_ASSERT_NE(pt_id, 0);
+	vfio_device_attach_iommufd_pt(device->fd, pt_id);

device->iommu = iommu;

Quoted 8 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -0
+}
+
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
 {
 	struct vfio_pci_device *device;
Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#4 lore raw

Re: [PATCH 2/3] vfio: selftests: Add support of creating iommus from iommufd

Quoted 107 lines · David Matlack
Quoted 105 lines · Samiullah Khawaja

Add API to init a struct iommu using an already opened iommufd instance and attach devices to it.

---
 .../vfio/lib/include/libvfio/iommu.h          |  2 +
 .../lib/include/libvfio/vfio_pci_device.h     |  2 +
 tools/testing/selftests/vfio/lib/iommu.c      | 60 +++++++++++++++++--
 .../selftests/vfio/lib/vfio_pci_device.c      | 16 ++++-
 4 files changed, 74 insertions(+), 6 deletions(-)
tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +2 -0
index 5c9b9dc6d993..9e96da1e6fd3 100644
@@ -29,10 +29,12 @@ struct iommu {
      int container_fd;
      int iommufd;
      u32 ioas_id;
+     u32 hwpt_id;
      struct list_head dma_regions;
 };
 
 struct iommu *iommu_init(const char *iommu_mode);
+struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id);
 void iommu_cleanup(struct iommu *iommu);
 
 int __iommu_map(struct iommu *iommu, struct dma_region *region);
tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +2 -0
index 2858885a89bb..1143ceb6a9b8 100644
@@ -19,6 +19,7 @@ struct vfio_pci_device {
      const char *bdf;
      int fd;
      int group_fd;
+     u32 dev_id;
 
      struct iommu *iommu;
 
@@ -65,6 +66,7 @@ void vfio_pci_config_access(struct vfio_pci_device *device, bool write,
 #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u16)
 #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _v, u32)
 
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu);
 void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index,
                       u32 vector, int count);
 void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index);
tools/testing/selftests/vfio/lib/iommu.c +23 -3
index 58b7fb7430d4..2c67d7e24d0c 100644
@@ -408,6 +408,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iommu *iommu, u32 *nranges)
      return ranges;
 }
 
+static u32 iommufd_hwpt_alloc(struct iommu *iommu, u32 dev_id)
+{
+     struct iommu_hwpt_alloc args = {
+             .size = sizeof(args),
+             .pt_id = iommu->ioas_id,
+             .dev_id = dev_id,
+     };
+
+     ioctl_assert(iommu->iommufd, IOMMU_HWPT_ALLOC, &args);
+     return args.out_hwpt_id;
+}
+
 static u32 iommufd_ioas_alloc(int iommufd)
 {
      struct iommu_ioas_alloc args = {
@@ -418,11 +430,9 @@ static u32 iommufd_ioas_alloc(int iommufd)
      return args.out_ioas_id;
 }
 
-struct iommu *iommu_init(const char *iommu_mode)
+static struct iommu *iommu_alloc(const char *iommu_mode)
 {
-     const char *container_path;
      struct iommu *iommu;
-     int version;
 
      iommu = calloc(1, sizeof(*iommu));
      VFIO_ASSERT_NOT_NULL(iommu);
@@ -430,6 +440,16 @@ struct iommu *iommu_init(const char *iommu_mode)
      INIT_LIST_HEAD(&iommu->dma_regions);
 
      iommu->mode = lookup_iommu_mode(iommu_mode);
+     return iommu;
+}
+
+struct iommu *iommu_init(const char *iommu_mode)
+{
+     const char *container_path;
+     struct iommu *iommu;
+     int version;
+
+     iommu = iommu_alloc(iommu_mode);
 
      container_path = iommu->mode->container_path;
      if (container_path) {
@@ -453,10 +473,42 @@ struct iommu *iommu_init(const char *iommu_mode)
      return iommu;
 }
 
Quoted 4 lines

tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 476

Quoted 1 line
tools/testing/selftests/vfio/lib/iommu.c +1 -0
+struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id)

I don't think the name really captures what this routine is doing. How about iommufd_dup()?

The reason I used _iommu_init because it creates a new hwpt and ioas also, and it represents a separate "struct iommu". dup might indicate that it is pointing to the same IOAS? Do you think maybe dup is an implementation detail that doesn't need to be conveyed?

Do you think maybe I should rename it to iommufd_device_iommu_init as it is creating an hwpt compatible with the device "dev_id"? Or iommufd_iommu_init_for_device?

Quoted 10 lines
Quoted 10 lines
tools/testing/selftests/vfio/lib/iommu.c +9 -0
+{
+     struct iommu *iommu;
+
+     iommu = iommu_alloc("iommufd");
+
+     iommu->iommufd = dup(iommufd);
+     VFIO_ASSERT_GT(iommu->iommufd, 0);
+
+     iommu->ioas_id = iommufd_ioas_alloc(iommu->iommufd);
Quoted 8 lines

tools/testing/selftests/vfio/lib/iommu.c struct iommu *iommu_init(const char *iommu_mode) · line 491

Quoted 6 lines
tools/testing/selftests/vfio/lib/iommu.c +6 -0
+     iommu->hwpt_id = iommufd_hwpt_alloc(iommu, dev_id);
+
+     return iommu;
+}
+
+static void iommufd_iommu_cleanup(struct iommu *iommu)

nit: iommufd_cleanup()

Agreed.

Quoted 55 lines
Quoted 55 lines
tools/testing/selftests/vfio/lib/iommu.c +17 -1
+{
+     struct iommu_destroy args = {
+             .size = sizeof(args),
+     };
+
+     if (iommu->hwpt_id) {
+             args.id = iommu->hwpt_id;
+             ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
+     }
+
+     args.id = iommu->ioas_id;
+     ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args);
+
+     VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
+}
+
 void iommu_cleanup(struct iommu *iommu)
 {
      if (iommu->iommufd)
-             VFIO_ASSERT_EQ(close(iommu->iommufd), 0);
+             iommufd_iommu_cleanup(iommu);
      else
              VFIO_ASSERT_EQ(close(iommu->container_fd), 0);
 
tools/testing/selftests/vfio/lib/vfio_pci_device.c +3 -2
index fac4c0ecadef..9bc1f5ade5c4 100644
@@ -298,7 +298,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf)
      return cdev_path;
 }
 
-static void vfio_device_bind_iommufd(int device_fd, int iommufd)
+static int vfio_device_bind_iommufd(int device_fd, int iommufd)
 {
      struct vfio_device_bind_iommufd args = {
              .argsz = sizeof(args),
@@ -306,6 +306,7 @@ static void vfio_device_bind_iommufd(int device_fd, int iommufd)
      };
 
      ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args);
+     return args.out_devid;
 }
 
 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id)
@@ -326,10 +327,21 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b
      VFIO_ASSERT_GE(device->fd, 0);
      free((void *)cdev_path);
 
-     vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
+     device->dev_id = vfio_device_bind_iommufd(device->fd, device->iommu->iommufd);
      vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id);
 }
 
Quoted 6 lines

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 336

Quoted 3 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +3 -0
+void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct iommu *iommu)
+{
+     u32 pt_id = iommu->ioas_id;

/* Only iommufd supports changing struct iommu attachments */ VFIO_ASSERT_TRUE(iommu->iommufd);

Agreed

Quoted 10 lines

tools/testing/selftests/vfio/lib/vfio_pci_device.c static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *b · line 342

Quoted 7 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +6 -0
+
+     if (iommu->hwpt_id)
+             pt_id = iommu->hwpt_id;
+
+     VFIO_ASSERT_NE(pt_id, 0);
+     vfio_device_attach_iommufd_pt(device->fd, pt_id);

device->iommu = iommu;

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/vfio/lib/vfio_pci_device.c +2 -1
+}
+
 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu)
 {
      struct vfio_pci_device *device;
--

2.52.0.351.gbe84eed79e-goog

Thank you for the feedback.

#5 lore raw

PATCH 3/3 vfio: selftests: Add iommufd hwpt replace test

In reply to Samiullah Khawaja

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.

---
 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
tools/testing/selftests/vfio/Makefile +1 -0
index 3c796ca99a50..09a1e57cc77d 100644
@@ -1,5 +1,6 @@
 CFLAGS = $(KHDR_INCLUDES)
 TEST_GEN_PROGS += vfio_dma_mapping_test
+TEST_GEN_PROGS += vfio_iommufd_hwpt_replace_test
 TEST_GEN_PROGS += vfio_iommufd_setup_test
 TEST_GEN_PROGS += vfio_pci_device_test
 TEST_GEN_PROGS += vfio_pci_device_init_perf_test
tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c new file +151 -0

151 lines not on this page · raw message

Signature · 1 line
2.52.0.351.gbe84eed79e-goog
#6 lore raw

Re: [PATCH 3/3] vfio: selftests: Add iommufd hwpt replace test

kernel test robot noticed the following build warnings:

Hi Samiullah,

kernel test robot noticed the following build warnings:

[auto build test WARNING on 6cd6c12031130a349a098dbeb19d8c3070d2dfbe]

url:    https://github.com/intel-lab-lkp/linux/commits/Samiullah-Khawaja/iommu-vt-d-Allow-replacing-no_pasid-iommu_domain/20260108-041955
base:   6cd6c12031130a349a098dbeb19d8c3070d2dfbe
patch link:    https://lore.kernel.org/r/20260107201800.2486137-4-skhawaja%40google.com
patch subject: [PATCH 3/3] vfio: selftests: Add iommufd hwpt replace test
config: i386-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260109/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260109/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:6:
   tools/testing/selftests/vfio/lib/include/libvfio/iommu.h:51:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      51 |         VFIO_ASSERT_EQ(__iommu_unmap(iommu, region, NULL), 0);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:6:
   tools/testing/selftests/vfio/lib/include/libvfio/iommu.h:58:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      58 |         VFIO_ASSERT_EQ(__iommu_unmap_all(iommu, NULL), 0);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:6:
   tools/testing/selftests/vfio/lib/include/libvfio/iommu.h:58:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      58 |         VFIO_ASSERT_EQ(__iommu_unmap_all(iommu, NULL), 0);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:8:
   tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h:80:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      80 |         VFIO_ASSERT_NE(r, -1, "F_GETFL failed for fd %d\n", fd);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:8:
   tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h:80:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      80 |         VFIO_ASSERT_NE(r, -1, "F_GETFL failed for fd %d\n", fd);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:8:
   tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h:83:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      83 |         VFIO_ASSERT_NE(r, -1, "F_SETFL O_NONBLOCK failed for fd %d\n", fd);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
   In file included from tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:8:
   In file included from tools/testing/selftests/vfio/lib/include/libvfio.h:8:
   tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h:83:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      83 |         VFIO_ASSERT_NE(r, -1, "F_SETFL O_NONBLOCK failed for fd %d\n", fd);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~
>> tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:23:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      23 |         VFIO_ASSERT_NE(vaddr, MAP_FAILED);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
>> tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:23:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      23 |         VFIO_ASSERT_NE(vaddr, MAP_FAILED);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:33:37: note: expanded from macro 'VFIO_ASSERT_NE'
      33 | #define VFIO_ASSERT_NE(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, !=, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~
   tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:35:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      35 |         VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:4: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                      ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                         ^~~~~~~~~~
   tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c:35:2: warning: format specifies type 'unsigned long' but the argument has type 'u64' (aka 'unsigned long long') [-Wformat]
      35 |         VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:32:37: note: expanded from macro 'VFIO_ASSERT_EQ'
      32 | #define VFIO_ASSERT_EQ(_a, _b, ...) VFIO_ASSERT_OP(_a, _b, ==, ##__VA_ARGS__)
         |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   tools/testing/selftests/vfio/lib/include/libvfio/assert.h:27:22: note: expanded from macro 'VFIO_ASSERT_OP'
      26 |         fprintf(stderr, "  Observed: %#lx %s %#lx\n",                           \
         |                                              ~~~~
      27 |                         (u64)__lhs, #_op, (u64)__rhs);                          \
         |                                           ^~~~~~~~~~

14 warnings generated.

vim +23 tools/testing/selftests/vfio/vfio_iommufd_hwpt_replace_test.c

    13	
    14	static void region_setup(struct iommu *iommu,
    15				 struct iova_allocator *iova_allocator,
    16				 struct dma_region *region, u64 size)
    17	{
    18		const int flags = MAP_SHARED | MAP_ANONYMOUS;
    19		const int prot = PROT_READ | PROT_WRITE;
    20		void *vaddr;
    21	
    22		vaddr = mmap(NULL, size, prot, flags, -1, 0);
  > 23		VFIO_ASSERT_NE(vaddr, MAP_FAILED);
    24	
    25		region->vaddr = vaddr;
    26		region->iova = iova_allocator_alloc(iova_allocator, size);
    27		region->size = size;
    28	
    29		iommu_map(iommu, region);
    30	}
    31	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw
In reply to Samiullah Khawaja
Quoted 2 lines · Samiullah Khawaja

Intel IOMMU Driver already supports replacing IOMMU domain hitlessly in scalable mode.

It does? We were just talking about how it doesn't work because it makes the PASID entry non-present while loading the new domain.

Jason

#8 lore raw
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Samiullah Khawaja

Intel IOMMU Driver already supports replacing IOMMU domain hitlessly in scalable mode.

It does? We were just talking about how it doesn't work because it makes the PASID entry non-present while loading the new domain.

If you tried your tests in scalable mode they are probably only working because the HW is holding the entry in cache while the CPU is completely mangling it:

int intel_pasid_replace_first_level(struct intel_iommu *iommu,

				    struct device *dev, phys_addr_t fsptptr,
				    u32 pasid, u16 did, u16 old_did,
				    int flags)
{
[..]
	*pte = new_pte;

That just doesn't work for "replace", it isn't hitless unless the entry stays in the cache. Since your test effectively will hold the context entry in the cache while testing for "hitless" it doesn't really test if it is really working without races..

All of this needs to be reworked to always use the stack to build the entry, like the replace path does, and have a ARM-like algorithm to update the live memory in just the right order to guarentee the HW does not see a corrupted entry.

It is a little bit tricky, but it should start with reworking everything to consistently use the stack to create the new entry and calling a centralized function to set the new entry to the live memory. This replace/not replace split should be purged completely.

Some discussion is here

https://lore.kernel.org/all/[email protected]/

It also needs to be very careful that the invalidation is doing both the old and new context entry concurrently while it is being replaced.

For instance the placement of cache_tag_assign_domain() looks wrong to me, it can't be *after* the HW has been programmed to use the new tags :\

I also didn't note where the currently active cache_tag is removed from the linked list during attach, is that another bug?

In short, this needs alot of work to actually properly implement hitless replace the way ARM can. Fortunately I think it is mostly mechanical and should be fairly straightfoward. Refer to the ARM driver and try to structure vtd to have the same essential flow..

Jason

#9 lore raw
Quoted 19 lines · Jason Gunthorpe
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Samiullah Khawaja

Intel IOMMU Driver already supports replacing IOMMU domain hitlessly in scalable mode.

It does? We were just talking about how it doesn't work because it makes the PASID entry non-present while loading the new domain.

If you tried your tests in scalable mode they are probably only working because the HW is holding the entry in cache while the CPU is completely mangling it:

int intel_pasid_replace_first_level(struct intel_iommu *iommu,
struct device *dev, phys_addr_t fsptptr, u32 pasid, u16 did, u16 old_did, int flags)
{
[..]

Quoted 6 lines
        *pte = new_pte;

That just doesn't work for "replace", it isn't hitless unless the entry stays in the cache. Since your test effectively will hold the context entry in the cache while testing for "hitless" it doesn't really test if it is really working without races..

Ah.. you are absolutely correct. This will not work if the entries are not cached.

Quoted 5 lines

All of this needs to be reworked to always use the stack to build the entry, like the replace path does, and have a ARM-like algorithm to update the live memory in just the right order to guarentee the HW does not see a corrupted entry.

Agreed. I will go through the VTD specs and also the ARM driver to determine the right order to set this up.

Quoted 6 lines

It is a little bit tricky, but it should start with reworking everything to consistently use the stack to create the new entry and calling a centralized function to set the new entry to the live memory. This replace/not replace split should be purged completely.

Quoted 6 lines

Some discussion is here

https://lore.kernel.org/all/[email protected]/

It also needs to be very careful that the invalidation is doing both the old and new context entry concurrently while it is being replaced.

Yes, let me follow the discussion over there closely.

Quoted 7 lines

For instance the placement of cache_tag_assign_domain() looks wrong to me, it can't be *after* the HW has been programmed to use the new tags :\

I also didn't note where the currently active cache_tag is removed from the linked list during attach, is that another bug?

You are correct, the placement of cache_tag_assign_domain is wrong. The removal of the currently active cache tag is done in dmar_domain_attach_device, but I will re-evaluate the placement of both in the v2.

Quoted 5 lines

In short, this needs alot of work to actually properly implement hitless replace the way ARM can. Fortunately I think it is mostly mechanical and should be fairly straightfoward. Refer to the ARM driver and try to structure vtd to have the same essential flow..

Thank you for the feedback. I will prepare a v2 series addressing these points.

Quoted 2 lines

Jason

#10 lore raw
Quoted 1 line

Thank you for the feedback. I will prepare a v2 series addressing these points.

I think there are so many problems here you should talk to Kevin and Baolu to come up with some plan. A single series is not going to be able to do all of this.

Jason

maintainer
#11 lore raw
Quoted 4 lines · Jason Gunthorpe
Quoted 1 line

Thank you for the feedback. I will prepare a v2 series addressing these points.

I think there are so many problems here you should talk to Kevin and Baolu to come up with some plan. A single series is not going to be able to do all of this.

Yes, absolutely. I am working on a patch series to address the fundamental atomicity issue. I hope to post it for discussion soon. Once that is addressed, we can discuss any additional problems.

Thanks,
baolu