Add AMD Secure Nested Paging (SEV-SNP)
Subsystems (12)
Ashish Kalra
maintainerv6 cover Add AMD Secure Nested Paging (SEV-SNP)
From: Ashish Kalra <[email protected]>
This part of the Secure Encrypted Paging (SEV-SNP) series focuses on the changes required in a host OS for SEV-SNP support. The series builds upon SEV-SNP Guest Support now part of mainline.
This series provides the basic building blocks to support booting the SEV-SNP VMs, it does not cover all the security enhancement introduced by the SEV-SNP such as interrupt protection.
The CCP driver is enhanced to provide new APIs that use the SEV-SNP specific commands defined in the SEV-SNP firmware specification. The KVM driver uses those APIs to create and managed the SEV-SNP guests.
The GHCB specification version 2 introduces new set of NAE's that is used by the SEV-SNP guest to communicate with the hypervisor. The series provides support to handle the following new NAE events:
- Register GHCB GPA
- Page State Change Request
- Hypevisor feature
- Guest message request
The RMP check is enforced as soon as SEV-SNP is enabled. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raises a page-fault exception. If RMP check failure is due to the page-size mismatch, then split the large page to resolve the fault.
The series does not provide support for the interrupt security and migration and those feature will be added after the base support.
Please note that some areas, such as how private guest pages are managed/pinned/protected, are likely to change once Unmapped Private Memory support is further along in development/design and can be incorporated into this series. We are posting these patches without UPM support for now to hopefully get some review on other aspects of the series in the meantime.
Here is a link to latest UPM v6 patches:
https://lore.kernel.org/linux-mm/[email protected]/
A branch containing these patches is available here:
https://github.com/AMDESE/linux/tree/sev-snp-5.18-rc3-v3
Changes since v5:
* Rebase to 5.18.0-rc3, these patches are just for review so they are based on 5.18.0-rc3 linux-next release as this included the SNP guest patches which weren't in mainline then.
* Using kvm_write_guest() to sync the GHCB scratch buffer can fail due to host mapping being 2M, but RMP being 4K. The page fault handling in do_user_addr_fault() fails to split the 2M page to handle RMP fault due it being called in a non-preemptible context. Instead, use the already kernel mapped ghcb to sync the scratch buffer when the scratch buffer is contained within the GHCB.
* warn and retry failed rmpupdates.
* Fix for stale per-cpu pointer due to cond_resched due during ghcb mapping.
* Multiple fixes for SEV-SNP AP Creation.
* Remove SRCU to synchronize the PSC and gfn mapping replacing it with a spinlock.
* Remove generic post_{map,unmap}_gfn ops, need to revisit these later with respect to UPM support.
* Fix kvm_mmu_get_tdp_walk() to handle "suspicious RCU usage" warning.
* Fix sev_snp_init() to do WBINVD/DF_FLUSH command after SNP_INIT command has been issued.
* Fix sev_free_vcpu() to flush the VMSA page after it is transitioned back to hypervisor state and restored in the kernel direct map.
Changes since v4:
* Move the RMP entry definition to x86 specific header file.
* Move the dump RMP entry function to SEV specific file.
* Use BIT_ULL while defining the #PF bit fields.
* Add helper function to check the IOMMU support for SEV-SNP feature.
* Add helper functions for the page state transition.
* Map and unmap the pages from the direct map after page is added or removed in RMP table.
* Enforce the minimum SEV-SNP firmware version.
* Extend the LAUNCH_UPDATE to accept the base_gfn and remove the logic to calculate the gfn from the hva.
* Add a check in LAUNCH_UPDATE to ensure that all the pages are shared before calling the PSP.
* Mark the memory failure when failing to remove the page from the RMP table or clearing the immutable bit.
* Exclude the encrypted hva range from the KSM.
* Remove the gfn tracking during the kvm_gfn_map() and use SRCU to syncronize the PSC and gfn mapping.
* Allow PSC on the registered hva range only.
* Add support for the Preferred GPA VMGEXIT.
* Simplify the PSC handling routines.
* Use the static_call() for the newly added kvm_x86_ops.
* Remove the long-lived GHCB map.
* Move the snp enable module parameter to the end of the file.
* Remove the kvm_x86_op for the RMP fault handling. Call the fault handler directly from the #NPF interception.
Changes since v3:
* Add support for extended guest message request.
* Add ioctl to query the SNP Platform status.
* Add ioctl to get and set the SNP config.
* Add check to verify that memory reserved for the RMP covers the full system RAM.
* Start the SNP specific commands from 256 instead of 255.
* Multiple cleanup and fixes based on the review feedback.
Changes since v2:
* Add AP creation support.
* Drop the patch to handle the RMP fault for the kernel address.
* Add functions to track the write access from the hypervisor.
* Do not enable the SNP feature when IOMMU is disabled or is in passthrough mode.
* Dump the RMP entry on RMP violation for the debug.
* Shorten the GHCB macro names.
* Start the SNP_INIT command id from 255 to give some gap for the legacy SEV.
* Sync the header with the latest 0.9 SNP spec.
Changes since v1:
* Add AP reset MSR protocol VMGEXIT NAE.
* Add Hypervisor features VMGEXIT NAE.
* Move the RMP table initialization and RMPUPDATE/PSMASH helper in arch/x86/kernel/sev.c.
* Add support to map/unmap SEV legacy command buffer to firmware state when SNP is active.
* Enhance PSP driver to provide helper to allocate/free memory used for the firmware context page.
* Add support to handle RMP fault for the kernel address.
* Add support to handle GUEST_REQUEST NAE event for attestation.
* Rename RMP table lookup helper.
* Drop typedef from rmpentry struct definition.
* Drop SNP static key and use cpu_feature_enabled() to check whether SEV-SNP is active.
* Multiple cleanup/fixes to address Boris review feedback.
Ashish Kalra (1):
KVM: SVM: Sync the GHCB scratch buffer using already mapped ghcb
Brijesh Singh (42):
x86/cpufeatures: Add SEV-SNP CPU feature
iommu/amd: Introduce function to check SEV-SNP support x86/sev: Add the host SEV-SNP initialization support x86/sev: set SYSCFG.MFMD
x86/sev: Add RMP entry lookup helpers
x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction x86/sev: Invalid pages from direct map when adding it to RMP table x86/traps: Define RMP violation #PF error code
x86/fault: Add support to handle the RMP fault for user address x86/fault: Add support to dump RMP entry on fault crypto:ccp: Define the SEV-SNP commands
crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP crypto:ccp: Provide APIs to issue SEV-SNP commands
crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
crypto: ccp: Handle the legacy SEV command when SNP is enabled
crypto: ccp: Add the SNP_PLATFORM_STATUS command
crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
crypto: ccp: Provide APIs to query extended attestation report
KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
KVM: SVM: Add initial SEV-SNP support
KVM: SVM: Add KVM_SNP_INIT command
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
KVM: SVM: Disallow registering memory range from HugeTLB for SNP guest
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
KVM: X86: Keep the NPT and RMP page level in sync
KVM: x86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use
KVM: x86: Define RMP page fault error bits for #NPF
KVM: x86: Update page-fault trace to log full 64-bit error code
KVM: SVM: Do not use long-lived GHCB map while setting scratch area
KVM: SVM: Remove the long-lived GHCB host map
KVM: SVM: Add support to handle GHCB GPA register VMGEXIT
KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT
KVM: SVM: Add support to handle Page State Change VMGEXIT
KVM: SVM: Introduce ops for the post gfn map and unmap
KVM: x86: Export the kvm_zap_gfn_range() for the SNP use
KVM: SVM: Add support to handle the RMP nested page fault
KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
KVM: SVM: Add module parameter to enable the SEV-SNP
ccp: add support to decrypt the page
Michael Roth (2):
*fix for stale per-cpu pointer due to cond_resched during ghcb
mapping
*debug: warn and retry failed rmpupdates
Sean Christopherson (1):
KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX and SNP
Tom Lendacky (3):
KVM: SVM: Add support to handle AP reset MSR protocol
KVM: SVM: Use a VMSA physical address variable for populating VMCB
KVM: SVM: Support SEV-SNP AP Creation NAE event
Documentation/virt/coco/sevguest.rst | 54 + .../virt/kvm/x86/amd-memory-encryption.rst | 102 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/kvm-x86-ops.h | 2 + arch/x86/include/asm/kvm_host.h | 15 + arch/x86/include/asm/msr-index.h | 9 + arch/x86/include/asm/sev-common.h | 28 + arch/x86/include/asm/sev.h | 45 + arch/x86/include/asm/svm.h | 6 + arch/x86/include/asm/trap_pf.h | 18 +- arch/x86/kernel/cpu/amd.c | 3 +- arch/x86/kernel/sev.c | 400 ++++ arch/x86/kvm/lapic.c | 5 +- arch/x86/kvm/mmu.h | 7 +- arch/x86/kvm/mmu/mmu.c | 90 + arch/x86/kvm/svm/sev.c | 1703 ++++++++++++++++- arch/x86/kvm/svm/svm.c | 62 +- arch/x86/kvm/svm/svm.h | 75 +- arch/x86/kvm/trace.h | 40 +- arch/x86/kvm/x86.c | 10 +- arch/x86/mm/fault.c | 84 +- drivers/crypto/ccp/sev-dev.c | 908 ++++++++- drivers/crypto/ccp/sev-dev.h | 17 + drivers/iommu/amd/init.c | 30 + include/linux/iommu.h | 9 + include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 + include/linux/psp-sev.h | 346 ++++ include/linux/sev.h | 32 + include/uapi/linux/kvm.h | 56 + include/uapi/linux/psp-sev.h | 60 + mm/memory.c | 13 + tools/arch/x86/include/asm/cpufeatures.h | 1 + 34 files changed, 4090 insertions(+), 155 deletions(-) create mode 100644 include/linux/sev.h
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 1/49 x86/cpufeatures: Add SEV-SNP CPU feature
From: Brijesh Singh <[email protected]>
Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -407,6 +407,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */ /*
arch/x86/kernel/cpu/amd.c +2 -1
@@ -559,7 +559,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* If the kernel has not enabled SME via any means then * don't advertise the SME feature. * For SEV: If BIOS has not enabled SEV then don't advertise the -* SEV and SEV_ES feature (set in scattered.c). +* SEV, SEV_ES and SEV_SNP feature. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32.
@@ -594,6 +594,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev:setup_clear_cpu_cap (X86_FEATURE_SEV );setup_clear_cpu_cap (X86_FEATURE_SEV_ES ); +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); } }
tools/arch/x86/include/asm/cpufeatures.h +1 -0
@@ -405,6 +405,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */ /*
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 01/49] x86/cpufeatures: Add SEV-SNP CPU feature
Quoted 8 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more.
Signed-off-by Brijesh Singh <[email protected]>
verify_tags: Warning: Sender Ashish Kalra <[email protected]> hasn't signed off on the patch!
When you send someone else's patch, you need to add your SOB underneath it to state that you have handled that patch too, on its way mainline.
While waiting for review, please brush up on the development process by perusing the documentation in Documentation/process/ and especially
Documentation/process/submitting-patches.rst
Quoted 11 lines
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)arch/x86/include/asm/cpufeatures.h +0
-0
arch/x86/include/asm/cpufeatures.h line 410
Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -407,6 +407,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */
Do you not see how there's a space between the '+' and the single-digit number so that the vertical formatting works?
arch/x86/kernel/cpu/amd.c static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) · line 562
Quoted 13 lines
arch/x86/include/asm/cpufeatures.h +0
-0#define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */ /* arch/x86/kernel/cpu/amd.c +1
-1@@ -559,7 +559,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* If the kernel has not enabled SME via any means then * don't advertise the SME feature. * For SEV: If BIOS has not enabled SEV then don't advertise the -* SEV and SEV_ES feature (set in scattered.c). +* SEV, SEV_ES and SEV_SNP feature.
Let's generalize that so that it doesn't get updated with every feature:
"... then don't advertize SEV and any additional functionality based on it."
Quoted 16 lines
arch/x86/kernel/cpu/amd.c +1
-0* * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32. @@ -594,6 +594,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev:setup_clear_cpu_cap (X86_FEATURE_SEV );setup_clear_cpu_cap (X86_FEATURE_SEV_ES ); +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); } }tools/arch/x86/include/asm/cpufeatures.h +0
-0
tools/arch/x86/include/asm/cpufeatures.h line 409
Quoted 5 lines
tools/arch/x86/include/asm/cpufeatures.h +1
-0@@ -405,6 +405,7 @@#define X86_FEATURE_SEV (19*32+ 1)/* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2)/* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3)/* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4)/* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_SME_COHERENT (19*32+10)/* "" AMD hardware-enforced cache coherency */
Ditto.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 2/49 iommu/amd: Introduce function to check SEV-SNP support
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The SEV-SNP support requires that IOMMU must to enabled, see the IOMMU spec section 2.12 for further details. If IOMMU is not enabled or the SNPSup extended feature register is not set then the SNP_INIT command (used for initializing firmware) will fail.
The iommu_sev_snp_supported() can be used to check if IOMMU supports the SEV-SNP feature.
--- drivers/iommu/amd/init.c | 30 ++++++++++++++++++++++++++++++ include/linux/iommu.h | 9 +++++++++ 2 files changed, 39 insertions(+)
drivers/iommu/amd/init.c +30 -0
@@ -3361,3 +3361,33 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64return iommu_pc_get_set_reg (iommu, bank, cntr, fxn, value, true); } + +bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" ); +return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) { +pr_err ("SNPSup is disabled (devid: %02x:%02x.%x)\n" , +PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), +PCI_FUNC (iommu->devid)); +return false; + } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);
include/linux/iommu.h +9 -0
@@ -675,6 +675,12 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,void iommu_sva_unbind_device (struct iommu_sva *handle);u32 iommu_sva_get_pasid (struct iommu_sva *handle); +#ifdef CONFIG_AMD_MEM_ENCRYPT +bool iommu_sev_snp_supported (void ); +#else +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif +#else /* CONFIG_IOMMU_API */ struct iommu_ops {};
@@ -1031,6 +1037,9 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) {return NULL ; } + +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif /* CONFIG_IOMMU_API */ /**
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 26 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The SEV-SNP support requires that IOMMU must to enabled, see the IOMMU spec section 2.12 for further details. If IOMMU is not enabled or the SNPSup extended feature register is not set then the
SNP_INITcommand (used for initializing firmware) will fail.The
iommu_sev_snp_supported()can be used to check if IOMMU supports the SEV-SNP feature.Signed-off-by Brijesh Singh <[email protected]>--- drivers/iommu/amd/init.c | 30 ++++++++++++++++++++++++++++++ include/linux/iommu.h | 9 +++++++++ 2 files changed, 39 insertions(+)drivers/iommu/amd/init.c +1
-0@@ -3361,3 +3361,33 @@ int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64return iommu_pc_get_set_reg (iommu, bank, cntr, fxn, value, true); } +
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3374
Quoted 10 lines
drivers/iommu/amd/init.c +10
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" );
Like below could this say something like snp support is disabled because of iommu settings.
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3384
Quoted 10 lines
drivers/iommu/amd/init.c +10
-0+return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) { + pr_err("SNPSup is disabled (devid: %02x:%02x.%x)\n",
SNPSup might not be obvious to readers, what about " SNP Support is disabled ...".
Also should this have the "SEV-SNP:" prefix like the above log?
Quoted 39 lines
drivers/iommu/amd/init.c +9
-0+PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), + PCI_FUNC(iommu->devid)); +return false; + } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);include/linux/iommu.h +9
-1@@ -675,6 +675,12 @@ struct iommu_sva *iommu_sva_bind_device(struct device *dev,void iommu_sva_unbind_device (struct iommu_sva *handle);u32 iommu_sva_get_pasid (struct iommu_sva *handle); +#ifdef CONFIG_AMD_MEM_ENCRYPT +bool iommu_sev_snp_supported (void ); +#else +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif +#else /* CONFIG_IOMMU_API */ struct iommu_ops {};@@ -1031,6 +1037,9 @@ static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) {return NULL ; } + +static inline bool iommu_sev_snp_supported (void ) {return false; } +#endif /* CONFIG_IOMMU_API */ /** -- 2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
[AMD Official Use Only - General]
Hello Peter,
Quoted 5 lines
Quoted 5 lines
drivers/iommu/amd/init.c +5
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/*
Quoted 8 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 6 lines
drivers/iommu/amd/init.c +6
-0+* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { + pr_err("SEV-SNP: IOMMU is either disabled or + configured in passthrough mode.\n");Like below could this say something like snp support is disabled because of iommu settings.
Here we may need to be more precise with the error information indicating why SNP is not enabled. Please note that this patch may actually become part of the IOMMU + SNP patch series, where additional checks are done, for example, not enabling SNP if IOMMU v2 page tables are enabled, so precise error information will be useful here.
Quoted 12 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 11 lines
drivers/iommu/amd/init.c +11
-0+return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) { + pr_err("SNPSup is disabled (devid: + %02x:%02x.%x)\n",
Quoted 1 line
SNPSup might not be obvious to readers, what about " SNP Support is disabled ...".
Yes, that makes sense.
Quoted 1 line
Also should this have the "SEV-SNP:" prefix like the above log?
Probably, we want to be more consistent with the SNP guest patches and replace SEV-SNP with SNP everywhere, including function names, etc.
Thanks,
Ashish
Re: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 19 lines · Ashish
[AMD Official Use Only - General]
Hello Peter,
Quoted 5 lines
Quoted 5 lines
drivers/iommu/amd/init.c +5
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* Quoted 8 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 6 lines
drivers/iommu/amd/init.c +6
-0+* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { + pr_err("SEV-SNP: IOMMU is either disabled or + configured in passthrough mode.\n");Like below could this say something like snp support is disabled because of iommu settings.
Quoted 4 lines
Here we may need to be more precise with the error information indicating why SNP is not enabled. Please note that this patch may actually become part of the IOMMU + SNP patch series, where additional checks are done, for example, not enabling SNP if IOMMU v2 page tables are enabled, so precise error information will be useful here.
I agree we should be more precise. I just thought we should explicitly state something like: "SEV-SNP: IOMMU is either disabled or configured in passthrough mode, SNP cannot be supported".
Suthikulpanit, Suravee
reviewerRe: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 20 lines · Peter Gonda
Quoted 19 lines · Ashish
[AMD Official Use Only - General]
Hello Peter,
Quoted 13 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 11 lines
drivers/iommu/amd/init.c +11
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { + pr_err("SEV-SNP: IOMMU is either disabled or + configured in passthrough mode.\n");Like below could this say something like snp support is disabled because of iommu settings.
Quoted 8 lines
Quoted 4 lines
Here we may need to be more precise with the error information indicating why SNP is not enabled. Please note that this patch may actually become part of the IOMMU + SNP patch series, where additional checks are done, for example, not enabling SNP if IOMMU v2 page tables are enabled, so precise error information will be useful here.
I agree we should be more precise. I just thought we should explicitly state something like: "SEV-SNP: IOMMU is either disabled or configured in passthrough mode, SNP cannot be supported".
I can update this in the other patch series here
https://lists.linuxfoundation.org/pipermail/iommu/2022-June/066399.html
Thank you,
Suravee
Re: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
In reply to Peter GondaQuoted 20 lines · Peter Gonda
Quoted 19 lines · Ashish
[AMD Official Use Only - General]
Hello Peter,
Quoted 13 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 11 lines
drivers/iommu/amd/init.c +11
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { + pr_err("SEV-SNP: IOMMU is either disabled or + configured in passthrough mode.\n");Like below could this say something like snp support is disabled because of iommu settings.
Quoted 8 lines
Quoted 4 lines
Here we may need to be more precise with the error information indicating why SNP is not enabled. Please note that this patch may actually become part of the IOMMU + SNP patch series, where additional checks are done, for example, not enabling SNP if IOMMU v2 page tables are enabled, so precise error information will be useful here.
I agree we should be more precise. I just thought we should explicitly state something like: "SEV-SNP: IOMMU is either disabled or configured in passthrough mode, SNP cannot be supported".
It really should be, in order to have any practical use:
if (no_iommu) {
pr_err("SEV-SNP: IOMMU is disabled.\n");
return false;
}
if (iommu_default_passthrough()) {
pr_err("SEV-SNP: IOMMU is configured in passthrough mode.\n");
return false;
}The comment is *completely* redundant, it absolutely does not serve any sane purpose. It just tells what the code already clearly stating.
The combo error message on the other hand leaves you to the question "which one was it", and for that reason combining the checks leaves you to a louse debugging experience.
BR, Jarkko
Re: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 42 lines · On Thu, Aug 25, 2022 at 04:28:12AM +0300, [email protected] wrote:
Quoted 20 lines · Peter Gonda
Quoted 19 lines · Ashish
[AMD Official Use Only - General]
Hello Peter,
Quoted 13 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64
Quoted 11 lines
drivers/iommu/amd/init.c +11
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { + pr_err("SEV-SNP: IOMMU is either disabled or + configured in passthrough mode.\n");Like below could this say something like snp support is disabled because of iommu settings.
Quoted 8 lines
Quoted 4 lines
Here we may need to be more precise with the error information indicating why SNP is not enabled. Please note that this patch may actually become part of the IOMMU + SNP patch series, where additional checks are done, for example, not enabling SNP if IOMMU v2 page tables are enabled, so precise error information will be useful here.
I agree we should be more precise. I just thought we should explicitly state something like: "SEV-SNP: IOMMU is either disabled or configured in passthrough mode, SNP cannot be supported".
It really should be, in order to have any practical use:
if (no_iommu) { pr_err("SEV-SNP: IOMMU is disabled.\n"); return false; } if (iommu_default_passthrough()) { pr_err("SEV-SNP: IOMMU is configured in passthrough mode.\n"); return false; }
Quoted 8 lines
The comment is *completely* redundant, it absolutely does not serve any sane purpose. It just tells what the code already clearly stating.
The combo error message on the other hand leaves you to the question "which one was it", and for that reason combining the checks leaves you to a louse debugging experience.
Also, are those really *errors*? That implies that there is something wrong.
Since you can have a legit configuration, IMHO they should be either warn or info. What do you think?
They are definitely not errors.
BR, Jarkko
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
[AMD Official Use Only - General]
Hello Jarkko,
Quoted 20 lines
Quoted 13 lines
It really should be, in order to have any practical use:
if (no_iommu) { pr_err("SEV-SNP: IOMMU is disabled.\n"); return false; } if (iommu_default_passthrough()) { pr_err("SEV-SNP: IOMMU is configured in passthrough mode.\n"); return false; }Quoted 6 lines
The comment is *completely* redundant, it absolutely does not serve any sane purpose. It just tells what the code already clearly stating.
The combo error message on the other hand leaves you to the question "which one was it", and for that reason combining the checks leaves you to a louse debugging experience.
Quoted 5 lines
Also, are those really *errors*? That implies that there is something wrong.
Since you can have a legit configuration, IMHO they should be either warn or info. What do you think?
They are definitely not errors
Yes, they can be warn or info, but as I mentioned above this patch is now part of IOMMU + SNP series, so these comments are now relevant for that.
Thanks,
Ashish
Re: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 24 lines · Ashish
[AMD Official Use Only - General]
Hello Jarkko,
Quoted 19 lines
Quoted 19 lines
It really should be, in order to have any practical use:
if (no_iommu) { pr_err("SEV-SNP: IOMMU is disabled.\n"); return false; } if (iommu_default_passthrough()) { pr_err("SEV-SNP: IOMMU is configured in passthrough mode.\n"); return false; }The comment is *completely* redundant, it absolutely does not serve any sane purpose. It just tells what the code already clearly stating.
The combo error message on the other hand leaves you to the question "which one was it", and for that reason combining the checks leaves you to a louse debugging experience.
Quoted 8 lines
Quoted 5 lines
Also, are those really *errors*? That implies that there is something wrong.
Since you can have a legit configuration, IMHO they should be either warn or info. What do you think?
They are definitely not errors
Yes, they can be warn or info, but as I mentioned above this patch is now part of IOMMU + SNP series, so these comments are now relevant for that.
Yeah, warn/info/error is less relevant than the second point I was making.
It's a good idea to spit out two instead of one to make best of spitting out anything in the first place :-) That way you make no mistake interpreting what does the log message connect to, which can sometimes make a difference while debugging a kernel issue.
BR, Jarkko
Borislav Petkov
maintainerRe: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
In reply to Ashish KalraQuoted 19 lines · Ashish Kalra
drivers/iommu/amd/init.c +19
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" ); +return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) {
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3393
Quoted 10 lines
drivers/iommu/amd/init.c +10
-0+pr_err ("SNPSup is disabled (devid: %02x:%02x.%x)\n" , +PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), +PCI_FUNC (iommu->devid)); +return false; + } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);
Why is this function exported?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
[AMD Official Use Only - General]
Hello Boris,
Quoted 23 lines
Quoted 23 lines
drivers/iommu/amd/init.c +23
-0+bool iommu_sev_snp_supported (void ) +{ +struct amd_iommu *iommu; + +/* +* The SEV-SNP support requires that IOMMU must be enabled, and is +* not configured in the passthrough mode. +*/ +if (no_iommu ||iommu_default_passthrough ()) { +pr_err ("SEV-SNP: IOMMU is either disabled or configured in passthrough mode.\n" ); +return false; + } + +/* +* Iterate through all the IOMMUs and verify the SNPSup feature is +* enabled. +*/ +for_each_iommu (iommu) { +if (!iommu_feature (iommu,FEATURE_SNP )) { +pr_err ("SNPSup is disabled (devid: %02x:%02x.%x)\n" , +PCI_BUS_NUM (iommu->devid),PCI_SLOT (iommu->devid), +PCI_FUNC (iommu->devid)); +return false;
Quoted 8 lines
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3393
Quoted 6 lines
drivers/iommu/amd/init.c +6
-0+ } + } + +return true; +} +EXPORT_SYMBOL_GPL (iommu_sev_snp_supported);Why is this function exported?
This function is required to ensure that IOMMU supports the SEV-SNP feature before enabling the SNP feature and calling SNP_INIT. This IOMMU support check is done in the AMD IOMMU driver with the iommu_sev_snp_supported() function so it is exported by the IOMMU driver and called by sev module later for SNP initialization in snp_rmptable_init().
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
Quoted 5 lines · Ashish
This function is required to ensure that IOMMU supports the SEV-SNP feature before enabling the SNP feature and calling
SNP_INIT. This IOMMU support check is done in the AMD IOMMU driver with theiommu_sev_snp_supported()function so it is exported by the IOMMU driver and called by sev module
What sev module?
The call to iommu_sev_snp_supported() is done by snp_rmptable_init() which is in arch/x86/kernel/sev.c. AFAICT.
And that is not a module. But function exports are done for modules.
So that export looks superfluous.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 02/49] iommu/amd: Introduce function to check SEV-SNP support
[AMD Official Use Only - General]
Hello Boris,
Quoted 7 lines
Quoted 5 lines
This function is required to ensure that IOMMU supports the SEV-SNP feature before enabling the SNP feature and calling
SNP_INIT. This IOMMU support check is done in the AMD IOMMU driver with theiommu_sev_snp_supported()function so it is exported by the IOMMU driver and called by sev moduleWhat sev module?
I meant the kvm-amd module.
Quoted 5 lines
The call to
iommu_sev_snp_supported()is done bysnp_rmptable_init()which is inarch/x86/kernel/sev.c. AFAICT.And that is not a module. But function exports are done for modules.
So that export looks superfluous.
Yes realized this is called in arch/x86/kernel/sev.c, so yes the export is not needed and will remove it.
Thanks,
Ashish
Ashish Kalra
maintainerPATCH v6 3/49 x86/sev: Add the host SEV-SNP initialization support
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP.
The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries.
Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the RMP_BASE and RMP_END MSRs. If the RMP_BASE and RMP_END are not set then disable the SEV-SNP feature.
The SEV-SNP feature is enabled only after the RMP table is successfully initialized.
--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 6 + arch/x86/kernel/sev.c | 144 +++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-)
arch/x86/include/asm/disabled-features.h +7 -1
@@ -68,6 +68,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif +/* * Make sure to add features to the correct mask */
@@ -91,7 +97,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20)#endif /* _ASM_X86_DISABLED_FEATURES_H */
arch/x86/include/asm/msr-index.h +6 -0
@@ -507,6 +507,8 @@#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f
@@ -581,6 +583,10 @@#define MSR_AMD64_SYSCFG 0xc0010010#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT)#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000
arch/x86/kernel/sev.c +144 -0
171 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
Quoted 219 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP.
The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries.
Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the
RMP_BASEandRMP_ENDMSRs. If theRMP_BASEandRMP_ENDare not set then disable the SEV-SNP feature.The SEV-SNP feature is enabled only after the RMP table is successfully initialized.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 6 + arch/x86/kernel/sev.c | 144 +++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-)arch/x86/include/asm/disabled-features.h +7
-1@@ -68,6 +68,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif +/* * Make sure to add features to the correct mask */ @@ -91,7 +97,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20)#endif /* _ASM_X86_DISABLED_FEATURES_H */ arch/x86/include/asm/msr-index.h +6
-0@@ -507,6 +507,8 @@#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f@@ -581,6 +583,10 @@#define MSR_AMD64_SYSCFG 0xc0010010#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT)#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000arch/x86/kernel/sev.c +103
-0@@ -22,6 +22,8 @@#include <linux/efi.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/cpumask.h> +#include <linux/iommu.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> @@ -38,6 +40,7 @@#include <asm/apic.h> #include <asm/cpuid.h> #include <asm/cmdline.h> +#include <asm/iommu.h> #define DR7_RESET_VALUE 0x400@@ -57,6 +60,12 @@#define AP_INIT_CR0_DEFAULT 0x60000010#define AP_INIT_MXCSR_DEFAULT 0x1f80 +/* +* The first 16KB from the RMP_BASE is used by the processor for the +* bookkeeping, the range need to be added during the RMP entry lookup. +*/ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -69,6 +78,10 @@ static struct ghcb *boot_ghcb __section(".data");/* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features__ro_after_init ; +static unsigned long rmptable_start__ro_after_init ; +static unsigned long rmptable_end__ro_after_init ; + +/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -2218,3 +2231,134 @@ static int __init snp_init_platform_device(void)return 0 ; }device_initcall (snp_init_platform_device); + +#undef pr_fmt +#define pr_fmt (fmt) "SEV-SNP: " fmt + +static int __snp_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_SNP_EN ; + val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ; + +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void snp_enable (void *arg) +{ +__snp_enable (smp_processor_id ()); +} + +static bool get_rmptable_info (u64 *start,u64 *len) +{ +u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages; + +rdmsrl (MSR_AMD64_RMP_BASE , rmp_base); +rdmsrl (MSR_AMD64_RMP_END , rmp_end); + +if (!rmp_base || !rmp_end) { +pr_info ("Memory for the RMP table has not been reserved by BIOS\n" ); +return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; + +/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the full system RAM. The reserved memory should also cover the +* RMP table itself. +* +* See PPR Family 19h Model 01h, Revision B1 section 2.1.4.2 for more +* information on memory requirement. +*/ + nr_pages =totalram_pages (); + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) + nr_pages) <<4 ) +RMPTABLE_CPU_BOOKKEEPING_SZ ; + +if (calc_rmp_sz > rmp_sz) { +pr_info ("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n" , + calc_rmp_sz, rmp_sz); +return false; + } + + *start = rmp_base; + *len = rmp_sz; + +pr_info ("RMP table physical address 0x%016llx - 0x%016llx\n" , rmp_base, rmp_end); + +return true; +} + +static __init int __snp_rmptable_init (void ) +{ +u64 rmp_base, sz; +void *start; +u64 val; + +if (!get_rmptable_info (&rmp_base, &sz)) +return 1 ; + + start =memremap (rmp_base, sz,MEMREMAP_WB ); +if (!start) { +pr_err ("Failed to map RMP table 0x%llx+0x%llx\n" , rmp_base, sz); +return 1 ; + } + +/* +* Check if SEV-SNP is already enabled, this can happen if we are coming from +* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz);
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2333
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +/* Flush the caches to ensure that data is written before SNP is enabled. */ +wbinvd_on_all_cpus (); + +/* Enable SNP on all CPUs. */ +on_each_cpu (snp_enable,NULL ,1 ); + +skip_enable: + rmptable_start = (unsigned long )start; + rmptable_end = rmptable_start + sz;
Since in get_rmptable_info() rmp_sz = rmp_end - rmp_base + 1; should this be rmptable_end = rmptable_start + sz - 1;?
Quoted 34 lines
arch/x86/kernel/sev.c +31
-1+ +return 0 ; +} + +static int __init snp_rmptable_init (void ) +{ +if (!boot_cpu_has (X86_FEATURE_SEV_SNP )) +return 0 ; + +if (!iommu_sev_snp_supported ()) + goto nosnp; + +if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); + +return 0 ; + +nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return 1 ; +} + +/* +* This must be called after the PCI subsystem. This is because before enabling +* the SNP feature we need to ensure that IOMMU supports the SEV-SNP feature. +* The iommu_sev_snp_support() is used for checking the feature, and it is +* available after subsys_initcall(). +*/ +fs_initcall (snp_rmptable_init); --2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
[Public]
Hello Peter,
Quoted 28 lines
Quoted 28 lines
arch/x86/kernel/sev.c +28
-0+static __init int __snp_rmptable_init (void ) { +u64 rmp_base, sz; +void *start; +u64 val; + +if (!get_rmptable_info (&rmp_base, &sz)) +return 1 ; + + start =memremap (rmp_base, sz,MEMREMAP_WB ); +if (!start) { +pr_err ("Failed to map RMP table 0x%llx+0x%llx\n" , rmp_base, sz); +return 1 ; + } + +/* +* Check if SEV-SNP is already enabled, this can happen if we are coming from +* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz); + +/* Flush the caches to ensure that data is written before SNP is enabled. */ +wbinvd_on_all_cpus (); +
Quoted 8 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2333
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+/* Enable SNP on all CPUs. */ +on_each_cpu (snp_enable,NULL ,1 ); + +skip_enable: + rmptable_start = (unsigned long )start; + rmptable_end = rmptable_start + sz;Since in
get_rmptable_info()rmp_sz = rmp_end - rmp_base + 1;should this bermptable_end = rmptable_start + sz - 1;?
Yes, it should be.
Thanks,
Ashish
Re: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
In reply to Ashish KalraQuoted 70 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP.
The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries.
Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the
RMP_BASEandRMP_ENDMSRs. If theRMP_BASEandRMP_ENDare not set then disable the SEV-SNP feature.The SEV-SNP feature is enabled only after the RMP table is successfully initialized.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 6 + arch/x86/kernel/sev.c | 144 +++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-)arch/x86/include/asm/disabled-features.h +7
-1@@ -68,6 +68,12 @@# define DISABLE_TDX_GUEST (1 << (X86_FEATURE_TDX_GUEST & 31))#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif +/* * Make sure to add features to the correct mask */ @@ -91,7 +97,7 @@ DISABLE_ENQCMD)#define DISABLED_MASK17 0#define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP)#define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20)#endif /* _ASM_X86_DISABLED_FEATURES_H */ arch/x86/include/asm/msr-index.h +2
-0@@ -507,6 +507,8 @@#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f
arch/x86/include/asm/msr-index.h line 589
Quoted 7 lines
arch/x86/include/asm/msr-index.h +4
-0@@ -581,6 +583,10 @@#define MSR_AMD64_SYSCFG 0xc0010010#define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23#define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT)
nit: The alignment here looks off. The rest of the file left-aligns the macro definition column under a comment header. The bad alignment can be viewed on the github version of this patch:
https://github.com/AMDESE/linux/commit/5101daef92f448c046207b701c0c420b1fce3eaf
Quoted 164 lines
arch/x86/include/asm/msr-index.h +0
-0#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000arch/x86/kernel/sev.c +125
-0@@ -22,6 +22,8 @@#include <linux/efi.h> #include <linux/platform_device.h> #include <linux/io.h> +#include <linux/cpumask.h> +#include <linux/iommu.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> @@ -38,6 +40,7 @@#include <asm/apic.h> #include <asm/cpuid.h> #include <asm/cmdline.h> +#include <asm/iommu.h> #define DR7_RESET_VALUE 0x400@@ -57,6 +60,12 @@#define AP_INIT_CR0_DEFAULT 0x60000010#define AP_INIT_MXCSR_DEFAULT 0x1f80 +/* +* The first 16KB from the RMP_BASE is used by the processor for the +* bookkeeping, the range need to be added during the RMP entry lookup. +*/ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -69,6 +78,10 @@ static struct ghcb *boot_ghcb __section(".data");/* Bitmap of SEV features supported by the hypervisor */ static u64 sev_hv_features__ro_after_init ; +static unsigned long rmptable_start__ro_after_init ; +static unsigned long rmptable_end__ro_after_init ; + +/* #VC handler runtime per-CPU data */ struct sev_es_runtime_data {struct ghcb ghcb_page;@@ -2218,3 +2231,134 @@ static int __init snp_init_platform_device(void)return 0 ; }device_initcall (snp_init_platform_device); + +#undef pr_fmt +#define pr_fmt (fmt) "SEV-SNP: " fmt + +static int __snp_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_SNP_EN ; + val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ; + +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void snp_enable (void *arg) +{ +__snp_enable (smp_processor_id ()); +} + +static bool get_rmptable_info (u64 *start,u64 *len) +{ +u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages; + +rdmsrl (MSR_AMD64_RMP_BASE , rmp_base); +rdmsrl (MSR_AMD64_RMP_END , rmp_end); + +if (!rmp_base || !rmp_end) { +pr_info ("Memory for the RMP table has not been reserved by BIOS\n" ); +return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; + +/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the full system RAM. The reserved memory should also cover the +* RMP table itself. +* +* See PPR Family 19h Model 01h, Revision B1 section 2.1.4.2 for more +* information on memory requirement. +*/ + nr_pages =totalram_pages (); + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) + nr_pages) <<4 ) +RMPTABLE_CPU_BOOKKEEPING_SZ ; + +if (calc_rmp_sz > rmp_sz) { +pr_info ("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n" , + calc_rmp_sz, rmp_sz); +return false; + } + + *start = rmp_base; + *len = rmp_sz; + +pr_info ("RMP table physical address 0x%016llx - 0x%016llx\n" , rmp_base, rmp_end); + +return true; +} + +static __init int __snp_rmptable_init (void ) +{ +u64 rmp_base, sz; +void *start; +u64 val; + +if (!get_rmptable_info (&rmp_base, &sz)) +return 1 ; + + start =memremap (rmp_base, sz,MEMREMAP_WB ); +if (!start) { +pr_err ("Failed to map RMP table 0x%llx+0x%llx\n" , rmp_base, sz); +return 1 ; + } + +/* +* Check if SEV-SNP is already enabled, this can happen if we are coming from +* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz); + +/* Flush the caches to ensure that data is written before SNP is enabled. */ +wbinvd_on_all_cpus (); + +/* Enable SNP on all CPUs. */ +on_each_cpu (snp_enable,NULL ,1 ); + +skip_enable: + rmptable_start = (unsigned long )start; + rmptable_end = rmptable_start + sz; + +return 0 ; +} + +static int __init snp_rmptable_init (void ) +{ +if (!boot_cpu_has (X86_FEATURE_SEV_SNP )) +return 0 ; + +if (!iommu_sev_snp_supported ()) + goto nosnp; +
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2355
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); + +return 0 ; + +nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return 1 ;
Seems odd that we're returning 1 here, rather than 0. I tried to figure out how the initcall return values are used and failed. My impression was 0 means success and a negative number means failure. But maybe this is normal.
Quoted 12 lines
arch/x86/kernel/sev.c +9
-1+} + +/* +* This must be called after the PCI subsystem. This is because before enabling +* the SNP feature we need to ensure that IOMMU supports the SEV-SNP feature. +* The iommu_sev_snp_support() is used for checking the feature, and it is +* available after subsys_initcall(). +*/ +fs_initcall (snp_rmptable_init); --2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
[AMD Official Use Only - General]
Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/sev.c +13
-0+static int __init snp_rmptable_init (void ) { +if (!boot_cpu_has (X86_FEATURE_SEV_SNP )) +return 0 ; + +if (!iommu_sev_snp_supported ()) + goto nosnp; + +if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"x86/rmptable_init:online" , __snp_enable,NULL ); +
Quoted 8 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2355
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+return 0 ; + +nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return 1 ;Seems odd that we're returning 1 here, rather than 0. I tried to figure out how the initcall return values are used and failed. My impression was 0 means success and a negative number means failure. But maybe this is normal.
I think that initcall values are typically ignored, but it should return 0 on success and negative on error. So probably should fix this to return something like -ENOSYS instead of 1.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
In reply to Ashish KalraQuoted 3 lines · Ashish Kalra
+/*
+ * The first 16KB from theRMP_BASEis used by the processor for the
+ * bookkeeping, the range need to be added during the RMP entry lookup.
needs
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+static int __snp_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_SNP_EN ; + val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ;
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2258
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void snp_enable (void *arg) +{ +__snp_enable (smp_processor_id ()); +}
Get rid of that silly wrapper - you're not even using that @cpu argument.
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2268
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+static bool get_rmptable_info (u64 *start,u64 *len) +{ +u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages; + +rdmsrl (MSR_AMD64_RMP_BASE , rmp_base); +rdmsrl (MSR_AMD64_RMP_END , rmp_end); + +if (!rmp_base || !rmp_end) { +pr_info ("Memory for the RMP table has not been reserved by BIOS\n" );
pr_err
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2276
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; + +/* +* Calculate the amount the memory that must be reserved by the BIOS to +* address the full system RAM. The reserved memory should also cover the
"... address the whole RAM."
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2280
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+* RMP table itself. +* +* See PPR Family 19h Model 01h, Revision B1 section 2.1.4.2 for more +* information on memory requirement.
That section number will change over time - if you want to refer to some section just use its title so that people can at least grep for the relevant text.
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2283
Quoted 3 lines
arch/x86/kernel/sev.c +3
-0+*/ + nr_pages =totalram_pages (); + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) + nr_pages) <<4 ) +RMPTABLE_CPU_BOOKKEEPING_SZ ;
use totalram_pages() directly and get rid of nr_pages.
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2287
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ +if (calc_rmp_sz > rmp_sz) { +pr_info ("Memory reserved for the RMP table does not cover full system RAM (expected 0x%llx got 0x%llx)\n" , + calc_rmp_sz, rmp_sz);
pr_err
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2294
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+return false; + } + + *start = rmp_base; + *len = rmp_sz; + +pr_info ("RMP table physical address 0x%016llx - 0x%016llx\n" , rmp_base, rmp_end);
"RMP table physical address range: ...[0x.. - 0x..]"
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2299
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ +return true; +} + +static __init int __snp_rmptable_init (void )
s/int/bool/
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2310
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+{ +u64 rmp_base, sz; +void *start; +u64 val; + +if (!get_rmptable_info (&rmp_base, &sz)) +return 1 ; + + start =memremap (rmp_base, sz,MEMREMAP_WB ); +if (!start) { +pr_err ("Failed to map RMP table 0x%llx+0x%llx\n" , rmp_base, sz);
^^^^^^
either write the size in decimal or do a normal interval.
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2315
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+return 1 ; + } + +/* +* Check if SEV-SNP is already enabled, this can happen if we are coming from
Who is "we"?
Pls get rid of all "we" in the comments and use passive formulations.
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2323
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz);
Do I understand it correctly that in the kexec case the second, kexec-ed kernel is reusing the previous kernel's RMP table so it should not be cleared?
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+ +/* Flush the caches to ensure that data is written before SNP is enabled. */ +wbinvd_on_all_cpus (); + +/* Enable SNP on all CPUs. */ +on_each_cpu (snp_enable,NULL ,1 ); +
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2340
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+skip_enable: + rmptable_start = (unsigned long )start; + rmptable_end = rmptable_start + sz; + +return 0 ; +} + +static int __init snp_rmptable_init (void ) +{ +if (!boot_cpu_has (X86_FEATURE_SEV_SNP ))
cpu_feature_enabled
Quoted 12 lines
arch/x86/kernel/sev.c +12
-0+return 0 ; + +if (!iommu_sev_snp_supported ()) + goto nosnp; + +if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); + +return 0 ; +
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2362
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return 1 ; +} + +/* +* This must be called after the PCI subsystem. This is because before enabling +* the SNP feature we need to ensure that IOMMU supports the SEV-SNP feature. +* The iommu_sev_snp_support() is used for checking the feature, and it is +* available after subsys_initcall().
I'd much more appreciate here a short formulation explaining why is IOMMU needed for SNP rather than the obvious.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
[AMD Official Use Only - General]
Hello Boris,
Quoted 4 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2280
Quoted 2 lines
arch/x86/kernel/sev.c +2
-0+* See PPR Family 19h Model 01h, Revision B1 section 2.1.4.2 for more +* information on memory requirement. That section number will change over time - if you want to refer to some section just use its title so that people can at least grep for the relevant text.
This will all go into sev.c, instead of the header file, as this is non-architectural and per-processor and the structure won't be exposed to the rest of the kernel. The above PPR reference and potentially in future an architectural method of reading the RMP table entries will be moved into it.
Quoted 6 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void)
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+*/ + nr_pages =totalram_pages (); + calc_rmp_sz = (((rmp_sz >>PAGE_SHIFT ) + nr_pages) <<4 ) + +RMPTABLE_CPU_BOOKKEEPING_SZ ;use
totalram_pages()directly and get rid ofnr_pages.
Ok.
Quoted 10 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2323
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+* kexec boot. +*/ +rdmsrl (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) + goto skip_enable; + +/* Initialize the RMP table to zero */ +memset (start,0 , sz);Do I understand it correctly that in the kexec case the second, kexec-ed kernel is reusing the previous kernel's RMP table so it should not be cleared?
I believe that with kexec and after issuing the shutdown command, the RMP table needs to be fully initialized, so we should be re-initializing the RMP table to zero here.
Quoted 6 lines
arch/x86/kernel/sev.c static int __init snp_init_platform_device(void) · line 2340
Quoted 4 lines
arch/x86/kernel/sev.c +3
-0+ +static int __init snp_rmptable_init (void ) { +if (!boot_cpu_has (X86_FEATURE_SEV_SNP ))
cpu_feature_enabled
Ok.
Quoted 19 lines
Quoted 19 lines
arch/x86/kernel/sev.c +19
-0+return 0 ; + +if (!iommu_sev_snp_supported ()) + goto nosnp; + +if (__snp_rmptable_init ()) + goto nosnp; + +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , +__snp_enable,NULL ); + +return 0 ; + +nosnp: +setup_clear_cpu_cap (X86_FEATURE_SEV_SNP ); +return 1 ; +} + +/*
Quoted 8 lines
Review
Quoted 6 lines
+ * This must be called after the PCI subsystem. This is because
(quoted diff) +5
-0+before enabling + * the SNP feature we need to ensure that IOMMU supports the SEV-SNP feature. + * The iommu_sev_snp_support() is used for checking the feature, and +it is + * available after subsys_initcall().I'd much more appreciate here a short formulation explaining why is IOMMU needed for SNP rather than the obvious.
Yes, IOMMU is enforced for SNP to ensure that HV cannot program DMA directly into guest private memory. In case of SNP, the IOMMU makes sure that the page(s) used for DMA are HV owned.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
Quoted 9 lines · Ashish
Quoted 3 lines
That section number will change over time - if you want to refer to some section just use its title so that people can at least grep for the relevant text.
This will all go into
sev.c, instead of the header file, as this is non-architectural and per-processor and the structure won't be exposed to the rest of the kernel. The above PPR reference and potentially in future an architectural method of reading the RMP table entries will be moved into it.
I fail to see how this addresses my comment... All I'm saying is, the "section 2.1.4.2" number will change so don't quote it in the text but quote the section *name* instead.
Quoted 3 lines
I believe that with kexec and after issuing the shutdown command, the RMP table needs to be fully initialized, so we should be re-initializing the RMP table to zero here.
And yet you're doing:
/*
* Check if SEV-SNP is already enabled, this can happen if we are coming from
* kexec boot.
*/
rdmsrl(MSR_AMD64_SYSCFG, val);
if (val & MSR_AMD64_SYSCFG_SNP_EN)
goto skip_enable; <-------- skip zeroingSo which is it?
Quoted 3 lines
Yes, IOMMU is enforced for SNP to ensure that HV cannot program DMA directly into guest private memory. In case of SNP, the IOMMU makes sure that the page(s) used for DMA are HV owned.
Yes, now put that in the comment above the
fs_initcall(snp_rmptable_init);
line.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 03/49] x86/sev: Add the host SEV-SNP initialization support
[AMD Official Use Only - General]
Hello Boris,
Quoted 11 lines
Quoted 9 lines
Quoted 3 lines
That section number will change over time - if you want to refer to some section just use its title so that people can at least grep for the relevant text.
This will all go into
sev.c, instead of the header file, as this is non-architectural and per-processor and the structure won't be exposed to the rest of the kernel. The above PPR reference and potentially in future an architectural method of reading the RMP table entries will be moved into it.I fail to see how this addresses my comment... All I'm saying is, the "section 2.1.4.2" number will change so don't quote it in the text but quote the section *name* instead.
Yes I agree with your comments, all I am saying is that these comments will move into sev.c instead of the header file.
Quoted 14 lines
Quoted 3 lines
I believe that with kexec and after issuing the shutdown command, the RMP table needs to be fully initialized, so we should be re-initializing the RMP table to zero here.
And yet you're doing:
/* * Check if SEV-SNP is already enabled, this can happen if we are coming from * kexec boot. */ rdmsrl(MSR_AMD64_SYSCFG, val); if (val & MSR_AMD64_SYSCFG_SNP_EN) goto skip_enable; <-------- skip zeroing
Quoted 1 line
So which is it?
Again what I meant is that this will be fixed to reset the RMP table for kexec boot too.
Quoted 9 lines
Quoted 5 lines
Yes, IOMMU is enforced for SNP to ensure that HV cannot program DMA directly into guest private memory. In case of SNP, the IOMMU makes sure that the page(s) used for DMA are HV owned.
Yes, now put that in the comment above the
fs_initcall(snp_rmptable_init);line.
Yes.
Thanks,
Ashish
Ashish Kalra
maintainerPATCH v6 4/49 x86/sev: set SYSCFG.MFMD
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be set.
Subsequent CCP patches while require 1.51 as the minimum SEV-SNP firmware version.
--- arch/x86/include/asm/msr-index.h | 3 +++ arch/x86/kernel/sev.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+)
arch/x86/include/asm/msr-index.h +3 -0
@@ -587,6 +587,9 @@#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT)#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT) +#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000
arch/x86/kernel/sev.c +24 -0
@@ -2257,6 +2257,27 @@ static __init void snp_enable(void *arg)__snp_enable (smp_processor_id ()); } +static int __mfdm_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_MFDM ; + +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void mfdm_enable (void *arg) +{ +__mfdm_enable (smp_processor_id ()); +} +static bool get_rmptable_info (u64 *start,u64 *len) {u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages;
@@ -2325,6 +2346,9 @@ static __init int __snp_rmptable_init(void)/* Flush the caches to ensure that data is written before SNP is enabled. */ wbinvd_on_all_cpus (); +/* MFDM must be enabled on all the CPUs prior to enabling SNP. */ +on_each_cpu (mfdm_enable,NULL ,1 ); +/* Enable SNP on all CPUs. */ on_each_cpu (snp_enable,NULL ,1 );
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 04/49] x86/sev: set SYSCFG.MFMD
Quoted 19 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be set.
Subsequent CCP patches while require 1.51 as the minimum SEV-SNP firmware version.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/msr-index.h | 3 +++ arch/x86/kernel/sev.c | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+)arch/x86/include/asm/msr-index.h +0
-0
arch/x86/include/asm/msr-index.h line 591
Quoted 5 lines
arch/x86/include/asm/msr-index.h +2
-0@@ -587,6 +587,9 @@#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT)#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) +#define MSR_AMD64_SYSCFG_MFDM_BIT 19 +#define MSR_AMD64_SYSCFG_MFDM BIT_ULL(MSR_AMD64_SYSCFG_MFDM_BIT)
nit: Similar to the previous patch, the alignment here doesn't look right. The bad alignment can be viewed on the github version of this patch:
https://github.com/AMDESE/linux/commit/6d4469b86f90e67119ff110230857788a0d9dbd0
Quoted 12 lines
arch/x86/include/asm/msr-index.h +1
-0+#define MSR_K8_INT_PENDING_MSG 0xc0010055/* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000arch/x86/kernel/sev.c +0
-0@@ -2257,6 +2257,27 @@ static __init void snp_enable(void *arg)__snp_enable (smp_processor_id ()); }
arch/x86/kernel/sev.c static __init void snp_enable(void *arg) · line 2269
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+static int __mfdm_enable (unsigned int cpu) +{ +u64 val; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return 0 ; + +rdmsrl (MSR_AMD64_SYSCFG , val); + + val |=MSR_AMD64_SYSCFG_MFDM ;
Can we do this inside __snp_enable(), above? Then, we'll execute if a hotplug event happens as well.
static int __snp_enable(unsigned int cpu)
{
u64 val;
if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))
return 0;
rdmsrl(MSR_AMD64_SYSCFG, val);
val |= MSR_AMD64_SYSCFG_SNP_EN;
val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN;
val |= MSR_AMD64_SYSCFG_MFDM;
wrmsrl(MSR_AMD64_SYSCFG, val);
return 0;
}Quoted 27 lines
arch/x86/kernel/sev.c +14
-1+ +wrmsrl (MSR_AMD64_SYSCFG , val); + +return 0 ; +} + +static __init void mfdm_enable (void *arg) +{ +__mfdm_enable (smp_processor_id ()); +} +static bool get_rmptable_info (u64 *start,u64 *len) {u64 calc_rmp_sz, rmp_sz, rmp_base, rmp_end, nr_pages;@@ -2325,6 +2346,9 @@ static __init int __snp_rmptable_init(void)/* Flush the caches to ensure that data is written before SNP is enabled. */ wbinvd_on_all_cpus (); +/* MFDM must be enabled on all the CPUs prior to enabling SNP. */ +on_each_cpu (mfdm_enable,NULL ,1 ); +/* Enable SNP on all CPUs. */ on_each_cpu (snp_enable,NULL ,1 ); --2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 04/49] x86/sev: set SYSCFG.MFMD
In reply to Ashish KalraQuoted 1 line · Ashish Kalra
Subject: [PATCH Part2 v6 04/49] x86/sev: set SYSCFG.MFMD
That subject title needs to be made human readable.
Quoted 3 lines
From: Brijesh Singh <[email protected]>
SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be set.
Because?
Also, commit message needs to be human-readable and not pseudocode.
arch/x86/kernel/sev.c static __init int __snp_rmptable_init(void) · line 2353
Quoted 9 lines
arch/x86/kernel/sev.c +3
-0@@ -2325,6 +2346,9 @@ static __init int __snp_rmptable_init(void)/* Flush the caches to ensure that data is written before SNP is enabled. */ wbinvd_on_all_cpus (); +/* MFDM must be enabled on all the CPUs prior to enabling SNP. */ +on_each_cpu (mfdm_enable,NULL ,1 ); +/* Enable SNP on all CPUs. */ on_each_cpu (snp_enable,NULL ,1 );
No, not two IPI generating function calls - one and do everything in it. I.e., what Marc said.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 04/49] x86/sev: set SYSCFG.MFMD
[AMD Official Use Only - General]
Hello Boris,
Quoted 3 lines
Quoted 1 line
Subject: [PATCH Part2 v6 04/49] x86/sev: set SYSCFG.MFMD
That subject title needs to be made human readable.
Ok.
Quoted 3 lines
Quoted 1 line
SEV-SNP FW >= 1.51 requires that SYSCFG.MFMD must be set.
Because?
This is a FW requirement.
Quoted 6 lines
Also, commit message needs to be human-readable and not pseudocode.
Quoted 4 lines
arch/x86/kernel/sev.c +0
-0@@ -2325,6 +2346,9 @@ static __init int __snp_rmptable_init(void)/* Flush the caches to ensure that data is written before SNP is enabled. */ wbinvd_on_all_cpus ();
Quoted 8 lines
arch/x86/kernel/sev.c static __init int __snp_rmptable_init(void) · line 2353
Quoted 5 lines
arch/x86/kernel/sev.c +3
-0+/* MFDM must be enabled on all the CPUs prior to enabling SNP. */ +on_each_cpu (mfdm_enable,NULL ,1 ); +/* Enable SNP on all CPUs. */ on_each_cpu (snp_enable,NULL ,1 );No, not two IPI generating function calls - one and do everything in it. I.e., what Marc said.
Ok got that.
Thanks,
Ashish
Ashish Kalra
maintainerPATCH v6 5/49 x86/sev: Add RMP entry lookup helpers
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The snp_lookup_page_in_rmptable() can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.
--- arch/x86/include/asm/sev.h | 27 ++++++++++++++++++++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ include/linux/sev.h | 30 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 include/linux/sev.h
arch/x86/include/asm/sev.h +27 -0
@@ -9,6 +9,7 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <linux/sev.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/bootparam.h>
@@ -84,6 +85,32 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) + +/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct __packed rmpentry { +union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 :9 , + gpa :39 , + asid :10 , + vmsa :1 , + validated :1 , + rsvd2 :1 ; + } info; +u64 low; + }; +u64 high; +}; + +#define rmpentry_assigned (x) ((x)->info.assigned) +#define rmpentry_pagesize (x) ((x)->info.pagesize)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)
arch/x86/kernel/sev.c +43 -0
52 lines not on this page · raw message
include/linux/sev.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]> +*/ + +#ifndef __LINUX_SEV_H +#define __LINUX_SEV_H + +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_lookup_rmpentry (u64 pfn,int *level); +int psmash (u64 pfn); +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable); +int rmp_make_shared (u64 pfn,enum pg_level level); +#else +static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; } +static inline int psmash (u64 pfn) {return -ENXIO ; } +static inline int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid, +bool immutable) +{ +return -ENODEV ; +} +static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 4 lines · Ashish Kalra
+/*
+ * The RMP entry format is not architectural. The format is defined in PPR
+ * Family 19h Model 01h, Rev B1 processor.
+ */
Let's say that Family 20h comes out and has a new RMP entry format. What keeps an old kernel from attempting to use this old format on that new CPU?
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
Quoted 8 lines
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 93
Quoted 5 lines
arch/x86/include/asm/sev.h +5
-0+/* +* The RMP entry format is not architectural. The format is defined +in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ Let's say that Family 20h comes out and has a new RMP entry format. What keeps an old kernel from attempting to use this old format on that new CPU?
As I replied previously on the same subject:
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Thanks,
Ashish
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 5 lines · Ashish
As I replied previously on the same subject: Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Yeah, arch-independent and non-architectural are quite different concepts.
At Intel, at least, when someone says "not architectural" mean that the behavior is implementation-specific. That, combined with the model/family/stepping gave me the wrong impression about what was going on.
Some more clarity would be greatly appreciated.
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[Public]
Quoted 9 lines · Ashish
Quoted 4 lines
As I replied previously on the same subject: Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Yeah, arch-independent and non-architectural are quite different concepts.
At Intel, at least, when someone says "not architectural" mean that the behavior is implementation-specific. That, combined with the model/family/stepping gave me the wrong impression about what was going on.
Some more clarity would be greatly appreciated.
Actually, the PPR for family 19h Model 01h, Rev B1 defines the RMP entry format as below:
2.1.4.2 RMP Entry Format
Architecturally the format of RMP entries are not specified in APM. In order to assist software, the following table specifies select portions of the RMP entry format for this specific product. Each RMP entry is 16B in size and is formatted as follows. Software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. So non-architectural in this context means that it is only defined in our PPR.
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the dump_rmpentry() code.
Thanks,
Ashish
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 3 lines · Ashish
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the
dump_rmpentry()code.
So, if the RMP entry format changes in future processors, how do we make sure that the kernel does not try to use *this* code on those processors?
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
-----Original Message-----
From: Dave Hansen <[email protected]>
Sent: Wednesday, June 22, 2022 1:18 PM
To: Kalra, Ashish <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 5 lines · Ashish
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the
dump_rmpentry()code.So, if the RMP entry format changes in future processors, how do we make sure that the kernel does not try to use *this* code on those processors?
Functions snp_lookup_rmpentry() and dump_rmpentry() which rely on this structure definition will need to handle it accordingly.
Thanks,
Ashish
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 5 lines · Ashish
Quoted 3 lines
So, if the RMP entry format changes in future processors, how do we make sure that the kernel does not try to use *this* code on those processors?
Functions
snp_lookup_rmpentry()anddump_rmpentry()which rely on this structure definition will need to handle it accordingly.
In other words, old kernels will break on new hardware?
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus snp_lookup_rmpentry() and dump_rmpentry()) code on a newer CPU, the kernel should refuse.
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
[AMD Official Use Only - General]
________________________________
From: Dave Hansen <[email protected]>
Sent: Wednesday, June 22, 2022 1:43 PM
To: Kalra, Ashish <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Lendacky, Thomas <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Roth, Michael <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 6 lines · Ashish
Quoted 3 lines
So, if the RMP entry format changes in future processors, how do we make sure that the kernel does not try to use *this* code on those processors?
Functions
snp_lookup_rmpentry()anddump_rmpentry()which rely on this structure definition will need to handle it accordingly.
Quoted 6 lines
In other words, old kernels will break on new >hardware?
I think that needs to be fixed. It should be as >simple as a model/family check, though. If someone (for >example) attempts to use SNP (and thus
snp_lookup_rmpentry()and >dump_rmpentry()) code on a newer CPU, the kernel should refuse.
More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Dave Hansen[AMD Official Use Only - General]
-----Original Message-----
From: Dave Hansen <[email protected]>
Sent: Wednesday, June 22, 2022 1:43 PM
To: Kalra, Ashish <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 9 lines · Ashish
Quoted 5 lines
Quoted 3 lines
So, if the RMP entry format changes in future processors, how do we make sure that the kernel does not try to use *this* code on those processors?
Functions
snp_lookup_rmpentry()anddump_rmpentry()which rely on this structure definition will need to handle it accordingly.In other words, old kernels will break on new hardware?
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.
More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
Thanks,
Ashish
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 7 lines · Ashish
Quoted 4 lines
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
That will be helpful down the road when there's more than one format. But, the real issue is that the kernel doesn't *support* a different RMP format. So, the SNP support should be disabled when encountering a model/family other than the known good one.
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
From: Dave Hansen <[email protected]>
Sent: Wednesday, June 22, 2022 2:50 PM
To: Kalra, Ashish <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 10 lines · Ashish
Quoted 7 lines
Quoted 4 lines
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
That will be helpful down the road when there's more than one format.
But, the real issue is that the kernel doesn't *support* a different RMP format. So, the SNP support should be disabled when encountering a model/family other than the known good one.
Yes, that makes sense, will add an additional check in snp_rmptable_init().
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[Public]
From: Dave Hansen <[email protected]>
Sent: Wednesday, June 22, 2022 2:50 PM
To: Kalra, Ashish <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 4 lines · Ashish
Quoted 4 lines
Quoted 4 lines
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.
Quoted 8 lines
Quoted 3 lines
More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
That will be helpful down the road when there's more than one format.
But, the real issue is that the kernel doesn't *support* a different RMP format. So, the SNP support should be disabled when encountering a model/family other than the known good one.Yes, that makes sense, will add an additional check in
snp_rmptable_init().
Also to add here, additionally we may create an architectural way to read the RMP entry in the future.
Thanks,
Ashish
Sean Christopherson
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Kalra, AshishQuoted 9 lines · Ashish
Quoted 7 lines · Ashish
Quoted 7 lines
Quoted 4 lines
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
Quoted 6 lines
Quoted 4 lines
That will be helpful down the road when there's more than one format. But, the real issue is that the kernel doesn't *support* a different RMP format. So, the SNP support should be disabled when encountering a model/family other than the known good one.
Yes, that makes sense, will add an additional check in
snp_rmptable_init().
And as I suggested in v5[*], bury the microarchitectural struct in sev.c so that nothing outside of the few bits of SNP code that absolutely need to know the layout of the struct should even be aware that there's a struct overlay for RMP entries.
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
Quoted 9 lines
Quoted 9 lines · Ashish
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
I think that needs to be fixed. It should be as simple as a model/family check, though. If someone (for example) attempts to use SNP (and thus
snp_lookup_rmpentry()anddump_rmpentry()) code on a newer CPU, the kernel should refuse.More specifically I am thinking of adding RMP entry field accessors so that they can do this cpu model/family check and return the correct field as per processor architecture.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
That will be helpful down the road when there's more than one format. But, the real issue is that the kernel doesn't *support* a different RMP format. So, the SNP support should be disabled when encountering a model/family other than the known good one.
Yes, that makes sense, will add an additional check in
snp_rmptable_init().And as I suggested in v5[*], bury the microarchitectural struct in
sev.cso that nothing outside of the few bits of SNP code that absolutely need to know the layout of the struct should even be aware that there's a struct overlay for RMP entries.
Yes, that's a nice way to hide it from the rest of the kernel which does not require access to this structure anyway, in essence, it becomes a private structure.
Thanks,
Ashish
Quoted 1 line
[*] https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fall%2FYPCAZaROOHNskGlO%40google.com&data=05%7C01%7CAshish.Kalra%40amd.com%7Ce210ec383f654556348c08da5568ca81%>7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637916205851843411%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=6TOpchjhgFg%>2F5JTa%2FqSviiTuehNoZgvTVBuZv6JxsXc%3D&reserved=0
Borislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 3 lines · Ashish
Yes, that's a nice way to hide it from the rest of the kernel which does not require access to this structure anyway, in essence, it becomes a private structure.
So this whole discussion whether there should be a model check or not in case a new RMP format gets added in the future is moot - when a new model format comes along, *then* the distinction should be done and added in code - not earlier.
This is nothing else but normal CPU enablement work - it should be done when it is really needed.
Because the opposite can happen: you can add a model check which excludes future model X, future model X comes along but does *not* change the RMP format and then you're going to have to relax that model check again to fix SNP on the new model X.
So pls add the model checks only when really needed.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteSean Christopherson
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 9 lines · Borislav Petkov
Quoted 3 lines · Ashish
Yes, that's a nice way to hide it from the rest of the kernel which does not require access to this structure anyway, in essence, it becomes a private structure.
So this whole discussion whether there should be a model check or not in case a new RMP format gets added in the future is moot - when a new model format comes along, *then* the distinction should be done and added in code - not earlier.
I disagree. Running an old kernel on new hardware with a different RMP layout should refuse to use SNP, not read/write garbage and likely corrupt the RMP and/or host memory.
And IMO, hiding the non-architectural RMP format in SNP-specific code so that we don't have to churn a bunch of call sites that don't _need_ access to the raw RMP format is a good idea regardless of whether we want to be optimistic or pessimistic about future formats.
Quoted 17 lines
This is nothing else but normal CPU enablement work - it should be done when it is really needed.
Because the opposite can happen: you can add a model check which excludes future model X, future model X comes along but does *not* change the RMP format and then you're going to have to relax that model check again to fix SNP on the new model X.
So pls add the model checks only when really needed.
Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
Borislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 3 lines · Sean Christopherson
I disagree. Running an old kernel on new hardware with a different RMP layout should refuse to use SNP, not read/write garbage and likely corrupt the RMP and/or host memory.
See my example below.
Quoted 4 lines
And IMO, hiding the non-architectural RMP format in SNP-specific code so that we don't have to churn a bunch of call sites that don't _need_ access to the raw RMP format is a good idea regardless of whether we want to be optimistic or pessimistic about future formats.
I don't think I ever objected to that.
Quoted 3 lines
Quoted 3 lines
This is nothing else but normal CPU enablement work - it should be done when it is really needed.
<--- this here.
Quoted 4 lines
Quoted 4 lines
Because the opposite can happen: you can add a model check which excludes future model X, future model X comes along but does *not* change the RMP format and then you're going to have to relax that model check again to fix SNP on the new model X.
So constantly adding new models to a list which support a certain version of the RMP format doesn't scale either.
If you corrupt the RMP because your kernel is old, you'll crash and burn very visibly so that you'll be forced to have to look for an updated kernel regardless.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Btw,
what could work is to spec only a *version* field somewhere in the HW or FW which says which version the RMP header has.
Then, OS would check that field and if it doesn't support that certain version, it'll bail.
I'd need to talk to folks first, though, what the whole story is behind not spec-ing the RMP format...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
As I mentioned before, in the future we will have architectural support to read RMP table entries, we will first check for availability of this feature and use it always if it is supported and enabled, and only fallback to doing raw RMP table access if this architectural support is not available.
Thanks,
Ashish
-----Original Message-----
From: Borislav Petkov <[email protected]>
Sent: Friday, July 22, 2022 2:38 PM
To: Sean Christopherson <[email protected]>
Cc: Kalra, Ashish <[email protected]>; Dave Hansen <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Btw,
what could work is to spec only a *version* field somewhere in the HW or FW which says which version the RMP header has.
Then, OS would check that field and if it doesn't support that certain version, it'll bail.
I'd need to talk to folks first, though, what the whole story is behind not spec-ing the RMP format...
--
Regards/Gruss,
Boris.
Sean Christopherson
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Borislav PetkovQuoted 5 lines · Borislav Petkov
Quoted 3 lines · Sean Christopherson
I disagree. Running an old kernel on new hardware with a different RMP layout should refuse to use SNP, not read/write garbage and likely corrupt the RMP and/or host memory.
Quoted 8 lines
See my example below.
Quoted 4 lines
And IMO, hiding the non-architectural RMP format in SNP-specific code so that we don't have to churn a bunch of call sites that don't _need_ access to the raw RMP format is a good idea regardless of whether we want to be optimistic or pessimistic about future formats.
I don't think I ever objected to that.
Yar, just wanted to be make sure we're all on the same page, I wasn't entirely sure what was get nacked :-)
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
This is nothing else but normal CPU enablement work - it should be done when it is really needed.
<--- this here.
Quoted 7 lines
Quoted 4 lines
Quoted 4 lines
Because the opposite can happen: you can add a model check which excludes future model X, future model X comes along but does *not* change the RMP format and then you're going to have to relax that model check again to fix SNP on the new model X.
So constantly adding new models to a list which support a certain version of the RMP format doesn't scale either.
Yeah, but either we get AMD to give us an architectural layout or we'll have to eat that cost at some point in the future.
Quoted 3 lines
If you corrupt the RMP because your kernel is old, you'll crash and burn very visibly so that you'll be forced to have to look for an updated kernel regardless.
Heh, you're definitely more optimistic than me. I can just see something truly ridiculous happening like moving the page size bit and then getting weird behavior only when KVM happens to need the page size for some edge case.
Anyways, it's not a sticking point, and I certainly am not volunteering to maintain the FMS list...
Borislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
Quoted 2 lines · Sean Christopherson
Yar, just wanted to be make sure we're all on the same page, I wasn't entirely sure what was get nacked :-)
Not nacked - we're all just talking here. :-)
Quoted 6 lines
Heh, you're definitely more optimistic than me. I can just see something truly ridiculous happening like moving the page size bit and then getting weird behavior only when KVM happens to need the page size for some edge case.
Anyways, it's not a sticking point, and I certainly am not volunteering to maintain the FMS list...
Yeah, no need for it to be a sticking point because a pretty reliable birdie just told me that we're worrying for nothing and it all will solve itself.
:-)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Borislav Petkov[AMD Official Use Only - General]
Quoted 4 lines
Quoted 3 lines
I disagree. Running an old kernel on new hardware with a different RMP layout should refuse to use SNP, not read/write garbage and likely corrupt the RMP and/or host memory.
Quoted 8 lines
See my example below.
And IMO, hiding the non-architectural RMP format in SNP-specific code so that we don't have to churn a bunch of call sites that don't _need_ access to the raw RMP format is a good idea regardless of whether we want to be optimistic or pessimistic about future formats.
I don't think I ever objected to that.
I agree with what Sean is recommending to do.
As I mentioned earlier, in the long term and with respect to future platforms, we are going to add architectural support to read RMP table entries, so this structure will exist only for older platform support.
Thanks,
Ashish
Re: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Ashish KalraQuoted 78 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The
snp_lookup_page_in_rmptable()can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see
https://bugzilla.kernel.org/attachment.cgi?id=296015.Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 27 ++++++++++++++++++++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ include/linux/sev.h | 30 ++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 include/linux/sev.harch/x86/include/asm/sev.h +27
-0@@ -9,6 +9,7 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <linux/sev.h> #include <asm/insn.h> #include <asm/sev-common.h> #include <asm/bootparam.h> @@ -84,6 +85,32 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) + +/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct __packed rmpentry { +union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 :9 , + gpa :39 , + asid :10 , + vmsa :1 , + validated :1 , + rsvd2 :1 ; + } info; +u64 low; + }; +u64 high; +}; + +#define rmpentry_assigned (x) ((x)->info.assigned) +#define rmpentry_pagesize (x) ((x)->info.pagesize)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)arch/x86/kernel/sev.c +3
-0@@ -65,6 +65,8 @@* bookkeeping, the range need to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + (((unsigned long)x) >> RMPENTRY_SHIFT))/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -2386,3 +2388,44 @@ static int __init snp_rmptable_init(void)* available after subsys_initcall(). */ fs_initcall (snp_rmptable_init); +
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2401
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+static struct rmpentry *__snp_lookup_rmpentry (u64 pfn,int *level) +{ +unsigned long vaddr, paddr = pfn <<PAGE_SHIFT ; +struct rmpentry *entry, *large_entry; + +if (!pfn_valid (pfn)) +return ERR_PTR (-EINVAL ); + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return ERR_PTR (-ENXIO );
nit: I think we should check if SNP is enabled first, before doing anything else. In other words, I think we should move this check above the !pfn_valid() check.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2405
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ + vaddr = rmptable_start +rmptable_page_offset (paddr); +if (unlikely (vaddr > rmptable_end)) +return ERR_PTR (-ENXIO );
nit: It would be nice to use a different error code here, from the SNP feature check. That way, if this function fails, it's easier to diagnose where the function failed from the error code.
Quoted 40 lines
arch/x86/kernel/sev.c +26
-0+ + entry = (struct rmpentry *)vaddr; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + vaddr = rmptable_start +rmptable_page_offset (paddr &PMD_MASK ); + large_entry = (struct rmpentry *)vaddr; + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (large_entry)); + +return entry; +} + +/* +* Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, +* and -errno if there is no corresponding RMP entry. +*/ +int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);include/linux/sev.h new file +8
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]> +*/ + +#ifndef __LINUX_SEV_H
include/linux/sev.h line 18
Quoted 10 lines
include/linux/sev.h new file +10
-0+#define __LINUX_SEV_H + +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_lookup_rmpentry (u64 pfn,int *level); +int psmash (u64 pfn); +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable); +int rmp_make_shared (u64 pfn,enum pg_level level);
nit: I think the declarations for psmash(), rmp_make_private(), and rmp_make_shared() should be introduced in the patches that have their definitions.
Quoted 15 lines
include/linux/sev.h new file +12
-1+#else +static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; } +static inline int psmash (u64 pfn) {return -ENXIO ; } +static inline int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid, +bool immutable) +{ +return -ENODEV ; +} +static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */ --2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Ashish KalraQuoted 18 lines · Ashish Kalra
arch/x86/kernel/sev.c +3
-0@@ -65,6 +65,8 @@* bookkeeping, the range need to be added during the RMP entry lookup. */ #define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset (x) (RMPTABLE_CPU_BOOKKEEPING_SZ + (((unsigned long)x) >> RMPENTRY_SHIFT))/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -2386,3 +2388,44 @@ static int __init snp_rmptable_init(void)* available after subsys_initcall(). */ fs_initcall (snp_rmptable_init); +
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2401
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+static struct rmpentry *__snp_lookup_rmpentry (u64 pfn,int *level) +{ +unsigned long vaddr, paddr = pfn <<PAGE_SHIFT ; +struct rmpentry *entry, *large_entry; + +if (!pfn_valid (pfn)) +return ERR_PTR (-EINVAL ); + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return ERR_PTR (-ENXIO );
That test should happen first.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2403
Quoted 1 line
arch/x86/kernel/sev.c +1
-0+ vaddr = rmptable_start +rmptable_page_offset (paddr);
Wait, what does that macro do?
It takes the physical address and gives the offset from the beginning of the RMP table in VA space?
So why don't you do
entry = rmptable_entry(paddr)
instead which simply gives you directly the entry in the RMP table with which you can work further?
Instead of this macro doing half the work and then callers having to add the RMP start address and cast.
And make it small function so that you can have typechecking too, while at it.
include/linux/sev.h
Quoted 33 lines
arch/x86/kernel/sev.c +28
-0+if (unlikely (vaddr > rmptable_end)) +return ERR_PTR (-ENXIO ); + + entry = (struct rmpentry *)vaddr; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + vaddr = rmptable_start +rmptable_page_offset (paddr &PMD_MASK ); + large_entry = (struct rmpentry *)vaddr; + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (large_entry)); + +return entry; +} + +/* +* Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, +* and -errno if there is no corresponding RMP entry. +*/ +int snp_lookup_rmpentry (u64 pfn,int *level) +{ +struct rmpentry *e; + + e =__snp_lookup_rmpentry (pfn, level); +if (IS_ERR (e)) +return PTR_ERR (e); + +return !!rmpentry_assigned (e); +} +EXPORT_SYMBOL_GPL (snp_lookup_rmpentry);include/linux/sev.h new file +0
-0
Why is this header in the linux/ namespace and not in arch/x86/ ?
All that stuff in here doesn't have any meaning outside of x86...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
Hello Boris,
Quoted 11 lines
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2395
Quoted 3 lines
arch/x86/kernel/sev.c +3
-0+static struct rmpentry *__snp_lookup_rmpentry (u64 pfn,int *level) { +unsigned long vaddr, paddr = pfn <<PAGE_SHIFT ; +struct rmpentry *entry, *large_entry;.> +
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2401
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+if (!pfn_valid (pfn)) +return ERR_PTR (-EINVAL ); + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return ERR_PTR (-ENXIO );That test should happen first.
Ok.
Quoted 8 lines
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2403
Quoted 1 line
arch/x86/kernel/sev.c +1
-0+ vaddr = rmptable_start +rmptable_page_offset (paddr);Wait, what does that macro do?
It takes the physical address and gives the offset from the beginning of the RMP table in VA space?
So why don't you do
Quoted 7 lines
entry =
rmptable_entry(paddr)instead which simply gives you directly the entry in the RMP table with which you can work further?
Instead of this macro doing half the work and then callers having to add the RMP start address and cast.
And make it small function so that you can have typechecking too, while at it.
Ok, I will add a new corresponding rmptable_entry() function here, should be usable for getting the large RMP entry below too.
Quoted 21 lines
include/linux/sev.h
Quoted 18 lines
arch/x86/kernel/sev.c +14
-0+if (unlikely (vaddr > rmptable_end)) +return ERR_PTR (-ENXIO ); + + entry = (struct rmpentry *)vaddr; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */ + vaddr = rmptable_start +rmptable_page_offset (paddr &PMD_MASK ); + large_entry = (struct rmpentry *)vaddr; + *level =RMP_TO_X86_PG_LEVEL (rmpentry_pagesize (large_entry)); + +return entry; +} + +/* include/linux/sev.h new file mode renamed from include/linux/sev.h +0
-0100644 index 000000000000..1a68842789e1
include/linux/sev.h new file +0
-0Why is this header in the linux/ namespace and not in
arch/x86/?
Quoted 1 line
All that stuff in here doesn't have any meaning outside of x86...
Yes, makes sense to move it to arch/x86.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
In reply to Ashish Kalraarch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 94
Quoted 5 lines · Ashish Kalra
arch/x86/include/asm/sev.h +5
-0+/* +* The RMP entry format is not architectural. The format is defined in PPR +* Family 19h Model 01h, Rev B1 processor. +*/ +struct __packed rmpentry {
That __packed goes...
Quoted 6 lines
arch/x86/include/asm/sev.h +6
-0+union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 : 9,
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 110
Quoted 10 lines
arch/x86/include/asm/sev.h +10
-0+ gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; +u64 low; + }; +u64 high; +};
... here, at the end.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 05/49] x86/sev: Add RMP entry lookup helpers
[AMD Official Use Only - General]
Hello Boris,
Quoted 17 lines
Quoted 4 lines
+ * The RMP entry format is not architectural. The format is defined in PPR
+ * Family 19h Model 01h, Rev B1 processor.
+ */
+struct__packedrmpentry {That
__packedgoes...Quoted 10 lines
arch/x86/include/asm/sev.h +10
-0+union { +struct { +u64 assigned :1 , + pagesize :1 , + immutable :1 , + rsvd1 :9 , + gpa :39 , + asid :10 , + vmsa :1 , + validated : 1,
Quoted 8 lines
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 110
Quoted 6 lines
arch/x86/include/asm/sev.h +6
-0+ rsvd2 :1 ; + } info; +u64 low; + }; +u64 high; +};... here, at the end.
Yes, will fix that.
Thanks,
Ashish
Ashish Kalra
maintainerPATCH v6 6/49 x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations.
The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry.
--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)
arch/x86/include/asm/sev.h +11 -0
@@ -85,7 +85,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL (level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)/* * The RMP entry format is not architectural. The format is defined in PPR
@@ -126,6 +128,15 @@ struct snp_guest_platform_data {u64 secrets_gpa; }; +struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed; +#ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key;extern void __sev_es_ist_enter (struct pt_regs *regs);
arch/x86/kernel/sev.c +72 -0
75 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 32 lines · * Ashish Kalra ([email protected]) wrote:
From: Brijesh Singh <[email protected]>
The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations.
The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)arch/x86/include/asm/sev.h +2
-0@@ -85,7 +85,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL (level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)/* * The RMP entry format is not architectural. The format is defined in PPR @@ -126,6 +128,15 @@ struct snp_guest_platform_data {u64 secrets_gpa;
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 10 lines
arch/x86/include/asm/sev.h +8
-0}; +struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;
I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple of SNP capable CPUs with different layouts?
Dave
Quoted 86 lines
arch/x86/include/asm/sev.h +0
-0#ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key;extern void __sev_es_ist_enter (struct pt_regs *regs);arch/x86/kernel/sev.c +72
-0@@ -2429,3 +2429,75 @@ int snp_lookup_rmpentry(u64 pfn, int *level)return !!rmpentry_assigned (e); }EXPORT_SYMBOL_GPL (snp_lookup_rmpentry); + +int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash); + +static int rmpupdate (u64 pfn,struct rmpupdate *val) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" + :"=a" (ret) + :"a" (paddr),"c" ((unsigned long )val) + :"memory" ,"cc" ); +return ret; +} + +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable) +{ +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +memset (&val,0 ,sizeof (val)); + val.assigned =1 ; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_private); + +int rmp_make_shared (u64 pfn,enum pg_level level) +{ +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +memset (&val,0 ,sizeof (val)); + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_shared);Signature · 1 line
2.25.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Hello Dave,
Quoted 6 lines
Quoted 6 lines
/*
* The RMP entry format is not architectural. The format is defined
in PPR @@ -126,6 +128,15 @@ structsnp_guest_platform_data{
u64secrets_gpa;
};
Quoted 10 lines
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple of SNP capable CPUs with different layouts?
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
/*
* The RMP entry format is not architectural. The format is defined
in PPR @@ -126,6 +128,15 @@ structsnp_guest_platform_data{
u64secrets_gpa;
};Quoted 10 lines
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple >of SNP capable CPUs with different layouts?
Quoted 2 lines
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Some more clarity on this:
Actually, the PPR for family 19h Model 01h, Rev B1 defines the RMP entry format as below:
2.1.4.2 RMP Entry Format
Architecturally the format of RMP entries are not specified in APM. In order to assist software, the following table specifies select portions of the RMP entry format for this specific product. Each RMP entry is 16B in size and is formatted as follows. Software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. So non-architectural in this context means that it is only defined in our PPR.
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the dump_rmpentry() code.
Thanks,
Ashish
Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 24 lines · * Kalra, Ashish ([email protected]) wrote:
[AMD Official Use Only - General]
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
/*
* The RMP entry format is not architectural. The format is defined
in PPR @@ -126,6 +128,15 @@ structsnp_guest_platform_data{
u64secrets_gpa;
};Quoted 10 lines
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple >of SNP capable CPUs with different layouts?
Quoted 2 lines
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Some more clarity on this:
Quoted 8 lines
Actually, the PPR for family 19h Model 01h, Rev B1 defines the RMP entry format as below:
2.1.4.2 RMP Entry Format
Architecturally the format of RMP entries are not specified in APM. In order to assist software, the following table specifies select portions of the RMP entry format for this specific product. Each RMP entry is 16B in size and is formatted as follows. Software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. So non-architectural in this context means that it is only defined in our PPR.
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the
dump_rmpentry()code.
You'll need a way to make that fail cleanly when run on a newer CPU with different layout, and a way to build kernels that can handle more than one layout.
Dave
Quoted 3 lines
Thanks,
Ashish
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Hello Dave,
-----Original Message-----
From: Dr. David Alan Gilbert <[email protected]>
Sent: Tuesday, June 28, 2022 5:51 AM
To: Kalra, Ashish <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 26 lines · * Kalra, Ashish ([email protected]) wrote:
[AMD Official Use Only - General]
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
/*
* The RMP entry format is not architectural. The format is
defined in PPR @@ -126,6 +128,15 @@ structsnp_guest_platform_data{
u64secrets_gpa;
};Quoted 10 lines
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple >of SNP capable CPUs with different layouts?
Quoted 2 lines
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Some more clarity on this:
Actually, the PPR for family 19h Model 01h, Rev B1 defines the RMP entry format as below:
Quoted 8 lines
2.1.4.2 RMP Entry Format
Architecturally the format of RMP entries are not specified in APM. In order to assist software, the following table specifies select portions of the RMP entry format for this specific product. Each RMP entry is 16B in size and is formatted as follows. Software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. So non-architectural in this context means that it is only defined in our PPR.
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the
dump_rmpentry()code.You'll need a way to make that fail cleanly when run on a newer CPU with different layout, and a way to build kernels that can handle more than one layout.
Yes, I will be adding a check for CPU family/model as following :
static int __init snp_rmptable_init(void)
{
+ int family, model;
if (!boot_cpu_has(X86_FEATURE_SEV_SNP))
return 0;+ family = boot_cpu_data.x86;
+ model = boot_cpu_data.x86_model;
+ /*
+ * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */
+ if (family != 0x19 || model > 0xaf)
+ goto nosnp;
+
This way SNP will only be enabled specifically on the platforms for which this RMP entry format is defined in those processor's PPR. This will work for Milan and Genoa as of now.
Additionally as per Sean's suggestion, I will be moving the RMP structure definition to sev.c, which will make it a private structure and not exposed to other parts of the kernel.
Also in the future we will have an architectural interface to read the RMP table entry, we will first check for it's availability and if not available fall back to the RMP table entry structure definition.
Thanks,
Ashish
Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 56 lines · * Kalra, Ashish ([email protected]) wrote:
[AMD Official Use Only - General]
Hello Dave,
-----Original Message-----
From: Dr. David Alan Gilbert <[email protected]>
Sent: Tuesday, June 28, 2022 5:51 AM
To: Kalra, Ashish <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instructionQuoted 26 lines · * Kalra, Ashish ([email protected]) wrote:
[AMD Official Use Only - General]
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
/*
* The RMP entry format is not architectural. The format is
defined in PPR @@ -126,6 +128,15 @@ structsnp_guest_platform_data{
u64secrets_gpa;
};Quoted 10 lines
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 138
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed;I see above it says the RMP entry format isn't architectural; is this 'rmpupdate' structure? If not how is this going to get handled when we have a couple >of SNP capable CPUs with different layouts?
Quoted 2 lines
Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. I probably think the wording here should be architecture independent or more precisely platform independent.
Some more clarity on this:
Actually, the PPR for family 19h Model 01h, Rev B1 defines the RMP entry format as below:
Quoted 8 lines
2.1.4.2 RMP Entry Format
Architecturally the format of RMP entries are not specified in APM. In order to assist software, the following table specifies select portions of the RMP entry format for this specific product. Each RMP entry is 16B in size and is formatted as follows. Software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.Architectural implies that it is defined in the APM and shouldn't change in such a way as to not be backward compatible. So non-architectural in this context means that it is only defined in our PPR.
So actually this RPM entry definition is platform dependent and will need to be changed for different AMD processors and that change has to be handled correspondingly in the
dump_rmpentry()code.You'll need a way to make that fail cleanly when run on a newer CPU with different layout, and a way to build kernels that can handle more than one layout.
Yes, I will be adding a check for CPU family/model as following :
static int
__initsnp_rmptable_init(void)
{(quoted diff) +1
-0+ int family, model; if (!boot_cpu_has(X86_FEATURE_SEV_SNP)) return 0;
Review
Quoted 10 lines
(quoted diff) +9
-0+ family = boot_cpu_data.x86; + model = boot_cpu_data.x86_model; + /* + * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */ + if (family != 0x19 || model > 0xaf) + goto nosnp;
please add a print there to say why you're not enabling SNP.
It would be great if your firmware could give you an 'rmpentry version'; and then if a new model came out that happened to have the same layout everything would just carryon working by checking that rather than the actual family/model.
Quoted 11 lines
+
This way SNP will only be enabled specifically on the platforms for which this RMP entry format is defined in those processor's PPR. This will work for Milan and Genoa as of now.
Additionally as per Sean's suggestion, I will be moving the RMP structure definition to
sev.c, which will make it a private structure and not exposed to other parts of the kernel.Also in the future we will have an architectural interface to read the RMP table entry, we will first check for it's availability and if not available fall back to the RMP table entry structure definition.
Dave
Quoted 3 lines
Thanks,
Ashish
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Kalra, AshishQuoted 11 lines · Ashish
(quoted diff) +8
-0+ /* + * RMP table entry format is not architectural and it can vary by processor and + * is defined by the per-processor PPR. Restrict SNP support on the known CPU + * model and family for which the RMP table entry format is currently defined for. + */ + if (family != 0x19 || model > 0xaf) + goto nosnp; +This way SNP will only be enabled specifically on the platforms for which this RMP entry format is defined in those processor's PPR. This will work for Milan and Genoa as of now.
At some point, it would be really nice if the AMD side of things could work to kick the magic number habit on these things. This:
arch/x86/include/asm/intel-family.h
has been really handy. It lets you do things like
grep INTEL_FAM6_SKYLAKE arch/x86
That's a *LOT* more precise than:
egrep -i '0x5E|94' arch/x86
Borislav Petkov
maintainerRe: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Kalra, AshishQuoted 1 line · Ashish
Yes, I will be adding a check for CPU family/model as following :
Why if the PPR is already kinda spelling the already architectural pieces of the RMP entry?
"In order to assist software" it says.
So you call the specified ones by their name and the rest is __rsvd.
No need for model checks at all.
Right?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Hello Boris,
Quoted 5 lines
Quoted 1 line
Yes, I will be adding a check for CPU family/model as following :
Why if the PPR is already kinda spelling the already architectural pieces of the RMP entry?
"In order to assist software" it says.
The PPR specifies select portions of the RMP entry format for a specific core/platform.
Therefore, the complete struct rmpentry definition is non-architectural.
As per PPR, software should not rely on any field definitions not specified in this table and the format of an RMP entry may change in future processors.
Quoted 5 lines
So you call the specified ones by their name and the rest is
__rsvd.No need for model checks at all.
Right?
But we can't use this struct on a core/platform which has a different layout, so aren't the model checks required ?
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 2 lines · Ashish
But we can't use this struct on a core/platform which has a different layout, so aren't the model checks required ?
That would be a problem only if the already specified fields move or get resized.
If their offset and size don't change, you're good.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Ashish KalraQuoted 10 lines · Ashish Kalra
arch/x86/kernel/sev.c +10
-0+int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2452
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash);
If a function gets an EXPORT_SYMBOL_GPL(), the least we can do is reasonably document it. We don't need full kerneldoc nonsense, but a one-line about what this does would be quite helpful. That goes for all the functions here.
It would also be extremely helpful to have the changelog explain why these functions are exported and how the exports will be used.
As a general rule, please push cpu_feature_enabled() checks as early as you reasonably can. They are *VERY* cheap and can even enable the compiler to completely zap code like an #ifdef.
There also seem to be a lot of pfn_valid() checks in here that aren't very well thought out. For instance, there's a pfn_valid() check here:
+int rmp_make_shared(u64 pfn, enum pg_level level) +{
+ struct rmpupdate val;
+
+ if (!pfn_valid(pfn))
+ return -EINVAL;
...
+ return rmpupdate(pfn, &val);
+}
and in rmpupdate():
+static int rmpupdate(u64 pfn, struct rmpupdate *val) +{
+ unsigned long paddr = pfn << PAGE_SHIFT;
+ int ret;
+
+ if (!pfn_valid(pfn))
+ return -EINVAL;
...
This is (at best) wasteful. Could it be refactored?
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Quoted 23 lines
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ;arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2452
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash);If a function gets an
EXPORT_SYMBOL_GPL(), the least we can do is reasonably document it. We don't need full kerneldoc nonsense, but a one-line about what this does would be quite helpful. That goes for all the functions here.
Quoted 1 line
It would also be extremely helpful to have the changelog explain why these functions are exported and how the exports will be used.
I will add basic descriptions for all these exported functions.
Thanks,
Ashish
Quoted 1 line
As a general rule, please push
cpu_feature_enabled()checks as early as you reasonably can. They are *VERY* cheap and can even enable the compiler to completely zap code like an #ifdef.
There also seem to be a lot of pfn_valid() checks in here that aren't very well thought out. For instance, there's a pfn_valid() check here:
+int rmp_make_shared(u64 pfn, enum pg_level level) {
+ struct rmpupdate val;
+
+ if (!pfn_valid(pfn))
+ return -EINVAL;
...
+ return rmpupdate(pfn, &val);
+}
and in rmpupdate():
+static int rmpupdate(u64 pfn, struct rmpupdate *val) {
+ unsigned long paddr = pfn << PAGE_SHIFT;
+ int ret;
+
+ if (!pfn_valid(pfn))
+ return -EINVAL;
...
This is (at best) wasteful. Could it be refactored?
Re: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Ashish KalraHi Ashish,
Quoted 92 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations.
The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 11 ++++++ arch/x86/kernel/sev.c | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+)arch/x86/include/asm/sev.h +11
-0@@ -85,7 +85,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL (level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)/* * The RMP entry format is not architectural. The format is defined in PPR @@ -126,6 +128,15 @@ struct snp_guest_platform_data {u64 secrets_gpa; }; +struct rmpupdate { +u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed; +#ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key;extern void __sev_es_ist_enter (struct pt_regs *regs);arch/x86/kernel/sev.c +38
-0@@ -2429,3 +2429,75 @@ int snp_lookup_rmpentry(u64 pfn, int *level)return !!rmpentry_assigned (e); }EXPORT_SYMBOL_GPL (snp_lookup_rmpentry); + +int psmash (u64 pfn) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret; +} +EXPORT_SYMBOL_GPL (psmash); + +static int rmpupdate (u64 pfn,struct rmpupdate *val) +{ +unsigned long paddr = pfn <<PAGE_SHIFT ; +int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENXIO ; + +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" + :"=a" (ret) + :"a" (paddr),"c" ((unsigned long )val) + :"memory" ,"cc" );
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2479
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+return ret; +} + +int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable) +{ +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ; +
Should we add more checks on the arguments?
1. asid must be > 0
2. gpa must be aligned according to 'level'
3. gpa must be below the maximal address for the guest
"Note that the guest physical address space is limited according to CPUID Fn80000008_EAX and thus the GPAs used by the firmware in measurement calculation are equally limited. Hypervisors should not attempt to map pages outside of this limit."
(-SNP ABI spec page 86, section 8.17 SNP_LAUNCH_UPDATE)
But note that in patch 28 of this series we have:
+ /* Transition the VMSA page to a firmware state. */
+ ret = rmp_make_private(pfn, -1, PG_LEVEL_4K, sev->asid, true);
That (u64)(-1) value for the gpa argument violates conditions 2 and 3 from my list above.
And indeed when calculating measurements we see that the GPA value for the VMSA pages is 0x0000FFFF_FFFFF000, and not (u64)(-1). [1] [2]
Instead of checks, we can mask the gpa argument so that rmpupdate will get the correct value. Not sure which approach is preferable.
[1] https://github.com/IBM/sev-snp-measure/blob/90f6e59831d20e44d03d5ee19388f624fca87291/sevsnpmeasure/gctx.py#L40
[2] https://github.com/slp/snp-digest-rs/blob/0e5a787e99069944467151101ae4db474793d657/src/main.rs#L86
-Dov
Quoted 24 lines
arch/x86/kernel/sev.c +24
-0+memset (&val,0 ,sizeof (val)); + val.assigned =1 ; + val.asid = asid; + val.immutable = immutable; + val.gpa = gpa; + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_private); + +int rmp_make_shared (u64 pfn,enum pg_level level) +{ +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +memset (&val,0 ,sizeof (val)); + val.pagesize =X86_TO_RMP_PG_LEVEL (level); + +return rmpupdate (pfn, &val); +} +EXPORT_SYMBOL_GPL (rmp_make_shared);
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Hello Dov,
Quoted 10 lines
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2479
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid, +bool immutable) { +struct rmpupdate val; + +if (!pfn_valid (pfn)) +return -EINVAL ; +Should we add more checks on the arguments?
Quoted 3 lines
1. asid must be > 0
2. gpa must be aligned according to 'level'
3. gpa must be below the maximal address for the guest
Ok, yes it surely makes sense to add more checks on the arguments.
Quoted 9 lines
"Note that the guest physical address space is limited according to CPUID
Fn80000008_EAXand thus the GPAs used by the firmware in measurement calculation are equally limited. Hypervisors should not attempt to map pages outside of this limit." (-SNP ABI spec page 86, section 8.17SNP_LAUNCH_UPDATE)But note that in patch 28 of this series we have:
(quoted diff) +2
-0+ /* Transition the VMSA page to a firmware state. */ + ret = rmp_make_private(pfn, -1, PG_LEVEL_4K, sev->asid, true);
Quoted 5 lines
That (u64)(-1) value for the gpa argument violates conditions 2 and 3 from my list above.
And indeed when calculating measurements we see that the GPA value for the VMSA pages is 0x0000FFFF_FFFFF000, and not (u64)(-1). [1] [2]
Instead of checks, we can mask the gpa argument so that rmpupdate will get the correct value. Not sure which approach is preferable.
Well, the firmware is anyway masking the gpa argument as you observe in the launch digest, so probably we should do the same here too.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Ashish KalraQuoted 15 lines · Ashish Kalra
arch/x86/include/asm/sev.h +2
-0@@ -85,7 +85,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1#define RMP_TO_X86_PG_LEVEL (level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL (level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M)/* * The RMP entry format is not architectural. The format is defined in PPR
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 131
Quoted 4 lines
arch/x86/include/asm/sev.h +1
-0@@ -126,6 +128,15 @@ struct snp_guest_platform_data {u64 secrets_gpa; }; +struct rmpupdate {
Why is there a struct rmpupdate *and* a struct rmpentry?!
One should be enough.
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed; +
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
[AMD Official Use Only - General]
Hello Boris,
Quoted 1 line
Quoted 1 line
+struct rmpupdate {
Why is there a struct rmpupdate *and* a struct rmpentry?!
The struct rmpentry is the raw layout of the RMP table entry while struct rmpupdate is the structure expected by the rmpupdate instruction for programming the RMP table entries.
Arguably, we can program a struct rmpupdate internally from a struct rmpentry.
But we will still need struct rmpupdate for issuing the rmpupdate instruction, so it is probably cleaner to keep it this way, as it only has two main callers - rmp_make_private() and rmp_make_shared().
Also due to non-architectural aspect of struct rmpentry, the above functions may need to be modified If there are changes in struct rmpentry, while struct rmpupdate remains consistent and persistent.
Quoted 10 lines
One should be enough.
arch/x86/include/asm/sev.h struct snp_guest_platform_data { · line 139
Quoted 8 lines
arch/x86/include/asm/sev.h +8
-0+u64 gpa; +u8 assigned; +u8 pagesize; +u8 immutable; +u8 rsvd; +u32 asid; +} __packed; +
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 06/49] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 10 lines · Ashish
The struct rmpentry is the raw layout of the RMP table entry while struct rmpupdate is the structure expected by the rmpupdate instruction for programming the RMP table entries.
Arguably, we can program a struct rmpupdate internally from a struct rmpentry.
But we will still need struct rmpupdate for issuing the rmpupdate instruction, so it is probably cleaner to keep it this way, as it only has two main callers -
rmp_make_private()andrmp_make_shared().
Ok, but then call it struct rmp_state. The APM says in the RMPUPDATE blurb:
"The RCX register provides the effective address of a 16-byte data structure which contains the new RMP state."
so the function signature should be:
static int rmpupdate(u64 pfn, struct rmp_state *new)
and this is basically the description of that. It can't get any more user-friendly than this.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 7/49 x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP checks failure, it raises a page-fault exception.
The rmp_make_private() and rmp_make_shared() helpers are used to add or remove the pages from the RMP table. Improve the rmp_make_private() to invalid state so that pages cannot be used in the direct-map after its added in the RMP table, and restore to its default valid permission after the pages are removed from the RMP table.
--- arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/sev.c +60 -1
@@ -2451,10 +2451,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i)); +if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i)); +if (ret) + goto cleanup; + } + +return 0 ; + +cleanup: +restore_direct_map (pfn, i); +return ret; +} +static int rmpupdate (u64 pfn,struct rmpupdate *val) {unsigned long paddr = pfn <<PAGE_SHIFT ; -int ret; +int ret, level, npages;if (!pfn_valid (pfn))return -EINVAL ;
@@ -2462,11 +2494,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val)if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))return -ENXIO ; + level =RMP_TO_X86_PG_LEVEL (val->pagesize); + npages =page_level_size (level) /PAGE_SIZE ; + +/* +* If page is getting assigned in the RMP table then unmap it from the +* direct map. +*/ +if (val->assigned) { +if (invalid_direct_map (pfn, npages)) { +pr_err ("Failed to unmap pfn 0x%llx pages %d from direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" :"=a" (ret) :"a" (paddr),"c" ((unsigned long )val) :"memory" ,"cc" ); + +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { +pr_err ("Failed to map pfn 0x%llx pages %d in direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +return ret; }
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 8 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. When hardware encounters an RMP checks failure, it raises a page-fault exception.
nit: "RMP checks ..." -> "RMP-check ..."
Quoted 4 lines
The
rmp_make_private()andrmp_make_shared()helpers are used to add or remove the pages from the RMP table. Improve thermp_make_private()to invalid state so that pages cannot be used in the direct-map after its
nit: "invalid state ..." -> "invalidate state ..."
nit: "... after its" -> "... after they're"
(Here, and in the patch subject too.)
Quoted 1 line
added in the RMP table, and restore to its default valid permission after
nit: "... restore to its ..." -> "... restored to their ..."
Quoted 21 lines
the pages are removed from the RMP table.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kernel/sev.c | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-)arch/x86/kernel/sev.c +6
-0@@ -2451,10 +2451,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i));
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2469
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i); +return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages)
I think we should rename this function to "invalidate_direct_map()".
Quoted 67 lines
arch/x86/kernel/sev.c +44
-2+{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i)); +if (ret) + goto cleanup; + } + +return 0 ; + +cleanup: +restore_direct_map (pfn, i); +return ret; +} +static int rmpupdate (u64 pfn,struct rmpupdate *val) {unsigned long paddr = pfn <<PAGE_SHIFT ; -int ret; +int ret, level, npages;if (!pfn_valid (pfn))return -EINVAL ;@@ -2462,11 +2494,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val)if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))return -ENXIO ; + level =RMP_TO_X86_PG_LEVEL (val->pagesize); + npages =page_level_size (level) /PAGE_SIZE ; + +/* +* If page is getting assigned in the RMP table then unmap it from the +* direct map. +*/ +if (val->assigned) { +if (invalid_direct_map (pfn, npages)) { +pr_err ("Failed to unmap pfn 0x%llx pages %d from direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" :"=a" (ret) :"a" (paddr),"c" ((unsigned long )val) :"memory" ,"cc" ); + +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { +pr_err ("Failed to map pfn 0x%llx pages %d in direct_map\n" , + pfn, npages); +return -EFAULT ; + } + } +return ret; } --2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Ashish KalraQuoted 1 line · Ashish Kalra
Subject: x86/sev: Invalid pages from direct map when adding it to RMP table
"...: Invalidate pages from the direct map when adding them to the RMP table"
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2459
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i));
set_memory_p() ?
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2465
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i);
Warn for each pfn?!
That'll flood dmesg mightily.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2474
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i));
As above, set_memory_np() doesn't work here instead of looping over each page?
Quoted 4 lines
arch/x86/kernel/sev.c +0
-0@@ -2462,11 +2494,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val)if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))return -ENXIO ;
arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2506
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ level =RMP_TO_X86_PG_LEVEL (val->pagesize); + npages =page_level_size (level) /PAGE_SIZE ; + +/* +* If page is getting assigned in the RMP table then unmap it from the +* direct map. +*/ +if (val->assigned) { +if (invalid_direct_map (pfn, npages)) { + pr_err("Failed to unmap pfn 0x%llx pages %d from direct_map\n",
"Failed to unmap %d pages at pfn 0x... from the direct map\n"
Quoted 7 lines
arch/x86/kernel/sev.c +5
-0+ pfn, npages); +return -EFAULT ; + } + } +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE"
arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2523
Quoted 10 lines
arch/x86/kernel/sev.c +7
-0: "=a"(ret) : "a"(paddr), "c"((unsigned long)val) : "memory", "cc"); + +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { + pr_err("Failed to map pfn 0x%llx pages %d in direct_map\n",
"Failed to map %d pages at pfn 0x... into the direct map\n"
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
[AMD Official Use Only - General]
Hello Boris,
Quoted 4 lines
Quoted 2 lines
Subject: x86/sev: Invalid pages from direct map when adding it to RMP table
"...: Invalidate pages from the direct map when adding them to the RMP table"
Ok
Quoted 7 lines
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2459
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+static int restore_direct_map (u64 pfn,int npages) { +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i));
set_memory_p()?
You mean set_memory_present() ?
Is there an issue with not using set_direct_map_default_noflush(), it is easier to correlate with this function and it's functionality of restoring the page in the kernel direct map ?
Quoted 21 lines
arch/x86/kernel/sev.c +7
-0+if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + +i);Warn for each pfn?!
That'll flood dmesg mightily.
arch/x86/kernel/sev.c +8
-0+return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages) { +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i));
Quoted 1 line
As above,
set_memory_np()doesn't work here instead of looping over each page?
Yes, set_memory_np() looks more efficient to use instead of looping over each page.
But again, calling set_direct_map_invalid_noflush() is easier to understand from the calling function's point of view as it correlates to the functionality of invalidating the page from kernel direct map ?
Quoted 5 lines
arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2505
Quoted 2 lines
arch/x86/kernel/sev.c +2
-0+if (val->assigned) { +if (invalid_direct_map (pfn, npages)) {. + pr_err("Failed to unmap pfn 0x%llx pages %d from direct_map\n",
"Failed to unmap %d pages at pfn 0x... from the direct map\n"
Ok.
Quoted 5 lines
arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2523
Quoted 3 lines
arch/x86/kernel/sev.c +3
-0+if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { + pr_err("Failed to map pfn 0x%llx pages %d in direct_map\n","Failed to map %d pages at pfn 0x... into the direct map\n"
Ok.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 1 line · Ashish
You mean
set_memory_present()?
Right, that.
We have set_memory_np() but set_memory_present(). Talk about consistence... ;-\
Quoted 3 lines
But again, calling
set_direct_map_invalid_noflush()is easier to understand from the calling function's point of view as it correlates to the functionality of invalidating the page from kernel direct map ?
You mean, we prefer easy to understand to performance?
set_direct_map_invalid_noflush() means crap to me. I have to go look it up - set memory P or NP is much clearer.
The patch which added those things you consider easier to understand is:
commit d253ca0c3865a8d9a8c01143cf20425e0be4d0ce
Author: Rick Edgecombe <[email protected]> Date: Thu Apr 25 17:11:34 2019 -0700
x86/mm/cpa: Add set_direct_map_*() functions
Add two new functions set_direct_map_default_noflush() and set_direct_map_invalid_noflush() for setting the direct map alias for the page to its default valid permissions and to an invalid state that cannot be cached in a TLB, respectively. These functions do not flush the TLB.
I don't see how this fits with your use case...
Also, your helpers are called restore_direct_map and invalidate_direct_map. That's already explaining what this is supposed to do.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Hello Boris,
Quoted 7 lines · Borislav Petkov
Quoted 1 line · Ashish
You mean
set_memory_present()?Right, that.
We have
set_memory_np()butset_memory_present(). Talk about consistence... ;-\
Following up on this, now, set_memory_present() is a static interface, so will need do add a new external API like set_memory_p() similar to set_memory_np().
Also, looking at arch/x86/include/asm/set_memory.h:
..
/*
* The set_memory_* API can be used to change various attributes of a
* virtual address range. The attributes include:
* Cacheability : UnCached, WriteCombining, WriteThrough, WriteBack
* Executability : eXecutable, NoteXecutable
* Read/Write : ReadOnly, ReadWrite * Presence : NotPresent * Encryption : Encrypted, Decrypted
*
..
int set_memory_np(unsigned long addr, int numpages); ..
So currently there is no interface defined for changing the attribute of a range to present or restoring the range in the direct map.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 3 lines · Ashish
Following up on this, now,
set_memory_present()is a static interface, so will need do add a new external API likeset_memory_p()similar toset_memory_np().
It is called set_memory_p() now and you can "un-static" it. :)
Quoted 2 lines
So currently there is no interface defined for changing the attribute of a range to present or restoring the range in the direct map.
No?
static int set_memory_p(unsigned long *addr, int numpages)
{
return change_page_attr_set(addr, numpages, __pgprot(_PAGE_PRESENT), 0);
}:-)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Borislav PetkovQuoted 6 lines · Borislav Petkov
Quoted 1 line · Ashish Kalra
Subject: x86/sev: Invalid pages from direct map when adding it to RMP table
"...: Invalidate pages from the direct map when adding them to the RMP table"
Quoted 8 lines
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2459
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+static int restore_direct_map (u64 pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_default_noflush (pfn_to_page (pfn + i));
set_memory_p()?
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
I still need to dig in a big but I'm guessing it's related to flushing behavior.
It would however be nice to have a set_direct_map_default_noflush() variant that accepted a 'npages' argument, since it would be more performant here and also would potentially allow for restoring the 2M direct mapping in some cases. Will look into this more for v8.
-Mike
Quoted 69 lines
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2465
Quoted 7 lines
arch/x86/kernel/sev.c +6
-0+if (ret) + goto cleanup; + } + +cleanup: +WARN (ret >0 ,"Failed to restore direct map for pfn 0x%llx\n" , pfn + i);Warn for each pfn?!
That'll flood dmesg mightily.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2474
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+return ret; +} + +static int invalid_direct_map (unsigned long pfn,int npages) +{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i));As above,
set_memory_np()doesn't work here instead of looping over each page?Quoted 4 lines
arch/x86/kernel/sev.c +0
-0@@ -2462,11 +2494,38 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val)if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))return -ENXIO ;arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2506
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ level =RMP_TO_X86_PG_LEVEL (val->pagesize); + npages =page_level_size (level) /PAGE_SIZE ; + +/* +* If page is getting assigned in the RMP table then unmap it from the +* direct map. +*/ +if (val->assigned) { +if (invalid_direct_map (pfn, npages)) { + pr_err("Failed to unmap pfn 0x%llx pages %d from direct_map\n","Failed to unmap %d pages at pfn 0x... from the direct map\n"
Quoted 7 lines
arch/x86/kernel/sev.c +5
-0+ pfn, npages); +return -EFAULT ; + } + } +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE"arch/x86/kernel/sev.c static int rmpupdate(u64 pfn, struct rmpupdate *val) · line 2523
Quoted 10 lines
arch/x86/kernel/sev.c +7
-0: "=a"(ret) : "a"(paddr), "c"((unsigned long)val) : "memory", "cc"); + +/* +* Restore the direct map after the page is removed from the RMP table. +*/ +if (!ret && !val->assigned) { +if (restore_direct_map (pfn, npages)) { + pr_err("Failed to map pfn 0x%llx pages %d in direct_map\n","Failed to map %d pages at pfn 0x... into the direct map\n"
Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
Borislav Petkov
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 6 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT, change_page_attr_set_clr() flushes once at the end.
Don't you need to flush when you modify the direct map?
Quoted 4 lines
It would however be nice to have a
set_direct_map_default_noflush()variant that accepted a 'npages' argument, since it would be more performant here and also would potentially allow for restoring the 2M direct mapping in some cases. Will look into this more for v8.
set_pages_direct_map_default_noflush()
I guess.
Although the name is a mouthful so I wouldn't mind having those shortened.
In any case, as long as that helper is properly defined and documented, I don't mind.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
Quoted 7 lines
Quoted 2 lines
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT,
change_page_attr_set_clr()flushes once at the end.Don't you need to flush when you modify the direct map?
Milan onward, there is H/W support for coherency between mappings of the same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.
I don't know if there is any other reason to flush after modifying the direct map ?
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 1 line · Ashish
Milan onward,
And before ML there's no SNP, right?
Quoted 4 lines
there is H/W support for coherency between mappings of the
same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.
Yah, that rings a bell.
In any case, the fact that flushing is not needed should be stated somewhere in text so that it is clear why.
Quoted 2 lines
I don't know if there is any other reason to flush after modifying the direct map ?
There's
/*
* No need to flush, when we did not set any of the caching
* attributes:
*/
cache = !!pgprot2cachemode(mask_set);Does the above HW cover this case too?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Ashish
Milan onward,
And before ML there's no SNP, right?
Yes, that's correct.
Quoted 15 lines
Quoted 4 lines
there is H/W support for coherency between mappings of the
same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.Yah, that rings a bell.
In any case, the fact that flushing is not needed should be stated somewhere in text so that it is clear why.
Quoted 2 lines
I don't know if there is any other reason to flush after modifying the direct map ?
There's
Quoted 8 lines
/* * No need to flush, when we did not set any of the caching * attributes: */ cache = !!pgprot2cachemode(mask_set);Does the above HW cover this case too?
Actually, as both set_memory_p() and set_memory_np() are only setting/clearing the _PAGE_PRESENT flag and not changing any of the page caching attributes so this flush won't be required anyway.
Thanks,
Ashish
Quoted 3 lines
Thx.
Re: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Kalra, AshishQuoted 17 lines · Ashish
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
Quoted 7 lines
Quoted 2 lines
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT,
change_page_attr_set_clr()flushes once at the end.Don't you need to flush when you modify the direct map?
Quoted 8 lines
Milan onward, there is H/W support for coherency between mappings of the same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.
I don't know if there is any other reason to flush after modifying the direct map ?
Isn't the Milan coherence feature (SME_COHERENT?) about the caches -- not the TLBs? And isn't the flushing being discussed here about the TLBs?
Also, I thought that Mingwei Zhang <[email protected]> found that the Milan SEV coherence feature was basically unusable in Linux because it only works across CPUs. It does not extend to IO (e.g., CPU caches need to be flushed prior to free'ing a SEV VM's private address and reallocating that location to a device driver to be used for IO). My understanding of this feature and its limitations may be too coarse. But I think we should be very careful about relying on this feature as it is implemented in Milan.
That being said, I guess I could see an argument to rely on the feature here, since we're not deallocating the memory and reallocating it to a device. But again, I thought the feature was about cache coherence -- not TLB coherence.
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Hello Marc,
Quoted 24 lines · Marc Orr
Quoted 23 lines · Ashish
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
Quoted 7 lines
Quoted 2 lines
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT,
change_page_attr_set_clr()flushes once at the end.Don't you need to flush when you modify the direct map?
Milan onward, there is H/W support for coherency between mappings of the same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.
Quoted 6 lines
Quoted 2 lines
I don't know if there is any other reason to flush after modifying the direct map ?
Isn't the Milan coherence feature (
SME_COHERENT?) about the caches -- not the TLBs? And isn't the flushing being discussed here about the TLBs?
Actually, the flush does both cache and TLB flushing.
Both cpa_flush() and cpa_flush_all() will also do cache flushing if cache argument is not NULL. As in this case, no page caching attributes are being changed so there is no need to do cache flushing.
But TLB flushing (as PTE is updated) is still required and will be done.
Quoted 10 lines
Also, I thought that Mingwei Zhang <[email protected]> found that the Milan SEV coherence feature was basically unusable in Linux because it only works across CPUs. It does not extend to IO (e.g., CPU caches need to be flushed prior to free'ing a SEV VM's private address and reallocating that location to a device driver to be used for IO). My understanding of this feature and its limitations may be too coarse. But I think we should be very careful about relying on this feature as it is implemented in Milan.
Quoted 4 lines
That being said, I guess I could see an argument to rely on the feature here, since we're not deallocating the memory and reallocating it to a device. But again, I thought the feature was about cache coherence -- not TLB coherence.
Yes, this is just invalidating or re-mapping into the kernel direct map, so we can rely on this feature for the use case here.
Thanks,
Ashish
Re: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 35 lines · Ashish
Hello Marc,
Quoted 24 lines · Marc Orr
Quoted 23 lines · Ashish
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
Quoted 7 lines
Quoted 2 lines
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT,
change_page_attr_set_clr()flushes once at the end.Don't you need to flush when you modify the direct map?
Milan onward, there is H/W support for coherency between mappings of the same physical page with different encryption keys, so AFAIK, there should be no need to flush during page state transitions, where we invoke these direct map interface functions for re-mapping/invalidating pages.
Quoted 6 lines
Quoted 2 lines
I don't know if there is any other reason to flush after modifying the direct map ?
Isn't the Milan coherence feature (
SME_COHERENT?) about the caches -- not the TLBs? And isn't the flushing being discussed here about the TLBs?
Quoted 7 lines
Actually, the flush does both cache and TLB flushing.
Both
cpa_flush()andcpa_flush_all()will also do cache flushing if cache argument is not NULL. As in this case, no page caching attributes are being changed so there is no need to do cache flushing.But TLB flushing (as PTE is updated) is still required and will be done.
Ah, got it now. Thanks for explaining. (I should've looked at the code a bit closer.)
Quoted 9 lines
Quoted 9 lines
Also, I thought that Mingwei Zhang <[email protected]> found that the Milan SEV coherence feature was basically unusable in Linux because it only works across CPUs. It does not extend to IO (e.g., CPU caches need to be flushed prior to free'ing a SEV VM's private address and reallocating that location to a device driver to be used for IO). My understanding of this feature and its limitations may be too coarse. But I think we should be very careful about relying on this feature as it is implemented in Milan.
Quoted 7 lines
Quoted 4 lines
That being said, I guess I could see an argument to rely on the feature here, since we're not deallocating the memory and reallocating it to a device. But again, I thought the feature was about cache coherence -- not TLB coherence.
Yes, this is just invalidating or re-mapping into the kernel direct map, so we can rely on this feature for the use case here.
SGTM and that does make sense then. Thanks for confirming.
Mike Rapoport
reviewerRe: [PATCH Part2 v6 07/49] x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Borislav PetkovQuoted 17 lines · Borislav Petkov
Quoted 6 lines · Michael Roth
We implemented this approach for v7, but it causes a fairly significant performance regression, particularly for the case for npages > 1 which this change was meant to optimize.
I still need to dig in a big but I'm guessing it's related to flushing behavior.
Well, AFAICT,
change_page_attr_set_clr()flushes once at the end.Don't you need to flush when you modify the direct map?
Quoted 4 lines
It would however be nice to have a
set_direct_map_default_noflush()variant that accepted a 'npages' argument, since it would be more performant here and also would potentially allow for restoring the 2M direct mapping in some cases. Will look into this more for v8.
Quoted 6 lines
set_pages_direct_map_default_noflush()I guess.
Although the name is a mouthful so I wouldn't mind having those shortened.
I had a patch that just adds numpages parameter:
https://lore.kernel.org/lkml/[email protected]/
The set_direct_map*() are not too widely used, so it's not a big deal to update all callers.
Quoted 11 lines
In any case, as long as that helper is properly defined and documented, I don't mind.
Thx.
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
Signature · 2 lines
Sincerely yours, Mike.
Ashish Kalra
maintainerPATCH v6 8/49 x86/traps: Define RMP violation #PF error code
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation.
While at it, use the BIT_ULL() macro.
--- arch/x86/include/asm/trap_pf.h | 18 +++++++++++------- arch/x86/mm/fault.c | 1 + 2 files changed, 12 insertions(+), 7 deletions(-)
arch/x86/include/asm/trap_pf.h +11 -7
@@ -2,6 +2,8 @@#ifndef _ASM_X86_TRAP_PF_H#define _ASM_X86_TRAP_PF_H +#include <linux/bits.h> /* BIT() macro */ +/* * Page fault error code bits: *
@@ -12,15 +14,17 @@* bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault +* bit 31 == 1: fault was due to RMP violation */ enum x86_pf_error_code { -X86_PF_PROT =1 <<0 , -X86_PF_WRITE =1 <<1 , -X86_PF_USER =1 <<2 , -X86_PF_RSVD =1 <<3 , -X86_PF_INSTR =1 <<4 , -X86_PF_PK =1 <<5 , -X86_PF_SGX =1 <<15 , +X86_PF_PROT =BIT_ULL (0 ), +X86_PF_WRITE =BIT_ULL (1 ), +X86_PF_USER =BIT_ULL (2 ), +X86_PF_RSVD =BIT_ULL (3 ), +X86_PF_INSTR =BIT_ULL (4 ), +X86_PF_PK =BIT_ULL (5 ), +X86_PF_SGX =BIT_ULL (15 ), +X86_PF_RMP =BIT_ULL (31 ), };#endif /* _ASM_X86_TRAP_PF_H */
arch/x86/mm/fault.c +1 -0
@@ -546,6 +546,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code &X86_PF_PROT ) ?"not-present page" : (error_code &X86_PF_RSVD ) ?"reserved bit violation" : (error_code &X86_PF_PK ) ?"protection keys violation" : + (error_code &X86_PF_RMP ) ?"RMP violation" : "permissions violation");if (!(error_code &X86_PF_USER ) &&user_mode (regs)) {
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 08/49] x86/traps: Define RMP violation #PF error code
Quoted 12 lines · Ashish Kalra
arch/x86/include/asm/trap_pf.h +1
-5@@ -12,15 +14,17 @@* bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault +* bit 31 == 1: fault was due to RMP violation */ enum x86_pf_error_code { -X86_PF_PROT =1 <<0 , -X86_PF_WRITE =1 <<1 , -X86_PF_USER =1 <<2 , -X86_PF_RSVD =1 <<3 , -X86_PF_INSTR =1 <<4 ,
arch/x86/include/asm/trap_pf.h line 27
Quoted 10 lines
arch/x86/include/asm/trap_pf.h +8
-2-X86_PF_PK =1 <<5 , - X86_PF_SGX = 1 << 15, +X86_PF_PROT =BIT_ULL (0 ), +X86_PF_WRITE =BIT_ULL (1 ), +X86_PF_USER =BIT_ULL (2 ), +X86_PF_RSVD =BIT_ULL (3 ), +X86_PF_INSTR =BIT_ULL (4 ), +X86_PF_PK =BIT_ULL (5 ), +X86_PF_SGX =BIT_ULL (15 ), + X86_PF_RMP = BIT_ULL(31),
Yeah, I remember dhansen asked for those to use the BIT() macro but the _ULL is an overkill. Those PF flags are 32 and they fit in an unsigned int.
But we don't have BUT_UI() so I guess the next best thing - BIT() - which uses UL internally, should be good enough.
So pls use BIT() here - not BIT_ULL().
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 9/49 x86/fault: Add support to handle the RMP fault for user address
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk:
1. Assigned bit in the RMP table is zero (i.e page is shared).
2. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
3. Immutable bit in the RMP table is not zero.
The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync.
--- arch/x86/mm/fault.c | 66 ++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 13 ++++++++ 4 files changed, 84 insertions(+), 1 deletion(-)
arch/x86/mm/fault.c +66 -0
@@ -19,6 +19,7 @@#include <linux/uaccess.h> /* faulthandler_disabled() */ #include <linux/efi.h> /* efi_crash_gracefully_on_page_fault()*/ #include <linux/mm_types.h> +#include <linux/sev.h> /* snp_lookup_rmpentry() */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */
@@ -1209,6 +1210,60 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +static inline size_t pages_per_hpage (int level) +{ +return page_level_size (level) /PAGE_SIZE ; +} + +/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ +if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask; + } + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) { +do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return 1 ; + } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ; + +return 1 ; +} +/* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost
@@ -1306,6 +1361,17 @@ void do_user_addr_fault(struct pt_regs *regs,if (error_code &X86_PF_INSTR ) flags |=FAULT_FLAG_INSTRUCTION ; +/* +* If its an RMP violation, try resolving it. +*/ +if (error_code &X86_PF_RMP ) { +if (handle_user_rmp_page_fault (regs, error_code, address)) +return ; + +/* Ask to split the page */ + flags |=FAULT_FLAG_PAGE_SPLIT ; + } +#ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The
include/linux/mm.h +2 -1
@@ -463,7 +463,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" }/* * vm_fault is filled by the pagefault handler and passed to the vma's
include/linux/mm_types.h +3 -0
@@ -818,6 +818,8 @@ typedef struct {* mapped R/O. * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. +* @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the +* region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two
@@ -855,6 +857,7 @@ enum fault_flag {FAULT_FLAG_INTERRUPTIBLE =1 <<9 ,FAULT_FLAG_UNSHARE =1 <<10 ,FAULT_FLAG_ORIG_PTE_VALID =1 <<11 , + FAULT_FLAG_PAGE_SPLIT = 1 << 12, };typedef unsigned int __bitwise zap_flags_t ;
mm/memory.c +13 -0
@@ -4945,6 +4945,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore *
@@ -5024,6 +5033,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf); +if (pmd_trans_huge (vmf.orig_pmd) ||pmd_devmap (vmf.orig_pmd)) {if (pmd_protnone (vmf.orig_pmd) &&vma_is_accessible (vma))return do_huge_pmd_numa_page (&vmf);
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 73 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk:
1. Assigned bit in the RMP table is zero (i.e page is shared).
2. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
3. Immutable bit in the RMP table is not zero.The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/mm/fault.c | 66 ++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 13 ++++++++ 4 files changed, 84 insertions(+), 1 deletion(-)arch/x86/mm/fault.c +31
-0@@ -19,6 +19,7 @@#include <linux/uaccess.h> /* faulthandler_disabled() */ #include <linux/efi.h> /* efi_crash_gracefully_on_page_fault()*/ #include <linux/mm_types.h> +#include <linux/sev.h> /* snp_lookup_rmpentry() */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */ @@ -1209,6 +1210,60 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +static inline size_t pages_per_hpage (int level) +{ +return page_level_size (level) /PAGE_SIZE ; +} + +/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ +if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask;
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1252
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+ + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask; + } + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) {
snp_lookup_rmpentry returns 0, 1 or -errno, so this should likely be:
if (snp_lookup_rmpentry(pfn, &rmp_level) != 1)) {Quoted 104 lines
arch/x86/mm/fault.c +25
-0+do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return 1 ; + } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ; + +return 1 ; +} +/* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost @@ -1306,6 +1361,17 @@ void do_user_addr_fault(struct pt_regs *regs,if (error_code &X86_PF_INSTR ) flags |=FAULT_FLAG_INSTRUCTION ; +/* +* If its an RMP violation, try resolving it. +*/ +if (error_code &X86_PF_RMP ) { +if (handle_user_rmp_page_fault (regs, error_code, address)) +return ; + +/* Ask to split the page */ + flags |=FAULT_FLAG_PAGE_SPLIT ; + } +#ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The include/linux/mm.h +2
-1@@ -463,7 +463,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" }/* * vm_fault is filled by the pagefault handler and passed to the vma's include/linux/mm_types.h +3
-0@@ -818,6 +818,8 @@ typedef struct {* mapped R/O. * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. +* @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the +* region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -855,6 +857,7 @@ enum fault_flag {FAULT_FLAG_INTERRUPTIBLE =1 <<9 ,FAULT_FLAG_UNSHARE =1 <<10 ,FAULT_FLAG_ORIG_PTE_VALID =1 <<11 , + FAULT_FLAG_PAGE_SPLIT = 1 << 12, };typedef unsigned int __bitwise zap_flags_t ;mm/memory.c +13
-0@@ -4945,6 +4945,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore * @@ -5024,6 +5033,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf); +if (pmd_trans_huge (vmf.orig_pmd) ||pmd_devmap (vmf.orig_pmd)) {if (pmd_protnone (vmf.orig_pmd) &&vma_is_accessible (vma))return do_huge_pmd_numa_page (&vmf);Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Ashish Kalraarch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1229
Quoted 12 lines · Ashish Kalra
arch/x86/mm/fault.c +12
-0+/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level);
As discussed in [1], the lookup should be done in kvm->mm, along the lines of host_pfn_mapping_level().
[1] https://lore.kernel.org/kvm/YmwIi3bXr%2F1yhYV%[email protected]/
|
BR, Jarkko
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Quoted 10 lines
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1229
Quoted 8 lines
arch/x86/mm/fault.c +8
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level);As discussed in [1], the lookup should be done in
kvm->mm, along the lines ofhost_pfn_mapping_level().
With lookup_address_in_mm() now removed in 5.19, this is now using lookup_address_in_pgd() though still using non init-mm, and as mentioned here in [1], it makes sense to not use lookup_address_in_pgd() as it does not play nice with userspace mappings, e.g. doesn't disable IRQs to block TLB shootdowns and doesn't use READ_ONCE() to ensure an upper level entry isn't converted to a huge page between checking the PAGE_SIZE bit and grabbing the address of the next level down.
But is KVM going to provide its own variant of lookup_address_in_pgd() that is safe for use with user addresses, i.e., a generic version of lookup_address() on kvm->mm or we need to duplicate page table walking code of host_pfn_mapping_level() ?
Thanks,
Ashish
Quoted 1 line
[1] https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Flore.kernel.org%2Fkvm%2FYmwIi3bXr%252F1yhYV%252F%40google.com%2F&data=05%7C01%7CAshish.Kalra%40amd.com%>7Ce300014162fc4d8b452708da63fdb970%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637932238689925974%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%>7C%7C%7C&sdata=GxPrEUxuVNEm6COdfHCILOwp9yuX48gpoYmtrOwMx8Q%3D&reserved=0
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 21 lines · Ashish
[AMD Official Use Only - General]
Quoted 10 lines
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1229
Quoted 8 lines
arch/x86/mm/fault.c +8
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level);As discussed in [1], the lookup should be done in
kvm->mm, along the lines ofhost_pfn_mapping_level().With
lookup_address_in_mm()now removed in 5.19, this is now usinglookup_address_in_pgd()though still using non init-mm, and as mentioned here in [1], it makes sense to not uselookup_address_in_pgd()as it does not play nice with userspace mappings, e.g. doesn't disable IRQs to block TLB shootdowns and doesn't useREAD_ONCE()to ensure an upper level entry isn't converted to a huge page between checking thePAGE_SIZEbit and grabbing the address of the next level down.
Quoted 4 lines
But is KVM going to provide its own variant of
lookup_address_in_pgd()that is safe for use with user addresses, i.e., a generic version oflookup_address()onkvm->mmor we need to duplicate page table walking code ofhost_pfn_mapping_level()?
It's probably cpen coded for the sole reason that there is only one call site, i.e. there has not been rational reason to have a helper function.
Helpers are usually created only in-need basis, and since the need comes from this patch set, it should include a patch, which simply encapsulates it into a helper.
Quoted 3 lines
Thanks,
Ashish
BR, Jarkko
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Ashish KalraQuoted 3 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the
globally?
Can SNP be even enabled any other way?
I see the APM talks about it being enabled globally, I guess this means the RMP represents *all* system memory?
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1221
Quoted 13 lines
arch/x86/mm/fault.c +9
-0@@ -1209,6 +1210,60 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +static inline size_t pages_per_hpage (int level) +{ +return page_level_size (level) /PAGE_SIZE ; +} + +/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/
Magic numbers.
Pls do instead:
enum rmp_pf_ret {
RMP_PF_SPLIT = 0,
RMP_PF_RETRY = 1,
};and use those instead.
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1234
Quoted 13 lines
arch/x86/mm/fault.c +13
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/
You need to elaborate more here: a RMP fault can happen and then the page can get unmapped? What is the exact scenario here?
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1245
Quoted 11 lines
arch/x86/mm/fault.c +11
-0+if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask;
Oh boy, this is unnecessarily complicated. Isn't this
pfn |= pud_index(address);
or
pfn |= pmd_index(address);
depending on the level?
I think it is but it needs more explaining.
In any case, those are two static masks exactly and they don't need to be computed for each #PF.
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4951
Quoted 12 lines
mm/memory.c +4
-0@@ -4945,6 +4945,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ;
Yah, this looks weird: generic code implies that page splitting after a #PF makes sense only when SEV is present and none otherwise.
Why?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 8 lines
Quoted 2 lines
When SEV-SNP is enabled globally, a write from the host goes through the
globally?
Can SNP be even enabled any other way?
I see the APM talks about it being enabled globally, I guess this means the RMP represents *all* system memory?
Actually SNP feature can be enabled globally, but SNP is activated on a per VM basis.
From the APM:
The term SNP-enabled indicates that SEV-SNP is globally enabled in the SYSCFG MSR. The term SNP-active indicates that SEV-SNP is enabled for a specific VM in the SEV_FEATURES field of its VMSA
Quoted 7 lines
Quoted 4 lines
+/*
+ * Return 1 if the caller need to retry, 0 if it the address need to be split
+ * in order to resolve the fault.
+ */Magic numbers.
Quoted 8 lines
Pls do instead:
enum rmp_pf_ret { RMP_PF_SPLIT = 0, RMP_PF_RETRY = 1, };and use those instead.
Ok.
Quoted 8 lines
Quoted 8 lines
arch/x86/mm/fault.c +8
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level);
Quoted 8 lines
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1234
Quoted 5 lines
arch/x86/mm/fault.c +5
-0+ +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ You need to elaborate more here: a RMP fault can happen and then the page can get unmapped? What is the exact scenario here?
Yes, if the page gets unmapped while the RMP fault was being handled, will add more explanation here.
Quoted 14 lines
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1245
Quoted 11 lines
arch/x86/mm/fault.c +11
-0+if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 6 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);depending on the level?
Actually, the above computes an index into the RMP table. It is basically an index into the 4K page within the hugepage mapped in the RMP table or in other words an index into the RMP table entry for 4K page(s) corresponding to a hugepage.
So, pud_index()/pmd_index() can't be used for the same.
Quoted 13 lines
I think it is but it needs more explaining.
In any case, those are two static masks exactly and they don't need to be computed for each #PF.
Quoted 8 lines
mm/memory.c +0
-0@@ -4945,6 +4945,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; }
Quoted 7 lines
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4951
Quoted 4 lines
mm/memory.c +3
-0. +static int handle_split_page_fault(struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ;Yah, this looks weird: generic code implies that page splitting after a #PF makes sense only when SEV is present and none otherwise.
It is mainly a wrapper around__split_huge_pmd() for SNP use case where the host hugepage is split to be in sync with the RMP table.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 6 lines · Ashish
Actually SNP feature can be enabled globally, but SNP is activated on a per VM basis.
From the APM:
The term SNP-enabled indicates that SEV-SNP is globally enabled in the SYSCFG MSR. The term SNP-active indicates that SEV-SNP is enabled for a specific VM in theSEV_FEATURESfield of its VMSA
Aha, and I was wondering whether "globally" meant the RMP needs to cover all physical memory but I guess that isn't the case:
"RMP-Covered: Checks that the target page is covered by the RMP. A page is covered by the RMP if its corresponding RMP entry is below RMP_END. Any page not covered by the RMP is considered a Hypervisor-Owned page."
Quoted 5 lines
Quoted 2 lines
You need to elaborate more here: a RMP fault can happen and then the page can get unmapped? What is the exact scenario here?
Yes, if the page gets unmapped while the RMP fault was being handled, will add more explanation here.
So what's the logic here to return 1, i.e., retry?
Why should a fault for a page that gets unmapped be retried? The fault in that case should be ignored, IMO. It'll have the same effect to return from do_user_addr_fault() there, without splitting but you need to have a separate return value definition so that it is clear what needs to happen. And that return value should be != 0 so that the current check still works.
Quoted 1 line
Actually, the above computes an index into the RMP table.
What index in the RMP table?
Quoted 3 lines
It is basically an index into the 4K page within the hugepage mapped in the RMP table or in other words an index into the RMP table entry for 4K page(s) corresponding to a hugepage.
So pte_index(address) and for 1G pages, pmd_index(address).
So no reinventing the wheel if we already have helpers for that.
Quoted 2 lines
It is mainly a wrapper
around__split_huge_pmd()for SNP use case where the host hugepage is split to be in sync with the RMP table.
I see what it is. And I'm saying this looks wrong. You're enforcing page splitting to be a valid thing to do only for SEV machines. Why?
Why is
if (!IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))
return VM_FAULT_SIGBUS;there at all?
This is generic code you're touching - not arch/x86/.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 9 lines
Quoted 5 lines
Quoted 2 lines
You need to elaborate more here: a RMP fault can happen and then the page can get unmapped? What is the exact scenario here?
Yes, if the page gets unmapped while the RMP fault was being handled, will add more explanation here.
So what's the logic here to return 1, i.e., retry?
Why should a fault for a page that gets unmapped be retried? The fault in that case should be ignored, IMO. It'll have the same effect to return from
do_user_addr_fault()there, without splitting but you need to have a separate return value >definition so that it is clear what needs to happen. And that return value should be != 0 so that the current check still works.
if (!pte || !pte_present(*pte))
return 1;
This is more like a sanity check and returning 1 will cause the fault handler to return and ignore the fault for current #PF case. If the page got unmapped, the fault will not happen again and there will be no retry, so the fault in this case is being ignored.
The other case where 1 is returned is RMP table lookup failure, in that case the faulting process is being terminated, that resolves the fault.
Quoted 11 lines
Quoted 1 line
Actually, the above computes an index into the RMP table.
What index in the RMP table?
Quoted 3 lines
It is basically an index into the 4K page within the hugepage mapped in the RMP table or in other words an index into the RMP table entry for 4K page(s) corresponding to a hugepage.
So
pte_index(address)and for 1G pages,pmd_index(address).So no reinventing the wheel if we already have helpers for that.
Yes that makes sense and pte_index(address) is exactly what is required for 2M hugepages.
Will use pte_index() for 2M pages and pmd_index() for 1G pages.
Quoted 5 lines
Quoted 2 lines
It is mainly a wrapper
around__split_huge_pmd()for SNP use case where the host hugepage is split to be in sync with the RMP table.I see what it is. And I'm saying this looks wrong. You're enforcing page splitting to be a valid thing to do only for SEV machines. Why?
Quoted 8 lines
Why is
if (!IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) return VM_FAULT_SIGBUS;there at all?
This is generic code you're touching - not
arch/x86/.
Ok, so you are suggesting that we remove this check and simply keep this function wrapping around __split_huge_pmd(). This becomes a generic utility function.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 4 lines · Ashish
This is more like a sanity check and returning 1 will cause the fault handler to return and ignore the fault for current #PF case. If the page got unmapped, the fault will not happen again and there will be no retry, so the fault in this case is being ignored.
I know what will happen. I'm asking you to make this explicit in the code because this separate define documents the situation.
One more return type != 0 won't hurt.
Quoted 3 lines
Ok, so you are suggesting that we remove this check and simply keep this function wrapping around
__split_huge_pmd(). This becomes a generic utility function.
Yes, it is in generic code so it better be generic function. That's why I'm questioning the vendor-specific check there.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Kalra, Ashish[AMD Official Use Only - General]
Hello Boris,
Quoted 11 lines
Quoted 3 lines
It is basically an index into the 4K page within the hugepage mapped in the RMP table or in other words an index into the RMP table entry for 4K page(s) corresponding to a hugepage.
So
pte_index(address)and for 1G pages,pmd_index(address).So no reinventing the wheel if we already have helpers for that.
Yes that makes sense and
pte_index(address)is exactly what is required for 2M hugepages.Will use
pte_index()for 2M pages andpmd_index()for 1G pages.
Had a relook into this.
As I mentioned earlier, this is computing an index into a 4K page within a hugepage mapping, therefore, though pte_index() works for 2M pages, but pmd_index() will not work for 1G pages.
We basically need to do :
pfn |= (address >> PAGE_SHIFT) & mask;
where mask is the (number of 4K pages per hugepage) - 1
So this still needs the original code but with a fix for mask computation as following :
static inline size_t pages_per_hpage(int level)
return page_level_size(level) / PAGE_SIZE;
}
static int handle_user_rmp_page_fault(struct pt_regs *regs, unsigned long error_code,
unsigned long address)
{
...
pfn = pte_pfn(*pte);
/* If its large page then calculte the fault pfn */
if (level > PG_LEVEL_4K) {
+ /*
+ * index into the 4K page within the hugepage mapping
+ * in the RMP table
+ */
unsigned long mask;- mask = pages_per_hpage(level) - pages_per_hpage(level - 1);
+ mask = pages_per_hpage(level) - 1;
pfn |= (address >> PAGE_SHIFT) & mask;Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 3 lines · Ashish
As I mentioned earlier, this is computing an index into a 4K page within a hugepage mapping, therefore, though
pte_index()works for 2M pages, butpmd_index()will not work for 1G pages.
Why not? What exactly do you need to get here?
So the way I understand it is, you want to map the faulting address to a RMP entry. And that is either the 2M PMD entry when the page is a 1G one and the 4K PTE entry when the page is a 2M one?
Why doesn't pmd_index() work?
Also, why isn't the lookup function's signature:
int snp_lookup_rmpentry(unsigned long address, int *level)
and all that logic to do the conversion to a PFN also not in it?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 9 lines
Quoted 3 lines
As I mentioned earlier, this is computing an index into a 4K page within a hugepage mapping, therefore, though
pte_index()works for 2M pages, butpmd_index()will not work for 1G pages.Why not? What exactly do you need to get here?
So the way I understand it is, you want to map the faulting address to a RMP entry. And that is either the 2M PMD entry when the page is a 1G one and the 4K PTE entry when the page is a 2M one?
Why doesn't
pmd_index()work?
Yes we want to map the faulting address to a RMP entry, but hugepage entries in RMP table are basically subpage 4K entries. So it is a 4K entry when the page is a 2M one and also a 4K entry when the page is a 1G one.
That's why the computation to get a 4K page index within a 2M/1G hugepage mapping is required.
Quoted 5 lines
Also, why isn't the lookup function's signature:
int
snp_lookup_rmpentry(unsigned long address, int *level)and all that logic to do the conversion to a PFN also not in it?
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 4 lines · Ashish
Yes we want to map the faulting address to a RMP entry, but hugepage entries in RMP table are basically subpage 4K entries. So it is a 4K entry when the page is a 2M one and also a 4K entry when the page is a 1G one.
Wait, what?!
APM v2 section "15.36.11 Large Page Management" and PSMASH are then for what exactly?
Quoted 2 lines
That's why the computation to get a 4K page index within a 2M/1G hugepage mapping is required.
What if a guest RMP-faults on a 2M page and there's a corresponding 2M RMP entry? What do you need the 4K entry then for?
Hell, __snp_lookup_rmpentry() even tries to return the proper page level...
/me looks in disbelief in your direction...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 8 lines
Quoted 4 lines
Yes we want to map the faulting address to a RMP entry, but hugepage entries in RMP table are basically subpage 4K entries. So it is a 4K entry when the page is a 2M one and also a 4K entry when the page is a 1G one.
Wait, what?!
APM v2 section "15.36.11 Large Page Management" and PSMASH are then for what exactly?
This is what exactly PSMASH is for, in case the 2MB RMP entry needs to be smashed if guest PVALIDATES a 4K page, the HV will need to PSMASH the 2MB RMP entry to corresponding 4K RMP entries during #VMEXIT(NPF).
What I meant above is that 4K RMP table entries need to be available in case the 2MB RMP entry needs to be smashed.
Quoted 4 lines
Quoted 2 lines
That's why the computation to get a 4K page index within a 2M/1G hugepage mapping is required.
What if a guest RMP-faults on a 2M page and there's a corresponding 2M RMP entry? What do you need the 4K entry then for?
There is no fault here, if guest pvalidates a 2M page that is backed by a 2MB RMP entry. We need the 4K entries in case the guest pvalidates a 4K page that is mapped by a 2MB RMP entry.
Quoted 3 lines
Hell,
__snp_lookup_rmpentry()even tries to return the proper page level.../me looks in disbelief in your direction...
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
So essentially we want to map the faulting address to a RMP entry, considering the fact that a 2M host hugepage can be mapped as 4K RMP table entries and 1G host hugepage can be mapped as 2M RMP table entries.
Hence, this mask computation is done as:
mask = pages_per_hpage(level) - pages_per_hpage(level -1);
and the final faulting pfn is computed as:
pfn |= (address >> PAGE_SHIFT) & mask;
Thanks,
Ashish
-----Original Message-----
From: Kalra, Ashish
Sent: Saturday, September 3, 2022 12:51 AM
To: Borislav Petkov <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Lendacky, Thomas <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Roth, Michael <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: RE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 8 lines
Quoted 4 lines
Yes we want to map the faulting address to a RMP entry, but hugepage entries in RMP table are basically subpage 4K entries. So it is a 4K entry when the page is a 2M one and also a 4K entry when the page is a 1G one.
Wait, what?!
APM v2 section "15.36.11 Large Page Management" and PSMASH are then for what exactly?
This is what exactly PSMASH is for, in case the 2MB RMP entry needs to be smashed if guest PVALIDATES a 4K page, the HV will need to PSMASH the 2MB RMP entry to corresponding 4K RMP entries during #VMEXIT(NPF).
What I meant above is that 4K RMP table entries need to be available in case the 2MB RMP entry needs to be smashed.
Quoted 4 lines
Quoted 2 lines
That's why the computation to get a 4K page index within a 2M/1G hugepage mapping is required.
What if a guest RMP-faults on a 2M page and there's a corresponding 2M RMP entry? What do you need the 4K entry then for?
There is no fault here, if guest pvalidates a 2M page that is backed by a 2MB RMP entry. We need the 4K entries in case the guest pvalidates a 4K page that is mapped by a 2MB RMP entry.
Quoted 3 lines
Hell,
__snp_lookup_rmpentry()even tries to return the proper page level.../me looks in disbelief in your direction...
Thanks,
Ashish
Boris Petkov
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 4 lines · Ashish
[AMD Official Use Only - General]
So essentially we want to map the faulting address to a RMP entry, considering the fact that a 2M host hugepage can be mapped as 4K RMP table entries and 1G host hugepage can be mapped as 2M RMP table entries.
So something's seriously confusing or missing here because if you fault on a 2M host page and the underlying RMP entries are 4K then you can use pte_index().
If the host page is 1G and the underlying RMP entries are 2M, pmd_index() should work here too.
But this piecemeal back'n'forth doesn't seem to resolve this so I'd like to ask you pls to sit down, take your time and give a detailed example of the two possible cases and what the difference is between pte_/pmd_index and your way. Feel free to add actual debug output and paste it here.
Thanks.
Signature · 1 line
Sent from a small device: formatting sux and brevity is inevitable.
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 8 lines
Quoted 2 lines
So essentially we want to map the faulting address to a RMP entry,
considering the fact that a 2M host hugepage can be mapped as 4K RMP table entries and 1G host hugepage can be mapped as 2M RMP table entries.So something's seriously confusing or missing here because if you fault on a 2M host page and the underlying RMP entries are 4K then you can use
pte_index().If the host page is 1G and the underlying RMP entries are 2M,
pmd_index()should work here too.But this piecemeal back'n'forth doesn't seem to resolve this so I'd like to ask you pls to sit down, take your time and give a detailed example of the two possible cases and what the difference is between pte_/pmd_index and your way. Feel free to >add actual debug output and paste it here.
There is 1 64-bit RMP entry for every physical 4k page of DRAM, so essentially every 4K page of DRAM is represented by a RMP entry.
So even if host page is 1G and underlying (smashed/split) RMP entries are 2M, the RMP table entry has to be indexed to a 4K entry corresponding to that.
If it was simply a 2M entry in the RMP table, then pmd_index() will work correctly.
Considering the following example:
address = 0x40200000;
level = PG_LEVEL_1G;
pfn = 0x40000;
pfn |= pmd_index(address);
This will give the RMP table index as 0x40001.
And it will work if the RMP table entry was simply a 2MB entry, but we need to map this further to its corresponding 4K entry.
With the same example as above:
level = PG_LEVEL_1G;
mask = pages_per_hpage(level) - pages_per_hpage(level - 1);
pfn |= (address >> PAGE_SHIFT) & mask;
This will give the RMP table index as 0x40200.
Which is the correct RMP table entry for a 2MB smashed/split 1G page mapped further to its corresponding 4K entry.
Hopefully this clarifies why pmd_index() can't be used here.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 2 lines · Ashish
There is 1 64-bit RMP entry for every physical 4k page of DRAM, so essentially every 4K page of DRAM is represented by a RMP entry.
Before we get to the rest - this sounds wrong to me. My APM has:
"PSMASH Page Smash
Expands a 2MB-page RMP entry into a corresponding set of contiguous 4KB-page RMP entries. The 2MB page’s system physical address is specified in the RAX register. The new entries inherit the attributes of the original entry. Upon completion, a return code is stored in EAX. rFLAGS bits OF, ZF, AF, PF and SF are set based on this return code..."
So there *are* 2M entries in the RMP table.
Quoted 3 lines
So even if host page is 1G and underlying (smashed/split) RMP entries are 2M, the RMP table entry has to be indexed to a 4K entry corresponding to that.
So if there are 2M entries in the RMP table, how does that indexing with 4K entries is supposed to work?
Hell, even PSMASH pseudocode shows how you go and write all those 512 4K entries using the 2M entry as a template. So *before* you have smashed that 2M entry, it *is* an *actual* 2M entry.
So if you fault on a page which is backed by that 2M RMP entry, you will get that 2M RMP entry.
Quoted 2 lines
If it was simply a 2M entry in the RMP table, then
pmd_index()will work correctly.
Judging by the above text, it *can* *be* a 2M RMP entry!
By reading your example you're trying to tell me that a RMP #PF will always need to work on 4K entries. Which would then need for a 2M entry as above to be PSMASHed in order to get the 4K thing. But that would be silly - RMP PFs will this way gradually break all 2M pages and degrage performance for no real reason.
So this still looks real wrong to me.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Hello Boris,
Quoted 22 lines
Quoted 2 lines
There is 1 64-bit RMP entry for every physical 4k page of DRAM, so essentially every 4K page of DRAM is represented by a RMP entry.
Before we get to the rest - this sounds wrong to me. My APM has:
"PSMASH Page Smash
Expands a 2MB-page RMP entry into a corresponding set of contiguous 4KB-page RMP entries. The 2MB page's system physical address is specified in the RAX register. The new entries inherit the attributes of the original entry. Upon completion, a >return code is stored in EAX. rFLAGS bits OF, ZF, AF, PF and SF are set based on this return code..."
So there *are* 2M entries in the RMP table.
So even if host page is 1G and underlying (smashed/split) RMP entries are 2M, the RMP table entry has to be indexed to a 4K entry corresponding to that.
So if there are 2M entries in the RMP table, how does that indexing with 4K entries is supposed to work?
Hell, even PSMASH pseudocode shows how you go and write all those 512 4K entries using the 2M entry as a template. So *before* you have smashed that 2M entry, it *is* an *actual* 2M entry.
So if you fault on a page which is backed by that 2M RMP entry, you will get that 2M RMP entry.
Quoted 8 lines
If it was simply a 2M entry in the RMP table, then
pmd_index()will work correctly.Judging by the above text, it *can* *be* a 2M RMP entry!
By reading your example you're trying to tell me that a RMP #PF will always need to work on 4K entries. Which would then need for a 2M entry as above to be PSMASHed in order to get the 4K thing. But that would be silly - RMP PFs will this way >gradually break all 2M pages and degrage performance for no real reason.
So this still looks real wrong to me.
Please note that RMP table entries have only 2 page size indicators 4k and 2M, so it covers a max physical address range of 2MB. In all cases, there is one RMP entry per 4K page and the index into the RMP table is basically address /PAGE_SIZE, and that does not change for hugepages. Therefore we need to capture the address bits (from address) so that we index into the 4K entry in the RMP table.
An important point to note here is that RMPUPDATE instruction sets the Assigned bit for all the sub-page entries for a hugepage mapping in RMP table, so we will get the correct "assigned" page information when we index into the 4K entry in the RMP table and additionally, __snp_lookup_rmpentry() gets the 2MB aligned entry in the RMP table to get the correct Page size. (as below)
static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)
{
..
/* Read a large RMP entry to get the correct page level used in RMP entry. */
large_entry = rmptable_entry(paddr & PMD_MASK);
*level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(large_entry));
..Therefore, the 2M entry and it's subpages in the RMP table will always exist because of the RMPUPDATE instruction even without smashing/splitting of the hugepage, so we really don't need the 2MB entry to be PSMASHed in order to get the 4K thing.
Thanks,
Ashish
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Borislav PetkovQuoted 12 lines · Borislav Petkov
Review
Quoted 8 lines · Ashish Kalra
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);
I played with this a bit and ended up with
pfn = pte_pfn(*pte) | PFN_DOWN(address & page_level_mask(level - 1));
Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
BR, Jarkko
From c92522f6199055cd609ddd785dc9d8e85153e3b4 Mon Sep 17 00:00:00 2001
From: Jarkko Sakkinen <[email protected]>
Date: Tue, 6 Sep 2022 09:51:59 +0300
Subject: [PATCH] x86/fault: Simplify PFN calculation in
handle_user_rmp_fault()
Use functions in asm/pgtable.h to calculate the PFN for the address inside PTE's page directory. PG_LEVEL_4K PTE's obviously do not have a page directory but it is not an issue as:
page_level_mask(PG_LEVEL_4K - 1) == page_level_mask(PG_LEVEL_NONE) == 0
--- arch/x86/mm/fault.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
arch/x86/mm/fault.c +2 -14
@@ -1219,11 +1219,6 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); -static inline size_t pages_per_hpage (int level) -{ -return page_level_size (level) /PAGE_SIZE ; -} -/* * Return 1 if the caller need to retry, 0 if it the address need to be split * in order to resolve the fault.
@@ -1248,15 +1243,8 @@ static int handle_user_rmp_page_fault(struct pt_regs *regs, unsigned long error_if (!pte || !pte_present (*pte))return 1 ; - pfn =pte_pfn (*pte); - -/* If its large page then calculte the fault pfn */ -if (level >PG_LEVEL_4K ) { -unsigned long mask; - - mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); - pfn |= (address >>PAGE_SHIFT ) & mask; - } +/* Calculate PFN inside the page directory: */ + pfn =pte_pfn (*pte) |PFN_DOWN (address &page_level_mask (level -1 ));/* * If its a guest private page, then the fault cannot be resolved.
Signature · 1 line
2.37.2
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 18 lines · Jarkko Sakkinen
Quoted 12 lines · Borislav Petkov
Review
Quoted 8 lines · Ashish Kalra
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);
Quoted 6 lines
I played with this a bit and ended up with
pfn = pte_pfn(*pte) | PFN_DOWN(address & page_level_mask(level - 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
IMHO a better name for this function would be do_user_rmp_addr_fault() as it is more consistent with the existing function names.
BR, Jarkko
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Jarkko SakkinenQuoted 19 lines · Jarkko Sakkinen
Quoted 12 lines · Borislav Petkov
Review
Quoted 8 lines · Ashish Kalra
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);
Quoted 6 lines
I played with this a bit and ended up with
pfn = pte_pfn(*pte) | PFN_DOWN(address & page_level_mask(level - 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level = PG_LEVEL_2M they're the same.
- For level = PG_LEVEL_1G:
The current code calculates a garbage mask:
mask = pages_per_hpage(level) - pages_per_hpage(level - 1); translates to:
Quoted 1 line
Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'0x3fe00'
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'0x3ffff'
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Quoted 26 lines
Quoted 18 lines · Borislav Petkov
Quoted 12 lines · Ashish Kalra
Review
Quoted 8 lines
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);Quoted 7 lines
I played with this a bit and ended up with
pfn =
pte_pfn(*pte)| PFN_DOWN(address & page_level_mask(level
- 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Quoted 7 lines
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'
No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits to get to the correct 4K index into the RMP table.
Therefore, for level = PG_LEVEL_1G:
mask = pages_per_hpage(level) - pages_per_hpage(level - 1) => 0x3fe00 (which is the correct mask).
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'
We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
Thanks,
Ashish
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 33 lines · Ashish
[AMD Official Use Only - General]
Quoted 31 lines
Quoted 18 lines · Borislav Petkov
Quoted 12 lines · Ashish Kalra
Review
Quoted 8 lines
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);Quoted 7 lines
I played with this a bit and ended up with
pfn =
pte_pfn(*pte)| PFN_DOWN(address & page_level_mask(level
- 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:
Quoted 7 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits to get to the correct 4K index into the RMP table.
Therefore, for level =PG_LEVEL_1G:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1)=>0x3fe00(which is the correct mask).
That's the correct mask to grab the 2M-aligned address bits, e.g:
pfn_mask = 3fe00h = 11 1111 1110 0000 0000b
So the last 9 bits are ignored, e.g. anything PFNs that are multiples of 512 (2^9), and the upper bits comes from the 1GB PTE entry.
But there is an open question of whether we actually want to index using 2M-aligned or specific 4K-aligned PFN indicated by the faulting address.
Quoted 6 lines
Quoted 4 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
If we actually want the 4K page, I think we would want to use the 0x3ffff mask as Marc suggested to get to the specific 4K RMP entry, which I don't think the current code is trying to do. But maybe that *should* be what we should be doing.
Based on your earlier explanation, if we index into the RMP table using 2M-aligned address, we might find that the entry does not have the page-size bit set (maybe it was PSMASH'd for some reason). If that's the cause we'd then have to calculate the index for the specific RMP entry for the specific 4K address that caused the fault, and then check that instead.
If however we simply index directly in the 4K RMP entry from the start, snp_lookup_rmpentry() should still tell us whether the page is private or not, because RMPUPDATE/PSMASH are both documented to also update the assigned bits for each 4K RMP entry even if you're using a 2M RMP entry and setting the page-size bit to cover the whole 2M range.
Additionally, snp_lookup_rmpentry() already has logic to also go check the 2M-aligned RMP entry to provide an indication of what level it is mapped at in the RMP table, so we can still use that to determine if the host mapping needs to be split or not.
One thing that could use some confirmation is what happens if you do an RMPUPDATE for a 2MB RMP entry, and then go back and try to RMPUPDATE a sub-page and change the assigned bit so it's not consistent with 2MB RMP entry. I would assume that would fail the instruction, but we should confirm that before relying on this logic.
-Mike
Quoted 3 lines
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
[AMD Official Use Only - General]
Quoted 23 lines
Quoted 5 lines
Quoted 5 lines
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:Quoted 7 lines
Quoted 2 lines
Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'No actually this is not a garbage mask, as I explained in earlier
responses we need to capture the address bits to get to the correct 4K index into the RMP table. Therefore, for level =PG_LEVEL_1G:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1)=>0x3fe00(which is the correct mask).That's the correct mask to grab the 2M-aligned address bits, e.g:
pfn_mask= 3fe00h = 11 1111 1110 0000 0000bSo the last 9 bits are ignored, e.g. anything PFNs that are multiples of 512 (2^9), and the upper bits comes from the 1GB PTE entry.
But there is an open question of whether we actually want to index using 2M-aligned or specific 4K-aligned PFN indicated by the faulting address.
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
If we actually want the 4K page, I think we would want to use the
0x3ffffmask as Marc suggested to get to the specific 4K RMP entry, which I don't think the current code is trying to do. But maybe that *should* be what we should be doing.
Ok, I agree to get to the specific 4K RMP entry.
Quoted 1 line
Based on your earlier explanation, if we index into the RMP table using 2M-aligned address, we might find that the entry does not have the page-size bit set (maybe it was PSMASH'd for some reason).
I believe that PSMASH does update the 2M-aligned RMP table entry to the smashed page size.
It sets all the 4K intermediate/smashed pages size to 4K and changes the page size of the base RMP table (2M-aligned) entry to 4K.
Quoted 4 lines
If that's the cause we'd then have to calculate the index for the specific RMP entry for the specific 4K address that caused the fault, and then check that instead.
If however we simply index directly in the 4K RMP entry from the start,
snp_lookup_rmpentry()should still tell us whether the page is private or not, because RMPUPDATE/PSMASH are both documented to also update the assigned bits for each 4K RMP entry even if you're using a 2M RMP entry and setting the page-size >bit to cover the whole 2M range.
I think it does make sense to index directly into the 4K RMP entry, as we should be indexing into the most granular entry in the RMP table, and that will have the page "assigned" information as both RMPUPDATE/PSMASH would update the assigned bits for each 4K RMP entry even if we using a 2MB RMP entry (this is an important point to note).
Quoted 1 line
Additionally,
snp_lookup_rmpentry()already has logic to also go check the 2M-aligned RMP entry to provide an indication of what level it is mapped at in the RMP table, so we can still use that to determine if the host mapping needs to be split or >not.
Yes.
Quoted 1 line
One thing that could use some confirmation is what happens if you do an RMPUPDATE for a 2MB RMP entry, and then go back and try to RMPUPDATE a sub-page and change the assigned bit so it's not consistent with 2MB RMP entry. I would assume >that would fail the instruction, but we should confirm that before relying on this logic.
I agree.
Thanks,
Ashish
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
If we actually want the 4K page, I think we would want to use the
0x3ffffmask as Marc suggested to get to the specific 4K RMP entry, which I don't think the current code is trying to do. But maybe that *should* be what we should be doing.Ok, I agree to get to the specific 4K RMP entry.
Thanks, Michael, for a thorough and complete reply! I have to admit, there was some nuance I missed in my earlier reply. But after reading through what you wrote, I agree, always going to the 4k-entry to get the "assigned" bit and also leveraging the implementation of snp_lookup_rmpentry() to lookup the size bit in the 2M-aligned entry seems like an elegant way to code this up. Assuming this suggestion becomes the consensus, we might consider a comment in the source code to capture this discussion. Otherwise, I think I'll forget all of this the next time I'm reading this code :-). Something like:
/*
* The guest-assigned bit is always propagated to the paddr's respective 4k RMP
* entry -- regardless of the actual RMP page size. In contrast, the RMP page
* size, handled in snp_lookup_rmpentry(), is indicated by the 2M-aligned RMP
* entry.
*/
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Kalra, AshishQuoted 41 lines · Ashish
[AMD Official Use Only - General]
Quoted 26 lines
Quoted 18 lines · Borislav Petkov
Quoted 12 lines · Ashish Kalra
Review
Quoted 8 lines
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);Quoted 7 lines
I played with this a bit and ended up with
pfn =
pte_pfn(*pte)| PFN_DOWN(address & page_level_mask(level
- 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Quoted 7 lines
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits to get to the correct 4K index into the RMP table.
Therefore, for level =PG_LEVEL_1G:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1)=>0x3fe00(which is the correct mask).
Quoted 5 lines
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
I think you're correct, so I'll retry:
(address / PAGE_SIZE) & (pages_per_hpage(level) - pages_per_hpage(level - 1)) =
(address / PAGE_SIZE) & ((page_level_size(level) / PAGE_SIZE) - (page_level_size(level - 1) / PAGE_SIZE)) =
[ factor out 1 / PAGE_SIZE ]
(address & (page_level_size(level) - page_level_size(level - 1))) / PAGE_SIZE =
[ Substitute with PFN_DOWN() ]
PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)))
So you can just:
pfn = pte_pfn(*pte) | PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)));
Which is IMHO way better still what it is now because no branching and no ad-hoc helpers (the current is essentially just page_level_size wrapper).
BR, Jarkko
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 62 lines · Jarkko Sakkinen
Quoted 41 lines · Ashish
[AMD Official Use Only - General]
Quoted 26 lines
Quoted 18 lines · Borislav Petkov
Quoted 12 lines · Ashish Kalra
Review
Quoted 8 lines
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);Quoted 7 lines
I played with this a bit and ended up with
pfn =
pte_pfn(*pte)| PFN_DOWN(address & page_level_mask(level
- 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Quoted 7 lines
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits to get to the correct 4K index into the RMP table.
Therefore, for level =PG_LEVEL_1G:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1)=>0x3fe00(which is the correct mask).Quoted 5 lines
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
I think you're correct, so I'll retry:
(address /
PAGE_SIZE) & (pages_per_hpage(level)-pages_per_hpage(level - 1)) =(address /
PAGE_SIZE) & ((page_level_size(level)/PAGE_SIZE) - (page_level_size(level - 1)/PAGE_SIZE)) =[ factor out 1 /
PAGE_SIZE](address & (
page_level_size(level)-page_level_size(level - 1))) /PAGE_SIZE=[ Substitute with
PFN_DOWN()]
PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)))
Quoted 7 lines
So you can just:
pfn =
pte_pfn(*pte)|PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)));Which is IMHO way better still what it is now because no branching and no ad-hoc helpers (the current is essentially just
page_level_sizewrapper).
I created a small test program:
$ cat test.c
#include <stdio.h>
int main(void)
{
unsigned long arr[] = {0x8, 0x1000, 0x200000, 0x40000000, 0x8000000000};
int i; for (i = 1; i < sizeof(arr)/sizeof(unsigned long); i++) {
printf("%048b\n", arr[i] - arr[i - 1]);
printf("%048b\n", (arr[i] - 1) ^ (arr[i - 1] - 1));
}
}kultaheltta in linux on host-snp-v7 [?]
$ gcc -o test test.c
kultaheltta in linux on host-snp-v7 [?]
$ ./test 000000000000000000000000000000000000111111111000 000000000000000000000000000000000000111111111000 000000000000000000000000000111111111000000000000 000000000000000000000000000111111111000000000000 000000000000000000111111111000000000000000000000 000000000000000000111111111000000000000000000000 000000000000000011000000000000000000000000000000 000000000000000011000000000000000000000000000000
So the operation could be described as:
pfn = PFN_DOWN(address & (~page_level_mask(level) ^ ~page_level_mask(level - 1)));
Which IMHO already documents itself quite well: index with the granularity of PGD by removing bits used for PGD's below it.
BR, Jarkko
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
Quoted 100 lines · Jarkko Sakkinen
Quoted 62 lines · Jarkko Sakkinen
Quoted 41 lines · Ashish
[AMD Official Use Only - General]
Quoted 26 lines
Quoted 18 lines · Borislav Petkov
Quoted 12 lines · Ashish Kalra
Review
Quoted 8 lines
(quoted diff) +8
-0+ pfn = pte_pfn(*pte); + + /* If its large page then calculte the fault pfn */ + if (level > PG_LEVEL_4K) { + unsigned long mask; + + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask;Oh boy, this is unnecessarily complicated. Isn't this
Quoted 4 lines
pfn |= pud_index(address);or
pfn |= pmd_index(address);Quoted 7 lines
I played with this a bit and ended up with
pfn =
pte_pfn(*pte)| PFN_DOWN(address & page_level_mask(level
- 1));Unless I got something terribly wrong, this should do the same (see the attached patch) as the existing calculations.
Quoted 7 lines
Actually, I don't think they're the same. I think Jarkko's version is correct. Specifically:
- For level =PG_LEVEL_2Mthey're the same.
- For level =PG_LEVEL_1G:
The current code calculates a garbage mask:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1); translates to:Quoted 1 line
Quoted 1 line
hex(262144 - 512)
'
0x3fe00'No actually this is not a garbage mask, as I explained in earlier responses we need to capture the address bits to get to the correct 4K index into the RMP table.
Therefore, for level =PG_LEVEL_1G:
mask =pages_per_hpage(level)-pages_per_hpage(level - 1)=>0x3fe00(which is the correct mask).Quoted 5 lines
Quoted 3 lines
But I believe Jarkko's version calculates the correct mask (below), incorporating all 18 offset bits into the 1G page.
Quoted 1 line
Quoted 1 line
hex(262144 -1)
'
0x3ffff'We can get this simply by doing (page_per_hpage(level)-1), but as I mentioned above this is not what we need.
I think you're correct, so I'll retry:
(address /
PAGE_SIZE) & (pages_per_hpage(level)-pages_per_hpage(level - 1)) =(address /
PAGE_SIZE) & ((page_level_size(level)/PAGE_SIZE) - (page_level_size(level - 1)/PAGE_SIZE)) =[ factor out 1 /
PAGE_SIZE](address & (
page_level_size(level)-page_level_size(level - 1))) /PAGE_SIZE=[ Substitute with
PFN_DOWN()]
PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)))Quoted 7 lines
So you can just:
pfn =
pte_pfn(*pte)|PFN_DOWN(address & (page_level_size(level) - page_level_size(level - 1)));Which is IMHO way better still what it is now because no branching and no ad-hoc helpers (the current is essentially just
page_level_sizewrapper).I created a small test program:
$ cat test.c #include <stdio.h> int main(void) { unsigned long arr[] = {0x8, 0x1000, 0x200000, 0x40000000, 0x8000000000}; int i;for (i = 1; i < sizeof(arr)/sizeof(unsigned long); i++) { printf("%048b\n", arr[i] - arr[i - 1]); printf("%048b\n", (arr[i] - 1) ^ (arr[i - 1] - 1)); } }kultaheltta in linux on host-snp-v7 [?]
$ gcc -o test test.ckultaheltta in linux on host-snp-v7 [?]
$ ./test 000000000000000000000000000000000000111111111000 000000000000000000000000000000000000111111111000 000000000000000000000000000111111111000000000000 000000000000000000000000000111111111000000000000 000000000000000000111111111000000000000000000000 000000000000000000111111111000000000000000000000 000000000000000011000000000000000000000000000000 000000000000000011000000000000000000000000000000
Quoted 7 lines
So the operation could be described as:
pfn = PFN_DOWN(address & (~page_level_mask(level) ^ ~page_level_mask(level - 1)));Which IMHO already documents itself quite well: index with the granularity of PGD by removing bits used for PGD's below it.
I mean:
pfn = pte_pfn(*pte) | PFN_DOWN(address & (~page_level_mask(level) ^ ~page_level_mask(level - 1)));
Note that PG_LEVEL_4K check is unnecessary as the result will be zero after PFN_DOWN().
BR, Jarkko
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Ashish KalraQuoted 83 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk:
1. Assigned bit in the RMP table is zero (i.e page is shared).
2. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
3. Immutable bit in the RMP table is not zero.The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/mm/fault.c | 66 ++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 3 +- include/linux/mm_types.h | 3 ++ mm/memory.c | 13 ++++++++ 4 files changed, 84 insertions(+), 1 deletion(-)arch/x86/mm/fault.c +41
-0@@ -19,6 +19,7 @@#include <linux/uaccess.h> /* faulthandler_disabled() */ #include <linux/efi.h> /* efi_crash_gracefully_on_page_fault()*/ #include <linux/mm_types.h> +#include <linux/sev.h> /* snp_lookup_rmpentry() */ #include <asm/cpufeature.h> /* boot_cpu_has, ... */ #include <asm/traps.h> /* dotraplinkage, ... */ @@ -1209,6 +1210,60 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, }NOKPROBE_SYMBOL (do_kern_addr_fault); +static inline size_t pages_per_hpage (int level) +{ +return page_level_size (level) /PAGE_SIZE ; +} + +/* +* Return 1 if the caller need to retry, 0 if it the address need to be split +* in order to resolve the fault. +*/ +static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{ +int rmp_level, level; +pte_t *pte; +u64 pfn; + + pte =lookup_address_in_mm (current->mm, address, &level); + +/* +* It can happen if there was a race between an unmap event and +* the RMP fault delivery. +*/ +if (!pte || !pte_present (*pte)) +return 1 ; + + pfn =pte_pfn (*pte); + +/* If its large page then calculte the fault pfn */ +if (level >PG_LEVEL_4K ) { +unsigned long mask; + + mask =pages_per_hpage (level) -pages_per_hpage (level -1 ); + pfn |= (address >>PAGE_SHIFT ) & mask; + } + +/* +* If its a guest private page, then the fault cannot be resolved. +* Send a SIGBUS to terminate the process. +*/ +if (snp_lookup_rmpentry (pfn, &rmp_level)) {
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1262
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+do_sigbus (regs, error_code, address,VM_FAULT_SIGBUS ); +return 1 ; + } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ;
I don't see any checks that make sure this is in fact a THP, and not e.g. hugetlb (which is disallowed only later in patch 25/49), or even something else unexpected. Calling blindly __split_huge_pmd() in handle_split_page_fault() on anything that's not a THP will just make it return without splitting anything, and then this will result in a page fault loop? Some kind of warning and a SIGBUS would be more safe I think.
Quoted 91 lines
arch/x86/mm/fault.c +15
-0+ +return 1 ; +} +/* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost @@ -1306,6 +1361,17 @@ void do_user_addr_fault(struct pt_regs *regs,if (error_code &X86_PF_INSTR ) flags |=FAULT_FLAG_INSTRUCTION ; +/* +* If its an RMP violation, try resolving it. +*/ +if (error_code &X86_PF_RMP ) { +if (handle_user_rmp_page_fault (regs, error_code, address)) +return ; + +/* Ask to split the page */ + flags |=FAULT_FLAG_PAGE_SPLIT ; + } +#ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The include/linux/mm.h +2
-1@@ -463,7 +463,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" }/* * vm_fault is filled by the pagefault handler and passed to the vma's include/linux/mm_types.h +3
-0@@ -818,6 +818,8 @@ typedef struct {* mapped R/O. * @FAULT_FLAG_ORIG_PTE_VALID: whether the fault has vmf->orig_pte cached. * We should only access orig_pte if this flag set. +* @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the +* region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -855,6 +857,7 @@ enum fault_flag {FAULT_FLAG_INTERRUPTIBLE =1 <<9 ,FAULT_FLAG_UNSHARE =1 <<10 ,FAULT_FLAG_ORIG_PTE_VALID =1 <<11 , + FAULT_FLAG_PAGE_SPLIT = 1 << 12, };typedef unsigned int __bitwise zap_flags_t ;mm/memory.c +13
-0@@ -4945,6 +4945,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)return 0 ; } +static int handle_split_page_fault (struct vm_fault *vmf) +{ +if (!IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT )) +return VM_FAULT_SIGBUS ; + +__split_huge_pmd (vmf->vma, vmf->pmd, vmf->address, false,NULL ); +return 0 ; +} +/* * By the time we get here, we already hold the mm semaphore * @@ -5024,6 +5033,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,pmd_migration_entry_wait (mm, vmf.pmd);return 0 ; } + +if (flags &FAULT_FLAG_PAGE_SPLIT ) +return handle_split_page_fault (&vmf); +if (pmd_trans_huge (vmf.orig_pmd) ||pmd_devmap (vmf.orig_pmd)) {if (pmd_protnone (vmf.orig_pmd) &&vma_is_accessible (vma))return do_huge_pmd_numa_page (&vmf);
Re: [PATCH Part2 v6 09/49] x86/fault: Add support to handle the RMP fault for user address
In reply to Ashish KalraQuoted 11 lines · Ashish Kalra
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk:
1. Assigned bit in the RMP table is zero (i.e page is shared).
2. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0.
3. Immutable bit in the RMP table is not zero.
Quoted 6 lines
The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUS signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync.
When you're working on this changelog for Borislav, I'd like to make one other suggestion: Please write it more logically and _less_ about what the hardware is doing. We don't need about the internal details of what hardware is doing in the changelog. Mentioning whether an RMP bit is 0 or 1 is kinda silly unless it matters to the code.
For instance, what does the immutable bit have to do with all of this? There's no specific handling for it. There are really only faults that you can handle and faults that you can't.
There's also some major missing context here about how it guarantees that pages that can't be handled *CAN* be split. I think it has to do with disallowing hugetlbfs which implies that the only pages that might
need splitting are THP's.+ /*
arch/x86/mm/fault.c void do_user_addr_fault(struct pt_regs *regs, · line 1373
Quoted 9 lines
arch/x86/mm/fault.c +9
-0+* If its an RMP violation, try resolving it. +*/ +if (error_code &X86_PF_RMP ) { +if (handle_user_rmp_page_fault (regs, error_code, address)) +return ; + +/* Ask to split the page */ + flags |=FAULT_FLAG_PAGE_SPLIT ; + }
This also needs some chatter about why any failure to handle the fault automatically means splitting a page.
Ashish Kalra
maintainerPATCH v6 10/49 x86/fault: Add support to dump RMP entry on fault
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)
arch/x86/include/asm/sev.h +7 -0
@@ -113,6 +113,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)
@@ -205,6 +210,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +void dump_rmpentry (u64 pfn);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }
@@ -229,6 +235,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline void dump_rmpentry (u64 pfn) {}#endif #endif
arch/x86/kernel/sev.c +43 -0
49 lines not on this page · raw message
arch/x86/mm/fault.c +13 -4
@@ -34,6 +34,7 @@#include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ #include <asm/irq_stack.h> +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
@@ -290,7 +291,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];
@@ -346,10 +347,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;p4d_t *p4d;pud_t *pud;pmd_t *pmd;
@@ -367,6 +369,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;
@@ -375,6 +378,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;
@@ -383,6 +387,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;
@@ -391,9 +396,13 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); + +if (show_rmpentry) +dump_rmpentry (pfn);return ; bad:pr_info ("BAD\n" );
@@ -579,7 +588,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); +dump_pagetable (address, error_code &X86_PF_RMP ); }static noinline void
@@ -596,7 +605,7 @@ pgtable_bad(struct pt_regs *regs, unsigned long error_code,printk (KERN_ALERT "%s: Corrupted page table at address %lx\n" , tsk->comm, address); -dump_pagetable (address); +dump_pagetable (address, false);if (__die ("Bad pagetable" , regs, error_code)) sig =0 ;
include/linux/sev.h +2 -0
@@ -16,6 +16,7 @@ int snp_lookup_rmpentry(u64 pfn, int *level);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void dump_rmpentry (u64 pfn);#else static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }static inline int psmash (u64 pfn) {return -ENXIO ; }
@@ -25,6 +26,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +static inline void dump_rmpentry (u64 pfn) { }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_H */
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 10/49] x86/fault: Add support to dump RMP entry on fault
Quoted 53 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +7
-0@@ -113,6 +113,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)@@ -205,6 +210,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +void dump_rmpentry (u64 pfn);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }@@ -229,6 +235,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline void dump_rmpentry (u64 pfn) {}#endif #endif arch/x86/kernel/sev.c +0
-0@@ -2414,6 +2414,49 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)return entry;
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2424
Quoted 10 lines
arch/x86/kernel/sev.c +8
-0} +void dump_rmpentry (u64 pfn) +{ +unsigned long pfn_end; +struct rmpentry *e; +int level; + + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) {
__snp_lookup_rmpentry may return -errno so this should be:
if (e != 1)
Quoted 15 lines
arch/x86/kernel/sev.c +15
-0+pr_alert ("failed to read RMP entry pfn 0x%llx\n" , pfn); +return ; + } + +if (rmpentry_assigned (e)) { +pr_alert ("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx" +" asid=%d vmsa=%d validated=%d]\n" , pfn <<PAGE_SHIFT , +rmpentry_assigned (e),rmpentry_immutable (e),rmpentry_pagesize (e), +rmpentry_gpa (e),rmpentry_asid (e),rmpentry_vmsa (e), +rmpentry_validated (e)); +return ; + } + +/* +* If the RMP entry at the faulting pfn was not assigned, then we do not
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2449
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+* know what caused the RMP violation. To get some useful debug information, +* let iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/ + pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + e =__snp_lookup_rmpentry (pfn, &level); +if (!e)
if (e != 1)
Quoted 126 lines
arch/x86/kernel/sev.c +10
-0+return ; + +if (e->low || e->high) +pr_alert ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e->high, e->low); + pfn++; + } +} +EXPORT_SYMBOL_GPL (dump_rmpentry); +/* * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, * and -errno if there is no corresponding RMP entry. arch/x86/mm/fault.c +13
-4@@ -34,6 +34,7 @@#include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ #include <asm/irq_stack.h> +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h> @@ -290,7 +291,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];@@ -346,10 +347,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;p4d_t *p4d;pud_t *pud;pmd_t *pmd;@@ -367,6 +369,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;@@ -375,6 +378,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;@@ -383,6 +387,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;@@ -391,9 +396,13 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); + +if (show_rmpentry) +dump_rmpentry (pfn);return ; bad:pr_info ("BAD\n" );@@ -579,7 +588,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); +dump_pagetable (address, error_code &X86_PF_RMP ); }static noinline void@@ -596,7 +605,7 @@ pgtable_bad(struct pt_regs *regs, unsigned long error_code,printk (KERN_ALERT "%s: Corrupted page table at address %lx\n" , tsk->comm, address); -dump_pagetable (address); +dump_pagetable (address, false);if (__die ("Bad pagetable" , regs, error_code)) sig =0 ;include/linux/sev.h +2
-0@@ -16,6 +16,7 @@ int snp_lookup_rmpentry(u64 pfn, int *level);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void dump_rmpentry (u64 pfn);#else static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }static inline int psmash (u64 pfn) {return -ENXIO ; }@@ -25,6 +26,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +static inline void dump_rmpentry (u64 pfn) { }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_H */ Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 10/49] x86/fault: Add support to dump RMP entry on fault
Quoted 67 lines · Jeremi Piotrowski
Quoted 53 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +7
-0@@ -113,6 +113,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)#define RMPADJUST_VMSA_PAGE_BIT BIT(16)@@ -205,6 +210,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +void dump_rmpentry (u64 pfn);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }@@ -229,6 +235,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline void dump_rmpentry (u64 pfn) {}#endif #endif arch/x86/kernel/sev.c +0
-0@@ -2414,6 +2414,49 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)return entry;arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2424
Quoted 10 lines
arch/x86/kernel/sev.c +8
-0} +void dump_rmpentry (u64 pfn) +{ +unsigned long pfn_end; +struct rmpentry *e; +int level; + + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) {
__snp_lookup_rmpentrymay return -errno so this should be:
Quoted 1 line
if (e != 1)
Sorry, actually it should be:
if (IS_ERR_OR_NULL(e)) {Quoted 20 lines
Quoted 20 lines
arch/x86/kernel/sev.c +19
-0+pr_alert ("failed to read RMP entry pfn 0x%llx\n" , pfn); +return ; + } + +if (rmpentry_assigned (e)) { +pr_alert ("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx" +" asid=%d vmsa=%d validated=%d]\n" , pfn <<PAGE_SHIFT , +rmpentry_assigned (e),rmpentry_immutable (e),rmpentry_pagesize (e), +rmpentry_gpa (e),rmpentry_asid (e),rmpentry_vmsa (e), +rmpentry_validated (e)); +return ; + } + +/* +* If the RMP entry at the faulting pfn was not assigned, then we do not +* know what caused the RMP violation. To get some useful debug information, +* let iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/
Quoted 9 lines
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2449
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+ pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + e =__snp_lookup_rmpentry (pfn, &level); +if (!e)if (e != 1)
and this too:
if (IS_ERR_OR_NULL(e))
Quoted 126 lines
Quoted 126 lines
arch/x86/kernel/sev.c +10
-0+return ; + +if (e->low || e->high) +pr_alert ("RMPEntry paddr 0x%llx: [high=0x%016llx low=0x%016llx]\n" , + pfn <<PAGE_SHIFT , e->high, e->low); + pfn++; + } +} +EXPORT_SYMBOL_GPL (dump_rmpentry); +/* * Return 1 if the RMP entry is assigned, 0 if it exists but is not assigned, * and -errno if there is no corresponding RMP entry. arch/x86/mm/fault.c +13
-4@@ -34,6 +34,7 @@#include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ #include <asm/irq_stack.h> +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h> @@ -290,7 +291,7 @@ static bool low_pfn(unsigned long pfn)return pfn < max_low_pfn; } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = &base[pgd_index (address)];@@ -346,10 +347,11 @@ static int bad_address(void *p)return get_kernel_nofault (dummy, (unsigned long *)p); } -static void dump_pagetable (unsigned long address) +static void dump_pagetable (unsigned long address,bool show_rmpentry) {pgd_t *base =__va (read_cr3_pa ());pgd_t *pgd = base +pgd_index (address); +unsigned long pfn;p4d_t *p4d;pud_t *pud;pmd_t *pmd;@@ -367,6 +369,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (p4d)) goto bad; + pfn =p4d_pfn (*p4d);pr_cont ("P4D %lx " ,p4d_val (*p4d));if (!p4d_present (*p4d) ||p4d_large (*p4d)) goto out;@@ -375,6 +378,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pud)) goto bad; + pfn =pud_pfn (*pud);pr_cont ("PUD %lx " ,pud_val (*pud));if (!pud_present (*pud) ||pud_large (*pud)) goto out;@@ -383,6 +387,7 @@ static void dump_pagetable(unsigned long address)if (bad_address (pmd)) goto bad; + pfn =pmd_pfn (*pmd);pr_cont ("PMD %lx " ,pmd_val (*pmd));if (!pmd_present (*pmd) ||pmd_large (*pmd)) goto out;@@ -391,9 +396,13 @@ static void dump_pagetable(unsigned long address)if (bad_address (pte)) goto bad; + pfn =pte_pfn (*pte);pr_cont ("PTE %lx" ,pte_val (*pte)); out:pr_cont ("\n" ); + +if (show_rmpentry) +dump_rmpentry (pfn);return ; bad:pr_info ("BAD\n" );@@ -579,7 +588,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); +dump_pagetable (address, error_code &X86_PF_RMP ); }static noinline void@@ -596,7 +605,7 @@ pgtable_bad(struct pt_regs *regs, unsigned long error_code,printk (KERN_ALERT "%s: Corrupted page table at address %lx\n" , tsk->comm, address); -dump_pagetable (address); +dump_pagetable (address, false);if (__die ("Bad pagetable" , regs, error_code)) sig =0 ;include/linux/sev.h +2
-0@@ -16,6 +16,7 @@ int snp_lookup_rmpentry(u64 pfn, int *level);int psmash (u64 pfn);int rmp_make_private (u64 pfn,u64 gpa,enum pg_level level,int asid,bool immutable);int rmp_make_shared (u64 pfn,enum pg_level level); +void dump_rmpentry (u64 pfn);#else static inline int snp_lookup_rmpentry (u64 pfn,int *level) {return 0 ; }static inline int psmash (u64 pfn) {return -ENXIO ; }@@ -25,6 +26,7 @@ static inline int rmp_make_private(u64 pfn, u64 gpa, enum pg_level level, int asreturn -ENODEV ; }static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } +static inline void dump_rmpentry (u64 pfn) { }#endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_H */ Signature · 1 line
2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 10/49] x86/fault: Add support to dump RMP entry on fault
[AMD Official Use Only - General]
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2424
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+void dump_rmpentry (u64 pfn) +{ +unsigned long pfn_end; +struct rmpentry *e; +int level; + + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) {
__snp_lookup_rmpentrymay return -errno so this should be:
Quoted 5 lines
Quoted 1 line
if (e != 1)
Sorry, actually it should be:
if (
IS_ERR_OR_NULL(e)) {
I will fix this accordingly.
Quoted 11 lines
Quoted 7 lines
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2449
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ +while (pfn < pfn_end) { + e =__snp_lookup_rmpentry (pfn, &level); +if (!e)if (e != 1)
and this too:
if (
IS_ERR_OR_NULL(e))
Same here.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 10/49] x86/fault: Add support to dump RMP entry on fault
In reply to Ashish KalraQuoted 20 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP entry at the faulting pfn to help the debug.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev.h | 7 +++++++ arch/x86/kernel/sev.c | 43 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/fault.c | 17 +++++++++++---- include/linux/sev.h | 2 ++ 4 files changed, 65 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +0
-0
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 120
Quoted 8 lines
arch/x86/include/asm/sev.h +5
-0@@ -113,6 +113,11 @@ struct __packed rmpentry {#define rmpentry_assigned (x) ((x)->info.assigned)#define rmpentry_pagesize (x) ((x)->info.pagesize) +#define rmpentry_vmsa (x) ((x)->info.vmsa) +#define rmpentry_asid (x) ((x)->info.asid) +#define rmpentry_validated (x) ((x)->info.validated) +#define rmpentry_gpa (x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable (x) ((x)->info.immutable)
If you're going to do that, use inline functions pls so that it checks the argument at least.
Also, add such functions only when they're called multiple times - no need to add one for every field if you're going to access that field only once in the whole kernel.
Quoted 24 lines
arch/x86/include/asm/sev.h +2
-0#define RMPADJUST_VMSA_PAGE_BIT BIT(16)@@ -205,6 +210,7 @@ void snp_set_wakeup_secondary_cpu(void);bool snp_init (struct boot_params *bp);void snp_abort (void );int snp_issue_guest_request (u64 exit_code,struct snp_req_data *input,unsigned long *fw_err); +void dump_rmpentry (u64 pfn);#else static inline void sev_es_ist_enter (struct pt_regs *regs) { }static inline void sev_es_ist_exit (void ) { }@@ -229,6 +235,7 @@ static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *in {return -ENOTTY ; } +static inline void dump_rmpentry (u64 pfn) {}#endif #endif arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2417
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2414,6 +2414,49 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)return entry; } +void dump_rmpentry (u64 pfn)
External function - it better belong to a namespace:
sev_dump_rmpentry()
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2425
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+{ +unsigned long pfn_end; +struct rmpentry *e; +int level; + + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) { +pr_alert ("failed to read RMP entry pfn 0x%llx\n" , pfn);
Why alert?
Dumping stuff is either pr_debug or pr_info...
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+return ; + } + +if (rmpentry_assigned (e)) {
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2439
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+pr_alert ("RMPEntry paddr 0x%llx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx" +" asid=%d vmsa=%d validated=%d]\n" , pfn <<PAGE_SHIFT , +rmpentry_assigned (e),rmpentry_immutable (e),rmpentry_pagesize (e), +rmpentry_gpa (e),rmpentry_asid (e),rmpentry_vmsa (e), +rmpentry_validated (e)); +return ; + } + +/* +* If the RMP entry at the faulting pfn was not assigned, then we do not
Who's "we"?
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2452
Quoted 13 lines
arch/x86/kernel/sev.c +13
-0+* know what caused the RMP violation. To get some useful debug information, +* let iterate through the entire 2MB region, and dump the RMP entries if +* one of the bit in the RMP entry is set. +*/ + pfn = pfn & ~(PTRS_PER_PMD -1 ); + pfn_end = pfn +PTRS_PER_PMD ; + +while (pfn < pfn_end) { + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) +return ; + +if (e->low || e->high)
This is going to confuse people because they're going to miss a zero entry. Just dump the whole thing.
...
arch/x86/kernel/sev.c show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad · line 591
Quoted 6 lines
arch/x86/kernel/sev.c +1
-1@@ -579,7 +588,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long adshow_ldttss (&gdt,"TR" , tr); } -dump_pagetable (address); +dump_pagetable (address, error_code &X86_PF_RMP );
Eww.
I'd prefer to see
pfn = dump_pagetable(address); if (error_code & X86_PF_RMP) sev_dump_rmpentry(pfn);
instead of passing around this SEV-specific arg in generic x86 fault code.
The change to return the pfn from dump_pagetable() should be a pre-patch ofc.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 11/49 crypto:ccp: Define the SEV-SNP commands
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection.
Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev.
--- drivers/crypto/ccp/sev-dev.c | 14 +++ include/linux/psp-sev.h | 222 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 42 +++++++ 3 files changed, 278 insertions(+)
drivers/crypto/ccp/sev-dev.c +14 -0
@@ -153,6 +153,20 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_GET_ID :return sizeof (struct sev_data_get_id );case SEV_CMD_ATTESTATION_REPORT :return sizeof (struct sev_data_attestation_report );case SEV_CMD_SEND_CANCEL :return sizeof (struct sev_data_send_cancel ); +case SEV_CMD_SNP_GCTX_CREATE :return sizeof (struct sev_data_snp_gctx_create ); +case SEV_CMD_SNP_LAUNCH_START :return sizeof (struct sev_data_snp_launch_start ); +case SEV_CMD_SNP_LAUNCH_UPDATE :return sizeof (struct sev_data_snp_launch_update ); +case SEV_CMD_SNP_ACTIVATE :return sizeof (struct sev_data_snp_activate ); +case SEV_CMD_SNP_DECOMMISSION :return sizeof (struct sev_data_snp_decommission ); +case SEV_CMD_SNP_PAGE_RECLAIM :return sizeof (struct sev_data_snp_page_reclaim ); +case SEV_CMD_SNP_GUEST_STATUS :return sizeof (struct sev_data_snp_guest_status ); +case SEV_CMD_SNP_LAUNCH_FINISH :return sizeof (struct sev_data_snp_launch_finish ); +case SEV_CMD_SNP_DBG_DECRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_DBG_ENCRYPT :return sizeof (struct sev_data_snp_dbg ); +case SEV_CMD_SNP_PAGE_UNSMASH :return sizeof (struct sev_data_snp_page_unsmash ); +case SEV_CMD_SNP_PLATFORM_STATUS :return sizeof (struct sev_data_snp_platform_status_buf ); +case SEV_CMD_SNP_GUEST_REQUEST :return sizeof (struct sev_data_snp_guest_request ); +case SEV_CMD_SNP_CONFIG :return sizeof (struct sev_user_data_snp_config );default :return 0 ; }
include/linux/psp-sev.h +222 -0
234 lines not on this page · raw message
include/uapi/linux/psp-sev.h +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 11/49] crypto:ccp: Define the SEV-SNP commands
Quoted 19 lines · Ashish Kalra
include/linux/psp-sev.h +19
-0+/** +* struct sev_data_snp_platform_status_buf - SNP_PLATFORM_STATUS command params +* +* @address: physical address where the status should be copied +*/ +struct sev_data_snp_platform_status_buf { +u64 status_paddr;/* In */ +} __packed; + +/** +* struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE command params +* +* @address: physical address of firmware image +* @len: len of the firmware image +*/ +struct sev_data_snp_download_firmware { +u64 address;/* In */ +u32 len;/* In */ +} __packed;
include/linux/psp-sev.h struct sev_data_attestation_report { · line 590
Quoted 10 lines
include/linux/psp-sev.h +10
-0+ +/** +* struct sev_data_snp_gctx_create - SNP_GCTX_CREATE command params +* +* @gctx_paddr: system physical address of the page donated to firmware by +* the hypervisor to contain the guest context. +*/ +struct sev_data_snp_gctx_create { +u64 gctx_paddr;/* In */ +} __packed;
So some of those structs have the same layout. Let's unify them pls. I.e., for
sev_data_send_finish, sev_data_send_cancel, sev_data_receive_finish
you do
struct sev_data_tx {
u32 handle; /* In */
} __packed;For sev_data_snp_platform_status_buf, sev_data_snp_gctx_create, sev_data_snp_decommission and all those others who are a single u64, you use a single
struct sev_data_addr {
u64 gctx_paddr; /* In */
} __packed;so that we don't have gazillion structs all of different names but a lot of them identical in content.
...
Quoted 4 lines
+/**
+ * structsev_data_snp_launch_finish-SNP_LAUNCH_FINISHcommand params
+ *
+ * @gctx_addr: system pphysical address of guest context page
^^^^^^^^^
physical
Quoted 26 lines
+ */
include/linux/psp-sev.h +25
-0+struct sev_data_snp_launch_finish { +u64 gctx_paddr; +u64 id_block_paddr; +u64 id_auth_paddr; +u8 id_block_en:1 ; +u8 auth_key_en:1 ; +u64 rsvd:62 ; +u8 host_data[32 ]; +} __packed; + +/** +* struct sev_data_snp_guest_status - SNP_GUEST_STATUS command params +* +* @gctx_paddr: system physical address of guest context page +* @address: system physical address of guest status page +*/ +struct sev_data_snp_guest_status { +u64 gctx_paddr; +u64 address; +} __packed; + +/** +* struct sev_data_snp_page_reclaim - SNP_PAGE_RECLAIM command params +* +* @paddr: system physical address of page to be claimed. The BIT0 indicate
include/linux/psp-sev.h struct sev_data_attestation_report { · line 710
Quoted 10 lines
include/linux/psp-sev.h +10
-0+* the page size. 0h indicates 4 kB and 1h indicates 2 MB page. +*/ +struct sev_data_snp_page_reclaim { +u64 paddr; +} __packed; + +/** +* struct sev_data_snp_page_unsmash - SNP_PAGE_UNMASH command params +* +* @paddr: system physical address of page to be unmashed. The BIT0 indicate
Is "BIT0" the 0th bit in the address? This needs to be spelled out explicitly.
also, s/unmash/unsmash/gi
Also, FW SPEC says bits 11:0 are MBZ. So I'm guessing bit 0 is being cleared before sending it to sw. I guess I'll see that later.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 11/49] crypto:ccp: Define the SEV-SNP commands
[AMD Official Use Only - General]
Hello Boris,
Quoted 42 lines
Quoted 20 lines
include/linux/psp-sev.h +20
-0+/** +* struct sev_data_snp_platform_status_buf - SNP_PLATFORM_STATUS +command params +* +* @address: physical address where the status should be copied */ +struct sev_data_snp_platform_status_buf { +u64 status_paddr;/* In */ +} __packed; + +/** +* struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE +command params +* +* @address: physical address of firmware image +* @len: len of the firmware image +*/ +struct sev_data_snp_download_firmware { +u64 address;/* In */ +u32 len;/* In */ +} __packed;include/linux/psp-sev.h struct sev_data_attestation_report { · line 590
Quoted 10 lines
include/linux/psp-sev.h +10
-0+ +/** +* struct sev_data_snp_gctx_create - SNP_GCTX_CREATE command params +* +* @gctx_paddr: system physical address of the page donated to firmware by +* the hypervisor to contain the guest context. +*/ +struct sev_data_snp_gctx_create { +u64 gctx_paddr;/* In */ +} __packed;So some of those structs have the same layout. Let's unify them pls. I.e., for
sev_data_send_finish,sev_data_send_cancel,sev_data_receive_finishyou do
struct sev_data_tx { u32 handle; /* In */ } __packed;
Quoted 7 lines
For
sev_data_snp_platform_status_buf,sev_data_snp_gctx_create,sev_data_snp_decommissionand all those others who are a single u64, you use a singlestruct sev_data_addr { u64 gctx_paddr; /* In */ } __packed;so that we don't have gazillion structs all of different names but a lot of them identical in content.
These are structure definitions as per SNP Firmware API specifications, and they match the SNP Firmware commands and required arguments.
As an example below:
8.12 SNP_DECOMMISSION
This command destroys a guest context. After this command successfully completes, the guest will not long be runnable.
8.12.1 Parameters
Table 55. Layout of the CMDBUF_SNP_DECOMMISSION StructureGCTX_PADDR Bits 63:12 of the sPA of the guest's
context page
Isn't it better to have 1:1 mapping between specification and structure definitions here ?
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 11/49] crypto:ccp: Define the SEV-SNP commands
Quoted 3 lines · Ashish
These are structure definitions as per SNP Firmware API specifications, and they match the SNP Firmware commands and required arguments.
Yes, I have the spec.
Quoted 2 lines
Isn't it better to have 1:1 mapping between specification and structure definitions here ?
Why would it be better if you can have a single struct serving multiple purposes and thus less code to stare at and deal with?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 12/49 crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Make sure to do a WBINVD and issue DF_FLUSH command to prepare for the first SNP guest launch after INIT.
Signed-off by: Ashish Kalra <[email protected]>
--- drivers/crypto/ccp/sev-dev.c | 121 +++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 16 +++++ 3 files changed, 139 insertions(+)
drivers/crypto/ccp/sev-dev.c +121 -0
@@ -33,6 +33,10 @@#define SEV_FW_FILE "amd/sev.fw"#define SEV_FW_NAME_SIZE 64 +/* Minimum firmware version required for the SEV-SNP support */ +#define SNP_MIN_API_MAJOR 1 +#define SNP_MIN_API_MINOR 51 +static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev;
@@ -775,6 +779,98 @@ static int sev_update_firmware(struct device *dev)return ret; } +static void snp_set_hsave_pa (void *arg) +{ +wrmsrl (MSR_VM_HSAVE_PA ,0 ); +} + +static int __sev_snp_init_locked (int *error) +{ +struct psp_device *psp = psp_master; +struct sev_device *sev; +int rc =0 ; + +if (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_inited) +return 0 ; + +/* +* The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h +* across all cores. +*/ +on_each_cpu (snp_set_hsave_pa,NULL ,1 ); + +/* Issue the SNP_INIT firmware command. */ + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,NULL , error); +if (rc) +return rc; + +/* Prepare for first SNP guest launch after INIT */ +wbinvd_on_all_cpus (); + rc =__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +if (rc) +return rc; + + sev->snp_inited = true; +dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + +return rc; +} + +int sev_snp_init (int *error) +{ +int rc; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return -ENODEV ; + +mutex_lock (&sev_cmd_mutex); + rc =__sev_snp_init_locked (error); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +EXPORT_SYMBOL_GPL (sev_snp_init); + +static int __sev_snp_shutdown_locked (int *error) +{ +struct sev_device *sev = psp_master->sev_data; +int ret; + +if (!sev->snp_inited) +return 0 ; + +/* SHUTDOWN requires the DF_FLUSH */ +wbinvd_on_all_cpus (); +__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL ); + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_SHUTDOWN ,NULL , error); +if (ret) { +dev_err (sev->dev,"SEV-SNP firmware shutdown failed\n" ); +return ret; + } + + sev->snp_inited = false; +dev_dbg (sev->dev,"SEV-SNP firmware shutdown\n" ); + +return ret; +} + +static int sev_snp_shutdown (int *error) +{ +int rc; + +mutex_lock (&sev_cmd_mutex); + rc =__sev_snp_shutdown_locked (NULL ); +mutex_unlock (&sev_cmd_mutex); + +return rc; +} +static int sev_ioctl_do_pek_import (struct sev_issue_cmd *argp,bool writable) {struct sev_device *sev = psp_master->sev_data;
@@ -1231,6 +1327,8 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(NV_LENGTH)); sev_init_ex_buffer =NULL ; } + +sev_snp_shutdown (NULL ); }void sev_dev_destroy (struct psp_device *psp)
@@ -1287,6 +1385,26 @@ void sev_pci_init(void) } } +/* +* If boot CPU supports the SNP, then first attempt to initialize +* the SNP firmware. +*/ +if (cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +if (!sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR )) { +dev_err (sev->dev,"SEV-SNP support requires firmware version >= %d:%d\n" , +SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR ); + }else { + rc =sev_snp_init (&error); +if (rc) { +/* +* If we failed to INIT SNP then don't abort the probe. +* Continue to initialize the legacy SEV firmware. +*/ +dev_err (sev->dev,"SEV-SNP: failed to INIT error %#x\n" , error); + } + } + } +/* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE );if (!sev_es_tmr)
@@ -1302,6 +1420,9 @@ void sev_pci_init(void)dev_err (sev->dev,"SEV: failed to INIT error %#x, rc %d\n" , error, rc); +dev_info (sev->dev,"SEV%s API:%d.%d build:%d\n" , sev->snp_inited ? +"-SNP" :"" , sev->api_major, sev->api_minor, sev->build); +return ; err:
drivers/crypto/ccp/sev-dev.h +2 -0
@@ -52,6 +52,8 @@ struct sev_device {u8 build;void *cmd_buf; + +bool snp_inited; };int sev_dev_init (struct psp_device *psp);
include/linux/psp-sev.h +16 -0
@@ -769,6 +769,20 @@ struct sev_data_snp_init_ex {*/ int sev_platform_init (int *error); +/** +* sev_snp_init - perform SEV SNP_INIT command +* +* @error: SEV command return code +* +* Returns: +* 0 if the SEV successfully processed the command +* -%ENODEV if the SEV device is not available +* -%ENOTSUPP if the SEV does not support SEV +* -%ETIMEDOUT if the SEV command timed out +* -%EIO if the SEV returned a non-zero return code +*/ +int sev_snp_init (int *error); +/** * sev_platform_status - perform SEV PLATFORM_STATUS command *
@@ -876,6 +890,8 @@ sev_platform_status(struct sev_user_data_status *status, int *error) { return -Estatic inline int sev_platform_init (int *error) {return -ENODEV ; } +static inline int sev_snp_init (int *error) {return -ENODEV ; } +static inline int sev_guest_deactivate (struct sev_data_deactivate *data,int *error) {return -ENODEV ; }
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 798
Quoted 12 lines · Ashish Kalra
drivers/crypto/ccp/sev-dev.c +12
-0+static int __sev_snp_init_locked (int *error) +{ +struct psp_device *psp = psp_master; +struct sev_device *sev; +int rc =0 ; + +if (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_inited)
snp_inited? That's silly.
snp_initialized
pls.
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 802
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+return 0 ; + +/* +* The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h
/* Clear MSR_VM_HSAVE_PA on all cores before SNP_INIT */
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 807
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+* across all cores. +*/ +on_each_cpu (snp_set_hsave_pa,NULL ,1 ); + +/* Issue the SNP_INIT firmware command. */
Useless comment.
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 813
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+ rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,NULL , error); +if (rc) +return rc; + +/* Prepare for first SNP guest launch after INIT */ +wbinvd_on_all_cpus ();
Can you put a wbinvd() in snp_set_hsave_pa() instead and save yourself the second IPI?
Or is that order of the commands:
1. clear MSR IPI
2. SNP_INIT
3. WBINVD IPI
4. ...
mandatory?
...
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 849
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +11
-0+static int __sev_snp_shutdown_locked (int *error) +{ +struct sev_device *sev = psp_master->sev_data; +int ret; + +if (!sev->snp_inited) +return 0 ; + +/* SHUTDOWN requires the DF_FLUSH */ +wbinvd_on_all_cpus (); +__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );
Why isn't this retval checked?
Quoted 12 lines
drivers/crypto/ccp/sev-dev.c +12
-0+ + ret =__sev_do_cmd_locked (SEV_CMD_SNP_SHUTDOWN ,NULL , error); +if (ret) { +dev_err (sev->dev,"SEV-SNP firmware shutdown failed\n" ); +return ret; + } + + sev->snp_inited = false; +dev_dbg (sev->dev,"SEV-SNP firmware shutdown\n" ); + +return ret; +}
...
drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 1389
Quoted 7 lines
void
sev_dev_destroy(struct psp_device *psp)drivers/crypto/ccp/sev-dev.c +2
-0@@ -1287,6 +1385,26 @@ void sev_pci_init(void) } } +/* +* If boot CPU supports the SNP, then first attempt to initialize
s/the SNP/SNP/g
drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 1400
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +11
-0+* the SNP firmware. +*/ +if (cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +if (!sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR )) { +dev_err (sev->dev,"SEV-SNP support requires firmware version >= %d:%d\n" , +SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR ); + }else { + rc =sev_snp_init (&error); +if (rc) { +/* +* If we failed to INIT SNP then don't abort the probe.
Who's "we"?
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +7
-0+* Continue to initialize the legacy SEV firmware. +*/ +dev_err (sev->dev,"SEV-SNP: failed to INIT error %#x\n" , error); + } + } + } +/* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE );if (!sev_es_tmr)
...
Quoted 9 lines
include/linux/psp-sev.h +1
-0@@ -769,6 +769,20 @@ struct sev_data_snp_init_ex {*/ int sev_platform_init (int *error); +/**
include/linux/psp-sev.h struct sev_data_snp_init_ex { · line 782
Quoted 10 lines
include/linux/psp-sev.h +10
-0+* sev_snp_init - perform SEV SNP_INIT command +* +* @error: SEV command return code +* +* Returns: +* 0 if the SEV successfully processed the command +* -%ENODEV if the SEV device is not available +* -%ENOTSUPP if the SEV does not support SEV +* -%ETIMEDOUT if the SEV command timed out +* -%EIO if the SEV returned a non-zero return code
Something's weird with those args. I think it should be
%-ENODEV
and so on...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
Hello Boris,
Quoted 16 lines · Borislav Petkov
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 798
Quoted 12 lines · Ashish Kalra
drivers/crypto/ccp/sev-dev.c +12
-0+static int __sev_snp_init_locked (int *error) +{ +struct psp_device *psp = psp_master; +struct sev_device *sev; +int rc =0 ; + +if (!psp || !psp->sev_data) +return -ENODEV ; + + sev = psp->sev_data; + +if (sev->snp_inited)
snp_inited? That's silly.
Quoted 3 lines
snp_initializedpls.
Yes.
Quoted 26 lines
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 802
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +4
-0+return 0 ; + +/* +* The SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h /* Clear MSR_VM_HSAVE_PA on all cores before SNP_INIT */drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 807
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+* across all cores. +*/ +on_each_cpu (snp_set_hsave_pa,NULL ,1 ); + +/* Issue the SNP_INIT firmware command. */ Useless comment.
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 813
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+ rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,NULL , error); +if (rc) +return rc; + +/* Prepare for first SNP guest launch after INIT */ +wbinvd_on_all_cpus ();Can you put a
wbinvd()insnp_set_hsave_pa()instead and save yourself the second IPI?
Quoted 9 lines
Or is that order of the commands:
1. clear MSR IPI
2.SNP_INIT
3. WBINVD IPI
4. ...mandatory?
Yes, we need to do:
wbinvd_on_all_cpus();SNP_DF_FLUSH
Need to ensure all the caches are clear before launching the first guest and this has to be a combination of WBINVD and SNP_DF_FLUSH command.
Quoted 15 lines
...
drivers/crypto/ccp/sev-dev.c static int sev_update_firmware(struct device *dev) · line 849
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +11
-0+static int __sev_snp_shutdown_locked (int *error) +{ +struct sev_device *sev = psp_master->sev_data; +int ret; + +if (!sev->snp_inited) +return 0 ; + +/* SHUTDOWN requires the DF_FLUSH */ +wbinvd_on_all_cpus (); +__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );Why isn't this retval checked?
From the SNP FW ABI specs, for the SNP_SHUTDOWN command:
Firmware checks for every encryption capable ASID that the ASID is not in use by a guest and a DF_FLUSH is not required. If a DF_FLUSH is required, the firmware returns DFFLUSH_REQUIRED.
Considering that SNP_SHUTDOWN command will check if DF_FLUSH was required and if so, and not invoked before that command, returns an error indicating that DFFLUSH is required.
This way, we can cleverly avoid taking the error code path for DF_FLUSH command here and instead let the SNP_SHUTDOWN command failure below indicate if DF_FLUSH command failed.
This also ensures that we always invoke SNP_SHUTDOWN command, irrespective of SNP_DF_FLUSH command failure as SNP_DF_FLUSH may actually not be required by the SHUTDOWN command.
Quoted 75 lines
Quoted 13 lines
drivers/crypto/ccp/sev-dev.c +12
-0+ + ret =__sev_do_cmd_locked (SEV_CMD_SNP_SHUTDOWN ,NULL , error); +if (ret) { +dev_err (sev->dev,"SEV-SNP firmware shutdown failed\n" ); +return ret; + } + + sev->snp_inited = false; +dev_dbg (sev->dev,"SEV-SNP firmware shutdown\n" ); + +return ret; +}...
drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 1389
Quoted 7 lines
void
sev_dev_destroy(struct psp_device *psp)drivers/crypto/ccp/sev-dev.c +2
-0@@ -1287,6 +1385,26 @@ void sev_pci_init(void) } } +/* +* If boot CPU supports the SNP, then first attempt to initialize s/the SNP/SNP/g
drivers/crypto/ccp/sev-dev.c void sev_pci_init(void) · line 1400
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +11
-0+* the SNP firmware. +*/ +if (cpu_feature_enabled (X86_FEATURE_SEV_SNP )) { +if (!sev_version_greater_or_equal (SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR )) { +dev_err (sev->dev,"SEV-SNP support requires firmware version >= %d:%d\n" , +SNP_MIN_API_MAJOR ,SNP_MIN_API_MINOR ); + }else { + rc =sev_snp_init (&error); +if (rc) { +/* +* If we failed to INIT SNP then don't abort the probe. Who's "we"?
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +7
-0+* Continue to initialize the legacy SEV firmware. +*/ +dev_err (sev->dev,"SEV-SNP: failed to INIT error %#x\n" , error); + } + } + } +/* Obtain the TMR memory area for SEV-ES use */ sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE );if (!sev_es_tmr)...
Quoted 9 lines
include/linux/psp-sev.h +1
-0@@ -769,6 +769,20 @@ struct sev_data_snp_init_ex {*/ int sev_platform_init (int *error); +/** include/linux/psp-sev.h struct sev_data_snp_init_ex { · line 782
Quoted 10 lines
include/linux/psp-sev.h +10
-0+* sev_snp_init - perform SEV SNP_INIT command +* +* @error: SEV command return code +* +* Returns: +* 0 if the SEV successfully processed the command +* -%ENODEV if the SEV device is not available +* -%ENOTSUPP if the SEV does not support SEV +* -%ETIMEDOUT if the SEV command timed out +* -%EIO if the SEV returned a non-zero return code Something's weird with those args. I think it should be
Quoted 4 lines
%-ENODEV
and so on...
Yes, off course %-<errno>
Thanks,
Ashish
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
Some more follow up regarding avoiding the second IPI:
Quoted 21 lines
Quoted 11 lines
Quoted 7 lines
+ rc =
__sev_do_cmd_locked(SEV_CMD_SNP_INIT, NULL, error); + if (rc)
+ return rc;
+
+ /* Prepare for first SNP guest launch after INIT */ +wbinvd_on_all_cpus();Can you put a
wbinvd()insnp_set_hsave_pa()instead and save yourself the second IPI?Quoted 9 lines
Or is that order of the commands:
1. clear MSR IPI
2.SNP_INIT
3. WBINVD IPI
4. ...mandatory?
Quoted 7 lines
Yes, we need to do:
wbinvd_on_all_cpus();SNP_DF_FLUSHNeed to ensure all the caches are clear before launching the first guest and this has to be a combination of WBINVD and
SNP_DF_FLUSHcommand.
I had related discussions with the HW architect:
SNP firmware will fail ACTIVATE if DFFLUSH isn't called, and DFFLUSH requires the WBINVD on all cores. By requiring WBIDVD on all cores, we're a) requiring the caches to be flushed, and b) forcing the hypervisor to exit all guests at least once since SEV/SNP has been enabled, since the WBINVDs must be done in host mode.
The order is:VM_HSAVE_PA IPISNP_INIT
WBIVND (IPI)DF_FLUSH
so that means we can't combine the IPIs.
Also, this is not a performance critical path, so should we really be so concerned about it?
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
Quoted 1 line · Ashish
so that means we can't combine the IPIs.
Ok, thanks for checking.
Quoted 2 lines
Also, this is not a performance critical path, so should we really be so concerned about it?
Not that concerned but we're always trying to be as optimal as possible.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Kalra, AshishAnother follow up:
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
int
sev_platform_init(int *error);
+/**
+ *sev_snp_init- perform SEVSNP_INITcommand
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the SEV successfully processed the command
+ * -%ENODEV if the SEV device is not available
+ * -%ENOTSUPP if the SEV does not support SEV
+ * -%ETIMEDOUT if the SEV command timed out
+ * -%EIO if the SEV returned a non-zero return code
Quoted 9 lines
Quoted 6 lines
Something's weird with those args. I think it should be
%-ENODEV
and so on...
Yes, off course %-<errno>
I see that other drivers are also using the same convention:
include/linux/regset.h:
..
/**
* user_regset_set_fn - type of @set function in &struct user_regset
* @target: thread being examined * @regset: regset being examined * @pos: offset into the regset data to access, in bytes * @count: amount of data to copy, in bytes * @kbuf: if not %NULL, a kernel-space pointer to copy from * @ubuf: if @kbuf is %NULL, a user-space pointer to copy from
*
* Store register values. Return %0 on success; -%EIO or -%ENODEV
* are usual failure returns. The @pos and @count values are in
...
include/linux/psp-tee.h:
..
/**
* psp_tee_process_cmd() - Process command in Trusted Execution Environment
* @cmd_id: TEE command ID (&enum tee_cmd_id) * @buf: Command buffer for TEE processing. On success, is updated * with the response * @len: Length of command buffer in bytes * @status: On success, holds the TEE command execution status
*
* This function submits a command to the Trusted OS for processing in the
* TEE environment and waits for a response or until the command times out. *
* Returns:
* 0 if TEE successfully processed the command
* -%ENODEV if PSP device not available
* -%EINVAL if invalid input
* -%ETIMEDOUT if TEE command timed out
* -%EBUSY if PSP device is not responsive
*/
...
Thanks,
Ashish
Re: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
Quoted 64 lines · Ashish
Another follow up:
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
int
sev_platform_init(int *error);
+/**
+ *sev_snp_init- perform SEVSNP_INITcommand
+ *
+ * @error: SEV command return code
+ *
+ * Returns:
+ * 0 if the SEV successfully processed the command
+ * -%ENODEV if the SEV device is not available
+ * -%ENOTSUPP if the SEV does not support SEV
+ * -%ETIMEDOUT if the SEV command timed out
+ * -%EIO if the SEV returned a non-zero return codeQuoted 9 lines
Quoted 6 lines
Something's weird with those args. I think it should be
%-ENODEV
and so on...
Yes, off course %-<errno>
I see that other drivers are also using the same convention:
include/linux/regset.h:
..
/**
*user_regset_set_fn- type of @set function in &structuser_regset* @target: thread being examined * @regset: regset being examined * @pos: offset into the regset data to access, in bytes * @count: amount of data to copy, in bytes * @kbuf: if not %NULL, a kernel-space pointer to copy from * @ubuf: if @kbuf is %NULL, a user-space pointer to copy from*
* Store register values. Return %0 on success; -%EIO or -%ENODEV
* are usual failure returns. The @pos and @count values are in
...
include/linux/psp-tee.h:
..
/**
*psp_tee_process_cmd()- Process command in Trusted Execution Environment* @cmd_id: TEE command ID (&enum tee_cmd_id) * @buf: Command buffer for TEE processing. On success, is updated * with the response * @len: Length of command buffer in bytes * @status: On success, holds the TEE command execution status*
* This function submits a command to the Trusted OS for processing in the
* TEE environment and waits for a response or until the command times out. *
* Returns:
* 0 if TEE successfully processed the command
* -%ENODEV if PSP device not available
* -%EINVAL if invalid input
* -%ETIMEDOUT if TEE command timed out
* -%EBUSY if PSP device is not responsive
*/
...
Quoted 2 lines
Thanks,
Ashis
In SGX driver the convention is:
* Return:
* - -ENODEV ...
* - -EINVAL ...
"Return:" because this is what kernel-nanodoc-HOWTO.txt suggests.
The dashes are recognized by Sphinx as list so each return code gets a newline when processed through "make htmldocs". Otherwise, you get a mess because newlines will be removed.
I don't actually have idea of '%' and neither see it in the kernel documenation.
BR, Jarkko
Borislav Petkov
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Kalra, AshishQuoted 1 line · Ashish
I see that other drivers are also using the same convention:
It is only convention. Look at the .rst output:
0 if the SEV successfully processed the command
-``ENODEV`` if the SEV device is not available
-``ENOTSUPP`` if the SEV does not support SEV
-``ETIMEDOUT`` if the SEV command timed out
-``EIO`` if the SEV returned a non-zero return code
vs
0 if the SEV successfully processed the command
``-ENODEV`` if the SEV device is not available
``-ENOTSUPP`` if the SEV does not support SEV
``-ETIMEDOUT`` if the SEV command timed out
``-EIO`` if the SEV returned a non-zero return code
so in the html output of this, the minus sign will be displayed either with text font or with monospaced font as part of the error type.
I wanna say the second is better as the '-' is part of the error code but won't waste too much time debating this. :)
Btw
$ ./scripts/kernel-doc include/linux/psp-sev.h
complains a lot. Might wanna fix those up when bored or someone else who's reading this and feels bored too. :-)
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v6 12/49] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Kalra, AshishQuoted 7 lines · Ashish
Yes, we need to do:
wbinvd_on_all_cpus();SNP_DF_FLUSHNeed to ensure all the caches are clear before launching the first guest and this has to be a combination of WBINVD and
SNP_DF_FLUSHcommand.
Ok.
Quoted 12 lines
Quoted 1 line
Why isn't this retval checked?
From the SNP FW ABI specs, for the
SNP_SHUTDOWNcommand:Firmware checks for every encryption capable ASID that the ASID is not in use by a guest and a
DF_FLUSHis not required. If aDF_FLUSHis required, the firmware returnsDFFLUSH_REQUIRED.Considering that
SNP_SHUTDOWNcommand will check ifDF_FLUSHwas required and if so, and not invoked before that command, returns an error indicating that DFFLUSH is required.
Quoted 7 lines
This way, we can cleverly avoid taking the error code path for
DF_FLUSHcommand here and instead let theSNP_SHUTDOWNcommand failure below indicate ifDF_FLUSHcommand failed.This also ensures that we always invoke
SNP_SHUTDOWNcommand, irrespective ofSNP_DF_FLUSHcommand failure asSNP_DF_FLUSHmay actually not be required by the SHUTDOWN command.
This all sounds just silly. The proper way to do this is:
retry:
ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN, NULL, error);
if (ret == DFFLUSH_REQUIRED) {
ret = __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, NULL);
if (ret)
"... DF_FLUSH failed...";
goto retry;
}I'm assuming here the firmware is smart enough to not keep returning DFFLUSH_REQUIRED constantly and cause an endless loop.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 13/49 crypto:ccp: Provide APIs to issue SEV-SNP commands
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification.
--- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)
drivers/crypto/ccp/sev-dev.c +24 -0
@@ -1205,6 +1205,30 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +} +EXPORT_SYMBOL_GPL (snp_guest_df_flush); + +int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);
include/linux/psp-sev.h +73 -0
85 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
(
Quoted 21 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)drivers/crypto/ccp/sev-dev.c +1
-0@@ -1205,6 +1205,30 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error)
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 1218
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+{ +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +} +EXPORT_SYMBOL_GPL (snp_guest_df_flush);
Why not instead change sev_guest_df_flush() to be SNP aware? That way callers get the right behavior without having to know if SNP is enabled or not.
int sev_guest_df_flush(int *error)
{
if (!psp_master || !psp_master->sev_data)
return -EINVAL;
if (psp_master->sev_data->snp_inited)
return sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error); return sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
}
Quoted 109 lines
drivers/crypto/ccp/sev-dev.c +12
-0+int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +73
-1@@ -881,6 +881,64 @@ int sev_guest_df_flush(int *error);*/ int sev_guest_decommission (struct sev_data_decommission *data,int *error); +/** +* snp_guest_df_flush - perform SNP DF_FLUSH command +* +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int snp_guest_df_flush (int *error); + +/** +* snp_guest_decommission - perform SNP_DECOMMISSION command +* +* @decommission: sev_data_decommission structure to be processed +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error); + +/** +* snp_guest_page_reclaim - perform SNP_PAGE_RECLAIM command +* +* @decommission: sev_snp_page_reclaim structure to be processed +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error); + +/** +* snp_guest_dbg_decrypt - perform SEV SNP_DBG_DECRYPT command +* +* @sev_ret: sev command return code +* +* Returns: +* 0 if the sev successfully processed the command +* -%ENODEV if the sev device is not available +* -%ENOTSUPP if the sev does not support SEV +* -%ETIMEDOUT if the sev command timed out +* -%EIO if the sev returned a non-zero return code +*/ +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error); +void *psp_copy_user_blob (u64 uaddr,u32 len);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -908,6 +966,21 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, intstatic inline void *psp_copy_user_blob (u64 __user uaddr,u32 len) {return ERR_PTR (-EINVAL ); } +static inline int +snp_guest_decommission (struct sev_data_snp_decommission *data,int *error) {return -ENODEV ; } + +static inline int snp_guest_df_flush (int *error) {return -ENODEV ; } + +static inline int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error) +{ +return -ENODEV ; +} + +static inline int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ --2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
[Public]
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 1218
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); } +EXPORT_SYMBOL_GPL (snp_guest_df_flush);Why not instead change
sev_guest_df_flush()to be SNP aware? That way callers get the right behavior without having to know if SNP is enabled or not.
It can be done, and actually both DF_FLUSH commands do exactly the same thing.
But as with other API interfaces here, I think it is better to differentiate between snp and sev API interfaces and the callers be aware of which interface they are invoking.
Thanks,
Ashish
Re: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
In reply to Peter GondaQuoted 23 lines · Peter Gonda
(
Quoted 20 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)drivers/crypto/ccp/sev-dev.c +0
-0@@ -1205,6 +1205,30 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush);
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 1218
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c +11
-0+int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +} +EXPORT_SYMBOL_GPL (snp_guest_df_flush);
Nit: undocumented exported functions. Both need kdoc.
Quoted 10 lines
Why not instead change
sev_guest_df_flush()to be SNP aware? That way callers get the right behavior without having to know if SNP is enabled or not.int
sev_guest_df_flush(int *error)
{
if (!psp_master || !psp_master->sev_data)
return-EINVAL;
Quoted 5 lines
if (psp_master->sev_data->snp_inited) return sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error);return
sev_do_cmd(SEV_CMD_DF_FLUSH, NULL, error);
}
Because it serves no purpose to fuse them into one, and is only more obfuscated (and also undocumented).
Two exported symbols can be traced also separately with ftrace/kprobes.
Degrading transparency is not great idea IMHO.
BR, Jarkko
Borislav Petkov
maintainerRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
In reply to Ashish KalraQuoted 32 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+)drivers/crypto/ccp/sev-dev.c +13
-0@@ -1205,6 +1205,30 @@ int sev_guest_df_flush(int *error) }EXPORT_SYMBOL_GPL (sev_guest_df_flush); +int snp_guest_decommission (struct sev_data_snp_decommission *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); +} +EXPORT_SYMBOL_GPL (snp_guest_df_flush); + +int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error)
drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 1230
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+{ +return sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); +} +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt);
So this mindless repetition is getting annoying. I see ~70 SEV commands. Adding ~70 functions which parrot all the same call to sev_do_cmd() is just insane.
I think you should simply export sev_do_cmd() and call it instead.
Yes, when it turns out that a command and the preparation to issue it before it starts repeating pretty often, you could do a helper. But adding those silly wrappers doesn't bring anything besides confusion and code bloat.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
[AMD Official Use Only - General]
Hello Boris,
Quoted 23 lines
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +9
-0+int snp_guest_decommission (struct sev_data_snp_decommission *data, +int *error) { +return sev_do_cmd (SEV_CMD_SNP_DECOMMISSION , data, error); } +EXPORT_SYMBOL_GPL (snp_guest_decommission); + +int snp_guest_df_flush (int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DF_FLUSH ,NULL , error); } +EXPORT_SYMBOL_GPL (snp_guest_df_flush);drivers/crypto/ccp/sev-dev.c int sev_guest_df_flush(int *error) · line 1230
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data, +int *error) { +return sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , data, error); } +EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +{ +return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); } +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt);So this mindless repetition is getting annoying. I see ~70 SEV commands. Adding ~70 functions which parrot all the same call to
sev_do_cmd()is just insane.
Quoted 3 lines
I think you should simply export
sev_do_cmd()and call it instead.Yes, when it turns out that a command and the preparation to issue it before it starts repeating pretty often, you could do a helper. But adding those silly wrappers doesn't bring anything besides confusion and code bloat.
There are actually only 8 functions in total which are simply calling sev_do_cmd(), all other functions calling sev_do_cmd() have a lot of other specific functionality in them.
Those are the earlier - sev_platform_status(), sev_guest_deactivate(), sev_guest_decomission() and sev_guest_df_flush().
And the 4 functions added in this patch - snp_guest_decomission(), snp_guest_df_flush(), snp_guest_page_reclaim(), and snp_guest_dbg_decrypt().
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
Quoted 1 line · Ashish
There are actually only 8 functions
Only 8?
Lemme ask it differently then: what is the point of the wrappers at all?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
[AMD Official Use Only - General]
Quoted 5 lines
Quoted 1 line
There are actually only 8 functions
Only 8?
Lemme ask it differently then: what is the point of the wrappers at all?
They are basically providing the APIs for the hypervisor to manage a SNP guest.
And this is the original commit for the API to manage the SEV guest:
commit 200664d5237f3f8cd2a2f9f5c5dea08502336bd1
Author: Brijesh Singh <[email protected]> Date: Mon Dec 4 10:57:28 2017 -0600
crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support
AMD's new Secure Encrypted Virtualization (SEV) feature allows the memory contents of virtual machines to be transparently encrypted with a key unique to the VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP) which exposes the commands for these tasks. The complete spec is available at:
http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf
Extend the AMD-SP driver to provide the following support:
- an in-kernel API to communicate with the SEV firmware. The API can be used by the hypervisor to create encryption context for a SEV guest.
- a userspace IOCTL to manage the platform certificates.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
Quoted 2 lines · Ashish
They are basically providing the APIs for the hypervisor to manage a SNP guest.
Yes, I know. But that is not my question. Lemme try again.
My previous comment was:
"I think you should simply export sev_do_cmd() and call it instead."
In this case, the API is a single function - sev_do_cmd() - which the hypervisor calls.
So my question still stands: why is it better to have silly wrappers of sev_do_cmd() instead of having the hypervisor call sev_do_cmd() directly?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
Quoted 9 lines · Borislav Petkov
Quoted 2 lines · Ashish
They are basically providing the APIs for the hypervisor to manage a SNP guest.
Yes, I know. But that is not my question. Lemme try again.
My previous comment was:
Quoted 8 lines
"I think you should simply export
sev_do_cmd()and call it instead."In this case, the API is a single function -
sev_do_cmd()- which the hypervisor calls.So my question still stands: why is it better to have silly wrappers of
sev_do_cmd()instead of having the hypervisor callsev_do_cmd()directly?
We already have sev_issue_cmd_external_user() exported right?
Another option could be to make these wrappers more helpful and less silly. For example callers need to know the PSP command format right now, see sev_guest_decommission().
int sev_guest_decommission(struct sev_data_decommission *data, int *error)
Instead of taking @data this function could just take inputs to create sev_data_decommission:
int sev_guest_decommission(u32 handle, int *error)
Quoted 6 lines
--
Regards/Gruss,
Boris.
Borislav Petkov
maintainerRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
Quoted 4 lines · Peter Gonda
We already have
sev_issue_cmd_external_user()exported right?Another option could be to make these wrappers more helpful and less silly.
For example.
My point is, whenever something needs to issue a SEV* fw command, something adds a silly wrapper and this will become unwieldy pretty quickly.
If a wrapper is not a dumb one but it actually does preparatory work before issuing the command so that the caller's life is made easy, then yes, by all means.
If it is only plain forwarding a call to sev_do_cmd(), then I question the whole point of the exercise...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRE: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
[AMD Official Use Only - General]
Quoted 5 lines
Quoted 4 lines
We already have
sev_issue_cmd_external_user()exported right?Another option could be to make these wrappers more helpful and less silly.
Quoted 7 lines
For example.
My point is, whenever something needs to issue a SEV* fw command, something adds a silly wrapper and this will become unwieldy pretty quickly.
If a wrapper is not a dumb one but it actually does preparatory work before issuing the command so that the caller's life is made easy, then yes, by all means.
If it is only plain forwarding a call to
sev_do_cmd(), then I question the whole point of the exercise...
Well, these all were added as APIs to serve as a abstraction for SEV/SNP guest, and probably it is nice to have an abstracted interface, but I have no issues with replacing these simply with calls to sev_do_cmd().
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 13/49] crypto:ccp: Provide APIs to issue SEV-SNP commands
Quoted 1 line · Ashish
probably it is nice to have an abstracted interface,
Why is it "probably nice" to have an abstracted interface?
Is the hypervisor allowed to issue only a subset of the commands?
Do you want to control the arguments the hypervisor is supposed to send down to the firmware?
There must be a reason why one would do an abstracted interface. Not just because and probably.
Because from where I'm standing this looks like adding a bunch of random wrappers without any logic to it.
So, if you wanna have an interface, you should think this through and design it properly and explain why it is there and how it is supposed to be used.
Don't get me wrong - a properly designed interface to control what the HV issues to the firmware is not a bad idea. But it needs to be properly designed.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteAshish Kalra
maintainerPATCH v6 14/49 crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details.
Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satify new requirements for the SNP. Continue allocating a 1mb region for !SNP configuration.
While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details.
--- drivers/crypto/ccp/sev-dev.c | 173 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 11 +++ 2 files changed, 178 insertions(+), 6 deletions(-)
drivers/crypto/ccp/sev-dev.c +167 -6
@@ -79,6 +79,14 @@ static void *sev_es_tmr;#define NV_LENGTH (32 * 1024)static void *sev_init_ex_buffer; +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size =SEV_ES_TMR_SIZE ; + +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret); +static int sev_do_cmd (int cmd,void *data,int *psp_ret); +static inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;
@@ -177,11 +185,161 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +static void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn); + pfn++; + } +} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) { +memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n); +return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +} + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim) +{ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */ +int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +} + +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) +{ +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; +if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +return NULL ; + +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) +{ +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; +} +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); + +static void __snp_free_firmware_pages (struct page *page,int order,bool locked) +{ +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ; + +__free_pages (page, order); +} + +void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ; + +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL (snp_free_firmware_page); +static void *sev_fw_alloc (unsigned long len) {struct page *page; - page =alloc_pages (GFP_KERNEL ,get_order (len)); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (len), false);if (!page)return NULL ;
@@ -393,7 +551,7 @@ static int __sev_init_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT , &data, error);
@@ -421,7 +579,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT_EX , &data, error);
@@ -818,6 +976,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_inited = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + sev_es_tmr_size =SEV_SNP_ES_TMR_SIZE ; +return rc; }
@@ -1341,8 +1501,9 @@ static void sev_firmware_shutdown(struct sev_device *sev)/* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus (); -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + false); sev_es_tmr =NULL ; }
@@ -1430,7 +1591,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);if (!sev_es_tmr)dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" );
include/linux/psp-sev.h +11 -0
@@ -12,6 +12,8 @@#ifndef __PSP_SEV_H__#define __PSP_SEV_H__ +#include <linux/sev.h> +#include <uapi/linux/psp-sev.h> #ifdef CONFIG_X86
@@ -940,6 +942,8 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error);int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len); +void *snp_alloc_firmware_page (gfp_t mask); +void snp_free_firmware_page (void *addr);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */
@@ -981,6 +985,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erroreturn -ENODEV ; } +static inline void *snp_alloc_firmware_page (gfp_t mask) +{ +return NULL ; +} + +static inline void snp_free_firmware_page (void *addr) { } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 9 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details.
Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satify new requirements for the SNP. Continue
satisfy
Quoted 19 lines
allocating a 1mb region for !SNP configuration.
While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 173 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 11 +++ 2 files changed, 178 insertions(+), 6 deletions(-)drivers/crypto/ccp/sev-dev.c +0
-0
drivers/crypto/ccp/sev-dev.c static void *sev_es_tmr; · line 85
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +4
-0@@ -79,6 +79,14 @@ static void *sev_es_tmr;#define NV_LENGTH (32 * 1024)static void *sev_init_ex_buffer; +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size =SEV_ES_TMR_SIZE ;
Why not keep all this TMR stuff together near the SEV_ES_TMR_SIZE define?
Quoted 17 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret); +static int sev_do_cmd (int cmd,void *data,int *psp_ret); +static inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;@@ -177,11 +185,161 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +static void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn);
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 203
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ pfn++; + } +} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) {
What about setting |n| here too, also the other increments.
for (i = 0, n = 0; i < npages; i++, n++, pfn++)
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 210
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +7
-0+memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err);
Can we change sev_cmd_mutex to some sort of nesting lock type? That could clean up this if (locked) code.
Quoted 19 lines
drivers/crypto/ccp/sev-dev.c +19
-0+if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n);
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 239
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +} + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)
This function can do a lot and when I read the call sites its hard to see what its doing since we have a combination of arguments which tell us what behavior is happening, some of which are not valid (ex: to_fw == true and need_reclaim == true is an invalid argument combination). Also this for loop over |npages| is duplicated from snp_reclaim_pages(). One improvement here is that on the current snp_reclaim_pages() if we fail to reclaim a page we assume we cannot reclaim the next pages, this may cause us to snp_leak_pages() more pages than we actually need too.
What about something like this?
static snp_leak_page(u64 pfn, enum pg_level level) {
memory_failure(pfn, 0); dump_rmpentry(pfn);
}
static int snp_reclaim_page(u64 pfn, enum pg_level level) {
int ret;
struct sev_data_snp_page_reclaim data;
ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err);
if (ret)
goto cleanup;
ret = rmp_make_shared(pfn, level);
if (ret)
goto cleanup;return 0;
cleanup:
snp_leak_page(pfn, level)
}
typedef int (*rmp_state_change_func) (u64 pfn, enum pg_level level);
static int snp_set_rmp_state(unsigned long paddr, unsigned int npages, rmp_state_change_func state_change, rmp_state_change_func cleanup) {
struct sev_data_snp_page_reclaim data;
int ret, err, i, n = 0;
for (i = 0, n = 0; i < npages; i++, n++, pfn++) {
ret = state_change(pfn, PG_LEVEL_4K)
if (ret)
goto cleanup;
}return 0;
cleanup:
for (; i>= 0; i--, n--, pfn--) {
cleanup(pfn, PG_LEVEL_4K);}
return ret;
}
Then inside of __snp_alloc_firmware_pages():
snp_set_rmp_state(paddr, npages, rmp_make_firmware, snp_reclaim_page);
And inside of __snp_free_firmware_pages():
snp_set_rmp_state(paddr, npages, snp_reclaim_page, snp_leak_page);
Just a suggestion feel free to ignore. The readability comment could be addressed much less invasively by just making separate functions for each valid combination of arguments here. Like snp_set_rmp_fw_state(), snp_set_rmp_shared_state(), snp_set_rmp_release_state() or something.
Quoted 51 lines
drivers/crypto/ccp/sev-dev.c +51
-0+{ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */ +int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +} + +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) +{ +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page)
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 300
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; +if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +return NULL ;
So what about the case where snp_set_rmp_state() fails but we were able to reclaim all the pages? Should we be able to signal that to callers so that we could free |page| here? But given this is an error path already maybe we can optimize this in a follow up series.
Quoted 14 lines
drivers/crypto/ccp/sev-dev.c +14
-0+ +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) +{ +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; +} +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); +
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 324
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+static void __snp_free_firmware_pages (struct page *page,int order,bool locked) +{ +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ;
Here we may be able to free some of |page| depending how where inside of snp_set_rmp_state() we failed. But again given this is an error path already maybe we can optimize this in a follow up series.
Quoted 109 lines
drivers/crypto/ccp/sev-dev.c +22
-6+ +__free_pages (page, order); +} + +void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ; + +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL (snp_free_firmware_page); +static void *sev_fw_alloc (unsigned long len) {struct page *page; - page =alloc_pages (GFP_KERNEL ,get_order (len)); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (len), false);if (!page)return NULL ;@@ -393,7 +551,7 @@ static int __sev_init_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT , &data, error);@@ -421,7 +579,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT_EX , &data, error);@@ -818,6 +976,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_inited = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + sev_es_tmr_size =SEV_SNP_ES_TMR_SIZE ; +return rc; }@@ -1341,8 +1501,9 @@ static void sev_firmware_shutdown(struct sev_device *sev)/* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus (); -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + false); sev_es_tmr =NULL ; }@@ -1430,7 +1591,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);if (!sev_es_tmr)dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" );include/linux/psp-sev.h +11
-1@@ -12,6 +12,8 @@#ifndef __PSP_SEV_H__#define __PSP_SEV_H__ +#include <linux/sev.h> +#include <uapi/linux/psp-sev.h> #ifdef CONFIG_X86 @@ -940,6 +942,8 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error);int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len); +void *snp_alloc_firmware_page (gfp_t mask); +void snp_free_firmware_page (void *addr);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -981,6 +985,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erroreturn -ENODEV ; } +static inline void *snp_alloc_firmware_page (gfp_t mask) +{ +return NULL ; +} + +static inline void snp_free_firmware_page (void *addr) { } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ --2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
[Public]
Hello Peter,
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 203
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+static int snp_reclaim_pages (unsigned long pfn,unsigned int npages, +bool locked) { +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) {What about setting |n| here too, also the other increments.
for (i = 0, n = 0; i < npages; i++, n++, pfn++)
Yes that is simpler.
Quoted 17 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd)
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , + &data, &err);Can we change
sev_cmd_mutexto some sort of nesting lock type? That could clean up this if (locked) code.drivers/crypto/ccp/sev-dev.c +5
-0+static inline int rmp_make_firmware (unsigned long pfn,int level) { +return rmp_make_private (pfn,0 , level,0 , true); } + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)
Quoted 1 line
This function can do a lot and when I read the call sites its hard to see what its doing since we have a combination of arguments which tell us what behavior is happening, some of which are not valid (ex:
to_fw== true andneed_reclaim== true is an >invalid argument combination).
to_fw is used to make a firmware page and need_reclaim is for freeing the firmware page, so they are going to be mutually exclusive.
I actually can connect with it quite logically with the callers :snp_alloc_firmware_pages will call with to_fw = true and need_reclaim = false and snp_free_firmware_pages will do the opposite, to_fw = false and need_reclaim = true.
That seems straightforward to look at.
Quoted 2 lines
Also this for loop over |npages| is duplicated from
snp_reclaim_pages(). One improvement here is that on the currentsnp_reclaim_pages()if we fail to reclaim a page we assume we cannot reclaim the next pages, this may cause us tosnp_leak_pages()more pages than we actually need too.
Yes that is true.
Quoted 73 lines
What about something like this?
static
snp_leak_page(u64 pfn, enum pg_level level){
memory_failure(pfn, 0);dump_rmpentry(pfn);
}static int
snp_reclaim_page(u64 pfn, enum pg_level level){
int ret;
structsev_data_snp_page_reclaimdata;ret =
sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); if (ret)
goto cleanup;ret =
rmp_make_shared(pfn, level);
if (ret)
goto cleanup;return 0;
cleanup:
snp_leak_page(pfn, level)
}typedef int (
*rmp_state_change_func) (u64 pfn, enumpg_levellevel);static int snp_set_rmp_state(unsigned long paddr, unsigned int npages,
rmp_state_change_funcstate_change,rmp_state_change_funccleanup) {
structsev_data_snp_page_reclaimdata;
int ret, err, i, n = 0;for (i = 0, n = 0; i < npages; i++, n++, pfn++) {
ret =state_change(pfn, PG_LEVEL_4K)
if (ret)goto cleanup;}
return 0;
cleanup:
for (; i>= 0; i--, n--, pfn--) { cleanup(pfn, PG_LEVEL_4K);}
return ret;
}Then inside of
__snp_alloc_firmware_pages():
snp_set_rmp_state(paddr, npages, rmp_make_firmware, snp_reclaim_page);And inside of
__snp_free_firmware_pages():
snp_set_rmp_state(paddr, npages, snp_reclaim_page, snp_leak_page);Just a suggestion feel free to ignore. The readability comment could be addressed much less invasively by just making separate functions for each valid combination of arguments here. Like
snp_set_rmp_fw_state(),snp_set_rmp_shared_state(),snp_set_rmp_release_state()or something.Quoted 15 lines
drivers/crypto/ccp/sev-dev.c +15
-0+static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int +order,bool locked) { +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data;
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 300
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +return NULL ;So what about the case where
snp_set_rmp_state()fails but we were able to reclaim all the pages? Should we be able to signal that to callers so that we could free |page| here? But given this is an error path already maybe we can optimize this in a >follow up series.
Yes, we should actually tie in to snp_reclaim_pages() success or failure here in the case we were able to successfully unroll some or all of the firmware state change.
Quoted 23 lines
drivers/crypto/ccp/sev-dev.c +22
-0+ +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) { +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; } +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); + +static void __snp_free_firmware_pages (struct page *page,int order, +bool locked) { +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ;
Quoted 1 line
Here we may be able to free some of |page| depending how where inside of
snp_set_rmp_state()we failed. But again given this is an error path already maybe we can optimize this in a follow up series.
Yes, we probably should be able to free some of the page(s) depending on how many page(s) got reclaimed in snp_set_rmp_state().
But these reclamation failures may not be very common, so any failure is indicative of a bigger issue, it might be the case when there is a single page reclamation error it might happen with all the subsequent pages and so follow a simple recovery procedure, then handling a more complex recovery for a chunk of pages being reclaimed and another chunk not.
Thanks,
Ashish
Re: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 37 lines · Ashish
[Public]
Hello Peter,
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 203
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+static int snp_reclaim_pages (unsigned long pfn,unsigned int npages, +bool locked) { +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) {What about setting |n| here too, also the other increments.
for (i = 0, n = 0; i < npages; i++, n++, pfn++)
Yes that is simpler.
Quoted 17 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd)
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , + &data, &err);Can we change
sev_cmd_mutexto some sort of nesting lock type? That could clean up this if (locked) code.drivers/crypto/ccp/sev-dev.c +5
-0+static inline int rmp_make_firmware (unsigned long pfn,int level) { +return rmp_make_private (pfn,0 , level,0 , true); } + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)Quoted 1 line
This function can do a lot and when I read the call sites its hard to see what its doing since we have a combination of arguments which tell us what behavior is happening, some of which are not valid (ex:
to_fw== true andneed_reclaim== true is an >invalid argument combination).
Quoted 7 lines
to_fwis used to make a firmware page andneed_reclaimis for freeing the firmware page, so they are going to be mutually exclusive.I actually can connect with it quite logically with the callers :
snp_alloc_firmware_pageswill call withto_fw= true andneed_reclaim= false andsnp_free_firmware_pageswill do the opposite,to_fw= false andneed_reclaim= true.That seems straightforward to look at.
This might be a preference thing but I find it not straightforward. When I am reading through unmap_firmware_writeable() and I see
/* Transition the pre-allocated buffer to the firmware state. */ if (snp_set_rmp_state(__pa(map->host), npages, true, true, false))
return -EFAULT;
I don't actually know what snp_set_rmp_state() is doing unless I go look at the definition and see what all those booleans mean. This is unlike the rmp_make_shared() and rmp_make_private() functions, each of which tells me a lot more about what the function will do just from the name.
Quoted 124 lines
Quoted 3 lines
Also this for loop over |npages| is duplicated from
snp_reclaim_pages(). One improvement here is that on the currentsnp_reclaim_pages()if we fail to reclaim a page we assume we cannot reclaim the next pages, this may cause us tosnp_leak_pages()more pages than we actually need too.Yes that is true.
Quoted 73 lines
What about something like this?
static
snp_leak_page(u64 pfn, enum pg_level level){
memory_failure(pfn, 0);dump_rmpentry(pfn);
}static int
snp_reclaim_page(u64 pfn, enum pg_level level){
int ret;
structsev_data_snp_page_reclaimdata;ret =
sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); if (ret)
goto cleanup;ret =
rmp_make_shared(pfn, level);
if (ret)
goto cleanup;return 0;
cleanup:
snp_leak_page(pfn, level)
}typedef int (
*rmp_state_change_func) (u64 pfn, enumpg_levellevel);static int snp_set_rmp_state(unsigned long paddr, unsigned int npages,
rmp_state_change_funcstate_change,rmp_state_change_funccleanup) {
structsev_data_snp_page_reclaimdata;
int ret, err, i, n = 0;for (i = 0, n = 0; i < npages; i++, n++, pfn++) {
ret =state_change(pfn, PG_LEVEL_4K)
if (ret)goto cleanup;}
return 0;
cleanup:
for (; i>= 0; i--, n--, pfn--) { cleanup(pfn, PG_LEVEL_4K);}
return ret;
}Then inside of
__snp_alloc_firmware_pages():
snp_set_rmp_state(paddr, npages, rmp_make_firmware, snp_reclaim_page);And inside of
__snp_free_firmware_pages():
snp_set_rmp_state(paddr, npages, snp_reclaim_page, snp_leak_page);Just a suggestion feel free to ignore. The readability comment could be addressed much less invasively by just making separate functions for each valid combination of arguments here. Like
snp_set_rmp_fw_state(),snp_set_rmp_shared_state(),snp_set_rmp_release_state()or something.Quoted 15 lines
drivers/crypto/ccp/sev-dev.c +15
-0+static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int +order,bool locked) { +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data;Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 300
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +return NULL ;So what about the case where
snp_set_rmp_state()fails but we were able to reclaim all the pages? Should we be able to signal that to callers so that we could free |page| here? But given this is an error path already maybe we can optimize this in a >follow up series.Yes, we should actually tie in to
snp_reclaim_pages()success or failure here in the case we were able to successfully unroll some or all of the firmware state change.Quoted 23 lines
drivers/crypto/ccp/sev-dev.c +22
-0+ +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) { +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; } +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); + +static void __snp_free_firmware_pages (struct page *page,int order, +bool locked) { +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ;Quoted 1 line
Here we may be able to free some of |page| depending how where inside of
snp_set_rmp_state()we failed. But again given this is an error path already maybe we can optimize this in a follow up series.Yes, we probably should be able to free some of the page(s) depending on how many page(s) got reclaimed in
snp_set_rmp_state().
But these reclamation failures may not be very common, so any failure is indicative of a bigger issue, it might be the case when there is a single page reclamation error it might happen with all the subsequent pages and so follow a simple recovery procedure, then handling a more complex recovery for a chunk of pages being reclaimed and another chunk not.Thanks,
Ashish
Re: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Kalra, AshishQuoted 156 lines · Ashish
[Public]
Hello Peter,
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 203
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+static int snp_reclaim_pages (unsigned long pfn,unsigned int npages, +bool locked) { +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) {What about setting |n| here too, also the other increments.
for (i = 0, n = 0; i < npages; i++, n++, pfn++)
Yes that is simpler.
Quoted 17 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd)
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , + &data, &err);Can we change
sev_cmd_mutexto some sort of nesting lock type? That could clean up this if (locked) code.drivers/crypto/ccp/sev-dev.c +5
-0+static inline int rmp_make_firmware (unsigned long pfn,int level) { +return rmp_make_private (pfn,0 , level,0 , true); } + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)Quoted 1 line
This function can do a lot and when I read the call sites its hard to see what its doing since we have a combination of arguments which tell us what behavior is happening, some of which are not valid (ex:
to_fw== true andneed_reclaim== true is an >invalid argument combination).
to_fwis used to make a firmware page andneed_reclaimis for freeing the firmware page, so they are going to be mutually exclusive.I actually can connect with it quite logically with the callers :
snp_alloc_firmware_pageswill call withto_fw= true andneed_reclaim= false andsnp_free_firmware_pageswill do the opposite,to_fw= false andneed_reclaim= true.That seems straightforward to look at.
Quoted 2 lines
Also this for loop over |npages| is duplicated from
snp_reclaim_pages(). One improvement here is that on the currentsnp_reclaim_pages()if we fail to reclaim a page we assume we cannot reclaim the next pages, this may cause us tosnp_leak_pages()more pages than we actually need too.Yes that is true.
Quoted 73 lines
What about something like this?
static
snp_leak_page(u64 pfn, enum pg_level level){
memory_failure(pfn, 0);dump_rmpentry(pfn);
}static int
snp_reclaim_page(u64 pfn, enum pg_level level){
int ret;
structsev_data_snp_page_reclaimdata;ret =
sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); if (ret)
goto cleanup;ret =
rmp_make_shared(pfn, level);
if (ret)
goto cleanup;return 0;
cleanup:
snp_leak_page(pfn, level)
}typedef int (
*rmp_state_change_func) (u64 pfn, enumpg_levellevel);static int snp_set_rmp_state(unsigned long paddr, unsigned int npages,
rmp_state_change_funcstate_change,rmp_state_change_funccleanup) {
structsev_data_snp_page_reclaimdata;
int ret, err, i, n = 0;for (i = 0, n = 0; i < npages; i++, n++, pfn++) {
ret =state_change(pfn, PG_LEVEL_4K)
if (ret)goto cleanup;}
return 0;
cleanup:
for (; i>= 0; i--, n--, pfn--) { cleanup(pfn, PG_LEVEL_4K);}
return ret;
}Then inside of
__snp_alloc_firmware_pages():
snp_set_rmp_state(paddr, npages, rmp_make_firmware, snp_reclaim_page);And inside of
__snp_free_firmware_pages():
snp_set_rmp_state(paddr, npages, snp_reclaim_page, snp_leak_page);Just a suggestion feel free to ignore. The readability comment could be addressed much less invasively by just making separate functions for each valid combination of arguments here. Like
snp_set_rmp_fw_state(),snp_set_rmp_shared_state(),snp_set_rmp_release_state()or something.Quoted 15 lines
drivers/crypto/ccp/sev-dev.c +15
-0+static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int +order,bool locked) { +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data;Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 300
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false)) +return NULL ;So what about the case where
snp_set_rmp_state()fails but we were able to reclaim all the pages? Should we be able to signal that to callers so that we could free |page| here? But given this is an error path already maybe we can optimize this in a >follow up series.Yes, we should actually tie in to
snp_reclaim_pages()success or failure here in the case we were able to successfully unroll some or all of the firmware state change.Quoted 22 lines
drivers/crypto/ccp/sev-dev.c +22
-0+ +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) { +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false); + +return page ?page_address (page) :NULL ; } +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page); + +static void __snp_free_firmware_pages (struct page *page,int order, +bool locked) { +unsigned long paddr, npages =1ul << order; + +if (!page) +return ; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ;
Quoted 5 lines
Quoted 1 line
Here we may be able to free some of |page| depending how where inside of
snp_set_rmp_state()we failed. But again given this is an error path already maybe we can optimize this in a follow up series.Yes, we probably should be able to free some of the page(s) depending on how many page(s) got reclaimed in
snp_set_rmp_state().
But these reclamation failures may not be very common, so any failure is indicative of a bigger issue, it might be the case when there is a single page reclamation error it might happen with all the subsequent pages and so follow a simple recovery procedure, then handling a more complex recovery for a chunk of pages being reclaimed and another chunk not.
Silent ignore is stil a bad idea. I.e. at minimum would make sense to print a warning to klog.
Quoted 3 lines
Thanks,
Ashish
BR, Jarkko
Re: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Ashish KalraQuoted 157 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details.
Allocate the Trusted Memory Region (TMR) as a 2mb sized/aligned region when SNP is enabled to satify new requirements for the SNP. Continue allocating a 1mb region for !SNP configuration.
While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details.
Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 173 +++++++++++++++++++++++++++++++++-- include/linux/psp-sev.h | 11 +++ 2 files changed, 178 insertions(+), 6 deletions(-)drivers/crypto/ccp/sev-dev.c +120
-0@@ -79,6 +79,14 @@ static void *sev_es_tmr;#define NV_LENGTH (32 * 1024)static void *sev_init_ex_buffer; +/* When SEV-SNP is enabled the TMR needs to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size =SEV_ES_TMR_SIZE ; + +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret); +static int sev_do_cmd (int cmd,void *data,int *psp_ret); +static inline bool sev_version_greater_or_equal (u8 maj,u8 min) {struct sev_device *sev = psp_master->sev_data;@@ -177,11 +185,161 @@ static int sev_cmd_buffer_len(int cmd)return 0 ; } +static void snp_leak_pages (unsigned long pfn,unsigned int npages) +{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn); + pfn++; + } +} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) { +memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ; + +if (locked) + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); +if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n); +return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +} + +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim) +{ +unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */ +int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +} + +static struct page *__snp_alloc_firmware_pages (gfp_t gfp_mask,int order,bool locked) +{ +unsigned long npages =1ul << order, paddr; +struct sev_device *sev; +struct page *page; + +if (!psp_master || !psp_master->sev_data) +return NULL ; + + page =alloc_pages (gfp_mask, order); +if (!page) +return NULL ; + +/* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp_master->sev_data; +if (!sev->snp_inited) +return page; + + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, true, locked, false))
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 309
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+return NULL ; + +return page; +} + +void *snp_alloc_firmware_page (gfp_t gfp_mask) +{ +struct page *page; + + page =__snp_alloc_firmware_pages (gfp_mask,0 , false);
Could be just
struct page *page == __snp_alloc_firmware_pages(gfp_mask, 0, false);
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 313
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+ +return page ?page_address (page) :NULL ; +} +EXPORT_SYMBOL_GPL (snp_alloc_firmware_page);
Undocumented API
Why don't you just export __snp_alloc_firmware_pages() and declare these trivial wrappers as "static inline" inside psp-sev.h?
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 320
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +7
-0+ +static void __snp_free_firmware_pages (struct page *page,int order,bool locked) +{ +unsigned long paddr, npages =1ul << order; + +if (!page) +return ;
Silently ignored NULL pointer.
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 332
Quoted 12 lines
drivers/crypto/ccp/sev-dev.c +12
-0+ + paddr =__pa ((unsigned long )page_address (page)); +if (snp_set_rmp_state (paddr, npages, false, locked, true)) +return ; + +__free_pages (page, order); +} + +void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ;
Why silently ignore a NULL pointer? At minimum, pr_warn() would be appropriate.
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 336
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+ +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL (snp_free_firmware_page);
Ditto, same comments as for allocation part.
Quoted 97 lines
drivers/crypto/ccp/sev-dev.c +10
-6+static void *sev_fw_alloc (unsigned long len) {struct page *page; - page =alloc_pages (GFP_KERNEL ,get_order (len)); + page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (len), false);if (!page)return NULL ;@@ -393,7 +551,7 @@ static int __sev_init_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT , &data, error);@@ -421,7 +579,7 @@ static int __sev_init_ex_locked(int *error) data.tmr_address =__pa (sev_es_tmr); data.flags |=SEV_INIT_FLAGS_SEV_ES ; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; }return __sev_do_cmd_locked (SEV_CMD_INIT_EX , &data, error);@@ -818,6 +976,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_inited = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized\n" ); + sev_es_tmr_size =SEV_SNP_ES_TMR_SIZE ; +return rc; }@@ -1341,8 +1501,9 @@ static void sev_firmware_shutdown(struct sev_device *sev)/* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus (); -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); +__snp_free_firmware_pages (virt_to_page (sev_es_tmr), +get_order (sev_es_tmr_size), + false); sev_es_tmr =NULL ; }@@ -1430,7 +1591,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - sev_es_tmr =sev_fw_alloc (SEV_ES_TMR_SIZE ); + sev_es_tmr =sev_fw_alloc (sev_es_tmr_size);if (!sev_es_tmr)dev_warn (sev->dev,"SEV: TMR allocation failed, SEV-ES support unavailable\n" );include/linux/psp-sev.h +11
-0@@ -12,6 +12,8 @@#ifndef __PSP_SEV_H__#define __PSP_SEV_H__ +#include <linux/sev.h> +#include <uapi/linux/psp-sev.h> #ifdef CONFIG_X86 @@ -940,6 +942,8 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error);int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len); +void *snp_alloc_firmware_page (gfp_t mask); +void snp_free_firmware_page (void *addr);#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -981,6 +985,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erroreturn -ENODEV ; } +static inline void *snp_alloc_firmware_page (gfp_t mask) +{ +return NULL ; +} + +static inline void snp_free_firmware_page (void *addr) { } +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ Signature · 1 line
2.25.1
BR, Jarkko
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Ashish KalraQuoted 1 line · Ashish Kalra
+static void
snp_leak_pages(unsigned long pfn, unsigned int npages)
That function name looks wrong.
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 192
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 );
^^^^^^^^^^^^^^^^^^^^^^
Why?
* This function is called by the low level machine check code
* of an architecture when it detects hardware memory corruption
* of a page. It tries its best to recover, which includes
* dropping pages, killing processes etc.
I don't think you wanna do that.
It looks like you want to prevent the page from being used again but not mark it as PG_hwpoison and whatnot. PG_reserved perhaps?
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 205
Quoted 13 lines
drivers/crypto/ccp/sev-dev.c +13
-0+dump_rmpentry (pfn); + pfn++; + } +} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) { +memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ;
Oh wow, this is just silly. A struct for a single u64. Just use a
u64 paddr;
directly. But we had this topic already...
Review
Quoted 2 lines
(quoted diff) +2
-0+ + if (locked)
Ew, that's never a good design - conditional locking.
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 210
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); + else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err);
<---- newline here.
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +9
-0+if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++;
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 229
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n);
So this looks real weird: we go and reclaim pages, we hit an error during reclaiming a page X somewhere in-between and then we go and mark the *remaining* pages as not to be used?!
Why?
Why not only that *one* page which failed and then we continue with the rest?!
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 236
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c +7
-0+return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +}
That's a silly wrapper used only once. Just do at the callsite:
/* Mark this page as belonging to firmware */ rc = rmp_make_private(pfn, 0, level, 0, true);
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 239
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)
Tangential to the above, this is just nuts with those bool arguments. Just look at the callsites: do you understand what they do?
snp_set_rmp_state(paddr, npages, true, locked, false);
what does that do? You need to go up to the definition of the function, count the arguments and see what that "true" arg stands for.
What you should do instead is, have separate helpers which do only one thing:
rmp_mark_pages_firmware(); rmp_mark_pages_shared(); rmp_mark_pages_...
and then have the *callers* issue snp_reclaim_pages() when needed. So you'd have
rmp_mark_pages_firmware(); rmp_mark_pages_shared()
and __snp_free_firmware_pages() would do
rmp_mark_pages_shared(); snp_reclaim_pages();
and so on.
And then if you need locking, the callers can decide which sev_do_cmd variant to issue.
And then if you have common code fragments which you can unify into a bigger helper function, *then* you can do that.
Instead of multiplexing it this way. Which makes it really hard to follow what the code does.
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 241
Quoted 1 line
drivers/crypto/ccp/sev-dev.c +1
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */
No side comments pls.
Quoted 37 lines
drivers/crypto/ccp/sev-dev.c +37
-0+int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +}
...
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 336
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ; + +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL (snp_free_firmware_page);
EXPORT_SYMBOL_GPL() ofc.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 1 line · Ashish Kalra
+static void
snp_leak_pages(unsigned long pfn, unsigned int npages)That function name looks wrong.
Quoted 7 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 192
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+{ +WARN (1 ,"psc failed, pfn 0x%lx pages %d (leaking)\n" , pfn, npages); +while (npages--) { +memory_failure (pfn,0 );^^^^^^^^^^^^^^^^^^^^^^Why?
The page was in FW state and we couldn't transition it back to HV/Shared state, any access to this page can cause RMP #PF.
Quoted 10 lines
* This function is called by the low level machine check code
* of an architecture when it detects hardware memory corruption
* of a page. It tries its best to recover, which includes
* dropping pages, killing processes etc.I don't think you wanna do that.
It looks like you want to prevent the page from being used again but not mark it as
PG_hwpoisonand whatnot.PG_reservedperhaps?
* PG_reserved is set for special pages. The "struct page" of such a
* page should in general not be touched (e.g. set dirty) except by its
* owner.
If it is "still" accessed/touched then it can cause RMP #PF. On the other hand,
* PG_hwpoison... Accessing is
* not safe since it may cause another machine check. Don't touch!
That sounds exactly the state we want these page(s) to be in ?
Another possibility is PG_error.
Quoted 17 lines
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +3
-0+dump_rmpentry (pfn); + pfn++; + }drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 205
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+} + +static int snp_reclaim_pages (unsigned long pfn,unsigned int npages,bool locked) +{ +struct sev_data_snp_page_reclaim data; +int ret, err, i, n =0 ; + +for (i =0 ; i < npages; i++) { +memset (&data,0 ,sizeof (data)); + data.paddr = pfn <<PAGE_SHIFT ;Oh wow, this is just silly. A struct for a single u64. Just use a
Quoted 1 line
u64 paddr;
Ok.
Quoted 7 lines
directly. But we had this topic already...
Review
Quoted 2 lines
(quoted diff) +2
-0+ + if (locked)Ew, that's never a good design - conditional locking.
There is a previous suggestion to change sev_cmd_mutex to some sort of nesting lock type to clean up this if (locked) code, though AFAIK, there is no support for nesting lock types.
Quoted 31 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 210
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ ret =__sev_do_cmd_locked (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err); + else + ret =sev_do_cmd (SEV_CMD_SNP_PAGE_RECLAIM , &data, &err);<---- newline here.
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +9
-0+if (ret) + goto cleanup; + + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + goto cleanup; + + pfn++; + n++;drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 229
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ } + +return 0 ; + +cleanup: +/* +* If failed to reclaim the page then page is no longer safe to +* be released, leak it. +*/ +snp_leak_pages (pfn, npages - n);So this looks real weird: we go and reclaim pages, we hit an error during reclaiming a page X somewhere in-between and then we go and mark the *remaining* pages as not to be used?!
Quoted 4 lines
Why?
Why not only that *one* page which failed and then we continue with the rest?!
I agree and will change to this approach.
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 236
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +7
-0+return ret; +} + +static inline int rmp_make_firmware (unsigned long pfn,int level) +{ +return rmp_make_private (pfn,0 , level,0 , true); +}That's a silly wrapper used only once. Just do at the callsite:
Quoted 3 lines
/* Mark this page as belonging to firmware */ rc = rmp_make_private(pfn, 0, level, 0, true);
Ok.
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 239
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+ +static int snp_set_rmp_state (unsigned long paddr,unsigned int npages,bool to_fw,bool locked, +bool need_reclaim)Tangential to the above, this is just nuts with those bool arguments. Just look at the callsites: do you understand what they do?
snp_set_rmp_state(paddr, npages, true, locked, false);what does that do? You need to go up to the definition of the function, count the arguments and see what that "true" arg stands for.
I totally agree, this is simply unreadable.
And this has been mentioned previously too ...
This function can do a lot and when I read the call sites its hard to see what its doing since we have a combination of arguments which tell us what behavior is happening ...
Quoted 10 lines
What you should do instead is, have separate helpers which do only one thing:
rmp_mark_pages_firmware(); rmp_mark_pages_shared(); rmp_mark_pages_...and then have the *callers* issue
snp_reclaim_pages()when needed. So you'd have
Quoted 8 lines
rmp_mark_pages_firmware(); rmp_mark_pages_shared()and
__snp_free_firmware_pages()would dormp_mark_pages_shared(); snp_reclaim_pages();
Actually, this only needs to call snp_reclaim_pages().
Quoted 11 lines
and so on.
And then if you need locking, the callers can decide which
sev_do_cmdvariant to issue.And then if you have common code fragments which you can unify into a bigger helper function, *then* you can do that.
Instead of multiplexing it this way. Which makes it really hard to follow what the code does.
Sure i will do this cleanup.
Quoted 45 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 241
Quoted 2 lines
drivers/crypto/ccp/sev-dev.c +1
-0+unsigned long pfn =__sme_clr (paddr) >>PAGE_SHIFT ;/* Cbit maybe set in the paddr */ No side comments pls.
Quoted 37 lines
drivers/crypto/ccp/sev-dev.c +37
-0+int rc, n =0 , i; + +for (i =0 ; i < npages; i++) { +if (to_fw) + rc =rmp_make_firmware (pfn,PG_LEVEL_4K ); +else + rc = need_reclaim ?snp_reclaim_pages (pfn,1 , locked) : +rmp_make_shared (pfn,PG_LEVEL_4K ); +if (rc) + goto cleanup; + + pfn++; + n++; + } + +return 0 ; + +cleanup: +/* Try unrolling the firmware state changes */ +if (to_fw) { +/* +* Reclaim the pages which were already changed to the +* firmware state. +*/ +snp_reclaim_pages (paddr >>PAGE_SHIFT , n, locked); + +return rc; + } + +/* +* If failed to change the page state to shared, then its not safe +* to release the page back to the system, leak it. +*/ +snp_leak_pages (pfn, npages - n); + +return rc; +}...
Quoted 11 lines
drivers/crypto/ccp/sev-dev.c static int sev_cmd_buffer_len(int cmd) · line 336
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +8
-0+void snp_free_firmware_page (void *addr) +{ +if (!addr) +return ; + +__snp_free_firmware_pages (virt_to_page (addr),0 , false); +} +EXPORT_SYMBOL (snp_free_firmware_page);
EXPORT_SYMBOL_GPL()ofc.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 9 lines · Ashish
If it is "still" accessed/touched then it can cause RMP #PF. On the other hand,
*
PG_hwpoison... Accessing is
* not safe since it may cause another machine check. Don't touch!That sounds exactly the state we want these page(s) to be in ?
Another possibility is
PG_error.
Something like this:
include/linux/page-flags.h +1 -0
@@ -186,6 +186,7 @@ enum pageflags {* THP. */ PG_has_hwpoisoned = PG_error, + PG_offlimits = PG_hwpoison,#endif /* non-lru isolated movable page */
and SNP will have to depend on CONFIG_MEMORY_FAILURE.
But I'd let mm folks correct me here on the details.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Just to add here, writing to any of these pages from the Host will trigger a RMP #PF which will cause the RMP page fault handler to send a SIGBUS to the current process, as this page is not owned by Host.
So calling memory_failure() is proactively doing the same, marking the page as poisoned and probably also killing the current process.
Thanks,
Ashish
Quoted 30 lines · Borislav Petkov
Quoted 9 lines · Ashish
If it is "still" accessed/touched then it can cause RMP #PF. On the other hand,
*
PG_hwpoison... Accessing is
* not safe since it may cause another machine check. Don't touch!That sounds exactly the state we want these page(s) to be in ?
Another possibility is
PG_error.Something like this:
include/linux/page-flags.h +1
-0@@ -186,6 +186,7 @@ enum pageflags {* THP. */ PG_has_hwpoisoned = PG_error, + PG_offlimits = PG_hwpoison,#endif /* non-lru isolated movable page */ and SNP will have to depend on
CONFIG_MEMORY_FAILURE.But I'd let mm folks correct me here on the details.
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 4 lines · Ashish
Just to add here, writing to any of these pages from the Host will trigger a RMP #PF which will cause the RMP page fault handler to send a SIGBUS to the current process, as this page is not owned by Host.
And kill the host process?
So this is another "policy" which sounds iffy. If we kill the process, we should at least say why. Are we doing that currently?
Quoted 2 lines
So calling
memory_failure()is proactively doing the same, marking the page as poisoned and probably also killing the current process.
But the page is not suffering a memory failure - it cannot be reclaimed for whatever reason. Btw, how can that reclaim failure ever happen? Any real scenarios?
Anyway, memory failure just happens to fit what you wanna do but you can't just reuse that - that's hacky. What is the problem with writing your own function which does that?
Also, btw, please do not top-post.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Hello Boris,
Quoted 10 lines · Borislav Petkov
Quoted 4 lines · Ashish
Just to add here, writing to any of these pages from the Host will trigger a RMP #PF which will cause the RMP page fault handler to send a SIGBUS to the current process, as this page is not owned by Host.
And kill the host process?
So this is another "policy" which sounds iffy. If we kill the process, we should at least say why. Are we doing that currently?
Yes, pasted below is the latest host RMP #PF handler, with new and additional comments added and there is a relevant comment added here for this behavior:
static int handle_user_rmp_page_fault(struct pt_regs *regs, unsigned long error_code,unsigned long address) {
...
...
/*
* If its a guest private page, then the fault cannot be resolved.
* Send a SIGBUS to terminate the process.
*
* As documented in APM vol3 pseudo-code for RMPUPDATE, when the
* 2M range is covered by a valid (Assigned=1) 2M entry, the middle
* 511 4k entries also have Assigned=1. This means that if there is
* an access to a page which happens to lie within an Assigned 2M
* entry, the 4k RMP entry will also have Assigned=1. Therefore, the
* kernel should see that the page is not a valid page and the fault
* cannot be resolved.
*/
if (snp_lookup_rmpentry(pfn, &rmp_level)) {
do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); return RMP_PF_RETRY;
}
...
...
I believe that we already had an off-list discussion on the same, copying David Kaplan's reply on the same below:
So what I think you want to do is:
1. Compute the pfn for the 4kb page you're trying to access (as your code below does) 2. Read that RMP entry -- If it is assigned then kill the process 3. Otherwise, check the level from the host page table. If level=PG_LEVEL_4K then somebody else may have already smashed this page, so just retry the instruction 4. If level=PG_LEVEL_2M/1G, then the host needs to split their page.
This is the current algorithm being followed by the host RMP #PF handler.
Quoted 7 lines
Quoted 3 lines
So calling
memory_failure()is proactively doing the same, marking the page as poisoned and probably also killing the current process.But the page is not suffering a memory failure - it cannot be reclaimed for whatever reason. Btw, how can that reclaim failure ever happen? Any real scenarios?
The scenarios here are either SNP FW failure (SNP_PAGE_RECLAIM command) in transitioning the page back to HV state and/or RMPUPDATE instruction failure to transition the page back to hypervisor/shared state.
Quoted 5 lines
Anyway, memory failure just happens to fit what you wanna do but you can't just reuse that - that's hacky. What is the problem with writing your own function which does that?
Ok.
Will look at adding our own recovery function for the same, but that will again mark the pages as poisoned, right ?
Still waiting for some/more feedback from mm folks on the same.
Thanks,
Ashish
Quoted 4 lines
Also, btw, please do not top-post.
Thx.
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 3 lines · Ashish
if (snp_lookup_rmpentry(pfn, &rmp_level)) {
do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS);
return RMP_PF_RETRY;Does this issue some halfway understandable error message why the process got killed?
Quoted 2 lines
Will look at adding our own recovery function for the same, but that will again mark the pages as poisoned, right ?
Well, not poisoned but PG_offlimits or whatever the mm folks agree upon. Semantically, it'll be handled the same way, ofc.
Quoted 1 line
Still waiting for some/more feedback from mm folks on the same.
Just send the patch and they'll give it.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Hello Boris,
Quoted 5 lines · Borislav Petkov
Quoted 3 lines · Ashish
if (snp_lookup_rmpentry(pfn, &rmp_level)) { do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); return RMP_PF_RETRY;
Quoted 8 lines
Does this issue some halfway understandable error message why the process got killed?
Quoted 2 lines
Will look at adding our own recovery function for the same, but that will again mark the pages as poisoned, right ?
Well, not poisoned but
PG_offlimitsor whatever the mm folks agree upon. Semantically, it'll be handled the same way, ofc.
Added a new PG_offlimits flag and a simple corresponding handler for it.
But there is still added complexity of handling hugepages as part of reclamation failures (both HugeTLB and transparent hugepages) and that means calling more static functions in mm/memory_failure.c
There is probably a more appropriate handler in mm/memory-failure.c:
soft_offline_page() - this will mark the page as HWPoisoned and also has handling for hugepages. And we can avoid adding a new page flag too.
soft_offline_page - Soft offline a page.
Soft offline a page, by migration or invalidation, without killing anything.
So, this looks like a good option to callsoft_offline_page() instead of memory_failure() in case of
failure to transition the page back to HV/shared state via SNP_RECLAIM_CMD and/or RMPUPDATE instruction.
Thanks,
Ashish
Quoted 7 lines
Quoted 2 lines
Still waiting for some/more feedback from mm folks on the same.
Just send the patch and they'll give it.
Thx.
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 2 lines · Ashish
But there is still added complexity of handling hugepages as part of reclamation failures (both HugeTLB and transparent hugepages) and that
Why?
You want to offline pfns of 4K pages. What hugepages?
Quoted 6 lines
means calling more static functions in
mm/memory_failure.cThere is probably a more appropriate handler in
mm/memory-failure.c:
soft_offline_page()- this will mark the page as HWPoisoned and also has handling for hugepages. And we can avoid adding a new page flag too.
So if some other code wants to dump the amount of all hwpoisoned pages, it'll dump those too.
Don't you see what is wrong with this picture?
And btw, reusing the hwpoison flag
PG_offlimits = PG_hwpoison
like previously suggested doesn't help here either.
IOW, I really don't like this lumping of semantics together. ;-\
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Kalra, AshishCc'ing memory failure folks, the beinning of this subthread is here:
Quoted 11 lines · Ashish
Hello Boris,
Quoted 8 lines · Borislav Petkov
Quoted 3 lines · Ashish
if (
snp_lookup_rmpentry(pfn, &rmp_level)) {do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); returnRMP_PF_RETRY;Does this issue some halfway understandable error message why the process got killed?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Will look at adding our own recovery function for the same, but that will again mark the pages as poisoned, right ?
Well, not poisoned but
PG_offlimitsor whatever the mm folks agree upon. Semantically, it'll be handled the same way, ofc.Added a new
PG_offlimitsflag and a simple corresponding handler for it.
One thing is, there's not enough page flags to be adding more (except aliases for existing) for cases that can avoid it, but as Boris says, if using alias to PG_hwpoison it depends what will become confused with the actual hwpoison.
Quoted 9 lines
But there is still added complexity of handling hugepages as part of reclamation failures (both HugeTLB and transparent hugepages) and that means calling more static functions in
mm/memory_failure.cThere is probably a more appropriate handler in
mm/memory-failure.c:
soft_offline_page()- this will mark the page as HWPoisoned and also has handling for hugepages. And we can avoid adding a new page flag too.
Quoted 7 lines
soft_offline_page- Soft offline a page.
Soft offline a page, by migration or invalidation, without killing anything.So, this looks like a good option to call
soft_offline_page()instead ofmemory_failure()in case of failure to transition the page back to HV/shared state viaSNP_RECLAIM_CMDand/or RMPUPDATE instruction.
So it's a bit unclear to me what exact situation we are handling here. The original patch here seems to me to be just leaking back pages that are unsafe for further use. soft_offline_page() seems to fit that scenario of a graceful leak before something is irrepairably corrupt and we page fault on it. But then in the thread you discus PF handling and killing. So what is the case here? If we detect this need to call snp_leak_pages() does it mean:
a) nobody that could page fault at them (the guest?) is running anymore, we are tearing it down, we just can't reuse the pages further on the host
- seem like soft_offline_page() could work, but maybe we could just put the pages on some leaked lists without special page? The only thing that should matter is not to free the pages to the page allocator so they would be reused by something else.
b) something can stil page fault at them (what?) - AFAIU can't be resolved without killing something, memory_failure() might limit the damage
Quoted 10 lines
Thanks,
AshishQuoted 7 lines
Quoted 2 lines
Still waiting for some/more feedback from mm folks on the same.
Just send the patch and they'll give it.
Thx.
Borislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 3 lines · Vlastimil Babka
but maybe we could just put the pages on some leaked lists without
special page? The only thing that should matter is not to free the pages to the page allocator so they would be reused by something else.
As said on IRC, I like this a *lot*. This perfectly represents what those leaked pages are: leaked, cannot be used and lost. Certainly not hwpoisoned.
Yeah, that's much better.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
And,
as dhansen connected the dots, this should be the exact same protection scenario as UPM:
https://lore.kernel.org/all/[email protected]
so you should be able to mark them inaccessible the same way and you won't need any poisoning dance.
And Michael has patches so you probably should talk to him...
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Hello Boris,
Quoted 9 lines · Borislav Petkov
And,
as dhansen connected the dots, this should be the exact same protection scenario as UPM:
so you should be able to mark them inaccessible the same way and you won't need any poisoning dance.
With UPM, the guest pages are all still freed back to the host after guest shutdown, so it's not clear how this would help with handling of leaked pages, for e.g, the host can still access these pages once the guest is shutdown and it will cause the RMP violation #PF at that point.
Additionally, our use case is of host allocated firmware pages as part of the crypto driver (to be passed to SNP firmware api calls and then re-transitioned back to host state on return) so these are not guest private pages in the true sense and they need to be handled differently in case there is a failure in reclaiming them.
Can you elaborate on what you have in mind ?
Thanks,
Ashish
Quoted 5 lines
And Michael has patches so you probably should talk to him...
Thx.
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 14/49] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Vlastimil BabkaHello Vlastimil,
Quoted 53 lines · Vlastimil Babka
Cc'ing memory failure folks, the beinning of this subthread is here:
Quoted 11 lines · Ashish
Hello Boris,
Quoted 8 lines · Borislav Petkov
Quoted 3 lines · Ashish
if (
snp_lookup_rmpentry(pfn, &rmp_level)) {do_sigbus(regs, error_code, address, VM_FAULT_SIGBUS); returnRMP_PF_RETRY;Does this issue some halfway understandable error message why the process got killed?
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Will look at adding our own recovery function for the same, but that will again mark the pages as poisoned, right ?
Well, not poisoned but
PG_offlimitsor whatever the mm folks agree upon. Semantically, it'll be handled the same way, ofc.Added a new
PG_offlimitsflag and a simple corresponding handler for it.One thing is, there's not enough page flags to be adding more (except aliases for existing) for cases that can avoid it, but as Boris says, if using alias to
PG_hwpoisonit depends what will become confused with the actual hwpoison.Quoted 9 lines
But there is still added complexity of handling hugepages as part of reclamation failures (both HugeTLB and transparent hugepages) and that means calling more static functions in
mm/memory_failure.cThere is probably a more appropriate handler in
mm/memory-failure.c:
soft_offline_page()- this will mark the page as HWPoisoned and also has handling for hugepages. And we can avoid adding a new page flag too.Quoted 7 lines
soft_offline_page- Soft offline a page.
Soft offline a page, by migration or invalidation, without killing anything.So, this looks like a good option to call
soft_offline_page()instead ofmemory_failure()in case of failure to transition the page back to HV/shared state viaSNP_RECLAIM_CMDand/or RMPUPDATE instruction.So it's a bit unclear to me what exact situation we are handling here. The original patch here seems to me to be just leaking back pages that are unsafe for further use.
soft_offline_page()seems to fit that scenario of a graceful leak before something is irrepairably corrupt and we page fault on it. But then in the thread you discus PF handling and killing. So what is the case here? If we detect this need to callsnp_leak_pages()does it mean:
Quoted 2 lines
a) nobody that could page fault at them (the guest?) is running anymore, we are tearing it down, we just can't reuse the pages further on the host
The host can page fault on them, if anything on the host tries to write to these pages. Host reads will return garbage data.
Quoted 7 lines
- seem like
soft_offline_page()could work, but maybe we could just put the pages on some leaked lists without special page? The only thing that should matter is not to free the pages to the page allocator so they would be reused by something else.b) something can stil page fault at them (what?) - AFAIU can't be resolved without killing something,
memory_failure()might limit the damage
As i mentioned above, host writes will cause RMP violation page fault.
Thanks,
Ashish
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Still waiting for some/more feedback from mm folks on the same.
Just send the patch and they'll give it.
Thx.