Skip to contentSkip to conversation

iommu/amd/pgtbl_v2: Fix domain max address

PATCH In review Vasant Hegde 3 participants 4 messages May 18, 2023 – May 23, 2023

Subsystems (3)
lore
Share & tools
lore

Vasant Hegde

author maintainer
#0 lore raw

PATCH 1/1 iommu/amd/pgtbl_v2: Fix domain max address

IOMMU v2 page table supports 4 level (47 bit) or 5 level (56 bit) virtual address space. Current code assumes it can support 64bit IOVA address space. If IOVA allocator allocates virtual address > 47/56 bit (depending on page table level) then it will do wrong mapping and cause invalid translation.

Hence adjust aperture size to use max address supported by the page table.

Fixes aaac38f61487 ("iommu/amd: Initial support for AMD IOMMU v2 page table")
Cc <[email protected]> # v6.0+
Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/iommu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +10 -1
index 5aaa4cf84506..e14c7c666745 100644
@@ -2128,6 +2128,15 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
 	return NULL;
 }
 
+static inline u64 dma_max_address(void)
+{
+	if (amd_iommu_pgtable == AMD_IOMMU_V1)
+		return ~0ULL;
+
+	/* V2 with 4/5 level page table */
+	return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
+}
+
 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 {
 	struct protection_domain *domain;
@@ -2144,7 +2153,7 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 		return NULL;
 
 	domain->domain.geometry.aperture_start = 0;
-	domain->domain.geometry.aperture_end   = ~0ULL;
+	domain->domain.geometry.aperture_end   = dma_max_address();
 	domain->domain.geometry.force_aperture = true;
 
 	return &domain->domain;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Vasant Hegde

IOMMU v2 page table supports 4 level (47 bit) or 5 level (56 bit) virtual address space. Current code assumes it can support 64bit IOVA address space. If IOVA allocator allocates virtual address > 47/56 bit (depending on page table level) then it will do wrong mapping and cause invalid translation.

Quoted 7 lines

Hence adjust aperture size to use max address supported by the page table.

Fixes aaac38f61487 ("iommu/amd: Initial support for AMD IOMMU v2 page table")
Cc <[email protected]> # v6.0+
Cc Suravee Suthikulpanit <[email protected]>
Quoted 36 lines
---
 drivers/iommu/amd/iommu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +10 -1
index 5aaa4cf84506..e14c7c666745 100644
@@ -2128,6 +2128,15 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
 	return NULL;
 }
 
+static inline u64 dma_max_address(void)
+{
+	if (amd_iommu_pgtable == AMD_IOMMU_V1)
+		return ~0ULL;
+
+	/* V2 with 4/5 level page table */
+	return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
+}
+
 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 {
 	struct protection_domain *domain;
@@ -2144,7 +2153,7 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 		return NULL;
 
 	domain->domain.geometry.aperture_start = 0;
-	domain->domain.geometry.aperture_end   = ~0ULL;
+	domain->domain.geometry.aperture_end   = dma_max_address();
 	domain->domain.geometry.force_aperture = true;
 
 	return &domain->domain;
Signature · 1 line
2.31.1
#2 lore raw
In reply to Vasant Hegde
Quoted 22 lines · Vasant Hegde

IOMMU v2 page table supports 4 level (47 bit) or 5 level (56 bit) virtual address space. Current code assumes it can support 64bit IOVA address space. If IOVA allocator allocates virtual address > 47/56 bit (depending on page table level) then it will do wrong mapping and cause invalid translation.

Hence adjust aperture size to use max address supported by the page table.

Fixes aaac38f61487 ("iommu/amd: Initial support for AMD IOMMU v2 page table")
Cc <[email protected]> # v6.0+
Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/iommu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +0 -0
index 5aaa4cf84506..e14c7c666745 100644

drivers/iommu/amd/iommu.c static struct protection_domain *protection_domain_alloc(unsigned int type) · line 2137

Quoted 10 lines
drivers/iommu/amd/iommu.c +7 -0
@@ -2128,6 +2128,15 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)
 	return NULL;
 }
 
+static inline u64 dma_max_address(void)
+{
+	if (amd_iommu_pgtable == AMD_IOMMU_V1)
+		return ~0ULL;
+
+	/* V2 with 4/5 level page table */
+	return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);

Hi Vasant,

For the stable releases, this will need to be PAGE_MODE_4_LEVEL instead of amd_iommu_gpt_level? The 6.4 merge window is when amd_iommu_gpt_level arrived with the 5 level page table changes.

Regards,
Jerry

Quoted 17 lines
drivers/iommu/amd/iommu.c +3 -1
+}
+
 static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 {
 	struct protection_domain *domain;
@@ -2144,7 +2153,7 @@ static struct iommu_domain *amd_iommu_domain_alloc(unsigned type)
 		return NULL;
 
 	domain->domain.geometry.aperture_start = 0;
-	domain->domain.geometry.aperture_end   = ~0ULL;
+	domain->domain.geometry.aperture_end   = dma_max_address();
 	domain->domain.geometry.force_aperture = true;
 
 	return &domain->domain;
Signature · 1 line
2.31.1
maintainer
#3 lore raw
In reply to Vasant Hegde
Quoted 8 lines · Vasant Hegde

IOMMU v2 page table supports 4 level (47 bit) or 5 level (56 bit) virtual address space. Current code assumes it can support 64bit IOVA address space. If IOVA allocator allocates virtual address > 47/56 bit (depending on page table level) then it will do wrong mapping and cause invalid translation.

Hence adjust aperture size to use max address supported by the page table.

Quoted 8 lines
Fixes aaac38f61487 ("iommu/amd: Initial support for AMD IOMMU v2 page table")
Cc <[email protected]> # v6.0+
Cc Suravee Suthikulpanit <[email protected]>
---
 drivers/iommu/amd/iommu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Applied for 6.4, thanks Vasant.