On Mon, Jul 26, 2021, Ben Gardon wrote: > On Mon, Jul 26, 2021 at 10:54 AM Mingwei Zhang <mizhang@xxxxxxxxxx> wrote: > > diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h > > index 83e6c6965f1e..ad5638815311 100644 > > --- a/arch/x86/kvm/mmu.h > > +++ b/arch/x86/kvm/mmu.h > > @@ -240,4 +240,6 @@ static inline bool kvm_memslots_have_rmaps(struct kvm *kvm) > > return smp_load_acquire(&kvm->arch.memslots_have_rmaps); > > } > > > > +void kvm_update_page_stats(struct kvm *kvm, int level, int count); > > + > > #endif > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index 442cc554ebd6..7e0fc760739b 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -588,16 +588,22 @@ static bool mmu_spte_update(u64 *sptep, u64 new_spte) > > return flush; > > } > > > > +void kvm_update_page_stats(struct kvm *kvm, int level, int count) > > +{ > > + atomic64_add(count, &kvm->stat.page_stats.pages[level - 1]); > > +} This can be static inline in the header. Ignoring prolog+RET, it's four instructions, and two of those are sign extending input params. > > + > > /* > > * Rules for using mmu_spte_clear_track_bits: > > * It sets the sptep from present to nonpresent, and track the > > * state bits, it is used to clear the last level sptep. > > * Returns non-zero if the PTE was previously valid. > > */ > > -static int mmu_spte_clear_track_bits(u64 *sptep) > > +static int mmu_spte_clear_track_bits(struct kvm *kvm, u64 *sptep) > > { > > kvm_pfn_t pfn; > > u64 old_spte = *sptep; > > + int level = sptep_to_sp(sptep)->role.level; > > > > if (!spte_has_volatile_bits(old_spte)) > > __update_clear_spte_fast(sptep, 0ull); > > @@ -607,6 +613,9 @@ static int mmu_spte_clear_track_bits(u64 *sptep) > > if (!is_shadow_present_pte(old_spte)) > > return 0; > > > > + if (is_last_spte(old_spte, level)) > > You can drop this check since it's part of the contract for calling > this function. Ah, nice! I overlooked that.