kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups
@ 2021-03-31  0:49 Sean Christopherson
  2021-03-31  0:49 ` [PATCH 1/2] KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE Sean Christopherson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sean Christopherson @ 2021-03-31  0:49 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel, Ben Gardon

Two minor fixes/cleanups for the TDP MMU, found by inspection.

Sean Christopherson (2):
  KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE
  KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU

 arch/x86/kvm/mmu/tdp_mmu.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

-- 
2.31.0.291.g576ba9dcdaf-goog


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

* [PATCH 1/2] KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE
  2021-03-31  0:49 [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Sean Christopherson
@ 2021-03-31  0:49 ` Sean Christopherson
  2021-03-31  0:49 ` [PATCH 2/2] KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU Sean Christopherson
  2021-03-31  9:36 ` [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2021-03-31  0:49 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel, Ben Gardon

Don't clear the dirty bit when aging a TDP MMU SPTE (in response to a MMU
notifier event).  Prematurely clearing the dirty bit could cause spurious
PML updates if aging a page happened to coincide with dirty logging.

Note, tdp_mmu_set_spte_no_acc_track() flows into __handle_changed_spte(),
so the host PFN will be marked dirty, i.e. there is no potential for data
corruption.

Fixes: a6a0b05da9f3 ("kvm: x86/mmu: Support dirty logging for the TDP MMU")
Cc: Ben Gardon <bgardon@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/mmu/tdp_mmu.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index f0c99fa04ef2..724088bea4b0 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -978,7 +978,6 @@ static int age_gfn_range(struct kvm *kvm, struct kvm_memory_slot *slot,
 
 			new_spte = mark_spte_for_access_track(new_spte);
 		}
-		new_spte &= ~shadow_dirty_mask;
 
 		tdp_mmu_set_spte_no_acc_track(kvm, &iter, new_spte);
 		young = 1;
-- 
2.31.0.291.g576ba9dcdaf-goog


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

* [PATCH 2/2] KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU
  2021-03-31  0:49 [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Sean Christopherson
  2021-03-31  0:49 ` [PATCH 1/2] KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE Sean Christopherson
@ 2021-03-31  0:49 ` Sean Christopherson
  2021-03-31  9:36 ` [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2021-03-31  0:49 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Sean Christopherson, Vitaly Kuznetsov, Wanpeng Li, Jim Mattson,
	Joerg Roedel, kvm, linux-kernel, Ben Gardon

Use a basic NOT+AND sequence to clear the Accessed bit in TDP MMU SPTEs,
as opposed to the fancy ffs()+clear_bit() logic that was copied from the
legacy MMU.  The legacy MMU uses clear_bit() because it is operating on
the SPTE itself, i.e. clearing needs to be atomic.  The TDP MMU operates
on a local variable that it later writes to the SPTE, and so doesn't need
to be atomic or even resident in memory.

Opportunistically drop unnecessary initialization of new_spte, it's
guaranteed to be written before being accessed.

Using NOT+AND instead of ffs()+clear_bit() reduces the sequence from:

   0x0000000000058be6 <+134>:	test   %rax,%rax
   0x0000000000058be9 <+137>:	je     0x58bf4 <age_gfn_range+148>
   0x0000000000058beb <+139>:	test   %rax,%rdi
   0x0000000000058bee <+142>:	je     0x58cdc <age_gfn_range+380>
   0x0000000000058bf4 <+148>:	mov    %rdi,0x8(%rsp)
   0x0000000000058bf9 <+153>:	mov    $0xffffffff,%edx
   0x0000000000058bfe <+158>:	bsf    %eax,%edx
   0x0000000000058c01 <+161>:	movslq %edx,%rdx
   0x0000000000058c04 <+164>:	lock btr %rdx,0x8(%rsp)
   0x0000000000058c0b <+171>:	mov    0x8(%rsp),%r15

to:

   0x0000000000058bdd <+125>:	test   %rax,%rax
   0x0000000000058be0 <+128>:	je     0x58beb <age_gfn_range+139>
   0x0000000000058be2 <+130>:	test   %rax,%r8
   0x0000000000058be5 <+133>:	je     0x58cc0 <age_gfn_range+352>
   0x0000000000058beb <+139>:	not    %rax
   0x0000000000058bee <+142>:	and    %r8,%rax
   0x0000000000058bf1 <+145>:	mov    %rax,%r15

thus eliminating several memory accesses, including a locked access.

Cc: Ben Gardon <bgardon@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/mmu/tdp_mmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
index 724088bea4b0..161b77925a19 100644
--- a/arch/x86/kvm/mmu/tdp_mmu.c
+++ b/arch/x86/kvm/mmu/tdp_mmu.c
@@ -951,7 +951,7 @@ static int age_gfn_range(struct kvm *kvm, struct kvm_memory_slot *slot,
 {
 	struct tdp_iter iter;
 	int young = 0;
-	u64 new_spte = 0;
+	u64 new_spte;
 
 	rcu_read_lock();
 
@@ -966,8 +966,7 @@ static int age_gfn_range(struct kvm *kvm, struct kvm_memory_slot *slot,
 		new_spte = iter.old_spte;
 
 		if (spte_ad_enabled(new_spte)) {
-			clear_bit((ffs(shadow_accessed_mask) - 1),
-				  (unsigned long *)&new_spte);
+			new_spte &= ~shadow_accessed_mask;
 		} else {
 			/*
 			 * Capture the dirty status of the page, so that it doesn't get
-- 
2.31.0.291.g576ba9dcdaf-goog


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

* Re: [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups
  2021-03-31  0:49 [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Sean Christopherson
  2021-03-31  0:49 ` [PATCH 1/2] KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE Sean Christopherson
  2021-03-31  0:49 ` [PATCH 2/2] KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU Sean Christopherson
@ 2021-03-31  9:36 ` Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2021-03-31  9:36 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Vitaly Kuznetsov, Wanpeng Li, Jim Mattson, Joerg Roedel, kvm,
	linux-kernel, Ben Gardon

On 31/03/21 02:49, Sean Christopherson wrote:
> Two minor fixes/cleanups for the TDP MMU, found by inspection.
> 
> Sean Christopherson (2):
>    KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE
>    KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU
> 
>   arch/x86/kvm/mmu/tdp_mmu.c | 6 ++----
>   1 file changed, 2 insertions(+), 4 deletions(-)
> 

Queued, thanks.

Paolo


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

end of thread, other threads:[~2021-03-31  9:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-31  0:49 [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups Sean Christopherson
2021-03-31  0:49 ` [PATCH 1/2] KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE Sean Christopherson
2021-03-31  0:49 ` [PATCH 2/2] KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU Sean Christopherson
2021-03-31  9:36 ` [PATCH 0/2] KVM: x86/mmu: TDP MMU fixes/cleanups 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).