Linux-HyperV List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction
Date: Fri, 27 Nov 2026 15:55:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-237797@kernel.org> (raw)
In-Reply-To: <20261127152730.99319-3-dwmw2@infradead.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: x86: stale emulate_regs_need_sync_to_vcpu in
  kvm_insn_uncompleted()
--

commit a2d5858612fe849c4fd217f459db157f10703c3f
Author: David Woodhouse <dwmw@amazon.co.uk>

KVM: Define what completing an exit does with its instruction

This commit defines KVM_INSN_COMPLETE, KVM_INSN_GP, KVM_INSN_UD, and
KVM_INSN_RESUME to let userspace choose how KVM handles an uncompleted
instruction after an exit, along with the x86 kvm_insn_uncompleted()
helper.

> diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
> index 436e2fb1396c4..7e5ccbce9114f 100644
> --- a/arch/x86/kvm/x86.h
> +++ b/arch/x86/kvm/x86.h
> @@ -526,6 +526,38 @@ static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
>  	kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
>  }
>  
> +/*
> + * Act on the KVM_INSN_* answer which userspace gave to an exit that left an
> + * instruction to complete.  Returns 0 if the caller is to complete the
> + * instruction, 1 if there is nothing more to do for it, or -EINVAL.
> + *
> + * A guest whose state is protected does not execute the instruction again,
> + * and KVM cannot inject #UD into it: all that can be done there is to
> + * complete the exit, with an outcome which is the caller's to choose from
> + * the answer, or to ignore it.
> + */
> +static inline int kvm_insn_uncompleted(struct kvm_vcpu *vcpu, u8 answer)
> +{
> +	if (vcpu->kvm->arch.has_protected_state ||
> +	    vcpu->arch.guest_state_protected)
> +		return 0;
> +
> +	switch (answer) {
> +	case KVM_INSN_COMPLETE:
> +		return 0;
> +	case KVM_INSN_GP:
> +		kvm_inject_gp(vcpu, 0);
> +		return 1;
> +	case KVM_INSN_UD:
> +		kvm_queue_exception(vcpu, UD_VECTOR);
> +		return 1;
> +	case KVM_INSN_RESUME:
> +		return 1;
> +	default:
> +		return -EINVAL;
> +	}
> +}

[Severity: High]
Does kvm_insn_uncompleted() leave vcpu->arch.emulate_regs_need_sync_to_vcpu
set when returning 1 for an emulated instruction exit?

When x86_emulate_instruction() exits to userspace without writing back
registers, it sets vcpu->arch.emulate_regs_need_sync_to_vcpu = true, while
x86_decode_insn() has already marked register operands in ctxt->regs_dirty
via decode_register() -> reg_rmw().  If userspace responds with KVM_INSN_GP,
KVM_INSN_UD, or KVM_INSN_RESUME without calling KVM_GET_REGS or
KVM_SET_REGS, completion skips x86_emulate_instruction() and resumes the
guest with emulate_regs_need_sync_to_vcpu still true.  A later __get_regs()
call then flushes the stale ctxt->_regs[] values over vcpu->arch.regs:

arch/x86/kvm/regs.c:__get_regs() {
	if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
		emulator_writeback_register_cache(vcpu->arch.emulate_ctxt);
		vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
	}
    ...
}

Clearing vcpu->arch.emulate_regs_need_sync_to_vcpu when skipping instruction
completion avoids clobbering guest GPRs after the guest resumes.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127152730.99319-1-dwmw2@infradead.org?part=2

  reply	other threads:[~2026-11-27 15:55 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:27 [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
2026-11-27 15:27 ` [PATCH v3 01/15] KVM: selftests: Make the x86 SSE register helpers volatile David Woodhouse
2026-11-27 15:27 ` [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction David Woodhouse
2026-11-27 15:55   ` sashiko-bot [this message]
2026-11-27 15:27 ` [PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 05/15] KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL David Woodhouse
2026-11-27 15:27 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
2026-11-27 15:27 ` [PATCH v3 07/15] KVM: x86: hyper-v: Advertise XMM fast hypercall output David Woodhouse
2026-11-27 15:27 ` [PATCH v3 08/15] KVM: selftests: Add Hyper-V hypercall forwarding test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 09/15] KVM: x86: Add a per-vCPU register write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes David Woodhouse
2026-11-27 15:48   ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 11/15] KVM: VMX: Support CR0 and CR4 write filters David Woodhouse
2026-11-27 15:27 ` [PATCH v3 12/15] KVM: SVM: " David Woodhouse
2026-11-27 15:49   ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 13/15] KVM: x86: Add descriptor-table registers to the write filter David Woodhouse
2026-11-27 15:52   ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 14/15] KVM: selftests: Add x86 register write filter test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 15/15] KVM: selftests: Test the register write filter with a nested guest David Woodhouse
2026-11-27 15:47   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-237797@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dwmw2@infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox