Skip to contentSkip to conversation

iommu: Domain allocation enhancements

PATCH Superseded Vasant Hegde 5 participants 75 messages Sep 11, 2024 – Oct 17, 2024 3 messages not in the archive

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

v2 cover iommu: Domain allocation enhancements

This series adds iommu_paging_domain_alloc_flags() which takes flags to pass additional details for domain allocation (like domain with PASID support).

Also updates AMD IOMMU driver domain allocation code. With this by default it will allocate domain with V2 page table for PASID capable device and v1 page table for rest of the devices.

@Baolu,
With this changes (patch 1), to allocate PASID capable device core will call domain_alloc_user() interface. Do we need any changes to intel driver?

@Jason,
Do we need any changes to arm-smm-v3 driver?

Thanks everyone who reviewed previous version and provided valuable feedbacks.

This series is based on top of iommu/next and protection domain free fix [1].
[1] https://lore.kernel.org/linux-iommu/[email protected]/

This is also available at github :
https://github.com/AMDESE/linux-iommu/tree/iommu_paging_domain_v2

Changes from v1 -> v2:
- Rebased on top of iommu/next
- Split patches into smaller patches
- Added global static identity domain support

V1 : https://lore.kernel.org/linux-iommu/[email protected]/

RFC v2: https://lore.kernel.org/linux-iommu/[email protected]/T/#t

RFC v1 : https://lore.kernel.org/linux-iommu/[email protected]/

Jason Gunthorpe (3):
iommu: Refactor __iommu_domain_alloc()
iommu: Introduce iommu_paging_domain_alloc_flags()
iommu: Add new flag to explictly request PASID capable domain

Vasant Hegde (5):
iommu/amd: Separate page table setup from domain allocation iommu/amd: Pass page table type as param to pdom_setup_pgtable() iommu/amd: Enhance domain_alloc_user() to allocate PASID capable
domain
iommu/amd: Add iommu_ops->domain_alloc_paging support iommu/amd: Implement global identity domain

 drivers/iommu/amd/amd_iommu.h |   1 +
 drivers/iommu/amd/init.c      |   3 +
 drivers/iommu/amd/iommu.c     | 133 ++++++++++++++++++++++++----------
 drivers/iommu/iommu.c         | 105 ++++++++++++++++++++-------
 include/linux/iommu.h         |  16 +++-
 include/uapi/linux/iommufd.h  |   6 ++
 6 files changed, 194 insertions(+), 70 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH v2 1/8 iommu: Refactor __iommu_domain_alloc()

From: Jason Gunthorpe <[email protected]>

Following patch will introduce iommu_paging_domain_alloc_flags() API. Hence move domain init code to separate function so that it can be resued.

Also move iommu_get_dma_cookie() setup iommu_setup_default_domain() as its required in DMA API mode only.

[Split the patch and added description - Vasant]
---
 drivers/iommu/iommu.c | 46 +++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 21 deletions(-)
drivers/iommu/iommu.c +25 -21
index ed6c5cb60c5a..4118d3b15310 100644
@@ -1934,6 +1934,22 @@ void iommu_set_fault_handler(struct iommu_domain *domain,
 }
 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
 
+static void iommu_domain_init(struct iommu_domain *domain, unsigned int type,
+			      const struct iommu_ops *ops)
+{
+	domain->type = type;
+	domain->owner = ops;
+	if (!domain->ops)
+		domain->ops = ops->default_domain_ops;
+
+	/*
+	 * If not already set, assume all sizes by default; the driver
+	 * may override this later
+	 */
+	if (!domain->pgsize_bitmap)
+		domain->pgsize_bitmap = ops->pgsize_bitmap;
+}
+
 static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
 						 struct device *dev,
 						 unsigned int type)
@@ -1962,27 +1978,7 @@ static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the driver
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
 
@@ -2965,6 +2961,14 @@ static int iommu_setup_default_domain(struct iommu_group *group,
 	if (group->default_domain == dom)
 		return 0;
 
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+
 	/*
 	 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
 	 * mapped before their device is attached, in order to guarantee
Signature · 1 line
2.31.1
maintainer Reviewed-by on v2 1/8
#2 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Quoted 9 lines · Vasant Hegde

From: Jason Gunthorpe<[email protected]>

Following patch will introduce iommu_paging_domain_alloc_flags() API. Hence move domain init code to separate function so that it can be resued.

Also move iommu_get_dma_cookie() setup iommu_setup_default_domain() as its required in DMA API mode only.

Quoted 6 lines
[Split the patch and added description - Vasant]
---
  drivers/iommu/iommu.c | 46 +++++++++++++++++++++++--------------------
  1 file changed, 25 insertions(+), 21 deletions(-)

Thanks,
baolu

#3 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

In reply to Vasant Hegde

Hi Vasant,

Quoted 32 lines · On Wed, 11 Sep 2024 10:19:04 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/iommu.c +0 -4
@@ -1962,27 +1978,7 @@ static struct iommu_domain
 *__iommu_domain_alloc(const struct iommu_ops *ops, if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the

driver

drivers/iommu/iommu.c +1 -17
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
@@ -2965,6 +2961,14 @@ static int iommu_setup_default_domain(struct
 iommu_group *group, if (group->default_domain == dom)

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct · line 2971

Quoted 10 lines
drivers/iommu/iommu.c +8 -0
 		return 0;
 
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+

nit, if a user changes default domain types via sysfs and the current domain is already DMA API type, i.e. DMA to DMA-FQ, I think it causes an extra round of iommu_get/put_dma_cookie() compared with doing this in allocation time.

author maintainer
#4 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Hi Jacob,

Quoted 39 lines · Jacob Pan

Hi Vasant,

Quoted 34 lines · On Wed, 11 Sep 2024 10:19:04 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/iommu.c +0 -4
@@ -1962,27 +1978,7 @@ static struct iommu_domain
 *__iommu_domain_alloc(const struct iommu_ops *ops, if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the

driver

drivers/iommu/iommu.c +1 -17
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
@@ -2965,6 +2961,14 @@ static int iommu_setup_default_domain(struct
 iommu_group *group, if (group->default_domain == dom)
 		return 0;
 
Quoted 12 lines

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 2971

Quoted 8 lines
drivers/iommu/iommu.c +8 -0
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+

nit, if a user changes default domain types via sysfs and the current domain is already DMA API type, i.e. DMA to DMA-FQ, I think it causes an extra round of iommu_get/put_dma_cookie() compared with doing this in allocation time.

DMA -> DMA_FQ will just initialize flush queue and exit. It will not enter iommu_setup_default_domain(). In case of DMA_FQ -> DMA it will do new domain allocation. Am I missing something here?

-Vasant

#5 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Hi Vasant,

Quoted 56 lines · On Thu, 26 Sep 2024 15:47:48 +0530 Vasant Hegde <[email protected]> wrote:

Hi Jacob,

Quoted 39 lines · Jacob Pan

Hi Vasant,

Quoted 34 lines · On Wed, 11 Sep 2024 10:19:04 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/iommu.c +0 -4
@@ -1962,27 +1978,7 @@ static struct iommu_domain
 *__iommu_domain_alloc(const struct iommu_ops *ops, if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the

driver

drivers/iommu/iommu.c +1 -17
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
@@ -2965,6 +2961,14 @@ static int iommu_setup_default_domain(struct
 iommu_group *group, if (group->default_domain == dom)
 		return 0;
 
Quoted 12 lines

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 2971

Quoted 8 lines
drivers/iommu/iommu.c +8 -0
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+  

nit, if a user changes default domain types via sysfs and the current domain is already DMA API type, i.e. DMA to DMA-FQ, I think it causes an extra round of iommu_get/put_dma_cookie() compared with doing this in allocation time.

Quoted 3 lines

DMA -> DMA_FQ will just initialize flush queue and exit. It will not enter iommu_setup_default_domain(). In case of DMA_FQ -> DMA it will do new domain allocation. Am I missing something here?

You are right. No extra get/put_dma_cookie there.
My other concern is that this change made domain alloc/free asymmetrical in terms of get/put_dma_cookie.

Currently, we have ftrace like this when I do:
echo DMA > /sys/bus/pci/devices/0000\:38\:00.0/iommu_group/type

    iommu_setup_default_domain() {
      __iommu_domain_alloc() {
        intel_iommu_domain_alloc();
        iommu_get_dma_cookie();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }

With this change, it will be:

    iommu_setup_default_domain() {
        iommu_get_dma_cookie();
	__iommu_domain_alloc() {
        intel_iommu_domain_alloc();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }
author maintainer
#6 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Hi Jacob,

Quoted 61 lines · Jacob Pan

Hi Vasant,

Quoted 56 lines · On Thu, 26 Sep 2024 15:47:48 +0530 Vasant Hegde <[email protected]> wrote:

Hi Jacob,

Quoted 51 lines · Jacob Pan

Hi Vasant,

Quoted 32 lines · On Wed, 11 Sep 2024 10:19:04 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/iommu.c +0 -4
@@ -1962,27 +1978,7 @@ static struct iommu_domain
 *__iommu_domain_alloc(const struct iommu_ops *ops, if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the

driver

drivers/iommu/iommu.c +1 -17
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
@@ -2965,6 +2961,14 @@ static int iommu_setup_default_domain(struct
 iommu_group *group, if (group->default_domain == dom)

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct · line 2971

Quoted 10 lines
drivers/iommu/iommu.c +8 -0
 		return 0;
 
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+  

nit, if a user changes default domain types via sysfs and the current domain is already DMA API type, i.e. DMA to DMA-FQ, I think it causes an extra round of iommu_get/put_dma_cookie() compared with doing this in allocation time.

Quoted 6 lines
Quoted 3 lines

DMA -> DMA_FQ will just initialize flush queue and exit. It will not enter iommu_setup_default_domain(). In case of DMA_FQ -> DMA it will do new domain allocation. Am I missing something here?

You are right. No extra get/put_dma_cookie there.
My other concern is that this change made domain alloc/free asymmetrical in terms of get/put_dma_cookie.

That's correct. And this should be fine right? These are internal functions.

-Vasant

Quoted 25 lines

Currently, we have ftrace like this when I do:
echo DMA > /sys/bus/pci/devices/0000\:38\:00.0/iommu_group/type

    iommu_setup_default_domain() {
      __iommu_domain_alloc() {
        intel_iommu_domain_alloc();
        iommu_get_dma_cookie();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }

With this change, it will be:

    iommu_setup_default_domain() {
        iommu_get_dma_cookie();
	__iommu_domain_alloc() {
        intel_iommu_domain_alloc();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }
#7 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Hi Vasant,

Quoted 67 lines · On Tue, 1 Oct 2024 10:01:31 +0530 Vasant Hegde <[email protected]> wrote:

Hi Jacob,

Quoted 64 lines · Jacob Pan

Hi Vasant,

Quoted 59 lines · On Thu, 26 Sep 2024 15:47:48 +0530 Vasant Hegde <[email protected]> wrote:

Hi Jacob,

Quoted 39 lines · Jacob Pan

Hi Vasant,

Quoted 34 lines · On Wed, 11 Sep 2024 10:19:04 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/iommu.c +0 -4
@@ -1962,27 +1978,7 @@ static struct iommu_domain
 *__iommu_domain_alloc(const struct iommu_ops *ops, if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	domain->type = type;
-	domain->owner = ops;
-	/*
-	 * If not already set, assume all sizes by default; the

driver

drivers/iommu/iommu.c +1 -17
-	 * may override this later
-	 */
-	if (!domain->pgsize_bitmap)
-		domain->pgsize_bitmap = ops->pgsize_bitmap;
-
-	if (!domain->ops)
-		domain->ops = ops->default_domain_ops;
-
-	if (iommu_is_dma_domain(domain)) {
-		int rc;
-
-		rc = iommu_get_dma_cookie(domain);
-		if (rc) {
-			iommu_domain_free(domain);
-			return ERR_PTR(rc);
-		}
-	}
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
@@ -2965,6 +2961,14 @@ static int

iommu_setup_default_domain(struct iommu_group *group, if (group->default_domain == dom) return 0;

Quoted 12 lines

drivers/iommu/iommu.c static int iommu_setup_default_domain(struct iommu_group *group, · line 2971

Quoted 8 lines
drivers/iommu/iommu.c +8 -0
+	if (iommu_is_dma_domain(dom)) {
+		ret = iommu_get_dma_cookie(dom);
+		if (ret) {
+			iommu_domain_free(dom);
+			return ret;
+		}
+	}
+    

nit, if a user changes default domain types via sysfs and the current domain is already DMA API type, i.e. DMA to DMA-FQ, I think it causes an extra round of iommu_get/put_dma_cookie() compared with doing this in allocation time.

DMA -> DMA_FQ will just initialize flush queue and exit. It will not enter iommu_setup_default_domain(). In case of DMA_FQ -> DMA it will do new domain allocation. Am I missing something here?

Quoted 7 lines
Quoted 3 lines

You are right. No extra get/put_dma_cookie there. My other concern is that this change made domain alloc/free asymmetrical in terms of get/put_dma_cookie.

That's correct. And this should be fine right? These are internal functions.

There are no functional issues. I don't have strong opinion other than maintaining symmetry reduces the likelihood of errors.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Quoted 13 lines · Jacob Pan

Currently, we have ftrace like this when I do:
echo DMA > /sys/bus/pci/devices/0000\:38\:00.0/iommu_group/type

    iommu_setup_default_domain() {
      __iommu_domain_alloc() {
        intel_iommu_domain_alloc();
        iommu_get_dma_cookie();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }
Quoted 6 lines

With this change, it will be:

    iommu_setup_default_domain() {
        iommu_get_dma_cookie();
	__iommu_domain_alloc() {
        intel_iommu_domain_alloc();
      }

?

iommu_get_dma_cookie() accepts a domain pointer, how can it be after intel_iommu_domain_alloc() ? That would be a bug, right?

Jason

#10 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

Hi Jason,

Quoted 21 lines · On Wed, 2 Oct 2024 11:19:51 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 13 lines · Jacob Pan

Currently, we have ftrace like this when I do:
echo DMA > /sys/bus/pci/devices/0000\:38\:00.0/iommu_group/type

    iommu_setup_default_domain() {
      __iommu_domain_alloc() {
        intel_iommu_domain_alloc();
        iommu_get_dma_cookie();
      }
      iommu_domain_free() {
         iommu_put_dma_cookie();
        intel_iommu_domain_free();
       }
Quoted 6 lines

With this change, it will be:

    iommu_setup_default_domain() {
        iommu_get_dma_cookie();
	__iommu_domain_alloc() {
        intel_iommu_domain_alloc();
      }  
Quoted 4 lines

?

iommu_get_dma_cookie() accepts a domain pointer, how can it be after intel_iommu_domain_alloc() ? That would be a bug, right?

My mistake, iommu_get_dma_cookie() is *after* __iommu_domain_alloc() with this change. No issues there. Sorry about the confusion. I was trying to show the asymmetrical part but missed the order.

Reviewed-by on v2 1/8
#11 lore raw

Re: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

In reply to Vasant Hegde
Quoted 9 lines · Vasant Hegde

From: Jason Gunthorpe <[email protected]>

Following patch will introduce iommu_paging_domain_alloc_flags() API. Hence move domain init code to separate function so that it can be resued.

Also move iommu_get_dma_cookie() setup iommu_setup_default_domain() as its required in DMA API mode only.

Quoted 6 lines
[Split the patch and added description - Vasant]
---
 drivers/iommu/iommu.c | 46 +++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 21 deletions(-)

Jason

maintainer Reviewed-by on v2 1/8
#12 lore raw

RE: [PATCH v2 1/8] iommu: Refactor __iommu_domain_alloc()

In reply to Vasant Hegde
Quoted 10 lines

From: Vasant Hegde <[email protected]>
Sent: Wednesday, September 11, 2024 6:19 PM

From: Jason Gunthorpe <[email protected]>

Following patch will introduce iommu_paging_domain_alloc_flags() API. Hence move domain init code to separate function so that it can be resued.

Also move iommu_get_dma_cookie() setup iommu_setup_default_domain()

s/setup/to/

Quoted 2 lines

as
its required in DMA API mode only.

s/its/it's/

Quoted 4 lines
[Split the patch and added description - Vasant]
author maintainer
#13 lore raw

PATCH v2 2/8 iommu: Introduce iommu_paging_domain_alloc_flags()

In reply to Vasant Hegde

From: Jason Gunthorpe <[email protected]>

Currently drivers calls iommu_paging_domain_alloc(dev) to get an UNMANAGED domain. This is not sufficient to support PASID with UNMANAGED domain as some HW like AMD requires certain page table type to support PASIDs.

Also domain_alloc_paging() passes device as param for domain allocation. This is not sufficient for AMD driver to decide the right page table.

Hence add iommu_paging_domain_alloc_flags() API which takes flags as parameter. Driver can pass additional parameter to indicate type of domain required, etc. iommu_paging_domain_alloc_flags() internally calls appropriate callback function to allocate a domain.

[Added description - Vasant]
---
 drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++----
 include/linux/iommu.h | 16 +++++++++++++---
 2 files changed, 39 insertions(+), 7 deletions(-)
drivers/iommu/iommu.c +26 -4
index 4118d3b15310..5d1cef36f633 100644
@@ -2027,20 +2027,42 @@ struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
 
 /**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
  * @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
  *
  * Allocate a paging domain which will be managed by a kernel driver. Return
  * allocated domain if successful, or a ERR pointer for failure.
  */
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
 {
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
 	if (!dev_has_iommu(dev))
 		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev, IOMMU_DOMAIN_UNMANAGED);
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+	else
+		return ERR_PTR(-EOPNOTSUPP);
+
+	if (IS_ERR(domain))
+		return domain;
+	if (!domain)
+		return ERR_PTR(-ENOMEM);
+
+	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	return domain;
 }
-EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc);
+EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);
 
 void iommu_domain_free(struct iommu_domain *domain)
 {
include/linux/iommu.h +13 -3
index bd722f473635..907957a88470 100644
@@ -511,8 +511,8 @@ static inline int __iommu_copy_struct_from_user_array(
  *                the caller iommu_domain_alloc() returns.
  * @domain_alloc_user: Allocate an iommu domain corresponding to the input
  *                     parameters as defined in include/uapi/linux/iommufd.h.
- *                     Unlike @domain_alloc, it is called only by IOMMUFD and
- *                     must fully initialize the new domain before return.
+ *                     Unlike @domain_alloc, it must fully initialize the new
+ *                     domain before return.
  *                     Upon success, if the @user_data is valid and the @parent
  *                     points to a kernel-managed domain, the new domain must be
  *                     IOMMU_DOMAIN_NESTED type; otherwise, the @parent must be
@@ -789,7 +789,11 @@ extern bool iommu_present(const struct bus_type *bus);
 extern bool device_iommu_capable(struct device *dev, enum iommu_cap cap);
 extern bool iommu_group_has_isolated_msi(struct iommu_group *group);
 extern struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus);
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev);
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, unsigned int flags);
+static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+{
+	return iommu_paging_domain_alloc_flags(dev, 0);
+}
 extern void iommu_domain_free(struct iommu_domain *domain);
 extern int iommu_attach_device(struct iommu_domain *domain,
 			       struct device *dev);
@@ -1096,6 +1100,12 @@ static inline struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus
 	return NULL;
 }
 
+struct inline iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+							    unsigned int flags)
+{
+	return ERR_PTR(-ENODEV);
+}
+
 static inline struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
 {
 	return ERR_PTR(-ENODEV);
Signature · 1 line
2.31.1
maintainer
#14 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

Quoted 20 lines · Vasant Hegde

/**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
* @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
*
* Allocate a paging domain which will be managed by a kernel driver. Return
* allocated domain if successful, or a ERR pointer for failure. */

drivers/iommu/iommu.c +5 -2
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
  {
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
  	if (!dev_has_iommu(dev))
  		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev, IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) · line 2055

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+	else
+		return ERR_PTR(-EOPNOTSUPP);

How about

	if (flags) {
		if (!ops->domain_alloc_user)
			return ERR_PTR(-EOPNOTSUPP);
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);
	} else if (ops->domain_alloc) {
		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
	} else {
		return ERR_PTR(-EOPNOTSUPP);
	}

?

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) · line 2065

