* [PATCH v2 1/7] iommufd: Assert devices_lock for iommufd_hw_pagetable_has_group()
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-22 21:02 ` [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts Jason Gunthorpe
` (5 subsequent siblings)
6 siblings, 0 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
The hwpt->devices list is locked by this, make it clearer.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index a0c66f47a65ada..dcfaf6567420e0 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -186,6 +186,8 @@ static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt,
{
struct iommufd_device *cur_dev;
+ lockdep_assert_held(&hwpt->devices_lock);
+
list_for_each_entry(cur_dev, &hwpt->devices, devices_item)
if (cur_dev->group == group)
return true;
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
2023-02-22 21:02 ` [PATCH v2 1/7] iommufd: Assert devices_lock for iommufd_hw_pagetable_has_group() Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-24 6:12 ` Tian, Kevin
2023-02-22 21:02 ` [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy() Jason Gunthorpe
` (4 subsequent siblings)
6 siblings, 1 reply; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
A later patch will require this locking - currently under the ioas mutex
the hwpt can not have a 0 reference and be on the list.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index dcfaf6567420e0..0a80ff7b2e0d79 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -282,7 +282,10 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
if (!hwpt->auto_domain)
continue;
+ if (!iommufd_lock_obj(&hwpt->obj))
+ continue;
rc = iommufd_device_do_attach(idev, hwpt);
+ iommufd_put_object(&hwpt->obj);
/*
* -EINVAL means the domain is incompatible with the device.
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts
2023-02-22 21:02 ` [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts Jason Gunthorpe
@ 2023-02-24 6:12 ` Tian, Kevin
0 siblings, 0 replies; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 6:12 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> A later patch will require this locking - currently under the ioas mutex
> the hwpt can not have a 0 reference and be on the list.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy()
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
2023-02-22 21:02 ` [PATCH v2 1/7] iommufd: Assert devices_lock for iommufd_hw_pagetable_has_group() Jason Gunthorpe
2023-02-22 21:02 ` [PATCH v2 2/7] iommufd: Add iommufd_lock_obj() around the auto-domains hwpts Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-24 6:25 ` Tian, Kevin
2023-02-22 21:02 ` [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h Jason Gunthorpe
` (3 subsequent siblings)
6 siblings, 1 reply; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
A HWPT is permanently associated with an IOAS when it is created, remove
the strange situation where a refcount != 0 HWPT can have been
disconnected from the IOAS by putting all the IOAS related destruction in
the object destroy function.
Initializing a HWPT is two stages, we have to allocate it, attach it to a
device and then populate the domain. Once the domain is populated it is
fully linked to the IOAS.
Arrange things so that all the error unwinds flow through the
iommufd_hw_pagetable_destroy() and allow it to handle all cases.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 20 +++++++-------------
drivers/iommu/iommufd/hw_pagetable.c | 27 +++++++++++++++++++--------
2 files changed, 26 insertions(+), 21 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 0a80ff7b2e0d79..6787a0d8d6e9f0 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -243,6 +243,7 @@ static int iommufd_device_do_attach(struct iommufd_device *idev,
hwpt->domain);
if (rc)
goto out_detach;
+ list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
}
}
@@ -307,7 +308,6 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
rc = iommufd_device_do_attach(idev, hwpt);
if (rc)
goto out_abort;
- list_add_tail(&hwpt->hwpt_item, &ioas->hwpt_list);
mutex_unlock(&ioas->mutex);
iommufd_object_finalize(idev->ictx, &hwpt->obj);
@@ -386,28 +386,19 @@ void iommufd_device_detach(struct iommufd_device *idev)
{
struct iommufd_hw_pagetable *hwpt = idev->hwpt;
- mutex_lock(&hwpt->ioas->mutex);
mutex_lock(&hwpt->devices_lock);
list_del(&idev->devices_item);
- if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) {
- if (list_empty(&hwpt->devices)) {
- iopt_table_remove_domain(&hwpt->ioas->iopt,
- hwpt->domain);
- list_del(&hwpt->hwpt_item);
- }
+ if (!iommufd_hw_pagetable_has_group(hwpt, idev->group))
iommu_detach_group(hwpt->domain, idev->group);
- }
iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
+ idev->hwpt = NULL;
mutex_unlock(&hwpt->devices_lock);
- mutex_unlock(&hwpt->ioas->mutex);
if (hwpt->auto_domain)
iommufd_object_destroy_user(idev->ictx, &hwpt->obj);
else
refcount_dec(&hwpt->obj.users);
- idev->hwpt = NULL;
-
refcount_dec(&idev->obj.users);
}
EXPORT_SYMBOL_NS_GPL(iommufd_device_detach, IOMMUFD);
@@ -753,6 +744,10 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
if (rc)
goto out_hwpt;
+ mutex_lock(&ioas->mutex);
+ list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
+ mutex_unlock(&ioas->mutex);
+
refcount_inc(&hwpt->obj.users);
iommufd_object_finalize(ictx, &hwpt->obj);
return hwpt;
@@ -765,7 +760,6 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
struct iommufd_hw_pagetable *hwpt)
{
- iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
refcount_dec(&hwpt->obj.users);
}
#endif
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index 43d473989a0667..10db1359c067c1 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -13,7 +13,17 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj)
WARN_ON(!list_empty(&hwpt->devices));
- iommu_domain_free(hwpt->domain);
+ if (!list_empty(&hwpt->hwpt_item)) {
+ mutex_lock(&hwpt->ioas->mutex);
+ list_del(&hwpt->hwpt_item);
+ mutex_unlock(&hwpt->ioas->mutex);
+
+ iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
+ }
+
+ if (hwpt->domain)
+ iommu_domain_free(hwpt->domain);
+
refcount_dec(&hwpt->ioas->obj.users);
mutex_destroy(&hwpt->devices_lock);
}
@@ -37,21 +47,22 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
if (IS_ERR(hwpt))
return hwpt;
- hwpt->domain = iommu_domain_alloc(dev->bus);
- if (!hwpt->domain) {
- rc = -ENOMEM;
- goto out_abort;
- }
-
INIT_LIST_HEAD(&hwpt->devices);
INIT_LIST_HEAD(&hwpt->hwpt_item);
mutex_init(&hwpt->devices_lock);
/* Pairs with iommufd_hw_pagetable_destroy() */
refcount_inc(&ioas->obj.users);
hwpt->ioas = ioas;
+
+ hwpt->domain = iommu_domain_alloc(dev->bus);
+ if (!hwpt->domain) {
+ rc = -ENOMEM;
+ goto out_abort;
+ }
+
return hwpt;
out_abort:
- iommufd_object_abort(ictx, &hwpt->obj);
+ iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
return ERR_PTR(rc);
}
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy()
2023-02-22 21:02 ` [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy() Jason Gunthorpe
@ 2023-02-24 6:25 ` Tian, Kevin
0 siblings, 0 replies; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 6:25 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
> @@ -753,6 +744,10 @@ iommufd_device_selftest_attach(struct
> iommufd_ctx *ictx,
> if (rc)
> goto out_hwpt;
>
> + mutex_lock(&ioas->mutex);
> + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
> + mutex_unlock(&ioas->mutex);
> +
> refcount_inc(&hwpt->obj.users);
> iommufd_object_finalize(ictx, &hwpt->obj);
> return hwpt;
> @@ -765,7 +760,6 @@ iommufd_device_selftest_attach(struct iommufd_ctx
> *ictx,
> void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
> struct iommufd_hw_pagetable *hwpt)
> {
> - iopt_table_remove_domain(&hwpt->ioas->iopt, hwpt->domain);
> refcount_dec(&hwpt->obj.users);
> }
> #endif
It will read clearer if first having a patch to add list_add_tail() to selftest
attach and list_del() in selftest detach and then having this patch to
move list_del()+iopt_table_remove_domain() to HWPT destruction.
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
` (2 preceding siblings ...)
2023-02-22 21:02 ` [PATCH v2 3/7] iommufd: Move ioas related HWPT destruction into iommufd_hw_pagetable_destroy() Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-24 6:27 ` Tian, Kevin
2023-02-22 21:02 ` [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain() Jason Gunthorpe
` (2 subsequent siblings)
6 siblings, 1 reply; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
hw_pagetable.c will need this in the next patches.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 17 -----------------
drivers/iommu/iommufd/iommufd_private.h | 18 ++++++++++++++++++
2 files changed, 18 insertions(+), 17 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 6787a0d8d6e9f0..4483c06e0ec38b 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -15,23 +15,6 @@ MODULE_PARM_DESC(
"Allow IOMMUFD to bind to devices even if the platform cannot isolate "
"the MSI interrupt window. Enabling this is a security weakness.");
-/*
- * A iommufd_device object represents the binding relationship between a
- * consuming driver and the iommufd. These objects are created/destroyed by
- * external drivers, not by userspace.
- */
-struct iommufd_device {
- struct iommufd_object obj;
- struct iommufd_ctx *ictx;
- struct iommufd_hw_pagetable *hwpt;
- /* Head at iommufd_hw_pagetable::devices */
- struct list_head devices_item;
- /* always the physical device */
- struct device *dev;
- struct iommu_group *group;
- bool enforce_cache_coherency;
-};
-
void iommufd_device_destroy(struct iommufd_object *obj)
{
struct iommufd_device *idev =
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 9d7f71510ca1bc..84fe19a195466b 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -12,6 +12,7 @@
struct iommu_domain;
struct iommu_group;
struct iommu_option;
+struct iommufd_device;
struct iommufd_ctx {
struct file *file;
@@ -257,6 +258,23 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
struct device *dev);
void iommufd_hw_pagetable_destroy(struct iommufd_object *obj);
+/*
+ * A iommufd_device object represents the binding relationship between a
+ * consuming driver and the iommufd. These objects are created/destroyed by
+ * external drivers, not by userspace.
+ */
+struct iommufd_device {
+ struct iommufd_object obj;
+ struct iommufd_ctx *ictx;
+ struct iommufd_hw_pagetable *hwpt;
+ /* Head at iommufd_hw_pagetable::devices */
+ struct list_head devices_item;
+ /* always the physical device */
+ struct device *dev;
+ struct iommu_group *group;
+ bool enforce_cache_coherency;
+};
+
void iommufd_device_destroy(struct iommufd_object *obj);
struct iommufd_access {
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h
2023-02-22 21:02 ` [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h Jason Gunthorpe
@ 2023-02-24 6:27 ` Tian, Kevin
0 siblings, 0 replies; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 6:27 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> hw_pagetable.c will need this in the next patches.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain()
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
` (3 preceding siblings ...)
2023-02-22 21:02 ` [PATCH v2 4/7] iommufd: Move iommufd_device to iommufd_private.h Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-23 9:03 ` Tian, Kevin
2023-02-24 6:35 ` Tian, Kevin
2023-02-22 21:02 ` [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device Jason Gunthorpe
2023-02-22 21:02 ` [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument Jason Gunthorpe
6 siblings, 2 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
The HWPT is always linked to an IOAS and once a HWPT exists its domain
should be fully mapped. This ended up being split up into device.c during
a two phase creation that was a bit confusing.
Move the iopt_table_add_domain() into iommufd_hw_pagetable_alloc() by
having it call back to device.c to complete the domain attach in the
required order.
Calling iommufd_hw_pagetable_alloc() with immediate_attach = false will
work on most drivers, but notably the SMMU drivers will fail because they
can't decide what kind of domain to create until they are attached. This
will be fixed when the domain_alloc function can take in a struct device.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 88 +++++++++++--------------
drivers/iommu/iommufd/hw_pagetable.c | 47 +++++++++++--
drivers/iommu/iommufd/iommufd_private.h | 6 +-
3 files changed, 88 insertions(+), 53 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 4483c06e0ec38b..cf2517a6472ff8 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -177,13 +177,16 @@ static bool iommufd_hw_pagetable_has_group(struct iommufd_hw_pagetable *hwpt,
return false;
}
-static int iommufd_device_do_attach(struct iommufd_device *idev,
- struct iommufd_hw_pagetable *hwpt)
+int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
+ struct iommufd_device *idev)
{
phys_addr_t sw_msi_start = PHYS_ADDR_MAX;
int rc;
- mutex_lock(&hwpt->devices_lock);
+ lockdep_assert_held(&hwpt->devices_lock);
+
+ if (WARN_ON(idev->hwpt))
+ return -EINVAL;
/*
* Try to upgrade the domain we have, it is an iommu driver bug to
@@ -198,19 +201,18 @@ static int iommufd_device_do_attach(struct iommufd_device *idev,
hwpt->domain);
if (!hwpt->enforce_cache_coherency) {
WARN_ON(list_empty(&hwpt->devices));
- rc = -EINVAL;
- goto out_unlock;
+ return -EINVAL;
}
}
rc = iopt_table_enforce_group_resv_regions(&hwpt->ioas->iopt, idev->dev,
idev->group, &sw_msi_start);
if (rc)
- goto out_unlock;
+ return rc;
rc = iommufd_device_setup_msi(idev, hwpt, sw_msi_start);
if (rc)
- goto out_iova;
+ goto err_unresv;
/*
* FIXME: Hack around missing a device-centric iommu api, only attach to
@@ -219,27 +221,36 @@ static int iommufd_device_do_attach(struct iommufd_device *idev,
if (!iommufd_hw_pagetable_has_group(hwpt, idev->group)) {
rc = iommu_attach_group(hwpt->domain, idev->group);
if (rc)
- goto out_iova;
-
- if (list_empty(&hwpt->devices)) {
- rc = iopt_table_add_domain(&hwpt->ioas->iopt,
- hwpt->domain);
- if (rc)
- goto out_detach;
- list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
- }
+ goto err_unresv;
}
+ return 0;
+err_unresv:
+ iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
+ return rc;
+}
+
+void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt,
+ struct iommufd_device *idev)
+{
+ if (!iommufd_hw_pagetable_has_group(hwpt, idev->group))
+ iommu_detach_group(hwpt->domain, idev->group);
+ iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
+}
+
+static int iommufd_device_do_attach(struct iommufd_device *idev,
+ struct iommufd_hw_pagetable *hwpt)
+{
+ int rc;
+
+ mutex_lock(&hwpt->devices_lock);
+ rc = iommufd_hw_pagetable_attach(hwpt, idev);
+ if (rc)
+ goto out_unlock;
idev->hwpt = hwpt;
refcount_inc(&hwpt->obj.users);
+ /* hwpt->devices is all domains that have been attached */
list_add(&idev->devices_item, &hwpt->devices);
- mutex_unlock(&hwpt->devices_lock);
- return 0;
-
-out_detach:
- iommu_detach_group(hwpt->domain, idev->group);
-out_iova:
- iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
out_unlock:
mutex_unlock(&hwpt->devices_lock);
return rc;
@@ -281,23 +292,16 @@ static int iommufd_device_auto_get_domain(struct iommufd_device *idev,
goto out_unlock;
}
- hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev->dev);
+ hwpt = iommufd_hw_pagetable_alloc(idev->ictx, ioas, idev, true);
if (IS_ERR(hwpt)) {
rc = PTR_ERR(hwpt);
goto out_unlock;
}
hwpt->auto_domain = true;
- rc = iommufd_device_do_attach(idev, hwpt);
- if (rc)
- goto out_abort;
-
mutex_unlock(&ioas->mutex);
iommufd_object_finalize(idev->ictx, &hwpt->obj);
return 0;
-
-out_abort:
- iommufd_object_abort_and_destroy(idev->ictx, &hwpt->obj);
out_unlock:
mutex_unlock(&ioas->mutex);
return rc;
@@ -371,10 +375,8 @@ void iommufd_device_detach(struct iommufd_device *idev)
mutex_lock(&hwpt->devices_lock);
list_del(&idev->devices_item);
- if (!iommufd_hw_pagetable_has_group(hwpt, idev->group))
- iommu_detach_group(hwpt->domain, idev->group);
- iopt_remove_reserved_iova(&hwpt->ioas->iopt, idev->dev);
idev->hwpt = NULL;
+ iommufd_hw_pagetable_detach(hwpt, idev);
mutex_unlock(&hwpt->devices_lock);
if (hwpt->auto_domain)
@@ -716,28 +718,18 @@ iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
struct iommufd_ioas *ioas,
struct device *mock_dev)
{
+ struct iommufd_device tmp_idev = { .dev = mock_dev };
struct iommufd_hw_pagetable *hwpt;
- int rc;
-
- hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, mock_dev);
- if (IS_ERR(hwpt))
- return hwpt;
-
- rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain);
- if (rc)
- goto out_hwpt;
mutex_lock(&ioas->mutex);
- list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
+ hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, &tmp_idev, false);
mutex_unlock(&ioas->mutex);
+ if (IS_ERR(hwpt))
+ return hwpt;
refcount_inc(&hwpt->obj.users);
iommufd_object_finalize(ictx, &hwpt->obj);
return hwpt;
-
-out_hwpt:
- iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
- return ERR_PTR(rc);
}
void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index 10db1359c067c1..da1df54877f43e 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -32,17 +32,22 @@ void iommufd_hw_pagetable_destroy(struct iommufd_object *obj)
* iommufd_hw_pagetable_alloc() - Get an iommu_domain for a device
* @ictx: iommufd context
* @ioas: IOAS to associate the domain with
- * @dev: Device to get an iommu_domain for
+ * @idev: Device to get an iommu_domain for
+ * @immediate_attach: True if idev should be attached to the hwpt
*
- * Allocate a new iommu_domain and return it as a hw_pagetable.
+ * Allocate a new iommu_domain and return it as a hw_pagetable. The HWPT
+ * will be linked to the given ioas and upon return the underlying iommu_domain
+ * is fully popoulated.
*/
struct iommufd_hw_pagetable *
iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
- struct device *dev)
+ struct iommufd_device *idev, bool immediate_attach)
{
struct iommufd_hw_pagetable *hwpt;
int rc;
+ lockdep_assert_held(&ioas->mutex);
+
hwpt = iommufd_object_alloc(ictx, hwpt, IOMMUFD_OBJ_HW_PAGETABLE);
if (IS_ERR(hwpt))
return hwpt;
@@ -54,14 +59,48 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
refcount_inc(&ioas->obj.users);
hwpt->ioas = ioas;
- hwpt->domain = iommu_domain_alloc(dev->bus);
+ hwpt->domain = iommu_domain_alloc(idev->dev->bus);
if (!hwpt->domain) {
rc = -ENOMEM;
goto out_abort;
}
+ mutex_lock(&hwpt->devices_lock);
+
+ /*
+ * immediate_attach exists only to accommodate iommu drivers that cannot
+ * directly allocate a domain. These drivers do not finish creating the
+ * domain until attach is completed. Thus we must have this call
+ * sequence. Once those drivers are fixed this should be removed.
+ */
+ if (immediate_attach) {
+ rc = iommufd_hw_pagetable_attach(hwpt, idev);
+ if (rc)
+ goto out_unlock;
+ }
+
+ rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain);
+ if (rc)
+ goto out_detach;
+
+ /* ioas->hwpt_list is hwpts after iopt_table_add_domain() */
+ list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
+
+ if (immediate_attach) {
+ /* See iommufd_device_do_attach() */
+ refcount_inc(&hwpt->obj.users);
+ idev->hwpt = hwpt;
+ list_add(&idev->devices_item, &hwpt->devices);
+ }
+
+ mutex_unlock(&hwpt->devices_lock);
return hwpt;
+out_detach:
+ if (immediate_attach)
+ iommufd_hw_pagetable_detach(hwpt, idev);
+out_unlock:
+ mutex_unlock(&hwpt->devices_lock);
out_abort:
iommufd_object_abort_and_destroy(ictx, &hwpt->obj);
return ERR_PTR(rc);
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 84fe19a195466b..331664e917b771 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -255,7 +255,11 @@ struct iommufd_hw_pagetable {
struct iommufd_hw_pagetable *
iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
- struct device *dev);
+ struct iommufd_device *idev, bool immediate_attach);
+int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
+ struct iommufd_device *idev);
+void iommufd_hw_pagetable_detach(struct iommufd_hw_pagetable *hwpt,
+ struct iommufd_device *idev);
void iommufd_hw_pagetable_destroy(struct iommufd_object *obj);
/*
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain()
2023-02-22 21:02 ` [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain() Jason Gunthorpe
@ 2023-02-23 9:03 ` Tian, Kevin
2023-02-23 13:07 ` Jason Gunthorpe
2023-02-24 6:35 ` Tian, Kevin
1 sibling, 1 reply; 19+ messages in thread
From: Tian, Kevin @ 2023-02-23 9:03 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> The HWPT is always linked to an IOAS and once a HWPT exists its domain
> should be fully mapped. This ended up being split up into device.c during
> a two phase creation that was a bit confusing.
>
> Move the iopt_table_add_domain() into iommufd_hw_pagetable_alloc() by
> having it call back to device.c to complete the domain attach in the
> required order.
>
> Calling iommufd_hw_pagetable_alloc() with immediate_attach = false will
> work on most drivers, but notably the SMMU drivers will fail because they
> can't decide what kind of domain to create until they are attached. This
> will be fixed when the domain_alloc function can take in a struct device.
>
Is below understanding correct on how to retire immediate_attach?
1) immediate_attach=true in auto domain path for back compat (what
this patch does);
2) immediate_attach=false when adding hwpt_alloc() uAPI and VT-d
support;
3) fix domain_alloc() to take struct device so the SMMU drivers can
decide the domain info w/o relying on attach;
4) remove immediate_attach and enable hwpt_alloc() on SMMU.
Of course 3) doesn't need to wait. If it happens before 2) is merged
then 2/4) can come together.
^ permalink raw reply [flat|nested] 19+ messages in thread
* Re: [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain()
2023-02-23 9:03 ` Tian, Kevin
@ 2023-02-23 13:07 ` Jason Gunthorpe
0 siblings, 0 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-23 13:07 UTC (permalink / raw)
To: Tian, Kevin
Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org,
Shuah Khan, Nicolin Chen, Liu, Yi L
On Thu, Feb 23, 2023 at 09:03:14AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Thursday, February 23, 2023 5:03 AM
> >
> > The HWPT is always linked to an IOAS and once a HWPT exists its domain
> > should be fully mapped. This ended up being split up into device.c during
> > a two phase creation that was a bit confusing.
> >
> > Move the iopt_table_add_domain() into iommufd_hw_pagetable_alloc() by
> > having it call back to device.c to complete the domain attach in the
> > required order.
> >
> > Calling iommufd_hw_pagetable_alloc() with immediate_attach = false will
> > work on most drivers, but notably the SMMU drivers will fail because they
> > can't decide what kind of domain to create until they are attached. This
> > will be fixed when the domain_alloc function can take in a struct device.
> >
>
> Is below understanding correct on how to retire immediate_attach?
>
> 1) immediate_attach=true in auto domain path for back compat (what
> this patch does);
yes
> 2) immediate_attach=false when adding hwpt_alloc() uAPI and VT-d
> support;
yes
> 3) fix domain_alloc() to take struct device so the SMMU drivers can
> decide the domain info w/o relying on attach;
Yes, this is where Robin's patches are going.
> 4) remove immediate_attach and enable hwpt_alloc() on SMMU.
Sort of, the domain_alloc_user already takes in the device so SMMU can
be fixed for that API immediately, in theory, but it looks like some
amount of work to get there.
Jason
^ permalink raw reply [flat|nested] 19+ messages in thread
* RE: [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain()
2023-02-22 21:02 ` [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain() Jason Gunthorpe
2023-02-23 9:03 ` Tian, Kevin
@ 2023-02-24 6:35 ` Tian, Kevin
2023-02-24 17:02 ` Jason Gunthorpe
1 sibling, 1 reply; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 6:35 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> +static int iommufd_device_do_attach(struct iommufd_device *idev,
> + struct iommufd_hw_pagetable *hwpt)
> +{
> + int rc;
> +
> + mutex_lock(&hwpt->devices_lock);
> + rc = iommufd_hw_pagetable_attach(hwpt, idev);
> + if (rc)
> + goto out_unlock;
>
> idev->hwpt = hwpt;
> refcount_inc(&hwpt->obj.users);
> + /* hwpt->devices is all domains that have been attached */
> list_add(&idev->devices_item, &hwpt->devices);
s/domains/devices/
but I didn't see what additional info this comment actually
give in this place. It's there in a function name xxx_attach then
certainly every device in that list has been attached.
> +
> + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain);
> + if (rc)
> + goto out_detach;
> +
> + /* ioas->hwpt_list is hwpts after iopt_table_add_domain() */
> + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
again the comment is meaningless
otherwise looks good to me,
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain()
2023-02-24 6:35 ` Tian, Kevin
@ 2023-02-24 17:02 ` Jason Gunthorpe
0 siblings, 0 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-24 17:02 UTC (permalink / raw)
To: Tian, Kevin
Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org,
Shuah Khan, Nicolin Chen, Liu, Yi L
On Fri, Feb 24, 2023 at 06:35:22AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Thursday, February 23, 2023 5:03 AM
> >
> > +static int iommufd_device_do_attach(struct iommufd_device *idev,
> > + struct iommufd_hw_pagetable *hwpt)
> > +{
> > + int rc;
> > +
> > + mutex_lock(&hwpt->devices_lock);
> > + rc = iommufd_hw_pagetable_attach(hwpt, idev);
> > + if (rc)
> > + goto out_unlock;
> >
> > idev->hwpt = hwpt;
> > refcount_inc(&hwpt->obj.users);
> > + /* hwpt->devices is all domains that have been attached */
> > list_add(&idev->devices_item, &hwpt->devices);
>
> s/domains/devices/
>
> but I didn't see what additional info this comment actually
> give in this place. It's there in a function name xxx_attach then
> certainly every device in that list has been attached.
>
> > +
> > + rc = iopt_table_add_domain(&hwpt->ioas->iopt, hwpt->domain);
> > + if (rc)
> > + goto out_detach;
> > +
> > + /* ioas->hwpt_list is hwpts after iopt_table_add_domain() */
> > + list_add_tail(&hwpt->hwpt_item, &hwpt->ioas->hwpt_list);
>
> again the comment is meaningless
Okay I just deleted them
Thanks,
Jason
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
` (4 preceding siblings ...)
2023-02-22 21:02 ` [PATCH v2 5/7] iommufd: Make iommufd_hw_pagetable_alloc() do iopt_table_add_domain() Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-24 6:50 ` Tian, Kevin
2023-02-22 21:02 ` [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument Jason Gunthorpe
6 siblings, 1 reply; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
iommufd wants to use more infrastructure, like the iommu_group, that the
mock device does not support. Create a more complete mock device that can
go through the whole cycle of ownership, blocking domain, and has an
iommu_group.
This requires creating a real struct device on a real bus to be able to
connect it to a iommu_group. Unfortunately we cannot formally attach the
mock iommu driver as an actual driver as the iommu core does not allow
more than one driver or provide a general way for busses to link to
iommus. This can be solved with a little hack to open code the dev_iommus
struct.
With this infrastructure things work exactly the same as the normal domain
path, including the auto domains mechanism and direct attach of hwpts. As
the created hwpt is now an autodomain it is no longer required to destroy
it and trying to do so will trigger a failure.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
drivers/iommu/iommufd/device.c | 40 +---
drivers/iommu/iommufd/iommufd_private.h | 11 +-
drivers/iommu/iommufd/selftest.c | 196 +++++++++++++++---
tools/testing/selftests/iommu/iommufd.c | 2 -
.../selftests/iommu/iommufd_fail_nth.c | 8 -
5 files changed, 181 insertions(+), 76 deletions(-)
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index cf2517a6472ff8..41cd65c758d3bc 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -22,7 +22,8 @@ void iommufd_device_destroy(struct iommufd_object *obj)
iommu_device_release_dma_owner(idev->dev);
iommu_group_put(idev->group);
- iommufd_ctx_put(idev->ictx);
+ if (!iommufd_selftest_is_mock_dev(idev->dev))
+ iommufd_ctx_put(idev->ictx);
}
/**
@@ -69,7 +70,8 @@ struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx,
goto out_release_owner;
}
idev->ictx = ictx;
- iommufd_ctx_get(ictx);
+ if (!iommufd_selftest_is_mock_dev(dev))
+ iommufd_ctx_get(ictx);
idev->dev = dev;
idev->enforce_cache_coherency =
device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY);
@@ -151,7 +153,8 @@ static int iommufd_device_setup_msi(struct iommufd_device *idev,
* operation from the device (eg a simple DMA) cannot trigger an
* interrupt outside this iommufd context.
*/
- if (!iommu_group_has_isolated_msi(idev->group)) {
+ if (!iommufd_selftest_is_mock_dev(idev->dev) &&
+ !iommu_group_has_isolated_msi(idev->group)) {
if (!allow_unsafe_interrupts)
return -EPERM;
@@ -707,34 +710,3 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
return rc;
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
-
-#ifdef CONFIG_IOMMUFD_TEST
-/*
- * Creating a real iommufd_device is too hard, bypass creating a iommufd_device
- * and go directly to attaching a domain.
- */
-struct iommufd_hw_pagetable *
-iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
- struct iommufd_ioas *ioas,
- struct device *mock_dev)
-{
- struct iommufd_device tmp_idev = { .dev = mock_dev };
- struct iommufd_hw_pagetable *hwpt;
-
- mutex_lock(&ioas->mutex);
- hwpt = iommufd_hw_pagetable_alloc(ictx, ioas, &tmp_idev, false);
- mutex_unlock(&ioas->mutex);
- if (IS_ERR(hwpt))
- return hwpt;
-
- refcount_inc(&hwpt->obj.users);
- iommufd_object_finalize(ictx, &hwpt->obj);
- return hwpt;
-}
-
-void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
- struct iommufd_hw_pagetable *hwpt)
-{
- refcount_dec(&hwpt->obj.users);
-}
-#endif
diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommufd/iommufd_private.h
index 331664e917b771..d523ef12890e1e 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -297,12 +297,6 @@ void iopt_remove_access(struct io_pagetable *iopt,
void iommufd_access_destroy_object(struct iommufd_object *obj);
#ifdef CONFIG_IOMMUFD_TEST
-struct iommufd_hw_pagetable *
-iommufd_device_selftest_attach(struct iommufd_ctx *ictx,
- struct iommufd_ioas *ioas,
- struct device *mock_dev);
-void iommufd_device_selftest_detach(struct iommufd_ctx *ictx,
- struct iommufd_hw_pagetable *hwpt);
int iommufd_test(struct iommufd_ucmd *ucmd);
void iommufd_selftest_destroy(struct iommufd_object *obj);
extern size_t iommufd_test_memory_limit;
@@ -311,6 +305,7 @@ void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
bool iommufd_should_fail(void);
void __init iommufd_test_init(void);
void iommufd_test_exit(void);
+bool iommufd_selftest_is_mock_dev(struct device *dev);
#else
static inline void iommufd_test_syz_conv_iova_id(struct iommufd_ucmd *ucmd,
unsigned int ioas_id,
@@ -327,5 +322,9 @@ static inline void __init iommufd_test_init(void)
static inline void iommufd_test_exit(void)
{
}
+static inline bool iommufd_selftest_is_mock_dev(struct device *dev)
+{
+ return false;
+}
#endif
#endif
diff --git a/drivers/iommu/iommufd/selftest.c b/drivers/iommu/iommufd/selftest.c
index cfb5fe9a5e0ee8..8540adcb68f1f0 100644
--- a/drivers/iommu/iommufd/selftest.c
+++ b/drivers/iommu/iommufd/selftest.c
@@ -91,23 +91,50 @@ enum selftest_obj_type {
TYPE_IDEV,
};
+struct mock_dev {
+ struct device dev;
+};
+
struct selftest_obj {
struct iommufd_object obj;
enum selftest_obj_type type;
union {
struct {
- struct iommufd_hw_pagetable *hwpt;
+ struct iommufd_device *idev;
struct iommufd_ctx *ictx;
- struct device mock_dev;
+ struct mock_dev *mock_dev;
} idev;
};
};
+static void mock_domain_blocking_free(struct iommu_domain *domain)
+{
+}
+
+static int mock_domain_nop_attach(struct iommu_domain *domain,
+ struct device *dev)
+{
+ return 0;
+}
+
+static const struct iommu_domain_ops mock_blocking_ops = {
+ .free = mock_domain_blocking_free,
+ .attach_dev = mock_domain_nop_attach,
+};
+
+static struct iommu_domain mock_blocking_domain = {
+ .type = IOMMU_DOMAIN_BLOCKED,
+ .ops = &mock_blocking_ops,
+};
+
static struct iommu_domain *mock_domain_alloc(unsigned int iommu_domain_type)
{
struct mock_iommu_domain *mock;
+ if (iommu_domain_type == IOMMU_DOMAIN_BLOCKED)
+ return &mock_blocking_domain;
+
if (WARN_ON(iommu_domain_type != IOMMU_DOMAIN_UNMANAGED))
return NULL;
@@ -236,19 +263,39 @@ static phys_addr_t mock_domain_iova_to_phys(struct iommu_domain *domain,
return (xa_to_value(ent) & MOCK_PFN_MASK) * MOCK_IO_PAGE_SIZE;
}
+static bool mock_domain_capable(struct device *dev, enum iommu_cap cap)
+{
+ return cap == IOMMU_CAP_CACHE_COHERENCY;
+}
+
+static void mock_domain_set_plaform_dma_ops(struct device *dev)
+{
+ /*
+ * mock doesn't setup default domains because we can't hook into the
+ * normal probe path
+ */
+}
+
static const struct iommu_ops mock_ops = {
.owner = THIS_MODULE,
.pgsize_bitmap = MOCK_IO_PAGE_SIZE,
.domain_alloc = mock_domain_alloc,
+ .capable = mock_domain_capable,
+ .set_platform_dma_ops = mock_domain_set_plaform_dma_ops,
.default_domain_ops =
&(struct iommu_domain_ops){
.free = mock_domain_free,
+ .attach_dev = mock_domain_nop_attach,
.map_pages = mock_domain_map_pages,
.unmap_pages = mock_domain_unmap_pages,
.iova_to_phys = mock_domain_iova_to_phys,
},
};
+struct iommu_device mock_iommu_device = {
+ .ops = &mock_ops,
+};
+
static inline struct iommufd_hw_pagetable *
get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
struct mock_iommu_domain **mock)
@@ -269,48 +316,142 @@ get_md_pagetable(struct iommufd_ucmd *ucmd, u32 mockpt_id,
return hwpt;
}
+static struct bus_type iommufd_mock_bus_type = {
+ .name = "iommufd_mock",
+ .iommu_ops = &mock_ops,
+};
+
+static void mock_dev_release(struct device *dev)
+{
+ struct mock_dev *mdev = container_of(dev, struct mock_dev, dev);
+
+ kfree(mdev);
+}
+
+static struct mock_dev *mock_dev_create(void)
+{
+ struct iommu_group *iommu_group;
+ struct dev_iommu *dev_iommu;
+ struct mock_dev *mdev;
+ int rc;
+
+ mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
+ if (!mdev)
+ return ERR_PTR(-ENOMEM);
+
+ device_initialize(&mdev->dev);
+ mdev->dev.release = mock_dev_release;
+ mdev->dev.bus = &iommufd_mock_bus_type;
+
+ iommu_group = iommu_group_alloc();
+ if (IS_ERR(iommu_group)) {
+ rc = PTR_ERR(iommu_group);
+ goto err_put;
+ }
+
+ rc = dev_set_name(&mdev->dev, "iommufd_mock%u",
+ iommu_group_id(iommu_group));
+ if (rc)
+ goto err_group;
+
+ /*
+ * The iommu core has no way to associate a single device with an iommu
+ * driver (heck currently it can't even support two iommu_drivers
+ * registering). Hack it together with an open coded dev_iommu_get().
+ * Notice that the normal notifier triggered iommu release process also
+ * does not work here because this bus is not in iommu_buses.
+ */
+ mdev->dev.iommu = kzalloc(sizeof(*dev_iommu), GFP_KERNEL);
+ if (!mdev->dev.iommu) {
+ rc = -ENOMEM;
+ goto err_group;
+ }
+ mutex_init(&mdev->dev.iommu->lock);
+ mdev->dev.iommu->iommu_dev = &mock_iommu_device;
+
+ rc = device_add(&mdev->dev);
+ if (rc)
+ goto err_dev_iommu;
+
+ rc = iommu_group_add_device(iommu_group, &mdev->dev);
+ if (rc)
+ goto err_del;
+ iommu_group_put(iommu_group);
+ return mdev;
+
+err_del:
+ device_del(&mdev->dev);
+err_dev_iommu:
+ kfree(mdev->dev.iommu);
+ mdev->dev.iommu = NULL;
+err_group:
+ iommu_group_put(iommu_group);
+err_put:
+ put_device(&mdev->dev);
+ return ERR_PTR(rc);
+}
+
+static void mock_dev_destroy(struct mock_dev *mdev)
+{
+ iommu_group_remove_device(&mdev->dev);
+ device_del(&mdev->dev);
+ kfree(mdev->dev.iommu);
+ mdev->dev.iommu = NULL;
+ put_device(&mdev->dev);
+}
+
+bool iommufd_selftest_is_mock_dev(struct device *dev)
+{
+ return dev->release == mock_dev_release;
+}
+
/* Create an hw_pagetable with the mock domain so we can test the domain ops */
static int iommufd_test_mock_domain(struct iommufd_ucmd *ucmd,
struct iommu_test_cmd *cmd)
{
- static struct bus_type mock_bus = { .iommu_ops = &mock_ops };
- struct iommufd_hw_pagetable *hwpt;
+ struct iommufd_device *idev;
struct selftest_obj *sobj;
- struct iommufd_ioas *ioas;
+ u32 pt_id = cmd->id;
+ u32 idev_id;
int rc;
- ioas = iommufd_get_ioas(ucmd, cmd->id);
- if (IS_ERR(ioas))
- return PTR_ERR(ioas);
-
sobj = iommufd_object_alloc(ucmd->ictx, sobj, IOMMUFD_OBJ_SELFTEST);
- if (IS_ERR(sobj)) {
- rc = PTR_ERR(sobj);
- goto out_ioas;
- }
+ if (IS_ERR(sobj))
+ return PTR_ERR(sobj);
+
sobj->idev.ictx = ucmd->ictx;
sobj->type = TYPE_IDEV;
- sobj->idev.mock_dev.bus = &mock_bus;
- hwpt = iommufd_device_selftest_attach(ucmd->ictx, ioas,
- &sobj->idev.mock_dev);
- if (IS_ERR(hwpt)) {
- rc = PTR_ERR(hwpt);
+ sobj->idev.mock_dev = mock_dev_create();
+ if (IS_ERR(sobj->idev.mock_dev)) {
+ rc = PTR_ERR(sobj->idev.mock_dev);
goto out_sobj;
}
- sobj->idev.hwpt = hwpt;
- /* Userspace must destroy both of these IDs to destroy the object */
- cmd->mock_domain.out_hwpt_id = hwpt->obj.id;
+ idev = iommufd_device_bind(ucmd->ictx, &sobj->idev.mock_dev->dev,
+ &idev_id);
+ if (IS_ERR(idev)) {
+ rc = PTR_ERR(idev);
+ goto out_mdev;
+ }
+ sobj->idev.idev = idev;
+
+ rc = iommufd_device_attach(idev, &pt_id);
+ if (rc)
+ goto out_unbind;
+
+ /* Userspace must destroy the device_id to destroy the object */
+ cmd->mock_domain.out_hwpt_id = pt_id;
cmd->mock_domain.out_device_id = sobj->obj.id;
iommufd_object_finalize(ucmd->ictx, &sobj->obj);
- iommufd_put_object(&ioas->obj);
return iommufd_ucmd_respond(ucmd, sizeof(*cmd));
+out_unbind:
+ iommufd_device_unbind(idev);
+out_mdev:
+ mock_dev_destroy(sobj->idev.mock_dev);
out_sobj:
iommufd_object_abort(ucmd->ictx, &sobj->obj);
-out_ioas:
- iommufd_put_object(&ioas->obj);
return rc;
}
@@ -780,8 +921,9 @@ void iommufd_selftest_destroy(struct iommufd_object *obj)
switch (sobj->type) {
case TYPE_IDEV:
- iommufd_device_selftest_detach(sobj->idev.ictx,
- sobj->idev.hwpt);
+ iommufd_device_detach(sobj->idev.idev);
+ iommufd_device_unbind(sobj->idev.idev);
+ mock_dev_destroy(sobj->idev.mock_dev);
break;
}
}
@@ -845,9 +987,11 @@ void __init iommufd_test_init(void)
{
dbgfs_root =
fault_create_debugfs_attr("fail_iommufd", NULL, &fail_iommufd);
+ WARN_ON(bus_register(&iommufd_mock_bus_type));
}
void iommufd_test_exit(void)
{
debugfs_remove_recursive(dbgfs_root);
+ bus_unregister(&iommufd_mock_bus_type);
}
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index fa08209268c42f..0c4bbd4079a425 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -645,7 +645,6 @@ TEST_F(iommufd_ioas, access_pin)
&check_map_cmd));
test_ioctl_destroy(mock_device_id);
- test_ioctl_destroy(mock_hwpt_id);
test_cmd_destroy_access_pages(
access_cmd.id,
access_cmd.access_pages.out_access_pages_id);
@@ -1214,7 +1213,6 @@ TEST_F(iommufd_mock_domain, all_aligns_copy)
1);
test_ioctl_destroy(mock_device_id);
- test_ioctl_destroy(self->domain_ids[1]);
self->domain_ids[1] = old_id;
test_ioctl_ioas_unmap(iova, length);
diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c
index 9713111b820dd7..dda8f0187cd88b 100644
--- a/tools/testing/selftests/iommu/iommufd_fail_nth.c
+++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c
@@ -323,8 +323,6 @@ TEST_FAIL_NTH(basic_fail_nth, map_domain)
if (_test_ioctl_destroy(self->fd, device_id))
return -1;
- if (_test_ioctl_destroy(self->fd, hwpt_id))
- return -1;
if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id))
return -1;
@@ -365,13 +363,9 @@ TEST_FAIL_NTH(basic_fail_nth, map_two_domains)
if (_test_ioctl_destroy(self->fd, device_id))
return -1;
- if (_test_ioctl_destroy(self->fd, hwpt_id))
- return -1;
if (_test_ioctl_destroy(self->fd, device_id2))
return -1;
- if (_test_ioctl_destroy(self->fd, hwpt_id2))
- return -1;
if (_test_cmd_mock_domain(self->fd, ioas_id, &device_id, &hwpt_id))
return -1;
@@ -572,8 +566,6 @@ TEST_FAIL_NTH(basic_fail_nth, access_pin_domain)
if (_test_ioctl_destroy(self->fd, device_id))
return -1;
- if (_test_ioctl_destroy(self->fd, hwpt_id))
- return -1;
return 0;
}
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device
2023-02-22 21:02 ` [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device Jason Gunthorpe
@ 2023-02-24 6:50 ` Tian, Kevin
2023-02-24 14:26 ` Jason Gunthorpe
0 siblings, 1 reply; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 6:50 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> iommufd wants to use more infrastructure, like the iommu_group, that the
> mock device does not support. Create a more complete mock device that can
> go through the whole cycle of ownership, blocking domain, and has an
> iommu_group.
this is a nice move! Presumable with a real struct device we can further
extend the mock device to test nested, pasid attach/alloc, etc. in the
future.
> +
> + /*
> + * The iommu core has no way to associate a single device with an
> iommu
> + * driver (heck currently it can't even support two iommu_drivers
> + * registering). Hack it together with an open coded dev_iommu_get().
> + * Notice that the normal notifier triggered iommu release process
> also
> + * does not work here because this bus is not in iommu_buses.
> + */
> + mdev->dev.iommu = kzalloc(sizeof(*dev_iommu), GFP_KERNEL);
> + if (!mdev->dev.iommu) {
> + rc = -ENOMEM;
> + goto err_group;
> + }
> + mutex_init(&mdev->dev.iommu->lock);
> + mdev->dev.iommu->iommu_dev = &mock_iommu_device;
I understand how this hack works but didn't get why in this case
dev_iommu_get() cannot be reused...
but this is minor.
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device
2023-02-24 6:50 ` Tian, Kevin
@ 2023-02-24 14:26 ` Jason Gunthorpe
0 siblings, 0 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-24 14:26 UTC (permalink / raw)
To: Tian, Kevin
Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org,
Shuah Khan, Nicolin Chen, Liu, Yi L
On Fri, Feb 24, 2023 at 06:50:43AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Thursday, February 23, 2023 5:03 AM
> >
> > iommufd wants to use more infrastructure, like the iommu_group, that the
> > mock device does not support. Create a more complete mock device that can
> > go through the whole cycle of ownership, blocking domain, and has an
> > iommu_group.
>
> this is a nice move! Presumable with a real struct device we can further
> extend the mock device to test nested, pasid attach/alloc, etc. in the
> future.
Yes, it should all work better
> > + /*
> > + * The iommu core has no way to associate a single device with an
> > iommu
> > + * driver (heck currently it can't even support two iommu_drivers
> > + * registering). Hack it together with an open coded dev_iommu_get().
> > + * Notice that the normal notifier triggered iommu release process
> > also
> > + * does not work here because this bus is not in iommu_buses.
> > + */
> > + mdev->dev.iommu = kzalloc(sizeof(*dev_iommu), GFP_KERNEL);
> > + if (!mdev->dev.iommu) {
> > + rc = -ENOMEM;
> > + goto err_group;
> > + }
> > + mutex_init(&mdev->dev.iommu->lock);
> > + mdev->dev.iommu->iommu_dev = &mock_iommu_device;
>
> I understand how this hack works but didn't get why in this case
> dev_iommu_get() cannot be reused...
I'd have to make it an exported symbol.. It could be done, maybe it
should be done. I'm unclear how much intrustion we should create for
the selftests.
The other thought I had is that perhaps we could fix the bus binding
so that it wasn't a hard coded list in the iommu.c Eg a flag in the
bus_type or provide the list when binding the driver or
something. Then things would work perfectly. Needs Robin's series
first though as it allows multiple iommu drivers.
So.. I kind of left it like this hoping that other parts would advance
more and we can revisit it later, it is only 4 lines.
Jason
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument
2023-02-22 21:02 [PATCH v2 0/7] Revise the hwpt lifetime model Jason Gunthorpe
` (5 preceding siblings ...)
2023-02-22 21:02 ` [PATCH v2 6/7] iommufd/selftest: Make selftest create a more complete mock device Jason Gunthorpe
@ 2023-02-22 21:02 ` Jason Gunthorpe
2023-02-24 7:02 ` Tian, Kevin
6 siblings, 1 reply; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-22 21:02 UTC (permalink / raw)
To: iommu, Kevin Tian, linux-kselftest, Shuah Khan; +Cc: Nicolin Chen, Yi Liu
This can now be covered since we have a full struct device.
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
tools/testing/selftests/iommu/iommufd.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index 0c4bbd4079a425..69cb79d6a0711d 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -187,6 +187,7 @@ FIXTURE(iommufd_ioas)
int fd;
uint32_t ioas_id;
uint32_t domain_id;
+ uint32_t hwpt_id;
uint64_t base_iova;
};
@@ -212,7 +213,7 @@ FIXTURE_SETUP(iommufd_ioas)
}
for (i = 0; i != variant->mock_domains; i++) {
- test_cmd_mock_domain(self->ioas_id, NULL, &self->domain_id);
+ test_cmd_mock_domain(self->ioas_id, &self->domain_id, &self->hwpt_id);
self->base_iova = MOCK_APERTURE_START;
}
}
@@ -259,6 +260,16 @@ TEST_F(iommufd_ioas, ioas_destroy)
}
}
+TEST_F(iommufd_ioas, hwpt_attach)
+{
+ /* Create a device attached directly to a hwpt */
+ if (self->domain_id) {
+ test_cmd_mock_domain(self->hwpt_id, NULL, NULL);
+ } else {
+ test_err_mock_domain(ENOENT, self->hwpt_id, NULL, NULL);
+ }
+}
+
TEST_F(iommufd_ioas, ioas_area_destroy)
{
/* Adding an area does not change ability to destroy */
--
2.39.1
^ permalink raw reply related [flat|nested] 19+ messages in thread* RE: [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument
2023-02-22 21:02 ` [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument Jason Gunthorpe
@ 2023-02-24 7:02 ` Tian, Kevin
2023-02-24 18:19 ` Jason Gunthorpe
0 siblings, 1 reply; 19+ messages in thread
From: Tian, Kevin @ 2023-02-24 7:02 UTC (permalink / raw)
To: Jason Gunthorpe, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org, Shuah Khan
Cc: Nicolin Chen, Liu, Yi L
> From: Jason Gunthorpe <jgg@nvidia.com>
> Sent: Thursday, February 23, 2023 5:03 AM
>
> This can now be covered since we have a full struct device.
>
> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> ---
> tools/testing/selftests/iommu/iommufd.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/iommu/iommufd.c
> b/tools/testing/selftests/iommu/iommufd.c
> index 0c4bbd4079a425..69cb79d6a0711d 100644
> --- a/tools/testing/selftests/iommu/iommufd.c
> +++ b/tools/testing/selftests/iommu/iommufd.c
> @@ -187,6 +187,7 @@ FIXTURE(iommufd_ioas)
> int fd;
> uint32_t ioas_id;
> uint32_t domain_id;
> + uint32_t hwpt_id;
> uint64_t base_iova;
> };
>
> @@ -212,7 +213,7 @@ FIXTURE_SETUP(iommufd_ioas)
> }
>
> for (i = 0; i != variant->mock_domains; i++) {
> - test_cmd_mock_domain(self->ioas_id, NULL, &self-
> >domain_id);
> + test_cmd_mock_domain(self->ioas_id, &self->domain_id,
> &self->hwpt_id);
I didn't get how this works. self->domain_id now means device_id but
other references still take it as a domain, e.g.:
TEST_F(iommufd_ioas, ioas_destroy)
{
if (self->domain_id) {
/* IOAS cannot be freed while a domain is on it */
EXPECT_ERRNO(EBUSY,
_test_ioctl_destroy(self->fd, self->ioas_id));
} else {
/* Can allocate and manually free an IOAS table */
test_ioctl_destroy(self->ioas_id);
}
}
^ permalink raw reply [flat|nested] 19+ messages in thread* Re: [PATCH v2 7/7] iommufd/selftest: Add a selftest for iommufd_device_attach() with a hwpt argument
2023-02-24 7:02 ` Tian, Kevin
@ 2023-02-24 18:19 ` Jason Gunthorpe
0 siblings, 0 replies; 19+ messages in thread
From: Jason Gunthorpe @ 2023-02-24 18:19 UTC (permalink / raw)
To: Tian, Kevin
Cc: iommu@lists.linux.dev, linux-kselftest@vger.kernel.org,
Shuah Khan, Nicolin Chen, Liu, Yi L
On Fri, Feb 24, 2023 at 07:02:06AM +0000, Tian, Kevin wrote:
> > From: Jason Gunthorpe <jgg@nvidia.com>
> > Sent: Thursday, February 23, 2023 5:03 AM
> >
> > This can now be covered since we have a full struct device.
> >
> > Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
> > ---
> > tools/testing/selftests/iommu/iommufd.c | 13 ++++++++++++-
> > 1 file changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/iommu/iommufd.c
> > b/tools/testing/selftests/iommu/iommufd.c
> > index 0c4bbd4079a425..69cb79d6a0711d 100644
> > --- a/tools/testing/selftests/iommu/iommufd.c
> > +++ b/tools/testing/selftests/iommu/iommufd.c
> > @@ -187,6 +187,7 @@ FIXTURE(iommufd_ioas)
> > int fd;
> > uint32_t ioas_id;
> > uint32_t domain_id;
> > + uint32_t hwpt_id;
> > uint64_t base_iova;
> > };
> >
> > @@ -212,7 +213,7 @@ FIXTURE_SETUP(iommufd_ioas)
> > }
> >
> > for (i = 0; i != variant->mock_domains; i++) {
> > - test_cmd_mock_domain(self->ioas_id, NULL, &self-
> > >domain_id);
> > + test_cmd_mock_domain(self->ioas_id, &self->domain_id,
> > &self->hwpt_id);
>
> I didn't get how this works. self->domain_id now means device_id but
> other references still take it as a domain, e.g.
The names are just wrong, Nicolin pointed this out and I had a later
patch to fix it, but lets move it earlier to this series.
Thanks,
Jason
^ permalink raw reply [flat|nested] 19+ messages in thread