]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: SEV: Move SEV-specific VM initialization to sev.c
authorSean Christopherson <seanjc@google.com>
Tue, 10 Mar 2026 23:48:19 +0000 (16:48 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 8 Apr 2026 23:04:27 +0000 (16:04 -0700)
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).

Link: https://patch.msgid.link/20260310234829.2608037-12-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/sev.c
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index 56ace27f739c6cf52028053534de991e84e57f78..4df0f17da3e220b02b220607ca47c9f98fad8ccb 100644 (file)
@@ -2925,6 +2925,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);
index 69a3efc14368247a4ff717c71e1eb3bc69bc48e2..261e563b9bab4c5f0618d9772bb0167e7b7e625a 100644 (file)
@@ -5123,17 +5123,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);
index 4c841e330aaf34c158e46721107cbb4f9fcc735d..089726eb06b21196ab097d4800e9038b2e74cd59 100644 (file)
@@ -900,6 +900,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);
@@ -926,6 +927,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) {}