Quoted 11 lines
drivers/iommu/iommu.c +9 -1
+
+	if (IS_ERR(domain))
+		return domain;
+	if (!domain)
+		return ERR_PTR(-ENOMEM);
+
+	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	return domain;
  }
-EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc);
+EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);

Thanks,
baolu

author maintainer
#15 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

Hi Baolu,

Quoted 46 lines · Baolu Lu
Quoted 22 lines · Vasant Hegde

  /**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
   * @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
   *
   * Allocate a paging domain which will be managed by a kernel driver. Return
   * allocated domain if successful, or a ERR pointer for failure. */

drivers/iommu/iommu.c +2 -1
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+                             unsigned int flags)

  {
+    const struct iommu_ops *ops;
+    struct iommu_domain *domain;
+
      if (!dev_has_iommu(dev))
          return ERR_PTR(-ENODEV);
  -    return __iommu_domain_alloc(dev_iommu_ops(dev), dev, IOMMU_DOMAIN_UNMANAGED);
+    ops = dev_iommu_ops(dev);
+

Quoted 8 lines

+    if (ops->domain_alloc_paging && !flags)
+        domain = ops->domain_alloc_paging(dev);
+    else if (ops->domain_alloc_user)
+        domain = ops->domain_alloc_user(dev, flags, NULL, NULL); +    else if (ops->domain_alloc && !flags)
+        domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED); +    else
+        return ERR_PTR(-EOPNOTSUPP);

How about

    if (flags) {
        if (!ops->domain_alloc_user)
            return ERR_PTR(-EOPNOTSUPP);
        domain = ops->domain_alloc_user(dev, flags, NULL, NULL); } else if (ops->domain_alloc_paging) {
        domain = ops->domain_alloc_paging(dev);
    } else if (ops->domain_alloc) {
        domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED); } else {
        return ERR_PTR(-EOPNOTSUPP);
    }

Quoted 1 line

?

Sure. This is bit more explicit. I will fix it.

-Vasant

maintainer
#16 lore raw

RE: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

In reply to Baolu Lu
Quoted 49 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, September 12, 2024 12:05 PM

Quoted 7 lines · Vasant Hegde

/**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
* @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
*
* Allocate a paging domain which will be managed by a kernel driver.

Return

Review

Quoted 4 lines

* allocated domain if successful, or a ERR pointer for failure. */

(quoted diff) +1 -1
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device

*dev,

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)

Quoted 9 lines
drivers/iommu/iommu.c +4 -1
+						     unsigned int flags)
  {
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
  	if (!dev_has_iommu(dev))
  		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev,

IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) · line 2055

Quoted 11 lines
drivers/iommu/iommu.c +8 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops-

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c +2 -0
+	else
+		return ERR_PTR(-EOPNOTSUPP);

How about

	if (flags) {
		if (!ops->domain_alloc_user)
			return ERR_PTR(-EOPNOTSUPP);
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);
	} else if (ops->domain_alloc) {
		domain = ops-
Quoted 5 lines
Quoted 1 line

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

	} else {
		return ERR_PTR(-EOPNOTSUPP);
	}

There is slight semantics difference.

Original code picks domain_alloc_user() if !domain_alloc_paging and !flags.

but your code will pick domain_alloc().

Based on discussions in this thread domain_alloc_user/ext is the preferred one. Then below is the right order?

	If (ops->domain_alloc_user) {
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (flags) {
		return ERR_PTR(-EOPNOTSUPP);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);

} else if (ops->domain_alloc) {
domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
} else {
return ERR_PTR(-EOPNOTSUPP);
}

#17 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

Quoted 71 lines · Kevin
Quoted 49 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, September 12, 2024 12:05 PM

Quoted 7 lines · Vasant Hegde

/**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
* @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
*
* Allocate a paging domain which will be managed by a kernel driver.

Return

Review

Quoted 4 lines

* allocated domain if successful, or a ERR pointer for failure. */

(quoted diff) +1 -1
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device

*dev,

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)

Quoted 9 lines
drivers/iommu/iommu.c +4 -1
+						     unsigned int flags)
  {
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
  	if (!dev_has_iommu(dev))
  		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev,

IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) · line 2055

Quoted 11 lines
drivers/iommu/iommu.c +8 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops-

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c +2 -0
+	else
+		return ERR_PTR(-EOPNOTSUPP);

How about

	if (flags) {
		if (!ops->domain_alloc_user)
			return ERR_PTR(-EOPNOTSUPP);
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);
	} else if (ops->domain_alloc) {
		domain = ops-
Quoted 5 lines
Quoted 1 line

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

	} else {
		return ERR_PTR(-EOPNOTSUPP);
	}

There is slight semantics difference.

Original code picks domain_alloc_user() if !domain_alloc_paging and !flags.

but your code will pick domain_alloc().

Based on discussions in this thread domain_alloc_user/ext is the preferred one. Then below is the right order?

	If (ops->domain_alloc_user) {
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (flags) {
		return ERR_PTR(-EOPNOTSUPP);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);

Review

Quoted 5 lines
(quoted diff) +0 -0
 	} else if (ops->domain_alloc) {
 		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
 	} else {
 		return ERR_PTR(-EOPNOTSUPP);
 	}

This can work too, but I think we need a few more patches still to get there?

The order above makes sense if drivers implement both ops->domain_alloc_paging/user, then you'd want to prefer the more narrow one.

Once drivers don't implement both then it would make sense to switch to your version

Jason

maintainer
#18 lore raw

RE: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

Quoted 87 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 15, 2024 8:32 PM

Quoted 11 lines · Kevin
Quoted 11 lines

From: Baolu Lu <[email protected]>
Sent: Thursday, September 12, 2024 12:05 PM

Quoted 7 lines · Vasant Hegde

/**
- * iommu_paging_domain_alloc() - Allocate a paging domain
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
* @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
*
* Allocate a paging domain which will be managed by a kernel driver.

Quoted 4 lines
Quoted 4 lines

Return

Quoted 3 lines

* allocated domain if successful, or a ERR pointer for failure. */
-struct iommu_domain *iommu_paging_domain_alloc(struct device

*dev)

Quoted 1 line
Quoted 1 line
Quoted 1 line

+struct iommu_domain *iommu_paging_domain_alloc_flags(struct

device

Quoted 58 lines
Quoted 33 lines

*dev,

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)

Quoted 9 lines
drivers/iommu/iommu.c +4 -1
+						     unsigned int flags)
  {
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
  	if (!dev_has_iommu(dev))
  		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev,

IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus) · line 2055

Quoted 11 lines
drivers/iommu/iommu.c +8 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops-

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

drivers/iommu/iommu.c +2 -0
+	else
+		return ERR_PTR(-EOPNOTSUPP);

How about

	if (flags) {
		if (!ops->domain_alloc_user)
			return ERR_PTR(-EOPNOTSUPP);
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);
	} else if (ops->domain_alloc) {
		domain = ops-
Quoted 5 lines
Quoted 1 line

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

	} else {
		return ERR_PTR(-EOPNOTSUPP);
	}

There is slight semantics difference.

Original code picks domain_alloc_user() if !domain_alloc_paging and !flags.

but your code will pick domain_alloc().

Based on discussions in this thread domain_alloc_user/ext is the preferred one. Then below is the right order?

	If (ops->domain_alloc_user) {
		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
	} else if (flags) {
		return ERR_PTR(-EOPNOTSUPP);
	} else if (ops->domain_alloc_paging) {
		domain = ops->domain_alloc_paging(dev);
(quoted diff) +0 -0
 	} else if (ops->domain_alloc) {
 		domain = ops-

domain_alloc(IOMMU_DOMAIN_UNMANAGED);

Review

Quoted 3 lines
(quoted diff) +0 -0
 	} else {
 		return ERR_PTR(-EOPNOTSUPP);
 	}

This can work too, but I think we need a few more patches still to get there?

Quoted 7 lines

The order above makes sense if drivers implement both ops->domain_alloc_paging/user, then you'd want to prefer the more narrow one.

Once drivers don't implement both then it would make sense to switch to your version

Okay, that makes sense.

Reviewed-by on v2 2/8
#19 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

In reply to Vasant Hegde
Quoted 16 lines · Vasant Hegde

From: Jason Gunthorpe <[email protected]>

Currently drivers calls iommu_paging_domain_alloc(dev) to get an UNMANAGED domain. This is not sufficient to support PASID with UNMANAGED domain as some HW like AMD requires certain page table type to support PASIDs.

Also domain_alloc_paging() passes device as param for domain allocation. This is not sufficient for AMD driver to decide the right page table.

Hence add iommu_paging_domain_alloc_flags() API which takes flags as parameter. Driver can pass additional parameter to indicate type of domain required, etc. iommu_paging_domain_alloc_flags() internally calls appropriate callback function to allocate a domain.

Quoted 7 lines
[Added description - Vasant]
---
 drivers/iommu/iommu.c | 30 ++++++++++++++++++++++++++----
 include/linux/iommu.h | 16 +++++++++++++---
 2 files changed, 39 insertions(+), 7 deletions(-)

Jason

#20 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

In reply to Vasant Hegde

Hi Vasant,

Quoted 19 lines · On Wed, 11 Sep 2024 10:19:05 +0000 Vasant Hegde <[email protected]> wrote:
(quoted diff) +1 -1
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device

*dev,

(quoted diff) +1 -0
+						     unsigned int

flags) {

(quoted diff) +3 -1
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
 	if (!dev_has_iommu(dev))
 		return ERR_PTR(-ENODEV);
 
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev,

IOMMU_DOMAIN_UNMANAGED);

(quoted diff) +6 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL,
Quoted 1 line

NULL);

Since this can be outside iommufd, the following comment needs update.

include/linux/iommu.h +1 -1
index 9842c6a797dc..6aeb6d9c985f 100644
@@ -517,7 +517,7 @@ static inline int __iommu_copy_struct_from_user_array(
  *                the caller iommu_domain_alloc() returns.
  * @domain_alloc_user: Allocate an iommu domain corresponding to the input
  *                     parameters as defined in include/uapi/linux/iommufd.h.
- *                     Unlike @domain_alloc, it is called only by IOMMUFD and
+ *                     Unlike @domain_alloc, it
  *                     must fully initialize the new domain before return.
  *                     Upon success, if the @user_data is valid and the @parent
  *                     points to a kernel-managed domain, the new
Quoted 15 lines
(quoted diff) +13 -1
+	else if (ops->domain_alloc && !flags)
+		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+	else
+		return ERR_PTR(-EOPNOTSUPP);
+
+	if (IS_ERR(domain))
+		return domain;
+	if (!domain)
+		return ERR_PTR(-ENOMEM);
+
+	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	return domain;
 }
-EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc);
+EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);
author maintainer
#21 lore raw

Re: [PATCH v2 2/8] iommu: Introduce iommu_paging_domain_alloc_flags()

Hi Jacob,

Quoted 16 lines · Jacob Pan

Hi Vasant,

Quoted 11 lines · On Wed, 11 Sep 2024 10:19:05 +0000 Vasant Hegde <[email protected]> wrote:
(quoted diff) +1 -1
-struct iommu_domain *iommu_paging_domain_alloc(struct device *dev)
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device

*dev,

(quoted diff) +1 -0
+						     unsigned int

flags) {

(quoted diff) +3 -0
+	const struct iommu_ops *ops;
+	struct iommu_domain *domain;
+
 	if (!dev_has_iommu(dev))
 		return ERR_PTR(-ENODEV);
Quoted 10 lines
Quoted 9 lines
(quoted diff) +0 -1
-	return __iommu_domain_alloc(dev_iommu_ops(dev), dev,

IOMMU_DOMAIN_UNMANAGED);

(quoted diff) +6 -0
+	ops = dev_iommu_ops(dev);
+
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL,

NULL);

Since this can be outside iommufd, the following comment needs update.

ACK. I will update.

-Vasant

author maintainer
#22 lore raw

PATCH v2 3/8 iommu: Add new flag to explictly request PASID capable domain

In reply to Vasant Hegde

From: Jason Gunthorpe <[email protected]>

Introduce new flag (IOMMU_HWPT_ALLOC_PASID) to domain_alloc_users() ops. If both IOMMU and device supports PASID it will allocate domain. Otherwise return error.

Also modify __iommu_group_alloc_default_domain() to call iommu_paging_domain_alloc_flags() with appropriate flag when allocating paging domain.

[Added __iommu_paging_domain_alloc_flags() and description - Vasant]
---
 drivers/iommu/iommu.c        | 45 +++++++++++++++++++++++++++---------
 include/uapi/linux/iommufd.h |  6 +++++
 2 files changed, 40 insertions(+), 11 deletions(-)
drivers/iommu/iommu.c +34 -11
index 5d1cef36f633..f3f1244fe90a 100644
@@ -32,6 +32,7 @@
 #include <trace/events/iommu.h>
 #include <linux/sched/mm.h>
 #include <linux/msi.h>
+#include <uapi/linux/iommufd.h>
 
 #include "dma-iommu.h"
 #include "iommu-priv.h"
@@ -99,6 +100,9 @@ static int __iommu_attach_device(struct iommu_domain *domain,
 				 struct device *dev);
 static int __iommu_attach_group(struct iommu_domain *domain,
 				struct iommu_group *group);
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev,
+						       unsigned int type,
+						       unsigned int flags);
 
 enum {
 	IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
@@ -1589,8 +1593,19 @@ EXPORT_SYMBOL_GPL(fsl_mc_device_group);
 static struct iommu_domain *
 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
 {
+	struct device *dev = iommu_group_first_dev(group);
+
 	if (group->default_domain && group->default_domain->type == req_type)
 		return group->default_domain;
+
+	/*
+	 * When allocating the DMA API domain assume that the driver is going to
+	 * use PASID and make sure the RID's domain is PASID compatible.
+	 */
+	if (req_type & __IOMMU_DOMAIN_PAGING)
+		return __iommu_paging_domain_alloc_flags(dev, req_type,
+				dev->iommu->max_pasids ? IOMMU_HWPT_ALLOC_PASID : 0);
+
 	return __iommu_group_domain_alloc(group, req_type);
 }
 
@@ -2026,16 +2041,9 @@ struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
 }
 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
 
