KVM: SEV: Support direct setting of VMSA for SEV-SNP guests
cover KVM: SEV: Support direct setting of VMSA for SEV-SNP guests
From: Joerg Roedel <[email protected]>
Hi,
Here is a set of patches to support the VMM to provide a VMSA directly to KVM which will then be used by the BSP of the SEV-SNP VM. The use-case is IGVM loading, where the IGVM file contains a VMSA image which must be loaded into the initial memory image of the VM as-is to guarantee the expected launch measurement.
The first patch re-works guest-VMSA handling and streamlines the state handling to make it more clear and maintainable. That patch accounts for the biggest part if the changes.
I have tested these changes together with the planes patches and COCONUT-SVSM and can confirm that the launch measurment is correct again with these changes.
The changes are based on previous work by Roy Hopkins[1].
Please review.
Thanks,
Joerg
[1] https://github.com/torvalds/linux/commit/e00e081276b2cd9f1400ec5b1a9cd97f8b5c4d58
Joerg Roedel (4):
kvm: svm: Streamline VMSA setting for VCPUs
kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
kvm: svm: Support guest-provided VMSA for launching
kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 419 +++++++++++++++++++++++--------- arch/x86/kvm/svm/svm.h | 32 ++- arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 5 files changed, 337 insertions(+), 117 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/4 kvm: svm: Streamline VMSA setting for VCPUs
From: Joerg Roedel <[email protected]>
Streamline the VMSA setting state of vcpus, where a VMSA can be either KVM-allocated or guest-provided. This consolidates the various tracking state around VMSAs.
--- arch/x86/kvm/svm/sev.c | 301 ++++++++++++++++++++++++++++------------- arch/x86/kvm/svm/svm.h | 31 ++++- 2 files changed, 237 insertions(+), 95 deletions(-)
arch/x86/kvm/svm/sev.c +210 -91
@@ -147,6 +147,9 @@ static bool sev_snp_guest(struct kvm *kvm) }static int snp_decommission_context (struct kvm *kvm); +static int kvm_rmp_make_shared (struct kvm *kvm,u64 pfn,enum pg_level level); +static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va); +static int snp_page_reclaim (struct kvm *kvm,u64 pfn);struct enc_region {struct list_head list;
@@ -156,6 +159,173 @@ struct enc_region {unsigned long size; }; +static void *sev_es_vmsa_ref (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa =NULL ; + +if (svm->sev_es.vmsa.vmsa_state ==VMSA_SHARED ) { + vmsa =page_address (svm->sev_es.vmsa.vmsa_page); + } + +return vmsa; +} + +static int sev_es_vcpu_alloc_vmsa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +struct page *vmsa_page; + +if (WARN_ON_ONCE (svm->sev_es.vmsa.vmsa_state !=VMSA_NONE )) +return -EINVAL ; + +/* +* SEV-ES guests require a separate (from the VMCB) VMSA page used to +* contain the encrypted register state of the guest. +*/ + vmsa_page =snp_safe_alloc_page (); +if (!vmsa_page) +return -ENOMEM ; + + svm->sev_es.vmsa.vmsa_state =VMSA_SHARED ; + svm->sev_es.vmsa.vmsa_page = vmsa_page; + +return 0 ; +} + +static int sev_es_vcpu_vmsa_make_private (struct kvm_vcpu *vcpu) +{ +struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa =sev_es_vmsa_ref (vcpu); + +if (!vmsa) +return -EINVAL ; + +if (is_sev_snp_guest (vcpu)) { +u64 pfn =__pa (vmsa) >>PAGE_SHIFT ; +int ret; + +/* Transition the VMSA page to a firmware state. */ + ret =rmp_make_private (pfn,INITIAL_VMSA_GPA ,PG_LEVEL_4K , sev->asid, true); +if (ret) +return ret; + } + + svm->sev_es.vmsa.vmsa_state =VMSA_PRIVATE ; + +return 0 ; +} + +static void sev_es_vcpu_free_vmsa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ptr; + +switch (svm->sev_es.vmsa.vmsa_state) { +case VMSA_NONE : +case VMSA_GUEST : +break ; +case VMSA_PRIVATE : + vmsa_ptr =page_address (svm->sev_es.vmsa.vmsa_page); + +if (is_sev_snp_guest (vcpu)) { +u64 pfn =__pa (vmsa_ptr) >>PAGE_SHIFT ; + +if (kvm_rmp_make_shared (vcpu->kvm, pfn,PG_LEVEL_4K )) { +pr_err ("Failed to make VMSA page shared - leaking it to avoid re-use\n" ); + goto out; + } + } + +if (vcpu->arch.guest_state_protected) +sev_flush_encrypted_page (vcpu, vmsa_ptr); + + fallthrough; +case VMSA_SHARED : +__free_page (svm->sev_es.vmsa.vmsa_page); +break ; +default : +BUG (); + } +out: + + svm->sev_es.vmsa.vmsa_page =NULL ; + svm->sev_es.vmsa.vmsa_state =VMSA_NONE ; +} + +static void sev_snp_vcpu_reclaim_vmsa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ptr; +u64 pfn; + +if (WARN_ON_ONCE (!is_sev_snp_guest (vcpu) || + svm->sev_es.vmsa.vmsa_state !=VMSA_PRIVATE )) +return ; + + vmsa_ptr =page_address (svm->sev_es.vmsa.vmsa_page); + pfn =__pa (vmsa_ptr) >>PAGE_SHIFT ; + +if (!snp_page_reclaim (vcpu->kvm, pfn)) +__free_page (svm->sev_es.vmsa.vmsa_page); + + svm->sev_es.vmsa.vmsa_page =NULL ; + svm->sev_es.vmsa.vmsa_state =VMSA_NONE ; +} + +static void sev_es_set_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +sev_es_vcpu_free_vmsa (vcpu); + + svm->sev_es.vmsa.vmsa_state =VMSA_GUEST ; + svm->sev_es.vmsa.vmsa_gpa = vmsa_gpa; +} + +static u64 sev_es_vmsa_pa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +enum vmsa_state vmsa_state = svm->sev_es.vmsa.vmsa_state; +u64 vmsa_pa =INVALID_PAGE ; + +if (vmsa_state ==VMSA_GUEST ) { +gpa_t vmsa_gpa = svm->sev_es.vmsa.vmsa_gpa; +struct kvm_memory_slot *slot; +struct page *page; +kvm_pfn_t pfn; +gfn_t gfn; + + gfn =gpa_to_gfn (vmsa_gpa); + + slot =gfn_to_memslot (vcpu->kvm, gfn); +if (!slot) + goto out; + +/* +* The new VMSA will be private memory guest memory, so retrieve the +* PFN from the gmem backend. +*/ +if (kvm_gmem_get_pfn (vcpu->kvm, slot, gfn, &pfn, &page,NULL )) + goto out; + + vmsa_pa =pfn_to_hpa (pfn); + +/* +* gmem pages aren't currently migratable, but if this ever changes +* then care should be taken to ensure the guest vmsa is pinned +* through some other means. +*/ +kvm_release_page_clean (page); + }else if (vmsa_state ==VMSA_PRIVATE || vmsa_state ==VMSA_SHARED ) { + vmsa_pa =__pa (page_address (svm->sev_es.vmsa.vmsa_page)); + } + +out: +return vmsa_pa; +} +/* Called with the sev_bitmap_lock held, or on shutdown */ static int sev_flush_asids (unsigned int min_asid,unsigned int max_asid) {
@@ -925,7 +1095,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); -struct sev_es_save_area *save = svm->sev_es.vmsa; +struct sev_es_save_area *save =sev_es_vmsa_ref (vcpu);struct xregs_state *xsave;const u8 *s;u8 *d;
@@ -1026,6 +1196,7 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, {struct sev_data_launch_update_vmsa vmsa;struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ref =sev_es_vmsa_ref (vcpu);int ret;if (vcpu->guest_debug) {
@@ -1043,15 +1214,19 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,* the VMSA memory content (i.e it will write the same memory region * with the guest's key), so invalidate it first. */ -clflush_cache_range (svm->sev_es.vmsa,PAGE_SIZE ); +clflush_cache_range (vmsa_ref,PAGE_SIZE ); vmsa.reserved =0 ; vmsa.handle =to_kvm_sev_info (kvm)->handle; - vmsa.address =__sme_pa (svm->sev_es.vmsa); + vmsa.address =__sme_pa (vmsa_ref); vmsa.len =PAGE_SIZE ; ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_VMSA , &vmsa, error);if (ret) -return ret; + goto free_vmsa; + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret) + goto free_vmsa;/* * SEV-ES guests maintain an encrypted version of their FPU
@@ -1069,7 +1244,13 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,* MSR_IA32_DEBUGCTLMSR when guest_state_protected is not set. */ svm_enable_lbrv (vcpu); +return 0 ; + +free_vmsa: +sev_es_vcpu_free_vmsa (vcpu); + +return ret; }static int sev_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp)
@@ -2508,23 +2689,22 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ; +void *vmsa =sev_es_vmsa_ref (vcpu); ret =sev_es_sync_vmsa (svm);if (ret) goto out; -/* Transition the VMSA page to a firmware state. */ - ret =rmp_make_private (pfn,INITIAL_VMSA_GPA ,PG_LEVEL_4K , sev->asid, true); + ret =sev_es_vcpu_vmsa_make_private (vcpu);if (ret) goto out;/* Issue the SNP command to encrypt the VMSA */ - data.address =__sme_pa (svm->sev_es.vmsa); + data.address =__sme_pa (vmsa); ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , &data, &argp->error);if (ret) { -snp_page_reclaim (kvm, pfn); +sev_snp_vcpu_reclaim_vmsa (vcpu); goto out; }
@@ -3593,31 +3773,13 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)void sev_free_vcpu (struct kvm_vcpu *vcpu) { -struct vcpu_svm *svm; +struct vcpu_svm *svm =to_svm (vcpu);if (!is_sev_es_guest (vcpu))return ; - svm =to_svm (vcpu); - -/* -* If it's an SNP guest, then the VMSA was marked in the RMP table as -* a guest-owned page. Transition the page to hypervisor state before -* releasing it back to the system. -*/ -if (is_sev_snp_guest (vcpu)) { -u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ; - -if (kvm_rmp_make_shared (vcpu->kvm, pfn,PG_LEVEL_4K )) - 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)); +sev_es_vcpu_free_vmsa (vcpu); -skip_vmsa_free:__sev_es_unmap_ghcb (svm); }
@@ -4067,10 +4229,7 @@ static int snp_begin_psc(struct vcpu_svm *svm)static void sev_snp_init_protected_guest_state (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_memory_slot *slot; -struct page *page; -kvm_pfn_t pfn; -gfn_t gfn; +u64 vmsa_pa;guard (mutex)(&svm->sev_es.snp_vmsa_mutex);
@@ -4092,46 +4251,17 @@ static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu)*/ vmcb_mark_all_dirty (svm->vmcb); -if (!VALID_PAGE (svm->sev_es.snp_vmsa_gpa)) -return ; - - gfn =gpa_to_gfn (svm->sev_es.snp_vmsa_gpa); - svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; +sev_es_set_guest_vmsa (vcpu, svm->sev_es.req_vmsa_gpa); + vmsa_pa =sev_es_vmsa_pa (vcpu); - slot =gfn_to_memslot (vcpu->kvm, gfn); -if (!slot) +if (!VALID_PAGE (vmsa_pa))return ; -/* -* The new VMSA will be private memory guest memory, so retrieve the -* PFN from the gmem backend. -*/ -if (kvm_gmem_get_pfn (vcpu->kvm, slot, gfn, &pfn, &page,NULL )) -return ; - -/* -* From this point forward, the VMSA will always be a guest-mapped page -* rather than the initial one allocated by KVM in svm->sev_es.vmsa. In -* theory, svm->sev_es.vmsa could be free'd and cleaned up here, but -* that involves cleanups like flushing caches, which would ideally be -* handled during teardown rather than guest boot. Deferring that also -* allows the existing logic for SEV-ES VMSAs to be re-used with -* minimal SNP-specific changes. -*/ - svm->sev_es.snp_has_guest_vmsa = true; -/* Use the new VMSA */ - svm->vmcb->control.vmsa_pa =pfn_to_hpa (pfn); + svm->vmcb->control.vmsa_pa = vmsa_pa;/* Mark the vCPU as runnable */ kvm_set_mp_state (vcpu,KVM_MP_STATE_RUNNABLE ); - -/* -* gmem pages aren't currently migratable, but if this ever changes -* then care should be taken to ensure svm->sev_es.vmsa is pinned -* through some other means. -*/ -kvm_release_page_clean (page); }static int sev_snp_ap_creation (struct vcpu_svm *svm)
@@ -4187,10 +4317,10 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm)return -EINVAL ; } - target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + target_svm->sev_es.req_vmsa_gpa = svm->vmcb->control.exit_info_2;break ;case SVM_VMGEXIT_AP_DESTROY : - target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.req_vmsa_gpa =INVALID_PAGE ;break ;default : vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n",
@@ -4708,20 +4838,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm, bool init_event)struct vmcb *vmcb = svm->vmcb01.ptr; svm->vmcb->control.misc_ctl |=SVM_MISC_ENABLE_SEV_ES ; - -/* -* An SEV-ES guest requires a VMSA area that is a separate from the -* VMCB page. Do not include the encryption mask on the VMSA physical -* address since hardware will access it using the guest key. Note, -* the VMSA will be NULL if this vCPU is the destination for intrahost -* migration, and will be copied later. -*/ -if (!svm->sev_es.snp_has_guest_vmsa) { -if (svm->sev_es.vmsa) - svm->vmcb->control.vmsa_pa =__pa (svm->sev_es.vmsa); -else - svm->vmcb->control.vmsa_pa =INVALID_PAGE ; - } + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (&svm->vcpu);if (cpu_feature_enabled (X86_FEATURE_ALLOWED_SEV_FEATURES )) svm->vmcb->control.allowed_sev_features = sev->vmsa_features |
@@ -4797,7 +4914,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)int sev_vcpu_create (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct page *vmsa_page; +int ret;mutex_init (&svm->sev_es.snp_vmsa_mutex);
@@ -4808,11 +4925,9 @@ int sev_vcpu_create(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate (from the VMCB) VMSA page used to * contain the encrypted register state of the guest. */ - vmsa_page =snp_safe_alloc_page (); -if (!vmsa_page) -return -ENOMEM ; - - svm->sev_es.vmsa =page_address (vmsa_page); + ret =sev_es_vcpu_alloc_vmsa (vcpu); +if (ret) +return ret; vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm);
@@ -5227,12 +5342,14 @@ struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)if (!is_sev_es_guest (vcpu))return NULL ; + vmsa =sev_es_vmsa_ref (vcpu); +/* * If the VMSA has not yet been encrypted, return a pointer to the * current un-encrypted VMSA. */ -if (!vcpu->arch.guest_state_protected) -return (struct vmcb_save_area *)svm->sev_es.vmsa; +if (vmsa) +return vmsa; sev =to_kvm_sev_info (vcpu->kvm);
@@ -5303,8 +5420,10 @@ struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)void sev_free_decrypted_vmsa (struct kvm_vcpu *vcpu,struct vmcb_save_area *vmsa) { +struct vmcb_save_area *vmsa_ptr =sev_es_vmsa_ref (vcpu); +/* If the VMSA has not yet been encrypted, nothing was allocated */ -if (!vcpu->arch.guest_state_protected || !vmsa) +if (vmsa == vmsa_ptr)return ;free_page ((unsigned long )vmsa);
arch/x86/kvm/svm/svm.h +27 -4
@@ -240,9 +240,29 @@ struct svm_nested_state {bool force_msr_bitmap_recalc; }; +enum vmsa_state { +/* No VMSA set */ +VMSA_NONE , +/* VMSA allocated by KVM - Shared in RMP (if applicable) */ +VMSA_SHARED , +/* VMSA allocated by KVM - Guest-private in RMP (SEV-SNP only) */ +VMSA_PRIVATE , +/* Guest-owned VMSA */ +VMSA_GUEST , +}; + +struct sev_es_vmsa_state { +enum vmsa_state vmsa_state; +union { +/* state == (KVM_SHARED || KVM_PRIVATE) */ +struct page *vmsa_page; +/* state == GUEST */ +gpa_t vmsa_gpa; + }; +}; +struct vcpu_sev_es_state {/* SEV-ES support */ -struct sev_es_save_area *vmsa;struct ghcb *ghcb;u8 valid_bitmap[16 ];struct kvm_host_map ghcb_map;
@@ -266,10 +286,13 @@ struct vcpu_sev_es_state {u64 ghcb_registered_gpa; -struct mutex snp_vmsa_mutex;/* Used to handle concurrent updates of VMSA. */ -gpa_t snp_vmsa_gpa; +/* VMSA related state */ +struct mutex snp_vmsa_mutex;/* Used to handle concurrent updates of VMSA. */ +struct sev_es_vmsa_state vmsa;/* VMSA currently used by the VCPU */ +gpa_t req_vmsa_gpa;/* Requested new VMSA GPA */ + +bool snp_ap_runnable;bool snp_ap_waiting_for_reset; -bool snp_has_guest_vmsa; };struct vcpu_svm {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 1/4] kvm: svm: Streamline VMSA setting for VCPUs
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Dynamically freeing an active initial VMSA page during SNP AP creation without using SNP_PAGE_RECLAIM triggers an RMP violation, which bug-halts the VM and leaks memory.
Pre-existing issues:
- [Critical] A malicious SEV-SNP guest can crash the host kernel by punching a hole in a guest_memfd page that is actively assigned as an AP VMSA. --
commit dfce748c773ed97c4a6e2b3a1ea5718862d5c1bf
Author: Joerg Roedel <[email protected]>
kvm: svm: Streamline VMSA setting for VCPUs
This commit streamlines the VMSA setting state of vcpus, allowing a VMSA to be either KVM-allocated or guest-provided. It consolidates the various tracking states around VMSAs.
Quoted 4 lines
arch/x86/kvm/svm/sev.c +0
-0
[ ... ]
Quoted 1 line
arch/x86/kvm/svm/sev.c +0
-0@@ -156,6 +159,173 @@ struct enc_region {
[ ... ]
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+static void sev_es_vcpu_free_vmsa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ptr; + +switch (svm->sev_es.vmsa.vmsa_state) {
arch/x86/kvm/svm/sev.c struct enc_region { · line 235
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+case VMSA_NONE : +case VMSA_GUEST : +break ; +case VMSA_PRIVATE : + vmsa_ptr =page_address (svm->sev_es.vmsa.vmsa_page); + +if (is_sev_snp_guest (vcpu)) { +u64 pfn =__pa (vmsa_ptr) >>PAGE_SHIFT ; + +if (kvm_rmp_make_shared (vcpu->kvm, pfn,PG_LEVEL_4K )) {
[Severity: High]
Does this sequence trigger an RMP violation?
When an SEV-SNP guest provisions an AP and triggers svm_vmgexit_ap_creation(), KVM replaces the initial KVM-managed VMSA with the guest-provided one using sev_es_set_guest_vmsa(), which eagerly frees the old VMSA via sev_es_vcpu_free_vmsa().
For SNP guests, the old VMSA page is in the VMSA_PRIVATE state and has been transitioned to the hardware VMSA state. To transition it back to shared memory, AMD architecture requires reclaiming the page via snp_page_reclaim() before issuing an RMPUPDATE.
Since sev_es_vcpu_free_vmsa() bypasses snp_page_reclaim() and directly calls kvm_rmp_make_shared(), could this fail with an RMP violation, causing the VM to bug-halt and leak the physical page?
Quoted 9 lines
arch/x86/kvm/svm/sev.c +9
-0+pr_err ("Failed to make VMSA page shared - leaking it to avoid re-use\n" ); + goto out; + } + } + +if (vcpu->arch.guest_state_protected) +sev_flush_encrypted_page (vcpu, vmsa_ptr); + + fallthrough;
[ ... ]
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+static u64 sev_es_vmsa_pa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +enum vmsa_state vmsa_state = svm->sev_es.vmsa.vmsa_state; +u64 vmsa_pa =INVALID_PAGE ; + +if (vmsa_state ==VMSA_GUEST ) {
[ ... ]
Quoted 5 lines
arch/x86/kvm/svm/sev.c +5
-0+/* +* The new VMSA will be private memory guest memory, so retrieve the +* PFN from the gmem backend. +*/ +if (kvm_gmem_get_pfn (vcpu->kvm, slot, gfn, &pfn, &page,NULL ))
arch/x86/kvm/svm/sev.c struct enc_region { · line 320
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ goto out; + + vmsa_pa =pfn_to_hpa (pfn); + +/* +* gmem pages aren't currently migratable, but if this ever changes +* then care should be taken to ensure the guest vmsa is pinned +* through some other means. +*/ +kvm_release_page_clean (page);
[Severity: Critical]
This is a pre-existing issue, but does dropping the page reference here allow a malicious guest to crash the host kernel?
When a guest-provided VMSA is used for an AP, KVM resolves the GPA to a PFN and immediately drops its page reference here. The physical address is then programmed into the hardware VMCB.
If a malicious guest issues a Page State Change request to transition this VMSA page back to shared memory, host userspace can handle this by calling fallocate(PUNCH_HOLE) on the guest_memfd. This would trigger sev_gmem_invalidate(), which attempts rmp_make_shared().
Because the page is still in the VMSA state, rmp_make_shared() would fail. sev_gmem_invalidate() only prints a warning and proceeds, freeing the page back to the buddy allocator while its RMP entry is still guest-restricted.
When the host buddy allocator reallocates and accesses this page, would it trigger an RMP violation and a host kernel panic?
Quoted 7 lines
arch/x86/kvm/svm/sev.c +7
-0+ }else if (vmsa_state ==VMSA_PRIVATE || vmsa_state ==VMSA_SHARED ) { + vmsa_pa =__pa (page_address (svm->sev_es.vmsa.vmsa_page)); + } + +out: +return vmsa_pa; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH 1/4] kvm: svm: Streamline VMSA setting for VCPUs
Quoted 8 lines · On Thu, Jun 11, 2026 at 12:56:28PM +0000, [email protected] wrote:
[Severity: High]
Does this sequence trigger an RMP violation?When an SEV-SNP guest provisions an AP and triggers
svm_vmgexit_ap_creation(), KVM replaces the initial KVM-managed VMSA with the guest-provided one usingsev_es_set_guest_vmsa(), which eagerly frees the old VMSA viasev_es_vcpu_free_vmsa().
Quoted 4 lines
For SNP guests, the old VMSA page is in the
VMSA_PRIVATEstate and has been transitioned to the hardware VMSA state. To transition it back to shared memory, AMD architecture requires reclaiming the page viasnp_page_reclaim()before issuing an RMPUPDATE.
Calling snp_page_reclaim() is only needed when the page is reclaimed before LAUNCH_FINISH. Once the VM is running, which is is when it issues a svm_vmgexit_ap_creation() request, then an RMPUPDATE is sufficient.
Quoted 13 lines
[Severity: Critical]
This is a pre-existing issue, but does dropping the page reference here allow a malicious guest to crash the host kernel?When a guest-provided VMSA is used for an AP, KVM resolves the GPA to a PFN and immediately drops its page reference here. The physical address is then programmed into the hardware VMCB.
If a malicious guest issues a Page State Change request to transition this VMSA page back to shared memory, host userspace can handle this by calling
fallocate(PUNCH_HOLE)on theguest_memfd. This would triggersev_gmem_invalidate(), which attemptsrmp_make_shared().
Quoted 6 lines
Because the page is still in the VMSA state,
rmp_make_shared()would fail.sev_gmem_invalidate()only prints a warning and proceeds, freeing the page back to the buddy allocator while its RMP entry is still guest-restricted.When the host buddy allocator reallocates and accesses this page, would it trigger an RMP violation and a host kernel panic?
This is not an issue because the RMPUPDATE will only fail when the VMSA is currently used by a CPU in guest-mode (== its VCPU is running).
But in the sev_es_vcpu_free_vmsa() path this can not happen because this function is always called in the context of the affecting VCPUs thread, which makes sure that the VCPU is not in guest-mode.
-Joerg
Re: [PATCH 1/4] kvm: svm: Streamline VMSA setting for VCPUs
In reply to Jörg RödelQuoted 17 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Streamline the VMSA setting state of vcpus, where a VMSA can be either KVM-allocated or guest-provided. This consolidates the various tracking state around VMSAs.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 301 ++++++++++++++++++++++++++++------------- arch/x86/kvm/svm/svm.h | 31 ++++- 2 files changed, 237 insertions(+), 95 deletions(-)arch/x86/kvm/svm/sev.c +0
-0
arch/x86/kvm/svm/sev.c static bool sev_snp_guest(struct kvm *kvm) · line 152
Quoted 6 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -147,6 +147,9 @@ static bool sev_snp_guest(struct kvm *kvm) }static int snp_decommission_context (struct kvm *kvm); +static int kvm_rmp_make_shared (struct kvm *kvm,u64 pfn,enum pg_level level); +static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va); +static int snp_page_reclaim (struct kvm *kvm,u64 pfn);
Can this be worked so that we don't add more forward declarations?
arch/x86/kvm/svm/sev.c struct enc_region { · line 162
Quoted 8 lines
arch/x86/kvm/svm/sev.c +1
-0struct enc_region {struct list_head list;@@ -156,6 +159,173 @@ struct enc_region {unsigned long size; }; +static void *sev_es_vmsa_ref (struct kvm_vcpu *vcpu)
sev_es_get_kvm_shared_vmsa ?
arch/x86/kvm/svm/sev.c struct enc_region { · line 171
Quoted 9 lines
arch/x86/kvm/svm/sev.c +9
-0+{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa =NULL ; + +if (svm->sev_es.vmsa.vmsa_state ==VMSA_SHARED ) { + vmsa =page_address (svm->sev_es.vmsa.vmsa_page); + } + +return vmsa;
How about just:
if (svm->sev_es.vmsa.vmsa_state == VMSA_SHARED) return page_address(svm->sev_es.vmsa.vmsa_page); return NULL;
arch/x86/kvm/svm/sev.c struct enc_region { · line 174
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+} + +static int sev_es_vcpu_alloc_vmsa (struct kvm_vcpu *vcpu)
sev_es_alloc_kvm_vmsa ?
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+{ +struct vcpu_svm *svm =to_svm (vcpu); +struct page *vmsa_page; + +if (WARN_ON_ONCE (svm->sev_es.vmsa.vmsa_state !=VMSA_NONE )) +return -EINVAL ; + +/* +* SEV-ES guests require a separate (from the VMCB) VMSA page used to +* contain the encrypted register state of the guest. +*/ + vmsa_page =snp_safe_alloc_page ();
arch/x86/kvm/svm/sev.c struct enc_region { · line 196
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (!vmsa_page) +return -ENOMEM ; + + svm->sev_es.vmsa.vmsa_state =VMSA_SHARED ; + svm->sev_es.vmsa.vmsa_page = vmsa_page; + +return 0 ; +} + +static int sev_es_vcpu_vmsa_make_private (struct kvm_vcpu *vcpu)
sev_es_make_kvm_vmsa_private ?
Quoted 14 lines
arch/x86/kvm/svm/sev.c +14
-0+{ +struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa =sev_es_vmsa_ref (vcpu); + +if (!vmsa) +return -EINVAL ; + +if (is_sev_snp_guest (vcpu)) { +u64 pfn =__pa (vmsa) >>PAGE_SHIFT ; +int ret; + +/* Transition the VMSA page to a firmware state. */ + ret =rmp_make_private (pfn,INITIAL_VMSA_GPA ,PG_LEVEL_4K , sev->asid, true);
arch/x86/kvm/svm/sev.c struct enc_region { · line 220
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+if (ret) +return ret; + } + + svm->sev_es.vmsa.vmsa_state =VMSA_PRIVATE ; + +return 0 ; +} + +static void sev_es_vcpu_free_vmsa (struct kvm_vcpu *vcpu)
sev_es_free_kvm_vmsa ?
arch/x86/kvm/svm/sev.c struct enc_region { · line 223
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ptr;
s/vmsa_ptr/vmsa/
arch/x86/kvm/svm/sev.c struct enc_region { · line 233
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +switch (svm->sev_es.vmsa.vmsa_state) { +case VMSA_NONE : +case VMSA_GUEST : +break ; +case VMSA_PRIVATE : + vmsa_ptr =page_address (svm->sev_es.vmsa.vmsa_page); + +if (is_sev_snp_guest (vcpu)) { +u64 pfn =__pa (vmsa_ptr) >>PAGE_SHIFT ;
PHYS_PFN(__pa(vmsa));
arch/x86/kvm/svm/sev.c struct enc_region { · line 237
Quoted 4 lines
arch/x86/kvm/svm/sev.c +4
-0+ +if (kvm_rmp_make_shared (vcpu->kvm, pfn,PG_LEVEL_4K )) { +pr_err ("Failed to make VMSA page shared - leaking it to avoid re-use\n" ); + goto out;
s/goto out/break/
arch/x86/kvm/svm/sev.c struct enc_region { · line 249
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+ } + } + +if (vcpu->arch.guest_state_protected) +sev_flush_encrypted_page (vcpu, vmsa_ptr); + + fallthrough; +case VMSA_SHARED : +__free_page (svm->sev_es.vmsa.vmsa_page); +break ; +default : +BUG ();
WARN_ON or WARN_ON_ONCE() instead of BUG().
arch/x86/kvm/svm/sev.c struct enc_region { · line 251
Quoted 2 lines
arch/x86/kvm/svm/sev.c +2
-0+ } +out:
If using the 'break' above, then no need for this label.
arch/x86/kvm/svm/sev.c struct enc_region { · line 257
Quoted 6 lines
arch/x86/kvm/svm/sev.c +6
-0+ + svm->sev_es.vmsa.vmsa_page =NULL ; + svm->sev_es.vmsa.vmsa_state =VMSA_NONE ; +} + +static void sev_snp_vcpu_reclaim_vmsa (struct kvm_vcpu *vcpu)
sev_snp_reclaim_kvm_vmsa ?
arch/x86/kvm/svm/sev.c struct enc_region { · line 223
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ptr;
s/vmsa_ptr/vmsa/
arch/x86/kvm/svm/sev.c struct enc_region { · line 268
Quoted 8 lines
arch/x86/kvm/svm/sev.c +8
-0+u64 pfn; + +if (WARN_ON_ONCE (!is_sev_snp_guest (vcpu) || + svm->sev_es.vmsa.vmsa_state !=VMSA_PRIVATE )) +return ; + + vmsa_ptr =page_address (svm->sev_es.vmsa.vmsa_page); + pfn =__pa (vmsa_ptr) >>PAGE_SHIFT ;
PHYS_PFN()
arch/x86/kvm/svm/sev.c struct enc_region { · line 271
Quoted 3 lines
arch/x86/kvm/svm/sev.c +3
-0+ +if (!snp_page_reclaim (vcpu->kvm, pfn)) +__free_page (svm->sev_es.vmsa.vmsa_page);
Should you issue a message here similar to above about leaking the page if snp_page_reclaim() fails?
Quoted 12 lines
arch/x86/kvm/svm/sev.c +12
-0+ + svm->sev_es.vmsa.vmsa_page =NULL ; + svm->sev_es.vmsa.vmsa_state =VMSA_NONE ; +} + +static void sev_es_set_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +struct vcpu_svm *svm =to_svm (vcpu); + +sev_es_vcpu_free_vmsa (vcpu); + + svm->sev_es.vmsa.vmsa_state =VMSA_GUEST ;
arch/x86/kvm/svm/sev.c struct enc_region { · line 293
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ svm->sev_es.vmsa.vmsa_gpa = vmsa_gpa; +} + +static u64 sev_es_vmsa_pa (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +enum vmsa_state vmsa_state = svm->sev_es.vmsa.vmsa_state; +u64 vmsa_pa =INVALID_PAGE ; + +if (vmsa_state ==VMSA_GUEST ) {
Would a switch statement like you have in sev_es_vcpu_free_vmsa() be more consistent?
Also, you could return INVALID_PAGE directly instead of having the goto's.
switch (svm->sev_es.vmsa.vmsa_state) {
case VMSA_NONE:
return INVALID_PAGE;
case VMSA_SHARED:
case VMSA_PRIVATE:
return __pa(page_address(svm->sev_es.vmsa.vmsa_page));
case VMSA_GUEST:
...
}Quoted 48 lines
arch/x86/kvm/svm/sev.c +36
-1+gpa_t vmsa_gpa = svm->sev_es.vmsa.vmsa_gpa; +struct kvm_memory_slot *slot; +struct page *page; +kvm_pfn_t pfn; +gfn_t gfn; + + gfn =gpa_to_gfn (vmsa_gpa); + + slot =gfn_to_memslot (vcpu->kvm, gfn); +if (!slot) + goto out; + +/* +* The new VMSA will be private memory guest memory, so retrieve the +* PFN from the gmem backend. +*/ +if (kvm_gmem_get_pfn (vcpu->kvm, slot, gfn, &pfn, &page,NULL )) + goto out; + + vmsa_pa =pfn_to_hpa (pfn); + +/* +* gmem pages aren't currently migratable, but if this ever changes +* then care should be taken to ensure the guest vmsa is pinned +* through some other means. +*/ +kvm_release_page_clean (page); + }else if (vmsa_state ==VMSA_PRIVATE || vmsa_state ==VMSA_SHARED ) { + vmsa_pa =__pa (page_address (svm->sev_es.vmsa.vmsa_page)); + } + +out: +return vmsa_pa; +} +/* Called with the sev_bitmap_lock held, or on shutdown */ static int sev_flush_asids (unsigned int min_asid,unsigned int max_asid) {@@ -925,7 +1095,7 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); -struct sev_es_save_area *save = svm->sev_es.vmsa; +struct sev_es_save_area *save =sev_es_vmsa_ref (vcpu);struct xregs_state *xsave;const u8 *s;u8 *d;
arch/x86/kvm/svm/sev.c static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, · line 1199
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -1026,6 +1196,7 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, {struct sev_data_launch_update_vmsa vmsa;struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa_ref =sev_es_vmsa_ref (vcpu);
Might make more sense rename the sev_data_launch_update_vmsa variable to vmsa_lu and keep this as just vmsa ?\
Quoted 13 lines
arch/x86/kvm/svm/sev.c +1
-2int ret;if (vcpu->guest_debug) {@@ -1043,15 +1214,19 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,* the VMSA memory content (i.e it will write the same memory region * with the guest's key), so invalidate it first. */ -clflush_cache_range (svm->sev_es.vmsa,PAGE_SIZE ); +clflush_cache_range (vmsa_ref,PAGE_SIZE ); vmsa.reserved =0 ; vmsa.handle =to_kvm_sev_info (kvm)->handle; - vmsa.address =__sme_pa (svm->sev_es.vmsa);
arch/x86/kvm/svm/sev.c static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, · line 1229
Quoted 10 lines
arch/x86/kvm/svm/sev.c +6
-1+ vmsa.address =__sme_pa (vmsa_ref); vmsa.len =PAGE_SIZE ; ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_VMSA , &vmsa, error);if (ret) -return ret; + goto free_vmsa; + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret) + goto free_vmsa;
Similar to the SNP path, you can probably move this to before the LAUNCH_UPDATE, just for consistency.
Quoted 35 lines
arch/x86/kvm/svm/sev.c +8
-3/* * SEV-ES guests maintain an encrypted version of their FPU @@ -1069,7 +1244,13 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,* MSR_IA32_DEBUGCTLMSR when guest_state_protected is not set. */ svm_enable_lbrv (vcpu); +return 0 ; + +free_vmsa: +sev_es_vcpu_free_vmsa (vcpu); + +return ret; }static int sev_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp)@@ -2508,23 +2689,22 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ; +void *vmsa =sev_es_vmsa_ref (vcpu); ret =sev_es_sync_vmsa (svm);if (ret) goto out; -/* Transition the VMSA page to a firmware state. */ - ret =rmp_make_private (pfn,INITIAL_VMSA_GPA ,PG_LEVEL_4K , sev->asid, true); + ret =sev_es_vcpu_vmsa_make_private (vcpu);if (ret) goto out;/* Issue the SNP command to encrypt the VMSA */
arch/x86/kvm/svm/sev.c static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2710
Quoted 10 lines
arch/x86/kvm/svm/sev.c +2
-2- data.address =__sme_pa (svm->sev_es.vmsa); + data.address =__sme_pa (vmsa); ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , &data, &argp->error);if (ret) { -snp_page_reclaim (kvm, pfn); +sev_snp_vcpu_reclaim_vmsa (vcpu); goto out; }
It might be nice to break the loop contents out into its own function like is done with ES. I haven't looked ahead in the series to see if that would make it easier to supply/measure a single VMSA or not, though.
arch/x86/kvm/svm/sev.c void sev_es_unmap_ghcb(struct vcpu_svm *svm) · line 3776
Quoted 6 lines
arch/x86/kvm/svm/sev.c +1
-1@@ -3593,31 +3773,13 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)void sev_free_vcpu (struct kvm_vcpu *vcpu) { -struct vcpu_svm *svm; +struct vcpu_svm *svm =to_svm (vcpu);
You can probably reduce the churn here by keeping this line unchanged and not deleting the assignment below.
Quoted 168 lines
arch/x86/kvm/svm/sev.c +17
-80if (!is_sev_es_guest (vcpu))return ; - svm =to_svm (vcpu); - -/* -* If it's an SNP guest, then the VMSA was marked in the RMP table as -* a guest-owned page. Transition the page to hypervisor state before -* releasing it back to the system. -*/ -if (is_sev_snp_guest (vcpu)) { -u64 pfn =__pa (svm->sev_es.vmsa) >>PAGE_SHIFT ; - -if (kvm_rmp_make_shared (vcpu->kvm, pfn,PG_LEVEL_4K )) - 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)); +sev_es_vcpu_free_vmsa (vcpu); -skip_vmsa_free:__sev_es_unmap_ghcb (svm); }@@ -4067,10 +4229,7 @@ static int snp_begin_psc(struct vcpu_svm *svm)static void sev_snp_init_protected_guest_state (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_memory_slot *slot; -struct page *page; -kvm_pfn_t pfn; -gfn_t gfn; +u64 vmsa_pa;guard (mutex)(&svm->sev_es.snp_vmsa_mutex);@@ -4092,46 +4251,17 @@ static void sev_snp_init_protected_guest_state(struct kvm_vcpu *vcpu)*/ vmcb_mark_all_dirty (svm->vmcb); -if (!VALID_PAGE (svm->sev_es.snp_vmsa_gpa)) -return ; - - gfn =gpa_to_gfn (svm->sev_es.snp_vmsa_gpa); - svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; +sev_es_set_guest_vmsa (vcpu, svm->sev_es.req_vmsa_gpa); + vmsa_pa =sev_es_vmsa_pa (vcpu); - slot =gfn_to_memslot (vcpu->kvm, gfn); -if (!slot) +if (!VALID_PAGE (vmsa_pa))return ; -/* -* The new VMSA will be private memory guest memory, so retrieve the -* PFN from the gmem backend. -*/ -if (kvm_gmem_get_pfn (vcpu->kvm, slot, gfn, &pfn, &page,NULL )) -return ; - -/* -* From this point forward, the VMSA will always be a guest-mapped page -* rather than the initial one allocated by KVM in svm->sev_es.vmsa. In -* theory, svm->sev_es.vmsa could be free'd and cleaned up here, but -* that involves cleanups like flushing caches, which would ideally be -* handled during teardown rather than guest boot. Deferring that also -* allows the existing logic for SEV-ES VMSAs to be re-used with -* minimal SNP-specific changes. -*/ - svm->sev_es.snp_has_guest_vmsa = true; -/* Use the new VMSA */ - svm->vmcb->control.vmsa_pa =pfn_to_hpa (pfn); + svm->vmcb->control.vmsa_pa = vmsa_pa;/* Mark the vCPU as runnable */ kvm_set_mp_state (vcpu,KVM_MP_STATE_RUNNABLE ); - -/* -* gmem pages aren't currently migratable, but if this ever changes -* then care should be taken to ensure svm->sev_es.vmsa is pinned -* through some other means. -*/ -kvm_release_page_clean (page); }static int sev_snp_ap_creation (struct vcpu_svm *svm)@@ -4187,10 +4317,10 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm)return -EINVAL ; } - target_svm->sev_es.snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + target_svm->sev_es.req_vmsa_gpa = svm->vmcb->control.exit_info_2;break ;case SVM_VMGEXIT_AP_DESTROY : - target_svm->sev_es.snp_vmsa_gpa =INVALID_PAGE ; + target_svm->sev_es.req_vmsa_gpa =INVALID_PAGE ;break ;default : vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n",@@ -4708,20 +4838,7 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm, bool init_event)struct vmcb *vmcb = svm->vmcb01.ptr; svm->vmcb->control.misc_ctl |=SVM_MISC_ENABLE_SEV_ES ; - -/* -* An SEV-ES guest requires a VMSA area that is a separate from the -* VMCB page. Do not include the encryption mask on the VMSA physical -* address since hardware will access it using the guest key. Note, -* the VMSA will be NULL if this vCPU is the destination for intrahost -* migration, and will be copied later. -*/ -if (!svm->sev_es.snp_has_guest_vmsa) { -if (svm->sev_es.vmsa) - svm->vmcb->control.vmsa_pa =__pa (svm->sev_es.vmsa); -else - svm->vmcb->control.vmsa_pa =INVALID_PAGE ; - } + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (&svm->vcpu);if (cpu_feature_enabled (X86_FEATURE_ALLOWED_SEV_FEATURES )) svm->vmcb->control.allowed_sev_features = sev->vmsa_features |@@ -4797,7 +4914,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)int sev_vcpu_create (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct page *vmsa_page; +int ret;mutex_init (&svm->sev_es.snp_vmsa_mutex);@@ -4808,11 +4925,9 @@ int sev_vcpu_create(struct kvm_vcpu *vcpu)* SEV-ES guests require a separate (from the VMCB) VMSA page used to * contain the encrypted register state of the guest. */ - vmsa_page =snp_safe_alloc_page (); -if (!vmsa_page) -return -ENOMEM ; - - svm->sev_es.vmsa =page_address (vmsa_page); + ret =sev_es_vcpu_alloc_vmsa (vcpu); +if (ret) +return ret; vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm);@@ -5227,12 +5342,14 @@ struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)if (!is_sev_es_guest (vcpu))return NULL ; + vmsa =sev_es_vmsa_ref (vcpu); +/* * If the VMSA has not yet been encrypted, return a pointer to the * current un-encrypted VMSA. */ -if (!vcpu->arch.guest_state_protected) -return (struct vmcb_save_area *)svm->sev_es.vmsa; +if (vmsa) +return vmsa; sev =to_kvm_sev_info (vcpu->kvm);
arch/x86/kvm/svm/sev.c struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu) · line 5426
Quoted 8 lines
arch/x86/kvm/svm/sev.c +3
-1@@ -5303,8 +5420,10 @@ struct vmcb_save_area *sev_decrypt_vmsa(struct kvm_vcpu *vcpu)void sev_free_decrypted_vmsa (struct kvm_vcpu *vcpu,struct vmcb_save_area *vmsa) { +struct vmcb_save_area *vmsa_ptr =sev_es_vmsa_ref (vcpu); +/* If the VMSA has not yet been encrypted, nothing was allocated */ -if (!vcpu->arch.guest_state_protected || !vmsa) +if (vmsa == vmsa_ptr)
Can't this just be
if (sev_es_vmsa_ref(vcpu))
And then you save defining a new variable.
Quoted 8 lines
arch/x86/kvm/svm/sev.c +0
-0return ;free_page ((unsigned long )vmsa);arch/x86/kvm/svm/svm.h +0
-0
arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 247
Quoted 8 lines
arch/x86/kvm/svm/svm.h +5
-0@@ -240,9 +240,29 @@ struct svm_nested_state {bool force_msr_bitmap_recalc; }; +enum vmsa_state { +/* No VMSA set */ +VMSA_NONE , +/* VMSA allocated by KVM - Shared in RMP (if applicable) */ +VMSA_SHARED ,
VMSA_KVM_SHARED
arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 249
Quoted 2 lines
arch/x86/kvm/svm/svm.h +2
-0+/* VMSA allocated by KVM - Guest-private in RMP (SEV-SNP only) */ + VMSA_PRIVATE,
VMSA_KVM_PRIVATE
arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 251
Quoted 2 lines
arch/x86/kvm/svm/svm.h +2
-0+/* Guest-owned VMSA */ + VMSA_GUEST,
VMSA_GUEST_OWNED
This way ownership of the page becomes more obvious when used in the code.
Quoted 20 lines
arch/x86/kvm/svm/svm.h +12
-1+}; + +struct sev_es_vmsa_state { +enum vmsa_state vmsa_state; +union { +/* state == (KVM_SHARED || KVM_PRIVATE) */ +struct page *vmsa_page; +/* state == GUEST */ +gpa_t vmsa_gpa; + }; +}; +struct vcpu_sev_es_state {/* SEV-ES support */ -struct sev_es_save_area *vmsa;struct ghcb *ghcb;u8 valid_bitmap[16 ];struct kvm_host_map ghcb_map;@@ -266,10 +286,13 @@ struct vcpu_sev_es_state {
arch/x86/kvm/svm/svm.h struct vcpu_sev_es_state { · line 294
Quoted 10 lines
arch/x86/kvm/svm/svm.h +6
-2u64 ghcb_registered_gpa; -struct mutex snp_vmsa_mutex;/* Used to handle concurrent updates of VMSA. */ -gpa_t snp_vmsa_gpa; +/* VMSA related state */ +struct mutex snp_vmsa_mutex;/* Used to handle concurrent updates of VMSA. */ +struct sev_es_vmsa_state vmsa;/* VMSA currently used by the VCPU */ +gpa_t req_vmsa_gpa;/* Requested new VMSA GPA */ + +bool snp_ap_runnable;
I don't see this used in the patch anywhere.
Thanks,
Tom
Quoted 5 lines
arch/x86/kvm/svm/svm.h +0
-1bool snp_ap_waiting_for_reset; -bool snp_has_guest_vmsa; };struct vcpu_svm {
Re: [PATCH 1/4] kvm: svm: Streamline VMSA setting for VCPUs
Hi Tom,
Quoted 7 lines · Tom Lendacky
arch/x86/kvm/svm/sev.c static bool sev_snp_guest(struct kvm *kvm) · line 152
Quoted 4 lines · Jörg Rödel
static int
snp_decommission_context(struct kvm *kvm);arch/x86/kvm/svm/sev.c +3
-0+static int kvm_rmp_make_shared (struct kvm *kvm,u64 pfn,enum pg_level level); +static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va); +static int snp_page_reclaim (struct kvm *kvm,u64 pfn);Can this be worked so that we don't add more forward declarations?
Thanks for the review. I have worked in all your comments into v2.
-Joerg
Sean Christopherson
maintainerRe: [PATCH 1/4] kvm: svm: Streamline VMSA setting for VCPUs
In reply to Jörg RödelQuoted 5 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Streamline the VMSA setting state of vcpus, where a VMSA can be either KVM-allocated or guest-provided. This consolidates the various tracking state around VMSAs.
This needs to be split up into distinct logic changes. There are renames, there are tracking changes, there is code movement, and who knows what else, all bundled together. And the changelog is little more than "consolidate stuff".
Quoted 6 lines
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 301 ++++++++++++++++++++++++++++------------- arch/x86/kvm/svm/svm.h | 31 ++++- 2 files changed, 237 insertions(+), 95 deletions(-)
+180 lines of code doesn't scream "streamline" to me.
Quoted 15 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -147,6 +147,9 @@ static bool sev_snp_guest(struct kvm *kvm) }static int snp_decommission_context (struct kvm *kvm); +static int kvm_rmp_make_shared (struct kvm *kvm,u64 pfn,enum pg_level level); +static void sev_flush_encrypted_page (struct kvm_vcpu *vcpu,void *va); +static int snp_page_reclaim (struct kvm *kvm,u64 pfn);struct enc_region {struct list_head list;
arch/x86/kvm/svm/sev.c struct enc_region { · line 167
Quoted 9 lines
arch/x86/kvm/svm/sev.c +6
-0@@ -156,6 +159,173 @@ struct enc_region {unsigned long size; }; +static void *sev_es_vmsa_ref (struct kvm_vcpu *vcpu) +{ +struct vcpu_svm *svm =to_svm (vcpu); +void *vmsa =NULL ; + +if (svm->sev_es.vmsa.vmsa_state ==VMSA_SHARED ) {
Unnecessary curly braces.
Quoted 5 lines
arch/x86/kvm/svm/sev.c +5
-0+ vmsa =page_address (svm->sev_es.vmsa.vmsa_page); + } + +return vmsa; +}
...
Quoted 8 lines
arch/x86/kvm/svm/svm.h +0
-0@@ -240,9 +240,29 @@ struct svm_nested_state {bool force_msr_bitmap_recalc; };
arch/x86/kvm/svm/svm.h struct svm_nested_state { · line 252
Quoted 10 lines
arch/x86/kvm/svm/svm.h +10
-0+enum vmsa_state { +/* No VMSA set */ +VMSA_NONE , +/* VMSA allocated by KVM - Shared in RMP (if applicable) */ +VMSA_SHARED , +/* VMSA allocated by KVM - Guest-private in RMP (SEV-SNP only) */ +VMSA_PRIVATE , +/* Guest-owned VMSA */ +VMSA_GUEST , +};
I am strongly against this tracking. AFAICT, every case is completely redundant with other information that *must* be tracked.
PATCH 2/4 kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
Do not allocate a KVM-managed VMSA for all VCPUs on VCPU creation, defer it to the LAUNCH_FINISH stage of SEV-ES and SEV-SNP. At this stage the VMSAs get used for the first time.
--- arch/x86/kvm/svm/sev.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-)
arch/x86/kvm/svm/sev.c +23 -17
@@ -1095,11 +1095,11 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); -struct sev_es_save_area *save =sev_es_vmsa_ref (vcpu); +struct sev_es_save_area *save;struct xregs_state *xsave;const u8 *s; +int ret, i;u8 *d; -int i;lockdep_assert_held (&vcpu->mutex);
@@ -1110,6 +1110,12 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1 ))return -EINVAL ; + ret =sev_es_vcpu_alloc_vmsa (vcpu); +if (ret) +return ret; + + save =sev_es_vmsa_ref (vcpu); +/* * SEV-ES will use a VMSA that is pointed to by the VMCB, not * the traditional VMSA that is part of the VMCB. Copy the
@@ -1196,7 +1202,7 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, {struct sev_data_launch_update_vmsa vmsa;struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa_ref =sev_es_vmsa_ref (vcpu); +void *vmsa_ref;int ret;if (vcpu->guest_debug) {
@@ -1209,6 +1215,8 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,if (ret)return ret; + vmsa_ref =sev_es_vmsa_ref (vcpu); +/* * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of * the VMSA memory content (i.e it will write the same memory region
@@ -1237,6 +1245,9 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,fpstate_set_confidential (&vcpu->arch.guest_fpu); vcpu->arch.guest_state_protected = true; +/* VMSA encrypted - put it into the VMCB */ + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (vcpu); +/* * SEV-ES guest mandates LBR Virtualization to be _always_ ON. Enable it * only after setting guest_state_protected because KVM_SET_MSRS allows
@@ -2689,12 +2700,14 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa =sev_es_vmsa_ref (vcpu); +void *vmsa; ret =sev_es_sync_vmsa (svm);if (ret) goto out; + vmsa =sev_es_vmsa_ref (vcpu); + ret =sev_es_vcpu_vmsa_make_private (vcpu);if (ret) goto out;
@@ -2710,6 +2723,10 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) } svm->vcpu.arch.guest_state_protected = true; + +/* VMSA encrypted - put it into the VMCB */ + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (vcpu); +/* * SEV-ES (and thus SNP) guest mandates LBR Virtualization to * be _always_ ON. Enable it only after setting
@@ -4914,22 +4931,11 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)int sev_vcpu_create (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -int ret;mutex_init (&svm->sev_es.snp_vmsa_mutex); -if (!is_sev_es_guest (vcpu)) -return 0 ; - -/* -* SEV-ES guests require a separate (from the VMCB) VMSA page used to -* contain the encrypted register state of the guest. -*/ - ret =sev_es_vcpu_alloc_vmsa (vcpu); -if (ret) -return ret; - - vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm); +if (is_sev_es_guest (vcpu)) + vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm);return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 2/4] kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Missing VMSA cleanup on the error path in `snp_init_kvm_vmsa()` causes a guest-reachable…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Missing VMSA cleanup on the error path in snp_init_kvm_vmsa() causes a guest-reachable WARN_ON_ONCE upon retry.
- [Medium] Late vCPU creation results in KVM's INVALID_PAGE sentinel being passed directly to the hardware PSP firmware when dumping VMCB. --
commit 31275eaac0590092737efa334c2a33922a5b5fd9
Author: Joerg Roedel <[email protected]>
kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
Do not allocate a KVM-managed VMSA for all VCPUs on VCPU creation, defer it to the LAUNCH_FINISH stage of SEV-ES and SEV-SNP. At this stage the VMSAs get used for the first time.
Quoted 4 lines
arch/x86/kvm/svm/sev.c +0
-0
[ ... ]
Quoted 6 lines
arch/x86/kvm/svm/sev.c +1
-1@@ -2689,12 +2700,14 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa =sev_es_vmsa_ref (vcpu); +void *vmsa;
arch/x86/kvm/svm/sev.c static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2713
Quoted 10 lines
arch/x86/kvm/svm/sev.c +2
-0ret =sev_es_sync_vmsa (svm);if (ret) goto out; + vmsa =sev_es_vmsa_ref (vcpu); + ret =sev_es_vcpu_vmsa_make_private (vcpu);if (ret) goto out;
[Severity: Medium]
Does this error path leak the newly allocated VMSA?
Since sev_es_sync_vmsa() now allocates the VMSA, if sev_es_vcpu_vmsa_make_private() fails, the code returns via the "out" label without freeing the VMSA or resetting its state.
If userspace retries the KVM_SEV_SNP_LAUNCH_FINISH ioctl, sev_es_sync_vmsa() is called again and will trigger the WARN_ON_ONCE() in sev_es_vcpu_alloc_vmsa() because the state is still VMSA_SHARED:
if (WARN_ON_ONCE(svm->sev_es.vmsa.vmsa_state != VMSA_NONE))
return -EINVAL;Can we add a call to sev_es_vcpu_free_vmsa() on this error path?
[ ... ]
Quoted 15 lines
arch/x86/kvm/svm/sev.c +0
-8@@ -4889,22 +4906,11 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)int sev_vcpu_create (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -int ret;mutex_init (&svm->sev_es.snp_vmsa_mutex); -if (!is_sev_es_guest (vcpu)) -return 0 ; - -/* -* SEV-ES guests require a separate (from the VMCB) VMSA page used to -* contain the encrypted register state of the guest. -*/
arch/x86/kvm/svm/sev.c void sev_init_vmcb(struct vcpu_svm *svm, bool init_event) · line 4916
Quoted 10 lines
arch/x86/kvm/svm/sev.c +2
-5- ret =sev_es_vcpu_alloc_vmsa (vcpu); -if (ret) -return ret; - - vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm); +if (is_sev_es_guest (vcpu)) + vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm);return 0 ; }
[Severity: Medium]
Does removing this allocation lead to KVM's INVALID_PAGE sentinel being passed to the PSP firmware?
If userspace creates a vCPU after the SEV-ES or SEV-SNP launch sequence has completed, the new vCPU's VMSA is never allocated.
When this vCPU is run, sev_es_vmsa_pa() returns KVM's INVALID_PAGE sentinel (~0ull), which is loaded into the VMCB, triggering an immediate #VMEXIT (SVM_EXIT_ERR).
If the dump_invalid_vmcb parameter is enabled, KVM handles this by calling dump_vmcb(), which then calls sev_decrypt_vmsa(). Since sev_es_vmsa_ref() returns NULL for VMSA_NONE, sev_decrypt_vmsa() erroneously assumes the VMSA is encrypted and passes the INVALID_PAGE sentinel directly to the PSP firmware as the source physical address for SEV_CMD_DBG_DECRYPT:
data.src_addr = svm->vmcb->control.vmsa_pa;
data.len = PAGE_SIZE;
ret = sev_issue_cmd(vcpu->kvm, SEV_CMD_DBG_DECRYPT, &data, &argp_err);Could this direct hardware exposure to an invalid page address risk firmware hangs or IOMMU faults?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/4] kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
Quoted 7 lines · On Thu, Jun 11, 2026 at 12:58:13PM +0000, [email protected] wrote:
[Severity: Medium]
Does this error path leak the newly allocated VMSA?Since
sev_es_sync_vmsa()now allocates the VMSA, ifsev_es_vcpu_vmsa_make_private()fails, the code returns via the "out" label without freeing the VMSA or resetting its state.
Quoted 8 lines
If userspace retries the
KVM_SEV_SNP_LAUNCH_FINISHioctl,sev_es_sync_vmsa()is called again and will trigger theWARN_ON_ONCE()insev_es_vcpu_alloc_vmsa()because the state is stillVMSA_SHARED:if (WARN_ON_ONCE(svm->sev_es.vmsa.vmsa_state != VMSA_NONE)) return -EINVAL;Can we add a call to
sev_es_vcpu_free_vmsa()on this error path?
Yes, this is a user-space triggerable WARN_ON_ONCE. Usually user-space does not re-issue a LAUNCH_FINISHED command after it failed and doing so is not supported to lead to anything productive anyway. The right fix is to remove the WARN_ON_ONCE().
Quoted 17 lines
[Severity: Medium]
Does removing this allocation lead to KVM'sINVALID_PAGEsentinel being passed to the PSP firmware?If userspace creates a vCPU after the SEV-ES or SEV-SNP launch sequence has completed, the new vCPU's VMSA is never allocated.
When this vCPU is run,
sev_es_vmsa_pa()returns KVM'sINVALID_PAGEsentinel (~0ull), which is loaded into the VMCB, triggering an immediate #VMEXIT (SVM_EXIT_ERR).If the
dump_invalid_vmcbparameter is enabled, KVM handles this by callingdump_vmcb(), which then callssev_decrypt_vmsa(). Sincesev_es_vmsa_ref()returns NULL forVMSA_NONE,sev_decrypt_vmsa()erroneously assumes the VMSA is encrypted and passes theINVALID_PAGEsentinel directly to the PSP firmware as the source physical address forSEV_CMD_DBG_DECRYPT:
Quoted 7 lines
data.src_addr = svm->vmcb->control.vmsa_pa; data.len = PAGE_SIZE; ret = sev_issue_cmd(vcpu->kvm, SEV_CMD_DBG_DECRYPT, &data, &argp_err);Could this direct hardware exposure to an invalid page address risk firmware hangs or IOMMU faults?
This is not true. The VMSAs are allocated in sev_es_sync_vmsa() for both SEV-ES and SEV-SNP guests. Both code path also propagate the VMSA into the VMCB before LAUNCH_FINISH is complete and the VM can be invoked.
-Joerg
Re: [PATCH 2/4] kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
In reply to Jörg RödelQuoted 52 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Do not allocate a KVM-managed VMSA for all VCPUs on VCPU creation, defer it to the
LAUNCH_FINISHstage of SEV-ES and SEV-SNP. At this stage the VMSAs get used for the first time.Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-)arch/x86/kvm/svm/sev.c +9
-3@@ -1095,11 +1095,11 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) {struct kvm_vcpu *vcpu = &svm->vcpu;struct kvm_sev_info *sev =to_kvm_sev_info (vcpu->kvm); -struct sev_es_save_area *save =sev_es_vmsa_ref (vcpu); +struct sev_es_save_area *save;struct xregs_state *xsave;const u8 *s; +int ret, i;u8 *d; -int i;lockdep_assert_held (&vcpu->mutex);@@ -1110,6 +1110,12 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1 ))return -EINVAL ; + ret =sev_es_vcpu_alloc_vmsa (vcpu); +if (ret) +return ret; + + save =sev_es_vmsa_ref (vcpu); +/* * SEV-ES will use a VMSA that is pointed to by the VMCB, not * the traditional VMSA that is part of the VMCB. Copy the @@ -1196,7 +1202,7 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, {struct sev_data_launch_update_vmsa vmsa;struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa_ref =sev_es_vmsa_ref (vcpu); +void *vmsa_ref;int ret;if (vcpu->guest_debug) {
arch/x86/kvm/svm/sev.c static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, · line 1218
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -1209,6 +1215,8 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,if (ret)return ret; + vmsa_ref =sev_es_vmsa_ref (vcpu);
Maybe just a comment above this that indicates if the sync was successful then a VMSA has been allocated and only now can you get a pointer to it ?
Quoted 42 lines
arch/x86/kvm/svm/sev.c +11
-1+/* * The LAUNCH_UPDATE_VMSA command will perform in-place encryption of * the VMSA memory content (i.e it will write the same memory region @@ -1237,6 +1245,9 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,fpstate_set_confidential (&vcpu->arch.guest_fpu); vcpu->arch.guest_state_protected = true; +/* VMSA encrypted - put it into the VMCB */ + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (vcpu); +/* * SEV-ES guest mandates LBR Virtualization to be _always_ ON. Enable it * only after setting guest_state_protected because KVM_SET_MSRS allows @@ -2689,12 +2700,14 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa =sev_es_vmsa_ref (vcpu); +void *vmsa; ret =sev_es_sync_vmsa (svm);if (ret) goto out; + vmsa =sev_es_vmsa_ref (vcpu); + ret =sev_es_vcpu_vmsa_make_private (vcpu);if (ret) goto out;@@ -2710,6 +2723,10 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) } svm->vcpu.arch.guest_state_protected = true; + +/* VMSA encrypted - put it into the VMCB */ + svm->vmcb->control.vmsa_pa =sev_es_vmsa_pa (vcpu); +/* * SEV-ES (and thus SNP) guest mandates LBR Virtualization to * be _always_ ON. Enable it only after setting
arch/x86/kvm/svm/sev.c void sev_init_vmcb(struct vcpu_svm *svm, bool init_event) · line 4922
Quoted 9 lines
arch/x86/kvm/svm/sev.c +0
-3@@ -4914,22 +4931,11 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event)int sev_vcpu_create (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -int ret;mutex_init (&svm->sev_es.snp_vmsa_mutex); -if (!is_sev_es_guest (vcpu)) -return 0 ;
If you leave this here, it might make it easier to add things in the future should something be needed. Just tack them on at the bottom. Not necessary, though.
Thanks,
Tom
Quoted 15 lines
arch/x86/kvm/svm/sev.c +2
-10- -/* -* SEV-ES guests require a separate (from the VMCB) VMSA page used to -* contain the encrypted register state of the guest. -*/ - ret =sev_es_vcpu_alloc_vmsa (vcpu); -if (ret) -return ret; - - vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm); +if (is_sev_es_guest (vcpu)) + vcpu->arch.guest_tsc_protected =snp_is_secure_tsc_enabled (vcpu->kvm);return 0 ; }
Re: [PATCH 2/4] kvm: svm: Defer VMSA allocation to LAUNCH_FINISH stage
Quoted 10 lines · Tom Lendacky
arch/x86/kvm/svm/sev.c static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu, · line 1218
Quoted 5 lines · Jörg Rödel
arch/x86/kvm/svm/sev.c +1
-0@@ -1209,6 +1215,8 @@ static int __sev_launch_update_vmsa(struct kvm *kvm, struct kvm_vcpu *vcpu,if (ret)return ret; + vmsa_ref =sev_es_vmsa_ref (vcpu);Maybe just a comment above this that indicates if the sync was successful then a VMSA has been allocated and only now can you get a pointer to it ?
Makes sense, added a comment here and to __snp_launch_update_vmsa().
Quoted 6 lines
arch/x86/kvm/svm/sev.c void sev_init_vmcb(struct vcpu_svm *svm, bool init_event) · line 4922
Quoted 2 lines
arch/x86/kvm/svm/sev.c +0
-2-if (!is_sev_es_guest (vcpu)) -return 0 ;If you leave this here, it might make it easier to add things in the future should something be needed. Just tack them on at the bottom. Not necessary, though.
Right, that is better, changed it back.
-Joerg
PATCH 3/4 kvm: svm: Support guest-provided VMSA for launching
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
Introduce a way to provide a guest GPA as the initial BSP VMSA and avoid allocating KVM-managed VMSAs in this case. Only one guest-provided VMSA is supported at the moment as IGVM also only supports to set a single VMSA.
--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++------------ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 45 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/sev.c +44 -18
@@ -726,6 +726,7 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,INIT_LIST_HEAD (&sev->regions_list);INIT_LIST_HEAD (&sev->mirror_vms); + sev->initial_vmsa_gpa =INVALID_PAGE ; sev->need_init = false;kvm_set_apicv_inhibit (kvm,APICV_INHIBIT_REASON_SEV );
@@ -2680,6 +2681,46 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)return 0 ; } +static int snp_init_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +/* Only one initial guest VMSA can exist (per IGVM) - so it belongs to the BSP */ +if (vcpu->vcpu_idx !=0 ) +return 0 ; + +/* VMSA already private and encrypted via LAUNCH_UPDATE */ +sev_es_set_guest_vmsa (vcpu, vmsa_gpa); + +return 0 ; +} + +static int snp_init_kvm_vmsa (struct kvm_vcpu *vcpu, +struct sev_data_snp_launch_update *data, +struct kvm_sev_cmd *argp) +{ +struct vcpu_svm *svm =to_svm (vcpu); +int ret; +void *vmsa; + + ret =sev_es_sync_vmsa (svm); +if (ret) +return ret; + + vmsa =sev_es_vmsa_ref (vcpu); + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret) +return ret; + +/* Issue the SNP command to encrypt the VMSA */ + data->address =__sme_pa (vmsa); + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + data, &argp->error); +if (ret) +sev_snp_vcpu_reclaim_vmsa (vcpu); + +return ret; +} +static int snp_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev =to_kvm_sev_info (kvm);
@@ -2700,28 +2741,13 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa; - ret =sev_es_sync_vmsa (svm); + ret =VALID_PAGE (sev->initial_vmsa_gpa) ? +snp_init_guest_vmsa (vcpu, sev->initial_vmsa_gpa) : +snp_init_kvm_vmsa (vcpu, &data, argp);if (ret) goto out; - vmsa =sev_es_vmsa_ref (vcpu); - - ret =sev_es_vcpu_vmsa_make_private (vcpu); -if (ret) - goto out; - -/* Issue the SNP command to encrypt the VMSA */ - data.address =__sme_pa (vmsa); - ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , - &data, &argp->error); -if (ret) { -sev_snp_vcpu_reclaim_vmsa (vcpu); - - goto out; - } - svm->vcpu.arch.guest_state_protected = true;/* VMSA encrypted - put it into the VMCB */
arch/x86/kvm/svm/svm.h +1 -0
@@ -117,6 +117,7 @@ struct kvm_sev_info {struct mutex guest_req_mutex;/* Must acquire before using bounce buffers */ cpumask_var_t have_run_cpus;/* CPUs that have done VMRUN for this VM. */ bool snp_certs_enabled;/* SNP certificate-fetching support. */ + gpa_t initial_vmsa_gpa; /* Optinal GPA of BSP VMSA - SEV-SNP only */ };#endif
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH 3/4] kvm: svm: Support guest-provided VMSA for launching
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [High] Resolving the guest-provided VMSA GPA via gfn_to_memslot() occurs without holding kvm->srcu or kvm->slots_lock, violating KVM's memslot locking hierarchy.
Pre-existing issues:
- [Critical] Guest-provided VMSA pages are freed to the host buddy allocator while still in the RMP Firmware state, allowing userspace to trigger a host kernel panic (Denial of Service).
- [High] KVM-allocated VMSA pages for successfully launched SNP VMs are permanently leaked during VM teardown.
--
commit 7ccfa1323e073ebe13797e68f8f429e2d5dfb5ec
Author: Joerg Roedel <[email protected]>
kvm: svm: Support guest-provided VMSA for launching
Introduce a way to provide a guest GPA as the initial BSP VMSA and avoid allocating KVM-managed VMSAs in this case. Only one guest-provided VMSA is supported at the moment as IGVM also only supports to set a single VMSA.
Quoted 4 lines
arch/x86/kvm/svm/sev.c +0
-0
[ ... ]
Quoted 5 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2680,6 +2681,46 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)return 0 ; } +static int snp_init_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa)
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2694
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+{ +/* Only one initial guest VMSA can exist (per IGVM) - so it belongs to the BSP */ +if (vcpu->vcpu_idx !=0 ) +return 0 ; + +/* VMSA already private and encrypted via LAUNCH_UPDATE */ +sev_es_set_guest_vmsa (vcpu, vmsa_gpa); + +return 0 ; +}
[Severity: Critical]
This is a pre-existing issue, but can unprivileged userspace panic the host kernel by punching a hole in the guest_memfd file backing the guest-provided VMSA pages?
Guest-provided VMSA pages are placed into the VMSA (Firmware) RMP state. However, KVM drops its page reference immediately after lookup:
arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() {
...
kvm_release_page_clean(page);
...
}If userspace triggers a page release, the page is freed by the core MM subsystem. During freeing, sev_gmem_invalidate() attempts to transition the page via rmp_make_shared(), which architecturally fails on VMSA-state pages:
arch/x86/kvm/svm/sev.c:sev_gmem_invalidate() {
...
rc = rmp_make_shared(pfn, use_2m_update ? PG_LEVEL_2M : PG_LEVEL_4K);
if (WARN_ONCE(rc, "SEV: Failed to update RMP entry for PFN 0x%llx error %d\n", pfn, rc))
goto next_pfn;
...
}Because the failure is only logged as a warning, the page is returned to the buddy allocator in Firmware state. When the host reallocates and accesses this page, it triggers a fatal RMP #PF and panics the kernel. KVM must properly reclaim VMSA pages before allowing them to be freed.
Quoted 18 lines
arch/x86/kvm/svm/sev.c +18
-0+ +static int snp_init_kvm_vmsa (struct kvm_vcpu *vcpu, +struct sev_data_snp_launch_update *data, +struct kvm_sev_cmd *argp) +{ +struct vcpu_svm *svm =to_svm (vcpu); +int ret; +void *vmsa; + + ret =sev_es_sync_vmsa (svm); +if (ret) +return ret; + + vmsa =sev_es_vmsa_ref (vcpu); + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret) +return ret;
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2722
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ +/* Issue the SNP command to encrypt the VMSA */ + data->address =__sme_pa (vmsa); + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + data, &argp->error); +if (ret) +sev_snp_vcpu_reclaim_vmsa (vcpu); + +return ret; +}
[Severity: High]
This is a pre-existing issue, but does KVM permanently leak successfully launched SNP VM VMSA pages during VM teardown?
During normal teardown of an SNP guest, KVM attempts to transition its allocated VMSA pages to shared state via kvm_rmp_make_shared():
arch/x86/kvm/svm/sev.c:sev_es_vcpu_free_vmsa() {
...
if (kvm_rmp_make_shared(vcpu->kvm, pfn, PG_LEVEL_4K)) {
pr_err("Failed to make VMSA page shared - leaking it to avoid re-use\n");
goto out;
}
...
}kvm_rmp_make_shared() uses the RMPUPDATE instruction, which architecturally fails on pages that are in the VMSA state. The function detects the failure, logs the error, and permanently leaks the page. Because VMs are destroyed routinely, this will eventually exhaust host memory. KVM must use snp_page_reclaim() instead of kvm_rmp_make_shared() for pages in the VMSA state.
Quoted 25 lines
arch/x86/kvm/svm/sev.c +3
-11static int snp_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev =to_kvm_sev_info (kvm);@@ -2700,28 +2741,13 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa; - ret =sev_es_sync_vmsa (svm); + ret =VALID_PAGE (sev->initial_vmsa_gpa) ? +snp_init_guest_vmsa (vcpu, sev->initial_vmsa_gpa) : +snp_init_kvm_vmsa (vcpu, &data, argp);if (ret) goto out; - vmsa =sev_es_vmsa_ref (vcpu); - - ret =sev_es_vcpu_vmsa_make_private (vcpu); -if (ret) - goto out; - -/* Issue the SNP command to encrypt the VMSA */ - data.address =__sme_pa (vmsa); - ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE,
arch/x86/kvm/svm/sev.c static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2753
Quoted 10 lines
arch/x86/kvm/svm/sev.c +0
-7- &data, &argp->error); -if (ret) { -sev_snp_vcpu_reclaim_vmsa (vcpu); - - goto out; - } - svm->vcpu.arch.guest_state_protected = true;/* VMSA encrypted - put it into the VMCB */
[Severity: High]
Does this code call sev_es_vmsa_pa() without holding kvm->srcu or kvm->slots_lock?
Looking at the execution path immediately following this comment in snp_launch_update_vmsa():
/* VMSA encrypted - put it into the VMCB */
svm->vmcb->control.vmsa_pa = sev_es_vmsa_pa(vcpu);arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() {
...
slot = gfn_to_memslot(vcpu->kvm, gfn);
...
}gfn_to_memslot() dereferences the KVM memslots array. Since this is called without SRCU or slots_lock, a concurrent thread executing KVM_SET_USER_MEMORY_REGION can swap and free the memslots array while gfn_to_memslot() is accessing it, leading to a use-after-free.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/4] kvm: svm: Support guest-provided VMSA for launching
Quoted 5 lines · On Thu, Jun 11, 2026 at 01:05:36PM +0000, [email protected] wrote:
[Severity: Critical]
This is a pre-existing issue, but can unprivileged userspace panic the host kernel by punching a hole in theguest_memfdfile backing the guest-provided VMSA pages?
Quoted 8 lines
Guest-provided VMSA pages are placed into the VMSA (Firmware) RMP state. However, KVM drops its page reference immediately after lookup:
arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() { ... kvm_release_page_clean(page); ... }
There is at least not a reference counting issue here, as GMEM still holds a reference to the page.
Quoted 3 lines
If userspace triggers a page release, the page is freed by the core MM subsystem. During freeing,
sev_gmem_invalidate()attempts to transition the page viarmp_make_shared(), which architecturally fails on VMSA-state pages:
Not sure about that, I havn't checked whether GMEM allows these kinds of page-state changes before LAUNCH_FINISH. After LAUNCH_FINISH it is not problem anymore because VMSA address in the VMCB does not point to a valid VMSA page, then VMRUN will fail.
Quoted 16 lines
[Severity: High]
This is a pre-existing issue, but does KVM permanently leak successfully launched SNP VM VMSA pages during VM teardown?During normal teardown of an SNP guest, KVM attempts to transition its allocated VMSA pages to shared state via
kvm_rmp_make_shared():arch/x86/kvm/svm/sev.c:sev_es_vcpu_free_vmsa() { ... if (kvm_rmp_make_shared(vcpu->kvm, pfn, PG_LEVEL_4K)) { pr_err("Failed to make VMSA page shared - leaking it to avoid re-use\n"); goto out; } ... }
Quoted 6 lines
kvm_rmp_make_shared()uses the RMPUPDATE instruction, which architecturally fails on pages that are in the VMSA state. The function detects the failure, logs the error, and permanently leaks the page. Because VMs are destroyed routinely, this will eventually exhaust host memory. KVM must usesnp_page_reclaim()instead ofkvm_rmp_make_shared()for pages in the VMSA state.
Wrong, VMSA pages are freed when they are KVM owned, when they are guest owned they are freed via gmem. This is all fine if it happens after LAUNCH_FINISH and no VCPU of the VM is still running.
Quoted 16 lines
[Severity: High]
Does this code callsev_es_vmsa_pa()without holdingkvm->srcuorkvm->slots_lock?Looking at the execution path immediately following this comment in
snp_launch_update_vmsa():/* VMSA encrypted - put it into the VMCB */ svm->vmcb->control.vmsa_pa = sev_es_vmsa_pa(vcpu);arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() { ... slot = gfn_to_memslot(vcpu->kvm, gfn); ... }
Quoted 4 lines
gfn_to_memslot()dereferences the KVM memslots array. Since this is called without SRCU orslots_lock, a concurrent thread executingKVM_SET_USER_MEMORY_REGIONcan swap and free the memslots array whilegfn_to_memslot()is accessing it, leading to a use-after-free.
Not sure if this is an issue. If it is then it is pre-existing. I think the worst that can happen is a failed VMRUN because the VMSAs become invalid, no?
-Joerg
Re: [PATCH 3/4] kvm: svm: Support guest-provided VMSA for launching
In reply to Jörg RödelQuoted 26 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Introduce a way to provide a guest GPA as the initial BSP VMSA and avoid allocating KVM-managed VMSAs in this case. Only one guest-provided VMSA is supported at the moment as IGVM also only supports to set a single VMSA.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++------------ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 45 insertions(+), 18 deletions(-)arch/x86/kvm/svm/sev.c +1
-0@@ -726,6 +726,7 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,INIT_LIST_HEAD (&sev->regions_list);INIT_LIST_HEAD (&sev->mirror_vms); + sev->initial_vmsa_gpa =INVALID_PAGE ; sev->need_init = false;kvm_set_apicv_inhibit (kvm,APICV_INHIBIT_REASON_SEV );
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2686
Quoted 6 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -2680,6 +2681,46 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)return 0 ; } +static int snp_init_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +/* Only one initial guest VMSA can exist (per IGVM) - so it belongs to the BSP */
Maybe expand this comment to indicate that none of the other vCPU VMSAs are created by KVM, that the guest is responsible for creating them for the first time.
Which reminds me that you will need to provide the GHCB APIC ID List NAE event support. If OVMF was ever to be built as an IGVM file, then without that GHCB event support it will perform a broadcast INIT-SIPI for the first AP startup, which will fail because no VMSAs will have been created. If OVMF sees that the HV has advertised the event, then it will create all the VMSAs itself and use the GHCB AP Create NAE event for initial startup of the APs.
Quoted 77 lines
arch/x86/kvm/svm/sev.c +40
-18+if (vcpu->vcpu_idx !=0 ) +return 0 ; + +/* VMSA already private and encrypted via LAUNCH_UPDATE */ +sev_es_set_guest_vmsa (vcpu, vmsa_gpa); + +return 0 ; +} + +static int snp_init_kvm_vmsa (struct kvm_vcpu *vcpu, +struct sev_data_snp_launch_update *data, +struct kvm_sev_cmd *argp) +{ +struct vcpu_svm *svm =to_svm (vcpu); +int ret; +void *vmsa; + + ret =sev_es_sync_vmsa (svm); +if (ret) +return ret; + + vmsa =sev_es_vmsa_ref (vcpu); + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret) +return ret; + +/* Issue the SNP command to encrypt the VMSA */ + data->address =__sme_pa (vmsa); + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + data, &argp->error); +if (ret) +sev_snp_vcpu_reclaim_vmsa (vcpu); + +return ret; +} +static int snp_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev =to_kvm_sev_info (kvm);@@ -2700,28 +2741,13 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa; - ret =sev_es_sync_vmsa (svm); + ret =VALID_PAGE (sev->initial_vmsa_gpa) ? +snp_init_guest_vmsa (vcpu, sev->initial_vmsa_gpa) : +snp_init_kvm_vmsa (vcpu, &data, argp);if (ret) goto out; - vmsa =sev_es_vmsa_ref (vcpu); - - ret =sev_es_vcpu_vmsa_make_private (vcpu); -if (ret) - goto out; - -/* Issue the SNP command to encrypt the VMSA */ - data.address =__sme_pa (vmsa); - ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , - &data, &argp->error); -if (ret) { -sev_snp_vcpu_reclaim_vmsa (vcpu); - - goto out; - } - svm->vcpu.arch.guest_state_protected = true;/* VMSA encrypted - put it into the VMCB */ arch/x86/kvm/svm/svm.h +0
-0
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 120
Quoted 4 lines
arch/x86/kvm/svm/svm.h +1
-0@@ -117,6 +117,7 @@ struct kvm_sev_info {struct mutex guest_req_mutex;/* Must acquire before using bounce buffers */ cpumask_var_t have_run_cpus;/* CPUs that have done VMRUN for this VM. */ bool snp_certs_enabled;/* SNP certificate-fetching support. */ +gpa_t initial_vmsa_gpa;/* Optinal GPA of BSP VMSA - SEV-SNP only */
s/Optinal/Optional/
Should it be called bsp_vmsa_gpa ?
Thanks,
Tom
Quoted 3 lines
arch/x86/kvm/svm/svm.h +0
-0};#endif
Re: [PATCH 3/4] kvm: svm: Support guest-provided VMSA for launching
Quoted 8 lines · Tom Lendacky
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2686
Quoted 3 lines · Jörg Rödel
arch/x86/kvm/svm/sev.c +3
-0+static int snp_init_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +/* Only one initial guest VMSA can exist (per IGVM) - so it belongs to the BSP */ Maybe expand this comment to indicate that none of the other vCPU VMSAs are created by KVM, that the guest is responsible for creating them for the first time.
Okay, updated the comment.
Quoted 7 lines
Which reminds me that you will need to provide the GHCB APIC ID List NAE event support. If OVMF was ever to be built as an IGVM file, then without that GHCB event support it will perform a broadcast INIT-SIPI for the first AP startup, which will fail because no VMSAs will have been created. If OVMF sees that the HV has advertised the event, then it will create all the VMSAs itself and use the GHCB AP Create NAE event for initial startup of the APs.
Right. Currently the GHCB APIC ID List NAE patch is part of the KVM planes patch-set, so whichever patch-set is accepted first should include it. I will add this patch here as well when posting v2.
Quoted 5 lines
arch/x86/kvm/svm/svm.h struct kvm_sev_info { · line 120
Quoted 1 line
arch/x86/kvm/svm/svm.h +1
-0+gpa_t initial_vmsa_gpa;/* Optinal GPA of BSP VMSA - SEV-SNP only */ s/Optinal/Optional/
Should it be called
bsp_vmsa_gpa?
Yes, that is better, changed it.
-Joerg
Sean Christopherson
maintainerRe: [PATCH 3/4] kvm: svm: Support guest-provided VMSA for launching
In reply to Jörg RödelQuoted 5 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Introduce a way to provide a guest GPA as the initial BSP VMSA and avoid allocating KVM-managed VMSAs in this case. Only one guest-provided VMSA is supported at the moment as IGVM also only
No. Design uAPI that makes sense for KVM and is extensible. If it turns out that allowing exactly one VMSA is the simplest, most logical approach, then so be it. But "thing X only needs Y" isn't sufficient justification.
But I'm not remotely convinced that hacking in BSP-only support like this is the way to go. This entire approach is convoluted, as is the code. E.g. the below iterates over all vCPUs, but then only actually does anything for vcpu_idx=0. And the ioctl is VM-scoped, but really operates on a vCPU.
At a (very rough) glance, I don't see any reason we can't have a vCPU-scoped ioctl to effectively mimic SVM_VMGEXIT_AP_CREATE.
Quoted 52 lines
supports to set a single VMSA.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kvm/svm/sev.c | 62 ++++++++++++++++++++++++++++++------------ arch/x86/kvm/svm/svm.h | 1 + 2 files changed, 45 insertions(+), 18 deletions(-)arch/x86/kvm/svm/sev.c +29
-0@@ -726,6 +726,7 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,INIT_LIST_HEAD (&sev->regions_list);INIT_LIST_HEAD (&sev->mirror_vms); + sev->initial_vmsa_gpa =INVALID_PAGE ; sev->need_init = false;kvm_set_apicv_inhibit (kvm,APICV_INHIBIT_REASON_SEV );@@ -2680,6 +2681,46 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)return 0 ; } +static int snp_init_guest_vmsa (struct kvm_vcpu *vcpu,gpa_t vmsa_gpa) +{ +/* Only one initial guest VMSA can exist (per IGVM) - so it belongs to the BSP */ +if (vcpu->vcpu_idx !=0 ) +return 0 ; + +/* VMSA already private and encrypted via LAUNCH_UPDATE */ +sev_es_set_guest_vmsa (vcpu, vmsa_gpa); + +return 0 ; +} + +static int snp_init_kvm_vmsa (struct kvm_vcpu *vcpu, +struct sev_data_snp_launch_update *data, +struct kvm_sev_cmd *argp) +{ +struct vcpu_svm *svm =to_svm (vcpu); +int ret; +void *vmsa; + + ret =sev_es_sync_vmsa (svm); +if (ret) +return ret; + + vmsa =sev_es_vmsa_ref (vcpu); + + ret =sev_es_vcpu_vmsa_make_private (vcpu); +if (ret)
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2721
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+return ret; + +/* Issue the SNP command to encrypt the VMSA */ + data->address =__sme_pa (vmsa); + ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , + data, &argp->error); +if (ret) +sev_snp_vcpu_reclaim_vmsa (vcpu); + +return ret;
Separate code movement from new functi0onality.
Quoted 52 lines
arch/x86/kvm/svm/sev.c +5
-18+} +static int snp_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) {struct kvm_sev_info *sev =to_kvm_sev_info (kvm);@@ -2700,28 +2741,13 @@ static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp)kvm_for_each_vcpu (i, vcpu, kvm) {struct vcpu_svm *svm =to_svm (vcpu); -void *vmsa; - ret =sev_es_sync_vmsa (svm); + ret =VALID_PAGE (sev->initial_vmsa_gpa) ? +snp_init_guest_vmsa (vcpu, sev->initial_vmsa_gpa) : +snp_init_kvm_vmsa (vcpu, &data, argp);if (ret) goto out; - vmsa =sev_es_vmsa_ref (vcpu); - - ret =sev_es_vcpu_vmsa_make_private (vcpu); -if (ret) - goto out; - -/* Issue the SNP command to encrypt the VMSA */ - data.address =__sme_pa (vmsa); - ret =__sev_issue_cmd (argp->sev_fd,SEV_CMD_SNP_LAUNCH_UPDATE , - &data, &argp->error); -if (ret) { -sev_snp_vcpu_reclaim_vmsa (vcpu); - - goto out; - } - svm->vcpu.arch.guest_state_protected = true;/* VMSA encrypted - put it into the VMCB */ arch/x86/kvm/svm/svm.h +1
-0@@ -117,6 +117,7 @@ struct kvm_sev_info {struct mutex guest_req_mutex;/* Must acquire before using bounce buffers */ cpumask_var_t have_run_cpus;/* CPUs that have done VMRUN for this VM. */ bool snp_certs_enabled;/* SNP certificate-fetching support. */ + gpa_t initial_vmsa_gpa; /* Optinal GPA of BSP VMSA - SEV-SNP only */ };#endif Signature · 1 line
2.53.0
PATCH 4/4 kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jörg RödelFrom: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 4 files changed, 46 insertions(+), 1 deletion(-)
arch/x86/include/uapi/asm/kvm.h +1 -0
@@ -885,6 +885,7 @@ struct kvm_sev_snp_launch_start {/* Kept in sync with firmware values for simplicity. */ #define KVM_SEV_PAGE_TYPE_INVALID 0x0#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
arch/x86/kvm/svm/sev.c +43 -1
88 lines not on this page · raw message
arch/x86/kvm/x86.c +1 -0
@@ -4870,6 +4870,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MEMORY_FAULT_INFO :case KVM_CAP_X86_GUEST_MODE :case KVM_CAP_ONE_REG : +case KVM_CAP_SNP_DIRECT_VMSA : r =1 ;break ;case KVM_CAP_PRE_FAULT_MEMORY :
include/uapi/linux/kvm.h +1 -0
@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_SNP_DIRECT_VMSA 249struct kvm_irq_routing_irqchip {__u32 irqchip;
Signature · 1 line
2.53.0
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 9 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
This needs a *much* longer explanation and more justification for exactly why this needs to be handled in KVM. I understand most of the words and acronyms, but that's about where my understanding stops.
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Hi Sean,
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
Quoted 6 lines
Quoted 2 lines
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
This needs a *much* longer explanation and more justification for exactly why this needs to be handled in KVM. I understand most of the words and acronyms, but that's about where my understanding stops.
Sure, how about:
For SEV-SNP VMs KVM currently allocates and measures one VMSA per VCPU into the initial memory image. Historically this behavior comes from the SEV-ES implementation, which has no concept of a guest-provided or guest-owned VMSA. So on SEV-ES there is no other choice than allocating the VMSAs in KVM.
In contrast, on SEV-SNP each VMSA has a GPA assigned and is (in theory) guest-owned, so that the old SEV-ES behavior of letting KVM manage the VMSAs causes several problems (especially together with IGVM-loading) and inefficiencies:
1. With the current KVM behavior the initial launch measurement depends on the number of VCPUs the VM has assigned.
2. Current SEV-SNP guest code will not use the KVM-allocated VMSAs for APs. Both EDK2 and the Linux kernel will allocate and provide their own VMSA pages for every AP. So the current allocation dance KVM is doing is useless for the APs.
3. The current behavior makes it impossible to implement the IGVM-promise of a predictable launch measurement derived from only the IGVM file and the target platform.
To solve these problems this patch adds support to measure an IGVM-provided VMSA page into the initial SEV-SNP memory image. Only one VMSA page is supported for now, which aligns with the IGVM requirement that each file can only provide one VP-context. The VMSA will be checked by KVM for supported SEV features and VMPL0 before being accepted.
When a VMSA page is measured in this way it will be used as the launch VMSA of the BSP for the VM. For all other VCPUs KVM will not allocate or measure VMSA pages, keeping the launch measurement in sync with the IGVM image. The guest has to provide VMSAs for all APs it intends to use, which common guest components already do anyway.
When the feature is not used the current behavior is preserved. The changes have been tested together with the KVM planes patches and COCONUT-SVSM and showed that using this feature leads to a launch measurement matching the IGVM-prediction.
?
-Joerg
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
+Jethro
Quoted 48 lines · Jörg Rödel
Hi Sean,
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
Quoted 6 lines
Quoted 2 lines
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
This needs a *much* longer explanation and more justification for exactly why this needs to be handled in KVM. I understand most of the words and acronyms, but that's about where my understanding stops.
Sure, how about:
For SEV-SNP VMs KVM currently allocates and measures one VMSA per VCPU into the initial memory image. Historically this behavior comes from the SEV-ES implementation, which has no concept of a guest-provided or guest-owned VMSA. So on SEV-ES there is no other choice than allocating the VMSAs in KVM.
In contrast, on SEV-SNP each VMSA has a GPA assigned and is (in theory) guest-owned, so that the old SEV-ES behavior of letting KVM manage the VMSAs causes several problems (especially together with IGVM-loading) and inefficiencies:
1. With the current KVM behavior the initial launch measurement depends on the number of VCPUs the VM has assigned.
2. Current SEV-SNP guest code will not use the KVM-allocated VMSAs for APs. Both EDK2 and the Linux kernel will allocate and provide their own VMSA pages for every AP. So the current allocation dance KVM is doing is useless for the APs.
3. The current behavior makes it impossible to implement the IGVM-promise of a predictable launch measurement derived from only the IGVM file and the target platform.
To solve these problems this patch adds support to measure an IGVM-provided VMSA page into the initial SEV-SNP memory image. Only one VMSA page is supported for now, which aligns with the IGVM requirement that each file can only provide one VP-context. The VMSA will be checked by KVM for supported SEV features and VMPL0 before being accepted.
Quoted 5 lines
When a VMSA page is measured in this way it will be used as the launch VMSA of the BSP for the VM. For all other VCPUs KVM will not allocate or measure VMSA pages, keeping the launch measurement in sync with the IGVM image. The guest has to provide VMSAs for all APs it intends to use, which common guest components already do anyway.
Isn't this essentially the same thing as hot-plugging vCPUs after launch? I have yet to review it in depth (sorry Jethro), but it looks a *lot* simpler.
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 2 lines · Sean Christopherson
Isn't this essentially the same thing as hot-plugging vCPUs after launch? I have yet to review it in depth (sorry Jethro), but it looks a *lot* simpler.
Replacing the VMSA after launch (as supported with the AP_CREATE GHCB call) is different because this has no influence on the launch measurement.
The point of providing an initial VMSA is to get a predictable launch measurement which is independent of the number of VCPUs the guest has.
With the current code KVM will create its own VMSA for each created VCPU and measure it into the guests initial image. This makes predicting the initial launch measurement difficult (as it depends on KVM internals) and fragile because KVM-internal changes always carry a risk to change the launch measurement (which has happened a couple of times already).
-Joerg
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 7 lines · Jörg Rödel
Quoted 2 lines · Sean Christopherson
Isn't this essentially the same thing as hot-plugging vCPUs after launch? I have yet to review it in depth (sorry Jethro), but it looks a *lot* simpler.
Replacing the VMSA after launch (as supported with the
AP_CREATEGHCB call) is different because this has no influence on the launch measurement.
Quoted 7 lines
The point of providing an initial VMSA is to get a predictable launch measurement which is independent of the number of VCPUs the guest has.
With the current code KVM will create its own VMSA for each created VCPU and measure it into the guests initial image. This makes predicting the initial launch measurement difficult (as it depends on KVM internals) and fragile because KVM-internal changes always carry a risk to change the launch measurement
The same holds true for userspace.
Quoted 1 line
(which has happened a couple of times already).
Examples? The SEV features thing jumps to mind, but I don't recall any others off the top of my head.
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Jörg Rödel
With the current code KVM will create its own VMSA for each created VCPU and measure it into the guests initial image. This makes predicting the initial launch measurement difficult (as it depends on KVM internals) and fragile because KVM-internal changes always carry a risk to change the launch measurement
The same holds true for userspace.
Right, but if user-space breaks the launch measurement it must be a problem in the IGVM loader, as the IGVM-file contains all necessary state from which the launch measurement can be derived, including the initial VMSA for the BSP.
Quoted 4 lines
Quoted 1 line
(which has happened a couple of times already).
Examples? The SEV features thing jumps to mind, but I don't recall any others off the top of my head.
I was referring to KVM-induced changes to SEV features, but thought this happened more than once?
-Joerg
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 9 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Jörg Rödel
(which has happened a couple of times already).
Examples? The SEV features thing jumps to mind, but I don't recall any others off the top of my head.
I was referring to KVM-induced changes to SEV features, but thought this happened more than once?
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.
The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
-Joerg
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
In other words, I want tests, not more complexity in KVM.
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Jörg Rödel
The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case,
The situation today is that the launch measurement can be predicted with a lot of internal KVM knowledge (Knowing that KVM creates a VMSA at a fixed GPA at launch time, how KVM sets it up, ...) and detailed knowledge of the VM instance (number of VCPUs).
What KVM can NOT do today is to launch an SNP VM with a launch measurement as predicted by the IGVM format.
And this is what concerns me because the industry is moving towards IGVM for predicting launch measurments of CoCo VMs. It is supported in QEMU, in Cloud Hypervisor, FUKI will use it, SVSM relies on it, and there are certainly more users I am not aware of.
Quoted 2 lines
then the launch measurement *must* be stable across kernels because it's part of KVM's ABI.
The current behavior stays in place and is kept as the default, no ABI breakage.
Quoted 2 lines
So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
I agree that KVM does not cause inherently unpredictable measurements, but a lot of internal KVM and VM knowledge is needed to predict it. This is a problem IGVM is meant to solve in a platform- and hypervisor-independent way.
Do you agree that supporting the IGVM use-case is valuable? If so, what would be your suggestion to make it work on KVM?
-Joerg
RE: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Sean ChristophersonQuoted 12 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
Quoted 5 lines
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors. This is what it means to have a stable ID block, so that the same ID block can be used every time the guest is launched. The synthetic GPA that KVM uses today to associate the VMSA is not architectural, so it is not used by any other hypervisor. The only architectural concept that exists is GPA, and the only consistent notion of GPA is the guest address space, so permitting the guest image to specify the VMSA GPA is not only the obvious choice, but it is the only apparent choice. Beyond that, the SNP architecture does not specify an architectural startup state for the boot processor, and guests expect to be able to provide whatever contents they need, which certainly suggests that the data of the VMSA is guest-controlled.
Regarding the treatment of VMSAs generally, the SNP architecture considers these to be guest-owned data because they have protected contents. Outside of the boot processor, every AP starts by donating a GPA page to become the VMSA of the newly started processor, which is necessary because the guest itself write protected contents into the VMSA to initialize it before it can become eligible for execution. And beyond the AP case, when multiple VMPLs are involved, each lower VMPLs VMSA also originates as a GPA page, and here it is not only for initialization purposes, but because the higher VMPL has an ongoing need to read and write the VMSAs of the lower VMPLs for management purposes. Without the lower VMSAs living in the GPA space, it is difficult to reason about how the highest VMPL could retain a valid mapping for this access.
So when you add up the fact that AP VMSAs are intended to be guest-owned (by the GHCB protocol) and the VMSAs of the lower VMPLs are required to be guest-owned (for ongoing access), the idea that the BSP VMSA must be host-controlled makes it the oddball. If the system has to support VMSAs living in GPA space generally, then it doesn't seem like a stretch to say that the BSP VMSAs could be treated identically. This gives consistency not only with the rest of the VMSA design, but gives cross-hypervisor consistency as well.
-Jon
Sean Christopherson
maintainerRe: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 13 lines · Jon Lange
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
Quoted 8 lines
Quoted 5 lines
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
*sigh*
So that, and also the multi-VMPL implications, absolutely need to be decribed in about this level of detail in the cover letter, and the changelog needs about the same level of documentation to justify the various design decisions.
Bluntly, all of the changelogs in this series are awful. +200 lines of code in arguably the nastiest bit of "architecture" KVM has to deal with, and the longest changelog barely hits 6 lines.
And whatever uAPI we end up with needs tests, and a lot of them, including coverage for negative testcases. Because I'm working on fixing the third? guest-exploitable DoS that's unique to SNP this year, and I've reached my breaking point: I'm not taking new functionality like this without sufficient test coverage.
Rant(s) aside, thanks for the information, it's super helpful!
Re: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jon LangeQuoted 13 lines · Jon Lange
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
Quoted 7 lines
Quoted 5 lines
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
If this is now suddenly an acceptable argument, please also merge https://lkml.org/lkml/2021/4/12/625
--
Jethro Beekman | CTO | Fortanix
Sean Christopherson
maintainerRe: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 20 lines · Jethro Beekman
Quoted 19 lines · Jon Lange
Quoted 17 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Quoted 5 lines
Quoted 3 lines
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
If this is now suddenly an acceptable argument, please also merge https://lkml.org/lkml/2021/4/12/625
No, that's not how this works. Dave and Jarkko both have unanswered questions regarding the use case. Answer their questions, and I'm confident you'll get traction.
: I don't believe we necessarily *WANT* or need Linux to support "all : possible ECREATE, EADD, EEXTEND, EINIT sequences". Yet, it's what is : being used to justify this series without any other justification. :
: It's going to be a different story if you bring me a real enclave that : *REALLY* wants to do this for good reasons.
and
: What specifically are you referring as the "rest of the world"? :
: That would be mean that there is reviewable workload "out there".
The start of that thread is *exactly* what's playing out here. The big difference, and why this one's likely getting a different result, is that Jon provided a very thorough explanation of exactly what use case Joerg and company want to support. The only "assumed knowledge" is why it's desirable for the measurement to be stable across hypervisors, but I'm comfortable stitching that together on my own.
In other words, they aren't asking KVM to support every possible way/time a VMSA could be associated with a vCPU, they're asking to extend KVM to support a concrete use case, with meaningful real world impact. In fact, I actually think this series is *too* narrowly focused on their use case.
FWIW, KVM_TDX_INIT_MEM_REGION has almost the exact same ABI that SGX has: pages contents are measured immediately after they're added.
Re: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 50 lines · Sean Christopherson
Quoted 20 lines · Jethro Beekman
Quoted 19 lines · Jon Lange
Quoted 17 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Quoted 5 lines
Quoted 3 lines
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
If this is now suddenly an acceptable argument, please also merge https://lkml.org/lkml/2021/4/12/625
No, that's not how this works. Dave and Jarkko both have unanswered questions regarding the use case. Answer their questions, and I'm confident you'll get traction.
: I don't believe we necessarily *WANT* or need Linux to support "all : possible ECREATE, EADD, EEXTEND, EINIT sequences". Yet, it's what is : being used to justify this series without any other justification. :
: It's going to be a different story if you bring me a real enclave that : *REALLY* wants to do this for good reasons.and
: What specifically are you referring as the "rest of the world"? :
: That would be mean that there is reviewable workload "out there".The start of that thread is *exactly* what's playing out here. The big difference, and why this one's likely getting a different result, is that Jon provided a very thorough explanation of exactly what use case Joerg and company want to support. The only "assumed knowledge" is why it's desirable for the measurement to be stable across hypervisors, but I'm comfortable stitching that together on my own.
Quoted 7 lines
In other words, they aren't asking KVM to support every possible way/time a VMSA could be associated with a vCPU, they're asking to extend KVM to support a concrete use case, with meaningful real world impact. In fact, I actually think this series is *too* narrowly focused on their use case.
FWIW,
KVM_TDX_INIT_MEM_REGIONhas almost the exact same ABI that SGX has: pages contents are measured immediately after they're added.
Sorry, the argument from Jon/Joerg is no different than the one we made years ago for SGX.
It was previously made abundantly clear that it's the kernel maintainers's stance that it's sufficient to offer a UAPI that allows the creation of a Linux-specific subset of enclaves with a stable measurement encompassing largely the enclave functionality available in the hardware. Or said another way: if you can take your enclave and modify it to work with Linux, albeit with a different measurement, that is acceptable. KVM-SNP is currently at that state: it is currently completely possible to load VMs that were designed with the current Linux VMSA in IGVM format.
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
--
Jethro Beekman | CTO | Fortanix
Sean Christopherson
maintainerRe: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 71 lines · Jethro Beekman
Quoted 50 lines · Sean Christopherson
Quoted 20 lines · Jethro Beekman
Quoted 19 lines · Jon Lange
Quoted 17 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Quoted 5 lines
Quoted 3 lines
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
If this is now suddenly an acceptable argument, please also merge https://lkml.org/lkml/2021/4/12/625
No, that's not how this works. Dave and Jarkko both have unanswered questions regarding the use case. Answer their questions, and I'm confident you'll get traction.
: I don't believe we necessarily *WANT* or need Linux to support "all : possible ECREATE, EADD, EEXTEND, EINIT sequences". Yet, it's what is : being used to justify this series without any other justification. :
: It's going to be a different story if you bring me a real enclave that : *REALLY* wants to do this for good reasons.and
: What specifically are you referring as the "rest of the world"? :
: That would be mean that there is reviewable workload "out there".The start of that thread is *exactly* what's playing out here. The big difference, and why this one's likely getting a different result, is that Jon provided a very thorough explanation of exactly what use case Joerg and company want to support. The only "assumed knowledge" is why it's desirable for the measurement to be stable across hypervisors, but I'm comfortable stitching that together on my own.
Quoted 7 lines
In other words, they aren't asking KVM to support every possible way/time a VMSA could be associated with a vCPU, they're asking to extend KVM to support a concrete use case, with meaningful real world impact. In fact, I actually think this series is *too* narrowly focused on their use case.
FWIW,
KVM_TDX_INIT_MEM_REGIONhas almost the exact same ABI that SGX has: pages contents are measured immediately after they're added.Sorry, the argument from Jon/Joerg is no different than the one we made years ago for SGX.
It was previously made abundantly clear that it's the kernel maintainers's stance that it's sufficient to offer a UAPI that allows the creation of a Linux-specific subset of enclaves with a stable measurement encompassing largely the enclave functionality available in the hardware. Or said another
way: if you can take your enclave and modify it to work with Linux, albeit with a different measurement, that is acceptable. KVM-SNP is currently at that state: it is currently completely possible to load VMs that were designed with the current Linux VMSA in IGVM format.
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary 0xFFFFFFFFF000, and (b) to play nice with multi-VMPL scenarios in the future. If that happens to let userspace build every architecturally possible VM, then yay!, we got lucky and probably won't ever need to revisit this. But "support every possibility" is not the goal.
Side topic, if someone is going to propose changes for TDX measurements related to IGVM, now would be a good time to mention them :-)
Re: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 72 lines · Sean Christopherson
Quoted 71 lines · Jethro Beekman
Quoted 50 lines · Sean Christopherson
Quoted 20 lines · Jethro Beekman
Quoted 19 lines · Jon Lange
Quoted 17 lines · Sean Christopherson
Quoted 10 lines · Jörg Rödel
Quoted 5 lines · Sean Christopherson
Ok, so it took us a few times to learn our lesson. I still don't see that as a strong argument for new uAPI, especially not for VMSA pages. I am very firmly of the opinion that letting anything but the host kernel configure the VMSA is beyond stupid, but unfortunately we're stuck with
AP_CREATION. Expanding that surface has a very, very, VERY high bar to get over.The strongest argument in my view (and the main reason we are doing this) is actually the predictable launch measurement. On SEV-SNP this is a requirement to use platform VM-identity features like the ID Block.
And I'm saying that unless KVM *can't* provide a predictable launch measurement, which AIUI isn't the case, then the launch measurement *must* be stable across kernels because it's part of KVM's ABI. So as I see it, the issue isn't that KVM is inherently unpredictable, it's that we lack tests to validate a thorny, subtle piece of KVM's ABI.
Quoted 5 lines
Quoted 3 lines
Joerg is suggesting that we need a launch measurement that is stable not just across multiple launches on the same system, but across multiple hypervisors.
If this is now suddenly an acceptable argument, please also merge https://lkml.org/lkml/2021/4/12/625
No, that's not how this works. Dave and Jarkko both have unanswered questions regarding the use case. Answer their questions, and I'm confident you'll get traction.
: I don't believe we necessarily *WANT* or need Linux to support "all : possible ECREATE, EADD, EEXTEND, EINIT sequences". Yet, it's what is : being used to justify this series without any other justification. :
: It's going to be a different story if you bring me a real enclave that : *REALLY* wants to do this for good reasons.and
: What specifically are you referring as the "rest of the world"? :
: That would be mean that there is reviewable workload "out there".The start of that thread is *exactly* what's playing out here. The big difference, and why this one's likely getting a different result, is that Jon provided a very thorough explanation of exactly what use case Joerg and company want to support. The only "assumed knowledge" is why it's desirable for the measurement to be stable across hypervisors, but I'm comfortable stitching that together on my own.
Quoted 7 lines
In other words, they aren't asking KVM to support every possible way/time a VMSA could be associated with a vCPU, they're asking to extend KVM to support a concrete use case, with meaningful real world impact. In fact, I actually think this series is *too* narrowly focused on their use case.
FWIW,
KVM_TDX_INIT_MEM_REGIONhas almost the exact same ABI that SGX has: pages contents are measured immediately after they're added.Sorry, the argument from Jon/Joerg is no different than the one we made years ago for SGX.
It was previously made abundantly clear that it's the kernel maintainers's stance that it's sufficient to offer a UAPI that allows the creation of a Linux-specific subset of enclaves with a stable measurement encompassing largely the enclave functionality available in the hardware. Or said another
way: if you can take your enclave and modify it to work with Linux, albeit with a different measurement, that is acceptable. KVM-SNP is currently at that state: it is currently completely possible to load VMs that were designed with the current Linux VMSA in IGVM format.
Quoted 7 lines
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary
0xFFFFFFFFF000, and
As Jon points out, the VMSA GPA is not architecturally relevant, so any enclave can be changed to support a BSP VMSA at 0xFFFFFFFFF000. The only reason to "support VMSA GPAs other than KVM's hardcoded, arbitrary 0xFFFFFFFFF000" is measurement portability.
Quoted 1 line
(b) to play nice with multi-VMPL scenarios in the future.
It's not clear to me what multi-VMPL scenario Jon is describing that requires custom VMSA content/address at launch time other than measurement portability. It's currently completely possible for VMPL0 code to set the VMPL of a VMSA or create a new VMSA with VMPL>0 at any GPA of their choosing.
Quoted 3 lines
If that happens to let userspace build
every architecturally possible VM, then yay!, we got lucky and probably won't ever need to revisit this. But "support every possibility" is not the goal.
Signature · 1 line
Jethro Beekman | CTO | Fortanix
Sean Christopherson
maintainerRe: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 6 lines · Jethro Beekman
Quoted 5 lines · Sean Christopherson
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
Quoted 7 lines
Quoted 2 lines
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary
0xFFFFFFFFF000, andAs Jon points out, the VMSA GPA is not architecturally relevant, so any enclave can be changed to support a BSP VMSA at
0xFFFFFFFFF000. The only reason to "support VMSA GPAs other than KVM's hardcoded, arbitrary0xFFFFFFFFF000" is measurement portability.
Well, that and hardcoding 0xFFFFFFFFF000 is bizarre and confusing, IMO.
Quoted 6 lines
Quoted 1 line
(b) to play nice with multi-VMPL scenarios in the future.
It's not clear to me what multi-VMPL scenario Jon is describing that requires custom VMSA content/address at launch time other than measurement portability. It's currently completely possible for VMPL0 code to set the VMPL of a VMSA or create a new VMSA with VMPL>0 at any GPA of their choosing.
Sorry, I phrased that poorly. It's not about directly playing nice with multi-VMPL scenarios, it's that if/when multi-VMPL support comes along, the BSP's VMSA for VMPL0 will likely be the only VMSA that is NOT in guest memory. And so supporting an in-guest VMSA for BSP VMPL0 would provide consistency on top of cross-hypervisor compatilibity, and I place a lot of value on consistency.
FWIW, I loathe the AP_CREATE scheme. I wish we could go back in time and burn it with fire. Unfortunately, we're stuck with it, i.e. we need to deal with the complexity of in-guest VMSAs regardless of whether or not KVM supports one for the BSP, at which point we don't gain much by refusing to support "bring your own VMSA" (so long as the uAPI and implementation are clean and don't add undue burden).
Re: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 24 lines · Sean Christopherson
Quoted 6 lines · Jethro Beekman
Quoted 5 lines · Sean Christopherson
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
Quoted 7 lines
Quoted 2 lines
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary
0xFFFFFFFFF000, andAs Jon points out, the VMSA GPA is not architecturally relevant, so any enclave can be changed to support a BSP VMSA at
0xFFFFFFFFF000. The only reason to "support VMSA GPAs other than KVM's hardcoded, arbitrary0xFFFFFFFFF000" is measurement portability.Well, that and hardcoding
0xFFFFFFFFF000is bizarre and confusing, IMO.Quoted 6 lines
Quoted 1 line
(b) to play nice with multi-VMPL scenarios in the future.
It's not clear to me what multi-VMPL scenario Jon is describing that requires custom VMSA content/address at launch time other than measurement portability. It's currently completely possible for VMPL0 code to set the VMPL of a VMSA or create a new VMSA with VMPL>0 at any GPA of their choosing.
Quoted 5 lines
Sorry, I phrased that poorly. It's not about directly playing nice with multi-VMPL scenarios, it's that if/when multi-VMPL support comes along, the BSP's VMSA for VMPL0 will likely be the only VMSA that is NOT in guest memory. And so supporting an in-guest VMSA for BSP VMPL0 would provide consistency on top of cross-hypervisor compatilibity, and I place a lot of value on consistency.
If the goal is to get rid of the arbitrary 0xFFFFFFFFF000 GPA/put the VMSA in guest memory, then we just need a vcpu ioctl that lets you set the GPA.
--
Jethro Beekman | CTO | Fortanix
Sean Christopherson
maintainerRe: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 25 lines · Jethro Beekman
Quoted 24 lines · Sean Christopherson
Quoted 6 lines · Jethro Beekman
Quoted 5 lines · Sean Christopherson
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
Quoted 7 lines
Quoted 2 lines
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary
0xFFFFFFFFF000, andAs Jon points out, the VMSA GPA is not architecturally relevant, so any enclave can be changed to support a BSP VMSA at
0xFFFFFFFFF000. The only reason to "support VMSA GPAs other than KVM's hardcoded, arbitrary0xFFFFFFFFF000" is measurement portability.Well, that and hardcoding
0xFFFFFFFFF000is bizarre and confusing, IMO.Quoted 6 lines
Quoted 1 line
(b) to play nice with multi-VMPL scenarios in the future.
It's not clear to me what multi-VMPL scenario Jon is describing that requires custom VMSA content/address at launch time other than measurement portability. It's currently completely possible for VMPL0 code to set the VMPL of a VMSA or create a new VMSA with VMPL>0 at any GPA of their choosing.
Quoted 8 lines
Quoted 5 lines
Sorry, I phrased that poorly. It's not about directly playing nice with multi-VMPL scenarios, it's that if/when multi-VMPL support comes along, the BSP's VMSA for VMPL0 will likely be the only VMSA that is NOT in guest memory. And so supporting an in-guest VMSA for BSP VMPL0 would provide consistency on top of cross-hypervisor compatilibity, and I place a lot of value on consistency.
If the goal is to get rid of the arbitrary
0xFFFFFFFFF000GPA/put the VMSA in guest memory, then we just need a vcpu ioctl that lets you set the GPA.
That's more or less what I suggested[*], hopefully by piggybacking the AP_CREATE logic inasmuch as possible. Or were you thinking something even simpler?
Re: [EXTERNAL] Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 32 lines · Sean Christopherson
Quoted 31 lines · Jethro Beekman
Quoted 29 lines · Sean Christopherson
Quoted 6 lines · Jethro Beekman
Quoted 5 lines · Sean Christopherson
Quoted 4 lines
A UAPI that allows creation of all architecturally-valid enclaves for purposes of measurement portability was stated as an explicit non-goal. As this is the only stated purpose of this patch set, it should also not be accepted.
Quoted 7 lines
Quoted 2 lines
No, the stated goals (thanks to the follow-up from Jon) are to (a) support VMSA GPAs other than KVM's hardcoded, arbitrary
0xFFFFFFFFF000, andAs Jon points out, the VMSA GPA is not architecturally relevant, so any enclave can be changed to support a BSP VMSA at
0xFFFFFFFFF000. The only reason to "support VMSA GPAs other than KVM's hardcoded, arbitrary0xFFFFFFFFF000" is measurement portability.Well, that and hardcoding
0xFFFFFFFFF000is bizarre and confusing, IMO.Quoted 6 lines
Quoted 1 line
(b) to play nice with multi-VMPL scenarios in the future.
It's not clear to me what multi-VMPL scenario Jon is describing that requires custom VMSA content/address at launch time other than measurement portability. It's currently completely possible for VMPL0 code to set the VMPL of a VMSA or create a new VMSA with VMPL>0 at any GPA of their choosing.
Sorry, I phrased that poorly. It's not about directly playing nice with multi-VMPL scenarios, it's that if/when multi-VMPL support comes along, the BSP's VMSA for VMPL0 will likely be the only VMSA that is NOT in guest memory. And so supporting an in-guest VMSA for BSP VMPL0 would provide consistency on top of cross-hypervisor compatilibity, and I place a lot of value on consistency.
Quoted 7 lines
Quoted 2 lines
If the goal is to get rid of the arbitrary
0xFFFFFFFFF000GPA/put the VMSA in guest memory, then we just need a vcpu ioctl that lets you set the GPA.That's more or less what I suggested[*], hopefully by piggybacking the
AP_CREATElogic inasmuch as possible. Or were you thinking something even simpler?
This seems pretty much in line with what I just suggested. In that message, I didn't realize you were already suggesting to replace the entire patch set with that ioctl.
--
Jethro Beekman | CTO | Fortanix
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jörg RödelQuoted 8 lines · Jörg Rödel
Quoted 3 lines · Sean Christopherson
Isn't this essentially the same thing as hot-plugging vCPUs after launch? I have yet to review it in depth (sorry Jethro), but it looks a *lot* simpler.
Replacing the VMSA after launch (as supported with the
AP_CREATEGHCB call) is different because this has no influence on the launch measurement.
That's true for the launch measure, but you do have a vTPM inside the SVSM which you could use to measure an updated GHCB if we could decide on a PCR to use (and a logging specification).
Regards,
James
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Hi James,
Quoted 3 lines · James Bottomley
That's true for the launch measure, but you do have a vTPM inside the SVSM which you could use to measure an updated GHCB if we could decide on a PCR to use (and a logging specification).
I guess you mean an updated VMSA? The guest-created VMSAs are trusted because they are created by trusted code from within the TEE. There is no need to runtime-measure their content, no?
-Joerg
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 10 lines · Jörg Rödel
Hi James,
Quoted 3 lines · James Bottomley
That's true for the launch measure, but you do have a vTPM inside the SVSM which you could use to measure an updated GHCB if we could decide on a PCR to use (and a logging specification).
I guess you mean an updated VMSA? The guest-created VMSAs are trusted because they are created by trusted code from within the TEE. There is no need to runtime-measure their content, no?
Well if the guest policy is I don't care how many CPUs you give me then certainly, yes. However, if the guest does care, they may want an attestable record of it somewhere. Since clouds do charge somewhat per-vCPU I can see this mattering to some tenants.
Regards,
James
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 4 lines · James Bottomley
Well if the guest policy is I don't care how many CPUs you give me then certainly, yes. However, if the guest does care, they may want an attestable record of it somewhere. Since clouds do charge somewhat per-vCPU I can see this mattering to some tenants.
Yes, the number of VCPUs certainly matters to most tenants. But I doubt there is much value in including this information into any measurment, as the measurement does not guarantee the tenant that the host actually executes the VCPU. The best proof for the existence of any given VCPU is the fact that it executes code.
Regards,
Joerg
[email protected]
AI reviewRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jörg RödelThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [High] Missing srcu_read_lock() when calling gfn_to_memslot() in sev_es_vmsa_pa(), leading to a potential Use-After-Free of KVM memslots.
- [Low] The local variable sev_features in sev_gmem_post_populate() is assigned but never used.
Pre-existing issues:
- [Critical] KVM writes firmware data to a potentially read-only userspace page during CPUID error recovery, which could corrupt the global zero page.
- [High] Out-of-order kunmap_local() calls violate the highmem API's strict LIFO unmapping requirement.
- [High] A malicious host userspace process can trigger a kernel warning (WARN_ON_ONCE) by passing a NULL user address (uaddr = 0) when issuing KVM_SEV_SNP_LAUNCH_UPDATE for any non-ZERO page type. --
commit f2475c503a445642b6525297ce3c8dd1d2a1067f
Author: Joerg Roedel <[email protected]>
kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
This commit adds support for setting a VMSA in guest physical memory during the SEV-SNP launch process. It introduces a new page type, KVM_SEV_SNP_PAGE_TYPE_VMSA, which is used for the BSP.
Quoted 4 lines
arch/x86/kvm/svm/sev.c +0
-0
[ ... ]
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2553
Quoted 9 lines
arch/x86/kvm/svm/sev.c +2
-0@@ -2532,7 +2546,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,struct sev_gmem_populate_args *sev_populate_args = opaque;struct sev_data_snp_launch_update fw_args = {0 };struct kvm_sev_info *sev =to_kvm_sev_info (kvm); +gpa_t gpa = gfn <<PAGE_SHIFT ;bool assigned = false; +u64 sev_features =0 ;int level;int ret;
[Severity: Low]
Is the sev_features variable necessary? It gets assigned below when processing a KVM_SEV_SNP_PAGE_TYPE_VMSA page but doesn't appear to be used anywhere else in this function.
[ ... ]
Quoted 7 lines
arch/x86/kvm/svm/sev.c +2
-0@@ -2550,14 +2566,27 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (src_page) {void *src_vaddr =kmap_local_page (src_page);void *dst_vaddr =kmap_local_pfn (pfn); +struct sev_es_save_area *vmsa = dst_vaddr; +bool accept_page = true;
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2581
Quoted 10 lines
arch/x86/kvm/svm/sev.c +5
-0memcpy (dst_vaddr, src_vaddr,PAGE_SIZE ); +if (sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { + accept_page =snp_check_launch_vmsa (sev, vmsa); +if (accept_page) + sev_features = vmsa->sev_features; + }kunmap_local (src_vaddr);kunmap_local (dst_vaddr);
[Severity: High]
This is a pre-existing issue, but does this violate the LIFO unmapping requirement of the highmem API?
The kmap_local_page() documentation requires mappings to be unmapped in the exact reverse order. Here src_vaddr is mapped first, then dst_vaddr, but they are unmapped in the same order instead of reverse. Could this corrupt the internal stack on the next kmap/kunmap operations?
[ ... ]
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2627
Quoted 7 lines
arch/x86/kvm/svm/sev.c +3
-0@@ -2593,6 +2622,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,kunmap_local (dst_vaddr); } +if (ret ==0 && sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) + sev->initial_vmsa_gpa = gpa; +
[Severity: High]
Will this new VMSA initialization path expose a use-after-free of KVM memslots during launch finish?
By setting sev->initial_vmsa_gpa to a valid page here, the ioctl path KVM_SEV_SNP_LAUNCH_FINISH can now call sev_es_vmsa_pa(), which looks up the slot:
arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() {
...
slot = gfn_to_memslot(vcpu->kvm, gfn);
...
}Since the launch finish ioctl takes kvm->lock and vcpu->mutex, but not srcu_read_lock(), could a concurrent memslot update swap and free the memslot before it is accessed in kvm_gmem_get_pfn()?
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2660
Quoted 13 lines
arch/x86/kvm/svm/sev.c +1
-0out:if (ret) pr_debug("%s: error updating GFN %llx, return code %d (fw_error %d)\n",@@ -2620,12 +2652,22 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!params.len || !PAGE_ALIGNED (params.len) || params.flags || (params.type !=KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type !=KVM_SEV_SNP_PAGE_TYPE_VMSA && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO && params.type !=KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && params.type !=KVM_SEV_SNP_PAGE_TYPE_SECRETS && params.type !=KVM_SEV_SNP_PAGE_TYPE_CPUID ))return -EINVAL ;
[Severity: Critical]
This isn't a bug introduced by this patch, but does the error recovery path for KVM_SEV_SNP_PAGE_TYPE_CPUID write to a potentially read-only page?
When handling the CPUID type, kvm_gmem_populate() calls get_user_pages_fast() with gup_flags = 0, requesting read-only access. If userspace mapped a read-only page like the global zero page, GUP will succeed.
If the firmware returns SEV_RET_INVALID_PARAM, sev_gmem_post_populate() maps the page with kmap_local_page() and copies the expected values into it:
memcpy(src_vaddr, dst_vaddr, PAGE_SIZE);
Could this illegally overwrite read-only memory and corrupt the global zero page?
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-0+if (params.type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { +/* VMSA page are allowed only once */ +if (sev->initial_vmsa_gpa !=INVALID_PAGE ) +return -EBUSY ; +/* Can only deploy a single page as VMSA */ +if (params.len !=PAGE_SIZE ) +return -EINVAL ; + } src = params.type ==KVM_SEV_SNP_PAGE_TYPE_ZERO ?NULL :u64_to_user_ptr (params.uaddr);
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?
If params.uaddr is 0, src becomes NULL and passes the PAGE_ALIGNED(src) check. kvm_gmem_populate() skips fetching the user page and passes src_page = NULL to sev_gmem_post_populate().
That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type != KVM_SEV_SNP_PAGE_TYPE_ZERO &&
!src_page)
Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 4 lines · On Thu, Jun 11, 2026 at 12:58:49PM +0000, [email protected] wrote:
[Severity: Low]
Is thesev_featuresvariable necessary? It gets assigned below when processing aKVM_SEV_SNP_PAGE_TYPE_VMSApage but doesn't appear to be used anywhere else in this function.
Yes, that is a left-over and can be removed.
Quoted 8 lines
[Severity: High]
This is a pre-existing issue, but does this violate the LIFO unmapping requirement of the highmem API?The
kmap_local_page()documentation requires mappings to be unmapped in the exact reverse order. Heresrc_vaddris mapped first, thendst_vaddr, but they are unmapped in the same order instead of reverse. Could this corrupt the internal stack on the next kmap/kunmap operations?
Right, I will fix it in this patch.
Quoted 14 lines
[Severity: High]
Will this new VMSA initialization path expose a use-after-free of KVM memslots during launch finish?By setting
sev->initial_vmsa_gpato a valid page here, the ioctl pathKVM_SEV_SNP_LAUNCH_FINISHcan now callsev_es_vmsa_pa(), which looks up the slot:arch/x86/kvm/svm/sev.c:sev_es_vmsa_pa() { ... slot = gfn_to_memslot(vcpu->kvm, gfn); ... }
Quoted 3 lines
Since the launch finish ioctl takes
kvm->lockandvcpu->mutex, but notsrcu_read_lock(), could a concurrent memslot update swap and free the memslot before it is accessed inkvm_gmem_get_pfn()?
Will further investigate this. If there is an issue it is independent of this series.
Quoted 8 lines
[Severity: Critical]
This isn't a bug introduced by this patch, but does the error recovery path forKVM_SEV_SNP_PAGE_TYPE_CPUIDwrite to a potentially read-only page?When handling the CPUID type,
kvm_gmem_populate()callsget_user_pages_fast()withgup_flags= 0, requesting read-only access. If userspace mapped a read-only page like the global zero page, GUP will succeed.
Quoted 7 lines
If the firmware returns
SEV_RET_INVALID_PARAM,sev_gmem_post_populate()maps the page withkmap_local_page()and copies the expected values into it:
memcpy(src_vaddr, dst_vaddr, PAGE_SIZE);Could this illegally overwrite read-only memory and corrupt the global zero page?
Looks like this can happen, but not introduced by this series. It needs a separate fix.
Quoted 8 lines
[Severity: High]
This is a pre-existing issue, but can a host userspace process trigger a kernel warning by passing a NULL user address (uaddr = 0) here?If params.uaddr is 0, src becomes NULL and passes the
PAGE_ALIGNED(src)check.kvm_gmem_populate()skips fetching the user page and passessrc_page= NULL tosev_gmem_post_populate().
Quoted 7 lines
That function then unconditionally evaluates:
WARN_ON_ONCE(sev_populate_args->type !=
KVM_SEV_SNP_PAGE_TYPE_ZERO&&!src_page)Since the type isn't ZERO, won't this allow an unprivileged user to spam the kernel log?
This is correct, we will send a separate fix.
-Joerg
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jörg RödelQuoted 69 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 4 files changed, 46 insertions(+), 1 deletion(-)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -885,6 +885,7 @@ struct kvm_sev_snp_launch_start {/* Kept in sync with firmware values for simplicity. */ #define KVM_SEV_PAGE_TYPE_INVALID 0x0#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 0x5arch/x86/kvm/svm/sev.c +16
-0@@ -2520,6 +2520,20 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return rc; } +static bool snp_check_launch_vmsa (struct kvm_sev_info *sev, +struct sev_es_save_area *vmsa) +{ +/* VMSA sev_features must match VMs vmsa_features */ +if (vmsa->sev_features != sev->vmsa_features) +return false; + +/* Must always boot from VMPL0 */ +if (vmsa->vmpl !=0 ) +return false; + +return true; +} +struct sev_gmem_populate_args {__u8 type;int sev_fd;@@ -2532,7 +2546,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,struct sev_gmem_populate_args *sev_populate_args = opaque;struct sev_data_snp_launch_update fw_args = {0 };struct kvm_sev_info *sev =to_kvm_sev_info (kvm); +gpa_t gpa = gfn <<PAGE_SHIFT ;bool assigned = false; +u64 sev_features =0 ;int level;int ret;@@ -2550,14 +2566,27 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (src_page) {void *src_vaddr =kmap_local_page (src_page);void *dst_vaddr =kmap_local_pfn (pfn);
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2578
Quoted 10 lines
arch/x86/kvm/svm/sev.c +7
-0+struct sev_es_save_area *vmsa = dst_vaddr; +bool accept_page = true;memcpy (dst_vaddr, src_vaddr,PAGE_SIZE ); +if (sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { + accept_page =snp_check_launch_vmsa (sev, vmsa); +if (accept_page) + sev_features = vmsa->sev_features; + }
I don't think there is a race here given the way guest_memfd works today. I haven't followed in-place conversion closely, but will that result in a race between when the snp_check_launch_vmsa() check is performed and before the page is made a firmware page?
Quoted 16 lines
arch/x86/kvm/svm/sev.c +7
-1+kunmap_local (src_vaddr);kunmap_local (dst_vaddr); + +if (!accept_page) { + ret = -EINVAL ; + goto out; + } } - ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT ,PG_LEVEL_4K , + ret =rmp_make_private (pfn, gpa,PG_LEVEL_4K ,sev_get_asid (kvm), true);if (ret) goto out;
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2625
Quoted 4 lines
arch/x86/kvm/svm/sev.c +1
-0@@ -2593,6 +2622,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,kunmap_local (dst_vaddr); } +if (ret ==0 && sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA )
s/ret == 0/!ret/
Thanks,
Tom
Quoted 52 lines
arch/x86/kvm/svm/sev.c +12
-0+ sev->initial_vmsa_gpa = gpa; + out:if (ret) pr_debug("%s: error updating GFN %llx, return code %d (fw_error %d)\n",@@ -2620,12 +2652,22 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp)if (!params.len || !PAGE_ALIGNED (params.len) || params.flags || (params.type !=KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type !=KVM_SEV_SNP_PAGE_TYPE_VMSA && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO && params.type !=KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && params.type !=KVM_SEV_SNP_PAGE_TYPE_SECRETS && params.type !=KVM_SEV_SNP_PAGE_TYPE_CPUID ))return -EINVAL ; +if (params.type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { +/* VMSA page are allowed only once */ +if (sev->initial_vmsa_gpa !=INVALID_PAGE ) +return -EBUSY ; +/* Can only deploy a single page as VMSA */ +if (params.len !=PAGE_SIZE ) +return -EINVAL ; + } + src = params.type ==KVM_SEV_SNP_PAGE_TYPE_ZERO ?NULL :u64_to_user_ptr (params.uaddr);if (!PAGE_ALIGNED (src))arch/x86/kvm/x86.c +1
-0@@ -4870,6 +4870,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MEMORY_FAULT_INFO :case KVM_CAP_X86_GUEST_MODE :case KVM_CAP_ONE_REG : +case KVM_CAP_SNP_DIRECT_VMSA : r =1 ;break ;case KVM_CAP_PRE_FAULT_MEMORY :include/uapi/linux/kvm.h +1
-0@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_SNP_DIRECT_VMSA 249struct kvm_irq_routing_irqchip {__u32 irqchip;
Re: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
Quoted 11 lines · Tom Lendacky
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2578
Quoted 5 lines · Jörg Rödel
arch/x86/kvm/svm/sev.c +5
-0+if (sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { + accept_page =snp_check_launch_vmsa (sev, vmsa); +if (accept_page) + sev_features = vmsa->sev_features; + }I don't think there is a race here given the way
guest_memfdworks today. I haven't followed in-place conversion closely, but will that result in a race between when thesnp_check_launch_vmsa()check is performed and before the page is made a firmware page?
I don't know too much about the in-place conversion changes, but when they directly pass user-memory to the PSP for encryption the code still needs to make sure it is not mapped to user-space anymore once it is made private.
So with in-place conversion this might need changing so that the sev_features check is placed after the page has been unmapped from user-mode and before it is passed to the PSP.
Quoted 3 lines
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2625
Quoted 1 line
arch/x86/kvm/svm/sev.c +1
-0+if (ret ==0 && sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA )s/ret == 0/!ret/
Updated.
Thanks,
Joerg
Sean Christopherson
maintainerRe: [PATCH 4/4] kvm: svm: Support KVM_SEV_SNP_PAGE_TYPE_VMSA at SNP_LAUNCH_UPDATE
In reply to Jörg RödelQuoted 36 lines · Jörg Rödel
From: Joerg Roedel <[email protected]>
Support setting a VMSA in guest physical memory during the SEV-SNP launch process. Only one VMSA can be provided which will then be used for the BSP. All of the APs will not have a VMSA allocated or assigned when this feature is used.
This ensures stable launch measurements on SEV-SNP which are independent of the number of VCPUs the VM is launched with.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/svm/sev.c | 44 ++++++++++++++++++++++++++++++++- arch/x86/kvm/x86.c | 1 + include/uapi/linux/kvm.h | 1 + 4 files changed, 46 insertions(+), 1 deletion(-)arch/x86/include/uapi/asm/kvm.h +1
-0@@ -885,6 +885,7 @@ struct kvm_sev_snp_launch_start {/* Kept in sync with firmware values for simplicity. */ #define KVM_SEV_PAGE_TYPE_INVALID 0x0#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 0x5arch/x86/kvm/svm/sev.c +0
-0@@ -2520,6 +2520,20 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)return rc;
arch/x86/kvm/svm/sev.c static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2530
Quoted 10 lines
arch/x86/kvm/svm/sev.c +8
-0} +static bool snp_check_launch_vmsa (struct kvm_sev_info *sev, +struct sev_es_save_area *vmsa) +{ +/* VMSA sev_features must match VMs vmsa_features */ +if (vmsa->sev_features != sev->vmsa_features) +return false; + +/* Must always boot from VMPL0 */
Why?
Quoted 20 lines
arch/x86/kvm/svm/sev.c +8
-0+if (vmsa->vmpl !=0 ) +return false; + +return true; +} +struct sev_gmem_populate_args {__u8 type;int sev_fd;@@ -2532,7 +2546,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,struct sev_gmem_populate_args *sev_populate_args = opaque;struct sev_data_snp_launch_update fw_args = {0 };struct kvm_sev_info *sev =to_kvm_sev_info (kvm); +gpa_t gpa = gfn <<PAGE_SHIFT ;bool assigned = false; +u64 sev_features =0 ;int level;int ret;
arch/x86/kvm/svm/sev.c static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, · line 2575
Quoted 10 lines
arch/x86/kvm/svm/sev.c +4
-0@@ -2550,14 +2566,27 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,if (src_page) {void *src_vaddr =kmap_local_page (src_page);void *dst_vaddr =kmap_local_pfn (pfn); +struct sev_es_save_area *vmsa = dst_vaddr; +bool accept_page = true;memcpy (dst_vaddr, src_vaddr,PAGE_SIZE ); +if (sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { + accept_page =snp_check_launch_vmsa (sev, vmsa);
Isn't this technically a breaking change? AFAICT, nothing prevents userspace from adding a VMSA page today. Ah, never mind, snp_launch_update() does the check.
Anyways, given that SVM_VMGEXIT_AP_CREATE doesn't check the actual memory, and that control.allowed_sev_features is what provides the "real" protection, I would rather either trust userspace to not screw up (first choice), or have userspace provide the requested features as a param to the ioctl (second choice, because it's purely a nice-to-have for userspace).
Then, assuming we don't *need* to enforce the VMPL0 thing, there's no need to modify sev_gmem_post_populate(). I.e. decouple adding VMSA page(s) to the image from associating a vCPU with a VMSA.
Quoted 31 lines
arch/x86/kvm/svm/sev.c +13
-1+if (accept_page) + sev_features = vmsa->sev_features; + } +kunmap_local (src_vaddr);kunmap_local (dst_vaddr); + +if (!accept_page) { + ret = -EINVAL ; + goto out; + } } - ret =rmp_make_private (pfn, gfn <<PAGE_SHIFT ,PG_LEVEL_4K , + ret =rmp_make_private (pfn, gpa,PG_LEVEL_4K ,sev_get_asid (kvm), true);if (ret) goto out;@@ -2593,6 +2622,9 @@ static int sev_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn,kunmap_local (dst_vaddr); } +if (ret ==0 && sev_populate_args->type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) + sev->initial_vmsa_gpa = gpa; + out:if (ret) pr_debug("%s: error updating GFN %llx, return code %d (fw_error %d)\n",@@ -2620,12 +2652,22 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!params.len || !PAGE_ALIGNED(params.len) || params.flags ||
arch/x86/kvm/svm/sev.c static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) · line 2663
Quoted 10 lines
arch/x86/kvm/svm/sev.c +3
-0(params.type !=KVM_SEV_SNP_PAGE_TYPE_NORMAL && + params.type !=KVM_SEV_SNP_PAGE_TYPE_VMSA && params.type !=KVM_SEV_SNP_PAGE_TYPE_ZERO && params.type !=KVM_SEV_SNP_PAGE_TYPE_UNMEASURED && params.type !=KVM_SEV_SNP_PAGE_TYPE_SECRETS && params.type != KVM_SEV_SNP_PAGE_TYPE_CPUID)) return -EINVAL; +if (params.type ==KVM_SEV_SNP_PAGE_TYPE_VMSA ) { +/* VMSA page are allowed only once */
Why? What would go wrong if a VM image has a pile of pre-baked VMSAs?
Quoted 37 lines
arch/x86/kvm/svm/sev.c +7
-0+if (sev->initial_vmsa_gpa !=INVALID_PAGE ) +return -EBUSY ; +/* Can only deploy a single page as VMSA */ +if (params.len !=PAGE_SIZE ) +return -EINVAL ; + } + src = params.type ==KVM_SEV_SNP_PAGE_TYPE_ZERO ?NULL :u64_to_user_ptr (params.uaddr);if (!PAGE_ALIGNED (src))arch/x86/kvm/x86.c +1
-0@@ -4870,6 +4870,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_MEMORY_FAULT_INFO :case KVM_CAP_X86_GUEST_MODE :case KVM_CAP_ONE_REG : +case KVM_CAP_SNP_DIRECT_VMSA : r =1 ;break ;case KVM_CAP_PRE_FAULT_MEMORY :include/uapi/linux/kvm.h +1
-0@@ -996,6 +996,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248 +#define KVM_CAP_SNP_DIRECT_VMSA 249struct kvm_irq_routing_irqchip {__u32 irqchip;Signature · 1 line
2.53.0