patches.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH rc] iommu/amd: Fix geometry.aperture_end for V2 tables
@ 2025-04-17 16:21 Jason Gunthorpe
  2025-04-24  7:56 ` Vasant Hegde
  0 siblings, 1 reply; 7+ messages in thread
From: Jason Gunthorpe @ 2025-04-17 16:21 UTC (permalink / raw)
  To: iommu, Joerg Roedel, Robin Murphy, Vasant Hegde, Will Deacon
  Cc: Joerg Roedel, Jerry Snitselaar, patches, Suravee Suthikulpanit

The AMD IOMMU documentation seems pretty clear that the V2 table follows
the normal CPU expectation of sign extension. This is shown in

  Figure 25: AMD64 Long Mode 4-Kbyte Page Address Translation

Where bits Sign-Extend [63:57] == [56]. This is typical for x86 which
would have three regions in the page table: lower, non-canonical, upper.

The manual describes that the V1 table does not sign extend in section
2.2.4 Sharing AMD64 Processor and IOMMU Page Tables GPA-to-SPA

The iommu domain geometry does not directly support sign extended page
tables. The driver should report only one of the lower/upper spaces. Solve
this by removing the top VA bit from the geometry to use only the lower
space.

Adjust dma_max_address() to do this. It now returns:

5 Level:
  Before 0x1ffffffffffffff
  After  0x0ffffffffffffff
4 Level:
  Before 0xffffffffffff
  After  0x7fffffffffff

Fixes: 11c439a19466 ("iommu/amd/pgtbl_v2: Fix domain max address")
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
---
 drivers/iommu/amd/iommu.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

AMD folks: I'm just reading the documentation, it would be good to confirm
this understanding. I'm a bit surprised nobody hit this, but given the domain
aperture was wildly wrong up till 2023 maybe it never gets exercised
carefully.

diff --git a/drivers/iommu/amd/iommu.c b/drivers/iommu/amd/iommu.c
index dea0fed7abb044..10af3e9d9ea54e 100644
--- a/drivers/iommu/amd/iommu.c
+++ b/drivers/iommu/amd/iommu.c
@@ -2495,8 +2495,14 @@ static inline u64 dma_max_address(enum protection_domain_mode pgtable)
 	if (pgtable == PD_MODE_V1)
 		return ~0ULL;
 
-	/* V2 with 4/5 level page table */
-	return ((1ULL << PM_LEVEL_SHIFT(amd_iommu_gpt_level)) - 1);
+	/*
+	 * V2 with 4/5 level page table. Note that "2.2.6.5 AMD64 4-Kbyte Page
+	 * Translation" shows that the V2 table sign extends the top of the
+	 * address space creating a reserved region in the middle of the
+	 * translation, just like the CPU does. Due to this we can only use half
+	 * of the IOVA.
+	 */
+	return ((1ULL << (PM_LEVEL_SHIFT(amd_iommu_gpt_level) - 1)) - 1);
 }
 
 static bool amd_iommu_hd_support(struct amd_iommu *iommu)

base-commit: 47aadfbd64cdf1f19c83e85ff4ffa9c024619ad6
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-06-12  4:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-17 16:21 [PATCH rc] iommu/amd: Fix geometry.aperture_end for V2 tables Jason Gunthorpe
2025-04-24  7:56 ` Vasant Hegde
2025-04-24 14:06   ` Jason Gunthorpe
2025-04-29  6:03     ` Vasant Hegde
2025-05-28  8:47     ` Vasant Hegde
2025-05-28 11:57       ` Jason Gunthorpe
2025-06-12  4:57         ` Vasant Hegde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).