Re: [PATCH v10 14/16] KVM: x86: Add guest support for detecting and enabling SEV Live Migration feature.

2021-02-18 Thread Sean Christopherson
On Thu, Feb 04, 2021, Ashish Kalra wrote:
> From: Ashish Kalra 

...

>  arch/x86/include/asm/mem_encrypt.h |  8 +
>  arch/x86/kernel/kvm.c  | 52 ++
>  arch/x86/mm/mem_encrypt.c  | 41 +++
>  3 files changed, 101 insertions(+)

Please use "x86/kvm:" in the shortlog for guest side changes so that reviewers
can easily differentiate between host and guest patches.  Past changes haven't
exactly been consistent for guest changes, but "x86/kvm:" is the most popular
at a glance, and IMO aligns best with other kernel (non-KVM) changes.


[PATCH v10 14/16] KVM: x86: Add guest support for detecting and enabling SEV Live Migration feature.

2021-02-03 Thread Ashish Kalra
From: Ashish Kalra 

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

 ii) if CPUID reports that migration is supported, issue a wrmsrl()
 to enable the SEV live migration support

   - If EFI,

 i) check for the KVM_FEATURE_CPUID

 ii) If CPUID reports that migration is supported, read the UEFI variable 
which
 indicates OVMF support for live migration

 iii) the variable indicates live migration is supported, issue a wrmsrl() 
to
  enable the SEV live migration support

The EFI live migration check is done using a late_initcall() callback.

Also, ensure that _bss_decrypted section is marked as decrypted in the
shared pages list.

Signed-off-by: Ashish Kalra 
---
 arch/x86/include/asm/mem_encrypt.h |  8 +
 arch/x86/kernel/kvm.c  | 52 ++
 arch/x86/mm/mem_encrypt.c  | 41 +++
 3 files changed, 101 insertions(+)

diff --git a/arch/x86/include/asm/mem_encrypt.h 
b/arch/x86/include/asm/mem_encrypt.h
index 31c4df123aa0..19b77f3a62dc 100644
--- a/arch/x86/include/asm/mem_encrypt.h
+++ b/arch/x86/include/asm/mem_encrypt.h
@@ -21,6 +21,7 @@
 extern u64 sme_me_mask;
 extern u64 sev_status;
 extern bool sev_enabled;
+extern bool sev_live_migration_enabled;
 
 void sme_encrypt_execute(unsigned long encrypted_kernel_vaddr,
 unsigned long decrypted_kernel_vaddr,
@@ -44,8 +45,11 @@ void __init sme_enable(struct boot_params *bp);
 
 int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size);
 int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size);
+void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages,
+   bool enc);
 
 void __init mem_encrypt_free_decrypted_mem(void);
+void __init check_kvm_sev_migration(void);
 
 /* Architecture __weak replacement functions */
 void __init mem_encrypt_init(void);
@@ -60,6 +64,7 @@ bool sev_es_active(void);
 #else  /* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define sme_me_mask0ULL
+#define sev_live_migration_enabled false
 
 static inline void __init sme_early_encrypt(resource_size_t paddr,
unsigned long size) { }
@@ -84,8 +89,11 @@ static inline int __init
 early_set_memory_decrypted(unsigned long vaddr, unsigned long size) { return 
0; }
 static inline int __init
 early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 
0; }
+static inline void __init
+early_set_mem_enc_dec_hypercall(unsigned long vaddr, int npages, bool enc) {}
 
 static inline void mem_encrypt_free_decrypted_mem(void) { }
+static inline void check_kvm_sev_migration(void) { }
 
 #define __bss_decrypted
 
diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c
index 5e78e01ca3b4..c4b8029c1442 100644
--- a/arch/x86/kernel/kvm.c
+++ b/arch/x86/kernel/kvm.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -429,6 +430,56 @@ 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)
+{
+   efi_char16_t efi_sev_live_migration_enabled[] = 
L"SevLiveMigrationEnabled";
+   efi_guid_t efi_variable_guid = MEM_ENCRYPT_GUID;
+   efi_status_t status;
+   unsigned long size;
+   bool enabled;
+
+   /*
+* check_kvm_sev_migration() invoked via kvm_init_platform() before
+* this callback would have setup the indicator that live migration
+* feature is supported/enabled.
+*/
+   if (!sev_live_migration_enabled)
+   return 0;
+
+   if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
+   pr_info("%s : EFI runtime services are not enabled\n", 
__func__);
+   return 0;
+   }
+
+   size = sizeof(enabled);
+
+   /* Get variable contents into buffer */
+   status = efi.get_variable(efi_sev_live_migration_enabled,
+ _variable_guid, NULL, , );
+
+   if (status == EFI_NOT_FOUND) {
+   pr_info("%s : EFI live migration variable not found\n", 
__func__);
+   return 0;
+   }
+
+   if (status != EFI_SUCCESS) {
+   pr_info("%s : EFI variable retrieval failed\n", __func__);
+   return 0;
+   }
+
+   if (enabled == 0) {
+   pr_info("%s: live migration disabled in EFI\n", __func__);
+   return 0;
+   }
+
+   pr_info("%s : live migration enabled in EFI\n", __func__);
+   wrmsrl(MSR_KVM_SEV_LIVE_MIGRATION, KVM_SEV_LIVE_MIGRATION_ENABLED);
+
+   return true;
+}
+
+late_initcall(setup_kvm_sev_migration);
+
 /*
  * Iterate through