Skip to content

KVM: x86/MMU: Formalize the Shadow MMU

Ben Gardon kvm lkml llvm oe-kbuild-all 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest21 patches 5 repliesIn review

Changes in v1 from the cover letter

- RFC: https://lore.kernel.org/all/[email protected]/
- 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.

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/21KVM: x86/mmu: Rename slot rmap walkers to add clarity and clean up codearch/x86/kvm/mmu/mmu.c+33 -330 replies
2/21KVM: x86/mmu: Replace comment with an actual lockdep assertion on mmu_lockarch/x86/kvm/mmu/mmu.c+2 -10 replies
3/21KVM: x86/mmu: Clean up mmu.c functions that put return type on separate linearch/x86/kvm/mmu/mmu.c+28 -310 replies
4/21KVM: x86/MMU: Add shadow_mmu.(c|h)arch/x86/kvm/Makefile arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c +1 more+46 -10 replies
5/21KVM: x86/MMU: Expose functions for the Shadow MMUarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h+41 -240 replies
6/21KVM: x86/mmu: Get rid of is_cpuid_PSE36()arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/paging_tmpl.h+3 -121 reply
7/21KVM: x86/MMU: Move the Shadow MMU implementation to shadow_mmu.carch/x86/kvm/debugfs.c arch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h +2 more+4083 -39950 replies
8/21KVM: x86/MMU: Expose functions for paging_tmpl.harch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h+68 -590 replies
9/21KVM: x86/MMU: Move paging_tmpl.h includes to shadow_mmu.carch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/paging_tmpl.h arch/x86/kvm/mmu/shadow_mmu.c +1 more+60 -362 replies
10/21KVM: x86/MMU: Clean up Shadow MMU exportsarch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+48 -810 replies
11/21KVM: x86/MMU: Cleanup shrinker interface with Shadow MMUarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+58 -500 replies
12/21KVM: x86/MMU: Clean up naming of exported Shadow MMU functionsarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/paging_tmpl.h arch/x86/kvm/mmu/shadow_mmu.c +1 more+30 -270 replies
13/21KVM: x86/MMU: Fix naming on prepare / commit zap page functionsarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+43 -390 replies
14/21KVM: x86/MMU: Factor Shadow MMU wrprot / clear dirty ops out of mmu.carch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+90 -410 replies
15/21KVM: x86/MMU: Remove unneeded exports from shadow_mmu.carch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+19 -310 replies
16/21KVM: x86/MMU: Wrap uses of kvm_handle_gfn_range in mmu.carch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+48 -390 replies
17/21KVM: x86/MMU: Add kvm_shadow_mmu_ to the last few functions in shadow_mmu.harch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+43 -360 replies
18/21KVM: x86/mmu: Move split cache topup functions to shadow_mmu.carch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h arch/x86/kvm/mmu/shadow_mmu.c+53 -550 replies
19/21KVM: x86/mmu: Move Shadow MMU part of kvm_mmu_zap_all() to shadow_mmu.harch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+23 -150 replies
20/21KVM: x86/mmu: Move Shadow MMU init/teardown to shadow_mmu.carch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/mmu_internal.h arch/x86/kvm/mmu/shadow_mmu.c +1 more+51 -420 replies
21/21KVM: x86/mmu: Split out Shadow MMU lockless walk begin/endarch/x86/kvm/mmu/mmu.c arch/x86/kvm/mmu/shadow_mmu.c arch/x86/kvm/mmu/shadow_mmu.h+36 -250 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

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

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/[email protected]/
- 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

Signature · 1 line
2.39.1.519.gcb327c4b5f-goog