Re: [PATCH v3 03/18] x86/reboot: KVM: Handle VMXOFF in KVM's reboot callback

Huang, Kai
2023-05-22T23:11:50Z

On Mon, 2023-05-22 at 10:58 -0700, Sean Christopherson wrote:
> On Mon, May 22, 2023, Kai Huang wrote:
> > On Fri, 2023-05-12 at 16:50 -0700, Sean Christopherson wrote:
> > > Use KVM VMX's reboot/crash callback to do VMXOFF in an emergency instead
> > > of manually and blindly doing VMXOFF. There's no need to attempt VMXOFF
> > > if a hypervisor, i.e. KVM, isn't loaded/active, i.e. if the CPU can't
> > > possibly be post-VMXON.
> > >
> > > Signed-off-by: Sean Christopherson <seanjc@google.com>
> > > ---
> > > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> > > index fc9cdb4114cc..76cdb189f1b5 100644
> > > --- a/arch/x86/kvm/vmx/vmx.c
> > > +++ b/arch/x86/kvm/vmx/vmx.c
> > > @@ -744,7 +744,7 @@ static int vmx_set_guest_uret_msr(struct vcpu_vmx *vmx,
> > > return ret;
> > > }
> > >
> > > -static void crash_vmclear_local_loaded_vmcss(void)
> > > +static void vmx_emergency_disable(void)
> > > {
> > > int cpu = raw_smp_processor_id();
> > > struct loaded_vmcs *v;
> > > @@ -752,6 +752,8 @@ static void crash_vmclear_local_loaded_vmcss(void)
> > > list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
> > > loaded_vmcss_on_cpu_link)
> > > vmcs_clear(v->vmcs);
> > > +
> > > + __cpu_emergency_vmxoff();
> >
> > __cpu_emergency_vmxoff() internally checks whether VMX is enabled in CR4.
> > Logically, looks it's more reasonable to do such check before VMCLEAR active
> > VMCSes, although in practice there should be no problem I think.
> >
> > But this problem inherits from the existing code in upstream, so not sure
> > whether it is worth fixing.
>
> Hmm, I think it's worth fixing, if only to avoid confusing future readers. Blindly
> doing VMCLEAR but then conditionally executing VMXOFF is nonsensical. I'll tack on
> a patch, and also add a comment to call out that CR4.VMXE can be _cleared_
> asynchronously by NMI, but can't be set after being checked. I.e. explain that
> checking CR4.VMXE is a "best effort" sort of thing.

Yeah looks good.