2023-09-28 11:42:14

by Yi Liu

[permalink] [raw]
Subject: [PATCH v2 2/6] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation

This makes IOMMUFD to use iommu_domain_alloc_user() for iommu_domain
creation as IOMMUFD needs to support iommu_domain allocation with
parameters from userspace in nested support. If the iommu driver
doesn't provide domain_alloc_user callback then IOMMUFD falls back to
use iommu_domain_alloc().

Suggested-by: Jason Gunthorpe <[email protected]>
Reviewed-by: Lu Baolu <[email protected]>
Reviewed-by: Kevin Tian <[email protected]>
Co-developed-by: Nicolin Chen <[email protected]>
Signed-off-by: Nicolin Chen <[email protected]>
Signed-off-by: Yi Liu <[email protected]>
---
drivers/iommu/iommufd/hw_pagetable.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/hw_pagetable.c
index cf2c1504e20d..48874f896521 100644
--- a/drivers/iommu/iommufd/hw_pagetable.c
+++ b/drivers/iommu/iommufd/hw_pagetable.c
@@ -5,6 +5,7 @@
#include <linux/iommu.h>
#include <uapi/linux/iommufd.h>

+#include "../iommu-priv.h"
#include "iommufd_private.h"

void iommufd_hw_pagetable_destroy(struct iommufd_object *obj)
@@ -74,6 +75,7 @@ struct iommufd_hw_pagetable *
iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
struct iommufd_device *idev, bool immediate_attach)
{
+ const struct iommu_ops *ops = dev_iommu_ops(idev->dev);
struct iommufd_hw_pagetable *hwpt;
int rc;

@@ -88,10 +90,19 @@ 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(idev->dev->bus);
- if (!hwpt->domain) {
- rc = -ENOMEM;
- goto out_abort;
+ if (ops->domain_alloc_user) {
+ hwpt->domain = ops->domain_alloc_user(idev->dev, 0);
+ if (IS_ERR(hwpt->domain)) {
+ rc = PTR_ERR(hwpt->domain);
+ hwpt->domain = NULL;
+ goto out_abort;
+ }
+ } else {
+ hwpt->domain = iommu_domain_alloc(idev->dev->bus);
+ if (!hwpt->domain) {
+ rc = -ENOMEM;
+ goto out_abort;
+ }
}

/*
--
2.34.1


2023-10-16 17:48:45

by Nicolin Chen

[permalink] [raw]
Subject: Re: [PATCH v2 2/6] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation

On Mon, Oct 16, 2023 at 04:16:05PM +0800, Liu, Jingqi wrote:

> @@ -88,10 +90,19 @@ 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(idev->dev->bus);
> - if (!hwpt->domain) {
> - rc = -ENOMEM;
> - goto out_abort;
> + if (ops->domain_alloc_user) {
> + hwpt->domain = ops->domain_alloc_user(idev->dev, 0);
>
> Seems a "flags" parameter needs to be passed to 'domain_alloc_user()'.
> Like this:
> hwpt->domain = ops->domain_alloc_user(idev->dev, flags);

There's no "flags" parameter until the following PATCH-3:
https://lore.kernel.org/linux-iommu/[email protected]/

Thanks
Nicolin

2023-10-17 03:46:59

by Jingqi Liu

[permalink] [raw]
Subject: Re: [PATCH v2 2/6] iommufd/hw_pagetable: Use domain_alloc_user op for domain allocation


On 10/17/2023 1:48 AM, Nicolin Chen wrote:
> On Mon, Oct 16, 2023 at 04:16:05PM +0800, Liu, Jingqi wrote:
>
>> @@ -88,10 +90,19 @@ 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(idev->dev->bus);
>> - if (!hwpt->domain) {
>> - rc = -ENOMEM;
>> - goto out_abort;
>> + if (ops->domain_alloc_user) {
>> + hwpt->domain = ops->domain_alloc_user(idev->dev, 0);
>>
>> Seems a "flags" parameter needs to be passed to 'domain_alloc_user()'.
>> Like this:
>> hwpt->domain = ops->domain_alloc_user(idev->dev, flags);
>
> There's no "flags" parameter until the following PATCH-3:
> https://lore.kernel.org/linux-iommu/[email protected]/
>
Yes. I had noticed that "flags" parameter is added in PATCH-3.
Thanks for your clarification.

BR,
Jingqi

> Thanks
> Nicolin