KVM: x86: small MMU-adjacent cleanups
Paolo Bonzini
maintainerv2 cover KVM: x86: small MMU-adjacent cleanups
The non-MMU changes/preliminary cleanups from my "separate kvm_mmu" series; fixed, test, and extracted for inclusion in 7.2. The rest of the series can be included later.
The meat here is in patches 2 and 3, which prepare for having a single copy of the PDPTRs (in vcpu->arch) instead of two (in root_mmu and nested_mmu). This is then done in the last patch.
I have redone the nVMX patch now that Sean pointed out that the duplicate write was a rebase goof. I kept the nSVM one as is though.
v1->v2:
- rewritten "KVM: nVMX: remove unnecessary code in prepare_vmcs02_rare"
- added "KVM: x86/mmu: move pdptrs out of the MMU"
Thanks,
Paolo
Paolo Bonzini (5):
KVM: x86: remove nested_mmu from mmu_is_nested()
KVM: nVMX: remove unnecessary code in prepare_vmcs02_rare
KVM: nSVM: invalidate cached PDPTRs across nested NPT transitions
KVM: x86: check that kvm_handle_invpcid is only invoked with shadow
paging
KVM: x86/mmu: move pdptrs out of the MMU
arch/x86/include/asm/kvm_host.h | 5 ++--- arch/x86/kvm/kvm_cache_regs.h | 12 ++++++++++-- arch/x86/kvm/svm/nested.c | 27 ++++++++++++++++++--------- arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/nested.c | 11 ----------- arch/x86/kvm/vmx/vmx.c | 20 ++++++++------------ arch/x86/kvm/x86.c | 9 ++++++--- arch/x86/kvm/x86.h | 2 +- 8 files changed, 46 insertions(+), 42 deletions(-)
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v2 1/5 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.
Message-ID: <[email protected]>
--- 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 v2 2/5 KVM: nVMX: remove unnecessary code in prepare_vmcs02_rare
In reply to Paolo BonziniThe early vmwrite of the PDPTRs in prepare_vmcs02_rare() is redundant, because every write it does will be performed by prepare_vmcs02() if it is actually needed.
In any case where the emulator or the processor need the PDPTR, either is_pae_paging() is true on vmentry, or a write of CR0, CR4 or EFER will cause a vmexit to L0. The next vmentry will refresh the PDPTRs in the vmcs02 from vmcs12.
In fact, the original version[1] of what ended up being commit c7554efc8335 ("KVM: nVMX: Copy PDPTRs to/from vmcs12 only when necessary"), the writes in what is now prepare_vmcs02_rare() were removed. When the mega-collection of optimizations was posted[2], the removal of that code got dropped as a rebase good, so reinstate it.
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
--- arch/x86/kvm/vmx/nested.c | 11 ----------- 1 file changed, 11 deletions(-)
arch/x86/kvm/vmx/nested.c +0 -11
@@ -2623,17 +2623,6 @@ static void prepare_vmcs02_rare(struct vcpu_vmx *vmx, struct vmcs12 *vmcs12)vmcs_writel (GUEST_SYSENTER_ESP , vmcs12->guest_sysenter_esp);vmcs_writel (GUEST_SYSENTER_EIP , vmcs12->guest_sysenter_eip); -/* -* L1 may access the L2's PDPTR, so save them to construct -* vmcs12 -*/ -if (enable_ept) { -vmcs_write64 (GUEST_PDPTR0 , vmcs12->guest_pdptr0); -vmcs_write64 (GUEST_PDPTR1 , vmcs12->guest_pdptr1); -vmcs_write64 (GUEST_PDPTR2 , vmcs12->guest_pdptr2); -vmcs_write64 (GUEST_PDPTR3 , vmcs12->guest_pdptr3); - } -if (kvm_mpx_supported () && vmx->vcpu.arch.nested_run_pending && (vmcs12->vm_entry_controls &VM_ENTRY_LOAD_BNDCFGS ))vmcs_write64 (GUEST_BNDCFGS , vmcs12->guest_bndcfgs);
Signature · 1 line
2.52.0
Paolo Bonzini
maintainerPATCH v2 3/5 KVM: nSVM: invalidate cached PDPTRs across nested NPT transitions
In reply to Paolo BonziniWhen L2 runs under nested NPT and uses PAE paging, KVM's cached PDPTRs in mmu->pdptrs[] can hold stale or wrong values after nested transitions and across migration restore, because both nested_svm_load_cr3() and svm_get_nested_state_pages() only refresh PDPTRs on the !nested_npt path.
The user-visible bug is on migration restore of an L2 running with nested NPT and 32-bit PAE paging, if userspace uses KVM_SET_SREGS rather than KVM_SET_SREGS2. In that case, load_pdptrs() leaves VCPU_EXREG_PDPTR marked as available, and kvm_pdptr_read() will use a stale translation that used L1 GPAs instead of L2 nGPAs. svm_get_nested_state_pages() runs on first KVM_RUN but skips the refresh because nested_npt_enabled() is true. The CPU itself reads L2's PDPTRs correctly from memory via L1's NPT, but KVM-side walking of guest PAE page tables uses the bogus cached values.
Unlike Intel's GUEST_PDPTR0..3 fields in the VMCS, SVM has no VMCB-cached PDPTR state: the in-memory PDPTEs at the current CR3 are the only source of truth, and svm_cache_reg(VCPU_EXREG_PDPTR) simply reloads them from memory via load_pdptrs(). Clearing the avail bit (and the dirty bit because !avail/dirty is invalid) to force a reload when PDPTRs as needed fixes the bug.
Do the same for nested_svm_load_cr3()'s nested_npt branch, so that the invariant "PDPTRs need reloading" is handled similarly for both immediate and deferred loading.
--- arch/x86/kvm/kvm_cache_regs.h | 8 ++++++++ arch/x86/kvm/svm/nested.c | 27 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-)
arch/x86/kvm/kvm_cache_regs.h +8 -0
@@ -77,6 +77,14 @@ static inline bool kvm_register_is_dirty(struct kvm_vcpu *vcpu,return test_bit (reg, vcpu->arch.regs_dirty); } +static inline void kvm_register_mark_for_reload (struct kvm_vcpu *vcpu, +enum kvm_reg reg) +{ +kvm_assert_register_caching_allowed (vcpu); +__clear_bit (reg, vcpu->arch.regs_avail); +__clear_bit (reg, vcpu->arch.regs_dirty); +} +static inline void kvm_register_mark_available (struct kvm_vcpu *vcpu,enum kvm_reg reg) {
arch/x86/kvm/svm/nested.c +18 -9
@@ -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_for_reload (vcpu,VCPU_REG_PDPTR ); +else if (CC (!load_pdptrs (vcpu, cr3))) +return -EINVAL ; + } vcpu->arch.cr3 = cr3;
@@ -2055,15 +2058,21 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)if (WARN_ON (!is_guest_mode (vcpu)))return true; -if (!vcpu->arch.pdptrs_from_userspace && - !nested_npt_enabled (to_svm (vcpu)) &&is_pae_paging (vcpu)) +if (is_pae_paging (vcpu)) {/* -* Reload the guest's PDPTRs since after a migration -* the guest CR3 might be restored prior to setting the nested -* state which can lead to a load of wrong PDPTRs. +* After migration, CR3 may have been restored before +* KVM_SET_NESTED_STATE, so the PDPTR load into mmu->pdptrs[] +* may have treated CR3 as an L1 GPA. For nNPT, drop the +* cache so the next access reloads them with the proper +* nGPA translation. For !nNPT, reload eagerly unless userspace +* already supplied authoritative PDPTRs via KVM_SET_SREGS2. */ -if (CC (!load_pdptrs (vcpu, vcpu->arch.cr3))) +if (nested_npt_enabled (to_svm (vcpu))) +kvm_register_mark_for_reload (vcpu,VCPU_REG_PDPTR ); +else if (!vcpu->arch.pdptrs_from_userspace && +CC (!load_pdptrs (vcpu, vcpu->arch.cr3)))return false; + }if (!nested_svm_merge_msrpm (vcpu)) { vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ;
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH v2 3/5] KVM: nSVM: invalidate cached PDPTRs across nested NPT transitions
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Uninitialized exit_reason when load_pdptrs() fails causes userspace abort.
- [High] svm_cache_reg() leaves stale PDPTRs marked as available upon load failure. --
commit b0f19570b3cb20ce2cbd28a0c0be3e7262edeceb
Author: Paolo Bonzini <[email protected]>
KVM: nSVM: invalidate cached PDPTRs across nested NPT transitions
This commit addresses an issue where KVM's cached PDPTRs hold stale values after nested transitions under nested NPT and PAE paging. It clears the available and dirty bits to force a reload when PDPTRs are needed.
Quoted 4 lines
arch/x86/kvm/svm/nested.c +0
-0
[ ... ]
Quoted 14 lines
arch/x86/kvm/svm/nested.c +4
-5@@ -2111,15 +2114,21 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)if (WARN_ON (!is_guest_mode (vcpu)))return true; -if (!vcpu->arch.pdptrs_from_userspace && - !nested_npt_enabled (to_svm (vcpu)) &&is_pae_paging (vcpu)) +if (is_pae_paging (vcpu)) {/* -* Reload the guest's PDPTRs since after a migration -* the guest CR3 might be restored prior to setting the nested -* state which can lead to a load of wrong PDPTRs. +* After migration, CR3 may have been restored before +* KVM_SET_NESTED_STATE, so the PDPTR load into mmu->pdptrs[] +* may have treated CR3 as an L1 GPA. For nNPT, drop the
arch/x86/kvm/svm/nested.c static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) · line 2130
Quoted 10 lines
arch/x86/kvm/svm/nested.c +7
-1+* cache so the next access reloads them with the proper +* nGPA translation. For !nNPT, reload eagerly unless userspace +* already supplied authoritative PDPTRs via KVM_SET_SREGS2. */ -if (CC (!load_pdptrs (vcpu, vcpu->arch.cr3))) +if (nested_npt_enabled (to_svm (vcpu))) +kvm_register_mark_for_reload (vcpu,VCPU_REG_PDPTR ); +else if (!vcpu->arch.pdptrs_from_userspace && +CC (!load_pdptrs (vcpu, vcpu->arch.cr3)))return false;
[Severity: High]
This isn't a bug introduced by this patch, but if load_pdptrs() fails here and we return false, is exit_reason left uninitialized?
If svm_get_nested_state_pages() returns false without setting exit_reason, vcpu_enter_guest() will return 0 and break the vcpu_run() loop. This returns to userspace with an unmodified exit_reason (often KVM_EXIT_UNKNOWN). Userspace like QEMU typically aborts the VM on KVM_EXIT_UNKNOWN.
Should we populate vcpu->run->exit_reason with KVM_EXIT_INTERNAL_ERROR before returning false?
arch/x86/kvm/svm/nested.c static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) · line 2077
Quoted 3 lines
arch/x86/kvm/svm/nested.c +1
-0+ }if (!nested_svm_merge_msrpm (vcpu)) {
[ ... ]
[Severity: High]
This is a pre-existing issue, but since we are discussing PDPTR loads, does svm_cache_reg() leave stale PDPTRs marked as available upon load failure?
Looking at svm_cache_reg() in arch/x86/kvm/svm/svm.c:
kvm_register_mark_available(vcpu, reg);
switch (reg) {
case VCPU_REG_PDPTR:
...
if (npt_enabled)
load_pdptrs(vcpu, kvm_read_cr3(vcpu));
break;If load_pdptrs() fails here (e.g., due to an invalid CR3 or unmapped guest memory), it returns 0 without updating mmu->pdptrs. Because the register is unconditionally marked as available before the load attempt, will subsequent calls to kvm_pdptr_read() during instruction emulation return the stale translations instead of failing?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Paolo Bonzini
maintainerPATCH v2 4/5 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
@@ -14350,6 +14350,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 v2 5/5 KVM: x86/mmu: 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 something to be careful about, in that PDPTRs are now not stored separately in root_mmu and nested_mmu for L1 and L2 guests. In practice this was already not an issue:
- for EPT the VMCS0x has to keep them up to date; and for the purpose of emulation they are always loaded from the VMCS on vmentry/vmexit, thanks to the clearing of dirty and available register bitmaps in vmx_switch_vmcs()
- for NPT, VCPU_EXREG_PDPTR is similarly cleared for nNPT, which does not cache the PDPTRs; while for non-nNPT the PDPTRs are loaded together with the load of CR3.
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/svm.c | 2 +- arch/x86/kvm/vmx/vmx.c | 20 ++++++++------------ arch/x86/kvm/x86.c | 6 +++--- 5 files changed, 16 insertions(+), 21 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -3
@@ -540,10 +540,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 {
@@ -902,6 +899,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
@@ -192,12 +192,12 @@ static inline u64 kvm_pdptr_read(struct kvm_vcpu *vcpu, int index)if (!kvm_register_is_available (vcpu,VCPU_REG_PDPTR ))kvm_x86_call (cache_reg)(vcpu,VCPU_REG_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/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_REG_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/vmx.c +8 -12
@@ -3376,30 +3376,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_REG_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_REG_PDPTR ); }
arch/x86/kvm/x86.c +3 -3
@@ -1071,7 +1071,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
@@ -1100,10 +1100,10 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3)* Marking VCPU_REG_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_REG_PDPTR );kvm_make_request (KVM_REQ_LOAD_MMU_PGD , vcpu); vcpu->arch.pdptrs_from_userspace = false;
Signature · 1 line
2.52.0
Queued, thanks.
Paolo