On 2/3/21 6:38 PM, Ashish Kalra wrote:
From: Brijesh Singh <brijesh.si...@amd.com>

This hypercall is used by the SEV guest to notify a change in the page
encryption status to the hypervisor. The hypercall should be invoked
only when the encryption attribute is changed from encrypted -> decrypted
and vice versa. By default all guest pages are considered encrypted.

The patch introduces a new shared pages list implemented as a
sorted linked list to track the shared/unencrypted regions marked by the
guest hypercall.


...

+
+       if (enc) {
+               ret = remove_shared_region(gfn_start, gfn_end,
+                                          &sev->shared_pages_list);
+               if (ret != -ENOMEM)
+                       sev->shared_pages_list_count += ret;
+       } else {
+               ret = add_shared_region(gfn_start, gfn_end,
+                                       &sev->shared_pages_list);
+               if (ret > 0)
+                       sev->shared_pages_list_count++;
+       }

I would move the shared_pages_list_count updates into the add/remove functions and then just return 0 or a -EXXXX error code from those functions. It seems simpler than "adding" ret or checking for a greater than 0 return code.

Thanks,
Tom

+
+       mutex_unlock(&kvm->lock);
+       return ret;
+}
+
  int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
  {
        struct kvm_sev_cmd sev_cmd;
@@ -1693,6 +1842,7 @@ void sev_vm_destroy(struct kvm *kvm)
sev_unbind_asid(kvm, sev->handle);
        sev_asid_free(sev->asid);
+       sev->shared_pages_list_count = 0;
  }
void __init sev_hardware_setup(void)
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index f923e14e87df..bb249ec625fc 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -4536,6 +4536,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
        .complete_emulated_msr = svm_complete_emulated_msr,
.vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
+
+       .page_enc_status_hc = svm_page_enc_status_hc,
  };
static struct kvm_x86_init_ops svm_init_ops __initdata = {
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index 0fe874ae5498..6437c1fa1f24 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -79,6 +79,9 @@ struct kvm_sev_info {
        unsigned long pages_locked; /* Number of pages locked */
        struct list_head regions_list;  /* List of registered regions */
        u64 ap_jump_table;      /* SEV-ES AP Jump Table address */
+       /* List and count of shared pages */
+       int shared_pages_list_count;
+       struct list_head shared_pages_list;
  };
struct kvm_svm {
@@ -472,6 +475,8 @@ int nested_svm_check_exception(struct vcpu_svm *svm, 
unsigned nr,
                               bool has_error_code, u32 error_code);
  int nested_svm_exit_special(struct vcpu_svm *svm);
  void sync_nested_vmcb_control(struct vcpu_svm *svm);
+int svm_page_enc_status_hc(struct kvm *kvm, unsigned long gpa,
+                          unsigned long npages, unsigned long enc);
extern struct kvm_x86_nested_ops svm_nested_ops; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index cc60b1fc3ee7..bcbf53851612 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -7705,6 +7705,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = {
        .can_emulate_instruction = vmx_can_emulate_instruction,
        .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
        .migrate_timers = vmx_migrate_timers,
+       .page_enc_status_hc = NULL,
.msr_filter_changed = vmx_msr_filter_changed,
        .complete_emulated_msr = kvm_complete_insn_gp,
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 76bce832cade..2f17f0f9ace7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -8162,6 +8162,12 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
                kvm_sched_yield(vcpu->kvm, a0);
                ret = 0;
                break;
+       case KVM_HC_PAGE_ENC_STATUS:
+               ret = -KVM_ENOSYS;
+               if (kvm_x86_ops.page_enc_status_hc)
+                       ret = kvm_x86_ops.page_enc_status_hc(vcpu->kvm,
+                                       a0, a1, a2);
+               break;
        default:
                ret = -KVM_ENOSYS;
                break;
diff --git a/include/uapi/linux/kvm_para.h b/include/uapi/linux/kvm_para.h
index 8b86609849b9..847b83b75dc8 100644
--- a/include/uapi/linux/kvm_para.h
+++ b/include/uapi/linux/kvm_para.h
@@ -29,6 +29,7 @@
  #define KVM_HC_CLOCK_PAIRING          9
  #define KVM_HC_SEND_IPI               10
  #define KVM_HC_SCHED_YIELD            11
+#define KVM_HC_PAGE_ENC_STATUS         12
/*
   * hypercalls use architecture specific

Reply via email to