-/**
- * iommu_paging_domain_alloc_flags() - Allocate a paging domain
- * @dev: device for which the domain is allocated
- * @flags: Bitmap of iommufd_hwpt_alloc_flags
- *
- * Allocate a paging domain which will be managed by a kernel driver. Return
- * allocated domain if successful, or a ERR pointer for failure.
- */
-struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
-						     unsigned int flags)
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev,
+						       unsigned int type,
+						       unsigned int flags)
 {
 	const struct iommu_ops *ops;
 	struct iommu_domain *domain;
@@ -2059,9 +2067,24 @@ struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
+
+/**
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
+ * @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
+ *
+ * Allocate a paging domain which will be managed by a kernel driver. Return
+ * allocated domain if successful, or a ERR pointer for failure.
+ */
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
+{
+	return __iommu_paging_domain_alloc_flags(dev,
+					 IOMMU_DOMAIN_UNMANAGED, flags);
+}
 EXPORT_SYMBOL_GPL(iommu_paging_domain_alloc_flags);
 
 void iommu_domain_free(struct iommu_domain *domain)
include/uapi/linux/iommufd.h +6 -0
index 4dde745cfb7e..4afcd3e2eac4 100644
@@ -359,11 +359,17 @@ struct iommu_vfio_ioas {
  *                                   enforced on device attachment
  * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation data is
  *                             valid.
+ * @IOMMU_HWPT_ALLOC_PASID: Requests a domain that can be used with PASID. The
+ *                          domain can be attached to any PASID on the device.
+ *                          Any domain attached to the non-PASID part of the
+ *                          device must also be flaged, otherwise attaching a
+ *                          PASID will blocked.
  */
 enum iommufd_hwpt_alloc_flags {
 	IOMMU_HWPT_ALLOC_NEST_PARENT = 1 << 0,
 	IOMMU_HWPT_ALLOC_DIRTY_TRACKING = 1 << 1,
 	IOMMU_HWPT_FAULT_ID_VALID = 1 << 2,
+	IOMMU_HWPT_ALLOC_PASID = 1 << 3,
 };
 
 /**
Signature · 1 line
2.31.1
maintainer
#23 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Quoted 46 lines · Vasant Hegde

From: Jason Gunthorpe<[email protected]>

Introduce new flag (IOMMU_HWPT_ALLOC_PASID) to domain_alloc_users() ops. If both IOMMU and device supports PASID it will allocate domain. Otherwise return error.

Also modify __iommu_group_alloc_default_domain() to call iommu_paging_domain_alloc_flags() with appropriate flag when allocating paging domain.

[Added __iommu_paging_domain_alloc_flags() and description - Vasant]
---
  drivers/iommu/iommu.c        | 45 +++++++++++++++++++++++++++---------
  include/uapi/linux/iommufd.h |  6 +++++
  2 files changed, 40 insertions(+), 11 deletions(-)
drivers/iommu/iommu.c +6 -0
index 5d1cef36f633..f3f1244fe90a 100644
@@ -32,6 +32,7 @@
  #include <trace/events/iommu.h>
  #include <linux/sched/mm.h>
  #include <linux/msi.h>
+#include <uapi/linux/iommufd.h>
 
  #include "dma-iommu.h"
  #include "iommu-priv.h"
@@ -99,6 +100,9 @@ static int __iommu_attach_device(struct iommu_domain *domain,
  				 struct device *dev);
  static int __iommu_attach_group(struct iommu_domain *domain,
  				struct iommu_group *group);
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev,
+						       unsigned int type,
+						       unsigned int flags);
 
  enum {
  	IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
@@ -1589,8 +1593,19 @@ EXPORT_SYMBOL_GPL(fsl_mc_device_group);
  static struct iommu_domain *
  __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
  {
+	struct device *dev = iommu_group_first_dev(group);
+

drivers/iommu/iommu.c EXPORT_SYMBOL_GPL(fsl_mc_device_group); · line 1607

Quoted 10 lines
drivers/iommu/iommu.c +8 -0
  	if (group->default_domain && group->default_domain->type == req_type)
  		return group->default_domain;
+
+	/*
+	 * When allocating the DMA API domain assume that the driver is going to
+	 * use PASID and make sure the RID's domain is PASID compatible.
+	 */
+	if (req_type & __IOMMU_DOMAIN_PAGING)
+		return __iommu_paging_domain_alloc_flags(dev, req_type,
+				dev->iommu->max_pasids ? IOMMU_HWPT_ALLOC_PASID : 0);

This changes default domain allocation paths.

Previously, the default domain was allocated by domain_alloc_paging or domain_alloc. Now, it will be allocated through domain_alloc_user, which, based on my understanding, is intended for user domain allocation.

Thanks,
baolu

author maintainer
#24 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Hi Baolu,

Quoted 59 lines · Baolu Lu
Quoted 47 lines · Vasant Hegde

From: Jason Gunthorpe<[email protected]>

Introduce new flag (IOMMU_HWPT_ALLOC_PASID) to domain_alloc_users() ops. If both IOMMU and device supports PASID it will allocate domain. Otherwise return error.

Also modify __iommu_group_alloc_default_domain() to call iommu_paging_domain_alloc_flags() with appropriate flag when allocating paging domain.

[Added __iommu_paging_domain_alloc_flags() and description - Vasant]

---
  drivers/iommu/iommu.c        | 45 +++++++++++++++++++++++++++--------- include/uapi/linux/iommufd.h |  6 +++++

  2 files changed, 40 insertions(+), 11 deletions(-)
drivers/iommu/iommu.c +4 -0
index 5d1cef36f633..f3f1244fe90a 100644
@@ -32,6 +32,7 @@
   #include <trace/events/iommu.h>
   #include <linux/sched/mm.h>
   #include <linux/msi.h>
+#include <uapi/linux/iommufd.h>
     #include "dma-iommu.h"
   #include "iommu-priv.h"
@@ -99,6 +100,9 @@ static int __iommu_attach_device(struct iommu_domain *domain,
                    struct device *dev);
   static int __iommu_attach_group(struct iommu_domain *domain,
                   struct iommu_group *group);
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device
 *dev,
+                               unsigned int type,
+                               unsigned int flags);
     enum {
       IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
@@ -1589,8 +1593,19 @@ EXPORT_SYMBOL_GPL(fsl_mc_device_group);

  static struct iommu_domain * __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) {
+    struct device *dev = iommu_group_first_dev(group);
+
      if (group->default_domain && group->default_domain->type == req_type) return group->default_domain;

Quoted 8 lines

+
+    /*
+     * When allocating the DMA API domain assume that the driver is going to +     * use PASID and make sure the RID's domain is PASID compatible. +     */
+    if (req_type & __IOMMU_DOMAIN_PAGING)
+        return __iommu_paging_domain_alloc_flags(dev, req_type, +                dev->iommu->max_pasids ? IOMMU_HWPT_ALLOC_PASID : 0);

This changes default domain allocation paths.

Quoted 4 lines

Previously, the default domain was allocated by domain_alloc_paging or domain_alloc. Now, it will be allocated through domain_alloc_user, which, based on my understanding, is intended for user domain allocation.

Right. This was discussed in during v1 [1] and Jason suggested to rename this API to domain_alloc_paging_extended() after everything settles.

That's why in cover letter I asked you and Jason whether we need any changes to intel and ARM SMM3 driver.

-Vasant

author maintainer
#25 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Quoted 63 lines · Vasant Hegde

Hi Baolu,

Quoted 59 lines · Baolu Lu
Quoted 47 lines · Vasant Hegde

From: Jason Gunthorpe<[email protected]>

Introduce new flag (IOMMU_HWPT_ALLOC_PASID) to domain_alloc_users() ops. If both IOMMU and device supports PASID it will allocate domain. Otherwise return error.

Also modify __iommu_group_alloc_default_domain() to call iommu_paging_domain_alloc_flags() with appropriate flag when allocating paging domain.

[Added __iommu_paging_domain_alloc_flags() and description - Vasant]

---
  drivers/iommu/iommu.c        | 45 +++++++++++++++++++++++++++--------- include/uapi/linux/iommufd.h |  6 +++++

  2 files changed, 40 insertions(+), 11 deletions(-)
drivers/iommu/iommu.c +4 -0
index 5d1cef36f633..f3f1244fe90a 100644
@@ -32,6 +32,7 @@
   #include <trace/events/iommu.h>
   #include <linux/sched/mm.h>
   #include <linux/msi.h>
+#include <uapi/linux/iommufd.h>
     #include "dma-iommu.h"
   #include "iommu-priv.h"
@@ -99,6 +100,9 @@ static int __iommu_attach_device(struct iommu_domain *domain,
                    struct device *dev);
   static int __iommu_attach_group(struct iommu_domain *domain,
                   struct iommu_group *group);
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device
 *dev,
+                               unsigned int type,
+                               unsigned int flags);
     enum {
       IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
@@ -1589,8 +1593,19 @@ EXPORT_SYMBOL_GPL(fsl_mc_device_group);

  static struct iommu_domain * __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) {
+    struct device *dev = iommu_group_first_dev(group);
+
      if (group->default_domain && group->default_domain->type == req_type) return group->default_domain;

Quoted 8 lines

+
+    /*
+     * When allocating the DMA API domain assume that the driver is going to +     * use PASID and make sure the RID's domain is PASID compatible. +     */
+    if (req_type & __IOMMU_DOMAIN_PAGING)
+        return __iommu_paging_domain_alloc_flags(dev, req_type, +                dev->iommu->max_pasids ? IOMMU_HWPT_ALLOC_PASID : 0);

This changes default domain allocation paths.

Quoted 7 lines
Quoted 4 lines

Previously, the default domain was allocated by domain_alloc_paging or domain_alloc. Now, it will be allocated through domain_alloc_user, which, based on my understanding, is intended for user domain allocation.

Right. This was discussed in during v1 [1] and Jason suggested to rename this API to domain_alloc_paging_extended() after everything settles.

Sorry. Missed to add the link

[1] https://lore.kernel.org/linux-iommu/[email protected]/

-Vasant

#26 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Quoted 11 lines · Vasant Hegde
Quoted 7 lines
Quoted 4 lines

Previously, the default domain was allocated by domain_alloc_paging or domain_alloc. Now, it will be allocated through domain_alloc_user, which, based on my understanding, is intended for user domain allocation.

Right. This was discussed in during v1 [1] and Jason suggested to rename this API to domain_alloc_paging_extended() after everything settles.

Sorry. Missed to add the link

[1] https://lore.kernel.org/linux-iommu/[email protected]/

Yeah, I think this is the right way to go. We just misnamed the _user op, lets correct it.

We end up with a simple op that most drivers use and then the more complex op that iommufd capable drivers will implement.

We work to merge things so drivers only implement one of the two ops.

Jason

#27 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Hi Jason,

Quoted 10 lines · On Wed, 2 Oct 2024 11:23:31 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 9 lines · Vasant Hegde
Quoted 8 lines
Quoted 4 lines

Previously, the default domain was allocated by domain_alloc_paging or domain_alloc. Now, it will be allocated through domain_alloc_user, which, based on my understanding, is intended for user domain allocation.

Right. This was discussed in during v1 [1] and Jason suggested to rename this API to domain_alloc_paging_extended() after everything settles.

Quoted 5 lines
Quoted 5 lines

Sorry. Missed to add the link

[1]
https://lore.kernel.org/linux-iommu/[email protected]/

Slightly off topic, in [1], you mentioned the following flow for iommu_group_alloc_default_domain()
{
...

	if (req_type || iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY)
{ dom = iommu_group_alloc_identity_domain();
		if (req_type || !IS_ERR(dom))
			return dom;
		/*
		 * if iommu_def_domain_type == IDENTITY fails then fall
through
		 * to PAGING
		 */
	}

If we were to have a vIOMMU that only supports "identity" and "sva" domains, there would be no need to fall back to PAGING. I don't see any issues if we take care of vendor driver iommu_ops accordingly. But wanted to see if I missed anything.

Quoted 9 lines

Yeah, I think this is the right way to go. We just misnamed the _user op, lets correct it.

We end up with a simple op that most drivers use and then the more complex op that iommufd capable drivers will implement.

We work to merge things so drivers only implement one of the two ops.

Jason

A message that is not in the archive (sent off-list or not yet received): [email protected]

#29 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Quoted 15 lines

Slightly off topic, in [1], you mentioned the following flow for iommu_group_alloc_default_domain()
{
...

	if (req_type || iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY)
{ dom = iommu_group_alloc_identity_domain();
		if (req_type || !IS_ERR(dom))
			return dom;
		/*
		 * if iommu_def_domain_type == IDENTITY fails then fall
through
		 * to PAGING
		 */
	}
Quoted 2 lines

If we were to have a vIOMMU that only supports "identity" and "sva" domains, there would be no need to fall back to PAGING.

That's illegal. If you support SVA the driver can and should support PAGING. I don't want to see SVA being special.

There must always be a DMA API domain of some kind, if identity doesn't work then paging must, otherwise the device is unusable.

Jason

#30 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Hi Jason,

Quoted 15 lines · On Wed, 2 Oct 2024 16:07:05 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 15 lines

Slightly off topic, in [1], you mentioned the following flow for iommu_group_alloc_default_domain()
{
...

	if (req_type || iommu_def_domain_type ==
IOMMU_DOMAIN_IDENTITY) { dom = iommu_group_alloc_identity_domain();
		if (req_type || !IS_ERR(dom))
			return dom;
		/*
		 * if iommu_def_domain_type == IDENTITY fails then
fall through
		 * to PAGING
		 */
	}
Quoted 9 lines
Quoted 2 lines

If we were to have a vIOMMU that only supports "identity" and "sva" domains, there would be no need to fall back to PAGING.

That's illegal. If you support SVA the driver can and should support PAGING. I don't want to see SVA being special.

There must always be a DMA API domain of some kind, if identity doesn't work then paging must, otherwise the device is unusable.

I also think that the PAGING domain is an integral part of any IOMMU driver and must be included. However, from a use case point of view, we can already have a configuration where, under a single IOMMU, some devices use SVA while others (e.g., trusted devices) use the identity domain.

My question is more about the incremental development of the pvIOMMU guest driver. Since the PAGING domain requires more infrastructure than SVA (e.g. may leverage your Generic Page Table work for constructing S1; please correct me if that is not the intended usage), would it make sense to add the PAGING domain after SVA and identity domain support?"

Thanks,

Jacob

Reviewed-by on v2 3/8
#31 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

In reply to Vasant Hegde
Quoted 18 lines · Vasant Hegde
drivers/iommu/iommu.c +8 -1
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev,
+						       unsigned int type,
+						       unsigned int flags)
 {
 	const struct iommu_ops *ops;
 	struct iommu_domain *domain;
@@ -2059,9 +2067,24 @@ struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
+
+/**
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
+ * @dev: device for which the domain is allocated

drivers/iommu/iommu.c struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, · line 2086

Quoted 10 lines
drivers/iommu/iommu.c +10 -0
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
+ *
+ * Allocate a paging domain which will be managed by a kernel driver. Return
+ * allocated domain if successful, or a ERR pointer for failure.
+ */
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
+{
+	return __iommu_paging_domain_alloc_flags(dev,
+					 IOMMU_DOMAIN_UNMANAGED, flags);

Can we get rid of the type parameter here? Like this:

       if (req_type & __IOMMU_DOMAIN_PAGING) {
               dom =  __iommu_paging_domain_alloc_flags(dev,
                               dev->iommu->max_pasids ?
			       IOMMU_HWPT_ALLOC_PASID : 0);
		if (IS_ERR(dom))
		   return dom;
		dom->type = req_type;
       }

Maybe at the end of the series, I guess you want AMD to not have a bisection issue?

I think this looks OK otherwise

Jason

author maintainer
#32 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Jason,

Quoted 23 lines · Jason Gunthorpe
Quoted 22 lines · Vasant Hegde
drivers/iommu/iommu.c +12 -1
+static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev,
+						       unsigned int type,
+						       unsigned int flags)
 {
 	const struct iommu_ops *ops;
 	struct iommu_domain *domain;
@@ -2059,9 +2067,24 @@ struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	iommu_domain_init(domain, IOMMU_DOMAIN_UNMANAGED, ops);
+	iommu_domain_init(domain, type, ops);
 	return domain;
 }
+
+/**
+ * iommu_paging_domain_alloc_flags() - Allocate a paging domain
+ * @dev: device for which the domain is allocated
+ * @flags: Bitmap of iommufd_hwpt_alloc_flags
+ *
+ * Allocate a paging domain which will be managed by a kernel driver. Return
+ * allocated domain if successful, or a ERR pointer for failure.
Quoted 8 lines

drivers/iommu/iommu.c struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, · line 2086

Quoted 6 lines
drivers/iommu/iommu.c +6 -0
+ */
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
+{
+	return __iommu_paging_domain_alloc_flags(dev,
+					 IOMMU_DOMAIN_UNMANAGED, flags);

Can we get rid of the type parameter here? Like this:

This will create original problem for AMD driver. i. e. for VFIO we want to allocate V1 page table (at least for now).

Quoted 10 lines
       if (req_type & __IOMMU_DOMAIN_PAGING) {
               dom =  __iommu_paging_domain_alloc_flags(dev,
                               dev->iommu->max_pasids ?
			       IOMMU_HWPT_ALLOC_PASID : 0);
		if (IS_ERR(dom))
		   return dom;
		dom->type = req_type;
       }
Quoted 6 lines

Maybe at the end of the series, I guess you want AMD to not have a bisection issue?

I think this looks OK otherwise

Thanks

-Vasant

#33 lore raw

Re: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

Quoted 10 lines · Vasant Hegde
Quoted 7 lines

drivers/iommu/iommu.c struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev, · line 2086

Quoted 5 lines
drivers/iommu/iommu.c +5 -0
+struct iommu_domain *iommu_paging_domain_alloc_flags(struct device *dev,
+						     unsigned int flags)
+{
+	return __iommu_paging_domain_alloc_flags(dev,
+					 IOMMU_DOMAIN_UNMANAGED, flags);

Can we get rid of the type parameter here? Like this:

This will create original problem for AMD driver. i. e. for VFIO we want to allocate V1 page table (at least for now).

At this point yes, but at the end of the series no, as the page table type decision should only be based on IOMMU_HWPT_ALLOC_PASID

And this is why I wonder if it really matters for bisection purposes as both options will be functionally fine.

Jason

maintainer Reviewed-by on v2 3/8
#34 lore raw

RE: [PATCH v2 3/8] iommu: Add new flag to explictly request PASID capable domain

In reply to Vasant Hegde
Quoted 10 lines

From: Vasant Hegde <[email protected]>
Sent: Wednesday, September 11, 2024 6:19 PM

(quoted diff) +0 -0
@@ -359,11 +359,17 @@ struct iommu_vfio_ioas {
  *                                   enforced on device attachment
  * @IOMMU_HWPT_FAULT_ID_VALID: The fault_id field of hwpt allocation

data is
* valid.
+ * @IOMMU_HWPT_ALLOC_PASID: Requests a domain that can be used with PASID. The

Quoted 5 lines
+ *                          domain can be attached to any PASID on the device.
+ *                          Any domain attached to the non-PASID part of the
+ *                          device must also be flaged, otherwise attaching a
+ *                          PASID will blocked.

*/

The last part is not very clear. Probably you meant that even if a domain is intended to attach to RID first, it should set the flag if it may be attached to PASID later?

Otherwise,

author maintainer
#35 lore raw

PATCH v2 4/8 iommu/amd: Separate page table setup from domain allocation

In reply to Vasant Hegde

Currently protection_domain_alloc() allocates domain and also sets up page table. Page table setup is required for PAGING domain only. Domain type like SVA doesn't need page table. Hence move page table setup code to separate function.

Also SVA domain allocation path does not call pdom_setup_pgtable(). Hence remove IOMMU_DOMAIN_SVA type check.

---
 drivers/iommu/amd/iommu.c | 42 ++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 16 deletions(-)
drivers/iommu/amd/iommu.c +26 -16
index 2d0a681a2052..6844040b3702 100644
@@ -2264,28 +2264,36 @@ void protection_domain_free(struct protection_domain *domain)
 
 struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 {
-	struct io_pgtable_ops *pgtbl_ops;
 	struct protection_domain *domain;
-	int pgtable;
 
 	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
 	if (!domain)
 		return NULL;
 
 	domain->id = domain_id_alloc();
-	if (!domain->id)
-		goto err_free;
+	if (!domain->id) {
+		kfree(domain);
+		return NULL;
+	}
 
 	spin_lock_init(&domain->lock);
 	INIT_LIST_HEAD(&domain->dev_list);
 	INIT_LIST_HEAD(&domain->dev_data_list);
 	domain->iop.pgtbl.cfg.amd.nid = nid;
 
+	return domain;
+}
+
+static int pdom_setup_pgtable(struct protection_domain *domain,
+			      unsigned int type)
+{
+	struct io_pgtable_ops *pgtbl_ops;
+	int pgtable;
+
 	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
 	case IOMMU_DOMAIN_IDENTITY:
-	case IOMMU_DOMAIN_SVA:
-		return domain;
+		return 0;
 	case IOMMU_DOMAIN_DMA:
 		pgtable = amd_iommu_pgtable;
 		break;
@@ -2297,7 +2305,7 @@ struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 		pgtable = AMD_IOMMU_V1;
 		break;
 	default:
-		goto err_id;
+		return -EINVAL;
 	}
 
 	switch (pgtable) {
@@ -2308,20 +2316,14 @@ struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 		domain->pd_mode = PD_MODE_V2;
 		break;
 	default:
-		goto err_id;
+		return -EINVAL;
 	}
-
 	pgtbl_ops =
 		alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl.cfg, domain);
 	if (!pgtbl_ops)
-		goto err_id;
+		return -ENOMEM;
 
-	return domain;
-err_id:
-	domain_id_free(domain->id);
-err_free:
-	kfree(domain);
-	return NULL;
+	return 0;
 }
 
 static inline u64 dma_max_address(void)
@@ -2344,6 +2346,7 @@ static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
 	bool dirty_tracking = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
 	struct protection_domain *domain;
 	struct amd_iommu *iommu = NULL;
+	int ret;
 
 	if (dev)
 		iommu = get_amd_iommu_from_dev(dev);
@@ -2363,6 +2366,13 @@ static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
+	ret = pdom_setup_pgtable(domain, type);
+	if (ret) {
+		domain_id_free(domain->id);
+		kfree(domain);
+		return ERR_PTR(ret);
+	}
+
 	domain->domain.geometry.aperture_start = 0;
 	domain->domain.geometry.aperture_end   = dma_max_address();
 	domain->domain.geometry.force_aperture = true;
Signature · 1 line
2.31.1
Reviewed-by on v2 4/8
#36 lore raw

Re: [PATCH v2 4/8] iommu/amd: Separate page table setup from domain allocation

Hi Vasant,

Quoted 106 lines · On Wed, 11 Sep 2024 10:19:07 +0000 Vasant Hegde <[email protected]> wrote:

Currently protection_domain_alloc() allocates domain and also sets up page table. Page table setup is required for PAGING domain only. Domain type like SVA doesn't need page table. Hence move page table setup code to separate function.

Also SVA domain allocation path does not call pdom_setup_pgtable(). Hence remove IOMMU_DOMAIN_SVA type check.

---
 drivers/iommu/amd/iommu.c | 42

++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-)

drivers/iommu/amd/iommu.c +19 -16
index 2d0a681a2052..6844040b3702 100644
@@ -2264,28 +2264,36 @@ void protection_domain_free(struct
 protection_domain *domain) 
 struct protection_domain *protection_domain_alloc(unsigned int type,
 int nid) {
-	struct io_pgtable_ops *pgtbl_ops;
 	struct protection_domain *domain;
-	int pgtable;
 
 	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
 	if (!domain)
 		return NULL;
 
 	domain->id = domain_id_alloc();
-	if (!domain->id)
-		goto err_free;
+	if (!domain->id) {
+		kfree(domain);
+		return NULL;
+	}
 
 	spin_lock_init(&domain->lock);
 	INIT_LIST_HEAD(&domain->dev_list);
 	INIT_LIST_HEAD(&domain->dev_data_list);
 	domain->iop.pgtbl.cfg.amd.nid = nid;
 
+	return domain;
+}
+
+static int pdom_setup_pgtable(struct protection_domain *domain,
+			      unsigned int type)
+{
+	struct io_pgtable_ops *pgtbl_ops;
+	int pgtable;
+
 	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
 	case IOMMU_DOMAIN_IDENTITY:
-	case IOMMU_DOMAIN_SVA:
-		return domain;
+		return 0;
 	case IOMMU_DOMAIN_DMA:
 		pgtable = amd_iommu_pgtable;
 		break;
@@ -2297,7 +2305,7 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) pgtable =
 AMD_IOMMU_V1; break;
 	default:
-		goto err_id;
+		return -EINVAL;
 	}
 
 	switch (pgtable) {
@@ -2308,20 +2316,14 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) domain->pd_mode
 = PD_MODE_V2; break;
 	default:
-		goto err_id;
+		return -EINVAL;
 	}
-
 	pgtbl_ops =
 		alloc_io_pgtable_ops(pgtable,
 &domain->iop.pgtbl.cfg, domain); if (!pgtbl_ops)
-		goto err_id;
+		return -ENOMEM;
 
-	return domain;
-err_id:
-	domain_id_free(domain->id);
-err_free:
-	kfree(domain);
-	return NULL;
+	return 0;
 }
 
 static inline u64 dma_max_address(void)
@@ -2344,6 +2346,7 @@ static struct iommu_domain
 *do_iommu_domain_alloc(unsigned int type, bool dirty_tracking = flags
 & IOMMU_HWPT_ALLOC_DIRTY_TRACKING; struct protection_domain *domain;
 	struct amd_iommu *iommu = NULL;
+	int ret;
 
 	if (dev)
 		iommu = get_amd_iommu_from_dev(dev);
@@ -2363,6 +2366,13 @@ static struct iommu_domain
 *do_iommu_domain_alloc(unsigned int type, if (!domain)
 		return ERR_PTR(-ENOMEM);
 

drivers/iommu/amd/iommu.c static struct iommu_domain · line 2378

Quoted 10 lines
drivers/iommu/amd/iommu.c +7 -0
+	ret = pdom_setup_pgtable(domain, type);
+	if (ret) {
+		domain_id_free(domain->id);
+		kfree(domain);
+		return ERR_PTR(ret);
+	}
+
 	domain->domain.geometry.aperture_start = 0;
 	domain->domain.geometry.aperture_end   = dma_max_address();
 	domain->domain.geometry.force_aperture = true;
Reviewed-by on v2 4/8
#37 lore raw

Re: [PATCH v2 4/8] iommu/amd: Separate page table setup from domain allocation

In reply to Vasant Hegde
Quoted 5 lines · Vasant Hegde

Currently protection_domain_alloc() allocates domain and also sets up page table. Page table setup is required for PAGING domain only. Domain type like SVA doesn't need page table. Hence move page table setup code to separate function.

Quoted 7 lines

Also SVA domain allocation path does not call pdom_setup_pgtable(). Hence remove IOMMU_DOMAIN_SVA type check.

---
 drivers/iommu/amd/iommu.c | 42 ++++++++++++++++++++++++---------------
 1 file changed, 26 insertions(+), 16 deletions(-)

Jason

author maintainer
#38 lore raw

PATCH v2 5/8 iommu/amd: Pass page table type as param to pdom_setup_pgtable()

In reply to Vasant Hegde

Current code forces v1 page table for UNMANAGED domain and global page table type (amd_iommu_pgtable) for rest of paging domain.

Following patch series adds support for domain_alloc_paging() ops. Also enhances domain_alloc_user() to allocate page table based on 'flags.

Hence pass page table type as parameter to pdomain_setup_pgtable(). So that caller can decide right page table type. Also update dma_max_address() to take pgtable as parameter.

---
 drivers/iommu/amd/iommu.c | 43 +++++++++++++++++----------------------
 1 file changed, 19 insertions(+), 24 deletions(-)
drivers/iommu/amd/iommu.c +19 -24
index 6844040b3702..cfeb3202ceee 100644
@@ -2285,28 +2285,13 @@ struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 }
 
 static int pdom_setup_pgtable(struct protection_domain *domain,
-			      unsigned int type)
+			      unsigned int type, int pgtable)
 {
 	struct io_pgtable_ops *pgtbl_ops;
-	int pgtable;
 
-	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
-	case IOMMU_DOMAIN_IDENTITY:
+	if (!(type & __IOMMU_DOMAIN_PAGING))
 		return 0;
-	case IOMMU_DOMAIN_DMA:
-		pgtable = amd_iommu_pgtable;
-		break;
-	/*
-	 * Force IOMMU v1 page table when allocating
-	 * domain for pass-through devices.
-	 */
-	case IOMMU_DOMAIN_UNMANAGED:
-		pgtable = AMD_IOMMU_V1;
-		break;
-	default:
-		return -EINVAL;
-	}
 
 	switch (pgtable) {
 	case AMD_IOMMU_V1:
@@ -2318,6 +2303,7 @@ static int pdom_setup_pgtable(struct protection_domain *domain,
 	default:
 		return -EINVAL;
 	}
+
 	pgtbl_ops =
 		alloc_io_pgtable_ops(pgtable, &domain->iop.pgtbl.cfg, domain);
 	if (!pgtbl_ops)
@@ -2326,9 +2312,9 @@ static int pdom_setup_pgtable(struct protection_domain *domain,
 	return 0;
 }
 
-static inline u64 dma_max_address(void)
+static inline u64 dma_max_address(int pgtable)
 {
-	if (amd_iommu_pgtable == AMD_IOMMU_V1)
+	if (pgtable == AMD_IOMMU_V1)
 		return ~0ULL;
 
 	/* V2 with 4/5 level page table */
@@ -2341,7 +2327,8 @@ static bool amd_iommu_hd_support(struct amd_iommu *iommu)
 }
 
 static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
-						  struct device *dev, u32 flags)
+						  struct device *dev,
+						  u32 flags, int pgtable)
 {
 	bool dirty_tracking = flags & IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
 	struct protection_domain *domain;
@@ -2366,7 +2353,7 @@ static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
 	if (!domain)
 		return ERR_PTR(-ENOMEM);
 
-	ret = pdom_setup_pgtable(domain, type);
+	ret = pdom_setup_pgtable(domain, type, pgtable);
 	if (ret) {
 		domain_id_free(domain->id);
 		kfree(domain);
@@ -2374,7 +2361,7 @@ static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
 	}
 
 	domain->domain.geometry.aperture_start = 0;
-	domain->domain.geometry.aperture_end   = dma_max_address();
+	domain->domain.geometry.aperture_end   = dma_max_address(pgtable);
 	domain->domain.geometry.force_aperture = true;
 	domain->domain.pgsize_bitmap = domain->iop.pgtbl.cfg.pgsize_bitmap;
 
@@ -2392,8 +2379,16 @@ static struct iommu_domain *do_iommu_domain_alloc(unsigned int type,
 static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type)
 {
 	struct iommu_domain *domain;
+	int pgtable = amd_iommu_pgtable;
+
+	/*
+	 * Force IOMMU v1 page table when allocating
+	 * domain for pass-through devices.
+	 */
+	if (type == IOMMU_DOMAIN_UNMANAGED)
+		pgtable = AMD_IOMMU_V1;
 
-	domain = do_iommu_domain_alloc(type, NULL, 0);
+	domain = do_iommu_domain_alloc(type, NULL, 0, pgtable);
 	if (IS_ERR(domain))
 		return NULL;
 
@@ -2411,7 +2406,7 @@ amd_iommu_domain_alloc_user(struct device *dev, u32 flags,
 	if ((flags & ~IOMMU_HWPT_ALLOC_DIRTY_TRACKING) || parent || user_data)
 		return ERR_PTR(-EOPNOTSUPP);
 
-	return do_iommu_domain_alloc(type, dev, flags);
+	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
 }
 
 void amd_iommu_domain_free(struct iommu_domain *dom)
Signature · 1 line
2.31.1
Reviewed-by on v2 5/8
#39 lore raw

Re: [PATCH v2 5/8] iommu/amd: Pass page table type as param to pdom_setup_pgtable()

Hi Vasant,

Quoted 7 lines · On Wed, 11 Sep 2024 10:19:08 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/amd/iommu.c +0 -1
@@ -2285,28 +2285,13 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) }
 
 static int pdom_setup_pgtable(struct protection_domain *domain,
-			      unsigned int type)

drivers/iommu/amd/iommu.c struct protection_domain · line 2294

Quoted 10 lines
drivers/iommu/amd/iommu.c +2 -3
+			      unsigned int type, int pgtable)
 {
 	struct io_pgtable_ops *pgtbl_ops;
-	int pgtable;
 
-	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
-	case IOMMU_DOMAIN_IDENTITY:
+	if (!(type & __IOMMU_DOMAIN_PAGING))
 		return 0;

Now that you pass in the pgtable format explicitly, I think you can delete 'type' parameter and move this check to do_iommu_domain_alloc(). As you mentioned in [4/8], pdom_setup_pgtable() is factored out for setting up page table only.

Other than that,

author maintainer
#40 lore raw

Re: [PATCH v2 5/8] iommu/amd: Pass page table type as param to pdom_setup_pgtable()

Jacob,

Quoted 17 lines · Jacob Pan

Hi Vasant,

Quoted 12 lines · On Wed, 11 Sep 2024 10:19:08 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/amd/iommu.c +1 -2
@@ -2285,28 +2285,13 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) }
 
 static int pdom_setup_pgtable(struct protection_domain *domain,
-			      unsigned int type)
+			      unsigned int type, int pgtable)
 {
 	struct io_pgtable_ops *pgtbl_ops;
-	int pgtable;
 
Quoted 9 lines

drivers/iommu/amd/iommu.c struct protection_domain *protection_domain_alloc(unsigned int type, int nid) · line 2294

Quoted 5 lines
drivers/iommu/amd/iommu.c +1 -2
-	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
-	case IOMMU_DOMAIN_IDENTITY:
+	if (!(type & __IOMMU_DOMAIN_PAGING))
 		return 0;

Now that you pass in the pgtable format explicitly, I think you can delete 'type' parameter and move this check to do_iommu_domain_alloc(). As you mentioned in [4/8], pdom_setup_pgtable() is factored out for setting up page table only.

I could have added check in do_iommu_domain_alloc(). As just kept it as is because eventually we can just remove this check. (Once I fix SVA domain and after having global static identity domain).

-Vasant

#41 lore raw

Re: [PATCH v2 5/8] iommu/amd: Pass page table type as param to pdom_setup_pgtable()

Hi Vasant,

Quoted 33 lines · On Thu, 26 Sep 2024 15:55:14 +0530 Vasant Hegde <[email protected]> wrote:

Jacob,

Quoted 17 lines · Jacob Pan

Hi Vasant,

Quoted 12 lines · On Wed, 11 Sep 2024 10:19:08 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/amd/iommu.c +1 -2
@@ -2285,28 +2285,13 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) }
 
 static int pdom_setup_pgtable(struct protection_domain *domain,
-			      unsigned int type)
+			      unsigned int type, int pgtable)
 {
 	struct io_pgtable_ops *pgtbl_ops;
-	int pgtable;
 
Quoted 11 lines

drivers/iommu/amd/iommu.c struct protection_domain *protection_domain_alloc(unsigned int type, int nid) · line 2294

Quoted 6 lines
drivers/iommu/amd/iommu.c +1 -2
-	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode
 */
-	case IOMMU_DOMAIN_IDENTITY:
+	if (!(type & __IOMMU_DOMAIN_PAGING))
 		return 0;  

Now that you pass in the pgtable format explicitly, I think you can delete 'type' parameter and move this check to do_iommu_domain_alloc(). As you mentioned in [4/8], pdom_setup_pgtable() is factored out for setting up page table only.

Quoted 4 lines

I could have added check in do_iommu_domain_alloc(). As just kept it as is because eventually we can just remove this check. (Once I fix SVA domain and after having global static identity domain).

sounds reasonable me.

Thanks,

Jacob

A message that is not in the archive (sent off-list or not yet received): [email protected]

#43 lore raw

Re: [PATCH v2 5/8] iommu/amd: Pass page table type as param to pdom_setup_pgtable()

Quoted 17 lines · Jacob Pan

Hi Vasant,

Quoted 12 lines · On Wed, 11 Sep 2024 10:19:08 +0000 Vasant Hegde <[email protected]> wrote:
drivers/iommu/amd/iommu.c +1 -2
@@ -2285,28 +2285,13 @@ struct protection_domain
 *protection_domain_alloc(unsigned int type, int nid) }
 
 static int pdom_setup_pgtable(struct protection_domain *domain,
-			      unsigned int type)
+			      unsigned int type, int pgtable)
 {
 	struct io_pgtable_ops *pgtbl_ops;
-	int pgtable;
 
Quoted 9 lines

drivers/iommu/amd/iommu.c struct protection_domain *protection_domain_alloc(unsigned int type, int nid) · line 2294

Quoted 5 lines
drivers/iommu/amd/iommu.c +1 -2
-	switch (type) {
 	/* No need to allocate io pgtable ops in passthrough mode */
-	case IOMMU_DOMAIN_IDENTITY:
+	if (!(type & __IOMMU_DOMAIN_PAGING))
 		return 0;

Now that you pass in the pgtable format explicitly, I think you can delete 'type' parameter and move this check to do_iommu_domain_alloc(). As you mentioned in [4/8], pdom_setup_pgtable() is factored out for setting up page table only.

It would be nice but I think the type is stil encoding the DMA API flag at this point. Adjusting that could be moved to where the cookie code ended up

Jason

author maintainer
#44 lore raw

PATCH v2 6/8 iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

In reply to Vasant Hegde

Allocate domain with v2 page table if IOMMU_HWPT_ALLOC_PASID flag is passed to ops->domain_alloc_user().

---
 drivers/iommu/amd/iommu.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +12 -1
index cfeb3202ceee..1c9e539f15a7 100644
@@ -2401,12 +2401,23 @@ amd_iommu_domain_alloc_user(struct device *dev, u32 flags,
 			    const struct iommu_user_data *user_data)
 
 {
+	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 	unsigned int type = IOMMU_DOMAIN_UNMANAGED;
+	int pgtable = AMD_IOMMU_V1;
 
 	if ((flags & ~IOMMU_HWPT_ALLOC_DIRTY_TRACKING) || parent || user_data)
 		return ERR_PTR(-EOPNOTSUPP);
 
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);
+
+		pgtable = AMD_IOMMU_V2;
+	}
+
+	return do_iommu_domain_alloc(type, dev, flags, pgtable);
 }
 
 void amd_iommu_domain_free(struct iommu_domain *dom)
