Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
Subsystems (12)
v5 cover Add AMD Secure Nested Paging (SEV-SNP) Hypervisor Support
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 Part-1.
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.
The series is based on the commit:
SNP part1 commit and
fa7a549d321a (kvm/next, next) KVM: x86: accept userspace interrupt only if no event is injected
TODO:
* Add support for command to ratelimit the guest message request.
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.
Brijesh Singh (40):
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: 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: shutdown SEV firmware on kexec
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: 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
Sean Christopherson (2):
KVM: x86/mmu: Move 'pfn' variable to caller of direct_page_fault()
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 | 55 + .../virt/kvm/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 | 5 + arch/x86/include/asm/kvm_host.h | 20 + arch/x86/include/asm/msr-index.h | 6 + arch/x86/include/asm/sev-common.h | 28 + arch/x86/include/asm/sev.h | 45 + arch/x86/include/asm/svm.h | 7 + arch/x86/include/asm/trap_pf.h | 18 +- arch/x86/kernel/cpu/amd.c | 3 +- arch/x86/kernel/sev.c | 361 ++++ arch/x86/kvm/lapic.c | 5 +- arch/x86/kvm/mmu.h | 7 +- arch/x86/kvm/mmu/mmu.c | 84 +- arch/x86/kvm/svm/sev.c | 1676 ++++++++++++++++- arch/x86/kvm/svm/svm.c | 62 +- arch/x86/kvm/svm/svm.h | 74 +- arch/x86/kvm/trace.h | 40 +- arch/x86/kvm/x86.c | 92 +- arch/x86/mm/fault.c | 84 +- drivers/crypto/ccp/sev-dev.c | 924 ++++++++- drivers/crypto/ccp/sev-dev.h | 17 + drivers/crypto/ccp/sp-pci.c | 12 + drivers/iommu/amd/init.c | 30 + include/linux/iommu.h | 9 + include/linux/mm.h | 6 +- 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, 4088 insertions(+), 201 deletions(-) create mode 100644 include/linux/sev.h
Signature · 1 line
2.17.1
PATCH v5 1/45 x86/cpufeatures: Add SEV-SNP CPU feature
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
@@ -398,6 +398,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
@@ -586,7 +586,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* If BIOS has not enabled SME then don't advertise the * SME feature (set in scattered.c). * 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.
@@ -618,6 +618,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
@@ -398,6 +398,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 01/45] x86/cpufeatures: Add SEV-SNP CPU feature
Quoted 17 lines · Brijesh Singh
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]>--- 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 401
Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -398,6 +398,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 */
s/AMD Secure Encrypted Virtualization/AMD SEV/g
Bit 1 above already has that string - no need for repeating it everywhere.
Also, note the vertical alignment (space after the '+'):
(19*32+ 4)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 01/45] x86/cpufeatures: Add SEV-SNP CPU feature
Quoted 25 lines · Borislav Petkov
Quoted 17 lines · Brijesh Singh
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]>--- 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
-0arch/x86/include/asm/cpufeatures.h line 401
Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1
-0@@ -398,6 +398,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 */ s/AMD Secure Encrypted Virtualization/AMD SEV/g
Quoted 7 lines
Bit 1 above already has that string - no need for repeating it everywhere.
Also, note the vertical alignment (space after the '+'):
(19*32+ 4)
Noted. I will fix in next rev. thanks
PATCH v5 2/45 iommu/amd: Introduce function to check SEV-SNP support
In reply to Brijesh SinghThe 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
@@ -3320,3 +3320,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
@@ -604,6 +604,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 {};
@@ -999,6 +1005,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 02/45] iommu/amd: Introduce function to check SEV-SNP support
Quoted 1 line · Brijesh Singh
The SEV-SNP support requires that IOMMU must to enabled, see the IOMMU
s/must to/is/
Quoted 5 lines
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
"can be used"?
Just say what is going to use it.
Quoted 36 lines
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 +20
-0@@ -3320,3 +3320,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 )) {
drivers/iommu/amd/init.c int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 · line 3352
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);
That export is not needed.
Quoted 18 lines
include/linux/iommu.h +6
-0@@ -604,6 +604,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 {};
include/linux/iommu.h static inline struct iommu_fwspec *dev_iommu_fwspec_get(struct device *dev) · line 1010
Quoted 6 lines
include/linux/iommu.h +3
-0@@ -999,6 +1005,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; } +
Most of those stubs and ifdeffery is not needed if you put the function itself in
#ifdef CONFIG_AMD_MEM_ENCRYPT
...
#endif
as it is called by sev.c only, AFAICT, and latter is enabled by CONFIG_AMD_MEM_ENCRYPT anyway.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 3/45 x86/sev: Add the host SEV-SNP initialization support
In reply to Brijesh SinghThe 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
@@ -65,6 +65,12 @@# define DISABLE_SGX (1 << (X86_FEATURE_SGX & 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 */
@@ -88,7 +94,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
@@ -485,6 +485,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
@@ -542,6 +544,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
170 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 03/45] x86/sev: Add the host SEV-SNP initialization support
arch/x86/include/asm/msr-index.h line 550
Quoted 8 lines · Brijesh Singh
arch/x86/include/asm/msr-index.h +4
-0@@ -542,6 +544,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)
While you're here, move all defines that belong to that MSR which we decided it is architectural after all, above the definition of MSR_AMD64_NB_CFG above.
Quoted 19 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 +2
-0@@ -24,6 +24,8 @@#include <linux/sev-guest.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> @@ -40,11 +42,19 @@#include <asm/efi.h> #include <asm/cpuid.h>
arch/x86/kernel/sev.c line 53
Quoted 10 lines
arch/x86/kernel/sev.c +4
-0#include <asm/setup.h> +#include <asm/apic.h> +#include <asm/iommu.h> #include "sev-internal.h" #define DR7_RESET_VALUE 0x400 +/* +* The first 16KB from the RMP_BASE is used by the processor for the
s/for the/for/
arch/x86/kernel/sev.c line 54
Quoted 1 line
arch/x86/kernel/sev.c +1
-0+* bookkeeping, the range need to be added during the RMP entry lookup.
s/need/needs/ ... s/during the/during /
arch/x86/kernel/sev.c line 56
Quoted 2 lines
arch/x86/kernel/sev.c +2
-0+*/ +#define RMPTABLE_CPU_BOOKKEEPING_SZ 0x4000
Too long: just call it RMP_BASE_OFFSET.
Quoted 14 lines
arch/x86/kernel/sev.c +14
-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" ); +return false; + } + + rmp_sz = rmp_end - rmp_base +1 ; +
arch/x86/kernel/sev.c static int __init add_snp_guest_request(void) · line 2297
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+/* +* 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.5.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 ;
use totalram_pages() directly here - nr_pages is assigned to only once.
arch/x86/kernel/sev.c static int __init add_snp_guest_request(void) · line 2308
Quoted 11 lines
arch/x86/kernel/sev.c +11
-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); +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: [ ... - ... ]\n", ...
...
Quoted 3 lines
+/*
+ * 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.
"... it needs to be ensured that... "
Quoted 7 lines
+ * The
iommu_sev_snp_support()is used for checking the feature, and it is
+ * available aftersubsys_initcall().
+ */
+fs_initcall(snp_rmptable_init);Signature · 1 line
2.17.1
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 4/45 x86/sev: Add RMP entry lookup helpers
In reply to Brijesh SinghThe 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>
@@ -77,6 +78,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
Quoted 7 lines · Brijesh Singh
arch/x86/include/asm/sev.h +7
-0+struct __packed rmpentry { + 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 103
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; +};
__packed goes at the end of the struct definition.
arch/x86/include/asm/sev.h extern bool handle_vc_boot_ghcb(struct pt_regs *regs); · line 106
Quoted 3 lines
arch/x86/include/asm/sev.h +3
-0+ +#define rmpentry_assigned (x) ((x)->info.assigned) +#define rmpentry_pagesize (x) ((x)->info.pagesize)
Inline functions pls so that you can get typechecking too.
Quoted 21 lines
#define
RMPADJUST_VMSA_PAGE_BITBIT(16)arch/x86/kernel/sev.c +3
-0@@ -54,6 +54,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);@@ -2376,3 +2378,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 2391
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 );
I think that check should happen first.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2395
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 );
Maybe the above -E should be -ENOENT instead so that you have unique error types for each check to facilitate debugging.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2399
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+ + entry = (struct rmpentry *)vaddr; + +/* Read a large RMP entry to get the correct page level used in RMP entry. */
That comment needs rewriting.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2410
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+ 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. +*/
kernel-doc format since it is being exported.
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2421
Quoted 11 lines
arch/x86/kernel/sev.c +11
-0+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);
This export is for kvm, I presume?
Quoted 27 lines
include/linux/sev.h new file +20
-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 ; }
include/linux/sev.h line 30
Quoted 10 lines
include/linux/sev.h new file +10
-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 */
What is going to use this linux/ namespace header?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
Hi Boris,
I agreed with all of your comment, responding to your specific questions.
On 9/24/21 4:49 AM, Borislav Petkov wrote:
...
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+} +EXPORT_SYMBOL_GPL(snp_lookup_rmpentry);This export is for kvm, I presume?
yes, both KVM and CCP (i.e PSP) driver will need to lookup RMP entries.
Quoted 32 lines
Quoted 32 lines
include/linux/sev.h new file +25
-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;
Quoted 9 lines
include/linux/sev.h line 30
Quoted 6 lines
include/linux/sev.h new file +5
-0+} +static inline int rmp_make_shared (u64 pfn,enum pg_level level) {return -ENODEV ; } + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */ What is going to use this linux/ namespace header?
The kvm and ccp drivers.
Re: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
Quoted 6 lines · Brijesh Singh
Quoted 4 lines
Quoted 1 line
What is going to use this linux/ namespace header?
The kvm and ccp drivers.
Currently, we have only x86 drivers using it, are you thinking to move this to arch/x86/include/asm/ ?
thanks
Borislav Petkov
maintainerRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
Quoted 2 lines · Brijesh Singh
Currently, we have only x86 drivers using it, are you thinking to move this to
arch/x86/include/asm/?
arch/x86/include/asm/sev.h, yap.
We can always create it later if needed by other architectures.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 04/45] x86/sev: Add RMP entry lookup helpers
In reply to Brijesh SinghQuoted 93 lines · Brijesh Singh
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> @@ -77,6 +78,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 +21
-0@@ -54,6 +54,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);@@ -2376,3 +2378,44 @@ static int __init snp_rmptable_init(void)* available after subsys_initcall(). */ fs_initcall (snp_rmptable_init); + +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 ); + + vaddr = rmptable_start +rmptable_page_offset (paddr); +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. */
arch/x86/kernel/sev.c static int __init snp_rmptable_init(void) · line 2409
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ 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.
According to /usr/include/asm-generic/errno.h, there is 133 error codes. Why you need so many just to say that there is no entry?
Quoted 18 lines
arch/x86/kernel/sev.c +12
-0+*/ +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
include/linux/sev.h line 5
Quoted 5 lines
include/linux/sev.h new file +5
-0@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Secure Encrypted Virtualization +* +* Author: Brijesh Singh <[email protected]>
nit: this is redundant.
Each git commit has an author-field.
Quoted 28 lines
include/linux/sev.h new file +25
-0+*/ + +#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.17.1
BR, Jarkko
PATCH v5 5/45 x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Brijesh SinghThe 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
@@ -78,7 +78,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
@@ -107,6 +109,15 @@ struct __packed rmpentry {#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 29 lines · Brijesh Singh
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@@ -78,7 +78,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 __packed rmpentry { · line 112
Quoted 4 lines
arch/x86/include/asm/sev.h +1
-0@@ -107,6 +109,15 @@ struct __packed rmpentry {#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +struct rmpupdate {
Function is called the same way - maybe this should be called rmpupdate_desc or so.
Quoted 16 lines
arch/x86/include/asm/sev.h +8
-0+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 +0
-0
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2422
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2419,3 +2419,75 @@ int snp_lookup_rmpentry(u64 pfn, int *level)return !!rmpentry_assigned (e); }EXPORT_SYMBOL_GPL (snp_lookup_rmpentry); +
<--- kernel-doc comment.
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2432
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 ;
Make that the first check pls.
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2442
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);
That's for kvm?
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2453
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+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 ;
Also first check.
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+ +/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" + :"=a" (ret) + : "a"(paddr), "c"((unsigned long)val)
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2468
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ : "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 ;
rmpupdate() does that check too so choose one place please and kill the other.
Quoted 14 lines
arch/x86/kernel/sev.c +14
-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;
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2493
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ +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);
Both exports for kvm I assume?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 31 lines · Borislav Petkov
Quoted 30 lines · Brijesh Singh
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@@ -78,7 +78,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 @@ -107,6 +109,15 @@ struct __packed rmpentry {
Quoted 6 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 112
Quoted 3 lines
arch/x86/include/asm/sev.h +1
-0#define RMPADJUST_VMSA_PAGE_BIT BIT(16) +struct rmpupdate {Function is called the same way - maybe this should be called
rmpupdate_descor so.
Noted.
Quoted 6 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 116
Quoted 4 lines
arch/x86/include/asm/sev.h +4
-0+ +return ret; +} +EXPORT_SYMBOL_GPL (psmash);That's for kvm?
Yes.
...
Quoted 3 lines
arch/x86/include/asm/sev.h struct __packed rmpentry { · line 117
Quoted 1 line
arch/x86/include/asm/sev.h +1
-0+EXPORT_SYMBOL_GPL (rmp_make_shared);Both exports for kvm I assume?
yes, both KVM and CCP drivers need them.
thanks
Sean Christopherson
maintainerRe: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
In reply to Brijesh Singharch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2425
Quoted 12 lines · Brijesh Singh
arch/x86/kernel/sev.c +4
-0@@ -2419,3 +2419,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 ;
Probably better to use __pfn_to_phys()?
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2431
Quoted 6 lines
arch/x86/kernel/sev.c +6
-0+int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))
Shouldn't this be a WARN_ON_ONCE()?
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2440
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+return -ENXIO ; + +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret;
I don't like returning the raw result from hardware; it's mostly works because hardware also uses '0' for success, but it will cause confusion should hardware ever set bit 31. There are also failures that likely should never happen unless there's a kernel bug, e.g. I suspect we can do:
if (WARN_ON_ONCE(ret == FAIL_INPUT)) return -EINVAL; if (WARN_ON_ONCE(ret == FAIL_PERMISSION)) return -EPERM; ....
or if all errors are "impossible"
if (WARN_ON_ONCE(ret)) return snp_error_code_to_errno(ret);
FAIL_INUSE and FAIL_OVERLAP also need further discussion. It's not clear to me that two well-behaved callers can't encounter collisions due to the 2mb <=> 4kb interactions. If collisions between well-behaved callers are possible, then this helper likely needs some form of serialization. Either, the concurrency rules for RMP access need explicit and lengthy documentation.
Re: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 14 lines · Sean Christopherson
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2425
Quoted 12 lines · Brijesh Singh
arch/x86/kernel/sev.c +4
-0@@ -2419,3 +2419,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 ;Probably better to use
__pfn_to_phys()?
Sure, I can use that instead of direct shift.
Quoted 8 lines
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2431
Quoted 7 lines
arch/x86/kernel/sev.c +6
-0+int ret; + +if (!pfn_valid (pfn)) +return -EINVAL ; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP ))Shouldn't this be a
WARN_ON_ONCE()?
Since some of these function are called while handling the PSC so I tried to avoid using the WARN -- mainly because if the warn_on_panic=1 is set on the host then it will result in the kernel panic.
Quoted 24 lines
arch/x86/kernel/sev.c int snp_lookup_rmpentry(u64 pfn, int *level) · line 2440
Quoted 10 lines
arch/x86/kernel/sev.c +9
-0+return -ENXIO ; + +/* Binutils version 2.36 supports the PSMASH mnemonic. */ + asmvolatile (".byte 0xF3, 0x0F, 0x01, 0xFF" + :"=a" (ret) + :"a" (paddr) + :"memory" ,"cc" ); + +return ret;I don't like returning the raw result from hardware; it's mostly works because hardware also uses '0' for success, but it will cause confusion should hardware ever set bit 31. There are also failures that likely should never happen unless there's a kernel bug, e.g. I suspect we can do:
if (WARN_ON_ONCE(ret == FAIL_INPUT)) return -EINVAL; if (WARN_ON_ONCE(ret == FAIL_PERMISSION)) return -EPERM; ....or if all errors are "impossible"
Quoted 8 lines
if (WARN_ON_ONCE(ret)) return snp_error_code_to_errno(ret);
FAIL_INUSEandFAIL_OVERLAPalso need further discussion. It's not clear to me that two well-behaved callers can't encounter collisions due to the 2mb <=> 4kb interactions. If collisions between well-behaved callers are possible, then this helper likely needs some form of serialization. Either, the concurrency rules for RMP access need explicit and lengthy documentation.
I don't think we need to serialize the calls. The hardware acquires the lock before changing the RMP table, and if another processor tries to access the same RMP table entry, the hardware will return FAIL_INUSE or #NPF. The FAIL_INUSE will happen on the RMPUPDATE, whereas the #NPF will occur if the guest attempts to modify the RMP entry (such as using the RMPADJUST).
As per the FAIL_OVERLAP is concerns, it's the case where the guest is asking to create an invalid situation and hardware detects it. In other words, it is a guest bug. e.g., the guest issued a PSC to add a page as 2MB and then asked to add the same page (or one of subpage) as a 4K. Hardware sees the overlap condition and fails to change the state on the second request.
thanks
Sean Christopherson
maintainerRe: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 8 lines · Brijesh Singh
Quoted 3 lines · Sean Christopherson
Review
Quoted 1 line · Brijesh Singh
(quoted diff) +1
-0+ if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))Shouldn't this be a
WARN_ON_ONCE()?Since some of these function are called while handling the PSC so I tried to avoid using the WARN -- mainly because if the warn_on_panic=1 is set on the host then it will result in the kernel panic.
But why would KVM be handling PSC requests if SNP is disabled?
Re: [PATCH Part2 v5 05/45] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction
Quoted 9 lines · Sean Christopherson
Quoted 7 lines · Brijesh Singh
Quoted 3 lines · Sean Christopherson
Review
Quoted 1 line · Brijesh Singh
(quoted diff) +1
-0+ if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP))Shouldn't this be a
WARN_ON_ONCE()?Since some of these function are called while handling the PSC so I tried to avoid using the WARN -- mainly because if the warn_on_panic=1 is set on the host then it will result in the kernel panic.
But why would KVM be handling PSC requests if SNP is disabled?
The RMPUPDATE is also used by the CCP drv to change the page state during the initialization. You are right that neither KVM nor CCP should be using these function when SNP is not enabled. In your peudo code you used the WARN_ON_ONCE() for the cpu_feature_enabled() and return code check. I was more concern about the return code WARN_ON_ONCE() because that will be called during the PSC. I am okay with using the WARN_ON_ONCE() for the cpu_feature_enabled() check.
thanks
PATCH v5 6/45 x86/sev: Invalid pages from direct map when adding it to RMP table
In reply to Brijesh SinghThe 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
@@ -2441,10 +2441,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 ;
@@ -2452,11 +2484,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 1 line · Brijesh Singh
Subject: Re: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
That subject needs to have a verb. I think that verb should be "Invalidate".
Quoted 6 lines
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.
Quoted 7 lines
The
rmp_make_private()andrmp_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.
That sentence needs rewriting into proper english.
The more important thing is, though, this doesn't talk about *why* you're doing this: you want to remove pages from the direct map when they're in the RMP table because something might modify the page and then the RMP check will fail?
Also, set_direct_map_invalid_noflush() simply clears the Present and RW bit of a pte.
So what's up?
Quoted 10 lines
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 +0
-0
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2444
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2441,10 +2441,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages)
restore_pages_in_direct_map()
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2452
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+{ +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; + }
So this is looping over a set of virtually contiguous pages, I presume, and if so, you should add a function called
set_memory_p_rw()
to arch/x86/mm/pat/set_memory.c which does
return change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);
so that you can do all pages in one go.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2459
Quoted 7 lines
arch/x86/kernel/sev.c +7
-0+ +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)
invalidate_pages_in_direct_map()
or so.
arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2464
Quoted 5 lines
arch/x86/kernel/sev.c +5
-0+{ +int i, ret =0 ; + +for (i =0 ; i < npages; i++) { + ret =set_direct_map_invalid_noflush (pfn_to_page (pfn + i));
Same as above but that helper should do the reverse:
set_memory_np_ro()
{
return change_page_attr_clear(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);
}Btw, please add those helpers in a separate patch.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
Hi Boris,
On 9/29/21 9:34 AM, Borislav Petkov wrote:
...
Quoted 10 lines
Quoted 4 lines
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.That sentence needs rewriting into proper english.
The more important thing is, though, this doesn't talk about *why* you're doing this: you want to remove pages from the direct map when they're in the RMP table because something might modify the page and then the RMP check will fail?
I'll work to improve the commit description.
Quoted 4 lines
Also,
set_direct_map_invalid_noflush()simply clears the Present and RW bit of a pte.So what's up?
The set_direct_map_invalid_noflush() does two steps
1) Split the host page table (if needed)
2) Clear the present and RW bit from pte
In previous patches I was using the set_memory_4k() to split the direct map before adding the pages in the RMP table. Based on Sean's review comment [1], I switched to using set_direct_map_invalid_noflush() so that page is not just split but also removed from the direct map. The thought process is if in the future set_direct_map_default_noflush() is improved to restore the large mapping then it will all work transparently.
[1] https://lore.kernel.org/lkml/[email protected]/#t
Quoted 27 lines
Quoted 10 lines
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 +0
-0arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2444
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2441,10 +2441,42 @@ int psmash(u64 pfn) }EXPORT_SYMBOL_GPL (psmash); +static int restore_direct_map (u64 pfn,int npages)
restore_pages_in_direct_map()arch/x86/kernel/sev.c int psmash(u64 pfn) · line 2452
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+{ +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; + }So this is looping over a set of virtually contiguous pages, I presume, and if so, you should add a function called
Quoted 7 lines
set_memory_p_rw()to
arch/x86/mm/pat/set_memory.cwhich doesreturn change_page_attr_set(&addr, numpages, __pgprot(_PAGE_PRESENT | _PAGE_RW), 0);so that you can do all pages in one go.
I will look into it.
thanks
Borislav Petkov
maintainerRe: [PATCH Part2 v5 06/45] x86/sev: Invalid pages from direct map when adding it to RMP table
Quoted 3 lines · Brijesh Singh
. The thought process is if in the future
set_direct_map_default_noflush()is improved to restore the large mapping then it will all work transparently.
That's only scratching the surface of the *why* this is done so please explain why this dance is being done in a comment above the code so that it is clear.
It is not really obvious why that hiding from the direct map is being done.
Good reason from that memfd_secret mail are:
"* Prevent cross-process secret userspace memory exposures. Once the secret memory is allocated, the user can't accidentally pass it into the kernel to be transmitted somewhere. The secreremem pages cannot be accessed via the direct map and they are disallowed in GUP."
and in general hiding RMP pages from the direct map is a nice additional protection.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 7/45 x86/traps: Define RMP violation #PF error code
In reply to Brijesh SinghBit 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
@@ -545,6 +545,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 07/45] x86/traps: Define RMP violation #PF error code
Quoted 6 lines · Brijesh Singh
enum
x86_pf_error_code{arch/x86/include/asm/trap_pf.h +0
-5-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),
Those are tested against error_code mostly, which is unsigned long so it looks like you wanna use _BITUL() here. Not that it matters on x86-64 but if we want to be precise...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 8/45 x86/fault: Add support to handle the RMP fault for user address
In reply to Brijesh SinghWhen 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 | 6 ++++- mm/memory.c | 13 +++++++++ 3 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, ... */
@@ -1202,6 +1203,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
@@ -1299,6 +1354,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 +5 -1
@@ -447,6 +447,8 @@ extern pgprot_t protection_map[16];* @FAULT_FLAG_REMOTE: The fault is not for current task/mm. * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. +* @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
@@ -478,6 +480,7 @@ enum fault_flag {FAULT_FLAG_REMOTE =1 <<7 ,FAULT_FLAG_INSTRUCTION =1 <<8 ,FAULT_FLAG_INTERRUPTIBLE =1 <<9 , + FAULT_FLAG_PAGE_SPLIT = 1 << 10, };/*
@@ -517,7 +520,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
mm/memory.c +13 -0
@@ -4589,6 +4589,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 *
@@ -4666,6 +4675,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.17.1
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-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 ; +}
We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
What was the result of those review comments?
I'm still worried that hugetlbfs (and others) are not properly handled by this series.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Hi Dave,
Quoted 14 lines · Dave Hansen
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-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 ; +}We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
Quoted 2 lines
What was the result of those review comments?
Based on previous review comments Sean was not keen on KVM having perform this detection and abort the guest SEV-SNP VM launch. So, I didn't implemented the check and waiting for more discussion before going at it.
SEV-SNP guest requires the VMM to register the guest backing pages before the VM launch. Personally, I would prefer KVM to check the backing page type during the registration and fail to register if its hugetlbfs (and others) to avoid us get into situation where we could not split the hugepage.
thanks
Quoted 3 lines
I'm still worried that hugetlbfs (and others) are not properly handled by this series.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 15 lines · Brijesh Singh
Quoted 14 lines · Dave Hansen
mm/memory.c static vm_fault_t handle_pte_fault(struct vm_fault *vmf) · line 4599
Quoted 8 lines · Brijesh Singh
mm/memory.c +8
-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; +}We had a whole conversation the last time this was posted about huge page types *other* than THP. I don't see any comprehension of those types or what would happen if one of those was used with SEV-SNP.
Quoted 6 lines
Quoted 1 line
What was the result of those review comments?
Based on previous review comments Sean was not keen on KVM having perform this detection and abort the guest SEV-SNP VM launch. So, I didn't implemented the check and waiting for more discussion before going at it.
OK. But, you need to *acknowledge* the situation somewhere. Maybe the cover letter of the series, maybe in this changelog.
As it stands, it looks like you're simply ignoring _some_ reviewer feedback.
Quoted 5 lines
SEV-SNP guest requires the VMM to register the guest backing pages before the VM launch. Personally, I would prefer KVM to check the backing page type during the registration and fail to register if its hugetlbfs (and others) to avoid us get into situation where we could not split the hugepage.
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 10 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
Quoted 6 lines
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
Regards,
Joerg
Re: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 15 lines · Joerg Roedel
Quoted 14 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
Quoted 6 lines
Quoted 2 lines
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
FTR, this is Sean's reply with concerns in v4:
https://lore.kernel.org/linux-coco/YPCuTiNET%[email protected]/
I think there are two main arguments there:
- it's not KVM business to decide
- guest may do all page state changes with 2mb granularity so it might be fine with hugetlb
The latter might become true, but I think it's more probable that sooner hugetlbfs will learn to split the mappings to base pages - I know people plan to work on that. At that point qemu will have to recognize if the host kernel is the new one that can do this splitting vs older one that can't. Preferably without relying on kernel version number, as backports exist. Thus, trying to register a hugetlbfs range that either is rejected (kernel can't split) or passes (kernel can split) seems like a straightforward way. So I'm also in favor of adding that, hopefuly temporary, check.
Vlastimil
Quoted 4 lines
Regards,
Joerg
Tom Lendacky
maintainerRe: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
Quoted 31 lines · Vlastimil Babka
Quoted 15 lines · Joerg Roedel
Quoted 14 lines · Dave Hansen
It *has* to be done in KVM, IMNHO.
The core kernel really doesn't know much about SEV. It *really* doesn't know when its memory is being exposed to a virtualization architecture that doesn't know how to split TLBs like every single one before it.
This essentially *must* be done at the time that the KVM code realizes that it's being asked to shove a non-splittable page mapping into the SEV hardware structures.
The only other alternative is raising a signal from the fault handler when the page can't be split. That's a *LOT* nastier because it's so much later in the process.
Quoted 6 lines
Quoted 2 lines
It's either that, or figure out a way to split hugetlbfs (and DAX) mappings in a failsafe way.
Yes, I agree with that. KVM needs a check to disallow HugeTLB pages in SEV-SNP guests, at least as a temporary workaround. When HugeTLBfs mappings can be split into smaller pages the check can be removed.
I think there are two main arguments there:
- it's not KVM business to decide
- guest may do all page state changes with 2mb granularity so it might be fine with hugetlb
Quoted 8 lines
The latter might become true, but I think it's more probable that sooner hugetlbfs will learn to split the mappings to base pages - I know people plan to work on that. At that point qemu will have to recognize if the host kernel is the new one that can do this splitting vs older one that can't. Preferably without relying on kernel version number, as backports exist. Thus, trying to register a hugetlbfs range that either is rejected (kernel can't split) or passes (kernel can split) seems like a straightforward way. So I'm also in favor of adding that, hopefuly temporary, check.
If that's the direction taken, I think we'd be able to use a KVM_CAP_ value that can be queried by the VMM to make the determination.
Thanks,
Tom
Quoted 8 lines
Vlastimil
Quoted 4 lines
Regards,
Joerg
Borislav Petkov
maintainerRe: [PATCH Part2 v5 08/45] x86/fault: Add support to handle the RMP fault for user address
In reply to Brijesh Singharch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1217
Quoted 3 lines · Brijesh Singh
arch/x86/mm/fault.c +3
-0+static int handle_user_rmp_page_fault (struct pt_regs *regs,unsigned long error_code, +unsigned long address) +{
#ifdef CONFIG_AMD_MEM_ENCRYPT
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+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. +*/
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1237
Quoted 10 lines
arch/x86/mm/fault.c +10
-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 );
Just use two helper variables named properly instead of this oneliner:
pages_level = page_level_size(level) / PAGE_SIZE; pages_prev_level = page_level_size(level - 1) / PAGE_SIZE;
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+ 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 ;
arch/x86/mm/fault.c do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, · line 1257
Quoted 10 lines
arch/x86/mm/fault.c +10
-0+ } + +/* +* The backing page level is higher than the RMP page level, request +* to split the page. +*/ +if (level > rmp_level) +return 0 ; + +return 1 ;
#else WARN_ONONCE(1); return -1;
#endif
and also handle that -1 negative value at the call site.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 9/45 x86/fault: Add support to dump RMP entry on fault
In reply to Brijesh SinghWhen 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
@@ -106,6 +106,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)
@@ -165,6 +170,7 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op opvoid snp_set_memory_shared (unsigned long vaddr,unsigned int npages);void snp_set_memory_private (unsigned long vaddr,unsigned int npages);void snp_set_wakeup_secondary_cpu (void ); +void dump_rmpentry (u64 pfn);#ifdef __BOOT_COMPRESSEDbool sev_snp_enabled (void );#else
@@ -188,6 +194,7 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npagestatic inline void snp_set_memory_private (unsigned long vaddr,unsigned int npages) { }static inline void snp_set_wakeup_secondary_cpu (void ) { }static inline void sev_snp_cpuid_init (struct boot_params *bp) { } +static inline void dump_rmpentry (u64 pfn) {}#ifdef __BOOT_COMPRESSEDstatic inline bool sev_snp_enabled {return false; }#else
arch/x86/kernel/sev.c +43 -0
49 lines not on this page · raw message
arch/x86/mm/fault.c +13 -4
@@ -33,6 +33,7 @@#include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ #include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
@@ -289,7 +290,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)];
@@ -345,10 +346,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;
@@ -366,6 +368,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;
@@ -374,6 +377,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;
@@ -382,6 +386,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;
@@ -390,9 +395,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" );
@@ -578,7 +587,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
@@ -595,7 +604,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.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 09/45] x86/fault: Add support to dump RMP entry on fault
Quoted 18 lines · Brijesh Singh
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 113
Quoted 8 lines
arch/x86/include/asm/sev.h +5
-0@@ -106,6 +106,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 some of those accessors are going to be used only in dump_rmpentry(), then you don't really need them.
Quoted 24 lines
arch/x86/include/asm/sev.h +2
-0#define RMPADJUST_VMSA_PAGE_BIT BIT(16)@@ -165,6 +170,7 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op opvoid snp_set_memory_shared (unsigned long vaddr,unsigned int npages);void snp_set_memory_private (unsigned long vaddr,unsigned int npages);void snp_set_wakeup_secondary_cpu (void ); +void dump_rmpentry (u64 pfn);#ifdef __BOOT_COMPRESSEDbool sev_snp_enabled (void );#else @@ -188,6 +194,7 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npagestatic inline void snp_set_memory_private (unsigned long vaddr,unsigned int npages) { }static inline void snp_set_wakeup_secondary_cpu (void ) { }static inline void sev_snp_cpuid_init (struct boot_params *bp) { } +static inline void dump_rmpentry (u64 pfn) {}#ifdef __BOOT_COMPRESSEDstatic inline bool sev_snp_enabled {return false; }#else arch/x86/kernel/sev.c +0
-0
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2407
Quoted 4 lines
arch/x86/kernel/sev.c +1
-0@@ -2404,6 +2404,49 @@ static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level)return entry; } +void dump_rmpentry (u64 pfn)
snp_dump_rmpentry()
Quoted 4 lines
arch/x86/kernel/sev.c +4
-0+{ +unsigned long pfn_end; +struct rmpentry *e; +int level;
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2421
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+ + e =__snp_lookup_rmpentry (pfn, &level); +if (!e) { +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,
WARNING: quoted string split across lines
#174: FILE: arch/x86/kernel/sev.c:2421:
+ 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,
Quoted 9 lines
arch/x86/kernel/sev.c +9
-0+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,
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2440
Quoted 10 lines
arch/x86/kernel/sev.c +10
-0+* 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 ;
return? You mean "continue;" ?
arch/x86/kernel/sev.c static struct rmpentry *__snp_lookup_rmpentry(u64 pfn, int *level) · line 2448
Quoted 8 lines
arch/x86/kernel/sev.c +8
-0+ +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);
Why is that exported? Some module will be calling it too?
Quoted 17 lines
arch/x86/kernel/sev.c +1
-0+/* * 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 +1
-0@@ -33,6 +33,7 @@#include <asm/pgtable_areas.h> /* VMALLOC_START, ... */ #include <asm/kvm_para.h> /* kvm_handle_async_pf */ #include <asm/vdso.h> /* fixup_vdso_exception() */ +#include <asm/sev.h> /* dump_rmpentry() */ #define CREATE_TRACE_POINTS #include <asm/trace/exceptions.h>
arch/x86/mm/fault.c static bool low_pfn(unsigned long pfn) · line 293
Quoted 5 lines
arch/x86/mm/fault.c +1
-1@@ -289,7 +290,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)
I think passing in error_code and testing X86_PF_RMP inside should make this a bit more palatable than simply "grafting" SNP-specific functionality to generic paths.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 10/45 crypto: ccp: shutdown SEV firmware on kexec
In reply to Brijesh SinghThe commit 97f9ac3db6612 ("crypto: ccp - Add support for SEV-ES to the PSP driver") added support to allocate Trusted Memory Region (TMR) used during the SEV-ES firmware initialization. The TMR gets locked during the firmware initialization and unlocked during the shutdown. While the TMR is locked, access to it is disallowed.
Currently, the CCP driver does not shutdown the firmware during the kexec reboot, leaving the TMR memory locked.
Register a callback to shutdown the SEV firmware on the kexec boot.
--- drivers/crypto/ccp/sev-dev.c | 49 +++++++++++++++++------------------- drivers/crypto/ccp/sp-pci.c | 12 +++++++++ 2 files changed, 35 insertions(+), 26 deletions(-)
drivers/crypto/ccp/sev-dev.c +23 -26
@@ -300,6 +300,9 @@ static int __sev_platform_shutdown_locked(int *error)struct sev_device *sev = psp_master->sev_data;int ret; +if (sev->state ==SEV_STATE_UNINIT ) +return 0 ; + ret =__sev_do_cmd_locked (SEV_CMD_SHUTDOWN ,NULL , error);if (ret)return ret;
@@ -1019,6 +1022,20 @@ int sev_dev_init(struct psp_device *psp)return ret; } +static void sev_firmware_shutdown (struct sev_device *sev) +{ +sev_platform_shutdown (NULL ); + +if (sev_es_tmr) { +/* 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 )); + sev_es_tmr =NULL ; + } +} +void sev_dev_destroy (struct psp_device *psp) {struct sev_device *sev = psp->sev_data;
@@ -1026,6 +1043,8 @@ void sev_dev_destroy(struct psp_device *psp)if (!sev)return ; +sev_firmware_shutdown (sev); +if (sev->misc)kref_put (&misc_dev->refcount, sev_exit);
@@ -1056,21 +1075,6 @@ void sev_pci_init(void)if (sev_get_api_version ()) goto err; -/* -* If platform is not in UNINIT state then firmware upgrade and/or -* platform INIT command will fail. These command require UNINIT state. -* -* In a normal boot we should never run into case where the firmware -* is not in UNINIT state on boot. But in case of kexec boot, a reboot -* may not go through a typical shutdown sequence and may leave the -* firmware in INIT or WORKING state. -*/ - -if (sev->state !=SEV_STATE_UNINIT ) { -sev_platform_shutdown (NULL ); - sev->state =SEV_STATE_UNINIT ; - } -if (sev_version_greater_or_equal (0 ,15 ) &&sev_update_firmware (sev->dev) ==0 )sev_get_api_version ();
@@ -1115,17 +1119,10 @@ void sev_pci_init(void)void sev_pci_exit (void ) { -if (!psp_master->sev_data) -return ; - -sev_platform_shutdown (NULL ); +struct sev_device *sev = psp_master->sev_data; -if (sev_es_tmr) { -/* The TMR area was encrypted, flush it from the cache */ -wbinvd_on_all_cpus (); +if (!sev) +return ; -free_pages ((unsigned long )sev_es_tmr, -get_order (SEV_ES_TMR_SIZE )); - sev_es_tmr =NULL ; - } +sev_firmware_shutdown (sev); }
drivers/crypto/ccp/sp-pci.c +12 -0
@@ -241,6 +241,17 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)return ret; } +static void sp_pci_shutdown (struct pci_dev *pdev) +{ +struct device *dev = &pdev->dev; +struct sp_device *sp =dev_get_drvdata (dev); + +if (!sp) +return ; + +sp_destroy (sp); +} +static void sp_pci_remove (struct pci_dev *pdev) {struct device *dev = &pdev->dev;
@@ -371,6 +382,7 @@ static struct pci_driver sp_pci_driver = { .id_table = sp_pci_table, .probe = sp_pci_probe, .remove = sp_pci_remove, + .shutdown = sp_pci_shutdown, .driver.pm = &sp_pci_pm_ops, };
Signature · 1 line
2.17.1
PATCH v5 11/45 crypto:ccp: Define the SEV-SNP commands
In reply to Brijesh SinghAMD 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 | 16 ++- include/linux/psp-sev.h | 222 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 42 +++++++ 3 files changed, 279 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +15 -1
@@ -134,7 +134,21 @@ static int sev_cmd_buffer_len(int cmd)case SEV_CMD_DOWNLOAD_FIRMWARE :return sizeof (struct sev_data_download_firmware );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_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.17.1
PATCH v5 12/45 crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP
In reply to Brijesh SinghBefore SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command.
--- drivers/crypto/ccp/sev-dev.c | 123 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 16 +++++ 3 files changed, 136 insertions(+), 5 deletions(-)
drivers/crypto/ccp/sev-dev.c +118 -5
@@ -32,6 +32,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 30 +static DEFINE_MUTEX (sev_cmd_mutex);static struct sev_misc_dev *misc_dev;
@@ -598,6 +602,95 @@ 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 ); + +/* Prepare for first SEV guest launch after INIT */ +wbinvd_on_all_cpus (); + +/* Issue the SNP_INIT firmware command. */ + rc =__sev_do_cmd_locked (SEV_CMD_SNP_INIT ,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;
@@ -1048,6 +1141,8 @@ static void sev_firmware_shutdown(struct sev_device *sev) get_order(SEV_ES_TMR_SIZE)); sev_es_tmr =NULL ; } + +sev_snp_shutdown (NULL ); }void sev_dev_destroy (struct psp_device *psp)
@@ -1093,6 +1188,26 @@ void sev_pci_init(void) sev_update_firmware(sev->dev) == 0)sev_get_api_version (); +/* +* 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 */ tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE ));if (tmr_page) {
@@ -1117,13 +1232,11 @@ void sev_pci_init(void) rc =sev_platform_init (&error); } -if (rc) { +if (rc)dev_err (sev->dev,"SEV: failed to INIT error %#x\n" , error); -return ; - } -dev_info (sev->dev,"SEV API:%d.%d build:%d\n" , sev->api_major, - sev->api_minor, sev->build); +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 ;
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
@@ -748,6 +748,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 *
@@ -855,6 +869,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.17.1
PATCH v5 13/45 crypto:ccp: Provide APIs to issue SEV-SNP commands
In reply to Brijesh SinghProvide 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
@@ -1025,6 +1025,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.17.1
PATCH v5 14/45 crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Brijesh SinghThe 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 | 169 ++++++++++++++++++++++++++++++++++- include/linux/psp-sev.h | 11 +++ 2 files changed, 176 insertions(+), 4 deletions(-)
drivers/crypto/ccp/sev-dev.c +165 -4
@@ -62,6 +62,14 @@ static int psp_timeout;#define SEV_ES_TMR_SIZE (1024 * 1024)static void *sev_es_tmr; +/* 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;
@@ -159,6 +167,156 @@ 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 ERR_PTR (-EINVAL ); + + 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 int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;
@@ -281,7 +439,7 @@ static int __sev_platform_init_locked(int *error) data.flags |=SEV_INIT_FLAGS_SEV_ES ; data.tmr_address = tmr_pa; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; } rc =__sev_do_cmd_locked (SEV_CMD_INIT , &data, error);
@@ -638,6 +796,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; }
@@ -1161,8 +1321,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 ; }
@@ -1233,7 +1394,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE )); + tmr_page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (sev_es_tmr_size), false);if (tmr_page) { sev_es_tmr =page_address (tmr_page); }else {
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
@@ -919,6 +921,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 */
@@ -960,6 +964,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.17.1
Re: [PATCH Part2 v5 14/45] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Quoted 216 lines · Brijesh Singh
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 | 169 ++++++++++++++++++++++++++++++++++- include/linux/psp-sev.h | 11 +++ 2 files changed, 176 insertions(+), 4 deletions(-)drivers/crypto/ccp/sev-dev.c +161
-1@@ -62,6 +62,14 @@ static int psp_timeout;#define SEV_ES_TMR_SIZE (1024 * 1024)static void *sev_es_tmr; +/* 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;@@ -159,6 +167,156 @@ 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 ERR_PTR (-EINVAL ); + + 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 int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;@@ -281,7 +439,7 @@ static int __sev_platform_init_locked(int *error) data.flags |=SEV_INIT_FLAGS_SEV_ES ; data.tmr_address = tmr_pa; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; } rc =__sev_do_cmd_locked (SEV_CMD_INIT , &data, error);@@ -638,6 +796,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; }
drivers/crypto/ccp/sev-dev.c static void sev_firmware_shutdown(struct sev_device *sev) · line 1326
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +3
-2@@ -1161,8 +1321,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);
Shouldn't there be a check here for snp_inited before calling rmpupdate. TMR page can exist even if the SNP is not supported.
Quoted 52 lines
drivers/crypto/ccp/sev-dev.c +1
-1sev_es_tmr =NULL ; }@@ -1233,7 +1394,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE )); + tmr_page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (sev_es_tmr_size), false);if (tmr_page) { sev_es_tmr =page_address (tmr_page); }else {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 @@ -919,6 +921,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 */ @@ -960,6 +964,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.17.1
Re: [PATCH Part2 v5 14/45] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
Hi Alper,
Quoted 9 lines · Alper Gun
drivers/crypto/ccp/sev-dev.c static void sev_firmware_shutdown(struct sev_device *sev) · line 1326
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +3
-2-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);Shouldn't there be a check here for
snp_initedbefore calling rmpupdate. TMR page can exist even if the SNP is not supported.
Yes, I have this fix in my WIP branch and will be included in the v6.
thanks
Re: [PATCH Part2 v5 14/45] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled
In reply to Brijesh SinghSimilar to the TMR page, sev_init_ex_buffer should be owned by firmware. Otherwise INIT_EX won't work with the SNP. Since v5 patches are prepared before INIT_EX work, I wanted to bring this to your attention.
One difference from the TMR page, sev_init_ex_buffer has to be in the direct map. Firmware pages are removed from directmap in v5 patches. But the kernel reads sev_init_ex_buffer later to write into a persistent file. I have a version to make it work, if you're interested I can share.
Quoted 276 lines · Brijesh Singh
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 | 169 ++++++++++++++++++++++++++++++++++- include/linux/psp-sev.h | 11 +++ 2 files changed, 176 insertions(+), 4 deletions(-)drivers/crypto/ccp/sev-dev.c +165
-4@@ -62,6 +62,14 @@ static int psp_timeout;#define SEV_ES_TMR_SIZE (1024 * 1024)static void *sev_es_tmr; +/* 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;@@ -159,6 +167,156 @@ 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 ERR_PTR (-EINVAL ); + + 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 int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;@@ -281,7 +439,7 @@ static int __sev_platform_init_locked(int *error) data.flags |=SEV_INIT_FLAGS_SEV_ES ; data.tmr_address = tmr_pa; - data.tmr_len =SEV_ES_TMR_SIZE ; + data.tmr_len = sev_es_tmr_size; } rc =__sev_do_cmd_locked (SEV_CMD_INIT , &data, error);@@ -638,6 +796,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; }@@ -1161,8 +1321,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 ; }@@ -1233,7 +1394,7 @@ void sev_pci_init(void) }/* Obtain the TMR memory area for SEV-ES use */ - tmr_page =alloc_pages (GFP_KERNEL ,get_order (SEV_ES_TMR_SIZE )); + tmr_page =__snp_alloc_firmware_pages (GFP_KERNEL ,get_order (sev_es_tmr_size), false);if (tmr_page) { sev_es_tmr =page_address (tmr_page); }else {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 @@ -919,6 +921,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 */ @@ -960,6 +964,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.17.1
PATCH v5 15/45 crypto: ccp: Handle the legacy SEV command when SNP is enabled
In reply to Brijesh SinghThe behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command..
A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled:
1) system physical address points to a guest memory
2) system physical address points to a host memory
To handle the case #1, change the page state to the firmware in the RMP table before issuing the command and restore the state to shared after the command completes.
For the case #2, use a bounce buffer to complete the request.
--- drivers/crypto/ccp/sev-dev.c | 346 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 348 insertions(+), 10 deletions(-)
drivers/crypto/ccp/sev-dev.c +336 -10
@@ -317,12 +317,295 @@ void snp_free_firmware_page(void *addr) }EXPORT_SYMBOL (snp_free_firmware_page); +static int alloc_snp_host_map (struct sev_device *sev) +{ +struct page *page; +int i; + +for (i =0 ; i <MAX_SNP_HOST_MAP_BUFS ; i++) { +struct snp_host_map *map = &sev->snp_host_map[i]; + +memset (map,0 ,sizeof (*map)); + + page =alloc_pages (GFP_KERNEL_ACCOUNT ,get_order (SEV_FW_BLOB_MAX_SIZE )); +if (!page) +return -ENOMEM ; + + map->host =page_address (page); + } + +return 0 ; +} + +static void free_snp_host_map (struct sev_device *sev) +{ +int i; + +for (i =0 ; i <MAX_SNP_HOST_MAP_BUFS ; i++) { +struct snp_host_map *map = &sev->snp_host_map[i]; + +if (map->host) { +__free_pages (virt_to_page (map->host),get_order (SEV_FW_BLOB_MAX_SIZE )); +memset (map,0 ,sizeof (*map)); + } + } +} + +static int map_firmware_writeable (u64 *paddr,u32 len,bool guest,struct snp_host_map *map) +{ +unsigned int npages =PAGE_ALIGN (len) >>PAGE_SHIFT ; + + map->active = false; + +if (!paddr || !len) +return 0 ; + + map->paddr = *paddr; + map->len = len; + +/* If paddr points to a guest memory then change the page state to firmwware. */ +if (guest) { +if (snp_set_rmp_state (*paddr, npages, true, true, false)) +return -EFAULT ; + + goto done; + } + +if (!map->host) +return -ENOMEM ; + +/* Check if the pre-allocated buffer can be used to fullfil the request. */ +if (len >SEV_FW_BLOB_MAX_SIZE ) +return -EINVAL ; + +/* Transition the pre-allocated buffer to the firmware state. */ +if (snp_set_rmp_state (__pa (map->host), npages, true, true, false)) +return -EFAULT ; + +/* Set the paddr to use pre-allocated firmware buffer */ + *paddr =__psp_pa (map->host); + +done: + map->active = true; +return 0 ; +} + +static int unmap_firmware_writeable (u64 *paddr,u32 len,bool guest,struct snp_host_map *map) +{ +unsigned int npages =PAGE_ALIGN (len) >>PAGE_SHIFT ; + +if (!map->active) +return 0 ; + +/* If paddr points to a guest memory then restore the page state to hypervisor. */ +if (guest) { +if (snp_set_rmp_state (*paddr, npages, false, true, true)) +return -EFAULT ; + + goto done; + } + +/* +* Transition the pre-allocated buffer to hypervisor state before the access. +* +* This is because while changing the page state to firmware, the kernel unmaps +* the pages from the direct map, and to restore the direct map we must +* transition the pages to shared state. +*/ +if (snp_set_rmp_state (__pa (map->host), npages, false, true, true)) +return -EFAULT ; + +/* Copy the response data firmware buffer to the callers buffer. */ +memcpy (__va (__sme_clr (map->paddr)), map->host,min_t (size_t, len, map->len)); + *paddr = map->paddr; + +done: + map->active = false; +return 0 ; +} + +static bool sev_legacy_cmd_buf_writable (int cmd) +{ +switch (cmd) { +case SEV_CMD_PLATFORM_STATUS : +case SEV_CMD_GUEST_STATUS : +case SEV_CMD_LAUNCH_START : +case SEV_CMD_RECEIVE_START : +case SEV_CMD_LAUNCH_MEASURE : +case SEV_CMD_SEND_START : +case SEV_CMD_SEND_UPDATE_DATA : +case SEV_CMD_SEND_UPDATE_VMSA : +case SEV_CMD_PEK_CSR : +case SEV_CMD_PDH_CERT_EXPORT : +case SEV_CMD_GET_ID : +case SEV_CMD_ATTESTATION_REPORT : +return true; +default : +return false; + } +} + +#define prep_buffer (name, addr, len, guest, map) \ + func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map) + +static int __snp_cmd_buf_copy (int cmd,void *cmd_buf,bool to_fw,int fw_err) +{ +int (*func)(u64 *paddr,u32 len,bool guest,struct snp_host_map *map); +struct sev_device *sev = psp_master->sev_data; +bool from_fw = !to_fw; + +/* +* After the command is completed, change the command buffer memory to +* hypervisor state. +* +* The immutable bit is automatically cleared by the firmware, so +* no not need to reclaim the page. +*/ +if (from_fw &&sev_legacy_cmd_buf_writable (cmd)) { +if (snp_set_rmp_state (__pa (cmd_buf),1 , false, true, false)) +return -EFAULT ; + +/* No need to go further if firmware failed to execute command. */ +if (fw_err) +return 0 ; + } + +if (to_fw) + func = map_firmware_writeable; +else + func = unmap_firmware_writeable; + +/* +* A command buffer may contains a system physical address. If the address +* points to a host memory then use an intermediate firmware page otherwise +* change the page state in the RMP table. +*/ +switch (cmd) { +case SEV_CMD_PDH_CERT_EXPORT : + if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address, + pdh_cert_len, false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address, + cert_chain_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_GET_ID : + if (prep_buffer(struct sev_data_get_id, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_PEK_CSR : + if (prep_buffer(struct sev_data_pek_csr, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_DATA : + if (prep_buffer(struct sev_data_launch_update_data, address, len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_VMSA : + if (prep_buffer(struct sev_data_launch_update_vmsa, address, len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_MEASURE : + if (prep_buffer(struct sev_data_launch_measure, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_LAUNCH_UPDATE_SECRET : + if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len, + true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_DBG_DECRYPT : + if (prep_buffer(struct sev_data_dbg, dst_addr, len, false, + &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_DBG_ENCRYPT : + if (prep_buffer(struct sev_data_dbg, dst_addr, len, true, + &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_ATTESTATION_REPORT : + if (prep_buffer(struct sev_data_attestation_report, address, len, + false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_SEND_START : + if (prep_buffer(struct sev_data_send_start, session_address, + session_len, false, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_SEND_UPDATE_DATA : + if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len, + false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_send_update_data, trans_address, + trans_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_SEND_UPDATE_VMSA : + if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len, + false, &sev->snp_host_map[0 ])) + goto err; + if (prep_buffer(struct sev_data_send_update_vmsa, trans_address, + trans_len, false, &sev->snp_host_map[1 ])) + goto err; +break ; +case SEV_CMD_RECEIVE_UPDATE_DATA : + if (prep_buffer(struct sev_data_receive_update_data, guest_address, + guest_len, true, &sev->snp_host_map[0 ])) + goto err; +break ; +case SEV_CMD_RECEIVE_UPDATE_VMSA : + if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address, + guest_len, true, &sev->snp_host_map[0 ])) + goto err; +break ; +default : +break ; + } + +/* The command buffer need to be in the firmware state. */ +if (to_fw &&sev_legacy_cmd_buf_writable (cmd)) { +if (snp_set_rmp_state (__pa (cmd_buf),1 , true, true, false)) +return -EFAULT ; + } + +return 0 ; + +err: +return -EINVAL ; +} + +static inline bool need_firmware_copy (int cmd) +{ +struct sev_device *sev = psp_master->sev_data; + +/* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */ +return ((cmd <SEV_CMD_SNP_INIT ) && sev->snp_inited) ? true : false; +} + +static int snp_aware_copy_to_firmware (int cmd,void *data) +{ +return __snp_cmd_buf_copy (cmd, data, true,0 ); +} + +static int snp_aware_copy_from_firmware (int cmd,void *data,int fw_err) +{ +return __snp_cmd_buf_copy (cmd, data, false, fw_err); +} +static int __sev_do_cmd_locked (int cmd,void *data,int *psp_ret) {struct psp_device *psp = psp_master;struct sev_device *sev;unsigned int phys_lsb, phys_msb;unsigned int reg, ret =0 ; +void *cmd_buf;int buf_len;if (!psp || !psp->sev_data)
@@ -342,12 +625,28 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret)* work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ -if (data) -memcpy (sev->cmd_buf, data, buf_len); +if (data) { +if (sev->cmd_buf_active >2 ) +return -EBUSY ; + + cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf; + +memcpy (cmd_buf, data, buf_len); + sev->cmd_buf_active++; + +/* +* The behavior of the SEV-legacy commands is altered when the +* SNP firmware is in the INIT state. +*/ +if (need_firmware_copy (cmd) &&snp_aware_copy_to_firmware (cmd, sev->cmd_buf)) +return -EFAULT ; + }else { + cmd_buf = sev->cmd_buf; + }/* Get the physical address of the command buffer */ - phys_lsb = data ?lower_32_bits (__psp_pa (sev->cmd_buf)) :0 ; - phys_msb = data ?upper_32_bits (__psp_pa (sev->cmd_buf)) :0 ; + phys_lsb = data ?lower_32_bits (__psp_pa (cmd_buf)) :0 ; + phys_msb = data ?upper_32_bits (__psp_pa (cmd_buf)) :0 ;dev_dbg (sev->dev,"sev command id %#x buffer 0x%08x%08x timeout %us\n" , cmd, phys_msb, phys_lsb, psp_timeout);
@@ -388,15 +687,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = -EIO ; } -print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, - buf_len, false); -/* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ -if (data) -memcpy (data, sev->cmd_buf, buf_len); +if (data) { +/* +* Restore the page state after the command completes. +*/ +if (need_firmware_copy (cmd) && +snp_aware_copy_from_firmware (cmd, cmd_buf, ret)) +return -EFAULT ; + +memcpy (data, cmd_buf, buf_len); + sev->cmd_buf_active--; + } + +print_hex_dump_debug ("(out): " ,DUMP_PREFIX_OFFSET ,16 ,2 , data, + buf_len, false);return ret; }
@@ -1271,10 +1579,12 @@ int sev_dev_init(struct psp_device *psp)if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages (dev,GFP_KERNEL ,0 ); + sev->cmd_buf = (void *)devm_get_free_pages (dev,GFP_KERNEL ,1 );if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf +PAGE_SIZE ; + psp->sev_data = sev; sev->dev = dev;
@@ -1327,6 +1637,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) sev_es_tmr =NULL ; } +/* +* The host map need to clear the immutable bit so it must be free'd before the +* SNP firmware shutdown. +*/ +free_snp_host_map (sev); +sev_snp_shutdown (NULL ); }
@@ -1391,6 +1707,14 @@ void sev_pci_init(void)dev_err (sev->dev,"SEV-SNP: failed to INIT error %#x\n" , error); } } + +/* +* Allocate the intermediate buffers used for the legacy command handling. +*/ +if (alloc_snp_host_map (sev)) { +dev_notice (sev->dev,"Failed to alloc host map (disabling legacy SEV)\n" ); + goto skip_legacy; + } }/* Obtain the TMR memory area for SEV-ES use */
@@ -1420,12 +1744,14 @@ void sev_pci_init(void)if (rc)dev_err (sev->dev,"SEV: failed to INIT error %#x\n" , error); +skip_legacy: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: +free_snp_host_map (sev); psp_master->sev_data =NULL ; }
drivers/crypto/ccp/sev-dev.h +12 -0
@@ -29,11 +29,20 @@#define SEV_CMDRESP_CMD_SHIFT 16#define SEV_CMDRESP_IOC BIT(0) +#define MAX_SNP_HOST_MAP_BUFS 2 +struct sev_misc_dev {struct kref refcount;struct miscdevice misc; }; +struct snp_host_map { +u64 paddr; +u32 len; +void *host; +bool active; +}; +struct sev_device {struct device *dev;struct psp_device *psp;
@@ -52,8 +61,11 @@ struct sev_device {u8 build;void *cmd_buf; +void *cmd_buf_backup; +int cmd_buf_active;bool snp_inited; +struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; };int sev_dev_init (struct psp_device *psp);
Signature · 1 line
2.17.1
PATCH v5 16/45 crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Brijesh SinghThe command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)
Documentation/virt/coco/sevguest.rst +27 -0
@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------
@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details.
drivers/crypto/ccp/sev-dev.c +45 -0
57 lines not on this page · raw message
include/uapi/linux/psp-sev.h +1 -0
@@ -28,6 +28,7 @@ enum {SEV_PEK_CERT_IMPORT ,SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 , +SNP_PLATFORM_STATUS , SEV_MAX, };
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
Quoted 78 lines · Brijesh Singh
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details.drivers/crypto/ccp/sev-dev.c +17
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) {
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1423
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 );
Calling snp_leak_pages() here seems wrong, because snp_set_rmp_state() calls snp_leak_pages() when it returns an error.
Quoted 44 lines
drivers/crypto/ccp/sev-dev.c +18
-0+return -EFAULT ; + } + +if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;include/uapi/linux/psp-sev.h +1
-1@@ -28,6 +28,7 @@ enum {SEV_PEK_CERT_IMPORT ,SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 , +SNP_PLATFORM_STATUS , SEV_MAX, }; --2.17.1
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
Quoted 83 lines · Marc Orr
Quoted 82 lines · Brijesh Singh
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details.drivers/crypto/ccp/sev-dev.c +22
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data);
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1423
Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5
-0+ ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 );Calling
snp_leak_pages()here seems wrong, becausesnp_set_rmp_state()callssnp_leak_pages()when it returns an error.
Agreed, i will fix in next rev.
Quoted 45 lines
Quoted 45 lines
drivers/crypto/ccp/sev-dev.c +18
-0+return -EFAULT ; + } + +if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;include/uapi/linux/psp-sev.h +1
-1@@ -28,6 +28,7 @@ enum {SEV_PEK_CERT_IMPORT ,SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 , +SNP_PLATFORM_STATUS , SEV_MAX, }; --2.17.1
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
In reply to Brijesh SinghQuoted 79 lines · * Brijesh Singh ([email protected]) wrote:
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details.drivers/crypto/ccp/sev-dev.c +19
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ;
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1425
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error); + +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 ); +return -EFAULT ; + }
Could we find a way of returning some of these errors into the output, rather than interepreting them from errno values?
Dave
Quoted 41 lines
drivers/crypto/ccp/sev-dev.c +15
-0+if (ret) + goto cleanup; + +if (copy_to_user ((void __user *)argp->data, data, +sizeof (struct sev_user_data_snp_status ))) + ret = -EFAULT ; + +cleanup: +__free_pages (status_page,0 ); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1445,6 +1487,9 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SEV_GET_ID2 : ret =sev_ioctl_do_get_id2 (&input);break ; +case SNP_PLATFORM_STATUS : + ret =sev_ioctl_snp_platform_status (&input); +break ;default : ret = -EINVAL ; goto out;include/uapi/linux/psp-sev.h +1
-0@@ -28,6 +28,7 @@ enum {SEV_PEK_CERT_IMPORT ,SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 , +SNP_PLATFORM_STATUS , SEV_MAX, };Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 16/45] crypto: ccp: Add the SNP_PLATFORM_STATUS command
Quoted 84 lines · Dr. David Alan Gilbert
Quoted 83 lines · * Brijesh Singh ([email protected]) wrote:
The command can be used by the userspace to query the SNP platform status report. See the SEV-SNP spec for more details.
Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 27 +++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 45 ++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 1 + 3 files changed, 73 insertions(+)Documentation/virt/coco/sevguest.rst +27
-0@@ -52,6 +52,22 @@ to execute due to the firmware error, then fw_err code will be set. __u64 fw_err; }; +The host ioctl should be called to /dev/sev device. The ioctl accepts command +id and command input structure. + +:: + struct sev_issue_cmd { + /* Command ID */ + __u32 cmd; + + /* Command request structure */ + __u64 data; + + /* firmware error code on failure (see psp-sev.h) */ + __u32 error; + }; + + 2.1 SNP_GET_REPORT ------------------@@ -107,3 +123,14 @@ length of the blob is lesser than expected then snp_ext_report_req.certs_len wil be updated with the expected value. See GHCB specification for further detail on how to parse the certificate blob. + +2.3 SNP_PLATFORM_STATUS +----------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_platform_status +:Returns (out): 0 on success, -negative on error + +The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The +status includes API major, minor version and more. See the SEV-SNP +specification for further details.drivers/crypto/ccp/sev-dev.c +23
-0@@ -1394,6 +1394,48 @@ static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable)return ret; } +static int sev_ioctl_snp_platform_status (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_data_snp_platform_status_buf buf; +struct page *status_page; +void *data; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + + status_page =alloc_page (GFP_KERNEL_ACCOUNT ); +if (!status_page) +return -ENOMEM ; + + data =page_address (status_page); +if (snp_set_rmp_state (__pa (data),1 , true, true, false)) { +__free_pages (status_page,0 ); +return -EFAULT ; + } + + buf.status_paddr =__psp_pa (data); + ret =__sev_do_cmd_locked (SEV_CMD_SNP_PLATFORM_STATUS , &buf, &argp->error);
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_pdh_export(struct sev_issue_cmd *argp, bool writable) · line 1425
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +6
-0+ +/* Change the page state before accessing it */ +if (snp_set_rmp_state (__pa (data),1 , false, true, true)) { +snp_leak_pages (__pa (data) >>PAGE_SHIFT ,1 ); +return -EFAULT ; + }Could we find a way of returning some of these errors into the output, rather than interepreting them from errno values?
I can try to find a corresponding fw error code and use it where applicable. e.g in this case I can use the SEV_RET_INVALID_PAGE_STATE.
thanks
PATCH v5 17/45 crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Brijesh SinghThe SEV-SNP firmware provides the SNP_CONFIG command used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.
Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.
In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the SNP_SET_EXT_CONFIG command to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.
While it is not defined in the spec, but let's add SNP_GET_EXT_CONFIG command that can be used to obtain the data programmed through the SNP_SET_EXT_CONFIG.
--- Documentation/virt/coco/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)
Documentation/virt/coco/sevguest.rst +28 -0
@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. + +2.4 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.
drivers/crypto/ccp/sev-dev.c +115 -0
133 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.h +3 -0
@@ -66,6 +66,9 @@ struct sev_device {bool snp_inited;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);
include/uapi/linux/psp-sev.h +17 -0
@@ -29,6 +29,8 @@ enum {SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 ,SNP_PLATFORM_STATUS , +SNP_SET_EXT_CONFIG , +SNP_GET_EXT_CONFIG , SEV_MAX, };
@@ -190,6 +192,21 @@ struct sev_user_data_snp_config {__u8 rsvd[52 ]; } __packed; +/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when +* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +}; +/** * struct sev_issue_cmd - SEV ioctl parameters *
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 6 lines · Brijesh Singh
+2.4
SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): structsev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on error
Documentation/virt/coco/sevguest.rst See GHCB specification for further detail on how to parse the certificate blob. · line 153
Quoted 10 lines
Documentation/virt/coco/sevguest.rst +10
-0+ +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message).
Hi Brijesh,
Just to be clear, is the documentation you're referring to regarding the layout of the certificate blob specified on page 47 of the GHCB spec? More specifically, is it the struct cert_table on that page?
https://developer.amd.com/wp-content/resources/56421.pdf
If so, where is the VCEK certificate layout documented?
Connor
Quoted 14 lines
include/uapi/linux/psp-sev.h +14
-0+/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when +* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +};
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 19 lines · Connor Kuehl
Quoted 6 lines · Brijesh Singh
+2.4
SNP_SET_EXT_CONFIG
+----------------------
+:Technology: sev-snp
+:Type: hypervisor ioctl cmd
+:Parameters (in): structsev_data_snp_ext_config
+:Returns (out): 0 on success, -negative on errorDocumentation/virt/coco/sevguest.rst See GHCB specification for further detail on how to parse the certificate blob. · line 153
Quoted 10 lines
Documentation/virt/coco/sevguest.rst +10
-0+ +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message).Hi Brijesh,
Quoted 3 lines
Just to be clear, is the documentation you're referring to regarding the layout of the certificate blob specified on page 47 of the GHCB spec? More specifically, is it the
struct cert_tableon that page?
Yes that is correct.
Quoted 4 lines
If so, where is the VCEK certificate layout documented?
You can get the VCEK from the KDS using the chip id. The certificate is standard X.509.
thanks
Quoted 17 lines
Connor
Quoted 14 lines
include/uapi/linux/psp-sev.h +14
-0+/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when +* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +};
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
In reply to Brijesh Singh`
Quoted 97 lines · Brijesh Singh
The SEV-SNP firmware provides the
SNP_CONFIGcommand used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.
In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the
SNP_SET_EXT_CONFIGcommand to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.While it is not defined in the spec, but let's add
SNP_GET_EXT_CONFIGcommand that can be used to obtain the data programmed through theSNP_SET_EXT_CONFIG.Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)Documentation/virt/coco/sevguest.rst +28
-0@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. + +2.4 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.drivers/crypto/ccp/sev-dev.c +18
-0@@ -1132,6 +1132,10 @@ static int __sev_snp_shutdown_locked(int *error)if (!sev->snp_inited)return 0 ; +/* Free the memory used for caching the certificate data */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +/* SHUTDOWN requires the DF_FLUSH */ wbinvd_on_all_cpus ();__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );@@ -1436,6 +1440,111 @@ static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_snp_get_config (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the TCB version programmed through the SET_CONFIG to userspace */ +if (input.config_address) {
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1466
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+if (copy_to_user ((void *__user )input.config_address, + &sev->snp_config,sizeof (struct sev_user_data_snp_config ))) +return -EFAULT ; + } + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */ +if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len;
This API to retrieve the length of the certs seems pretty odd. We only return the length if the input.certs_address is non-NULL. But if we know the length how did we allocate an address to write to input.certs_address?
Quoted 32 lines
drivers/crypto/ccp/sev-dev.c +32
-0+ + ret = -ENOSR ; + goto e_done; + } + +if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +static int sev_ioctl_snp_set_config (struct sev_issue_cmd *argp,bool writable) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input; +struct sev_user_data_snp_config config; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ;
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1508
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the certs from userspace */ +if (input.certs_address) { +if (!input.certs_len || !IS_ALIGNED (input.certs_len,PAGE_SIZE )) +return -EINVAL ; + + certs =psp_copy_user_blob (input.certs_address, input.certs_len);
Is psp_copy_user_blob() implemented in this patch series? When I searched through the patches, I only found an implementation that always returns an error. But maybe I missed the implementation?
Also, out of curiosity, any reason we cannot use copy_from_user here?
Quoted 43 lines
drivers/crypto/ccp/sev-dev.c +39
-0+if (IS_ERR (certs)) +return PTR_ERR (certs); + } + +/* Issue the PSP command to update the TCB version using the SNP_CONFIG. */ +if (input.config_address) { +if (copy_from_user (&config, + (void __user *)input.config_address,sizeof (config))) { + ret = -EFAULT ; + goto e_free; + } + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_CONFIG , &config, &argp->error); +if (ret) + goto e_free; + +memcpy (&sev->snp_config, &config,sizeof (config)); + } + +/* +* If the new certs are passed then cache it else free the old certs. +*/ +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } + +return 0 ; + +e_free: +kfree (certs); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;
drivers/crypto/ccp/sev-dev.c static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) · line 1607
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c +6
-0@@ -1490,6 +1599,12 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SNP_PLATFORM_STATUS : ret =sev_ioctl_snp_platform_status (&input);break ; +case SNP_SET_EXT_CONFIG : + ret =sev_ioctl_snp_set_config (&input, writable); +break ; +case SNP_GET_EXT_CONFIG : + ret =sev_ioctl_snp_get_config (&input); +break ;
What is the intended use of SNP_GET_EXT_CONFIG. Yes, I get that it returns the "EXT config" previously set via SNP_SET_EXT_CONFIG. But presumably the caller can keep track of what it's previously passed to SNP_SET_EXT_CONFIG. Does it really need to call into the kernel to get these certs?
Quoted 55 lines
drivers/crypto/ccp/sev-dev.c +0
-0default : ret = -EINVAL ; goto out;drivers/crypto/ccp/sev-dev.h +3
-0@@ -66,6 +66,9 @@ struct sev_device {bool snp_inited;struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS ]; +void *snp_certs_data; +u32 snp_certs_len; +struct sev_user_data_snp_config snp_config; };int sev_dev_init (struct psp_device *psp);include/uapi/linux/psp-sev.h +17
-1@@ -29,6 +29,8 @@ enum {SEV_GET_ID ,/* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2 ,SNP_PLATFORM_STATUS , +SNP_SET_EXT_CONFIG , +SNP_GET_EXT_CONFIG , SEV_MAX, };@@ -190,6 +192,21 @@ struct sev_user_data_snp_config {__u8 rsvd[52 ]; } __packed; +/** +* struct sev_data_snp_ext_config - system wide configuration value for SNP. +* +* @config_address: address of the struct sev_user_data_snp_config or 0 when +* reported_tcb does not need to be updated. +* @certs_address: address of extended guest request certificate chain or +* 0 when previous certificate should be removed on SNP_SET_EXT_CONFIG. +* @certs_len: length of the certs +*/ +struct sev_user_data_ext_snp_config { +__u64 config_address;/* In */ +__u64 certs_address;/* In */ +__u32 certs_len;/* In */ +}; +/** * struct sev_issue_cmd - SEV ioctl parameters * -- 2.17.1
Re: [PATCH Part2 v5 17/45] crypto: ccp: Add the SNP_{SET,GET}_EXT_CONFIG command
Quoted 105 lines · Marc Orr
`
Quoted 102 lines · Brijesh Singh
The SEV-SNP firmware provides the
SNP_CONFIGcommand used to set the system-wide configuration value for SNP guests. The information includes the TCB version string to be reported in guest attestation reports.Version 2 of the GHCB specification adds an NAE (SNP extended guest request) that a guest can use to query the reports that include additional certificates.
In both cases, userspace provided additional data is included in the attestation reports. The userspace will use the
SNP_SET_EXT_CONFIGcommand to give the certificate blob and the reported TCB version string at once. Note that the specification defines certificate blob with a specific GUID format; the userspace is responsible for building the proper certificate blob. The ioctl treats it an opaque blob.While it is not defined in the spec, but let's add
SNP_GET_EXT_CONFIGcommand that can be used to obtain the data programmed through theSNP_SET_EXT_CONFIG.Signed-off-by Brijesh Singh <[email protected]>--- Documentation/virt/coco/sevguest.rst | 28 +++++++ drivers/crypto/ccp/sev-dev.c | 115 +++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.h | 3 + include/uapi/linux/psp-sev.h | 17 ++++ 4 files changed, 163 insertions(+)Documentation/virt/coco/sevguest.rst +28
-0@@ -134,3 +134,31 @@ See GHCB specification for further detail on how to parse the certificate blob. The SNP_PLATFORM_STATUS command is used to query the SNP platform status. The status includes API major, minor version and more. See the SEV-SNP specification for further details. + +2.4 SNP_SET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to set the system-wide configuration such as +reported TCB version in the attestation report. The command is similar to +SNP_CONFIG command defined in the SEV-SNP spec. The main difference is the +command also accepts an additional certificate blob defined in the GHCB +specification. + +If the certs_address is zero, then previous certificate blob will deleted. +For more information on the certificate blob layout, see the GHCB spec +(extended guest request message). + + +2.4 SNP_GET_EXT_CONFIG +---------------------- +:Technology: sev-snp +:Type: hypervisor ioctl cmd +:Parameters (in): struct sev_data_snp_ext_config +:Returns (out): 0 on success, -negative on error + +The SNP_SET_EXT_CONFIG is used to query the system-wide configuration set +through the SNP_SET_EXT_CONFIG.drivers/crypto/ccp/sev-dev.c +24
-0@@ -1132,6 +1132,10 @@ static int __sev_snp_shutdown_locked(int *error)if (!sev->snp_inited)return 0 ; +/* Free the memory used for caching the certificate data */ +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; +/* SHUTDOWN requires the DF_FLUSH */ wbinvd_on_all_cpus ();__sev_do_cmd_locked (SEV_CMD_SNP_DF_FLUSH ,NULL ,NULL );@@ -1436,6 +1440,111 @@ static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp)return ret; } +static int sev_ioctl_snp_get_config (struct sev_issue_cmd *argp) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input; +int ret; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the TCB version programmed through the SET_CONFIG to userspace */ +if (input.config_address) { +if (copy_to_user ((void *__user )input.config_address, + &sev->snp_config,sizeof (struct sev_user_data_snp_config ))) +return -EFAULT ; + } + +/* Copy the extended certs programmed through the SNP_SET_CONFIG */
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1466
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +4
-0+if (input.certs_address && sev->snp_certs_data) { +if (input.certs_len < sev->snp_certs_len) { +/* Return the certs length to userspace */ + input.certs_len = sev->snp_certs_len;This API to retrieve the length of the certs seems pretty odd. We only return the length if the input.certs_address is non-NULL. But if we know the length how did we allocate an address to write to
input.certs_address?
Ah good point, I should provide an option to query the length when input.cert_address == 0. This will make it much cleaner that there are two approaches to get the length.
Quoted 46 lines
Quoted 32 lines
drivers/crypto/ccp/sev-dev.c +32
-0+ + ret = -ENOSR ; + goto e_done; + } + +if (copy_to_user ((void *__user )input.certs_address, + sev->snp_certs_data, sev->snp_certs_len)) +return -EFAULT ; + } + + ret =0 ; + +e_done: +if (copy_to_user ((void __user *)argp->data, &input,sizeof (input))) + ret = -EFAULT ; + +return ret; +} + +static int sev_ioctl_snp_set_config (struct sev_issue_cmd *argp,bool writable) +{ +struct sev_device *sev = psp_master->sev_data; +struct sev_user_data_ext_snp_config input; +struct sev_user_data_snp_config config; +void *certs =NULL ; +int ret =0 ; + +if (!sev->snp_inited || !argp->data) +return -EINVAL ; + +if (!writable) +return -EPERM ;drivers/crypto/ccp/sev-dev.c static int sev_ioctl_snp_platform_status(struct sev_issue_cmd *argp) · line 1508
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ +if (copy_from_user (&input, (void __user *)argp->data,sizeof (input))) +return -EFAULT ; + +/* Copy the certs from userspace */ +if (input.certs_address) { +if (!input.certs_len || !IS_ALIGNED (input.certs_len,PAGE_SIZE )) +return -EINVAL ; + + certs =psp_copy_user_blob (input.certs_address, input.certs_len);Is
psp_copy_user_blob()implemented in this patch series? When I searched through the patches, I only found an implementation that always returns an error. But maybe I missed the implementation?
Quoted 1 line
Also, out of curiosity, any reason we cannot use
copy_from_userhere?
psp_copy_user_blob() is a wrapper around memdup_user() -- which does kmalloc() + copy_to_user(). The wrapper performs some additional checks such as the blob size etc, we limit the blob size to 16K to avoid copying a random large data from userspace.
Quoted 49 lines
Quoted 49 lines
drivers/crypto/ccp/sev-dev.c +42
-0+if (IS_ERR (certs)) +return PTR_ERR (certs); + } + +/* Issue the PSP command to update the TCB version using the SNP_CONFIG. */ +if (input.config_address) { +if (copy_from_user (&config, + (void __user *)input.config_address,sizeof (config))) { + ret = -EFAULT ; + goto e_free; + } + + ret =__sev_do_cmd_locked (SEV_CMD_SNP_CONFIG , &config, &argp->error); +if (ret) + goto e_free; + +memcpy (&sev->snp_config, &config,sizeof (config)); + } + +/* +* If the new certs are passed then cache it else free the old certs. +*/ +if (certs) { +kfree (sev->snp_certs_data); + sev->snp_certs_data = certs; + sev->snp_certs_len = input.certs_len; + }else { +kfree (sev->snp_certs_data); + sev->snp_certs_data =NULL ; + sev->snp_certs_len =0 ; + } + +return 0 ; + +e_free: +kfree (certs); +return ret; +} +static long sev_ioctl (struct file *file,unsigned int ioctl,unsigned long arg) {void __user *argp = (void __user *)arg;@@ -1490,6 +1599,12 @@ static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg)case SNP_PLATFORM_STATUS : ret =sev_ioctl_snp_platform_status (&input);break ; +case SNP_SET_EXT_CONFIG : + ret =sev_ioctl_snp_set_config (&input, writable); +break ;
Quoted 8 lines
drivers/crypto/ccp/sev-dev.c static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) · line 1607
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+case SNP_GET_EXT_CONFIG : + ret =sev_ioctl_snp_get_config (&input); +break ;What is the intended use of
SNP_GET_EXT_CONFIG. Yes, I get that it returns the "EXT config" previously set viaSNP_SET_EXT_CONFIG. But presumably the caller can keep track of what it's previously passed toSNP_SET_EXT_CONFIG. Does it really need to call into the kernel to get these certs?
This is mainly to help in the cases where the provisioning tools may not keep track of the programmed TCB version and would prefer to read the TCB version before updating.
PATCH v5 18/45 crypto: ccp: Provide APIs to query extended attestation report
In reply to Brijesh SinghVersion 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the SNP_SET_EXT_CONFIG.
The snp_guest_ext_guest_request() will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.
--- drivers/crypto/ccp/sev-dev.c | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)
drivers/crypto/ccp/sev-dev.c +43 -0
@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h>
@@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_inited) +return -EINVAL ; + +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ; +if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +return rc; + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);
include/linux/psp-sev.h +24 -0
@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +*/ +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int
@@ -971,6 +988,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 18/45] crypto: ccp: Provide APIs to query extended attestation report
Quoted 41 lines · Brijesh Singh
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the
SNP_SET_EXT_CONFIG.The
snp_guest_ext_guest_request()will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)drivers/crypto/ccp/sev-dev.c +11
-0@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h> @@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; +
drivers/crypto/ccp/sev-dev.c int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) · line 1700
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+ sev = psp_master->sev_data; + +if (!sev->snp_inited) +return -EINVAL ; + +/* +* Check if there is enough space to copy the certificate chain. Otherwise +* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ;
Is this calculation for expected_npages correct? Assume that sev->snp_certs_len is less than a page (e.g., 2000). Then, this calculation will return 0 for expected_npages, rather than round up to 1.
Quoted 70 lines
drivers/crypto/ccp/sev-dev.c +22
-0+if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +return rc; + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +24
-1@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +*/ +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -971,6 +988,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ --2.17.1
Re: [PATCH Part2 v5 18/45] crypto: ccp: Provide APIs to query extended attestation report
Quoted 49 lines · Marc Orr
Quoted 48 lines · Brijesh Singh
Version 2 of the GHCB specification defines VMGEXIT that is used to get the extended attestation report. The extended attestation report includes the certificate blobs provided through the
SNP_SET_EXT_CONFIG.The
snp_guest_ext_guest_request()will be used by the hypervisor to get the extended attestation report. See the GHCB specification for more details.Signed-off-by Brijesh Singh <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 43 ++++++++++++++++++++++++++++++++++++ include/linux/psp-sev.h | 24 ++++++++++++++++++++ 2 files changed, 67 insertions(+)drivers/crypto/ccp/sev-dev.c +18
-0@@ -22,6 +22,7 @@#include <linux/firmware.h> #include <linux/gfp.h> #include <linux/cpufeature.h> +#include <linux/sev-guest.h> #include <asm/smp.h> @@ -1677,6 +1678,48 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages,unsigned long *fw_err) +{ +unsigned long expected_npages; +struct sev_device *sev; +int rc; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_inited) +return -EINVAL ; + +/* +* Check if there is enough space to copy the certificate chain. Otherwise
Quoted 9 lines
drivers/crypto/ccp/sev-dev.c int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) · line 1700
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +3
-0+* return ERROR code defined in the GHCB specification. +*/ + expected_npages = sev->snp_certs_len >>PAGE_SHIFT ;Is this calculation for
expected_npagescorrect? Assume thatsev->snp_certs_lenis less than a page (e.g., 2000). Then, this calculation will return0forexpected_npages, rather than round up to 1.
In patch 17 in sev_ioctl_snp_set_config there's a check that certs_len is aligned to PAGE_SIZE (and this value is later in that function assigned to sev->snp_certs_len):
+ /* Copy the certs from userspace */
+ if (input.certs_address) {
+ if (!input.certs_len || !IS_ALIGNED(input.certs_len, PAGE_SIZE))
+ return -EINVAL;
but I agree that rounding up (DIV_ROUND_UP) or asserting that sev->snp_certs_len is aligned to PAGE_SIZE (and non-zero) makes sense here.
-Dov
Quoted 70 lines
Quoted 70 lines
drivers/crypto/ccp/sev-dev.c +22
-0+if (*npages < expected_npages) { + *npages = expected_npages; + *fw_err =SNP_GUEST_REQ_INVALID_LEN ; +return -EINVAL ; + } + + rc =sev_do_cmd (SEV_CMD_SNP_GUEST_REQUEST , data, (int *)&fw_err); +if (rc) +return rc; + +/* Copy the certificate blob */ +if (sev->snp_certs_data) { + *npages = expected_npages; +memcpy ((void *)vaddr, sev->snp_certs_data, *npages <<PAGE_SHIFT ); + }else { + *npages =0 ; + } + +return rc; +} +EXPORT_SYMBOL_GPL (snp_guest_ext_guest_request); +static void sev_exit (struct kref *ref) {misc_deregister (&misc_dev->misc);include/linux/psp-sev.h +24
-1@@ -924,6 +924,23 @@ void *psp_copy_user_blob(u64 uaddr, u32 len);void *snp_alloc_firmware_page (gfp_t mask);void snp_free_firmware_page (void *addr); +/** +* snp_guest_ext_guest_request - perform the SNP extended guest request command +* defined in the GHCB specification. +* +* @data: the input guest request structure +* @vaddr: address where the certificate blob need to be copied. +* @npages: number of pages for the certificate blob. +* If the specified page count is less than the certificate blob size, then the +* required page count is returned with error code defined in the GHCB spec. +* If the specified page count is more than the certificate blob size, then +* page count is updated to reflect the amount of valid data copied in the +* vaddr. +*/ +int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *npages, +unsigned long *error); +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ static inline int@@ -971,6 +988,13 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)static inline void snp_free_firmware_page (void *addr) { } +static inline int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data, +unsigned long vaddr,unsigned long *n, +unsigned long *error) +{ +return -ENODEV ; +} +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ --2.17.1
PATCH v5 19/45 KVM: SVM: Add support to handle AP reset MSR protocol
In reply to Brijesh SinghFrom: Tom Lendacky <[email protected]>
Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification.
--- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 51 insertions(+), 8 deletions(-)
arch/x86/include/asm/sev-common.h +2 -0
@@ -56,6 +56,8 @@/* AP Reset Hold */ #define GHCB_MSR_AP_RESET_HOLD_REQ 0x006#define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0)/* GHCB GPA Register */ #define GHCB_MSR_REG_GPA_REQ 0x012
arch/x86/kvm/svm/sev.c +48 -8
85 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -188,6 +188,7 @@ struct vcpu_svm {struct ghcb *ghcb;struct kvm_host_map ghcb_map;bool received_first_sipi; +unsigned int ap_reset_hold_type;/* SEV-ES scratch area support */ void *ghcb_sa;
Signature · 1 line
2.17.1
PATCH v5 20/45 KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
In reply to Brijesh SinghVersion 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor.
Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version.
--- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/include/asm/sev-common.h +2 -0
@@ -97,6 +97,8 @@ enum psc_op {/* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080#define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0)#define GHCB_MSR_HV_FT_RESP_VAL (v) \/* GHCBData[63:12] */ \ (((u64)(v) & GENMASK_ULL(63, 12)) >> 12)
arch/x86/kvm/svm/sev.c +14 -0
@@ -2184,6 +2184,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_AP_HLT_LOOP :case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT : +case SVM_VMGEXIT_HV_FEATURES :break ;default : goto vmgexit_err;
@@ -2438,6 +2439,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)GHCB_MSR_INFO_MASK , GHCB_MSR_INFO_POS);break ; +case GHCB_MSR_HV_FT_REQ : { +set_ghcb_msr_bits (svm,GHCB_HV_FT_SUPPORTED , +GHCB_MSR_HV_FT_MASK ,GHCB_MSR_HV_FT_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_HV_FT_RESP , +GHCB_MSR_INFO_MASK ,GHCB_MSR_INFO_POS ); +break ; + }case GHCB_MSR_TERM_REQ : {u64 reason_set, reason_code;
@@ -2553,6 +2561,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_HV_FEATURES : { +ghcb_set_sw_exit_info_2 (ghcb,GHCB_HV_FT_SUPPORTED ); + + ret =1 ; +break ; + }case SVM_VMGEXIT_UNSUPPORTED_EVENT : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
arch/x86/kvm/svm/svm.h +2 -1
@@ -548,9 +548,10 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);/* sev.c */ -#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MAX 2ULL#define GHCB_VERSION_MIN 1ULL +#define GHCB_HV_FT_SUPPORTED 0extern unsigned int max_sev_asid;
Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH Part2 v5 20/45] KVM: SVM: Provide the Hypervisor Feature support VMGEXIT
Quoted 39 lines · Brijesh Singh
Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor.
Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version.
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/sev-common.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-)arch/x86/include/asm/sev-common.h +2
-0@@ -97,6 +97,8 @@ enum psc_op {/* GHCB Hypervisor Feature Request/Response */ #define GHCB_MSR_HV_FT_REQ 0x080#define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0)#define GHCB_MSR_HV_FT_RESP_VAL (v) \/* GHCBData[63:12] */ \ (((u64)(v) & GENMASK_ULL(63, 12)) >> 12)arch/x86/kvm/svm/sev.c +1
-0@@ -2184,6 +2184,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)case SVM_VMGEXIT_AP_HLT_LOOP :case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT : +case SVM_VMGEXIT_HV_FEATURES :break ;default : goto vmgexit_err;
arch/x86/kvm/svm/sev.c static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) · line 2442
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2438,6 +2439,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)GHCB_MSR_INFO_MASK , GHCB_MSR_INFO_POS);break ; +case GHCB_MSR_HV_FT_REQ : {
Unnecessary braces.
Quoted 10 lines
arch/x86/kvm/svm/sev.c +6
-0+set_ghcb_msr_bits (svm,GHCB_HV_FT_SUPPORTED , +GHCB_MSR_HV_FT_MASK ,GHCB_MSR_HV_FT_POS ); +set_ghcb_msr_bits (svm,GHCB_MSR_HV_FT_RESP , +GHCB_MSR_INFO_MASK ,GHCB_MSR_INFO_POS ); +break ; + }case GHCB_MSR_TERM_REQ : {u64 reason_set, reason_code;
arch/x86/kvm/svm/sev.c int sev_handle_vmgexit(struct kvm_vcpu *vcpu) · line 2564
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2553,6 +2561,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret =1 ;break ; } +case SVM_VMGEXIT_HV_FEATURES : {
Same here.
Quoted 8 lines
arch/x86/kvm/svm/sev.c +5
-0+ghcb_set_sw_exit_info_2 (ghcb,GHCB_HV_FT_SUPPORTED ); + + ret =1 ; +break ; + }case SVM_VMGEXIT_UNSUPPORTED_EVENT : vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n",
PATCH v5 21/45 KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghImplement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/kvm/lapic.c +4 -1
@@ -2457,7 +2457,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);
arch/x86/kvm/svm/sev.c +35 -0
@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } } + +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (__pfn_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}
arch/x86/kvm/svm/svm.c +14 -2
@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;
@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;
@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,
@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +1 -0
@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.
Can you explain what 'globally enabled' means?
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Dave
Quoted 170 lines
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);arch/x86/kvm/svm/sev.c +35
-0@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } } + +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (__pfn_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the SNP_INIT to initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).
The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
thanks
Quoted 173 lines
Dave
Quoted 170 lines
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);arch/x86/kvm/svm/sev.c +35
-0@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } } + +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (__pfn_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 23 lines · * Brijesh Singh ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
Quoted 8 lines
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
So I think that means we don't need to backport this to older kernels that don't know about SNP but might run on SNP enabled hardware (1), since those kernels won't do step2.
Dave
Quoted 176 lines
thanks
Quoted 173 lines
Dave
Quoted 170 lines
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);arch/x86/kvm/svm/sev.c +35
-0@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } } + +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (__pfn_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 24 lines · Dr. David Alan Gilbert
Quoted 23 lines · * Brijesh Singh ([email protected]) wrote:
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
Quoted 8 lines
Quoted 5 lines
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
So I think that means we don't need to backport this to older kernels that don't know about SNP but might run on SNP enabled hardware (1), since those kernels won't do step2.
Correct.
thanks
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghQuoted 24 lines · Brijesh Singh
Quoted 9 lines · Dr. David Alan Gilbert
Quoted 7 lines · * Brijesh Singh ([email protected]) wrote:
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
When SEV-SNP is globally enabled, the CPU marks the VMCB, VMSA, and AVIC
backing pages as "in-use" in the RMP after a successful VMRUN. This is
done for _all_ VMs, not just SNP-Active VMs.Can you explain what 'globally enabled' means?
This means that SNP is enabled in host SYSCFG_MSR.Snp=1. Once its enabled then RMP checks are enforced.
Quoted 2 lines
Or more specifically, can we trip this bug on public hardware that has the SNP enabled in the bios, but no SNP init in the host OS?
Enabling the SNP support on host is 3 step process:
step1 (bios): reserve memory for the RMP table.
Quoted 8 lines
step2 (host): initialize the RMP table memory, set the SYSCFG msr to enable the SNP feature
step3 (host): call the
SNP_INITto initialize the SNP firmware (this is needed only if you ever plan to launch SNP guest from this host).The "SNP globally enabled" means the step 1 to 2. The RMP checks are enforced as soon as step 2 is completed.
It might be good to update the code to reflect this reply, such that the new snp_safe_alloc_page() function introduced in this patch only deviates from "normal" page allocation logic when these two conditions are met. We could introduce a global variable, snp_globally_enabled that defaults to false and gets set to true after we write the SYSCFG MSR.
Quoted 76 lines
thanks
Quoted 74 lines
Dave
Quoted 71 lines
If the hypervisor accesses an in-use page through a writable translation, the CPU will throw an RMP violation #PF. On early SNP hardware, if an in-use page is 2mb aligned and software accesses any part of the associated 2mb region with a hupage, the CPU will incorrectly treat the entire 2mb region as in-use and signal a spurious RMP violation #PF.
The recommended is to not use the hugepage for the VMCB, VMSA or AVIC backing page. Add a generic allocator that will ensure that the page returns is not hugepage (2mb or 1gb) and is safe to be used when SEV-SNP is enabled.
Co-developed-by Marc Orr <[email protected]>Signed-off-by Marc Orr <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 35 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+), 3 deletions(-)arch/x86/include/asm/kvm-x86-ops.h +1
-0@@ -122,6 +122,7 @@ KVM_X86_OP_NULL(enable_direct_tlbflush) KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) +KVM_X86_OP(alloc_apic_backing_page)#undef KVM_X86_OP#undef KVM_X86_OP_NULLarch/x86/include/asm/kvm_host.h +1
-0@@ -1453,6 +1453,7 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {arch/x86/kvm/lapic.c +4
-1@@ -2457,7 +2457,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT ); +if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs =static_call (kvm_x86_alloc_apic_backing_page)(vcpu); +else + apic->regs = (void *)get_zeroed_page (GFP_KERNEL_ACCOUNT );if (!apic->regs) {printk (KERN_ERR "malloc apic regs error for vcpu %x\n" , vcpu->vcpu_id);arch/x86/kvm/svm/sev.c +0
-0@@ -2703,3 +2703,38 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)break ; } }
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Continuing my other comment, above: if we introduce a snp_globally_enabled var, we could use that here, rather than cpu_feature_enabled(X86_FEATURE_SEV_SNP).
Quoted 91 lines
Quoted 91 lines
Quoted 91 lines
arch/x86/kvm/svm/sev.c +27
-0+ +/* +* Allocate an SNP safe page to workaround the SNP erratum where +* the CPU will incorrectly signal an RMP violation #PF if a +* hugepage (2mb or 1gb) collides with the RMP entry of VMCB, VMSA +* or AVIC backing page. The recommeded workaround is to not use the +* hugepage. +* +* Allocate one extra page, use a page which is not 2mb aligned +* and free the other. +*/ + p =alloc_pages (GFP_KERNEL_ACCOUNT | __GFP_ZERO,1 ); +if (!p) +return NULL ; + +split_page (p,1 ); + + pfn =page_to_pfn (p); +if (IS_ALIGNED (__pfn_to_phys (pfn),PMD_SIZE )) { + pfn++; +__free_page (p); + }else { +__free_page (pfn_to_page (pfn +1 )); + } + +return pfn_to_page (pfn); +}arch/x86/kvm/svm/svm.c +14
-2@@ -1368,7 +1368,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); err = -ENOMEM ; - vmcb01_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page =snp_safe_alloc_page (vcpu);if (!vmcb01_page) goto out;@@ -1377,7 +1377,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page =snp_safe_alloc_page (vcpu);if (!vmsa_page) goto error_free_vmcb_page;@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, };static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-1@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ --2.17.1
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 11 lines · Marc Orr
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);Continuing my other comment, above: if we introduce a
snp_globally_enabledvar, we could use that here, rather thancpu_feature_enabled(X86_FEATURE_SEV_SNP).
Maybe I am missing something, what is wrong with cpu_feature_enabled(...) check ? It's same as creating a global variable. The feature enabled bit is not set if the said is not enabled. See the patch #3 [1] in this series.
[1]
https://lore.kernel.org/linux-mm/[email protected]/T/#m2ac1242b33abfcd0d9fb22a89f4c103eacf67ea7
thanks
Re: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
Quoted 22 lines · Brijesh Singh
Quoted 11 lines · Marc Orr
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/svm/sev.c void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) · line 2713
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+ +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu) +{ +unsigned long pfn; +struct page *p; + +if (!cpu_feature_enabled (X86_FEATURE_SEV_SNP )) +return alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);Continuing my other comment, above: if we introduce a
snp_globally_enabledvar, we could use that here, rather thancpu_feature_enabled(X86_FEATURE_SEV_SNP).Maybe I am missing something, what is wrong with
cpu_feature_enabled(...)check ? It's same as creating a global variable. The feature enabled bit is not set if the said is not enabled. See the patch #3 [1] in this series.
Quoted 4 lines
[1]
https://lore.kernel.org/linux-mm/[email protected]/T/#m2ac1242b33abfcd0d9fb22a89f4c103eacf67ea7thanks
You are right. Patch #3 does exactly what I was asking for in snp_rmptable_init(). Thanks!
Sean Christopherson
maintainerRe: [PATCH Part2 v5 21/45] KVM: SVM: Make AVIC backing, VMSA and VMCB memory allocation SNP safe
In reply to Brijesh SinghQuoted 3 lines · Brijesh Singh
Implement a workaround for an SNP erratum where the CPU will incorrectly signal an RMP violation #PF if a hugepage (2mb or 1gb) collides with the RMP entry of a VMCB, VMSA or AVIC backing page.
...
Quoted 18 lines
arch/x86/kvm/svm/svm.c +10
-0@@ -4539,6 +4539,16 @@ static int svm_vm_init(struct kvm *kvm)return 0 ; } +static void *svm_alloc_apic_backing_page (struct kvm_vcpu *vcpu) +{ +struct page *page =snp_safe_alloc_page (vcpu); + +if (!page) +return NULL ; + +return page_address (page); +} +static struct kvm_x86_ops svm_x86_ops__initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable,
arch/x86/kvm/svm/svm.c static struct kvm_x86_ops svm_x86_ops __initdata = { · line 4681
Quoted 5 lines
arch/x86/kvm/svm/svm.c +2
-0@@ -4667,6 +4677,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page,
IMO, this should be guarded by a module param or X86_BUG_* to make it clear that this is a bug and not working as intended.
And doesn't the APIC page need these shenanigans iff AVIC is enabled? (the module param, not necessarily in the VM)
Quoted 18 lines
arch/x86/kvm/svm/svm.c +0
-0};static struct kvm_x86_init_ops svm_init_ops__initdata = {arch/x86/kvm/svm/svm.h +1
-0@@ -575,6 +575,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm); +struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu);/* vmenter.S */ Signature · 1 line
2.17.1
PATCH v5 22/45 KVM: SVM: Add initial SEV-SNP support
In reply to Brijesh SinghThe next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment.
The SNP feature is added incrementally, the later patches adds a new module parameters that can be used to enabled SEV-SNP in the KVM.
--- arch/x86/kvm/svm/sev.c | 10 +++++++++- arch/x86/kvm/svm/svm.h | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +9 -1
@@ -58,6 +58,9 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444);#define sev_es_enabled false#endif /* CONFIG_KVM_AMD_SEV */ +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled; +#define AP_RESET_HOLD_NONE 0#define AP_RESET_HOLD_NAE_EVENT 1#define AP_RESET_HOLD_MSR_PROTO 2
@@ -1836,6 +1839,7 @@ void __init sev_hardware_setup(void) {#ifdef CONFIG_KVM_AMD_SEV unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; +bool sev_snp_supported = false;bool sev_es_supported = false;bool sev_supported = false;
@@ -1896,12 +1900,16 @@ void __init sev_hardware_setup(void)if (misc_cg_set_capacity (MISC_CG_RES_SEV_ES , sev_es_asid_count)) goto out; -pr_info ("SEV-ES supported: %u ASIDs\n" , sev_es_asid_count); sev_es_supported = true; + sev_snp_supported = sev_snp_enabled &&cpu_feature_enabled (X86_FEATURE_SEV_SNP ); + +pr_info ("SEV-ES %ssupported: %u ASIDs\n" , + sev_snp_supported ?"and SEV-SNP " :"" , sev_es_asid_count); out: sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported;#endif }
arch/x86/kvm/svm/svm.h +8 -0
@@ -72,6 +72,7 @@ enum {struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ +bool snp_active;/* SEV-SNP enabled guest */ unsigned int asid;/* ASID used for this guest */ unsigned int handle;/* SEV firmware handle */ int fd;/* SEV device fd */
@@ -246,6 +247,13 @@ static inline bool sev_es_guest(struct kvm *kvm)#endif } +static inline bool sev_snp_guest (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; + +return sev_es_guest (kvm) && sev->snp_active; +} +static inline void vmcb_mark_all_dirty (struct vmcb *vmcb) { vmcb->control.clean =0 ;
Signature · 1 line
2.17.1
PATCH v5 23/45 KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghThe KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT.
The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)
Documentation/virt/kvm/amd-memory-encryption.rst +27 -0
@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========
arch/x86/include/asm/svm.h +2 -0
@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;
arch/x86/kvm/svm/sev.c +42 -2
76 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +4 -0
@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */
@@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {
include/uapi/linux/kvm.h +13 -0
@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };
@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Hi Brijesh,
Quoted 40 lines · Brijesh Singh
The
KVM_SNP_INITcommand is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of theKVM_SEV_INITorKVM_SEV_ES_INIT.The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +13
-0@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init {
Documentation/virt/kvm/amd-memory-encryption.rst issued by the hypervisor to make the guest ready for execution. · line 452
Quoted 10 lines
Documentation/virt/kvm/amd-memory-encryption.rst +10
-0+ __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1)
Typo in these flags: s/INJET/INJECT/
Also in the actual .h file below.
-Dov
Quoted 158 lines
Documentation/virt/kvm/amd-memory-encryption.rst +4
-0+ +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +4
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {include/uapi/linux/kvm.h +13
-0@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Hi Dov,
Quoted 8 lines · Dov Murik
+ /* enable the restricted injection */
Documentation/virt/kvm/amd-memory-encryption.rst issued by the hypervisor to make the guest ready for execution. · line 452
Quoted 4 lines
Documentation/virt/kvm/amd-memory-encryption.rst +4
-0+ #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1)Typo in these flags: s/INJET/INJECT/
Also in the actual .h file below.
Noted. thanks
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghQuoted 170 lines · Brijesh Singh
The
KVM_SNP_INITcommand is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of theKVM_SEV_INITorKVM_SEV_ES_INIT.The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +3
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 87
Quoted 4 lines
arch/x86/kvm/svm/svm.h +1
-0@@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags;
This field never gets set anywhere. Should it get set in verify_snp_init_flags()?
Quoted 38 lines
arch/x86/kvm/svm/svm.h +0
-0};struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 163 lines · Marc Orr
Quoted 162 lines · Brijesh Singh
The
KVM_SNP_INITcommand is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of theKVM_SEV_INITorKVM_SEV_ES_INIT.The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +42
-2@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +0
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2))
Quoted 13 lines
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 87
Quoted 11 lines
arch/x86/kvm/svm/svm.h +4
-0+/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags;This field never gets set anywhere. Should it get set in
verify_snp_init_flags()?
Actually the supported flag value is zero, so didn't update it. But to make code cleaner I will set the flag after the negotiation.
Quoted 39 lines
Quoted 39 lines
arch/x86/kvm/svm/svm.h +0
-0};struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghQuoted 95 lines · Brijesh Singh
The
KVM_SNP_INITcommand is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of theKVM_SEV_INITorKVM_SEV_ES_INIT.The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +16
-0@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; +
arch/x86/kvm/svm/sev.c static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) · line 262
Quoted 10 lines
arch/x86/kvm/svm/sev.c +5
-1+return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;
Not sure if this is the patch place for this but I think you want to disallow svm_vm_copy_asid_from() if snp_active == true.
Quoted 107 lines
arch/x86/kvm/svm/sev.c +21
-1if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid; - ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error); + } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +4
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
In reply to Brijesh SinghQuoted 116 lines · Brijesh Singh
The
KVM_SNP_INITcommand is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of theKVM_SEV_INITorKVM_SEV_ES_INIT.The flags value must be zero, it will be extended in future SNP support to communicate the optional features (such as restricted INT injection etc).
Co-developed-by Pavan Kumar Paluri <[email protected]>Signed-off-by Pavan Kumar Paluri <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 27 ++++++++++++ arch/x86/include/asm/svm.h | 2 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++- arch/x86/kvm/svm/svm.h | 4 ++ include/uapi/linux/kvm.h | 13 ++++++ 5 files changed, 88 insertions(+), 2 deletions(-)Documentation/virt/kvm/amd-memory-encryption.rst +27
-0@@ -427,6 +427,33 @@ issued by the hypervisor to make the guest ready for execution. Returns: 0 on success, -negative on error +18. KVM_SNP_INIT +---------------- + +The KVM_SNP_INIT command can be used by the hypervisor to initialize SEV-SNP +context. In a typical workflow, this command should be the first command issued. + +Parameters (in/out): struct kvm_snp_init + +Returns: 0 on success, -negative on error + +:: + + struct kvm_snp_init { + __u64 flags; + }; + +The flags bitmap is defined as:: + + /* enable the restricted injection */ + #define KVM_SEV_SNP_RESTRICTED_INJET (1<<0) + + /* enable the restricted injection timer */ + #define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1<<1) + +If the specified flags is not supported then return -EOPNOTSUPP, and the supported +flags are returned. + References ==========arch/x86/include/asm/svm.h +2
-0@@ -218,6 +218,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_SEV_ENABLE BIT(1)#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +struct vmcb_seg {u16 selector;u16 attrib;arch/x86/kvm/svm/sev.c +22
-1@@ -235,10 +235,30 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)sev_decommission (handle); } +static int verify_snp_init_flags (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_snp_init params; +int ret =0 ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +if (params.flags & ~SEV_SNP_SUPPORTED_FLAGS ) + ret = -EOPNOTSUPP ; + + params.flags =SEV_SNP_SUPPORTED_FLAGS ; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; + +return ret; +} +static int sev_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) { +bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;
arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);
After SEV INIT_EX support patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail with SEV_DF_FLUSH required error. I tried calling SEV_DF_FLUSH right after the SNP platform init but this time it failed later on the SNP launch update command with SEV_RET_INVALID_PARAM error. Looks like there is another dependency on SEV platform initialization.
Calling sev_platform_init for SNP VMs fixes the problem in our tests.
Quoted 86 lines
arch/x86/kvm/svm/sev.c +12
-0+ } +if (ret) goto e_free;@@ -600,6 +630,10 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; +/* Enable the SEV-SNP feature */ +if (sev_snp_guest (svm->vcpu.kvm)) + save->sev_features |=SVM_SEV_FEAT_SNP_ACTIVE ; +return 0 ; }@@ -1532,6 +1566,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) }switch (sev_cmd.id) { +case KVM_SEV_SNP_INIT : +if (!sev_snp_enabled) { + r = -ENOTTY ; + goto out; + } + fallthrough;case KVM_SEV_ES_INIT :if (!sev_es_enabled) { r = -ENOTTY ;arch/x86/kvm/svm/svm.h +4
-0@@ -69,6 +69,9 @@ enum {/* TPR and CR2 are always written before VMRUN */ #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) +/* Supported init feature flags */ +#define SEV_SNP_SUPPORTED_FLAGS 0x0 +struct kvm_sev_info {bool active;/* SEV enabled guest */ bool es_active;/* SEV-ES enabled guest */ @@ -81,6 +84,7 @@ struct kvm_sev_info {u64 ap_jump_table;/* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ +u64 snp_init_flags; };struct kvm_svm {include/uapi/linux/kvm.h +13
-1@@ -1712,6 +1712,9 @@ enum sev_cmd_id {/* Guest Migration Extension */ KVM_SEV_SEND_CANCEL , +/* SNP specific commands */ +KVM_SEV_SNP_INIT , + KVM_SEV_NR_MAX, };@@ -1808,6 +1811,16 @@ struct kvm_sev_receive_update_data {__u32 trans_len; }; +/* enable the restricted injection */ +#define KVM_SEV_SNP_RESTRICTED_INJET (1 << 0) + +/* enable the restricted injection timer */ +#define KVM_SEV_SNP_RESTRICTED_TIMER_INJET (1 << 1) + +struct kvm_snp_init { +__u64 flags; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Hello Alper,
Quoted 36 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 17 lines
{arch/x86/kvm/svm/sev.c +3
-1+bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.
Trying to get some more context for this issue.
When you say after SEV_INIT_EX support patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ... sev_platform_init() code path has still not executed on the host BSP ?
Before launching the first SNP/SEV guest launch after INIT, we need to issue SEV_CMD_DF_FLUSH command.
I assume that we will always be initially doing SNP firmware initialization with SNP_INIT command followed by sev_platform_init(), if SNP is enabled on boot CPU.
Thanks, Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 40 lines · Ashish Kalra
Hello Alper,
Quoted 36 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 17 lines
{arch/x86/kvm/svm/sev.c +3
-1+bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.
Quoted 8 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?
Correct, INIT_EX requires the file system to be ready and there is a ccp module param to call it only when needed.
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Quoted 9 lines
Before launching the first SNP/SEV guest launch after INIT, we need to issue
SEV_CMD_DF_FLUSHcommand.I assume that we will always be initially doing SNP firmware initialization with
SNP_INITcommand followed bysev_platform_init(), if SNP is enabled on boot CPU.Thanks, Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 50 lines · Alper Gun
Quoted 39 lines · Ashish Kalra
Hello Alper,
Quoted 36 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 17 lines
{arch/x86/kvm/svm/sev.c +3
-1+bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.
Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
So the fix will be to call sev_platform_init() unconditionally here in sev_guest_init(), and both sev_snp_init() and sev_platform_init() are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.
Thanks, Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 60 lines · Ashish Kalra
Quoted 50 lines · Alper Gun
Quoted 39 lines · Ashish Kalra
Hello Alper,
Quoted 36 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 17 lines
{arch/x86/kvm/svm/sev.c +3
-1+bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret;if (kvm->created_vcpus)@@ -249,12 +269,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Quoted 7 lines
Ok, that makes sense.
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.
That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a DF_FLUSH command instead of calling sev_platform_init().
Quoted 3 lines
Thanks, Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; the arch/x86 maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 64 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{arch/x86/kvm/svm/sev.c +3
-1+bool es_active = (argp->id ==KVM_SEV_ES_INIT || argp->id + ==KVM_SEV_SNP_INIT );struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; -bool es_active = argp->id ==KVM_SEV_ES_INIT ; +bool snp_active = argp->id ==KVM_SEV_SNP_INIT ;int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@ staticint
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;arch/x86/kvm/svm/sev.c +1
-0+ sev->snp_active = snp_active; asid =sev_asid_new (sev);if (asid <0 ) goto e_no_asid; sev->asid = asid;arch/x86/kvm/svm/sev.c static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 285
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-1- ret =sev_platform_init (&argp->error); +if (snp_active) { + ret =verify_snp_init_flags (kvm, argp); +if (ret) + goto e_free; + + ret =sev_snp_init (&argp->error); + }else { + ret =sev_platform_init (&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().
Actually sev_platform_init() is already called on module init if psp_init_on_probe is not false. Only need to ensure that SNP firmware is initialized first with SNP_INIT command.
Thanks,
Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 82 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 69 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || argp->id + == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@ staticint
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.
Quoted 3 lines
Quoted 1 line
That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.
But if psp_init_on_probe is false, sev_platform_init() isn't called down this path. Alper has suggested we always call sev_platform_init() but we could just place an SEV_DF_FLUSH command instead. Or am I still missing something?
Quoted 3 lines
Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
[AMD Official Use Only - General]
Hello Alper, Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 11:30 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; the arch/x86 maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 107 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 64 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || + argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@ staticint
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().
Quoted 8 lines
Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.But if
psp_init_on_probeis false,sev_platform_init()isn't called down this path. Alper has suggested we always callsev_platform_init()but we could just place anSEV_DF_FLUSHcommand instead. Or am I still missing something?After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init.
Are you getting the DLFLUSH_REQUIRED error after the SNP activate command ?
Also did you use the SEV_DF_FLUSH command or the SNP_DF_FLUSH command ?
With SNP you need to use SNP_DF_FLUSH command.
Thanks,
Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Let me summarize what I tried.
1- when using psp_init_probe false, the SNP VM fails in SNP_LAUNCH_START step with error SEV_RET_DFFLUSH_REQUIRED(15). 2- added SEV_DF_FLUSH just after SNP platform init and it didn't fail in launch start but failed later during SNP_LAUNCH_UPDATE with SEV_RET_INVALID_PARAM(22)
3- added SNP_DF_FLUSH just after SNP platform init and it failed again during SNP_LAUNCH_UPDATE with SEV_RET_INVALID_PARAM(22)
4- added sev_platform_init for SNP VMs and it worked.
For me DF_FLUSH alone didn' help boot a VM. I don't know yet why sev platform status impacts the SNP VM, but sev_platform_init fixes the problem.
Quoted 137 lines · Ashish
[AMD Official Use Only - General]
Hello Alper, Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 11:30 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 107 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 64 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || + argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@ staticint
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but this time it failed later on the SNP launch update command withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().Quoted 8 lines
Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.But if
psp_init_on_probeis false,sev_platform_init()isn't called down this path. Alper has suggested we always callsev_platform_init()but we could just place anSEV_DF_FLUSHcommand instead. Or am I still missing something?After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init.Are you getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?Also did you use the
SEV_DF_FLUSHcommand or theSNP_DF_FLUSHcommand ?With SNP you need to use
SNP_DF_FLUSHcommand.Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
[AMD Official Use Only - General]
Hello Alper,
Here is the feedback from the SEV/SNP firmware team:
The SNP spec has this line in SNP_INIT_EX:
“The firmware marks all encryption capable ASIDs as unusable for encrypted virtualization.”
This is a back-handed way of saying that after SNP_INIT, all of the ASIDs are considered “dirty”. None are in use, but the FW can’t know if there’s any data in the caches left over from some prior guest. So after doing an SNP_INIT (or SNP_INIT_EX), a WBINVD (on all threads) and DF_FLUSH sequence is required. It doesn’t matter whether it’s an SEV DF_FLUSH or an SNP_DF_FLUSH… they both do EXACTLY the same thing.
I don’t understand off hand why SNP_LAUNCH_START would require a DF_FLUSH… Usually, it’s only an “activate” command that requires the DF_FLUSH. ACTIVATE/ACTIVATE_EX/SNP_ACTIVATE/SNP_ACTIVATE_EX
[Ashish] That's why I asked if you are getting the DLFLUSH_REQUIRED error after the SNP activate command ?
It’s when you attempt to (re-)use >an ASID that’s “dirty” that you should get the DF_FLUSH_REQUIRED error.
[Ashish] And we do SNP_DF_FLUSH/SEV_DF_FLUSH whenever ASIDs are reused/re-cycled.
If the host only wants to run SNP guests, there’s no need to do an SEV INIT or any other SEV operations. If the host DOES want to run SEV AND SNP guests, then the required sequence is to do the SNP_INIT before the SEV INIT. Doing the WBINVD/DF_FLUSH any time between the SNP_INIT and any ACTIVATE command should be sufficient.
Thanks,
Ashish
-----Original Message-----
From: Alper Gun <[email protected]>
Sent: Tuesday, June 14, 2022 1:58 PM
To: Kalra, Ashish <[email protected]>
Cc: Peter Gonda <[email protected]>; the arch/x86 maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Let me summarize what I tried.
1- when using psp_init_probe false, the SNP VM fails in SNP_LAUNCH_START step with error SEV_RET_DFFLUSH_REQUIRED(15). 2- added SEV_DF_FLUSH just after SNP platform init and it didn't fail in launch start but failed later during SNP_LAUNCH_UPDATE with SEV_RET_INVALID_PARAM(22)
3- added SNP_DF_FLUSH just after SNP platform init and it failed again during SNP_LAUNCH_UPDATE with SEV_RET_INVALID_PARAM(22) 4- added sev_platform_init for SNP VMs and it worked.
For me DF_FLUSH alone didn' help boot a VM. I don't know yet why sev platform status impacts the SNP VM, but sev_platform_init fixes the problem.
Quoted 160 lines · Ashish
[AMD Official Use Only - General]
Hello Alper, Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 11:30 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 108 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 64 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || + argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but
this time it failed later on the SNP launch update command
withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().Quoted 8 lines
Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.But if
psp_init_on_probeis false,sev_platform_init()isn't called down this path. Alper has suggested we always callsev_platform_init()but we could just place anSEV_DF_FLUSHcommand instead. Or am I still missing something?After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init.Are you getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?Also did you use the
SEV_DF_FLUSHcommand or theSNP_DF_FLUSHcommand ?With SNP you need to use
SNP_DF_FLUSHcommand.Thanks,
Ashish
Re: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
Quoted 15 lines · Ashish
[AMD Official Use Only - General]
Hello Alper,
Here is the feedback from the SEV/SNP firmware team:
The SNP spec has this line in
SNP_INIT_EX:“The firmware marks all encryption capable ASIDs as unusable for encrypted virtualization.”
This is a back-handed way of saying that after
SNP_INIT, all of the ASIDs are considered “dirty”. None are in use, but the FW can’t know if there’s any data in the caches left over from some prior guest. So after doing anSNP_INIT(orSNP_INIT_EX), a WBINVD (on all threads) andDF_FLUSHsequence is required. It doesn’t matter whether it’s an SEVDF_FLUSHor an SNP_DF_FLUSH… they both do EXACTLY the same thing.I don’t understand off hand why
SNP_LAUNCH_STARTwould require a DF_FLUSH… Usually, it’s only an “activate” command that requires theDF_FLUSH. ACTIVATE/ACTIVATE_EX/SNP_ACTIVATE/SNP_ACTIVATE_EX
Quoted 7 lines
[Ashish] That's why I asked if you are getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?It’s when you attempt to (re-)use >an ASID that’s “dirty” that you should get the
DF_FLUSH_REQUIREDerror.[Ashish] And we do SNP_DF_FLUSH/SEV_DF_FLUSH whenever ASIDs are reused/re-cycled.
If the host only wants to run SNP guests, there’s no need to do an SEV INIT or any other SEV operations. If the host DOES want to run SEV AND SNP guests, then the required sequence is to do the
SNP_INITbefore the SEV INIT. Doing the WBINVD/DF_FLUSH any time between theSNP_INITand any ACTIVATE command should be sufficient.
Thanks for the follow up Ashish! I was assuming there was some issue with the ASIDs here but didn't have time to experiment yet. Is there any downside to enabling SEV? If not these patches make sense but if there is some cost should we make KVM capable of running SNP VMs without enabling SEV?
Quoted 183 lines
Thanks,
Ashish-----Original Message-----
From: Alper Gun <[email protected]>
Sent: Tuesday, June 14, 2022 1:58 PM
To: Kalra, Ashish <[email protected]>
Cc: Peter Gonda <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandLet me summarize what I tried.
1- when using
psp_init_probefalse, the SNP VM fails inSNP_LAUNCH_STARTstep with errorSEV_RET_DFFLUSH_REQUIRED(15). 2- addedSEV_DF_FLUSHjust after SNP platform init and it didn't fail in launch start but failed later duringSNP_LAUNCH_UPDATEwithSEV_RET_INVALID_PARAM(22)
3- addedSNP_DF_FLUSHjust after SNP platform init and it failed again duringSNP_LAUNCH_UPDATEwithSEV_RET_INVALID_PARAM(22)4- addedsev_platform_initfor SNP VMs and it worked.For me
DF_FLUSHalone didn' help boot a VM. I don't know yet why sev platform status impacts the SNP VM, butsev_platform_initfixes the problem.Quoted 160 lines · Ashish
[AMD Official Use Only - General]
Hello Alper, Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 11:30 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 108 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 64 lines · Ashish Kalra
Quoted 52 lines · Alper Gun
Quoted 41 lines · Ashish Kalra
Hello Alper,
Quoted 38 lines · Alper Gun
static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || + argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but
this time it failed later on the SNP launch update command
withSEV_RET_INVALID_PARAMerror. Looks like there is another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().Quoted 8 lines
Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.But if
psp_init_on_probeis false,sev_platform_init()isn't called down this path. Alper has suggested we always callsev_platform_init()but we could just place anSEV_DF_FLUSHcommand instead. Or am I still missing something?After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init.Are you getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?Also did you use the
SEV_DF_FLUSHcommand or theSNP_DF_FLUSHcommand ?With SNP you need to use
SNP_DF_FLUSHcommand.Thanks,
Ashish
Kalra, Ashish
maintainerRE: [PATCH Part2 v5 23/45] KVM: SVM: Add KVM_SNP_INIT command
[AMD Official Use Only - General]
Hello Peter,
Quoted 14 lines
Quoted 14 lines
Here is the feedback from the SEV/SNP firmware team:
The SNP spec has this line in
SNP_INIT_EX:“The firmware marks all encryption capable ASIDs as unusable for encrypted virtualization.”
This is a back-handed way of saying that after
SNP_INIT, all of the ASIDs are considered “dirty”. None are in use, but the FW can’t know if there’s any data in the caches left over from some prior guest. So after doing anSNP_INIT(orSNP_INIT_EX), a WBINVD (on all threads) andDF_FLUSHsequence is required. It doesn’t matter whether it’s an SEVDF_FLUSHor an SNP_DF_FLUSH… they both do EXACTLY the same thing.I don’t understand off hand why
SNP_LAUNCH_STARTwould require a DF_FLUSH… Usually, it’s only an “activate” command that requires theDF_FLUSH. ACTIVATE/ACTIVATE_EX/SNP_ACTIVATE/SNP_ACTIVATE_EX[Ashish] That's why I asked if you are getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?
Quoted 7 lines
Quoted 5 lines
It’s when you attempt to (re-)use >an ASID that’s “dirty” that you should get the
DF_FLUSH_REQUIREDerror.[Ashish] And we do SNP_DF_FLUSH/SEV_DF_FLUSH whenever ASIDs are reused/re-cycled.
If the host only wants to run SNP guests, there’s no need to do an SEV INIT or any other SEV operations. If the host DOES want to run SEV AND SNP guests, then the required sequence is to do the
SNP_INITbefore the SEV INIT. Doing the >>WBINVD/DF_FLUSH any time between theSNP_INITand any ACTIVATE command should be sufficient.Thanks for the follow up Ashish! I was assuming there was some issue with the ASIDs here but didn't have time to experiment yet.
Yes, it looks to be some issue with ASIDs.
Quoted 1 line
Is there any downside to enabling SEV? If not these patches make sense but if there is some cost should we make KVM capable of running SNP VMs without enabling SEV?
As mentioned above we only need to do this if the host wants to run both SEV and SNP guests. I assume the cost should not be considered as this is only a init or guest launch time addition.
Thanks,
Ashish
Quoted 203 lines
-----Original Message-----
From: Alper Gun <[email protected]>
Sent: Tuesday, June 14, 2022 1:58 PM
To: Kalra, Ashish <[email protected]>
Cc: Peter Gonda <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandLet me summarize what I tried.
1- when using
psp_init_probefalse, the SNP VM fails inSNP_LAUNCH_STARTstep with errorSEV_RET_DFFLUSH_REQUIRED(15). 2- addedSEV_DF_FLUSHjust after SNP platform init and it didn't fail
in launch start but failed later duringSNP_LAUNCH_UPDATEwithSEV_RET_INVALID_PARAM(22)
3- addedSNP_DF_FLUSHjust after SNP platform init and it failed again
duringSNP_LAUNCH_UPDATEwithSEV_RET_INVALID_PARAM(22)
4- addedsev_platform_initfor SNP VMs and it worked.For me
DF_FLUSHalone didn' help boot a VM. I don't know yet why sev platform status impacts the SNP VM, butsev_platform_initfixes the problem.Quoted 162 lines · Ashish
[AMD Official Use Only - General]
Hello Alper, Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 11:30 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 109 lines · Ashish
[AMD Official Use Only - General]
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Tuesday, June 14, 2022 10:38 AM
To: Kalra, Ashish <[email protected]>
Cc: Alper Gun <[email protected]>; Brijesh Singh <[email protected]>; Kalra, Ashish <[email protected]>; thearch/x86maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Pavan Kumar Paluri <[email protected]>
Subject: Re: [PATCH Part2 v5 23/45] KVM: SVM: AddKVM_SNP_INITcommandQuoted 65 lines · Ashish Kalra
Quoted 53 lines · Alper Gun
Quoted 42 lines · Ashish Kalra
Hello Alper,
Quoted 39 lines · Alper Gun
static int sev_guest_init(struct kvm *kvm, struct
kvm_sev_cmd
*argp)Quoted 18 lines
{(quoted diff) +3
-1+ bool es_active = (argp->id == KVM_SEV_ES_INIT || + argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -249,12 +269,22 @@static int
sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; sev->es_active = es_active;(quoted diff) +1
-0+ sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid;Review
Quoted 10 lines
(quoted diff) +8
-1- ret = sev_platform_init(&argp->error); + if (snp_active) { + ret = verify_snp_init_flags(kvm, argp); + if (ret) + goto e_free; + + ret = sev_snp_init(&argp->error); + } else { + ret = sev_platform_init(&argp->error);After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform
yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init but
this time it failed later on the SNP launch update command
withSEV_RET_INVALID_PARAMerror. Looks like there is
another dependency on SEV platform initialization.Quoted 7 lines
Quoted 1 line
Calling
sev_platform_initfor SNP VMs fixes the problem in our tests.Trying to get some more context for this issue.
When you say after
SEV_INIT_EXsupport patches, SEV may be initialized in the platform late, do you mean sev_pci_init()->sev_snp_init() ...sev_platform_init()code path has still not executed on the host BSP ?Correct,
INIT_EXrequires the file system to be ready and there is a ccp module param to call it only when needed.Quoted 7 lines
MODULE_PARM_DESC(psp_init_on_probe, " if true, the PSP will be initialized on module init. Else the PSP will be initialized on the first command requiring it");
If this module param is false, it won't initialize SEV on the platform until the first SEV VM.
Ok, that makes sense.
Quoted 7 lines
So the fix will be to call
sev_platform_init()unconditionally here insev_guest_init(), and bothsev_snp_init()andsev_platform_init()are protected from being called again, so there won't be any issues if these functions are invoked again at SNP/SEV VM launch if they have been invoked earlier during module init.That's one solution. I don't know if there is a downside to the system for enabling SEV if SNP is being enabled but another solution could be to just directly place a
DF_FLUSHcommand instead of callingsev_platform_init().Quoted 8 lines
Actually
sev_platform_init()is already called on module init ifpsp_init_on_probeis not false. Only need to ensure that SNP firmware is initialized first withSNP_INITcommand.But if
psp_init_on_probeis false,sev_platform_init()isn't called down this path. Alper has suggested we always callsev_platform_init()but we could just place anSEV_DF_FLUSHcommand instead. Or am I still missing something?After SEV
INIT_EXsupport patches, SEV may be initialized in the platform late. In my tests, if SEV has not been initialized in the platform yet, SNP VMs fail withSEV_DF_FLUSHrequired error. I tried callingSEV_DF_FLUSHright after the SNP platform init.Are you getting the
DLFLUSH_REQUIREDerror after the SNP activate command ?Also did you use the
SEV_DF_FLUSHcommand or theSNP_DF_FLUSHcommand ?With SNP you need to use
SNP_DF_FLUSHcommand.Thanks,
Ashish
PATCH v5 24/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command
In reply to Brijesh SinghKVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest.
For more information see the SEV-SNP specification.
--- .../virt/kvm/amd-memory-encryption.rst | 24 ++++ arch/x86/kvm/svm/sev.c | 116 +++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 10 ++ 4 files changed, 148 insertions(+), 3 deletions(-)
Documentation/virt/kvm/amd-memory-encryption.rst +24 -0
@@ -454,6 +454,30 @@ The flags bitmap is defined as:: If the specified flags is not supported then return -EOPNOTSUPP, and the supported flags are returned. +19. KVM_SNP_LAUNCH_START +------------------------ + +The KVM_SNP_LAUNCH_START command is used for creating the memory encryption +context for the SEV-SNP guest. To create the encryption context, user must +provide a guest policy, migration agent (if any) and guest OS visible +workarounds value as defined SEV-SNP specification. + +Parameters (in): struct kvm_snp_launch_start + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_start { + __u64 policy; /* Guest policy to use. */ + __u64 ma_uaddr; /* userspace address of migration agent */ + __u8 ma_en; /* 1 if the migtation agent is enabled */ + __u8 imi_en; /* set IMI to 1. */ + __u8 gosvw[16]; /* guest OS visible workarounds */ + }; + +See the SEV-SNP specification for further detail on the launch input. + References ==========
arch/x86/kvm/svm/sev.c +113 -3
169 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +1 -0
@@ -85,6 +85,7 @@ struct kvm_sev_info {struct kvm *enc_context_owner;/* Owner of copied encryption context */ struct misc_cg *misc_cg;/* For misc cgroup accounting */ u64 snp_init_flags; +void *snp_context;/* SNP guest context page */ };struct kvm_svm {
include/uapi/linux/kvm.h +10 -0
@@ -1714,6 +1714,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT , +KVM_SEV_SNP_LAUNCH_START , KVM_SEV_NR_MAX, };
@@ -1821,6 +1822,15 @@ struct kvm_snp_init {__u64 flags; }; +struct kvm_sev_snp_launch_start { +__u64 policy; +__u64 ma_uaddr; +__u8 ma_en; +__u8 imi_en; +__u8 gosvw[16 ]; +__u8 pad[6 ]; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.17.1
PATCH v5 25/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
In reply to Brijesh SinghThe KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START.
In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.
While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.
For more information see the SEV-SNP specification.
--- .../virt/kvm/amd-memory-encryption.rst | 29 +++ arch/x86/kvm/svm/sev.c | 187 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 235 insertions(+)
Documentation/virt/kvm/amd-memory-encryption.rst +29 -0
@@ -478,6 +478,35 @@ Returns: 0 on success, -negative on error See the SEV-SNP specification for further detail on the launch input. +20. KVM_SNP_LAUNCH_UPDATE +------------------------- + +The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also +calculates a measurement of the memory contents. The measurement is a signature +of the memory contents that can be sent to the guest owner as an attestation +that the memory was encrypted correctly by the firmware. + +Parameters (in): struct kvm_snp_launch_update + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_update { + __u64 start_gfn; /* Guest page number to start from. */ + __u64 uaddr; /* userspace address need to be encrypted */ + __u32 len; /* length of memory region */ + __u8 imi_page; /* 1 if memory is part of the IMI */ + __u8 page_type; /* page type */ + __u8 vmpl3_perms; /* VMPL3 permission mask */ + __u8 vmpl2_perms; /* VMPL2 permission mask */ + __u8 vmpl1_perms; /* VMPL1 permission mask */ + }; + +See the SEV-SNP spec for further details on how to build the VMPL permission +mask and page type. + + References ==========
arch/x86/kvm/svm/sev.c +187 -0
217 lines not on this page · raw message
include/uapi/linux/kvm.h +19 -0
@@ -1715,6 +1715,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START , +KVM_SEV_SNP_LAUNCH_UPDATE , KVM_SEV_NR_MAX, };
@@ -1831,6 +1832,24 @@ struct kvm_sev_snp_launch_start {__u8 pad[6 ]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { +__u64 start_gfn; +__u64 uaddr; +__u32 len; +__u8 imi_page; +__u8 page_type; +__u8 vmpl3_perms; +__u8 vmpl2_perms; +__u8 vmpl1_perms; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
Quoted 135 lines · Brijesh Singh
The
KVM_SEV_SNP_LAUNCH_UPDATEcommand can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with theKVM_SEV_SNP_LAUNCH_START.In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page.
While terminating the guest, reclaim the guest pages added in the RMP table. If the reclaim fails, then the page is no longer safe to be released back to the system and leak them.
For more information see the SEV-SNP specification.
Signed-off-by Brijesh Singh <[email protected]>--- .../virt/kvm/amd-memory-encryption.rst | 29 +++ arch/x86/kvm/svm/sev.c | 187 ++++++++++++++++++ include/uapi/linux/kvm.h | 19 ++ 3 files changed, 235 insertions(+)Documentation/virt/kvm/amd-memory-encryption.rst +29
-0@@ -478,6 +478,35 @@ Returns: 0 on success, -negative on error See the SEV-SNP specification for further detail on the launch input. +20. KVM_SNP_LAUNCH_UPDATE +------------------------- + +The KVM_SNP_LAUNCH_UPDATE is used for encrypting a memory region. It also +calculates a measurement of the memory contents. The measurement is a signature +of the memory contents that can be sent to the guest owner as an attestation +that the memory was encrypted correctly by the firmware. + +Parameters (in): struct kvm_snp_launch_update + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_update { + __u64 start_gfn; /* Guest page number to start from. */ + __u64 uaddr; /* userspace address need to be encrypted */ + __u32 len; /* length of memory region */ + __u8 imi_page; /* 1 if memory is part of the IMI */ + __u8 page_type; /* page type */ + __u8 vmpl3_perms; /* VMPL3 permission mask */ + __u8 vmpl2_perms; /* VMPL2 permission mask */ + __u8 vmpl1_perms; /* VMPL1 permission mask */ + }; + +See the SEV-SNP spec for further details on how to build the VMPL permission +mask and page type. + + References ==========arch/x86/kvm/svm/sev.c +52
-0@@ -17,6 +17,7 @@#include <linux/misc_cgroup.h> #include <linux/processor.h> #include <linux/trace_events.h> +#include <linux/sev.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> @@ -227,6 +228,49 @@ static void sev_decommission(unsigned int handle)sev_guest_decommission (&decommission,NULL ); } +static inline void snp_leak_pages (u64 pfn,enum pg_level level) +{ +unsigned int npages =page_level_size (level) >>PAGE_SHIFT ; + +WARN (1 ,"psc failed pfn 0x%llx pages %d (leaking)\n" , pfn, npages); + +while (npages) { +memory_failure (pfn,0 ); +dump_rmpentry (pfn); + npages--; + pfn++; + } +} + +static int snp_page_reclaim (u64 pfn) +{ +struct sev_data_snp_page_reclaim data = {0 }; +int err, rc; + + data.paddr =__sme_set (pfn <<PAGE_SHIFT ); + rc =snp_guest_page_reclaim (&data, &err); +if (rc) { +/* +* If the reclaim failed, then page is no longer safe +* to use. +*/ +snp_leak_pages (pfn,PG_LEVEL_4K ); + } + +return rc; +} + +static int host_rmp_make_shared (u64 pfn,enum pg_level level,bool leak) +{ +int rc; + + rc =rmp_make_shared (pfn, level); +if (rc && leak) +snp_leak_pages (pfn, level); + +return rc; +} +static void sev_unbind_asid (struct kvm *kvm,unsigned int handle) {struct sev_data_deactivate deactivate;@@ -1620,6 +1664,123 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return rc; } +static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); +
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1684
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false; +}
Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ +static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;
Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;
Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Quoted 18 lines
arch/x86/kvm/svm/sev.c +18
-0+u64 gfn; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 ); +if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try
This is *too* avoid cases...
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , &data, error); +if (ret) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }
Hmm if this call fails after the first iteration of this loop it will lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then HASH(A & B) right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
Quoted 108 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_START : r =snp_launch_start (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_UPDATE : + r =snp_launch_update (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START , +KVM_SEV_SNP_LAUNCH_UPDATE , KVM_SEV_NR_MAX, };@@ -1831,6 +1832,24 @@ struct kvm_sev_snp_launch_start {__u8 pad[6 ]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { +__u64 start_gfn; +__u64 uaddr; +__u32 len; +__u8 imi_page; +__u8 page_type; +__u8 vmpl3_perms; +__u8 vmpl2_perms; +__u8 vmpl1_perms; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
On 9/27/21 11:43 AM, Peter Gonda wrote:
...
Quoted 18 lines
Quoted 18 lines
arch/x86/kvm/svm/sev.c +17
-0+static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); + +list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false;
Quoted 9 lines
Quoted 1 line
+}
Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
Quoted 1 line
+
Interesting, for qemu we had very few number of regions so there was no strong reason for me to think something otherwise. Do you have any preference on what data structure you will use ?
Quoted 10 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
Sure, I will pick a better name and no need to zero above. I will fix it.
Quoted 9 lines
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Noted.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/svm/sev.c +22
-0+u64 gfn; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know +* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 );
Quoted 9 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try This is *too* avoid cases...
Noted
Quoted 39 lines
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , &data, error); +if (ret) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }Hmm if this call fails after the first iteration of this loop it will
Quoted 9 lines
lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then
HASH(A & B)right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
I can add a comment in documentation that if a LAUNCH_UPDATE fails then user need to destroy the existing context and start from the beginning. I am not sure if we want to support the partial update cases. But in case we have two choices a) decommission the context on failure or b) add a new command to destroy the existing context.
Quoted 108 lines
Quoted 108 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_START : r =snp_launch_start (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_UPDATE : + r =snp_launch_update (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START , +KVM_SEV_SNP_LAUNCH_UPDATE , KVM_SEV_NR_MAX, };@@ -1831,6 +1832,24 @@ struct kvm_sev_snp_launch_start {__u8 pad[6 ]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { +__u64 start_gfn; +__u64 uaddr; +__u32 len; +__u8 imi_page; +__u8 page_type; +__u8 vmpl3_perms; +__u8 vmpl2_perms; +__u8 vmpl1_perms; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
Re: [PATCH Part2 v5 25/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command
Quoted 31 lines · Brijesh Singh
On 9/27/21 11:43 AM, Peter Gonda wrote:
...Quoted 26 lines
Quoted 9 lines
arch/x86/kvm/svm/sev.c +8
-0+static bool is_hva_registered (struct kvm *kvm,hva_t hva,size_t len) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct list_head *head = &sev->regions_list; +struct enc_region *i; + +lockdep_assert_held (&kvm->lock); +arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1684
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+list_for_each_entry (i, head, list) { +u64 start = i->uaddr; +u64 end = start + i->size; + +if (start <= hva && end >= (hva + len)) +return true; + } + +return false; +}Internally we actually register the guest memory in chunks for various reasons. So for our largest SEV VM we have 768 1 GB entries in |sev->regions_list|. This was OK before because no look ups were done. Now that we are performing a look ups a linked list with linear time lookups seems not ideal, could we switch the back data structure here to something more conducive too fast lookups?
Quoted 5 lines
Quoted 1 line
Quoted 1 line
+
Interesting, for qemu we had very few number of regions so there was no strong reason for me to think something otherwise. Do you have any preference on what data structure you will use ?
Chatted offline. I think this is fine for now, we won't want to use our userspace demand pinning with SNP yet.
Quoted 108 lines
Quoted 11 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1691
Quoted 7 lines
arch/x86/kvm/svm/sev.c +6
-0+static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_snp_launch_update data = {0 }; +struct kvm_sev_snp_launch_update params; +unsigned long npages, pfn, n =0 ;Could we have a slightly more descriptive name for |n|? nprivate maybe? Also why not zero in the loop below?
Sure, I will pick a better name and no need to zero above. I will fix it.
Quoted 9 lines
for (i = 0, n = 0; i < npages; ++i)
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1694
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+int *error = &argp->error; +struct page **inpages; +int ret, i, level;Should |i| be an unsigned long since it can is tracked in a for loop with "i < npages" npages being an unsigned long? (|n| too)
Noted.
Quoted 22 lines
Quoted 22 lines
arch/x86/kvm/svm/sev.c +22
-0+u64 gfn; + +if (!sev_snp_guest (kvm)) +return -ENOTTY ; + +if (!sev->snp_context) +return -EINVAL ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + +/* Verify that the specified address range is registered. */ +if (!is_hva_registered (kvm, params.uaddr, params.len)) +return -EINVAL ; + +/* +* The userspace memory is already locked so technically we don't +* need to lock it again. Later part of the function needs to know +* pfn so call the sev_pin_memory() so that we can get the list of +* pages to iterate through. +*/ + inpages =sev_pin_memory (kvm, params.uaddr, params.len, &npages,1 );Quoted 9 lines
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1722
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+if (!inpages) +return -ENOMEM ; + +/* +* Verify that all the pages are marked shared in the RMP table before +* going further. This is avoid the cases where the userspace may try This is *too* avoid cases...
Noted
Quoted 39 lines
Quoted 27 lines
arch/x86/kvm/svm/sev.c +27
-0+* updating the same page twice. +*/ +for (i =0 ; i < npages; i++) { +if (snp_lookup_rmpentry (page_to_pfn (inpages[i]), &level) !=0 ) { +sev_unpin_memory (kvm, inpages, npages); +return -EFAULT ; + } + } + + gfn = params.start_gfn; + level =PG_LEVEL_4K ; + data.gctx_paddr =__psp_pa (sev->snp_context); + +for (i =0 ; i < npages; i++) { + pfn =page_to_pfn (inpages[i]); + + ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT , level,sev_get_asid (kvm), true); +if (ret) { + ret = -EFAULT ; + goto e_unpin; + } + + n++; + data.address =__sme_page_pa (inpages[i]); + data.page_size =X86_TO_RMP_PG_LEVEL (level); + data.page_type = params.page_type; + data.vmpl3_perms = params.vmpl3_perms;arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1759
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ data.vmpl2_perms = params.vmpl2_perms; + data.vmpl1_perms = params.vmpl1_perms; + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , &data, error); +if (ret) { +/* +* If the command failed then need to reclaim the page. +*/ +snp_page_reclaim (pfn); + goto e_unpin; + }Hmm if this call fails after the first iteration of this loop it will
Quoted 9 lines
lead to a hard to reproduce LaunchDigest right? Say if we are SnpLaunchUpdating just 2 pages A and B. If we first call this ioctl and A is SNP_LAUNCH_UPDATED'd but B fails, we then make A shared again in the RMP. So we must call the ioctl with 2 pages again, after fixing the issue with page B. Now the Launch digest has something like Hash(A) then
HASH(A & B)right (overly simplified) so A will be included twice right? I am not sure if anything better can be done here but might be worth documenting IIUC.
Quoted 6 lines
I can add a comment in documentation that if a
LAUNCH_UPDATEfails then user need to destroy the existing context and start from the beginning. I am not sure if we want to support the partial update cases. But in case we have two choices a) decommission the context on failure or b) add a new command to destroy the existing context.
Agreed supporting the partial update case seems very tricky.
Quoted 109 lines
Quoted 109 lines
Quoted 109 lines
arch/x86/kvm/svm/sev.c +50
-0+ + gfn++; + } + +e_unpin: +/* Content of memory is updated, mark pages dirty */ +for (i =0 ; i < n; i++) { +set_page_dirty_lock (inpages[i]); +mark_page_accessed (inpages[i]); + +/* +* If its an error, then update RMP entry to change page ownership +* to the hypervisor. +*/ +if (ret) +host_rmp_make_shared (pfn, level, true); + } + +/* Unlock the user pages */ +sev_unpin_memory (kvm, inpages, npages); + +return ret; +} +int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;@@ -1712,6 +1873,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_SNP_LAUNCH_START : r =snp_launch_start (kvm, &sev_cmd);break ; +case KVM_SEV_SNP_LAUNCH_UPDATE : + r =snp_launch_update (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;@@ -1794,6 +1958,29 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)static void __unregister_enc_region_locked (struct kvm *kvm,struct enc_region *region) { +unsigned long i, pfn; +int level; + +/* +* The guest memory pages are assigned in the RMP table. Unassign it +* before releasing the memory. +*/ +if (sev_snp_guest (kvm)) { +for (i =0 ; i < region->npages; i++) { + pfn =page_to_pfn (region->pages[i]); + +if (!snp_lookup_rmpentry (pfn, &level)) +continue ; + +cond_resched (); + +if (level >PG_LEVEL_4K ) + pfn &= ~(KVM_PAGES_PER_HPAGE (PG_LEVEL_2M ) -1 ); + +host_rmp_make_shared (pfn, level, true); + } + } +sev_unpin_memory (kvm, region->pages, region->npages);list_del (®ion->list);kfree (region);include/uapi/linux/kvm.h +19
-1@@ -1715,6 +1715,7 @@ enum sev_cmd_id {/* SNP specific commands */ KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START , +KVM_SEV_SNP_LAUNCH_UPDATE , KVM_SEV_NR_MAX, };@@ -1831,6 +1832,24 @@ struct kvm_sev_snp_launch_start {__u8 pad[6 ]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { +__u64 start_gfn; +__u64 uaddr; +__u32 len; +__u8 imi_page; +__u8 page_type; +__u8 vmpl3_perms; +__u8 vmpl2_perms; +__u8 vmpl1_perms; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) --2.17.1
PATCH v5 26/45 KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Brijesh SinghWhen SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the rmp_make_private() unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).
Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
arch/x86/kvm/svm/sev.c +32 -0
@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h"
@@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size) +{ +struct vm_area_struct *vma; +u64 end = start + size; +int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +return ret; +} +static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;
@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm); +if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 8 lines · * Brijesh Singh ([email protected]) wrote:
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
^^^^^^^^^
(and in the subject) -> unmergeable
Quoted 25 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/x86/kvm/svm/sev.c +2
-0@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h"
arch/x86/kvm/svm/sev.c static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len) · line 1688
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size)
^^^^^^^^^^
arch/x86/kvm/svm/sev.c static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len) · line 1691
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+{ +struct vm_area_struct *vma; +u64 end = start + size;
Do you need to worry about wrap there? (User supplied start/size?)
Dave
Quoted 40 lines
arch/x86/kvm/svm/sev.c +26
-0+int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +return ret; +} +static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm); +if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Sean Christopherson
maintainerRe: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
In reply to Brijesh SinghQuoted 8 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Quoted 55 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)arch/x86/kvm/svm/sev.c +26
-0@@ -18,11 +18,13 @@#include <linux/processor.h> #include <linux/trace_events.h> #include <linux/sev.h> +#include <linux/ksm.h> #include <asm/fpu/internal.h> #include <asm/pkru.h> #include <asm/trapnr.h> #include <asm/sev.h> +#include <asm/mman.h> #include "x86.h" #include "svm.h" @@ -1683,6 +1685,30 @@ static bool is_hva_registered(struct kvm *kvm, hva_t hva, size_t len)return false; } +static int snp_mark_unmergable (struct kvm *kvm,u64 start,u64 size) +{ +struct vm_area_struct *vma; +u64 end = start + size; +int ret; + +do { + vma =find_vma_intersection (kvm->mm, start, end); +if (!vma) { + ret = -EINVAL ; +break ; + } + + ret =ksm_madvise (vma, vma->vm_start, vma->vm_end, +MADV_UNMERGEABLE , &vma->vm_flags); +if (ret) +break ; + + start = vma->vm_end; + }while (end > vma->vm_end); + +return ret; +} +static int snp_launch_update (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 6 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -1707,6 +1733,12 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!is_hva_registered (kvm, params.uaddr, params.len))return -EINVAL ; +mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);
This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in __snp_handle_page_state_change(). Userspace can madvise() the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying on MADV_UNMERGEABLE for functional correctness simply cannot work in KVM, barring mmu_notifier and a big pile of code.
Quoted 9 lines
arch/x86/kvm/svm/sev.c +3
-0+if (ret) +return -EFAULT ; +/* * The userspace memory is already locked so technically we don't * need to lock it again. Later part of the function needs to know Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
[...]
Quoted 8 lines
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in
__snp_handle_page_state_change(). Userspace canmadvise()the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying onMADV_UNMERGEABLEfor functional correctness simply cannot work in KVM, barringmmu_notifierand a big pile of code.
AFAICT, ksm does not exclude the unmapped pfn from its scan list. We need to tell ksm somehow to exclude the unmapped pfn from its scan list. I understand that if userspace is messing with us, we have an issue, but it's a userspace bug ;) To fix it right, we need to enhance ksm to exclude the pfn when it is getting unmapped from the direct map. I believe that work can be done outside of the SNP series. I am okay to drop snp_mark_unmerable(), and until then, we just run with KSM disabled. Thoughts?
thanks
Sean Christopherson
maintainerRe: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 8 lines · Brijesh Singh
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).
Quoted 6 lines
Quoted 4 lines
Quoted 3 lines
Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
Is this problem unique to nuking the direct map (patch 05), or would it also be a problem (in the form of an RMP violation) if the direct map were demoted to 4k pages?
Quoted 20 lines
[...]
Quoted 8 lines
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in
__snp_handle_page_state_change(). Userspace canmadvise()the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying onMADV_UNMERGEABLEfor functional correctness simply cannot work in KVM, barringmmu_notifierand a big pile of code.AFAICT, ksm does not exclude the unmapped pfn from its scan list. We need to tell ksm somehow to exclude the unmapped pfn from its scan list. I understand that if userspace is messing with us, we have an issue, but it's a userspace bug ;) To fix it right, we need to enhance ksm to exclude the pfn when it is getting unmapped from the direct map. I believe that work can be done outside of the SNP series. I am okay to drop
snp_mark_unmerable(), and until then, we just run with KSM disabled. Thoughts?thanks
Re: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Brijesh Singh
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Quoted 3 lines
Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
Is this problem unique to nuking the direct map (patch 05),
Yes. This problem didn't exist in previous series because we were not nuking the page from direct map and KSM was able to read the memory just fine. Now with the page removed from the direct map causes #PF (not-present).
Quoted 4 lines
or would it also be
a problem (in the form of an RMP violation) if the direct map were demoted to 4k pages?
No, this problem does happen due to the demotion. In previous series, we were demoting the pages to 4k and everyone was happy (including ksm). In the case of ksm, the page will *never* be merged because ciphertext for two private pages will never be the same. Removing the pages from direct map certainly brings additional complexity in the KVM and other places in the kernel. From architecture point of view, there is actually no need to mark the page *not present* in the direct map. I believe in TDX that is must but for the SEV-SNP its not required at all. A hypervisor can read the guest private pages just fine, only the write will cause an RMP fault.
Quoted 19 lines
Quoted 19 lines
[...]
Quoted 8 lines
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 1738
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+mmap_write_lock (kvm->mm); + ret =snp_mark_unmergable (kvm, params.uaddr, params.len); +mmap_write_unlock (kvm->mm);This does not, and practically speaking cannot, work. There are multiple TOCTOU bugs, here and in
__snp_handle_page_state_change(). Userspace canmadvise()the range at any later point, munmap()/mmap() the entire range, mess with the memslots in the PSC case, and so on and so forth. Relying onMADV_UNMERGEABLEfor functional correctness simply cannot work in KVM, barringmmu_notifierand a big pile of code.AFAICT, ksm does not exclude the unmapped pfn from its scan list. We need to tell ksm somehow to exclude the unmapped pfn from its scan list. I understand that if userspace is messing with us, we have an issue, but it's a userspace bug ;) To fix it right, we need to enhance ksm to exclude the pfn when it is getting unmapped from the direct map. I believe that work can be done outside of the SNP series. I am okay to drop
snp_mark_unmerable(), and until then, we just run with KSM disabled. Thoughts?thanks
Sean Christopherson
maintainerRe: [PATCH Part2 v5 26/45] KVM: SVM: Mark the private vma unmerable for SEV-SNP guests
Quoted 13 lines · Brijesh Singh
Quoted 11 lines · Sean Christopherson
Quoted 10 lines · Brijesh Singh
Quoted 9 lines · Sean Christopherson
Quoted 8 lines · Brijesh Singh
When SEV-SNP is enabled, the guest private pages are added in the RMP table; while adding the pages, the
rmp_make_private()unmaps the pages from the direct map. If KSM attempts to access those unmapped pages then it will trigger #PF (page-not-present).Encrypted guest pages cannot be shared between the process, so an userspace should not mark the region mergeable but to be safe, mark the process vma unmerable before adding the pages in the RMP table.
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
Quoted 1 line
To be safe from what? Does the !PRESENT #PF crash the kernel?
Yes, kernel crashes when KSM attempts to access to an unmaped pfn.
Is this problem unique to nuking the direct map (patch 05),
Yes. This problem didn't exist in previous series because we were not nuking the page from direct map and KSM was able to read the memory just fine. Now with the page removed from the direct map causes #PF (not-present).
Hrm, so regardless of what manipulations are done to the direct map, any errant write to guest private memory via the direct map would be fatal to the kernel. That's both mildly terrifying and oddly encouraging, as it means silent guest data corruption is no longer a thing, at least for private memory.
One concrete takeaway for me is that "silently" nuking the direct map on RMP assignment is not an option. Nuking the direct map if the kernel has a way to determine that the backing store is for guest private memory is perfectly ok, but pulling the rug out so to speak is setting us up for maintenance hell.
Quoted 4 lines
Quoted 3 lines
or would it also be a problem (in the form of an RMP violation) if the direct map were demoted to 4k pages?
Quoted 8 lines
No, this problem does happen due to the demotion. In previous series, we were demoting the pages to 4k and everyone was happy (including ksm). In the case of ksm, the page will *never* be merged because ciphertext for two private pages will never be the same. Removing the pages from direct map certainly brings additional complexity in the KVM and other places in the kernel. From architecture point of view, there is actually no need to mark the page *not present* in the direct map. I believe in TDX that is must but for the SEV-SNP its not required at all.
Nuking the direct map is not strictly required for TDX either, as reads do not compromise the integrity of the memory, i.e. don't poison memory and lead to #MC. Like SNP, writes via the direct map would be fatal.
The issue with TDX that is not shared by SNP is that writes through _user_ mappings can be fatal the system. With SNP, those generate RMP violations, but because they are "just" page faults, the normal uaccess machinery happily eats them and SIGBUSes the VMM.
Quoted 2 lines
A hypervisor can read the guest private pages just fine, only the write will cause an RMP fault.
Well, for some definitions of "read". I'm kinda joking, kinda serious. KSM may "work" when it reads garbage, but the same is likely not true for other kernel code that wanders into guest private memory. Ideally, the kernel would provide a mechanism to _prevent_ any such reads/writes, and violations would be treated as kernel bugs. Given that SEV has been successfully deployed, the probability of lurking bugs is quite low, but I still dislike the idea of latent bugs going unnoticed or manifesting in weird ways.
PATCH v5 27/45 KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
In reply to Brijesh SinghThe KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch.
While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages.
--- .../virt/kvm/amd-memory-encryption.rst | 22 ++++ arch/x86/kvm/svm/sev.c | 116 ++++++++++++++++++ include/uapi/linux/kvm.h | 14 +++ 3 files changed, 152 insertions(+)
Documentation/virt/kvm/amd-memory-encryption.rst +22 -0
@@ -506,6 +506,28 @@ Returns: 0 on success, -negative on error See the SEV-SNP spec for further details on how to build the VMPL permission mask and page type. +21. KVM_SNP_LAUNCH_FINISH +------------------------- + +After completion of the SNP guest launch flow, the KVM_SNP_LAUNCH_FINISH command can be +issued to make the guest ready for the execution. + +Parameters (in): struct kvm_sev_snp_launch_finish + +Returns: 0 on success, -negative on error + +:: + + struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[32]; + }; + + +See SEV-SNP specification for further details on launch finish input parameters. References ==========
arch/x86/kvm/svm/sev.c +116 -0
144 lines not on this page · raw message
include/uapi/linux/kvm.h +14 -0
@@ -1716,6 +1716,7 @@ enum sev_cmd_id {KVM_SEV_SNP_INIT ,KVM_SEV_SNP_LAUNCH_START ,KVM_SEV_SNP_LAUNCH_UPDATE , +KVM_SEV_SNP_LAUNCH_FINISH , KVM_SEV_NR_MAX, };
@@ -1850,6 +1851,19 @@ struct kvm_sev_snp_launch_update {__u8 vmpl1_perms; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { +__u64 id_block_uaddr; +__u64 id_auth_uaddr; +__u8 id_block_en; +__u8 auth_key_en; +__u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE ]; +__u8 pad[6 ]; +}; +#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 27/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
Quoted 20 lines
arch/x86/kvm/svm/sev.c +8
-0@@ -2364,16 +2467,29 @@ static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va,void sev_free_vcpu (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm; +u64 pfn;if (!sev_es_guest (vcpu->kvm))return ; svm =to_svm (vcpu); + pfn =__pa (svm->vmsa) >>PAGE_SHIFT ;if (vcpu->arch.guest_state_protected)sev_flush_guest_memory (svm, svm->vmsa,PAGE_SIZE ); + +/* +* If its an SNP guest, then VMSA was added in the RMP entry as +* a guest owned page. Transition the page to hyperivosr state +* before releasing it back to the system. +*/
arch/x86/kvm/svm/sev.c static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va, · line 2495
Quoted 10 lines
arch/x86/kvm/svm/sev.c +5
-0+if (sev_snp_guest (vcpu->kvm) && +host_rmp_make_shared (pfn,PG_LEVEL_4K , false)) + goto skip_vmsa_free; +__free_page (virt_to_page (svm->vmsa)); +skip_vmsa_free:if (svm->ghcb_sa_free)kfree (svm->ghcb_sa); }
Hi Ashish. We're still working with this patch set internally. We found a bug that I wanted to report in this patch. Above, we need to flush the VMSA page, svm->vmsa, _after_ we call host_rmp_make_shared() to mark the page is shared. Otherwise, the host gets an RMP violation when it tries to flush the guest-owned VMSA page.
The bug was silent, at least on our Milan platforms, bef reo d45829b351ee6 ("KVM: SVM: Flush when freeing encrypted pages even on SME_COHERENT CPUs"), because the sev_flush_guest_memory() helper was a noop on platforms with the SME_COHERENT feature. However, after d45829b351ee6, we unconditionally do the flush to keep the IO address space coherent. And then we hit this bug.
Thanks,
Marc
Kalra, Ashish
maintainerRE: [PATCH Part2 v5 27/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
[AMD Official Use Only - General]
Hello Marc,
-----Original Message-----
From: Marc Orr <[email protected]>
Sent: Wednesday, May 18, 2022 3:21 PM
To: Kalra, Ashish <[email protected]>
Cc: x86 <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Wanpeng Li <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Gonda <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Alper Gun <[email protected]>
Subject: Re: [PATCH Part2 v5 27/45] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command
Quoted 32 lines
(quoted diff) +13
-0@@ -2364,16 +2467,29 @@ static void sev_flush_guest_memory(struct vcpu_svm *svm, void *va, void sev_free_vcpu(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm; + u64 pfn; if (!sev_es_guest(vcpu->kvm)) return; svm = to_svm(vcpu); + pfn = __pa(svm->vmsa) >> PAGE_SHIFT; if (vcpu->arch.guest_state_protected) sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); + + /* + * If its an SNP guest, then VMSA was added in the RMP entry as + * a guest owned page. Transition the page to hyperivosr state + * before releasing it back to the system. + */ + if (sev_snp_guest(vcpu->kvm) && + host_rmp_make_shared(pfn, PG_LEVEL_4K, false)) + goto skip_vmsa_free; + __free_page(virt_to_page(svm->vmsa)); +skip_vmsa_free: if (svm->ghcb_sa_free) kfree(svm->ghcb_sa); }Hi Ashish. We're still working with this patch set internally. We found a bug that I wanted to report in this patch. Above, we need to flush the VMSA page,
svm->vmsa, _after_ we callhost_rmp_make_shared()to mark the page is shared. >Otherwise, the host gets an RMP violation when it tries to flush the guest-owned VMSA page.
Quoted 2 lines
The bug was silent, at least on our Milan platforms, bef reo
d45829b351ee6("KVM: SVM: Flush when freeing encrypted pages even onSME_COHERENTCPUs"), because thesev_flush_guest_memory()helper was a noop on platforms with theSME_COHERENTfeature. However, afterd45829b351ee6, we >unconditionally do the flush to keep the IO address space coherent. And then we hit this bug.
Yes I have already hit this bug and added a fix as below:
commit 944fba38cbd3baf1ece76197630bd45e83089f14
Author: Ashish Kalra <[email protected]> Date: Tue May 3 14:33:29 2022 +0000
KVM: SVM: Fix VMSA flush for an SNP guest.
If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page and also removed from the kernel direct map so flush it later after it is transitioned back to hypervisor state and restored in the direct map.
arch/x86/kvm/svm/sev.c +6 -3
@@ -2840,27 +2840,23 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) svm =to_svm (vcpu); -if (vcpu->arch.guest_state_protected) -sev_flush_encrypted_page (vcpu, svm->sev_es.vmsa); -/* * If its an SNP guest, then VMSA was added in the RMP entry as * a guest owned page. Transition the page to hyperivosr state * before releasing it back to the system. +* Also the page is removed from the kernel direct map, so flush it +* later after it is transitioned back to hypervisor state and +* restored in the direct map. */ if (sev_snp_guest (vcpu->kvm)) {u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ;if (host_rmp_make_shared (pfn,PG_LEVEL_4K , false)) goto skip_vmsa_free; } +if (vcpu->arch.guest_state_protected) +sev_flush_encrypted_page (vcpu, svm->sev_es.vmsa); +__free_page (virt_to_page (svm->sev_es.vmsa)); skip_vmsa_free:
This will be part of the next hypervisor patches which we will be posting next. Thanks,
Ashish
PATCH v5 28/45 KVM: X86: Keep the NPT and RMP page level in sync
In reply to Brijesh SinghWhen running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the NPT translation (gva->gpa->spa). The NPT page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 + arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 5 ++++ arch/x86/kvm/svm/sev.c | 46 ++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + 6 files changed, 56 insertions(+)
arch/x86/include/asm/kvm-x86-ops.h +1 -0
@@ -123,6 +123,7 @@ KVM_X86_OP_NULL(migrate_timers) KVM_X86_OP(msr_filter_changed) KVM_X86_OP_NULL(complete_emulated_msr) KVM_X86_OP(alloc_apic_backing_page) +KVM_X86_OP_NULL(rmp_page_level_adjust)#undef KVM_X86_OP#undef KVM_X86_OP_NULL
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1453,7 +1453,9 @@ struct kvm_x86_ops {int (*complete_emulated_msr)(struct kvm_vcpu *vcpu,int err);void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); +void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); +void (*rmp_page_level_adjust)(struct kvm *kvm,kvm_pfn_t pfn,int *level); };struct kvm_x86_nested_ops {
arch/x86/kvm/mmu/mmu.c +5 -0
@@ -43,6 +43,7 @@#include <linux/hash.h> #include <linux/kern_levels.h> #include <linux/kthread.h> +#include <linux/sev.h> #include <asm/page.h> #include <asm/memtype.h>
@@ -2818,6 +2819,10 @@ static int host_pfn_mapping_level(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (unlikely (!pte))return PG_LEVEL_4K ; +/* Adjust the page level based on the SEV-SNP RMP page level. */ +if (kvm_x86_ops.rmp_page_level_adjust) +static_call (kvm_x86_rmp_page_level_adjust)(kvm, pfn, &level); +return level; }
arch/x86/kvm/svm/sev.c +46 -0
49 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +1 -0
@@ -4679,6 +4679,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + .rmp_page_level_adjust = sev_rmp_page_level_adjust, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +1 -0
@@ -589,6 +589,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);void sev_es_prepare_guest_switch (struct vcpu_svm *svm,unsigned int cpu);void sev_es_unmap_ghcb (struct vcpu_svm *svm);struct page *snp_safe_alloc_page (struct kvm_vcpu *vcpu); +void sev_rmp_page_level_adjust (struct kvm *kvm,kvm_pfn_t pfn,int *level);/* vmenter.S */
Signature · 1 line
2.17.1
PATCH v5 29/45 KVM: x86/mmu: Move 'pfn' variable to caller of direct_page_fault()
In reply to Brijesh SinghFrom: Sean Christopherson <[email protected]>
When adding pages prior to boot, TDX will need the resulting host pfn so that it can be passed to TDADDPAGE (TDX-SEAM always works with physical addresses as it has its own page tables). Start plumbing pfn back up the page fault stack.
--- arch/x86/kvm/mmu/mmu.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
arch/x86/kvm/mmu/mmu.c +14 -10
@@ -3818,7 +3818,8 @@ static bool try_async_pf(struct kvm_vcpu *vcpu, bool prefault, gfn_t gfn, }static int direct_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code, -bool prefault,int max_level,bool is_tdp) +bool prefault,int max_level,bool is_tdp, +kvm_pfn_t *pfn) {bool is_tdp_mmu_fault =is_tdp_mmu (vcpu->arch.mmu);bool write = error_code &PFERR_WRITE_MASK ;
@@ -3826,7 +3827,6 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,gfn_t gfn = gpa >>PAGE_SHIFT ;unsigned long mmu_seq; -kvm_pfn_t pfn;hva_t hva;int r;
@@ -3846,11 +3846,11 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, mmu_seq = vcpu->kvm->mmu_notifier_seq;smp_rmb (); -if (try_async_pf (vcpu, prefault, gfn, gpa, &pfn, &hva, +if (try_async_pf (vcpu, prefault, gfn, gpa, pfn, &hva, write, &map_writable))return RET_PF_RETRY ; -if (handle_abnormal_pfn (vcpu, is_tdp ?0 : gpa, gfn, pfn,ACC_ALL , &r)) +if (handle_abnormal_pfn (vcpu, is_tdp ?0 : gpa, gfn, *pfn,ACC_ALL , &r))return r; r =RET_PF_RETRY ;
@@ -3860,7 +3860,8 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,else write_lock (&vcpu->kvm->mmu_lock); -if (!is_noslot_pfn (pfn) &&mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, hva)) +if (!is_noslot_pfn (*pfn) && +mmu_notifier_retry_hva (vcpu->kvm, mmu_seq, hva)) goto out_unlock; r =make_mmu_pages_available (vcpu);if (r)
@@ -3868,9 +3869,9 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,if (is_tdp_mmu_fault) r =kvm_tdp_mmu_map (vcpu, gpa, error_code, map_writable, max_level, - pfn, prefault); + *pfn, prefault);else - r =__direct_map (vcpu, gpa, error_code, map_writable, max_level, pfn, + r =__direct_map (vcpu, gpa, error_code, map_writable, max_level, *pfn, prefault, is_tdp); out_unlock:
@@ -3878,18 +3879,20 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,read_unlock (&vcpu->kvm->mmu_lock); else write_unlock(&vcpu->kvm->mmu_lock); -kvm_release_pfn_clean (pfn); +kvm_release_pfn_clean (*pfn);return r; }static int nonpaging_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault) { +kvm_pfn_t pfn; +pgprintk ("%s: gva %lx error %x\n" , __func__, gpa, error_code);/* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ return direct_page_fault (vcpu, gpa &PAGE_MASK , error_code, prefault, -PG_LEVEL_2M , false); +PG_LEVEL_2M , false, &pfn); } int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
@@ -3928,6 +3931,7 @@ EXPORT_SYMBOL_GPL(kvm_handle_page_fault);int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault) { +kvm_pfn_t pfn;int max_level; for (max_level = KVM_MAX_HUGEPAGE_LEVEL;
@@ -3941,7 +3945,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, }return direct_page_fault (vcpu, gpa, error_code, prefault, - max_level, true); + max_level, true, &pfn); }static void nonpaging_init_context (struct kvm_mmu *context)
Signature · 1 line
2.17.1
PATCH v5 30/45 KVM: x86/mmu: Introduce kvm_mmu_map_tdp_page() for use by TDX and SNP
In reply to Brijesh SinghFrom: Sean Christopherson <[email protected]>
Introduce a helper to directly (pun intended) fault-in a TDP page without having to go through the full page fault path. This allows TDX to get the resulting pfn and also allows the RET_PF_* enums to stay in mmu.c where they belong.
--- arch/x86/kvm/mmu.h | 3 +++ arch/x86/kvm/mmu/mmu.c | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+)
arch/x86/kvm/mmu.h +3 -0
@@ -127,6 +127,9 @@ static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,return vcpu->arch.mmu->page_fault (vcpu, cr2_or_gpa, err, prefault); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level); +/* * Currently, we have two sorts of write-protection, a) the first one * write-protects guest page to sync the guest modification, b) another one is
arch/x86/kvm/mmu/mmu.c +25 -0
@@ -3948,6 +3948,31 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, max_level, true, &pfn); } +kvm_pfn_t kvm_mmu_map_tdp_page (struct kvm_vcpu *vcpu,gpa_t gpa, +u32 error_code,int max_level) +{ +kvm_pfn_t pfn; +int r; + +if (mmu_topup_memory_caches (vcpu, false)) +return KVM_PFN_ERR_FAULT ; + +/* +* Loop on the page fault path to handle the case where an mmu_notifier +* invalidation triggers RET_PF_RETRY. In the normal page fault path, +* KVM needs to resume the guest in case the invalidation changed any +* of the page fault properties, i.e. the gpa or error code. For this +* path, the gpa and error code are fixed by the caller, and the caller +* expects failure if and only if the page fault can't be fixed. +*/ +do { + r =direct_page_fault (vcpu, gpa, error_code, false, max_level, + true, &pfn); + }while (r ==RET_PF_RETRY && !is_error_noslot_pfn (pfn)); +return pfn; +} +EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.17.1
PATCH v5 31/45 KVM: x86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use
In reply to Brijesh SinghThe SEV-SNP VMs may call the page state change VMGEXIT to add the GPA as private or shared in the RMP table. The page state change VMGEXIT will contain the RMP page level to be used in the RMP entry. If the page level between the TDP and RMP does not match then, it will result in nested-page-fault (RMP violation).
The SEV-SNP VMGEXIT handler will use the kvm_mmu_get_tdp_walk() to get the current page-level in the TDP for the given GPA and calculate a workable page level. If a GPA is mapped as a 4K-page in the TDP, but the guest requested to add the GPA as a 2M in the RMP entry then the 2M request will be broken into 4K-pages to keep the RMP and TDP page-levels in sync.
--- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+)
arch/x86/kvm/mmu.h +2 -0
@@ -117,6 +117,8 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu)int kvm_tdp_page_fault (struct kvm_vcpu *vcpu,gpa_t gpa,u32 error_code,bool prefault); +bool kvm_mmu_get_tdp_walk (struct kvm_vcpu *vcpu,gpa_t gpa,kvm_pfn_t *pfn,int *level); +static inline int kvm_mmu_do_page_fault (struct kvm_vcpu *vcpu,gpa_t cr2_or_gpa,u32 err,bool prefault) {
arch/x86/kvm/mmu/mmu.c +29 -0
@@ -3973,6 +3973,35 @@ kvm_pfn_t kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, }EXPORT_SYMBOL_GPL (kvm_mmu_map_tdp_page); +bool kvm_mmu_get_tdp_walk (struct kvm_vcpu *vcpu,gpa_t gpa,kvm_pfn_t *pfn,int *level) +{ +u64 sptes[PT64_ROOT_MAX_LEVEL +1 ]; +int leaf, root; + +if (is_tdp_mmu (vcpu->arch.mmu)) + leaf =kvm_tdp_mmu_get_walk (vcpu, gpa, sptes, &root); +else + leaf =get_walk (vcpu, gpa, sptes, &root); + +if (unlikely (leaf <0 )) +return false; + +/* Check if the leaf SPTE is present */ +if (!is_shadow_present_pte (sptes[leaf])) +return false; + + *pfn =spte_to_pfn (sptes[leaf]); +if (leaf >PG_LEVEL_4K ) { +u64 page_mask =KVM_PAGES_PER_HPAGE (leaf) -KVM_PAGES_PER_HPAGE (leaf -1 ); + *pfn |= (gpa_to_gfn (gpa) & page_mask); + } + + *level = leaf; + +return true; +} +EXPORT_SYMBOL_GPL (kvm_mmu_get_tdp_walk); +static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault;
Signature · 1 line
2.17.1
PATCH v5 32/45 KVM: x86: Define RMP page fault error bits for #NPF
In reply to Brijesh SinghWhen SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF).
See APM2 section 16.36.10 for more details.
--- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/include/asm/kvm_host.h +8 -0
@@ -239,8 +239,12 @@ enum x86_intercept_stage;#define PFERR_FETCH_BIT 4#define PFERR_PK_BIT 5#define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31#define PFERR_GUEST_FINAL_BIT 32#define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
@@ -251,6 +255,10 @@ enum x86_intercept_stage;#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 32/45] KVM: x86: Define RMP page fault error bits for #NPF
Quoted 7 lines · Brijesh Singh
When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF).
See APM2 section 16.36.10 for more details.
nit: Section # should be 15.36.10 (rather than 16.36.10). Also, is it better to put section headings, rather than numbers in the commit logs and comments? Someone mentioned to me that the section numbering in APM and SDM can move around over time, but the section titles tend to be more stable. I'm not sure how true this is, so feel free to disregard this comment.
Quoted 37 lines
Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/include/asm/kvm_host.h +8
-1@@ -239,8 +239,12 @@ enum x86_intercept_stage;#define PFERR_FETCH_BIT 4#define PFERR_PK_BIT 5#define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31#define PFERR_GUEST_FINAL_BIT 32#define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)@@ -251,6 +255,10 @@ enum x86_intercept_stage;#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT)#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ --2.17.1
Borislav Petkov
maintainerRe: [PATCH Part2 v5 32/45] KVM: x86: Define RMP page fault error bits for #NPF
Quoted 7 lines · Marc Orr
Quoted 6 lines · Brijesh Singh
When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hypervisor or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF).
Quoted 8 lines
Quoted 1 line
See APM2 section 16.36.10 for more details.
nit: Section # should be 15.36.10 (rather than 16.36.10). Also, is it better to put section headings, rather than numbers in the commit logs and comments? Someone mentioned to me that the section numbering in APM and SDM can move around over time, but the section titles tend to be more stable. I'm not sure how true this is, so feel free to disregard this comment.
No, that comment is correct, please make it unambiguous so that if someone's looking later, someone can find the section even in future docs. (I'm hoping they don't change headings, that is...).
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v5 33/45 KVM: x86: Update page-fault trace to log full 64-bit error code
In reply to Brijesh SinghThe #NPT error code is a 64-bit value but the trace prints only the lower 32-bits. Some of the fault error code (e.g PFERR_GUEST_FINAL_MASK) are available in the upper 32-bits.
--- arch/x86/kvm/trace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/trace.h +3 -3
@@ -365,12 +365,12 @@ TRACE_EVENT(kvm_inj_exception,* Tracepoint for page fault. */ TRACE_EVENT(kvm_page_fault, - TP_PROTO(unsigned long fault_address, unsigned int error_code), + TP_PROTO(unsigned long fault_address, u64 error_code), TP_ARGS(fault_address, error_code), TP_STRUCT__entry( __field( unsigned long, fault_address ) - __field( unsigned int, error_code ) + __field( u64, error_code ) ), TP_fast_assign(
@@ -378,7 +378,7 @@ TRACE_EVENT(kvm_page_fault, __entry->error_code = error_code; ), -TP_printk ("address %lx error_code %x" , +TP_printk ("address %lx error_code %llx" , __entry->fault_address, __entry->error_code) );
Signature · 1 line
2.17.1
Re: [PATCH Part2 v5 33/45] KVM: x86: Update page-fault trace to log full 64-bit error code
Quoted 7 lines · Brijesh Singh
The #NPT error code is a 64-bit value but the trace prints only the lower 32-bits. Some of the fault error code (e.g
PFERR_GUEST_FINAL_MASK) are available in the upper 32-bits.Cc <[email protected]>Signed-off-by Brijesh Singh <[email protected]>---
Paolo, can you grab this one sooner than later? Alternatively, maybe post this separately Brijesh?
PATCH v5 34/45 KVM: SVM: Do not use long-lived GHCB map while setting scratch area
In reply to Brijesh SinghThe setup_vmgexit_scratch() function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.
--- arch/x86/kvm/svm/sev.c | 70 +++++++++++++++++++----------------------- arch/x86/kvm/svm/svm.h | 3 +- 2 files changed, 34 insertions(+), 39 deletions(-)
arch/x86/kvm/svm/sev.c +32 -38
@@ -2490,8 +2490,7 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu)__free_page (virt_to_page (svm->vmsa)); skip_vmsa_free: -if (svm->ghcb_sa_free) -kfree (svm->ghcb_sa); +kfree (svm->ghcb_sa); }static void dump_ghcb (struct vcpu_svm *svm)
@@ -2579,6 +2578,9 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm) control->exit_info_1 =ghcb_get_sw_exit_info_1 (ghcb); control->exit_info_2 =ghcb_get_sw_exit_info_2 (ghcb); +/* Copy the GHCB scratch area GPA */ + svm->ghcb_sa_gpa =ghcb_get_sw_scratch (ghcb); +/* Clear the valid entries fields */ memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap)); }
@@ -2714,22 +2716,12 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)if (!svm->ghcb)return ; -if (svm->ghcb_sa_free) { -/* -* The scratch area lives outside the GHCB, so there is a -* buffer that, depending on the operation performed, may -* need to be synced, then freed. -*/ -if (svm->ghcb_sa_sync) { -kvm_write_guest (svm->vcpu.kvm, -ghcb_get_sw_scratch (svm->ghcb), - svm->ghcb_sa, svm->ghcb_sa_len); - svm->ghcb_sa_sync = false; - } - -kfree (svm->ghcb_sa); - svm->ghcb_sa =NULL ; - svm->ghcb_sa_free = false; +/* Sync the scratch buffer area. */ +if (svm->ghcb_sa_sync) { +kvm_write_guest (svm->vcpu.kvm, +ghcb_get_sw_scratch (svm->ghcb), + svm->ghcb_sa, svm->ghcb_sa_len); + svm->ghcb_sa_sync = false; }trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, svm->ghcb);
@@ -2767,12 +2759,11 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)static bool setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) {struct vmcb_control_area *control = &svm->vmcb->control; -struct ghcb *ghcb = svm->ghcb;u64 ghcb_scratch_beg, ghcb_scratch_end;u64 scratch_gpa_beg, scratch_gpa_end;void *scratch_va; - scratch_gpa_beg =ghcb_get_sw_scratch (ghcb); + scratch_gpa_beg = svm->ghcb_sa_gpa;if (!scratch_gpa_beg) {pr_err ("vmgexit: scratch gpa not provided\n" );return false;
@@ -2802,9 +2793,6 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg, scratch_gpa_end);return false; } - - scratch_va = (void *)svm->ghcb; - scratch_va += (scratch_gpa_beg - control->ghcb_gpa); }else {/* * The guest memory must be read into a kernel buffer, so
@@ -2815,29 +2803,35 @@ static bool setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT);return false; } + } + +if (svm->ghcb_sa_alloc_len < len) { scratch_va =kzalloc (len,GFP_KERNEL_ACCOUNT );if (!scratch_va)return false; -if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { -/* Unable to copy scratch area from guest */ -pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); - -kfree (scratch_va); -return false; - } -/* -* The scratch area is outside the GHCB. The operation will -* dictate whether the buffer needs to be synced before running -* the vCPU next time (i.e. a read was requested so the data -* must be written back to the guest memory). +* Free the old scratch area and switch to using newly +* allocated. */ - svm->ghcb_sa_sync = sync; - svm->ghcb_sa_free = true; +kfree (svm->ghcb_sa); + + svm->ghcb_sa_alloc_len = len; + svm->ghcb_sa = scratch_va; } - svm->ghcb_sa = scratch_va; +if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, svm->ghcb_sa, len)) { +/* Unable to copy scratch area from guest */ +pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); +return false; + } + +/* +* The operation will dictate whether the buffer needs to be synced +* before running the vCPU next time (i.e. a read was requested so +* the data must be written back to the guest memory). +*/ + svm->ghcb_sa_sync = sync; svm->ghcb_sa_len = len;return true;
arch/x86/kvm/svm/svm.h +2 -1
@@ -199,8 +199,9 @@ struct vcpu_svm {/* SEV-ES scratch area support */ void *ghcb_sa;u64 ghcb_sa_len; +u64 ghcb_sa_gpa; +u32 ghcb_sa_alloc_len;bool ghcb_sa_sync; -bool ghcb_sa_free;bool guest_state_loaded; };
Signature · 1 line
2.17.1
Sean Christopherson
maintainerRe: [PATCH Part2 v5 34/45] KVM: SVM: Do not use long-lived GHCB map while setting scratch area
Quoted 5 lines · Brijesh Singh
The
setup_vmgexit_scratch()function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.
Would it make sense to post this patch and the next (Remove the long-lived GHCB host map) in a separate mini-series? It's needed for SNP, but AFAICT there's nothing that depends on SNP. Getting this merged ahead of time would reduce the size of the SNP series by a smidge.
Re: [PATCH Part2 v5 34/45] KVM: SVM: Do not use long-lived GHCB map while setting scratch area
Quoted 10 lines · Sean Christopherson
Quoted 5 lines · Brijesh Singh
The
setup_vmgexit_scratch()function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.Would it make sense to post this patch and the next (Remove the long-lived GHCB host map) in a separate mini-series? It's needed for SNP, but AFAICT there's nothing that depends on SNP. Getting this merged ahead of time would reduce the size of the SNP series by a smidge.
While testing with random configs, I am seeing some might_sleep() warns. This is happening mainly because during the vmrun the GHCB is accessed with preempt disabled. The kvm_vcpu_map() -> kmap() reports the warning. I am leaning towards creating a cache on the vmgexit and use that cache instead of the doing a kmap() on every access. Does that sound okay to you ?
-Brijesh
Sean Christopherson
maintainerRe: [PATCH Part2 v5 34/45] KVM: SVM: Do not use long-lived GHCB map while setting scratch area
Quoted 13 lines · Brijesh Singh
Quoted 10 lines · Sean Christopherson
Quoted 5 lines · Brijesh Singh
The
setup_vmgexit_scratch()function may rely on a long-lived GHCB mapping if the GHCB shared buffer area was used for the scratch area. In preparation for eliminating the long-lived GHCB mapping, always allocate a buffer for the scratch area so it can be accessed without the GHCB mapping.Would it make sense to post this patch and the next (Remove the long-lived GHCB host map) in a separate mini-series? It's needed for SNP, but AFAICT there's nothing that depends on SNP. Getting this merged ahead of time would reduce the size of the SNP series by a smidge.
Quoted 5 lines
While testing with random configs, I am seeing some
might_sleep()warns. This is happening mainly because during the vmrun the GHCB is accessed with preempt disabled. Thekvm_vcpu_map()->kmap()reports the warning. I am leaning towards creating a cache on the vmgexit and use that cache instead of the doing akmap()on every access. Does that sound okay to you ?
Since SEV is 64-bit only, it should be ok to add a kvm_vcpu_map_atomic() variant.