[PATCH 00/21] KVM: x86/MMU: Formalize the Shadow MMU

From: Ben Gardon
Date: Thu Feb 02 2023 - 13:28:17 EST


This series makes the Shadow MMU a distinct part of the KVM x86 MMU,
implemented in separate files, with a defined interface to common code.

When the TDP (Two Dimensional Paging) MMU was added to x86 KVM, it came in
a separate file with a (reasonably) clear interface. This lead to many
points in the KVM MMU like this:

if (tdp_mmu_on())
kvm_tdp_mmu_do_stuff()

if (memslots_have_rmaps())
/* Do whatever was being done before */

The implementations of various functions which preceded the TDP MMU have
remained scattered around mmu.c with no clear identity or interface. Over the
last couple years, the KVM x86 community has settled on calling the KVM MMU
implementation which preceded the TDP MMU the "Shadow MMU", as it grew
from shadow paging, which supported virtualization on hardware pre-TDP.
(Note that the Shadow MMU can also build TDP page tables, and doesn't
only do shadow paging, so the meaning is a bit overloaded.)

Splitting it out into separate files will give a clear interface and make it
easier to distinguish common x86 MMU code from the code specific to the two
MMU implementations.

Patches 1-3 are cleanups from Sean

Patches 4-6 prepare for the refactor by adding files and exporting
functions.

Patch 7 the big move, transferring 3.5K lines from mmu.c to
shadow_mmu.c
(It may be best if whoever ends up preparing the pull request with
this patch just dumps my version and re-does the move so that no code is
lost.)

Patches 8 and 9 move the includes for paging_tmpl.h to shadow_mmu.c

Patches 10-17 clean up the interface between the Shadow MMU and
common MMU code.

The last few patches are in response to feedback on the RFC and move
additional code to the Shadow MMU.

Patch 7 is an enormous change, and doing it all at once in a single
commit all but guarantees merge conflicts and makes it hard to review. I
don't have a good answer to this problem as there's no easy way to move
3.5K lines between files. I tried moving the code bit-by-bit but the
intermediate steps added complexity and ultimately the 50+ patches it
created didn't seem any easier to review.
Doing the big move all at once at least makes it easier to get past when
doing Git archeology, and doing it at the beginning of the series allows the
rest of the commits to still show up in Git blame.

I've tested this series on an Intel Skylake host with kvm-unit-tests and
selftests.

RFC -> v1:
- RFC: https://lore.kernel.org/all/20221221222418.3307832-1-bgardon@xxxxxxxxxx/
- Moved some more Shadow MMU content to shadow_mmu.c. David Matlack
pointed out some code I'd missed in the first pass. Added commits
to the end of the series to achieve this.
- Dropped is_cpuid_PSE36 and moved all the BUILD_MMU_ROLE*() macros
to mmu_internal, also as suggested by David Matlack.
- Added copyright comments to the tops of shadow_mmu.c and .h
- Tacked some cleanups from Sean onto the beginning of the series.

Ben Gardon (18):
KVM: x86/MMU: Add shadow_mmu.(c|h)
KVM: x86/MMU: Expose functions for the Shadow MMU
KVM: x86/mmu: Get rid of is_cpuid_PSE36()
KVM: x86/MMU: Move the Shadow MMU implementation to shadow_mmu.c
KVM: x86/MMU: Expose functions for paging_tmpl.h
KVM: x86/MMU: Move paging_tmpl.h includes to shadow_mmu.c
KVM: x86/MMU: Clean up Shadow MMU exports
KVM: x86/MMU: Cleanup shrinker interface with Shadow MMU
KVM: x86/MMU: Clean up naming of exported Shadow MMU functions
KVM: x86/MMU: Fix naming on prepare / commit zap page functions
KVM: x86/MMU: Factor Shadow MMU wrprot / clear dirty ops out of mmu.c
KVM: x86/MMU: Remove unneeded exports from shadow_mmu.c
KVM: x86/MMU: Wrap uses of kvm_handle_gfn_range in mmu.c
KVM: x86/MMU: Add kvm_shadow_mmu_ to the last few functions in
shadow_mmu.h
KVM: x86/mmu: Move split cache topup functions to shadow_mmu.c
KVM: x86/mmu: Move Shadow MMU part of kvm_mmu_zap_all() to
shadow_mmu.h
KVM: x86/mmu: Move Shadow MMU init/teardown to shadow_mmu.c
KVM: x86/mmu: Split out Shadow MMU lockless walk begin/end

Sean Christopherson (3):
KVM: x86/mmu: Rename slot rmap walkers to add clarity and clean up
code
KVM: x86/mmu: Replace comment with an actual lockdep assertion on
mmu_lock
KVM: x86/mmu: Clean up mmu.c functions that put return type on
separate line

arch/x86/kvm/Makefile | 2 +-
arch/x86/kvm/debugfs.c | 1 +
arch/x86/kvm/mmu/mmu.c | 4834 ++++---------------------------
arch/x86/kvm/mmu/mmu_internal.h | 87 +-
arch/x86/kvm/mmu/paging_tmpl.h | 15 +-
arch/x86/kvm/mmu/shadow_mmu.c | 3692 +++++++++++++++++++++++
arch/x86/kvm/mmu/shadow_mmu.h | 132 +
7 files changed, 4498 insertions(+), 4265 deletions(-)
create mode 100644 arch/x86/kvm/mmu/shadow_mmu.c
create mode 100644 arch/x86/kvm/mmu/shadow_mmu.h


base-commit: 7cb79f433e75b05d1635aefaa851cfcd1cb7dc4f
--
2.39.1.519.gcb327c4b5f-goog