>From 835b63222be184596060207b8f9880266b3836ca Mon Sep 17 00:00:00 2001 From: Sean Christopherson Date: Fri, 23 Feb 2024 07:23:57 -0800 Subject: [PATCH 1/4] KVM: SVM: Call sev_vm_destroy() and sev_free_vcpu() only for SEV+ guests Wrap the calls to sev_vm_destroy() and sev_free_vcpu() with sev_guest() to take advantage of dead code elimination when CONFIG_KVM_AMD_SEV=n. This will allow compiling sev.c if and only if CONFIG_KVM_AMD_SEV=y without needing to provide stubs. Note, sev_free_vcpu() only frees resources for SEV-ES guests, which is why the diff doesn't show any code removal. Alternatively, sev_free_vcpu() could be wrapped with sev_es_guest(), but then the name would also need to be updated, skipping the call for SEV guests isn't all that interesting, and doing so would create even more churn if KVM ever does need to free resources for SEV guests. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/sev.c | 3 --- arch/x86/kvm/svm/svm.c | 7 +++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f06f9e51ad9d..4f6052e29eb1 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2137,9 +2137,6 @@ void sev_vm_destroy(struct kvm *kvm) struct list_head *head = &sev->regions_list; struct list_head *pos, *q; - if (!sev_guest(kvm)) - return; - WARN_ON(!list_empty(&sev->mirror_vms)); /* If this is a mirror_kvm release the enc_context_owner and skip sev cleanup */ diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e90b429c84f1..c657e75fd2c6 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1497,7 +1497,8 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) svm_leave_nested(vcpu); svm_free_nested(svm); - sev_free_vcpu(vcpu); + if (sev_guest(vcpu->kvm)) + sev_free_vcpu(vcpu); __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT)); __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE)); @@ -4883,7 +4884,9 @@ static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) static void svm_vm_destroy(struct kvm *kvm) { avic_vm_destroy(kvm); - sev_vm_destroy(kvm); + + if (sev_guest(kvm)) + sev_vm_destroy(kvm); } static int svm_vm_init(struct kvm *kvm) base-commit: ec1e3d33557babed2c2c2c7da6e84293c2f56f58 -- 2.44.0.rc0.258.g7320e95886-goog