Skip to contentSkip to conversation

KVM: x86/mmu: TDP MMU fixes/cleanups

PATCH Applied Sean Christopherson 2 participants 4 messages Mar 31, 2021

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86/mmu: TDP MMU fixes/cleanups

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(-)
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
author maintainer
#1 lore raw

PATCH 1/2 KVM: x86/mmu: Remove spurious clearing of dirty bit from TDP MMU SPTE

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 <[email protected]>
---
 arch/x86/kvm/mmu/tdp_mmu.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +0 -1
index f0c99fa04ef2..724088bea4b0 100644
@@ -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;
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
author maintainer
#2 lore raw

PATCH 2/2 KVM: x86/mmu: Simplify code for aging SPTEs in TDP MMU

In reply to Sean Christopherson

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.

---
 arch/x86/kvm/mmu/tdp_mmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +2 -3
index 724088bea4b0..161b77925a19 100644
@@ -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
Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
maintainer Applied
#3 lore raw
In reply to Sean Christopherson
Quoted 9 lines · Sean Christopherson

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