All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>,
	Vitaly Kuznetsov <vkuznets@redhat.com>,
	Wanpeng Li <wanpengli@tencent.com>,
	Jim Mattson <jmattson@google.com>, Joerg Roedel <joro@8bytes.org>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Reiji Watanabe <reijiw@google.com>
Subject: [PATCH v2 41/46] KVM: VMX: Smush x2APIC MSR bitmap adjustments into single function
Date: Tue, 13 Jul 2021 09:33:19 -0700	[thread overview]
Message-ID: <20210713163324.627647-42-seanjc@google.com> (raw)
In-Reply-To: <20210713163324.627647-1-seanjc@google.com>

Consolidate all of the dynamic MSR bitmap adjustments into
vmx_update_msr_bitmap_x2apic(), and rename the mode tracker to reflect
that it is x2APIC specific.  If KVM gains more cases of dynamic MSR
pass-through, odds are very good that those new cases will be better off
with their own logic, e.g. see Intel PT MSRs and MSR_IA32_SPEC_CTRL.

Attempting to handle all updates in a common helper did more harm than
good, as KVM ended up collecting a large number of useless "updates".

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/vmx/vmx.c | 55 ++++++++++++++++--------------------------
 arch/x86/kvm/vmx/vmx.h |  2 +-
 2 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index bc09a2f7cb5f..cdde1dfaa574 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3812,21 +3812,6 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type)
 		vmx_set_msr_bitmap_write(msr_bitmap, msr);
 }
 
-static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
-{
-	u8 mode = 0;
-
-	if (cpu_has_secondary_exec_ctrls() &&
-	    (secondary_exec_controls_get(to_vmx(vcpu)) &
-	     SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
-		mode |= MSR_BITMAP_MODE_X2APIC;
-		if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
-			mode |= MSR_BITMAP_MODE_X2APIC_APICV;
-	}
-
-	return mode;
-}
-
 static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode)
 {
 	unsigned long *msr_bitmap = to_vmx(vcpu)->vmcs01.msr_bitmap;
@@ -3844,11 +3829,29 @@ static void vmx_reset_x2apic_msrs(struct kvm_vcpu *vcpu, u8 mode)
 	}
 }
 
