kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements
@ 2020-02-28 22:52 Sean Christopherson
  2020-02-28 22:52 ` [PATCH 1/2] KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU Sean Christopherson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sean Christopherson @ 2020-02-28 22:52 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel

Two improvements for fast CR3 switch, implemented with nested VMX in mind,
but they should be helpful in general.

Sean Christopherson (2):
  KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU
  KVM: x86/mmu: Reuse the current root if possible for fast switch

 arch/x86/kvm/mmu/mmu.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

-- 
2.24.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU
  2020-02-28 22:52 [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Sean Christopherson
@ 2020-02-28 22:52 ` Sean Christopherson
  2020-02-28 22:52 ` [PATCH 2/2] KVM: x86/mmu: Reuse the current root if possible for fast switch Sean Christopherson
  2020-03-02 16:20 ` [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2020-02-28 22:52 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel

Ignore the guest's CR3 when looking for a cached root for a direct MMU,
the guest's CR3 has no impact on the direct MMU's shadow pages (the
role check ensures compatibility with CR0.WP, etc...).

Zero out root_cr3 when allocating the direct roots to make it clear that
it's ignored.

Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 arch/x86/kvm/mmu/mmu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index c4e0b97f82ac..9d617b9dc78f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3730,7 +3730,9 @@ static int mmu_alloc_direct_roots(struct kvm_vcpu *vcpu)
 		vcpu->arch.mmu->root_hpa = __pa(vcpu->arch.mmu->pae_root);
 	} else
 		BUG();
-	vcpu->arch.mmu->root_cr3 = vcpu->arch.mmu->get_cr3(vcpu);
+
+	/* root_cr3 is ignored for direct MMUs. */
+	vcpu->arch.mmu->root_cr3 = 0;
 
 	return 0;
 }
@@ -4272,8 +4274,8 @@ static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_cr3,
 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
 		swap(root, mmu->prev_roots[i]);
 
-		if (new_cr3 == root.cr3 && VALID_PAGE(root.hpa) &&
-		    page_header(root.hpa) != NULL &&
+		if ((new_role.direct || new_cr3 == root.cr3) &&
+		    VALID_PAGE(root.hpa) && page_header(root.hpa) &&
 		    new_role.word == page_header(root.hpa)->role.word)
 			break;
 	}
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] KVM: x86/mmu: Reuse the current root if possible for fast switch
  2020-02-28 22:52 [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Sean Christopherson
  2020-02-28 22:52 ` [PATCH 1/2] KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU Sean Christopherson
@ 2020-02-28 22:52 ` Sean Christopherson
  2020-03-02 16:20 ` [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2020-02-28 22:52 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel

Reuse the current root when possible instead of grabbing a different
root from the array of cached roots.  Doing so avoids unnecessary MMU
switches and also fixes a quirk where KVM can't reuse roots without
creating multiple roots since the cache is a victim cache, i.e. roots
are added to the cache when they're "evicted", not when they are
created.  The quirk could be fixed by adding roots to the cache on
creation, but that would reduce the effective size of the cache as one
of its entries would be burned to track the current root.

Reusing the current root is especially helpful for nested virt as the
current root is almost always usable for the "new" MMU on nested
VM-entry/VM-exit.

Cc: Vitaly Kuznetsov <vkuznets@redhat.com>
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
---
 arch/x86/kvm/mmu/mmu.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 9d617b9dc78f..53b776dfc949 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -4253,6 +4253,14 @@ static void nonpaging_init_context(struct kvm_vcpu *vcpu,
 	context->nx = false;
 }
 
+static inline bool is_root_usable(struct kvm_mmu_root_info *root, gpa_t cr3,
+				  union kvm_mmu_page_role role)
+{
+	return (role.direct || cr3 == root->cr3) &&
+	       VALID_PAGE(root->hpa) && page_header(root->hpa) &&
+	       role.word == page_header(root->hpa)->role.word;
+}
+
 /*
  * Find out if a previously cached root matching the new CR3/role is available.
  * The current root is also inserted into the cache.
@@ -4271,12 +4279,13 @@ static bool cached_root_available(struct kvm_vcpu *vcpu, gpa_t new_cr3,
 	root.cr3 = mmu->root_cr3;
 	root.hpa = mmu->root_hpa;
 
+	if (is_root_usable(&root, new_cr3, new_role))
+		return true;
+
 	for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++) {
 		swap(root, mmu->prev_roots[i]);
 
-		if ((new_role.direct || new_cr3 == root.cr3) &&
-		    VALID_PAGE(root.hpa) && page_header(root.hpa) &&
-		    new_role.word == page_header(root.hpa)->role.word)
+		if (is_root_usable(&root, new_cr3, new_role))
 			break;
 	}
 
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements
  2020-02-28 22:52 [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Sean Christopherson
  2020-02-28 22:52 ` [PATCH 1/2] KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU Sean Christopherson
  2020-02-28 22:52 ` [PATCH 2/2] KVM: x86/mmu: Reuse the current root if possible for fast switch Sean Christopherson
@ 2020-03-02 16:20 ` Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2020-03-02 16:20 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel, kvm,
	linux-kernel

On 28/02/20 23:52, Sean Christopherson wrote:
> Two improvements for fast CR3 switch, implemented with nested VMX in mind,
> but they should be helpful in general.
> 
> Sean Christopherson (2):
>   KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU
>   KVM: x86/mmu: Reuse the current root if possible for fast switch
> 
>  arch/x86/kvm/mmu/mmu.c | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
> 

Queued, thanks.

Paolo


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-03-02 16:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-28 22:52 [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Sean Christopherson
2020-02-28 22:52 ` [PATCH 1/2] KVM: x86/mmu: Ignore guest CR3 on fast root switch for direct MMU Sean Christopherson
2020-02-28 22:52 ` [PATCH 2/2] KVM: x86/mmu: Reuse the current root if possible for fast switch Sean Christopherson
2020-03-02 16:20 ` [PATCH 0/2] KVM: x86/mmu: Fast CR3 switch improvements Paolo Bonzini

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).