Signature · 1 line
2.31.1
#45 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 18 lines · Vasant Hegde

Allocate domain with v2 page table if IOMMU_HWPT_ALLOC_PASID flag is passed to ops->domain_alloc_user().

---
 drivers/iommu/amd/iommu.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +1 -0
index cfeb3202ceee..1c9e539f15a7 100644
@@ -2401,12 +2401,23 @@ amd_iommu_domain_alloc_user(struct device *dev, u32 flags,
 			    const struct iommu_user_data *user_data)
 
 {
+	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 	unsigned int type = IOMMU_DOMAIN_UNMANAGED;

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2414

Quoted 10 lines
drivers/iommu/amd/iommu.c +5 -1
+	int pgtable = AMD_IOMMU_V1;
 
 	if ((flags & ~IOMMU_HWPT_ALLOC_DIRTY_TRACKING) || parent || user_data)
 		return ERR_PTR(-EOPNOTSUPP);
 
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))

The test to disable v2 support uses:

	if (!check_feature(FEATURE_GIOSUP) ||
	    !check_feature(FEATURE_GT)) {

But this doesn't check FEATURE_GIOSUP, just GT? Is it OK?

Jason

author maintainer
#46 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Jason,

Quoted 32 lines · Jason Gunthorpe
Quoted 18 lines · Vasant Hegde

Allocate domain with v2 page table if IOMMU_HWPT_ALLOC_PASID flag is passed to ops->domain_alloc_user().

---
 drivers/iommu/amd/iommu.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +1 -0
index cfeb3202ceee..1c9e539f15a7 100644
@@ -2401,12 +2401,23 @@ amd_iommu_domain_alloc_user(struct device *dev, u32 flags,
 			    const struct iommu_user_data *user_data)
 
 {
+	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 	unsigned int type = IOMMU_DOMAIN_UNMANAGED;

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2414

Quoted 10 lines
drivers/iommu/amd/iommu.c +5 -1
+	int pgtable = AMD_IOMMU_V1;
 
 	if ((flags & ~IOMMU_HWPT_ALLOC_DIRTY_TRACKING) || parent || user_data)
 		return ERR_PTR(-EOPNOTSUPP);
 
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))

The test to disable v2 support uses:

Quoted 4 lines
	if (!check_feature(FEATURE_GIOSUP) ||
	    !check_feature(FEATURE_GT)) {

But this doesn't check FEATURE_GIOSUP, just GT? Is it OK?

Ah! You are right. To support V2 page table, yes. Will fix it.

-Vasant

#47 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 8 lines · Vasant Hegde
Quoted 8 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2414

Quoted 5 lines
drivers/iommu/amd/iommu.c +4 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))

The test to disable v2 support uses:

