Skip to contentSkip to conversation

iommufd: Infrastructure for vIOMMU creation for confidential guests and guest TSM requests

RFC PATCH Superseded Aneesh Kumar K.V (Arm) 8 participants 74 messages Sep 17 – Oct 22

Subsystems (14)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Aneesh Kumar K.V (Arm)

author maintainer
#0 lore raw

v6 cover iommufd: Infrastructure for vIOMMU creation for confidential guests and guest TSM requests

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It groups the vIOMMU operations with their module owner and private data, and makes that implementation discoverable during vIOMMU allocation.

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

Guest TSM requests are dispatched through the vdevice. PCI/TSM uses reference-counted contexts to retain the resources needed by providers, without introducing separate IOMMUFD TSM bind or unbind ioctls.

Note: Codex was used to assist with commit message formatting and code rearrangement.

Changes from v5:
https://lore.kernel.org/all/[email protected]
* Replace the TSM bind/unbind interface with reference-counted contexts.
* Add the IOMMUFD vIOMMU provider abstraction.
* Route TSM guest requests through viommu operations.

Changes from v4:
https://lore.kernel.org/all/[email protected]
* Switch VFIO/iommufd to use struct file *kvm_file instead of relying on kvm->users_count references.
* Define TSM request scope values globally in iommufd.
* Rename the ioctl to IOMMU_VDEVICE_TSM_REQ.
* Address other review feedback.

Changes from v2:
https://lore.kernel.org/all/[email protected]
* Bump the series revision to v4 to keep it in sync with the dependent CCA DA patchsets. There was no v3 posting.
* Drop [PATCH v2 1/3] iommufd/viommu: Allow associating a KVM VM fd with a vIOMMU
* Add two new patches to associate a struct kvm * with iommufd objects:
iommufd/device: Associate a kvm pointer to iommufd_device iommufd/viommu: Associate a kvm pointer to iommufd_viommu
* Address review feedback

Changes from v1:
https://lore.kernel.org/all/[email protected]
* Rebase onto the latest kernel
* Address review feedback
* Drop the TSM map ioctl; the KVM prefault patch will be used instead to ensure that private memory is preallocated

Cc Jason Gunthorpe <[email protected]>
Cc Alexey Kardashevskiy <[email protected]>
Cc Bjorn Helgaas <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Jonathan Cameron <[email protected]>
Cc Kevin Tian <[email protected]>
Cc Nicolin Chen <[email protected]>
Cc Samuel Ortiz <[email protected]>
Cc Steven Price <[email protected]>
Cc Suzuki K Poulose <[email protected]>
Cc Will Deacon <[email protected]>
Cc Xu Yilun <[email protected]>
Cc Shameer Kolothum <[email protected]>
Cc Paolo Bonzini <[email protected]>

Aneesh Kumar K.V (Arm) (9):
vfio: cache KVM VM file references instead of raw struct kvm pointers
vfio: cdev: Reject duplicate bind before updating KVM file
iommu: Add a helper to validate a vIOMMU parent
iommu: Add a helper to query vIOMMU hardware parameters
coco: tsm: Expose active-user lifetime references
iommufd: Add vIOMMU provider support
iommufd: Add the vdevice TSM request ioctl
PCI/TSM: Remove the legacy guest request interface PCI/TSM: Add reference-counted contexts for vdevice providers

Nicolin Chen (1):
iommufd/viommu: Keep a reference to the KVM file

Shameer Kolothum (1):
iommufd/device: Associate KVM file pointer with iommufd_device

 Documentation/ABI/testing/sysfs-bus-pci |  29 ++-
 drivers/iommu/iommu.c                   |  22 ++
 drivers/iommu/iommufd/Makefile          |   5 +-
 drivers/iommu/iommufd/device.c          |   7 +-
 drivers/iommu/iommufd/iommufd_private.h |  32 +++
 drivers/iommu/iommufd/main.c            |   3 +
 drivers/iommu/iommufd/selftest.c        |   2 +-
 drivers/iommu/iommufd/tsm.c             |  98 ++++++++
 drivers/iommu/iommufd/viommu.c          |  36 ++-
 drivers/iommu/iommufd/viommu_provider.c | 160 ++++++++++++
 drivers/pci/tsm/core.c                  | 312 ++++++++++--------------
 drivers/s390/crypto/vfio_ap_ops.c       |   5 +-
 drivers/vfio/device_cdev.c              |  14 +-
 drivers/vfio/group.c                    |  14 +-
 drivers/vfio/iommufd.c                  |   3 +-
 drivers/vfio/pci/vfio_pci_zdev.c        |   7 +-
 drivers/vfio/vfio.h                     |  16 +-
 drivers/vfio/vfio_main.c                |  81 +++---
 drivers/virt/coco/tsm-core.c            |  55 ++++-
 include/linux/iommu.h                   |  15 ++
 include/linux/iommufd.h                 |  42 +++-
 include/linux/kvm_host.h                |   3 +
 include/linux/pci-tsm.h                 | 140 ++++-------
 include/linux/tsm.h                     |  38 +++
 include/linux/vfio.h                    |  17 +-
 include/uapi/linux/iommufd.h            |  73 ++++++
 samples/devsec/link_tsm.c               | 146 -----------
 tools/testing/devsec/devsec.sh          |  27 +-
 virt/kvm/kvm_main.c                     |   2 +
 29 files changed, 866 insertions(+), 538 deletions(-)
 create mode 100644 drivers/iommu/iommufd/tsm.c
 create mode 100644 drivers/iommu/iommufd/viommu_provider.c
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

RFC PATCH v6 1/11 vfio: cache KVM VM file references instead of raw struct kvm pointers

VFIO currently records struct kvm pointers on vfio_group, vfio_device_file and the opened vfio_device. Switch VFIO to track the VM's struct file instead, so VFIO and iommufd can use normal file references for VM lifetime instead of depending on KVM's internal struct kvm refcounting.

KVM_CREATE_DEVICE binds the KVM VM lifetime to the KVM device fd lifetime. For KVM_DEV_TYPE_VFIO, the KVM VFIO device fd also takes references to each VFIO file added through KVM_DEV_VFIO_FILE_ADD. The KVM VFIO device fd therefore owns both the internal KVM reference and the VFIO file references in kvf->file.

KVM_DEV_VFIO_FILE_ADD further installs the VM file association into the VFIO file. VFIO converts the struct kvm pointer to a VM file reference with get_file_active(&kvm->_file), because the KVM device fd can keep struct kvm alive after the original VM fd is already in final release.

The association intentionally pins the VM file until KVM_DEV_VFIO_FILE_DEL or until the KVM VFIO device fd is released. This gives VFIO/iommufd a stable VM file reference source without taking a dependency on KVM's struct kvm lifetime. The KVM VFIO device release path clears the VFIO-side association before dropping its VFIO file references.

When a VFIO device is opened or bound, VFIO takes an additional reference from the associated VM file and stores it in vfio_device::kvm_file for driver and iommufd use. That open-time reference is released from vfio_device_put_kvm() when the VFIO device is closed or unbound.

This gives the ownership model:

- KVM device fd pins struct kvm through kvm->users_count
- KVM VFIO device fd pins VFIO files through kvf->file
- VFIO group/device-file state pins the VM file while associated with KVM
- vfio_device::kvm_file pins the VM file during active VFIO device use

---
 drivers/s390/crypto/vfio_ap_ops.c |  5 +-
 drivers/vfio/device_cdev.c        | 10 ++--
 drivers/vfio/group.c              | 14 +++---
 drivers/vfio/pci/vfio_pci_zdev.c  |  7 +--
 drivers/vfio/vfio.h               | 16 ++++--
 drivers/vfio/vfio_main.c          | 81 ++++++++++++++++---------------
 include/linux/kvm_host.h          |  3 ++
 include/linux/vfio.h              | 17 ++++++-
 virt/kvm/kvm_main.c               |  2 +
 9 files changed, 91 insertions(+), 64 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +3 -2
index 44b3a1dcc1b3..05996a8fd860 100644
@@ -2054,11 +2054,12 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
 {
 	struct ap_matrix_mdev *matrix_mdev =
 		container_of(vdev, struct ap_matrix_mdev, vdev);
+	struct kvm *kvm = vfio_device_get_kvm(vdev);
 
-	if (!vdev->kvm)
+	if (!kvm)
 		return -EINVAL;
 
-	return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
+	return vfio_ap_mdev_set_kvm(matrix_mdev, kvm);
 }
 
 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
drivers/vfio/device_cdev.c +5 -5
index 54abf312cf04..ca75ab8eb7bd 100644
@@ -56,7 +56,7 @@ int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep)
 static void vfio_df_get_kvm_safe(struct vfio_device_file *df)
 {
 	spin_lock(&df->kvm_ref_lock);
-	vfio_device_get_kvm_safe(df->device, df->kvm);
+	vfio_device_get_kvm_safe(df->device, df->kvm_file);
 	spin_unlock(&df->kvm_ref_lock);
 }
 
@@ -133,10 +133,10 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 	}
 
 	/*
-	 * Before the device open, get the KVM pointer currently
-	 * associated with the device file (if there is) and obtain
-	 * a reference.  This reference is held until device closed.
-	 * Save the pointer in the device for use by drivers.
+	 * Before the device open, get the VM struct file currently
+	 * associated with the device file (if there is one) and obtain a
+	 * reference. This reference is held until the device is closed.
+	 * Save the file in the device for use by drivers.
 	 */
 	vfio_df_get_kvm_safe(df);
 
drivers/vfio/group.c +6 -8
index b2299e5bc6df..8950cfb9405d 100644
@@ -163,7 +163,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
 static void vfio_device_group_get_kvm_safe(struct vfio_device *device)
 {
 	spin_lock(&device->group->kvm_ref_lock);
-	vfio_device_get_kvm_safe(device, device->group->kvm);
+	vfio_device_get_kvm_safe(device, device->group->kvm_file);
 	spin_unlock(&device->group->kvm_ref_lock);
 }
 
@@ -181,10 +181,10 @@ static int vfio_df_group_open(struct vfio_device_file *df)
 	mutex_lock(&device->dev_set->lock);
 
 	/*
-	 * Before the first device open, get the KVM pointer currently
-	 * associated with the group (if there is one) and obtain a reference
-	 * now that will be held until the open_count reaches 0 again.  Save
-	 * the pointer in the device for use by drivers.
+	 * Before the first device open, get the VM struct file currently
+	 * associated with the group (if there is one) and obtain a
+	 * reference now that will be held until the open_count reaches 0
+	 * again. Save the file in the device for use by drivers.
 	 */
 	if (device->open_count == 0)
 		vfio_device_group_get_kvm_safe(device);
@@ -862,9 +862,7 @@ bool vfio_group_enforced_coherent(struct vfio_group *group)
 
 void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
 {
-	spin_lock(&group->kvm_ref_lock);
-	group->kvm = kvm;
-	spin_unlock(&group->kvm_ref_lock);
+	vfio_kvm_file_replace(&group->kvm_file, &group->kvm_ref_lock, kvm);
 }
 
 /**
drivers/vfio/pci/vfio_pci_zdev.c +4 -3
index 0990fdb146b7..a9d8e6aa3839 100644
@@ -144,15 +144,16 @@ int vfio_pci_info_zdev_add_caps(struct vfio_pci_core_device *vdev,
 int vfio_pci_zdev_open_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
+	struct kvm *kvm = vfio_device_get_kvm(&vdev->vdev);
 
 	if (!zdev)
 		return -ENODEV;
 
-	if (!vdev->vdev.kvm)
+	if (!kvm)
 		return 0;
 
 	if (zpci_kvm_hook.kvm_register)
-		return zpci_kvm_hook.kvm_register(zdev, vdev->vdev.kvm);
+		return zpci_kvm_hook.kvm_register(zdev, kvm);
 
 	return -ENOENT;
 }
@@ -161,7 +162,7 @@ void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
 {
 	struct zpci_dev *zdev = to_zpci(vdev->pdev);
 
-	if (!zdev || !vdev->vdev.kvm)
+	if (!zdev || !vfio_device_get_kvm(&vdev->vdev))
 		return;
 
 	if (zpci_kvm_hook.kvm_unregister)
drivers/vfio/vfio.h +11 -5
index e4b72e79b7e3..41032104eb36 100644
@@ -22,8 +22,8 @@ struct vfio_device_file {
 
 	u8 access_granted;
 	u32 devid; /* only valid when iommufd is valid */
-	spinlock_t kvm_ref_lock; /* protect kvm field */
-	struct kvm *kvm;
+	spinlock_t kvm_ref_lock; /* protect kvm_file */
+	struct file *kvm_file;
 	struct iommufd_ctx *iommufd; /* protected by struct vfio_device_set::lock */
 };
 
@@ -88,7 +88,7 @@ struct vfio_group {
 #endif
 	enum vfio_group_type		type;
 	struct mutex			group_lock;
-	struct kvm			*kvm;
+	struct file			*kvm_file;
 	struct file			*opened_file;
 	struct iommufd_ctx		*iommufd;
 	spinlock_t			kvm_ref_lock;
@@ -434,11 +434,17 @@ static inline void vfio_virqfd_exit(void)
 #endif
 
 #if IS_ENABLED(CONFIG_KVM)
-void vfio_device_get_kvm_safe(struct vfio_device *device, struct kvm *kvm);
+void vfio_kvm_file_replace(struct file **dst, spinlock_t *lock, struct kvm *kvm);
+void vfio_device_get_kvm_safe(struct vfio_device *device, struct file *kvm_file);
 void vfio_device_put_kvm(struct vfio_device *device);
 #else
+static inline void vfio_kvm_file_replace(struct file **dst,
+		spinlock_t *lock, struct kvm *kvm)
+{
+}
+
 static inline void vfio_device_get_kvm_safe(struct vfio_device *device,
-					    struct kvm *kvm)
+					    struct file *kvm_file)
 {
 }
 
drivers/vfio/vfio_main.c +42 -39

118 lines not on this page · raw message

include/linux/kvm_host.h +3 -0
index 9c1cf1a6559e..b3b0fa7d53a2 100644
@@ -45,6 +45,8 @@
 #include <asm/kvm_host.h>
 #include <linux/kvm_dirty_ring.h>
 
+struct file;
+
 #ifndef KVM_MAX_VCPU_IDS
 #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS
 #endif
@@ -840,6 +842,7 @@ struct kvm {
 	struct srcu_struct srcu;
 	struct srcu_struct irq_srcu;
 	pid_t userspace_pid;
+	struct file __rcu *_file;
 	bool override_halt_poll_ns;
 	unsigned int max_halt_poll_ns;
 	u32 dirty_ring_size;
include/linux/vfio.h +15 -2
index 31b826efba00..bca1d00f7845 100644
@@ -22,8 +22,22 @@ struct kvm;
 struct iommufd_ctx;
 struct iommufd_device;
 struct iommufd_access;
+struct vfio_device;
 struct vfio_info_cap;
 
+#if IS_ENABLED(CONFIG_KVM)
+/*
+ * Return the KVM associated with @vdev's kvm_file. The returned pointer
+ * is valid only while VFIO device open holds the kvm_file reference.
+ */
+struct kvm *vfio_device_get_kvm(struct vfio_device *vdev);
+#else
+static inline struct kvm *vfio_device_get_kvm(struct vfio_device *vdev)
+{
+	return NULL;
+}
+#endif
+
 /*
  * VFIO devices can be placed in a set, this allows all devices to share this
  * structure and the VFIO core will provide a lock that is held around
@@ -54,7 +68,7 @@ struct vfio_device {
 	struct list_head dev_set_list;
 	unsigned int migration_flags;
 	u8 precopy_info_v2;
-	struct kvm *kvm;
+	struct file *kvm_file;
 
 	/* Members below here are private, not for driver use */
 	unsigned int index;
@@ -66,7 +80,6 @@ struct vfio_device {
 	unsigned int open_count;
 	struct completion comp;
 	struct iommufd_access *iommufd_access;
-	void (*put_kvm)(struct kvm *kvm);
 	struct inode *inode;
 #if IS_ENABLED(CONFIG_IOMMUFD)
 	struct iommufd_device *iommufd_device;
virt/kvm/kvm_main.c +2 -0
index 46e92b5dc380..ba39e3857e7d 100644
@@ -1369,6 +1369,7 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
 
 	kvm_irqfd_release(kvm);
 
+	RCU_INIT_POINTER(kvm->_file, NULL);
 	kvm_put_kvm(kvm);
 	return 0;
 }
@@ -5550,6 +5551,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
 		r = PTR_ERR(file);
 		goto put_kvm;
 	}
