Re: [PATCH v11 08/13] KVM: X86: Introduce KVM_HC_PAGE_ENC_STATUS hypercall

From: Sean Christopherson
Date: Tue Apr 06 2021 - 11:20:35 EST


On Tue, Apr 06, 2021, Sean Christopherson wrote:
> On Tue, Apr 06, 2021, Ashish Kalra wrote:
> > On Mon, Apr 05, 2021 at 01:42:42PM -0700, Steve Rutherford wrote:
> > > On Mon, Apr 5, 2021 at 7:28 AM Ashish Kalra <Ashish.Kalra@xxxxxxx> wrote:
> > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > > > index f7d12fca397b..ef5c77d59651 100644
> > > > --- a/arch/x86/kvm/x86.c
> > > > +++ b/arch/x86/kvm/x86.c
> > > > @@ -8273,6 +8273,18 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> > > > kvm_sched_yield(vcpu->kvm, a0);
> > > > ret = 0;
> > > > break;
> > > > + case KVM_HC_PAGE_ENC_STATUS: {
> > > > + int r;
> > > > +
> > > > + ret = -KVM_ENOSYS;
> > > > + if (kvm_x86_ops.page_enc_status_hc) {
> > > > + r = kvm_x86_ops.page_enc_status_hc(vcpu, a0, a1, a2);
> > > > + if (r >= 0)
> > > > + return r;
> > > > + ret = r;
> > > Style nit: Why not just set ret, and return ret if ret >=0?
> > >
> >
> > But ret is "unsigned long", if i set ret and return, then i will return to guest
> > even in case of error above ?
>
> As proposed, svm_page_enc_status_hc() already hooks complete_userspace_io(), so
> this could be hoisted out of the switch statement.
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 16fb39503296..794dde3adfab 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -8261,6 +8261,10 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
> goto out;
> }
>
> + /* comment goes here */
> + if (nr == KVM_HC_PAGE_ENC_STATUS && kvm_x86_ops.page_enc_status_hc)
> + return static_call(kvm_x86_page_enc_status_hc(vcpu, a0, a1, a2));

Gah, the SEV implementation can also return -EINVAL, and that should fail the
hypercall, not kill the guest. Normally we try to avoid output params, but
in this case it might be less ugly to do:

case KVM_HC_PAGE_ENC_STATUS: {
if (!kvm_x86_ops.page_enc_status_hc)
break;

if (!static_call(kvm_x86_page_enc_status_hc(vcpu, a0, a1,
a2, &ret));
return 0;
break;

> +
> ret = -KVM_ENOSYS;
>
> switch (nr) {
>