Re: [PATCH v3 06/12] KVM: x86: Forward VMware hypercalls to userspace

From: Sean Christopherson

Date: Mon Nov 16 2026 - 17:27:42 EST


On Fri, Nov 13, 2026, David Woodhouse wrote:
> On Fri, 2026-11-13 at 01:33 -0500, Zack Rusin wrote:
> > > Userspace *already* needed to
> > > know about those¹ because it can't migrate while those are pending;
> > > it has to do at least one more run, even if it's with the immediate_exit
> > > flag set.
> >
> > True, but, again afaict, that rule exists because those completions
> > have to run, so I don't think it tells us they can be dropped.
> >
> > > If it's deemed clearer, I'd also be OK with an explicit
> > > KVM_VCPUEVENT_CANCEL_HCALL which cancels *only* hypercalls. But I don't
> > > really see the point; userspace shouldn't be getting confused when it
> > > was actually some other kind of completion that was active anyway.
> > >
> > > ¹ https://lore.kernel.org/all/20250111012450.1262638-1-seanjc@xxxxxxxxxx/
> > >
> > > Looks something like this in my Hyper-V hcall series (except I already
> > > shouted at the agent for stupid struct layout and made it go run pahole
> > > on everything it's working on).
> >
> > FWIW, restricting cancellation to explicitly marked completions looks
> > good to me.  If Sean/Paolo prefer that, I'm happy to mark our
> > completion cancellable and drop our exception fields.
>
> I posted that as
> https://lore.kernel.org/kvm/20261113170535.79246-3-dwmw2@xxxxxxxxxxxxx/T/#u
> and caught both Paolo and Sean here at KVM Forum and both seem OK with it
> (although acquiescence when badgered at a conference doesn't necessarily
> translate to applying the patch... :)

Heh, you jinxed yourself.

Adding something to KVM_SET_VCPU_EVENTS is complete overkill, and will create a
confusing ABI for hypercalls versus MSR exits. For the latter, KVM lets userspace
signal success/failure, and injects a #GP on failure.

The VMware case in particular is a bit different since that case needs to inject
a variety of faults, with a payload, but the concept is the same: give userspace
a way to cancel/reject the hypercall/action. And that's dead simple, e.g.

static int complete_vmware_hypercall_exit(struct kvm_vcpu *vcpu)
{
if (vcpu->run->vmware.ret)
return 1;

return kvm_skip_emulated_instruction(vcpu);
}

For Hyper-V exits, from what I can tell, the plumbing already exists, i.e. I
suspect that this:

A VMM implementing Hyper-V hypercalls behind KVM_EXIT_HYPERV_HCALL can only
complete them: write a status, advance RIP. The TLFS specifies some hypercalls
to raise an exception at the hypercall instruction instead, and the exit has no
way to say that.

is partly wrong. Presumably the TLFS provides a HV_STATUS_xxx flag or three to
indicate "exception", KVM just needs to honor those, e.g. if the behavior only
applies to userspace exits *and* userspace is responsible for injecting the fault:

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 5d2e3617c952..7933389731e6 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -2430,7 +2430,12 @@ static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)

static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
{
- return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
+ u64 result = READ_ONCE(vcpu->run->hyperv.u.hcall.result);
+
+ if (hv_result_exception(result))
+ return 1;
+
+ return kvm_hv_hypercall_complete(vcpu, result);
}

or if the type of fault is hardcoded and we want to apply the behavior to both
userspace exits and KVM-internal exits:

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 5d2e3617c952..c726a707a7ca 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -2420,6 +2420,9 @@ static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
kvm_hv_hypercall_set_result(vcpu, result);
++vcpu->stat.hypercalls;

+ if (hv_result_exception(result))
+ return 1;
+
ret = kvm_skip_emulated_instruction(vcpu);

if (tlb_lock_count)

If it's a mix of those two, we can obviously sort that out.

If the exception is communicated through some other means, then (a) that needs
to be explained in detail, and (b) we should be able to use the same appraoch,
just with new "ret" field or whatever in kvm_hyperv_exit.u.hcall.