KVM/MMU: Add tlb flush with range helper function
authorLan Tianyu <Tianyu.Lan@microsoft.com>
Thu, 6 Dec 2018 13:21:08 +0000 (21:21 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 21 Dec 2018 10:28:40 +0000 (11:28 +0100)
This patch is to add wrapper functions for tlb_remote_flush_with_range
callback and flush tlb directly in kvm_mmu_zap_collapsible_spte().
kvm_mmu_zap_collapsible_spte() returns flush request to the
slot_handle_leaf() and the latter does flush on demand. When
range flush is available, make kvm_mmu_zap_collapsible_spte()
to flush tlb with range directly to avoid returning range back
to slot_handle_leaf().

Signed-off-by: Lan Tianyu <Tianyu.Lan@microsoft.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/mmu.c

index 1fba9f3562184bc9ae484679e147ab869a7af393..f105f17fb4c71b160394c0fa5b0e731a5e3cae63 100644 (file)
@@ -264,6 +264,35 @@ static void mmu_spte_set(u64 *sptep, u64 spte);
 static union kvm_mmu_page_role
 kvm_mmu_calc_root_page_role(struct kvm_vcpu *vcpu);
 
+
+static inline bool kvm_available_flush_tlb_with_range(void)
+{
+       return kvm_x86_ops->tlb_remote_flush_with_range;
+}
+
+static void kvm_flush_remote_tlbs_with_range(struct kvm *kvm,
+               struct kvm_tlb_range *range)
+{
+       int ret = -ENOTSUPP;
+
+       if (range && kvm_x86_ops->tlb_remote_flush_with_range)
+               ret = kvm_x86_ops->tlb_remote_flush_with_range(kvm, range);
+
+       if (ret)
+               kvm_flush_remote_tlbs(kvm);
+}
+
+static void kvm_flush_remote_tlbs_with_address(struct kvm *kvm,
+               u64 start_gfn, u64 pages)
+{
+       struct kvm_tlb_range range;
+
+       range.start_gfn = start_gfn;
+       range.pages = pages;
+
+       kvm_flush_remote_tlbs_with_range(kvm, &range);
+}
+
 void kvm_mmu_set_mmio_spte_mask(u64 mmio_mask, u64 mmio_value)
 {
        BUG_ON((mmio_mask & mmio_value) != mmio_value);
@@ -5671,7 +5700,13 @@ restart:
                        !kvm_is_reserved_pfn(pfn) &&
                        PageTransCompoundMap(pfn_to_page(pfn))) {
                        pte_list_remove(rmap_head, sptep);
-                       need_tlb_flush = 1;
+
+                       if (kvm_available_flush_tlb_with_range())
+                               kvm_flush_remote_tlbs_with_address(kvm, sp->gfn,
+                                       KVM_PAGES_PER_HPAGE(sp->role.level));
+                       else
+                               need_tlb_flush = 1;
+
                        goto restart;
                }
        }