Quoted 6 lines
Quoted 4 lines
	if (!check_feature(FEATURE_GIOSUP) ||
	    !check_feature(FEATURE_GT)) {

But this doesn't check FEATURE_GIOSUP, just GT? Is it OK?

Ah! You are right. To support V2 page table, yes. Will fix it.

I think you should eventually define more useful feature flags:

FEATURE_AMD_PGTBL_V1
FEATURE_AMD_PGTBL_V2
FEATURE_AMD_PASID

etc

GIOSUP is too narrow

Then all the figuring out can be done once in the init code. There is also some CC stuff changing this as well :\

Jason

author maintainer
#48 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Jason,

Quoted 22 lines · Jason Gunthorpe
Quoted 8 lines · Vasant Hegde
Quoted 8 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2414

Quoted 5 lines
drivers/iommu/amd/iommu.c +4 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))

The test to disable v2 support uses:

Quoted 6 lines
Quoted 4 lines
	if (!check_feature(FEATURE_GIOSUP) ||
	    !check_feature(FEATURE_GT)) {

But this doesn't check FEATURE_GIOSUP, just GT? Is it OK?

Ah! You are right. To support V2 page table, yes. Will fix it.

I think you should eventually define more useful feature flags:

FEATURE_AMD_PGTBL_V1
FEATURE_AMD_PGTBL_V2
FEATURE_AMD_PASID

Quoted 6 lines

etc

GIOSUP is too narrow

Then all the figuring out can be done once in the init code. There is also some CC stuff changing this as well :\

Right. I have few things to improve in page select stuff. I will try to cover these things as well.

-Vasant

maintainer
#49 lore raw

RE: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

In reply to Vasant Hegde
Quoted 17 lines

From: Vasant Hegde <[email protected]>
Sent: Wednesday, September 11, 2024 6:19 PM

Allocate domain with v2 page table if IOMMU_HWPT_ALLOC_PASID flag is passed to ops->domain_alloc_user().

---
 drivers/iommu/amd/iommu.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +0 -0
index cfeb3202ceee..1c9e539f15a7 100644
@@ -2401,12 +2401,23 @@ amd_iommu_domain_alloc_user(struct device
 *dev, u32 flags,

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device · line 2411

Quoted 10 lines
drivers/iommu/amd/iommu.c +2 -0
 			    const struct iommu_user_data *user_data)
 
 {
+	struct iommu_dev_data *dev_data = dev_iommu_priv_get(dev);
 	unsigned int type = IOMMU_DOMAIN_UNMANAGED;
+	int pgtable = AMD_IOMMU_V1;
 
 	if ((flags & ~IOMMU_HWPT_ALLOC_DIRTY_TRACKING) || parent ||
 user_data)
 		return ERR_PTR(-EOPNOTSUPP);

Hmm is this series actually tested? Above check rejects the new PASID flag...

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 7 lines
drivers/iommu/amd/iommu.c +5 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

IIUC the v2 format can be attached to any RID.

Quoted 10 lines
drivers/iommu/amd/iommu.c +5 -0
+
+		pgtable = AMD_IOMMU_V2;
+	}
+
+	return do_iommu_domain_alloc(type, dev, flags, pgtable);
 }
 

void amd_iommu_domain_free(struct iommu_domain *dom)
--
2.31.1

#50 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 10 lines · Kevin

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 6 lines
drivers/iommu/amd/iommu.c +5 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

The point of this series is to allow AMD to select the v1 format as often as possible because v1 has better performance than v2

Jason

maintainer
#51 lore raw

RE: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 15, 2024 8:40 PM

Quoted 10 lines · Kevin

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 6 lines
drivers/iommu/amd/iommu.c +5 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

Quoted 3 lines

The point of this series is to allow AMD to select the v1 format as often as possible because v1 has better performance than v2

If the user wants better performance then it should not set the flag.

Not sure the kernel driver should enforce a policy preferring performance over an user opt which is compatible to any RID...

#52 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 16 lines · Kevin
Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 15, 2024 8:40 PM

Quoted 10 lines · Kevin

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 6 lines
drivers/iommu/amd/iommu.c +5 -1
-	return do_iommu_domain_alloc(type, dev, flags, AMD_IOMMU_V1);
+	/* Allocate v2 page table if IOMMU and device supports PASID. */
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

Quoted 4 lines
Quoted 2 lines

The point of this series is to allow AMD to select the v1 format as often as possible because v1 has better performance than v2

If the user wants better performance then it should not set the flag.

Well, I do tend to agree with that, you mean remove the pdev_pasid_supported() ?

Jason

maintainer
#53 lore raw

RE: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Quoted 22 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, October 16, 2024 11:29 PM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 15, 2024 8:40 PM

Quoted 1 line · Kevin

Review

Quoted 1 line
(quoted diff) +0 -1
-	return do_iommu_domain_alloc(type, dev, flags,

AMD_IOMMU_V1);

Quoted 1 line
Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	/* Allocate v2 page table if IOMMU and device supports

PASID. */

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 4 lines
drivers/iommu/amd/iommu.c +4 -0
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The point of this series is to allow AMD to select the v1 format as often as possible because v1 has better performance than v2

If the user wants better performance then it should not set the flag.

Well, I do tend to agree with that, you mean remove the pdev_pasid_supported() ?

yes

author maintainer
#54 lore raw

Re: [PATCH v2 6/8] iommu/amd: Enhance domain_alloc_user() to allocate PASID capable domain

Hi Kevin,

Quoted 25 lines · Kevin
Quoted 25 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, October 16, 2024 11:29 PM

Quoted 6 lines · Kevin
Quoted 6 lines

From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, October 15, 2024 8:40 PM

Quoted 1 line · Kevin

Review

Quoted 1 line
(quoted diff) +0 -1
-	return do_iommu_domain_alloc(type, dev, flags,

AMD_IOMMU_V1);

Quoted 1 line
Quoted 1 line
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +1 -0
+	/* Allocate v2 page table if IOMMU and device supports

PASID. */

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

drivers/iommu/amd/iommu.c amd_iommu_domain_alloc_user(struct device *dev, u32 flags, · line 2415

Quoted 4 lines
drivers/iommu/amd/iommu.c +4 -0
+	if (flags & IOMMU_HWPT_ALLOC_PASID) {
+		if (!amd_iommu_pasid_supported() ||
+		    !pdev_pasid_supported(dev_data))
+			return ERR_PTR(-EINVAL);

Just check the iommu capability here? IMHO it's a sane case to first create a PASID-compatible domain for a device which doesn't support PASID and then attach the domain to a PASID-capable device later.

The point of this series is to allow AMD to select the v1 format as often as possible because v1 has better performance than v2

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

If the user wants better performance then it should not set the flag.

Well, I do tend to agree with that, you mean remove the pdev_pasid_supported() ?

yes

Yeah. That should be fine I guess. Will fix it.

-Vasant

author maintainer
#55 lore raw

PATCH v2 7/8 iommu/amd: Add iommu_ops->domain_alloc_paging support

In reply to Vasant Hegde

Add support to allocate paging domain. Note that core will call domain_alloc_user() to allocate PASID capable domain. Hence its not checking device capability and allocates page table based on amd_iommu_pgtable.

---
 drivers/iommu/amd/iommu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/iommu/amd/iommu.c +11 -0
index 1c9e539f15a7..e6b4460c485d 100644
@@ -2395,6 +2395,16 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type)
 	return domain;
 }
 
+/*
+ * Allocate domain with default (amd_iommu_pgtable) page table type. Core will
+ * call domain_alloc_user() interface to allocate PASID capable domain.
+ */
+static struct iommu_domain *amd_iommu_domain_alloc_paging(struct device *dev)
+{
+	return do_iommu_domain_alloc(IOMMU_DOMAIN_DMA,
+				     dev, 0, amd_iommu_pgtable);
+}
+
 static struct iommu_domain *
 amd_iommu_domain_alloc_user(struct device *dev, u32 flags,
 			    struct iommu_domain *parent,
@@ -2858,6 +2868,7 @@ const struct iommu_ops amd_iommu_ops = {
 	.capable = amd_iommu_capable,
 	.blocked_domain = &blocked_domain,
 	.domain_alloc = amd_iommu_domain_alloc,
+	.domain_alloc_paging = amd_iommu_domain_alloc_paging,
 	.domain_alloc_user = amd_iommu_domain_alloc_user,
 	.domain_alloc_sva = amd_iommu_domain_alloc_sva,
 	.probe_device = amd_iommu_probe_device,
Signature · 1 line
2.31.1
#56 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Quoted 17 lines · Vasant Hegde

Add support to allocate paging domain. Note that core will call domain_alloc_user() to allocate PASID capable domain. Hence its not checking device capability and allocates page table based on amd_iommu_pgtable.

---
 drivers/iommu/amd/iommu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/iommu/amd/iommu.c +0 -0
index 1c9e539f15a7..e6b4460c485d 100644
@@ -2395,6 +2395,16 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type)
 	return domain;
 }

drivers/iommu/amd/iommu.c static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type) · line 2406

Quoted 10 lines
drivers/iommu/amd/iommu.c +9 -0
 
+/*
+ * Allocate domain with default (amd_iommu_pgtable) page table type. Core will
+ * call domain_alloc_user() interface to allocate PASID capable domain.
+ */
+static struct iommu_domain *amd_iommu_domain_alloc_paging(struct device *dev)
+{
+	return do_iommu_domain_alloc(IOMMU_DOMAIN_DMA,
+				     dev, 0, amd_iommu_pgtable);
+}

I think you should avoid this. The _user one (aka extended) should handle everthing. It doesn't really make sense anyhow, as the legacy VFIO path is going to take this branch as it will have 0 flags.

Can we just get rid of amd_iommu_pgtable ? The logic to do feature negotiation with HW support needs to be embedded inside amd_iommu_domain_alloc_user() anyhow

And the two kernel command line options seem totally useless now that we select the page table format correctly automatically. As you've explained the v1 format is always better.

Maybe the kernel command line options should be revised to force ONLY v2 format and ONLY v1 format, but that still wouldn't need this function. But also maybe they should just be removed.

Jason

author maintainer
#57 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Jason,

Quoted 19 lines · Jason Gunthorpe
Quoted 18 lines · Vasant Hegde

Add support to allocate paging domain. Note that core will call domain_alloc_user() to allocate PASID capable domain. Hence its not checking device capability and allocates page table based on amd_iommu_pgtable.

---
 drivers/iommu/amd/iommu.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/iommu/amd/iommu.c +0 -0
index 1c9e539f15a7..e6b4460c485d 100644
@@ -2395,6 +2395,16 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type)
 	return domain;
 }
 
Quoted 13 lines

drivers/iommu/amd/iommu.c static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type) · line 2406

Quoted 9 lines
drivers/iommu/amd/iommu.c +9 -0
+/*
+ * Allocate domain with default (amd_iommu_pgtable) page table type. Core will
+ * call domain_alloc_user() interface to allocate PASID capable domain.
+ */
+static struct iommu_domain *amd_iommu_domain_alloc_paging(struct device *dev)
+{
+	return do_iommu_domain_alloc(IOMMU_DOMAIN_DMA,
+				     dev, 0, amd_iommu_pgtable);
+}

I think you should avoid this. The _user one (aka extended) should handle everthing. It doesn't really make sense anyhow, as the legacy VFIO path is going to take this branch as it will have 0 flags.

I should have been bit more clear in cover letter.

I thought eventually we want to remove ops->domain_alloc() interface. Hence I introduced domain_alloc_paging(). I didn't remove amd_iommu_domain_alloc() in this series. I was planning to do it in later.

If we are fine to keep ops->domain_alloc() then I can just drop this patch.

Quoted 5 lines

Can we just get rid of amd_iommu_pgtable ? The logic to do feature negotiation with HW support needs to be embedded inside amd_iommu_domain_alloc_user() anyhow

Quoted 7 lines

And the two kernel command line options seem totally useless now that we select the page table format correctly automatically. As you've explained the v1 format is always better.

Maybe the kernel command line options should be revised to force ONLY v2 format and ONLY v1 format, but that still wouldn't need this function. But also maybe they should just be removed.

Yeah. Some enforcement and cleanup is needed in that path. Its in my list.

Before that I need to fix few more things like amd_iommu_def_domain_type(). I have few patches internally. Will post it after this series.

-Vasant

#58 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Quoted 14 lines · Vasant Hegde
Quoted 5 lines
Quoted 5 lines
drivers/iommu/amd/iommu.c +5 -0
+/*
+ * Allocate domain with default (amd_iommu_pgtable) page table type. Core will
+ * call domain_alloc_user() interface to allocate PASID capable domain.
+ */
+static struct iommu_domain *amd_iommu_domain_alloc_paging(struct device *dev)
Quoted 8 lines

drivers/iommu/amd/iommu.c static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type) · line 2406

Quoted 4 lines
drivers/iommu/amd/iommu.c +4 -0
+{
+	return do_iommu_domain_alloc(IOMMU_DOMAIN_DMA,
+				     dev, 0, amd_iommu_pgtable);
+}

I think you should avoid this. The _user one (aka extended) should handle everthing. It doesn't really make sense anyhow, as the legacy VFIO path is going to take this branch as it will have 0 flags.

Quoted 5 lines

I should have been bit more clear in cover letter.

I thought eventually we want to remove ops->domain_alloc() interface. Hence I introduced domain_alloc_paging(). I didn't remove amd_iommu_domain_alloc() in this series. I was planning to do it in later.

We do want to remove domain_alloc

Quoted 1 line

If we are fine to keep ops->domain_alloc() then I can just drop this patch.

But why not just remove it and not add amd_iommu_domain_alloc_paging ?

Your prior patch does this:

+ if (ops->domain_alloc_paging && !flags)
+ domain = ops->domain_alloc_paging(dev);
+ else if (ops->domain_alloc_user)
+ domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+ else if (ops->domain_alloc && !flags)
+ domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+ else
+ return ERR_PTR(-EOPNOTSUPP);

So if !ops->domain_alloc_paging && ops->domain_alloc_user it calls ops->domain_alloc_user()

You just need ops->domain_alloc_user(flags == 0) to allocate the fasted/best page table with no-PASID support (ie v1 in most cases) and everything is fine.

Don't add amd_iommu_domain_alloc_paging(), just add the above paragraph of logic to ops->domain_alloc_user()

The point of all this fixing was to stop using IOMMU_DOMAIN_DMA to select a page table format and instead rely on IOMMU_HWPT_ALLOC_PASID to make the selection. The core code and VFIO will set IOMMU_HWPT_ALLOC_PASID properly for what it wants to do. I expect the driver to rely on IOMMU_HWPT_ALLOC_PASID exclusively.

Jason

author maintainer
#59 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Jason,

Quoted 22 lines · Jason Gunthorpe
Quoted 14 lines · Vasant Hegde
Quoted 5 lines
Quoted 5 lines
drivers/iommu/amd/iommu.c +5 -0
+/*
+ * Allocate domain with default (amd_iommu_pgtable) page table type. Core will
+ * call domain_alloc_user() interface to allocate PASID capable domain.
+ */
+static struct iommu_domain *amd_iommu_domain_alloc_paging(struct device *dev)
Quoted 8 lines

drivers/iommu/amd/iommu.c static struct iommu_domain *amd_iommu_domain_alloc(unsigned int type) · line 2406

Quoted 4 lines
drivers/iommu/amd/iommu.c +4 -0
+{
+	return do_iommu_domain_alloc(IOMMU_DOMAIN_DMA,
+				     dev, 0, amd_iommu_pgtable);
+}

I think you should avoid this. The _user one (aka extended) should handle everthing. It doesn't really make sense anyhow, as the legacy VFIO path is going to take this branch as it will have 0 flags.

