Re: [PATCH v13 12/12] x86/kvm: Add guest support for detecting and enabling SEV Live Migration feature.

From: Paolo Bonzini
Date: Wed Apr 21 2021 - 11:39:02 EST


On 21/04/21 16:44, Borislav Petkov wrote:
On Thu, Apr 15, 2021 at 04:01:16PM +0000, Ashish Kalra wrote:
From: Ashish Kalra <ashish.kalra@xxxxxxx>

The guest support for detecting and enabling SEV Live migration
feature uses the following logic :

- kvm_init_plaform() invokes check_kvm_sev_migration() which
checks if its booted under the EFI

- If not EFI,

i) check for the KVM_FEATURE_CPUID

Where do you do that?

$ git grep KVM_FEATURE_CPUID
$

Do you mean

kvm_para_has_feature(KVM_FEATURE_SEV_LIVE_MIGRATION)

per chance?

Yep. Or KVM_CPUID_FEATURES perhaps.


diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 78bb0fae3982..94ef16d263a7 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -26,6 +26,7 @@
#include <linux/kprobes.h>
#include <linux/nmi.h>
#include <linux/swait.h>
+#include <linux/efi.h>
#include <asm/timer.h>
#include <asm/cpu.h>
#include <asm/traps.h>
@@ -429,6 +430,59 @@ static inline void __set_percpu_decrypted(void *ptr, unsigned long size)
early_set_memory_decrypted((unsigned long) ptr, size);
}
+static int __init setup_kvm_sev_migration(void)

kvm_init_sev_migration() or so.

...

@@ -48,6 +50,8 @@ EXPORT_SYMBOL_GPL(sev_enable_key);
bool sev_enabled __section(".data");
+bool sev_live_migration_enabled __section(".data");

Pls add a function called something like:

bool sev_feature_enabled(enum sev_feature)

and gets SEV_FEATURE_LIVE_MIGRATION and then use it instead of adding
yet another boolean which contains whether some aspect of SEV has been
enabled or not.

Then add a

static enum sev_feature sev_features;

in mem_encrypt.c and that function above will query that sev_features
enum for set flags.

Even better: let's stop callings things SEV/SEV_ES. Long term we want anyway to use things like mem_encrypt_enabled (SEV), guest_instruction_trap_enabled (SEV/ES), etc.

For this one we don't need a bool at all, we can simply check whether the pvop points to paravirt_nop. Also keep everything but the BSS handling in arch/x86/kernel/kvm.c. Only the BSS handling should be in arch/x86/mm/mem_encrypt.c. This way all KVM paravirt hypercalls and MSRs are in kvm.c.

That is:

void kvm_init_platform(void)
{
if (sev_active() &&
kvm_para_has_feature(KVM_FEATURE_SEV_LIVE_MIGRATION)) {
pv_ops.mmu.notify_page_enc_status_changed =
kvm_sev_hc_page_enc_status;
/* this takes care of bss_decrypted */
early_set_page_enc_status();
if (!efi_enabled(EFI_BOOT))
wrmsrl(MSR_KVM_SEV_LIVE_MIGRATION,
KVM_SEV_LIVE_MIGRATION_ENABLED);
}
/* existing kvm_init_platform code goes here */
}

// the pvop is changed to take the pfn, so that the vaddr loop
// is not KVM specific
static inline void notify_page_enc_status_changed(unsigned long pfn,
int npages, bool enc)
{
PVOP_VCALL3(mmu.page_encryption_changed, pfn, npages, enc);
}

static void notify_addr_enc_status_changed(unsigned long addr,
int numpages, bool enc)
{
#ifdef CONFIG_PARAVIRT
if (pv_ops.mmu.notify_page_enc_status_changed == paravirt_nop)
return;

/* the body of set_memory_enc_dec_hypercall goes here */
for (; vaddr < vaddr_end; vaddr = vaddr_next) {
...
notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT,
enc);
vaddr_next = (vaddr & pmask) + psize;
}
#endif
}

static int __set_memory_enc_dec(unsigned long addr,
int numpages, bool enc)
{
...
cpa_flush(&cpa, 0);
notify_addr_enc_status_changed(addr, numpages, enc);
return ret;
}


+static int __init setup_kvm_sev_migration(void)

Please rename this to include efi in the function name.


+ */
+ if (!efi_enabled(EFI_BOOT))
+ wrmsrl(MSR_KVM_SEV_LIVE_MIGRATION,
+ KVM_SEV_LIVE_MIGRATION_ENABLED);
+ } else {
+ pr_info("KVM enable live migration feature unsupported\n");
+ }
+}

I think this pr_info is incorrect, because it can still be enabled in the late_initcall. Just remove it as in the sketch above.

Paolo