kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Robert Hoo <robert.hoo.linux@gmail.com>
Cc: Robert Hoo <robert.hu@intel.com>,
	pbonzini@redhat.com, kvm@vger.kernel.org
Subject: Re: [PATCH 1/3] KVM: VMX: Rename vmx_umip_emulated() to cpu_has_vmx_desc()
Date: Wed, 15 Mar 2023 10:50:56 -0700	[thread overview]
Message-ID: <ZBIFgH4YBC71n6KR@google.com> (raw)
In-Reply-To: <CA+wubQBWgz4YAi=T3MV82HrC3=gXSC_yD50ip0=N_x3MnTE1UA@mail.gmail.com>

Please fix your editor or whatever it is that is resulting your emails being
wrapped at very bizarre boundaries.

On Sat, Mar 11, 2023, Robert Hoo wrote:
> Also, vmx_umip_emulated() == true doesn't necessarily mean, as its name
> indicates, UMIP-being-emulated, e.g. Host has UMIP capability, then UMIP
> isn't emulated though vmx_umip_emulated() indicates true.

True.  I was going to say "there's no perfect solution" since KVM needs to check
both "is UMIP emulated" and "can UMIP be emulated", but that's not actually the
case.  vmx_emulate_umip() _should_ check for native support, as there's no
legitimate use for checking if UMIP _can_ be emulated.

Functionally, this should be a glorified nop, but I agree it's worth changing.
I'll formally post this after testing.

From: Sean Christopherson <seanjc@google.com>
Date: Wed, 15 Mar 2023 10:27:40 -0700
Subject: [PATCH] KVM: VMX: Treat UMIP as emulated if and only if the host
 doesn't have UMIP

Advertise UMIP as emulated if and only if the host doesn't natively
support UMIP, otherwise vmx_umip_emulated() is misleading when the host
_does_ support UMIP.  Of the four users of vmx_umip_emulated(), two
already check for native support, and the logic in vmx_set_cpu_caps() is
relevant if and only if UMIP isn't natively supported as UMIP is set in
KVM's caps by kvm_set_cpu_caps() when UMIP is present in hardware.

That leaves KVM's stuffing of X86_CR4_UMIP into the default cr4_fixed1
value enumerated for nested VMX.  In that case, checking for (lack of)
host support is actually a bug fix of sorts, as enumerating UMIP support
based solely on descriptor table existing works only because KVM doesn't
sanity check MSR_IA32_VMX_CR4_FIXED1.  E.g. if a (very theoretical) host
supported UMIP in hardware but didn't allow UMIP+VMX, KVM would advertise
UMIP but not actually emulate UMIP.  Of course, KVM would explode long
before it could run a nested VM on said theoretical CPU, as KVM doesn't
modify host CR4 when enabling VMX.

Reported-by: Robert Hoo <robert.hu@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/vmx/capabilities.h | 4 ++--
 arch/x86/kvm/vmx/nested.c       | 3 +--
 arch/x86/kvm/vmx/vmx.c          | 2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kvm/vmx/capabilities.h b/arch/x86/kvm/vmx/capabilities.h
index 45162c1bcd8f..d0abee35d7ba 100644
--- a/arch/x86/kvm/vmx/capabilities.h
+++ b/arch/x86/kvm/vmx/capabilities.h
@@ -152,8 +152,8 @@ static inline bool cpu_has_vmx_ept(void)
 
 static inline bool vmx_umip_emulated(void)
 {
-	return vmcs_config.cpu_based_2nd_exec_ctrl &
-		SECONDARY_EXEC_DESC;
+	return !boot_cpu_has(X86_FEATURE_UMIP) &&
+	       (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_DESC);
 }
 
 static inline bool cpu_has_vmx_rdtscp(void)
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 7c4f5ca405c7..e8347bf2e4fa 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2322,8 +2322,7 @@ static void prepare_vmcs02_early(struct vcpu_vmx *vmx, struct loaded_vmcs *vmcs0
 		 * Preset *DT exiting when emulating UMIP, so that vmx_set_cr4()
 		 * will not have to rewrite the controls just for this bit.
 		 */
-		if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated() &&
-		    (vmcs12->guest_cr4 & X86_CR4_UMIP))
+		if (vmx_umip_emulated() && (vmcs12->guest_cr4 & X86_CR4_UMIP))
 			exec_control |= SECONDARY_EXEC_DESC;
 
 		if (exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 8626010f5d54..c7bd8931eda6 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3467,7 +3467,7 @@ void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 	else
 		hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
 
-	if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
+	if (vmx_umip_emulated()) {
 		if (cr4 & X86_CR4_UMIP) {
 			secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
 			hw_cr4 &= ~X86_CR4_UMIP;

base-commit: 0da2a674e56a7fb429eb1f96a3da04d56ec167fd
-- 


  reply	other threads:[~2023-03-15 17:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10 12:57 [PATCH 0/3] Some code refactor surround CR4.UMIP virtualization Robert Hoo
2023-03-10 12:57 ` [PATCH 1/3] KVM: VMX: Rename vmx_umip_emulated() to cpu_has_vmx_desc() Robert Hoo
2023-03-10 15:59   ` Sean Christopherson
2023-03-11  1:59     ` Robert Hoo
2023-03-15 17:50       ` Sean Christopherson [this message]
2023-03-31  9:48         ` Robert Hoo
2023-04-10 18:12           ` Sean Christopherson
2023-03-10 12:57 ` [PATCH 2/3] KVM: VMX: Remove a unnecessary cpu_has_vmx_desc() check in vmx_set_cr4() Robert Hoo
2023-03-10 16:12   ` Sean Christopherson
2023-03-11  2:36     ` Robert Hoo
2023-03-15 16:35       ` Sean Christopherson
2023-03-31  9:48         ` Robert Hoo
2023-04-10 18:35           ` Sean Christopherson
2023-04-11  5:04             ` Hoo Robert
2023-03-10 12:57 ` [PATCH 3/3] KVM: VMX: Use the canonical interface to read CR4.UMIP bit Robert Hoo
2023-03-10 16:27   ` Sean Christopherson
     [not found]     ` <CA+wubQBsiaH_==UJ-JUi7hwS8W1i5MLZ-dPuw2smVH8Z0sqXsw@mail.gmail.com>
2023-03-28  4:38       ` Sean Christopherson
2023-03-31  9:48     ` Robert Hoo
2023-04-10 18:35       ` 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=ZBIFgH4YBC71n6KR@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=robert.hoo.linux@gmail.com \
    --cc=robert.hu@intel.com \
    /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).