Re: [PATCH v4 5/6] KVM: x86: Always complete hypercall via function callback
From: Sean Christopherson
Date: Mon Dec 02 2024 - 13:44:43 EST
On Thu, Nov 28, 2024, Xiaoyao Li wrote:
> On 11/28/2024 8:43 AM, Sean Christopherson wrote:
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 11434752b467..39be2a891ab4 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -9982,10 +9982,11 @@ static int complete_hypercall_exit(struct kvm_vcpu *vcpu)
> > return kvm_skip_emulated_instruction(vcpu);
> > }
> > -unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> > - unsigned long a0, unsigned long a1,
> > - unsigned long a2, unsigned long a3,
> > - int op_64_bit, int cpl)
> > +int __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> > + unsigned long a0, unsigned long a1,
> > + unsigned long a2, unsigned long a3,
> > + int op_64_bit, int cpl,
> > + int (*complete_hypercall)(struct kvm_vcpu *))
> > {
> > unsigned long ret;
> > @@ -10061,7 +10062,7 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> > vcpu->run->hypercall.flags |= KVM_EXIT_HYPERCALL_LONG_MODE;
> > WARN_ON_ONCE(vcpu->run->hypercall.flags & KVM_EXIT_HYPERCALL_MBZ);
> > - vcpu->arch.complete_userspace_io = complete_hypercall_exit;
> > + vcpu->arch.complete_userspace_io = complete_hypercall;
> > /* stat is incremented on completion. */
> > return 0;
> > }
> > @@ -10071,13 +10072,15 @@ unsigned long __kvm_emulate_hypercall(struct kvm_vcpu *vcpu, unsigned long nr,
> > }
> > out:
> > - return ret;
> > + vcpu->run->hypercall.ret = ret;
> > + complete_hypercall(vcpu);
> > + return 1;
>
> shouldn't it be
>
> return complete_hypercall(vcpu);
>
> ?
Ouch. Yes, it most definitely should be.
> Originally, kvm_emulate_hypercall() returns kvm_skip_emulated_instruction().
> Now it becomes
>
> kvm_skip_emulated_instruction();
> return 1;
>
> I don't go deep to see if kvm_skip_emulated_instruction() always return 1
> for this case.
It doesn't. KVM needs to exit to userspace if userspace is single-stepping, or
in the extremely unlikely scenario that KVM can't skip the emulated instruction
(which can very theoretically happen on older AMD CPUs).