]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: Allow arch code to elide TLB flushes when aging a young page
authorSean Christopherson <seanjc@google.com>
Fri, 11 Oct 2024 02:10:49 +0000 (19:10 -0700)
committerSean Christopherson <seanjc@google.com>
Wed, 30 Oct 2024 22:25:40 +0000 (15:25 -0700)
Add a Kconfig to allow architectures to opt-out of a TLB flush when a
young page is aged, as invalidating TLB entries is not functionally
required on most KVM-supported architectures.  Stale TLB entries can
result in false negatives and theoretically lead to suboptimal reclaim,
but in practice all observations have been that the performance gained by
skipping TLB flushes outweighs any performance lost by reclaiming hot
pages.

E.g. the primary MMUs for x86 RISC-V, s390, and PPC Book3S elide the TLB
flush for ptep_clear_flush_young(), and arm64's MMU skips the trailing DSB
that's required for ordering (presumably because there are optimizations
related to eliding other TLB flushes when doing make-before-break).

Link: https://lore.kernel.org/r/20241011021051.1557902-18-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
virt/kvm/Kconfig
virt/kvm/kvm_main.c

index fd6a3010afa833e077623065b80bdbb5b1012250..54e959e7d68fb4f935a726055ec51a9202f95942 100644 (file)
@@ -100,6 +100,10 @@ config KVM_GENERIC_MMU_NOTIFIER
        select MMU_NOTIFIER
        bool
 
+config KVM_ELIDE_TLB_FLUSH_IF_YOUNG
+       depends on KVM_GENERIC_MMU_NOTIFIER
+       bool
+
 config KVM_GENERIC_MEMORY_ATTRIBUTES
        depends on KVM_GENERIC_MMU_NOTIFIER
        bool
index b1b10dc408a0968fb1d6c339a4eb7dd92197f4c2..83b525d16b61b63c91a0379b4f96abbad2030501 100644 (file)
@@ -630,7 +630,8 @@ mmu_unlock:
 static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,
                                                unsigned long start,
                                                unsigned long end,
-                                               gfn_handler_t handler)
+                                               gfn_handler_t handler,
+                                               bool flush_on_ret)
 {
        struct kvm *kvm = mmu_notifier_to_kvm(mn);
        const struct kvm_mmu_notifier_range range = {
@@ -638,7 +639,7 @@ static __always_inline int kvm_handle_hva_range(struct mmu_notifier *mn,
                .end            = end,
                .handler        = handler,
                .on_lock        = (void *)kvm_null_fn,
-               .flush_on_ret   = true,
+               .flush_on_ret   = flush_on_ret,
                .may_block      = false,
        };
 
@@ -650,17 +651,7 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn
                                                         unsigned long end,
                                                         gfn_handler_t handler)
 {
-       struct kvm *kvm = mmu_notifier_to_kvm(mn);
-       const struct kvm_mmu_notifier_range range = {
-               .start          = start,
-               .end            = end,
-               .handler        = handler,
-               .on_lock        = (void *)kvm_null_fn,
-               .flush_on_ret   = false,
-               .may_block      = false,
-       };
-
-       return __kvm_handle_hva_range(kvm, &range).ret;
+       return kvm_handle_hva_range(mn, start, end, handler, false);
 }
 
 void kvm_mmu_invalidate_begin(struct kvm *kvm)
@@ -825,7 +816,8 @@ static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
 {
        trace_kvm_age_hva(start, end);
 
-       return kvm_handle_hva_range(mn, start, end, kvm_age_gfn);
+       return kvm_handle_hva_range(mn, start, end, kvm_age_gfn,
+                                   !IS_ENABLED(CONFIG_KVM_ELIDE_TLB_FLUSH_IF_YOUNG));
 }
 
 static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,