Add iommufd ioctls to support TSM operations
Subsystems (5)
Aneesh Kumar K.V (Arm)
maintainerv4 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 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) (2):
iommufd/tsm: add vdevice TSM bind/unbind ioctl
iommufd/vdevice: add TSM guest request ioctl
Nicolin Chen (1):
iommufd/viommu: Associate a kvm pointer to iommufd_viommu
Shameer Kolothum (1):
iommufd/device: Associate a kvm pointer to iommufd_device
drivers/iommu/iommufd/Makefile | 2 + drivers/iommu/iommufd/device.c | 5 +- drivers/iommu/iommufd/iommufd_private.h | 17 ++++ drivers/iommu/iommufd/main.c | 6 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/iommu/iommufd/tsm.c | 109 ++++++++++++++++++++++++ drivers/iommu/iommufd/viommu.c | 24 +++++- drivers/vfio/iommufd.c | 2 +- drivers/virt/coco/tsm-core.c | 30 +++++++ include/linux/iommu.h | 1 + include/linux/iommufd.h | 5 +- include/linux/tsm.h | 42 +++++++++ include/uapi/linux/iommufd.h | 67 +++++++++++++++ 13 files changed, 305 insertions(+), 7 deletions(-) create mode 100644 drivers/iommu/iommufd/tsm.c
Signature · 1 line
2.43.0
Aneesh Kumar K.V (Arm)
maintainerPATCH v4 1/4 iommufd/device: Associate a kvm pointer to iommufd_device
From: Shameer Kolothum <[email protected]>
Add a struct kvm * to iommufd_device_bind() fn and associate it with idev if bind is successful.
--- drivers/iommu/iommufd/device.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 4 +++- 5 files changed, 11 insertions(+), 4 deletions(-)
drivers/iommu/iommufd/device.c +4 -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: Pointer to struct kvm 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,8 @@ 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 kvm *kvm, +u32 *id) {struct iommufd_device *idev;struct iommufd_group *igroup;
@@ -266,6 +268,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (!iommufd_selftest_is_mock_dev (dev))iommufd_ctx_get (ictx); idev->dev = dev; + idev->kvm = kvm; 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 kvm if available */ +struct kvm *kvm;bool enforce_cache_coherency;struct iommufd_vdevice *vdev;bool destroying;
drivers/iommu/iommufd/selftest.c +1 -1
@@ -1100,7 +1100,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 +1 -1
@@ -119,7 +119,7 @@ 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, out_device_id);if (IS_ERR (idev))return PTR_ERR (idev); vdev->iommufd_device = idev;
include/linux/iommufd.h +3 -1
@@ -24,6 +24,7 @@ struct iommufd_ctx;struct iommufd_device ;struct iommufd_viommu_ops ;struct page ; +struct kvm ;enum iommufd_object_type {IOMMUFD_OBJ_NONE ,
@@ -59,7 +60,8 @@ struct iommufd_object { };struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id); +struct device *dev,struct kvm *kvm, +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 v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 100 lines · Aneesh Kumar K.V (Arm)
From: Shameer Kolothum <[email protected]>
Add a struct kvm * to
iommufd_device_bind()fn and associate it with idev if bind is successful.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]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/device.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 4 +++- 5 files changed, 11 insertions(+), 4 deletions(-)drivers/iommu/iommufd/device.c +4
-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: Pointer to struct kvm 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,8 @@ 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 kvm *kvm, +u32 *id) {struct iommufd_device *idev;struct iommufd_group *igroup;@@ -266,6 +268,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (!iommufd_selftest_is_mock_dev (dev))iommufd_ctx_get (ictx); idev->dev = dev; + idev->kvm = kvm; 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 kvm if available */ +struct kvm *kvm;bool enforce_cache_coherency;struct iommufd_vdevice *vdev;bool destroying;drivers/iommu/iommufd/selftest.c +1
-1@@ -1100,7 +1100,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 +1
-1@@ -119,7 +119,7 @@ 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, out_device_id);if (IS_ERR (idev))return PTR_ERR (idev); vdev->iommufd_device = idev;include/linux/iommufd.h +1
-0@@ -24,6 +24,7 @@ struct iommufd_ctx;struct iommufd_device ;struct iommufd_viommu_ops ;struct page ; +struct kvm ;enum iommufd_object_type {IOMMUFD_OBJ_NONE ,
include/linux/iommufd.h struct iommufd_object { · line 67
Quoted 9 lines
include/linux/iommufd.h +2
-1@@ -59,7 +60,8 @@ struct iommufd_object { };struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx, -struct device *dev,u32 *id); +struct device *dev,struct kvm *kvm, +u32 *id);void iommufd_device_unbind (struct iommufd_device *idev); int iommufd_device_attach(struct iommufd_device *idev, ioasid_t pasid,
iommufd_device_unbind() is an asynchronous teardown operation. Is it possible for idev->kvm to persist or be accessed within the iommufd subsystem after iommufd_device_unbind() has returned? Should we add the change below to prevent this potential UAF case?
drivers/iommu/iommufd/device.c +1 -0
@@ -333,6 +333,7 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, "IOMMUFD");*/ void iommufd_device_unbind (struct iommufd_device *idev) { + idev->kvm =NULL ;iommufd_object_destroy_user (idev->ictx, &idev->obj); }EXPORT_SYMBOL_NS_GPL (iommufd_device_unbind,"IOMMUFD" );
Otherwise, it looks good to me.
Re: [PATCH v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 10 lines · Baolu Lu
iommufd_device_unbind()is an asynchronous teardown operation. Is it possible foridev->kvmto persist or be accessed within the iommufd subsystem afteriommufd_device_unbind()has returned? Should we add the change below to prevent this potential UAF case?drivers/iommu/iommufd/device.c +0
-0
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, "IOMMUFD"); · line 339
Quoted 7 lines
drivers/iommu/iommufd/device.c +1
-0@@ -333,6 +333,7 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, "IOMMUFD");*/ void iommufd_device_unbind (struct iommufd_device *idev) { + idev->kvm =NULL ;iommufd_object_destroy_user (idev->ictx, &idev->obj); }EXPORT_SYMBOL_NS_GPL (iommufd_device_unbind,"IOMMUFD" );
iommufd_object_destroy_user() frees the memory of idev, it is not useful to null before free.
Jason
Re: [PATCH v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
In reply to Aneesh Kumar K.V (Arm)Quoted 31 lines · Aneesh Kumar K.V (Arm)
From: Shameer Kolothum <[email protected]>
Add a struct kvm * to
iommufd_device_bind()fn and associate it with idev if bind is successful.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]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/device.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 4 +++- 5 files changed, 11 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: Pointer to struct kvm 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 221
Quoted 6 lines
drivers/iommu/iommufd/device.c +2
-1@@ -216,7 +217,8 @@ 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 kvm *kvm, +u32 *id)
I thought we were trying to get away from struct kvm?
https://lore.kernel.org/all/[email protected]/
Ie this should be a 'struct file *kvm_fd'
?
Though I am wondering how practical it is to do this at this moment :\
Maybe ask Paolo how his series is going?
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 271
Quoted 8 lines
drivers/iommu/iommufd/device.c +1
-0{struct iommufd_device *idev;struct iommufd_group *igroup;@@ -266,6 +268,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (!iommufd_selftest_is_mock_dev (dev))iommufd_ctx_get (ictx); idev->dev = dev; + idev->kvm = kvm;
If we do that then I'd take a file refcount here ^^^
And then put it back when the idev is freed
That makes the lifecycle robust.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 41 lines · Jason Gunthorpe
Quoted 31 lines · Aneesh Kumar K.V (Arm)
From: Shameer Kolothum <[email protected]>
Add a struct kvm * to
iommufd_device_bind()fn and associate it with idev if bind is successful.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]>Signed-off-by Aneesh Kumar K.V (Arm) <[email protected]>--- drivers/iommu/iommufd/device.c | 5 ++++- drivers/iommu/iommufd/iommufd_private.h | 2 ++ drivers/iommu/iommufd/selftest.c | 2 +- drivers/vfio/iommufd.c | 2 +- include/linux/iommufd.h | 4 +++- 5 files changed, 11 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: Pointer to struct kvm 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 221
Quoted 6 lines
drivers/iommu/iommufd/device.c +2
-1@@ -216,7 +217,8 @@ 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 kvm *kvm, +u32 *id)I thought we were trying to get away from struct kvm?
Quoted 8 lines
https://lore.kernel.org/all/[email protected]/
Ie this should be a 'struct file
*kvm_fd'?
Though I am wondering how practical it is to do this at this moment :\
Should we also switch
modified drivers/vfio/vfio.h
@@ -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 blocking_notifier_head notifier;
struct iommufd_ctx *iommufd;
ie,KVM_CREATE_DEVICE with KVM_DEV_TYPE_VFIO still use kvm->users_count, KVM_DEV_VFIO_FILE_ADD -> will switch to get_file(kvm->_file); and VFIO_DEVICE_BIND_IOMMUFD -> will switch to get_file(df->kvm_file)
Quoted 13 lines
Maybe ask Paolo how his series is going?
drivers/iommu/iommufd/device.c struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, · line 271
Quoted 8 lines
drivers/iommu/iommufd/device.c +1
-0{struct iommufd_device *idev;struct iommufd_group *igroup;@@ -266,6 +268,7 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,if (!iommufd_selftest_is_mock_dev (dev))iommufd_ctx_get (ictx); idev->dev = dev; + idev->kvm = kvm;If we do that then I'd take a file refcount here ^^^
Quoted 5 lines
And then put it back when the idev is freed
That makes the lifecycle robust.
Jason
-aneesh
Re: [PATCH v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 26 lines · Aneesh Kumar K.V
Quoted 10 lines
I thought we were trying to get away from struct kvm?
https://lore.kernel.org/all/[email protected]/
Ie this should be a 'struct file
*kvm_fd'?
Though I am wondering how practical it is to do this at this moment :\
Should we also switch
modified
drivers/vfio/vfio.h(quoted diff) +2
-2@@ -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 */ };
Review struct vfio_group { · line 94
Quoted 8 lines
(quoted diff) +1
-1@@ -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 blocking_notifier_head notifier; struct iommufd_ctx *iommufd;
Ideally, yes everything outside kvm should just use the struct file and there should be quite narrow places where you do some kvm operation on it.
Quoted 6 lines
ie,
KVM_CREATE_DEVICEwithKVM_DEV_TYPE_VFIOstill usekvm->users_count,KVM_DEV_VFIO_FILE_ADD-> will switch toget_file(kvm->_file); andVFIO_DEVICE_BIND_IOMMUFD-> will switch toget_file(df->kvm_file)Quoted 1 line
Maybe ask Paolo how his series is going?
I thought the above is what Paolo's series was going after?
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 40 lines · Jason Gunthorpe
Quoted 26 lines · Aneesh Kumar K.V
Quoted 10 lines
I thought we were trying to get away from struct kvm?
https://lore.kernel.org/all/[email protected]/
Ie this should be a 'struct file
*kvm_fd'?
Though I am wondering how practical it is to do this at this moment :\
Should we also switch
modified
drivers/vfio/vfio.h(quoted diff) +2
-2@@ -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 */ };Review struct vfio_group { · line 94
Quoted 8 lines
(quoted diff) +1
-1@@ -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 blocking_notifier_head notifier; struct iommufd_ctx *iommufd;Ideally, yes everything outside kvm should just use the struct file and there should be quite narrow places where you do some kvm operation on it.
Quoted 9 lines
Quoted 6 lines
ie,
KVM_CREATE_DEVICEwithKVM_DEV_TYPE_VFIOstill usekvm->users_count,KVM_DEV_VFIO_FILE_ADD-> will switch toget_file(kvm->_file); andVFIO_DEVICE_BIND_IOMMUFD-> will switch toget_file(df->kvm_file)Quoted 1 line
Maybe ask Paolo how his series is going?
I thought the above is what Paolo's series was going after?
Hi Paolo,
Could you please take a look at this? I’ve tried to document the reference count details and hope I’ve captured everything correctly, but I’d appreciate your review to make sure I haven’t missed anything.
From 141fb9430d6c4227144221410121780c6681d1ce Mon Sep 17 00:00:00 2001
From: "Aneesh Kumar K.V (Arm)" <[email protected]>
Date: Wed, 29 Apr 2026 12:14:36 +0530
Subject: [PATCH] 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 of a raw struct kvm pointer so that VFIO holds a reference to the VM fd's struct file, rather than caching a struct kvm pointer and using KVM-managed object lifetime (kvm->users_count) to make that pointer usable.
This also lets VFIO stop taking KVM object references through kvm_get_kvm_safe()/kvm_put_kvm() via symbol_get(), and instead pin the VM through references to kvm->_file.
KVM publishes the association through KVM_DEV_VFIO_FILE_ADD:
- kvm_vfio_file_add() gets the userspace VFIO file with fget(fd)
- it then takes its own persistent reference with kvf->file = get_file(filp)
- that kvf->file reference is owned by the KVM VFIO device and lives on
kv->file_list until KVM_DEV_VFIO_FILE_DEL or kvm_vfio_release()
- kvm_vfio_file_set_kvm(kvf->file, dev->kvm) then updates the VFIO side association to the VM
On the VFIO side, vfio_kvm_file_replace() converts struct kvm to a VM file reference with get_file_active(&kvm->_file). get_file_active() is required because dev->kvm may still be alive due to the KVM device fd's kvm_get_kvm() reference from KVM_CREATE_DEVICE, while the VM fd itself may already be in final __fput(). In that case kvm->_file must not be re-acquired with plain get_file().
The stored group/device-file kvm_file references are long-lived association references managed by KVM:
- they are installed from KVM_DEV_VFIO_FILE_ADD
- they are replaced/cleared by kvm_vfio_file_set_kvm()
- they are dropped from kvm_vfio_file_del() and kvm_vfio_release(), which
call vfio_file_set_kvm(file, NULL) before dropping kvf->file
The opened vfio_device still takes its own kvm_file reference for driver use:
- group path: vfio_device_group_get_kvm_safe()
- cdev/iommufd path: vfio_df_get_kvm_safe() from VFIO_DEVICE_BIND_IOMMUFD
- both paths call vfio_device_get_kvm_safe(), which get_file()s the
already-associated kvm_file and stores it in device->kvm_file
- that open-time reference is dropped by vfio_device_put_kvm() when the device is closed or unbound
This gives each layer a clear ownership model:
- KVM device fd: pins struct kvm through kvm->users_count via
kvm_get_kvm()/kvm_put_kvm()
- VM fd and VFIO kvm_file references: pin the VM file through kvm->_file
- kvf->file: pins the VFIO file until KVM removes the association
- device->kvm_file: pins the VM file for the duration of an active VFIO device open
--- drivers/vfio/device_cdev.c | 10 ++--- drivers/vfio/group.c | 14 +++---- drivers/vfio/vfio.h | 16 +++++--- drivers/vfio/vfio_main.c | 76 ++++++++++++++++++-------------------- include/linux/kvm_host.h | 3 ++ include/linux/vfio.h | 3 +- virt/kvm/kvm_main.c | 1 + 7 files changed, 62 insertions(+), 61 deletions(-)
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
@@ -158,7 +158,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); }
@@ -176,10 +176,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);
@@ -860,9 +860,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/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 blocking_notifier_head notifier;struct iommufd_ctx *iommufd;
@@ -435,11 +435,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 +35 -41
110 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
@@ -860,6 +862,7 @@ struct kvm {struct srcu_struct srcu;struct srcu_struct irq_srcu;pid_t userspace_pid; +struct file *_file;bool override_halt_poll_ns;unsigned int max_halt_poll_ns;u32 dirty_ring_size;
include/linux/vfio.h +1 -2
@@ -52,7 +52,7 @@ struct vfio_device {struct vfio_device_set *dev_set;struct list_head dev_set_list;unsigned int migration_flags; -struct kvm *kvm; +struct file *kvm_file;/* Members below here are private, not for driver use */ unsigned int index;
@@ -64,7 +64,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 +1 -0
@@ -5507,6 +5507,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type) r =PTR_ERR (file); goto put_kvm; } + 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 v4 1/4] iommufd/device: Associate a kvm pointer to iommufd_device
Quoted 50 lines · Aneesh Kumar K.V
Quoted 47 lines · Jason Gunthorpe
Quoted 26 lines · Aneesh Kumar K.V
Quoted 10 lines
I thought we were trying to get away from struct kvm?
https://lore.kernel.org/all/[email protected]/
Ie this should be a 'struct file
*kvm_fd'?
Though I am wondering how practical it is to do this at this moment :\
Should we also switch
modified
drivers/vfio/vfio.hdrivers/vfio/vfio.h +2
-2@@ -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 */ };drivers/vfio/vfio.h struct vfio_group { · line 94
Quoted 8 lines
drivers/vfio/vfio.h +1
-1@@ -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 blocking_notifier_head notifier;struct iommufd_ctx *iommufd;Ideally, yes everything outside kvm should just use the struct file and there should be quite narrow places where you do some kvm operation on it.
Quoted 6 lines
ie,
KVM_CREATE_DEVICEwithKVM_DEV_TYPE_VFIOstill usekvm->users_count,KVM_DEV_VFIO_FILE_ADD-> will switch toget_file(kvm->_file); andVFIO_DEVICE_BIND_IOMMUFD-> will switch toget_file(df->kvm_file)Quoted 1 line
Maybe ask Paolo how his series is going?
Quoted 8 lines
Quoted 2 lines
I thought the above is what Paolo's series was going after?
Hi Paolo,
Could you please take a look at this? I’ve tried to document the reference count details and hope I’ve captured everything correctly, but I’d appreciate your review to make sure I haven’t missed anything.
Reminder?
Jason
Aneesh Kumar K.V (Arm)
maintainerPATCH v4 2/4 iommufd/viommu: Associate a kvm pointer to iommufd_viommu
In reply to Aneesh Kumar K.V (Arm)From: Nicolin Chen <[email protected]>
For TSM use case, vIOMMU will need to get access to KVM to make different TSM calls. Associate the kvm pointer to the vIOMMU and take kvm's refcount.
Based on an original patch by Shameer Kolothum <[email protected]>
--- drivers/iommu/iommufd/viommu.c | 20 +++++++++++++++++--- include/linux/iommu.h | 1 + include/linux/iommufd.h | 1 + 3 files changed, 19 insertions(+), 3 deletions(-)
drivers/iommu/iommufd/viommu.c +17 -3
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only /* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES */ +#include <linux/kvm_host.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) +kvm_put_kvm (viommu->kvm);xa_destroy (&viommu->vdevs); }
@@ -68,14 +71,22 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) goto out_put_hwpt; } +if (idev->kvm && !kvm_get_kvm_safe (idev->kvm)) { + rc = -ENOENT ; + goto out_put_hwpt; + } + viommu = (struct iommufd_viommu *)_iommufd_object_alloc_ucmd ( ucmd, viommu_size,IOMMUFD_OBJ_VIOMMU );if (IS_ERR (viommu)) { rc =PTR_ERR (viommu); - goto out_put_hwpt; + goto out_put_kvm; }xa_init (&viommu->vdevs); + viommu->kvm = idev->kvm; +if (viommu->kvm) +kvm_get_kvm (viommu->kvm); viommu->type = cmd->type; viommu->ictx = ucmd->ictx; viommu->hwpt = hwpt_paging;
@@ -92,17 +103,20 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) rc = ops->viommu_init (viommu, hwpt_paging->common.domain, user_data.len ? &user_data :NULL );if (rc) - goto out_put_hwpt; + goto out_put_kvm;/* It is a driver bug that viommu->ops isn't filled */ if (WARN_ON_ONCE (!viommu->ops)) { rc = -EOPNOTSUPP ; - goto out_put_hwpt; + goto out_put_kvm; } cmd->out_viommu_id = viommu->obj.id; rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); +out_put_kvm: +if (idev->kvm) +kvm_put_kvm (idev->kvm); out_put_hwpt:iommufd_put_object (ucmd->ictx, &hwpt_paging->common.obj); out_put_idev:
include/linux/iommu.h +1 -0
@@ -46,6 +46,7 @@ struct iommu_dma_msi_cookie;struct iommu_fault_param ;struct iommufd_ctx ;struct iommufd_viommu ; +struct kvm ;struct msi_desc ;struct msi_msg ;
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 kvm *kvm;const struct iommufd_viommu_ops *ops;
Signature · 1 line
2.43.0
Re: [PATCH v4 2/4] iommufd/viommu: Associate a kvm pointer to iommufd_viommu
drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 77
Quoted 8 lines · Aneesh Kumar K.V (Arm)
drivers/iommu/iommufd/viommu.c +4
-0@@ -68,14 +71,22 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) goto out_put_hwpt; } +if (idev->kvm && !kvm_get_kvm_safe (idev->kvm)) { + rc = -ENOENT ; + goto out_put_hwpt; + }
Can we actually do this or is this circular module references if kvm isn't built in? kvm->vfio->iommufd->kvm isn't it?
This is why I want to use file *...
Jason
Aneesh Kumar K.V (Arm)
maintainerPATCH v4 3/4 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 | 60 +++++++++++++++++++++++++ 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, 139 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 +60 -0
60 lines not on this page · raw message
drivers/iommu/iommufd/viommu.c +4 -0
@@ -3,6 +3,8 @@*/ #include <linux/kvm_host.h> #include "iommufd_private.h" +#include <linux/cleanup.h> +#include <linux/tsm.h> void iommufd_viommu_destroy (struct iommufd_object *obj) {
@@ -133,6 +135,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
@@ -240,6 +240,25 @@ void tsm_ide_stream_unregister(struct pci_ide *ide) }EXPORT_SYMBOL_GPL (tsm_ide_stream_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
@@ -126,4 +126,21 @@ struct tsm_dev *find_tsm_dev(int id);struct pci_ide ;int tsm_ide_stream_register (struct pci_ide *ide);void tsm_ide_stream_unregister (struct pci_ide *ide); + +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, };/**
@@ -1134,6 +1135,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 v4 4/4 iommufd/vdevice: add TSM guest request ioctl
In reply to Aneesh Kumar K.V (Arm)Add IOMMU_VDEVICE_TSM_GUEST_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 | 7 ++++ drivers/iommu/iommufd/main.c | 3 ++ drivers/iommu/iommufd/tsm.c | 49 +++++++++++++++++++++++++ drivers/virt/coco/tsm-core.c | 11 ++++++ include/linux/tsm.h | 25 +++++++++++++ include/uapi/linux/iommufd.h | 41 +++++++++++++++++++++ 6 files changed, 136 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +7 -0
@@ -701,11 +701,18 @@ 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_guest_request_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_guest_request_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_guest_request gr;#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_GUEST_REQUEST ,iommufd_vdevice_tsm_guest_request_ioctl , +struct iommu_vdevice_tsm_guest_request ,resp_uptr ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
drivers/iommu/iommufd/tsm.c +49 -0
52 lines not on this page · raw message
drivers/virt/coco/tsm-core.c +11 -0
@@ -259,6 +259,17 @@ int tsm_unbind(struct device *dev) }EXPORT_SYMBOL_GPL (tsm_unbind); +ssize_t tsm_guest_req (struct device *dev,struct tsm_guest_req_info *info) +{ +if (!dev_is_pci (dev)) +return -EINVAL ; + +return pci_tsm_guest_req (to_pci_dev (dev), info->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/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
@@ -131,6 +132,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: 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) {
@@ -141,6 +159,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 +41 -0
51 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v4 4/4] iommufd/vdevice: add TSM guest request ioctl
Quoted 9 lines · Aneesh Kumar K.V (Arm)
+/**
+ * structiommu_vdevice_tsm_guest_request-ioctl(IOMMU_VDEVICE_TSM_GUEST_REQUEST)
+ * @size:sizeof(struct iommu_vdevice_tsm_guest_request)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @scope: Bus-specific scope classification for the guest request
+ * @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
Quoted 8 lines
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ *
+ * Forward a guest request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The meaning and valid values of @scope are defined by the TSM backend for
+ * the device bus type.
If you want to do this then you have to also provide a way to discover what the TSM backend is so userspace can form the correct numbers.
Also, since scope is now a uapi whatever in tsm is defining the values must be moved to the uapi headers.
Probably this is not a good idea and iommufd should just have its own enum with unique numbers for every bus and do the validation and translation.
Jason
Aneesh Kumar K.V
maintainerRe: [PATCH v4 4/4] iommufd/vdevice: add TSM guest request ioctl
Quoted 19 lines · Jason Gunthorpe
Quoted 9 lines · Aneesh Kumar K.V (Arm)
+/**
+ * structiommu_vdevice_tsm_guest_request-ioctl(IOMMU_VDEVICE_TSM_GUEST_REQUEST)
+ * @size:sizeof(struct iommu_vdevice_tsm_guest_request)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @scope: Bus-specific scope classification for the guest request
+ * @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 payloadQuoted 8 lines
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ *
+ * Forward a guest request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The meaning and valid values of @scope are defined by the TSM backend for
+ * the device bus type.
Quoted 3 lines
If you want to do this then you have to also provide a way to discover what the TSM backend is so userspace can form the correct numbers.
These guest-driven requests end up in the correct VMM backend, which should already be aware of the scope value to use. In the case of ARM CCA, the RHI calls include a device ID (vdev_id), which the VMM can use to determine the appropriate scope value.
Quoted 4 lines
Also, since scope is now a uapi whatever in tsm is defining the values must be moved to the uapi headers.
Sure, I will update this in the next revision. Dan, should this be part of the TSM series?
Quoted 4 lines
Probably this is not a good idea and iommufd should just have its own enum with unique numbers for every bus and do the validation and translation.
-aneesh
Re: [PATCH v4 4/4] iommufd/vdevice: add TSM guest request ioctl
Quoted 21 lines · Aneesh Kumar K.V
Quoted 19 lines · Jason Gunthorpe
Quoted 17 lines · Aneesh Kumar K.V (Arm)
+/**
+ * structiommu_vdevice_tsm_guest_request-ioctl(IOMMU_VDEVICE_TSM_GUEST_REQUEST)
+ * @size:sizeof(struct iommu_vdevice_tsm_guest_request)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @scope: Bus-specific scope classification for the guest request
+ * @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 guest request to the TSM bound vDevice. This is intended for
+ * guest TSM/TDISP message transport where the host kernel only marshals
+ * bytes between userspace and the TSM implementation.
+ *
+ * The meaning and valid values of @scope are defined by the TSM backend for
+ * the device bus type.
Quoted 8 lines
Quoted 3 lines
If you want to do this then you have to also provide a way to discover what the TSM backend is so userspace can form the correct numbers.
These guest-driven requests end up in the correct VMM backend, which should already be aware of the scope value to use. In the case of ARM CCA, the RHI calls include a device ID (
vdev_id), which the VMM can use to determine the appropriate scope value.
That seems kind of indirect, we technically support multiple TSMs in the kernel, how does it all get reconciled?
Passing overlapping numbers here without any way to tell them apart is not a good uapi design.
Jason
Tian, Kevin
maintainerRE: [PATCH v4 4/4] iommufd/vdevice: add TSM guest request ioctl
Quoted 40 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 28, 2026 8:49 PMQuoted 3 lines · Aneesh Kumar K.V
Quoted 1 line · Jason Gunthorpe
On Mon, Apr 27, 2026 at 11:40:05AM +0530, Aneesh Kumar K.V (Arm)
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
+/**
+ * structiommu_vdevice_tsm_guest_request-
ioctl(IOMMU_VDEVICE_TSM_GUEST_REQUEST)Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
+ * @size:
sizeof(struct iommu_vdevice_tsm_guest_request)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @scope: Bus-specific scope classification for the guest request
+ * @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 requestpayload
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ *
+ * Forward a guest request to the TSM bound vDevice. This is intendedfor
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ * guest TSM/TDISP message transport where the host kernel only
marshals
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ * bytes between userspace and the TSM implementation.
+ *
+ * The meaning and valid values of @scope are defined by the TSMbackend for
Quoted 10 lines
Quoted 5 lines
Quoted 1 line
+ * the device bus type.
If you want to do this then you have to also provide a way to discover what the TSM backend is so userspace can form the correct numbers.
These guest-driven requests end up in the correct VMM backend, which should already be aware of the scope value to use. In the case of ARM CCA, the RHI calls include a device ID (
vdev_id), which the VMM can use to determine the appropriate scope value.
Quoted 6 lines
That seems kind of indirect, we technically support multiple TSMs in the kernel, how does it all get reconciled?
Passing overlapping numbers here without any way to tell them apart is not a good uapi design.
Agree. another thing is, as I replied to last version, what about userspace providing a wrong scope value then what would be the consequence...
Aneesh Kumar K.V
maintainerRE: [PATCH v4 4/4] iommufd/vdevice: add TSM guest request ioctl
Quoted 43 lines · Kevin
Quoted 43 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, April 28, 2026 8:49 PMQuoted 3 lines · Aneesh Kumar K.V
Quoted 1 line · Jason Gunthorpe
On Mon, Apr 27, 2026 at 11:40:05AM +0530, Aneesh Kumar K.V (Arm)
Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
+/**
+ * structiommu_vdevice_tsm_guest_request-
ioctl(IOMMU_VDEVICE_TSM_GUEST_REQUEST)Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
+ * @size:
sizeof(struct iommu_vdevice_tsm_guest_request)
+ * @vdevice_id: vDevice ID the guest request is for
+ * @scope: Bus-specific scope classification for the guest request
+ * @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 requestpayload
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ * @resp_uptr: Userspace pointer to the guest response buffer
+ *
+ * Forward a guest request to the TSM bound vDevice. This is intendedfor
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ * guest TSM/TDISP message transport where the host kernel only
marshals
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ * bytes between userspace and the TSM implementation.
+ *
+ * The meaning and valid values of @scope are defined by the TSMbackend for
Quoted 10 lines
Quoted 5 lines
Quoted 1 line
+ * the device bus type.
If you want to do this then you have to also provide a way to discover what the TSM backend is so userspace can form the correct numbers.
These guest-driven requests end up in the correct VMM backend, which should already be aware of the scope value to use. In the case of ARM CCA, the RHI calls include a device ID (
vdev_id), which the VMM can use to determine the appropriate scope value.That seems kind of indirect, we technically support multiple TSMs in the kernel, how does it all get reconciled?
Quoted 6 lines
Quoted 3 lines
Passing overlapping numbers here without any way to tell them apart is not a good uapi design.
Agree. another thing is, as I replied to last version, what about userspace providing a wrong scope value then what would be the consequence...
I will wait for Dan’s feedback on how we should handle the scope. We may have to move that to the generic headers as part of the TSM patchset.
Regarding what happens when an invalid scope is passed: tsm_guest_req() currently only handles pci_device, and the backend driver’s pci_tsm_guest_req() callback will reject unsupported scope values.
I understand that we may want to add some checks in the generic ioctl handler once these scope values are moved out of PCI TSM.
-aneesh