Skip to content

KVM: x86/mmu: Restore fast invalidate/zap flow

Sean Christopherson kvm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest11 patches 2 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/11KVM: x86/mmu: Reintroduce fast invalidate/zap for flushing memslotarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu.c+101 -20 replies
2/11KVM: x86/mmu: Treat invalid shadow pages as obsoletearch/x86/kvm/mmu.c+3 -20 replies
3/11KVM: x86/mmu: Use fast invalidate mechanism to zap MMIO sptesarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu.c+3 -150 replies
4/11KVM: x86/mmu: Revert "Revert "KVM: MMU: show mmu_valid_gen in shadow page related tracepoints""arch/x86/kvm/mmutrace.h+12 -90 replies
5/11KVM: x86/mmu: Revert "Revert "KVM: MMU: add tracepoint for kvm_mmu_invalidate_all_pages""arch/x86/kvm/mmu.c arch/x86/kvm/mmutrace.h+22 -00 replies
6/11KVM: x86/mmu: Revert "Revert "KVM: MMU: zap pages in batch""arch/x86/kvm/mmu.c+9 -260 replies
7/11KVM: x86/mmu: Revert "Revert "KVM: MMU: collapse TLB flushes when zap all pages""arch/x86/kvm/mmu.c+22 -30 replies
8/11KVM: x86/mmu: Revert "Revert "KVM: MMU: reclaim the zapped-obsolete page first""arch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu.c arch/x86/kvm/x86.c+19 -50 replies
9/11KVM: x86/mmu: Revert "KVM: x86/mmu: Remove is_obsolete() call"arch/x86/kvm/mmu.c+6 -10 replies
10/11KVM: x86/mmu: Explicitly track only a single invalid mmu generationarch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu.c arch/x86/kvm/mmutrace.h+22 -120 replies
11/11KVM: x86/mmu: Skip invalid pages during zapping iff root_count is zeroarch/x86/kvm/mmu.c+5 -40 replies

Reviews trailers per patch

None of the 11 patches has a Reviewed-by or Acked-by.

PatchAlex Williamson
SeriesT-b
1/11
2/11
3/11
4/11
5/11
6/11
7/11
8/11
9/11
10/11
11/11

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

Read the v1 thread

Cover letter [PATCH 00/11] KVM: x86/mmu: Restore fast invalidate/zap flow

Restore the fast invalidate flow for zapping shadow pages and use it whenever vCPUs can be active in the VM. This fixes (in theory, not yet confirmed) a regression reported by James Harvey where KVM can livelock in kvm_mmu_zap_all() when it's invoked in response to a memslot update.

The fast invalidate flow was removed as it was deemed to be unnecessary after its primary user, memslot flushing, was reworked to zap only the memslot in question instead of all shadow pages. Unfortunately, zapping only the memslot being (re)moved during a memslot update introduced a regression for VMs with assigned devices. Because we could not discern why zapping only the relevant memslot broke device assignment, or if the regression extended beyond device assignment, we reverted to zapping all shadow pages when a memslot is (re)moved.

The revert to "zap all" failed to account for subsequent changes that have been made to kvm_mmu_zap_all() between then and now. Specifically, kvm_mmu_zap_all() now conditionally drops reschedules and drops mmu_lock if a reschedule is needed or if the lock is contended. Dropping the lock allows other vCPUs to add shadow pages, and, with enough vCPUs, can cause kvm_mmu_zap_all() to get stuck in an infinite loop as it can never zap all pages before observing lock contention or the need to reschedule.

The reasoning behind having kvm_mmu_zap_all() conditionally reschedule was that it would only be used when the VM is inaccesible, e.g. when its mm_struct is dying or when the VM itself is being destroyed. In that case, playing nice with the rest of the kernel instead of hogging cycles to free unused shadow pages made sense.

Since it's unlikely we'll root cause the device assignment regression any time soon, and that simply removing the conditional rescheduling isn't guaranteed to return us to a known good state, restore the fast invalidate flow for zapping on memslot updates, including mmio generation wraparound. Opportunisticaly tack on a bug fix and a couple enhancements.

Alex and James, it probably goes without saying... please test, especially patch 01/11 as a standalone patch as that'll likely need to be applied to stable branches, assuming it works. Thanks!

Sean Christopherson (11):
KVM: x86/mmu: Reintroduce fast invalidate/zap for flushing memslot
KVM: x86/mmu: Treat invalid shadow pages as obsolete
KVM: x86/mmu: Use fast invalidate mechanism to zap MMIO sptes
KVM: x86/mmu: Revert "Revert "KVM: MMU: show mmu_valid_gen in shadow
page related tracepoints""
KVM: x86/mmu: Revert "Revert "KVM: MMU: add tracepoint for
kvm_mmu_invalidate_all_pages""
KVM: x86/mmu: Revert "Revert "KVM: MMU: zap pages in batch""
KVM: x86/mmu: Revert "Revert "KVM: MMU: collapse TLB flushes when zap
all pages""
KVM: x86/mmu: Revert "Revert "KVM: MMU: reclaim the zapped-obsolete
page first""
KVM: x86/mmu: Revert "KVM: x86/mmu: Remove is_obsolete() call"
KVM: x86/mmu: Explicitly track only a single invalid mmu generation
KVM: x86/mmu: Skip invalid pages during zapping iff root_count is zero

 arch/x86/include/asm/kvm_host.h |   4 +-
 arch/x86/kvm/mmu.c              | 154 ++++++++++++++++++++++++++++----
 arch/x86/kvm/mmutrace.h         |  42 +++++++--
 arch/x86/kvm/x86.c              |   1 +
 4 files changed, 173 insertions(+), 28 deletions(-)
Signature · 1 line
2.22.0