Re: [RFC 11/19] KVM: x86/mmu: Factor shadow_zero_check out of make_spte

From: Paolo Bonzini
Date: Thu Nov 11 2021 - 02:06:53 EST


On 11/11/21 02:18, Sean Christopherson wrote:
But what would you actually move? Even shadow_zero_check barely squeaks by,
e.g. if NX is ever used to for NPT, then maybe it stops being a per-VM setting.

Hmm, I think it would still be per-VM, just like 32-bit shadow page tables
are always built for EFER.NXE=CR4.PAE=1. Anyway, the rough sketch is to have
three structs:

* struct kvm_mmu_kind has the function pointers and the state that is
needed to operate on page tables

* struct kvm_mmu has the function pointers and the state that is
needed while the vCPU runs, including the role

* struct kvm_paging_context has the stuff related to emulation;
shadow page tables of course needs it but EPT/NPT do not (with
either the old or the new MMU)

So you'd have a "struct kvm_mmu_kind direct_mmu" in struct kvm_arch (for
either legacy EPT/NPT or the new MMU), and

struct kvm_mmu_kind shadow_mmu;
struct kvm_mmu root_mmu; /* either TDP or shadow */
struct kvm_mmu tdp12_mmu; /* always shadow */
struct kvm_mmu *mmu; /* either &kvm->direct_mmu or &vcpu->shadow_mmu */
struct kvm_paging_context root_walk; /* maybe unified with walk01 below? dunno yet */
struct kvm_paging_context walk01;
struct kvm_paging_context walk12;
struct kvm_paging_context *walk; /* either &vcpu->root_walk or &vcpu->walk12 */

in struct kvm_vcpu_arch. struct kvm_mmu* has a pointer to
struct kvm_mmu_kind*; however, if an spte.c function does not need
the data in struct kvm_mmu_state*, it can take a struct kvm_mmu_kind*
and it won't need a vCPU. Likewise the TDP MMU knows its kvm_mmu_kind
is always in &kvm->direct_mmu so it can take a struct kvm* if the struct
kvm_mmu_state* is not needed.

The first part of the refactoring would be to kill the nested_mmu
and walk_mmu, replacing them by &vcpu->walk12 and vcpu->walk
respectively. The half-useless nested_mmu has always bothered me,
I was going to play with it anyway because I want to remove the
kvm_mmu_reset_context from CR0.WP writes, I'll see if I get
something useful out of it.

Paolo