Add iommufd ioctls to support TSM operations
Aneesh Kumar K.V (Arm)
maintainerv5 cover Add iommufd ioctls to support TSM operations
This patch series adds iommufd ioctl support for TSM-related operations. These ioctls allow VMMs to perform TSM management tasks such as bind and unbind operations, and to handle guest requests.
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
Aneesh Kumar K.V (Arm) (3):
vfio: cache KVM VM file references instead of raw struct kvm pointers iommufd/tsm: add vdevice TSM bind/unbind ioctl iommufd/vdevice: add TSM request ioctl
Nicolin Chen (1):
iommufd/viommu: Keep a reference to the KVM file
Shameer Kolothum (1):
iommufd/device: Associate KVM file pointer with iommufd_device
drivers/iommu/iommufd/Makefile | 2 + drivers/iommu/iommufd/device.c | 7 +- drivers/iommu/iommufd/iommufd_private.h | 16 +++ drivers/iommu/iommufd/main.c | 6 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/iommu/iommufd/tsm.c | 130 ++++++++++++++++++++++++ drivers/iommu/iommufd/viommu.c | 9 ++ drivers/s390/crypto/vfio_ap_ops.c | 5 +- drivers/vfio/device_cdev.c | 10 +- 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 | 58 +++++++++++ include/linux/iommufd.h | 5 +- include/linux/kvm_host.h | 3 + include/linux/pci-tsm.h | 9 +- include/linux/tsm.h | 42 ++++++++ include/linux/vfio.h | 17 +++- include/uapi/linux/iommufd.h | 106 +++++++++++++++++++ virt/kvm/kvm_main.c | 2 + 22 files changed, 478 insertions(+), 72 deletions(-) create mode 100644 drivers/iommu/iommufd/tsm.c
base-commit: 50897c955902c93ae71c38698abb910525ebdc89
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH v5 1/5 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -861,6 +863,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
@@ -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
@@ -1351,6 +1351,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 ; }
@@ -5500,6 +5501,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
Aneesh Kumar K.V (Arm)
maintainerPATCH v5 2/5 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.
--- 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Aneesh Kumar K.V (Arm)
maintainerPATCH v5 3/5 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]>
--- drivers/iommu/iommufd/viommu.c | 5 +++++ include/linux/iommufd.h | 1 + 2 files changed, 6 insertions(+)
drivers/iommu/iommufd/viommu.c +5 -0
@@ -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
@@ -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
Aneesh Kumar K.V (Arm)
maintainerPATCH v5 4/5 iommufd/tsm: add vdevice TSM bind/unbind ioctl
In reply to Aneesh Kumar K.V (Arm)Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM bind/unbind operations for an iommufd vdevice.
The new ioctl:
- looks up the vdevice object from vdevice_id
- resolves the associated KVM VM from the vIOMMU KVM file reference
- dispatches bind/unbind via tsm_bind()/tsm_unbind()
Also add common TSM helpers in tsm-core and wire vdevice teardown to unbind the device from TSM state.
This provides iommufd plumbing to bind a TDI to a confidential guest through the TSM layer.
--- drivers/iommu/iommufd/Makefile | 2 + drivers/iommu/iommufd/iommufd_private.h | 8 ++++ drivers/iommu/iommufd/main.c | 3 ++ drivers/iommu/iommufd/tsm.c | 62 +++++++++++++++++++++++++ drivers/iommu/iommufd/viommu.c | 4 ++ drivers/virt/coco/tsm-core.c | 19 ++++++++ include/linux/tsm.h | 17 +++++++ include/uapi/linux/iommufd.h | 26 +++++++++++ 8 files changed, 141 insertions(+) create mode 100644 drivers/iommu/iommufd/tsm.c
drivers/iommu/iommufd/Makefile +2 -0
@@ -10,6 +10,8 @@ iommufd-y := \ vfio_compat.o \ viommu.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
@@ -699,6 +699,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_op_ioctl (struct iommufd_ucmd *ucmd); +#else +static inline int iommufd_vdevice_tsm_op_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
@@ -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_op tsm_op;#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_OP ,iommufd_vdevice_tsm_op_ioctl , +struct iommu_vdevice_tsm_op ,vdevice_id ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/tsm.c new file +62 -0
62 lines not on this page · raw message
drivers/iommu/iommufd/viommu.c +4 -0
@@ -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) {
@@ -124,6 +126,8 @@ void iommufd_vdevice_abort(struct iommufd_object *obj)lockdep_assert_held (&idev->igroup->lock); +tsm_unbind (idev->dev); +if (vdev->destroy) vdev->destroy (vdev);/* xa_cmpxchg is okay to fail if alloc failed xa_cmpxchg previously */
drivers/virt/coco/tsm-core.c +19 -0
@@ -108,6 +108,25 @@ void tsm_unregister(struct tsm_dev *tsm_dev) }EXPORT_SYMBOL_GPL (tsm_unregister); +int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +return pci_tsm_bind (to_pci_dev (dev), kvm, tdi_id); +} +EXPORT_SYMBOL_GPL (tsm_bind); + +int tsm_unbind (struct device *dev) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +pci_tsm_unbind (to_pci_dev (dev)); +return 0 ; +} +EXPORT_SYMBOL_GPL (tsm_unbind); +static void tsm_release (struct device *dev) {struct tsm_dev *tsm_dev =container_of (dev,typeof (*tsm_dev), dev);
include/linux/tsm.h +17 -0
@@ -123,4 +123,21 @@ 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);struct tsm_dev *find_tsm_dev (int id); + +struct kvm ; +#ifdef CONFIG_TSM +int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id); +int tsm_unbind (struct device *dev); +#else +static inline int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) +{ +return -EINVAL ; +} + +static inline int tsm_unbind (struct device *dev) +{ +return 0 ; +} +#endif +#endif /* __TSM_H */
include/uapi/linux/iommufd.h +26 -0
@@ -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_OP = 0x95, };/**
@@ -1143,6 +1144,31 @@ struct iommu_vdevice_alloc { };#define IOMMU_VDEVICE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_ALLOC) +/** +* enum iommu_vdevice_tsm_op_type - operation type for struct iommu_vdevice_tsm_op +* @IOMMU_VDEVICE_TSM_BIND: Bind a vDevice to TSM +* @IOMMU_VDEVICE_TSM_UNBIND: Unbind a vDevice from TSM +*/ +enum iommu_vdevice_tsm_op_type { +IOMMU_VDEVICE_TSM_BIND =0x1 , +IOMMU_VDEVICE_TSM_UNBIND , +}; + +/** +* struct iommu_vdevice_tsm_op - ioctl(IOMMU_VDEVICE_TSM_OP) +* @size: sizeof(struct iommu_vdevice_tsm_op) +* @type: Type of TSM operation. Must be defined in enum iommu_vdevice_tsm_op_type +* @flags: Must be 0 +* @vdevice_id: Object handle for the vDevice. Returned from IOMMU_VDEVICE_ALLOC +*/ +struct iommu_vdevice_tsm_op { +__u32 size; +__u32 type; +__u32 flags; +__u32 vdevice_id; +}; +#define IOMMU_VDEVICE_TSM_OP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_OP) +/** * struct iommu_ioas_change_process - ioctl(VFIO_IOAS_CHANGE_PROCESS) * @size: sizeof(struct iommu_ioas_change_process)
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH v5 5/5 iommufd/vdevice: add TSM request ioctl
In reply to Aneesh Kumar K.V (Arm)Add IOMMU_VDEVICE_TSM_REQUEST for issuing TSM guest request/response transactions against an iommufd vdevice.
The ioctl takes a vdevice_id plus request/response user buffers and length fields, and forwards the request through tsm_guest_req() to the PCI TSM backend. This provides the host-side passthrough path used by CoCo guests for TSM device attestation and acceptance flows after the device has been bound to TSM.
Also add the supporting tsm_guest_req() helper and associated TSM core interface definitions.
Based on changes from: Alexey Kardashevskiy <[email protected]>
--- drivers/iommu/iommufd/iommufd_private.h | 6 ++ drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/tsm.c | 68 +++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 39 ++++++++++++ include/linux/pci-tsm.h | 9 +-- include/linux/tsm.h | 25 ++++++++ include/uapi/linux/iommufd.h | 80 +++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +6 -0
@@ -701,11 +701,17 @@ 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_op_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd);#else static inline int iommufd_vdevice_tsm_op_ioctl (struct iommufd_ucmd *ucmd) {return -EOPNOTSUPP ; } + +static inline int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd) +{ +return -EOPNOTSUPP ; +}#endif static inline struct iommufd_vdevice *
drivers/iommu/iommufd/main.c +3 -0
@@ -433,6 +433,7 @@ union ucmd_buffer {struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu;struct iommu_vdevice_tsm_op tsm_op; +struct iommu_vdevice_tsm_req tsm_req;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -496,6 +497,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_viommu_alloc, out_viommu_id),IOCTL_OP (IOMMU_VDEVICE_TSM_OP ,iommufd_vdevice_tsm_op_ioctl ,struct iommu_vdevice_tsm_op ,vdevice_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_REQ ,iommufd_vdevice_tsm_req_ioctl , +struct iommu_vdevice_tsm_req ,resp_uptr ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/tsm.c +68 -0
71 lines not on this page · raw message
drivers/virt/coco/tsm-core.c +39 -0
@@ -8,6 +8,7 @@#include <linux/module.h> #include <linux/cleanup.h> #include <linux/pci-tsm.h> +#include <uapi/linux/iommufd.h> static void tsm_release (struct device *);static const struct class tsm_class = {
@@ -127,6 +128,44 @@ int tsm_unbind(struct device *dev) }EXPORT_SYMBOL_GPL (tsm_unbind); +static int tsm_pci_req_scope (u32 scope,enum pci_tsm_req_scope *pci_scope) +{ +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : + *pci_scope =PCI_TSM_REQ_INFO ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : + *pci_scope =PCI_TSM_REQ_STATE_CHANGE ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : + *pci_scope =PCI_TSM_REQ_DEBUG_READ ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : + *pci_scope =PCI_TSM_REQ_DEBUG_WRITE ; +return 0 ; +default : +return -EINVAL ; + } +} + +ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info) +{ +int ret; +enum pci_tsm_req_scope pci_scope; + +if (!dev_is_pci (dev)) +return -EINVAL ; + + ret =tsm_pci_req_scope (info->scope, &pci_scope); +if (ret) +return ret; + +return pci_tsm_guest_req (to_pci_dev (dev), pci_scope, info->req, + info->req_len, info->resp, info->resp_len, +NULL ); +} +EXPORT_SYMBOL_GPL (tsm_guest_req); +static void tsm_release (struct device *dev) {struct tsm_dev *tsm_dev =container_of (dev,typeof (*tsm_dev), dev);
include/linux/pci-tsm.h +5 -4
@@ -4,6 +4,7 @@#include <linux/mutex.h> #include <linux/pci.h> #include <linux/sockptr.h> +#include <uapi/linux/iommufd.h> struct pci_tsm ;struct tsm_dev ;
@@ -173,7 +174,7 @@ enum pci_tsm_req_scope {* typical TDISP collateral information like Device Interface Reports. * No device secrets are permitted, and no device state is changed. */ - PCI_TSM_REQ_INFO = 0, + PCI_TSM_REQ_INFO = IOMMU_VDEVICE_TSM_REQ_PCI_INFO,/** * @PCI_TSM_REQ_STATE_CHANGE: Request to change the TDISP state from * UNLOCKED->LOCKED, LOCKED->RUN, or other architecture specific state
@@ -181,14 +182,14 @@ enum pci_tsm_req_scope {* to TDISP) device / host state, configuration, or data change is * permitted. */ -PCI_TSM_REQ_STATE_CHANGE =1 , +PCI_TSM_REQ_STATE_CHANGE =IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE ,/** * @PCI_TSM_REQ_DEBUG_READ: Read-only request for debug information * * A method to facilitate TVM information retrieval outside of typical * TDISP operational requirements. No device secrets are permitted. */ - PCI_TSM_REQ_DEBUG_READ = 2, + PCI_TSM_REQ_DEBUG_READ = IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ,/** * @PCI_TSM_REQ_DEBUG_WRITE: Device state changes for debug purposes *
@@ -196,7 +197,7 @@ enum pci_tsm_req_scope {* the TDISP operational model. If allowed, requires CAP_SYS_RAW_IO, and * will taint the kernel. */ - PCI_TSM_REQ_DEBUG_WRITE = 3, + PCI_TSM_REQ_DEBUG_WRITE = IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE, };#ifdef CONFIG_PCI_TSM
include/linux/tsm.h +25 -0
@@ -6,6 +6,7 @@#include <linux/types.h> #include <linux/uuid.h> #include <linux/device.h> +#include <linux/sockptr.h> #define TSM_REPORT_INBLOB_MAX 64#define TSM_REPORT_OUTBLOB_MAX SZ_16M
@@ -128,6 +129,23 @@ struct kvm;#ifdef CONFIG_TSM int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id);int tsm_unbind (struct device *dev); + +/** +* struct tsm_guest_req_info - parameter for tsm_guest_req() +* @scope: iommufd allocated scope for tsm guest request +* @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 { +u32 scope; +sockptr_t req; +size_t req_len; +sockptr_t resp; +size_t resp_len; +}; +ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info);#else static inline int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) {
@@ -138,6 +156,13 @@ static inline int tsm_unbind(struct device *dev) {return 0 ; } + +struct tsm_guest_req_info ; +static inline ssize_t tsm_guest_req (struct device *dev, +struct tsm_guest_req_info *info) +{ +return -EINVAL ; +}#endif #endif /* __TSM_H */
include/uapi/linux/iommufd.h +80 -0
90 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v5 1/5] vfio: cache KVM VM file references instead of raw struct kvm pointers
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Concurrent BIND_IOMMUFD ioctls on multiple cdev file descriptors for the same device unconditionally overwrite device->kvm_file, leading to reference leaks and Use-After-Free. --
commit 9a5907f1fb1069cc250703cfb404e5a7be33bf23
Author: Aneesh Kumar K.V (Arm) <[email protected]>
vfio: cache KVM VM file references instead of raw struct kvm pointers
This commit switches VFIO to track the VM's struct file instead of raw struct kvm pointers to manage the KVM VM lifetime. This change allows VFIO and iommufd to use normal file references for VM lifetime rather than relying on KVM's internal refcounting.
Quoted 4 lines
drivers/vfio/device_cdev.c +0
-0
[ ... ]
Quoted 5 lines
drivers/vfio/device_cdev.c +0
-1@@ -133,10 +133,10 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, }/* -* Before the device open, get the KVM pointer currently
drivers/vfio/device_cdev.c long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, · line 142
Quoted 10 lines
drivers/vfio/device_cdev.c +4
-3-* 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);
[Severity: High]
This isn't a bug introduced by this patch, but does this unconditionally overwrite device->kvm_file if concurrent BIND_IOMMUFD ioctls are called on multiple cdev file descriptors for the same device?
If a user opens a VFIO device cdev twice, getting two distinct file descriptors (df1 and df2) for the same device, and successfully binds df1 to IOMMUFD:
- device->kvm_file is set (with an extra refcount)
- device->open_count increments to 1
When the user attempts to bind df2, the check for df->access_granted only checks df2, so execution proceeds.
Calling vfio_df_get_kvm_safe(df2) will overwrite device->kvm_file with df2's KVM file. Could the previous file reference from df1 be leaked because no fput() is called?
If vfio_df_open(df2) then returns an error (because device->open_count is 1), the error path jumps to out_put_kvm and calls vfio_device_put_kvm(device). This drops the new reference and clears device->kvm_file to NULL.
This leaves df1 bound and functional, but its KVM association is destroyed. When df1 is eventually closed, hardware drivers like vfio_pci_zdev that check vfio_device_get_kvm() will see a NULL pointer and skip their KVM unregistration hooks. Can this leave KVM hooked to a closed device, potentially leading to a use-after-free when KVM accesses it?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v5 2/5] iommufd/device: Associate KVM file pointer with iommufd_device
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Use-After-Free / Lifetime Mismatch due to borrowed KVM file reference --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Use-After-Free / Lifetime Mismatch due to borrowed KVM file reference --
commit e5661ddf7c17ac12b18e45c9b4e38b80511ba1c0
Author: Shameer Kolothum <[email protected]>
iommufd/device: Associate KVM file pointer with iommufd_device
This commit extends iommufd_device_bind() to accept the device's KVM file and stores it in the iommufd_device struct. This provides TSM vDevice support access to the KVM associated with a VFIO device.
Quoted 15 lines
drivers/iommu/iommufd/device.c +3
-1@@ -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;
[ ... ]
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 273
Quoted 6 lines
drivers/iommu/iommufd/device.c +2
-0@@ -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;
[Severity: Critical]
Does this borrowed reference create a potential use-after-free regression if the iommufd_device outlives the VFIO device?
If a concurrent ioctl on the iommufd device is maliciously stalled (for example, using userfaultfd during a copy_to_user in IOMMU_GET_HW_INFO), iommufd_object_remove() can time out after 60 seconds and return -EBUSY. This would fail to remove the idev object, leaving it alive in the iommufd xarray.
VFIO owns the KVM file reference and unconditionally drops it when the VFIO device file is closed. If VFIO drops this reference while idev remains alive, the file could be freed, leaving idev->kvm_file as a dangling pointer.
If the stalled ioctl is subsequently allowed to finish, a new IOMMU_VIOMMU_ALLOC ioctl could look up the leaked idev and attempt to call get_file(idev->kvm_file), resulting in a use-after-free on the freed struct file.
Should this function take its own independent reference to kvm_file to ensure the file remains valid for the entire lifetime of the iommufd_device?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v5 4/5] iommufd/tsm: add vdevice TSM bind/unbind ioctl
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Silent truncation of `virt_id` from 64-bit to 32-bit when calling `pci_tsm_bind`. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Silent truncation of virt_id from 64-bit to 32-bit when calling pci_tsm_bind. --
commit b7dc3508d5fe2b83fd395c4d50b0951a9d3d243b
Author: Aneesh Kumar K.V (Arm) <[email protected]>
iommufd/tsm: add vdevice TSM bind/unbind ioctl
Introduce IOMMU_VDEVICE_TSM_OP to allow userspace to issue TSM bind/unbind operations for an iommufd vdevice. Also add common TSM helpers in tsm-core and wire vdevice teardown to unbind the device from TSM state.
Quoted 4 lines
drivers/virt/coco/tsm-core.c +0
-0
[ ... ]
drivers/virt/coco/tsm-core.c void tsm_unregister(struct tsm_dev *tsm_dev) · line 116
Quoted 10 lines
drivers/virt/coco/tsm-core.c +6
-0@@ -108,6 +108,25 @@ void tsm_unregister(struct tsm_dev *tsm_dev) }EXPORT_SYMBOL_GPL (tsm_unregister); +int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +return pci_tsm_bind (to_pci_dev (dev), kvm, tdi_id);
[Severity: Medium]
Does this cause a silent truncation of the virtual ID?
While tsm_bind accepts a u64 tdi_id, it passes this argument directly to pci_tsm_bind, which expects a u32 tdi_id.
Because iommufd_vdevice_alloc_ioctl allows userspace to supply a 64-bit virt_id, passing a value larger than 32 bits here will result in a truncated, incorrect virtual device ID being registered with the TSM layer.
Could this lead to state inconsistency between iommufd and the TSM subsystem?
Quoted 2 lines
drivers/virt/coco/tsm-core.c +2
-0+} +EXPORT_SYMBOL_GPL (tsm_bind);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Aneesh Kumar K.V (Arm)Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The ioctl handler incorrectly truncates the `ssize_t` residue from `tsm_guest_req()` to a 32-bit…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The ioctl handler incorrectly truncates the ssize_t residue from tsm_guest_req() to a 32-bit int, causing valid large positive residues to be misinterpreted as negative error codes.
- [Medium] The IOMMU_VDEVICE_TSM_REQ ioctl omits the TSM implementation-specific result code (tsm_code), preventing userspace from forwarding it to the guest.
- [Low] Manual initialization of sockptr_t bypasses kernel macros.
--
commit d5ee59b85809ac88694767fcc899de1ccc2821ca
Author: Aneesh Kumar K.V (Arm) <[email protected]>
iommufd/vdevice: add TSM request ioctl
This commit adds the IOMMU_VDEVICE_TSM_REQ ioctl to issue TSM guest request/response transactions against an iommufd vdevice. It forwards these requests through the new tsm_guest_req() helper to the PCI TSM backend, supporting CoCo guest device attestation.
Quoted 4 lines
drivers/iommu/iommufd/tsm.c +0
-0
[ ... ]
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 98
Quoted 3 lines
drivers/iommu/iommufd/tsm.c +3
-0+int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd) +{ +int rc;
[Severity: High]tsm_guest_req() returns an ssize_t representing the unconsumed byte residue, and the user-provided req_len and resp_len are __u32.
If the residue is between 2GB and 4GB, could it overflow this 32-bit signed rc variable and become a negative value? This could cause the libc ioctl() wrapper to incorrectly treat a successful request as a failure.
Would it be safer to return the residue by modifying a field in struct iommu_vdevice_tsm_req instead of returning unbounded positive values via the ioctl return code?
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 106
Quoted 8 lines
drivers/iommu/iommufd/tsm.c +8
-0+struct iommufd_vdevice *vdev; +struct iommu_vdevice_tsm_req *cmd = ucmd->cmd; +struct tsm_guest_req_info info = { + .scope = cmd->scope, + .req = { + .user =u64_to_user_ptr (cmd->req_uptr), + .is_kernel = false, + },
[Severity: Low]
Could this use the USER_SOCKPTR() macro to initialize the sockptr_t safely instead of manually setting .user and .is_kernel? Bypassing the macro makes this fragile against future layout changes in the sockptr_t structure.
Quoted 7 lines
drivers/iommu/iommufd/tsm.c +7
-0+ .req_len = cmd->req_len, + .resp = { + .user = u64_to_user_ptr(cmd->resp_uptr), + .is_kernel = false, + }, + .resp_len = cmd->resp_len, + };
[ ... ]
Quoted 4 lines
drivers/virt/coco/tsm-core.c +0
-0
[ ... ]
Quoted 5 lines
drivers/virt/coco/tsm-core.c +5
-0+ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info) +{ +int ret; +enum pci_tsm_req_scope pci_scope; +
drivers/virt/coco/tsm-core.c int tsm_unbind(struct device *dev) · line 165
Quoted 10 lines
drivers/virt/coco/tsm-core.c +10
-0+if (!dev_is_pci (dev)) +return -EINVAL ; + + ret =tsm_pci_req_scope (info->scope, &pci_scope); +if (ret) +return ret; + +return pci_tsm_guest_req (to_pci_dev (dev), pci_scope, info->req, + info->req_len, info->resp, info->resp_len, +NULL );
[Severity: Medium]pci_tsm_guest_req() supports returning a TSM implementation-specific result code via the tsm_code parameter, but it is passed NULL here and omitted from struct iommu_vdevice_tsm_req.
Do userspace VMMs need this architecture-specific result code to determine the outcome and failure reasons of attestation flows, and accurately emulate the hardware TSM interface for the guest?
Quoted 2 lines
drivers/virt/coco/tsm-core.c +2
-0+} +EXPORT_SYMBOL_GPL (tsm_guest_req);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Aneesh Kumar K.V
maintainerRe: [PATCH v5 1/5] vfio: cache KVM VM file references instead of raw struct kvm pointers
In reply to [email protected]Quoted 62 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] ConcurrentBIND_IOMMUFDioctls on multiple cdev file descriptors for the same device unconditionally overwritedevice->kvm_file, leading to reference leaks and Use-After-Free. --commit
9a5907f1fb1069cc250703cfb404e5a7be33bf23
Author: Aneesh Kumar K.V (Arm) <[email protected]>vfio: cache KVM VM file references instead of raw struct kvm pointers
This commit switches VFIO to track the VM's struct file instead of raw struct kvm pointers to manage the KVM VM lifetime. This change allows VFIO and iommufd to use normal file references for VM lifetime rather than relying on KVM's internal refcounting.
Quoted 4 lines
drivers/vfio/device_cdev.c +0
-0[ ... ]
Quoted 5 lines
drivers/vfio/device_cdev.c +0
-1@@ -133,10 +133,10 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, }/* -* Before the device open, get the KVM pointer currently drivers/vfio/device_cdev.c long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df, · line 142
Quoted 10 lines
drivers/vfio/device_cdev.c +4
-3-* 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);[Severity: High]
This isn't a bug introduced by this patch, but does this unconditionally overwritedevice->kvm_fileif concurrentBIND_IOMMUFDioctls are called on multiple cdev file descriptors for the same device?If a user opens a VFIO device cdev twice, getting two distinct file descriptors (df1 and df2) for the same device, and successfully binds df1 to IOMMUFD:
-
device->kvm_fileis set (with an extra refcount)
-device->open_countincrements to 1When the user attempts to bind df2, the check for
df->access_grantedonly checks df2, so execution proceeds.Calling
vfio_df_get_kvm_safe(df2)will overwritedevice->kvm_filewith df2's KVM file. Could the previous file reference from df1 be leaked because nofput()is called?If
vfio_df_open(df2)then returns an error (becausedevice->open_countis 1), the error path jumps toout_put_kvmand callsvfio_device_put_kvm(device). This drops the new reference and clearsdevice->kvm_fileto NULL.
Quoted 6 lines
This leaves df1 bound and functional, but its KVM association is destroyed. When df1 is eventually closed, hardware drivers like
vfio_pci_zdevthat checkvfio_device_get_kvm()will see a NULL pointer and skip their KVM unregistration hooks. Can this leave KVM hooked to a closed device, potentially leading to a use-after-free when KVM accesses it?
I guess we can fix this by
modified drivers/vfio/device_cdev.c
@@ -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;
}
Aneesh Kumar K.V
maintainerRe: [PATCH v5 2/5] iommufd/device: Associate KVM file pointer with iommufd_device
In reply to [email protected]Quoted 52 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Use-After-Free / Lifetime Mismatch due to borrowed KVM file reference --commit
e5661ddf7c17ac12b18e45c9b4e38b80511ba1c0
Author: Shameer Kolothum <[email protected]>iommufd/device: Associate KVM file pointer with
iommufd_deviceThis commit extends
iommufd_device_bind()to accept the device's KVM file and stores it in theiommufd_devicestruct. This provides TSM vDevice support access to the KVM associated with a VFIO device.Quoted 15 lines
drivers/iommu/iommufd/device.c +3
-1@@ -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;[ ... ]
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 273
Quoted 6 lines
drivers/iommu/iommufd/device.c +2
-0@@ -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;[Severity: Critical]
Does this borrowed reference create a potential use-after-free regression if theiommufd_deviceoutlives the VFIO device?If a concurrent ioctl on the iommufd device is maliciously stalled (for example, using userfaultfd during a
copy_to_userinIOMMU_GET_HW_INFO),iommufd_object_remove()can time out after 60 seconds and return-EBUSY. This would fail to remove the idev object, leaving it alive in the iommufd xarray.VFIO owns the KVM file reference and unconditionally drops it when the VFIO device file is closed. If VFIO drops this reference while idev remains alive, the file could be freed, leaving
idev->kvm_fileas a dangling pointer.
Quoted 8 lines
If the stalled ioctl is subsequently allowed to finish, a new
IOMMU_VIOMMU_ALLOCioctl could look up the leaked idev and attempt to callget_file(idev->kvm_file), resulting in a use-after-free on the freed struct file.Should this function take its own independent reference to
kvm_fileto ensure the file remains valid for the entire lifetime of theiommufd_device?
I don’t think we need to handle this because closing a VFIO cdev will result in an iommufd unbind. That is, we should not find an idev that outlives a VFIO cdev.
Shouldn’t iommufd_object_remove() failing to remove an object be considered a bug?
-aneesh
Aneesh Kumar K.V
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to [email protected]Quoted 29 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The ioctl handler incorrectly truncates thessize_tresidue fromtsm_guest_req()to a 32-bitint, causing valid large positive residues to be misinterpreted as negative error codes.
- [Medium] TheIOMMU_VDEVICE_TSM_REQioctl omits the TSM implementation-specific result code (tsm_code), preventing userspace from forwarding it to the guest.
- [Low] Manual initialization ofsockptr_tbypasses kernel macros.
--commit
d5ee59b85809ac88694767fcc899de1ccc2821ca
Author: Aneesh Kumar K.V (Arm) <[email protected]>iommufd/vdevice: add TSM request ioctl
This commit adds the
IOMMU_VDEVICE_TSM_REQioctl to issue TSM guest request/response transactions against an iommufd vdevice. It forwards these requests through the newtsm_guest_req()helper to the PCI TSM backend, supporting CoCo guest device attestation.Quoted 4 lines
drivers/iommu/iommufd/tsm.c +0
-0[ ... ]
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 98
Quoted 3 lines
drivers/iommu/iommufd/tsm.c +3
-0+int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd) +{ +int rc;[Severity: High]
tsm_guest_req()returns anssize_trepresenting the unconsumed byte residue, and the user-providedreq_lenandresp_lenare__u32.
Quoted 8 lines
If the residue is between 2GB and 4GB, could it overflow this 32-bit signed rc variable and become a negative value? This could cause the libc
ioctl()wrapper to incorrectly treat a successful request as a failure.Would it be safer to return the residue by modifying a field in struct
iommu_vdevice_tsm_reqinstead of returning unbounded positive values via the ioctl return code?
req_len and resp_len are both 32 bits.
Quoted 49 lines
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 106
Quoted 9 lines
drivers/iommu/iommufd/tsm.c +8
-0+struct iommufd_vdevice *vdev; +struct iommu_vdevice_tsm_req *cmd = ucmd->cmd; +struct tsm_guest_req_info info = { + .scope = cmd->scope, + .req = { + .user =u64_to_user_ptr (cmd->req_uptr), + .is_kernel = false, + },[Severity: Low]
Could this use theUSER_SOCKPTR()macro to initialize thesockptr_tsafely instead of manually setting .user and .is_kernel? Bypassing the macro makes this fragile against future layout changes in thesockptr_tstructure.Quoted 7 lines
drivers/iommu/iommufd/tsm.c +7
-0+ .req_len = cmd->req_len, + .resp = { + .user = u64_to_user_ptr(cmd->resp_uptr), + .is_kernel = false, + }, + .resp_len = cmd->resp_len, + };[ ... ]
Quoted 4 lines
drivers/virt/coco/tsm-core.c +0
-0[ ... ]
Quoted 5 lines
drivers/virt/coco/tsm-core.c +5
-0+ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info) +{ +int ret; +enum pci_tsm_req_scope pci_scope; +drivers/virt/coco/tsm-core.c int tsm_unbind(struct device *dev) · line 165
Quoted 10 lines
drivers/virt/coco/tsm-core.c +10
-0+if (!dev_is_pci (dev)) +return -EINVAL ; + + ret =tsm_pci_req_scope (info->scope, &pci_scope); +if (ret) +return ret; + +return pci_tsm_guest_req (to_pci_dev (dev), pci_scope, info->req, + info->req_len, info->resp, info->resp_len, +NULL );[Severity: Medium]
pci_tsm_guest_req()supports returning a TSM implementation-specific result code via thetsm_codeparameter, but it is passed NULL here and omitted from structiommu_vdevice_tsm_req.
Quoted 4 lines
Do userspace VMMs need this architecture-specific result code to determine the outcome and failure reasons of attestation flows, and accurately emulate the hardware TSM interface for the guest?
Will update the iotcl to include tsm_code as part of the response.
Quoted 4 lines
drivers/virt/coco/tsm-core.c
Quoted 3 lines
drivers/virt/coco/tsm-core.c +2
-0+} +EXPORT_SYMBOL_GPL (tsm_guest_req);
-aneesh
Re: [PATCH v5 1/5] vfio: cache KVM VM file references instead of raw struct kvm pointers
In reply to Aneesh Kumar K.V (Arm)Quoted 27 lines · Aneesh Kumar K.V (Arm)
VFIO currently records struct kvm pointers on
vfio_group,vfio_device_fileand the openedvfio_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_DEVICEbinds the KVM VM lifetime to the KVM device fd lifetime. ForKVM_DEV_TYPE_VFIO, the KVM VFIO device fd also takes references to each VFIO file added throughKVM_DEV_VFIO_FILE_ADD. The KVM VFIO device fd therefore owns both the internal KVM reference and the VFIO file references inkvf->file.
KVM_DEV_VFIO_FILE_ADDfurther installs the VM file association into the VFIO file. VFIO converts the struct kvm pointer to a VM file reference withget_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_DELor 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_filefor driver and iommufd use. That open-time reference is released fromvfio_device_put_kvm()when the VFIO device is closed or unbound.
Quoted 8 lines
This gives the ownership model:
- KVM device fd pins struct kvm through
kvm->users_count
- KVM VFIO device fd pins VFIO files throughkvf->file
- VFIO group/device-file state pins the VM file while associated with KVM
-vfio_device::kvm_filepins the VM file during active VFIO device useSigned-off-by Aneesh Kumar K.V (Arm) <[email protected]>
Quoted 384 lines
--- 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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -442,55 +442,61 @@ void vfio_unregister_group_dev(struct vfio_device *device)EXPORT_SYMBOL_GPL (vfio_unregister_group_dev);#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 (*pfn)(struct kvm *kvm); -bool (*fn)(struct kvm *kvm); -bool ret; +struct file *old_kvm_file, *new_kvm_file =NULL ; -lockdep_assert_held (&device->dev_set->lock); +/* +* @kvm can outlive the VM fd and its final __fput(). Only take a +* new reference if the VM file is still active. +*/ +if (kvm) + new_kvm_file =get_file_active (&kvm->_file); -if (!kvm) -return ; +spin_lock (lock); + old_kvm_file = *dst; + *dst = new_kvm_file; +spin_unlock (lock); - pfn =symbol_get (kvm_put_kvm); -if (WARN_ON (!pfn)) -return ; +if (old_kvm_file) +fput (old_kvm_file); +} - fn =symbol_get (kvm_get_kvm_safe); -if (WARN_ON (!fn)) { -symbol_put (kvm_put_kvm); -return ; - } +void vfio_device_get_kvm_safe (struct vfio_device *device,struct file *kvm_file) +{ +lockdep_assert_held (&device->dev_set->lock); - ret =fn (kvm); -symbol_put (kvm_get_kvm_safe); -if (!ret) { -symbol_put (kvm_put_kvm); -return ; - } +/* +* Take a VM file reference if the KVM fd is still active. +*/ +if (kvm_file) + kvm_file =get_file (kvm_file); - device->put_kvm = pfn; - device->kvm = kvm; + device->kvm_file = kvm_file; }void vfio_device_put_kvm (struct vfio_device *device) { +struct file *kvm_file; +lockdep_assert_held (&device->dev_set->lock); -if (!device->kvm) + kvm_file = device->kvm_file; +if (!kvm_file)return ; -if (WARN_ON (!device->put_kvm)) - goto clear; + device->kvm_file =NULL ; +fput (kvm_file); +} - device->put_kvm (device->kvm); - device->put_kvm =NULL ; -symbol_put (kvm_put_kvm); +struct kvm *vfio_device_get_kvm (struct vfio_device *device) +{ +if (!device->kvm_file) +return NULL ; -clear: - device->kvm =NULL ; +return device->kvm_file->private_data; } +EXPORT_SYMBOL_GPL (vfio_device_get_kvm);#endif /* true if the vfio_device has open_device() called but not close_device() */ @@ -1518,13 +1524,10 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)struct vfio_device_file *df = file->private_data;/* -* The kvm is first recorded in the vfio_device_file, and will -* be propagated to vfio_device::kvm when the file is bound to -* iommufd successfully in the vfio device cdev path. +* Cache the VM file reference associated with this VFIO file so it +* can be pinned into vfio_device while the device is open. */ -spin_lock (&df->kvm_ref_lock); - df->kvm = kvm; -spin_unlock (&df->kvm_ref_lock); +vfio_kvm_file_replace (&df->kvm_file, &df->kvm_ref_lock, kvm); }/** @@ -1532,8 +1535,8 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)* @file: VFIO group file or VFIO device file * @kvm: KVM to link * -* When a VFIO device is first opened the KVM will be available in -* device->kvm if one was associated with the file. +* When a VFIO device is first opened, VFIO caches a VM file reference if +* one was associated with the file. */ void vfio_file_set_kvm (struct file *file,struct kvm *kvm) {include/linux/kvm_host.h +3
-0@@ -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 @@ -861,6 +863,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@@ -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@@ -1351,6 +1351,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 ; }@@ -5500,6 +5501,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
Alexey Kardashevskiy
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Aneesh Kumar K.V (Arm)Quoted 77 lines · Aneesh Kumar K.V (Arm)
Add
IOMMU_VDEVICE_TSM_REQUESTfor issuing TSM guest request/response transactions against an iommufd vdevice.The ioctl takes a
vdevice_idplus request/response user buffers and length fields, and forwards the request throughtsm_guest_req()to the PCI TSM backend. This provides the host-side passthrough path used by CoCo guests for TSM device attestation and acceptance flows after the device has been bound to TSM.Also add the supporting
tsm_guest_req()helper and associated TSM core interface definitions.Based on changes from: Alexey Kardashevskiy <[email protected]>
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 6 ++ drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/tsm.c | 68 +++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 39 ++++++++++++ include/linux/pci-tsm.h | 9 +-- include/linux/tsm.h | 25 ++++++++ include/uapi/linux/iommufd.h | 80 +++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-)drivers/iommu/iommufd/iommufd_private.h +6
-0@@ -701,11 +701,17 @@ 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_op_ioctl (struct iommufd_ucmd *ucmd); +int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd);#else static inline int iommufd_vdevice_tsm_op_ioctl (struct iommufd_ucmd *ucmd) {return -EOPNOTSUPP ; } + +static inline int iommufd_vdevice_tsm_req_ioctl (struct iommufd_ucmd *ucmd) +{ +return -EOPNOTSUPP ; +}#endif static inline struct iommufd_vdevice *drivers/iommu/iommufd/main.c +3
-0@@ -433,6 +433,7 @@ union ucmd_buffer {struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu;struct iommu_vdevice_tsm_op tsm_op; +struct iommu_vdevice_tsm_req tsm_req;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif @@ -496,6 +497,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { struct iommu_viommu_alloc, out_viommu_id),IOCTL_OP (IOMMU_VDEVICE_TSM_OP ,iommufd_vdevice_tsm_op_ioctl ,struct iommu_vdevice_tsm_op ,vdevice_id ), +IOCTL_OP (IOMMU_VDEVICE_TSM_REQ ,iommufd_vdevice_tsm_req_ioctl , +struct iommu_vdevice_tsm_req ,resp_uptr ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endifdrivers/iommu/iommufd/tsm.c +1
-0@@ -60,3 +60,71 @@ int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &vdev->obj);return rc; } +
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 73
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +10
-0+static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +{ +if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +return false; + +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE :
This scope thing still needs clarification.
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU -> IOMMUFD -> TSM.
1) bind/unbind TDI <- moves to CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but which scope is it here?
thanks,
Quoted 307 lines
drivers/iommu/iommufd/tsm.c +57
-0+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 the TSM core. +* +* 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 rc; +struct iommufd_vdevice *vdev; +struct iommu_vdevice_tsm_req *cmd = ucmd->cmd; +struct tsm_guest_req_info info = { + .scope = cmd->scope, + .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 (cmd->__reserved) +return -EOPNOTSUPP ; + +if (!iommufd_vdevice_tsm_req_scope_valid (cmd->scope)) +return -EINVAL ; + + vdev =iommufd_get_vdevice (ucmd->ictx, cmd->vdevice_id); +if (IS_ERR (vdev)) +return PTR_ERR (vdev); + + rc =tsm_guest_req (vdev->idev->dev, &info); + +/* No inline response, hence we don't need to copy the response */ +iommufd_put_object (ucmd->ictx, &vdev->obj); +return rc; +}drivers/virt/coco/tsm-core.c +39
-0@@ -8,6 +8,7 @@#include <linux/module.h> #include <linux/cleanup.h> #include <linux/pci-tsm.h> +#include <uapi/linux/iommufd.h> static void tsm_release (struct device *);static const struct class tsm_class = {@@ -127,6 +128,44 @@ int tsm_unbind(struct device *dev) }EXPORT_SYMBOL_GPL (tsm_unbind); +static int tsm_pci_req_scope (u32 scope,enum pci_tsm_req_scope *pci_scope) +{ +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : + *pci_scope =PCI_TSM_REQ_INFO ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : + *pci_scope =PCI_TSM_REQ_STATE_CHANGE ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : + *pci_scope =PCI_TSM_REQ_DEBUG_READ ; +return 0 ; +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : + *pci_scope =PCI_TSM_REQ_DEBUG_WRITE ; +return 0 ; +default : +return -EINVAL ; + } +} + +ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info) +{ +int ret; +enum pci_tsm_req_scope pci_scope; + +if (!dev_is_pci (dev)) +return -EINVAL ; + + ret =tsm_pci_req_scope (info->scope, &pci_scope); +if (ret) +return ret; + +return pci_tsm_guest_req (to_pci_dev (dev), pci_scope, info->req, + info->req_len, info->resp, info->resp_len, +NULL ); +} +EXPORT_SYMBOL_GPL (tsm_guest_req); +static void tsm_release (struct device *dev) {struct tsm_dev *tsm_dev =container_of (dev,typeof (*tsm_dev), dev);include/linux/pci-tsm.h +5
-4@@ -4,6 +4,7 @@#include <linux/mutex.h> #include <linux/pci.h> #include <linux/sockptr.h> +#include <uapi/linux/iommufd.h> struct pci_tsm ;struct tsm_dev ;@@ -173,7 +174,7 @@ enum pci_tsm_req_scope {* typical TDISP collateral information like Device Interface Reports. * No device secrets are permitted, and no device state is changed. */ - PCI_TSM_REQ_INFO = 0, + PCI_TSM_REQ_INFO = IOMMU_VDEVICE_TSM_REQ_PCI_INFO,/** * @PCI_TSM_REQ_STATE_CHANGE: Request to change the TDISP state from * UNLOCKED->LOCKED, LOCKED->RUN, or other architecture specific state @@ -181,14 +182,14 @@ enum pci_tsm_req_scope {* to TDISP) device / host state, configuration, or data change is * permitted. */ -PCI_TSM_REQ_STATE_CHANGE =1 , +PCI_TSM_REQ_STATE_CHANGE =IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE ,/** * @PCI_TSM_REQ_DEBUG_READ: Read-only request for debug information * * A method to facilitate TVM information retrieval outside of typical * TDISP operational requirements. No device secrets are permitted. */ - PCI_TSM_REQ_DEBUG_READ = 2, + PCI_TSM_REQ_DEBUG_READ = IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ,/** * @PCI_TSM_REQ_DEBUG_WRITE: Device state changes for debug purposes * @@ -196,7 +197,7 @@ enum pci_tsm_req_scope {* the TDISP operational model. If allowed, requires CAP_SYS_RAW_IO, and * will taint the kernel. */ - PCI_TSM_REQ_DEBUG_WRITE = 3, + PCI_TSM_REQ_DEBUG_WRITE = IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE, };#ifdef CONFIG_PCI_TSM include/linux/tsm.h +25
-0@@ -6,6 +6,7 @@#include <linux/types.h> #include <linux/uuid.h> #include <linux/device.h> +#include <linux/sockptr.h> #define TSM_REPORT_INBLOB_MAX 64#define TSM_REPORT_OUTBLOB_MAX SZ_16M@@ -128,6 +129,23 @@ struct kvm;#ifdef CONFIG_TSM int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id);int tsm_unbind (struct device *dev); + +/** +* struct tsm_guest_req_info - parameter for tsm_guest_req() +* @scope: iommufd allocated scope for tsm guest request +* @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 { +u32 scope; +sockptr_t req; +size_t req_len; +sockptr_t resp; +size_t resp_len; +}; +ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info);#else static inline int tsm_bind (struct device *dev,struct kvm *kvm,u64 tdi_id) {@@ -138,6 +156,13 @@ static inline int tsm_unbind(struct device *dev) {return 0 ; } + +struct tsm_guest_req_info ; +static inline ssize_t tsm_guest_req (struct device *dev, +struct tsm_guest_req_info *info) +{ +return -EINVAL ; +}#endif #endif /* __TSM_H */ include/uapi/linux/iommufd.h +80
-0@@ -58,6 +58,7 @@ enum {IOMMUFD_CMD_VEVENTQ_ALLOC =0x93 ,IOMMUFD_CMD_HW_QUEUE_ALLOC =0x94 ,IOMMUFD_CMD_VDEVICE_TSM_OP =0x95 , + IOMMUFD_CMD_VDEVICE_TSM_REQ = 0x96, };/** @@ -1373,4 +1374,83 @@ struct iommu_hw_queue_alloc { __aligned_u64 length; };#define IOMMU_HW_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_HW_QUEUE_ALLOC) + +/* +* TSM request scope values are allocated by iommufd. Each device-bus transport +* gets a range from this number space. +*/ +#define IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_BASE 0 + +enum iommu_vdevice_tsm_req_scope { +/* +* Read-only, without side effects, request for typical TDISP +* collateral information like Device Interface Reports. No device +* secrets are permitted, and no device state is changed. +*/ +IOMMU_VDEVICE_TSM_REQ_PCI_INFO = +IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_BASE , +/* +* Request to change the TDISP state from UNLOCKED->LOCKED, +* LOCKED->RUN, or other architecture specific state changes to +* support those transitions for a TDI. No other device or host state, +* configuration, or data change is permitted. +*/ +IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE = +IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_BASE +1 , +/* +* Read-only request for debug information outside of typical TDISP +* operational requirements. No device secrets are permitted. +*/ +IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ = +IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_BASE +2 , +/* +* Device state changes for debug purposes. The request may affect the +* operational state of the device outside of the TDISP operational +* model. If allowed, this requires CAP_SYS_RAW_IO and taints the +* kernel. +*/ +IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE = +IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_BASE +3 , +IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST = +IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE , +}; + +/** +* 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 +* @scope: One of enum iommu_vdevice_tsm_req_scope +* @req_len: Size in bytes of the input payload at @req_uptr +* @resp_len: Size in bytes of the output buffer at @resp_uptr +* @__reserved: Must be 0 +* @req_uptr: Userspace pointer to the guest-provided request payload +* @resp_uptr: Userspace pointer to the guest response buffer +* +* 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. +* +* Requests outside the iommufd allocated scope values are rejected. Lower +* layers may reject scope values that are valid in the global iommufd +* namespace, but not permitted for a specific bus. +* +* 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 scope; +__u32 req_len; +__u32 resp_len; +__u32 __reserved; +__aligned_u64 req_uptr; +__aligned_u64 resp_uptr; +}; + +#define IOMMU_VDEVICE_TSM_REQ _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_TSM_REQ)#endif
Signature · 1 line
Alexey
Re: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 27 lines · Alexey Kardashevskiy
Quoted 24 lines · Aneesh Kumar K.V (Arm)
Add
IOMMU_VDEVICE_TSM_REQUESTfor issuing TSM guest request/response transactions against an iommufd vdevice.The ioctl takes a
vdevice_idplus request/response user buffers and length fields, and forwards the request throughtsm_guest_req()to the PCI TSM backend. This provides the host-side passthrough path used by CoCo guests for TSM device attestation and acceptance flows after the device has been bound to TSM.Also add the supporting
tsm_guest_req()helper and associated TSM core interface definitions.Based on changes from: Alexey Kardashevskiy <[email protected]>
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 6 ++ drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/tsm.c | 68 +++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 39 ++++++++++++ include/linux/pci-tsm.h | 9 +-- include/linux/tsm.h | 25 ++++++++ include/uapi/linux/iommufd.h | 80 +++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-)
[..]
Quoted 22 lines
Quoted 9 lines
drivers/iommu/iommufd/tsm.c +1
-0@@ -60,3 +60,71 @@ int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &vdev->obj);return rc; } +drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 73
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +10
-0+static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +{ +if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +return false; + +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE :This scope thing still needs clarification.
Quoted 5 lines
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU -> IOMMUFD -> TSM.
1) bind/unbind TDI <- moves to
CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but which scope is it here?
The scope parameter was meant to enumerate a security model for classes of commands that are otherwise opaque to the kernel. However, none of the commands we are targeting are opaque (private specification with unknown effect). It now turns out there is no role for @scope for security.
Now a command family that iommufd can validate seems useful. As it stands this implementation aliases command codes across TSMs. Do we proceed with creating an actual shared command uapi for the truly shared commands:
TSM_REQ_TYPE_DEFAULT: Commands every arch needsTSM_REQ_READ_OBJECTTSM_REQ_REGEN_OBJECTTSM_REQ_OBJECT_INFOTSM_REQ_VALIDATE_MMIOTSM_REQ_SET_TDI_STATE
TSM_REQ_TYPE_SEV: Commands only SEV needsTSM_REQ_SEV_ENABLE_DMATSM_REQ_SEV_DISABLE_DMA
...or just observe that per CC arch commands are needed to setup the VM so per CC arch commands are needed to marshal device assignment support requests.
In that case pci_tsm_req_scope becomes tsm_req_type and is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDX
I am leaning towards the latter at this point.
Tian, Kevin
maintainerRE: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 26 lines
From: Dan Williams (nvidia) <[email protected]>
Sent: Wednesday, May 27, 2026 2:18 PMQuoted 16 lines · Alexey Kardashevskiy
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 73
Quoted 10 lines · Aneesh Kumar K.V (Arm)
drivers/iommu/iommufd/tsm.c +10
-0+static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +{ +if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +return false; + +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE :This scope thing still needs clarification.
Quoted 1 line
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU ->
IOMMUFD -> TSM.
Quoted 4 lines
1) bind/unbind TDI <- moves to
CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but
Quoted 7 lines
which scope is it here?
The scope parameter was meant to enumerate a security model for classes of commands that are otherwise opaque to the kernel. However, none of the commands we are targeting are opaque (private specification with unknown effect). It now turns out there is no role for @scope for security.
yeah, I haven't succeeded on figuring out that role for now. It sounds an unnecessary abstraction asking vendor specific code to translate its command into opaque then in the end we go back to the vendor code to decide the security scope of that opaque.
[...]
Quoted 11 lines
...or just observe that per CC arch commands are needed to setup the VM so per CC arch commands are needed to marshal device assignment support requests.
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDXI am leaning towards the latter at this point.
+1
Re: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Dan Williams (nvidia)Quoted 7 lines · Dan Williams (nvidia)
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDXI am leaning towards the latter at this point.
Yeah, this sounds good. I would also include an common op field that can be decoded by the TSM driver based on the TYPE above, and the usual in/out message buffers.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Dan Williams (nvidia)Quoted 83 lines · Dan Williams (nvidia)
Quoted 27 lines · Alexey Kardashevskiy
Quoted 24 lines · Aneesh Kumar K.V (Arm)
Add
IOMMU_VDEVICE_TSM_REQUESTfor issuing TSM guest request/response transactions against an iommufd vdevice.The ioctl takes a
vdevice_idplus request/response user buffers and length fields, and forwards the request throughtsm_guest_req()to the PCI TSM backend. This provides the host-side passthrough path used by CoCo guests for TSM device attestation and acceptance flows after the device has been bound to TSM.Also add the supporting
tsm_guest_req()helper and associated TSM core interface definitions.Based on changes from: Alexey Kardashevskiy <[email protected]>
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 6 ++ drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/tsm.c | 68 +++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 39 ++++++++++++ include/linux/pci-tsm.h | 9 +-- include/linux/tsm.h | 25 ++++++++ include/uapi/linux/iommufd.h | 80 +++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-)[..]
Quoted 22 lines
Quoted 9 lines
drivers/iommu/iommufd/tsm.c +1
-0@@ -60,3 +60,71 @@ int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &vdev->obj);return rc; } +drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 73
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +10
-0+static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +{ +if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +return false; + +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE :This scope thing still needs clarification.
Quoted 5 lines
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU -> IOMMUFD -> TSM.
1) bind/unbind TDI <- moves to
CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but which scope is it here?The scope parameter was meant to enumerate a security model for classes of commands that are otherwise opaque to the kernel. However, none of the commands we are targeting are opaque (private specification with unknown effect). It now turns out there is no role for @scope for security.
Now a command family that iommufd can validate seems useful. As it stands this implementation aliases command codes across TSMs. Do we proceed with creating an actual shared command uapi for the truly shared commands:
TSM_REQ_TYPE_DEFAULT: Commands every arch needsTSM_REQ_READ_OBJECTTSM_REQ_REGEN_OBJECTTSM_REQ_OBJECT_INFOTSM_REQ_VALIDATE_MMIOTSM_REQ_SET_TDI_STATE
TSM_REQ_TYPE_SEV: Commands only SEV needsTSM_REQ_SEV_ENABLE_DMATSM_REQ_SEV_DISABLE_DMA...or just observe that per CC arch commands are needed to setup the VM so per CC arch commands are needed to marshal device assignment support requests.
Quoted 7 lines
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDXI am leaning towards the latter at this point.
But we already have struct pci_tsm_ops::guest_req, which is specific to the underlying CC architecture. From the above, pci_tsm_req_scope also appears to carry the same information. Is that useful?
-aneesh
Aneesh Kumar K.V
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 4 lines · Aneesh Kumar K.V
Quoted 2 lines · Dan Williams (nvidia)
Quoted 1 line · Alexey Kardashevskiy
......
Quoted 39 lines
Quoted 39 lines
Quoted 6 lines
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU -> IOMMUFD -> TSM.
1) bind/unbind TDI <- moves to
CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but which scope is it here?The scope parameter was meant to enumerate a security model for classes of commands that are otherwise opaque to the kernel. However, none of the commands we are targeting are opaque (private specification with unknown effect). It now turns out there is no role for @scope for security.
Now a command family that iommufd can validate seems useful. As it stands this implementation aliases command codes across TSMs. Do we proceed with creating an actual shared command uapi for the truly shared commands:
TSM_REQ_TYPE_DEFAULT: Commands every arch needsTSM_REQ_READ_OBJECTTSM_REQ_REGEN_OBJECTTSM_REQ_OBJECT_INFOTSM_REQ_VALIDATE_MMIOTSM_REQ_SET_TDI_STATE
TSM_REQ_TYPE_SEV: Commands only SEV needsTSM_REQ_SEV_ENABLE_DMATSM_REQ_SEV_DISABLE_DMA...or just observe that per CC arch commands are needed to setup the VM so per CC arch commands are needed to marshal device assignment support requests.
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDX
Quoted 6 lines
Quoted 1 line
I am leaning towards the latter at this point.
But we already have struct
pci_tsm_ops::guest_req, which is specific to the underlying CC architecture. From the above,pci_tsm_req_scopealso appears to carry the same information. Is that useful?
I think there is value in having the VMM express the guest’s confidential computing architecture, so that the TSM backend can validate whether it should handle that guest request ?.
So it would not be the IOMMU validating the scope value, but rather pci_tsm_ops::guest_req.
static ssize_t cca_tsm_guest_req(struct pci_tdi *tdi, enum pci_tsm_req_scope scope,
sockptr_t req, size_t req_len, sockptr_t resp,
size_t resp_len, u64 *tsm_code)
{
struct pci_dev *pdev = tdi->pdev;
/* reject the guest request if VMM was using the link tsm wrongly. The guest
* was using a wrong CC archiecture with this link tsm
*/
if (scope != TSM_REQ_TYPE_CCA)
return -EINVAL;Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Dan Williams (nvidia)
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDX
Quoted 5 lines
Quoted 1 line
I am leaning towards the latter at this point.
Yeah, this sounds good. I would also include an common op field that can be decoded by the TSM driver based on the TYPE above, and the usual in/out message buffers.
We already have iommufd_vdevice_tsm_op_ioctl() to handle common operations. Right now, it handles IOMMU_VDEVICE_TSM_BIND and IOMMU_VDEVICE_TSM_UNBIND. I guess we should move TSM_REQ_SET_TDI_STATE operations to that as well?
-aneesh
Re: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 10 lines · Aneesh Kumar K.V
Quoted 6 lines
Quoted 1 line
I am leaning towards the latter at this point.
But we already have struct
pci_tsm_ops::guest_req, which is specific to the underlying CC architecture. From the above,pci_tsm_req_scopealso appears to carry the same information. Is that useful?I think there is value in having the VMM express the guest’s confidential computing architecture, so that the TSM backend can validate whether it should handle that guest request ?.
Yes, that is the idea.
Quoted 4 lines
So it would not be the IOMMU validating the scope value, but rather
pci_tsm_ops::guest_req.static
ssize_tcca_tsm_guest_req(structpci_tdi*tdi, enumpci_tsm_req_scopescope,
Quoted 10 lines
sockptr_t req, size_t req_len, sockptr_t resp,
size_t resp_len, u64 *tsm_code)
{
struct pci_dev *pdev = tdi->pdev;
/* reject the guest request if VMM was using the link tsm wrongly. The guest
* was using a wrong CC archiecture with this link tsm
*/
if (scope != TSM_REQ_TYPE_CCA)
return -EINVAL;Right, iommufd is tunneling TSM requests. The tunnel should have an envelope of TSM_REQ_TYPE_* and an @op field. The TSM driver gets those from iommufd, validates the envelope and then processes @req.
This self-consistency and explicitness also buys some future-proofing. It allows for alternate command sets within an arch, cross TSM implementation shared commands, IOMMUFD-to-TSM requests outside of guest requests.
Quoted 10 lines
Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Dan Williams (nvidia)
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDX
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
I am leaning towards the latter at this point.
Yeah, this sounds good. I would also include an common op field that can be decoded by the TSM driver based on the TYPE above, and the usual in/out message buffers.
We already have
iommufd_vdevice_tsm_op_ioctl()to handle common operations.
Per above, I believe this is about an @op value in a common location that iommufd can forward to the backend for validation of guest requests.
Quoted 3 lines
Right now, it handles
IOMMU_VDEVICE_TSM_BINDandIOMMU_VDEVICE_TSM_UNBIND. I guess we should moveTSM_REQ_SET_TDI_STATEoperations to that as well?
I think we can wait to move it to its own IOMMU operation unless/until there is a need to set RUN outside of an explicit guest request, right?
Aneesh Kumar K.V
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 61 lines · Dan Williams (nvidia)
Quoted 10 lines · Aneesh Kumar K.V
Quoted 6 lines
Quoted 1 line
I am leaning towards the latter at this point.
But we already have struct
pci_tsm_ops::guest_req, which is specific to the underlying CC architecture. From the above,pci_tsm_req_scopealso appears to carry the same information. Is that useful?I think there is value in having the VMM express the guest’s confidential computing architecture, so that the TSM backend can validate whether it should handle that guest request ?.
Yes, that is the idea.
Quoted 4 lines
So it would not be the IOMMU validating the scope value, but rather
pci_tsm_ops::guest_req.static
ssize_tcca_tsm_guest_req(structpci_tdi*tdi, enumpci_tsm_req_scopescope,Quoted 10 lines
sockptr_t req, size_t req_len, sockptr_t resp, size_t resp_len, u64 *tsm_code) { struct pci_dev *pdev = tdi->pdev; /* reject the guest request if VMM was using the link tsm wrongly. The guest * was using a wrong CC archiecture with this link tsm */ if (scope != TSM_REQ_TYPE_CCA) return -EINVAL;Right, iommufd is tunneling TSM requests. The tunnel should have an envelope of TSM_REQ_TYPE_* and an @op field. The TSM driver gets those from iommufd, validates the envelope and then processes @req.
This self-consistency and explicitness also buys some future-proofing. It allows for alternate command sets within an arch, cross TSM implementation shared commands, IOMMUFD-to-TSM requests outside of guest requests.
Quoted 10 lines
Quoted 8 lines · Jason Gunthorpe
Quoted 6 lines · Dan Williams (nvidia)
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDXQuoted 8 lines
Quoted 5 lines
Quoted 1 line
I am leaning towards the latter at this point.
Yeah, this sounds good. I would also include an common op field that can be decoded by the TSM driver based on the TYPE above, and the usual in/out message buffers.
We already have
iommufd_vdevice_tsm_op_ioctl()to handle common operations.Per above, I believe this is about an @op value in a common location that iommufd can forward to the backend for validation of guest requests.
Quoted 6 lines
Quoted 3 lines
Right now, it handles
IOMMU_VDEVICE_TSM_BINDandIOMMU_VDEVICE_TSM_UNBIND. I guess we should moveTSM_REQ_SET_TDI_STATEoperations to that as well?I think we can wait to move it to its own IOMMU operation unless/until there is a need to set RUN outside of an explicit guest request, right?
Something like the below? (the diff against this series)
I have not yet integrated this into the full CCA patchset for testing, but I wanted to make sure we are aligned on the UAPI.
drivers/iommu/iommufd/tsm.c +25 -11
@@ -61,17 +61,30 @@ int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)return ret; } -static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +static bool iommufd_vdevice_tsm_req_arch_valid (u32 tvm_arch) { -if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +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; + } +} -switch (scope) { -case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : -case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : +static bool iommufd_vdevice_tsm_req_op_valid (u32 op,u32 tvm_arch) +{ +switch (op) { +case TSM_REQ_READ_OBJECT : +case TSM_REQ_REGEN_OBJECT : +case TSM_REQ_OBJECT_INFO : +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 ;default :return false; }
@@ -99,7 +112,8 @@ int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd)struct iommufd_vdevice *vdev;struct iommu_vdevice_tsm_req *cmd = ucmd->cmd;struct tsm_guest_req_info info = { - .scope = cmd->scope, + .op = cmd->op, + .tvm_arch = cmd->tvm_arch, .req = { .user =u64_to_user_ptr (cmd->req_uptr), .is_kernel = false,
@@ -112,10 +126,10 @@ int iommufd_vdevice_tsm_req_ioctl(struct iommufd_ucmd *ucmd) .resp_len = cmd->resp_len, }; -if (cmd->__reserved) -return -EOPNOTSUPP ; +if (!iommufd_vdevice_tsm_req_arch_valid (cmd->tvm_arch)) +return -EINVAL ; -if (!iommufd_vdevice_tsm_req_scope_valid (cmd->scope)) +if (!iommufd_vdevice_tsm_req_op_valid (cmd->op, cmd->tvm_arch))return -EINVAL ; vdev =iommufd_get_vdevice (ucmd->ictx, cmd->vdevice_id);
drivers/pci/tsm.c +32 -7
@@ -378,7 +378,8 @@ 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 +* @op: guest-initiated request operation +* @tvm_arch: guest TVM architecture * @req_in: Input payload forwarded from the guest * @in_len: Length of @req_in * @req_out: Output payload buffer response to the guest
@@ -387,7 +388,7 @@ EXPORT_SYMBOL_GPL(pci_tsm_bind);* * 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. +* operation parameter limits requests to guest-initiated TSM operations. * 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. *
@@ -400,7 +401,9 @@ EXPORT_SYMBOL_GPL(pci_tsm_bind);* 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, +ssize_t pci_tsm_guest_req (struct pci_dev *pdev, +enum iommu_vdevice_tsm_guest_req_op op, +enum iommu_vdevice_tsm_guest_tvm_arch tvm_arch,sockptr_t req_in,size_t in_len,sockptr_t req_out,size_t out_len,u64 *tsm_code) {
@@ -408,9 +411,30 @@ ssize_t pci_tsm_guest_req(struct pci_dev *pdev, enum pci_tsm_req_scope scope,struct pci_tdi *tdi;int rc; -/* Forbid requests that are not directly related to TDISP operations */ -if (scope >PCI_TSM_REQ_STATE_CHANGE ) +switch (tvm_arch) { +case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA : +case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV : +case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX : +break ; +default :return -EINVAL ; + } + +switch (op) { +case TSM_REQ_READ_OBJECT : +case TSM_REQ_REGEN_OBJECT : +case TSM_REQ_OBJECT_INFO : +case TSM_REQ_VALIDATE_MMIO : +case TSM_REQ_SET_TDI_STATE : +break ; +case TSM_REQ_SEV_ENABLE_DMA : +case TSM_REQ_SEV_DISABLE_DMA : +if (tvm_arch ==IOMMU_VDEVICE_TSM_TVM_ARCH_SEV ) +break ; + fallthrough; +default : +return -EINVAL ; + }ACQUIRE (rwsem_read_intr, lock)(&pci_tsm_rwsem);if ((rc =ACQUIRE_ERR (rwsem_read_intr, &lock)))
@@ -430,8 +454,9 @@ ssize_t pci_tsm_guest_req(struct pci_dev *pdev, enum pci_tsm_req_scope scope, 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); +return to_pci_tsm_ops (pdev->tsm)->guest_req (tdi, op, tvm_arch, req_in, + in_len, req_out, out_len, + tsm_code); }EXPORT_SYMBOL_GPL (pci_tsm_guest_req);
drivers/virt/coco/tsm-core.c +3 -30
@@ -128,42 +128,15 @@ int tsm_unbind(struct device *dev) }EXPORT_SYMBOL_GPL (tsm_unbind); -static int tsm_pci_req_scope (u32 scope,enum pci_tsm_req_scope *pci_scope) -{ -switch (scope) { -case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : - *pci_scope =PCI_TSM_REQ_INFO ; -return 0 ; -case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : - *pci_scope =PCI_TSM_REQ_STATE_CHANGE ; -return 0 ; -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : - *pci_scope =PCI_TSM_REQ_DEBUG_READ ; -return 0 ; -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : - *pci_scope =PCI_TSM_REQ_DEBUG_WRITE ; -return 0 ; -default : -return -EINVAL ; - } -} -ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info,u64 *tsm_code) { -int ret; -enum pci_tsm_req_scope pci_scope; -if (!dev_is_pci (dev))return -EINVAL ; - ret =tsm_pci_req_scope (info->scope, &pci_scope); -if (ret) -return ret; - -return pci_tsm_guest_req (to_pci_dev (dev), pci_scope, info->req, - info->req_len, info->resp, info->resp_len, - tsm_code); +return pci_tsm_guest_req (to_pci_dev (dev), info->op, info->tvm_arch, + info->req, info->req_len, info->resp, + info->resp_len, tsm_code); }EXPORT_SYMBOL_GPL (tsm_guest_req);
include/linux/pci-tsm.h +7 -44
81 lines not on this page · raw message
include/linux/tsm.h +5 -2
@@ -7,6 +7,7 @@#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
@@ -132,14 +133,16 @@ int tsm_unbind(struct device *dev);/** * struct tsm_guest_req_info - parameter for tsm_guest_req() -* @scope: iommufd allocated scope for tsm guest 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 { -u32 scope; +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;
include/uapi/linux/iommufd.h +35 -43
108 lines not on this page · raw message
Alexey Kardashevskiy
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Dan Williams (nvidia)Quoted 70 lines · Dan Williams (nvidia)
[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
Quoted 27 lines · Alexey Kardashevskiy
Quoted 24 lines · Aneesh Kumar K.V (Arm)
Add
IOMMU_VDEVICE_TSM_REQUESTfor issuing TSM guest request/response transactions against an iommufd vdevice.The ioctl takes a
vdevice_idplus request/response user buffers and length fields, and forwards the request throughtsm_guest_req()to the PCI TSM backend. This provides the host-side passthrough path used by CoCo guests for TSM device attestation and acceptance flows after the device has been bound to TSM.Also add the supporting
tsm_guest_req()helper and associated TSM core interface definitions.Based on changes from: Alexey Kardashevskiy <[email protected]>
Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/iommufd_private.h | 6 ++ drivers/iommu/iommufd/main.c | 3 + drivers/iommu/iommufd/tsm.c | 68 +++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 39 ++++++++++++ include/linux/pci-tsm.h | 9 +-- include/linux/tsm.h | 25 ++++++++ include/uapi/linux/iommufd.h | 80 +++++++++++++++++++++++++ 7 files changed, 226 insertions(+), 4 deletions(-)[..]
Quoted 22 lines
Quoted 9 lines
drivers/iommu/iommufd/tsm.c +1
-0@@ -60,3 +60,71 @@ int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd)iommufd_put_object (ucmd->ictx, &vdev->obj);return rc; } +drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 73
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +10
-0+static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +{ +if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +return false; + +switch (scope) { +case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : +case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : +case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE :This scope thing still needs clarification.
Quoted 5 lines
I have 3 types of requests to fit here, all go via VM -> KVM -> QEMU -> IOMMUFD -> TSM.
1) bind/unbind TDI <- moves to
CONFIG_LOCKED, this is "OP";
2) start/stop TDI <- moves to RUN, this is "GR"? Right now I route it via "OP";
3) enable/disable MMIO/DMA <- no TDI state change, this is "GR" but which scope is it here?The scope parameter was meant to enumerate a security model for classes of commands that are otherwise opaque to the kernel. However, none of the commands we are targeting are opaque (private specification with unknown effect). It now turns out there is no role for @scope for security.
Now a command family that iommufd can validate seems useful. As it stands this implementation aliases command codes across TSMs. Do we proceed with creating an actual shared command uapi for the truly shared commands:
Quoted 4 lines
TSM_REQ_TYPE_DEFAULT: Commands every arch needsTSM_REQ_READ_OBJECTTSM_REQ_REGEN_OBJECTTSM_REQ_OBJECT_INFO
These 3 are already in that netlink interface of the TSM (so common for all arches), right?
Quoted 1 line
TSM_REQ_VALIDATE_MMIO
SEV handles this in the KVM as this is where RMP and NPT are managed + opaque guest request to the TSM, I'd think it is the same for others.
Quoted 1 line
TSM_REQ_SET_TDI_STATE
This is a common one.
Quoted 3 lines
TSM_REQ_TYPE_SEV: Commands only SEV needsTSM_REQ_SEV_ENABLE_DMATSM_REQ_SEV_DISABLE_DMA
No change to host owned part of the IOMMU when TDX or CCA moves the device to secure? Or it is packed into those opaque requests to the TSM?
Quoted 11 lines
...or just observe that per CC arch commands are needed to setup the VM so per CC arch commands are needed to marshal device assignment support requests.
In that case
pci_tsm_req_scopebecomestsm_req_typeand is just:
TSM_REQ_TYPE_CCATSM_REQ_TYPE_SEVTSM_REQ_TYPE_TDXI am leaning towards the latter at this point.
Dunno, besides the DMA thing, these CCA/SEV/TDX types will only appear in WARN_ON of the arch TSM drivers and will not really be seen. If a wrong TSM driver is loaded (say, TDX on AMD), then something just went terribly wrong. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Aneesh Kumar K.VAneesh Kumar K.V wrote:
[..]
Quoted 7 lines
Quoted 2 lines
I think we can wait to move it to its own IOMMU operation unless/until there is a need to set RUN outside of an explicit guest request, right?
Something like the below? (the diff against this series)
I have not yet integrated this into the full CCA patchset for testing, but I wanted to make sure we are aligned on the UAPI.
[..]
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 69
Quoted 8 lines
drivers/iommu/iommufd/tsm.c +5
-2-static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +static bool iommufd_vdevice_tsm_req_arch_valid (u32 tvm_arch) { -if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +switch (tvm_arch) { +case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA : +case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV : +case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX :
Makes sense for any command that needs tunneling. However, see below, what is that set, and do we need a IOMMU_VDEVICE_TSM_COMMON when architecture differentiation is not required?
Quoted 7 lines
drivers/iommu/iommufd/tsm.c +4
-1+return true; +default :return false; + } +} -switch (scope) {
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 81
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +6
-4-case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : -case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : +static bool iommufd_vdevice_tsm_req_op_valid (u32 op,u32 tvm_arch) +{ +switch (op) { +case TSM_REQ_READ_OBJECT : +case TSM_REQ_REGEN_OBJECT : +case TSM_REQ_OBJECT_INFO :
The design goal of the netlink device-evidence interface is to be able to respond to all shapes of requests for evidence. So netlink caches objects that the hypercall handler can fill responses from.
It eliminates a class of commands that need tunneling.
Review
Quoted 2 lines
(quoted diff) +2
-0+ case TSM_REQ_VALIDATE_MMIO: + case TSM_REQ_SET_TDI_STATE:
Are these potentially candidates for a IOMMU_VDEVICE_TSM_COMMON? The handler knows how to do the arch-specific response from the common iommufd result, or is there TSM-specific payload beyond @tsm_code for these.
Make it the case that guest_req only needs non-common arch for operations that are implementation unique, or where the response payload exceeds what can be conveyed via @tsm_code.
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 87
Quoted 4 lines
drivers/iommu/iommufd/tsm.c +3
-0return true; +case TSM_REQ_SEV_ENABLE_DMA : +case TSM_REQ_SEV_DISABLE_DMA : +return tvm_arch ==IOMMU_VDEVICE_TSM_TVM_ARCH_SEV ;
Right, this appears to be the only case where the command is implementation unique. The handler can only ask iommufd to take arch-specific action.
Aneesh Kumar K.V
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
Quoted 45 lines · Dan Williams (nvidia)
Aneesh Kumar K.V wrote:
[..]Quoted 7 lines
Quoted 2 lines
I think we can wait to move it to its own IOMMU operation unless/until there is a need to set RUN outside of an explicit guest request, right?
Something like the below? (the diff against this series)
I have not yet integrated this into the full CCA patchset for testing, but I wanted to make sure we are aligned on the UAPI.
[..]
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 69
Quoted 8 lines
drivers/iommu/iommufd/tsm.c +5
-2-static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +static bool iommufd_vdevice_tsm_req_arch_valid (u32 tvm_arch) { -if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +switch (tvm_arch) { +case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA : +case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV : +case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX :Makes sense for any command that needs tunneling. However, see below, what is that set, and do we need a
IOMMU_VDEVICE_TSM_COMMONwhen architecture differentiation is not required?Quoted 7 lines
drivers/iommu/iommufd/tsm.c +4
-1+return true; +default :return false; + } +} -switch (scope) {drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 81
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +6
-4-case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : -case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : +static bool iommufd_vdevice_tsm_req_op_valid (u32 op,u32 tvm_arch) +{ +switch (op) { +case TSM_REQ_READ_OBJECT : +case TSM_REQ_REGEN_OBJECT : +case TSM_REQ_OBJECT_INFO :The design goal of the netlink device-evidence interface is to be able to respond to all shapes of requests for evidence. So netlink caches objects that the hypercall handler can fill responses from.
Quoted 2 lines
It eliminates a class of commands that need tunneling.
Sure, I can drop this from the iommufd ioctl and use netlink to read and regenerate the objects from the VMM.
Can I use netlink to find the cached object size? CCA supports RHI_DA_OBJECT_SIZE, which can be used to query the object size. If not should we have TSM_REQ_OBJECT_INFO?
Quoted 9 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ case TSM_REQ_VALIDATE_MMIO: + case TSM_REQ_SET_TDI_STATE:Are these potentially candidates for a
IOMMU_VDEVICE_TSM_COMMON? The handler knows how to do the arch-specific response from the common iommufd result, or is there TSM-specific payload beyond @tsm_code for these.
Quoted 4 lines
Make it the case that
guest_reqonly needs non-common arch for operations that are implementation unique, or where the response payload exceeds what can be conveyed via @tsm_code.
I am not sure I follow the IOMMU_VDEVICE_TSM_COMMON feedback above.
Earlier discussions around this concluded that we may want iommufd to validate all input commands, rather than making the guest request ioctl a passthrough interface.
If we make the ops IOMMU_VDEVICE_TSM_COMMON, we would still need to add TSM_REQ_VALIDATE_MMIO and TSM_REQ_SET_TDI_STATE for the arch-specific handler. Why not expose those to the generic iommufd layer, so that we can add operation validation there and completely drop IOMMU_VDEVICE_TSM_COMMON?
Quoted 8 lines
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 87
Quoted 4 lines
drivers/iommu/iommufd/tsm.c +3
-0return true; +case TSM_REQ_SEV_ENABLE_DMA : +case TSM_REQ_SEV_DISABLE_DMA : +return tvm_arch ==IOMMU_VDEVICE_TSM_TVM_ARCH_SEV ;Right, this appears to be the only case where the command is implementation unique. The handler can only ask iommufd to take arch-specific action.
-aneesh
Alexey Kardashevskiy
maintainerRe: [PATCH v5 5/5] iommufd/vdevice: add TSM request ioctl
In reply to Dan Williams (nvidia)Quoted 10 lines · Dan Williams (nvidia)
Aneesh Kumar K.V wrote:
[..]Quoted 7 lines
Quoted 2 lines
I think we can wait to move it to its own IOMMU operation unless/until there is a need to set RUN outside of an explicit guest request, right?
Something like the below? (the diff against this series)
I have not yet integrated this into the full CCA patchset for testing, but I wanted to make sure we are aligned on the UAPI.
[..]
Quoted 12 lines
drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 69
Quoted 8 lines
drivers/iommu/iommufd/tsm.c +5
-2-static bool iommufd_vdevice_tsm_req_scope_valid (u32 scope) +static bool iommufd_vdevice_tsm_req_arch_valid (u32 tvm_arch) { -if (scope >IOMMU_VDEVICE_TSM_REQ_SCOPE_PCI_LAST ) +switch (tvm_arch) { +case IOMMU_VDEVICE_TSM_TVM_ARCH_CCA : +case IOMMU_VDEVICE_TSM_TVM_ARCH_SEV : +case IOMMU_VDEVICE_TSM_TVM_ARCH_TDX :Makes sense for any command that needs tunneling. However, see below, what is that set, and do we need a
IOMMU_VDEVICE_TSM_COMMONwhen architecture differentiation is not required?
I still do not follow why making these arches checks in runtime, should be caught at the build time (ARM vs x86 vs RiscV) or at the TSM modprobe (AMD vs Intel).
The scope becomes just IOMMU_VDEVICE_TSM_REQ_SCOPE_TUNNEL imho.
Quoted 23 lines
Quoted 8 lines
drivers/iommu/iommufd/tsm.c +4
-1+return true; +default :return false; + } +} -switch (scope) {drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 81
Quoted 10 lines
drivers/iommu/iommufd/tsm.c +6
-4-case IOMMU_VDEVICE_TSM_REQ_PCI_INFO : -case IOMMU_VDEVICE_TSM_REQ_PCI_STATE_CHANGE : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_READ : -case IOMMU_VDEVICE_TSM_REQ_PCI_DEBUG_WRITE : +static bool iommufd_vdevice_tsm_req_op_valid (u32 op,u32 tvm_arch) +{ +switch (op) { +case TSM_REQ_READ_OBJECT : +case TSM_REQ_REGEN_OBJECT : +case TSM_REQ_OBJECT_INFO :The design goal of the netlink device-evidence interface is to be able to respond to all shapes of requests for evidence. So netlink caches objects that the hypercall handler can fill responses from.
Quoted 1 line
It eliminates a class of commands that need tunneling.
+1.
Quoted 8 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ case TSM_REQ_VALIDATE_MMIO: + case TSM_REQ_SET_TDI_STATE:Are these potentially candidates for a
IOMMU_VDEVICE_TSM_COMMON? The handler knows how to do the arch-specific response from the common iommufd result, or is there TSM-specific payload beyond @tsm_code for these.
These are not common to put in IOMMUFD - it is either TSM (for TDI states) or KVM (for MMIO validate) on AMD and other arches won't share much either, right?
Quoted 11 lines
Make it the case that
guest_reqonly needs non-common arch for operations that are implementation unique, or where the response payload exceeds what can be conveyed via @tsm_code.drivers/iommu/iommufd/tsm.c int iommufd_vdevice_tsm_op_ioctl(struct iommufd_ucmd *ucmd) · line 87
Quoted 4 lines
return true;drivers/iommu/iommufd/tsm.c +3
-0+case TSM_REQ_SEV_ENABLE_DMA : +case TSM_REQ_SEV_DISABLE_DMA : +return tvm_arch ==IOMMU_VDEVICE_TSM_TVM_ARCH_SEV ;Right, this appears to be the only case where the command is implementation unique. The handler can only ask iommufd to take arch-specific action.
There are 2 arch-specific actions - one is calling TSM to execute actual guest request, the other one is notifying the host IOMMU driver about the device going secure. Like this:
https://github.com/AMDESE/linux-kvm/blob/tsm/drivers/iommu/iommufd/viommu.c#L603
I can tuck domain->ops->tsm_enable into my TSM but rather would not. Thanks,
Signature · 1 line
Alexey
Re: [PATCH v5 1/5] vfio: cache KVM VM file references instead of raw struct kvm pointers
In reply to Aneesh Kumar K.V (Arm)Quoted 22 lines · Aneesh Kumar K.V (Arm)
VFIO currently records struct kvm pointers on
vfio_group,vfio_device_fileand the openedvfio_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_DEVICEbinds the KVM VM lifetime to the KVM device fd lifetime. ForKVM_DEV_TYPE_VFIO, the KVM VFIO device fd also takes references to each VFIO file added throughKVM_DEV_VFIO_FILE_ADD. The KVM VFIO device fd therefore owns both the internal KVM reference and the VFIO file references inkvf->file.
KVM_DEV_VFIO_FILE_ADDfurther installs the VM file association into the VFIO file. VFIO converts the struct kvm pointer to a VM file reference withget_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_DELor 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.
Quoted 5 lines
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_filefor driver and iommufd use. That open-time reference is released fromvfio_device_put_kvm()when the VFIO device is closed or unbound.
can the "VM file" that VFIO caches be abstracted from "the KVM VM file" to "a VM file the VMM registered"?
Quoted 395 lines
This gives the ownership model:
- KVM device fd pins struct kvm through
kvm->users_count
- KVM VFIO device fd pins VFIO files throughkvf->file
- VFIO group/device-file state pins the VM file while associated with KVM
-vfio_device::kvm_filepins the VM file during active VFIO device useSigned-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -442,55 +442,61 @@ void vfio_unregister_group_dev(struct vfio_device *device)EXPORT_SYMBOL_GPL (vfio_unregister_group_dev);#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 (*pfn)(struct kvm *kvm); -bool (*fn)(struct kvm *kvm); -bool ret; +struct file *old_kvm_file, *new_kvm_file =NULL ; -lockdep_assert_held (&device->dev_set->lock); +/* +* @kvm can outlive the VM fd and its final __fput(). Only take a +* new reference if the VM file is still active. +*/ +if (kvm) + new_kvm_file =get_file_active (&kvm->_file); -if (!kvm) -return ; +spin_lock (lock); + old_kvm_file = *dst; + *dst = new_kvm_file; +spin_unlock (lock); - pfn =symbol_get (kvm_put_kvm); -if (WARN_ON (!pfn)) -return ; +if (old_kvm_file) +fput (old_kvm_file); +} - fn =symbol_get (kvm_get_kvm_safe); -if (WARN_ON (!fn)) { -symbol_put (kvm_put_kvm); -return ; - } +void vfio_device_get_kvm_safe (struct vfio_device *device,struct file *kvm_file) +{ +lockdep_assert_held (&device->dev_set->lock); - ret =fn (kvm); -symbol_put (kvm_get_kvm_safe); -if (!ret) { -symbol_put (kvm_put_kvm); -return ; - } +/* +* Take a VM file reference if the KVM fd is still active. +*/ +if (kvm_file) + kvm_file =get_file (kvm_file); - device->put_kvm = pfn; - device->kvm = kvm; + device->kvm_file = kvm_file; }void vfio_device_put_kvm (struct vfio_device *device) { +struct file *kvm_file; +lockdep_assert_held (&device->dev_set->lock); -if (!device->kvm) + kvm_file = device->kvm_file; +if (!kvm_file)return ; -if (WARN_ON (!device->put_kvm)) - goto clear; + device->kvm_file =NULL ; +fput (kvm_file); +} - device->put_kvm (device->kvm); - device->put_kvm =NULL ; -symbol_put (kvm_put_kvm); +struct kvm *vfio_device_get_kvm (struct vfio_device *device) +{ +if (!device->kvm_file) +return NULL ; -clear: - device->kvm =NULL ; +return device->kvm_file->private_data; } +EXPORT_SYMBOL_GPL (vfio_device_get_kvm);#endif /* true if the vfio_device has open_device() called but not close_device() */ @@ -1518,13 +1524,10 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)struct vfio_device_file *df = file->private_data;/* -* The kvm is first recorded in the vfio_device_file, and will -* be propagated to vfio_device::kvm when the file is bound to -* iommufd successfully in the vfio device cdev path. +* Cache the VM file reference associated with this VFIO file so it +* can be pinned into vfio_device while the device is open. */ -spin_lock (&df->kvm_ref_lock); - df->kvm = kvm; -spin_unlock (&df->kvm_ref_lock); +vfio_kvm_file_replace (&df->kvm_file, &df->kvm_ref_lock, kvm); }/** @@ -1532,8 +1535,8 @@ static void vfio_device_file_set_kvm(struct file *file, struct kvm *kvm)* @file: VFIO group file or VFIO device file * @kvm: KVM to link * -* When a VFIO device is first opened the KVM will be available in -* device->kvm if one was associated with the file. +* When a VFIO device is first opened, VFIO caches a VM file reference if +* one was associated with the file. */ void vfio_file_set_kvm (struct file *file,struct kvm *kvm) {include/linux/kvm_host.h +3
-0@@ -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 @@ -861,6 +863,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@@ -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@@ -1351,6 +1351,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 ; }@@ -5500,6 +5501,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
Re: [PATCH v5 2/5] iommufd/device: Associate KVM file pointer with iommufd_device
In reply to Aneesh Kumar K.V (Arm)Quoted 36 lines · 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 theiommufd_device. The KVM file reference is owned by VFIO and is already held for the duration of the device open path.Signed-off-by Shameer Kolothum <[email protected]>Reviewed-by Jason Gunthorpe <[email protected]>[nicolinc: fix build error in iommufd_test_mock_domain()]Signed-off-by Nicolin Chen <[email protected]>[aneesh.kumar: Switch to use kvm_file]Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- 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 +1
-0@@ -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
drivers/iommu/iommufd/device.c void iommufd_device_destroy(struct iommufd_object *obj) · line 222
Quoted 7 lines
drivers/iommu/iommufd/device.c +3
-1@@ -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)
could it be a neutrally-named VM-file handle (e.g. vm_file)?
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 271
Quoted 7 lines
drivers/iommu/iommufd/device.c +0
-0{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;
For a non-KVM VMM a registrable "which VM-file type" hook (a file_is_mshv_partition() analogue) rather than an implicit "whatever VFIO set" contract will be helpful, so the type can be validated where it matters.
Quoted 63 lines
drivers/iommu/iommufd/device.c +2
-0+/* 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@@ -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@@ -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@@ -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@@ -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
Re: [PATCH v5 3/5] iommufd/viommu: Keep a reference to the KVM file
In reply to Aneesh Kumar K.V (Arm)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_viommuwhen 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]Signed-off-by Nicolin Chen <[email protected]>[aneesh.kumar: Switch to use kvm_file]Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/viommu.c | 5 +++++ include/linux/iommufd.h | 1 + 2 files changed, 6 insertions(+)drivers/iommu/iommufd/viommu.c +3
-0@@ -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);
There is now no uapi path at all for a non-KVM VMM to associate its VM with a viommu. Can we (re)introduce an explicit, neutral association, that accepts a KVM fd or another VMM's VM fd, in addition to the implicit VFIO path?
Quoted 18 lines
drivers/iommu/iommufd/viommu.c +0
-0viommu->type = cmd->type; viommu->ictx = ucmd->ictx; viommu->hwpt = hwpt_paging;include/linux/iommufd.h +1
-0@@ -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