[PATCH 11/21] KVM: SEV: Move SEV-specific VM initialization to sev.c

From: Sean Christopherson

Date: Tue Mar 10 2026 - 19:49:46 EST


Move SEV+ VM initialization to sev.c (as sev_vm_init()) so that
kvm_sev_info (and all usage) can be gated on CONFIG_KVM_AMD_SEV=y without
needing more #ifdefs. As a bonus, isolating the logic will make it easier
to harden the flow, e.g. to WARN if the vm_type is unknown.

No functional change intended (SEV, SEV_ES, and SNP VM types are only
supported if CONFIG_KVM_AMD_SEV=y).

Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/svm/sev.c | 15 +++++++++++++++
arch/x86/kvm/svm/svm.c | 12 +-----------
arch/x86/kvm/svm/svm.h | 2 ++
3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 0c5b47272335..e5dae76d2986 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -2928,6 +2928,21 @@ static int snp_decommission_context(struct kvm *kvm)
return 0;
}

+void sev_vm_init(struct kvm *kvm)
+{
+ int type = kvm->arch.vm_type;
+
+ if (type == KVM_X86_DEFAULT_VM || type == KVM_X86_SW_PROTECTED_VM)
+ return;
+
+ kvm->arch.has_protected_state = (type == KVM_X86_SEV_ES_VM ||
+ type == KVM_X86_SNP_VM);
+ to_kvm_sev_info(kvm)->need_init = true;
+
+ kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM);
+ kvm->arch.pre_fault_allowed = !kvm->arch.has_private_mem;
+}
+
void sev_vm_destroy(struct kvm *kvm)
{
struct kvm_sev_info *sev = to_kvm_sev_info(kvm);
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index bd0c497c6040..780acd454913 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -5093,17 +5093,7 @@ static void svm_vm_destroy(struct kvm *kvm)

static int svm_vm_init(struct kvm *kvm)
{
- int type = kvm->arch.vm_type;
-
- if (type != KVM_X86_DEFAULT_VM &&
- type != KVM_X86_SW_PROTECTED_VM) {
- kvm->arch.has_protected_state =
- (type == KVM_X86_SEV_ES_VM || type == KVM_X86_SNP_VM);
- to_kvm_sev_info(kvm)->need_init = true;
-
- kvm->arch.has_private_mem = (type == KVM_X86_SNP_VM);
- kvm->arch.pre_fault_allowed = !kvm->arch.has_private_mem;
- }
+ sev_vm_init(kvm);

if (!pause_filter_count || !pause_filter_thresh)
kvm_disable_exits(kvm, KVM_X86_DISABLE_EXITS_PAUSE);
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 1b27d20f3022..7f28445766b6 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -899,6 +899,7 @@ static inline struct page *snp_safe_alloc_page(void)

int sev_vcpu_create(struct kvm_vcpu *vcpu);
void sev_free_vcpu(struct kvm_vcpu *vcpu);
+void sev_vm_init(struct kvm *kvm);
void sev_vm_destroy(struct kvm *kvm);
void __init sev_set_cpu_caps(void);
void __init sev_hardware_setup(void);
@@ -925,6 +926,7 @@ static inline struct page *snp_safe_alloc_page(void)

static inline int sev_vcpu_create(struct kvm_vcpu *vcpu) { return 0; }
static inline void sev_free_vcpu(struct kvm_vcpu *vcpu) {}
+static inline void sev_vm_init(struct kvm *kvm) {}
static inline void sev_vm_destroy(struct kvm *kvm) {}
static inline void __init sev_set_cpu_caps(void) {}
static inline void __init sev_hardware_setup(void) {}
--
2.53.0.473.g4a7958ca14-goog