KVM: x86/mmu: Misc cleanups, mostly TDP MMU
Sean Christopherson
maintainercover KVM: x86/mmu: Misc cleanups, mostly TDP MMU
Effectively belated code review of a few pieces of the TDP MMU.
Sean Christopherson (5):
KVM: x86/mmu: Remove spurious TLB flush from TDP MMU's change_pte()
hook
KVM: x86/mmu: WARN if TDP MMU's set_tdp_spte() sees multiple GFNs
KVM: x86/mmu: Use 'end' param in TDP MMU's test_age_gfn()
KVM: x86/mmu: Add typedefs for rmap/iter handlers
KVM: x86/mmu: Add convenience wrapper for acting on single hva in TDP
MMU
arch/x86/kvm/mmu/mmu.c | 27 +++++++------------ arch/x86/kvm/mmu/tdp_mmu.c | 54 ++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 40 deletions(-)
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Sean Christopherson
maintainerPATCH 1/5 KVM: x86/mmu: Remove spurious TLB flush from TDP MMU's change_pte() hook
Remove an unnecessary remote TLB flush from set_tdp_spte(), the TDP MMu's hook for handling change_pte() invocations from the MMU notifier. If the new host PTE is writable, the flush is completely redundant as there are no futher changes to the SPTE before the post-loop flush. If the host PTE is read-only, then the primary MMU is responsible for ensuring that the contents of the old and new pages are identical, thus it's safe to let the guest continue reading both the old and new pages. KVM must only ensure the old page cannot be referenced after returning from its callback; this is handled by the post-loop flush.
--- arch/x86/kvm/mmu/tdp_mmu.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +6 -2
@@ -1044,10 +1044,14 @@ static int set_tdp_spte(struct kvm *kvm, struct kvm_memory_slot *slot,if (!is_shadow_present_pte (iter.old_spte))break ; +/* +* Note, when changing a read-only SPTE, it's not strictly +* necessary to zero the SPTE before setting the new PFN, but +* doing so preserves the invariant that the PFN of a present +* leaf SPTE can never change. See __handle_changed_spte(). +*/ tdp_mmu_set_spte (kvm, &iter,0 ); -kvm_flush_remote_tlbs_with_address (kvm, iter.gfn,1 ); -if (!pte_write (*ptep)) { new_spte =kvm_mmu_changed_pte_notifier_make_spte ( iter.old_spte, new_pfn);
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Sean Christopherson
maintainerPATCH 2/5 KVM: x86/mmu: WARN if TDP MMU's set_tdp_spte() sees multiple GFNs
In reply to Sean ChristophersonWARN if set_tdp_spte() is invoked with multipel GFNs. It is specifically a callback to handle a single host PTE being changed. Consuming the @end parameter also eliminates the confusing 'unused' parameter.
--- arch/x86/kvm/mmu/tdp_mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +2 -2
@@ -1022,7 +1022,7 @@ int kvm_tdp_mmu_test_age_hva(struct kvm *kvm, unsigned long hva)* Returns non-zero if a flush is needed before releasing the MMU lock. */ static int set_tdp_spte (struct kvm *kvm,struct kvm_memory_slot *slot, -struct kvm_mmu_page *root,gfn_t gfn,gfn_t unused, +struct kvm_mmu_page *root,gfn_t gfn,gfn_t end,unsigned long data) {struct tdp_iter iter;
@@ -1033,7 +1033,7 @@ static int set_tdp_spte(struct kvm *kvm, struct kvm_memory_slot *slot,rcu_read_lock (); -WARN_ON (pte_huge (*ptep)); +WARN_ON (pte_huge (*ptep) || (gfn +1 ) != end); new_pfn =pte_pfn (*ptep);
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Sean Christopherson
maintainerPATCH 3/5 KVM: x86/mmu: Use 'end' param in TDP MMU's test_age_gfn()
In reply to Sean ChristophersonUse the @end param when aging a GFN instead of hardcoding the walk to a single GFN. Unlike tdp_set_spte(), which simply cannot work with more than one GFN, aging multiple GFNs would not break, though admittedly it would be weird. Be nice to the casual reader and don't make them puzzle out why the end GFN is unused.
No functional change intended.
--- arch/x86/kvm/mmu/tdp_mmu.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +3 -3
@@ -997,12 +997,12 @@ int kvm_tdp_mmu_age_hva_range(struct kvm *kvm, unsigned long start, }static int test_age_gfn (struct kvm *kvm,struct kvm_memory_slot *slot, -struct kvm_mmu_page *root,gfn_t gfn,gfn_t unused, -unsigned long unused2) +struct kvm_mmu_page *root,gfn_t gfn,gfn_t end, +unsigned long unused) {struct tdp_iter iter; -tdp_root_for_each_leaf_pte (iter, root, gfn, gfn +1 ) +tdp_root_for_each_leaf_pte (iter, root, gfn, end)if (is_accessed_spte (iter.old_spte))return 1 ;
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Sean Christopherson
maintainerPATCH 4/5 KVM: x86/mmu: Add typedefs for rmap/iter handlers
In reply to Sean ChristophersonAdd typedefs for the MMU handlers that are invoked when walking the MMU SPTEs (rmaps in legacy MMU) to act on a host virtual address range.
No functional change intended.
--- arch/x86/kvm/mmu/mmu.c | 27 ++++++++++----------------- arch/x86/kvm/mmu/tdp_mmu.c | 20 +++++++++----------- 2 files changed, 19 insertions(+), 28 deletions(-)
arch/x86/kvm/mmu/mmu.c +10 -17
@@ -1424,17 +1424,15 @@ static void slot_rmap_walk_next(struct slot_rmap_walk_iterator *iterator)slot_rmap_walk_okay (_iter_); \ slot_rmap_walk_next(_iter_)) -static __always_inline int -kvm_handle_hva_range (struct kvm *kvm, -unsigned long start, -unsigned long end, -unsigned long data, -int (*handler)(struct kvm *kvm, -struct kvm_rmap_head *rmap_head, -struct kvm_memory_slot *slot, -gfn_t gfn, -int level, -unsigned long data)) +typedef int (*rmap_handler_t )(struct kvm *kvm,struct kvm_rmap_head *rmap_head, +struct kvm_memory_slot *slot,gfn_t gfn, +int level,unsigned long data); + +static __always_inline int kvm_handle_hva_range(struct kvm *kvm, +unsigned long start, +unsigned long end, +unsigned long data, +rmap_handler_t handler) {struct kvm_memslots *slots;struct kvm_memory_slot *memslot;
@@ -1473,12 +1471,7 @@ kvm_handle_hva_range(struct kvm *kvm, }static int kvm_handle_hva (struct kvm *kvm,unsigned long hva, -unsigned long data, -int (*handler)(struct kvm *kvm, -struct kvm_rmap_head *rmap_head, -struct kvm_memory_slot *slot, -gfn_t gfn,int level, -unsigned long data)) +unsigned long data,rmap_handler_t handler) {return kvm_handle_hva_range (kvm, hva, hva +1 , data, handler); }
arch/x86/kvm/mmu/tdp_mmu.c +9 -11
@@ -879,17 +879,15 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code,return ret; } -static __always_inline int -kvm_tdp_mmu_handle_hva_range (struct kvm *kvm, -unsigned long start, -unsigned long end, -unsigned long data, -int (*handler)(struct kvm *kvm, -struct kvm_memory_slot *slot, -struct kvm_mmu_page *root, -gfn_t start, -gfn_t end, -unsigned long data)) +typedef int (*tdp_handler_t )(struct kvm *kvm,struct kvm_memory_slot *slot, +struct kvm_mmu_page *root,gfn_t start,gfn_t end, +unsigned long data); + +static __always_inline int kvm_tdp_mmu_handle_hva_range(struct kvm *kvm, +unsigned long start, +unsigned long end, +unsigned long data, +tdp_handler_t handler) {struct kvm_memslots *slots;struct kvm_memory_slot *memslot;
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Sean Christopherson
maintainerPATCH 5/5 KVM: x86/mmu: Add convenience wrapper for acting on single hva in TDP MMU
In reply to Sean ChristophersonAdd a TDP MMU helper to handle a single HVA hook, the name is a nice reminder that the flow in question is operating on a single HVA.
No functional change intended.
--- arch/x86/kvm/mmu/tdp_mmu.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu/tdp_mmu.c +11 -5
@@ -922,6 +922,14 @@ static __always_inline int kvm_tdp_mmu_handle_hva_range(struct kvm *kvm,return ret; } +static __always_inline int kvm_tdp_mmu_handle_hva(struct kvm *kvm, +unsigned long addr, +unsigned long data, +tdp_handler_t handler) +{ +return kvm_tdp_mmu_handle_hva_range (kvm, addr, addr +1 , data, handler); +} + static int zap_gfn_range_hva_wrapper(struct kvm *kvm, struct kvm_memory_slot *slot, struct kvm_mmu_page *root, gfn_t start,
@@ -1009,8 +1017,7 @@ static int test_age_gfn(struct kvm *kvm, struct kvm_memory_slot *slot,int kvm_tdp_mmu_test_age_hva (struct kvm *kvm,unsigned long hva) { -return kvm_tdp_mmu_handle_hva_range (kvm, hva, hva +1 ,0 , - test_age_gfn); +return kvm_tdp_mmu_handle_hva (kvm, hva,0 , test_age_gfn); }/*
@@ -1071,9 +1078,8 @@ static int set_tdp_spte(struct kvm *kvm, struct kvm_memory_slot *slot,int kvm_tdp_mmu_set_spte_hva (struct kvm *kvm,unsigned long address,pte_t *host_ptep) { -return kvm_tdp_mmu_handle_hva_range (kvm, address, address +1 , - (unsigned long )host_ptep, - set_tdp_spte); +return kvm_tdp_mmu_handle_hva (kvm, address, (unsigned long )host_ptep, + set_tdp_spte); }/*
Signature · 1 line
2.30.1.766.gb4fecdf3b7-goog
Quoted 11 lines · Sean Christopherson
Effectively belated code review of a few pieces of the TDP MMU.
Sean Christopherson (5):
KVM: x86/mmu: Remove spurious TLB flush from TDP MMU'schange_pte()
hook
KVM: x86/mmu: WARN if TDP MMU'sset_tdp_spte()sees multiple GFNs
KVM: x86/mmu: Use 'end' param in TDP MMU'stest_age_gfn()
KVM: x86/mmu: Add typedefs for rmap/iter handlers
KVM: x86/mmu: Add convenience wrapper for acting on single hva in TDP
MMU
Quoted 4 lines
arch/x86/kvm/mmu/mmu.c | 27 +++++++------------ arch/x86/kvm/mmu/tdp_mmu.c | 54 ++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 40 deletions(-)
Queued (for 5.13), thanks.
Paolo