-static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode)
+static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu)
 {
+	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	u8 mode;
+
 	if (!cpu_has_vmx_msr_bitmap())
 		return;
 
+	if (cpu_has_secondary_exec_ctrls() &&
+	    (secondary_exec_controls_get(vmx) &
+	     SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
+		mode = MSR_BITMAP_MODE_X2APIC;
+		if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
+			mode |= MSR_BITMAP_MODE_X2APIC_APICV;
+	} else {
+		mode = 0;
+	}
+
+	if (!(mode ^ vmx->x2apic_msr_bitmap_mode))
+		return;
+
+	vmx->x2apic_msr_bitmap_mode = mode;
+
 	vmx_reset_x2apic_msrs(vcpu, mode);
 
 	/*
@@ -3865,21 +3868,6 @@ static void vmx_update_msr_bitmap_x2apic(struct kvm_vcpu *vcpu, u8 mode)
 	}
 }
 
-static void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
-{
-	struct vcpu_vmx *vmx = to_vmx(vcpu);
-	u8 mode = vmx_msr_bitmap_mode(vcpu);
-	u8 changed = mode ^ vmx->msr_bitmap_mode;
-
-	if (!changed)
-		return;
-
-	if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
-		vmx_update_msr_bitmap_x2apic(vcpu, mode);
-
-	vmx->msr_bitmap_mode = mode;
-}
-
 void pt_update_intercept_for_msr(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
@@ -4139,8 +4127,7 @@ static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
 					SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
 	}
 
-	if (cpu_has_vmx_msr_bitmap())
-		vmx_update_msr_bitmap(vcpu);
+	vmx_update_msr_bitmap_x2apic(vcpu);
 }
 
 u32 vmx_exec_control(struct vcpu_vmx *vmx)
@@ -6186,7 +6173,7 @@ void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
 	}
 	secondary_exec_controls_set(vmx, sec_exec_control);
 
-	vmx_update_msr_bitmap(vcpu);
+	vmx_update_msr_bitmap_x2apic(vcpu);
 }
 
 static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 1b3dd5ddf235..e370091d57c6 100644
--- a/arch/x86/kvm/vmx/vmx.h
+++ b/arch/x86/kvm/vmx/vmx.h
@@ -229,7 +229,7 @@ struct nested_vmx {
 struct vcpu_vmx {
 	struct kvm_vcpu       vcpu;
 	u8                    fail;
-	u8		      msr_bitmap_mode;
+	u8		      x2apic_msr_bitmap_mode;
 
 	/*
 	 * If true, host state has been stored in vmx->loaded_vmcs for
-- 
2.32.0.93.g670b81a890-goog


  parent reply	other threads:[~2021-07-13 16:35 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-13 16:32 [PATCH v2 00/46] KVM: x86: vCPU RESET/INIT fixes and consolidation Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 01/46] KVM: x86: Flush the guest's TLB on INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 02/46] KVM: nVMX: Set LDTR to its architecturally defined value on nested VM-Exit Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 03/46] KVM: SVM: Zero out GDTR.base and IDTR.base on INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 04/46] KVM: VMX: Set EDX at INIT with CPUID.0x1, Family-Model-Stepping Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 05/46] KVM: SVM: Require exact CPUID.0x1 match when stuffing EDX at INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 06/46] KVM: SVM: Fall back to KVM's hardcoded value for EDX at RESET/INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 07/46] KVM: VMX: Remove explicit MMU reset in enter_rmode() Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 08/46] KVM: SVM: Drop explicit MMU reset at RESET/INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 09/46] KVM: SVM: Drop a redundant init_vmcb() from svm_create_vcpu() Sean Christopherson
2021-07-26 20:33   ` Paolo Bonzini
2021-07-26 22:26     ` Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 10/46] KVM: VMX: Move init_vmcs() invocation to vmx_vcpu_reset() Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 11/46] KVM: x86: WARN if the APIC map is dirty without an in-kernel local APIC Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 12/46] KVM: x86: Remove defunct BSP "update" in local APIC reset Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 13/46] KVM: x86: Migrate the PIT only if vcpu0 is migrated, not any BSP Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 14/46] KVM: x86: Don't force set BSP bit when local APIC is managed by userspace Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 15/46] KVM: x86: Set BSP bit in reset BSP vCPU's APIC base by default Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 16/46] KVM: VMX: Stuff vcpu->arch.apic_base directly at vCPU RESET Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 17/46] KVM: x86: Open code necessary bits of kvm_lapic_set_base() " Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 18/46] KVM: x86: Consolidate APIC base RESET initialization code Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 19/46] KVM: x86: Move EDX initialization at vCPU RESET to common code Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 20/46] KVM: SVM: Don't bother writing vmcb->save.rip at vCPU RESET/INIT Sean Christopherson
2021-07-13 16:32 ` [PATCH v2 21/46] KVM: VMX: Invert handling of CR0.WP for EPT without unrestricted guest Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 22/46] KVM: VMX: Remove direct write to vcpu->arch.cr0 during vCPU RESET/INIT Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 23/46] KVM: VMX: Fold ept_update_paging_mode_cr0() back into vmx_set_cr0() Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 24/46] KVM: nVMX: Do not clear CR3 load/store exiting bits if L1 wants 'em Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 25/46] KVM: VMX: Pull GUEST_CR3 from the VMCS iff CR3 load exiting is disabled Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 26/46] KVM: x86/mmu: Skip the permission_fault() check on MMIO if CR0.PG=0 Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 27/46] KVM: VMX: Process CR0.PG side effects after setting CR0 assets Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 28/46] KVM: VMX: Skip emulation required checks during pmode/rmode transitions Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 29/46] KVM: nVMX: Don't evaluate "emulation required" on nested VM-Exit Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 30/46] KVM: SVM: Tweak order of cr0/cr4/efer writes at RESET/INIT Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 31/46] KVM: SVM: Drop redundant writes to vmcb->save.cr4 " Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 32/46] KVM: SVM: Stuff save->dr6 at during VMSA sync, not " Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 33/46] KVM: VMX: Skip pointless MSR bitmap update when setting EFER Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 34/46] KVM: VMX: Refresh list of user return MSRs after setting guest CPUID Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 35/46] KVM: VMX: Don't _explicitly_ reconfigure user return MSRs on vCPU INIT Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 36/46] KVM: x86: Move setting of sregs during vCPU RESET/INIT to common x86 Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 37/46] KVM: VMX: Remove obsolete MSR bitmap refresh at vCPU RESET/INIT Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 38/46] KVM: nVMX: Remove obsolete MSR bitmap refresh at nested transitions Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 39/46] KVM: VMX: Don't redo x2APIC MSR bitmaps when userspace filter is changed Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 40/46] KVM: VMX: Remove unnecessary initialization of msr_bitmap_mode Sean Christopherson
2021-07-13 16:33 ` Sean Christopherson [this message]
2021-07-26 21:00   ` [PATCH v2 41/46] KVM: VMX: Smush x2APIC MSR bitmap adjustments into single function Paolo Bonzini
2021-07-26 22:21     ` Sean Christopherson
2021-07-26 22:22       ` Paolo Bonzini
2021-07-13 16:33 ` [PATCH v2 42/46] KVM: VMX: Remove redundant write to set vCPU as active at RESET/INIT Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 43/46] KVM: VMX: Move RESET-only VMWRITE sequences to init_vmcs() Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 44/46] KVM: SVM: Emulate #INIT in response to triple fault shutdown Sean Christopherson
2021-07-13 16:33 ` [PATCH v2 45/46] KVM: SVM: Drop redundant clearing of vcpu->arch.hflags at INIT/RESET Sean Christopherson
2021-07-20  4:36   ` Reiji Watanabe
2021-07-26 21:04   ` Paolo Bonzini
2021-07-13 16:33 ` [PATCH v2 46/46] KVM: x86: Preserve guest's CR0.CD/NW on INIT Sean Christopherson
2021-07-20  4:37   ` Reiji Watanabe
2021-07-27  0:01     ` Nadav Amit
2021-07-28 20:44       ` Sean Christopherson
2021-07-26 21:12 ` [PATCH v2 00/46] KVM: x86: vCPU RESET/INIT fixes and consolidation Paolo Bonzini

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=20210713163324.627647-42-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=jmattson@google.com \
    --cc=joro@8bytes.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=reijiw@google.com \
    --cc=vkuznets@redhat.com \
    --cc=wanpengli@tencent.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.