iommufd: Add vIOMMU infrastructure (Part-2: vDEVICE)
Subsystems (5)
v5 cover iommufd: Add vIOMMU infrastructure (Part-2: vDEVICE)
Following the previous vIOMMU series, this adds another vDEVICE structure, representing the association from an iommufd_device to an iommufd_viommu. This gives the whole architecture a new "v" layer:
_______________________________________________________________________
| iommufd (with vIOMMU/vDEVICE) |
| _____________ _____________ |
| | | | | |
| |----------------| vIOMMU |<---| vDEVICE |<------| |
| | | | |_____________| | |
| | ______ | | _____________ ___|____ |
| | | | | | | | | | |
| | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | |
| | |______| |_____________| |_____________| |________| |
|______|________|______________|__________________|_______________|_____|
| | | | |
______v_____ | ______v_____ ______v_____ ___v__
| struct | | PFN | (paging) | | (nested) | |struct|
|iommu_device| |------>|iommu_domain|<----|iommu_domain|<----|device|
|____________| storage|____________| |____________| |______|This vDEVICE object is used to collect and store all vIOMMU-related device information/attributes in a VM. As an initial series for vDEVICE, add only the virt_id to the vDEVICE, which is a vIOMMU specific device ID in a VM:
e.g. vSID of ARM SMMUv3, vDeviceID of AMD IOMMU, and vID of Intel VT-d to a Context Table. This virt_id helps IOMMU drivers to link the vID to a pID of the device against the physical IOMMU instance. This is essential for a vIOMMU-based invalidation, where the request contains a device's vID for a device cache flush, e.g. ATC invalidation.
Therefore, with this vDEVICE object, support a vIOMMU-based invalidation, by reusing IOMMUFD_CMD_HWPT_INVALIDATE for a vIOMMU object to flush cache with a given driver data.
As for the implementation of the series, add driver support in ARM SMMUv3 for a real world use case.
This series is on Github:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p2-v5
For testing, try this "with-rmr" branch:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p2-v5-with-rmr
Paring QEMU branch for testing:
https://github.com/nicolinc/qemu/commits/wip/for_iommufd_viommu_p2-v5
Changelog
v5
* Dropped driver-allocated vDEVICE support
* Changed vdev_to_dev helper to iommufd_viommu_find_dev
v4
https://lore.kernel.org/all/[email protected]/
* Added missing brackets in switch-case
* Fixed the unreleased idev refcount issue
* Reworked the iommufd_vdevice_alloc allocator
* Dropped support for IOMMU_VIOMMU_TYPE_DEFAULT
* Added missing TEST_LENGTH and fail_nth coverages
* Added a verification to the driver-allocated vDEVICE object
* Added an iommufd_vdevice_abort for a missing mutex protection
* Added a u64 structure arm_vsmmu_invalidation_cmd for user command conversion
v3
https://lore.kernel.org/all/[email protected]/
* Added Jason's Reviewed-by
* Split this invalidation part out of the part-1 series
* Repurposed VDEV_ID ioctl to a wider vDEVICE structure and ioctl
* Reduced viommu_api functions by allowing drivers to access viommu and vdevice structure directly
* Dropped vdevs_rwsem by using xa_lock instead
* Dropped arm_smmu_cache_invalidate_user
v2
https://lore.kernel.org/all/[email protected]/
* Limited vdev_id to one per idev
* Added a rw_sem to protect the vdev_id list
* Reworked driver-level APIs with proper lockings
* Added a new viommu_api file for IOMMUFD_DRIVER config
* Dropped useless iommu_dev point from the viommu structure
* Added missing index numnbers to new types in the uAPI header
* Dropped IOMMU_VIOMMU_INVALIDATE uAPI; Instead, reuse the HWPT one
* Reworked mock_viommu_cache_invalidate() using the new iommu helper
* Reordered details of set/unset_vdev_id handlers for proper lockings
v1
https://lore.kernel.org/all/[email protected]/
Thanks!
Nicolin
Jason Gunthorpe (2):
iommu: Add iommu_copy_struct_from_full_user_array helper iommu/arm-smmu-v3: Allow ATS for IOMMU_DOMAIN_NESTED
Nicolin Chen (11):
iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl iommufd/selftest: Add IOMMU_VDEVICE_ALLOC test coverage iommu/viommu: Add cache_invalidate to iommufd_viommu_ops iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based
hwpt_nested
iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE iommufd/viommu: Add iommufd_viommu_find_dev helper iommufd/selftest: Add mock_viommu_cache_invalidate iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command iommufd/selftest: Add vIOMMU coverage for IOMMU_HWPT_INVALIDATE ioctl
Documentation: userspace-api: iommufd: Update vDEVICE iommu/arm-smmu-v3: Add arm_vsmmu_cache_invalidate
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 9 +- drivers/iommu/iommufd/iommufd_private.h | 20 ++ drivers/iommu/iommufd/iommufd_test.h | 30 +++ include/linux/iommu.h | 48 ++++- include/linux/iommufd.h | 21 ++ include/uapi/linux/iommufd.h | 61 +++++- tools/testing/selftests/iommu/iommufd_utils.h | 83 +++++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 161 +++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 32 ++- drivers/iommu/iommufd/device.c | 11 + drivers/iommu/iommufd/driver.c | 13 ++ drivers/iommu/iommufd/hw_pagetable.c | 36 +++- drivers/iommu/iommufd/main.c | 7 + drivers/iommu/iommufd/selftest.c | 98 ++++++++- drivers/iommu/iommufd/viommu.c | 101 +++++++++ tools/testing/selftests/iommu/iommufd.c | 204 +++++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 4 + Documentation/userspace-api/iommufd.rst | 41 +++- 18 files changed, 942 insertions(+), 38 deletions(-)
Signature · 1 line
2.43.0
PATCH v5 1/13 iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Introduce a new IOMMUFD_OBJ_VDEVICE to represent a physical device, i.e. iommufd_device (idev) object, against an iommufd_viommu (vIOMMU) object in the VM. This vDEVICE object (and its structure) holds all the information and attributes in a VM, regarding the device related to the vIOMMU.
As an initial patch, add a per-vIOMMU virtual ID. This can be:
- Virtual StreamID on a nested ARM SMMUv3, an index to a Stream Table
- Virtual DeviceID on a nested AMD IOMMU, an index to a Device Table
- Virtual ID on a nested Intel VT-D IOMMU, an index to a Context Table
Potentially, this vDEVICE structure would hold some vData for Confidential Compute Architecture (CCA). Use this virtual ID to index an "vdevs" xarray that belongs to a vIOMMU object.
Add a new ioctl for vDEVICE allocations. Since a vDEVICE is a connection of an iommufd_device object and an iommufd_viommu object, require both as the ioctl inputs and take refcounts in the ioctl handler.
Then, let the idev structure hold the allocated vdev pointer with a proper locking protection.
--- drivers/iommu/iommufd/iommufd_private.h | 20 +++++ include/linux/iommufd.h | 3 + include/uapi/linux/iommufd.h | 26 ++++++ drivers/iommu/iommufd/device.c | 11 +++ drivers/iommu/iommufd/main.c | 7 ++ drivers/iommu/iommufd/viommu.c | 101 ++++++++++++++++++++++++ 6 files changed, 168 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +20 -0
@@ -391,6 +391,7 @@ struct iommufd_device {struct iommufd_object obj;struct iommufd_ctx *ictx;struct iommufd_group *igroup; +struct iommufd_vdevice *vdev;struct list_head group_item;/* always the physical device */ struct device *dev;
@@ -505,8 +506,27 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,return iommu_group_replace_domain (idev->igroup->group, hwpt->domain); } +static inline struct iommufd_viommu * +iommufd_get_viommu (struct iommufd_ucmd *ucmd,u32 id) +{ +return container_of (iommufd_get_object (ucmd->ictx, id, +IOMMUFD_OBJ_VIOMMU ), + struct iommufd_viommu, obj); +} +int iommufd_viommu_alloc_ioctl (struct iommufd_ucmd *ucmd);void iommufd_viommu_destroy (struct iommufd_object *obj); +int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd); +void iommufd_vdevice_destroy (struct iommufd_object *obj); +void iommufd_vdevice_abort (struct iommufd_object *obj); + +struct iommufd_vdevice { +struct iommufd_object obj; +struct iommufd_ctx *ictx; +struct iommufd_device *idev; +struct iommufd_viommu *viommu; +u64 id;/* per-vIOMMU virtual ID */ +};#ifdef CONFIG_IOMMUFD_TEST int iommufd_test (struct iommufd_ucmd *ucmd);
include/linux/iommufd.h +3 -0
@@ -31,6 +31,7 @@ enum iommufd_object_type {IOMMUFD_OBJ_ACCESS ,IOMMUFD_OBJ_FAULT ,IOMMUFD_OBJ_VIOMMU , +IOMMUFD_OBJ_VDEVICE , #ifdef CONFIG_IOMMUFD_TEST IOMMUFD_OBJ_SELFTEST, #endif
@@ -89,6 +90,8 @@ struct iommufd_viommu {const struct iommufd_viommu_ops *ops; +struct xarray vdevs; +unsigned int type; };
include/uapi/linux/iommufd.h +26 -0
@@ -52,6 +52,7 @@ enum {IOMMUFD_CMD_HWPT_INVALIDATE =0x8d ,IOMMUFD_CMD_FAULT_QUEUE_ALLOC =0x8e ,IOMMUFD_CMD_VIOMMU_ALLOC =0x8f , + IOMMUFD_CMD_VDEVICE_ALLOC = 0x90, };/**
@@ -896,4 +897,29 @@ struct iommu_viommu_alloc { __u32 out_viommu_id; };#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC) + +/** +* struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) +* @size: sizeof(struct iommu_vdevice_alloc) +* @viommu_id: vIOMMU ID to associate with the virtual device +* @dev_id: The pyhsical device to allocate a virtual instance on the vIOMMU +* @__reserved: Must be 0 +* @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID +* of AMD IOMMU, and vID of a nested Intel VT-d to a Context Table. +* @out_vdevice_id: Output virtual instance ID for the allocated object +* @__reserved2: Must be 0 +* +* Allocate a virtual device instance (for a physical device) against a vIOMMU. +* This instance holds the device's information (related to its vIOMMU) in a VM. +*/ +struct iommu_vdevice_alloc { +__u32 size; +__u32 viommu_id; +__u32 dev_id; +__u32 __reserved; +__aligned_u64 virt_id; +__u32 out_vdevice_id; +__u32 __reserved2; +}; +#define IOMMU_VDEVICE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VDEVICE_ALLOC)#endif
drivers/iommu/iommufd/device.c +11 -0
@@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { +u32 vdev_id =0 ; + +/* idev->vdev object should be destroyed prior, yet just in case.. */ +mutex_lock (&idev->igroup->lock); +if (idev->vdev) + vdev_id = idev->vdev->obj.id; +mutex_unlock (&idev->igroup->lock); +/* Relying on xa_lock against a race with iommufd_destroy() */ +if (vdev_id) +iommufd_object_remove (idev->ictx,NULL , vdev_id,0 ); +iommufd_object_destroy_user (idev->ictx, &idev->obj); }EXPORT_SYMBOL_NS_GPL (iommufd_device_unbind,IOMMUFD );
drivers/iommu/iommufd/main.c +7 -0
@@ -322,6 +322,7 @@ union ucmd_buffer {struct iommu_option option;struct iommu_vfio_ioas vfio_ioas;struct iommu_viommu_alloc viommu; +struct iommu_vdevice_alloc vdev;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -375,6 +376,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_ALLOC ,iommufd_vdevice_alloc_ioctl , +struct iommu_vdevice_alloc ,__reserved2 ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
@@ -513,6 +516,10 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { [IOMMUFD_OBJ_VIOMMU ] = { .destroy = iommufd_viommu_destroy, }, + [IOMMUFD_OBJ_VDEVICE ] = { + .destroy = iommufd_vdevice_destroy, + .abort = iommufd_vdevice_abort, + }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST ] = { .destroy = iommufd_selftest_destroy,
drivers/iommu/iommufd/viommu.c +101 -0
116 lines not on this page · raw message
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 10 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMinclude/uapi/linux/iommufd.h +6
-0+ +/** +* struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) +* @size: sizeof(struct iommu_vdevice_alloc) +* @viommu_id: vIOMMU ID to associate with the virtual device +* @dev_id: The pyhsical device to allocate a virtual instance on the vIOMMU
s/pyhsical/physical/, or just say 'iommufd device"
Quoted 31 lines
drivers/iommu/iommufd/viommu.c +30
-0+ +int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_alloc *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev, *curr; +struct iommufd_viommu *viommu; +struct iommufd_device *idev; +u64 virt_id = cmd->virt_id; +int rc =0 ; + +if (virt_id >ULONG_MAX ) +return -EINVAL ; + + viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); +if (IS_ERR (viommu)) +return PTR_ERR (viommu); + + idev =iommufd_get_device (ucmd, cmd->dev_id); +if (IS_ERR (idev)) { + rc =PTR_ERR (idev); + goto out_put_viommu; + } + +mutex_lock (&idev->igroup->lock); +if (idev->vdev) { + rc = -EEXIST ; + goto out_unlock_igroup; + } + + vdev = iommufd_object_alloc(ucmd->ictx, vdev,
IOMMUFD_OBJ_VDEVICE);
drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 154
Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5
-0+if (IS_ERR (vdev)) { + rc =PTR_ERR (vdev); + goto out_unlock_igroup; + } +
also need to check that the device and the viommu are associated to a same physical iommu.
Quoted 39 lines
drivers/iommu/iommufd/viommu.c +15
-0+ rc =iommufd_verify_unfinalized_object (ucmd->ictx, &vdev->obj); +if (rc) { +kfree (vdev); + goto out_unlock_igroup; + } + + vdev->idev = idev; + vdev->id = virt_id; + vdev->viommu = viommu; + + idev->vdev = vdev; +refcount_inc (&idev->obj.users); +refcount_inc (&viommu->obj.users); + + curr = xa_cmpxchg(&viommu->vdevs, virt_id, NULL, vdev,
GFP_KERNEL);drivers/iommu/iommufd/viommu.c +21
-1+if (curr) { + rc =xa_err (curr) ?: -EBUSY ; + goto out_abort; + } + + cmd->out_vdevice_id = vdev->obj.id; + rc =iommufd_ucmd_respond (ucmd,sizeof (*cmd)); +if (rc) + goto out_abort; +iommufd_object_finalize (ucmd->ictx, &vdev->obj); + goto out_unlock_igroup; + +out_abort: +iommufd_object_abort_and_destroy (ucmd->ictx, &vdev->obj); +out_unlock_igroup: +mutex_unlock (&idev->igroup->lock); +iommufd_put_object (ucmd->ictx, &idev->obj); +out_put_viommu: +iommufd_put_object (ucmd->ictx, &viommu->obj); +return rc; +} --2.43.0
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 12 lines · Kevin
Quoted 10 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMinclude/uapi/linux/iommufd.h +6
-0+ +/** +* struct iommu_vdevice_alloc - ioctl(IOMMU_VDEVICE_ALLOC) +* @size: sizeof(struct iommu_vdevice_alloc) +* @viommu_id: vIOMMU ID to associate with the virtual device +* @dev_id: The pyhsical device to allocate a virtual instance on the vIOMMU
s/pyhsical/physical/, or just say 'iommufd device"
Ack for "physical", aligning with other @dev_id lines.
Quoted 31 lines
Quoted 31 lines
drivers/iommu/iommufd/viommu.c +30
-0+ +int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd) +{ +struct iommu_vdevice_alloc *cmd = ucmd->cmd; +struct iommufd_vdevice *vdev, *curr; +struct iommufd_viommu *viommu; +struct iommufd_device *idev; +u64 virt_id = cmd->virt_id; +int rc =0 ; + +if (virt_id >ULONG_MAX ) +return -EINVAL ; + + viommu =iommufd_get_viommu (ucmd, cmd->viommu_id); +if (IS_ERR (viommu)) +return PTR_ERR (viommu); + + idev =iommufd_get_device (ucmd, cmd->dev_id); +if (IS_ERR (idev)) { + rc =PTR_ERR (idev); + goto out_put_viommu; + } + +mutex_lock (&idev->igroup->lock); +if (idev->vdev) { + rc = -EEXIST ; + goto out_unlock_igroup; + } + + vdev = iommufd_object_alloc(ucmd->ictx, vdev,
IOMMUFD_OBJ_VDEVICE);
Quoted 8 lines
drivers/iommu/iommufd/viommu.c int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) · line 154
Quoted 5 lines
drivers/iommu/iommufd/viommu.c +5
-0+if (IS_ERR (vdev)) { + rc =PTR_ERR (vdev); + goto out_unlock_igroup; + } +also need to check that the device and the viommu are associated to a same physical iommu.
Ack. Will add this prior to mutex_lock(&idev->igroup->lock);
+ if (viommu->iommu_dev != __iommu_get_iommu_dev(idev->dev)) {
+ rc = -EINVAL;
+ goto out_put_idev;
+ }
Thanks!
Nicolin
Jason Gunthorpe
maintainerRe: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
In reply to Nicolin ChenQuoted 9 lines · Nicolin Chen
+/**
+ * structiommu_vdevice_alloc-ioctl(IOMMU_VDEVICE_ALLOC)
+ * @size:sizeof(struct iommu_vdevice_alloc)
+ * @viommu_id: vIOMMU ID to associate with the virtual device
+ * @dev_id: The pyhsical device to allocate a virtual instance on the vIOMMU
+ * @__reserved: Must be 0
+ * @virt_id: Virtual device ID per vIOMMU, e.g. vSID of ARM SMMUv3, vDeviceID
+ * of AMD IOMMU, and vID of a nested Intel VT-d to a Context Table.
+ * @out_vdevice_id: Output virtual instance ID for the allocated object
How about:
@out_vdevice_id: Object handle for the vDevice. Pass to IOMMU_DESTORY
Review
Quoted 11 lines
+ * Allocate a virtual device instance (for a physical device) against a vIOMMU.
+ * This instance holds the device's information (related to its vIOMMU) in a VM.
+ */(quoted diff) +8
-0+struct iommu_vdevice_alloc { + __u32 size; + __u32 viommu_id; + __u32 dev_id; + __u32 __reserved; + __aligned_u64 virt_id; + __u32 out_vdevice_id; + __u32 __reserved2;
Lets not have two u32 reserved, put the out_vdevice_id above virt_id
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 284
Quoted 13 lines
drivers/iommu/iommufd/device.c +5
-0@@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { +u32 vdev_id =0 ; + +/* idev->vdev object should be destroyed prior, yet just in case.. */ +mutex_lock (&idev->igroup->lock); +if (idev->vdev)
Then should it have a WARN_ON here?
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 289
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+ vdev_id = idev->vdev->obj.id; +mutex_unlock (&idev->igroup->lock); +/* Relying on xa_lock against a race with iommufd_destroy() */ +if (vdev_id) +iommufd_object_remove (idev->ictx,NULL , vdev_id,0 );
That doesn't seem right, iommufd_object_remove() should never be used to destroy an object that userspace created with an IOCTL, in fact that just isn't allowed.
Ugh, there is worse here, we can't hold a long term reference on a kernel owned object:
idev->vdev = vdev; refcount_inc(&idev->obj.users);
As it prevents the kernel from disconnecting it.
I came up with this that seems like it will work. Maybe we will need to improve it later. Instead of using the idev, just keep the raw struct device. We can hold a refcount on the struct device without races. There is no need for the idev igroup lock since the xa_lock does everything we need.
drivers/iommu/iommufd/device.c +0 -11
@@ -277,17 +277,6 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { -u32 vdev_id =0 ; - -/* idev->vdev object should be destroyed prior, yet just in case.. */ -mutex_lock (&idev->igroup->lock); -if (idev->vdev) - vdev_id = idev->vdev->obj.id; -mutex_unlock (&idev->igroup->lock); -/* Relying on xa_lock against a race with iommufd_destroy() */ -if (vdev_id) -iommufd_object_remove (idev->ictx,NULL , vdev_id,0 ); -iommufd_object_destroy_user (idev->ictx, &idev->obj); }EXPORT_SYMBOL_NS_GPL (iommufd_device_unbind,IOMMUFD );
drivers/iommu/iommufd/driver.c +1 -1
@@ -46,6 +46,6 @@ struct device *iommufd_viommu_find_dev(struct iommufd_viommu *viommu,lockdep_assert_held (&viommu->vdevs.xa_lock); vdev =xa_load (&viommu->vdevs, vdev_id); -return vdev ? vdev->idev->dev :NULL ; +return vdev ? vdev->dev :NULL ; }EXPORT_SYMBOL_NS_GPL (iommufd_viommu_find_dev,IOMMUFD );
drivers/iommu/iommufd/iommufd_private.h +1 -5
@@ -152,9 +152,6 @@ static inline void iommufd_put_object(struct iommufd_ctx *ictx,wake_up_interruptible_all (&ictx->destroy_wait); } -int iommufd_verify_unfinalized_object (struct iommufd_ctx *ictx, -struct iommufd_object *to_verify); -void iommufd_object_abort (struct iommufd_ctx *ictx,struct iommufd_object *obj);void iommufd_object_abort_and_destroy (struct iommufd_ctx *ictx,struct iommufd_object *obj);
@@ -391,7 +388,6 @@ struct iommufd_device {struct iommufd_object obj;struct iommufd_ctx *ictx;struct iommufd_group *igroup; -struct iommufd_vdevice *vdev;struct list_head group_item;/* always the physical device */ struct device *dev;
@@ -523,7 +519,7 @@ void iommufd_vdevice_abort(struct iommufd_object *obj);struct iommufd_vdevice {struct iommufd_object obj;struct iommufd_ctx *ictx; -struct iommufd_device *idev; +struct device *dev;struct iommufd_viommu *viommu;u64 id;/* per-vIOMMU virtual ID */ };
drivers/iommu/iommufd/main.c +3 -22
@@ -43,9 +43,10 @@ void iommufd_object_finalize(struct iommufd_ctx *ictx, {void *old; - old =xa_store (&ictx->objects, obj->id, obj,GFP_KERNEL ); + old =xa_cmpxchg (&ictx->objects, obj->id,XA_ZERO_ENTRY , obj, +GFP_KERNEL );/* obj->id was returned from xa_alloc() so the xa_store() cannot fail */ -WARN_ON (old); +WARN_ON (old !=XA_ZERO_ENTRY ); }/* Undo _iommufd_object_alloc() if iommufd_object_finalize() was not called */
@@ -89,26 +90,6 @@ struct iommufd_object *iommufd_get_object(struct iommufd_ctx *ictx, u32 id,return obj; } -int iommufd_verify_unfinalized_object (struct iommufd_ctx *ictx, -struct iommufd_object *to_verify) -{ -XA_STATE (xas, &ictx->objects,0 ); -struct iommufd_object *obj; -int rc =0 ; - -if (!to_verify || !to_verify->id) -return -EINVAL ; - xas.xa_index = to_verify->id; - -xa_lock (&ictx->objects); - obj =xas_load (&xas); -/* Being an unfinalized object, the loaded obj is a reserved space */ -if (obj !=XA_ZERO_ENTRY ) - rc = -ENOENT ; -xa_unlock (&ictx->objects); -return rc; -} -static int iommufd_object_dec_wait_shortterm (struct iommufd_ctx *ictx,struct iommufd_object *to_destroy) {
drivers/iommu/iommufd/viommu.c +3 -33
@@ -55,12 +55,6 @@ int iommufd_viommu_alloc_ioctl(struct iommufd_ucmd *ucmd) goto out_put_hwpt; } - rc =iommufd_verify_unfinalized_object (ucmd->ictx, &viommu->obj); -if (rc) { -kfree (viommu); - goto out_put_hwpt; - } - viommu->type = cmd->type; viommu->ictx = ucmd->ictx; viommu->hwpt = hwpt_paging;
@@ -95,27 +89,18 @@ void iommufd_vdevice_abort(struct iommufd_object *obj)struct iommufd_vdevice *old, *vdev = container_of(obj, struct iommufd_vdevice, obj);struct iommufd_viommu *viommu = vdev->viommu; -struct iommufd_device *idev = vdev->idev; - -lockdep_assert_held (&idev->igroup->lock); old =xa_cmpxchg (&viommu->vdevs, vdev->id, vdev,NULL ,GFP_KERNEL );if (old)WARN_ON (old != vdev);refcount_dec (&viommu->obj.users); -refcount_dec (&idev->obj.users); - idev->vdev =NULL ; +put_device (vdev->dev); }void iommufd_vdevice_destroy (struct iommufd_object *obj) { -struct iommufd_vdevice *vdev = - container_of(obj, struct iommufd_vdevice, obj); - -mutex_lock (&vdev->idev->igroup->lock);iommufd_vdevice_abort (obj); -mutex_unlock (&vdev->idev->igroup->lock); }int iommufd_vdevice_alloc_ioctl (struct iommufd_ucmd *ucmd)
@@ -140,30 +125,16 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) goto out_put_viommu; } -mutex_lock (&idev->igroup->lock); -if (idev->vdev) { - rc = -EEXIST ; - goto out_unlock_igroup; - } - vdev =iommufd_object_alloc (ucmd->ictx, vdev,IOMMUFD_OBJ_VDEVICE );if (IS_ERR (vdev)) { rc =PTR_ERR (vdev); goto out_unlock_igroup; } - rc =iommufd_verify_unfinalized_object (ucmd->ictx, &vdev->obj); -if (rc) { -kfree (vdev); - goto out_unlock_igroup; - } - - vdev->idev = idev; vdev->id = virt_id; + vdev->dev = idev->dev; +get_device (idev->dev); vdev->viommu = viommu; - - idev->vdev = vdev; -refcount_inc (&idev->obj.users);refcount_inc (&viommu->obj.users); curr =xa_cmpxchg (&viommu->vdevs, virt_id,NULL , vdev,GFP_KERNEL );
@@ -182,7 +153,6 @@ int iommufd_vdevice_alloc_ioctl(struct iommufd_ucmd *ucmd) out_abort:iommufd_object_abort_and_destroy (ucmd->ictx, &vdev->obj); out_unlock_igroup: -mutex_unlock (&idev->igroup->lock);iommufd_put_object (ucmd->ictx, &idev->obj); out_put_viommu:iommufd_put_object (ucmd->ictx, &viommu->obj);
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 16 lines · Jason Gunthorpe
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 284
Quoted 13 lines · Nicolin Chen
drivers/iommu/iommufd/device.c +5
-0@@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { +u32 vdev_id =0 ; + +/* idev->vdev object should be destroyed prior, yet just in case.. */ +mutex_lock (&idev->igroup->lock); +if (idev->vdev)Then should it have a
WARN_ONhere?
It'd be a user space mistake that forgot to call the destroy ioctl to the object, in which case I recall kernel shouldn't WARN_ON?
Quoted 9 lines
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 289
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+ vdev_id = idev->vdev->obj.id; +mutex_unlock (&idev->igroup->lock); +/* Relying on xa_lock against a race with iommufd_destroy() */ +if (vdev_id) +iommufd_object_remove (idev->ictx,NULL , vdev_id,0 );That doesn't seem right,
iommufd_object_remove()should never be used to destroy an object that userspace created with an IOCTL, in fact that just isn't allowed.
It was for our auto destroy feature. If user space forgot to destroy the object while trying to unplug the device from VM. This saves the day.
Quoted 7 lines
Ugh, there is worse here, we can't hold a long term reference on a kernel owned object:
idev->vdev = vdev; refcount_inc(&idev->obj.users);As it prevents the kernel from disconnecting it.
Hmm, mind elaborating? I think the iommufd_fops_release() would xa_for_each the object list that destroys the vdev object first then this idev (and viommu too)?
Quoted 5 lines
I came up with this that seems like it will work. Maybe we will need to improve it later. Instead of using the idev, just keep the raw struct device. We can hold a refcount on the struct device without races. There is no need for the idev igroup lock since the
xa_lockdoes everything we need.
OK. If user space forgot to destroy its vdev while unplugging the device, it would not be allowed to hotplug another device (or the same device) back to the same slot having the same RID, since the RID on the vIOMMU would be occupied by the undestroyed vdev.
If we decide to do so, I think we should highlight this somewhere in the doc.
Thanks
Nicolin
Jason Gunthorpe
maintainerRe: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 18 lines · Nicolin Chen
Quoted 16 lines · Jason Gunthorpe
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 284
Quoted 13 lines · Nicolin Chen
drivers/iommu/iommufd/device.c +5
-0@@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { +u32 vdev_id =0 ; + +/* idev->vdev object should be destroyed prior, yet just in case.. */ +mutex_lock (&idev->igroup->lock); +if (idev->vdev)Then should it have a
WARN_ONhere?
Quoted 2 lines
It'd be a user space mistake that forgot to call the destroy ioctl to the object, in which case I recall kernel shouldn't
WARN_ON?
But you can't get here because:
refcount_inc(&idev->obj.users);
And kernel doesn't destroy objects with elevated ref counts?
Quoted 11 lines
Quoted 9 lines
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 289
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+ vdev_id = idev->vdev->obj.id; +mutex_unlock (&idev->igroup->lock); +/* Relying on xa_lock against a race with iommufd_destroy() */ +if (vdev_id) +iommufd_object_remove (idev->ictx,NULL , vdev_id,0 );That doesn't seem right,
iommufd_object_remove()should never be used to destroy an object that userspace created with an IOCTL, in fact that just isn't allowed.It was for our auto destroy feature.
auto domains are "hidden" hwpts that are kernel managed. They are not "userspace created".
"Usespace created" objects are ones that userspace is expected to call destroy on.
If you destroy them behind the scenes in the kerenl then the objecd ID can be reallocated for something else and when userspace does DESTROY on the ID it thought was still allocated it will malfunction.
So, only userspace can destroy objects that userspace created.
Quoted 2 lines
If user space forgot to destroy the object while trying to unplug the device from VM. This saves the day.
No, it should/does fail destroy of the VIOMMU object because the users refcount is elevated.
Quoted 11 lines
Quoted 7 lines
Ugh, there is worse here, we can't hold a long term reference on a kernel owned object:
idev->vdev = vdev; refcount_inc(&idev->obj.users);As it prevents the kernel from disconnecting it.
Hmm, mind elaborating? I think the
iommufd_fops_release()wouldxa_for_eachthe object list that destroys the vdev object first then this idev (and viommu too)?
iommufd_device_unbind() can't fail, and if the object can't be destroyed because it has an elevated long term refcount it WARN's:
ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); /* * If there is a bug and we couldn't destroy the object then we did put * back the caller's users refcount and will eventually try to free it * again during close. */ WARN_ON(ret);
So you cannot take long term references on kernel owned objects. Only userspace owned objects.
Quoted 4 lines
OK. If user space forgot to destroy its vdev while unplugging the device, it would not be allowed to hotplug another device (or the same device) back to the same slot having the same RID, since the RID on the vIOMMU would be occupied by the undestroyed vdev.
Yes, that seems correct and obvious to me. Until the vdev is explicitly destroyed the ID is in-use.
Good userspace should destroy the iommufd vDEVICE object before closing the VFIO file descriptor.
If it doesn't, then the VDEVICE object remains even though the VFIO it was linked to is gone.
Jason
Re: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Nicolin Chen
Quoted 16 lines · Jason Gunthorpe
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 284
Quoted 13 lines · Nicolin Chen
drivers/iommu/iommufd/device.c +5
-0@@ -277,6 +277,17 @@ EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD);*/ void iommufd_device_unbind (struct iommufd_device *idev) { +u32 vdev_id =0 ; + +/* idev->vdev object should be destroyed prior, yet just in case.. */ +mutex_lock (&idev->igroup->lock); +if (idev->vdev)Then should it have a
WARN_ONhere?
Quoted 8 lines
Quoted 2 lines
It'd be a user space mistake that forgot to call the destroy ioctl to the object, in which case I recall kernel shouldn't
WARN_ON?But you can't get here because:
refcount_inc(&idev->obj.users);And kernel doesn't destroy objects with elevated ref counts?
Hmm, this is not a ->destroy() but iommufd_device_unbind called by VFIO. And we actually ran into this routine when QEMU didn't destroy vdev. So, I added this chunk.
The iommufd_object_remove(vdev_id) here would destroy the vdev where its destroy() does refcount_dec(&idev->obj.users). Then, the following iommufd_object_destroy_user(.., &idev->obj) will succeed.
With that said, let's just mandate userspace to destroy vdev.
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
drivers/iommu/iommufd/device.c EXPORT_SYMBOL_NS_GPL(iommufd_ctx_has_group, IOMMUFD); · line 289
Quoted 5 lines
drivers/iommu/iommufd/device.c +5
-0+ vdev_id = idev->vdev->obj.id; +mutex_unlock (&idev->igroup->lock); +/* Relying on xa_lock against a race with iommufd_destroy() */ +if (vdev_id) +iommufd_object_remove (idev->ictx,NULL , vdev_id,0 );That doesn't seem right,
iommufd_object_remove()should never be used to destroy an object that userspace created with an IOCTL, in fact that just isn't allowed.
Quoted 7 lines
Quoted 1 line
It was for our auto destroy feature.
auto domains are "hidden" hwpts that are kernel managed. They are not "userspace created".
"Usespace created" objects are ones that userspace is expected to call destroy on.
OK. I misunderstood that.
Quoted 5 lines
If you destroy them behind the scenes in the kerenl then the objecd ID can be reallocated for something else and when userspace does DESTROY on the ID it thought was still allocated it will malfunction.
So, only userspace can destroy objects that userspace created.
I see. That makes sense.
Quoted 5 lines
Quoted 2 lines
If user space forgot to destroy the object while trying to unplug the device from VM. This saves the day.
No, it should/does fail destroy of the VIOMMU object because the users refcount is elevated.
The vIOMMU object is refcount_dec also from the unbind() calling remove(). But anyway, we aligned that userspace should destroy it explicitly.
Quoted 25 lines
Quoted 11 lines
Quoted 7 lines
Ugh, there is worse here, we can't hold a long term reference on a kernel owned object:
idev->vdev = vdev; refcount_inc(&idev->obj.users);As it prevents the kernel from disconnecting it.
Hmm, mind elaborating? I think the
iommufd_fops_release()wouldxa_for_eachthe object list that destroys the vdev object first then this idev (and viommu too)?
iommufd_device_unbind()can't fail, and if the object can't be destroyed because it has an elevated long term refcount it WARN's:ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); /* * If there is a bug and we couldn't destroy the object then we did put * back the caller's users refcount and will eventually try to free it * again during close. */ WARN_ON(ret);
Quoted 2 lines
So you cannot take long term references on kernel owned objects. Only userspace owned objects.
OK. I think I had got this part. Gao ran into this WARN_ON at v3, so I added iommufd_object_remove(vdev_id) in unbind() prior to this iommufd_object_destroy_user(idev->ictx, &idev->obj).
Quoted 5 lines
Quoted 4 lines
OK. If user space forgot to destroy its vdev while unplugging the device, it would not be allowed to hotplug another device (or the same device) back to the same slot having the same RID, since the RID on the vIOMMU would be occupied by the undestroyed vdev.
Quoted 8 lines
Yes, that seems correct and obvious to me. Until the vdev is explicitly destroyed the ID is in-use.
Good userspace should destroy the iommufd vDEVICE object before closing the VFIO file descriptor.
If it doesn't, then the VDEVICE object remains even though the VFIO it was linked to is gone.
I see.
Thanks
Nicolin
Jason Gunthorpe
maintainerRe: [PATCH v5 01/13] iommufd/viommu: Add IOMMUFD_OBJ_VDEVICE and IOMMU_VDEVICE_ALLOC ioctl
Quoted 13 lines · Nicolin Chen
Quoted 13 lines
iommufd_device_unbind()can't fail, and if the object can't be destroyed because it has an elevated long term refcount it WARN's:ret = iommufd_object_remove(ictx, obj, obj->id, REMOVE_WAIT_SHORTTERM); /* * If there is a bug and we couldn't destroy the object then we did put * back the caller's users refcount and will eventually try to free it * again during close. */ WARN_ON(ret);
Quoted 6 lines
Quoted 2 lines
So you cannot take long term references on kernel owned objects. Only userspace owned objects.
OK. I think I had got this part. Gao ran into this
WARN_ONat v3, so I addediommufd_object_remove(vdev_id)inunbind()prior to thisiommufd_object_destroy_user(idev->ictx, &idev->obj).
Oh I see, so the fix to that is to not take a longterm reference, not to try to destroy a vdev.
The alternative ould be to try to unlink the idev from the vdev and leave a zombie vdev, but that didn't look so nice to implement. If we need it we can do it later
Jason
PATCH v5 2/13 iommufd/selftest: Add IOMMU_VDEVICE_ALLOC test coverage
In reply to Nicolin ChenAdd a vdevice_alloc op to the viommu mock_viommu_ops for the coverage of IOMMU_VIOMMU_TYPE_SELFTEST allocations. Then, add a vdevice_alloc TEST_F to cover the IOMMU_VDEVICE_ALLOC ioctl.
--- tools/testing/selftests/iommu/iommufd_utils.h | 27 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 20 ++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 4 +++ 3 files changed, 51 insertions(+)
tools/testing/selftests/iommu/iommufd_utils.h +27 -0
@@ -790,3 +790,30 @@ static int _test_cmd_viommu_alloc(int fd, __u32 device_id, __u32 hwpt_id,EXPECT_ERRNO (_errno, \_test_cmd_viommu_alloc (self->fd, device_id, hwpt_id, \ type, 0, viommu_id)) + +static int _test_cmd_vdevice_alloc (int fd,__u32 viommu_id,__u32 idev_id, +__u64 virt_id,__u32 *vdev_id) +{ +struct iommu_vdevice_alloc cmd = { + .size =sizeof (cmd), + .dev_id = idev_id, + .viommu_id = viommu_id, + .virt_id = virt_id, + }; +int ret; + + ret =ioctl (fd,IOMMU_VDEVICE_ALLOC , &cmd); +if (ret) +return ret; +if (vdev_id) + *vdev_id = cmd.out_vdevice_id; +return 0 ; +} + +#define test_cmd_vdevice_alloc (viommu_id, idev_id, virt_id, vdev_id) \ + ASSERT_EQ(0, _test_cmd_vdevice_alloc(self->fd, viommu_id, idev_id, \ + virt_id, vdev_id)) +#define test_err_vdevice_alloc (_errno, viommu_id, idev_id, virt_id, vdev_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_vdevice_alloc(self->fd, viommu_id, idev_id, \ + virt_id, vdev_id))
tools/testing/selftests/iommu/iommufd.c +20 -0
@@ -129,6 +129,7 @@ TEST_F(iommufd, cmd_length)TEST_LENGTH (iommu_option,IOMMU_OPTION , val64);TEST_LENGTH (iommu_vfio_ioas,IOMMU_VFIO_IOAS , __reserved);TEST_LENGTH (iommu_viommu_alloc,IOMMU_VIOMMU_ALLOC , out_viommu_id); +TEST_LENGTH (iommu_vdevice_alloc,IOMMU_VDEVICE_ALLOC , __reserved2);#undef TEST_LENGTH }
@@ -2473,4 +2474,23 @@ TEST_F(iommufd_viommu, viommu_auto_destroy) { } +TEST_F(iommufd_viommu, vdevice_alloc) +{ +uint32_t viommu_id = self->viommu_id; +uint32_t dev_id = self->device_id; +uint32_t vdev_id =0 ; + +if (dev_id) { +/* Set vdev_id to 0x99, unset it, and set to 0x88 */ +test_cmd_vdevice_alloc (viommu_id, dev_id,0x99 , &vdev_id); +test_err_vdevice_alloc (EEXIST , viommu_id, dev_id,0x99 , + &vdev_id); +test_ioctl_destroy (vdev_id); +test_cmd_vdevice_alloc (viommu_id, dev_id,0x88 , &vdev_id); +test_ioctl_destroy (vdev_id); + }else { +test_err_vdevice_alloc (ENOENT , viommu_id, dev_id,0x99 ,NULL ); + } +} +TEST_HARNESS_MAIN
tools/testing/selftests/iommu/iommufd_fail_nth.c +4 -0
@@ -583,6 +583,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)uint32_t idev_id;uint32_t hwpt_id;uint32_t viommu_id; +uint32_t vdev_id;__u64 iova; self->fd =open ("/dev/iommu" ,O_RDWR );
@@ -635,6 +636,9 @@ TEST_FAIL_NTH(basic_fail_nth, device) IOMMU_VIOMMU_TYPE_SELFTEST, 0, &viommu_id))return -1 ; +if (_test_cmd_vdevice_alloc (self->fd, viommu_id, idev_id,0 , &vdev_id)) +return -1 ; +return 0 ; }
Signature · 1 line
2.43.0
RE: [PATCH v5 02/13] iommufd/selftest: Add IOMMU_VDEVICE_ALLOC test coverage
Quoted 10 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMAdd a
vdevice_allocop to the viommumock_viommu_opsfor the coverage ofIOMMU_VIOMMU_TYPE_SELFTESTallocations. Then, add avdevice_allocTEST_F
to cover theIOMMU_VDEVICE_ALLOCioctl.Signed-off-by Nicolin Chen <[email protected]>
Re: [PATCH v5 02/13] iommufd/selftest: Add IOMMU_VDEVICE_ALLOC test coverage
In reply to Nicolin ChenQuoted 10 lines · Nicolin Chen
Add a
vdevice_allocop to the viommumock_viommu_opsfor the coverage ofIOMMU_VIOMMU_TYPE_SELFTESTallocations. Then, add avdevice_allocTEST_Fto cover theIOMMU_VDEVICE_ALLOCioctl.Signed-off-by Nicolin Chen <[email protected]>--- tools/testing/selftests/iommu/iommufd_utils.h | 27 +++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 20 ++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 4 +++ 3 files changed, 51 insertions(+)
Jason
PATCH v5 3/13 iommu/viommu: Add cache_invalidate to iommufd_viommu_ops
In reply to Nicolin ChenThis per-vIOMMU cache_invalidate op is like the cache_invalidate_user op in struct iommu_domain_ops, but wider, supporting device cache (e.g. PCI ATC invaldiations).
--- include/linux/iommufd.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/iommufd.h +10 -0
@@ -15,6 +15,7 @@ struct device;struct file ;struct iommu_group ;struct iommu_user_data ; +struct iommu_user_data_array ;struct iommufd_access ;struct iommufd_ctx ;struct iommufd_device ;
@@ -104,12 +105,21 @@ struct iommufd_viommu {* must be defined in include/uapi/linux/iommufd.h. * It must fully initialize the new iommu_domain before * returning. Upon failure, ERR_PTR must be returned. +* @cache_invalidate: Flush hardware cache used by a vIOMMU. It can be used for +* any IOMMU hardware specific cache: TLB and device cache. +* The @array passes in the cache invalidation requests, in +* form of a driver data structure. A driver must update the +* array->entry_num to report the number of handled requests. +* The data structure of the array entry must be defined in +* include/uapi/linux/iommufd.h */ struct iommufd_viommu_ops {void (*free)(struct iommufd_viommu *viommu);struct iommu_domain *(*alloc_domain_nested)(struct iommufd_viommu *viommu,const struct iommu_user_data *user_data); +int (*cache_invalidate)(struct iommufd_viommu *viommu, +struct iommu_user_data_array *array); };#if IS_ENABLED (CONFIG_IOMMUFD )
Signature · 1 line
2.43.0
RE: [PATCH v5 03/13] iommu/viommu: Add cache_invalidate to iommufd_viommu_ops
Quoted 9 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMThis per-vIOMMU
cache_invalidateop is like thecache_invalidate_userop in structiommu_domain_ops, but wider, supporting device cache (e.g. PCI ATC invaldiations).Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
PATCH v5 4/13 iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based hwpt_nested
In reply to Nicolin ChenA vIOMMU-based hwpt_nested requires a cache invalidation op too, either using the one in iommu_domain_ops or the one in viommu_ops. Enforce that upon the allocated hwpt_nested.
--- drivers/iommu/iommufd/hw_pagetable.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/hw_pagetable.c +3 -1
@@ -302,7 +302,9 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, } hwpt->domain->owner = viommu->iommu_dev->ops; -if (WARN_ON_ONCE (hwpt->domain->type !=IOMMU_DOMAIN_NESTED )) { +if (WARN_ON_ONCE (hwpt->domain->type !=IOMMU_DOMAIN_NESTED || + (!viommu->ops->cache_invalidate && + !hwpt->domain->ops->cache_invalidate_user))) { rc = -EINVAL ; goto out_abort; }
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v5 04/13] iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based hwpt_nested
Quoted 12 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AM(quoted diff) +1
-1@@ -302,7 +302,9 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, } hwpt->domain->owner = viommu->iommu_dev->ops; - if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) { + if (WARN_ON_ONCE(hwpt->domain->type !=
IOMMU_DOMAIN_NESTED||
drivers/iommu/iommufd/hw_pagetable.c iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, · line 310
Quoted 5 lines
drivers/iommu/iommufd/hw_pagetable.c +2
-0+ (!viommu->ops->cache_invalidate && + !hwpt->domain->ops->cache_invalidate_user))) { rc = -EINVAL ; goto out_abort; }
According to patch5, cache invalidate in viommu only uses viommu->ops->cache_invalidate. Is here intended to allow nested hwpt created via viommu to still support the old method?
Jason Gunthorpe
maintainerRe: [PATCH v5 04/13] iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based hwpt_nested
Quoted 8 lines · Kevin
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMdrivers/iommu/iommufd/hw_pagetable.c +0
-0@@ -302,7 +302,9 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, } hwpt->domain->owner = viommu->iommu_dev->ops;
Quoted 14 lines
drivers/iommu/iommufd/hw_pagetable.c iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, · line 310
Quoted 9 lines
(quoted diff) +1
-1- if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) { + if (WARN_ON_ONCE(hwpt->domain->type !=
IOMMU_DOMAIN_NESTED||drivers/iommu/iommufd/hw_pagetable.c +2
-0+ (!viommu->ops->cache_invalidate && + !hwpt->domain->ops->cache_invalidate_user))) { rc = -EINVAL ; goto out_abort; }According to patch5, cache invalidate in viommu only uses
viommu->ops->cache_invalidate. Is here intended to allow nested hwpt created via viommu to still support the old method?
I think that is reasonable?
Jason
RE: [PATCH v5 04/13] iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based hwpt_nested
Quoted 27 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, October 30, 2024 12:05 AMQuoted 8 lines · Kevin
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMdrivers/iommu/iommufd/hw_pagetable.c +0
-0@@ -302,7 +302,9 @@ iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, } hwpt->domain->owner = viommu->iommu_dev->ops;Quoted 14 lines
drivers/iommu/iommufd/hw_pagetable.c iommufd_viommu_alloc_hwpt_nested(struct iommufd_viommu *viommu, u32 flags, · line 310
Quoted 9 lines
(quoted diff) +1
-1- if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED)) { + if (WARN_ON_ONCE(hwpt->domain->type !=
IOMMU_DOMAIN_NESTED||drivers/iommu/iommufd/hw_pagetable.c +2
-0+ (!viommu->ops->cache_invalidate && + !hwpt->domain->ops->cache_invalidate_user))) { rc = -EINVAL ; goto out_abort; }According to patch5, cache invalidate in viommu only uses
viommu->ops->cache_invalidate. Is here intended to allow nested hwpt created via viommu to still support the old method?
Quoted 2 lines
I think that is reasonable?
Yes, just want to confirm.
Re: [PATCH v5 04/13] iommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based hwpt_nested
In reply to Nicolin ChenQuoted 8 lines · Nicolin Chen
A vIOMMU-based
hwpt_nestedrequires a cache invalidation op too, either using the one iniommu_domain_opsor the one inviommu_ops. Enforce that upon the allocatedhwpt_nested.Signed-off-by Nicolin Chen <[email protected]>--- drivers/iommu/iommufd/hw_pagetable.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Jason
PATCH v5 5/13 iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE
In reply to Nicolin ChenWith a vIOMMU object, use space can flush any IOMMU related cache that can be directed via a vIOMMU object. It is similar to the IOMMU_HWPT_INVALIDATE uAPI, but can cover a wider range than IOTLB, e.g. device/desciprtor cache.
Allow hwpt_id of the iommu_hwpt_invalidate structure to carry a viommu_id, and reuse the IOMMU_HWPT_INVALIDATE uAPI for vIOMMU invalidations. Drivers can define different structures for vIOMMU invalidations v.s. HWPT ones.
Update the uAPI, kdoc, and selftest case accordingly.
--- include/uapi/linux/iommufd.h | 9 ++++--- drivers/iommu/iommufd/hw_pagetable.c | 32 +++++++++++++++++++------ tools/testing/selftests/iommu/iommufd.c | 4 ++-- 3 files changed, 33 insertions(+), 12 deletions(-)
include/uapi/linux/iommufd.h +6 -3
@@ -730,7 +730,7 @@ struct iommu_hwpt_vtd_s1_invalidate {/** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate) -* @hwpt_id: ID of a nested HWPT for cache invalidation +* @hwpt_id: ID of a nested HWPT or a vIOMMU, for cache invalidation * @data_uptr: User pointer to an array of driver-specific cache invalidation * data. * @data_type: One of enum iommu_hwpt_invalidate_data_type, defining the data
@@ -741,8 +741,11 @@ struct iommu_hwpt_vtd_s1_invalidate {* Output the number of requests successfully handled by kernel. * @__reserved: Must be 0. * -* Invalidate the iommu cache for user-managed page table. Modifications on a -* user-managed page table should be followed by this operation to sync cache. +* Invalidate iommu cache for user-managed page table or vIOMMU. Modifications +* on a user-managed page table should be followed by this operation, if a HWPT +* is passed in via @hwpt_id. Other caches, such as device cache or descriptor +* cache can be flushed if a vIOMMU is passed in via the @hwpt_id field. +* * Each ioctl can support one or more cache invalidation requests in the array * that has a total size of @entry_len * @entry_num. *
drivers/iommu/iommufd/hw_pagetable.c +25 -7
@@ -483,7 +483,7 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) .entry_len = cmd->entry_len, .entry_num = cmd->entry_num, }; -struct iommufd_hw_pagetable *hwpt; +struct iommufd_object *pt_obj;u32 done_num =0 ;int rc;
@@ -497,17 +497,35 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) goto out; } - hwpt =iommufd_get_hwpt_nested (ucmd, cmd->hwpt_id); -if (IS_ERR (hwpt)) { - rc =PTR_ERR (hwpt); + pt_obj =iommufd_get_object (ucmd->ictx, cmd->hwpt_id,IOMMUFD_OBJ_ANY ); +if (IS_ERR (pt_obj)) { + rc =PTR_ERR (pt_obj); goto out; } +if (pt_obj->type ==IOMMUFD_OBJ_HWPT_NESTED ) { +struct iommufd_hw_pagetable *hwpt = + container_of(pt_obj, struct iommufd_hw_pagetable, obj); + + rc = hwpt->domain->ops->cache_invalidate_user (hwpt->domain, + &data_array); + }else if (pt_obj->type ==IOMMUFD_OBJ_VIOMMU ) { +struct iommufd_viommu *viommu = + container_of(pt_obj, struct iommufd_viommu, obj); + +if (!viommu->ops || !viommu->ops->cache_invalidate) { + rc = -EOPNOTSUPP ; + goto out_put_pt; + } + rc = viommu->ops->cache_invalidate (viommu, &data_array); + }else { + rc = -EINVAL ; + goto out_put_pt; + } - rc = hwpt->domain->ops->cache_invalidate_user (hwpt->domain, - &data_array); done_num = data_array.entry_num; -iommufd_put_object (ucmd->ictx, &hwpt->obj); +out_put_pt: +iommufd_put_object (ucmd->ictx, pt_obj); out: cmd->entry_num = done_num;if (iommufd_ucmd_respond (ucmd,sizeof (*cmd)))
tools/testing/selftests/iommu/iommufd.c +2 -2
@@ -362,9 +362,9 @@ TEST_F(iommufd_ioas, alloc_hwpt_nested)EXPECT_ERRNO (EBUSY ,_test_ioctl_destroy (self->fd, parent_hwpt_id)); -/* hwpt_invalidate only supports a user-managed hwpt (nested) */ +/* hwpt_invalidate does not support a parent hwpt */ num_inv =1 ; -test_err_hwpt_invalidate (ENOENT , parent_hwpt_id, inv_reqs, +test_err_hwpt_invalidate (EINVAL , parent_hwpt_id, inv_reqs,IOMMU_HWPT_INVALIDATE_DATA_SELFTEST ,sizeof (*inv_reqs), &num_inv);assert (!num_inv);
Signature · 1 line
2.43.0
RE: [PATCH v5 05/13] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE
Quoted 14 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMWith a vIOMMU object, use space can flush any IOMMU related cache that can
be directed via a vIOMMU object. It is similar to theIOMMU_HWPT_INVALIDATE
uAPI, but can cover a wider range than IOTLB, e.g. device/desciprtor cache.Allow
hwpt_idof theiommu_hwpt_invalidatestructure to carry aviommu_id, and reuse theIOMMU_HWPT_INVALIDATEuAPI for vIOMMU invalidations. Drivers
can define different structures for vIOMMU invalidations v.s. HWPT ones.
Quoted 4 lines
Update the uAPI, kdoc, and selftest case accordingly.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
Jason Gunthorpe
maintainerRe: [PATCH v5 05/13] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE
In reply to Nicolin ChenQuoted 21 lines · Nicolin Chen
drivers/iommu/iommufd/hw_pagetable.c +12
-3@@ -497,17 +497,35 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) goto out; } - hwpt =iommufd_get_hwpt_nested (ucmd, cmd->hwpt_id); -if (IS_ERR (hwpt)) { - rc =PTR_ERR (hwpt); + pt_obj =iommufd_get_object (ucmd->ictx, cmd->hwpt_id,IOMMUFD_OBJ_ANY ); +if (IS_ERR (pt_obj)) { + rc =PTR_ERR (pt_obj); goto out; } +if (pt_obj->type ==IOMMUFD_OBJ_HWPT_NESTED ) { +struct iommufd_hw_pagetable *hwpt = + container_of(pt_obj, struct iommufd_hw_pagetable, obj); + + rc = hwpt->domain->ops->cache_invalidate_user (hwpt->domain, + &data_array); + }else if (pt_obj->type ==IOMMUFD_OBJ_VIOMMU ) { +struct iommufd_viommu *viommu = + container_of(pt_obj, struct iommufd_viommu, obj);
drivers/iommu/iommufd/hw_pagetable.c int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) · line 523
Quoted 10 lines
drivers/iommu/iommufd/hw_pagetable.c +10
-0+ +if (!viommu->ops || !viommu->ops->cache_invalidate) { + rc = -EOPNOTSUPP ; + goto out_put_pt; + } + rc = viommu->ops->cache_invalidate (viommu, &data_array); + }else { + rc = -EINVAL ; + goto out_put_pt; + }
Given the test in iommufd_viommu_alloc_hwpt_nested() is:
if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED ||
(!viommu->ops->cache_invalidate &&
!hwpt->domain->ops->cache_invalidate_user)))
{We will crash if the user passes a viommu allocated domain as IOMMUFD_OBJ_HWPT_NESTED since the above doesn't check it.
I suggest we put the required if (ops..) -EOPNOTSUPP above and remove the ops->cache_invalidate checks from both WARN_ONs.
Jason
Re: [PATCH v5 05/13] iommufd: Allow hwpt_id to carry viommu_id for IOMMU_HWPT_INVALIDATE
Quoted 35 lines · Jason Gunthorpe
Quoted 21 lines · Nicolin Chen
drivers/iommu/iommufd/hw_pagetable.c +12
-3@@ -497,17 +497,35 @@ int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) goto out; } - hwpt =iommufd_get_hwpt_nested (ucmd, cmd->hwpt_id); -if (IS_ERR (hwpt)) { - rc =PTR_ERR (hwpt); + pt_obj =iommufd_get_object (ucmd->ictx, cmd->hwpt_id,IOMMUFD_OBJ_ANY ); +if (IS_ERR (pt_obj)) { + rc =PTR_ERR (pt_obj); goto out; } +if (pt_obj->type ==IOMMUFD_OBJ_HWPT_NESTED ) { +struct iommufd_hw_pagetable *hwpt = + container_of(pt_obj, struct iommufd_hw_pagetable, obj); + + rc = hwpt->domain->ops->cache_invalidate_user (hwpt->domain, + &data_array); + }else if (pt_obj->type ==IOMMUFD_OBJ_VIOMMU ) { +struct iommufd_viommu *viommu = + container_of(pt_obj, struct iommufd_viommu, obj);drivers/iommu/iommufd/hw_pagetable.c int iommufd_hwpt_invalidate(struct iommufd_ucmd *ucmd) · line 523
Quoted 10 lines
drivers/iommu/iommufd/hw_pagetable.c +10
-0+ +if (!viommu->ops || !viommu->ops->cache_invalidate) { + rc = -EOPNOTSUPP ; + goto out_put_pt; + } + rc = viommu->ops->cache_invalidate (viommu, &data_array); + }else { + rc = -EINVAL ; + goto out_put_pt; + }Given the test in
iommufd_viommu_alloc_hwpt_nested()is:
Quoted 7 lines
if (WARN_ON_ONCE(hwpt->domain->type != IOMMU_DOMAIN_NESTED || (!viommu->ops->cache_invalidate && !hwpt->domain->ops->cache_invalidate_user))) {We will crash if the user passes a viommu allocated domain as
IOMMUFD_OBJ_HWPT_NESTEDsince the above doesn't check it.
Ah, that was missed.
Quoted 2 lines
I suggest we put the required if (ops..)
-EOPNOTSUPPabove and remove theops->cache_invalidatechecks from bothWARN_ONs.
Ack. I will add hwpt->domain->ops check:
---------------------------------------------------------------------
if (pt_obj->type == IOMMUFD_OBJ_HWPT_NESTED) {
struct iommufd_hw_pagetable *hwpt =
container_of(pt_obj, struct iommufd_hw_pagetable, obj);
if (!hwpt->domain->ops ||
!hwpt->domain->ops->cache_invalidate_user) {
rc = -EOPNOTSUPP;
goto out_put_pt;
}
rc = hwpt->domain->ops->cache_invalidate_user(hwpt->domain,
&data_array);
} else if (pt_obj->type == IOMMUFD_OBJ_VIOMMU) {
struct iommufd_viommu *viommu =
container_of(pt_obj, struct iommufd_viommu, obj);
if (!viommu->ops || !viommu->ops->cache_invalidate) {
rc = -EOPNOTSUPP;
goto out_put_pt;
}
rc = viommu->ops->cache_invalidate(viommu, &data_array);
} else {---------------------------------------------------------------------
Thanks
Nicolin
PATCH v5 6/13 iommu: Add iommu_copy_struct_from_full_user_array helper
In reply to Nicolin ChenFrom: Jason Gunthorpe <[email protected]>
The iommu_copy_struct_from_user_array helper can be used to copy a single entry from a user array which might not be efficient if the array is big.
Add a new iommu_copy_struct_from_full_user_array to copy the entire user array at once. Update the existing iommu_copy_struct_from_user_array kdoc accordingly.
--- include/linux/iommu.h | 48 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-)
include/linux/iommu.h +47 -1
@@ -493,7 +493,9 @@ static inline int __iommu_copy_struct_from_user_array(* @index: Index to the location in the array to copy user data from * @min_last: The last member of the data structure @kdst points in the * initial version. -* Return 0 for success, otherwise -error. +* +* Copy a single entry from a user array. Return 0 for success, otherwise +* -error. */ #define iommu_copy_struct_from_user_array (kdst, user_array, data_type, index, \ min_last) \
@@ -501,6 +503,50 @@ static inline int __iommu_copy_struct_from_user_array( kdst, user_array, data_type, index, sizeof(*(kdst)), \ offsetofend(typeof(*(kdst)), min_last)) +/** +* iommu_copy_struct_from_full_user_array - Copy iommu driver specific user +* space data from an iommu_user_data_array +* @kdst: Pointer to an iommu driver specific user data that is defined in +* include/uapi/linux/iommufd.h +* @kdst_entry_size: sizeof(*kdst) +* @user_array: Pointer to a struct iommu_user_data_array for a user space +* array +* @data_type: The data type of the @kdst. Must match with @user_array->type +* +* Copy the entire user array. kdst must have room for kdst_entry_size * +* user_array->entry_num bytes. Return 0 for success, otherwise -error. +*/ +static inline int +iommu_copy_struct_from_full_user_array (void *kdst,size_t kdst_entry_size, +struct iommu_user_data_array *user_array, +unsigned int data_type) +{ +unsigned int i; +int ret; + +if (user_array->type != data_type) +return -EINVAL ; +if (!user_array->entry_num) +return -EINVAL ; +if (likely (user_array->entry_len == kdst_entry_size)) { +if (copy_from_user (kdst, user_array->uptr, + user_array->entry_num * + user_array->entry_len)) +return -EFAULT ; + } + +/* Copy item by item */ +for (i =0 ; i != user_array->entry_num; i++) { + ret =copy_struct_from_user ( + kdst + kdst_entry_size * i, kdst_entry_size, + user_array->uptr + user_array->entry_len * i, + user_array->entry_len); +if (ret) +return ret; + } +return 0 ; +} +/** * struct iommu_ops - iommu ops and capabilities * @capable: check capability
Signature · 1 line
2.43.0
RE: [PATCH v5 06/13] iommu: Add iommu_copy_struct_from_full_user_array helper
Quoted 5 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMFrom: Jason Gunthorpe <[email protected]>
Quoted 8 lines
The
iommu_copy_struct_from_user_arrayhelper can be used to copy a single
entry from a user array which might not be efficient if the array is big.Add a new
iommu_copy_struct_from_full_user_arrayto copy the entire user array at once. Update the existingiommu_copy_struct_from_user_arraykdoc
accordingly.
what about:
iommu_copy_struct_from_user_array_single()iommu_copy_struct_from_user_array_full()
?
Quoted 3 lines
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
Re: [PATCH v5 06/13] iommu: Add iommu_copy_struct_from_full_user_array helper
Quoted 14 lines · Kevin
Quoted 5 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMFrom: Jason Gunthorpe <[email protected]>
Quoted 8 lines
The
iommu_copy_struct_from_user_arrayhelper can be used to copy a single
entry from a user array which might not be efficient if the array is big.Add a new
iommu_copy_struct_from_full_user_arrayto copy the entire user array at once. Update the existingiommu_copy_struct_from_user_arraykdoc
accordingly.
Quoted 4 lines
what about:
iommu_copy_struct_from_user_array_single()iommu_copy_struct_from_user_array_full()
I am okay with that, yet might prefer that in a separate patch? As I am trying to reduce the number of changes in the series since we are likely merging three series at once :)
Thanks
Nicolin
Quoted 4 lines
Quoted 2 lines
Signed-off-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>Reviewed-by Kevin Tian <[email protected]>
PATCH v5 7/13 iommufd/viommu: Add iommufd_viommu_find_dev helper
In reply to Nicolin ChenThis avoids a bigger trouble of exposing struct iommufd_device and struct iommufd_vdevice in the public header.
--- include/linux/iommufd.h | 8 ++++++++ drivers/iommu/iommufd/driver.c | 13 +++++++++++++ 2 files changed, 21 insertions(+)
include/linux/iommufd.h +8 -0
@@ -184,6 +184,8 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)struct iommufd_object *_iommufd_object_alloc (struct iommufd_ctx *ictx,size_t size,enum iommufd_object_type type); +struct device *iommufd_viommu_find_dev (struct iommufd_viommu *viommu, +unsigned long vdev_id);#else /* !CONFIG_IOMMUFD_DRIVER */ static inline struct iommufd_object * _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
@@ -191,6 +193,12 @@ _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, {return ERR_PTR (-EOPNOTSUPP ); } + +static inline struct device * +iommufd_viommu_find_dev (struct iommufd_viommu *viommu,unsigned long vdev_id) +{ +return NULL ; +}#endif /* CONFIG_IOMMUFD_DRIVER */ /*
drivers/iommu/iommufd/driver.c +13 -0
@@ -36,3 +36,16 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,return ERR_PTR (rc); }EXPORT_SYMBOL_NS_GPL (_iommufd_object_alloc,IOMMUFD ); + +/* Caller should xa_lock(&viommu->vdevs) to protect the return value */ +struct device *iommufd_viommu_find_dev (struct iommufd_viommu *viommu, +unsigned long vdev_id) +{ +struct iommufd_vdevice *vdev; + +lockdep_is_held (&viommu->vdevs.xa_lock); + + vdev =xa_load (&viommu->vdevs, vdev_id); +return vdev ? vdev->idev->dev :NULL ; +} +EXPORT_SYMBOL_NS_GPL (iommufd_viommu_find_dev,IOMMUFD );
Signature · 1 line
2.43.0
Re: [PATCH v5 07/13] iommufd/viommu: Add iommufd_viommu_find_dev helper
Quoted 46 lines · Nicolin Chen
This avoids a bigger trouble of exposing struct
iommufd_deviceand structiommufd_vdevicein the public header.Signed-off-by Nicolin Chen <[email protected]>--- include/linux/iommufd.h | 8 ++++++++ drivers/iommu/iommufd/driver.c | 13 +++++++++++++ 2 files changed, 21 insertions(+)include/linux/iommufd.h +8
-0@@ -184,6 +184,8 @@ static inline int iommufd_vfio_compat_set_no_iommu(struct iommufd_ctx *ictx)struct iommufd_object *_iommufd_object_alloc (struct iommufd_ctx *ictx,size_t size,enum iommufd_object_type type); +struct device *iommufd_viommu_find_dev (struct iommufd_viommu *viommu, +unsigned long vdev_id);#else /* !CONFIG_IOMMUFD_DRIVER */ static inline struct iommufd_object * _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,@@ -191,6 +193,12 @@ _iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, {return ERR_PTR (-EOPNOTSUPP ); } + +static inline struct device * +iommufd_viommu_find_dev (struct iommufd_viommu *viommu,unsigned long vdev_id) +{ +return NULL ; +}#endif /* CONFIG_IOMMUFD_DRIVER */ /* drivers/iommu/iommufd/driver.c +2
-0@@ -36,3 +36,16 @@ struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx,return ERR_PTR (rc); }EXPORT_SYMBOL_NS_GPL (_iommufd_object_alloc,IOMMUFD ); + +/* Caller should xa_lock(&viommu->vdevs) to protect the return value */
drivers/iommu/iommufd/driver.c struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, · line 50
Quoted 10 lines
drivers/iommu/iommufd/driver.c +10
-0+struct device *iommufd_viommu_find_dev (struct iommufd_viommu *viommu, +unsigned long vdev_id) +{ +struct iommufd_vdevice *vdev; + +lockdep_is_held (&viommu->vdevs.xa_lock); + + vdev =xa_load (&viommu->vdevs, vdev_id); +return vdev ? vdev->idev->dev :NULL ; +}
Got this error?
ld: Unexpected GOT/PLT entries detected!
ld: Unexpected run-time procedure linkages detected!
ld: drivers/iommu/iommufd/driver.o: in function `iommufd_viommu_find_dev':linux/drivers/iommu/iommufd/driver.c:47: undefined reference to `lockdep_is_held'
make[2]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1 make[1]: *** [/home/linaro/iommufd/linux/Makefile:1166: vmlinux] Error 2
make: *** [Makefile:224: __sub-make] Error 2
Re: [PATCH v5 07/13] iommufd/viommu: Add iommufd_viommu_find_dev helper
Quoted 15 lines · Zhangfei Gao
drivers/iommu/iommufd/driver.c struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, · line 50
Quoted 11 lines · Nicolin Chen
drivers/iommu/iommufd/driver.c +11
-0+/* Caller should xa_lock(&viommu->vdevs) to protect the return value */ +struct device *iommufd_viommu_find_dev (struct iommufd_viommu *viommu, +unsigned long vdev_id) +{ +struct iommufd_vdevice *vdev; + +lockdep_is_held (&viommu->vdevs.xa_lock); + + vdev =xa_load (&viommu->vdevs, vdev_id); +return vdev ? vdev->idev->dev :NULL ; +}Got this error?
Quoted 8 lines
ld: Unexpected GOT/PLT entries detected!
ld: Unexpected run-time procedure linkages detected!
ld:drivers/iommu/iommufd/driver.o: in function `iommufd_viommu_find_dev':linux/drivers/iommu/iommufd/driver.c:47: undefined reference to `lockdep_is_held'
make[2]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1 make[1]: *** [/home/linaro/iommufd/linux/Makefile:1166: vmlinux] Error 2
make: *** [Makefile:224: __sub-make] Error 2
Should fix it with:
- lockdep_is_held(&viommu->vdevs.xa_lock);
+ lockdep_assert_held(&viommu->vdevs.xa_lock);
Thanks
Nicolin
RE: [PATCH v5 07/13] iommufd/viommu: Add iommufd_viommu_find_dev helper
Quoted 34 lines
From: Nicolin Chen <[email protected]>
Sent: Monday, October 28, 2024 6:49 AMQuoted 2 lines · Zhangfei Gao
Quoted 1 line · Nicolin Chen
+/* Caller should
xa_lock(&viommu->vdevs)to protect the return value*/
Quoted 1 line
Quoted 1 line
+struct device *iommufd_viommu_find_dev(struct
iommufd_viommu*viommu,
Quoted 12 lines
drivers/iommu/iommufd/driver.c struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, · line 50
Quoted 9 lines
drivers/iommu/iommufd/driver.c +9
-0+ unsigned long vdev_id) +{ +struct iommufd_vdevice *vdev; + +lockdep_is_held (&viommu->vdevs.xa_lock); + + vdev =xa_load (&viommu->vdevs, vdev_id); +return vdev ? vdev->idev->dev :NULL ; +}Got this error?
Quoted 3 lines
ld: Unexpected GOT/PLT entries detected!
ld: Unexpected run-time procedure linkages detected!
ld:drivers/iommu/iommufd/driver.o: in function`iommufd_viommu_find_dev':
Quoted 4 lines
linux/drivers/iommu/iommufd/driver.c:47: undefined reference to `lockdep_is_held'
make[2]: *** [scripts/Makefile.vmlinux:34: vmlinux] Error 1 make[1]: *** [/home/linaro/iommufd/linux/Makefile:1166: vmlinux] Error2
Quoted 1 line
make: *** [Makefile:224: __sub-make] Error 2
Should fix it with:
Review
Quoted 2 lines
(quoted diff) +1
-1- lockdep_is_held(&viommu->vdevs.xa_lock); + lockdep_assert_held(&viommu->vdevs.xa_lock);
with that,
PATCH v5 8/13 iommufd/selftest: Add mock_viommu_cache_invalidate
In reply to Nicolin ChenSimilar to the coverage of cache_invalidate_user for iotlb invalidation, add a device cache and a viommu_cache_invalidate function to test it out.
--- drivers/iommu/iommufd/iommufd_test.h | 25 +++++++++ drivers/iommu/iommufd/selftest.c | 76 +++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/iommufd_test.h +25 -0
@@ -54,6 +54,11 @@ enum { MOCK_NESTED_DOMAIN_IOTLB_NUM = 4, }; +enum { +MOCK_DEV_CACHE_ID_MAX =3 , +MOCK_DEV_CACHE_NUM =4 , +}; +struct iommu_test_cmd {__u32 size;__u32 op;
@@ -152,6 +157,7 @@ struct iommu_test_hw_info {/* Should not be equal to any defined value in enum iommu_hwpt_data_type */ #define IOMMU_HWPT_DATA_SELFTEST 0xdead#define IOMMU_TEST_IOTLB_DEFAULT 0xbadbeef +#define IOMMU_TEST_DEV_CACHE_DEFAULT 0xbaddad/** * struct iommu_hwpt_selftest
@@ -182,4 +188,23 @@ struct iommu_hwpt_invalidate_selftest {#define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef +/* Should not be equal to any defined value in enum iommu_viommu_invalidate_data_type */ +#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST 0xdeadbeef +#define IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST_INVALID 0xdadbeef + +/** +* struct iommu_viommu_invalidate_selftest - Invalidation data for Mock VIOMMU +* (IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST) +* @flags: Invalidate flags +* @cache_id: Invalidate cache entry index +* +* If IOMMU_TEST_INVALIDATE_ALL is set in @flags, @cache_id will be ignored +*/ +struct iommu_viommu_invalidate_selftest { +#define IOMMU_TEST_INVALIDATE_FLAG_ALL (1 << 0) +__u32 flags; +__u32 vdev_id; +__u32 cache_id; +}; +#endif
drivers/iommu/iommufd/selftest.c +75 -1
103 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v5 08/13] iommufd/selftest: Add mock_viommu_cache_invalidate
Quoted 7 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMSimilar to the coverage of
cache_invalidate_userfor iotlb invalidation, add a device cache and aviommu_cache_invalidatefunction to test it out.Signed-off-by Nicolin Chen <[email protected]>
PATCH v5 9/13 iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command
In reply to Nicolin ChenSimilar to IOMMU_TEST_OP_MD_CHECK_IOTLB verifying a mock_domain's iotlb, IOMMU_TEST_OP_DEV_CHECK_CACHE will be used to verify a mock_dev's cache.
--- drivers/iommu/iommufd/iommufd_test.h | 5 ++++ tools/testing/selftests/iommu/iommufd_utils.h | 24 +++++++++++++++++++ drivers/iommu/iommufd/selftest.c | 22 +++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 7 +++++- 4 files changed, 57 insertions(+), 1 deletion(-)
drivers/iommu/iommufd/iommufd_test.h +5 -0
@@ -23,6 +23,7 @@ enum {IOMMU_TEST_OP_DIRTY ,IOMMU_TEST_OP_MD_CHECK_IOTLB ,IOMMU_TEST_OP_TRIGGER_IOPF , + IOMMU_TEST_OP_DEV_CHECK_CACHE, }; enum {
@@ -140,6 +141,10 @@ struct iommu_test_cmd {__u32 perm;__u64 addr; } trigger_iopf; +struct { +__u32 id; +__u32 cache; + } check_dev_cache; };__u32 last; };
tools/testing/selftests/iommu/iommufd_utils.h +24 -0
@@ -234,6 +234,30 @@ static int _test_cmd_hwpt_alloc(int fd, __u32 device_id, __u32 pt_id, __u32 ft_itest_cmd_hwpt_check_iotlb (hwpt_id, i, expected); \ }) +#define test_cmd_dev_check_cache (device_id, cache_id, expected) \ + ({ \ + struct iommu_test_cmd test_cmd = { \ + .size = sizeof(test_cmd), \ + .op = IOMMU_TEST_OP_DEV_CHECK_CACHE, \ + .id = device_id, \ + .check_dev_cache = { \ + .id = cache_id, \ + .cache = expected, \ + }, \ + }; \ + ASSERT_EQ(0, ioctl(self->fd, \ + _IOMMU_TEST_CMD( \ + IOMMU_TEST_OP_DEV_CHECK_CACHE), \ + &test_cmd)); \ + }) + +#define test_cmd_dev_check_cache_all (device_id, expected) \ + ({ \ + int c; \ + for (c = 0; c < MOCK_DEV_CACHE_NUM; c++) \ + test_cmd_dev_check_cache(device_id, c, expected); \ + }) +static int _test_cmd_hwpt_invalidate (int fd,__u32 hwpt_id,void *reqs,uint32_t data_type,uint32_t lreq,uint32_t *nreqs)
drivers/iommu/iommufd/selftest.c +22 -0
@@ -1122,6 +1122,24 @@ static int iommufd_test_md_check_iotlb(struct iommufd_ucmd *ucmd,return rc; } +static int iommufd_test_dev_check_cache (struct iommufd_ucmd *ucmd,u32 idev_id, +unsigned int cache_id,u32 cache) +{ +struct iommufd_device *idev; +struct mock_dev *mdev; +int rc =0 ; + + idev =iommufd_get_device (ucmd, idev_id); +if (IS_ERR (idev)) +return PTR_ERR (idev); + mdev = container_of(idev->dev, struct mock_dev, dev); + +if (cache_id >MOCK_DEV_CACHE_ID_MAX || mdev->cache[cache_id] != cache) + rc = -EINVAL ; +iommufd_put_object (ucmd->ictx, &idev->obj); +return rc; +} +struct selftest_access {struct iommufd_access *access;struct file *file;
@@ -1631,6 +1649,10 @@ int iommufd_test(struct iommufd_ucmd *ucmd)return iommufd_test_md_check_iotlb (ucmd, cmd->id, cmd->check_iotlb.id, cmd->check_iotlb.iotlb); +case IOMMU_TEST_OP_DEV_CHECK_CACHE : +return iommufd_test_dev_check_cache (ucmd, cmd->id, + cmd->check_dev_cache.id, + cmd->check_dev_cache.cache);case IOMMU_TEST_OP_CREATE_ACCESS :return iommufd_test_create_access (ucmd, cmd->id, cmd->create_access.flags);
tools/testing/selftests/iommu/iommufd.c +6 -1
@@ -222,6 +222,8 @@ FIXTURE_SETUP(iommufd_ioas)for (i =0 ; i != variant->mock_domains; i++) {test_cmd_mock_domain (self->ioas_id, &self->stdev_id, &self->hwpt_id, &self->device_id); +test_cmd_dev_check_cache_all (self->device_id, +IOMMU_TEST_DEV_CACHE_DEFAULT ); self->base_iova =MOCK_APERTURE_START ; } }
@@ -1386,9 +1388,12 @@ FIXTURE_SETUP(iommufd_mock_domain)ASSERT_GE (ARRAY_SIZE (self->hwpt_ids), variant->mock_domains); -for (i =0 ; i != variant->mock_domains; i++) +for (i =0 ; i != variant->mock_domains; i++) {test_cmd_mock_domain (self->ioas_id, &self->stdev_ids[i], &self->hwpt_ids[i], &self->idev_ids[i]); +test_cmd_dev_check_cache_all (self->idev_ids[0 ], +IOMMU_TEST_DEV_CACHE_DEFAULT ); + } self->hwpt_id = self->hwpt_ids[0 ]; self->mmap_flags =MAP_SHARED |MAP_ANONYMOUS ;
Signature · 1 line
2.43.0
RE: [PATCH v5 09/13] iommufd/selftest: Add IOMMU_TEST_OP_DEV_CHECK_CACHE test command
Quoted 9 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMSimilar to
IOMMU_TEST_OP_MD_CHECK_IOTLBverifying a mock_domain's iotlb,IOMMU_TEST_OP_DEV_CHECK_CACHEwill be used to verify a mock_dev's cache.Signed-off-by Nicolin Chen <[email protected]>
PATCH v5 10/13 iommufd/selftest: Add vIOMMU coverage for IOMMU_HWPT_INVALIDATE ioctl
In reply to Nicolin ChenAdd a viommu_cache test function to cover vIOMMU invalidations using the updated IOMMU_HWPT_INVALIDATE ioctl, which now allows passing in a vIOMMU via its hwpt_id field.
--- tools/testing/selftests/iommu/iommufd_utils.h | 32 ++++ tools/testing/selftests/iommu/iommufd.c | 173 ++++++++++++++++++ 2 files changed, 205 insertions(+)
tools/testing/selftests/iommu/iommufd_utils.h +32 -0
@@ -289,6 +289,38 @@ static int _test_cmd_hwpt_invalidate(int fd, __u32 hwpt_id, void *reqs, data_type, lreq, nreqs)); \ }) +static int _test_cmd_viommu_invalidate (int fd,__u32 viommu_id,void *reqs, +uint32_t data_type,uint32_t lreq, +uint32_t *nreqs) +{ +struct iommu_hwpt_invalidate cmd = { + .size =sizeof (cmd), + .hwpt_id = viommu_id, + .data_type = data_type, + .data_uptr = (uint64_t )reqs, + .entry_len = lreq, + .entry_num = *nreqs, + }; +int rc =ioctl (fd,IOMMU_HWPT_INVALIDATE , &cmd); + *nreqs = cmd.entry_num; +return rc; +} + +#define test_cmd_viommu_invalidate (viommu, reqs, lreq, nreqs) \ + ({ \ + ASSERT_EQ(0, \ + _test_cmd_viommu_invalidate(self->fd, viommu, reqs, \ + IOMMU_VIOMMU_INVALIDATE_DATA_SELFTEST, \ + lreq, nreqs)); \ + }) +#define test_err_viommu_invalidate (_errno, viommu_id, reqs, data_type, lreq, \ + nreqs) \ + ({ \ + EXPECT_ERRNO(_errno, _test_cmd_viommu_invalidate( \ + self->fd, viommu_id, reqs, \ + data_type, lreq, nreqs)); \ + }) +static int _test_cmd_access_replace_ioas (int fd,__u32 access_id,unsigned int ioas_id) {
tools/testing/selftests/iommu/iommufd.c +173 -0
177 lines not on this page · raw message
Signature · 1 line
2.43.0
RE: [PATCH v5 10/13] iommufd/selftest: Add vIOMMU coverage for IOMMU_HWPT_INVALIDATE ioctl
Quoted 9 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMAdd a
viommu_cachetest function to cover vIOMMU invalidations using the updatedIOMMU_HWPT_INVALIDATEioctl, which now allows passing in a vIOMMU
via itshwpt_idfield.Signed-off-by Nicolin Chen <[email protected]>
PATCH v5 11/13 Documentation: userspace-api: iommufd: Update vDEVICE
In reply to Nicolin ChenWith the introduction of the new object and its infrastructure, update the doc and the vIOMMU graph to reflect that.
--- Documentation/userspace-api/iommufd.rst | 41 +++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-)
Documentation/userspace-api/iommufd.rst +32 -9
@@ -94,6 +94,19 @@ Following IOMMUFD objects are exposed to userspace: backed by corresponding vIOMMU objects, in which case a guest OS would do the "dispatch" naturally instead of VMM trappings. + - IOMMUFD_OBJ_VDEVICE, representing a virtual device for an IOMMUFD_OBJ_DEVICE + against an IOMMUFD_OBJ_VIOMMU. This virtual device holds the device's virtual + information or attributes (related to the vIOMMU) in a VM. An immediate vDATA + example can be the virtual ID of the device on a vIOMMU, which is a unique ID + that VMM assigns to the device for a translation channel/port of the vIOMMU, + e.g. vSID of ARM SMMUv3, vDeviceID of AMD IOMMU, and vID of Intel VT-d to a + Context Table. Potential use cases of some advanced security information can + be forwarded via this object too, such as security level or realm information + in a Confidential Compute Architecture. A VMM should create a vDEVICE object + to forward all the device information in a VM, when it connects a device to a + vIOMMU, which is a separate ioctl call from attaching the same device to an + HWPT_PAGING that the vIOMMU holds. + All user-visible objects are destroyed via the IOMMU_DESTROY uAPI. The diagrams below show relationships between user-visible objects and kernel
@@ -133,16 +146,16 @@ creating the objects and links:: |____________| |____________| |______| _______________________________________________________________________ - | iommufd (with vIOMMU) | + | iommufd (with vIOMMU/vDEVICE) | | | - | [5] | - | _____________ | - | | | | - | |----------------| vIOMMU | | - | | | | | - | | | | | - | | [1] | | [4] [2] | - | | ______ | | _____________ ________ | + | [5] [6] | + | _____________ _____________ | + | | | | | | + | |----------------| vIOMMU |<---| vDEVICE |<----| | + | | | | |_____________| | | + | | | | | | + | | [1] | | [4] | [2] | + | | ______ | | _____________ _|______ | | | | | | [3] | | | | | | | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | | | |______| |_____________| |_____________| |________| |
@@ -215,6 +228,15 @@ creating the objects and links:: the vIOMMU object and the HWPT_PAGING, then this vIOMMU object can be used as a nesting parent object to allocate an HWPT_NESTED object described above. +6. IOMMUFD_OBJ_VDEVICE can be only manually created via the IOMMU_VDEVICE_ALLOC + uAPI, provided a viommu_id for an iommufd_viommu object and a dev_id for an + iommufd_device object. The vDEVICE object will be the binding between these + two parent objects. Another @virt_id will be also set via the uAPI providing + the iommufd core an index to store the vDEVICE object to a vDEVICE array per + vIOMMU. If necessary, the IOMMU driver may choose to implement a vdevce_alloc + op to init its HW for virtualization feature related to a vDEVICE. Successful + completion of this operation sets up the linkages between vIOMMU and device. + A device can only bind to an iommufd due to DMA ownership claim and attach to at most one IOAS object (no support of PASID yet).
@@ -228,6 +250,7 @@ User visible objects are backed by following datastructures: - iommufd_hwpt_paging for IOMMUFD_OBJ_HWPT_PAGING. - iommufd_hwpt_nested for IOMMUFD_OBJ_HWPT_NESTED. - iommufd_viommu for IOMMUFD_OBJ_VIOMMU. +- iommufd_vdevice for IOMMUFD_OBJ_VDEVICE Several terminologies when looking at these datastructures:
Signature · 1 line
2.43.0
RE: [PATCH v5 11/13] Documentation: userspace-api: iommufd: Update vDEVICE
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMWith the introduction of the new object and its infrastructure, update the doc and the vIOMMU graph to reflect that.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
PATCH v5 12/13 iommu/arm-smmu-v3: Add arm_vsmmu_cache_invalidate
In reply to Nicolin ChenImplement the vIOMMU's cache_invalidate op for user space to invalidate the IOTLB entries, Device ATS and CD entries that are still cached by hardware.
Add struct iommu_viommu_arm_smmuv3_invalidate defining invalidation entries that are simply in the native format of a 128-bit TLBI command. Scan those commands against the permitted command list and fix their VMID/SID fields.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 5 + include/uapi/linux/iommufd.h | 24 ++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 130 ++++++++++++++++++ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 6 +- 4 files changed, 162 insertions(+), 3 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +5 -0
@@ -529,6 +529,7 @@ struct arm_smmu_cmdq_ent {#define CMDQ_OP_TLBI_NH_ALL 0x10#define CMDQ_OP_TLBI_NH_ASID 0x11#define CMDQ_OP_TLBI_NH_VA 0x12 +#define CMDQ_OP_TLBI_NH_VAA 0x13#define CMDQ_OP_TLBI_EL2_ALL 0x20#define CMDQ_OP_TLBI_EL2_ASID 0x21#define CMDQ_OP_TLBI_EL2_VA 0x22
@@ -949,6 +950,10 @@ void arm_smmu_attach_commit(struct arm_smmu_attach_state *state);void arm_smmu_install_ste_for_dev (struct arm_smmu_master *master,const struct arm_smmu_ste *target); +int arm_smmu_cmdq_issue_cmdlist (struct arm_smmu_device *smmu, +struct arm_smmu_cmdq *cmdq,u64 *cmds,int n, +bool sync); +#ifdef CONFIG_ARM_SMMU_V3_SVA bool arm_smmu_sva_supported (struct arm_smmu_device *smmu);bool arm_smmu_master_sva_supported (struct arm_smmu_master *master);
include/uapi/linux/iommufd.h +24 -0
@@ -686,9 +686,11 @@ struct iommu_hwpt_get_dirty_bitmap {* enum iommu_hwpt_invalidate_data_type - IOMMU HWPT Cache Invalidation * Data Type * @IOMMU_HWPT_INVALIDATE_DATA_VTD_S1: Invalidation data for VTD_S1 +* @IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3: Invalidation data for ARM SMMUv3 */ enum iommu_hwpt_invalidate_data_type {IOMMU_HWPT_INVALIDATE_DATA_VTD_S1 =0 , +IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3 =1 , };/**
@@ -727,6 +729,28 @@ struct iommu_hwpt_vtd_s1_invalidate {__u32 __reserved; }; +/** +* struct iommu_viommu_arm_smmuv3_invalidate - ARM SMMUv3 cahce invalidation +* (IOMMU_VIOMMU_INVALIDATE_DATA_ARM_SMMUV3) +* @cmd: 128-bit cache invalidation command that runs in SMMU CMDQ. +* Must be little-endian. +* +* Supported command list only when passing in a vIOMMU via @hwpt_id: +* CMDQ_OP_TLBI_NSNH_ALL +* CMDQ_OP_TLBI_NH_VA +* CMDQ_OP_TLBI_NH_VAA +* CMDQ_OP_TLBI_NH_ALL +* CMDQ_OP_TLBI_NH_ASID +* CMDQ_OP_ATC_INV +* CMDQ_OP_CFGI_CD +* CMDQ_OP_CFGI_CD_ALL +* +* -EIO will be returned if the command is not supported. +*/ +struct iommu_viommu_arm_smmuv3_invalidate { +__aligned_le64 cmd[2 ]; +}; +/** * struct iommu_hwpt_invalidate - ioctl(IOMMU_HWPT_INVALIDATE) * @size: sizeof(struct iommu_hwpt_invalidate)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +130 -0
145 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -3
@@ -766,9 +766,9 @@ static void arm_smmu_cmdq_write_entries(struct arm_smmu_cmdq *cmdq, u64 *cmds,* insert their own list of commands then all of the commands from one * CPU will appear before any of the commands from the other CPU. */ -static int arm_smmu_cmdq_issue_cmdlist (struct arm_smmu_device *smmu, -struct arm_smmu_cmdq *cmdq, -u64 *cmds,int n,bool sync) +int arm_smmu_cmdq_issue_cmdlist (struct arm_smmu_device *smmu, +struct arm_smmu_cmdq *cmdq,u64 *cmds,int n, +bool sync) {u64 cmd_sync[CMDQ_ENT_DWORDS ];u32 prod;
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v5 12/13] iommu/arm-smmu-v3: Add arm_vsmmu_cache_invalidate
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:51 AMImplement the vIOMMU's
cache_invalidateop for user space to invalidate the
IOTLB entries, Device ATS and CD entries that are still cached by hardware.Add struct
iommu_viommu_arm_smmuv3_invalidatedefining invalidation
there are three 'iommu' in the name. What about:
arm_vsmmuv3_invalidate?
PATCH v5 13/13 iommu/arm-smmu-v3: Allow ATS for IOMMU_DOMAIN_NESTED
In reply to Nicolin ChenFrom: Jason Gunthorpe <[email protected]>
Now, ATC invalidation can be done with the vIOMMU invalidation op. A guest owned IOMMU_DOMAIN_NESTED can do an ATS too. Allow it to pass in the EATS field via the vSTE words.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 4 ++- include/uapi/linux/iommufd.h | 2 +- .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 31 ++++++++++++++++--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 26 +++++++++++++--- 4 files changed, 53 insertions(+), 10 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +3 -1
@@ -305,7 +305,7 @@ static inline u32 arm_smmu_strtab_l2_idx(u32 sid)#define STRTAB_STE_1_NESTING_ALLOWED \ cpu_to_le64(STRTAB_STE_1_S1DSS | STRTAB_STE_1_S1CIR | \ STRTAB_STE_1_S1COR | STRTAB_STE_1_S1CSH | \ - STRTAB_STE_1_S1STALLD) + STRTAB_STE_1_S1STALLD | STRTAB_STE_1_EATS)/* * Context descriptors.
@@ -838,6 +838,7 @@ struct arm_smmu_domain {struct arm_smmu_nested_domain {struct iommu_domain domain;struct arm_vsmmu *vsmmu; +bool enable_ats :1 ;__le64 ste[2 ]; };
@@ -879,6 +880,7 @@ struct arm_smmu_master_domain {struct list_head devices_elm;struct arm_smmu_master *master;ioasid_t ssid; + bool nested_ats_flush : 1; };static inline struct arm_smmu_domain *to_smmu_domain (struct iommu_domain *dom)
include/uapi/linux/iommufd.h +1 -1
@@ -404,7 +404,7 @@ struct iommu_hwpt_vtd_s1 {* a user stage-1 Context Descriptor Table. Must be little-endian. * Allowed fields: (Refer to "5.2 Stream Table Entry" in SMMUv3 HW Spec) * - word-0: V, Cfg, S1Fmt, S1ContextPtr, S1CDMax -* - word-1: S1DSS, S1CIR, S1COR, S1CSH, S1STALLD +* - word-1: EATS, S1DSS, S1CIR, S1COR, S1CSH, S1STALLD * * -EIO will be returned if @ste is not legal or contains any non-allowed field. * Cfg can be used to select a S1, Bypass or Abort configuration. A Bypass
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +27 -4
@@ -96,8 +96,6 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain, .master = master, .old_domain = iommu_get_domain_for_dev(dev), .ssid = IOMMU_NO_PASID, - /* Currently invalidation of ATC is not supported */ - .disable_ats = true, };struct arm_smmu_ste ste;int ret;
@@ -108,6 +106,15 @@ static int arm_smmu_attach_dev_nested(struct iommu_domain *domain,return -EBUSY ;mutex_lock (&arm_smmu_asid_lock); +/* +* The VM has to control the actual ATS state at the PCI device because +* we forward the invalidations directly from the VM. If the VM doesn't +* think ATS is on it will not generate ATC flushes and the ATC will +* become incoherent. Since we can't access the actual virtual PCI ATS +* config bit here base this off the EATS value in the STE. If the EATS +* is set then the VM must generate ATC flushes. +*/ + state.disable_ats = !nested_domain->enable_ats; ret =arm_smmu_attach_prepare (&state, domain);if (ret) {mutex_unlock (&arm_smmu_asid_lock);
@@ -132,8 +139,10 @@ static const struct iommu_domain_ops arm_smmu_nested_ops = { .free = arm_smmu_domain_nested_free, }; -static int arm_smmu_validate_vste (struct iommu_hwpt_arm_smmuv3 *arg) +static int arm_smmu_validate_vste (struct iommu_hwpt_arm_smmuv3 *arg, +bool *enable_ats) { +unsigned int eats;unsigned int cfg;if (!(arg->ste[0 ] &cpu_to_le64 (STRTAB_STE_0_V ))) {
@@ -150,6 +159,18 @@ static int arm_smmu_validate_vste(struct iommu_hwpt_arm_smmuv3 *arg)if (cfg !=STRTAB_STE_0_CFG_ABORT && cfg !=STRTAB_STE_0_CFG_BYPASS && cfg !=STRTAB_STE_0_CFG_S1_TRANS )return -EIO ; + +/* +* Only Full ATS or ATS UR is supported +* The EATS field will be set by arm_smmu_make_nested_domain_ste() +*/ + eats =FIELD_GET (STRTAB_STE_1_EATS ,le64_to_cpu (arg->ste[1 ])); + arg->ste[1 ] &= ~cpu_to_le64 (STRTAB_STE_1_EATS ); +if (eats !=STRTAB_STE_1_EATS_ABT && eats !=STRTAB_STE_1_EATS_TRANS ) +return -EIO ; + +if (cfg ==STRTAB_STE_0_CFG_S1_TRANS ) + *enable_ats = (eats ==STRTAB_STE_1_EATS_TRANS );return 0 ; }
@@ -160,6 +181,7 @@ arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu, struct arm_vsmmu *vsmmu = container_of(viommu, struct arm_vsmmu, core);struct arm_smmu_nested_domain *nested_domain;struct iommu_hwpt_arm_smmuv3 arg; +bool enable_ats = false;int ret; ret = iommu_copy_struct_from_user(&arg, user_data,
@@ -167,7 +189,7 @@ arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu,if (ret)return ERR_PTR (ret); - ret =arm_smmu_validate_vste (&arg); + ret =arm_smmu_validate_vste (&arg, &enable_ats);if (ret)return ERR_PTR (ret);
@@ -177,6 +199,7 @@ arm_vsmmu_alloc_domain_nested(struct iommufd_viommu *viommu, nested_domain->domain.type =IOMMU_DOMAIN_NESTED ; nested_domain->domain.ops = &arm_smmu_nested_ops; + nested_domain->enable_ats = enable_ats; nested_domain->vsmmu = vsmmu; nested_domain->ste[0 ] = arg.ste[0 ]; nested_domain->ste[1 ] = arg.ste[1 ] & ~cpu_to_le64 (STRTAB_STE_1_EATS );
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +22 -4
@@ -2107,7 +2107,16 @@ int arm_smmu_atc_inv_domain(struct arm_smmu_domain *smmu_domain,if (!master->ats_enabled)continue ; -arm_smmu_atc_inv_to_cmd (master_domain->ssid, iova, size, &cmd); +if (master_domain->nested_ats_flush) { +/* +* If a S2 used as a nesting parent is changed we have +* no option but to completely flush the ATC. +*/ +arm_smmu_atc_inv_to_cmd (IOMMU_NO_PASID ,0 ,0 , &cmd); + }else { +arm_smmu_atc_inv_to_cmd (master_domain->ssid, iova, size, + &cmd); + }for (i =0 ; i < master->num_streams; i++) { cmd.atc.sid = master->streams[i].id;
@@ -2631,7 +2640,7 @@ static void arm_smmu_disable_pasid(struct arm_smmu_master *master)static struct arm_smmu_master_domain *arm_smmu_find_master_domain (struct arm_smmu_domain *smmu_domain,struct arm_smmu_master *master, -ioasid_t ssid) +ioasid_t ssid,bool nested_ats_flush) {struct arm_smmu_master_domain *master_domain;
@@ -2640,7 +2649,8 @@ arm_smmu_find_master_domain(struct arm_smmu_domain *smmu_domain,list_for_each_entry (master_domain, &smmu_domain->devices, devices_elm) {if (master_domain->master == master && - master_domain->ssid == ssid) + master_domain->ssid == ssid && + master_domain->nested_ats_flush == nested_ats_flush)return master_domain; }return NULL ;
@@ -2671,13 +2681,18 @@ static void arm_smmu_remove_master_domain(struct arm_smmu_master *master, {struct arm_smmu_domain *smmu_domain =to_smmu_domain_devices (domain);struct arm_smmu_master_domain *master_domain; +bool nested_ats_flush = false;unsigned long flags;if (!smmu_domain)return ; +if (domain->type ==IOMMU_DOMAIN_NESTED ) + nested_ats_flush =to_smmu_nested_domain (domain)->enable_ats; +spin_lock_irqsave (&smmu_domain->devices_lock, flags); - master_domain =arm_smmu_find_master_domain (smmu_domain, master, ssid); + master_domain =arm_smmu_find_master_domain (smmu_domain, master, ssid, + nested_ats_flush);if (master_domain) {list_del (&master_domain->devices_elm);kfree (master_domain);
@@ -2744,6 +2759,9 @@ int arm_smmu_attach_prepare(struct arm_smmu_attach_state *state,return -ENOMEM ; master_domain->master = master; master_domain->ssid = state->ssid; +if (new_domain->type ==IOMMU_DOMAIN_NESTED ) + master_domain->nested_ats_flush = +to_smmu_nested_domain (new_domain)->enable_ats;/* * During prepare we want the current smmu_domain and new
Signature · 1 line
2.43.0
Tian, Kevin
maintainerQuoted 29 lines
From: Nicolin Chen <[email protected]>
Sent: Saturday, October 26, 2024 7:50 AMFollowing the previous vIOMMU series, this adds another vDEVICE structure, representing the association from an
iommufd_deviceto aniommufd_viommu.
This gives the whole architecture a new "v" layer:________________________________________________________________
_______| iommufd (with vIOMMU/vDEVICE) | | _____________ _____________ | | | | | | | | |----------------| vIOMMU |<---| vDEVICE |<------| | | | | | |_____________| | | | | ______ | | _____________ ___|____ | | | | | | | | | | | | | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | | | |______| |_____________| |_____________| |________| ||______|________|______________|__________________|_____________ __|_____| | | | | | ______v_____ | ______v_____ ______v_____ ___v__ | struct | | PFN | (paging) | | (nested) | |struct| |iommu_device| |------>|iommu_domain|<----|iommu_domain|<---- |device| |____________| storage|____________| |____________| |______|
Quoted 4 lines
This vDEVICE object is used to collect and store all vIOMMU-related device information/attributes in a VM. As an initial series for vDEVICE, add only the
virt_idto the vDEVICE, which is a vIOMMU specific device ID in a VM:
e.g. vSID of ARM SMMUv3, vDeviceID of AMD IOMMU, and vID of Intel VT-d
s/vID/vRID/ for VT-d
Quoted 5 lines
to
a Context Table. Thisvirt_idhelps IOMMU drivers to link the vID to a pID of the device against the physical IOMMU instance. This is essential for a vIOMMU-based invalidation, where the request contains a device's vID for a device cache flush, e.g. ATC invalidation.
probably connect this to vCMDQ passthrough? otherwise for sw-based invalidation the userspace can always replace vID with pID before submitting the request.
Quoted 105 lines
Therefore, with this vDEVICE object, support a vIOMMU-based invalidation, by reusing
IOMMUFD_CMD_HWPT_INVALIDATEfor a vIOMMU object to flush cache
with a given driver data.As for the implementation of the series, add driver support in ARM SMMUv3 for a real world use case.
This series is on Github:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p2-v5For testing, try this "with-rmr" branch:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p2-v5-
with-rmr
Paring QEMU branch for testing:
https://github.com/nicolinc/qemu/commits/wip/for_iommufd_viommu_p2-
v5Changelog
v5
* Dropped driver-allocated vDEVICE support
* Changedvdev_to_devhelper toiommufd_viommu_find_dev
v4
https://lore.kernel.org/all/[email protected]/
* Added missing brackets in switch-case
* Fixed the unreleased idev refcount issue
* Reworked theiommufd_vdevice_allocallocator
* Dropped support forIOMMU_VIOMMU_TYPE_DEFAULT
* Added missingTEST_LENGTHandfail_nthcoverages
* Added a verification to the driver-allocated vDEVICE object
* Added aniommufd_vdevice_abortfor a missing mutex protection
* Added a u64 structurearm_vsmmu_invalidation_cmdfor user command conversion
v3
https://lore.kernel.org/all/[email protected]/
* Added Jason's Reviewed-by
* Split this invalidation part out of the part-1 series
* RepurposedVDEV_IDioctl to a wider vDEVICE structure and ioctl
* Reducedviommu_apifunctions by allowing drivers to access viommu and vdevice structure directly
* Droppedvdevs_rwsemby usingxa_lockinstead
* Droppedarm_smmu_cache_invalidate_user
v2
https://lore.kernel.org/all/[email protected]/
* Limitedvdev_idto one per idev
* Added arw_semto protect thevdev_idlist
* Reworked driver-level APIs with proper lockings
* Added a newviommu_apifile forIOMMUFD_DRIVERconfig
* Dropped uselessiommu_devpoint from the viommu structure
* Added missing index numnbers to new types in the uAPI header
* DroppedIOMMU_VIOMMU_INVALIDATEuAPI; Instead, reuse the HWPT
one
* Reworkedmock_viommu_cache_invalidate()using the new iommu helper
* Reordered details of set/unset_vdev_id handlers for proper lockings
v1
https://lore.kernel.org/all/[email protected]/Thanks!
NicolinJason Gunthorpe (2):
iommu: Addiommu_copy_struct_from_full_user_arrayhelper iommu/arm-smmu-v3: Allow ATS forIOMMU_DOMAIN_NESTEDNicolin Chen (11):
iommufd/viommu: AddIOMMUFD_OBJ_VDEVICEandIOMMU_VDEVICE_ALLOCioctl
iommufd/selftest: AddIOMMU_VDEVICE_ALLOCtest coverage iommu/viommu: Addcache_invalidatetoiommufd_viommu_opsiommufd/hw_pagetable: Enforce invalidation op on vIOMMU-based
hwpt_nested
iommufd: Allowhwpt_idto carryviommu_idforIOMMU_HWPT_INVALIDATE
iommufd/viommu: Addiommufd_viommu_find_devhelper iommufd/selftest: Addmock_viommu_cache_invalidateiommufd/selftest: AddIOMMU_TEST_OP_DEV_CHECK_CACHEtest
command
iommufd/selftest: Add vIOMMU coverage forIOMMU_HWPT_INVALIDATE
ioctl
Documentation: userspace-api: iommufd: Update vDEVICE iommu/arm-smmu-v3: Addarm_vsmmu_cache_invalidatedrivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 9 +- drivers/iommu/iommufd/iommufd_private.h | 20 ++ drivers/iommu/iommufd/iommufd_test.h | 30 +++ include/linux/iommu.h | 48 ++++- include/linux/iommufd.h | 21 ++ include/uapi/linux/iommufd.h | 61 +++++- tools/testing/selftests/iommu/iommufd_utils.h | 83 +++++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 161 +++++++++++++- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 32 ++- drivers/iommu/iommufd/device.c | 11 + drivers/iommu/iommufd/driver.c | 13 ++ drivers/iommu/iommufd/hw_pagetable.c | 36 +++- drivers/iommu/iommufd/main.c | 7 + drivers/iommu/iommufd/selftest.c | 98 ++++++++- drivers/iommu/iommufd/viommu.c | 101 +++++++++ tools/testing/selftests/iommu/iommufd.c | 204 +++++++++++++++++- .../selftests/iommu/iommufd_fail_nth.c | 4 + Documentation/userspace-api/iommufd.rst | 41 +++- 18 files changed, 942 insertions(+), 38 deletions(-)--
2.43.0
Jason Gunthorpe
maintainerQuoted 9 lines
Quoted 5 lines
to
a Context Table. Thisvirt_idhelps IOMMU drivers to link the vID to a pID of the device against the physical IOMMU instance. This is essential for a vIOMMU-based invalidation, where the request contains a device's vID for a device cache flush, e.g. ATC invalidation.probably connect this to vCMDQ passthrough? otherwise for sw-based invalidation the userspace can always replace vID with pID before submitting the request.
You can't just do that, the ID in the invalidation command has to be validated by the kernel.
At that point you may as well just use the vID instead of inventing a new means to validate raw pIDs.
VT-D avoided this so far because it is pushing the invalidation through the device object and the device object provides the ID directly. No IDs in commands, no switching devices during a command batch.
Jason
Tian, Kevin
maintainerQuoted 8 lines
From: Jason Gunthorpe <[email protected]>
Sent: Monday, October 28, 2024 10:17 PMQuoted 4 lines
Quoted 4 lines
to
a Context Table. Thisvirt_idhelps IOMMU drivers to link the vID to a pID of the device against the physical IOMMU instance. This is essential for a vIOMMU-based invalidation, where the request contains a device's vIDfor a
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
device cache flush, e.g. ATC invalidation.
probably connect this to vCMDQ passthrough? otherwise for sw-based invalidation the userspace can always replace vID with pID before submitting the request.
You can't just do that, the ID in the invalidation command has to be validated by the kernel.
sure the ID must be validated to match the iommufd_device but not exactly going through a vID indirectly.
Quoted 3 lines
At that point you may as well just use the vID instead of inventing a new means to validate raw pIDs.
w/o VCMDQ stuff validating raw pID sounds the natural way while vID is more like a new means and not mandatory.
I'm fine with this design but just didn't feel the above description is accurate.