iommufd: Add vIOMMU infrastructure (Part-1)
Subsystems (5)
v4 cover iommufd: Add vIOMMU infrastructure (Part-1)
This series introduces a new vIOMMU infrastructure and related ioctls.
IOMMUFD has been using the HWPT infrastructure for all cases, including a nested IO page table support. Yet, there're limitations for an HWPT-based structure to support some advanced HW-accelerated features, such as CMDQV on NVIDIA Grace, and HW-accelerated vIOMMU on AMD. Even for a multi-IOMMU environment, it is not straightforward for nested HWPTs to share the same parent HWPT (stage-2 IO pagetable), with the HWPT infrastructure alone: a parent HWPT typically hold one stage-2 IO pagetable and tag it with only one ID in the cache entries. When sharing one large stage-2 IO pagetable across physical IOMMU instances, that one ID may not always be available across all the IOMMU instances. In other word, it's ideal for SW to have a different container for the stage-2 IO pagetable so it can hold another ID that's available.
For this "different container", add vIOMMU, an additional layer to hold extra virtualization information:
_______________________________________________________________________
| iommufd (with vIOMMU) |
| |
| [5] |
| _____________ |
| | | |
| |----------------| vIOMMU | |
| | | | |
| | | | |
| | [1] | | [4] [2] |
| | ______ | | _____________ ________ |
| | | | | [3] | | | | | |
| | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | |
| | |______| |_____________| |_____________| |________| |
| | | | | | |
|______|________|______________|__________________|_______________|_____|
| | | | |
______v_____ | ______v_____ ______v_____ ___v__
| struct | | PFN | (paging) | | (nested) | |struct|
|iommu_device| |------>|iommu_domain|<----|iommu_domain|<----|device|
|____________| storage|____________| |____________| |______|The vIOMMU object should be seen as a slice of a physical IOMMU instance that is passed to or shared with a VM. That can be some HW/SW resources:
- Security namespace for guest owned ID, e.g. guest-controlled cache tags
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reporting
On a multi-IOMMU system, the vIOMMU object must be instanced to the number of the physical IOMMUs that are passed to (via devices) a guest VM, while being able to hold the shareable parent HWPT. Each vIOMMU then just needs to allocate its own individual ID to tag its own cache:
----------------------------
---------------- | | paging_hwpt0 |
| hwpt_nested0 |--->| viommu0 ------------------
---------------- | | IDx |
----------------------------
----------------------------
---------------- | | paging_hwpt0 |
| hwpt_nested1 |--->| viommu1 ------------------
---------------- | | IDy |
----------------------------As an initial part-1, add IOMMUFD_CMD_VIOMMU_ALLOC ioctl for an allocation only. And implement it in arm-smmu-v3 driver as a real world use case.
More vIOMMU-based structs and ioctls will be introduced in the follow-up series to support vDEVICE, vIRQ (vEVENT) and vQUEUE objects. Although we repurposed the vIOMMU object from an earlier RFC, just for a referece:
https://lore.kernel.org/all/[email protected]/
This series is on Github:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p1-v4
(paring QEMU branch for testing will be provided with the part2 series)
Changelog
v4
* Added "Reviewed-by" from Jason
* Dropped IOMMU_VIOMMU_TYPE_DEFAULT support
* Dropped iommufd_object_alloc_elm renamings
* Renamed iommufd's viommu_api.c to driver.c
* Reworked iommufd_viommu_alloc helper
* Added a separate iommufd_hwpt_nested_alloc_for_viommu function for hwpt_nested allocations on a vIOMMU, and added comparison between viommu->iommu_dev->ops and dev_iommu_ops(idev->dev)
* Replaced s2_parent with vsmmu in arm_smmu_nested_domain
* Replaced domain_alloc_user in iommu_ops with domain_alloc_nested in viommu_ops
* Replaced wait_queue_head_t with a completion, to delay the unplug of mock_iommu_dev
* Corrected documentation graph that was missing struct iommu_device
* Added an iommufd_verify_unfinalized_object helper to verify driver- allocated vIOMMU/vDEVICE objects
* Added missing test cases for TEST_LENGTH and fail_nth
v3
https://lore.kernel.org/all/[email protected]/
* Rebased on top of Jason's nesting v3 series
https://lore.kernel.org/all/[email protected]/
* Split the series into smaller parts
* Added Jason's Reviewed-by
* Added back viommu->iommu_dev
* Added support for driver-allocated vIOMMU v.s. core-allocated
* Dropped arm_smmu_cache_invalidate_user
* Added an iommufd_test_wait_for_users() in selftest
* Reworked test code to make viommu an individual FIXTURE
* Added missing TEST_LENGTH case for the new ioctl command
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
Nicolin Chen (11):
iommufd: Move struct iommufd_object to public iommufd header
iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
iommufd: Add iommufd_verify_unfinalized_object iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
iommufd: Add domain_alloc_nested op to iommufd_viommu_ops
iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC iommufd/selftest: Add refcount to mock_iommu_device iommufd/selftest: Add IOMMU_VIOMMU_TYPE_SELFTEST iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage
Documentation: userspace-api: iommufd: Update vIOMMU iommu/arm-smmu-v3: Add IOMMU_VIOMMU_TYPE_ARM_SMMUV3 support
drivers/iommu/iommufd/Makefile | 5 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 26 +++--- drivers/iommu/iommufd/iommufd_private.h | 36 ++------ drivers/iommu/iommufd/iommufd_test.h | 2 + include/linux/iommu.h | 14 +++ include/linux/iommufd.h | 89 +++++++++++++++++++ include/uapi/linux/iommufd.h | 56 ++++++++++-- tools/testing/selftests/iommu/iommufd_utils.h | 28 ++++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 79 ++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +- drivers/iommu/iommufd/driver.c | 38 ++++++++ drivers/iommu/iommufd/hw_pagetable.c | 69 +++++++++++++- drivers/iommu/iommufd/main.c | 58 ++++++------ drivers/iommu/iommufd/selftest.c | 73 +++++++++++++-- drivers/iommu/iommufd/viommu.c | 85 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 78 ++++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++ Documentation/userspace-api/iommufd.rst | 69 +++++++++++++- 18 files changed, 701 insertions(+), 124 deletions(-) create mode 100644 drivers/iommu/iommufd/driver.c create mode 100644 drivers/iommu/iommufd/viommu.c
Signature · 1 line
2.43.0
PATCH v4 1/11 iommufd: Move struct iommufd_object to public iommufd header
Prepare for an embedded structure design for driver-level iommufd_viommu objects:
// include/linux/iommufd.h
struct iommufd_viommu {
struct iommufd_object obj;
....
};
// Some IOMMU driver
struct iommu_driver_viommu {
struct iommufd_viommu core;
....
};It has to expose struct iommufd_object and enum iommufd_object_type from the core-level private header to the public iommufd header.
--- drivers/iommu/iommufd/iommufd_private.h | 25 +------------------------ include/linux/iommufd.h | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 24 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +1 -24
@@ -5,8 +5,8 @@#define __IOMMUFD_PRIVATE_H#include <linux/iommu.h> +#include <linux/iommufd.h> #include <linux/iova_bitmap.h> -#include <linux/refcount.h> #include <linux/rwsem.h> #include <linux/uaccess.h> #include <linux/xarray.h>
@@ -122,29 +122,6 @@ static inline int iommufd_ucmd_respond(struct iommufd_ucmd *ucmd,return 0 ; } -enum iommufd_object_type { -IOMMUFD_OBJ_NONE , -IOMMUFD_OBJ_ANY =IOMMUFD_OBJ_NONE , -IOMMUFD_OBJ_DEVICE , -IOMMUFD_OBJ_HWPT_PAGING , -IOMMUFD_OBJ_HWPT_NESTED , -IOMMUFD_OBJ_IOAS , -IOMMUFD_OBJ_ACCESS , -IOMMUFD_OBJ_FAULT , -#ifdef CONFIG_IOMMUFD_TEST -IOMMUFD_OBJ_SELFTEST , -#endif -IOMMUFD_OBJ_MAX , -}; - -/* Base struct for all objects with a userspace ID handle. */ -struct iommufd_object { -refcount_t shortterm_users; -refcount_t users; -enum iommufd_object_type type; -unsigned int id; -}; -static inline bool iommufd_lock_obj (struct iommufd_object *obj) {if (!refcount_inc_not_zero (&obj->users))
include/linux/iommufd.h +24 -0
@@ -8,6 +8,7 @@#include <linux/err.h> #include <linux/errno.h> +#include <linux/refcount.h> #include <linux/types.h> struct device ;
@@ -18,6 +19,29 @@ struct iommufd_ctx;struct iommufd_device ;struct page ; +enum iommufd_object_type { +IOMMUFD_OBJ_NONE , +IOMMUFD_OBJ_ANY =IOMMUFD_OBJ_NONE , +IOMMUFD_OBJ_DEVICE , +IOMMUFD_OBJ_HWPT_PAGING , +IOMMUFD_OBJ_HWPT_NESTED , +IOMMUFD_OBJ_IOAS , +IOMMUFD_OBJ_ACCESS , +IOMMUFD_OBJ_FAULT , +#ifdef CONFIG_IOMMUFD_TEST +IOMMUFD_OBJ_SELFTEST , +#endif +IOMMUFD_OBJ_MAX , +}; + +/* Base struct for all objects with a userspace ID handle. */ +struct iommufd_object { +refcount_t shortterm_users; +refcount_t users; +enum iommufd_object_type type; +unsigned int id; +}; +struct iommufd_device *iommufd_device_bind (struct iommufd_ctx *ictx,struct device *dev,u32 *id);void iommufd_device_unbind (struct iommufd_device *idev);
Signature · 1 line
2.43.0
RE: [PATCH v4 01/11] iommufd: Move struct iommufd_object to public iommufd header
Quoted 17 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMPrepare for an embedded structure design for driver-level
iommufd_viommuobjects:// include/linux/iommufd.h struct iommufd_viommu { struct iommufd_object obj; .... }; // Some IOMMU driver struct iommu_driver_viommu { struct iommufd_viommu core; .... };
Quoted 6 lines
It has to expose struct
iommufd_objectand enumiommufd_object_typefrom
the core-level private header to the public iommufd header.Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
PATCH v4 2/11 iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
In reply to Nicolin ChenAdd a new IOMMUFD_OBJ_VIOMMU with an iommufd_viommu structure to represent a slice of physical IOMMU device passed to or shared with a user space VM. This slice, now a vIOMMU object, is a group of virtualization resources of a physical IOMMU's, such as:
- Security namespace for guest owned ID, e.g. guest-controlled cache tags
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reporting
Add a new viommu_alloc op in iommu_ops, for drivers to allocate their own vIOMMU structures. And this allocation also needs a free(), so add struct iommufd_viommu_ops.
To simplify a vIOMMU allocation, provide a iommufd_viommu_alloc() helper. It's suggested that a driver should embed a core-level viommu structure in its driver-level viommu struct and call the iommufd_viommu_alloc() helper, meanwhile the driver can also implement a viommu ops:
struct my_driver_viommu {
struct iommufd_viommu core;
/* driver-owned properties/features */
....
};
static const struct iommufd_viommu_ops my_driver_viommu_ops = {
.free = my_driver_viommu_free,
/* future ops for virtualization features */
....
};
static struct iommufd_viommu my_driver_viommu_alloc(...)
{
struct my_driver_viommu *my_viommu =
iommufd_viommu_alloc(ictx, my_driver_viommu, core,
my_driver_viommu_ops);
/* Init my_viommu and related HW feature */
....
return &my_viommu->core;
}
static struct iommu_domain_ops my_driver_domain_ops = {
....
.viommu_alloc = my_driver_viommu_alloc,
};To make the Kernel config work between a driver and the iommufd core, move the _iommufd_object_alloc helper into a new driver.c file that builds with CONFIG_IOMMUFD_DRIVER.
--- drivers/iommu/iommufd/Makefile | 2 +- drivers/iommu/iommufd/iommufd_private.h | 4 -- include/linux/iommu.h | 14 +++++++ include/linux/iommufd.h | 56 +++++++++++++++++++++++++ drivers/iommu/iommufd/driver.c | 38 +++++++++++++++++ drivers/iommu/iommufd/main.c | 32 -------------- 6 files changed, 109 insertions(+), 37 deletions(-) create mode 100644 drivers/iommu/iommufd/driver.c
drivers/iommu/iommufd/Makefile +1 -1
@@ -12,4 +12,4 @@ iommufd-y := \ iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o obj-$(CONFIG_IOMMUFD) += iommufd.o -obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o +obj-$(CONFIG_IOMMUFD_DRIVER) += iova_bitmap.o driver.o
drivers/iommu/iommufd/iommufd_private.h +0 -4
@@ -202,10 +202,6 @@ iommufd_object_put_and_try_destroy(struct iommufd_ctx *ictx,iommufd_object_remove (ictx, obj, obj->id,0 ); } -struct iommufd_object *_iommufd_object_alloc (struct iommufd_ctx *ictx, -size_t size, -enum iommufd_object_type type); -#define __iommufd_object_alloc (ictx, ptr, type, obj) \ container_of(_iommufd_object_alloc( \ ictx, \
include/linux/iommu.h +14 -0
@@ -42,6 +42,8 @@ struct notifier_block;struct iommu_sva ;struct iommu_dma_cookie ;struct iommu_fault_param ; +struct iommufd_ctx ; +struct iommufd_viommu ;#define IOMMU_FAULT_PERM_READ (1 << 0)/* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1)/* write */
@@ -542,6 +544,14 @@ static inline int __iommu_copy_struct_from_user_array(* @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid * will be blocked by the hardware. +* @viommu_alloc: Allocate an iommufd_viommu on a physical IOMMU instance behind +* the @dev, as the set of virtualization resources shared/passed +* to user space IOMMU instance. And associate it with a nesting +* @parent_domain. The @viommu_type must be defined in the header +* include/uapi/linux/iommufd.h +* It is suggested to call iommufd_viommu_alloc() helper for +* a bundled allocation of the core and the driver structures, +* using the given @ictx pointer. * @pgsize_bitmap: bitmap of all possible supported page sizes * @owner: Driver module providing these ops * @identity_domain: An always available, always attachable identity
@@ -591,6 +601,10 @@ struct iommu_ops {void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid,struct iommu_domain *domain); +struct iommufd_viommu *(*viommu_alloc)( +struct device *dev,struct iommu_domain *parent_domain, +struct iommufd_ctx *ictx,unsigned int viommu_type); +const struct iommu_domain_ops *default_domain_ops;unsigned long pgsize_bitmap;struct module *owner;
include/linux/iommufd.h +56 -0
78 lines not on this page · raw message
drivers/iommu/iommufd/driver.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES +*/ + +#include "iommufd_private.h" + +struct iommufd_object *_iommufd_object_alloc (struct iommufd_ctx *ictx, +size_t size, +enum iommufd_object_type type) +{ +struct iommufd_object *obj; +int rc; + + obj =kzalloc (size,GFP_KERNEL_ACCOUNT ); +if (!obj) +return ERR_PTR (-ENOMEM ); + obj->type = type; +/* Starts out bias'd by 1 until it is removed from the xarray */ +refcount_set (&obj->shortterm_users,1 ); +refcount_set (&obj->users,1 ); + +/* +* Reserve an ID in the xarray but do not publish the pointer yet since +* the caller hasn't initialized it yet. Once the pointer is published +* in the xarray and visible to other threads we can't reliably destroy +* it anymore, so the caller must complete all errorable operations +* before calling iommufd_object_finalize(). +*/ + rc =xa_alloc (&ictx->objects, &obj->id,XA_ZERO_ENTRY , xa_limit_31b, +GFP_KERNEL_ACCOUNT ); +if (rc) + goto out_free; +return obj; +out_free: +kfree (obj); +return ERR_PTR (rc); +} +EXPORT_SYMBOL_NS_GPL (_iommufd_object_alloc,IOMMUFD );
drivers/iommu/iommufd/main.c +0 -32
@@ -29,38 +29,6 @@ struct iommufd_object_ops {static const struct iommufd_object_ops iommufd_object_ops[];static struct miscdevice vfio_misc_dev; -struct iommufd_object *_iommufd_object_alloc (struct iommufd_ctx *ictx, -size_t size, -enum iommufd_object_type type) -{ -struct iommufd_object *obj; -int rc; - - obj =kzalloc (size,GFP_KERNEL_ACCOUNT ); -if (!obj) -return ERR_PTR (-ENOMEM ); - obj->type = type; -/* Starts out bias'd by 1 until it is removed from the xarray */ -refcount_set (&obj->shortterm_users,1 ); -refcount_set (&obj->users,1 ); - -/* -* Reserve an ID in the xarray but do not publish the pointer yet since -* the caller hasn't initialized it yet. Once the pointer is published -* in the xarray and visible to other threads we can't reliably destroy -* it anymore, so the caller must complete all errorable operations -* before calling iommufd_object_finalize(). -*/ - rc =xa_alloc (&ictx->objects, &obj->id,XA_ZERO_ENTRY , - xa_limit_31b,GFP_KERNEL_ACCOUNT ); -if (rc) - goto out_free; -return obj; -out_free: -kfree (obj); -return ERR_PTR (rc); -} -/* * Allow concurrent access to the object. *
Signature · 1 line
2.43.0
Re: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 12 lines · Nicolin Chen
+ * @viommu_alloc: Allocate an
iommufd_viommuon a physical IOMMU instance behind+ * the @dev, as the set of virtualization resources shared/passed + * to user space IOMMU instance. And associate it with a nesting + * @parent_domain. The @viommu_type must be defined in the header + * include/uapi/linux/iommufd.h + * It is suggested to call iommufd_viommu_alloc() helper for + * a bundled allocation of the core and the driver structures, + * using the given @ictx pointer.* @pgsize_bitmap: bitmap of all possible supported page sizes
* @owner: Driver module providing these ops
* @identity_domain: An always available, always attachable identity
include/linux/iommu.h struct iommu_ops { · line 606
Quoted 6 lines
include/linux/iommu.h +3
-0@@ -591,6 +601,10 @@ struct iommu_ops {void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid,struct iommu_domain *domain); +struct iommufd_viommu *(*viommu_alloc)( +struct device *dev,struct iommu_domain *parent_domain, +struct iommufd_ctx *ictx,unsigned int viommu_type);
Is the vIOMMU object limited to a parent domain?
Thanks,
baolu
Re: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 16 lines · Baolu Lu
Quoted 15 lines · Nicolin Chen
+ * @viommu_alloc: Allocate an
iommufd_viommuon a physical IOMMU instance behind+ * the @dev, as the set of virtualization resources shared/passed + * to user space IOMMU instance. And associate it with a nesting + * @parent_domain. The @viommu_type must be defined in the header + * include/uapi/linux/iommufd.h + * It is suggested to call iommufd_viommu_alloc() helper for + * a bundled allocation of the core and the driver structures, + * using the given @ictx pointer.* @pgsize_bitmap: bitmap of all possible supported page sizes
* @owner: Driver module providing these ops
* @identity_domain: An always available, always attachable identityinclude/linux/iommu.h +0
-0@@ -591,6 +601,10 @@ struct iommu_ops {void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid,struct iommu_domain *domain);
Quoted 5 lines
include/linux/iommu.h struct iommu_ops { · line 606
Quoted 3 lines
include/linux/iommu.h +3
-0+struct iommufd_viommu *(*viommu_alloc)( +struct device *dev,struct iommu_domain *parent_domain, +struct iommufd_ctx *ictx,unsigned int viommu_type);Is the vIOMMU object limited to a parent domain?
Yes, for each vIOMMU (a slice of physical IOMMU per VM), one S2 parent domain is enough. Typically, it has the mappings between gPAs and hPAs. If its format/compatibility allows, this single parent domain can be shared with other vIOMMUs.
Thanks
Nicolin
Re: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 17 lines · Nicolin Chen
Quoted 16 lines · Baolu Lu
Quoted 15 lines · Nicolin Chen
+ * @viommu_alloc: Allocate an
iommufd_viommuon a physical IOMMU instance behind+ * the @dev, as the set of virtualization resources shared/passed + * to user space IOMMU instance. And associate it with a nesting + * @parent_domain. The @viommu_type must be defined in the header + * include/uapi/linux/iommufd.h + * It is suggested to call iommufd_viommu_alloc() helper for + * a bundled allocation of the core and the driver structures, + * using the given @ictx pointer.* @pgsize_bitmap: bitmap of all possible supported page sizes
* @owner: Driver module providing these ops
* @identity_domain: An always available, always attachable identityinclude/linux/iommu.h +0
-0@@ -591,6 +601,10 @@ struct iommu_ops {void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid,struct iommu_domain *domain);
Quoted 8 lines
Quoted 4 lines
include/linux/iommu.h struct iommu_ops { · line 606
Quoted 3 lines
include/linux/iommu.h +3
-0+struct iommufd_viommu *(*viommu_alloc)( +struct device *dev,struct iommu_domain *parent_domain, +struct iommufd_ctx *ictx,unsigned int viommu_type);Is the vIOMMU object limited to a parent domain?
Yes, for each vIOMMU (a slice of physical IOMMU per VM), one S2 parent domain is enough. Typically, it has the mappings between gPAs and hPAs. If its format/compatibility allows, this single parent domain can be shared with other vIOMMUs.
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain that translates gPAs to hPAs could also have a vIOMMU object associated with it.
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Thanks,
baolu
Jason Gunthorpe
maintainerRe: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 9 lines · Baolu Lu
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain that translates gPAs to hPAs could also have a vIOMMU object associated with it.
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Why? The nested parent flavour of the domain is basically free to create, what reason would be to not do that?
If the HW doesn't support it, then does the HW really need/support a VIOMMU?
I suppose it could be made up to the driver, but for now I think we should leave it as is in the core code requiring it until we have a reason to relax it.
Jason
Re: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Baolu Lu
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain that translates gPAs to hPAs could also have a vIOMMU object associated with it.
Quoted 6 lines
Quoted 4 lines
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Why? The nested parent flavour of the domain is basically free to create, what reason would be to not do that?
Above addressed my question. The software using vIOMMU should allocate a domain of nested parent type.
Quoted 7 lines
If the HW doesn't support it, then does the HW really need/support a VIOMMU?
I suppose it could be made up to the driver, but for now I think we should leave it as is in the core code requiring it until we have a reason to relax it.
Yes. In such cases, the iommu driver could always allow nested parent domain allocation, but fails to allocate a nested domain if the hardware is not capable of nesting translation.
Thanks,
baolu
Tian, Kevin
maintainerRE: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
In reply to Jason GunthorpeQuoted 15 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 22, 2024 9:16 PMQuoted 9 lines · Baolu Lu
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain that translates gPAs to hPAs could also have a vIOMMU object associated with it.
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Quoted 6 lines
Why? The nested parent flavour of the domain is basically free to create, what reason would be to not do that?
If the HW doesn't support it, then does the HW really need/support a VIOMMU?
Now it's agreed to build trusted I/O on top of this new vIOMMU object. format-wise probably it's free to assume that nested parent is supported on any new platform which will support trusted I/O. But I'm not sure all the conditions around allowing nested are same as for trusted I/O, e.g. for ARM nesting is allowed only for CANWBS/S2FWB. Are they always guaranteed in trusted I/O configuration?
Baolu did raise a good open to confirm given it will be used beyond nesting. 😊
Jason Gunthorpe
maintainerRe: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 21 lines · Kevin
Quoted 15 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 22, 2024 9:16 PMQuoted 9 lines · Baolu Lu
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain that translates gPAs to hPAs could also have a vIOMMU object associated with it.
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Quoted 5 lines
Why? The nested parent flavour of the domain is basically free to create, what reason would be to not do that?
If the HW doesn't support it, then does the HW really need/support a VIOMMU?
Quoted 6 lines
Now it's agreed to build trusted I/O on top of this new vIOMMU object. format-wise probably it's free to assume that nested parent is supported on any new platform which will support trusted I/O. But I'm not sure all the conditions around allowing nested are same as for trusted I/O, e.g. for ARM nesting is allowed only for CANWBS/S2FWB. Are they always guaranteed in trusted I/O configuration?
ARM is a big ? what exactly will come, but I'm expecting that to be resolved either with continued HW support or Linux will add the cache flushing and relax the test.
Quoted 2 lines
Baolu did raise a good open to confirm given it will be used beyond nesting. 😊
Even CC is "nesting", it is just nested with a fixed Identity S1 in the baseline case. The S2 translation still exists and still has to be consistent with whatever the secure world is doing.
So, my feeling is that the S2 nested domain is mandatory for the viommu, especially for CC, it must exists. In the end there may be more options than just a nested parent.
For instance if the CC design relies on the secure world sharing the CPU and IOMMU page table we might need a new HWPT type to represent that configuration.
From a uapi perspective we seem OK here as the hwpt input could be anything. We might have to adjust some checks in the kernel someday.
Jason
Tian, Kevin
maintainerRE: [PATCH v4 02/11] iommufd: Introduce IOMMUFD_OBJ_VIOMMU and its related struct
Quoted 38 lines
From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 25, 2024 11:24 PMQuoted 7 lines · Kevin
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 22, 2024 9:16 PMQuoted 2 lines · Baolu Lu
Is it feasible to make vIOMMU object more generic, rather than strictly tying it to nested translation? For example, a normal paging domain
that
Quoted 1 line
Quoted 1 line
Quoted 1 line
translates gPAs to hPAs could also have a vIOMMU object associated
with
Quoted 13 lines
Quoted 7 lines
Quoted 6 lines
it.
While we can only support vIOMMU object allocation uAPI for S2 paging domains in the context of this series, we could consider leaving the option open to associate a vIOMMU object with other normal paging domains that are not a nested parent?
Quoted 5 lines
Why? The nested parent flavour of the domain is basically free to create, what reason would be to not do that?
If the HW doesn't support it, then does the HW really need/support a VIOMMU?
Quoted 6 lines
Now it's agreed to build trusted I/O on top of this new vIOMMU object. format-wise probably it's free to assume that nested parent is supported on any new platform which will support trusted I/O. But I'm not sure all the conditions around allowing nested are same as for trusted I/O, e.g. for ARM nesting is allowed only for CANWBS/S2FWB. Are they always guaranteed in trusted I/O configuration?
ARM is a big ? what exactly will come, but I'm expecting that to be resolved either with continued HW support or Linux will add the cache flushing and relax the test.
Quoted 6 lines
Quoted 2 lines
Baolu did raise a good open to confirm given it will be used beyond nesting. 😊
Even CC is "nesting", it is just nested with a fixed Identity S1 in the baseline case. The S2 translation still exists and still has to be consistent with whatever the secure world is doing.
this is true. That is why I asked more from the conditions around enabling nested instead of the translation/format itself.
Quoted 5 lines
So, my feeling is that the S2 nested domain is mandatory for the viommu, especially for CC, it must exists. In the end there may be more options than just a nested parent.
Quoted 7 lines
For instance if the CC design relies on the secure world sharing the CPU and IOMMU page table we might need a new HWPT type to represent that configuration.
From a uapi perspective we seem OK here as the hwpt input could be anything. We might have to adjust some checks in the kernel someday.
yes, that could be extended in case of a need.
PATCH v4 3/11 iommufd: Add iommufd_verify_unfinalized_object
In reply to Nicolin ChenTo support driver-allocated vIOMMU objects, it's suggested to call the allocator helper in IOMMU dirvers. However, there is no guarantee that drivers will all use it and allocate objects properly.
Add a helper for iommufd core to verify if an unfinalized object is at least reserved in the ictx.
--- drivers/iommu/iommufd/iommufd_private.h | 3 +++ drivers/iommu/iommufd/main.c | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+)
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -152,6 +152,9 @@ 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);
drivers/iommu/iommufd/main.c +20 -0
@@ -89,6 +89,26 @@ 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) {
Signature · 1 line
2.43.0
RE: [PATCH v4 03/11] iommufd: Add iommufd_verify_unfinalized_object
Quoted 11 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMTo support driver-allocated vIOMMU objects, it's suggested to call the allocator helper in IOMMU dirvers. However, there is no guarantee that drivers will all use it and allocate objects properly.
Add a helper for iommufd core to verify if an unfinalized object is at least reserved in the ictx.
Signed-off-by Nicolin Chen <[email protected]>
PATCH v4 4/11 iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
In reply to Nicolin ChenAdd a new ioctl for user space to do a vIOMMU allocation. It must be based on a nesting parent HWPT, so take its refcount.
If an IOMMU driver supports a driver-managed vIOMMU object, it must define its own IOMMU_VIOMMU_TYPE_ in the uAPI header and implement a viommu_alloc op in its iommu_ops.
--- drivers/iommu/iommufd/Makefile | 3 +- drivers/iommu/iommufd/iommufd_private.h | 3 + include/uapi/linux/iommufd.h | 40 ++++++++++++ drivers/iommu/iommufd/main.c | 6 ++ drivers/iommu/iommufd/viommu.c | 85 +++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 drivers/iommu/iommufd/viommu.c
drivers/iommu/iommufd/Makefile +2 -1
@@ -7,7 +7,8 @@ iommufd-y := \ ioas.o \ main.o \ pages.o \ - vfio_compat.o + vfio_compat.o \ + viommu.o iommufd-$(CONFIG_IOMMUFD_TEST) += selftest.o
drivers/iommu/iommufd/iommufd_private.h +3 -0
@@ -504,6 +504,9 @@ static inline int iommufd_hwpt_replace_device(struct iommufd_device *idev,return iommu_group_replace_domain (idev->igroup->group, hwpt->domain); } +int iommufd_viommu_alloc_ioctl (struct iommufd_ucmd *ucmd); +void iommufd_viommu_destroy (struct iommufd_object *obj); +#ifdef CONFIG_IOMMUFD_TEST int iommufd_test (struct iommufd_ucmd *ucmd);void iommufd_selftest_destroy (struct iommufd_object *obj);
include/uapi/linux/iommufd.h +40 -0
@@ -51,6 +51,7 @@ enum {IOMMUFD_CMD_HWPT_GET_DIRTY_BITMAP =0x8c ,IOMMUFD_CMD_HWPT_INVALIDATE =0x8d ,IOMMUFD_CMD_FAULT_QUEUE_ALLOC =0x8e , + IOMMUFD_CMD_VIOMMU_ALLOC = 0x8f, };/**
@@ -852,4 +853,43 @@ struct iommu_fault_alloc { __u32 out_fault_fd; };#define IOMMU_FAULT_QUEUE_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_FAULT_QUEUE_ALLOC) + +/** +* enum iommu_viommu_type - Virtual IOMMU Type +* @IOMMU_VIOMMU_TYPE_DEFAULT: Reserved for future use +*/ +enum iommu_viommu_type { +IOMMU_VIOMMU_TYPE_DEFAULT =0 , +}; + +/** +* struct iommu_viommu_alloc - ioctl(IOMMU_VIOMMU_ALLOC) +* @size: sizeof(struct iommu_viommu_alloc) +* @flags: Must be 0 +* @type: Type of the virtual IOMMU. Must be defined in enum iommu_viommu_type +* @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU +* @hwpt_id: ID of a nesting parent HWPT to associate to +* @out_viommu_id: Output virtual IOMMU ID for the allocated object +* +* Allocate a virtual IOMMU object that represents the underlying physical +* IOMMU's virtualization support. The vIOMMU object is a security-isolated +* slice of the physical IOMMU HW that is unique to a specific VM. Operations +* global to the IOMMU are connected to the vIOMMU, such as: +* - Security namespace for guest owned ID, e.g. guest-controlled cache tags +* - Access to a sharable nesting parent pagetable across physical IOMMUs +* - Virtualization of various platforms IDs, e.g. RIDs and others +* - Delivery of paravirtualized invalidation +* - Direct assigned invalidation queues +* - Direct assigned interrupts +* - Non-affiliated event reporting +*/ +struct iommu_viommu_alloc { +__u32 size; +__u32 flags; +__u32 type; +__u32 dev_id; +__u32 hwpt_id; +__u32 out_viommu_id; +}; +#define IOMMU_VIOMMU_ALLOC _IO(IOMMUFD_TYPE, IOMMUFD_CMD_VIOMMU_ALLOC)#endif
drivers/iommu/iommufd/main.c +6 -0
@@ -321,6 +321,7 @@ union ucmd_buffer {struct iommu_ioas_unmap unmap;struct iommu_option option;struct iommu_vfio_ioas vfio_ioas; +struct iommu_viommu_alloc viommu;#ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test;#endif
@@ -372,6 +373,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[] = { val64),IOCTL_OP (IOMMU_VFIO_IOAS ,iommufd_vfio_ioas ,struct iommu_vfio_ioas ,__reserved ), +IOCTL_OP (IOMMU_VIOMMU_ALLOC ,iommufd_viommu_alloc_ioctl , +struct iommu_viommu_alloc ,out_viommu_id ), #ifdef CONFIG_IOMMUFD_TEST IOCTL_OP(IOMMU_TEST_CMD, iommufd_test, struct iommu_test_cmd, last), #endif
@@ -507,6 +510,9 @@ static const struct iommufd_object_ops iommufd_object_ops[] = { [IOMMUFD_OBJ_FAULT ] = { .destroy = iommufd_fault_destroy, }, + [IOMMUFD_OBJ_VIOMMU ] = { + .destroy = iommufd_viommu_destroy, + }, #ifdef CONFIG_IOMMUFD_TEST [IOMMUFD_OBJ_SELFTEST ] = { .destroy = iommufd_selftest_destroy,
drivers/iommu/iommufd/viommu.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v4 04/11] iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMAdd a new ioctl for user space to do a vIOMMU allocation. It must be based on a nesting parent HWPT, so take its refcount.
If an IOMMU driver supports a driver-managed vIOMMU object, it must define
why highlight 'driver-managed', implying a core-managed vIOMMU object some day?
Quoted 6 lines
+/**
+ * structiommu_viommu_alloc-ioctl(IOMMU_VIOMMU_ALLOC)
+ * @size:sizeof(struct iommu_viommu_alloc)
+ * @flags: Must be 0
+ * @type: Type of the virtual IOMMU. Must be defined in enumiommu_viommu_type
Quoted 8 lines
+ * @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU
+ * @hwpt_id: ID of a nesting parent HWPT to associate to
+ * @out_viommu_id: Output virtual IOMMU ID for the allocated object
+ *
+ * Allocate a virtual IOMMU object that represents the underlying physical
+ * IOMMU's virtualization support. The vIOMMU object is a security-isolated
+ * slice of the physical IOMMU HW that is unique to a specific VM.
the object itself is a software abstraction, while a 'slice' is a set of real hw resources.
Re: [PATCH v4 04/11] iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
Quoted 11 lines · Kevin
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMAdd a new ioctl for user space to do a vIOMMU allocation. It must be based on a nesting parent HWPT, so take its refcount.
If an IOMMU driver supports a driver-managed vIOMMU object, it must define
why highlight 'driver-managed', implying a core-managed vIOMMU object some day?
Oh, core-managed vIOMMU is gone since this version. I should have updated the commit message here too.
Quoted 15 lines
Quoted 6 lines
+/**
+ * structiommu_viommu_alloc-ioctl(IOMMU_VIOMMU_ALLOC)
+ * @size:sizeof(struct iommu_viommu_alloc)
+ * @flags: Must be 0
+ * @type: Type of the virtual IOMMU. Must be defined in enumiommu_viommu_typeQuoted 8 lines
+ * @dev_id: The device's physical IOMMU will be used to back the virtual IOMMU
+ * @hwpt_id: ID of a nesting parent HWPT to associate to
+ * @out_viommu_id: Output virtual IOMMU ID for the allocated object
+ *
+ * Allocate a virtual IOMMU object that represents the underlying physical
+ * IOMMU's virtualization support. The vIOMMU object is a security-isolated
+ * slice of the physical IOMMU HW that is unique to a specific VM.
Quoted 2 lines
the object itself is a software abstraction, while a 'slice' is a set of real hw resources.
Yea, let's do this:
* Allocate a virtual IOMMU object, representing the underlying physical IOMMU's
* virtualization support that is a security-isolated slice of the real IOMMU HW
* that is unique to a specific VM.
Thanks
Nicolin
Tian, Kevin
maintainerRE: [PATCH v4 04/11] iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
In reply to Nicolin ChenReview
Quoted 10 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMdrivers/iommu/iommufd/viommu.c new file +5
-0+ + viommu->type = cmd->type; + viommu->ictx = ucmd->ictx; + viommu->hwpt = hwpt_paging; +/* Assume physical IOMMUs are unpluggable (the most likely case) */
(quoted diff) +2
-0+ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); +
so what would happen if this assumption breaks?
Re: [PATCH v4 04/11] iommufd/viommu: Add IOMMU_VIOMMU_ALLOC ioctl
Quoted 4 lines · Kevin
Quoted 4 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AM(quoted diff) +2
-0+ + viommu->type = cmd->type;
Quoted 8 lines
Review
Quoted 6 lines
drivers/iommu/iommufd/viommu.c new file +3
-0+ viommu->ictx = ucmd->ictx; + viommu->hwpt = hwpt_paging; +/* Assume physical IOMMUs are unpluggable (the most likely case) */
(quoted diff) +2
-0+ viommu->iommu_dev = __iommu_get_iommu_dev(idev->dev); +so what would happen if this assumption breaks?
I had a very verbose comments previously that Alexey suggested to optimize away.. Perhaps I should add back the part that mentions adding a refcount for pluggable ones..
Nicolin
PATCH v4 5/11 iommufd: Add domain_alloc_nested op to iommufd_viommu_ops
In reply to Nicolin ChenAllow IOMMU driver to use a vIOMMU object that holds a nesting parent hwpt/domain to allocate a nested domain.
--- include/linux/iommufd.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/iommufd.h +9 -0
@@ -14,6 +14,7 @@struct device ;struct file ;struct iommu_group ; +struct iommu_user_data ;struct iommufd_access ;struct iommufd_ctx ;struct iommufd_device ;
@@ -95,9 +96,17 @@ struct iommufd_viommu {* struct iommufd_viommu_ops - vIOMMU specific operations * @free: Free all driver-specific parts of an iommufd_viommu. The memory of the * vIOMMU will be free-ed by iommufd core after calling this free op. +* @domain_alloc_nested: Allocate a IOMMU_DOMAIN_NESTED on a vIOMMU that holds a +* nesting parent domain (IOMMU_DOMAIN_PAGING). @user_data +* 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. */ struct iommufd_viommu_ops {void (*free)(struct iommufd_viommu *viommu); +struct iommu_domain *(*domain_alloc_nested)( +struct iommufd_viommu *viommu, +const struct iommu_user_data *user_data); };#if IS_ENABLED (CONFIG_IOMMUFD )
Signature · 1 line
2.43.0
RE: [PATCH v4 05/11] iommufd: Add domain_alloc_nested op to iommufd_viommu_ops
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMAllow IOMMU driver to use a vIOMMU object that holds a nesting parent hwpt/domain to allocate a nested domain.
Suggested-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
PATCH v4 6/11 iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC
In reply to Nicolin ChenNow a vIOMMU holds a shareable nesting parent HWPT. So, it can act like that nesting parent HWPT to allocate a nested HWPT.
Support that in the IOMMU_HWPT_ALLOC ioctl handler, and update its kdoc.
Also, add an iommufd_hwpt_nested_alloc_for_viommu helper to allocate a nested HWPT for a vIOMMU object. Since a vIOMMU object holds the parent hwpt's refcount already, increase the refcount of the vIOMMU only.
--- drivers/iommu/iommufd/iommufd_private.h | 1 + include/uapi/linux/iommufd.h | 14 ++--- drivers/iommu/iommufd/hw_pagetable.c | 69 ++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 7 deletions(-)
drivers/iommu/iommufd/iommufd_private.h +1 -0
@@ -288,6 +288,7 @@ struct iommufd_hwpt_paging {struct iommufd_hwpt_nested {struct iommufd_hw_pagetable common;struct iommufd_hwpt_paging *parent; +struct iommufd_viommu *viommu; };static inline bool hwpt_is_paging (struct iommufd_hw_pagetable *hwpt)
include/uapi/linux/iommufd.h +8 -6
@@ -430,7 +430,7 @@ enum iommu_hwpt_data_type {* @size: sizeof(struct iommu_hwpt_alloc) * @flags: Combination of enum iommufd_hwpt_alloc_flags * @dev_id: The device to allocate this HWPT for -* @pt_id: The IOAS or HWPT to connect this HWPT to +* @pt_id: The IOAS or HWPT or vIOMMU to connect this HWPT to * @out_hwpt_id: The ID of the new HWPT * @__reserved: Must be 0 * @data_type: One of enum iommu_hwpt_data_type
@@ -449,11 +449,13 @@ enum iommu_hwpt_data_type {* IOMMU_HWPT_DATA_NONE. The HWPT can be allocated as a parent HWPT for a * nesting configuration by passing IOMMU_HWPT_ALLOC_NEST_PARENT via @flags. * -* A user-managed nested HWPT will be created from a given parent HWPT via -* @pt_id, in which the parent HWPT must be allocated previously via the -* same ioctl from a given IOAS (@pt_id). In this case, the @data_type -* must be set to a pre-defined type corresponding to an I/O page table -* type supported by the underlying IOMMU hardware. +* A user-managed nested HWPT will be created from a given vIOMMU (wrapping a +* parent HWPT) or a parent HWPT via @pt_id, in which the parent HWPT must be +* allocated previously via the same ioctl from a given IOAS (@pt_id). In this +* case, the @data_type must be set to a pre-defined type corresponding to an +* I/O page table type supported by the underlying IOMMU hardware. The device +* via @dev_id and the vIOMMU via @pt_id must be associated to the same IOMMU +* instance. * * If the @data_type is set to IOMMU_HWPT_DATA_NONE, @data_len and * @data_uptr should be zero. Otherwise, both @data_len and @data_uptr
drivers/iommu/iommufd/hw_pagetable.c +68 -1
87 lines not on this page · raw message
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v4 06/11] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC
drivers/iommu/iommufd/hw_pagetable.c iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, · line 276
Quoted 7 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AM(quoted diff) +2
-0+static struct iommufd_hwpt_nested * +iommufd_hwpt_nested_alloc_for_viommu(struct iommufd_viommu*viommu,
drivers/iommu/iommufd/hw_pagetable.c +1
-0+ const struct iommu_user_data *user_data)
probably "_for" can be skipped to reduce the name length
Quoted 30 lines
drivers/iommu/iommufd/hw_pagetable.c +10
-0+{ +struct iommufd_hwpt_nested *hwpt_nested; +struct iommufd_hw_pagetable *hwpt; +int rc; + +if (!viommu->ops || !viommu->ops->domain_alloc_nested) +return ERR_PTR (-EOPNOTSUPP ); + + hwpt_nested = __iommufd_object_alloc( + viommu->ictx, hwpt_nested, IOMMUFD_OBJ_HWPT_NESTED,common.obj);
drivers/iommu/iommufd/hw_pagetable.c +9
-0+if (IS_ERR (hwpt_nested)) +return ERR_CAST (hwpt_nested); + hwpt = &hwpt_nested->common; + + hwpt_nested->viommu = viommu; + hwpt_nested->parent = viommu->hwpt; +refcount_inc (&viommu->obj.users); + + hwpt->domain = viommu->ops->domain_alloc_nested(viommu,
user_data);drivers/iommu/iommufd/hw_pagetable.c +8
-0+if (IS_ERR (hwpt->domain)) { + rc =PTR_ERR (hwpt->domain); + hwpt->domain =NULL ; + goto out_abort; + } + hwpt->domain->owner = viommu->iommu_dev->ops; + + if (WARN_ON_ONCE(hwpt->domain->type !=
IOMMU_DOMAIN_NESTED)) {
drivers/iommu/iommufd/hw_pagetable.c iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, · line 313
Quoted 10 lines
drivers/iommu/iommufd/hw_pagetable.c +10
-0+ rc = -EINVAL ; + goto out_abort; + } +return hwpt_nested; + +out_abort: +iommufd_object_abort_and_destroy (viommu->ictx, &hwpt->obj); +return ERR_PTR (rc); +} +
looks there missed a check on flags in this path.
Re: [PATCH v4 06/11] iommufd: Allow pt_id to carry viommu_id for IOMMU_HWPT_ALLOC
Quoted 9 lines · Kevin
Quoted 7 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AM(quoted diff) +2
-0+static struct iommufd_hwpt_nested * +iommufd_hwpt_nested_alloc_for_viommu(struct iommufd_viommu*viommu,
+ const structiommu_user_data*user_data)probably "
_for" can be skipped to reduce the name length
That would sound like a hwpt_nested allocating vIOMMU...
It'd be probably neutral to have iommufd_viommu_alloc_hwpt_nested, yet we have iommufd_hwpt_nested_alloc (HWPT-based) to align with..
Quoted 1 line
looks there missed a check on flags in this path.
Oh yes, I missed that. Will pass in the cmd->flags.
Thanks
Nicolin
PATCH v4 7/11 iommufd/selftest: Add refcount to mock_iommu_device
In reply to Nicolin ChenFor an iommu_dev that can unplug (so far only this selftest does so), the viommu->iommu_dev pointer has no guarantee of its life cycle after it is copied from the idev->dev->iommu->iommu_dev.
Track the user count of the iommu_dev. Postpone the exit routine using a completion, if refcount is unbalanced. The refcount inc/dec will be added in the following patch.
--- drivers/iommu/iommufd/selftest.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-)
drivers/iommu/iommufd/selftest.c +24 -8
@@ -507,14 +507,17 @@ static bool mock_domain_capable(struct device *dev, enum iommu_cap cap)static struct iopf_queue *mock_iommu_iopf_queue; -static struct iommu_device mock_iommu_device = { -}; +static struct mock_iommu_device { +struct iommu_device iommu_dev; +struct completion complete; +refcount_t users; +} mock_iommu;static struct iommu_device *mock_probe_device (struct device *dev) {if (dev->bus != &iommufd_mock_bus_type.bus)return ERR_PTR (-ENODEV ); -return &mock_iommu_device; +return &mock_iommu.iommu_dev; } static void mock_domain_page_response(struct device *dev, struct iopf_fault *evt,
@@ -1536,24 +1539,27 @@ int __init iommufd_test_init(void)if (rc) goto err_platform; - rc =iommu_device_sysfs_add (&mock_iommu_device, + rc =iommu_device_sysfs_add (&mock_iommu.iommu_dev, &selftest_iommu_dev->dev,NULL ,"%s" ,dev_name (&selftest_iommu_dev->dev));if (rc) goto err_bus; - rc =iommu_device_register_bus (&mock_iommu_device, &mock_ops, + rc =iommu_device_register_bus (&mock_iommu.iommu_dev, &mock_ops, &iommufd_mock_bus_type.bus, &iommufd_mock_bus_type.nb);if (rc) goto err_sysfs; +refcount_set (&mock_iommu.users,1 ); +init_completion (&mock_iommu.complete); + mock_iommu_iopf_queue =iopf_queue_alloc ("mock-iopfq" );return 0 ; err_sysfs: -iommu_device_sysfs_remove (&mock_iommu_device); +iommu_device_sysfs_remove (&mock_iommu.iommu_dev); err_bus:bus_unregister (&iommufd_mock_bus_type.bus); err_platform:
@@ -1563,6 +1569,15 @@ int __init iommufd_test_init(void)return rc; } +static void iommufd_test_wait_for_users (void ) +{ +if (refcount_dec_and_test (&mock_iommu.users)) +return ; +/* Time out waiting for iommu device user count to become 0 */ +WARN_ON (!wait_for_completion_timeout (&mock_iommu.complete, +msecs_to_jiffies (10000 ))); +} +void iommufd_test_exit (void ) {if (mock_iommu_iopf_queue) {
@@ -1570,8 +1585,9 @@ void iommufd_test_exit(void) mock_iommu_iopf_queue =NULL ; } -iommu_device_sysfs_remove (&mock_iommu_device); -iommu_device_unregister_bus (&mock_iommu_device, +iommufd_test_wait_for_users (); +iommu_device_sysfs_remove (&mock_iommu.iommu_dev); +iommu_device_unregister_bus (&mock_iommu.iommu_dev, &iommufd_mock_bus_type.bus, &iommufd_mock_bus_type.nb);bus_unregister (&iommufd_mock_bus_type.bus);
Signature · 1 line
2.43.0
PATCH v4 8/11 iommufd/selftest: Add IOMMU_VIOMMU_TYPE_SELFTEST
In reply to Nicolin ChenImplement the viommu alloc/free functions to increase/reduce refcount of its dependent mock iommu device. User space can verify this loop via the IOMMU_VIOMMU_TYPE_SELFTEST.
--- drivers/iommu/iommufd/iommufd_test.h | 2 ++ drivers/iommu/iommufd/selftest.c | 41 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+)
drivers/iommu/iommufd/iommufd_test.h +2 -0
@@ -180,4 +180,6 @@ struct iommu_hwpt_invalidate_selftest { __u32 iotlb_id; }; +#define IOMMU_VIOMMU_TYPE_SELFTEST 0xdeadbeef +#endif
drivers/iommu/iommufd/selftest.c +41 -0
59 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v4 9/11 iommufd/selftest: Add IOMMU_VIOMMU_ALLOC test coverage
In reply to Nicolin ChenAdd a new iommufd_viommu FIXTURE and setup it up with a vIOMMU object.
Any new vIOMMU feature will be added as a TEST_F under that.
--- tools/testing/selftests/iommu/iommufd_utils.h | 28 +++++++ tools/testing/selftests/iommu/iommufd.c | 78 +++++++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++ 3 files changed, 117 insertions(+)
tools/testing/selftests/iommu/iommufd_utils.h +28 -0
@@ -762,3 +762,31 @@ static int _test_cmd_trigger_iopf(int fd, __u32 device_id, __u32 fault_fd)#define test_cmd_trigger_iopf (device_id, fault_fd) \ ASSERT_EQ(0, _test_cmd_trigger_iopf(self->fd, device_id, fault_fd)) + +static int _test_cmd_viommu_alloc (int fd,__u32 device_id,__u32 hwpt_id, +__u32 type,__u32 flags,__u32 *viommu_id) +{ +struct iommu_viommu_alloc cmd = { + .size =sizeof (cmd), + .flags = flags, + .type = type, + .dev_id = device_id, + .hwpt_id = hwpt_id, + }; +int ret; + + ret =ioctl (fd,IOMMU_VIOMMU_ALLOC , &cmd); +if (ret) +return ret; +if (viommu_id) + *viommu_id = cmd.out_viommu_id; +return 0 ; +} + +#define test_cmd_viommu_alloc (device_id, hwpt_id, type, viommu_id) \ + ASSERT_EQ(0, _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \ + type, 0, viommu_id)) +#define test_err_viommu_alloc (_errno, device_id, hwpt_id, type, viommu_id) \ + EXPECT_ERRNO(_errno, \ + _test_cmd_viommu_alloc(self->fd, device_id, hwpt_id, \ + type, 0, viommu_id))
tools/testing/selftests/iommu/iommufd.c +78 -0
88 lines not on this page · raw message
tools/testing/selftests/iommu/iommufd_fail_nth.c +11 -0
@@ -582,6 +582,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)uint32_t stdev_id;uint32_t idev_id;uint32_t hwpt_id; +uint32_t viommu_id;__u64 iova; self->fd =open ("/dev/iommu" ,O_RDWR );
@@ -624,6 +625,16 @@ TEST_FAIL_NTH(basic_fail_nth, device)if (_test_cmd_mock_domain_replace (self->fd, stdev_id, hwpt_id,NULL ))return -1 ; + +if (_test_cmd_hwpt_alloc (self->fd, idev_id, ioas_id,0 , +IOMMU_HWPT_ALLOC_NEST_PARENT , &hwpt_id, +IOMMU_HWPT_DATA_NONE ,0 ,0 )) +return -1 ; + +if (_test_cmd_viommu_alloc (self->fd, idev_id, hwpt_id, +IOMMU_VIOMMU_TYPE_SELFTEST ,0 , &viommu_id)) +return -1 ; +return 0 ; }
Signature · 1 line
2.43.0
PATCH v4 10/11 Documentation: userspace-api: iommufd: Update vIOMMU
In reply to Nicolin ChenWith the introduction of the new object and its infrastructure, update the doc to reflect that and add a new graph.
--- Documentation/userspace-api/iommufd.rst | 69 ++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-)
Documentation/userspace-api/iommufd.rst +68 -1
@@ -63,6 +63,37 @@ Following IOMMUFD objects are exposed to userspace: space usually has mappings from guest-level I/O virtual addresses to guest- level physical addresses. + - IOMMUFD_OBJ_VIOMMU, representing a slice of the physical IOMMU instance, + passed to or shared with a VM. It may be some HW-accelerated virtualization + features and some SW resources used by the VM. For examples: + * Security namespace for guest owned ID, e.g. guest-controlled cache tags + * Access to a sharable nesting parent pagetable across physical IOMMUs + * Virtualization of various platforms IDs, e.g. RIDs and others + * Delivery of paravirtualized invalidation + * Direct assigned invalidation queues + * Direct assigned interrupts + * Non-affiliated event reporting + Such a vIOMMU object generally has the access to a nesting parent pagetable + to support some HW-accelerated virtualization features. So, a vIOMMU object + must be created given a nesting parent HWPT_PAGING object, and then it would + encapsulate that HWPT_PAGING object. Therefore, a vIOMMU object can be used + to allocate an HWPT_NESTED object in place of the encapsulated HWPT_PAGING. + + .. note:: + + The name "vIOMMU" isn't necessarily identical to a virtualized IOMMU in a + VM. A VM can have one giant virtualized IOMMU running on a machine having + multiple physical IOMMUs, in which case the VMM will dispatch the requests + or configurations from this single virtualized IOMMU instance to multiple + vIOMMU objects created for individual slices of different physical IOMMUs. + In other words, a vIOMMU object is always a representation of one physical + IOMMU, not necessarily of a virtualized IOMMU. For VMMs that want the full + virtualization features from physical IOMMUs, it is suggested to build the + same number of virtualized IOMMUs as the number of physical IOMMUs, so the + passed-through devices would be connected to their own virtualized IOMMUs + backed by corresponding vIOMMU objects, in which case a guest OS would do + the "dispatch" naturally instead of VMM trappings. + All user-visible objects are destroyed via the IOMMU_DESTROY uAPI. The diagrams below show relationships between user-visible objects and kernel
@@ -101,6 +132,28 @@ creating the objects and links:: |------------>|iommu_domain|<----|iommu_domain|<----|device| |____________| |____________| |______| + _______________________________________________________________________ + | iommufd (with vIOMMU) | + | | + | [5] | + | _____________ | + | | | | + | |----------------| vIOMMU | | + | | | | | + | | | | | + | | [1] | | [4] [2] | + | | ______ | | _____________ ________ | + | | | | | [3] | | | | | | + | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | + | | |______| |_____________| |_____________| |________| | + | | | | | | | + |______|________|______________|__________________|_______________|_____| + | | | | | + ______v_____ | ______v_____ ______v_____ ___v__ + | struct | | PFN | (paging) | | (nested) | |struct| + |iommu_device| |------>|iommu_domain|<----|iommu_domain|<----|device| + |____________| storage|____________| |____________| |______| + 1. IOMMUFD_OBJ_IOAS is created via the IOMMU_IOAS_ALLOC uAPI. An iommufd can hold multiple IOAS objects. IOAS is the most generic object and does not expose interfaces that are specific to single IOMMU drivers. All operations
@@ -132,7 +185,8 @@ creating the objects and links:: flag is set. 4. IOMMUFD_OBJ_HWPT_NESTED can be only manually created via the IOMMU_HWPT_ALLOC - uAPI, provided an hwpt_id via @pt_id to associate the new HWPT_NESTED object + uAPI, provided an hwpt_id or a viommu_id of a vIOMMU object encapsulating a + nesting parent HWPT_PAGING via @pt_id to associate the new HWPT_NESTED object to the corresponding HWPT_PAGING object. The associating HWPT_PAGING object must be a nesting parent manually allocated via the same uAPI previously with an IOMMU_HWPT_ALLOC_NEST_PARENT flag, otherwise the allocation will fail. The
@@ -149,6 +203,18 @@ creating the objects and links:: created via the same IOMMU_HWPT_ALLOC uAPI. The difference is at the type of the object passed in via the @pt_id field of struct iommufd_hwpt_alloc. +5. IOMMUFD_OBJ_VIOMMU can be only manually created via the IOMMU_VIOMMU_ALLOC + uAPI, provided a dev_id (for the device's physical IOMMU to back the vIOMMU) + and an hwpt_id (to associate the vIOMMU to a nesting parent HWPT_PAGING). The + iommufd core will link the vIOMMU object to the struct iommu_device that the + struct device is behind. And an IOMMU driver can implement a viommu_alloc op + to allocate its own vIOMMU data structure embedding the core-level structure + iommufd_viommu and some driver-specific data. If necessary, the driver can + also configure its HW virtualization feature for that vIOMMU (and thus for + the VM). Successful completion of this operation sets up the linkages between + 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. + 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).
@@ -161,6 +227,7 @@ User visible objects are backed by following datastructures: - iommufd_device for IOMMUFD_OBJ_DEVICE. - iommufd_hwpt_paging for IOMMUFD_OBJ_HWPT_PAGING. - iommufd_hwpt_nested for IOMMUFD_OBJ_HWPT_NESTED. +- iommufd_viommu for IOMMUFD_OBJ_VIOMMU. Several terminologies when looking at these datastructures:
Signature · 1 line
2.43.0
RE: [PATCH v4 10/11] Documentation: userspace-api: iommufd: Update vIOMMU
Quoted 8 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:20 AMWith the introduction of the new object and its infrastructure, update the doc to reflect that and add a new graph.
Reviewed-by Jason Gunthorpe <[email protected]>Signed-off-by Nicolin Chen <[email protected]>
PATCH v4 11/11 iommu/arm-smmu-v3: Add IOMMU_VIOMMU_TYPE_ARM_SMMUV3 support
In reply to Nicolin ChenAdd a new driver-type for ARM SMMUv3 to enum iommu_viommu_type. Implement an arm_vsmmu_alloc() with its viommu op arm_vsmmu_domain_alloc_nested(), to replace arm_smmu_domain_alloc_nesting(). As an initial step, copy the VMID from s2_parent. A later cleanup series is required to move the VMID allocation out of the stage-2 domain allocation routine to this.
After that, replace nested_domain->s2_parent with nested_domain->vsmmu.
Note that the validatting conditions for a nested_domain allocation are moved from arm_vsmmu_domain_alloc_nested to arm_vsmmu_alloc, since there is no point in creating a vIOMMU (vsmmu) from the beginning if it would not support a nested_domain.
--- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 26 +++--- include/uapi/linux/iommufd.h | 2 + .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 79 ++++++++++++------- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +-- 4 files changed, 70 insertions(+), 46 deletions(-)
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h +14 -12
@@ -10,6 +10,7 @@#include <linux/bitfield.h> #include <linux/iommu.h> +#include <linux/iommufd.h> #include <linux/kernel.h> #include <linux/mmzone.h> #include <linux/sizes.h>
@@ -835,7 +836,7 @@ struct arm_smmu_domain {struct arm_smmu_nested_domain {struct iommu_domain domain; -struct arm_smmu_domain *s2_parent; +struct arm_vsmmu *vsmmu;__le64 ste[2 ]; };
@@ -1005,21 +1006,22 @@ tegra241_cmdqv_probe(struct arm_smmu_device *smmu) }#endif /* CONFIG_TEGRA241_CMDQV */ +struct arm_vsmmu { +struct iommufd_viommu core; +struct arm_smmu_device *smmu; +struct arm_smmu_domain *s2_parent; +u16 vmid; +}; +#if IS_ENABLED (CONFIG_ARM_SMMU_V3_IOMMUFD )void *arm_smmu_hw_info (struct device *dev,u32 *length,u32 *type); -struct iommu_domain * -arm_smmu_domain_alloc_nesting (struct device *dev,u32 flags, -struct iommu_domain *parent, -const struct iommu_user_data *user_data); +struct iommufd_viommu *arm_vsmmu_alloc (struct device *dev, +struct iommu_domain *parent, +struct iommufd_ctx *ictx, +unsigned int viommu_type);#else #define arm_smmu_hw_info NULL -static inline struct iommu_domain * -arm_smmu_domain_alloc_nesting (struct device *dev,u32 flags, -struct iommu_domain *parent, -const struct iommu_user_data *user_data) -{ -return ERR_PTR (-EOPNOTSUPP ); -} +#define arm_vsmmu_alloc NULL#endif /* CONFIG_ARM_SMMU_V3_IOMMUFD */ #endif /* _ARM_SMMU_V3_H */
include/uapi/linux/iommufd.h +2 -0
@@ -859,9 +859,11 @@ struct iommu_fault_alloc {/** * enum iommu_viommu_type - Virtual IOMMU Type * @IOMMU_VIOMMU_TYPE_DEFAULT: Reserved for future use +* @IOMMU_VIOMMU_TYPE_ARM_SMMUV3: ARM SMMUv3 driver specific type */ enum iommu_viommu_type {IOMMU_VIOMMU_TYPE_DEFAULT =0 , +IOMMU_VIOMMU_TYPE_ARM_SMMUV3 =1 , };/**
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3-iommufd.c +51 -28
117 lines not on this page · raw message
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -6
@@ -2661,7 +2661,7 @@ to_smmu_domain_devices(struct iommu_domain *domain) domain->type == IOMMU_DOMAIN_SVA)return to_smmu_domain (domain);if (domain->type ==IOMMU_DOMAIN_NESTED ) -return to_smmu_nested_domain (domain)->s2_parent; +return to_smmu_nested_domain (domain)->vsmmu->s2_parent;return NULL ; }
@@ -3126,13 +3126,9 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags,struct arm_smmu_domain *smmu_domain;int ret; -if (parent) -return arm_smmu_domain_alloc_nesting (dev, flags, parent, - user_data); -if (flags & ~PAGING_FLAGS )return ERR_PTR (-EOPNOTSUPP ); -if (user_data) +if (parent || user_data)return ERR_PTR (-EOPNOTSUPP ); smmu_domain =arm_smmu_domain_alloc ();
@@ -3541,6 +3537,7 @@ static struct iommu_ops arm_smmu_ops = { .dev_disable_feat = arm_smmu_dev_disable_feature, .page_response = arm_smmu_page_response, .def_domain_type = arm_smmu_def_domain_type, + .viommu_alloc = arm_vsmmu_alloc, .user_pasid_table = 1, .pgsize_bitmap = -1UL, /* Restricted during device attach */ .owner = THIS_MODULE,
Signature · 1 line
2.43.0
Tian, Kevin
maintainerRE: [PATCH v4 11/11] iommu/arm-smmu-v3: Add IOMMU_VIOMMU_TYPE_ARM_SMMUV3 support
Quoted 13 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:20 AMAdd a new driver-type for ARM SMMUv3 to enum
iommu_viommu_type. Implement
anarm_vsmmu_alloc()with its viommu oparm_vsmmu_domain_alloc_nested(),
to replacearm_smmu_domain_alloc_nesting(). As an initial step, copy the VMID froms2_parent. A later cleanup series is required to move the VMID allocation out of the stage-2 domain allocation routine to this.After that, replace
nested_domain->s2_parentwithnested_domain->vsmmu.
Quoted 7 lines
Note that the validatting conditions for a
nested_domainallocation are moved fromarm_vsmmu_domain_alloc_nestedtoarm_vsmmu_alloc, since there
is no point in creating a vIOMMU (vsmmu) from the beginning if it would not support anested_domain.Signed-off-by Nicolin Chen <[email protected]>
hmm I wonder whether this series should be merged with Jason's nesting series together and directly use vIOMMU to create nesting. Otherwise it looks a bit weird for one series to first enable a uAPI which is immediately replaced by another uAPI from the following series. Even if both are merged in one cycle, logically it doesn't sound clean when looking at the git history.
Jason Gunthorpe
maintainerRe: [PATCH v4 11/11] iommu/arm-smmu-v3: Add IOMMU_VIOMMU_TYPE_ARM_SMMUV3 support
Quoted 19 lines · Kevin
Quoted 19 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:20 AMAdd a new driver-type for ARM SMMUv3 to enum
iommu_viommu_type. Implement
anarm_vsmmu_alloc()with its viommu oparm_vsmmu_domain_alloc_nested(),
to replacearm_smmu_domain_alloc_nesting(). As an initial step, copy the VMID froms2_parent. A later cleanup series is required to move the VMID allocation out of the stage-2 domain allocation routine to this.After that, replace
nested_domain->s2_parentwithnested_domain->vsmmu.Note that the validatting conditions for a
nested_domainallocation are moved fromarm_vsmmu_domain_alloc_nestedtoarm_vsmmu_alloc, since there
is no point in creating a vIOMMU (vsmmu) from the beginning if it would not support anested_domain.
Quoted 7 lines
Quoted 1 line
Signed-off-by Nicolin Chen <[email protected]>hmm I wonder whether this series should be merged with Jason's nesting series together and directly use vIOMMU to create nesting. Otherwise it looks a bit weird for one series to first enable a uAPI which is immediately replaced by another uAPI from the following series.
It has changed from my original expectation, that's for sure. I've wondered the same thing.
For now I've been keeping them separate and was going to review when this is all settled down.
It is troublesome because of all the branches, but if we don't have a conflict we could take the whole lot through iommufd.
Jason
Tian, Kevin
maintainerQuoted 5 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMThis series introduces a new vIOMMU infrastructure and related ioctls.
Quoted 14 lines
IOMMUFD has been using the HWPT infrastructure for all cases, including a nested IO page table support. Yet, there're limitations for an HWPT-based structure to support some advanced HW-accelerated features, such as CMDQV
on NVIDIA Grace, and HW-accelerated vIOMMU on AMD. Even for a multi- IOMMU
environment, it is not straightforward for nested HWPTs to share the same parent HWPT (stage-2 IO pagetable), with the HWPT infrastructure alone: a parent HWPT typically hold one stage-2 IO pagetable and tag it with only one ID in the cache entries. When sharing one large stage-2 IO pagetable across physical IOMMU instances, that one ID may not always be available across all the IOMMU instances. In other word, it's ideal for SW to have a different container for the stage-2 IO pagetable so it can hold another ID that's available.
Just holding multiple IDs doesn't require a different container. This is just a side effect when vIOMMU will be required for other said reasons.
If we have to put more words here I'd prefer to adding a bit more for CMDQV which is more compelling. not a big deal though. 😊
Quoted 21 lines
For this "different container", add vIOMMU, an additional layer to hold extra virtualization information:
________________________________________________________________
_______| iommufd (with vIOMMU) | | | | [5] | | _____________ | | | | | | |----------------| vIOMMU | | | | | | | | | | | | | | [1] | | [4] [2] | | | ______ | | _____________ ________ | | | | | | [3] | | | | | | | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | | | |______| |_____________| |_____________| |________| | | | | | | | |
Quoted 9 lines
|______|________|______________|__________________|_____________
__|_____|
| | | | |
______v_____ | ______v_____ ______v_____ ___v__
| struct | | PFN | (paging) | | (nested) | |struct|
|iommu_device| |------>|iommu_domain|<----|iommu_domain|<----
|device|
|____________| storage|____________| |____________| |______|nit - [1] ... [5] can be removed.
Quoted 9 lines
The vIOMMU object should be seen as a slice of a physical IOMMU instance that is passed to or shared with a VM. That can be some HW/SW resources:
- Security namespace for guest owned ID, e.g. guest-controlled cache tags
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reporting
sorry no idea about 'non-affiliated event'. Can you elaborate?
Quoted 4 lines
On a multi-IOMMU system, the vIOMMU object must be instanced to the number
of the physical IOMMUs that are passed to (via devices) a guest VM, while
'to the number of the physical IOMMUs that have a slice passed to ..."
Quoted 116 lines
being able to hold the shareable parent HWPT. Each vIOMMU then just needs
to allocate its own individual ID to tag its own cache:---------------------------- ---------------- | | paging_hwpt0 | | hwpt_nested0 |--->| viommu0 ------------------ ---------------- | | IDx | ---------------------------- ---------------------------- ---------------- | | paging_hwpt0 | | hwpt_nested1 |--->| viommu1 ------------------ ---------------- | | IDy | ----------------------------As an initial part-1, add
IOMMUFD_CMD_VIOMMU_ALLOCioctl for an allocation
only. And implement it in arm-smmu-v3 driver as a real world use case.More vIOMMU-based structs and ioctls will be introduced in the follow-up series to support vDEVICE, vIRQ (vEVENT) and vQUEUE objects. Although we repurposed the vIOMMU object from an earlier RFC, just for a referece:
https://lore.kernel.org/all/[email protected]/This series is on Github:
https://github.com/nicolinc/iommufd/commits/iommufd_viommu_p1-v4
(paring QEMU branch for testing will be provided with the part2 series)Changelog
v4
* Added "Reviewed-by" from Jason
* DroppedIOMMU_VIOMMU_TYPE_DEFAULTsupport
* Droppediommufd_object_alloc_elmrenamings
* Renamed iommufd'sviommu_api.ctodriver.c
* Reworkediommufd_viommu_allochelper
* Added a separateiommufd_hwpt_nested_alloc_for_viommufunction forhwpt_nestedallocations on a vIOMMU, and added comparison betweenviommu->iommu_dev->opsanddev_iommu_ops(idev->dev)
* Replaceds2_parentwith vsmmu inarm_smmu_nested_domain
* Replaceddomain_alloc_useriniommu_opswithdomain_alloc_nestedinviommu_ops
* Replacedwait_queue_head_twith a completion, to delay the unplug ofmock_iommu_dev
* Corrected documentation graph that was missing structiommu_device
* Added aniommufd_verify_unfinalized_objecthelper to verify driver- allocated vIOMMU/vDEVICE objects
* Added missing test cases forTEST_LENGTHandfail_nth
v3
https://lore.kernel.org/all/[email protected]/
* Rebased on top of Jason's nesting v3 series
https://lore.kernel.org/all/0-v3-e2e16cd7467f+2a6a1-
[email protected]/
* Split the series into smaller parts
* Added Jason's Reviewed-by
* Added backviommu->iommu_dev
* Added support for driver-allocated vIOMMU v.s. core-allocated
* Droppedarm_smmu_cache_invalidate_user
* Added aniommufd_test_wait_for_users()in selftest
* Reworked test code to make viommu an individual FIXTURE
* Added missingTEST_LENGTHcase for the new ioctl command
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!
NicolinNicolin Chen (11):
iommufd: Move structiommufd_objectto public iommufd header
iommufd: IntroduceIOMMUFD_OBJ_VIOMMUand its related struct
iommufd: Addiommufd_verify_unfinalized_objectiommufd/viommu: AddIOMMU_VIOMMU_ALLOCioctl
iommufd: Adddomain_alloc_nestedop toiommufd_viommu_ops
iommufd: Allowpt_idto carryviommu_idforIOMMU_HWPT_ALLOCiommufd/selftest: Add refcount tomock_iommu_deviceiommufd/selftest: AddIOMMU_VIOMMU_TYPE_SELFTESTiommufd/selftest: AddIOMMU_VIOMMU_ALLOCtest coverage
Documentation: userspace-api: iommufd: Update vIOMMU iommu/arm-smmu-v3: AddIOMMU_VIOMMU_TYPE_ARM_SMMUV3
supportdrivers/iommu/iommufd/Makefile | 5 +- drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.h | 26 +++--- drivers/iommu/iommufd/iommufd_private.h | 36 ++------ drivers/iommu/iommufd/iommufd_test.h | 2 + include/linux/iommu.h | 14 +++ include/linux/iommufd.h | 89 +++++++++++++++++++ include/uapi/linux/iommufd.h | 56 ++++++++++-- tools/testing/selftests/iommu/iommufd_utils.h | 28 ++++++ .../arm/arm-smmu-v3/arm-smmu-v3-iommufd.c | 79 ++++++++++------ drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c | 9 +- drivers/iommu/iommufd/driver.c | 38 ++++++++ drivers/iommu/iommufd/hw_pagetable.c | 69 +++++++++++++- drivers/iommu/iommufd/main.c | 58 ++++++------ drivers/iommu/iommufd/selftest.c | 73 +++++++++++++-- drivers/iommu/iommufd/viommu.c | 85 ++++++++++++++++++ tools/testing/selftests/iommu/iommufd.c | 78 ++++++++++++++++ .../selftests/iommu/iommufd_fail_nth.c | 11 +++ Documentation/userspace-api/iommufd.rst | 69 +++++++++++++- 18 files changed, 701 insertions(+), 124 deletions(-) create mode 100644 drivers/iommu/iommufd/driver.c create mode 100644 drivers/iommu/iommufd/viommu.c--
2.43.0
Jason Gunthorpe
maintainerQuoted 11 lines · Kevin
Quoted 9 lines
The vIOMMU object should be seen as a slice of a physical IOMMU instance that is passed to or shared with a VM. That can be some HW/SW resources:
- Security namespace for guest owned ID, e.g. guest-controlled cache tags
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reportingsorry no idea about 'non-affiliated event'. Can you elaborate?
This would be an even that is not a connected to a device
For instance a CMDQ experienced a problem.
Jason
Tian, Kevin
maintainerQuoted 17 lines
From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 25, 2024 11:43 PMQuoted 1 line · Kevin
Quoted 1 line
The vIOMMU object should be seen as a slice of a physical IOMMU
instance
Quoted 1 line
Quoted 1 line
that is passed to or shared with a VM. That can be some HW/SW
resources:
Quoted 1 line
Quoted 1 line
- Security namespace for guest owned ID, e.g. guest-controlled cache
tags
Quoted 7 lines
Quoted 6 lines
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reporting
Quoted 6 lines
Quoted 1 line
sorry no idea about 'non-affiliated event'. Can you elaborate?
This would be an even that is not a connected to a device
For instance a CMDQ experienced a problem.
Okay, then 'non-device-affiliated' is probably clearer.
Quoted 20 lines · Kevin
Quoted 5 lines
From: Nicolin Chen <[email protected]>
Sent: Tuesday, October 22, 2024 8:19 AMThis series introduces a new vIOMMU infrastructure and related ioctls.
Quoted 14 lines
IOMMUFD has been using the HWPT infrastructure for all cases, including a nested IO page table support. Yet, there're limitations for an HWPT-based structure to support some advanced HW-accelerated features, such as CMDQV
on NVIDIA Grace, and HW-accelerated vIOMMU on AMD. Even for a multi- IOMMU
environment, it is not straightforward for nested HWPTs to share the same parent HWPT (stage-2 IO pagetable), with the HWPT infrastructure alone: a parent HWPT typically hold one stage-2 IO pagetable and tag it with only one ID in the cache entries. When sharing one large stage-2 IO pagetable across physical IOMMU instances, that one ID may not always be available across all the IOMMU instances. In other word, it's ideal for SW to have a different container for the stage-2 IO pagetable so it can hold another ID that's available.
Quoted 5 lines
Just holding multiple IDs doesn't require a different container. This is just a side effect when vIOMMU will be required for other said reasons.
If we have to put more words here I'd prefer to adding a bit more for CMDQV which is more compelling. not a big deal though. 😊
Ack.
Quoted 30 lines
Quoted 20 lines
For this "different container", add vIOMMU, an additional layer to hold extra virtualization information:
________________________________________________________________
_______| iommufd (with vIOMMU) | | | | [5] | | _____________ | | | | | | |----------------| vIOMMU | | | | | | | | | | | | | | [1] | | [4] [2] | | | ______ | | _____________ ________ | | | | | | [3] | | | | | | | | | IOAS |<---|(HWPT_PAGING)|<---| HWPT_NESTED |<--| DEVICE | | | | |______| |_____________| |_____________| |________| | | | | | | | |Quoted 9 lines
|______|________|______________|__________________|_____________ __|_____| | | | | | ______v_____ | ______v_____ ______v_____ ___v__ | struct | | PFN | (paging) | | (nested) | |struct| |iommu_device| |------>|iommu_domain|<----|iommu_domain|<---- |device| |____________| storage|____________| |____________| |______|
Quoted 1 line
nit - [1] ... [5] can be removed.
They are copied from the Documentation where numbers are needed. I will take all the numbers out in the cover-letters.
Quoted 11 lines
Quoted 9 lines
The vIOMMU object should be seen as a slice of a physical IOMMU instance that is passed to or shared with a VM. That can be some HW/SW resources:
- Security namespace for guest owned ID, e.g. guest-controlled cache tags
- Access to a sharable nesting parent pagetable across physical IOMMUs
- Virtualization of various platforms IDs, e.g. RIDs and others
- Delivery of paravirtualized invalidation
- Direct assigned invalidation queues
- Direct assigned interrupts
- Non-affiliated event reportingsorry no idea about 'non-affiliated event'. Can you elaborate?
I'll put an "e.g.".
Quoted 5 lines
Quoted 3 lines
On a multi-IOMMU system, the vIOMMU object must be instanced to the number
of the physical IOMMUs that are passed to (via devices) a guest VM, while'to the number of the physical IOMMUs that have a slice passed to ..."
Ack.
Thanks
Nicolin