KVM: apply chainsaw to struct kvm_mmu
Subsystems (4)
Paolo Bonzini
maintainercover KVM: apply chainsaw to struct kvm_mmu
The kvm_mmu is a "god data structure" that includes three different
tasks: describing the guest page table's format, walking the guest page tables and building the page tables. This means that the (already poorly named) nested_mmu is only used in part, since it has no page tables to construct.
Furthermore, some parts are reused across guest and host page tables (such as the reserved bits detector) but others are not; for example permission_fault is replaced by simplified code such as is_executable_pte().
This series cleans this up by splitting kvm_mmu in three parts:
- kvm_pagewalk is the page table walker. There are two of them per vCPU, cpu_walk and tdp_walk. walk_mmu is *always* replaced by cpu_walk no matter if running an L1 or L2 guest, unlike in the current code that moves it between root_mmu and nested_mmu.
- kvm_mmu retains the page table building functionality. It uses a page table walker to build shadow pages; that is always cpu_walk for root_mmu or tdp_walk for guest_mmu.
- kvm_page_format allows KVM to operate on PTEs that already exist. Both kvm_pagewalk and kvm_mmu have their own kvm_page_format, though at least for now kvm_mmu only uses it for reserved bit checks.
This is in general an interesting cleanup, not least because it reduces the confusion between guest_mmu and nested_mmu. See for example the comment "Exempt nested MMUs" which actually exempts guest_mmu. While I'm not going as far as renaming guest_mmu, there is indeed less confusion due nested_mmu coming before the introduction of guest_mmu and stealing the obvious name. However, the last patch also shows the code reuse benefits can be used for new features too.
By adapting the permission_fault() machinery and using it to test SPTEs against struct kvm_page_fault, it makes it possible to support SPTEs that have XS!=XU; these were not supported yet by KVM, but could now be added via memory attributes.
I'm posting this as RFC to give an early preview of this, while trying to sort out David's reported issue with MBEC. It's tested very lightly; in particular, right now npt=0 seems broken for Linux guests and I also have not tried Intel or 32-bit hosts at all.
Paolo
ps: part of the work was done with help from AI, especially for the more
mechanical patches. However all the planning of each commit was done by me and I used the LLM essentially as a "natural language Coccinelle" (e.g., "move gva_to_gpa from struct kvm_mmu to struct kvm_pagewalk. if the function that calls it has a variable of type kvm_pagewalk, use it instead of mmu->w"). Since there's really just one way to do the work given the prompts that I used, I still consider even the individual patches to be assisted by LLMs and not generated. Alas, the patches were created prior to the introduction of Documentation/process/coding-assistants.rst; if required, I can go back and try to figure out which of the refactoring patches were done this way.
Paolo Bonzini (22):
KVM: x86: remove nested_mmu from mmu_is_nested()
KVM: x86: move pdptrs out of the MMU
KVM: x86: check that kvm_handle_invpcid is only invoked with shadow
paging
KVM: x86/hyperv: remove unnecessary mmu_is_nested() check
KVM: x86/mmu: introduce struct kvm_pagewalk
KVM: x86/mmu: move get_guest_pgd to struct kvm_pagewalk
KVM: x86/mmu: move gva_to_gpa to struct kvm_pagewalk
KVM: x86/mmu: move get_pdptr to struct kvm_pagewalk
KVM: x86/mmu: move inject_page_fault to struct kvm_pagewalk
KVM: x86/mmu: move CPU-related fields to struct kvm_pagewalk
KVM: x86/mmu: change CPU-role accessor fields to take struct
kvm_pagewalk
KVM: x86/mmu: move remaining permission fields to struct kvm_pagewalk
KVM: x86/mmu: pass struct kvm_pagewalk to kvm_mmu_invalidate_addr
KVM: x86/mmu: change walk_mmu to struct kvm_pagewalk
KVM: x86/mmu: change nested_mmu.w to nested_cpu_walk
KVM: x86/mmu: make cpu_walk a value
KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
KVM: x86/mmu: cleanup functions that initialize shadow MMU
KVM: x86/mmu: pull page format to a new struct
KVM: x86/mmu: merge struct rsvd_bits_validate into struct
kvm_page_format
KVM: x86/mmu: parameterize update_permission_bitmask()
KVM: x86/mmu: use kvm_page_format to test SPTEs
arch/x86/include/asm/kvm_host.h | 75 +++--- arch/x86/kvm/hyperv.c | 7 +- arch/x86/kvm/kvm_cache_regs.h | 4 +- arch/x86/kvm/mmu.h | 31 +-- arch/x86/kvm/mmu/mmu.c | 411 +++++++++++++++----------------- arch/x86/kvm/mmu/paging_tmpl.h | 88 +++---- arch/x86/kvm/mmu/spte.c | 4 +- arch/x86/kvm/mmu/spte.h | 64 ++--- arch/x86/kvm/mmu/tdp_mmu.c | 3 +- arch/x86/kvm/svm/nested.c | 22 +- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/nested.c | 29 ++- arch/x86/kvm/vmx/vmx.c | 22 +- arch/x86/kvm/x86.c | 67 +++--- arch/x86/kvm/x86.h | 2 +- 15 files changed, 411 insertions(+), 420 deletions(-)
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 1/22 KVM: x86: remove nested_mmu from mmu_is_nested()
nested_mmu is always stored into vcpu->arch.walk_mmu at the same time as guest_mmu is stored into vcpu->arch.mmu. But nested_mmu is not even a proper MMU, it is only used for page walking; plus the fact that walk_mmu has to be switched at all is just an implementation detail.
In the end what matters here is whether the guest is using nested page tables; vmx/nested.c and svm/nested.c check it to see if they are in nEPT or nNPT context respectively. So switch to checking root_mmu vs. guest_mmu, which is a more cogent test.
--- arch/x86/kvm/x86.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.h +1 -1
@@ -290,7 +290,7 @@ static inline bool x86_exception_has_error_code(unsigned int vector)static inline bool mmu_is_nested (struct kvm_vcpu *vcpu) { -return vcpu->arch.walk_mmu == &vcpu->arch.nested_mmu; +return vcpu->arch.mmu == &vcpu->arch.guest_mmu; }static inline bool is_pae (struct kvm_vcpu *vcpu)
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 2/22 KVM: x86: move pdptrs out of the MMU
In reply to Paolo BonziniPDPTRs are part of the CPU state. A bit unconventionally, they are reached via vcpu->arch.walk_mmu instead of being stored in vcpu->arch directly. That is nice in principle---it would allow TDP shadow paging to have its own PDPTRs---but it is not necessary, because EPT has no PDPTRs and NPT does not cache them.
Since kvm_pdptr_read does not otherwise need the MMU, drop the pdptrs from the MMU altogether. There is however a negative effect, in that they are now not stored separately in root_mmu and nested_mmu for L1 and L2 guests. This means that they are overwritten by nested VM entry and exit, and need to be manually marked dirty.
Note that page table PDPTRs are not affected, since they are stored in pae_root.
--- arch/x86/include/asm/kvm_host.h | 5 ++--- arch/x86/kvm/kvm_cache_regs.h | 4 ++-- arch/x86/kvm/svm/nested.c | 9 ++++++--- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/nested.c | 14 +++++++++----- arch/x86/kvm/vmx/vmx.c | 20 ++++++++------------ arch/x86/kvm/x86.c | 6 +++--- 7 files changed, 31 insertions(+), 29 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -3
@@ -519,10 +519,7 @@ struct kvm_mmu {* the bits spte never used. */ struct rsvd_bits_validate shadow_zero_check; -struct rsvd_bits_validate guest_rsvd_check; - -u64 pdptrs[4 ];/* pae */ };enum pmc_type {
@@ -879,6 +876,8 @@ struct kvm_vcpu_arch {*/ struct kvm_mmu *walk_mmu; +u64 pdptrs[4 ];/* pae */ +struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;struct kvm_mmu_memory_cache mmu_shadow_page_cache;struct kvm_mmu_memory_cache mmu_shadowed_info_cache;
arch/x86/kvm/kvm_cache_regs.h +2 -2
@@ -158,12 +158,12 @@ static inline u64 kvm_pdptr_read(struct kvm_vcpu *vcpu, int index)if (!kvm_register_is_available (vcpu,VCPU_EXREG_PDPTR ))kvm_x86_call (cache_reg)(vcpu,VCPU_EXREG_PDPTR ); -return vcpu->arch.walk_mmu->pdptrs[index]; +return vcpu->arch.pdptrs[index]; }static inline void kvm_pdptr_write (struct kvm_vcpu *vcpu,int index,u64 value) { - vcpu->arch.walk_mmu->pdptrs[index] = value; + vcpu->arch.pdptrs[index] = value; }static inline ulong kvm_read_cr0_bits (struct kvm_vcpu *vcpu,ulong mask)
arch/x86/kvm/svm/nested.c +6 -3
@@ -680,9 +680,12 @@ static int nested_svm_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,if (CC (!kvm_vcpu_is_legal_cr3 (vcpu, cr3)))return -EINVAL ; -if (reload_pdptrs && !nested_npt &&is_pae_paging (vcpu) && -CC (!load_pdptrs (vcpu, cr3))) -return -EINVAL ; +if (reload_pdptrs &&is_pae_paging (vcpu)) { +if (nested_npt) +kvm_register_mark_dirty (vcpu,VCPU_EXREG_PDPTR ); +else if (CC (!load_pdptrs (vcpu, cr3))) +return -EINVAL ; + } vcpu->arch.cr3 = cr3;
arch/x86/kvm/svm/svm.c +1 -1
@@ -1526,7 +1526,7 @@ static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)switch (reg) {case VCPU_EXREG_PDPTR :/* -* When !npt_enabled, mmu->pdptrs[] is already available since +* When !npt_enabled, vcpu->pdptrs[] is already available since * it is always updated per SDM when moving to CRs. */ if (npt_enabled)
arch/x86/kvm/vmx/nested.c +9 -5
@@ -1192,12 +1192,16 @@ static int nested_vmx_load_cr3(struct kvm_vcpu *vcpu, unsigned long cr3,/* * If PAE paging and EPT are both on, CR3 is not used by the CPU and -* must not be dereferenced. +* must not be dereferenced. Marking the PDPTRs dirty is enough, +* if ever needed they will be fished out of VMCS02's GUEST_PDPTRx. */ -if (reload_pdptrs && !nested_ept &&is_pae_paging (vcpu) && -CC (!load_pdptrs (vcpu, cr3))) { - *entry_failure_code =ENTRY_FAIL_PDPTE ; -return -EINVAL ; +if (reload_pdptrs &&is_pae_paging (vcpu)) { +if (nested_ept) { +kvm_register_mark_dirty (vcpu,VCPU_EXREG_PDPTR ); + }else if (CC (!load_pdptrs (vcpu, cr3))) { + *entry_failure_code =ENTRY_FAIL_PDPTE ; +return -EINVAL ; + } } vcpu->arch.cr3 = cr3;
arch/x86/kvm/vmx/vmx.c +8 -12
@@ -3363,30 +3363,26 @@ void vmx_flush_tlb_guest(struct kvm_vcpu *vcpu)void vmx_ept_load_pdptrs (struct kvm_vcpu *vcpu) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; -if (!kvm_register_is_dirty (vcpu,VCPU_EXREG_PDPTR ))return ;if (is_pae_paging (vcpu)) { -vmcs_write64 (GUEST_PDPTR0 , mmu->pdptrs[0 ]); -vmcs_write64 (GUEST_PDPTR1 , mmu->pdptrs[1 ]); -vmcs_write64 (GUEST_PDPTR2 , mmu->pdptrs[2 ]); -vmcs_write64 (GUEST_PDPTR3 , mmu->pdptrs[3 ]); +vmcs_write64 (GUEST_PDPTR0 , vcpu->arch.pdptrs[0 ]); +vmcs_write64 (GUEST_PDPTR1 , vcpu->arch.pdptrs[1 ]); +vmcs_write64 (GUEST_PDPTR2 , vcpu->arch.pdptrs[2 ]); +vmcs_write64 (GUEST_PDPTR3 , vcpu->arch.pdptrs[3 ]); } }void ept_save_pdptrs (struct kvm_vcpu *vcpu) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; -if (WARN_ON_ONCE (!is_pae_paging (vcpu)))return ; - mmu->pdptrs[0 ] =vmcs_read64 (GUEST_PDPTR0 ); - mmu->pdptrs[1 ] =vmcs_read64 (GUEST_PDPTR1 ); - mmu->pdptrs[2 ] =vmcs_read64 (GUEST_PDPTR2 ); - mmu->pdptrs[3 ] =vmcs_read64 (GUEST_PDPTR3 ); + vcpu->arch.pdptrs[0 ] =vmcs_read64 (GUEST_PDPTR0 ); + vcpu->arch.pdptrs[1 ] =vmcs_read64 (GUEST_PDPTR1 ); + vcpu->arch.pdptrs[2 ] =vmcs_read64 (GUEST_PDPTR2 ); + vcpu->arch.pdptrs[3 ] =vmcs_read64 (GUEST_PDPTR3 );kvm_register_mark_available (vcpu,VCPU_EXREG_PDPTR ); }
arch/x86/kvm/x86.c +3 -3
@@ -1065,7 +1065,7 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)gpa_t real_gpa;int i;int ret; -u64 pdpte[ARRAY_SIZE (mmu->pdptrs)]; +u64 pdpte[ARRAY_SIZE (vcpu->arch.pdptrs)];/* * If the MMU is nested, CR3 holds an L2 GPA and needs to be translated
@@ -1094,10 +1094,10 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)* Marking VCPU_EXREG_PDPTR dirty doesn't work for !tdp_enabled. * Shadow page roots need to be reconstructed instead. */ -if (!tdp_enabled &&memcmp (mmu->pdptrs, pdpte,sizeof (mmu->pdptrs))) +if (!tdp_enabled &&memcmp (vcpu->arch.pdptrs, pdpte,sizeof (vcpu->arch.pdptrs)))kvm_mmu_free_roots (vcpu->kvm, mmu,KVM_MMU_ROOT_CURRENT ); -memcpy (mmu->pdptrs, pdpte,sizeof (mmu->pdptrs)); +memcpy (vcpu->arch.pdptrs, pdpte,sizeof (vcpu->arch.pdptrs));kvm_register_mark_dirty (vcpu,VCPU_EXREG_PDPTR );kvm_make_request (KVM_REQ_LOAD_MMU_PGD , vcpu); vcpu->arch.pdptrs_from_userspace = false;
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerRe: [PATCH 02/22] KVM: x86: move pdptrs out of the MMU
Quoted 6 lines · Paolo Bonzini
PDPTRs are part of the CPU state. A bit unconventionally, they are reached via
vcpu->arch.walk_mmuinstead of being stored invcpu->archdirectly. That is nice in principle---it would allow TDP shadow paging to have its own PDPTRs---but it is not necessary, because EPT has no PDPTRs and NPT does not cache them.
Quoted 8 lines
Since
kvm_pdptr_readdoes not otherwise need the MMU, drop the pdptrs from the MMU altogether. There is however a negative effect, in that they are now not stored separately inroot_mmuandnested_mmufor L1 and L2 guests. This means that they are overwritten by nested VM entry and exit, and need to be manually marked dirty.Note that page table PDPTRs are not affected, since they are stored in
pae_root.
The vmx/nested.c and svm/nested.c changes here are untested and completely broken - I mentioned this in the cover letter but should have done it here too in case anyone reviews this. But there are some cleanup opportunities now that I looked at it more closely.
Paolo
Paolo Bonzini
maintainerPATCH 3/22 KVM: x86: check that kvm_handle_invpcid is only invoked with shadow paging
In reply to Paolo BonziniThis is true for both Intel and AMD. On Intel, "enable INVPCID" is set unconditionally if supported, but the vmexit is triggered by the "INVLPG exiting" control which is disabled by enable_ept. On AMD, KVM can intercept INVPCID if NPT is enabled but only in order to inject #UD in the guest.
--- arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+)
arch/x86/kvm/x86.c +3 -0
@@ -14282,6 +14282,9 @@ int kvm_handle_invpcid(struct kvm_vcpu *vcpu, unsigned long type, gva_t gva)return 1 ; } +if (WARN_ON_ONCE (tdp_enabled)) +return 0 ; + pcid_enabled =kvm_is_cr4_bit_set (vcpu,X86_CR4_PCIDE );switch (type) {
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 4/22 KVM: x86/hyperv: remove unnecessary mmu_is_nested() check
In reply to Paolo BonziniJust always go through kvm_translate_gpa(), which will either invoke the vendor check or just return hc->ingpa back.
This is a better way to fix the issue of commit 464af6fc2b1d ("KVM:
x86: check for nEPT/nNPT in slow flush hypercalls", 2026-05-03).
--- arch/x86/kvm/hyperv.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
arch/x86/kvm/hyperv.c +3 -4
@@ -2040,10 +2040,9 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)* flush). Translate the address here so the memory can be uniformly * read with kvm_read_guest(). */ -if (!hc->fast &&mmu_is_nested (vcpu)) { - hc->ingpa = kvm_x86_ops.nested_ops->translate_nested_gpa ( - vcpu, hc->ingpa, -PFERR_GUEST_FINAL_MASK ,NULL ,0 ); +if (!hc->fast) { + hc->ingpa =kvm_translate_gpa (vcpu, vcpu->arch.walk_mmu, hc->ingpa, +PFERR_GUEST_FINAL_MASK ,NULL ,0 );if (unlikely (hc->ingpa ==INVALID_GPA ))return HV_STATUS_INVALID_HYPERCALL_INPUT ; }
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 5/22 KVM: x86/mmu: introduce struct kvm_pagewalk
In reply to Paolo BonziniIn preparation for separating walking and building of page tables, introduce a dummy struct kvm_pagewalk and pass it around instead of its containing kvm_mmu to functions that do not build the page tables.
Outermost functions retrieve the mmu via container_of, while internal functions can pass around the struct kvm_pagewalk pointer. x86.c is still (mostly) oblivious to the existence of struct kvm_pagewalk. There are only a couple exceptions for now, which were done already here for simplicity, but the plan is for the KVM code to use struct kvm_pagewalk whenever dealing with guest page tables.
--- arch/x86/include/asm/kvm_host.h | 7 +++++- arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/mmu.h | 19 +++++++++------ arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 43 +++++++++++++++++++-------------- arch/x86/kvm/x86.c | 4 +-- 6 files changed, 46 insertions(+), 31 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -1
@@ -473,10 +473,15 @@ struct kvm_page_fault;/* * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit, -* and 2-level 32-bit). The kvm_mmu structure abstracts the details of the +* and 2-level 32-bit). The kvm_pagewalk structure abstracts the details of the * current mmu mode. */ +struct kvm_pagewalk { +}; +struct kvm_mmu { +struct kvm_pagewalk w; +unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index);int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);
arch/x86/kvm/hyperv.c +1 -1
@@ -2041,7 +2041,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)* read with kvm_read_guest(). */ if (!hc->fast) { - hc->ingpa =kvm_translate_gpa (vcpu, vcpu->arch.walk_mmu, hc->ingpa, + hc->ingpa =kvm_translate_gpa (vcpu, &vcpu->arch.walk_mmu->w, hc->ingpa,PFERR_GUEST_FINAL_MASK ,NULL ,0 );if (unlikely (hc->ingpa ==INVALID_GPA ))return HV_STATUS_INVALID_HYPERCALL_INPUT ;
arch/x86/kvm/mmu.h +11 -8
@@ -169,21 +169,22 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) }static inline void kvm_mmu_refresh_passthrough_bits (struct kvm_vcpu *vcpu, -struct kvm_mmu *mmu) +struct kvm_pagewalk *w) {/* * When EPT is enabled, KVM may passthrough CR0.WP to the guest, i.e. -* @mmu's snapshot of CR0.WP and thus all related paging metadata may +* @w's snapshot of CR0.WP and thus all related paging metadata may * be stale. Refresh CR0.WP and the metadata on-demand when checking * for permission faults. Exempt nested MMUs, i.e. MMUs for shadowing * nEPT and nNPT, as CR0.WP is ignored in both cases. Note, KVM does * need to refresh nested_mmu, a.k.a. the walker used to translate L2 * GVAs to GPAs, as that "MMU" needs to honor L2's CR0.WP. */ -if (!tdp_enabled || mmu == &vcpu->arch.guest_mmu) +if (!tdp_enabled || w == &vcpu->arch.guest_mmu.w)return ; -__kvm_mmu_refresh_passthrough_bits (vcpu, mmu); +__kvm_mmu_refresh_passthrough_bits (vcpu, + container_of(w, struct kvm_mmu, w)); }/*
@@ -194,10 +195,12 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,* Return zero if the access does not fault; return the page fault error code * if the access faults. */ -static inline u8 permission_fault (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +static inline u8 permission_fault (struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,unsigned pte_access,unsigned pte_pkey,u64 access) { + struct kvm_mmu *mmu = container_of(w, struct kvm_mmu, w); +/* strip nested paging fault error codes */ unsigned int pfec = access;unsigned long rflags =kvm_x86_call (get_rflags)(vcpu);
@@ -220,7 +223,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,u32 errcode =PFERR_PRESENT_MASK ;bool fault; -kvm_mmu_refresh_passthrough_bits (vcpu, mmu); +kvm_mmu_refresh_passthrough_bits (vcpu, w); fault = (mmu->permissions[index] >> pte_access) &1 ;
@@ -301,12 +304,12 @@ static inline void kvm_update_page_stats(struct kvm *kvm, int level, int count) }static inline gpa_t kvm_translate_gpa (struct kvm_vcpu *vcpu, -struct kvm_mmu *mmu, +struct kvm_pagewalk *w,gpa_t gpa,u64 access,struct x86_exception *exception,u64 pte_access) { -if (mmu != &vcpu->arch.nested_mmu) +if (w != &vcpu->arch.nested_mmu.w)return gpa; return kvm_x86_ops.nested_ops->translate_nested_gpa(vcpu, gpa, access, exception,
arch/x86/kvm/mmu/mmu.c +1 -1
@@ -4354,7 +4354,7 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,* user-mode address if CR0.PG=0. Therefore *include* ACC_USER_MASK in * the last argument to kvm_translate_gpa (which NPT does not use). */ -return kvm_translate_gpa (vcpu, mmu, vaddr, access |PFERR_GUEST_FINAL_MASK , +return kvm_translate_gpa (vcpu, &mmu->w, vaddr, access |PFERR_GUEST_FINAL_MASK , exception,ACC_ALL ); }
arch/x86/kvm/mmu/paging_tmpl.h +25 -18
161 lines not on this page · raw message
arch/x86/kvm/x86.c +2 -2
@@ -1071,7 +1071,7 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)* If the MMU is nested, CR3 holds an L2 GPA and needs to be translated * to an L1 GPA. */ - real_gpa =kvm_translate_gpa (vcpu, mmu,gfn_to_gpa (pdpt_gfn), + real_gpa =kvm_translate_gpa (vcpu, &mmu->w,gfn_to_gpa (pdpt_gfn),PFERR_USER_MASK |PFERR_WRITE_MASK |PFERR_GUEST_PAGE_MASK ,NULL ,0 );if (real_gpa ==INVALID_GPA )
@@ -8090,7 +8090,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,* shadow page table for L2 guest. */ if (vcpu_match_mmio_gva (vcpu, gva) && (!is_paging (vcpu) || - !permission_fault (vcpu, vcpu->arch.walk_mmu, + !permission_fault (vcpu, &vcpu->arch.walk_mmu->w, vcpu->arch.mmio_access,0 , access))) { *gpa = vcpu->arch.mmio_gfn <<PAGE_SHIFT | (gva & (PAGE_SIZE -1 ));
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 6/22 KVM: x86/mmu: move get_guest_pgd to struct kvm_pagewalk
In reply to Paolo BonziniStart moving page walking functionality out of kvm_mmu. The easiest target is the callbacks; change the kvm_mmu_get_guest_pgd() wrapper to take a struct kvm_pagewalk too, and avoid the MMU indirection whenever the caller has one.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++--------- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/svm/nested.c | 4 +++- arch/x86/kvm/vmx/nested.c | 3 ++- 5 files changed, 19 insertions(+), 13 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -477,12 +477,12 @@ struct kvm_page_fault;* current mmu mode. */ struct kvm_pagewalk { +unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); };struct kvm_mmu {struct kvm_pagewalk w; -unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index);int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); void (*inject_page_fault)(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu/mmu.c +12 -9
@@ -269,12 +269,12 @@ static unsigned long get_guest_cr3(struct kvm_vcpu *vcpu) }static inline unsigned long kvm_mmu_get_guest_pgd (struct kvm_vcpu *vcpu, -struct kvm_mmu *mmu) +struct kvm_pagewalk *w) { -if (IS_ENABLED (CONFIG_MITIGATION_RETPOLINE ) && mmu->get_guest_pgd == get_guest_cr3) +if (IS_ENABLED (CONFIG_MITIGATION_RETPOLINE ) && w->get_guest_pgd == get_guest_cr3)return kvm_read_cr3 (vcpu); -return mmu->get_guest_pgd (vcpu); +return w->get_guest_pgd (vcpu); }static inline bool kvm_available_flush_remote_tlbs_range (void )
@@ -4071,7 +4071,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)int quadrant, i, r;hpa_t root; - root_pgd =kvm_mmu_get_guest_pgd (vcpu, mmu); + root_pgd =kvm_mmu_get_guest_pgd (vcpu, &mmu->w); root_gfn = (root_pgd & __PT_BASE_ADDR_MASK) >>PAGE_SHIFT ;if (!kvm_vcpu_is_visible_gfn (vcpu, root_gfn)) {
@@ -4543,7 +4543,7 @@ static bool kvm_arch_setup_async_pf(struct kvm_vcpu *vcpu,if (arch.direct_map) arch.cr3 = (unsigned long )INVALID_GPA ;else - arch.cr3 =kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu); + arch.cr3 =kvm_mmu_get_guest_pgd (vcpu, &vcpu->arch.mmu->w);return kvm_setup_async_pf (vcpu, fault->addr,kvm_vcpu_gfn_to_hva (vcpu, fault->gfn), &arch);
@@ -4565,7 +4565,7 @@ void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)return ;if (!vcpu->arch.mmu->root_role.direct && - work->arch.cr3 !=kvm_mmu_get_guest_pgd (vcpu, vcpu->arch.mmu)) + work->arch.cr3 !=kvm_mmu_get_guest_pgd (vcpu, &vcpu->arch.mmu->w))return ; r = kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, work->arch.error_code,
@@ -5880,10 +5880,11 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->root_role.word = root_role.word; context->page_fault = kvm_tdp_page_fault; context->sync_spte =NULL ; - context->get_guest_pgd = get_guest_cr3; context->get_pdptr = kvm_pdptr_read; context->inject_page_fault = kvm_inject_page_fault; + context->w.get_guest_pgd = get_guest_cr3; +if (!is_cr0_pg (context)) context->gva_to_gpa = nonpaging_gva_to_gpa;else if (is_cr4_pae (context))
@@ -6031,7 +6032,8 @@ static void init_kvm_softmmu(struct kvm_vcpu *vcpu,kvm_init_shadow_mmu (vcpu, cpu_role); - context->get_guest_pgd = get_guest_cr3; + context->w.get_guest_pgd = get_guest_cr3; + context->get_pdptr = kvm_pdptr_read; context->inject_page_fault = kvm_inject_page_fault; }
@@ -6045,10 +6047,11 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,return ; g_context->cpu_role.as_u64 = new_mode.as_u64; - g_context->get_guest_pgd = get_guest_cr3; g_context->get_pdptr = kvm_pdptr_read; g_context->inject_page_fault = kvm_inject_page_fault; + g_context->w.get_guest_pgd = get_guest_cr3; +/* * L2 page tables are never shadowed, so there is no need to sync * SPTEs.
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -342,7 +342,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,trace_kvm_mmu_pagetable_walk (addr, access); retry_walk: walker->level = mmu->cpu_role.base.level; - pte =kvm_mmu_get_guest_pgd (vcpu, mmu); + pte =kvm_mmu_get_guest_pgd (vcpu, w); have_ad =PT_HAVE_ACCESSED_DIRTY (mmu);#if PTTYPE ==64
arch/x86/kvm/svm/nested.c +3 -1
@@ -97,7 +97,9 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) svm->vmcb01.ptr->save.efer, svm->nested.ctl.nested_cr3, svm->nested.ctl.misc_ctl); - vcpu->arch.mmu->get_guest_pgd = nested_svm_get_tdp_cr3; + + vcpu->arch.mmu->w.get_guest_pgd = nested_svm_get_tdp_cr3; + vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
arch/x86/kvm/vmx/nested.c +2 -1
@@ -494,7 +494,8 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.guest_mmu;nested_ept_new_eptp (vcpu); - vcpu->arch.mmu->get_guest_pgd = nested_ept_get_eptp; + vcpu->arch.mmu->w.get_guest_pgd = nested_ept_get_eptp; + vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; vcpu->arch.mmu->get_pdptr = kvm_pdptr_read;
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 7/22 KVM: x86/mmu: move gva_to_gpa to struct kvm_pagewalk
In reply to Paolo Bonzinigva_to_gpa is the main entry point into walk_mmu, which is only used for guest page table walking (as opposed to building the page tables). Moving gva_to_gpa to struct kvm_pagewalk is a steps towards making walk_mmu a struct kvm_pagewalk.
--- arch/x86/include/asm/kvm_host.h | 6 +++--- arch/x86/kvm/mmu/mmu.c | 26 +++++++++++++------------- arch/x86/kvm/mmu/paging_tmpl.h | 6 +++--- arch/x86/kvm/svm/nested.c | 4 ++-- arch/x86/kvm/vmx/nested.c | 4 ++-- arch/x86/kvm/x86.c | 30 +++++++++++++++--------------- 6 files changed, 38 insertions(+), 38 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -3
@@ -478,6 +478,9 @@ struct kvm_page_fault;*/ struct kvm_pagewalk {unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); +gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w, +gpa_t gva_or_gpa,u64 access, +struct x86_exception *exception); };struct kvm_mmu {
@@ -487,9 +490,6 @@ struct kvm_mmu {int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);void (*inject_page_fault)(struct kvm_vcpu *vcpu,struct x86_exception *fault); -gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, -gpa_t gva_or_gpa,u64 access, -struct x86_exception *exception);int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root;
arch/x86/kvm/mmu/mmu.c +13 -13
@@ -4342,7 +4342,7 @@ void kvm_mmu_sync_prev_roots(struct kvm_vcpu *vcpu)kvm_mmu_free_roots (vcpu->kvm, vcpu->arch.mmu, roots_to_free); } -static gpa_t nonpaging_gva_to_gpa (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +static gpa_t nonpaging_gva_to_gpa (struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,gpa_t vaddr,u64 access,struct x86_exception *exception) {
@@ -4354,7 +4354,7 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,* user-mode address if CR0.PG=0. Therefore *include* ACC_USER_MASK in * the last argument to kvm_translate_gpa (which NPT does not use). */ -return kvm_translate_gpa (vcpu, &mmu->w, vaddr, access |PFERR_GUEST_FINAL_MASK , +return kvm_translate_gpa (vcpu, w, vaddr, access |PFERR_GUEST_FINAL_MASK , exception,ACC_ALL ); }
@@ -5119,7 +5119,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_tdp_mmu_map_private_pfn);static void nonpaging_init_context (struct kvm_mmu *context) { context->page_fault = nonpaging_page_fault; - context->gva_to_gpa = nonpaging_gva_to_gpa; + context->w.gva_to_gpa = nonpaging_gva_to_gpa; context->sync_spte =NULL ; }
@@ -5750,14 +5750,14 @@ static void reset_guest_paging_metadata(struct kvm_vcpu *vcpu,static void paging64_init_context (struct kvm_mmu *context) { context->page_fault = paging64_page_fault; - context->gva_to_gpa = paging64_gva_to_gpa; + context->w.gva_to_gpa = paging64_gva_to_gpa; context->sync_spte = paging64_sync_spte; }static void paging32_init_context (struct kvm_mmu *context) { context->page_fault = paging32_page_fault; - context->gva_to_gpa = paging32_gva_to_gpa; + context->w.gva_to_gpa = paging32_gva_to_gpa; context->sync_spte = paging32_sync_spte; }
@@ -5886,11 +5886,11 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->w.get_guest_pgd = get_guest_cr3;if (!is_cr0_pg (context)) - context->gva_to_gpa = nonpaging_gva_to_gpa; + context->w.gva_to_gpa = nonpaging_gva_to_gpa;else if (is_cr4_pae (context)) - context->gva_to_gpa = paging64_gva_to_gpa; + context->w.gva_to_gpa = paging64_gva_to_gpa;else - context->gva_to_gpa = paging32_gva_to_gpa; + context->w.gva_to_gpa = paging32_gva_to_gpa;reset_guest_paging_metadata (vcpu, context);reset_tdp_shadow_zero_bits_mask (context);
@@ -6012,7 +6012,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->root_role.word = new_mode.base.word; context->page_fault = ept_page_fault; - context->gva_to_gpa = ept_gva_to_gpa; + context->w.gva_to_gpa = ept_gva_to_gpa; context->sync_spte = ept_sync_spte;update_permission_bitmask (context, true, true);
@@ -6067,13 +6067,13 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,* the gva_to_gpa functions between mmu and nested_mmu are swapped. */ if (!is_paging (vcpu)) - g_context->gva_to_gpa = nonpaging_gva_to_gpa; + g_context->w.gva_to_gpa = nonpaging_gva_to_gpa;else if (is_long_mode (vcpu)) - g_context->gva_to_gpa = paging64_gva_to_gpa; + g_context->w.gva_to_gpa = paging64_gva_to_gpa;else if (is_pae (vcpu)) - g_context->gva_to_gpa = paging64_gva_to_gpa; + g_context->w.gva_to_gpa = paging64_gva_to_gpa;else - g_context->gva_to_gpa = paging32_gva_to_gpa; + g_context->w.gva_to_gpa = paging32_gva_to_gpa;reset_guest_paging_metadata (vcpu, g_context); }
arch/x86/kvm/mmu/paging_tmpl.h +3 -3
@@ -889,7 +889,7 @@ static gpa_t FNAME(get_level1_sp_gpa)(struct kvm_mmu_page *sp) }/* Note, @addr is a GPA when gva_to_gpa() translates an L2 GPA to an L1 GPA. */ -static gpa_t FNAME (gva_to_gpa )(struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +static gpa_t FNAME (gva_to_gpa )(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,gpa_t addr,u64 access,struct x86_exception *exception) {
@@ -899,10 +899,10 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,#ifndef CONFIG_X86_64 /* A 64-bit GVA should be impossible on 32-bit KVM. */ -WARN_ON_ONCE ((addr >>32 ) && mmu == vcpu->arch.walk_mmu); +WARN_ON_ONCE ((addr >>32 ) && w == &vcpu->arch.walk_mmu->w);#endif - r =FNAME (walk_addr_generic)(&walker, vcpu, &mmu->w, addr, access); + r =FNAME (walk_addr_generic)(&walker, vcpu, w, addr, access);if (r) { gpa =gfn_to_gpa (walker.gfn);
arch/x86/kvm/svm/nested.c +2 -2
@@ -2090,7 +2090,7 @@ static gpa_t svm_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 pte_access) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_mmu *mmu = vcpu->arch.mmu; +struct kvm_pagewalk *w = &vcpu->arch.mmu->w;BUG_ON (!mmu_is_nested (vcpu));
@@ -2098,7 +2098,7 @@ static gpa_t svm_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,if (!(svm->nested.ctl.misc_ctl &SVM_MISC_ENABLE_GMET )) access |=PFERR_USER_MASK ; -return mmu->gva_to_gpa (vcpu, mmu, gpa, access, exception); +return w->gva_to_gpa (vcpu, w, gpa, access, exception); }struct kvm_x86_nested_ops svm_nested_ops = {
arch/x86/kvm/vmx/nested.c +2 -2
@@ -7465,7 +7465,7 @@ static gpa_t vmx_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, struct x86_exception *exception, u64 pte_access) { -struct kvm_mmu *mmu = vcpu->arch.mmu; +struct kvm_pagewalk *w = &vcpu->arch.mmu->w;BUG_ON (!mmu_is_nested (vcpu));
@@ -7477,7 +7477,7 @@ static gpa_t vmx_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,if ((pte_access &ACC_USER_MASK ) && (access &PFERR_GUEST_FINAL_MASK )) access |=PFERR_USER_MASK ; -return mmu->gva_to_gpa (vcpu, mmu, gpa, access, exception); +return w->gva_to_gpa (vcpu, w, gpa, access, exception); }struct kvm_x86_nested_ops vmx_nested_ops = {
arch/x86/kvm/x86.c +15 -15
@@ -7851,21 +7851,21 @@ void kvm_get_segment(struct kvm_vcpu *vcpu,gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; -return mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); +return cpu_walk->gva_to_gpa (vcpu, cpu_walk, gva, access, exception); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_mmu_gva_to_gpa_read);gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; access |=PFERR_WRITE_MASK ; -return mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); +return cpu_walk->gva_to_gpa (vcpu, cpu_walk, gva, access, exception); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_mmu_gva_to_gpa_write);
@@ -7873,21 +7873,21 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_gva_to_gpa_write);gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w; -return mmu->gva_to_gpa (vcpu, mmu, gva,0 , exception); +return cpu_walk->gva_to_gpa (vcpu, cpu_walk, gva,0 , exception); }static int kvm_read_guest_virt_helper (gva_t addr,void *val,unsigned int bytes,struct kvm_vcpu *vcpu,u64 access,struct x86_exception *exception) { -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w;void *data = val;int r =X86EMUL_CONTINUE ;while (bytes) { -gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access, exception); +gpa_t gpa = cpu_walk->gva_to_gpa (vcpu, cpu_walk, addr, access, exception);unsigned offset = addr & (PAGE_SIZE -1 );unsigned toread =min (bytes, (unsigned )PAGE_SIZE - offset);int ret;
@@ -7915,14 +7915,14 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, struct x86_exception *exception) {struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -struct kvm_mmu *mmu = vcpu->arch.walk_mmu; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ;unsigned offset;int ret;/* Inline kvm_read_guest_virt_helper for speed. */ -gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access|PFERR_FETCH_MASK , - exception); +gpa_t gpa = cpu_walk->gva_to_gpa (vcpu, cpu_walk, addr, access|PFERR_FETCH_MASK , + exception);if (unlikely (gpa ==INVALID_GPA ))return X86EMUL_PROPAGATE_FAULT ;
@@ -7974,12 +7974,12 @@ static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes struct kvm_vcpu *vcpu, u64 access, struct x86_exception *exception) { - struct kvm_mmu *mmu = vcpu->arch.walk_mmu; + struct kvm_pagewalk *cpu_walk = &vcpu->arch.walk_mmu->w;void *data = val;int r =X86EMUL_CONTINUE ;while (bytes) { -gpa_t gpa = mmu->gva_to_gpa (vcpu, mmu, addr, access, exception); +gpa_t gpa = cpu_walk->gva_to_gpa (vcpu, cpu_walk, addr, access, exception);unsigned offset = addr & (PAGE_SIZE -1 );unsigned towrite =min (bytes, (unsigned )PAGE_SIZE - offset);int ret;
@@ -8098,7 +8098,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,return 1 ; } - *gpa = mmu->gva_to_gpa (vcpu, mmu, gva, access, exception); + *gpa = mmu->w.gva_to_gpa (vcpu, &mmu->w, gva, access, exception);if (*gpa ==INVALID_GPA )return -1 ;
@@ -14217,7 +14217,7 @@ void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_c (PFERR_WRITE_MASK |PFERR_FETCH_MASK |PFERR_USER_MASK );if (!(error_code &PFERR_PRESENT_MASK ) || - mmu->gva_to_gpa (vcpu, mmu, gva, access, &fault) !=INVALID_GPA ) { + mmu->w.gva_to_gpa (vcpu, &mmu->w, gva, access, &fault) !=INVALID_GPA ) {/* * If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page * tables probably do not match the TLB. Just proceed
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 8/22 KVM: x86/mmu: move get_pdptr to struct kvm_pagewalk
In reply to Paolo BonziniContinue with yet another callback used in FNAME(walk_addr_generic), as another step towards removing container_of() from there.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 8 ++++---- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -478,6 +478,7 @@ struct kvm_page_fault;*/ struct kvm_pagewalk {unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); +u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index);gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,gpa_t gva_or_gpa,u64 access,struct x86_exception *exception);
@@ -486,7 +487,6 @@ struct kvm_pagewalk {struct kvm_mmu {struct kvm_pagewalk w; -u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index);int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);void (*inject_page_fault)(struct kvm_vcpu *vcpu,struct x86_exception *fault);
arch/x86/kvm/mmu/mmu.c +4 -4
@@ -4085,7 +4085,7 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu)*/ if (mmu->cpu_role.base.level ==PT32E_ROOT_LEVEL ) {for (i =0 ; i <4 ; ++i) { - pdptrs[i] = mmu->get_pdptr (vcpu, i); + pdptrs[i] = mmu->w.get_pdptr (vcpu, i);if (!(pdptrs[i] &PT_PRESENT_MASK ))continue ;
@@ -5880,9 +5880,9 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->root_role.word = root_role.word; context->page_fault = kvm_tdp_page_fault; context->sync_spte =NULL ; - context->get_pdptr = kvm_pdptr_read; context->inject_page_fault = kvm_inject_page_fault; + context->w.get_pdptr = kvm_pdptr_read; context->w.get_guest_pgd = get_guest_cr3;if (!is_cr0_pg (context))
@@ -6032,9 +6032,9 @@ static void init_kvm_softmmu(struct kvm_vcpu *vcpu,kvm_init_shadow_mmu (vcpu, cpu_role); + context->w.get_pdptr = kvm_pdptr_read; context->w.get_guest_pgd = get_guest_cr3; - context->get_pdptr = kvm_pdptr_read; context->inject_page_fault = kvm_inject_page_fault; }
@@ -6047,9 +6047,9 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,return ; g_context->cpu_role.as_u64 = new_mode.as_u64; - g_context->get_pdptr = kvm_pdptr_read; g_context->inject_page_fault = kvm_inject_page_fault; + g_context->w.get_pdptr = kvm_pdptr_read; g_context->w.get_guest_pgd = get_guest_cr3;/*
arch/x86/kvm/mmu/paging_tmpl.h +1 -1
@@ -348,7 +348,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,#if PTTYPE ==64 walk_nx_mask =1ULL <<PT64_NX_SHIFT ;if (walker->level ==PT32E_ROOT_LEVEL ) { - pte = mmu->get_pdptr (vcpu, (addr >>30 ) &3 ); + pte = w->get_pdptr (vcpu, (addr >>30 ) &3 );trace_kvm_mmu_paging_element (pte, walker->level);if (!FNAME (is_present_gpte)(mmu, pte)) goto error;
arch/x86/kvm/svm/nested.c +1 -1
@@ -99,8 +99,8 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) svm->nested.ctl.misc_ctl); vcpu->arch.mmu->w.get_guest_pgd = nested_svm_get_tdp_cr3; + vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; - vcpu->arch.mmu->get_pdptr = nested_svm_get_tdp_pdptr; vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; }
arch/x86/kvm/vmx/nested.c +1 -1
@@ -495,9 +495,9 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.guest_mmu;nested_ept_new_eptp (vcpu); vcpu->arch.mmu->w.get_guest_pgd = nested_ept_get_eptp; + vcpu->arch.mmu->w.get_pdptr = kvm_pdptr_read; vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; - vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; }
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 9/22 KVM: x86/mmu: move inject_page_fault to struct kvm_pagewalk
In reply to Paolo BonziniInjection of page faults is also part of accesses to guest page tables. In particular, kvm_inject_emulated_page_fault calls it on walk_mmu. Move it to struct kvm_pagewalk as part of converting walk_mmu to a struct kvm_pagewalk.
--- arch/x86/include/asm/kvm_host.h | 4 ++-- arch/x86/kvm/mmu/mmu.c | 8 +++----- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/x86.c | 4 ++-- 5 files changed, 9 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -2
@@ -479,6 +479,8 @@ struct kvm_page_fault;struct kvm_pagewalk {unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index); +void (*inject_page_fault)(struct kvm_vcpu *vcpu, +struct x86_exception *fault);gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,gpa_t gva_or_gpa,u64 access,struct x86_exception *exception);
@@ -488,8 +490,6 @@ struct kvm_mmu {struct kvm_pagewalk w;int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); -void (*inject_page_fault)(struct kvm_vcpu *vcpu, -struct x86_exception *fault);int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i);struct kvm_mmu_root_info root;
arch/x86/kvm/mmu/mmu.c +3 -5
@@ -5880,8 +5880,8 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->root_role.word = root_role.word; context->page_fault = kvm_tdp_page_fault; context->sync_spte =NULL ; - context->inject_page_fault = kvm_inject_page_fault; + context->w.inject_page_fault = kvm_inject_page_fault; context->w.get_pdptr = kvm_pdptr_read; context->w.get_guest_pgd = get_guest_cr3;
@@ -6032,10 +6032,9 @@ static void init_kvm_softmmu(struct kvm_vcpu *vcpu,kvm_init_shadow_mmu (vcpu, cpu_role); + context->w.inject_page_fault = kvm_inject_page_fault; context->w.get_pdptr = kvm_pdptr_read; context->w.get_guest_pgd = get_guest_cr3; - - context->inject_page_fault = kvm_inject_page_fault; } static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,
@@ -6047,8 +6046,7 @@ static void init_kvm_nested_mmu(struct kvm_vcpu *vcpu,return ; g_context->cpu_role.as_u64 = new_mode.as_u64; - g_context->inject_page_fault = kvm_inject_page_fault; - + g_context->w.inject_page_fault = kvm_inject_page_fault; g_context->w.get_pdptr = kvm_pdptr_read; g_context->w.get_guest_pgd = get_guest_cr3;
arch/x86/kvm/svm/nested.c +1 -1
@@ -101,7 +101,7 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_guest_pgd = nested_svm_get_tdp_cr3; vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; - vcpu->arch.mmu->inject_page_fault = nested_svm_inject_npf_exit; + vcpu->arch.mmu->w.inject_page_fault = nested_svm_inject_npf_exit; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; }
arch/x86/kvm/vmx/nested.c +1 -1
@@ -497,7 +497,7 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_guest_pgd = nested_ept_get_eptp; vcpu->arch.mmu->w.get_pdptr = kvm_pdptr_read; - vcpu->arch.mmu->inject_page_fault = nested_ept_inject_page_fault; + vcpu->arch.mmu->w.inject_page_fault = nested_ept_inject_page_fault; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; }
arch/x86/kvm/x86.c +2 -2
@@ -1005,7 +1005,7 @@ void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,kvm_mmu_invalidate_addr (vcpu, fault_mmu, fault->address,KVM_MMU_ROOT_CURRENT ); - fault_mmu->inject_page_fault (vcpu, fault); + fault_mmu->w.inject_page_fault (vcpu, fault); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_inject_emulated_page_fault);
@@ -14230,7 +14230,7 @@ void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_c fault.address = gva; fault.async_page_fault = false; } - vcpu->arch.walk_mmu->inject_page_fault (vcpu, &fault); + vcpu->arch.walk_mmu->w.inject_page_fault (vcpu, &fault); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_fixup_and_inject_pf_error);
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 10/22 KVM: x86/mmu: move CPU-related fields to struct kvm_pagewalk
In reply to Paolo Bonzinistruct kvm_pagewalk's behavior depends on the CPU state and its page format. Move related fields so that walk_mmu remains self contained.
Note that for now, some of the accessors still use kvm_mmu to split the churn.
--- arch/x86/include/asm/kvm_host.h | 4 +-- arch/x86/kvm/mmu/mmu.c | 52 ++++++++++++++++----------------- arch/x86/kvm/mmu/paging_tmpl.h | 40 ++++++++++++------------- 3 files changed, 46 insertions(+), 50 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -2
@@ -484,6 +484,8 @@ struct kvm_pagewalk {gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,gpa_t gva_or_gpa,u64 access,struct x86_exception *exception); +union kvm_cpu_role cpu_role; +struct rsvd_bits_validate guest_rsvd_check; };struct kvm_mmu {
@@ -494,7 +496,6 @@ struct kvm_mmu { struct kvm_mmu_page *sp, int i);struct kvm_mmu_root_info root;hpa_t mirror_root_hpa; -union kvm_cpu_role cpu_role;union kvm_mmu_page_role root_role;/*
@@ -524,7 +525,6 @@ struct kvm_mmu {* the bits spte never used. */ struct rsvd_bits_validate shadow_zero_check; -struct rsvd_bits_validate guest_rsvd_check; };enum pmc_type {
arch/x86/kvm/mmu/mmu.c +26 -26
172 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +18 -22
@@ -55,7 +55,7 @@#define PT_LEVEL_BITS 9#define PT_GUEST_DIRTY_SHIFT 9#define PT_GUEST_ACCESSED_SHIFT 8 -#define PT_HAVE_ACCESSED_DIRTY (mmu) (!(mmu)->cpu_role.base.ad_disabled) +#define PT_HAVE_ACCESSED_DIRTY (w) (!(w)->cpu_role.base.ad_disabled)#define PT_MAX_FULL_LEVELS PT64_ROOT_MAX_LEVEL#else #error Invalid PTTYPE value
@@ -109,11 +109,10 @@ static gfn_t gpte_to_gfn_lvl(pt_element_t gpte, int lvl)static inline void FNAME (protect_clean_gpte )(struct kvm_pagewalk *w,unsigned *access,unsigned gpte) { - struct kvm_mmu __maybe_unused *mmu = container_of(w, struct kvm_mmu, w);unsigned mask;/* dirty bit is not supported, so no need to track it */ -if (!PT_HAVE_ACCESSED_DIRTY (mmu)) +if (!PT_HAVE_ACCESSED_DIRTY (w))return ;BUILD_BUG_ON (PT_WRITABLE_MASK !=ACC_WRITE_MASK );
@@ -125,7 +124,7 @@ static inline void FNAME(protect_clean_gpte)(struct kvm_pagewalk *w, unsigned *a *access &= mask; } -static inline int FNAME (is_present_gpte )(struct kvm_mmu *mmu, +static inline int FNAME (is_present_gpte )(struct kvm_pagewalk *w,unsigned long pte) {#if PTTYPE !=PTTYPE_EPT
@@ -135,7 +134,7 @@ static inline int FNAME(is_present_gpte)(struct kvm_mmu *mmu,* For EPT, an entry is present if any of bits 2:0 are set. * With mode-based execute control, bit 10 also indicates presence. */ -return pte & (7 | (mmu_has_mbec (mmu) ?VMX_EPT_USER_EXECUTABLE_MASK :0 )); +return pte & (7 | (w->cpu_role.base.cr4_smep ?VMX_EPT_USER_EXECUTABLE_MASK :0 ));#endif }
@@ -150,25 +149,25 @@ static bool FNAME(is_bad_mt_xwr)(struct rsvd_bits_validate *rsvd_check, u64 gptestatic bool FNAME (is_rsvd_bits_set )(struct kvm_pagewalk *w,u64 gpte,int level) { - struct kvm_mmu *mmu = container_of(w, struct kvm_mmu, w); - -return __is_rsvd_bits_set (&mmu->guest_rsvd_check, gpte, level) || -FNAME (is_bad_mt_xwr)(&mmu->guest_rsvd_check, gpte); +return __is_rsvd_bits_set (&w->guest_rsvd_check, gpte, level) || +FNAME (is_bad_mt_xwr)(&w->guest_rsvd_check, gpte); }static bool FNAME (prefetch_invalid_gpte )(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,u64 *spte,u64 gpte) { -if (!FNAME (is_present_gpte)(vcpu->arch.mmu, gpte)) +struct kvm_pagewalk *w = &vcpu->arch.mmu->w; + +if (!FNAME (is_present_gpte)(w, gpte)) goto no_present;/* Prefetch only accessed entries (unless A/D bits are disabled). */ -if (PT_HAVE_ACCESSED_DIRTY (vcpu->arch.mmu) && +if (PT_HAVE_ACCESSED_DIRTY (w) && !(gpte &PT_GUEST_ACCESSED_MASK )) goto no_present; -if (FNAME (is_rsvd_bits_set)(&vcpu->arch.mmu->w, gpte,PG_LEVEL_4K )) +if (FNAME (is_rsvd_bits_set)(w, gpte,PG_LEVEL_4K )) goto no_present;return false;
@@ -213,7 +212,6 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,struct guest_walker *walker, gpa_t addr, int write_fault) { - struct kvm_mmu __maybe_unused *mmu = container_of(w, struct kvm_mmu, w); unsigned level, index;pt_element_t pte, orig_pte;pt_element_t __user *ptep_user;
@@ -221,7 +219,7 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu,int ret;/* dirty/accessed bits are not supported, so no need to update them */ -if (!PT_HAVE_ACCESSED_DIRTY (mmu)) +if (!PT_HAVE_ACCESSED_DIRTY (w))return 0 ;for (level = walker->max_level; level >= walker->level; --level) {
@@ -285,8 +283,6 @@ static inline unsigned FNAME(gpte_pkeys)(struct kvm_vcpu *vcpu, u64 gpte)static inline bool FNAME (is_last_gpte )(struct kvm_pagewalk *w,unsigned int level,unsigned int gpte) { - struct kvm_mmu __maybe_unused *mmu = container_of(w, struct kvm_mmu, w); -/* * For EPT and PAE paging (both variants), bit 7 is either reserved at * all level or indicates a huge page (ignoring CR3/EPTP). In either
@@ -302,7 +298,7 @@ static inline bool FNAME(is_last_gpte)(struct kvm_pagewalk *w,* is not reserved and does not indicate a large page at this level, * so clear PT_PAGE_SIZE_MASK in gpte if that is the case. */ - gpte &= level - (PT32_ROOT_LEVEL + mmu->cpu_role.ext.cr4_pse); + gpte &= level - (PT32_ROOT_LEVEL + w->cpu_role.ext.cr4_pse);#endif /* * PG_LEVEL_4K always terminates. The RHS has bit 7 set
@@ -341,16 +337,16 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,trace_kvm_mmu_pagetable_walk (addr, access); retry_walk: - walker->level = mmu->cpu_role.base.level; + walker->level = w->cpu_role.base.level; pte =kvm_mmu_get_guest_pgd (vcpu, w); - have_ad =PT_HAVE_ACCESSED_DIRTY (mmu); + have_ad =PT_HAVE_ACCESSED_DIRTY (w);#if PTTYPE ==64 walk_nx_mask =1ULL <<PT64_NX_SHIFT ;if (walker->level ==PT32E_ROOT_LEVEL ) { pte = w->get_pdptr (vcpu, (addr >>30 ) &3 );trace_kvm_mmu_paging_element (pte, walker->level); -if (!FNAME (is_present_gpte)(mmu, pte)) +if (!FNAME (is_present_gpte)(w, pte)) goto error; --walker->level; }
@@ -433,7 +429,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,*/ pte_access = pt_access & (pte ^ walk_nx_mask); -if (unlikely (!FNAME (is_present_gpte)(mmu, pte))) +if (unlikely (!FNAME (is_present_gpte)(w, pte))) goto error;if (unlikely (FNAME (is_rsvd_bits_set)(w, pte, walker->level))) {
@@ -655,7 +651,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,WARN_ON_ONCE (gw->gfn != base_gfn); direct_access = gw->pte_access; - top_level = vcpu->arch.mmu->cpu_role.base.level; + top_level = vcpu->arch.mmu->w.cpu_role.base.level;if (top_level ==PT32E_ROOT_LEVEL ) top_level =PT32_ROOT_LEVEL ;/*
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 11/22 KVM: x86/mmu: change CPU-role accessor fields to take struct kvm_pagewalk
In reply to Paolo BonziniWith this change, walk_addr_generic and its callees do not need to use container_of() anymore.
The next step is removing it from permission_fault() and kvm_mmu_refresh_passthrough_bits().
--- arch/x86/kvm/mmu/mmu.c | 44 +++++++++++++++++----------------- arch/x86/kvm/mmu/paging_tmpl.h | 11 ++++----- 2 files changed, 27 insertions(+), 28 deletions(-)
arch/x86/kvm/mmu/mmu.c +22 -22
@@ -224,9 +224,9 @@ BUILD_MMU_ROLE_REGS_ACCESSOR(efer, lma, EFER_LMA);* and the vCPU may be incorrect/irrelevant. */ #define BUILD_MMU_ROLE_ACCESSOR (base_or_ext, reg, name) \ -static inline bool __maybe_unused is_##reg##_##name(struct kvm_mmu *mmu) \ +static inline bool __maybe_unused is_##reg##_##name(struct kvm_pagewalk *w) \ { \ - return !!(mmu->w.cpu_role. base_or_ext . reg##_##name); \ + return !!(w->cpu_role. base_or_ext . reg##_##name); \ }BUILD_MMU_ROLE_ACCESSOR (base, cr0, wp);BUILD_MMU_ROLE_ACCESSOR (ext, cr4, pse);
@@ -237,19 +237,19 @@ BUILD_MMU_ROLE_ACCESSOR(ext, cr4, la57);BUILD_MMU_ROLE_ACCESSOR (base, efer, nx);BUILD_MMU_ROLE_ACCESSOR (ext, efer, lma); -static inline bool has_pferr_fetch (struct kvm_mmu *mmu) +static inline bool has_pferr_fetch (struct kvm_pagewalk *w) { -return mmu->w.cpu_role.ext.has_pferr_fetch; +return w->cpu_role.ext.has_pferr_fetch; } -static inline bool is_cr0_pg (struct kvm_mmu *mmu) +static inline bool is_cr0_pg (struct kvm_pagewalk *w) { -return mmu->w.cpu_role.base.level >0 ; +return w->cpu_role.base.level >0 ; } -static inline bool is_cr4_pae (struct kvm_mmu *mmu) +static inline bool is_cr4_pae (struct kvm_pagewalk *w) { -return !mmu->w.cpu_role.base.has_4_byte_gpte; +return !w->cpu_role.base.has_4_byte_gpte; }static struct kvm_mmu_role_regs vcpu_to_role_regs (struct kvm_vcpu *vcpu)
@@ -5389,9 +5389,9 @@ static void reset_guest_rsvds_bits_mask(struct kvm_vcpu *vcpu, {__reset_rsvds_bits_mask (&context->w.guest_rsvd_check, vcpu->arch.reserved_gpa_bits, - context->w.cpu_role.base.level,is_efer_nx (context), + context->w.cpu_role.base.level,is_efer_nx (&context->w),guest_cpu_cap_has (vcpu,X86_FEATURE_GBPAGES ), -is_cr4_pse (context), +is_cr4_pse (&context->w),guest_cpuid_is_amd_compatible (vcpu)); }
@@ -5573,10 +5573,10 @@ static void update_permission_bitmask(struct kvm_mmu *mmu, bool tdp, bool ept)const u16 w =ACC_BITS_MASK (ACC_WRITE_MASK );const u16 r =ACC_BITS_MASK (ACC_READ_MASK ); -bool cr4_smep =is_cr4_smep (mmu); -bool cr4_smap =is_cr4_smap (mmu); -bool cr0_wp =is_cr0_wp (mmu); -bool efer_nx =is_efer_nx (mmu); +bool cr4_smep =is_cr4_smep (&mmu->w); +bool cr4_smap =is_cr4_smap (&mmu->w); +bool cr0_wp =is_cr0_wp (&mmu->w); +bool efer_nx =is_efer_nx (&mmu->w);/* * In hardware, page fault error codes are generated (as the name
@@ -5699,10 +5699,10 @@ static void update_pkru_bitmask(struct kvm_mmu *mmu) mmu->pkru_mask =0 ; -if (!is_cr4_pke (mmu)) +if (!is_cr4_pke (&mmu->w))return ; - wp =is_cr0_wp (mmu); + wp =is_cr0_wp (&mmu->w);for (bit =0 ; bit <ARRAY_SIZE (mmu->permissions); ++bit) {unsigned pfec, pkey_bits;
@@ -5739,7 +5739,7 @@ static void update_pkru_bitmask(struct kvm_mmu *mmu)static void reset_guest_paging_metadata (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu) { -if (!is_cr0_pg (mmu)) +if (!is_cr0_pg (&mmu->w))return ;reset_guest_rsvds_bits_mask (vcpu, mmu);
@@ -5810,7 +5810,7 @@ void __kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,BUILD_BUG_ON ((KVM_MMU_CR0_ROLE_BITS &KVM_POSSIBLE_CR0_GUEST_BITS ) !=X86_CR0_WP );BUILD_BUG_ON ((KVM_MMU_CR4_ROLE_BITS &KVM_POSSIBLE_CR4_GUEST_BITS )); -if (is_cr0_wp (mmu) == cr0_wp) +if (is_cr0_wp (&mmu->w) == cr0_wp)return ; mmu->w.cpu_role.base.cr0_wp = cr0_wp;
@@ -5885,9 +5885,9 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->w.get_pdptr = kvm_pdptr_read; context->w.get_guest_pgd = get_guest_cr3; -if (!is_cr0_pg (context)) +if (!is_cr0_pg (&context->w)) context->w.gva_to_gpa = nonpaging_gva_to_gpa; -else if (is_cr4_pae (context)) +else if (is_cr4_pae (&context->w)) context->w.gva_to_gpa = paging64_gva_to_gpa;else context->w.gva_to_gpa = paging32_gva_to_gpa;
@@ -5907,9 +5907,9 @@ static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *conte context->w.cpu_role.as_u64 = cpu_role.as_u64; context->root_role.word = root_role.word; -if (!is_cr0_pg (context)) +if (!is_cr0_pg (&context->w))nonpaging_init_context (context); -else if (is_cr4_pae (context)) +else if (is_cr4_pae (&context->w))paging64_init_context (context);else paging32_init_context (context);
arch/x86/kvm/mmu/paging_tmpl.h +5 -6
@@ -134,7 +134,7 @@ static inline int FNAME(is_present_gpte)(struct kvm_pagewalk *w,* For EPT, an entry is present if any of bits 2:0 are set. * With mode-based execute control, bit 10 also indicates presence. */ -return pte & (7 | (w->cpu_role.base.cr4_smep ?VMX_EPT_USER_EXECUTABLE_MASK :0 )); +return pte & (7 | (is_cr4_smep (w) ?VMX_EPT_USER_EXECUTABLE_MASK :0 ));#endif }
@@ -316,7 +316,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, struct kvm_vcpu *vcpu, struct kvm_pagewalk *w, gpa_t addr, u64 access) { - struct kvm_mmu *mmu = container_of(w, struct kvm_mmu, w); int ret;pt_element_t pte;pt_element_t __user *ptep_user;
@@ -492,7 +491,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, error: errcode |= write_fault | user_fault; -if (fetch_fault &&has_pferr_fetch (mmu)) +if (fetch_fault &&has_pferr_fetch (w)) errcode |=PFERR_FETCH_MASK ; walker->fault.vector =PF_VECTOR ;
@@ -536,7 +535,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,* ACC_*_MASK flags! */ walker->fault.exit_qualification |=EPT_VIOLATION_RWX_TO_PROT (pte_access); -if (mmu_has_mbec (mmu)) +if (is_cr4_smep (w)) walker->fault.exit_qualification |=EPT_VIOLATION_USER_EXEC_TO_PROT (pte_access); }
@@ -840,7 +839,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* otherwise KVM will cache incorrect access information in the SPTE. */ if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (vcpu->arch.mmu) && !fault->user && fault->slot) { + !is_cr0_wp (&vcpu->arch.mmu->w) && !fault->user && fault->slot) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -850,7 +849,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* then we should prevent the kernel from executing it * if SMEP is enabled. */ -if (is_cr4_smep (vcpu->arch.mmu)) +if (is_cr4_smep (&vcpu->arch.mmu->w)) walker.pte_access &= ~ACC_EXEC_MASK; }#endif
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 12/22 KVM: x86/mmu: move remaining permission fields to struct kvm_pagewalk
In reply to Paolo BonziniAs promised, this removes the remaining instances of container_of(w, struct kvm_mmu, w).
--- arch/x86/include/asm/kvm_host.h | 30 ++++++++-------- arch/x86/kvm/mmu.h | 13 +++---- arch/x86/kvm/mmu/mmu.c | 62 ++++++++++++++++----------------- 3 files changed, 51 insertions(+), 54 deletions(-)
arch/x86/include/asm/kvm_host.h +15 -15
@@ -486,6 +486,21 @@ struct kvm_pagewalk { struct x86_exception *exception);union kvm_cpu_role cpu_role;struct rsvd_bits_validate guest_rsvd_check; + +/* +* The pkru_mask indicates if protection key checks are needed. It +* consists of 16 domains indexed by page fault error code bits [4:1], +* with PFEC.RSVD replaced by ACC_USER_MASK from the page tables. +* Each domain has 2 bits which are ANDed with AD and WD from PKRU. +*/ +u32 pkru_mask; + +/* +* Bitmap; bit set = permission fault +* Array index: page fault error code [4:1] +* Bit index: pte permissions in ACC_* format +*/ +u16 permissions[16 ]; };struct kvm_mmu {
@@ -498,23 +513,8 @@ struct kvm_mmu {hpa_t mirror_root_hpa;union kvm_mmu_page_role root_role; -/* -* The pkru_mask indicates if protection key checks are needed. It -* consists of 16 domains indexed by page fault error code bits [4:1], -* with PFEC.RSVD replaced by ACC_USER_MASK from the page tables. -* Each domain has 2 bits which are ANDed with AD and WD from PKRU. -*/ -u32 pkru_mask; -struct kvm_mmu_root_info prev_roots[KVM_MMU_NUM_PREV_ROOTS ]; -/* -* Bitmap; bit set = permission fault -* Byte index: page fault error code [4:1] -* Bit index: pte permissions in ACC_* format -*/ -u16 permissions[16 ]; -u64 *pae_root;u64 *pml4_root;u64 *pml5_root;
arch/x86/kvm/mmu.h +5 -8
@@ -105,7 +105,7 @@ bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu);int kvm_handle_page_fault (struct kvm_vcpu *vcpu,u64 error_code,u64 fault_address,char *insn,int insn_len);void __kvm_mmu_refresh_passthrough_bits (struct kvm_vcpu *vcpu, -struct kvm_mmu *mmu); +struct kvm_pagewalk *pw);int kvm_mmu_load (struct kvm_vcpu *vcpu);void kvm_mmu_unload (struct kvm_vcpu *vcpu);
@@ -183,8 +183,7 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,if (!tdp_enabled || w == &vcpu->arch.guest_mmu.w)return ; -__kvm_mmu_refresh_passthrough_bits (vcpu, - container_of(w, struct kvm_mmu, w)); +__kvm_mmu_refresh_passthrough_bits (vcpu, w); }/*
@@ -199,8 +198,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w, unsigned pte_access, unsigned pte_pkey, u64 access) { - struct kvm_mmu *mmu = container_of(w, struct kvm_mmu, w); - /* strip nested paging fault error codes */ unsigned int pfec = access;unsigned long rflags =kvm_x86_call (get_rflags)(vcpu);
@@ -225,10 +222,10 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,kvm_mmu_refresh_passthrough_bits (vcpu, w); - fault = (mmu->permissions[index] >> pte_access) &1 ; + fault = (w->permissions[index] >> pte_access) &1 ;WARN_ON_ONCE (pfec & (PFERR_PK_MASK |PFERR_SS_MASK |PFERR_RSVD_MASK )); -if (unlikely (mmu->pkru_mask)) { +if (unlikely (w->pkru_mask)) {u32 pkru_bits, offset;/*
@@ -242,7 +239,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ offset = (pfec & ~1 ) | ((pte_access &PT_USER_MASK ) ?PFERR_RSVD_MASK :0 ); - pkru_bits &= mmu->pkru_mask >> offset; + pkru_bits &= w->pkru_mask >> offset; errcode |= -pkru_bits &PFERR_PK_MASK ; fault |= (pkru_bits !=0 ); }
arch/x86/kvm/mmu/mmu.c +31 -31
160 lines not on this page · raw message
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 13/22 KVM: x86/mmu: pass struct kvm_pagewalk to kvm_mmu_invalidate_addr
In reply to Paolo Bonzinikvm_mmu_invalidate_addr only needs to know if what's being invalidated is a GVA or GPA. This will ultimately be represented by two different kvm_pagewalk structs, so adjust the type of the parameter.
For now the GVA case is represented by both root_mmu and nested_mmu. Since nested_mmu never has a sync_spte callback, it would exit at its check; but really nested_mmu should not be a kvm_mmu in the first place and the container_of() would be bogus, so introduce a separate check for whether the invalidation is happening for a nested GVA. In that case there's nothing needed beyond kvm_x86_call(flush_tlb_gva).
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/mmu/mmu.c | 12 ++++++++---- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/x86.c | 2 +- 4 files changed, 11 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -2384,7 +2384,7 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, void *insn, int insn_len);void kvm_mmu_print_sptes (struct kvm_vcpu *vcpu,gpa_t gpa,const char *msg);void kvm_mmu_invlpg (struct kvm_vcpu *vcpu,gva_t gva); -void kvm_mmu_invalidate_addr (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +void kvm_mmu_invalidate_addr (struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,u64 addr,unsigned long roots);void kvm_mmu_invpcid_gva (struct kvm_vcpu *vcpu,gva_t gva,unsigned long pcid);void kvm_mmu_new_pgd (struct kvm_vcpu *vcpu,gpa_t new_pgd);
arch/x86/kvm/mmu/mmu.c +8 -4
@@ -6596,22 +6596,26 @@ static void __kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmuwrite_unlock (&vcpu->kvm->mmu_lock); } -void kvm_mmu_invalidate_addr (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu, +void kvm_mmu_invalidate_addr (struct kvm_vcpu *vcpu,struct kvm_pagewalk *w,u64 addr,unsigned long roots) { +struct kvm_mmu *mmu;int i;WARN_ON_ONCE (roots & ~KVM_MMU_ROOTS_ALL );/* It's actually a GPA for vcpu->arch.guest_mmu. */ -if (mmu != &vcpu->arch.guest_mmu) { +if (w != &vcpu->arch.guest_mmu.w) {/* INVLPG on a non-canonical address is a NOP according to the SDM. */ if (is_noncanonical_invlpg_address (addr, vcpu))return ;kvm_x86_call (flush_tlb_gva)(vcpu, addr); +if (w == &vcpu->arch.nested_mmu.w) +return ; } + mmu = container_of(w, struct kvm_mmu, w);if (!mmu->sync_spte)return ;
@@ -6637,7 +6641,7 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)* be synced when switching to that new cr3, so nothing needs to be * done here for them. */ -kvm_mmu_invalidate_addr (vcpu, vcpu->arch.walk_mmu, gva,KVM_MMU_ROOTS_ALL ); +kvm_mmu_invalidate_addr (vcpu, &vcpu->arch.walk_mmu->w, gva,KVM_MMU_ROOTS_ALL ); ++vcpu->stat.invlpg; }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_mmu_invlpg);
@@ -6659,7 +6663,7 @@ void kvm_mmu_invpcid_gva(struct kvm_vcpu *vcpu, gva_t gva, unsigned long pcid) }if (roots) -kvm_mmu_invalidate_addr (vcpu, mmu, gva, roots); +kvm_mmu_invalidate_addr (vcpu, &mmu->w, gva, roots); ++vcpu->stat.invlpg;/*
arch/x86/kvm/vmx/nested.c +1 -1
@@ -407,7 +407,7 @@ static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, roots |=KVM_MMU_ROOT_PREVIOUS (i); }if (roots) -kvm_mmu_invalidate_addr (vcpu, vcpu->arch.mmu, addr, roots); +kvm_mmu_invalidate_addr (vcpu, &vcpu->arch.guest_mmu.w, addr, roots); } static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
arch/x86/kvm/x86.c +1 -1
@@ -1002,7 +1002,7 @@ void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,*/ if ((fault->error_code &PFERR_PRESENT_MASK ) && !(fault->error_code &PFERR_RSVD_MASK )) -kvm_mmu_invalidate_addr (vcpu, fault_mmu, fault->address, +kvm_mmu_invalidate_addr (vcpu, &fault_mmu->w, fault->address,KVM_MMU_ROOT_CURRENT ); fault_mmu->w.inject_page_fault (vcpu, fault);
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 14/22 KVM: x86/mmu: change walk_mmu to struct kvm_pagewalk
In reply to Paolo BonziniNow that walk_mmu is only accessed for its "w" member, store directly the pointer to it. This also means that nested_mmu is only accessed for its "w" member.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/mmu/mmu.c | 4 +-- arch/x86/kvm/mmu/paging_tmpl.h | 4 +-- arch/x86/kvm/svm/nested.c | 4 +-- arch/x86/kvm/vmx/nested.c | 4 +-- arch/x86/kvm/x86.c | 44 +++++++++++++++++---------------- 7 files changed, 33 insertions(+), 31 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -879,7 +879,7 @@ struct kvm_vcpu_arch {* Pointer to the mmu context currently used for * gva_to_gpa translations. */ -struct kvm_mmu *walk_mmu; +struct kvm_pagewalk *cpu_walk;u64 pdptrs[4 ];/* pae */
arch/x86/kvm/hyperv.c +1 -1
@@ -2041,7 +2041,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)* read with kvm_read_guest(). */ if (!hc->fast) { - hc->ingpa =kvm_translate_gpa (vcpu, &vcpu->arch.walk_mmu->w, hc->ingpa, + hc->ingpa =kvm_translate_gpa (vcpu, vcpu->arch.cpu_walk, hc->ingpa,PFERR_GUEST_FINAL_MASK ,NULL ,0 );if (unlikely (hc->ingpa ==INVALID_GPA ))return HV_STATUS_INVALID_HYPERCALL_INPUT ;
arch/x86/kvm/mmu/mmu.c +2 -2
@@ -6641,7 +6641,7 @@ void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva)* be synced when switching to that new cr3, so nothing needs to be * done here for them. */ -kvm_mmu_invalidate_addr (vcpu, &vcpu->arch.walk_mmu->w, gva,KVM_MMU_ROOTS_ALL ); +kvm_mmu_invalidate_addr (vcpu, vcpu->arch.cpu_walk, gva,KVM_MMU_ROOTS_ALL ); ++vcpu->stat.invlpg; }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_mmu_invlpg);
@@ -6778,7 +6778,7 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; + vcpu->arch.cpu_walk = &vcpu->arch.root_mmu.w; ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu);if (ret)
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
@@ -541,7 +541,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, }#endif walker->fault.address = addr; - walker->fault.nested_page_fault = w != &vcpu->arch.walk_mmu->w; + walker->fault.nested_page_fault = w != vcpu->arch.cpu_walk; walker->fault.async_page_fault = false;trace_kvm_mmu_walker_error (walker->fault.error_code);
@@ -894,7 +894,7 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,#ifndef CONFIG_X86_64 /* A 64-bit GVA should be impossible on 32-bit KVM. */ -WARN_ON_ONCE ((addr >>32 ) && w == &vcpu->arch.walk_mmu->w); +WARN_ON_ONCE ((addr >>32 ) && w == vcpu->arch.cpu_walk);#endif r =FNAME (walk_addr_generic)(&walker, vcpu, w, addr, access);
arch/x86/kvm/svm/nested.c +2 -2
@@ -102,13 +102,13 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; vcpu->arch.mmu->w.inject_page_fault = nested_svm_inject_npf_exit; - vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; + vcpu->arch.cpu_walk = &vcpu->arch.nested_mmu.w; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu) { vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; + vcpu->arch.cpu_walk = &vcpu->arch.root_mmu.w; }static bool nested_vmcb_needs_vls_intercept (struct vcpu_svm *svm)
arch/x86/kvm/vmx/nested.c +2 -2
@@ -499,13 +499,13 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.inject_page_fault = nested_ept_inject_page_fault; - vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; + vcpu->arch.cpu_walk = &vcpu->arch.nested_mmu.w; }static void nested_ept_uninit_mmu_context (struct kvm_vcpu *vcpu) { vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; + vcpu->arch.cpu_walk = &vcpu->arch.root_mmu.w; } static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
arch/x86/kvm/x86.c +23 -21
150 lines not on this page · raw message
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 15/22 KVM: x86/mmu: change nested_mmu.w to nested_cpu_walk
In reply to Paolo Bonzininested_mmu is now only used for its w member. Rename it, and change its type.
--- arch/x86/include/asm/kvm_host.h | 5 ++-- arch/x86/kvm/mmu.h | 6 ++--- arch/x86/kvm/mmu/mmu.c | 41 ++++++++++++++------------------- arch/x86/kvm/svm/nested.c | 2 +- arch/x86/kvm/vmx/nested.c | 2 +- 5 files changed, 24 insertions(+), 32 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -3
@@ -873,11 +873,10 @@ struct kvm_vcpu_arch {* walking and not for faulting since we never handle l2 page faults on * the host. */ -struct kvm_mmu nested_mmu; +struct kvm_pagewalk nested_cpu_walk;/* -* Pointer to the mmu context currently used for -* gva_to_gpa translations. +* Pagewalk context used for gva_to_gpa translations. */ struct kvm_pagewalk *cpu_walk;
arch/x86/kvm/mmu.h +3 -3
@@ -177,8 +177,8 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,* be stale. Refresh CR0.WP and the metadata on-demand when checking * for permission faults. Exempt nested MMUs, i.e. MMUs for shadowing * nEPT and nNPT, as CR0.WP is ignored in both cases. Note, KVM does -* need to refresh nested_mmu, a.k.a. the walker used to translate L2 -* GVAs to GPAs, as that "MMU" needs to honor L2's CR0.WP. +* need to refresh nested_cpu_walk, a.k.a. the walker used to translate L2 +* GVAs to GPAs, so as to honor L2's CR0.WP. */ if (!tdp_enabled || w == &vcpu->arch.guest_mmu.w)return ;
@@ -306,7 +306,7 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, struct x86_exception *exception, u64 pte_access) { -if (w != &vcpu->arch.nested_mmu.w) +if (w != &vcpu->arch.nested_cpu_walk)return gpa; return kvm_x86_ops.nested_ops->translate_nested_gpa(vcpu, gpa, access, exception,
arch/x86/kvm/mmu/mmu.c +17 -24
84 lines not on this page · raw message
arch/x86/kvm/svm/nested.c +1 -1
@@ -102,7 +102,7 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; vcpu->arch.mmu->w.inject_page_fault = nested_svm_inject_npf_exit; - vcpu->arch.cpu_walk = &vcpu->arch.nested_mmu.w; + vcpu->arch.cpu_walk = &vcpu->arch.nested_cpu_walk; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu)
arch/x86/kvm/vmx/nested.c +1 -1
@@ -499,7 +499,7 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.inject_page_fault = nested_ept_inject_page_fault; - vcpu->arch.cpu_walk = &vcpu->arch.nested_mmu.w; + vcpu->arch.cpu_walk = &vcpu->arch.nested_cpu_walk; }static void nested_ept_uninit_mmu_context (struct kvm_vcpu *vcpu)
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 16/22 KVM: x86/mmu: make cpu_walk a value
In reply to Paolo BonziniAlways use the same instance of kvm_pagewalk to do GVA->GPA translations, instead of flipping the cpu_walk pointer back and forth. After all the page walking does behave the same no matter if you are in guest mode or not; the difference lies in the behavior of kvm_translate_gpa and thus in vcpu->arch.mmu, not in the page walker itself.
At this point, vcpu->arch.cpu_walk and vcpu->arch.root_mmu.w contain the same information (at least when KVM is not running a nested guest, i.e. when root_mmu is actually in use); compare init_kvm_page_walk() on one side with init_kvm_softmmu() + shadow_mmu_init_context() on the other. root_mmu.w is still used by shadow paging, via FNAME(walk_addr) and its callers.
vcpu->arch.guest_mmu.w instead is used for both guest emulation (kvm_translate_gpa) and shadow paging.
--- arch/x86/include/asm/kvm_host.h | 12 +---- arch/x86/kvm/hyperv.c | 2 +- arch/x86/kvm/mmu.h | 8 +-- arch/x86/kvm/mmu/mmu.c | 86 +++++++++++++++------------------ arch/x86/kvm/mmu/paging_tmpl.h | 4 +- arch/x86/kvm/svm/nested.c | 2 - arch/x86/kvm/vmx/nested.c | 3 -- arch/x86/kvm/x86.c | 20 ++++---- 8 files changed, 58 insertions(+), 79 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -11
@@ -865,20 +865,10 @@ struct kvm_vcpu_arch {/* L1 MMU when running nested */ struct kvm_mmu guest_mmu; -/* -* Paging state of an L2 guest (used for nested npt) -* -* This context will save all necessary information to walk page tables -* of an L2 guest. This context is only initialized for page table -* walking and not for faulting since we never handle l2 page faults on -* the host. -*/ -struct kvm_pagewalk nested_cpu_walk; -/* * Pagewalk context used for gva_to_gpa translations. */ -struct kvm_pagewalk *cpu_walk; +struct kvm_pagewalk cpu_walk;u64 pdptrs[4 ];/* pae */
arch/x86/kvm/hyperv.c +1 -1
@@ -2041,7 +2041,7 @@ static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)* read with kvm_read_guest(). */ if (!hc->fast) { - hc->ingpa =kvm_translate_gpa (vcpu, vcpu->arch.cpu_walk, hc->ingpa, + hc->ingpa =kvm_translate_gpa (vcpu, &vcpu->arch.cpu_walk, hc->ingpa,PFERR_GUEST_FINAL_MASK ,NULL ,0 );if (unlikely (hc->ingpa ==INVALID_GPA ))return HV_STATUS_INVALID_HYPERCALL_INPUT ;
arch/x86/kvm/mmu.h +4 -4
@@ -176,9 +176,9 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,* @w's snapshot of CR0.WP and thus all related paging metadata may * be stale. Refresh CR0.WP and the metadata on-demand when checking * for permission faults. Exempt nested MMUs, i.e. MMUs for shadowing -* nEPT and nNPT, as CR0.WP is ignored in both cases. Note, KVM does -* need to refresh nested_cpu_walk, a.k.a. the walker used to translate L2 -* GVAs to GPAs, so as to honor L2's CR0.WP. +* nEPT and nNPT, as CR0.WP is ignored in both cases. Note, KVM will +* still refresh cpu_walk, so as to honor L2's CR0.WP when translating +* L2 GVAs to GPAs. */ if (!tdp_enabled || w == &vcpu->arch.guest_mmu.w)return ;
@@ -306,7 +306,7 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, struct x86_exception *exception, u64 pte_access) { -if (w != &vcpu->arch.nested_cpu_walk) +if (!mmu_is_nested (vcpu) || w == &vcpu->arch.guest_mmu.w)return gpa; return kvm_x86_ops.nested_ops->translate_nested_gpa(vcpu, gpa, access, exception,
arch/x86/kvm/mmu/mmu.c +40 -46
141 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +2 -2
@@ -541,7 +541,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, }#endif walker->fault.address = addr; - walker->fault.nested_page_fault = w != vcpu->arch.cpu_walk; + walker->fault.nested_page_fault = w != &vcpu->arch.cpu_walk; walker->fault.async_page_fault = false;trace_kvm_mmu_walker_error (walker->fault.error_code);
@@ -894,7 +894,7 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,#ifndef CONFIG_X86_64 /* A 64-bit GVA should be impossible on 32-bit KVM. */ -WARN_ON_ONCE ((addr >>32 ) && w == vcpu->arch.cpu_walk); +WARN_ON_ONCE ((addr >>32 ) && w == &vcpu->arch.cpu_walk);#endif r =FNAME (walk_addr_generic)(&walker, vcpu, w, addr, access);
arch/x86/kvm/svm/nested.c +0 -2
@@ -102,13 +102,11 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; vcpu->arch.mmu->w.inject_page_fault = nested_svm_inject_npf_exit; - vcpu->arch.cpu_walk = &vcpu->arch.nested_cpu_walk; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu) { vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.cpu_walk = &vcpu->arch.root_mmu.w; }static bool nested_vmcb_needs_vls_intercept (struct vcpu_svm *svm)
arch/x86/kvm/vmx/nested.c +0 -3
@@ -498,14 +498,11 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->w.get_pdptr = kvm_pdptr_read; vcpu->arch.mmu->w.inject_page_fault = nested_ept_inject_page_fault; - - vcpu->arch.cpu_walk = &vcpu->arch.nested_cpu_walk; }static void nested_ept_uninit_mmu_context (struct kvm_vcpu *vcpu) { vcpu->arch.mmu = &vcpu->arch.root_mmu; - vcpu->arch.cpu_walk = &vcpu->arch.root_mmu.w; } static bool nested_vmx_is_page_fault_vmexit(struct vmcs12 *vmcs12,
arch/x86/kvm/x86.c +10 -10
@@ -995,7 +995,7 @@ void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,WARN_ON_ONCE (fault->vector !=PF_VECTOR ); fault_walk = fault->nested_page_fault ? &vcpu->arch.mmu->w : - vcpu->arch.cpu_walk; + &vcpu->arch.cpu_walk;/* * Invalidate the TLB entry for the faulting address, if it exists,
@@ -1061,7 +1061,7 @@ static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)*/ int load_pdptrs (struct kvm_vcpu *vcpu,unsigned long cr3) { -struct kvm_pagewalk *w = vcpu->arch.cpu_walk; +struct kvm_pagewalk *w = &vcpu->arch.cpu_walk;gfn_t pdpt_gfn = cr3 >>PAGE_SHIFT ;gpa_t real_gpa;int i;
@@ -7853,7 +7853,7 @@ void kvm_get_segment(struct kvm_vcpu *vcpu,gpa_t kvm_mmu_gva_to_gpa_read (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ;return cpu_walk->gva_to_gpa (vcpu, cpu_walk, gva, access, exception);
@@ -7863,7 +7863,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_gva_to_gpa_read);gpa_t kvm_mmu_gva_to_gpa_write (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ; access |=PFERR_WRITE_MASK ;
@@ -7875,7 +7875,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_mmu_gva_to_gpa_write);gpa_t kvm_mmu_gva_to_gpa_system (struct kvm_vcpu *vcpu,gva_t gva,struct x86_exception *exception) { -struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;return cpu_walk->gva_to_gpa (vcpu, cpu_walk, gva,0 , exception); }
@@ -7884,7 +7884,7 @@ static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, struct kvm_vcpu *vcpu, u64 access, struct x86_exception *exception) { - struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; + struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;void *data = val;int r =X86EMUL_CONTINUE ;
@@ -7917,7 +7917,7 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, struct x86_exception *exception) {struct kvm_vcpu *vcpu =emul_to_vcpu (ctxt); -struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;u64 access = (kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ;unsigned offset;int ret;
@@ -7976,7 +7976,7 @@ static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes struct kvm_vcpu *vcpu, u64 access, struct x86_exception *exception) { - struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; + struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;void *data = val;int r =X86EMUL_CONTINUE ;
@@ -8082,7 +8082,7 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, gpa_t *gpa, struct x86_exception *exception, bool write) { - struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; + struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;u64 access = ((kvm_x86_call (get_cpl)(vcpu) ==3 ) ?PFERR_USER_MASK :0 ) | (write ?PFERR_WRITE_MASK :0 );
@@ -14213,7 +14213,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_spec_ctrl_test_value);void kvm_fixup_and_inject_pf_error (struct kvm_vcpu *vcpu,gva_t gva,u16 error_code) { -struct kvm_pagewalk *cpu_walk = vcpu->arch.cpu_walk; +struct kvm_pagewalk *cpu_walk = &vcpu->arch.cpu_walk;struct x86_exception fault;u64 access = error_code & (PFERR_WRITE_MASK |PFERR_FETCH_MASK |PFERR_USER_MASK );
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 17/22 KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
In reply to Paolo BonziniNow that root_mmu.w always has the same content as cpu_walk, replace it with just a pointer to cpu_walk. For guest_mmu, introduce a second struct kvm_pagewalk and point to it. It is now clear that non-MMU code does cares about page walks, but it funnels (almost) all interactions with the TLB to mmu.c.
It is left as an exercise to the reader to split kvm_pagewalk to its own file...
--- arch/x86/include/asm/kvm_host.h | 7 ++- arch/x86/kvm/mmu.h | 4 +- arch/x86/kvm/mmu/mmu.c | 97 +++++++++++++-------------------- arch/x86/kvm/mmu/paging_tmpl.h | 14 ++--- arch/x86/kvm/svm/nested.c | 9 ++- arch/x86/kvm/vmx/nested.c | 11 ++-- arch/x86/kvm/x86.c | 2 +- 7 files changed, 63 insertions(+), 81 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -3
@@ -504,11 +504,11 @@ struct kvm_pagewalk { };struct kvm_mmu { -struct kvm_pagewalk w; -int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault);int (*sync_spte)(struct kvm_vcpu *vcpu,struct kvm_mmu_page *sp,int i); +struct kvm_pagewalk *w; +struct kvm_mmu_root_info root;hpa_t mirror_root_hpa;union kvm_mmu_page_role root_role;
@@ -862,8 +862,9 @@ struct kvm_vcpu_arch {/* Non-nested MMU for L1 */ struct kvm_mmu root_mmu; -/* L1 MMU when running nested */ +/* L1 TDP when running nested */ struct kvm_mmu guest_mmu; +struct kvm_pagewalk tdp_walk;/* * Pagewalk context used for gva_to_gpa translations.
arch/x86/kvm/mmu.h +2 -2
@@ -180,7 +180,7 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,* still refresh cpu_walk, so as to honor L2's CR0.WP when translating * L2 GVAs to GPAs. */ -if (!tdp_enabled || w == &vcpu->arch.guest_mmu.w) +if (!tdp_enabled || w == &vcpu->arch.tdp_walk)return ;__kvm_mmu_refresh_passthrough_bits (vcpu, w);
@@ -306,7 +306,7 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu, struct x86_exception *exception, u64 pte_access) { -if (!mmu_is_nested (vcpu) || w == &vcpu->arch.guest_mmu.w) +if (!mmu_is_nested (vcpu) || w == &vcpu->arch.tdp_walk)return gpa; return kvm_x86_ops.nested_ops->translate_nested_gpa(vcpu, gpa, access, exception,
arch/x86/kvm/mmu/mmu.c +39 -58
294 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +7 -7
@@ -157,7 +157,7 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 *spte, u64 gpte) { - struct kvm_pagewalk *w = &vcpu->arch.mmu->w; + struct kvm_pagewalk *w = vcpu->arch.mmu->w;if (!FNAME (is_present_gpte)(w, gpte)) goto no_present;
@@ -551,7 +551,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,static int FNAME (walk_addr )(struct guest_walker *walker,struct kvm_vcpu *vcpu,gpa_t addr,u64 access) { -return FNAME (walk_addr_generic)(walker, vcpu, &vcpu->arch.mmu->w, addr, +return FNAME (walk_addr_generic)(walker, vcpu, vcpu->arch.mmu->w, addr, access); }
@@ -567,7 +567,7 @@ FNAME(prefetch_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, gfn =gpte_to_gfn (gpte); pte_access = sp->role.access &FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(&vcpu->arch.mmu->w, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.mmu->w, &pte_access, gpte);return kvm_mmu_prefetch_sptes (vcpu, gfn, spte,1 , pte_access); }
@@ -650,7 +650,7 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault,WARN_ON_ONCE (gw->gfn != base_gfn); direct_access = gw->pte_access; - top_level = vcpu->arch.mmu->w.cpu_role.base.level; + top_level = vcpu->arch.mmu->w->cpu_role.base.level;if (top_level ==PT32E_ROOT_LEVEL ) top_level =PT32_ROOT_LEVEL ;/*
@@ -839,7 +839,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* otherwise KVM will cache incorrect access information in the SPTE. */ if (fault->write && !(walker.pte_access &ACC_WRITE_MASK ) && - !is_cr0_wp (&vcpu->arch.mmu->w) && !fault->user && fault->slot) { + !is_cr0_wp (vcpu->arch.mmu->w) && !fault->user && fault->slot) { walker.pte_access |=ACC_WRITE_MASK ; walker.pte_access &= ~ACC_USER_MASK ;
@@ -849,7 +849,7 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault* then we should prevent the kernel from executing it * if SMEP is enabled. */ -if (is_cr4_smep (&vcpu->arch.mmu->w)) +if (is_cr4_smep (vcpu->arch.mmu->w)) walker.pte_access &= ~ACC_EXEC_MASK; }#endif
@@ -947,7 +947,7 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int gfn =gpte_to_gfn (gpte); pte_access = sp->role.access; pte_access &=FNAME (gpte_access)(gpte); -FNAME (protect_clean_gpte)(&vcpu->arch.mmu->w, &pte_access, gpte); +FNAME (protect_clean_gpte)(vcpu->arch.mmu->w, &pte_access, gpte);if (sync_mmio_spte (vcpu, &sp->spt[i], gfn, pte_access))return 0 ;
arch/x86/kvm/svm/nested.c +4 -5
@@ -98,10 +98,9 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) svm->nested.ctl.nested_cr3, svm->nested.ctl.misc_ctl); - vcpu->arch.mmu->w.get_guest_pgd = nested_svm_get_tdp_cr3; - vcpu->arch.mmu->w.get_pdptr = nested_svm_get_tdp_pdptr; - - vcpu->arch.mmu->w.inject_page_fault = nested_svm_inject_npf_exit; + vcpu->arch.tdp_walk.get_guest_pgd = nested_svm_get_tdp_cr3; + vcpu->arch.tdp_walk.get_pdptr = nested_svm_get_tdp_pdptr; + vcpu->arch.tdp_walk.inject_page_fault = nested_svm_inject_npf_exit; }static void nested_svm_uninit_mmu_context (struct kvm_vcpu *vcpu)
@@ -2088,7 +2087,7 @@ static gpa_t svm_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u64 pte_access) {struct vcpu_svm *svm =to_svm (vcpu); -struct kvm_pagewalk *w = &vcpu->arch.mmu->w; +struct kvm_pagewalk *w = &vcpu->arch.tdp_walk;BUG_ON (!mmu_is_nested (vcpu));
arch/x86/kvm/vmx/nested.c +6 -5
@@ -407,7 +407,7 @@ static void nested_ept_invalidate_addr(struct kvm_vcpu *vcpu, gpa_t eptp, roots |=KVM_MMU_ROOT_PREVIOUS (i); }if (roots) -kvm_mmu_invalidate_addr (vcpu, &vcpu->arch.guest_mmu.w, addr, roots); +kvm_mmu_invalidate_addr (vcpu, &vcpu->arch.tdp_walk, addr, roots); } static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu,
@@ -494,10 +494,10 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.guest_mmu;nested_ept_new_eptp (vcpu); - vcpu->arch.mmu->w.get_guest_pgd = nested_ept_get_eptp; - vcpu->arch.mmu->w.get_pdptr = kvm_pdptr_read; + vcpu->arch.tdp_walk.get_guest_pgd = nested_ept_get_eptp; + vcpu->arch.tdp_walk.get_pdptr = kvm_pdptr_read; - vcpu->arch.mmu->w.inject_page_fault = nested_ept_inject_page_fault; + vcpu->arch.tdp_walk.inject_page_fault = nested_ept_inject_page_fault; }static void nested_ept_uninit_mmu_context (struct kvm_vcpu *vcpu)
@@ -7457,12 +7457,13 @@ __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))return 0 ; } +static gpa_t vmx_translate_nested_gpa (struct kvm_vcpu *vcpu,gpa_t gpa,u64 access,struct x86_exception *exception,u64 pte_access) { -struct kvm_pagewalk *w = &vcpu->arch.mmu->w; +struct kvm_pagewalk *w = &vcpu->arch.tdp_walk;BUG_ON (!mmu_is_nested (vcpu));
arch/x86/kvm/x86.c +1 -1
@@ -994,7 +994,7 @@ void kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu,WARN_ON_ONCE (fault->vector !=PF_VECTOR ); - fault_walk = fault->nested_page_fault ? &vcpu->arch.mmu->w : + fault_walk = fault->nested_page_fault ? &vcpu->arch.tdp_walk : &vcpu->arch.cpu_walk;/*
Signature · 1 line
2.52.0
Re: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
arch/x86/kvm/mmu/mmu.c int kvm_mmu_create(struct kvm_vcpu *vcpu) · line 6757
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +2
-2@@ -6767,11 +6748,11 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.root_mmu; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu, &vcpu->arch.tdp_walk);if (ret)return ret; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu, &vcpu->arch.cpu_walk);if (ret) goto fail_allocate_root;
I admittedly did not take a close look at the series. Just wanted to chime in about naming. I *love* that we are getting rid of nested_mmu because I can never immediately tell the difference between guest_mmu and nested_mmu, so thank you for that.
However, I can't immediately tell what vcpu->arch.cpu_walk is doing either (compared to vcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g. root_pg_walk and guest_pg_walk)?
I also think root_mmu and guest_mmu could still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right? Maybe just mmu and nested_mmu could work? But I am not sure if we can reclaim the nested_mmu name, it's gonna screw with anyone doing backports :/
Re: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
Quoted 24 lines · Yosry Ahmed
arch/x86/kvm/mmu/mmu.c int kvm_mmu_create(struct kvm_vcpu *vcpu) · line 6757
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +2
-2@@ -6767,11 +6748,11 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.root_mmu; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu, &vcpu->arch.tdp_walk);if (ret)return ret; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu, &vcpu->arch.cpu_walk);if (ret) goto fail_allocate_root;I admittedly did not take a close look at the series. Just wanted to chime in about naming. I *love* that we are getting rid of
nested_mmubecause I can never immediately tell the difference betweenguest_mmuandnested_mmu, so thank you for that.However, I can't immediately tell what
vcpu->arch.cpu_walkis doing either (compared tovcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g.root_pg_walkandguest_pg_walk)?
Quoted 5 lines
I also think
root_mmuandguest_mmucould still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right? Maybe just mmu andnested_mmucould work? But I am not sure if we can reclaim thenested_mmuname, it's gonna screw with anyone doing backports :/
+1, and I wonder if it helps to name the walkers after what they walk, to shape one's conceptual model. E.g. cr3_walk (instead of cpu_walk) for the walker over CR3-rooted tables of the currently executing guest (L1's or L2's CR3), and nested_tdp_walk for the walker over L1's nested EPT/NPT when L0 services L2. tdp_walk alone felt a bit vague — it read as "the TDP MMU's walker," whereas this specifically applies to the nested TDP path.
Sean Christopherson
maintainerRe: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
Quoted 20 lines · Kishen Maloor
Quoted 19 lines · Yosry Ahmed
arch/x86/kvm/mmu/mmu.c int kvm_mmu_create(struct kvm_vcpu *vcpu) · line 6757
Quoted 13 lines
arch/x86/kvm/mmu/mmu.c +2
-2@@ -6767,11 +6748,11 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) vcpu->arch.mmu = &vcpu->arch.root_mmu; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.guest_mmu, &vcpu->arch.tdp_walk);if (ret)return ret; - ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu); + ret =__kvm_mmu_create (vcpu, &vcpu->arch.root_mmu, &vcpu->arch.cpu_walk);if (ret) goto fail_allocate_root;I admittedly did not take a close look at the series. Just wanted to chime in about naming. I *love* that we are getting rid of
nested_mmubecause I can never immediately tell the difference betweenguest_mmuandnested_mmu, so thank you for that.
Quoted 4 lines
Quoted 4 lines
However, I can't immediately tell what
vcpu->arch.cpu_walkis doing either (compared tovcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g.root_pg_walkandguest_pg_walk)?
They aren't tied to MMUs in that way though. If L1 is using shadow paging for L2, than the so called "root" MMU is used for both L1 and L2, albeit with a different role (kvm_mmu_page_role.guest_mode will be different).
Quoted 3 lines
Quoted 3 lines
I also think
root_mmuandguest_mmucould still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right?
Almost, but not quite. See above.
Quoted 4 lines
Quoted 4 lines
Maybe just mmu and
nested_mmucould work?But I am not sure if we can reclaim the
nested_mmuname, it's gonna screw with anyone doing backports :/
Heh, could be fun.
Quoted 6 lines
+1, and I wonder if it helps to name the walkers after what they walk, to shape one's conceptual model. E.g.
cr3_walk(instead ofcpu_walk) for the walker over CR3-rooted tables of the currently executing guest (L1's or L2's CR3), andnested_tdp_walkfor the walker over L1's nested EPT/NPT when L0 services L2.tdp_walkalone felt a bit vague — it read as "the TDP MMU's walker," whereas this specifically applies to the nested TDP path.
Or maybe legacy_walk instead of cr3_walk? Purely because of nCR3 with NPT :-/
Good xxx_mmu names remain elusive...
Paolo Bonzini
maintainerRe: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
In reply to Kishen MaloorQuoted 1 line · Kishen Maloor
+1, and I wonder if it helps to name the walkers after what they walk, to shape one's conceptual model. E.g.
cr3_walk(instead ofcpu_walk) for the walker over CR3-rooted tables of the currently executing guest (L1's or L2's CR3), andnested_tdp_walkfor the walker over L1's nested EPT/NPT when L0 services L2.tdp_walkalone felt a bit vague — it read as "the TDP MMU's walker," whereas this specifically applies to the nested TDP path.
You are influenced by the old names though. If you stop thinking of kvm_mmu and kvm_pagewalk as related in any way, it helps. kvm_pagewalk exists independent of the MMU, except inasmuch a (shadow) kvm_mmu has a kvm_pagewalk that it uses to walk the page tables it shadows.
Paolo
Paolo Bonzini
maintainerRe: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
In reply to Yosry AhmedIl mer 13 mag 2026, 23:36 Yosry Ahmed <[email protected]> ha scritto:
Quoted 5 lines
However, I can't immediately tell what
vcpu->arch.cpu_walkis doing either (compared tovcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g.root_pg_walkandguest_pg_walk)?
No, cpu_walk is always GVA->(n)GPA and tdp_walk is the optional nGPA->GPA stage. While there is a 1:1 mapping from a struct kvm_mmu to kvm_pagewalk when doing shadow paging, for *emulation* purposes cpu_walk is used for both L1 and L2 and It replaces the vcpu->arch.walk_mmu pointer from the old code (which led to either root_mmu or nested_mmu). In fact the main change in the series is the removal of walk_mmu, with cpu_walk always representing the CR0/CR3/CR4 page tables.
I could call them gva_walk and ngpa_walk, but I think the current name are also self-explanatory (especially once you understand that walk_mmu is no more and cpu_walk can be used for both L1 and L2). The confusion comes more if you look at the walkers from the POV of struct kvm_mmu. Which leads to the other half...
Quoted 5 lines
I also think
root_mmuandguest_mmucould still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right? Maybe just mmu andnested_mmucould work? But I am not sure if we can reclaim thenested_mmuname, it's gonna screw with anyone doing backports :/
And even more important vcpu->arch.mmu is the pointer to either root_mmu or guest_mmu. I wouldn't reclaim either mmu or nested_mmu.
guest_mmu is not L2 MMU if L1 does not use two-dimensional paging, so l1_mmu and l2_mmu does not cut it entirely. And root_mmu can be either GVA->HPA or GPA->HPA, therefore applying the idea above (e.g., gpa_mmu and ngpa_mmu) would not work well.
I suppose guest_mmu could be ngpa_mmu or shadow_tdp_mmu, but another possibility/refactoring would be to adjust the code and call the two MMUs direct_mmu and shadow_mmu. I haven't looked into what this means for the code but it would definitely make for the clearest naming. Having direct_mmu->w be NULL would be nice, so it seems promising but not something I was going to do soon (the *real* reason to submit this patch now is to get rid of is_executable_pte() half-assed support for MBEC/GMET, and that's where I stopped).
Even with the small complication that CR0.PG=0 is a direct mapping but would use shadow_mmu, it's still a GVA->HPA mapping and thus pretty understandable.
tl;dr: naming is hard so I tried to change as little as possible in this respect...
Paolo
Quoted 1 line
Re: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
Quoted 16 lines · Paolo Bonzini
Il mer 13 mag 2026, 23:36 Yosry Ahmed <[email protected]> ha scritto:
Quoted 5 lines
However, I can't immediately tell what
vcpu->arch.cpu_walkis doing either (compared tovcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g.root_pg_walkandguest_pg_walk)?No,
cpu_walkis always GVA->(n)GPA andtdp_walkis the optionalnGPA->GPAstage. While there is a 1:1 mapping from a structkvm_mmutokvm_pagewalkwhen doing shadow paging, for *emulation* purposescpu_walkis used for both L1 and L2 and It replaces thevcpu->arch.walk_mmupointer from the old code (which led to eitherroot_mmuornested_mmu). In fact the main change in the series is the removal ofwalk_mmu, withcpu_walkalways representing the CR0/CR3/CR4 page tables.
Quoted 5 lines
I could call them
gva_walkandngpa_walk, but I think the current name are also self-explanatory (especially once you understand thatwalk_mmuis no more andcpu_walkcan be used for both L1 and L2). The confusion comes more if you look at the walkers from the POV of structkvm_mmu. Which leads to the other half...
I think the source of my confusion is that when TDP is enabled 'root_mmu' is used for tracking the TDP for L1 (GPA->HPA), as well as walking L1's own page tables (GVA->GPA).
I guess the main point of the series (in the context of root_mmu) is to stop doing that and separate the former (building GPA->HPA) from the latter (GVA->GPA).
So after this series:
- root_mmu only tracks building TDP for L1, or shadowing L1's page tables without TDP.
- guest_mmu is only used to shadow L1's TDP for L2.
- cpu_walk is used to walk L1's or L2's CR3-based page tables (i.e. replaces part of old root_mmu and nested_mmu).
- tdp_walk is used to walk L1's TDPs for L2 (i.e. replaces part of old guest_mmu). I guess tdp_walk is always tied to guest_mmu?
Is my understanding correct? I feel like I missed something.
That being said, I still don't like cpu_walk tbh. Maybe cr3_walk as suggested by Kishen, or more explicitly gva_walk or sth. The CPU walks both CR3-based page tables as well as TDP page tables, so I am not sure why use 'cpu' here?
Quoted 10 lines
Quoted 6 lines
I also think
root_mmuandguest_mmucould still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right? Maybe just mmu andnested_mmucould work? But I am not sure if we can reclaim thenested_mmuname, it's gonna screw with anyone doing backports :/And even more important
vcpu->arch.mmuis the pointer to eitherroot_mmuorguest_mmu. I wouldn't reclaim either mmu ornested_mmu.
Quoted 6 lines
guest_mmuis not L2 MMU if L1 does not use two-dimensional paging, sol1_mmuandl2_mmudoes not cut it entirely. Androot_mmucan be eitherGVA->HPAorGPA->HPA, therefore applying the idea above (e.g.,gpa_mmuandngpa_mmu) would not work well.I suppose
guest_mmucould bengpa_mmuorshadow_tdp_mmu, but another
I was going to suggest shadow_tdp_mmu as I was reading this, I like that name. Still can't think of a good name for root_mmu.
I thought about a union of two names, shadow_mmu and tdp_mmu, but I suspect most code paths would apply equally to both?
Quoted 3 lines
possibility/refactoring would be to adjust the code and call the two MMUs
direct_mmuandshadow_mmu. I haven't looked into what this means for the code but it would definitely make for the clearest naming.
IIUC direct_mmu would only be used for L1 TDP, and shadow_mmu would for either shadowing L1 page tables (with TDP=0) or shadowing L1's TDP for L2?
The naming sounds nice, but as you say below, I don't know how that would work code-wise.
Quoted 11 lines
Having
direct_mmu->wbe NULL would be nice, so it seems promising but not something I was going to do soon (the *real* reason to submit this patch now is to get rid ofis_executable_pte()half-assed support for MBEC/GMET, and that's where I stopped).Even with the small complication that CR0.PG=0 is a direct mapping but would use
shadow_mmu, it's still aGVA->HPAmapping and thus pretty understandable.tl;dr: naming is hard so I tried to change as little as possible in this respect...
Makes sense. Thanks for bearing with me.
Sean Christopherson
maintainerRe: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
In reply to Paolo BonziniQuoted 16 lines · Paolo Bonzini
Il mer 13 mag 2026, 23:36 Yosry Ahmed <[email protected]> ha scritto:
Quoted 5 lines
However, I can't immediately tell what
vcpu->arch.cpu_walkis doing either (compared tovcpu->arch.tdp_walk), so maybe the names can be improved? If these walks are tied to these MMUs, maybe name them as such (e.g.root_pg_walkandguest_pg_walk)?No,
cpu_walkis always GVA->(n)GPA andtdp_walkis the optionalnGPA->GPAstage. While there is a 1:1 mapping from a structkvm_mmutokvm_pagewalkwhen doing shadow paging, for *emulation* purposescpu_walkis used for both L1 and L2 and It replaces thevcpu->arch.walk_mmupointer from the old code (which led to eitherroot_mmuornested_mmu). In fact the main change in the series is the removal ofwalk_mmu, withcpu_walkalways representing the CR0/CR3/CR4 page tables.
Quoted 5 lines
I could call them
gva_walkandngpa_walk, but I think the current name are also self-explanatory (especially once you understand thatwalk_mmuis no more andcpu_walkcan be used for both L1 and L2). The confusion comes more if you look at the walkers from the POV of structkvm_mmu. Which leads to the other half...
I like gva_walk and ngpa_walk, a lot. A lot, a lot. I also find cpu_walk to be ambiguous and confusing. It's not like the CPU walks GVA=>GPA but something else walks GPA=>HPA.
Quoted 14 lines
Quoted 5 lines
I also think
root_mmuandguest_mmucould still use some improvement but that's probably outside the scope of this series. These are essentially L1 MMU and L2 MMU, right? Maybe just mmu andnested_mmucould work? But I am not sure if we can reclaim thenested_mmuname, it's gonna screw with anyone doing backports :/And even more important
vcpu->arch.mmuis the pointer to eitherroot_mmuorguest_mmu. I wouldn't reclaim either mmu ornested_mmu.
guest_mmuis not L2 MMU if L1 does not use two-dimensional paging, sol1_mmuandl2_mmudoes not cut it entirely. Androot_mmucan be eitherGVA->HPAorGPA->HPA, therefore applying the idea above (e.g.,gpa_mmuandngpa_mmu) would not work well.
Quoted 4 lines
I suppose
guest_mmucould bengpa_mmuorshadow_tdp_mmu, but another possibility/refactoring would be to adjust the code and call the two MMUsdirect_mmuandshadow_mmu. I haven't looked into what this means for the code but it would definitely make for the clearest naming.
I'm not a fan of "direct". Even if we deal with the CR0.PG=0 case, there's still the direct shadow pages wrinkle.
I do like the idea of reworking the code so that there's an MMU for non-nested TDP and a shadow MMU for everything else. Given that the only thing standing in our way of having tdp_mmu and shadow_mmu is the existence of TDP support in the shadow MMU, what if we finally drop that? Then we can have tdp_mmu, which is literally the TDP MMU, and shadow_mmu.
Quoted 16 lines
Having
direct_mmu->wbe NULL would be nice, so it seems promising but not something I was going to do soon (the *real* reason to submit this patch now is to get rid ofis_executable_pte()half-assed support for MBEC/GMET, and that's where I stopped).Even with the small complication that CR0.PG=0 is a direct mapping but would use
shadow_mmu, it's still aGVA->HPAmapping and thus pretty understandable.tl;dr: naming is hard so I tried to change as little as possible in this respect...
Paolo
Quoted 1 line
Sean Christopherson
maintainerRe: [PATCH 17/22] KVM: x86/mmu: pull struct kvm_pagewalk out of struct kvm_mmu
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Paolo Bonzini
Il mer 13 mag 2026, 23:36 Yosry Ahmed <[email protected]> ha scritto:
guest_mmuis not L2 MMU if L1 does not use two-dimensional paging, sol1_mmuandl2_mmudoes not cut it entirely. Androot_mmucan be eitherGVA->HPAorGPA->HPA, therefore applying the idea above (e.g.,gpa_mmuandngpa_mmu) would not work well.I suppose
guest_mmucould bengpa_mmuorshadow_tdp_mmu, but another possibility/refactoring would be to adjust the code and call the two MMUsdirect_mmuandshadow_mmu. I haven't looked into what this means for the code but it would definitely make for the clearest naming.
Quoted 8 lines
I'm not a fan of "direct". Even if we deal with the CR0.PG=0 case, there's still the direct shadow pages wrinkle.
I do like the idea of reworking the code so that there's an MMU for non-nested TDP and a shadow MMU for everything else. Given that the only thing standing in our way of having
tdp_mmuandshadow_mmuis the existence of TDP support in the shadow MMU, what if we finally drop that? Then we can havetdp_mmu, which is literally the TDP MMU, andshadow_mmu.
And then I remembered the TDP MMU is 64-bit only. But, I saw you posted 32-bit nested tests for KUT. Maybe this could be the motivation to invest more in that test coverage, and be the juicy carrot that we need to finally kill off 32-bit KVM x86?
Paolo Bonzini
maintainerPATCH 18/22 KVM: x86/mmu: cleanup functions that initialize shadow MMU
In reply to Paolo BonziniNow that the GVA->GPA page walker is initialized independently, init_kvm_softmmu() does not do anything more than calling kvm_init_shadow_mmu() so eliminate it from the call chain. At the same time, rename kvm_init_shadow_mmu() to init_kvm_shadow_mmu() for consistency with init_kvm_tdp_mmu().
--- arch/x86/kvm/mmu/mmu.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu/mmu.c +2 -8
@@ -5901,7 +5901,7 @@ static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *contereset_shadow_zero_bits_mask (vcpu, context); } -static void kvm_init_shadow_mmu (struct kvm_vcpu *vcpu, +static void init_kvm_shadow_mmu (struct kvm_vcpu *vcpu,union kvm_cpu_role cpu_role) {struct kvm_mmu *context = &vcpu->arch.root_mmu;
@@ -6033,12 +6033,6 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_init_shadow_ept_mmu); -static void init_kvm_softmmu (struct kvm_vcpu *vcpu, -union kvm_cpu_role cpu_role) -{ -kvm_init_shadow_mmu (vcpu, cpu_role); -} -void kvm_init_mmu (struct kvm_vcpu *vcpu) {struct kvm_mmu_role_regs regs =vcpu_to_role_regs (vcpu);
@@ -6050,7 +6044,7 @@ void kvm_init_mmu(struct kvm_vcpu *vcpu)if (tdp_enabled)init_kvm_tdp_mmu (vcpu, cpu_role);else -init_kvm_softmmu (vcpu, cpu_role); +init_kvm_shadow_mmu (vcpu, cpu_role); } }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_init_mmu);
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 19/22 KVM: x86/mmu: pull page format to a new struct
In reply to Paolo BonziniKVM is doing reserved bits checks on both guest and host page tables, though the latter are only for consistency. Create a new struct for this common code as well as for all data that is extracted from the CPU role.
--- arch/x86/include/asm/kvm_host.h | 23 ++++++++++++++--------- arch/x86/kvm/mmu.h | 7 ++++--- arch/x86/kvm/mmu/mmu.c | 16 ++++++++-------- arch/x86/kvm/mmu/paging_tmpl.h | 10 +++++----- 4 files changed, 31 insertions(+), 25 deletions(-)
arch/x86/include/asm/kvm_host.h +14 -9
@@ -476,15 +476,7 @@ struct kvm_page_fault;* and 2-level 32-bit). The kvm_pagewalk structure abstracts the details of the * current mmu mode. */ -struct kvm_pagewalk { -unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); -u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index); -void (*inject_page_fault)(struct kvm_vcpu *vcpu, -struct x86_exception *fault); -gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w, -gpa_t gva_or_gpa,u64 access, -struct x86_exception *exception); -union kvm_cpu_role cpu_role; +struct kvm_page_format {struct rsvd_bits_validate guest_rsvd_check;/*
@@ -503,6 +495,19 @@ struct kvm_pagewalk {u16 permissions[16 ]; }; +struct kvm_pagewalk { +unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu); +u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index); +void (*inject_page_fault)(struct kvm_vcpu *vcpu, +struct x86_exception *fault); +gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu,struct kvm_pagewalk *w, +gpa_t gva_or_gpa,u64 access, +struct x86_exception *exception); + +union kvm_cpu_role cpu_role; +struct kvm_page_format fmt; +}; +struct kvm_mmu {int (*page_fault)(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault); int (*sync_spte)(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu.h +4 -3
@@ -217,15 +217,16 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,u64 implicit_access = access &PFERR_IMPLICIT_ACCESS ;bool not_smap = ((rflags &X86_EFLAGS_AC ) | implicit_access) ==X86_EFLAGS_AC ;int index = (pfec | (not_smap ?PFERR_RSVD_MASK :0 )) >>1 ; +struct kvm_page_format *fmt = &w->fmt;u32 errcode =PFERR_PRESENT_MASK ;bool fault;kvm_mmu_refresh_passthrough_bits (vcpu, w); - fault = (w->permissions[index] >> pte_access) &1 ; + fault = (fmt->permissions[index] >> pte_access) &1 ;WARN_ON_ONCE (pfec & (PFERR_PK_MASK |PFERR_SS_MASK |PFERR_RSVD_MASK )); -if (unlikely (w->pkru_mask)) { +if (unlikely (fmt->pkru_mask)) {u32 pkru_bits, offset;/*
@@ -239,7 +240,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,/* clear present bit, replace PFEC.RSVD with ACC_USER_MASK. */ offset = (pfec & ~1 ) | ((pte_access &PT_USER_MASK ) ?PFERR_RSVD_MASK :0 ); - pkru_bits &= w->pkru_mask >> offset; + pkru_bits &= fmt->pkru_mask >> offset; errcode |= -pkru_bits &PFERR_PK_MASK ; fault |= (pkru_bits !=0 ); }
arch/x86/kvm/mmu/mmu.c +8 -8
@@ -5390,7 +5390,7 @@ static void __reset_rsvds_bits_mask(struct rsvd_bits_validate *rsvd_check,static void reset_guest_rsvds_bits_mask (struct kvm_vcpu *vcpu,struct kvm_pagewalk *w) { - __reset_rsvds_bits_mask(&w->guest_rsvd_check, + __reset_rsvds_bits_mask(&w->fmt.guest_rsvd_check, vcpu->arch.reserved_gpa_bits, w->cpu_role.base.level, is_efer_nx(w), guest_cpu_cap_has(vcpu, X86_FEATURE_GBPAGES),
@@ -5439,7 +5439,7 @@ static void __reset_rsvds_bits_mask_ept(struct rsvd_bits_validate *rsvd_check,static void reset_rsvds_bits_mask_ept (struct kvm_vcpu *vcpu,bool execonly,int huge_page_level) { -__reset_rsvds_bits_mask_ept (&vcpu->arch.tdp_walk.guest_rsvd_check, +__reset_rsvds_bits_mask_ept (&vcpu->arch.tdp_walk.fmt.guest_rsvd_check, vcpu->arch.reserved_gpa_bits, execonly, huge_page_level); }
@@ -5593,7 +5593,7 @@ static void update_permission_bitmask(struct kvm_pagewalk *pw, bool tdp, bool ep* permission_fault() to indicate accesses that are *not* subject to * SMAP restrictions. */ -for (index =0 ; index <ARRAY_SIZE (pw->permissions); ++index) { +for (index =0 ; index <ARRAY_SIZE (pw->fmt.permissions); ++index) {unsigned pfec = index <<1 ;/*
@@ -5667,7 +5667,7 @@ static void update_permission_bitmask(struct kvm_pagewalk *pw, bool tdp, bool ep smapf = (pfec & (PFERR_RSVD_MASK |PFERR_FETCH_MASK )) ?0 : kf; } - pw->permissions[index] = ff | uf | wf | rf | smapf; + pw->fmt.permissions[index] = ff | uf | wf | rf | smapf; } }
@@ -5700,14 +5700,14 @@ static void update_pkru_bitmask(struct kvm_pagewalk *w)unsigned bit;bool wp; - w->pkru_mask =0 ; + w->fmt.pkru_mask =0 ;if (!is_cr4_pke (w))return ; wp =is_cr0_wp (w); -for (bit =0 ; bit <ARRAY_SIZE (w->permissions); ++bit) { +for (bit =0 ; bit <ARRAY_SIZE (w->fmt.permissions); ++bit) {unsigned pfec, pkey_bits;bool check_pkey, check_write, ff, uf, wf, pte_user;
@@ -5735,7 +5735,7 @@ static void update_pkru_bitmask(struct kvm_pagewalk *w)/* PKRU.WD stops write access. */ pkey_bits |= (!!check_write) <<1 ; - w->pkru_mask |= (pkey_bits &3 ) << pfec; + w->fmt.pkru_mask |= (pkey_bits &3 ) << pfec; } }
@@ -6024,7 +6024,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly, context->sync_spte = ept_sync_spte;update_permission_bitmask (tdp_walk, true, true); - tdp_walk->pkru_mask =0 ; + tdp_walk->fmt.pkru_mask =0 ;reset_rsvds_bits_mask_ept (vcpu, execonly, huge_page_level);reset_ept_shadow_zero_bits_mask (context, execonly); }
arch/x86/kvm/mmu/paging_tmpl.h +5 -5
@@ -147,10 +147,10 @@ static bool FNAME(is_bad_mt_xwr)(struct rsvd_bits_validate *rsvd_check, u64 gpte#endif } -static bool FNAME (is_rsvd_bits_set )(struct kvm_pagewalk *w,u64 gpte,int level) +static bool FNAME (is_rsvd_bits_set )(struct kvm_page_format *fmt,u64 gpte,int level) { -return __is_rsvd_bits_set (&w->guest_rsvd_check, gpte, level) || -FNAME (is_bad_mt_xwr)(&w->guest_rsvd_check, gpte); +return __is_rsvd_bits_set (&fmt->guest_rsvd_check, gpte, level) || +FNAME (is_bad_mt_xwr)(&fmt->guest_rsvd_check, gpte); } static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
@@ -167,7 +167,7 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, !(gpte & PT_GUEST_ACCESSED_MASK)) goto no_present; -if (FNAME (is_rsvd_bits_set)(w, gpte,PG_LEVEL_4K )) +if (FNAME (is_rsvd_bits_set)(&w->fmt, gpte,PG_LEVEL_4K )) goto no_present;return false;
@@ -431,7 +431,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,if (unlikely (!FNAME (is_present_gpte)(w, pte))) goto error; -if (unlikely (FNAME (is_rsvd_bits_set)(w, pte, walker->level))) { +if (unlikely (FNAME (is_rsvd_bits_set)(&w->fmt, pte, walker->level))) { errcode =PFERR_RSVD_MASK |PFERR_PRESENT_MASK ; goto error; }
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 20/22 KVM: x86/mmu: merge struct rsvd_bits_validate into struct kvm_page_format
In reply to Paolo BonziniRemove one level of indirection, and prepare for using the permission bitmask machinery for shadow pages as well.
--- arch/x86/include/asm/kvm_host.h | 38 +++++------ arch/x86/kvm/mmu/mmu.c | 116 ++++++++++++++++---------------- arch/x86/kvm/mmu/paging_tmpl.h | 8 +-- arch/x86/kvm/mmu/spte.c | 4 +- arch/x86/kvm/mmu/spte.h | 18 ++--- arch/x86/kvm/vmx/vmx.c | 2 +- 6 files changed, 91 insertions(+), 95 deletions(-)
arch/x86/include/asm/kvm_host.h +17 -21
@@ -447,9 +447,24 @@ struct kvm_pio_request {#define PT64_ROOT_MAX_LEVEL 5 -struct rsvd_bits_validate { +struct kvm_page_format {u64 rsvd_bits_mask[2 ][PT64_ROOT_MAX_LEVEL ];u64 bad_mt_xwr; + +/* +* The pkru_mask indicates if protection key checks are needed. It +* consists of 16 domains indexed by page fault error code bits [4:1], +* with PFEC.RSVD replaced by ACC_USER_MASK from the page tables. +* Each domain has 2 bits which are ANDed with AD and WD from PKRU. +*/ +u32 pkru_mask; + +/* +* Bitmap; bit set = permission fault +* Array index: page fault error code [4:1] +* Bit index: pte permissions in ACC_* format +*/ +u16 permissions[16 ]; };struct kvm_mmu_root_info {
@@ -476,25 +491,6 @@ struct kvm_page_fault;* and 2-level 32-bit). The kvm_pagewalk structure abstracts the details of the * current mmu mode. */ -struct kvm_page_format { -struct rsvd_bits_validate guest_rsvd_check; - -/* -* The pkru_mask indicates if protection key checks are needed. It -* consists of 16 domains indexed by page fault error code bits [4:1], -* with PFEC.RSVD replaced by ACC_USER_MASK from the page tables. -* Each domain has 2 bits which are ANDed with AD and WD from PKRU. -*/ -u32 pkru_mask; - -/* -* Bitmap; bit set = permission fault -* Array index: page fault error code [4:1] -* Bit index: pte permissions in ACC_* format -*/ -u16 permissions[16 ]; -}; -struct kvm_pagewalk {unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);u64 (*get_pdptr)(struct kvm_vcpu *vcpu,int index);
@@ -529,7 +525,7 @@ struct kvm_mmu {* bits include not only hardware reserved bits but also * the bits spte never used. */ -struct rsvd_bits_validate shadow_zero_check; +struct kvm_page_format fmt; };enum pmc_type {
arch/x86/kvm/mmu/mmu.c +58 -58
246 lines not on this page · raw message
arch/x86/kvm/mmu/paging_tmpl.h +4 -4
@@ -138,19 +138,19 @@ static inline int FNAME(is_present_gpte)(struct kvm_pagewalk *w,#endif } -static bool FNAME (is_bad_mt_xwr )(struct rsvd_bits_validate *rsvd_check,u64 gpte) +static bool FNAME (is_bad_mt_xwr )(struct kvm_page_format *fmt,u64 gpte) {#if PTTYPE !=PTTYPE_EPT return false;#else -return __is_bad_mt_xwr (rsvd_check, gpte); +return __is_bad_mt_xwr (fmt, gpte);#endif }static bool FNAME (is_rsvd_bits_set )(struct kvm_page_format *fmt,u64 gpte,int level) { -return __is_rsvd_bits_set (&fmt->guest_rsvd_check, gpte, level) || -FNAME (is_bad_mt_xwr)(&fmt->guest_rsvd_check, gpte); +return __is_rsvd_bits_set (fmt, gpte, level) || +FNAME (is_bad_mt_xwr)(fmt, gpte); } static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu,
arch/x86/kvm/mmu/spte.c +2 -2
@@ -280,9 +280,9 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,if (prefetch && !synchronizing) spte =mark_spte_for_access_track (spte); -WARN_ONCE (is_rsvd_spte (&vcpu->arch.mmu->shadow_zero_check, spte, level), +WARN_ONCE (is_rsvd_spte (&vcpu->arch.mmu->fmt, spte, level),"spte = 0x%llx, level = %d, rsvd bits = 0x%llx" , spte, level, -get_rsvd_bits (&vcpu->arch.mmu->shadow_zero_check, spte, level)); +get_rsvd_bits (&vcpu->arch.mmu->fmt, spte, level));/* * Mark the memslot dirty *after* modifying it for access tracking.
arch/x86/kvm/mmu/spte.h +9 -9
@@ -378,33 +378,33 @@ static inline bool is_accessed_spte(u64 spte)return spte & shadow_accessed_mask; } -static inline u64 get_rsvd_bits (struct rsvd_bits_validate *rsvd_check,u64 pte, +static inline u64 get_rsvd_bits (struct kvm_page_format *fmt,u64 pte,int level) {int bit7 = (pte >>7 ) &1 ; -return rsvd_check->rsvd_bits_mask[bit7][level-1 ]; +return fmt->rsvd_bits_mask[bit7][level-1 ]; } -static inline bool __is_rsvd_bits_set (struct rsvd_bits_validate *rsvd_check, +static inline bool __is_rsvd_bits_set (struct kvm_page_format *fmt,u64 pte,int level) { -return pte &get_rsvd_bits (rsvd_check, pte, level); +return pte &get_rsvd_bits (fmt, pte, level); } -static inline bool __is_bad_mt_xwr (struct rsvd_bits_validate *rsvd_check, +static inline bool __is_bad_mt_xwr (struct kvm_page_format *fmt,u64 pte) {if (pte &VMX_EPT_USER_EXECUTABLE_MASK ) pte |=VMX_EPT_EXECUTABLE_MASK ; -return rsvd_check->bad_mt_xwr &BIT_ULL (pte &0x3f ); +return fmt->bad_mt_xwr &BIT_ULL (pte &0x3f ); } -static __always_inline bool is_rsvd_spte(struct rsvd_bits_validate *rsvd_check, +static __always_inline bool is_rsvd_spte(struct kvm_page_format *fmt,u64 spte,int level) { -return __is_bad_mt_xwr (rsvd_check, spte) || -__is_rsvd_bits_set (rsvd_check, spte, level); +return __is_bad_mt_xwr (fmt, spte) || +__is_rsvd_bits_set (fmt, spte, level); }/*
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -8703,7 +8703,7 @@ __init int vmx_hardware_setup(void)/* * Setup shadow_me_value/shadow_me_mask to include MKTME KeyID -* bits to shadow_zero_check. +* bits into the MMU's struct kvm_page_format. */ vmx_setup_me_spte_mask ();
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 21/22 KVM: x86/mmu: parameterize update_permission_bitmask()
In reply to Paolo BonziniMake it possible to apply the computation loop to both guest and shadow PTEs formats; the latter do not have an extended role, so pass the four parameters to the function one by one.
--- arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
arch/x86/kvm/mmu/mmu.c +12 -8
@@ -5569,18 +5569,15 @@ reset_ept_shadow_zero_bits_mask(struct kvm_mmu *context, bool execonly) (14 & (access) ? 1 << 14 : 0) | \ (15 & (access) ? 1 << 15 : 0)) -static void update_permission_bitmask (struct kvm_pagewalk *pw,bool tdp,bool ept) +static void __update_permission_bitmask (struct kvm_page_format *fmt,bool tdp, +bool ept,bool cr4_smep,bool cr4_smap, +bool cr0_wp,bool efer_nx) {unsigned index;const u16 w =ACC_BITS_MASK (ACC_WRITE_MASK );const u16 r =ACC_BITS_MASK (ACC_READ_MASK ); -bool cr4_smep =is_cr4_smep (pw); -bool cr4_smap =is_cr4_smap (pw); -bool cr0_wp =is_cr0_wp (pw); -bool efer_nx =is_efer_nx (pw); -/* * In hardware, page fault error codes are generated (as the name * suggests) on any kind of page fault. permission_fault() and
@@ -5593,7 +5590,7 @@ static void update_permission_bitmask(struct kvm_pagewalk *pw, bool tdp, bool ep* permission_fault() to indicate accesses that are *not* subject to * SMAP restrictions. */ -for (index =0 ; index <ARRAY_SIZE (pw->fmt.permissions); ++index) { +for (index =0 ; index <ARRAY_SIZE (fmt->permissions); ++index) {unsigned pfec = index <<1 ;/*
@@ -5667,10 +5664,17 @@ static void update_permission_bitmask(struct kvm_pagewalk *pw, bool tdp, bool ep smapf = (pfec & (PFERR_RSVD_MASK |PFERR_FETCH_MASK )) ?0 : kf; } - pw->fmt.permissions[index] = ff | uf | wf | rf | smapf; + fmt->permissions[index] = ff | uf | wf | rf | smapf; } } +static void update_permission_bitmask (struct kvm_pagewalk *w,bool tdp,bool ept) +{ +__update_permission_bitmask (&w->fmt, tdp, ept, +is_cr4_smep (w),is_cr4_smap (w), +is_cr0_wp (w),is_efer_nx (w)); +} +/* * PKU is an additional mechanism by which the paging controls access to * user-mode addresses based on the value in the PKRU register. Protection
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH 22/22 KVM: x86/mmu: use kvm_page_format to test SPTEs
In reply to Paolo Bonziniis_access_allowed(), and is_executable_pte() within it, are effectively a special version of permission_fault() that only supports a subset of roles. In particular it does not allow SMEP, SMAP and PKE.
Replace its implementation with a modified version of permission_fault(); the new version will support SMEP (and hence AMD GMET) for free as soon as update_spte_permission_bitmask() stops hardcoding cr4_smep == false.
This prepares for a possible future where TDP entries could have XS!=XU, for example as part of implementing Hyper-V VSM natively inside KVM.
--- arch/x86/kvm/mmu/mmu.c | 18 ++++++++++++--- arch/x86/kvm/mmu/spte.h | 46 +++++++++++++++++++++----------------- arch/x86/kvm/mmu/tdp_mmu.c | 3 ++- 3 files changed, 42 insertions(+), 25 deletions(-)
arch/x86/kvm/mmu/mmu.c +15 -3
@@ -3670,6 +3670,7 @@ static u64 *fast_pf_get_last_sptep(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *spte)*/ static int fast_page_fault (struct kvm_vcpu *vcpu,struct kvm_page_fault *fault) { +struct kvm_mmu *mmu;struct kvm_mmu_page *sp;int ret =RET_PF_INVALID ;u64 spte;
@@ -3679,6 +3680,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (!page_fault_can_be_fast (vcpu->kvm, fault))return ret; + mmu = vcpu->arch.mmu;walk_shadow_page_lockless_begin (vcpu); do {
@@ -3714,7 +3716,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* Need not check the access of upper level table entries since * they are always ACC_ALL. */ -if (is_access_allowed (fault, spte)) { +if (!spte_permission_fault (mmu, spte, fault)) { ret =RET_PF_SPURIOUS ;break ; }
@@ -3737,7 +3739,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* that were write-protected for dirty-logging or access * tracking are handled here. Don't bother checking if the * SPTE is writable to prioritize running with A/D bits enabled. -* The is_access_allowed() check above handles the common case +* The spte_permission_fault() check above handles the common case * of the fault being spurious, and the SPTE is known to be * shadow-present, i.e. except for access tracking restoration * making the new SPTE writable, the check is wasteful.
@@ -3762,7 +3764,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)/* Verify that the fault can be handled in the fast path */ if (new_spte == spte || - !is_access_allowed (fault, new_spte)) +spte_permission_fault (mmu, new_spte, fault))break ;/*
@@ -5675,6 +5677,12 @@ static void update_permission_bitmask(struct kvm_pagewalk *w, bool tdp, bool ept is_cr0_wp(w), is_efer_nx(w)); } +static void update_spte_permission_bitmask (struct kvm_mmu *mmu,bool tdp,bool ept) +{ +__update_permission_bitmask (&mmu->fmt, tdp, ept, + mmu->root_role.cr4_smep, false, true, true); +} +/* * PKU is an additional mechanism by which the paging controls access to * user-mode addresses based on the value in the PKRU register. Protection
@@ -5884,6 +5892,7 @@ static void init_kvm_tdp_mmu(struct kvm_vcpu *vcpu, context->page_fault = kvm_tdp_page_fault; context->sync_spte =NULL ; +update_spte_permission_bitmask (context, true, shadow_xs_mask);reset_tdp_shadow_zero_bits_mask (context); }
@@ -5902,6 +5911,7 @@ static void shadow_mmu_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *conteelse paging32_init_context (context); +update_spte_permission_bitmask (context, context == &vcpu->arch.guest_mmu, false);reset_shadow_zero_bits_mask (vcpu, context); }
@@ -6030,6 +6040,8 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,update_permission_bitmask (tdp_walk, true, true); tdp_walk->fmt.pkru_mask =0 ;reset_rsvds_bits_mask_ept (vcpu, execonly, huge_page_level); + +update_spte_permission_bitmask (context, true, true);reset_ept_shadow_zero_bits_mask (context, execonly); }
arch/x86/kvm/mmu/spte.h +25 -21
62 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +2 -1
@@ -1169,6 +1169,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,struct kvm_page_fault *fault, struct tdp_iter *iter) { + struct kvm_mmu *mmu = vcpu->arch.mmu;struct kvm_mmu_page *sp =sptep_to_sp (rcu_dereference (iter->sptep));u64 new_spte;int ret =RET_PF_FIXED ;
@@ -1178,7 +1179,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,return RET_PF_RETRY ;if (is_shadow_present_pte (iter->old_spte) && - (fault->prefetch ||is_access_allowed (fault, iter->old_spte)) && + (fault->prefetch || !spte_permission_fault (mmu, iter->old_spte, fault)) &&is_last_spte (iter->old_spte, iter->level)) {WARN_ON_ONCE (fault->pfn !=spte_to_pfn (iter->old_spte));return RET_PF_SPURIOUS ;
Signature · 1 line
2.52.0