iommu/amd: Enforce 4k mapping for certain IOMMU data structures
Subsystems (2)
Suravee Suthikulpanit
reviewerPATCH v2 1/1 iommu/amd: Enforce 4k mapping for certain IOMMU data structures
AMD IOMMU requires 4k-aligned pages for the event log, the PPR log, and the completion wait write-back regions. However, when allocating the pages, they could be part of large mapping (e.g. 2M) page. This causes #PF due to the SNP RMP hardware enforces the check based on the page level for these data structures.
So, fix by calling set_memory_4k() on the allocated pages.
--- drivers/iommu/amd/init.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-)
drivers/iommu/amd/init.c +22 -5
@@ -29,6 +29,7 @@#include <asm/iommu_table.h> #include <asm/io_apic.h> #include <asm/irq_remapping.h> +#include <asm/set_memory.h> #include <linux/crash_dump.h>
@@ -672,11 +673,27 @@ static void __init free_command_buffer(struct amd_iommu *iommu)free_pages ((unsigned long )iommu->cmd_buf,get_order (CMD_BUFFER_SIZE )); } +static void *__init iommu_alloc_4k_pages (struct amd_iommu *iommu, +gfp_t gfp,size_t size) +{ +int order =get_order (size); +void *buf = (void *)__get_free_pages (gfp, order); + +if (buf && +iommu_feature (iommu,FEATURE_SNP ) && +set_memory_4k ((unsigned long )buf, (1 << order))) { +free_pages ((unsigned long )buf, order); + buf =NULL ; + } + +return buf; +} +/* allocates the memory where the IOMMU will log its events to */ static int __init alloc_event_buffer (struct amd_iommu *iommu) { - iommu->evt_buf = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, -get_order (EVT_BUFFER_SIZE )); + iommu->evt_buf =iommu_alloc_4k_pages (iommu,GFP_KERNEL | __GFP_ZERO, +EVT_BUFFER_SIZE );return iommu->evt_buf ?0 : -ENOMEM ; }
@@ -715,8 +732,8 @@ static void __init free_event_buffer(struct amd_iommu *iommu)/* allocates the memory where the IOMMU will log its events to */ static int __init alloc_ppr_log (struct amd_iommu *iommu) { - iommu->ppr_log = (void *)__get_free_pages (GFP_KERNEL | __GFP_ZERO, -get_order (PPR_LOG_SIZE )); + iommu->ppr_log =iommu_alloc_4k_pages (iommu,GFP_KERNEL | __GFP_ZERO, +PPR_LOG_SIZE );return iommu->ppr_log ?0 : -ENOMEM ; }
@@ -838,7 +855,7 @@ static int iommu_init_ga(struct amd_iommu *iommu)static int __init alloc_cwwb_sem (struct amd_iommu *iommu) { - iommu->cmd_sem = (void *)get_zeroed_page (GFP_KERNEL ); + iommu->cmd_sem =iommu_alloc_4k_pages (iommu,GFP_KERNEL | __GFP_ZERO,1 );return iommu->cmd_sem ?0 : -ENOMEM ; }
Signature · 1 line
2.17.1
Suravee Suthikulpanit
reviewerWill,
To answer your questions from v1 thread.
Quoted 8 lines · Will Deacon
Quoted 5 lines · Suravee Suthikulpanit
AMD IOMMU requires 4k-aligned pages for the event log, the PPR log, and the completion wait write-back regions. However, when allocating the pages, they could be part of large mapping (e.g. 2M) page. This causes #PF due to the SNP RMP hardware enforces the check based on the page level for these data structures.
Please could you include an example backtrace here?
Unfortunately, we don't actually have the backtrace available here. This information is based on the SEV-SNP specification.
Quoted 5 lines
Quoted 1 line
So, fix by calling
set_memory_4k()on the allocated pages.I think I'm missing something here.
set_memory_4k()will break the kernel linear mapping up into page granular mappings, but the IOMMU isn't using that mapping, right?
That's correct. This does not affect the IOMMU, but it affects the PSP FW.
Quoted 4 lines
It's just using the physical address returned by
iommu_virt_to_phys(), so why does it matter?Just be nice to capture some of this rationale in the log, especially as I'm not familiar with this device.
According to the AMD SEV-SNP white paper (https://www.amd.com/system/files/TechDocs/SEV-SNP-strengthening-vm-isolation-with-integrity-protection-and-more.pdf), the Reverse Map Table (RMP) contains one entry for every 4K page of DRAM that may be used by the VM. In this case, the pages allocated by the IOMMU driver are added as 4K entries in the RMP table by the SEV-SNP FW.
During the page table walk, the RMP checks if the page is owned by the hypervisor. Without calling set_memory_4k() to break the mapping up into 4K pages, pages could end up being part of large mapping (e.g. 2M page), in which the page access would be denied and result in #PF.
Quoted 4 lines
Quoted 1 line
Fixes commit c69d89aff393 ("iommu/amd: Use 4K page for completion wait write-back semaphore")I couldn't figure out how that commit could cause this problem. Please can you explain that to me?
Hope this helps clarify. If so, I'll update the commit log and send out V3.
Thanks,
Suravee
Quoted 42 lines · Suravee Suthikulpanit
Will,
To answer your questions from v1 thread.
Quoted 8 lines · Will Deacon
Quoted 5 lines · Suravee Suthikulpanit
AMD IOMMU requires 4k-aligned pages for the event log, the PPR log, and the completion wait write-back regions. However, when allocating the pages, they could be part of large mapping (e.g. 2M) page. This causes #PF due to the SNP RMP hardware enforces the check based on the page level for these data structures.
Please could you include an example backtrace here?
Unfortunately, we don't actually have the backtrace available here. This information is based on the SEV-SNP specification.
Quoted 3 lines
Quoted 1 line
So, fix by calling
set_memory_4k()on the allocated pages.I think I'm missing something here.
set_memory_4k()will break thekernel
Quoted 1 line
linear mapping up into page granular mappings, but the IOMMU isn't
using
Quoted 1 line
that mapping, right?
That's correct. This does not affect the IOMMU, but it affects the PSP FW.
Quoted 1 line
It's just using the physical address returned by
iommu_virt_to_phys(), so why does it matter?Quoted 2 lines
Just be nice to capture some of this rationale in the log,
especially as
Quoted 1 line
I'm not familiar with this device.
According to the AMD SEV-SNP white paper
(https://www.amd.com/system/files/TechDocs/SEV-SNP-strengthening-vm-isolation-with-integrity-protection-and-more.pdf),
the Reverse Map Table (RMP) contains one entry for every 4K page of DRAM that may be used by the VM. In this case, the pages allocated by the IOMMU driver are added as 4K entries in the RMP table by the SEV-SNP FW.
Quoted 4 lines
During the page table walk, the RMP checks if the page is owned by the hypervisor. Without calling
set_memory_4k()to break the mapping up into 4K pages, pages could end up being part of large mapping (e.g. 2M page), in which the page access would be denied and result in #PF.
Since the page is added as a 4K page in the RMP table by the SEV-SNP FW, so we need to split the physmap to ensure that this page will be access with a 4K mapping from the x86. If the page is part of large page then write access will cause a RMP violation (i.e #PF), this is because SNP hardware enforce that the CPU page level walk must match with page-level programmed in the RMP table.
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Fixes commit c69d89aff393 ("iommu/amd: Use 4K page for completionwait write-back semaphore")
Quoted 2 lines
I couldn't figure out how that commit could cause this problem.
Quoted 8 lines
Please can
Quoted 1 line
you explain that to me?
Hope this helps clarify. If so, I'll update the commit log and send out V3.
Thanks,
Suravee
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Will Deacon
maintainerQuoted 31 lines · Brijesh Singh
Quoted 24 lines · Suravee Suthikulpanit
Quoted 1 line · Will Deacon
I think I'm missing something here.
set_memory_4k()will break thekernel
Quoted 1 line
linear mapping up into page granular mappings, but the IOMMU isn't
using
Quoted 1 line
that mapping, right?
That's correct. This does not affect the IOMMU, but it affects the PSP FW.
Quoted 1 line
It's just using the physical address returned by
iommu_virt_to_phys(), so why does it matter?Quoted 2 lines
Just be nice to capture some of this rationale in the log,
especially as
Quoted 1 line
I'm not familiar with this device.
According to the AMD SEV-SNP white paper
(https://www.amd.com/system/files/TechDocs/SEV-SNP-strengthening-vm-isolation-with-integrity-protection-and-more.pdf),
the Reverse Map Table (RMP) contains one entry for every 4K page of DRAM that may be used by the VM. In this case, the pages allocated by the IOMMU driver are added as 4K entries in the RMP table by the SEV-SNP FW.Quoted 4 lines
During the page table walk, the RMP checks if the page is owned by the hypervisor. Without calling
set_memory_4k()to break the mapping up into 4K pages, pages could end up being part of large mapping (e.g. 2M page), in which the page access would be denied and result in #PF.
Quoted 6 lines
Since the page is added as a 4K page in the RMP table by the SEV-SNP FW, so we need to split the physmap to ensure that this page will be access with a 4K mapping from the x86. If the page is part of large page then write access will cause a RMP violation (i.e #PF), this is because SNP hardware enforce that the CPU page level walk must match with page-level programmed in the RMP table.
Got it; thanks.
Quoted 9 lines
Quoted 9 lines
Quoted 1 line
Quoted 1 line
Fixes commit c69d89aff393 ("iommu/amd: Use 4K page for completionwait write-back semaphore")
Quoted 2 lines
I couldn't figure out how that commit could cause this problem.
Please can
Quoted 1 line
you explain that to me?
Hope this helps clarify. If so, I'll update the commit log and send out V3.
Cheers. No need for a v2, as I've queued this up with a Link: tag.
Will