Skip to content

KVM: Consolidate and optimize MMU notifiers

Sean Christopherson kvm kvm-ppc kvmarm linux-arm-kernel linux-mips lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest10 patches 13 repliesIn review
  2. v118 patches 25 repliesApplied

Changes in v2 from the cover letter

- Drop the patches that have already been pushed to kvm/queue.
- Drop two selftest changes that had snuck in via "git commit -a".
- Add a patch to assert that mmu_notifier_count is elevated when .change_pte() runs. [Paolo]
- Split out moving KVM_MMU_(UN)LOCK() to __kvm_handle_hva_range() to a separate patch. Opted not to squash it with the introduction of the common hva walkers (patch 02), as that prevented sharing code between the old and new APIs. [Paolo]
- Tweak the comment in kvm_vm_destroy() above the smashing of the new slots lock. [Paolo]
- Make mmu_notifier_slots_lock unconditional to avoid #ifdefs. [Paolo]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/10KVM: Assert that notifier count is elevated in .change_pte()virt/kvm/kvm_main.c+7 -21 reply
2/10KVM: Move x86's MMU notifier memslot walkers to generic codearch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/tdp_mmu.c +3 more+317 -2490 replies
3/10KVM: arm64: Convert to the gfn-based MMU notifier callbacksarch/arm64/include/asm/kvm_host.h arch/arm64/kvm/mmu.c+33 -851 reply
4/10KVM: MIPS/MMU: Convert to the gfn-based MMU notifier callbacksarch/mips/include/asm/kvm_host.h arch/mips/kvm/mmu.c+17 -810 replies
5/10KVM: PPC: Convert to the gfn-based MMU notifier callbacksarch/powerpc/include/asm/kvm_book3s.h arch/powerpc/include/asm/kvm_host.h arch/powerpc/include/asm/kvm_ppc.h +7 more+95 -1730 replies
6/10KVM: Kill off the old hva-based MMU notifier callbacksarch/arm64/include/asm/kvm_host.h arch/mips/include/asm/kvm_host.h arch/powerpc/include/asm/kvm_host.h +3 more+0 -970 replies
7/10KVM: Move MMU notifier's mmu_lock acquisition into common helpervirt/kvm/kvm_main.c+82 -432 replies
8/10KVM: Take mmu_lock when handling MMU notifier iff the hva hits a memslotvirt/kvm/kvm_main.c+11 -40 replies
9/10KVM: Don't take mmu_lock for range invalidation unless necessaryinclude/linux/kvm_host.h virt/kvm/kvm_main.c+80 -227 replies
10/10KVM: x86/mmu: Allow yielding during MMU notifier unmap/zap, if possiblearch/x86/kvm/mmu/tdp_mmu.c+5 -10 replies

Reviews trailers per patch

1 of 10 patches has a Reviewed-by or Acked-by.

PatchMarc Zyngier
SeriesT-b
1/10
2/10
3/10R-b
4/10
5/10
6/10
7/10
8/10
9/10
10/10

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 00/10] KVM: Consolidate and optimize MMU notifiers

The end goal of this series is to optimize the MMU notifiers to take mmu_lock if and only if the notification is relevant to KVM, i.e. the hva
range overlaps a memslot. Large VMs (hundreds of vCPUs) are very
sensitive to mmu_lock being taken for write at inopportune times, and such VMs also tend to be "static", e.g. backed by HugeTLB with minimal page shenanigans. The vast majority of notifications for these VMs will be spurious (for KVM), and eliding mmu_lock for spurious notifications avoids an otherwise unacceptable disruption to the guest.

To get there without potentially degrading performance, e.g. due to multiple memslot lookups, especially on non-x86 where the use cases are largely unknown (from my perspective), first consolidate the MMU notifier logic by moving the hva->gfn lookups into common KVM.

Based on kvm/queue, commit 5f986f748438 ("KVM: x86: dump_vmcs should include the autoload/autostore MSR lists").

Well tested on Intel and AMD. Compile tested for arm64, MIPS, PPC, PPC e500, and s390. Absolutely needs to be tested for real on non-x86, I give it even odds that I introduced an off-by-one bug somewhere.

v2:
- Drop the patches that have already been pushed to kvm/queue.
- Drop two selftest changes that had snuck in via "git commit -a".
- Add a patch to assert that mmu_notifier_count is elevated when .change_pte() runs. [Paolo]
- Split out moving KVM_MMU_(UN)LOCK() to __kvm_handle_hva_range() to a separate patch. Opted not to squash it with the introduction of the common hva walkers (patch 02), as that prevented sharing code between the old and new APIs. [Paolo]
- Tweak the comment in kvm_vm_destroy() above the smashing of the new slots lock. [Paolo]
- Make mmu_notifier_slots_lock unconditional to avoid #ifdefs. [Paolo]

v1:
- https://lkml.kernel.org/r/[email protected]

Sean Christopherson (10):
KVM: Assert that notifier count is elevated in .change_pte()
KVM: Move x86's MMU notifier memslot walkers to generic code
KVM: arm64: Convert to the gfn-based MMU notifier callbacks
KVM: MIPS/MMU: Convert to the gfn-based MMU notifier callbacks
KVM: PPC: Convert to the gfn-based MMU notifier callbacks
KVM: Kill off the old hva-based MMU notifier callbacks
KVM: Move MMU notifier's mmu_lock acquisition into common helper
KVM: Take mmu_lock when handling MMU notifier iff the hva hits a
memslot
KVM: Don't take mmu_lock for range invalidation unless necessary
KVM: x86/mmu: Allow yielding during MMU notifier unmap/zap, if
possible

 arch/arm64/kvm/mmu.c                   | 117 +++------
 arch/mips/kvm/mmu.c                    |  97 ++------
 arch/powerpc/include/asm/kvm_book3s.h  |  12 +-
 arch/powerpc/include/asm/kvm_ppc.h     |   9 +-
 arch/powerpc/kvm/book3s.c              |  18 +-
 arch/powerpc/kvm/book3s.h              |  10 +-
 arch/powerpc/kvm/book3s_64_mmu_hv.c    |  98 ++------
 arch/powerpc/kvm/book3s_64_mmu_radix.c |  25 +-
 arch/powerpc/kvm/book3s_hv.c           |  12 +-
 arch/powerpc/kvm/book3s_pr.c           |  56 ++---
 arch/powerpc/kvm/e500_mmu_host.c       |  27 +-
 arch/x86/kvm/mmu/mmu.c                 | 127 ++++------
 arch/x86/kvm/mmu/tdp_mmu.c             | 245 +++++++------------
 arch/x86/kvm/mmu/tdp_mmu.h             |  14 +-
 include/linux/kvm_host.h               |  22 +-
 virt/kvm/kvm_main.c                    | 325 +++++++++++++++++++------
 16 files changed, 552 insertions(+), 662 deletions(-)
Signature · 1 line
2.31.0.208.g409f899ff0-goog