Re: [PATCH] KVM: X86: set EXITING_GUEST_MODE as soon as vCPU exits

From: Chao Gao
Date: Wed Nov 30 2022 - 01:30:01 EST


On Tue, Nov 29, 2022 at 01:22:25PM -0500, Jon Kohler wrote:
>@@ -7031,6 +7042,18 @@ void noinstr vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
> void noinstr vmx_spec_ctrl_restore_host(struct vcpu_vmx *vmx,
> unsigned int flags)
> {
>+ struct kvm_vcpu *vcpu = &vmx->vcpu;
>+
>+ /* Optimize IPI reduction by setting mode immediately after vmexit
>+ * without a memmory barrier as this as not paired anywhere. vcpu->mode
>+ * is will be set to OUTSIDE_GUEST_MODE in x86 common code with a memory
>+ * barrier, after the host is done fully restoring various host states.
>+ * Since the rdmsr and wrmsr below are expensive, this must be done
>+ * first, so that the IPI suppression window covers the time dealing
>+ * with fixing up SPEC_CTRL.
>+ */
>+ vcpu->mode = EXITING_GUEST_MODE;

Does this break kvm_vcpu_kick()? IIUC, kvm_vcpu_kick() does nothing if
vcpu->mode is already EXITING_GUEST_MODE, expecting the vCPU will exit
guest mode. But ...

>+
> u64 hostval = this_cpu_read(x86_spec_ctrl_current);
>
> if (!cpu_feature_enabled(X86_FEATURE_MSR_SPEC_CTRL))
>diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
>index 2835bd796639..0e0d228f3fa5 100644
>--- a/arch/x86/kvm/x86.c
>+++ b/arch/x86/kvm/x86.c
>@@ -2160,6 +2160,14 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu)
> data = kvm_read_edx_eax(vcpu);
> if (!handle_fastpath_set_tscdeadline(vcpu, data)) {
> kvm_skip_emulated_instruction(vcpu);
>+ /* Reset IN_GUEST_MODE since we're going to reenter
>+ * guest as part of this fast path. This is done as
>+ * an optimization without a memory barrier since
>+ * EXITING_GUEST_MODE is also set without a memory
>+ * barrier.
>+ */
>+ if (vcpu->mode == EXITING_GUEST_MODE)
>+ vcpu->mode = IN_GUEST_MODE;

... the vCPU enters guest mode again.

I believe mode transition from EXITING_GUEST_MODE to IN_GUEST_MODE
directly isn't valid for current KVM.