Quoted 5 lines

I should have been bit more clear in cover letter.

I thought eventually we want to remove ops->domain_alloc() interface. Hence I introduced domain_alloc_paging(). I didn't remove amd_iommu_domain_alloc() in this series. I was planning to do it in later.

Quoted 5 lines

We do want to remove domain_alloc

Quoted 1 line

If we are fine to keep ops->domain_alloc() then I can just drop this patch.

But why not just remove it and not add amd_iommu_domain_alloc_paging ?

Because we have __iommu_domain_alloc() which calls domain_alloc_paging / domain_alloc().

Quoted 12 lines

Your prior patch does this:

(quoted diff) +8 -0
+	if (ops->domain_alloc_paging && !flags)
+		domain = ops->domain_alloc_paging(dev);
+	else if (ops->domain_alloc_user)
+		domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
+	else if (ops->domain_alloc && !flags)
+		domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+	else
+		return ERR_PTR(-EOPNOTSUPP);
Quoted 6 lines

So if !ops->domain_alloc_paging && ops->domain_alloc_user it calls ops->domain_alloc_user()

You just need ops->domain_alloc_user(flags == 0) to allocate the fasted/best page table with no-PASID support (ie v1 in most cases) and everything is fine.

This is fine. I can add it.

Quoted 9 lines

Don't add amd_iommu_domain_alloc_paging(), just add the above paragraph of logic to ops->domain_alloc_user()

The point of all this fixing was to stop using IOMMU_DOMAIN_DMA to select a page table format and instead rely on IOMMU_HWPT_ALLOC_PASID to make the selection. The core code and VFIO will set IOMMU_HWPT_ALLOC_PASID properly for what it wants to do. I expect the driver to rely on IOMMU_HWPT_ALLOC_PASID exclusively.

Sure. Will fix it. But as said above, we still need domain_alloc_paging() or domain_alloc() interface.

-Vasant

#60 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Quoted 4 lines · Vasant Hegde
Quoted 4 lines
Quoted 3 lines

I thought eventually we want to remove ops->domain_alloc() interface. Hence I introduced domain_alloc_paging(). I didn't remove amd_iommu_domain_alloc() in this series. I was planning to do it in later.

Quoted 8 lines
Quoted 5 lines

We do want to remove domain_alloc

Quoted 1 line

If we are fine to keep ops->domain_alloc() then I can just drop this patch.

But why not just remove it and not add amd_iommu_domain_alloc_paging ?

Because we have __iommu_domain_alloc() which calls domain_alloc_paging / domain_alloc().

Oh, I see what you are worried about. __iommu_domain_alloc() is almost never called now that most of Lu's patches are merged, and it is not needed for any x86 platform case.

Here are two more patches you can grab that will remove it from the remaining core cases and then you don't need to worry about it, everything for AMD will go through __iommu_paging_domain_alloc_flags():

https://github.com/jgunthorpe/linux/commits/for-vasant

Lu should be able to delete the function this cycle as well.

Also I noticed a little mistake:

drivers/iommu/iommu.c +1 -1
@@ -2071,7 +2071,7 @@ static struct iommu_domain *__iommu_paging_domain_alloc_flags(struct device *dev
        else if (ops->domain_alloc_user)
                domain = ops->domain_alloc_user(dev, flags, NULL, NULL);
        else if (ops->domain_alloc && !flags)
-               domain = ops->domain_alloc(IOMMU_DOMAIN_UNMANAGED);
+               domain = ops->domain_alloc(type);
        else
                return ERR_PTR(-EOPNOTSUPP);
 

Jason

author maintainer
#61 lore raw

Re: [PATCH v2 7/8] iommu/amd: Add iommu_ops->domain_alloc_paging support

Hi Jason,

Quoted 18 lines · Jason Gunthorpe
Quoted 4 lines · Vasant Hegde
Quoted 4 lines
Quoted 3 lines

I thought eventually we want to remove ops->domain_alloc() interface. Hence I introduced domain_alloc_paging(). I didn't remove amd_iommu_domain_alloc() in this series. I was planning to do it in later.

Quoted 8 lines
Quoted 5 lines

We do want to remove domain_alloc

Quoted 1 line

If we are fine to keep ops->domain_alloc() then I can just drop this patch.

But why not just remove it and not add amd_iommu_domain_alloc_paging ?

Because we have __iommu_domain_alloc() which calls domain_alloc_paging / domain_alloc().

Oh, I see what you are worried about. __iommu_domain_alloc() is almost never called now that most of Lu's patches are merged, and it is not needed for any x86 platform case.

Quoted 5 lines

Here are two more patches you can grab that will remove it from the remaining core cases and then you don't need to worry about it, everything for AMD will go through __iommu_paging_domain_alloc_flags():

https://github.com/jgunthorpe/linux/commits/for-vasant

Sure. Will pick those patches and get the v3 soon.

-Vasant

author maintainer
#62 lore raw

PATCH v2 8/8 iommu/amd: Implement global identity domain

In reply to Vasant Hegde

Implement global identity domain. All devices groups in identity domain will share this domain.

In attach device path, based on device capability it will allocate per device domain ID and GCR3 table. So that it can support SVA.

---
 drivers/iommu/amd/amd_iommu.h |  1 +
 drivers/iommu/amd/init.c      |  3 +++
 drivers/iommu/amd/iommu.c     | 36 +++++++++++++++++++++++++++++++----
 3 files changed, 36 insertions(+), 4 deletions(-)
drivers/iommu/amd/amd_iommu.h +1 -0
index 6386fa4556d9..983a9d109206 100644
@@ -46,6 +46,7 @@ extern int amd_iommu_gpt_level;
 extern unsigned long amd_iommu_pgsize_bitmap;
 
 /* Protection domain ops */
+void amd_iommu_init_identity_domain(void);
 struct protection_domain *protection_domain_alloc(unsigned int type, int nid);
 void protection_domain_free(struct protection_domain *domain);
 struct iommu_domain *amd_iommu_domain_alloc_sva(struct device *dev,
drivers/iommu/amd/init.c +3 -0
index 43131c3a2172..8517a854c331 100644
@@ -2172,6 +2172,9 @@ static int __init amd_iommu_init_pci(void)
 	struct amd_iommu_pci_seg *pci_seg;
 	int ret;
 
+	/* Init global identity domain before registering IOMMU */
+	amd_iommu_init_identity_domain();
+
 	for_each_iommu(iommu) {
 		ret = iommu_init_pci(iommu);
 		if (ret) {
drivers/iommu/amd/iommu.c +32 -4
index e6b4460c485d..6b1bb07e0aae 100644
@@ -74,6 +74,9 @@ struct kmem_cache *amd_iommu_irq_cache;
 
 static void detach_device(struct device *dev);
 
+static int amd_iommu_attach_device(struct iommu_domain *dom,
+				   struct device *dev);
+
 static void set_dte_entry(struct amd_iommu *iommu,
 			  struct iommu_dev_data *dev_data);
 
@@ -2262,6 +2265,14 @@ void protection_domain_free(struct protection_domain *domain)
 	kfree(domain);
 }
 
+static void protection_domain_init(struct protection_domain *domain, int nid)
+{
+	spin_lock_init(&domain->lock);
+	INIT_LIST_HEAD(&domain->dev_list);
+	INIT_LIST_HEAD(&domain->dev_data_list);
+	domain->iop.pgtbl.cfg.amd.nid = nid;
+}
+
 struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 {
 	struct protection_domain *domain;
@@ -2276,10 +2287,7 @@ struct protection_domain *protection_domain_alloc(unsigned int type, int nid)
 		return NULL;
 	}
 
-	spin_lock_init(&domain->lock);
-	INIT_LIST_HEAD(&domain->dev_list);
-	INIT_LIST_HEAD(&domain->dev_data_list);
-	domain->iop.pgtbl.cfg.amd.nid = nid;
+	protection_domain_init(domain, nid);
 
 	return domain;
 }
@@ -2469,6 +2477,25 @@ static struct iommu_domain blocked_domain = {
 	}
 };
 
+static struct protection_domain identity_domain;
+
+const struct iommu_domain_ops identity_domain_ops = {
+	.attach_dev = amd_iommu_attach_device,
+};
+
+void amd_iommu_init_identity_domain(void)
+{
+	struct iommu_domain *domain = &identity_domain.domain;
+
+	domain->type = IOMMU_DOMAIN_IDENTITY;
+	domain->ops = &identity_domain_ops;
+	domain->owner = &amd_iommu_ops;
+
+	identity_domain.id = domain_id_alloc();
+
+	protection_domain_init(&identity_domain, NUMA_NO_NODE);
+}
+
 static int amd_iommu_attach_device(struct iommu_domain *dom,
 				   struct device *dev)
 {
@@ -2867,6 +2894,7 @@ static int amd_iommu_dev_disable_feature(struct device *dev,
 const struct iommu_ops amd_iommu_ops = {
 	.capable = amd_iommu_capable,
 	.blocked_domain = &blocked_domain,
+	.identity_domain = &identity_domain.domain,
 	.domain_alloc = amd_iommu_domain_alloc,
 	.domain_alloc_paging = amd_iommu_domain_alloc_paging,
 	.domain_alloc_user = amd_iommu_domain_alloc_user,
Signature · 1 line
2.31.1
Reviewed-by on v2 8/8
#63 lore raw

Re: [PATCH v2 8/8] iommu/amd: Implement global identity domain

drivers/iommu/amd/iommu.c static struct iommu_domain blocked_domain = { · line 2484

Quoted 5 lines · Vasant Hegde
drivers/iommu/amd/iommu.c +5 -0
+static struct protection_domain identity_domain;
+
+const struct iommu_domain_ops identity_domain_ops = {
+	.attach_dev = amd_iommu_attach_device,
+};

It is fine for now, but attach_dev really should be a function that just does identity and what is left should really just only do paging. Same for blocked. That simplifies the flows quite a bit..

It would also be really nice if the identity_domain and blocked_domain was not a struct protection_domain like all the other drivers have managed..

Jason

author maintainer
#64 lore raw

Re: [PATCH v2 8/8] iommu/amd: Implement global identity domain

Quoted 11 lines · Jason Gunthorpe

drivers/iommu/amd/iommu.c static struct iommu_domain blocked_domain = { · line 2484

Quoted 5 lines · Vasant Hegde
drivers/iommu/amd/iommu.c +5 -0
+static struct protection_domain identity_domain;
+
+const struct iommu_domain_ops identity_domain_ops = {
+	.attach_dev = amd_iommu_attach_device,
+};

It is fine for now, but attach_dev really should be a function that just does identity and what is left should really just only do paging. Same for blocked. That simplifies the flows quite a bit..

Quoted 3 lines

It would also be really nice if the identity_domain and blocked_domain was not a struct protection_domain like all the other drivers have managed..

We support SVA with identity domain. So for now I had to create protection_domain as identity domain.

I will revisit after fixing attach domain code.

-Vasant

Quoted 4 lines

Jason

author maintainer
#65 lore raw
In reply to Vasant Hegde

Jason, Baolu,

Quoted 4 lines · Vasant Hegde

This series adds iommu_paging_domain_alloc_flags() which takes flags to pass additional details for domain allocation (like domain with PASID support).

Quoted 8 lines

Also updates AMD IOMMU driver domain allocation code. With this by default it will allocate domain with V2 page table for PASID capable device and v1 page table for rest of the devices.

@Baolu,
With this changes (patch 1), to allocate PASID capable device core will call domain_alloc_user() interface. Do we need any changes to intel driver?

@Baolu,
Looks like intel driver works fine with this change. Is that correct -OR- do
we need any changes to intel_iommu_domain_alloc_user() ?

Quoted 3 lines

@Jason,
Do we need any changes to arm-smm-v3 driver?

@Jason,

Looking into SMM3 driver it looks like some changes required. Does something like below works?

(quoted diff) +0 -0

b/drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c
index 737c5b882355..4d4af11c5fda 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
@@ -3088,6 +3088,9 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags,
 	struct arm_smmu_domain *smmu_domain;
 	int ret;
 
+	if (flags & IOMMU_HWPT_ALLOC_PASID)
+		return arm_smmu_domain_alloc_paging(dev);
+
 	if (flags & ~PAGING_FLAGS)
 		return ERR_PTR(-EOPNOTSUPP);
 	if (parent || user_data)

-Vasant

#66 lore raw
Quoted 10 lines · Vasant Hegde

Looking into SMM3 driver it looks like some changes required. Does something like below works?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 737c5b882355..4d4af11c5fda 100644

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c arm_smmu_domain_alloc_user(struct device *dev, u32 flags, · line 3093

Quoted 6 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
@@ -3088,6 +3088,9 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags,
 	struct arm_smmu_domain *smmu_domain;
 	int ret;
 
+	if (flags & IOMMU_HWPT_ALLOC_PASID)
+		return arm_smmu_domain_alloc_paging(dev);
+

Yes, I think that is good enough for now.

Thanks,
Jason

author maintainer
#67 lore raw

Jason,

Quoted 15 lines · Jason Gunthorpe
Quoted 13 lines · Vasant Hegde

Looking into SMM3 driver it looks like some changes required. Does something like below works?

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +0 -0
index 737c5b882355..4d4af11c5fda 100644
@@ -3088,6 +3088,9 @@ arm_smmu_domain_alloc_user(struct device *dev, u32 flags,
 	struct arm_smmu_domain *smmu_domain;
 	int ret;
 
Quoted 5 lines

drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c arm_smmu_domain_alloc_user(struct device *dev, u32 flags, · line 3093

Quoted 3 lines
drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +3 -0
+	if (flags & IOMMU_HWPT_ALLOC_PASID)
+		return arm_smmu_domain_alloc_paging(dev);
+

Yes, I think that is good enough for now.

Thanks! I will add this patch in next version.

-Vasant

maintainer
#68 lore raw
In reply to Vasant Hegde
Quoted 9 lines · Vasant Hegde
Quoted 8 lines · Vasant Hegde

This series adds iommu_paging_domain_alloc_flags() which takes flags to pass additional details for domain allocation (like domain with PASID support).

Also updates AMD IOMMU driver domain allocation code. With this by default it will allocate domain with V2 page table for PASID capable device and v1 page table for rest of the devices.

Quoted 7 lines
Quoted 4 lines

@Baolu,
With this changes (patch 1), to allocate PASID capable device core will call domain_alloc_user() interface. Do we need any changes to intel driver?

@Baolu,
Looks like intel driver works fine with this change. Is that correct -OR- do
we need any changes to intel_iommu_domain_alloc_user() ?

I still have a question about the following change:

https://lore.kernel.org/linux-iommu/[email protected]/

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Thanks,
baolu

author maintainer
#69 lore raw

Hi Baolu,

Quoted 28 lines · Baolu Lu
Quoted 9 lines · Vasant Hegde
Quoted 8 lines · Vasant Hegde

This series adds iommu_paging_domain_alloc_flags() which takes flags to pass additional details for domain allocation (like domain with PASID support).

Also updates AMD IOMMU driver domain allocation code. With this by default it will allocate domain with V2 page table for PASID capable device and v1 page table for rest of the devices.

Quoted 7 lines
Quoted 4 lines

@Baolu,
   With this changes (patch 1), to allocate PASID capable device core will call domain_alloc_user() interface. Do we need any changes to intel driver?

@Baolu,
   Looks like intel driver works fine with this change. Is that correct -OR- do we need any changes to intel_iommu_domain_alloc_user() ?

I still have a question about the following change:

https://lore.kernel.org/linux-iommu/[email protected]/

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 5 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

IIUC intel driver wants something like below.

- Host : DMA API Mode : first level page table
(PASID is supported in both level, so you don't need PASID capability check)
- Host UNMANAGED (VFIO Passthrough) : second level page table
- Guest : first level page table
- Nested : second level page table

Is this the correct understanding?

W/ this series (domain_alloc_user() interface), you will not be able to decide right page table (second level page table) for UNMANAGED domain. Is that correct?

-Vasant

#70 lore raw
Quoted 6 lines · Vasant Hegde
Quoted 6 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 7 lines
Quoted 5 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

Something like this:

drivers/iommu/intel/iommu.c +13 -8
index 9f6b0780f2ef5e..cfae6c2973e0ee 100644
@@ -1440,7 +1440,7 @@ static void free_dmar_iommu(struct intel_iommu *iommu)
  * Check and return whether first level is used by default for
  * DMA translation.
  */
-static bool first_level_by_default(unsigned int type)
+static bool first_level_by_default(void)
 {
 	/* Only SL is available in legacy mode */
 	if (!scalable_mode_support())
@@ -1450,8 +1450,8 @@ static bool first_level_by_default(unsigned int type)
 	if (intel_cap_flts_sanity() ^ intel_cap_slts_sanity())
 		return intel_cap_flts_sanity();
 
-	/* Both levels are available, decide it based on domain type */
-	return type != IOMMU_DOMAIN_UNMANAGED;
+	/* Both levels are available, use FL */
+	return true;
 }
 
 static struct dmar_domain *alloc_domain(unsigned int type)
@@ -1463,7 +1463,7 @@ static struct dmar_domain *alloc_domain(unsigned int type)
 		return NULL;
 
 	domain->nid = NUMA_NO_NODE;
-	if (first_level_by_default(type))
+	if (first_level_by_default())
 		domain->use_first_level = true;
 	INIT_LIST_HEAD(&domain->devices);
 	INIT_LIST_HEAD(&domain->dev_pasids);
@@ -3287,8 +3287,7 @@ int __init intel_iommu_init(void)
 		 * is likely to be much lower than the overhead of synchronizing
 		 * the virtual and physical IOMMU page-tables.
 		 */
-		if (cap_caching_mode(iommu->cap) &&
-		    !first_level_by_default(IOMMU_DOMAIN_DMA)) {
+		if (cap_caching_mode(iommu->cap) && !first_level_by_default()) {
 			pr_info_once("IOMMU batching disallowed due to virtualization\n");
 			iommu_set_dma_strict();
 		}
@@ -3530,6 +3529,7 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
 	struct intel_iommu *iommu = info->iommu;
 	struct dmar_domain *dmar_domain;
 	struct iommu_domain *domain;
+	bool first_level;
 
 	/* Must be NESTING domain */
 	if (parent) {
@@ -3541,13 +3541,18 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
 	if (flags &
 	    (~(IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)))
 		return ERR_PTR(-EOPNOTSUPP);
+	if ((flags & IOMMU_HWPT_ALLOC_NEST_PARENT) && !intel_cap_slts_sanity())
+		return ERR_PTR(-EOPNOTSUPP);
 	if (nested_parent && !nested_supported(iommu))
 		return ERR_PTR(-EOPNOTSUPP);
 	if (user_data || (dirty_tracking && !ssads_supported(iommu)))
 		return ERR_PTR(-EOPNOTSUPP);
 
-	/* Do not use first stage for user domain translation. */
-	dmar_domain = paging_domain_alloc(dev, false);
+	if (flags & IOMMU_HWPT_ALLOC_NEST_PARENT)
+		first_level = false;
+	else
+		first_level = first_level_by_default();
+	dmar_domain = paging_domain_alloc(dev, first_level);
 	if (IS_ERR(dmar_domain))
 		return ERR_CAST(dmar_domain);
 	domain = &dmar_domain->domain;
maintainer
#71 lore raw
Quoted 20 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde
Quoted 6 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 6 lines
Quoted 5 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

Quoted 5 lines

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

Something like this:

This will break the existing dirty page tracking functionality. Intel IOMMU only supports enabling or disabling dirty page tracking at the second-stage page table.

/*
* Set up dirty tracking on a second only or nested translation type. */
int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
struct device *dev, u32 pasid, bool enabled)
{
[...]

        pgtt = pasid_pte_get_pgtt(pte);
        if (pgtt != PASID_ENTRY_PGTT_SL_ONLY &&
            pgtt != PASID_ENTRY_PGTT_NESTED) {
                spin_unlock(&iommu->lock);
                dev_err_ratelimited(
                        dev,
                        "Dirty tracking not supported on translation 
type %d\n",
                        pgtt);
                return -EOPNOTSUPP;
        }

Thanks,
baolu

maintainer
#72 lore raw
Quoted 25 lines · Baolu Lu
Quoted 24 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde
Quoted 6 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 6 lines
Quoted 5 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

Quoted 5 lines
Quoted 1 line

Something like this:

This will break the existing dirty page tracking functionality. Intel IOMMU only supports enabling or disabling dirty page tracking at the second-stage page table.

So, perhaps something like below?

drivers/iommu/intel/iommu.h +2 -0
index 1497f3112b12..e11dde259afa 100644
@@ -544,6 +544,8 @@ enum {
                                ecap_slads((iommu)->ecap))
 #define nested_supported(iommu)        (sm_supported(iommu) && 
        \
                                 ecap_nest((iommu)->ecap))
+#define flts_supported(iommu)  (sm_supported(iommu) &&                 \
+                                ecap_flts((iommu)->ecap))
 
 struct pasid_entry;

struct pasid_state_entry;

drivers/iommu/intel/iommu.c +9 -2
index 9f6b0780f2ef..d1a7378489a4 100644
@@ -3530,6 +3530,7 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
        struct intel_iommu *iommu = info->iommu;
        struct dmar_domain *dmar_domain;
        struct iommu_domain *domain;
+       bool first_stage;
 
        /* Must be NESTING domain */
        if (parent) {
@@ -3546,8 +3547,14 @@ intel_iommu_domain_alloc_user(struct device *dev, u32 flags,
        if (user_data || (dirty_tracking && !ssads_supported(iommu)))
                return ERR_PTR(-EOPNOTSUPP);
 
-       /* Do not use first stage for user domain translation. */
-       dmar_domain = paging_domain_alloc(dev, false);
+       /*
+        * Always allocate the guest compatible page table unless
+        * IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING
+        * is specified.
+        */
+       first_stage = (nested_parent || dirty_tracking) ?
+                       false : flts_supported(iommu);
+       dmar_domain = paging_domain_alloc(dev, first_stage);
        if (IS_ERR(dmar_domain))
                return ERR_CAST(dmar_domain);
        domain = &dmar_domain->domain;

Thanks,
baolu

#73 lore raw
Quoted 66 lines · Baolu Lu
Quoted 27 lines · Baolu Lu
Quoted 26 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde
Quoted 6 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 8 lines
Quoted 6 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds
true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

Quoted 5 lines
Quoted 1 line

Something like this:

This will break the existing dirty page tracking functionality. Intel IOMMU only supports enabling or disabling dirty page tracking at the second-stage page table.

So, perhaps something like below?

drivers/iommu/intel/iommu.h +2 -0
index 1497f3112b12..e11dde259afa 100644
@@ -544,6 +544,8 @@ enum {
                                ecap_slads((iommu)->ecap))
 #define nested_supported(iommu)        (sm_supported(iommu) &&         \
                                 ecap_nest((iommu)->ecap))
+#define flts_supported(iommu)  (sm_supported(iommu) &&                 \
+                                ecap_flts((iommu)->ecap))
 
 struct pasid_entry;
 struct pasid_state_entry;
drivers/iommu/intel/iommu.c +1 -0
index 9f6b0780f2ef..d1a7378489a4 100644
@@ -3530,6 +3530,7 @@ intel_iommu_domain_alloc_user(struct device *dev, u32
 flags,
        struct intel_iommu *iommu = info->iommu;
        struct dmar_domain *dmar_domain;
        struct iommu_domain *domain;
+       bool first_stage;
 
        /* Must be NESTING domain */
        if (parent) {
drivers/iommu/intel/iommu.c +0 -0
@@ -3546,8 +3547,14 @@ intel_iommu_domain_alloc_user(struct device *dev, u32
 flags,
        if (user_data || (dirty_tracking && !ssads_supported(iommu)))
                return ERR_PTR(-EOPNOTSUPP);

drivers/iommu/intel/iommu.c intel_iommu_domain_alloc_user(struct device *dev, u32 · line 3557

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -2
 
-       /* Do not use first stage for user domain translation. */
-       dmar_domain = paging_domain_alloc(dev, false);
+       /*
+        * Always allocate the guest compatible page table unless
+        * IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING
+        * is specified.
+        */
+       first_stage = (nested_parent || dirty_tracking) ?
+                       false : flts_supported(iommu);

That makes sense, but these flags still need to be rejected if the second level is not supported in the HW.

+ if ((flags & (IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)) && !intel_cap_slts_sanity())
+ return ERR_PTR(-EOPNOTSUPP);

I also think my version was cleaner as we should be using first_level_by_default() consistently to make that decision.

Also don't care for ternary expressions :)

Jason

maintainer
#74 lore raw
Quoted 81 lines · Jason Gunthorpe
Quoted 64 lines · Baolu Lu
Quoted 23 lines · Baolu Lu
Quoted 22 lines · Jason Gunthorpe
Quoted 6 lines · Vasant Hegde
Quoted 6 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in the first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Quoted 8 lines
Quoted 6 lines

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds
true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

Quoted 8 lines
Quoted 5 lines

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

Something like this:

This will break the existing dirty page tracking functionality. Intel IOMMU only supports enabling or disabling dirty page tracking at the second-stage page table.

So, perhaps something like below?

drivers/iommu/intel/iommu.h +2 -0
index 1497f3112b12..e11dde259afa 100644
@@ -544,6 +544,8 @@ enum {
                                 ecap_slads((iommu)->ecap))
  #define nested_supported(iommu)        (sm_supported(iommu) &&         \
                                  ecap_nest((iommu)->ecap))
+#define flts_supported(iommu)  (sm_supported(iommu) &&                 \
+                                ecap_flts((iommu)->ecap))
 
  struct pasid_entry;
  struct pasid_state_entry;
drivers/iommu/intel/iommu.c +1 -0
index 9f6b0780f2ef..d1a7378489a4 100644
@@ -3530,6 +3530,7 @@ intel_iommu_domain_alloc_user(struct device *dev, u32
 flags,
         struct intel_iommu *iommu = info->iommu;
         struct dmar_domain *dmar_domain;
         struct iommu_domain *domain;
+       bool first_stage;
 
         /* Must be NESTING domain */
         if (parent) {
drivers/iommu/intel/iommu.c +0 -0
@@ -3546,8 +3547,14 @@ intel_iommu_domain_alloc_user(struct device *dev, u32
 flags,
         if (user_data || (dirty_tracking && !ssads_supported(iommu)))
                 return ERR_PTR(-EOPNOTSUPP);

drivers/iommu/intel/iommu.c intel_iommu_domain_alloc_user(struct device *dev, u32 · line 3557

Quoted 10 lines
drivers/iommu/intel/iommu.c +7 -2
 
-       /* Do not use first stage for user domain translation. */
-       dmar_domain = paging_domain_alloc(dev, false);
+       /*
+        * Always allocate the guest compatible page table unless
+        * IOMMU_HWPT_ALLOC_NEST_PARENT or IOMMU_HWPT_ALLOC_DIRTY_TRACKING
+        * is specified.
+        */
+       first_stage = (nested_parent || dirty_tracking) ?
+                       false : flts_supported(iommu);

That makes sense, but these flags still need to be rejected if the second level is not supported in the HW.

(quoted diff) +2 -0
+	if ((flags & (IOMMU_HWPT_ALLOC_NEST_PARENT | IOMMU_HWPT_ALLOC_DIRTY_TRACKING)) && !intel_cap_slts_sanity())
+		return ERR_PTR(-EOPNOTSUPP);
Quoted 4 lines

I also think my version was cleaner as we should be using first_level_by_default() consistently to make that decision.

Also don't care for ternary expressions 🙂

Okay, seems that we are on the same page now. :-)

I have a series to add domain_alloc_paging support in the Intel iommu driver. I will convert the change in a formal patch and put it in that series for further review.

Thanks,
baolu

maintainer
#75 lore raw
In reply to Jason Gunthorpe
Quoted 18 lines

From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, October 9, 2024 8:16 PM

Quoted 3 lines · Vasant Hegde
Quoted 3 lines

This change might cause a functional regression when it comes to nested translation. In nested translation mode, the user page table (e.g., created and managed by a guest VM for guest kernel DMA) must be in

the

Quoted 9 lines
Quoted 8 lines

first-stage page table format. Then, it can be nested on a second-stage page table managed by the host kernel.

Currently, the kernel automatically selects the page table formats. For example, the Intel IOMMU driver always uses the first-stage page table for guest kernel DMA. After this change, this assumption no longer holds true. This means the kernel might use a second-stage page table for guest kernel DMA, breaking nested translation.

Quoted 8 lines
Quoted 1 line

Hmmm. I assumed after discussion in v1 series you are fine. Looks like I

misread it?

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

I don't think it's a bug. If nested is broken then VMM can fall back to the shadowing approach. If the VMM only supports nested then the only way to guarantee it is to not advertise 2nd-level capability on the vIOMMU.

The VT-d spec never says that guest software must use 1st-level.

Quoted 5 lines

For this reason Intel driver should always allocate the guest compatible page table unless IOMMU_HWPT_ALLOC_NEST_PARENT is specified.

But I'm fine with this *policy* change.

#76 lore raw
Quoted 10 lines · Kevin
Quoted 5 lines

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

I don't think it's a bug. If nested is broken then VMM can fall back to the shadowing approach. If the VMM only supports nested then the only way to guarantee it is to not advertise 2nd-level capability on the vIOMMU.

Right, which is the bug, the alloc function isn't checking the capabilities of the HW before going ahead to allocate a table format.

So it can choose a table format the HW doesn't advertise support for. Whoops.

Jason

maintainer
#77 lore raw
Quoted 16 lines

From: Jason Gunthorpe <[email protected]>
Sent: Friday, October 11, 2024 7:39 PM

Quoted 10 lines · Kevin
Quoted 5 lines

It is a bug in the implementation in the intel driver.

intel_iommu_domain_alloc_user() should always allocate a page table that works, if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken.

I don't think it's a bug. If nested is broken then VMM can fall back to the shadowing approach. If the VMM only supports nested then the only way to guarantee it is to not advertise 2nd-level capability on the vIOMMU.

Quoted 6 lines

Right, which is the bug, the alloc function isn't checking the capabilities of the HW before going ahead to allocate a table format.

So it can choose a table format the HW doesn't advertise support for. Whoops.

That's certainly a bug.

My comment was about " if you are in a guest context it must allocate a first level/guest page table otherwise nested VFIO will be broken ", which is a policy thing in supported caps.