On Wed, May 10, 2023 at 01:30:29PM +0800, Chao Gao wrote: > On Tue, May 09, 2023 at 09:50:06PM +0800, Yan Zhao wrote: > >Add a helper to indicate that the kvm_zap_gfn_range() request is to update > >memory type. > > > >Then the zap can be avoided in cases: > >1. TDP is not enabled. > >2. EPT is not enabled. > > > >This is because only memory type of EPT leaf entries are subjected to > >change when noncoherent DMA/guest CR0.CD/guest MTRR settings change. > > > >Signed-off-by: Yan Zhao <yan.y.zhao@xxxxxxxxx> > >--- > > arch/x86/kvm/mmu.h | 1 + > > arch/x86/kvm/mmu/mmu.c | 16 ++++++++++++++++ > > 2 files changed, 17 insertions(+) > > > >diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h > >index 92d5a1924fc1..a04577afbc71 100644 > >--- a/arch/x86/kvm/mmu.h > >+++ b/arch/x86/kvm/mmu.h > >@@ -236,6 +236,7 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, > > } > > > > void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); > >+void kvm_zap_gfn_for_memtype(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); > > > > int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); > > > >diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > >index c8961f45e3b1..2706754794d1 100644 > >--- a/arch/x86/kvm/mmu/mmu.c > >+++ b/arch/x86/kvm/mmu/mmu.c > >@@ -6272,6 +6272,22 @@ static bool kvm_rmap_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_e > > return flush; > > } > > > >+/* > >+ * Invalidate (zap) TDP SPTEs that cover GFNs from gfn_start and up to gfn_end > >+ * (not including it) for reason of memory type being updated. > >+ */ > >+void kvm_zap_gfn_for_memtype(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) > >+{ > >+ /* Currently only memory type of EPT leaf entries are affected by > >+ * guest CR0.CD and guest MTRR. > >+ * So skip invalidation (zap) in other cases > >+ */ > >+ if (!shadow_memtype_mask) > > Do you need to check kvm_arch_has_noncoherent_dma()? if yes, maybe just extract > the first if() statement and its associated comment from kvm_tdp_page_fault()? > This kvm_zap_gfn_for_memtype() helper will be called in kvm_arch_unregister_noncoherent_dma() as well when noncoherent_dma_count goes from 1 to 0. So, checking kvm_arch_has_noncoherent_dma() is not desired. > >+ return; > >+ > >+ kvm_zap_gfn_range(kvm, gpa_to_gfn(0), gpa_to_gfn(~0ULL)); > > This should be: > > kvm_zap_gfn_range(kvm, start, end); > Oops. sorry about this mistake. Will fix it in next version. > >+} > >+ > > /* > > * Invalidate (zap) SPTEs that cover GFNs from gfn_start and up to gfn_end > > * (not including it) > >-- > >2.17.1 > >