kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Chao Gao <chao.gao@intel.com>
Cc: Qi Ai <aiqi.i7@bytedance.com>,
	pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com,
	kvm@vger.kernel.org, fengzhimin@bytedance.com,
	cenjiahui@bytedance.com, fangying.tommy@bytedance.com,
	dengqiao.joey@bytedance.com
Subject: Re: [PATCH] kvm/x86: clear hlt for intel cpu when resetting vcpu
Date: Fri, 30 Jun 2023 15:56:14 -0700	[thread overview]
Message-ID: <ZJ9djqQZWSEjJlfb@google.com> (raw)
In-Reply-To: <ZJ6rBwy9p5bbdWrs@chao-email>

mn Fri, Jun 30, 2023, Chao Gao wrote:
> On Fri, Jun 30, 2023 at 03:26:12PM +0800, Qi Ai wrote:
> >+				!is_protmode(vcpu))
> >+			kvm_x86_ops.clear_hlt(vcpu);
> 
> Use static_call_cond(kvm_x86_clear_hlt)(vcpu) instead.
> 
> It looks incorrect that we add this side-effect heuristically here. I

Yeah, adding heuristics to KVM_SET_REGS isn't happening.  KVM's existing hack
for "Older userspace" in __set_sregs_common() is bad enough:

	/* Older userspace won't unhalt the vcpu on reset. */
	if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
	    sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
	    !is_protmode(vcpu))
		vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;

> am wondering if we can link vcpu->arch.mp_state to VMCS activity state,

Hrm, maybe.

> i.e., when mp_state is set to RUNNABLE in KVM_SET_MP_STATE ioctl, KVM
> sets VMCS activity state to active.

Not in the ioctl(), there needs to be a proper set of APIs, e.g. so that the
existing hack works, and so that KVM actually reports out to userspace that a
vCPU is HALTED if userspace gained control of the vCPU, e.g. after an IRQ exit,
while the vCPU was HALTED.  I.e. mp_state versus vmcs.ACTIVITY_STATE needs to be
bidirectional, not one-way.  E.g. if a vCPU is live migrated, I'm pretty sure
vmcs.ACTIVITY_STATE is lost, which is wrong.

The downside is that if KVM propagates vmcs.ACTIVITY_STATE to mp_state for the
halted case, then KVM will enter kvm_vcpu_halt() instead of entering the guest
in halted state, which is undesirable.   Hmm, that can be handled by treating
the vCPU as running, e.g. 

static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
{
	return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE ||
		(vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
		 kvm_hlt_in_guest(vcpu->kvm))) &&
	       !vcpu->arch.apf.halted);
}

but that would have cascading effect to a whole pile of things.  I don't *think*
they'd be used with kvm_hlt_in_guest(), but we've had weirder stuff.

I'm half tempted to solve this particular issue by stuffing vmcs.ACTIVITY_STATE on
shutdown, similar to what SVM does on shutdown interception.  KVM doesn't come
anywhere near faithfully emulating shutdown, so it's unlikely to break anything.
And then the mp_state vs. hlt_in_guest coulbe tackled separately.  Ugh, but that
wouldn't cover a synthesized KVM_REQ_TRIPLE_FAULT.

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 44fb619803b8..ee4bb37067d1 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -5312,6 +5312,8 @@ static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
 
 static int handle_triple_fault(struct kvm_vcpu *vcpu)
 {
+       vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
+
        vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
        vcpu->mmio_needed = 0;
        return 0;


I don't suppose QEMU can to blast INIT at all vCPUs for this case?

  parent reply	other threads:[~2023-06-30 22:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-30  7:26 [PATCH] kvm/x86: clear hlt for intel cpu when resetting vcpu Qi Ai
2023-06-30 10:14 ` Chao Gao
2023-06-30 22:23   ` Isaku Yamahata
2023-06-30 22:56   ` Sean Christopherson [this message]
2023-07-04 11:34     ` Qi Ai
2023-07-05  8:40       ` Chao Gao
2023-07-05  9:04     ` Chao Gao
2023-07-31 19:15       ` Sean Christopherson

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=ZJ9djqQZWSEjJlfb@google.com \
    --to=seanjc@google.com \
    --cc=aiqi.i7@bytedance.com \
    --cc=bp@alien8.de \
    --cc=cenjiahui@bytedance.com \
    --cc=chao.gao@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=dengqiao.joey@bytedance.com \
    --cc=fangying.tommy@bytedance.com \
    --cc=fengzhimin@bytedance.com \
    --cc=hpa@zytor.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    /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;
as well as URLs for NNTP newsgroup(s).