+	rcu_assign_pointer(kvm->_file, file);
 
 	/*
 	 * Don't call kvm_put_kvm anymore at this point; file->f_op is
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [RFC PATCH v6 01/11] vfio: cache KVM VM file references instead of raw struct kvm pointers

Quoted 4 lines

VFIO currently records struct kvm pointers on vfio_group, vfio_device_file and the opened vfio_device. Switch VFIO to track the VM's struct file instead, so VFIO and iommufd can use normal file references for VM lifetime instead of depending on KVM's internal struct kvm refcounting.

Are these few patches the same as:
https://lore.kernel.org/all/[email protected]/

Can we align on that series or is there something else here?

Signature · 1 line
Jason
author maintainer
#3 lore raw

Re: [RFC PATCH v6 01/11] vfio: cache KVM VM file references instead of raw struct kvm pointers

Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines

VFIO currently records struct kvm pointers on vfio_group, vfio_device_file and the opened vfio_device. Switch VFIO to track the VM's struct file instead, so VFIO and iommufd can use normal file references for VM lifetime instead of depending on KVM's internal struct kvm refcounting.

Are these few patches the same as:
https://lore.kernel.org/all/[email protected]/

Can we align on that series or is there something else here?

Yes. I will use v4 of that series as the basis for this patch series.

-aneesh

author maintainer
#4 lore raw

RFC PATCH v6 2/11 vfio: cdev: Reject duplicate bind before updating KVM file

In reply to Aneesh Kumar K.V (Arm)

The cdev path only supports one bound/open device fd, but VFIO_DEVICE_BIND_IOMMUFD only checked the per-file access_granted flag before capturing the KVM file reference. A second fd for the same device could therefore replace device->kvm_file, fail later in vfio_df_open() because open_count is already nonzero, and then clear the active KVM association during error cleanup.

Reject the bind while holding dev_set->lock if the device is already open, matching the existing cdev single-open rule before touching the device-wide KVM state.

---
 drivers/vfio/device_cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/device_cdev.c +2 -2
index ca75ab8eb7bd..67e48f7ebfc3 100644
@@ -115,8 +115,8 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,
 		return ret;
 
 	mutex_lock(&device->dev_set->lock);
-	/* one device cannot be bound twice */
-	if (df->access_granted) {
+	/* The cdev path only supports one bound/open device fd. */
+	if (df->access_granted || device->open_count) {
 		ret = -EINVAL;
 		goto out_unlock;
 	}
Signature · 1 line
2.43.0
maintainer
#5 lore raw

RE: [RFC PATCH v6 02/11] vfio: cdev: Reject duplicate bind before updating KVM file

Quoted 10 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

The cdev path only supports one bound/open device fd, but VFIO_DEVICE_BIND_IOMMUFD only checked the per-file access_granted flag before capturing the KVM file reference. A second fd for the same device could therefore replace device->kvm_file, fail later in vfio_df_open() because open_count is already nonzero, and then clear the active KVM association during error cleanup.

Quoted 5 lines

Reject the bind while holding dev_set->lock if the device is already open, matching the existing cdev single-open rule before touching the device-wide KVM state.

could you send it out as a separate fix?

Quoted 22 lines
---
 drivers/vfio/device_cdev.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/device_cdev.c +2 -2
index ca75ab8eb7bd..67e48f7ebfc3 100644
@@ -115,8 +115,8 @@ long vfio_df_ioctl_bind_iommufd(struct
 vfio_device_file *df,
 		return ret;
 
 	mutex_lock(&device->dev_set->lock);
-	/* one device cannot be bound twice */
-	if (df->access_granted) {
+	/* The cdev path only supports one bound/open device fd. */
+	if (df->access_granted || device->open_count) {
 		ret = -EINVAL;
 		goto out_unlock;

}
--
2.43.0

author maintainer
#6 lore raw

RE: [RFC PATCH v6 02/11] vfio: cdev: Reject duplicate bind before updating KVM file

Quoted 10 lines · Kevin
Quoted 10 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

The cdev path only supports one bound/open device fd, but VFIO_DEVICE_BIND_IOMMUFD only checked the per-file access_granted flag before capturing the KVM file reference. A second fd for the same device could therefore replace device->kvm_file, fail later in vfio_df_open() because open_count is already nonzero, and then clear the active KVM association during error cleanup.

Quoted 8 lines
Quoted 5 lines

Reject the bind while holding dev_set->lock if the device is already open, matching the existing cdev single-open rule before touching the device-wide KVM state.

could you send it out as a separate fix?

Sure, I'll send it as a separate fix.

-aneesh

author maintainer
#7 lore raw

RFC PATCH v6 3/11 iommufd/device: Associate KVM file pointer with iommufd_device

In reply to Aneesh Kumar K.V (Arm)

From: Shameer Kolothum <[email protected]>

TSM vDevice support needs access to the KVM associated with a VFIO device after the device has been bound to iommufd.

Extend iommufd_device_bind() to accept the device's KVM file and store it in the iommufd_device. The KVM file reference is owned by VFIO and is already held for the duration of the device open path.

[nicolinc: fix build error in iommufd_test_mock_domain()]
[aneesh.kumar: Switch to use kvm_file]
---
 drivers/iommu/iommufd/device.c          | 7 ++++++-
 drivers/iommu/iommufd/iommufd_private.h | 2 ++
 drivers/iommu/iommufd/selftest.c        | 2 +-
 drivers/vfio/iommufd.c                  | 3 ++-
 include/linux/iommufd.h                 | 4 +++-
 5 files changed, 14 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/device.c +6 -1
index 170a7005f0bc..718abdc0e627 100644
@@ -203,6 +203,7 @@ void iommufd_device_destroy(struct iommufd_object *obj)
  * iommufd_device_bind - Bind a physical device to an iommu fd
  * @ictx: iommufd file descriptor
  * @dev: Pointer to a physical device struct
+ * @kvm_file: VM file if device belongs to a KVM VM
  * @id: Output ID number to return to userspace for this device
  *
  * A successful bind establishes an ownership over the device and returns
@@ -216,7 +217,9 @@ void iommufd_device_destroy(struct iommufd_object *obj)
  * The caller must undo this with iommufd_device_unbind()
  */
 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
-					   struct device *dev, u32 *id)
+					   struct device *dev,
+					   struct file *kvm_file,
+					   u32 *id)
 {
 	struct iommufd_device *idev;
 	struct iommufd_group *igroup;
@@ -266,6 +269,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
 	if (!iommufd_selftest_is_mock_dev(dev))
 		iommufd_ctx_get(ictx);
 	idev->dev = dev;
+	/* reference is already taken in vfio_df_ioctl_bind_iommufd() */
+	idev->kvm_file = kvm_file;
 	idev->enforce_cache_coherency =
 		device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
 	/* The calling driver is a user until iommufd_device_unbind() */
drivers/iommu/iommufd/iommufd_private.h +2 -0
index 43fbc5bed8de..6cf76f7ca379 100644
@@ -488,6 +488,8 @@ struct iommufd_device {
 	struct list_head group_item;
 	/* always the physical device */
 	struct device *dev;
+	/* ..and the VM file if available */
+	struct file *kvm_file;
 	bool enforce_cache_coherency;
 	struct iommufd_vdevice *vdev;
 	bool destroying;
drivers/iommu/iommufd/selftest.c +1 -1
index af07c642a526..a193390f9d07 100644
@@ -1069,7 +1069,7 @@ static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
 		goto out_sobj;
 	}
 
-	idev = iommufd_device_bind(ucmd->ictx, &sobj->idev.mock_dev->dev,
+	idev = iommufd_device_bind(ucmd->ictx, &sobj->idev.mock_dev->dev, NULL,
 				   &idev_id);
 	if (IS_ERR(idev)) {
 		rc = PTR_ERR(idev);
drivers/vfio/iommufd.c +2 -1
index a38d262c6028..d2d0bd9382a1 100644
@@ -119,7 +119,8 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev,
 {
 	struct iommufd_device *idev;
 
-	idev = iommufd_device_bind(ictx, vdev->dev, out_device_id);
+	idev = iommufd_device_bind(ictx, vdev->dev, vdev->kvm_file,
+				   out_device_id);
 	if (IS_ERR(idev))
 		return PTR_ERR(idev);
 	vdev->iommufd_device = idev;
include/linux/iommufd.h +3 -1
index 6e7efe83bc5d..0a0bb4abfbd2 100644
@@ -59,7 +59,9 @@ struct iommufd_object {
 };
 
 struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
-					   struct device *dev, u32 *id);
+					   struct device *dev,
+					   struct file *kvm_file,
+					   u32 *id);
 void iommufd_device_unbind(struct iommufd_device *idev);
 
 int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
Signature · 1 line
2.43.0
author maintainer
#8 lore raw

RFC PATCH v6 4/11 iommufd/viommu: Keep a reference to the KVM file

In reply to Aneesh Kumar K.V (Arm)

From: Nicolin Chen <[email protected]>

The TSM vDevice operations need access to the KVM associated with the device's vIOMMU. Save the device's KVM file in the iommufd_viommu when the vIOMMU is allocated, and take a file reference so it remains valid for the lifetime of the vIOMMU.

Release the reference when the vIOMMU is destroyed.

Based on an original patch by Shameer Kolothum <[email protected]>

[nicolinc: hold kvm's users_count]
[aneesh.kumar: Switch to use kvm_file]
---
 drivers/iommu/iommufd/viommu.c | 5 +++++
 include/linux/iommufd.h        | 1 +
 2 files changed, 6 insertions(+)
drivers/iommu/iommufd/viommu.c +5 -0
index 4081deda9b33..bf5d58d55939 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
  */
+#include <linux/file.h>
 #include "iommufd_private.h"
 
 void iommufd_viommu_destroy(struct iommufd_object *obj)
@@ -11,6 +12,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
 	if (viommu->ops && viommu->ops->destroy)
 		viommu->ops->destroy(viommu);
 	refcount_dec(&viommu->hwpt->common.obj.users);
+	if (viommu->kvm_file)
+		fput(viommu->kvm_file);
 	xa_destroy(&viommu->vdevs);
 }
 
@@ -76,6 +79,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	}
 
 	xa_init(&viommu->vdevs);
+	if (idev->kvm_file)
+		viommu->kvm_file = get_file(idev->kvm_file);
 	viommu->type = cmd->type;
 	viommu->ictx = ucmd->ictx;
 	viommu->hwpt = hwpt_paging;
include/linux/iommufd.h +1 -0
index 0a0bb4abfbd2..3267717f676d 100644
@@ -103,6 +103,7 @@ struct iommufd_viommu {
 	struct iommufd_ctx *ictx;
 	struct iommu_device *iommu_dev;
 	struct iommufd_hwpt_paging *hwpt;
+	struct file *kvm_file;
 
 	const struct iommufd_viommu_ops *ops;
 
Signature · 1 line
2.43.0
maintainer
#9 lore raw

Re: [RFC PATCH v6 04/11] iommufd/viommu: Keep a reference to the KVM file

Quoted 42 lines · Aneesh Kumar K.V (Arm)

From: Nicolin Chen <[email protected]>

The TSM vDevice operations need access to the KVM associated with the device's vIOMMU. Save the device's KVM file in the iommufd_viommu when the vIOMMU is allocated, and take a file reference so it remains valid for the lifetime of the vIOMMU.

Release the reference when the vIOMMU is destroyed.

Based on an original patch by Shameer Kolothum <[email protected]>

[nicolinc: hold kvm's users_count]
[aneesh.kumar: Switch to use kvm_file]
---
 drivers/iommu/iommufd/viommu.c | 5 +++++
 include/linux/iommufd.h        | 1 +
 2 files changed, 6 insertions(+)
drivers/iommu/iommufd/viommu.c +3 -0
index 4081deda9b33..bf5d58d55939 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
  */
+#include <linux/file.h>
 #include "iommufd_private.h"
 
 void iommufd_viommu_destroy(struct iommufd_object *obj)
@@ -11,6 +12,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
 	if (viommu->ops && viommu->ops->destroy)
 		viommu->ops->destroy(viommu);
 	refcount_dec(&viommu->hwpt->common.obj.users);
+	if (viommu->kvm_file)
+		fput(viommu->kvm_file);
 	xa_destroy(&viommu->vdevs);
 }
 

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 83

Quoted 5 lines
drivers/iommu/iommufd/viommu.c +2 -0
@@ -76,6 +79,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	}
 
 	xa_init(&viommu->vdevs);
+	if (idev->kvm_file)
+		viommu->kvm_file = get_file(idev->kvm_file);

Nice! This is needed for AMD IOMMU as well!

Minor nit. You have to add fput() in error path.

-Vasant

author maintainer
#10 lore raw

Re: [RFC PATCH v6 04/11] iommufd/viommu: Keep a reference to the KVM file

Quoted 45 lines · Vasant Hegde
Quoted 44 lines · Aneesh Kumar K.V (Arm)

From: Nicolin Chen <[email protected]>

The TSM vDevice operations need access to the KVM associated with the device's vIOMMU. Save the device's KVM file in the iommufd_viommu when the vIOMMU is allocated, and take a file reference so it remains valid for the lifetime of the vIOMMU.

Release the reference when the vIOMMU is destroyed.

Based on an original patch by Shameer Kolothum <[email protected]>

[nicolinc: hold kvm's users_count]
[aneesh.kumar: Switch to use kvm_file]
---
 drivers/iommu/iommufd/viommu.c | 5 +++++
 include/linux/iommufd.h        | 1 +
 2 files changed, 6 insertions(+)
drivers/iommu/iommufd/viommu.c +3 -0
index 4081deda9b33..bf5d58d55939 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
  */
+#include <linux/file.h>
 #include "iommufd_private.h"
 
 void iommufd_viommu_destroy(struct iommufd_object *obj)
@@ -11,6 +12,8 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
 	if (viommu->ops && viommu->ops->destroy)
 		viommu->ops->destroy(viommu);
 	refcount_dec(&viommu->hwpt->common.obj.users);
+	if (viommu->kvm_file)
+		fput(viommu->kvm_file);
 	xa_destroy(&viommu->vdevs);
 }
 
@@ -76,6 +79,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	}
 
Quoted 7 lines

drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 83

Quoted 3 lines
drivers/iommu/iommufd/viommu.c +2 -0
 	xa_init(&viommu->vdevs);
+	if (idev->kvm_file)
+		viommu->kvm_file = get_file(idev->kvm_file);

Nice! This is needed for AMD IOMMU as well!

Minor nit. You have to add fput() in error path.

The error path is handled by iommufd_viommu_destroy(). iommufd_fops_ioctl() calls either destroy or finalize, as required.

-aneesh

author maintainer
#11 lore raw

RFC PATCH v6 5/11 iommu: Add a helper to validate a vIOMMU parent

In reply to Aneesh Kumar K.V (Arm)

Let a vIOMMU implementation ask the physical IOMMU driver whether a device and nesting parent are compatible. Return EOPNOTSUPP when the driver does not supply the validation callback.

---
 drivers/iommu/iommu.c | 11 +++++++++++
 include/linux/iommu.h |  8 ++++++++
 2 files changed, 19 insertions(+)
drivers/iommu/iommu.c +11 -0
index e8f13dcebbde..04cd5a686e18 100644
@@ -47,6 +47,17 @@ static unsigned int iommu_def_domain_type __read_mostly;
 static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
 static u32 iommu_cmd_line __read_mostly;
 
+int iommu_viommu_validate_parent(struct device *dev,
+		enum iommu_viommu_type type, struct iommu_domain *parent_domain)
+{
+	const struct iommu_ops *ops = dev_iommu_ops(dev);
+
+	if (!ops->viommu_validate_parent)
+		return -EOPNOTSUPP;
+	return ops->viommu_validate_parent(dev, type, parent_domain);
+}
+EXPORT_SYMBOL_GPL(iommu_viommu_validate_parent);
+
 /* Tags used with xa_tag_pointer() in group->pasid_array */
 enum { IOMMU_PASID_ARRAY_DOMAIN = 0, IOMMU_PASID_ARRAY_HANDLE = 1 };
 
include/linux/iommu.h +8 -0
index d20aa6f6863a..ee016a7c7a41 100644
@@ -435,6 +435,9 @@ struct iommu_user_data {
 	size_t len;
 };
 
+int iommu_viommu_validate_parent(struct device *dev,
+		enum iommu_viommu_type type, struct iommu_domain *parent_domain);
+
 /**
  * struct iommu_user_data_array - iommu driver specific user space data array
  * @type: The data type of all the entries in the user buffer array
@@ -679,6 +682,8 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,
  *               resources shared/passed to user space IOMMU instance. Associate
  *               it with a nesting @parent_domain. It is required for driver to
  *               set @viommu->ops pointing to its own viommu_ops
+ * @viommu_validate_parent: Validate that @parent_domain is compatible with
+ *                          @dev for @viommu_type.
  * @owner: Driver module providing these ops
  * @identity_domain: An always available, always attachable identity
  *                   translation.
@@ -734,6 +739,9 @@ struct iommu_ops {
 	int (*viommu_init)(struct iommufd_viommu *viommu,
 			   struct iommu_domain *parent_domain,
 			   const struct iommu_user_data *user_data);
+	int (*viommu_validate_parent)(struct device *dev,
+				      enum iommu_viommu_type viommu_type,
+				      struct iommu_domain *parent_domain);
 
 	const struct iommu_domain_ops *default_domain_ops;
 	struct module *owner;
Signature · 1 line
2.43.0
maintainer
#12 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

drivers/iommu/iommu.c static unsigned int iommu_def_domain_type __read_mostly; · line 52

Quoted 4 lines

[ ... 18 lines skipped ... ]

drivers/iommu/iommu.c +3 -0
+int iommu_viommu_validate_parent(struct device *dev,
+		enum iommu_viommu_type type, struct iommu_domain *parent_domain)
+{

I didn't see anything that calls this? I can't imagine what it would be needed for?

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

Signature · 1 line
Jason
author maintainer
#13 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 8 lines · Jason Gunthorpe

drivers/iommu/iommu.c static unsigned int iommu_def_domain_type __read_mostly; · line 52

Quoted 4 lines

[ ... 18 lines skipped ... ]

drivers/iommu/iommu.c +3 -0
+int iommu_viommu_validate_parent(struct device *dev,
+		enum iommu_viommu_type type, struct iommu_domain *parent_domain)
+{

I didn't see anything that calls this? I can't imagine what it would be needed for?

That call is part of the TSM backend driver. The full backend driver is available here:

https://git.gitlab.arm.com/linux-arm/linux-cca/-/commits/cca/scratch/vdevice-tdi?ref_type=heads

int cca_viommu_init(struct iommufd_viommu *viommu, struct device *dev,
struct iommu_domain *parent,
const struct iommu_user_data *user_data)

{
	struct arm_smmu_realm_params params;
	struct cca_viommu *cca = to_cca_viommu(viommu);
	struct cca_psmmu *psmmu;
	struct kvm *kvm;
	int ret;

	(void)user_data;

	if (viommu->type != IOMMU_VIOMMU_TYPE_ARM_REALM_SMMUV3)
		return -EOPNOTSUPP;
	if (!viommu->kvm_file)
		return -EINVAL;
	kvm = viommu->kvm_file->private_data;
	ret = kvm_realm_ensure_created(kvm);
	if (ret)
		return ret;

	ret = iommu_viommu_validate_parent(dev, viommu->type, parent);
	if (ret)
		return ret;
	ret = iommu_viommu_get_params(dev, viommu->type, &params,
				      sizeof(params));
	if (ret)
		return ret;

	psmmu = cca_psmmu_get(&params);
	if (IS_ERR(psmmu))
		return PTR_ERR(psmmu);
	cca->psmmu = psmmu;
	viommu->ops = &cca_viommu_ops;
	return 0;
}
Quoted 4 lines

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

How would we assign an untrusted device? I currently follow these steps:

1. Create an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT.
2. Allocate a vIOMMU with viommu.hwpt_id set to that hwpt_id.
3. Allocate a vdevice with alloc_vdev.viommu_id set to that viommu_id.
4. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id.

I use the same iommufd for both trusted and untrusted devices because the guest controls that transition. While operating in untrusted mode, I use the iommufd to insert DMA mappings via IOMMU_IOAS_MAP.

-aneesh

maintainer
#14 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 9 lines · Aneesh Kumar K.V
Quoted 2 lines

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

How would we assign an untrusted device? I currently follow these steps:

1. Create an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT.
2. Allocate a vIOMMU with viommu.hwpt_id set to that hwpt_id.
3. Allocate a vdevice with alloc_vdev.viommu_id set to that viommu_id.
4. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id.

The vmiommu.hwpt_id should be 0.

1. Create a a HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT
2. VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id to establish the T=0 identity S2, no T=0 vSMMU
3. Create a VIOMMU with no hwpt_id and the RMM's type. This triggers RMM to create the the T=1 vSMMU inside the realm
4. Allocate a vdevice on the viommu_id. This triggers RMM to create the VDEV inside the realm
5. Setup guest ACPI tables/etc to point at the RMM's T=1 vsmmu.

Now both the T=1 VSUMM and T=0 fixed translation are setup.

If we loose our minds someday and want to concurrently have a T=0&1 VSMMU then the the first two steps become:

1. Create a a HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT
2. Create a VIOMMU with no hwpt_id and IOMMU_VIOMMU_TYPE_ARM_SMMUV3
3. Create a vdevice on #2, we'd need to fixup iommufd to support two vdevices for the same device.
4. Create a nested child HWPT IOMMU_HWPT_DATA_ARM_SMMUV3 from the T=0 vSTE
5. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT on #3

ie there would be two viommu objects inside iommufd.

Quoted 3 lines

I use the same iommufd for both trusted and untrusted devices because the guest controls that transition. While operating in untrusted mode, I use the iommufd to insert DMA mappings via IOMMU_IOAS_MAP.

Sure, that's not an issue, just don't create the *TSM VIOMMU* with a hwpt.

TSM viommu driver just asserts the parent is NULL, since it doesn't use a S2.

Jason

author maintainer
#15 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 22 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V
Quoted 2 lines

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

How would we assign an untrusted device? I currently follow these steps:

1. Create an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT.
2. Allocate a vIOMMU with viommu.hwpt_id set to that hwpt_id.
3. Allocate a vdevice with alloc_vdev.viommu_id set to that viommu_id.
4. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id.

The vmiommu.hwpt_id should be 0.

1. Create a a HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT
2. VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id to establish the T=0 identity S2, no T=0 vSMMU
3. Create a VIOMMU with no hwpt_id and the RMM's type. This triggers RMM to create the the T=1 vSMMU inside the realm
4. Allocate a vdevice on the viommu_id. This triggers RMM to create the VDEV inside the realm
5. Setup guest ACPI tables/etc to point at the RMM's T=1 vsmmu.

Quoted 2 lines

Now both the T=1 VSUMM and T=0 fixed translation are setup.

ok so iommufd_viommu_alloc_ioctl() will do this based on type.

+ if (iommufd_viommu_type_requires_hwpt(cmd->type)) {
+ hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
+ if (IS_ERR(hwpt_paging)) {

-aneesh

maintainer
#16 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 29 lines · Aneesh Kumar K.V
Quoted 22 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V
Quoted 2 lines

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

How would we assign an untrusted device? I currently follow these steps:

1. Create an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT.
2. Allocate a vIOMMU with viommu.hwpt_id set to that hwpt_id.
3. Allocate a vdevice with alloc_vdev.viommu_id set to that viommu_id.
4. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id.

The vmiommu.hwpt_id should be 0.

1. Create a a HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT
2. VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id to establish the T=0 identity S2, no T=0 vSMMU
3. Create a VIOMMU with no hwpt_id and the RMM's type. This triggers RMM to create the the T=1 vSMMU inside the realm
4. Allocate a vdevice on the viommu_id. This triggers RMM to create the VDEV inside the realm
5. Setup guest ACPI tables/etc to point at the RMM's T=1 vsmmu.

Quoted 2 lines

Now both the T=1 VSUMM and T=0 fixed translation are setup.

ok so iommufd_viommu_alloc_ioctl() will do this based on type.

Review

Quoted 3 lines
(quoted diff) +3 -0
+	if (iommufd_viommu_type_requires_hwpt(cmd->type)) {
+		hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
+		if (IS_ERR(hwpt_paging)) {

The core code should not decode type, that's always a hack..

The ideal thing is to order things so we get a viommu_ops before trying to get the hwpt, then use a flag in the ops

Jason

author maintainer
#17 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 30 lines · Jason Gunthorpe
Quoted 29 lines · Aneesh Kumar K.V
Quoted 22 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V
Quoted 2 lines

The TSM viommu should use a NULL parent domain, it doesn't have an iommufd managed S2.

How would we assign an untrusted device? I currently follow these steps:

1. Create an HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT.
2. Allocate a vIOMMU with viommu.hwpt_id set to that hwpt_id.
3. Allocate a vdevice with alloc_vdev.viommu_id set to that viommu_id.
4. Use VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id.

The vmiommu.hwpt_id should be 0.

1. Create a a HWPT with IOMMU_HWPT_ALLOC_NEST_PARENT
2. VFIO_DEVICE_ATTACH_IOMMUFD_PT with the hwpt_id to establish the T=0 identity S2, no T=0 vSMMU
3. Create a VIOMMU with no hwpt_id and the RMM's type. This triggers RMM to create the the T=1 vSMMU inside the realm
4. Allocate a vdevice on the viommu_id. This triggers RMM to create the VDEV inside the realm
5. Setup guest ACPI tables/etc to point at the RMM's T=1 vsmmu.

Quoted 2 lines

Now both the T=1 VSUMM and T=0 fixed translation are setup.

ok so iommufd_viommu_alloc_ioctl() will do this based on type.

Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+	if (iommufd_viommu_type_requires_hwpt(cmd->type)) {
+		hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
+		if (IS_ERR(hwpt_paging)) {

The core code should not decode type, that's always a hack..

The ideal thing is to order things so we get a viommu_ops before trying to get the hwpt, then use a flag in the ops

Does that mean the SMMU driver will return viommu_ops before iommu_ops->viommu_init() is called? If so, should viommu_init() be moved into viommu_ops?

Would the core then only need to do something like:

	rc = tsm_viommu_get_info(idev->dev, cmd->type, &info);
	if (not_using_tsm) // All smmu driver will now support get_viommu_info.
		rc = iommu_ops->get_viommu_info(idev->dev, cmd->type, &info);

and then call info->ops->viommu_init()?

-aneesh

maintainer
#18 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 3 lines · Aneesh Kumar K.V

Does that mean the SMMU driver will return viommu_ops before iommu_ops->viommu_init() is called? If so, should viommu_init() be moved into viommu_ops?

That's probably the cleanest arrangement, yeah. Then the TSM ops are just the same 'get_viommu_ops' call under TSM and it is easy to put in a flag 'must have null hwpt' that goes at the right point.

drivers/iommu/amd/iommu.c +1 -2
index 4dc306a4b5c620..269f67346ac760 100644
@@ -3214,8 +3214,7 @@ const struct iommu_ops amd_iommu_ops = {
 	.is_attach_deferred = amd_iommu_is_attach_deferred,
 	.def_domain_type = amd_iommu_def_domain_type,
 	.page_response = amd_iommu_page_response,
-	.get_viommu_size = amd_iommufd_get_viommu_size,
-	.viommu_init = amd_iommufd_viommu_init,
+	.get_viommu_ops = amd_iommufd_get_viommu_ops,
 };
 
 #ifdef CONFIG_IRQ_REMAP
drivers/iommu/amd/iommufd.c +11 -1
index 52300b867c1f20..e86890a59865db 100644
@@ -32,11 +32,19 @@ void *amd_iommufd_hw_info(struct device *dev, u32 *length, enum iommu_hw_info_ty
 	return hwinfo;
 }
 
-size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viommu_type)
+static size_t amd_iommufd_get_viommu_size(struct device *dev,
+					  enum iommu_viommu_type viommu_type)
 {
 	return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core);
 }
 
+const struct iommufd_viommu_ops *
+amd_iommufd_get_viommu_ops(struct device *dev,
+			   enum iommu_viommu_type viommu_type)
+{
+	return &amd_viommu_ops;
+}
+
 int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent,
 			    const struct iommu_user_data *user_data)
 {
@@ -73,5 +81,7 @@ static void amd_iommufd_viommu_destroy(struct iommufd_viommu *viommu)
  * struct iommufd_viommu_ops - vIOMMU specific operations
  */
 static const struct iommufd_viommu_ops amd_viommu_ops = {
+	.get_viommu_size = amd_iommufd_get_viommu_size,
+	.viommu_init = amd_iommufd_viommu_init,
 	.destroy = amd_iommufd_viommu_destroy,
 };
drivers/iommu/amd/iommufd.h +4 -5
index 62e9e1bebfbe59..6729f8f37506b7 100644
@@ -8,13 +8,12 @@
 
 #if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
 void *amd_iommufd_hw_info(struct device *dev, u32 *length, enum iommu_hw_info_type *type);
-size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viommu_type);
-int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent,
-			    const struct iommu_user_data *user_data);
+const struct iommufd_viommu_ops *
+amd_iommufd_get_viommu_ops(struct device *dev,
+			   enum iommu_viommu_type viommu_type);
 #else
 #define amd_iommufd_hw_info NULL
-#define amd_iommufd_viommu_init NULL
-#define amd_iommufd_get_viommu_size NULL
+#define amd_iommufd_get_viommu_ops NULL
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_IOMMUFD_H */
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +17 -9
index ab1078a97d801e..b848219f91c984 100644
@@ -532,20 +532,28 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 	return ret;
 }
 
+static size_t arm_vsmmu_get_size(struct device *dev,
+				 enum iommu_viommu_type viommu_type)
+{
+	return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
+}
+
 static const struct iommufd_viommu_ops arm_vsmmu_ops = {
+	.get_viommu_size = arm_vsmmu_get_size,
+	.viommu_init = arm_vsmmu_init,
 	.alloc_domain_nested = arm_vsmmu_alloc_domain_nested,
 	.cache_invalidate = arm_vsmmu_cache_invalidate,
 	.vdevice_init = arm_vsmmu_vdevice_init,
 };
 
-size_t arm_smmu_get_viommu_size(struct device *dev,
-				enum iommu_viommu_type viommu_type)
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type viommu_type)
 {
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
 	struct arm_smmu_device *smmu = master->smmu;
 
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
-		return 0;
+		return NULL;
 
 	/*
 	 * FORCE_SYNC is not set with FEAT_NESTING. Some study of the exact HW
@@ -553,7 +561,7 @@ size_t arm_smmu_get_viommu_size(struct device *dev,
 	 * any change to remove this.
 	 */
 	if (WARN_ON(smmu->options & ARM_SMMU_OPT_CMDQ_FORCE_SYNC))
-		return 0;
+		return NULL;
 
 	/*
 	 * Must support some way to prevent the VM from bypassing the cache
@@ -565,14 +573,14 @@ size_t arm_smmu_get_viommu_size(struct device *dev,
 	 */
 	if (!arm_smmu_master_canwbs(master) &&
 	    !(smmu->features & ARM_SMMU_FEAT_S2FWB))
-		return 0;
+		return NULL;
 
 	if (viommu_type == IOMMU_VIOMMU_TYPE_ARM_SMMUV3)
-		return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
+		return &arm_vsmmu_ops;
 
-	if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_size)
-		return 0;
-	return smmu->impl_ops->get_viommu_size(viommu_type);
+	if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_ops)
+		return NULL;
+	return smmu->impl_ops->get_viommu_ops(viommu_type);
 }
 
 int arm_vsmmu_init(struct iommufd_viommu *viommu,
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -4
index 5732f3ba0122d6..eaf7fb1f40453f 100644
@@ -4389,8 +4389,7 @@ static const struct iommu_ops arm_smmu_ops = {
 	.get_resv_regions	= arm_smmu_get_resv_regions,
 	.page_response		= arm_smmu_page_response,
 	.def_domain_type	= arm_smmu_def_domain_type,
-	.get_viommu_size	= arm_smmu_get_viommu_size,
-	.viommu_init		= arm_vsmmu_init,
+	.get_viommu_ops		= arm_smmu_get_viommu_ops,
 	.user_pasid_table	= 1,
 	.owner			= THIS_MODULE,
 	.default_domain_ops = &(const struct iommu_domain_ops) {
@@ -5489,8 +5488,8 @@ static struct arm_smmu_device *arm_smmu_impl_probe(struct arm_smmu_device *smmu)
 
 	ops = new_smmu->impl_ops;
 	if (ops) {
-		/* get_viommu_size and vsmmu_init ops must be paired */
-		if (WARN_ON(!ops->get_viommu_size != !ops->vsmmu_init)) {
+		/* get_viommu_ops and vsmmu_init ops must be paired */
+		if (WARN_ON(!ops->get_viommu_ops != !ops->vsmmu_init)) {
 			ret = -EINVAL;
 			goto err_remove;
 		}
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +6 -5
index 50f8321e979cef..6675f4232be075 100644
@@ -886,7 +886,8 @@ struct arm_smmu_impl_ops {
 	 */
 	void *(*hw_info)(struct arm_smmu_device *smmu, u32 *length,
 			 enum iommu_hw_info_type *type);
-	size_t (*get_viommu_size)(enum iommu_viommu_type viommu_type);
+	const struct iommufd_viommu_ops *(*get_viommu_ops)(
+		enum iommu_viommu_type viommu_type);
 	int (*vsmmu_init)(struct arm_vsmmu *vsmmu,
 			  const struct iommu_user_data *user_data);
 };
@@ -1260,8 +1261,8 @@ struct arm_vsmmu {
 #if IS_ENABLED(CONFIG_ARM_SMMU_V3_IOMMUFD)
 void *arm_smmu_hw_info(struct device *dev, u32 *length,
 		       enum iommu_hw_info_type *type);
-size_t arm_smmu_get_viommu_size(struct device *dev,
-				enum iommu_viommu_type viommu_type);
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type viommu_type);
 int arm_vsmmu_init(struct iommufd_viommu *viommu,
 		   struct iommu_domain *parent_domain,
 		   const struct iommu_user_data *user_data);
@@ -1276,9 +1277,9 @@ arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu, u32 flags,
 int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu,
 			       struct iommu_user_data_array *array);
 #else
-#define arm_smmu_get_viommu_size NULL
-#define arm_smmu_hw_info NULL
+#define arm_smmu_get_viommu_ops NULL
 #define arm_vsmmu_init NULL
+#define arm_smmu_hw_info NULL
 #define arm_vsmmu_alloc_domain_nested NULL
 #define arm_vsmmu_cache_invalidate NULL
 
drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +16 -5
index 6644075c1431ec..bcb5f3efdb190d 100644
@@ -892,11 +892,14 @@ static void *tegra241_cmdqv_hw_info(struct arm_smmu_device *smmu, u32 *length,
 	return info;
 }
 
-static size_t tegra241_cmdqv_get_vintf_size(enum iommu_viommu_type viommu_type)
+static const struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops;
+
+static const struct iommufd_viommu_ops *
+tegra241_cmdqv_get_viommu_ops(enum iommu_viommu_type viommu_type)
 {
 	if (viommu_type != IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV)
-		return 0;
-	return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core);
+		return NULL;
+	return &tegra241_cmdqv_viommu_ops;
 }
 
 static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = {
@@ -907,7 +910,7 @@ static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = {
 	.device_remove = tegra241_cmdqv_remove,
 	/* For user-space use */
 	.hw_info = tegra241_cmdqv_hw_info,
-	.get_viommu_size = tegra241_cmdqv_get_vintf_size,
+	.get_viommu_ops = tegra241_cmdqv_get_viommu_ops,
 	.vsmmu_init = tegra241_cmdqv_init_vintf_user,
 };
 
@@ -1289,7 +1292,15 @@ static int tegra241_vintf_init_vsid(struct iommufd_vdevice *vdev)
 	return 0;
 }
 
-static struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops = {
+static size_t tegra241_cmdqv_get_vintf_size(struct device *dev,
+					    enum iommu_viommu_type viommu_type)
+{
+	return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core);
+}
+
+static const struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops = {
+	.get_viommu_size = tegra241_cmdqv_get_vintf_size,
+	.viommu_init = arm_vsmmu_init,
 	.destroy = tegra241_cmdqv_destroy_vintf_user,
 	.alloc_domain_nested = arm_vsmmu_alloc_domain_nested,
 	/* Non-accelerated commands will be still handled by the kernel */
drivers/iommu/iommufd/selftest.c +18 -3
index 25387969ab7536..3c997de987247b 100644
@@ -767,7 +767,15 @@ static int mock_hw_queue_init_phys(struct iommufd_hw_queue *hw_queue, u32 index,
 	return rc;
 }
 
-static struct iommufd_viommu_ops mock_viommu_ops = {
+static size_t mock_get_viommu_size(struct device *dev,
+				   enum iommu_viommu_type viommu_type);
+static int mock_viommu_init(struct iommufd_viommu *viommu,
+			    struct iommu_domain *parent_domain,
+			    const struct iommu_user_data *user_data);
+
+static const struct iommufd_viommu_ops mock_viommu_ops = {
+	.get_viommu_size = mock_get_viommu_size,
+	.viommu_init = mock_viommu_init,
 	.destroy = mock_viommu_destroy,
 	.alloc_domain_nested = mock_viommu_alloc_domain_nested,
 	.cache_invalidate = mock_viommu_cache_invalidate,
@@ -783,6 +791,14 @@ static size_t mock_get_viommu_size(struct device *dev,
 	return VIOMMU_STRUCT_SIZE(struct mock_viommu, core);
 }
 
+static const struct iommufd_viommu_ops *
+mock_get_viommu_ops(struct device *dev, enum iommu_viommu_type viommu_type)
+{
+	if (viommu_type != IOMMU_VIOMMU_TYPE_SELFTEST)
+		return NULL;
+	return &mock_viommu_ops;
+}
+
 static int mock_viommu_init(struct iommufd_viommu *viommu,
 			    struct iommu_domain *parent_domain,
 			    const struct iommu_user_data *user_data)
@@ -854,8 +870,7 @@ static const struct iommu_ops mock_ops = {
 	.probe_device = mock_probe_device,
 	.page_response = mock_domain_page_response,
 	.user_pasid_table = true,
-	.get_viommu_size = mock_get_viommu_size,
-	.viommu_init = mock_viommu_init,
+	.get_viommu_ops = mock_get_viommu_ops,
 };
 
 static void mock_domain_free_nested(struct iommu_domain *domain)
drivers/iommu/iommufd/viommu.c +9 -5
index f7951057a1e564..25eef8aef3e84e 100644
@@ -26,7 +26,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	struct iommufd_viommu *viommu;
 	struct iommufd_device *idev;
 	struct iommu_device *iommu_dev;
-	const struct iommu_ops *ops;
+	const struct iommufd_viommu_ops *ops;
 	size_t viommu_size;
 	int rc;
 
@@ -42,8 +42,12 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 		rc = -EOPNOTSUPP;
 		goto out_put_idev;
 	}
-	ops = iommu_dev->ops;
-	if (!ops->get_viommu_size || !ops->viommu_init) {
+	if (!iommu_dev->ops->get_viommu_ops) {
+		rc = -EOPNOTSUPP;
+		goto out_put_idev;
+	}
+	ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
+	if (!ops || WARN_ON_ONCE(!ops->get_viommu_size || !ops->viommu_init)) {
 		rc = -EOPNOTSUPP;
 		goto out_put_idev;
 	}
@@ -100,8 +104,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	if (rc)
 		goto out_put_hwpt;
 
-	/* It is a driver bug that viommu->ops isn't filled */
-	if (WARN_ON_ONCE(!viommu->ops)) {
+	/* It is a driver bug if init selects a different ops table */
+	if (WARN_ON_ONCE(viommu->ops != ops)) {
 		rc = -EOPNOTSUPP;
 		goto out_put_hwpt;
 	}
include/linux/iommu.h +5 -15
index 05f7cee1375eed..b37b09f934c1dd 100644
@@ -46,6 +46,7 @@ struct iommu_dma_msi_cookie;
 struct iommu_fault_param;
 struct iommufd_ctx;
 struct iommufd_viommu;
+struct iommufd_viommu_ops;
 struct msi_desc;
 struct msi_msg;
 
@@ -669,16 +670,8 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,
  *		- IOMMU_DOMAIN_DMA: must use a dma domain
  *		- 0: use the default setting
  * @default_domain_ops: the default ops for domains
- * @get_viommu_size: Get the size of a driver-level vIOMMU structure for a given
- *                   @dev corresponding to @viommu_type. Driver should return 0
- *                   if vIOMMU isn't supported accordingly. It is required for
- *                   driver to use the VIOMMU_STRUCT_SIZE macro to sanitize the
- *                   driver-level vIOMMU structure related to the core one
- * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
- *               IOMMU instance @viommu->iommu_dev, as the set of virtualization
- *               resources shared/passed to user space IOMMU instance. Associate
- *               it with a nesting @parent_domain. It is required for driver to
- *               set @viommu->ops pointing to its own viommu_ops
+ * @get_viommu_ops: Return the vIOMMU operations supported by @dev for
+ *                  @viommu_type, or NULL if the type is unsupported
  * @owner: Driver module providing these ops
  * @identity_domain: An always available, always attachable identity
  *                   translation.
@@ -729,11 +722,8 @@ struct iommu_ops {
 
 	int (*def_domain_type)(struct device *dev);
 
-	size_t (*get_viommu_size)(struct device *dev,
-				  enum iommu_viommu_type viommu_type);
-	int (*viommu_init)(struct iommufd_viommu *viommu,
-			   struct iommu_domain *parent_domain,
-			   const struct iommu_user_data *user_data);
+	const struct iommufd_viommu_ops *(*get_viommu_ops)(
+		struct device *dev, enum iommu_viommu_type viommu_type);
 
 	const struct iommu_domain_ops *default_domain_ops;
 	struct module *owner;
include/linux/iommufd.h +15 -0
index 3087f5b2def848..a13fa3d447467a 100644
@@ -142,6 +142,16 @@ struct iommufd_hw_queue {
 
 /**
  * struct iommufd_viommu_ops - vIOMMU specific operations
+ * @get_viommu_size: Get the size of a driver-level vIOMMU structure for a given
+ *                   @dev corresponding to @viommu_type. Driver should return 0
+ *                   if vIOMMU isn't supported accordingly. It is required for
+ *                   driver to use the VIOMMU_STRUCT_SIZE macro to sanitize the
+ *                   driver-level vIOMMU structure related to the core one
+ * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
+ *               IOMMU instance @viommu->iommu_dev, as the set of virtualization
+ *               resources shared/passed to user space IOMMU instance. Associate
+ *               it with a nesting @parent_domain. It is required for driver to
+ *               set @viommu->ops pointing to its own viommu_ops
  * @destroy: Clean up all driver-specific parts of an iommufd_viommu. The memory
  *           of the vIOMMU will be free-ed by iommufd core after calling this op
  * @alloc_domain_nested: Allocate a IOMMU_DOMAIN_NESTED on a vIOMMU that holds a
@@ -184,6 +194,11 @@ struct iommufd_hw_queue {
  *                      does, it should set it to the @hw_queue->destroy pointer
  */
 struct iommufd_viommu_ops {
+	size_t (*get_viommu_size)(struct device *dev,
+				  enum iommu_viommu_type viommu_type);
+	int (*viommu_init)(struct iommufd_viommu *viommu,
+			   struct iommu_domain *parent_domain,
+			   const struct iommu_user_data *user_data);
 	void (*destroy)(struct iommufd_viommu *viommu);
 	struct iommu_domain *(*alloc_domain_nested)(
 		struct iommufd_viommu *viommu, u32 flags,
#19 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Hi Jason,

Quoted 10 lines · On Mon, 28 Sep 2026 13:17:38 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Aneesh Kumar K.V

Does that mean the SMMU driver will return viommu_ops before iommu_ops->viommu_init() is called? If so, should viommu_init() be moved into viommu_ops?

That's probably the cleanest arrangement, yeah. Then the TSM ops are just the same 'get_viommu_ops' call under TSM and it is easy to put in a flag 'must have null hwpt' that goes at the right point.

This should also work for the parentless hypervisor vIOMMU in my RFC:
https://lore.kernel.org/linux-iommu/[email protected]/T/#t

I will give below a try, I currently have below (can be avoided if get_viommu_ops() can simply return NULL for the hypervisor type):

size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viommu_type) {
+ /* AMD vIOMMUs require a nesting parent; hypervisor vIOMMUs
are parentless. */
+ if (viommu_type == IOMMU_VIOMMU_TYPE_HYPERVISOR)
+ return 0;
+

Quoted 443 lines
drivers/iommu/amd/iommu.c +1 -2
index 4dc306a4b5c620..269f67346ac760 100644
@@ -3214,8 +3214,7 @@ const struct iommu_ops amd_iommu_ops = {
 	.is_attach_deferred = amd_iommu_is_attach_deferred,
 	.def_domain_type = amd_iommu_def_domain_type,
 	.page_response = amd_iommu_page_response,
-	.get_viommu_size = amd_iommufd_get_viommu_size,
-	.viommu_init = amd_iommufd_viommu_init,
+	.get_viommu_ops = amd_iommufd_get_viommu_ops,
 };
 
 #ifdef CONFIG_IRQ_REMAP
drivers/iommu/amd/iommufd.c +0 -1
index 52300b867c1f20..e86890a59865db 100644
@@ -32,11 +32,19 @@ void *amd_iommufd_hw_info(struct device *dev, u32
 *length, enum iommu_hw_info_ty return hwinfo;
 }
 
-size_t amd_iommufd_get_viommu_size(struct device *dev, enum

iommu_viommu_type viommu_type) +static size_t
amd_iommufd_get_viommu_size(struct device *dev,

drivers/iommu/amd/iommufd.c +1 -0
+					  enum iommu_viommu_type

viommu_type) {

drivers/iommu/amd/iommufd.c +7 -0
 	return VIOMMU_STRUCT_SIZE(struct amd_iommu_viommu, core);
 }
 
+const struct iommufd_viommu_ops *
+amd_iommufd_get_viommu_ops(struct device *dev,
+			   enum iommu_viommu_type viommu_type)
+{
+	return &amd_viommu_ops;
+}
+
 int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct

iommu_domain *parent, const struct iommu_user_data *user_data)
{

drivers/iommu/amd/iommufd.c +2 -0
@@ -73,5 +81,7 @@ static void amd_iommufd_viommu_destroy(struct
 iommufd_viommu *viommu)
  * struct iommufd_viommu_ops - vIOMMU specific operations
  */
 static const struct iommufd_viommu_ops amd_viommu_ops = {
+	.get_viommu_size = amd_iommufd_get_viommu_size,
+	.viommu_init = amd_iommufd_viommu_init,
 	.destroy = amd_iommufd_viommu_destroy,

};

drivers/iommu/amd/iommufd.h +0 -0
index 62e9e1bebfbe59..6729f8f37506b7 100644
@@ -8,13 +8,12 @@
 
 #if IS_ENABLED(CONFIG_AMD_IOMMU_IOMMUFD)
 void *amd_iommufd_hw_info(struct device *dev, u32 *length, enum

iommu_hw_info_type *type); -size_t amd_iommufd_get_viommu_size(struct device *dev, enum iommu_viommu_type viommu_type); -int amd_iommufd_viommu_init(struct iommufd_viommu *viommu, struct iommu_domain *parent,

drivers/iommu/amd/iommufd.h +4 -3
-			    const struct iommu_user_data *user_data);
+const struct iommufd_viommu_ops *
+amd_iommufd_get_viommu_ops(struct device *dev,
+			   enum iommu_viommu_type viommu_type);
 #else
 #define amd_iommufd_hw_info NULL
-#define amd_iommufd_viommu_init NULL
-#define amd_iommufd_get_viommu_size NULL
+#define amd_iommufd_get_viommu_ops NULL
 #endif /* CONFIG_AMD_IOMMU_IOMMUFD */
 
 #endif /* AMD_IOMMUFD_H */
(quoted diff) +0 -0

b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c index ab1078a97d801e..b848219f91c984 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c @@ -532,20 +532,28 @@ int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, return ret; }

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +10 -2
+static size_t arm_vsmmu_get_size(struct device *dev,
+				 enum iommu_viommu_type viommu_type)
+{
+	return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
+}
+
 static const struct iommufd_viommu_ops arm_vsmmu_ops = {
+	.get_viommu_size = arm_vsmmu_get_size,
+	.viommu_init = arm_vsmmu_init,
 	.alloc_domain_nested = arm_vsmmu_alloc_domain_nested,
 	.cache_invalidate = arm_vsmmu_cache_invalidate,
 	.vdevice_init = arm_vsmmu_vdevice_init,
 };
 
-size_t arm_smmu_get_viommu_size(struct device *dev,
-				enum iommu_viommu_type viommu_type)
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type

viommu_type) {

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -1
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
 	struct arm_smmu_device *smmu = master->smmu;
 
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
-		return 0;
+		return NULL;
 
 	/*
 	 * FORCE_SYNC is not set with FEAT_NESTING. Some study of

the exact HW @@ -553,7 +561,7 @@ size_t
arm_smmu_get_viommu_size(struct device *dev,

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -1
 	 * any change to remove this.
 	 */
 	if (WARN_ON(smmu->options & ARM_SMMU_OPT_CMDQ_FORCE_SYNC))
-		return 0;
+		return NULL;
 
 	/*
 	 * Must support some way to prevent the VM from bypassing

the cache @@ -565,14 +573,14 @@ size_t
arm_smmu_get_viommu_size(struct device *dev, */

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +5 -5
 	if (!arm_smmu_master_canwbs(master) &&
 	    !(smmu->features & ARM_SMMU_FEAT_S2FWB))
-		return 0;
+		return NULL;
 
 	if (viommu_type == IOMMU_VIOMMU_TYPE_ARM_SMMUV3)
-		return VIOMMU_STRUCT_SIZE(struct arm_vsmmu, core);
+		return &arm_vsmmu_ops;
 
-	if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_size)
-		return 0;
-	return smmu->impl_ops->get_viommu_size(viommu_type);
+	if (!smmu->impl_ops || !smmu->impl_ops->get_viommu_ops)
+		return NULL;
+	return smmu->impl_ops->get_viommu_ops(viommu_type);
 }
 
 int arm_vsmmu_init(struct iommufd_viommu *viommu,
(quoted diff) +0 -0

b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c index 5732f3ba0122d6..eaf7fb1f40453f 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c @@ -4389,8 +4389,7 @@ static const struct iommu_ops arm_smmu_ops = {
.get_resv_regions = arm_smmu_get_resv_regions,
.page_response = arm_smmu_page_response,
.def_domain_type = arm_smmu_def_domain_type,

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -4
-	.get_viommu_size	= arm_smmu_get_viommu_size,
-	.viommu_init		= arm_vsmmu_init,
+	.get_viommu_ops		= arm_smmu_get_viommu_ops,
 	.user_pasid_table	= 1,
 	.owner			= THIS_MODULE,
 	.default_domain_ops = &(const struct iommu_domain_ops) {
@@ -5489,8 +5488,8 @@ static struct arm_smmu_device
 *arm_smmu_impl_probe(struct arm_smmu_device *smmu) 
 	ops = new_smmu->impl_ops;
 	if (ops) {
-		/* get_viommu_size and vsmmu_init ops must be paired
 */
-		if (WARN_ON(!ops->get_viommu_size !=
 !ops->vsmmu_init)) {
+		/* get_viommu_ops and vsmmu_init ops must be paired
 */
+		if (WARN_ON(!ops->get_viommu_ops !=

!ops->vsmmu_init)) { ret = -EINVAL;

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
 			goto err_remove;
 		}
(quoted diff) +0 -0

b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h index 50f8321e979cef..6675f4232be075 100644 --- a/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +++ b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h @@ -886,7 +886,8 @@ struct arm_smmu_impl_ops { */

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +0 -1
 	void *(*hw_info)(struct arm_smmu_device *smmu, u32 *length,
 			 enum iommu_hw_info_type *type);
-	size_t (*get_viommu_size)(enum iommu_viommu_type

viommu_type);

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +4 -2
+	const struct iommufd_viommu_ops *(*get_viommu_ops)(
+		enum iommu_viommu_type viommu_type);
 	int (*vsmmu_init)(struct arm_vsmmu *vsmmu,
 			  const struct iommu_user_data *user_data);
 };
@@ -1260,8 +1261,8 @@ struct arm_vsmmu {
 #if IS_ENABLED(CONFIG_ARM_SMMU_V3_IOMMUFD)
 void *arm_smmu_hw_info(struct device *dev, u32 *length,
 		       enum iommu_hw_info_type *type);
-size_t arm_smmu_get_viommu_size(struct device *dev,
-				enum iommu_viommu_type viommu_type);
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type
 viommu_type); int arm_vsmmu_init(struct iommufd_viommu *viommu,
 		   struct iommu_domain *parent_domain,
 		   const struct iommu_user_data *user_data);
@@ -1276,9 +1277,9 @@ arm_vsmmu_alloc_domain_nested(struct

iommufd_viommu *viommu, u32 flags, int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, struct iommu_user_data_array *array); #else

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +2 -2
-#define arm_smmu_get_viommu_size NULL
-#define arm_smmu_hw_info NULL
+#define arm_smmu_get_viommu_ops NULL
 #define arm_vsmmu_init NULL
+#define arm_smmu_hw_info NULL
 #define arm_vsmmu_alloc_domain_nested NULL
 #define arm_vsmmu_cache_invalidate NULL
(quoted diff) +0 -0

b/drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c index 6644075c1431ec..bcb5f3efdb190d 100644 --- a/drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +++ b/drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c @@ -892,11 +892,14 @@ static void *tegra241_cmdqv_hw_info(struct arm_smmu_device *smmu, u32 *length, return info; }

-static size_t tegra241_cmdqv_get_vintf_size(enum iommu_viommu_type viommu_type) +static const struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops; +

drivers/iommu/arm/arm-smmu-v3/tegra241-cmdqv.c +13 -4
+static const struct iommufd_viommu_ops *
+tegra241_cmdqv_get_viommu_ops(enum iommu_viommu_type viommu_type)
 {
 	if (viommu_type != IOMMU_VIOMMU_TYPE_TEGRA241_CMDQV)
-		return 0;
-	return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core);
+		return NULL;
+	return &tegra241_cmdqv_viommu_ops;
 }
 
 static struct arm_smmu_impl_ops tegra241_cmdqv_impl_ops = {
@@ -907,7 +910,7 @@ static struct arm_smmu_impl_ops
 tegra241_cmdqv_impl_ops = { .device_remove = tegra241_cmdqv_remove,
 	/* For user-space use */
 	.hw_info = tegra241_cmdqv_hw_info,
-	.get_viommu_size = tegra241_cmdqv_get_vintf_size,
+	.get_viommu_ops = tegra241_cmdqv_get_viommu_ops,
 	.vsmmu_init = tegra241_cmdqv_init_vintf_user,
 };
 
@@ -1289,7 +1292,15 @@ static int tegra241_vintf_init_vsid(struct
 iommufd_vdevice *vdev) return 0;
 }
 
-static struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops = {
+static size_t tegra241_cmdqv_get_vintf_size(struct device *dev,
+					    enum iommu_viommu_type
 viommu_type) +{
+	return VIOMMU_STRUCT_SIZE(struct tegra241_vintf, vsmmu.core);
+}
+
+static const struct iommufd_viommu_ops tegra241_cmdqv_viommu_ops = {
+	.get_viommu_size = tegra241_cmdqv_get_vintf_size,
+	.viommu_init = arm_vsmmu_init,
 	.destroy = tegra241_cmdqv_destroy_vintf_user,
 	.alloc_domain_nested = arm_vsmmu_alloc_domain_nested,
 	/* Non-accelerated commands will be still handled by the

kernel */ diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c index 25387969ab7536..3c997de987247b 100644 --- a/drivers/iommu/iommufd/selftest.c +++ b/drivers/iommu/iommufd/selftest.c @@ -767,7 +767,15 @@ static int mock_hw_queue_init_phys(struct iommufd_hw_queue *hw_queue, u32 index, return rc; }

drivers/iommu/iommufd/selftest.c +2 -1
-static struct iommufd_viommu_ops mock_viommu_ops = {
+static size_t mock_get_viommu_size(struct device *dev,
+				   enum iommu_viommu_type

viommu_type); +static int mock_viommu_init(struct iommufd_viommu *viommu,

drivers/iommu/iommufd/selftest.c +14 -2
+			    struct iommu_domain *parent_domain,
+			    const struct iommu_user_data *user_data);
+
+static const struct iommufd_viommu_ops mock_viommu_ops = {
+	.get_viommu_size = mock_get_viommu_size,
+	.viommu_init = mock_viommu_init,
 	.destroy = mock_viommu_destroy,
 	.alloc_domain_nested = mock_viommu_alloc_domain_nested,
 	.cache_invalidate = mock_viommu_cache_invalidate,
@@ -783,6 +791,14 @@ static size_t mock_get_viommu_size(struct device
 *dev, return VIOMMU_STRUCT_SIZE(struct mock_viommu, core);
 }
 
+static const struct iommufd_viommu_ops *
+mock_get_viommu_ops(struct device *dev, enum iommu_viommu_type
 viommu_type) +{
+	if (viommu_type != IOMMU_VIOMMU_TYPE_SELFTEST)
+		return NULL;
+	return &mock_viommu_ops;
+}
+
 static int mock_viommu_init(struct iommufd_viommu *viommu,
 			    struct iommu_domain *parent_domain,
 			    const struct iommu_user_data *user_data)
@@ -854,8 +870,7 @@ static const struct iommu_ops mock_ops = {
 	.probe_device = mock_probe_device,
 	.page_response = mock_domain_page_response,
 	.user_pasid_table = true,
-	.get_viommu_size = mock_get_viommu_size,
-	.viommu_init = mock_viommu_init,
+	.get_viommu_ops = mock_get_viommu_ops,
 };
 
 static void mock_domain_free_nested(struct iommu_domain *domain)
(quoted diff) +0 -0

b/drivers/iommu/iommufd/viommu.c index f7951057a1e564..25eef8aef3e84e 100644 --- a/drivers/iommu/iommufd/viommu.c
+++ b/drivers/iommu/iommufd/viommu.c

drivers/iommu/iommufd/viommu.c +9 -5
@@ -26,7 +26,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd
 *ucmd) struct iommufd_viommu *viommu;
 	struct iommufd_device *idev;
 	struct iommu_device *iommu_dev;
-	const struct iommu_ops *ops;
+	const struct iommufd_viommu_ops *ops;
 	size_t viommu_size;
 	int rc;
 
@@ -42,8 +42,12 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd
 *ucmd) rc = -EOPNOTSUPP;
 		goto out_put_idev;
 	}
-	ops = iommu_dev->ops;
-	if (!ops->get_viommu_size || !ops->viommu_init) {
+	if (!iommu_dev->ops->get_viommu_ops) {
+		rc = -EOPNOTSUPP;
+		goto out_put_idev;
+	}
+	ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
+	if (!ops || WARN_ON_ONCE(!ops->get_viommu_size ||
 !ops->viommu_init)) { rc = -EOPNOTSUPP;
 		goto out_put_idev;
 	}
@@ -100,8 +104,8 @@ int iommufd_viommu_alloc_ioctl(struct
 iommufd_ucmd *ucmd) if (rc)
 		goto out_put_hwpt;
 
-	/* It is a driver bug that viommu->ops isn't filled */
-	if (WARN_ON_ONCE(!viommu->ops)) {
+	/* It is a driver bug if init selects a different ops table
 */
+	if (WARN_ON_ONCE(viommu->ops != ops)) {
 		rc = -EOPNOTSUPP;
 		goto out_put_hwpt;
(quoted diff) +0 -0
 	}
include/linux/iommu.h +1 -1
index 05f7cee1375eed..b37b09f934c1dd 100644
@@ -46,6 +46,7 @@ struct iommu_dma_msi_cookie;
 struct iommu_fault_param;
 struct iommufd_ctx;
 struct iommufd_viommu;
+struct iommufd_viommu_ops;
 struct msi_desc;
 struct msi_msg;
 
@@ -669,16 +670,8 @@ __iommu_copy_struct_to_user(const struct
 iommu_user_data *dst_data,
  *		- IOMMU_DOMAIN_DMA: must use a dma domain
  *		- 0: use the default setting
  * @default_domain_ops: the default ops for domains
- * @get_viommu_size: Get the size of a driver-level vIOMMU structure

for a given
- * @dev corresponding to @viommu_type. Driver
should return 0
- * if vIOMMU isn't supported accordingly. It is
required for
- * driver to use the VIOMMU_STRUCT_SIZE macro to
sanitize the
- * driver-level vIOMMU structure related to the
core one
- * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
- * IOMMU instance @viommu->iommu_dev, as the set of
virtualization
- * resources shared/passed to user space IOMMU
instance. Associate
- * it with a nesting @parent_domain. It is required
for driver to
- * set @viommu->ops pointing to its own viommu_ops
+ * @get_viommu_ops: Return the vIOMMU operations supported by @dev for
+ * @viommu_type, or NULL if the type is unsupported
* @owner: Driver module providing these ops
* @identity_domain: An always available, always attachable identity
* translation.

include/linux/iommu.h +2 -5
@@ -729,11 +722,8 @@ struct iommu_ops {
 
 	int (*def_domain_type)(struct device *dev);
 
-	size_t (*get_viommu_size)(struct device *dev,
-				  enum iommu_viommu_type
 viommu_type);
-	int (*viommu_init)(struct iommufd_viommu *viommu,
-			   struct iommu_domain *parent_domain,
-			   const struct iommu_user_data *user_data);
+	const struct iommufd_viommu_ops *(*get_viommu_ops)(
+		struct device *dev, enum iommu_viommu_type
 viommu_type); 
 	const struct iommu_domain_ops *default_domain_ops;
include/linux/iommu.h +0 -0
 	struct module *owner;
include/linux/iommufd.h +1 -0
index 3087f5b2def848..a13fa3d447467a 100644
@@ -142,6 +142,16 @@ struct iommufd_hw_queue {
 
 /**
  * struct iommufd_viommu_ops - vIOMMU specific operations
+ * @get_viommu_size: Get the size of a driver-level vIOMMU structure

for a given
+ * @dev corresponding to @viommu_type. Driver
should return 0
+ * if vIOMMU isn't supported accordingly. It is
required for
+ * driver to use the VIOMMU_STRUCT_SIZE macro to
sanitize the
+ * driver-level vIOMMU structure related to the
core one
+ * @viommu_init: Init the driver-level struct of an iommufd_viommu on a physical
+ * IOMMU instance @viommu->iommu_dev, as the set of
virtualization
+ * resources shared/passed to user space IOMMU
instance. Associate
+ * it with a nesting @parent_domain. It is required
for driver to
+ * set @viommu->ops pointing to its own viommu_ops
* @destroy: Clean up all driver-specific parts of an
iommufd_viommu. The memory
* of the vIOMMU will be free-ed by iommufd core after
calling this op
* @alloc_domain_nested: Allocate a IOMMU_DOMAIN_NESTED on a vIOMMU
that holds a @@ -184,6 +194,11 @@ struct iommufd_hw_queue {
* does, it should set it to the
@hw_queue->destroy pointer */
struct iommufd_viommu_ops {

include/linux/iommufd.h +2 -0
+	size_t (*get_viommu_size)(struct device *dev,
+				  enum iommu_viommu_type

viommu_type);

include/linux/iommufd.h +3 -0
+	int (*viommu_init)(struct iommufd_viommu *viommu,
+			   struct iommu_domain *parent_domain,
+			   const struct iommu_user_data *user_data);
 	void (*destroy)(struct iommufd_viommu *viommu);
 	struct iommu_domain *(*alloc_domain_nested)(
 		struct iommufd_viommu *viommu, u32 flags,
maintainer
#20 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 11 lines · Jacob Pan

Hi Jason,

Quoted 6 lines · On Mon, 28 Sep 2026 13:17:38 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Aneesh Kumar K.V

Does that mean the SMMU driver will return viommu_ops before iommu_ops->viommu_init() is called? If so, should viommu_init() be moved into viommu_ops?

Quoted 6 lines
Quoted 4 lines

That's probably the cleanest arrangement, yeah. Then the TSM ops are just the same 'get_viommu_ops' call under TSM and it is easy to put in a flag 'must have null hwpt' that goes at the right point.

This should also work for the parentless hypervisor vIOMMU in my RFC:
https://lore.kernel.org/linux-iommu/[email protected]/T/#t

Yes! That case is very similar, the hypervisor under Linux is a close cousin to RMM/TDX.

Quoted 2 lines

I will give below a try, I currently have below (can be avoided if get_viommu_ops() can simply return NULL for the hypervisor type):

The amd_iommufd_get_viommu_ops() must only permit its own type, but AMD doesn't have a type right now because they are merging their patches bit by bit..

ARM already returns NULL:

Quoted 8 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, · line 556

Quoted 8 lines
(quoted diff) +2 -0
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type

viommu_type) {

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -0
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
 	struct arm_smmu_device *smmu = master->smmu;
 
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
+		return NULL;

Though I wonder why would you check for IOMMU_VIOMMU_TYPE_HYPERVISOR in the amd driver?

Maybe hyperv should be a TSM? Maybe we should also route the get_viommu through the the "kvm" fd somehow?

Jason

#21 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Hi Jason,

Quoted 37 lines · On Mon, 28 Sep 2026 15:20:22 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 11 lines · Jacob Pan

Hi Jason,

Quoted 6 lines · On Mon, 28 Sep 2026 13:17:38 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Aneesh Kumar K.V

Does that mean the SMMU driver will return viommu_ops before iommu_ops->viommu_init() is called? If so, should viommu_init() be moved into viommu_ops?

Quoted 6 lines
Quoted 4 lines

That's probably the cleanest arrangement, yeah. Then the TSM ops are just the same 'get_viommu_ops' call under TSM and it is easy to put in a flag 'must have null hwpt' that goes at the right point.

This should also work for the parentless hypervisor vIOMMU in my
RFC: https://lore.kernel.org/linux-iommu/[email protected]/T/#t

Yes! That case is very similar, the hypervisor under Linux is a close cousin to RMM/TDX.

Quoted 2 lines

I will give below a try, I currently have below (can be avoided if get_viommu_ops() can simply return NULL for the hypervisor type):

The amd_iommufd_get_viommu_ops() must only permit its own type, but AMD doesn't have a type right now because they are merging their patches bit by bit..

ARM already returns NULL:

Quoted 6 lines
Quoted 6 lines
(quoted diff) +2 -0
+const struct iommufd_viommu_ops *
+arm_smmu_get_viommu_ops(struct device *dev, enum

iommu_viommu_type viommu_type) {

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +0 -0
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
 	struct arm_smmu_device *smmu = master->smmu;
Quoted 6 lines
Quoted 2 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, · line 556

Quoted 2 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -0
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
+		return NULL;  

Though I wonder why would you check for IOMMU_VIOMMU_TYPE_HYPERVISOR in the amd driver?

It is an early capability check. AMD vIOMMUs require a nesting parent, while IOMMU_VIOMMU_TYPE_HYPERVISOR is parentless. Returning zero prevents the core from allocating an AMD vIOMMU and calling viommu_init() with a NULL parent. As you said above, AMD doesn't have a allowed vIOMMU type yet.

Quoted 2 lines

Maybe hyperv should be a TSM? Maybe we should also route the get_viommu through the the "kvm" fd somehow?

Just to understand this better, are you suggesting to do away with hv-iommu-root driver in the context of external domain attach, instead let mshv (hyperv) register as a vIOMMU external provider?

maintainer
#22 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 11 lines · Jacob Pan
Quoted 11 lines
Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, · line 556

Quoted 7 lines

+arm_smmu_get_viommu_ops(struct device *dev, enum
iommu_viommu_type viommu_type) {

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -0
 	struct arm_smmu_master *master = dev_iommu_priv_get(dev);
 	struct arm_smmu_device *smmu = master->smmu;
 
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
+		return NULL;  

Though I wonder why would you check for IOMMU_VIOMMU_TYPE_HYPERVISOR in the amd driver?

Quoted 5 lines

It is an early capability check. AMD vIOMMUs require a nesting parent, while IOMMU_VIOMMU_TYPE_HYPERVISOR is parentless. Returning zero prevents the core from allocating an AMD vIOMMU and calling viommu_init() with a NULL parent. As you said above, AMD doesn't have a allowed vIOMMU type yet.

I'm a bit confused why a hyperv root parition has an AMD iommu inside it.. I thought that was what the hv-iommu-root driver was for?

But we should fix the AMD driver to have a type (ooops that is a troublesome bug!), and you should call add-on-top drivers like TSM before calling the real iommu driver..

Quoted 5 lines
Quoted 2 lines

Maybe hyperv should be a TSM? Maybe we should also route the get_viommu through the the "kvm" fd somehow?

Just to understand this better, are you suggesting to do away with hv-iommu-root driver in the context of external domain attach, instead let mshv (hyperv) register as a vIOMMU external provider?

Maybe? I don't quite understand what you've got here..

If the VM path doesn't interact with the iommut at all, which is how these CC cases are, then sure that might be a good idea.

I think you are going to have module dependency issues trying to get the mshv driver's info into the iommu driver, adding a "TSM" like driver would resolve that. ie mshv could just provide the viommu.

But, you do have a hv-iommu-root driver so it certainly can provide the viommu too, if that can be done cleanly.

I guess think about it ?

Jason

#23 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Hi Jason,

Quoted 12 lines · On Mon, 28 Sep 2026 20:03:28 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 11 lines · Jacob Pan
Quoted 11 lines
Quoted 7 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c int arm_vsmmu_cache_invalidate(struct iommufd_viommu *viommu, · line 556

Quoted 7 lines

+arm_smmu_get_viommu_ops(struct device *dev, enum iommu_viommu_type viommu_type) {
struct arm_smmu_master *master =
dev_iommu_priv_get(dev); struct arm_smmu_device *smmu = master->smmu;

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +1 -0
 	if (!(smmu->features & ARM_SMMU_FEAT_NESTING))
+		return NULL;    

Though I wonder why would you check for
IOMMU_VIOMMU_TYPE_HYPERVISOR in the amd driver?

Quoted 9 lines
Quoted 5 lines

It is an early capability check. AMD vIOMMUs require a nesting parent, while IOMMU_VIOMMU_TYPE_HYPERVISOR is parentless. Returning zero prevents the core from allocating an AMD vIOMMU and calling viommu_init() with a NULL parent. As you said above, AMD doesn't have a allowed vIOMMU type yet.

I'm a bit confused why a hyperv root parition has an AMD iommu inside it.. I thought that was what the hv-iommu-root driver was for?

You are right, AMD iommu driver should never load in hyperv root. I introduced this check in AMD driver for completeness as part of the generic iommufd change.
https://lore.kernel.org/linux-iommu/[email protected]/T/#u

Quoted 2 lines

But we should fix the AMD driver to have a type (ooops that is a troublesome bug!),

Agreed.

Quoted 3 lines

and you should call add-on-top drivers like TSM
before calling the real iommu driver..

I am not seeing a need for mshv to use the add-on driver like TSM, since MSHV is not layered on top of a physical IOMMU driver.

Also, we don't have a T=0 to T=1 transition, our intended external attach can be transitioned from a blocked domain, not from a paging domain to external.

Quoted 9 lines
Quoted 5 lines
Quoted 2 lines

Maybe hyperv should be a TSM? Maybe we should also route the get_viommu through the the "kvm" fd somehow?

Just to understand this better, are you suggesting to do away with hv-iommu-root driver in the context of external domain attach, instead let mshv (hyperv) register as a vIOMMU external provider?

Maybe? I don't quite understand what you've got here..
If the VM path doesn't interact with the iommut at all, which is how these CC cases are, then sure that might be a good idea.

I do think keeping the hv-iommu-root route is cleaner and logical. I misunderstood your comment about "hyperv should be a TSM".

Quoted 4 lines

I think you are going to have module dependency issues trying to get the mshv driver's info into the iommu driver, adding a "TSM" like driver would resolve that. ie mshv could just provide the viommu.

I introduced a similar registration interface in hv-common to solve the module dep issue. Will look into if I can leverage the vIOMMU helper here.

Quoted 4 lines

But, you do have a hv-iommu-root driver so it certainly can provide the viommu too, if that can be done cleanly.

I guess think about it ?

Yes, my RFC has hv-iommu-root provide the hypervisor vIOMMU directly, and I think the resulting layering is clean. Could you review that approach in the MSHV iommufd RFC thread?

Quoted 2 lines

Jason

maintainer
#24 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 3 lines · Jacob Pan

Also, we don't have a T=0 to T=1 transition, our intended external attach can be transitioned from a blocked domain, not from a paging domain to external.

You kind of do, external is close to T=1..

Quoted 7 lines
Quoted 4 lines

I think you are going to have module dependency issues trying to get the mshv driver's info into the iommu driver, adding a "TSM" like driver would resolve that. ie mshv could just provide the viommu.

I introduced a similar registration interface in hv-common to solve the module dep issue. Will look into if I can leverage the vIOMMU helper here.

That doesn't sound so nice..

If we have to have registrations it is better to have the viommu registration that Aneesh drafted than several schemes inside specific drivers..

Jason

#25 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Hi Jason,

Quoted 16 lines · On Tue, 29 Sep 2026 09:30:48 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Jacob Pan

Also, we don't have a T=0 to T=1 transition, our intended external attach can be transitioned from a blocked domain, not from a paging domain to external.

You kind of do, external is close to T=1..

Quoted 7 lines
Quoted 4 lines

I think you are going to have module dependency issues trying to get the mshv driver's info into the iommu driver, adding a "TSM" like driver would resolve that. ie mshv could just provide the viommu.

I introduced a similar registration interface in hv-common to solve the module dep issue. Will look into if I can leverage the vIOMMU helper here.

Quoted 5 lines

That doesn't sound so nice..

If we have to have registrations it is better to have the viommu registration that Aneesh drafted than several schemes inside specific drivers..

Agreed. Now that the commonality with the TSM provider path is clear, I will adapt the MSHV support to use the generic vIOMMU provider registration.

maintainer
#26 lore raw

Re: [RFC PATCH v6 05/11] iommu: Add a helper to validate a vIOMMU parent

Quoted 21 lines · Jacob Pan

Hi Jason,

Quoted 16 lines · On Tue, 29 Sep 2026 09:30:48 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Jacob Pan

Also, we don't have a T=0 to T=1 transition, our intended external attach can be transitioned from a blocked domain, not from a paging domain to external.

You kind of do, external is close to T=1..

Quoted 7 lines
Quoted 4 lines

I think you are going to have module dependency issues trying to get the mshv driver's info into the iommu driver, adding a "TSM" like driver would resolve that. ie mshv could just provide the viommu.

I introduced a similar registration interface in hv-common to solve the module dep issue. Will look into if I can leverage the vIOMMU helper here.

Quoted 8 lines
Quoted 5 lines

That doesn't sound so nice..

If we have to have registrations it is better to have the viommu registration that Aneesh drafted than several schemes inside specific drivers..

Agreed. Now that the commonality with the TSM provider path is clear, I will adapt the MSHV support to use the generic vIOMMU provider registration.

Ah, I so I suggested he remove it as it was just for TSM.. We'd have to ask it to come back

I still prefer the way TSM is shaping up because it solves all these locking and matching issues nicely. I think you would have the same general locking problems with a provider (regardless where you put it) too..

Are you sure it can't be a TSM?

Jason

author maintainer
#27 lore raw

RFC PATCH v6 6/11 iommu: Add a helper to query vIOMMU hardware parameters

In reply to Aneesh Kumar K.V (Arm)

Expose a type-specific kernel parameter query through the physical IOMMU driver. This lets an external vIOMMU provider obtain hardware parameters without depending on private driver structures.

---
 drivers/iommu/iommu.c | 11 +++++++++++
 include/linux/iommu.h |  7 +++++++
 2 files changed, 18 insertions(+)
drivers/iommu/iommu.c +11 -0
index 04cd5a686e18..70ca10380742 100644
@@ -58,6 +58,17 @@ int iommu_viommu_validate_parent(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(iommu_viommu_validate_parent);
 
+int iommu_viommu_get_params(struct device *dev,
+		enum iommu_viommu_type type, void *params, size_t params_size)
+{
+	const struct iommu_ops *ops = dev_iommu_ops(dev);
+
+	if (!ops->viommu_get_params)
+		return -EOPNOTSUPP;
+	return ops->viommu_get_params(dev, type, params, params_size);
+}
+EXPORT_SYMBOL_GPL(iommu_viommu_get_params);
+
 /* Tags used with xa_tag_pointer() in group->pasid_array */
 enum { IOMMU_PASID_ARRAY_DOMAIN = 0, IOMMU_PASID_ARRAY_HANDLE = 1 };
 
include/linux/iommu.h +7 -0
index ee016a7c7a41..c006478c4b04 100644
@@ -437,6 +437,8 @@ struct iommu_user_data {
 
 int iommu_viommu_validate_parent(struct device *dev,
 		enum iommu_viommu_type type, struct iommu_domain *parent_domain);
+int iommu_viommu_get_params(struct device *dev,
+		enum iommu_viommu_type type, void *params, size_t params_size);
 
 /**
  * struct iommu_user_data_array - iommu driver specific user space data array
@@ -684,6 +686,8 @@ __iommu_copy_struct_to_user(const struct iommu_user_data *dst_data,
  *               set @viommu->ops pointing to its own viommu_ops
  * @viommu_validate_parent: Validate that @parent_domain is compatible with
  *                          @dev for @viommu_type.
+ * @viommu_get_params: Fill a kernel buffer with parameters for @viommu_type.
+ *                     The buffer format is defined by that vIOMMU type.
  * @owner: Driver module providing these ops
  * @identity_domain: An always available, always attachable identity
  *                   translation.
@@ -742,6 +746,9 @@ struct iommu_ops {
 	int (*viommu_validate_parent)(struct device *dev,
 				      enum iommu_viommu_type viommu_type,
 				      struct iommu_domain *parent_domain);
+	int (*viommu_get_params)(struct device *dev,
+				 enum iommu_viommu_type viommu_type,
+				 void *params, size_t params_size);
 
 	const struct iommu_domain_ops *default_domain_ops;
 	struct module *owner;
Signature · 1 line
2.43.0
maintainer
#28 lore raw

Re: [RFC PATCH v6 06/11] iommu: Add a helper to query vIOMMU hardware parameters

drivers/iommu/iommu.c int iommu_viommu_validate_parent(struct device *dev, · line 63

Quoted 4 lines

[ ... 18 lines skipped ... ]

drivers/iommu/iommu.c +3 -0
+int iommu_viommu_get_params(struct device *dev,
+		enum iommu_viommu_type type, void *params, size_t params_size)
+{

I imagined this would be a direct function call from TSM to iommu driver? Is there a reason to do it this way? I think a TSM -> iommu module dependency is OK

Signature · 1 line
Jason
author maintainer
#29 lore raw

Re: [RFC PATCH v6 06/11] iommu: Add a helper to query vIOMMU hardware parameters

Quoted 9 lines · Jason Gunthorpe

drivers/iommu/iommu.c int iommu_viommu_validate_parent(struct device *dev, · line 63

Quoted 4 lines

[ ... 18 lines skipped ... ]

drivers/iommu/iommu.c +3 -0
+int iommu_viommu_get_params(struct device *dev,
+		enum iommu_viommu_type type, void *params, size_t params_size)
+{

I imagined this would be a direct function call from TSM to iommu driver? Is there a reason to do it this way? I think a TSM -> iommu module dependency is OK

Do you mean that the TSM backend should call arm_smmu_get_realm_params() directly? I was trying to avoid that dependency. TSM depends only on iommufd, not on the SMMU driver. Making this part of iommu_ops also simplifies supporting different get_params implementations for different hardware IOMMUs on the same architecture, if needed.

-aneesh

maintainer
#30 lore raw

Re: [RFC PATCH v6 06/11] iommu: Add a helper to query vIOMMU hardware parameters

Quoted 12 lines · Aneesh Kumar K.V
Quoted 9 lines · Jason Gunthorpe

drivers/iommu/iommu.c int iommu_viommu_validate_parent(struct device *dev, · line 63

Quoted 4 lines

[ ... 18 lines skipped ... ]

drivers/iommu/iommu.c +3 -0
+int iommu_viommu_get_params(struct device *dev,
+		enum iommu_viommu_type type, void *params, size_t params_size)
+{

I imagined this would be a direct function call from TSM to iommu driver? Is there a reason to do it this way? I think a TSM -> iommu module dependency is OK

Quoted 2 lines

Do you mean that the TSM backend should call arm_smmu_get_realm_params() directly?

Yes

Quoted 2 lines

I was trying to avoid that dependency. TSM depends only on iommufd, not on the SMMU driver.

I'm not sure there is a good reason to. RMM systems will always have a SMMU.

Also TSM should only depend on iommu_driver if at all possible.

It is another reason to not like this because it pulls in a full iommufd.ko dependency...

Quoted 3 lines

Making this part of iommu_ops also simplifies supporting different get_params implementations for different hardware IOMMUs on the same architecture, if needed.

If that happens the TSM needs unique coding for all of them, and then the direct link might be a bit of overhead.. But given people are not making new iommus routinely (for good reason!) I would prefer to kick that can down the road..

Jason

author maintainer
#31 lore raw

RFC PATCH v6 7/11 coco: tsm: Expose active-user lifetime references

In reply to Aneesh Kumar K.V (Arm)

Add separate active-user references for consumers that require both the TSM device and its PCI/TSM resources.

Initialize the users count to one when allocating the TSM device. This initial reference represents the registration and keeps the PCI/TSM resources registered until tsm_unregister() drops it.

tsm_get() takes both an active-user reference and a device reference. tsm_put() drops the active-user reference first, allowing the last active user to tear down PCI/TSM resources while struct tsm_dev and the driver operations remain valid, and then drops the device reference.

tsm_unregister() drops the initial registration reference and unregisters the device. Existing active users retain the PCI/TSM resources, while their device references keep struct tsm_dev alive. The final active-user reference tears down the PCI/TSM resources, independently of the final device reference releasing struct tsm_dev.

Restructure PCI/TSM registration error handling so tsm_register() removes the device and lets its scoped device reference perform the final release.

---
 drivers/virt/coco/tsm-core.c | 55 ++++++++++++++++++++++++++++++------
 include/linux/tsm.h          | 13 +++++++++
 2 files changed, 59 insertions(+), 9 deletions(-)
drivers/virt/coco/tsm-core.c +46 -9
index 0843b77c6549..90d304d55b59 100644
@@ -57,6 +57,39 @@ static const struct class tsm_class = {
 	.dev_groups	= tsm_pci_groups,
 };
 static DEFINE_IDA(tsm_ida);
+static void tsm_put_active(struct tsm_dev *tsm_dev)
+{
+	if (!refcount_dec_and_test(&tsm_dev->users))
+		return;
+	if (tsm_dev->pci_ops)
+		pci_tsm_unregister(tsm_dev);
+}
+
+/**
+ * tsm_get() - Take an active reference to a TSM
+ * @tsm_dev: registered TSM or TSM already held by an active reference
+ *
+ * Keeps both the device and its PCI/TSM resources alive.
+ */
+void tsm_get(struct tsm_dev *tsm_dev)
+{
+	get_device(&tsm_dev->dev);
+	refcount_inc(&tsm_dev->users);
+}
+EXPORT_SYMBOL_GPL(tsm_get);
+
+/**
+ * tsm_put() - Release an active TSM reference
+ * @tsm_dev: TSM acquired with tsm_get()
+ *
+ * The last active reference tears down PCI/TSM resources after unregister.
+ */
+void tsm_put(struct tsm_dev *tsm_dev)
+{
+	tsm_put_active(tsm_dev);
+	put_device(&tsm_dev->dev);
+}
+EXPORT_SYMBOL_GPL(tsm_put);
 
 static int match_id(struct device *dev, const void *data)
 {
@@ -90,6 +123,7 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
 		return ERR_PTR(id);
 
 	tsm_dev->id = id;
+	refcount_set(&tsm_dev->users, 1);
 	dev = &tsm_dev->dev;
 	dev->parent = parent;
 	dev->class = &tsm_class;
@@ -98,27 +132,26 @@ static struct tsm_dev *alloc_tsm_dev(struct device *parent)
 	return no_free_ptr(tsm_dev);
 }
 
-static struct tsm_dev *tsm_register_pci_or_reset(struct tsm_dev *tsm_dev,
-						 struct pci_tsm_ops *pci_ops)
+static int tsm_register_pci(struct tsm_dev *tsm_dev, struct pci_tsm_ops *pci_ops)
 {
 	int rc;
 
 	if (!pci_ops)
-		return tsm_dev;
+		return 0;
 
 	tsm_dev->pci_ops = pci_ops;
 	rc = pci_tsm_register(tsm_dev);
 	if (rc) {
+		tsm_dev->pci_ops = NULL;
 		dev_err(tsm_dev->dev.parent,
 			"PCI/TSM registration failure: %d\n", rc);
-		device_unregister(&tsm_dev->dev);
-		return ERR_PTR(rc);
+		return rc;
 	}
 	sysfs_update_group(&tsm_dev->dev.kobj, &tsm_pci_group);
 
 	/* Notify TSM userspace that PCI/TSM operations are now possible */
 	kobject_uevent(&tsm_dev->dev.kobj, KOBJ_CHANGE);
-	return tsm_dev;
+	return 0;
 }
 
 struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
@@ -139,14 +172,18 @@ struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *pci_ops)
 	if (rc)
 		return ERR_PTR(rc);
 
-	return tsm_register_pci_or_reset(no_free_ptr(tsm_dev), pci_ops);
+	rc = tsm_register_pci(tsm_dev, pci_ops);
+	if (rc) {
+		device_del(dev);
+		return ERR_PTR(rc);
+	}
+	return no_free_ptr(tsm_dev);
 }
 EXPORT_SYMBOL_GPL(tsm_register);
 
 void tsm_unregister(struct tsm_dev *tsm_dev)
 {
-	if (tsm_dev->pci_ops)
-		pci_tsm_unregister(tsm_dev);
+	tsm_put_active(tsm_dev);
 	device_unregister(&tsm_dev->dev);
 }
 EXPORT_SYMBOL_GPL(tsm_unregister);
include/linux/tsm.h +13 -0
index 7f72a154b6b2..f38d6fcf9cc9 100644
@@ -2,6 +2,7 @@
 #ifndef __TSM_H
 #define __TSM_H
 
+#include <linux/refcount.h>
 #include <linux/sizes.h>
 #include <linux/types.h>
 #include <linux/uuid.h>
@@ -109,8 +110,18 @@ struct tsm_report_ops {
 };
 
 struct pci_tsm_ops;
+
+/**
+ * struct tsm_dev - TEE Security Manager device
+ * @dev: device-model representation of the TSM
+ * @users: registration reference plus active references that retain the
+ *	PCI/TSM resources; reaching zero tears down those resources
+ * @id: instance identifier
+ * @pci_ops: PCI/TSM operations, or %NULL when PCI/TSM is unsupported
+ */
 struct tsm_dev {
 	struct device dev;
+	refcount_t users;
 	int id;
 	const struct pci_tsm_ops *pci_ops;
 };
@@ -122,6 +133,8 @@ int tsm_report_register(const struct tsm_report_ops *ops, void *priv);
 int tsm_report_unregister(const struct tsm_report_ops *ops);
 struct tsm_dev *tsm_register(struct device *parent, struct pci_tsm_ops *ops);
 void tsm_unregister(struct tsm_dev *tsm_dev);
+void tsm_get(struct tsm_dev *tsm_dev);
+void tsm_put(struct tsm_dev *tsm_dev);
 struct tsm_dev *find_tsm_dev(int id);
 struct pci_ide;
 int tsm_ide_stream_register(struct pci_ide *ide);
Signature · 1 line
2.43.0
author maintainer
#32 lore raw

RFC PATCH v6 8/11 iommufd: Add vIOMMU provider support

In reply to Aneesh Kumar K.V (Arm)

vIOMMU creation currently dispatches directly through the physical IOMMU driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

Look up an external provider first when allocating a vIOMMU. If none matches the requested type, represent the physical IOMMU driver as a fallback provider and dispatch through its get_viommu_size() and viommu_init() operations.

A registered provider starts with one registration reference. Registration does not pin the provider module. Each successful lookup takes both a provider reference and a module reference. Unregistration removes the provider from lookup before dropping the registration reference and does not wait for existing vIOMMUs.

A fallback provider instance is created for each vIOMMU allocation. Its initial users count of one represents the reference returned to that allocation. It has no registration reference and is not shared with other allocations, but otherwise follows the same lifetime rules as a registered provider.

---
 drivers/iommu/iommufd/Makefile          |   3 +-
 drivers/iommu/iommufd/iommufd_private.h |  22 ++++
 drivers/iommu/iommufd/viommu.c          |  29 +++--
 drivers/iommu/iommufd/viommu_provider.c | 160 ++++++++++++++++++++++++
 include/linux/iommufd.h                 |  32 +++++
 5 files changed, 234 insertions(+), 12 deletions(-)
 create mode 100644 drivers/iommu/iommufd/viommu_provider.c
drivers/iommu/iommufd/Makefile +2 -1
index 71d692c9a8f4..7ed46c286c42 100644
@@ -8,7 +8,8 @@ iommufd-y := \
 	main.o \
 	pages.o \
 	vfio_compat.o \
-	viommu.o
+	viommu.o \
+	viommu_provider.o
 
 iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
 
drivers/iommu/iommufd/iommufd_private.h +22 -0
index 6cf76f7ca379..eae607eb5d76 100644
@@ -21,6 +21,28 @@ struct iommu_option;
 struct iommufd_device;
 struct dma_buf_attachment;
 
+/**
+ * struct iommufd_viommu_provider - vIOMMU implementation provider
+ * @node: entry in the provider registry
+ * @users: references retaining @ops, @owner, and @data; registered providers
+ *	have one registry reference plus one per live vIOMMU, while a fallback
+ *	provider is private to one vIOMMU
+ * @ops: provider operations
+ * @owner: module implementing @ops
+ * @data: provider-private data retained until the final reference is released
+ */
+struct iommufd_viommu_provider {
+	struct list_head node;
+	const struct iommufd_viommu_provider_ops *ops;
+	refcount_t users;
+	struct module *owner;
+	void *data;
+};
+
+void iommufd_put_viommu_provider(struct iommufd_viommu_provider *provider);
+struct iommufd_viommu_provider *
+iommufd_get_viommu_provider(struct device *dev, enum iommu_viommu_type type);
+
 struct iommufd_sw_msi_map {
 	struct list_head sw_msi_item;
 	phys_addr_t sw_msi_start;
drivers/iommu/iommufd/viommu.c +18 -11
index bf5d58d55939..66bbd6e4571d 100644
@@ -11,6 +11,7 @@ void iommufd_viommu_destroy(struct iommufd_object *obj)
 
 	if (viommu->ops && viommu->ops->destroy)
 		viommu->ops->destroy(viommu);
+	iommufd_put_viommu_provider(viommu->provider);
 	refcount_dec(&viommu->hwpt->common.obj.users);
 	if (viommu->kvm_file)
 		fput(viommu->kvm_file);
@@ -28,7 +29,7 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	struct iommufd_hwpt_paging *hwpt_paging;
 	struct iommufd_viommu *viommu;
 	struct iommufd_device *idev;
-	const struct iommu_ops *ops;
+	struct iommufd_viommu_provider *provider = NULL;
 	size_t viommu_size;
 	int rc;
 
@@ -39,16 +40,16 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	if (IS_ERR(idev))
 		return PTR_ERR(idev);
 
-	ops = dev_iommu_ops(idev->dev);
-	if (!ops->get_viommu_size || !ops->viommu_init) {
-		rc = -EOPNOTSUPP;
+	provider = iommufd_get_viommu_provider(idev->dev, cmd->type);
+	if (IS_ERR(provider)) {
+		rc = PTR_ERR(provider);
+		provider = NULL;
 		goto out_put_idev;
 	}
-
-	viommu_size = ops->get_viommu_size(idev->dev, cmd->type);
+	viommu_size = provider->ops->get_size(idev->dev, cmd->type);
 	if (!viommu_size) {
 		rc = -EOPNOTSUPP;
-		goto out_put_idev;
+		goto out_put_provider;
 	}
 
 	/*
@@ -57,13 +58,13 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	 */
 	if (WARN_ON_ONCE(viommu_size < sizeof(*viommu))) {
 		rc = -EOPNOTSUPP;
-		goto out_put_idev;
+		goto out_put_provider;
 	}
 
 	hwpt_paging = iommufd_get_hwpt_paging(ucmd, cmd->hwpt_id);
 	if (IS_ERR(hwpt_paging)) {
 		rc = PTR_ERR(hwpt_paging);
-		goto out_put_idev;
+		goto out_put_provider;
 	}
 
 	if (!hwpt_paging->nest_parent) {
@@ -84,6 +85,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	viommu->type = cmd->type;
 	viommu->ictx = ucmd->ictx;
 	viommu->hwpt = hwpt_paging;
+	viommu->provider = provider;
+	viommu->provider_data = provider->data;
+	provider = NULL;
 	refcount_inc(&viommu->hwpt->common.obj.users);
 	INIT_LIST_HEAD(&viommu->veventqs);
 	init_rwsem(&viommu->veventqs_rwsem);
@@ -94,8 +98,9 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 	 */
 	viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev);
 
-	rc = ops->viommu_init(viommu, hwpt_paging->common.domain,
-			      user_data.len ? &user_data : NULL);
+	rc = viommu->provider->ops->init(viommu, idev->dev,
+					 hwpt_paging->common.domain,
+					 user_data.len ? &user_data : NULL);
 	if (rc)
 		goto out_put_hwpt;
 
@@ -110,6 +115,8 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd)
 
 out_put_hwpt:
 	iommufd_put_object(ucmd->ictx, &hwpt_paging->common.obj);
+out_put_provider:
+	iommufd_put_viommu_provider(provider);
 out_put_idev:
 	iommufd_put_object(ucmd->ictx, &idev->obj);
 	return rc;
drivers/iommu/iommufd/viommu_provider.c new file +160 -0

160 lines not on this page · raw message

include/linux/iommufd.h +32 -0
index 3267717f676d..7b906e0d6400 100644
@@ -22,9 +22,39 @@ struct iommu_user_data_array;
 struct iommufd_access;
 struct iommufd_ctx;
 struct iommufd_device;
+struct iommufd_viommu;
 struct iommufd_viommu_ops;
+struct iommufd_viommu_provider;
+struct module;
 struct page;
 
+/**
+ * struct iommufd_viommu_provider_ops - External vIOMMU implementation
+ * @owner: Module implementing the callbacks
+ * @type: Exact vIOMMU type implemented by this provider
+ * @get_size: Allocation size for a device, or zero if unsupported
+ * @init: Initialize the vIOMMU; private data is in viommu->provider_data
+ * @release: Optional final release of the registration's private data
+ *
+ * Callbacks run without the registry lock held. The operations must remain
+ * valid until @release completes. An allocation failure is authoritative:
+ * it must not cause allocation through the physical IOMMU driver instead.
+ */
+struct iommufd_viommu_provider_ops {
+	struct module *owner;
+	enum iommu_viommu_type type;
+	size_t (*get_size)(struct device *dev, enum iommu_viommu_type type);
+	int (*init)(struct iommufd_viommu *viommu, struct device *dev,
+		    struct iommu_domain *parent,
+		    const struct iommu_user_data *user_data);
+	void (*release)(void *data);
+};
+
+struct iommufd_viommu_provider *
+iommufd_register_viommu_provider(const struct iommufd_viommu_provider_ops *ops,
+	     void *data);
+void iommufd_unregister_viommu_provider(struct iommufd_viommu_provider *provider);
+
 enum iommufd_object_type {
 	IOMMUFD_OBJ_NONE,
 	IOMMUFD_OBJ_ANY = IOMMUFD_OBJ_NONE,
@@ -104,6 +134,8 @@ struct iommufd_viommu {
 	struct iommu_device *iommu_dev;
 	struct iommufd_hwpt_paging *hwpt;
 	struct file *kvm_file;
+	struct iommufd_viommu_provider *provider;
+	void *provider_data;
 
 	const struct iommufd_viommu_ops *ops;
 
Signature · 1 line
2.43.0
maintainer
#33 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 8 lines

vIOMMU creation currently dispatches directly through the physical IOMMU driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

This seems much more extensive than I imagined when I proposed this.

I imagined you'd just have iommufd see there is a TSM attached the pci device with a function call, and check if that TSM handled the requested iommu ID.

No need for a registration system because we already know the only possible TSM driver that can handle it, indeed a registration system makes this more fraught since that key relationship is no longer guarenteed.

Signature · 1 line
Jason
author maintainer
#34 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines

vIOMMU creation currently dispatches directly through the physical IOMMU driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

Quoted 9 lines

This seems much more extensive than I imagined when I proposed this.

I imagined you'd just have iommufd see there is a TSM attached the pci device with a function call, and check if that TSM handled the requested iommu ID.

No need for a registration system because we already know the only possible TSM driver that can handle it,

Agreed, there will be only one vIOMMU provider. But, without a registration mechanism, what would the module dependency look like?

I currently have the TSM driver depending on IOMMUFD. Therefore, the TSM driver needs to tell iommufd which vIOMMU type it provides and register the corresponding provider. IOMMUFD does not call into TSM directly.

-aneesh

maintainer
#35 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 21 lines · Aneesh Kumar K.V
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines

vIOMMU creation currently dispatches directly through the physical IOMMU driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

Quoted 9 lines

This seems much more extensive than I imagined when I proposed this.

I imagined you'd just have iommufd see there is a TSM attached the pci device with a function call, and check if that TSM handled the requested iommu ID.

No need for a registration system because we already know the only possible TSM driver that can handle it,

Quoted 6 lines

Agreed, there will be only one vIOMMU provider. But, without a registration mechanism, what would the module dependency look like?

I currently have the TSM driver depending on IOMMUFD. Therefore, the TSM driver needs to tell iommufd which vIOMMU type it provides and register the corresponding provider. IOMMUFD does not call into TSM directly.

Just put the two new ops in:

struct pci_tsm_ops {
	size_t (*viommu_get_size)(struct device *dev, enum iommu_viommu_type type);
	int (*viommu_init)(struct iommufd_viommu *viommu, struct device *dev,
		    struct iommu_domain *parent,
		    const struct iommu_user_data *user_data);
}

And bounce them with a static inline

static inline
ssize_t pci_tsm_viommu_get_size(struct device *dev, enum iommu_viommu_type type)

{
	const struct pci_tsm_ops *ops;
	struct pci_dev *pdev;

	if (!dev_is_pci(dev))
		return -EINVAL;

	pdev = to_pci_dev(dev);
	if (!pdev->tsm)
		return -ENODEV;

	ops = to_pci_tsm_ops(pdev->tsm);
	if (!ops->viommu_get_size)
		return 0;
	return ops->viommu_get_size(dev, type);
}

No module dependency.

The iommufd side is simple, it just calls this before calling the iommu driver. First one to return a size wins and creates the viommu.

We'd want to have a reasonable lifetime model where the the pdev->tsm is guarenteed stable while a driver is bound.

Jason

maintainer
#36 lore raw

RE: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 66 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 25, 2026 8:39 PM

Quoted 3 lines · Aneesh Kumar K.V
Quoted 1 line · Jason Gunthorpe
Quoted 1 line

vIOMMU creation currently dispatches directly through the physical

IOMMU

Quoted 18 lines
Quoted 8 lines
Quoted 7 lines

driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

Quoted 9 lines

This seems much more extensive than I imagined when I proposed this.

I imagined you'd just have iommufd see there is a TSM attached the pci device with a function call, and check if that TSM handled the requested iommu ID.

No need for a registration system because we already know the only possible TSM driver that can handle it,

Quoted 6 lines

Agreed, there will be only one vIOMMU provider. But, without a registration mechanism, what would the module dependency look like?

I currently have the TSM driver depending on IOMMUFD. Therefore, the TSM driver needs to tell iommufd which vIOMMU type it provides and register the corresponding provider. IOMMUFD does not call into TSM directly.

Just put the two new ops in:

struct pci_tsm_ops {
size_t (*viommu_get_size)(struct device *dev, enum
iommu_viommu_type type);
int (*viommu_init)(struct iommufd_viommu *viommu, struct device
*dev,

		    struct iommu_domain *parent,
		    const struct iommu_user_data *user_data);
}

And bounce them with a static inline

static inline
ssize_t pci_tsm_viommu_get_size(struct device *dev, enum iommu_viommu_type type)

{
	const struct pci_tsm_ops *ops;
	struct pci_dev *pdev;

	if (!dev_is_pci(dev))
		return -EINVAL;

	pdev = to_pci_dev(dev);
	if (!pdev->tsm)
		return -ENODEV;

	ops = to_pci_tsm_ops(pdev->tsm);
	if (!ops->viommu_get_size)
		return 0;
	return ops->viommu_get_size(dev, type);
}
Quoted 8 lines

No module dependency.

The iommufd side is simple, it just calls this before calling the iommu driver. First one to return a size wins and creates the viommu.

We'd want to have a reasonable lifetime model where the the pdev->tsm is guarenteed stable while a driver is bound.

+1

author maintainer
#37 lore raw

RE: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 6 lines · Kevin
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 25, 2026 8:39 PM

Quoted 2 lines · Aneesh Kumar K.V

Jason Gunthorpe <[email protected]> writes:

[ ... 26 lines skipped ... ]

Quoted 36 lines
Quoted 36 lines

Just put the two new ops in:

struct pci_tsm_ops {
size_t (*viommu_get_size)(struct device *dev, enum
iommu_viommu_type type);
int (*viommu_init)(struct iommufd_viommu *viommu, struct device
*dev,

		    struct iommu_domain *parent,
		    const struct iommu_user_data *user_data);
}

And bounce them with a static inline

static inline
ssize_t pci_tsm_viommu_get_size(struct device *dev, enum iommu_viommu_type type)

{
	const struct pci_tsm_ops *ops;
	struct pci_dev *pdev;

	if (!dev_is_pci(dev))
		return -EINVAL;

	pdev = to_pci_dev(dev);
	if (!pdev->tsm)
		return -ENODEV;

	ops = to_pci_tsm_ops(pdev->tsm);
	if (!ops->viommu_get_size)
		return 0;
	return ops->viommu_get_size(dev, type);
}

No module dependency.

Quoted 8 lines
Quoted 6 lines

The iommufd side is simple, it just calls this before calling the iommu driver. First one to return a size wins and creates the viommu.

We'd want to have a reasonable lifetime model where the the pdev->tsm is guarenteed stable while a driver is bound.

+1

I am looking into implementing this as follows:

tsm_viommu_get_ops() runs with pci_tsm_rwsem held for read and takes a temporary reference on the backend module (the CCA module). This keeps the selected ops callable until vIOMMU initialization completes. iommufd then drops the reference. This does not pin a particular TSM registration or prevent tsm_unregister().

CCA vIOMMU initialization takes a tsm_dev reference, keeping the TSM object and its PCI/TSM resources alive until the vIOMMU is destroyed. The initialization callback also takes a reference on the CCA module so that the vIOMMU callbacks remain available after iommufd drops the temporary discovery reference described above.

For a link TSM-connected device, a vdevice holds a pci_tsm_context reference. The context holds device references and increments PF0's context_users under the PF0 mutex. PCI/TSM disconnect checks that count under the same mutex and returns -EBUSY while contexts remain. The context is released during vdevice teardown. This prevents link disconnect while a vdevice is active without blocking tsm_unregister().

A new unregistering state is added to tsm_dev. tsm_unregister() sets it, unregisters the class device, and drops the registration reference. New vIOMMU allocations, vdevice contexts, and PCI TSM connect/lock operations reject the TSM once this state is set. Existing users retain their references and can be torn down normally, so tsm_unregister() does not need to wait for them. PCI/TSM teardown occurs when the last active tsm_dev reference is dropped.

-aneesh

maintainer
#38 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 5 lines · Aneesh Kumar K.V

tsm_viommu_get_ops() runs with pci_tsm_rwsem held for read and takes a temporary reference on the backend module (the CCA module). This keeps the selected ops callable until vIOMMU initialization completes. iommufd then drops the reference. This does not pin a particular TSM registration or prevent tsm_unregister().

That seems over complicated. Maybe we can't get to the sane locking I suggested earlier where TSM module is stable while a driver is bound, but we absolutely must have sane locking where we can "pin" the tsm for a pdev and it cannot be unregistered for long periods of time, such as while a viommu/vdev exists.

That period should start right before getting the ops and continue to until the viommu is destroyed.

No hot unplug of tsm modules while things are active.

Quoted 3 lines

CCA vIOMMU initialization takes a tsm_dev reference, keeping the TSM object and its PCI/TSM resources alive until the vIOMMU is destroyed.

iommufd should do this, so long as the viommu object exists the tsm for it exists. It should not be inside tsm drivers.

Quoted 3 lines

The initialization callback also takes a reference on the CCA module so that the vIOMMU callbacks remain available after iommufd drops the temporary discovery reference described above.

The initial pin should do this since it is the only way to prevent unregistration.

Quoted 6 lines

For a link TSM-connected device, a vdevice holds a pci_tsm_context reference. The context holds device references and increments PF0's context_users under the PF0 mutex. PCI/TSM disconnect checks that count under the same mutex and returns -EBUSY while contexts remain. The context is released during vdevice teardown. This prevents link disconnect while a vdevice is active without blocking tsm_unregister().

This one seems reasonable, but not sure a mutex is needed on top of a a simple refcount scheme.

Quoted 7 lines

A new unregistering state is added to tsm_dev. tsm_unregister() sets it, unregisters the class device, and drops the registration reference. New vIOMMU allocations, vdevice contexts, and PCI TSM connect/lock operations reject the TSM once this state is set. Existing users retain their references and can be torn down normally, so tsm_unregister() does not need to wait for them. PCI/TSM teardown occurs when the last active tsm_dev reference is dropped.

This seems over complicated, tsm unregistration should be made impossible while it is not able to complete. We shouldn't need the complexity of states here when we don't need to support tsm hot-unplug.

ideally the module refcount handles this and the only way to trigger a tsm remove is through module unload, with no sysfs path?

Jason

author maintainer
#39 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 14 lines · Jason Gunthorpe
Quoted 5 lines · Aneesh Kumar K.V

tsm_viommu_get_ops() runs with pci_tsm_rwsem held for read and takes a temporary reference on the backend module (the CCA module). This keeps the selected ops callable until vIOMMU initialization completes. iommufd then drops the reference. This does not pin a particular TSM registration or prevent tsm_unregister().

That seems over complicated. Maybe we can't get to the sane locking I suggested earlier where TSM module is stable while a driver is bound, but we absolutely must have sane locking where we can "pin" the tsm for a pdev and it cannot be unregistered for long periods of time, such as while a viommu/vdev exists.

Quoted 5 lines

That period should start right before getting the ops and continue to until the viommu is destroyed.

No hot unplug of tsm modules while things are active.

That is essentially how it works. I decided to take the module reference here and the tsm_dev reference in viommu_init() to keep the rest of viommu_alloc() cleaner. That is, we have:

	struct module *owner = NULL;

	ops = tsm_viommu_get_ops(idev->dev, cmd->type, &owner);

	if (!ops) {
		ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
                
	rc = ops->viommu_init(viommu, idev->dev,
	if (rc)
		goto out_put_hwpt;

out_put_idev:

	module_put(owner);

The tsm_dev and module details are needed only by tsm_viommu, not by a generic SMMU driver. For viommu_init() to take ownership of the resources acquired by get_ops(), I would either need to add a viommu_info argument to viommu_init(), affecting all IOMMU driver implementations, or make the error handling conditional and awkward.

What I have now is that get_ops() pins the module, so the CCA module cannot disappear after get_ops() returns. We retain that reference until viommu_init() completes, and viommu_init() establishes the long-term pins internally. This keeps the API simpler.

Quoted 8 lines
Quoted 4 lines

CCA vIOMMU initialization takes a tsm_dev reference, keeping the TSM object and its PCI/TSM resources alive until the vIOMMU is destroyed.

iommufd should do this, so long as the viommu object exists the tsm for it exists. It should not be inside tsm drivers.

That would expose more TSM details to iommufd. The reference must also be acquired under pci_tsm_rwsem.

/* Pin the current TSM and revalidate the selected vIOMMU operations. */ struct tsm_dev * pci_tsm_viommu_get_tsm_dev(struct pci_dev *pdev,
enum iommu_viommu_type type,
const struct iommufd_viommu_ops *expected_ops)

{
	const struct pci_tsm_ops *ops;
	const struct iommufd_viommu_ops *viommu_ops;
	struct tsm_dev *tsm_dev;
	int ret = -ENODEV;

	{
		guard(rwsem_read)(&pci_tsm_rwsem);
		if (!pdev->tsm)
			return ERR_PTR(-ENODEV);
		if (pdev->tsm->tsm_dev->unregistering)
			return ERR_PTR(-ENODEV);
		ops = to_pci_tsm_ops(pdev->tsm);
		if (!ops->viommu_get_ops)
			return ERR_PTR(-ENODEV);
		tsm_dev = pdev->tsm->tsm_dev;
		/* Unlike get_device(), this also keeps PCI/TSM resources active. */
		if (!tsm_try_get(tsm_dev))
			return ERR_PTR(-ENODEV);

		viommu_ops = ops->viommu_get_ops(&pdev->dev, type);
		if (viommu_ops == expected_ops)
			return tsm_dev;
		if (IS_ERR(viommu_ops))
			ret = PTR_ERR(viommu_ops);
	}

	tsm_put(tsm_dev);
	return ERR_PTR(ret);
}

EXPORT_SYMBOL_GPL(pci_tsm_viommu_get_tsm_dev);

Quoted 8 lines
Quoted 4 lines

The initialization callback also takes a reference on the CCA module so that the vIOMMU callbacks remain available after iommufd drops the temporary discovery reference described above.

The initial pin should do this since it is the only way to prevent unregistration.

see above

Quoted 11 lines
Quoted 7 lines

For a link TSM-connected device, a vdevice holds a pci_tsm_context reference. The context holds device references and increments PF0's context_users under the PF0 mutex. PCI/TSM disconnect checks that count under the same mutex and returns -EBUSY while contexts remain. The context is released during vdevice teardown. This prevents link disconnect while a vdevice is active without blocking tsm_unregister().

This one seems reasonable, but not sure a mutex is needed on top of a a simple refcount scheme.

The PF0 mutex is already used by PCI/TSM to serialize some of these operations. I can double-check whether it is necessary here.

Quoted 9 lines
Quoted 8 lines

A new unregistering state is added to tsm_dev. tsm_unregister() sets it, unregisters the class device, and drops the registration reference. New vIOMMU allocations, vdevice contexts, and PCI TSM connect/lock operations reject the TSM once this state is set. Existing users retain their references and can be torn down normally, so tsm_unregister() does not need to wait for them. PCI/TSM teardown occurs when the last active tsm_dev reference is dropped.

Quoted 9 lines

This seems over complicated, tsm unregistration should be made impossible while it is not able to complete. We shouldn't need the complexity of states here when we don't need to support tsm hot-unplug.

ideally the module refcount handles this and the only way to trigger a tsm remove is through module unload, with no sysfs path?

I agree. The existing code takes extra care to allow tsm_unregister(). However, if unloading arm-cca-host.ko is the only way to trigger unregistration, as it currently is, we can avoid this complexity.

-aneesh

maintainer
#40 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 38 lines · Aneesh Kumar K.V
Quoted 14 lines · Jason Gunthorpe
Quoted 5 lines · Aneesh Kumar K.V

tsm_viommu_get_ops() runs with pci_tsm_rwsem held for read and takes a temporary reference on the backend module (the CCA module). This keeps the selected ops callable until vIOMMU initialization completes. iommufd then drops the reference. This does not pin a particular TSM registration or prevent tsm_unregister().

That seems over complicated. Maybe we can't get to the sane locking I suggested earlier where TSM module is stable while a driver is bound, but we absolutely must have sane locking where we can "pin" the tsm for a pdev and it cannot be unregistered for long periods of time, such as while a viommu/vdev exists.

Quoted 5 lines

That period should start right before getting the ops and continue to until the viommu is destroyed.

No hot unplug of tsm modules while things are active.

That is essentially how it works. I decided to take the module reference here and the tsm_dev reference in viommu_init() to keep the rest of viommu_alloc() cleaner. That is, we have:

	struct module *owner = NULL;

	ops = tsm_viommu_get_ops(idev->dev, cmd->type, &owner);

	if (!ops) {
		ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
                
	rc = ops->viommu_init(viommu, idev->dev,
	if (rc)
		goto out_put_hwpt;
Quoted 2 lines

out_put_idev:

	module_put(owner);

if we have a get_ops we need a put_ops()..

Quoted 5 lines

The tsm_dev and module details are needed only by tsm_viommu, not by a generic SMMU driver. For viommu_init() to take ownership of the resources acquired by get_ops(), I would either need to add a viommu_info argument to viommu_init(), affecting all IOMMU driver implementations, or make the error handling conditional and awkward.

Just don't, iommufd can hold the tsm ops if it knows it created the viommu through tsm.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

CCA vIOMMU initialization takes a tsm_dev reference, keeping the TSM object and its PCI/TSM resources alive until the vIOMMU is destroyed.

iommufd should do this, so long as the viommu object exists the tsm for it exists. It should not be inside tsm drivers.

That would expose more TSM details to iommufd. The reference must also be acquired under pci_tsm_rwsem.

That seems like overkill.

Quoted 15 lines

/* Pin the current TSM and revalidate the selected vIOMMU operations. */ struct tsm_dev * pci_tsm_viommu_get_tsm_dev(struct pci_dev *pdev,
enum iommu_viommu_type type,
const struct iommufd_viommu_ops *expected_ops)

{
	const struct pci_tsm_ops *ops;
	const struct iommufd_viommu_ops *viommu_ops;
	struct tsm_dev *tsm_dev;
	int ret = -ENODEV;

	{
		guard(rwsem_read)(&pci_tsm_rwsem);
		if (!pdev->tsm)
			return ERR_PTR(-ENODEV);
		if (pdev->tsm->tsm_dev->unregistering)
Quoted 8 lines
			return ERR_PTR(-ENODEV);
		ops = to_pci_tsm_ops(pdev->tsm);
		if (!ops->viommu_get_ops)
			return ERR_PTR(-ENODEV);
		tsm_dev = pdev->tsm->tsm_dev;
		/* Unlike get_device(), this also keeps PCI/TSM resources active. */
		if (!tsm_try_get(tsm_dev))
			return ERR_PTR(-ENODEV);

This is way too complicated for what should be a very simple scheme :(

rcu_read_lock()
tsm = rcu_derference(pdev->tsm);
if (!tsm)
return NULL;
/* Prevent the module from unloading which must be the only way to
trigger unregister */
if (!try_module_get(tsm->ops->module))
return NULL;
rcu_read_unlock()

And this should be exposed to iommufd..

viommu_create:
tsm = tsm_get_device(pdev)
tsm_get_viommu_ops(tsm,...)
[..]

viommu_destroy:
tsm_put_device(tsm)

No lock, no registering FSM, just RCU free the pdev->tsm memory and do that module unload rcu synchronize during module unload. No hand of of the lifecylce to other layers.

Hold the module get in iommufd inside the iommufd viommu object.

It is simple and easy to understand.

Quoted 6 lines
		viommu_ops = ops->viommu_get_ops(&pdev->dev, type);
		if (viommu_ops == expected_ops)
			return tsm_dev;
		if (IS_ERR(viommu_ops))
			ret = PTR_ERR(viommu_ops);
	}

This should just be try_module_get and a touch of RCU.

Quoted 6 lines
Quoted 2 lines

ideally the module refcount handles this and the only way to trigger a tsm remove is through module unload, with no sysfs path?

I agree. The existing code takes extra care to allow tsm_unregister(). However, if unloading arm-cca-host.ko is the only way to trigger unregistration, as it currently is, we can avoid this complexity.

I've been badly traumitized by hot unplug races, bugs and deadlock, let's not introduce anything like this here, there is no need.

Jason

author maintainer
#41 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 113 lines · Jason Gunthorpe
Quoted 38 lines · Aneesh Kumar K.V
Quoted 14 lines · Jason Gunthorpe
Quoted 5 lines · Aneesh Kumar K.V

tsm_viommu_get_ops() runs with pci_tsm_rwsem held for read and takes a temporary reference on the backend module (the CCA module). This keeps the selected ops callable until vIOMMU initialization completes. iommufd then drops the reference. This does not pin a particular TSM registration or prevent tsm_unregister().

That seems over complicated. Maybe we can't get to the sane locking I suggested earlier where TSM module is stable while a driver is bound, but we absolutely must have sane locking where we can "pin" the tsm for a pdev and it cannot be unregistered for long periods of time, such as while a viommu/vdev exists.

Quoted 5 lines

That period should start right before getting the ops and continue to until the viommu is destroyed.

No hot unplug of tsm modules while things are active.

That is essentially how it works. I decided to take the module reference here and the tsm_dev reference in viommu_init() to keep the rest of viommu_alloc() cleaner. That is, we have:

	struct module *owner = NULL;

	ops = tsm_viommu_get_ops(idev->dev, cmd->type, &owner);

	if (!ops) {
		ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
                
	rc = ops->viommu_init(viommu, idev->dev,
	if (rc)
		goto out_put_hwpt;
Quoted 2 lines

out_put_idev:

	module_put(owner);

if we have a get_ops we need a put_ops()..

Quoted 5 lines

The tsm_dev and module details are needed only by tsm_viommu, not by a generic SMMU driver. For viommu_init() to take ownership of the resources acquired by get_ops(), I would either need to add a viommu_info argument to viommu_init(), affecting all IOMMU driver implementations, or make the error handling conditional and awkward.

Just don't, iommufd can hold the tsm ops if it knows it created the viommu through tsm.

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

CCA vIOMMU initialization takes a tsm_dev reference, keeping the TSM object and its PCI/TSM resources alive until the vIOMMU is destroyed.

iommufd should do this, so long as the viommu object exists the tsm for it exists. It should not be inside tsm drivers.

That would expose more TSM details to iommufd. The reference must also be acquired under pci_tsm_rwsem.

That seems like overkill.

Quoted 15 lines

/* Pin the current TSM and revalidate the selected vIOMMU operations. */ struct tsm_dev * pci_tsm_viommu_get_tsm_dev(struct pci_dev *pdev,
enum iommu_viommu_type type,
const struct iommufd_viommu_ops *expected_ops)

{
	const struct pci_tsm_ops *ops;
	const struct iommufd_viommu_ops *viommu_ops;
	struct tsm_dev *tsm_dev;
	int ret = -ENODEV;

	{
		guard(rwsem_read)(&pci_tsm_rwsem);
		if (!pdev->tsm)
			return ERR_PTR(-ENODEV);
		if (pdev->tsm->tsm_dev->unregistering)
Quoted 8 lines
			return ERR_PTR(-ENODEV);
		ops = to_pci_tsm_ops(pdev->tsm);
		if (!ops->viommu_get_ops)
			return ERR_PTR(-ENODEV);
		tsm_dev = pdev->tsm->tsm_dev;
		/* Unlike get_device(), this also keeps PCI/TSM resources active. */
		if (!tsm_try_get(tsm_dev))
			return ERR_PTR(-ENODEV);

This is way too complicated for what should be a very simple scheme :(

rcu_read_lock()
tsm = rcu_derference(pdev->tsm);
if (!tsm)
return NULL;
/* Prevent the module from unloading which must be the only way to
trigger unregister */
if (!try_module_get(tsm->ops->module))
return NULL;
rcu_read_unlock()

And this should be exposed to iommufd..

viommu_create:
tsm = tsm_get_device(pdev)
tsm_get_viommu_ops(tsm,...)
[..]

viommu_destroy:
tsm_put_device(tsm)

Quoted 8 lines

No lock, no registering FSM, just RCU free the pdev->tsm memory and do that module unload rcu synchronize during module unload. No hand of of the lifecylce to other layers.

Hold the module get in iommufd inside the iommufd viommu object.

It is simple and easy to understand.

Switching pdev->tsm to an RCU-protected pointer requires broader changes to the existing TSM code. To move this series forward, I will continue protecting it with pci_tsm_rwsem in the next revision, as that requires fewer changes. We can revisit an RCU conversion later if needed?

With this approach, viommu_alloc() will do:

	struct tsm_dev *tsm_dev = NULL;


	tsm_dev = tsm_get_device(idev->dev);
	ops = tsm_dev ? tsm_viommu_get_ops(tsm_dev, idev->dev, cmd->type) : NULL;
	if (!ops) {
                tsm_dev = NULL;
		ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
        }

	viommu = (struct iommufd_viommu *)_iommufd_object_alloc_ucmd(
	viommu->tsm_dev = tsm_dev;
	tsm_dev = NULL;

	rc = ops->viommu_init(viommu, idev->dev,....)

....

	if (tsm_dev)
		tsm_put_device(tsm_dev);

void iommufd_viommu_destroy(struct iommufd_object *obj) {
..

	if (viommu->tsm_dev)
		tsm_put_device(viommu->tsm_dev);

...
}

struct tsm_dev *pci_tsm_get_device(struct pci_dev *pdev)

{
	const struct pci_tsm_ops *ops;
	struct tsm_dev *tsm_dev;

	guard(rwsem_read)(&pci_tsm_rwsem);
	if (!pdev->tsm)
		return NULL;

	tsm_dev = pdev->tsm->tsm_dev;

	ops = tsm_dev->pci_ops;
	if (!try_module_get(ops->owner)) // arm-cca-host
		return ERR_PTR(-ENODEV);
	if (!tsm_try_get(tsm_dev)) {
		module_put(ops->owner);
		return ERR_PTR(-ENODEV);
	}
	return tsm_dev;
}
Quoted 14 lines
Quoted 7 lines
		viommu_ops = ops->viommu_get_ops(&pdev->dev, type);
		if (viommu_ops == expected_ops)
			return tsm_dev;
		if (IS_ERR(viommu_ops))
			ret = PTR_ERR(viommu_ops);
	}

This should just be try_module_get and a touch of RCU.

Quoted 3 lines
Quoted 2 lines

ideally the module refcount handles this and the only way to trigger a tsm remove is through module unload, with no sysfs path?

Quoted 7 lines
Quoted 3 lines

I agree. The existing code takes extra care to allow tsm_unregister(). However, if unloading arm-cca-host.ko is the only way to trigger unregistration, as it currently is, we can avoid this complexity.

I've been badly traumitized by hot unplug races, bugs and deadlock, let's not introduce anything like this here, there is no need.

Are you suggesting setting suppress_bind_attrs = true for the arm-cca-host driver?

The driver model otherwise allows the driver to be unbound. For example, what happens when arm-rmi device is unbound from the arm-cca-host driver? We need to support tsm_unregister() in that case. I am also not sure whether there are other paths that can call tsm_unregister(). Currently, we register the cleanup callback via

	tsm_dev = tsm_register(&sdev->dev, &cca_link_pci_ops);

	ret = devm_add_action_or_reset(&sdev->dev, cca_link_tsm_remove, tsm_dev);
	if (ret)

-aneesh

maintainer
#42 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

Quoted 4 lines · Aneesh Kumar K.V

Switching pdev->tsm to an RCU-protected pointer requires broader changes to the existing TSM code. To move this series forward, I will continue protecting it with pci_tsm_rwsem in the next revision, as that requires fewer changes. We can revisit an RCU conversion later if needed?

Sure, as long as we get the general big picture properties of no hot unplug and a very simple lifecycle model

Quoted 17 lines

With this approach, viommu_alloc() will do:

	struct tsm_dev *tsm_dev = NULL;


	tsm_dev = tsm_get_device(idev->dev);
	ops = tsm_dev ? tsm_viommu_get_ops(tsm_dev, idev->dev, cmd->type) : NULL;
	if (!ops) {
                tsm_dev = NULL;
		ops = iommu_dev->ops->get_viommu_ops(idev->dev, cmd->type);
        }

	viommu = (struct iommufd_viommu *)_iommufd_object_alloc_ucmd(
	viommu->tsm_dev = tsm_dev;
	tsm_dev = NULL;

	rc = ops->viommu_init(viommu, idev->dev,....)
Quoted 3 lines

....

	if (tsm_dev)
		tsm_put_device(tsm_dev);

This probably shouldn't be here? The way iommufd usually works is these undos are alway here:

Quoted 10 lines

void iommufd_viommu_destroy(struct iommufd_object *obj) {
..

	if (viommu->tsm_dev)
		tsm_put_device(viommu->tsm_dev);

...
}

struct tsm_dev *pci_tsm_get_device(struct pci_dev *pdev)

Quoted 14 lines
{
	const struct pci_tsm_ops *ops;
	struct tsm_dev *tsm_dev;

	guard(rwsem_read)(&pci_tsm_rwsem);
	if (!pdev->tsm)
		return NULL;

	tsm_dev = pdev->tsm->tsm_dev;

	ops = tsm_dev->pci_ops;
	if (!try_module_get(ops->owner)) // arm-cca-host
		return ERR_PTR(-ENODEV);
	if (!tsm_try_get(tsm_dev)) {

What/why is this tsm_try_get()? I wouldn't expect to see both try_module_get() and tsm_try_get()?

Quoted 2 lines

Are you suggesting setting suppress_bind_attrs = true for the arm-cca-host driver?

That or it blocks waiting on unbind.

No hot unplug of TSMs under active use. This is very hard to implement correctly, and there is no use case.

Quoted 1 line

The driver model otherwise allows the driver to be unbound.

Isn't this is a side effect of introducing smcc bus? We didn't used to have actual drivers for this stuff that isn't physically hot pluggable.

Quoted 8 lines

We need to support tsm_unregister() in that
case. I am also not sure whether there are other paths that can call tsm_unregister(). Currently, we register the cleanup callback via

	tsm_dev = tsm_register(&sdev->dev, &cca_link_pci_ops);

	ret = devm_add_action_or_reset(&sdev->dev, cca_link_tsm_remove, tsm_dev);
	if (ret)

Yeah, since you have a driver it has to be structured like that, but that doesn't mean we have to be forced into implementing full general hot unplug across all of tsm.

Either block sysfs and use the module refcount, or hang in removal until all the users naturally close (like vfio). Everything else is too complicated to be justified..

Jason

author maintainer
#43 lore raw

Re: [RFC PATCH v6 08/11] iommufd: Add vIOMMU provider support

In reply to Jason Gunthorpe
Quoted 59 lines · Jason Gunthorpe
Quoted 21 lines · Aneesh Kumar K.V
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines

vIOMMU creation currently dispatches directly through the physical IOMMU driver. Some vIOMMU types need an implementation supplied by another subsystem, together with private data whose lifetime extends across all vIOMMUs using that implementation.

Introduce a vIOMMU provider that associates implementation operations, their module owner and private data. Allow one external provider to register for each non-default vIOMMU type.

Quoted 9 lines

This seems much more extensive than I imagined when I proposed this.

I imagined you'd just have iommufd see there is a TSM attached the pci device with a function call, and check if that TSM handled the requested iommu ID.

No need for a registration system because we already know the only possible TSM driver that can handle it,

Quoted 6 lines

Agreed, there will be only one vIOMMU provider. But, without a registration mechanism, what would the module dependency look like?

I currently have the TSM driver depending on IOMMUFD. Therefore, the TSM driver needs to tell iommufd which vIOMMU type it provides and register the corresponding provider. IOMMUFD does not call into TSM directly.

Just put the two new ops in:

struct pci_tsm_ops {
	size_t (*viommu_get_size)(struct device *dev, enum iommu_viommu_type type);
	int (*viommu_init)(struct iommufd_viommu *viommu, struct device *dev,
		    struct iommu_domain *parent,
		    const struct iommu_user_data *user_data);
}

And bounce them with a static inline

static inline
ssize_t pci_tsm_viommu_get_size(struct device *dev, enum iommu_viommu_type type)

{
	const struct pci_tsm_ops *ops;
	struct pci_dev *pdev;

	if (!dev_is_pci(dev))
		return -EINVAL;

	pdev = to_pci_dev(dev);
	if (!pdev->tsm)
		return -ENODEV;

	ops = to_pci_tsm_ops(pdev->tsm);
	if (!ops->viommu_get_size)
		return 0;
	return ops->viommu_get_size(dev, type);
}
Quoted 8 lines

No module dependency.

The iommufd side is simple, it just calls this before calling the iommu driver. First one to return a size wins and creates the viommu.

We'd want to have a reasonable lifetime model where the the pdev->tsm is guarenteed stable while a driver is bound.

And the init side will be as below.

bool use_tsm = false;

viommu_size = tsm_viommu_get_size(dev, type);

if (viommu_size) {
	use_tsm = true;
} else {
	viommu_size = iommu_ops->get_viommu_size(dev, type);
	if (!viommu_size)
		return -EOPNOTSUPP;
}

Initialization becomes:

if (use_tsm)

	ret = tsm_viommu_init(viommu, dev, parent, user_data);
else
	ret = iommu_ops->viommu_init(viommu, parent, user_data);

-aneesh

author maintainer
#44 lore raw

RFC PATCH v6 9/11 iommufd: Add the vdevice TSM request ioctl

In reply to Aneesh Kumar K.V (Arm)

A VMM needs to forward guest-originated TSM commands to the TSM implementation managing an assigned device. The IOMMUFD vdevice identifies that device within its vIOMMU and provides the appropriate dispatch point.

Add IOMMU_VDEVICE_TSM_REQ to send an opaque request to a vdevice and optionally return a response. Define common operation and guest architecture identifiers for CCA, SEV and TDX requests.

The ioctl return value reports dispatch or data transfer errors, while tsm_code carries the TSM-specific result.

Use sockptr_t for the internal request and response buffers so TSM implementations are not tied to userspace pointers.

---
 drivers/iommu/iommufd/Makefile          |  2 +
 drivers/iommu/iommufd/iommufd_private.h |  8 ++
 drivers/iommu/iommufd/main.c            |  3 +
 drivers/iommu/iommufd/tsm.c             | 98 +++++++++++++++++++++++++
 drivers/iommu/iommufd/viommu.c          |  2 +
 include/linux/iommufd.h                 |  5 ++
 include/linux/tsm.h                     | 25 +++++++
 include/uapi/linux/iommufd.h            | 73 ++++++++++++++++++
 8 files changed, 216 insertions(+)
 create mode 100644 drivers/iommu/iommufd/tsm.c
drivers/iommu/iommufd/Makefile +2 -0
index 7ed46c286c42..105a54ff89f5 100644
@@ -11,6 +11,8 @@ iommufd-y := \
 	viommu.o \
 	viommu_provider.o
 
+iommufd-$(CONFIG_TSM) += tsm.o
+
 iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
 
 obj-$(CONFIG_IOMMUFD) += iommufd.o
drivers/iommu/iommufd/iommufd_private.h +8 -0
index eae607eb5d76..0c67f2e1f137 100644
@@ -721,6 +721,14 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj);
 void iommufd_vdevice_abort(struct iommufd_object *obj);
 int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd);
 void iommufd_hw_queue_destroy(struct iommufd_object *obj);
+#ifdef CONFIG_TSM
+int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd);
+#else
+static inline int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd)
+{
+	return -EOPNOTSUPP;
+}
+#endif
 
 static inline struct iommufd_vdevice *
 iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
drivers/iommu/iommufd/main.c +3 -0
index 8c6d43601afb..4a42cda0e8f8 100644
@@ -432,6 +432,7 @@ union ucmd_buffer {
 	struct iommu_veventq_alloc veventq;
 	struct iommu_vfio_ioas vfio_ioas;
 	struct iommu_viommu_alloc viommu;
+	struct iommu_vdevice_tsm_req tsm_req;
 #ifdef CONFIG_IOMMUFD_TEST
 	struct iommu_test_cmd test;
 #endif
@@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
 		 __reserved),
 	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
 		 struct iommu_viommu_alloc, out_viommu_id),
+	IOCTL_OP(IOMMU_VDEVICE_TSM_REQ, iommufd_vdevice_tsm_req_ioctl,
+		 struct iommu_vdevice_tsm_req, tsm_code),
 #ifdef CONFIG_IOMMUFD_TEST
 	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
 #endif
drivers/iommu/iommufd/tsm.c new file +98 -0

98 lines not on this page · raw message

drivers/iommu/iommufd/viommu.c +2 -0
index 66bbd6e4571d..c462086ff5df 100644
@@ -3,6 +3,8 @@
  */
 #include <linux/file.h>
 #include "iommufd_private.h"
+#include <linux/cleanup.h>
+#include <linux/tsm.h>
 
 void iommufd_viommu_destroy(struct iommufd_object *obj)
 {
include/linux/iommufd.h +5 -0
index 7b906e0d6400..f16b82034be9 100644
@@ -27,6 +27,7 @@ struct iommufd_viommu_ops;
 struct iommufd_viommu_provider;
 struct module;
 struct page;
+struct tsm_guest_req_info;
 
 /**
  * struct iommufd_viommu_provider_ops - External vIOMMU implementation
@@ -199,6 +200,7 @@ struct iommufd_hw_queue {
  *                include/uapi/linux/iommufd.h)
  *                If driver has a deinit function to revert what vdevice_init op
  *                does, it should set it to the @vdev->destroy function pointer
+ * @vdevice_tsm_req: Forward a guest TSM request to a driver-owned vDEVICE
  * @get_hw_queue_size: Get the size of a driver-defined HW queue structure for a
  *                     given @viommu corresponding to @queue_type. Driver should
  *                     return 0 if HW queue aren't supported accordingly. It is
@@ -225,6 +227,9 @@ struct iommufd_viommu_ops {
 				struct iommu_user_data_array *array);
 	const size_t vdevice_size;
 	int (*vdevice_init)(struct iommufd_vdevice *vdev);
+	ssize_t (*vdevice_tsm_req)(struct iommufd_vdevice *vdev,
+				   struct tsm_guest_req_info *info,
+				   u64 *tsm_code);
 	size_t (*get_hw_queue_size)(struct iommufd_viommu *viommu,
 				    enum iommu_hw_queue_type queue_type);
 	/* AMD's HW will add hw_queue_init simply using @hw_queue->base_addr */
include/linux/tsm.h +25 -0
index f38d6fcf9cc9..39bad60b4815 100644
@@ -7,6 +7,8 @@
 #include <linux/types.h>
 #include <linux/uuid.h>
 #include <linux/device.h>
+#include <linux/sockptr.h>
+#include <uapi/linux/iommufd.h>
 
 #define TSM_REPORT_INBLOB_MAX 64
 #define TSM_REPORT_OUTBLOB_MAX SZ_16M
@@ -139,4 +141,27 @@ struct tsm_dev *find_tsm_dev(int id);
 struct pci_ide;
 int tsm_ide_stream_register(struct pci_ide *ide);
 void tsm_ide_stream_unregister(struct pci_ide *ide);
+
+#ifdef CONFIG_TSM
+/**
+ * struct tsm_guest_req_info - parameters for a guest-initiated TSM request
+ * @op: operation for the guest-initiated request
+ * @tvm_arch: guest TVM architecture
+ * @req: request data buffer filled by guest
+ * @req_len: the size of @req filled by guest
+ * @resp: response data buffer filled by host
+ * @resp_len: the size of @resp buffer filled by guest
+ */
+struct tsm_guest_req_info {
+	enum iommu_vdevice_tsm_guest_req_op op;
+	enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch;
+	sockptr_t req;
+	size_t req_len;
+	sockptr_t resp;
+	size_t resp_len;
+};
+#else
+struct tsm_guest_req_info;
+#endif
+
 #endif /* __TSM_H */
include/uapi/linux/iommufd.h +73 -0

83 lines not on this page · raw message

Signature · 1 line
2.43.0
#45 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 8 lines · Aneesh Kumar K.V (Arm)

A VMM needs to forward guest-originated TSM commands to the TSM implementation managing an assigned device. The IOMMUFD vdevice identifies that device within its vIOMMU and provides the appropriate dispatch point.

Add IOMMU_VDEVICE_TSM_REQ to send an opaque request to a vdevice and optionally return a response. Define common operation and guest architecture identifiers for CCA, SEV and TDX requests.

iommufd_viommu_provider_ops already knows what architecture it is, why bother with these identifiers? Thanks,

Quoted 350 lines

The ioctl return value reports dispatch or data transfer errors, while tsm_code carries the TSM-specific result.

Use sockptr_t for the internal request and response buffers so TSM implementations are not tied to userspace pointers.

---
  drivers/iommu/iommufd/Makefile          |  2 +
  drivers/iommu/iommufd/iommufd_private.h |  8 ++
  drivers/iommu/iommufd/main.c            |  3 +
  drivers/iommu/iommufd/tsm.c             | 98 +++++++++++++++++++++++++
  drivers/iommu/iommufd/viommu.c          |  2 +
  include/linux/iommufd.h                 |  5 ++
  include/linux/tsm.h                     | 25 +++++++
  include/uapi/linux/iommufd.h            | 73 ++++++++++++++++++
  8 files changed, 216 insertions(+)

create mode 100644 drivers/iommu/iommufd/tsm.c

drivers/iommu/iommufd/Makefile +2 -0
index 7ed46c286c42..105a54ff89f5 100644
@@ -11,6 +11,8 @@ iommufd-y := \
  	viommu.o \
  	viommu_provider.o
 
+iommufd-$(CONFIG_TSM) += tsm.o
+
  iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
 
  obj-$(CONFIG_IOMMUFD) += iommufd.o
drivers/iommu/iommufd/iommufd_private.h +8 -0
index eae607eb5d76..0c67f2e1f137 100644
@@ -721,6 +721,14 @@ void iommufd_vdevice_destroy(struct iommufd_object *obj);
  void iommufd_vdevice_abort(struct iommufd_object *obj);
  int iommufd_hw_queue_alloc_ioctl(struct iommufd_ucmd *ucmd);
  void iommufd_hw_queue_destroy(struct iommufd_object *obj);
+#ifdef CONFIG_TSM
+int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd);
+#else
+static inline int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd)
+{
+	return -EOPNOTSUPP;
+}
+#endif
 
  static inline struct iommufd_vdevice *
  iommufd_get_vdevice(struct iommufd_ctx *ictx, u32 id)
drivers/iommu/iommufd/main.c +3 -0
index 8c6d43601afb..4a42cda0e8f8 100644
@@ -432,6 +432,7 @@ union ucmd_buffer {
  	struct iommu_veventq_alloc veventq;
  	struct iommu_vfio_ioas vfio_ioas;
  	struct iommu_viommu_alloc viommu;
+	struct iommu_vdevice_tsm_req tsm_req;
  #ifdef CONFIG_IOMMUFD_TEST
  	struct iommu_test_cmd test;
  #endif
@@ -493,6 +494,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = {
  		 __reserved),
  	IOCTL_OP(IOMMU_VIOMMU_ALLOC, iommufd_viommu_alloc_ioctl,
  		 struct iommu_viommu_alloc, out_viommu_id),
+	IOCTL_OP(IOMMU_VDEVICE_TSM_REQ, iommufd_vdevice_tsm_req_ioctl,
+		 struct iommu_vdevice_tsm_req, tsm_code),
  #ifdef CONFIG_IOMMUFD_TEST
  	IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last),
  #endif
drivers/iommu/iommufd/tsm.c new file +98 -0
new file mode 100644
index 000000000000..2fb51a86c5ec
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2026 ARM Ltd.
+ */
+
+#include <linux/tsm.h>
+#include "iommufd_private.h"
+
+static bool iommufd_vdevice_tsm_req_arch_valid(u32 tvm_arch)
+{
+	switch (tvm_arch) {
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX:
+		return true;
+	default:
+		return false;
+	}
+}
+
+static bool iommufd_vdevice_tsm_req_op_valid(u32 op, u32 tvm_arch)
+{
+	switch (op) {
+	case TSM_REQ_VALIDATE_MMIO:
+	case TSM_REQ_SET_TDI_STATE:
+		return true;
+	case TSM_REQ_SEV_ENABLE_DMA:
+	case TSM_REQ_SEV_DISABLE_DMA:
+		return tvm_arch == IOMMU_VDEVICE_TSM_TVM_ARCH_SEV;
+	case TSM_REQ_READ_OBJECT:
+	case TSM_REQ_REGEN_OBJECT:
+	case TSM_REQ_OBJECT_INFO:
+		return true;
+	default:
+		return false;
+	}
+}
+
+/**
+ * iommufd_vdevice_tsm_req_ioctl - Forward TSM requests
+ * @ucmd: user command data for IOMMU_VDEVICE_TSM_REQ
+ *
+ * Resolve @iommu_vdevice_tsm_req::vdevice_id to a vdevice and pass the
+ * request/response buffers to its vIOMMU provider.
+ *
+ * Return:
+ *  -errno on error.
+ *  positive residue if response/request bytes were left unconsumed.
+ *    if response buffer is provided, residue indicates the number of bytes
+ *    not used in response buffer
+ *    if there is no response buffer, residue indicates the number of bytes
+ *    not consumed in req buffer
+ *  0 otherwise.
+ */
+int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd)
+{
+	int ret;
+	struct iommufd_vdevice *vdev;
+	struct iommu_vdevice_tsm_req *cmd = ucmd->cmd;
+	struct tsm_guest_req_info info = {
+		.op = cmd->op,
+		.tvm_arch = cmd->tvm_arch,
+		.req   = {
+			.user = u64_to_user_ptr(cmd->req_uptr),
+			.is_kernel = false,
+		},
+		.req_len = cmd->req_len,
+		.resp    =  {
+			.user = u64_to_user_ptr(cmd->resp_uptr),
+			.is_kernel = false,
+		},
+		.resp_len = cmd->resp_len,
+	};
+
+	if (!iommufd_vdevice_tsm_req_arch_valid(cmd->tvm_arch))
+		return -EINVAL;
+
+	if (!iommufd_vdevice_tsm_req_op_valid(cmd->op, cmd->tvm_arch))
+		return -EINVAL;
+
+	vdev = iommufd_get_vdevice(ucmd->ictx, cmd->vdevice_id);
+	if (IS_ERR(vdev))
+		return PTR_ERR(vdev);
+
+	cmd->tsm_code = 0;
+	if (!vdev->viommu->ops || !vdev->viommu->ops->vdevice_tsm_req)
+		ret = -EOPNOTSUPP;
+	else
+		ret = vdev->viommu->ops->vdevice_tsm_req(vdev, &info,
+							 &cmd->tsm_code);
+
+	/* Always copy the tsm_code as response */
+	if (iommufd_ucmd_respond(ucmd, sizeof(*cmd)))
+		ret = -EFAULT;
+
+	iommufd_put_object(ucmd->ictx, &vdev->obj);
+	return ret;
+}
drivers/iommu/iommufd/viommu.c +2 -0
index 66bbd6e4571d..c462086ff5df 100644
@@ -3,6 +3,8 @@
   */
  #include <linux/file.h>
  #include "iommufd_private.h"
+#include <linux/cleanup.h>
+#include <linux/tsm.h>
 
  void iommufd_viommu_destroy(struct iommufd_object *obj)
  {
include/linux/iommufd.h +5 -0
index 7b906e0d6400..f16b82034be9 100644
@@ -27,6 +27,7 @@ struct iommufd_viommu_ops;
  struct iommufd_viommu_provider;
  struct module;
  struct page;
+struct tsm_guest_req_info;
 
  /**
   * struct iommufd_viommu_provider_ops - External vIOMMU implementation
@@ -199,6 +200,7 @@ struct iommufd_hw_queue {
   *                include/uapi/linux/iommufd.h)
   *                If driver has a deinit function to revert what vdevice_init op
   *                does, it should set it to the @vdev->destroy function pointer
+ * @vdevice_tsm_req: Forward a guest TSM request to a driver-owned vDEVICE
   * @get_hw_queue_size: Get the size of a driver-defined HW queue structure for a
   *                     given @viommu corresponding to @queue_type. Driver should
   *                     return 0 if HW queue aren't supported accordingly. It is
@@ -225,6 +227,9 @@ struct iommufd_viommu_ops {
  				struct iommu_user_data_array *array);
  	const size_t vdevice_size;
  	int (*vdevice_init)(struct iommufd_vdevice *vdev);
+	ssize_t (*vdevice_tsm_req)(struct iommufd_vdevice *vdev,
+				   struct tsm_guest_req_info *info,
+				   u64 *tsm_code);
  	size_t (*get_hw_queue_size)(struct iommufd_viommu *viommu,
  				    enum iommu_hw_queue_type queue_type);
  	/* AMD's HW will add hw_queue_init simply using @hw_queue->base_addr */
include/linux/tsm.h +25 -0
index f38d6fcf9cc9..39bad60b4815 100644
@@ -7,6 +7,8 @@
  #include <linux/types.h>
  #include <linux/uuid.h>
  #include <linux/device.h>
+#include <linux/sockptr.h>
+#include <uapi/linux/iommufd.h>
 
  #define TSM_REPORT_INBLOB_MAX 64
  #define TSM_REPORT_OUTBLOB_MAX SZ_16M
@@ -139,4 +141,27 @@ struct tsm_dev *find_tsm_dev(int id);
  struct pci_ide;
  int tsm_ide_stream_register(struct pci_ide *ide);
  void tsm_ide_stream_unregister(struct pci_ide *ide);
+
+#ifdef CONFIG_TSM
+/**
+ * struct tsm_guest_req_info - parameters for a guest-initiated TSM request
+ * @op: operation for the guest-initiated request
+ * @tvm_arch: guest TVM architecture
+ * @req: request data buffer filled by guest
+ * @req_len: the size of @req filled by guest
+ * @resp: response data buffer filled by host
+ * @resp_len: the size of @resp buffer filled by guest
+ */
+struct tsm_guest_req_info {
+	enum iommu_vdevice_tsm_guest_req_op op;
+	enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch;
+	sockptr_t req;
+	size_t req_len;
+	sockptr_t resp;
+	size_t resp_len;
+};iommufd_viommu_provider_ops
+#else
+struct tsm_guest_req_info;
+#endif
+
  #endif /* __TSM_H */
include/uapi/linux/iommufd.h +73 -0
index 0425d452d41e..43ed082a9421 100644
@@ -57,6 +57,7 @@ enum {
  	IOMMUFD_CMD_IOAS_CHANGE_PROCESS = 0x92,
  	IOMMUFD_CMD_VEVENTQ_ALLOC = 0x93,
  	IOMMUFD_CMD_HW_QUEUE_ALLOC = 0x94,
+	IOMMUFD_CMD_VDEVICE_TSM_REQ = 0x96,
  };
 
  /**
@@ -1351,4 +1352,76 @@ struct iommu_hw_queue_alloc {
  	__aligned_u64 length;
  };
  #define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC)
+
+/**
+ * enum iommu_vdevice_tsm_guest_tvm_arch - guest TVM architecture
+ * @IOMMU_VDEVICE_TSM_TVM_ARCH_CCA: Arm CCA TVM
+ * @IOMMU_VDEVICE_TSM_TVM_ARCH_SEV: AMD SEV TVM
+ * @IOMMU_VDEVICE_TSM_TVM_ARCH_TDX: Intel TDX TVM
+ */
+enum iommu_vdevice_tsm_guest_tvm_arch {
+	IOMMU_VDEVICE_TSM_TVM_ARCH_CCA = 1,
+	IOMMU_VDEVICE_TSM_TVM_ARCH_SEV,
+	IOMMU_VDEVICE_TSM_TVM_ARCH_TDX,
+};
+
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA
+ * @TSM_REQ_SEV_DISABLE_DMA: Disable SEV DMA
+ * @TSM_REQ_READ_OBJECT: Read a TSM object
+ * @TSM_REQ_REGEN_OBJECT: Regenerate a TSM object
+ * @TSM_REQ_OBJECT_INFO: Read TSM object information
+ */
+enum iommu_vdevice_tsm_guest_req_op {
+	TSM_REQ_VALIDATE_MMIO = 1,
+	TSM_REQ_SET_TDI_STATE,
+	TSM_REQ_SEV_ENABLE_DMA,
+	TSM_REQ_SEV_DISABLE_DMA,
+	TSM_REQ_READ_OBJECT,
+	TSM_REQ_REGEN_OBJECT,
+	TSM_REQ_OBJECT_INFO,
+};
+
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;
+};
+
+#define IOMMU_VDEVICE_TSM_REQ _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_REQ)
  #endif
Signature · 1 line
Alexey
#46 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 8 lines · Alexey Kardashevskiy
Quoted 5 lines · Aneesh Kumar K.V (Arm)

A VMM needs to forward guest-originated TSM commands to the TSM implementation managing an assigned device. The IOMMUFD vdevice identifies that device within its vIOMMU and provides the appropriate dispatch point.

Quoted 6 lines
Quoted 3 lines

Add IOMMU_VDEVICE_TSM_REQ to send an opaque request to a vdevice and optionally return a response. Define common operation and guest architecture identifiers for CCA, SEV and TDX requests.

iommufd_viommu_provider_ops already knows what architecture it is, why bother with these identifiers? Thanks,

At the uAPI boundary we want something like a strace to be able to dump and inspect the system call without having to inspect hidden knowledge of what is inside the FD. That's a huge PITA.

So saything 'this is an x86 format request that uses this coding' is the right thing.

Jason

maintainer
#47 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

In reply to Aneesh Kumar K.V (Arm)

drivers/iommu/iommufd/tsm.c line 19

Quoted 12 lines

[ ... 95 lines skipped ... ]

drivers/iommu/iommufd/tsm.c new file +11 -0
+static bool iommufd_vdevice_tsm_req_arch_valid(u32 tvm_arch)
+{
+	switch (tvm_arch) {
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX:
+		return true;
+	default:
+		return false;
+	}
+}

Generally speaking we should not do things like this, the viommu object should declare what it supports if we want to have validation on the iommufd side.

Quoted 9 lines

[ ... 140 lines skipped ... ]

include/linux/tsm.h +8 -0
+#ifdef CONFIG_TSM
+/**
+ * struct tsm_guest_req_info - parameters for a guest-initiated TSM request
+ * @op: operation for the guest-initiated request
+ * @tvm_arch: guest TVM architecture
+ * @req: request data buffer filled by guest
+ * @req_len: the size of @req filled by guest
+ * @resp: response data buffer filled by host

include/linux/tsm.h struct tsm_dev *find_tsm_dev(int id); · line 162

Quoted 10 lines
include/linux/tsm.h +10 -0
+ * @resp_len: the size of @resp buffer filled by guest
+ */
+struct tsm_guest_req_info {
+	enum iommu_vdevice_tsm_guest_req_op op;
+	enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch;
+	sockptr_t req;
+	size_t req_len;
+	sockptr_t resp;
+	size_t resp_len;
+};

Why is this struct in tsm land?

I'm not seeing why the iommufd interface should be locked to tsm, as I said other viommus need this kind of command channel too. Can't we have a general one?

Why would it ever not be tied to userspace pointers? I don't want an in kernel user ever using this kind of struct?

Quoted 6 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

That seems wrong.. The hypervisor should not have control over T=1 DMA.

Quoted 5 lines

+ * @TSM_REQ_SEV_DISABLE_DMA: Disable SEV DMA
+ * @TSM_REQ_READ_OBJECT: Read a TSM object
+ * @TSM_REQ_REGEN_OBJECT: Regenerate a TSM object
+ * @TSM_REQ_OBJECT_INFO: Read TSM object information
+ */

These operations will need alot more commentary. Use kdocs inside the enum below to make that easier.

Quoted 27 lines

[ ... 10 lines skipped ... ]

include/uapi/linux/iommufd.h +26 -0
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */

include/uapi/linux/iommufd.h struct iommu_hw_queue_alloc { · line 1423

Quoted 10 lines
include/uapi/linux/iommufd.h +10 -0
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;

out_tsm_code

Signature · 1 line
Jason
author maintainer
#48 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 39 lines · Jason Gunthorpe

drivers/iommu/iommufd/tsm.c line 19

Quoted 12 lines

[ ... 95 lines skipped ... ]

drivers/iommu/iommufd/tsm.c new file +11 -0
+static bool iommufd_vdevice_tsm_req_arch_valid(u32 tvm_arch)
+{
+	switch (tvm_arch) {
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX:
+		return true;
+	default:
+		return false;
+	}
+}

Generally speaking we should not do things like this, the viommu object should declare what it supports if we want to have validation on the iommufd side.

Quoted 9 lines

[ ... 140 lines skipped ... ]

include/linux/tsm.h +8 -0
+#ifdef CONFIG_TSM
+/**
+ * struct tsm_guest_req_info - parameters for a guest-initiated TSM request
+ * @op: operation for the guest-initiated request
+ * @tvm_arch: guest TVM architecture
+ * @req: request data buffer filled by guest
+ * @req_len: the size of @req filled by guest
+ * @resp: response data buffer filled by host

include/linux/tsm.h struct tsm_dev *find_tsm_dev(int id); · line 162

Quoted 10 lines
include/linux/tsm.h +10 -0
+ * @resp_len: the size of @resp buffer filled by guest
+ */
+struct tsm_guest_req_info {
+	enum iommu_vdevice_tsm_guest_req_op op;
+	enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch;
+	sockptr_t req;
+	size_t req_len;
+	sockptr_t resp;
+	size_t resp_len;
+};

Why is this struct in tsm land?

Quoted 7 lines

I'm not seeing why the iommufd interface should be locked to tsm, as I said other viommus need this kind of command channel too. Can't we have a general one?

Why would it ever not be tied to userspace pointers? I don't want an in kernel user ever using this kind of struct?

The previous discussion suggested that another kernel subsystem might need to use this low-level TSM interface, although no concrete example was identified. In other words, an opaque guest request could be issued from either userspace or kernel space.

Quoted 11 lines
Quoted 7 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

That seems wrong.. The hypervisor should not have control over T=1 DMA.

This was added specifically for AMD SEV, which requires an IOMMU-side update to enable DMA.

Quoted 10 lines
Quoted 6 lines

+ * @TSM_REQ_SEV_DISABLE_DMA: Disable SEV DMA
+ * @TSM_REQ_READ_OBJECT: Read a TSM object
+ * @TSM_REQ_REGEN_OBJECT: Regenerate a TSM object
+ * @TSM_REQ_OBJECT_INFO: Read TSM object information
+ */

These operations will need alot more commentary. Use kdocs inside the enum below to make that easier.

I will add kernel-doc comments for the operations used by CCA.

We have gone through several iterations to identify the guest passthrough request facility we need. IIUC, both TDX and CCA give the hypervisor some control over the request type, while SEV-TIO is more opaque.

Quoted 32 lines
Quoted 32 lines

[ ... 10 lines skipped ... ]

include/uapi/linux/iommufd.h +30 -0
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
Quoted 9 lines

include/uapi/linux/iommufd.h struct iommu_hw_queue_alloc { · line 1423

Quoted 6 lines
include/uapi/linux/iommufd.h +6 -0
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;

out_tsm_code

That is required for SEV-TIO.

-aneesh

maintainer
#49 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 2 lines · Aneesh Kumar K.V

Jason Gunthorpe <[email protected]> writes:

.../...

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

Quoted 3 lines
Quoted 3 lines

That seems wrong.. The hypervisor should not have control over T=1 DMA.

(Still catching up with entire thread)

In case of AMD IOMMU, before allowing DMA from secure guest, we have to update IOMMU host (nested) page table entry size to match the RMP table.

-Vasant

Quoted 69 lines

This was added specifically for AMD SEV, which requires an IOMMU-side update to enable DMA.

Quoted 10 lines
Quoted 6 lines

+ * @TSM_REQ_SEV_DISABLE_DMA: Disable SEV DMA
+ * @TSM_REQ_READ_OBJECT: Read a TSM object
+ * @TSM_REQ_REGEN_OBJECT: Regenerate a TSM object
+ * @TSM_REQ_OBJECT_INFO: Read TSM object information
+ */

These operations will need alot more commentary. Use kdocs inside the enum below to make that easier.

I will add kernel-doc comments for the operations used by CCA.

We have gone through several iterations to identify the guest passthrough request facility we need. IIUC, both TDX and CCA give the hypervisor some control over the request type, while SEV-TIO is more opaque.

Quoted 32 lines
Quoted 32 lines

[ ... 10 lines skipped ... ]

include/uapi/linux/iommufd.h +30 -0
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
Quoted 9 lines

include/uapi/linux/iommufd.h struct iommu_hw_queue_alloc { · line 1423

Quoted 6 lines
include/uapi/linux/iommufd.h +6 -0
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;

out_tsm_code

That is required for SEV-TIO.

-aneesh

maintainer
#50 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 7 lines · Vasant Hegde
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

That seems wrong.. The hypervisor should not have control over T=1 DMA.

(Still catching up with entire thread)

In case of AMD IOMMU, before allowing DMA from secure guest, we have to update IOMMU host (nested) page table entry size to match the RMP table.

Okay, but that is not "Enable SEV DMA" that is "something something RMP"

I do not like these obfuscated names that are still architecture specific behaviors but obscure what the actual spec defined underlying action is.

If AMD needs some RMP maintinance then have a clearly named RMP maintainence op that links back to the specification documentation.

I've never liked this approach since I saw the first iommufd patches.

iommufd, philisophically, is about providing truely general APIs that can work for everyone, and very device specific APIs that directly work the device's interface in the natural device specific way according to their specs.

So I would hope to see some general APIs for the SPDM related stuff, I think everyone has that and it should work in exactly the same way? Or not?

Everything else should point down to arch specific stuff. Like if we need to do some RMI_VDEV_XX then it should be called that here, not obfuscated.

Jason

maintainer
#51 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

In reply to Aneesh Kumar K.V
Quoted 7 lines · Aneesh Kumar K.V
Quoted 2 lines

Why would it ever not be tied to userspace pointers? I don't want an in kernel user ever using this kind of struct?

The previous discussion suggested that another kernel subsystem might need to use this low-level TSM interface, although no concrete example was identified. In other words, an opaque guest request could be issued from either userspace or kernel space.

Don't do it without a user then.

Directly coupling KVM to iommufd is some other future topic. I don't think KVM should be coupled to TSM.

Quoted 5 lines
Quoted 2 lines

That seems wrong.. The hypervisor should not have control over T=1 DMA.

This was added specifically for AMD SEV, which requires an IOMMU-side update to enable DMA.

See my remarks to Vasant. I want to take a very careful look at this list eventually, but for now lets focus on the other parts and keep this seperate.

Quoted 4 lines

We have gone through several iterations to identify the guest passthrough request facility we need. IIUC, both TDX and CCA give the hypervisor some control over the request type, while SEV-TIO is more opaque.

Is there a record? I would be interested to read a summary

Maybe you can summarize the details in the comments. Eg define exactly what spec operation each arch will implement under every proposed call?

Quoted 40 lines
Quoted 30 lines
Quoted 30 lines
include/uapi/linux/iommufd.h +30 -0
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
Quoted 9 lines

include/uapi/linux/iommufd.h struct iommu_hw_queue_alloc { · line 1423

Quoted 6 lines
include/uapi/linux/iommufd.h +6 -0
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;

out_tsm_code

Quoted 1 line

That is required for SEV-TIO.

I mean spell it 'out_tsm_code' since it is written as an output, that is the convention in iommufd structs

Separately I also don't know why we'd need it since we have an entire resp_uptr. If the arch specific action needs an arch specific code, it can go in the arch specific resp struct.

Jason

#52 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

In reply to Jason Gunthorpe
Quoted 39 lines · Jason Gunthorpe

drivers/iommu/iommufd/tsm.c line 19

Quoted 12 lines

[ ... 95 lines skipped ... ]

drivers/iommu/iommufd/tsm.c new file +11 -0
+static bool iommufd_vdevice_tsm_req_arch_valid(u32 tvm_arch)
+{
+	switch (tvm_arch) {
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV:
+	case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX:
+		return true;
+	default:
+		return false;
+	}
+}

Generally speaking we should not do things like this, the viommu object should declare what it supports if we want to have validation on the iommufd side.

Quoted 9 lines

[ ... 140 lines skipped ... ]

include/linux/tsm.h +8 -0
+#ifdef CONFIG_TSM
+/**
+ * struct tsm_guest_req_info - parameters for a guest-initiated TSM request
+ * @op: operation for the guest-initiated request
+ * @tvm_arch: guest TVM architecture
+ * @req: request data buffer filled by guest
+ * @req_len: the size of @req filled by guest
+ * @resp: response data buffer filled by host

include/linux/tsm.h struct tsm_dev *find_tsm_dev(int id); · line 162

Quoted 10 lines
include/linux/tsm.h +10 -0
+ * @resp_len: the size of @resp buffer filled by guest
+ */
+struct tsm_guest_req_info {
+	enum iommu_vdevice_tsm_guest_req_op op;
+	enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch;
+	sockptr_t req;
+	size_t req_len;
+	sockptr_t resp;
+	size_t resp_len;
+};

Why is this struct in tsm land?

Quoted 6 lines

I'm not seeing why the iommufd interface should be locked to tsm, as I said other viommus need this kind of command channel too. Can't we have a general one?

Why would it ever not be tied to userspace pointers? I don't want an in kernel user ever using this kind of struct?

This mimics the TSM's pci_tsm_guest_req() API which it is a kernel component we did not want to hardcode to use userspace pointers as it does not have uAPI. Makes no sense in tsm_guest_req_info though.

Quoted 9 lines
Quoted 6 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

That seems wrong.. The hypervisor should not have control over T=1 DMA.

This is a guest request to the PSP which does control T=1, the HV is forwarding it and does some extra stuff while there. Probably could be decoupled and go to a separate ioctl().

The HV does not control DMA, it is for configuring DTE in a specific way - the domainid becomes reserved (and its sDTE counterpart starts working) and some other DTE bits have to match whatever the PSP writes to sDTE ("I" == IOTLB enable) and probably more.

And then, yeah, "something something RMP" == cut 2MB mappings if corresponding RMP is not 2MB.

Thanks,

Quoted 50 lines
Quoted 6 lines

+ * @TSM_REQ_SEV_DISABLE_DMA: Disable SEV DMA
+ * @TSM_REQ_READ_OBJECT: Read a TSM object
+ * @TSM_REQ_REGEN_OBJECT: Regenerate a TSM object
+ * @TSM_REQ_OBJECT_INFO: Read TSM object information
+ */

These operations will need alot more commentary. Use kdocs inside the enum below to make that easier.

Quoted 27 lines

[ ... 10 lines skipped ... ]

include/uapi/linux/iommufd.h +26 -0
+/**
+ * struct iommu_vdevice_tsm_req - ioctl(IOMMU_VDEVICE_TSM_REQ)
+ * @size: sizeof(struct iommu_vdevice_tsm_req)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @op: One of enum iommu_vdevice_tsm_guest_req_op
+ * @tvm_arch: One of enum iommu_vdevice_tsm_guest_tvm_arch
+ * @req_len: Size in bytes of the input payload at @req_uptr
+ * @resp_len: Size in bytes of the output buffer at @resp_uptr
+ * @req_uptr: Userspace pointer to the guest-provided request payload
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ * @tsm_code: TSM-specific result code returned by the TSM implementation
+ *
+ * Forward a TSM request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The request operation is guest initiated. The TSM backend validates
+ * @tvm_arch against its bound TVM architecture assumptions.
+ *
+ * The request payload is read from @req_uptr/@req_len. If a response is
+ * expected, userspace provides @resp_uptr/@resp_len as writable storage for
+ * response bytes returned by the TSM path.
+ *
+ * The ioctl is only suitable for commands and results that the host kernel
+ * has no use, the host is only facilitating guest to TSM communication.
+ */

include/uapi/linux/iommufd.h struct iommu_hw_queue_alloc { · line 1423

Quoted 10 lines
include/uapi/linux/iommufd.h +10 -0
+struct iommu_vdevice_tsm_req {
+	__u32 size;
+	__u32 vdevice_id;
+	__u32 op;
+	__u32 tvm_arch;
+	__u32 req_len;
+	__u32 resp_len;
+	__aligned_u64 req_uptr;
+	__aligned_u64 resp_uptr;
+	__aligned_u64 tsm_code;

out_tsm_code

Signature · 1 line
Alexey
maintainer
#53 lore raw

Re: [RFC PATCH v6 09/11] iommufd: Add the vdevice TSM request ioctl

Quoted 7 lines · Alexey Kardashevskiy
Quoted 7 lines
Quoted 6 lines

[ ... 34 lines skipped ... ]
+/**
+ * enum iommu_vdevice_tsm_guest_req_op - operation for guest TSM requests
+ * @TSM_REQ_VALIDATE_MMIO: Validate MMIO for the TDI
+ * @TSM_REQ_SET_TDI_STATE: Set TDI state
+ * @TSM_REQ_SEV_ENABLE_DMA: Enable SEV DMA

Quoted 6 lines
Quoted 2 lines

That seems wrong.. The hypervisor should not have control over T=1 DMA.

This is a guest request to the PSP which does control T=1, the HV is forwarding it and does some extra stuff while there. Probably could be decoupled and go to a separate ioctl().

Presumably the guest request is encrypted/authenticated somehow? Otherwise it is security broken if the host can forge a T=1 control.

If that's so this should just be some AMD_PSP_AUTHENTICATED_COMMAND it doesn't need more detail?

Also, reminder, since none of this is actually multi-vendor before anything goes into the uapi header it needs a concrete public spec reference to what exactly it is carrying through the uAPI.

Jason

author maintainer
#54 lore raw

RFC PATCH v6 10/11 PCI/TSM: Remove the legacy guest request interface

In reply to Aneesh Kumar K.V (Arm)

Guest TSM requests can now be dispatched through the IOMMUFD vdevice operation. Remove the PCI-device-based guest request entry point, its scope enum and the corresponding PCI/TSM driver callback.

Remove the sample guest request callback and tsm_request attribute along with the API.

---
 drivers/pci/tsm/core.c    | 60 --------------------------------------
 include/linux/pci-tsm.h   | 61 +--------------------------------------
 samples/devsec/link_tsm.c | 52 +--------------------------------
 3 files changed, 2 insertions(+), 171 deletions(-)
drivers/pci/tsm/core.c +0 -60
index 7082fdbe8c7e..c7e0d241e55d 100644
@@ -398,66 +398,6 @@ int pci_tsm_bind(struct pci_dev *pdev, struct kvm *kvm, u32 tdi_id)
 }
 EXPORT_SYMBOL_GPL(pci_tsm_bind);
 
-/**
- * pci_tsm_guest_req() - helper to marshal guest requests to the TSM driver
- * @pdev: @pdev representing a bound tdi
- * @scope: caller asserts this passthrough request is limited to TDISP operations
- * @req_in: Input payload forwarded from the guest
- * @in_len: Length of @req_in
- * @req_out: Output payload buffer response to the guest
- * @out_len: Length of @req_out on input, bytes filled in @req_out on output
- * @tsm_code: Optional TSM arch specific result code for the guest TSM
- *
- * This is a common entry point for requests triggered by userspace KVM-exit
- * service handlers responding to TDI information or state change requests. The
- * scope parameter limits requests to TDISP state management, or limited debug.
- * This path is only suitable for commands and results that are the host kernel
- * has no use, the host is only facilitating guest to TSM communication.
- *
- * Returns 0 on success and -error on failure and positive "residue" on success
- * but @req_out is filled with less then @out_len, or @req_out is NULL and a
- * residue number of bytes were not consumed from @req_in.  On success or
- * failure @tsm_code may be populated with a TSM implementation specific result
- * code for the guest to consume.
- *
- * Context: Caller is responsible for calling this within the pci_tsm_bind()
- * state of the TDI.
- */
-ssize_t pci_tsm_guest_req(struct pci_dev *pdev, enum pci_tsm_req_scope scope,
-			  sockptr_t req_in, size_t in_len, sockptr_t req_out,
-			  size_t out_len, u64 *tsm_code)
-{
-	struct pci_tsm_pf0 *tsm_pf0;
-	struct pci_tdi *tdi;
-	int rc;
-
-	/* Forbid requests that are not directly related to TDISP operations */
-	if (scope > PCI_TSM_REQ_STATE_CHANGE)
-		return -EINVAL;
-
-	ACQUIRE(rwsem_read_intr, lock)(&pci_tsm_rwsem);
-	if ((rc = ACQUIRE_ERR(rwsem_read_intr, &lock)))
-		return rc;
-
-	if (!pdev->tsm)
-		return -ENXIO;
-
-	if (!is_link_tsm(pdev->tsm->tsm_dev))
-		return -ENXIO;
-
-	tsm_pf0 = to_pci_tsm_pf0(pdev->tsm);
-	ACQUIRE(mutex_intr, ops_lock)(&tsm_pf0->lock);
-	if ((rc = ACQUIRE_ERR(mutex_intr, &ops_lock)))
-		return rc;
-
-	tdi = pdev->tsm->tdi;
-	if (!tdi)
-		return -ENXIO;
-	return to_pci_tsm_ops(pdev->tsm)->guest_req(tdi, scope, req_in, in_len,
-						    req_out, out_len, tsm_code);
-}
-EXPORT_SYMBOL_GPL(pci_tsm_guest_req);
-
 static void pci_tsm_unbind_all(struct pci_dev *pdev)
 {
 	pci_tsm_walk_fns_reverse(pdev, __pci_tsm_unbind, NULL);
include/linux/pci-tsm.h +1 -60

109 lines not on this page · raw message

samples/devsec/link_tsm.c +1 -51

76 lines not on this page · raw message

Signature · 1 line
2.43.0
author maintainer
#55 lore raw

RFC PATCH v6 11/11 PCI/TSM: Add reference-counted contexts for vdevice providers

In reply to Aneesh Kumar K.V (Arm)

Replace the PCI/TSM bind/unbind interface with reference-counted contexts that retain the function, DSM device and Link TSM for a vdevice provider. Reject disconnect while a context is live.

Keep the existing bound sysfs attribute and derive its status from successful provider binding, with balanced per-function accounting on context release. Update the ABI documentation and retire the obsolete sample bind/unbind operations in the same patch.

---
 Documentation/ABI/testing/sysfs-bus-pci |  29 ++-
 drivers/pci/tsm/core.c                  | 256 +++++++++++++-----------
 include/linux/pci-tsm.h                 |  85 +++++---
 samples/devsec/link_tsm.c               |  96 ---------
 tools/testing/devsec/devsec.sh          |  27 +--
 5 files changed, 211 insertions(+), 282 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +18 -11
index c2a5c4fe9373..5288ea4ed343 100644
@@ -657,6 +657,9 @@ Description:
 		to 'connect' to teardown the connection. This is a
 		"link" TSM attribute, see
 		Documentation/ABI/testing/sysfs-class-tsm.
+		The write fails with EBUSY while any vdevice depends on the
+		connection. Userspace must destroy those vdevices before
+		disconnecting the link.
 
 What:		/sys/bus/pci/devices/.../tsm/dsm
 Contact:	[email protected]
@@ -676,17 +679,21 @@ Description:	(RO) Return PCI device name of this device's DSM (Device
 
 What:		/sys/bus/pci/devices/.../tsm/bound
 Contact:	[email protected]
-Description:	(RO) Return the device name of the TSM when the device is in a
-		TDISP (TEE Device Interface Security Protocol) operational state
-		(LOCKED, RUN, or ERROR, not UNLOCKED). Bound devices consume
-		platform TSM resources and depend on the device's configuration
-		(e.g. BME (Bus Master Enable) and MSE (Memory Space Enable)
-		among other settings) to remain stable for the duration of the
-		bound state. This attribute is only visible for devices that
-		support TDISP operation, and it is only populated after
-		successful connect and TSM bind. The TSM bind operation is
-		initiated by VFIO/IOMMUFD. This is a "link" TSM attribute, see
-		Documentation/ABI/testing/sysfs-class-tsm.
+Description:	(RO) Return the device name of the TSM when this PCI function
+		has a successfully initialized TSM-backed vdevice binding, or
+		an empty line when no such binding exists. The binding is
+		established through VFIO/IOMMUFD and remains visible until
+		the provider releases its context during vdevice teardown.
+		Merely connecting the device to a TSM or acquiring a context
+		does not establish a binding. Bindings of other functions
+		managed by the same DSM do not affect this attribute.
+
+		This reports the binding lifetime, not the current TDISP
+		(TEE Device Interface Security Protocol) state. A bound vdevice
+		may be UNLOCKED, and TDISP lock/unlock transitions do not
+		change this attribute. This attribute is only visible for
+		devices that support TDISP operation. This is a "link" TSM
+		attribute, see Documentation/ABI/testing/sysfs-class-tsm.
 
 What:		/sys/bus/pci/devices/.../authenticated
 Contact:	[email protected]
drivers/pci/tsm/core.c +137 -119

366 lines not on this page · raw message

include/linux/pci-tsm.h +55 -30

159 lines not on this page · raw message

samples/devsec/link_tsm.c +0 -96

120 lines not on this page · raw message

tools/testing/devsec/devsec.sh +1 -26
index 6a9313e7104f..159b1a6fb133 100755
@@ -94,14 +94,10 @@ validate_disconnected() {
 	fn_dev=${FN_DEVS[$1]}
 	host_bridge=$(dirname $(dirname $(readlink -f $pci_dev)))
 
-	# validate that the dsm is not yet detected and that the sub-function
-	# is aware of any TSM capabilities
+	# validate that the dsm is not yet detected
 	dsm=$(cat $pci_dev/tsm/dsm) || err "$LINENO from $2"
-	bound=$(cat $pci_dev/tsm/bound) || err "$LINENO from $2"
 	[[ -z $dsm ]] || err "$LINENO from $2"
-	[[ -z $bound ]] || err "$LINENO from $2"
 	[[ ! -e $fn_dev/tsm/dsm ]] || err "$LINENO from $2"
-	[[ ! -e $fn_dev/tsm/bound ]] || err "$LINENO from $2"
 	[[ ! -e $fn_dev/tsm/connect ]] || err "$LINENO from $2"
 	[[ ! -e $fn_dev/tsm/disconnect ]] || err "$LINENO from $2"
 }
@@ -197,27 +193,6 @@ ide_test() {
 
 	check_evidence $pci_dev
 
-	# bind both functions and validate that they display bound to
-	# the TSM device
-	echo $(basename $pci_dev) > $tsm_link/device/tsm_bind
-	bound=$(cat $pci_dev/tsm/bound)
-	[[ $bound == $(basename $tsm_link) ]] || err "$LINENO"
-	echo $(basename $fn_dev) > $tsm_link/device/tsm_bind
-	bound=$(cat $fn_dev/tsm/bound)
-	[[ $bound == $(basename $tsm_link) ]] || err "$LINENO"
-
-	# test manual unbind
-	echo $(basename $pci_dev) > $tsm_link/device/tsm_unbind
-	bound=$(cat $pci_dev/tsm/bound)
-	[[ -z $bound ]] || err "$LINENO"
-	echo $(basename $fn_dev) > $tsm_link/device/tsm_unbind
-	bound=$(cat $fn_dev/tsm/bound)
-	[[ -z $bound ]] || err "$LINENO"
-
-	# rebind to test automatic unbind at disconnect
-	echo $(basename $pci_dev) > $tsm_link/device/tsm_bind
-	echo $(basename $fn_dev) > $tsm_link/device/tsm_bind
-
 	# check that the links disappear at disconnect and the stream
 	# pool is refilled
 	echo $(basename $tsm_link) > $pci_dev/tsm/disconnect
Signature · 1 line
2.43.0
maintainer
#56 lore raw

RE: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

Quoted 2 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

[...]

Quoted 5 lines
drivers/pci/tsm/core.c +5 -0
+	pf0 = to_pci_tsm_pf0(pdev->tsm);
+	if (!pf0)
+		return ERR_PTR(-ENXIO);
+
+	context = kzalloc_obj(*context);

drivers/pci/tsm/core.c static int remove_fn(struct pci_dev *pdev, void *data) · line 357

Quoted 10 lines
drivers/pci/tsm/core.c +10 -0
+	if (!context)
+		return ERR_PTR(-ENOMEM);
+
+	guard(mutex)(&pf0->lock);
+	pf0->context_users++;
+	context->pf0 = pf0;
+	context->pdev = pci_dev_get(pdev);
+	INIT_LIST_HEAD(&context->bound_node);
+	context->dsm_dev = pci_dev_get(pf0->base_tsm.pdev);
+	tsm_device = get_device(&pdev->tsm->tsm_dev->dev);

is it redundant? Ideally we just need hold one reference when initializing the vIOMMU...

btw if it's indeed required, use tsm_get() instead?

maintainer
#57 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

In reply to Aneesh Kumar K.V (Arm)
Quoted 21 lines

[ ... 32 lines skipped ... ]

Documentation/ABI/testing/sysfs-bus-pci +5 -11
@@ -676,17 +679,21 @@ Description:	(RO) Return PCI device name of this device's DSM (Device
 
 What:		/sys/bus/pci/devices/.../tsm/bound
 Contact:	[email protected]
-Description:	(RO) Return the device name of the TSM when the device is in a
-		TDISP (TEE Device Interface Security Protocol) operational state
-		(LOCKED, RUN, or ERROR, not UNLOCKED). Bound devices consume
-		platform TSM resources and depend on the device's configuration
-		(e.g. BME (Bus Master Enable) and MSE (Memory Space Enable)
-		among other settings) to remain stable for the duration of the
-		bound state. This attribute is only visible for devices that
-		support TDISP operation, and it is only populated after
-		successful connect and TSM bind. The TSM bind operation is
-		initiated by VFIO/IOMMUFD. This is a "link" TSM attribute, see
-		Documentation/ABI/testing/sysfs-class-tsm.
+Description:	(RO) Return the device name of the TSM when this PCI function
+		has a successfully initialized TSM-backed vdevice binding, or
+		an empty line when no such binding exists. The binding is
+		established through VFIO/IOMMUFD and remains visible until
+		the provider releases its context during vdevice teardown.

Documentation/ABI/testing/sysfs-bus-pci Description: (RO) Return PCI device name of this device's DSM (Device · line 696

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +10 -0
+		Merely connecting the device to a TSM or acquiring a context
+		does not establish a binding. Bindings of other functions
+		managed by the same DSM do not affect this attribute.
+
+		This reports the binding lifetime, not the current TDISP
+		(TEE Device Interface Security Protocol) state. A bound vdevice
+		may be UNLOCKED, and TDISP lock/unlock transitions do not
+		change this attribute. This attribute is only visible for
+		devices that support TDISP operation. This is a "link" TSM
+		attribute, see Documentation/ABI/testing/sysfs-class-tsm.

Should we just delete this sysfs instead of torturing the code to implement it?

Why would anyone ever read it? What purpose would it serve to read it?

Leaking the iommufd vdevice outside the iommufd world is a horrible idea from a lifetime perspective. We should only do it with an incredibly strong reason. A debugging sysfs should be deleted. We can create a debugfs around the viommu if that is really important, somehow I doubt it is..

Signature · 1 line
Jason
author maintainer
#58 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

Quoted 35 lines · Jason Gunthorpe
Quoted 21 lines

[ ... 32 lines skipped ... ]

Documentation/ABI/testing/sysfs-bus-pci +5 -11
@@ -676,17 +679,21 @@ Description:	(RO) Return PCI device name of this device's DSM (Device
 
 What:		/sys/bus/pci/devices/.../tsm/bound
 Contact:	[email protected]
-Description:	(RO) Return the device name of the TSM when the device is in a
-		TDISP (TEE Device Interface Security Protocol) operational state
-		(LOCKED, RUN, or ERROR, not UNLOCKED). Bound devices consume
-		platform TSM resources and depend on the device's configuration
-		(e.g. BME (Bus Master Enable) and MSE (Memory Space Enable)
-		among other settings) to remain stable for the duration of the
-		bound state. This attribute is only visible for devices that
-		support TDISP operation, and it is only populated after
-		successful connect and TSM bind. The TSM bind operation is
-		initiated by VFIO/IOMMUFD. This is a "link" TSM attribute, see
-		Documentation/ABI/testing/sysfs-class-tsm.
+Description:	(RO) Return the device name of the TSM when this PCI function
+		has a successfully initialized TSM-backed vdevice binding, or
+		an empty line when no such binding exists. The binding is
+		established through VFIO/IOMMUFD and remains visible until
+		the provider releases its context during vdevice teardown.

Documentation/ABI/testing/sysfs-bus-pci Description: (RO) Return PCI device name of this device's DSM (Device · line 696

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-pci +10 -0
+		Merely connecting the device to a TSM or acquiring a context
+		does not establish a binding. Bindings of other functions
+		managed by the same DSM do not affect this attribute.
+
+		This reports the binding lifetime, not the current TDISP
+		(TEE Device Interface Security Protocol) state. A bound vdevice
+		may be UNLOCKED, and TDISP lock/unlock transitions do not
+		change this attribute. This attribute is only visible for
+		devices that support TDISP operation. This is a "link" TSM
+		attribute, see Documentation/ABI/testing/sysfs-class-tsm.

Should we just delete this sysfs instead of torturing the code to implement it?

Quoted 7 lines

Why would anyone ever read it? What purpose would it serve to read it?

Leaking the iommufd vdevice outside the iommufd world is a horrible idea from a lifetime perspective. We should only do it with an incredibly strong reason. A debugging sysfs should be deleted. We can create a debugfs around the viommu if that is really important, somehow I doubt it is..

OK, I will drop support for the "tsm/bound" sysfs attribute.

-aneesh

#59 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

In reply to Aneesh Kumar K.V (Arm)

drivers/pci/tsm/core.c static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev) · line 1292

Quoted 6 lines · Aneesh Kumar K.V (Arm)
drivers/pci/tsm/core.c +6 -0
+		if (is_pci_tsm_pf0(pdev)) {
+			if (pci_tsm_disconnect(pdev))
+				pci_warn(pdev, "TSM connection is still in use\n");
+		} else {
+			tsm_remove(pdev->tsm);
+		}

What happens if the PCI device is removed (e.g. sysfs remove, surprise hot-unplug) while the vDEVICE/TDI still exists?
Since removal cannot be refused, should the remove path still force the unbind/unlock?

Thanks,
Sonang Patel

#60 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

Quoted 12 lines · Sonang Patel

drivers/pci/tsm/core.c static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev) · line 1292

Quoted 6 lines · Aneesh Kumar K.V (Arm)
drivers/pci/tsm/core.c +6 -0
+		if (is_pci_tsm_pf0(pdev)) {
+			if (pci_tsm_disconnect(pdev))
+				pci_warn(pdev, "TSM connection is still in use\n");
+		} else {
+			tsm_remove(pdev->tsm);
+		}

What happens if the PCI device is removed (e.g. sysfs remove, surprise hot-unplug) while the vDEVICE/TDI still exists?
Since removal cannot be refused, should the remove path still force the unbind/unlock?

vfio prevents that. It currently will block the sysfs remove until vfio is closed.

If we ever decide to fix that then vfio would have to tear down the iommufd vdevice before allowing itself to be destroyed.

We don't need any lifetime nonsense once we are inside an iommufd context, its existing locking scheme is very strong already. tsm should not be allowed to change while a driver is bound, and basically I shouldn't see any refcounting or locking in any of these paths stemming from a bound driver context in iommufd.

Jason

author maintainer
#61 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

Quoted 14 lines · Jason Gunthorpe
Quoted 12 lines · Sonang Patel

drivers/pci/tsm/core.c static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev) · line 1292

Quoted 6 lines · Aneesh Kumar K.V (Arm)
drivers/pci/tsm/core.c +6 -0
+		if (is_pci_tsm_pf0(pdev)) {
+			if (pci_tsm_disconnect(pdev))
+				pci_warn(pdev, "TSM connection is still in use\n");
+		} else {
+			tsm_remove(pdev->tsm);
+		}

What happens if the PCI device is removed (e.g. sysfs remove, surprise hot-unplug) while the vDEVICE/TDI still exists?
Since removal cannot be refused, should the remove path still force the unbind/unlock?

Quoted 3 lines

vfio prevents that. It currently will block the sysfs remove until vfio is closed.

This came up during a Codex review of my patch set. The question is what happens if function 1 is assigned to the guest and function 0 is then removed through sysfs. Is that allowed? If so, it could be a problem because function 1 is not unbound and can continue to be used, while removing function 0 tears down DOE and IDE via:

	pci_doe_destroy(dev);
	pci_ide_destroy(dev);
Quoted 10 lines

If we ever decide to fix that then vfio would have to tear down the iommufd vdevice before allowing itself to be destroyed.

We don't need any lifetime nonsense once we are inside an iommufd context, its existing locking scheme is very strong already. tsm should not be allowed to change while a driver is bound, and basically I shouldn't see any refcounting or locking in any of these paths stemming from a bound driver context in iommufd.

-aneesh

#62 lore raw

Re: [RFC PATCH v6 11/11] PCI/TSM: Add reference-counted contexts for vdevice providers

Quoted 16 lines · Aneesh Kumar K.V
Quoted 14 lines · Jason Gunthorpe
Quoted 12 lines · Sonang Patel

drivers/pci/tsm/core.c static void __pci_tsm_destroy(struct pci_dev *pdev, struct tsm_dev *tsm_dev) · line 1292

Quoted 6 lines · Aneesh Kumar K.V (Arm)
drivers/pci/tsm/core.c +6 -0
+		if (is_pci_tsm_pf0(pdev)) {
+			if (pci_tsm_disconnect(pdev))
+				pci_warn(pdev, "TSM connection is still in use\n");
+		} else {
+			tsm_remove(pdev->tsm);
+		}

What happens if the PCI device is removed (e.g. sysfs remove, surprise hot-unplug) while the vDEVICE/TDI still exists?
Since removal cannot be refused, should the remove path still force the unbind/unlock?

Quoted 6 lines
Quoted 2 lines

vfio prevents that. It currently will block the sysfs remove until vfio is closed.

This came up during a Codex review of my patch set. The question is what happens if function 1 is assigned to the guest and function 0 is then removed through sysfs. Is that allowed?

It can happen and I don't know we can do anything to prevent it.

Quoted 6 lines

If so, it could be a problem because function 1 is not unbound and can continue to be used, while
removing function 0 tears down DOE and IDE via:

	pci_doe_destroy(dev);
	pci_ide_destroy(dev);

Ah, PCI sig keeps on giving..

The actual measurements are per function but the SPDM channel is per device!

At least for RMM is this a problem? It looks like the spec handles this case the function 0 destroys IDE and commands related to the other functions will just start failing.

So I would expect the OS implementation here to have similar properties that even though function 0 is gone the other functions still 'work' in that they get the RMM defined failures for these conditions.

Basically, this shouldn't result in a TSM hot unplug of function 1.. Trying to do multi-device operations is always a locking mess it should be avoided.

Jason

author maintainer
#63 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 14 lines · Aneesh Kumar K.V (Arm)

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It groups the vIOMMU operations with their module owner and private data, and makes that implementation discoverable during vIOMMU allocation.

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

Quoted 7 lines

Guest TSM requests are dispatched through the vdevice. PCI/TSM uses reference-counted contexts to retain the resources needed by providers, without introducing separate IOMMUFD TSM bind or unbind ioctls.

Note: Codex was used to assist with commit message formatting and code rearrangement.

[ ... 99 lines skipped ... ]

Related Arm CCA host changes is below

commit 66225895e48fa38b13aea493e1e6e3babab8f6aa

Author: Aneesh Kumar K.V (Arm) <[email protected]>
Date:   Wed Sep 16 14:06:15 2026 +0530

virt: arm-cca-host: Register the Realm vIOMMU provider with IOMMUFD

Register the Realm SMMUv3 provider directly with IOMMUFD. Retain active TSM resources until the final provider release and remove the registration before TSM teardown.

Create CCA-owned vdevices using PCI/TSM contexts, balance pSMMU stream- table lifetime, and mark a function bound only after successful initialization. Keep the KVM file and active pSMMU alive through the vIOMMU lifetime.

drivers/virt/coco/arm-cca-host/Kconfig +1 -0
index 4a2bc64c2568..d0c636f92d9e 100644
@@ -7,6 +7,7 @@ config ARM_CCA_HOST
 	depends on ARM64
 	depends on PCI
 	depends on ARM_RMM
+	depends on IOMMUFD
 	depends on HAVE_ARM_SMCCC_DISCOVERY
 	select PCI_TSM
 	select KEYS
drivers/virt/coco/arm-cca-host/Makefile +1 -1
index d48e8940af46..2732352a7ae2 100644
@@ -2,4 +2,4 @@
 #
 obj-$(CONFIG_ARM_CCA_HOST) += arm-cca-host.o
 
-arm-cca-host-y	+=  arm-cca.o rmi-da.o
+arm-cca-host-y	+=  arm-cca.o rmi-da.o viommu.o
drivers/virt/coco/arm-cca-host/arm-cca.c +37 -2
index 182733e00dfc..b5d50685eaeb 100644
@@ -5,6 +5,7 @@
 
 #include <linux/arm-smccc.h>
 #include <linux/arm-smccc-bus.h>
+#include <linux/iommufd.h>
 #include <linux/pci-tsm.h>
 #include <linux/pci-ide.h>
 #include <linux/module.h>
@@ -427,6 +428,24 @@ static struct pci_tsm_ops cca_link_pci_ops = {
 	.disconnect = cca_tsm_disconnect,
 };
 
+static void cca_viommu_provider_release(void *data)
+{
+	tsm_put(data);
+}
+
+static const struct iommufd_viommu_provider_ops cca_viommu_provider_ops = {
+	.owner = THIS_MODULE,
+	.type = IOMMU_VIOMMU_TYPE_ARM_REALM_SMMUV3,
+	.get_size = cca_viommu_get_size,
+	.init = cca_viommu_init,
+	.release = cca_viommu_provider_release,
+};
+
+static void cca_viommu_provider_remove(void *provider)
+{
+	iommufd_unregister_viommu_provider(provider);
+}
+
 static void cca_link_tsm_remove(void *tsm_dev)
 {
 	tsm_unregister(tsm_dev);
@@ -439,7 +458,9 @@ static bool rmi_has_reg2_feature(unsigned long feature)
 
 static int cca_link_tsm_probe(struct arm_smccc_device *sdev)
 {
+	struct iommufd_viommu_provider *provider;
 	struct tsm_dev *tsm_dev;
+	int ret;
 
 	if (!rmi_has_reg2_feature(RMI_FEATURE_REGISTER_2_DA))
 		return -ENODEV;
@@ -448,8 +469,22 @@ static int cca_link_tsm_probe(struct arm_smccc_device *sdev)
 	if (IS_ERR(tsm_dev))
 		return PTR_ERR(tsm_dev);
 
-	return devm_add_action_or_reset(&sdev->dev, cca_link_tsm_remove,
-					tsm_dev);
+	ret = devm_add_action_or_reset(&sdev->dev, cca_link_tsm_remove, tsm_dev);
+	if (ret)
+		return ret;
+
+	/* Retain PCI/TSM resources until the last provider user is released. */
+	tsm_get(tsm_dev);
+	provider = iommufd_register_viommu_provider(&cca_viommu_provider_ops,
+						  tsm_dev);
+	if (IS_ERR(provider)) {
+		tsm_put(tsm_dev);
+		return PTR_ERR(provider);
+	}
+
+	/* Remove from lookup before the earlier TSM unregister action runs. */
+	return devm_add_action_or_reset(&sdev->dev, cca_viommu_provider_remove,
+					provider);
 }
 
 static const struct arm_smccc_device_id cca_link_tsm_id_table[] = {
drivers/virt/coco/arm-cca-host/rmi-da.h +10 -0
index 306f64749f26..07642eeb3245 100644
@@ -16,6 +16,11 @@
 #include <linux/atomic.h>
 #include <linux/kref.h>
 #include <linux/wait.h>
+#include <uapi/linux/iommufd.h>
+
+struct iommufd_viommu;
+struct iommu_domain;
+struct iommu_user_data;
 
 #define MAX_CACHE_OBJ_SIZE	SZ_16M
 #define CACHE_CHUNK_SIZE	SZ_4K
@@ -119,6 +124,11 @@ struct cca_host_fn_dsc {
 	struct pci_tsm pci;
 };
 
+size_t cca_viommu_get_size(struct device *dev, enum iommu_viommu_type type);
+int cca_viommu_init(struct iommufd_viommu *viommu, struct device *dev,
+		    struct iommu_domain *parent,
+		    const struct iommu_user_data *user_data);
+
 enum dev_comm_type {
 	PDEV_COMMUNICATE = 0x1,
 	VDEV_COMMUNICATE = 0x2,
drivers/virt/coco/arm-cca-host/viommu.c +201 -0

216 lines not on this page · raw message

maintainer
#64 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 13 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the
IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It groups the vIOMMU operations with their module owner and private data, and makes that implementation discoverable during vIOMMU allocation.

Quoted 5 lines

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

I wonder whether this abstraction is necessary.

The underlying IOMMU driver still needs to understand this vIOMMU type to check vendor-specific compatibility and provide the relevant hardware parameters. There may also be further vendor-specific interactions between the IOMMU and TSM drivers. In that case, the abstraction risks becoming little more than a connection between two vendor-specific drivers, while making it harder to maintain a clear scope for its operations.

Would it be simpler to provide helper APIs and let the IOMMU driver use its own interface to delegate vIOMMU operations to the TSM driver?

From iommufd's perspective, the vIOMMU type and its operations would still be handled by the IOMMU driver.

#65 lore raw
Quoted 13 lines · Kevin
Quoted 13 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the
IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It groups the vIOMMU operations with their module owner and private data, and makes that implementation discoverable during vIOMMU allocation.

Quoted 7 lines
Quoted 5 lines

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

I wonder whether this abstraction is necessary.

The main purpose and point would be to keep the TSM code in TSM and out of iommu drivers.

And we don't want a module dependency from iommu -> tsm either..

Quoted 3 lines

The underlying IOMMU driver still needs to understand this vIOMMU type to check vendor-specific compatibility and provide the relevant hardware parameters.

It shouldn't, if the user requestes a TSM iommu type that should go directly to the TSM driver, and the TSM driver should involve its physical iommu as necessary.

Quoted 2 lines

There may also be further vendor-specific interactions between the IOMMU and TSM drivers.

This is the big question - how big is the entanglement. ARM's is small. What is Intel like?

If Intel and ARM are small, and they should be because the T=1 VIOMMU is entirely handled by TDX/RMM for security, then I feel this is the right way for them. It keeps the code nicely isolated with only a tiny bridge.

AMD is probably not like that.

Quoted 3 lines

In that case, the abstraction risks becoming little more than a connection between two vendor-specific drivers, while making it harder to maintain a clear scope for its operations.

Glancing through these patches I think the abstraction may be too big, really the point is to allow TSM to create a viommu, I'm a little confused why there is so much stuff here.

Quoted 2 lines

Would it be simpler to provide helper APIs and let the IOMMU driver use its own interface to delegate vIOMMU operations to the TSM driver?

Now iommu driver module depends on TSM, do not want.

Jason

maintainer
#66 lore raw
Quoted 27 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, September 25, 2026 3:21 AM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

This series adds the IOMMUFD and PCI/TSM infrastructure required for

device

Quoted 1 line
Quoted 1 line

assignment. It introduces an IOMMUFD-owned vIOMMU provider registry

and

Quoted 5 lines
Quoted 5 lines

the
IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It

groups

Quoted 1 line
Quoted 1 line

the vIOMMU operations with their module owner and private data, and

makes

Quoted 8 lines
Quoted 7 lines

that implementation discoverable during vIOMMU allocation.

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

Quoted 6 lines
Quoted 1 line

I wonder whether this abstraction is necessary.

The main purpose and point would be to keep the TSM code in TSM and out of iommu drivers.

And we don't want a module dependency from iommu -> tsm either..

Yes, but the reverse should be OK (as you also replied to patch6).

Quoted 8 lines
Quoted 4 lines

The underlying IOMMU driver still needs to understand this vIOMMU type to check vendor-specific compatibility and provide the relevant hardware parameters.

It shouldn't, if the user requestes a TSM iommu type that should go directly to the TSM driver, and the TSM driver should involve its physical iommu as necessary.

I was talking about what this abstraction provides. The validate/ get_param() callbacks implies that the related knowledge is kept in the iommu driver, but I agree that they should be put in the TSM driver directly.

Quoted 11 lines
Quoted 3 lines

There may also be further vendor-specific interactions between the IOMMU and TSM drivers.

This is the big question - how big is the entanglement. ARM's is small. What is Intel like?

If Intel and ARM are small, and they should be because the T=1 VIOMMU is entirely handled by TDX/RMM for security, then I feel this is the right way for them. It keeps the code nicely isolated with only a tiny bridge.

there are more interactions in the TDX side, but all from the TSM driver to the IOMMU driver from what I checked now, e.g. about DID reservation at the initialization phase.

Looks there are some resource shared between the untrust and trust invalidation paths, as iommu->qi->q_lock is also acquired in the trust path. I still need to understand the motivation behind, but this can be resolved properly by exporting a helper to the TSM driver even if it's truly required.

There may be more... I'll try to figure them out. Likely there wouldn't be a requirement leading to the dependency from IOMMU to TSM.

Quoted 10 lines

AMD is probably not like that.

Quoted 3 lines

In that case, the abstraction risks becoming little more than a connection between two vendor-specific drivers, while making it harder to maintain a clear scope for its operations.

Glancing through these patches I think the abstraction may be too big, really the point is to allow TSM to create a viommu, I'm a little confused why there is so much stuff here.

that's my feeling too.

#67 lore raw
Quoted 5 lines · Kevin

Looks there are some resource shared between the untrust and trust invalidation paths, as iommu->qi->q_lock is also acquired in the trust path. I still need to understand the motivation behind, but this can be resolved properly by exporting a helper to the TSM driver even if it's truly required.

That seems a bit suspicious from a security POV.. The secure side shouldn't be sharing anything like this with the insecure side, that's just inviting invalidation integrity attacks???

Jason

author maintainer
#68 lore raw
In reply to Tian, Kevin
Quoted 19 lines · Kevin
Quoted 13 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the
IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It groups the vIOMMU operations with their module owner and private data, and makes that implementation discoverable during vIOMMU allocation.

Quoted 5 lines

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

Quoted 6 lines

I wonder whether this abstraction is necessary.

The underlying IOMMU driver still needs to understand this vIOMMU type to check vendor-specific compatibility and provide the relevant hardware parameters.

There is an iommu_ops callback, viommu_get_params, that can be used to check compatibility and return hardware parameters.

Quoted 3 lines

There may also be further vendor-specific interactions between the IOMMU and TSM drivers.

This is an important point. Can you identify those interactions so we can determine whether an iommu_ops callback is the right abstraction?

Quoted 7 lines

In that case, the abstraction risks becoming
little more than a connection between two vendor-specific drivers, while making it harder to maintain a clear scope for its operations.

Would it be simpler to provide helper APIs and let the IOMMU driver use its own interface to delegate vIOMMU operations to the TSM driver?

Does this mean that the vIOMMU would be created by iommufd, with the TSM driver registering callbacks that iommufd invokes for TSM-related operations?

Quoted 2 lines

From iommufd's perspective, the vIOMMU type and its operations would still be handled by the IOMMU driver.

-aneesh

maintainer
#69 lore raw
Quoted 30 lines

From: Aneesh Kumar K.V <[email protected]>
Sent: Friday, September 25, 2026 4:30 PM

Quoted 4 lines · Kevin
Quoted 4 lines

From: Aneesh Kumar K.V (Arm) <[email protected]>
Sent: Thursday, September 17, 2026 10:02 PM

This series adds the IOMMUFD and PCI/TSM infrastructure required for

device

Quoted 1 line
Quoted 1 line

assignment. It introduces an IOMMUFD-owned vIOMMU provider registry

and

Quoted 5 lines
Quoted 5 lines

the
IOMMU_VDEVICE_TSM_REQ ioctl.

The series adds a vIOMMU provider abstraction that allows a subsystem other than the physical IOMMU driver to implement a vIOMMU type. It

groups

Quoted 1 line
Quoted 1 line

the vIOMMU operations with their module owner and private data, and

makes

Quoted 10 lines
Quoted 7 lines

that implementation discoverable during vIOMMU allocation.

External providers are selected by exact vIOMMU type. When no provider matches, vIOMMU creation falls back to the physical IOMMU driver. Once a provider matches, its result is authoritative and failures do not trigger fallback.

I wonder whether this abstraction is necessary.

Quoted 7 lines
Quoted 4 lines

The underlying IOMMU driver still needs to understand this vIOMMU type to check vendor-specific compatibility and provide the relevant hardware parameters.

There is an iommu_ops callback, viommu_get_params, that can be used to check compatibility and return hardware parameters.

iommu_ops is provided by the IOMMU driver, which implies the related knowledge is kept in the IOMMU driver.

Quoted 7 lines
Quoted 4 lines

There may also be further vendor-specific interactions between the IOMMU and TSM drivers.

This is an important point. Can you identify those interactions so we can determine whether an iommu_ops callback is the right abstraction?

will do. Hopefully they are all about the TSM driver calling some IOMMU driver helpers, then would be less a concern to abstract.

Quoted 5 lines
Quoted 5 lines

In that case, the abstraction risks becoming
little more than a connection between two vendor-specific drivers, while making it harder to maintain a clear scope for its operations.

Quoted 8 lines
Quoted 3 lines

Would it be simpler to provide helper APIs and let the IOMMU driver use its own interface to delegate vIOMMU operations to the TSM driver?

Does this mean that the vIOMMU would be created by iommufd, with the TSM driver registering callbacks that iommufd invokes for TSM-related operations?

I originally considered to have the TSM driver register callbacks to the IOMMU driver. No change to IOMMUFD. But Jason's idea on connecting it directly via tsm_ops is cleaner.

maintainer
#70 lore raw
Quoted 11 lines

From: Tian, Kevin <[email protected]>
Sent: Monday, September 28, 2026 11:41 AM

Quoted 8 lines

From: Aneesh Kumar K.V <[email protected]>
Sent: Friday, September 25, 2026 4:30 PM

Quoted 3 lines · Kevin

There may also be further vendor-specific interactions between the IOMMU and TSM drivers.

Quoted 6 lines
Quoted 2 lines

This is an important point. Can you identify those interactions so we can determine whether an iommu_ops callback is the right abstraction?

will do. Hopefully they are all about the TSM driver calling some IOMMU driver helpers, then would be less a concern to abstract.

Correction - I meant "less need for a general abstraction". :)

maintainer
#71 lore raw
In reply to Aneesh Kumar K.V (Arm)
Quoted 3 lines · Aneesh Kumar K.V (Arm)

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the IOMMU_VDEVICE_TSM_REQ ioctl.

I couldn't guess how to apply this with b4 review. Since it requires the series to apply I can't use it without knowing your base..

.. and for some reason this series doesn't have a base-commit trailer

.. nor does it specify a git URL to find it on.

I strongly recommend consistently doing both of these, by script, so it happens right every time.

Please send all your series in a rigerous way. Make sure you have a script that does all of this stuff, ideally automatically. I have one if you want to see it..

Anyhow, I used a crazy AI prompt to make this apply on top of the last tsm series..

Jason

author maintainer
#72 lore raw
Quoted 15 lines · Jason Gunthorpe
Quoted 3 lines · Aneesh Kumar K.V (Arm)

This series adds the IOMMUFD and PCI/TSM infrastructure required for device assignment. It introduces an IOMMUFD-owned vIOMMU provider registry and the IOMMU_VDEVICE_TSM_REQ ioctl.

I couldn't guess how to apply this with b4 review. Since it requires the series to apply I can't use it without knowing your base..

.. and for some reason this series doesn't have a base-commit trailer

.. nor does it specify a git URL to find it on.

I strongly recommend consistently doing both of these, by script, so it happens right every time.

Quoted 7 lines

Please send all your series in a rigerous way. Make sure you have a script that does all of this stuff, ideally automatically. I have one if you want to see it..

Anyhow, I used a crazy AI prompt to make this apply on top of the last tsm series..

I should be able to split these changes into an independent series on top of upstream. I did not include a base-commit because the series is currently based on other CCA patches, solely to enable testing of the complete stack.

The full tree, including the other CCA changes, is available at:

https://git.gitlab.arm.com/linux-arm/linux-cca/-/tree/cca/scratch/vdevice-tdi?ref_type=heads

For the next iteration, I will rebase the IOMMUFD series onto the upstream kernel and include the base-commit trailer.

-aneesh

maintainer
#73 lore raw
Quoted 4 lines · Aneesh Kumar K.V

I should be able to split these changes into an independent series on top of upstream. I did not include a base-commit because the series is currently based on other CCA patches, solely to enable testing of the complete stack.

One of the things I do is have a repo that is just full of other peoples repos as remotes, and I fetch it from time to time. So I added arm's cca repo now.

But this means I would have your base-commit so if you send it I can use it.

Jason