[PATCH 3/4] KVM: SVM: Gate calls to SEV (un)setup helpers with IS_ENABLED(CONFIG_KVM_AMD_SEV)

From: Sean Christopherson
Date: Fri Feb 23 2024 - 10:57:57 EST


Invoke SEV helpers that aren't associated with a VM/vCPU, i.e. aren't
already guarded by sev_guest() or sev_es_guest(), if and only if
CONFIG_KVM_AMD_SEV=y. This will allow compiling sev.c only when SEV
support is enabled without needing to add stubs.

Use IS_ENABLED() to avoid #ifdefs, as it's easy to unconditionally
declare the helpers. Dead code elimination will take care of the rest.

Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
---
arch/x86/kvm/svm/svm.c | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index c657e75fd2c6..e036a0852161 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -707,9 +707,11 @@ static int svm_cpu_init(int cpu)
if (!sd->save_area)
return ret;

- ret = sev_cpu_init(sd);
- if (ret)
- goto free_save_area;
+ if (IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
+ ret = sev_cpu_init(sd);
+ if (ret)
+ goto free_save_area;
+ }

sd->save_area_pa = __sme_page_pa(sd->save_area);
return 0;
@@ -1110,7 +1112,8 @@ static void svm_hardware_unsetup(void)
{
int cpu;

- sev_hardware_unsetup();
+ if (IS_ENABLED(CONFIG_KVM_AMD_SEV))
+ sev_hardware_unsetup();

for_each_possible_cpu(cpu)
svm_cpu_uninit(cpu);
@@ -5149,7 +5152,8 @@ static __init void svm_set_cpu_caps(void)
}

/* CPUID 0x8000001F (SME/SEV features) */
- sev_set_cpu_caps();
+ if (IS_ENABLED(CONFIG_KVM_AMD_SEV))
+ sev_set_cpu_caps();
}

static __init int svm_hardware_setup(void)
@@ -5243,7 +5247,8 @@ static __init int svm_hardware_setup(void)
* Note, SEV setup consumes npt_enabled and enable_mmio_caching (which
* may be modified by svm_adjust_mmio_mask()), as well as nrips.
*/
- sev_hardware_setup();
+ if (IS_ENABLED(CONFIG_KVM_AMD_SEV))
+ sev_hardware_setup();

svm_hv_hardware_setup();

--
2.44.0.rc0.258.g7320e95886-goog


--ZUo03/L5Jsb9rYOS
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
filename="0004-KVM-SVM-Compile-sev.c-if-and-only-if-CONFIG_KVM_AMD_.patch"