Re: [Patch v4 07/18] KVM: x86/mmu: Unconditionally count allocations from MMU page caches
From: Zhi Wang
Date: Thu Mar 09 2023 - 11:11:43 EST
On Mon, 6 Mar 2023 14:41:16 -0800
Vipin Sharma <vipinsh@xxxxxxxxxx> wrote:
Ah, it is removed here. :)
> Remove count_shadow_page_allocations from struct shadow_page_caches{}.
> Remove count_allocation boolean condition check from
> mmu_sp_memory_cache_alloc().
>
> Both split_shadow_page_cache and mmu_shadow_page_cache are counted in
> global count of unused cache pages. count_shadow_page_allocations
> boolean is obsolete and can be removed.
>
> Signed-off-by: Vipin Sharma <vipinsh@xxxxxxxxxx>
> ---
> arch/x86/kvm/mmu/mmu.c | 11 +++--------
> arch/x86/kvm/mmu/mmu_internal.h | 3 +--
> arch/x86/kvm/mmu/tdp_mmu.c | 3 +--
> 3 files changed, 5 insertions(+), 12 deletions(-)
>
> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index 73a0ac9c11ce..0a0962d8108b 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c
> @@ -2184,7 +2184,6 @@ struct shadow_page_caches {
> struct kvm_mmu_memory_cache *page_header_cache;
> struct kvm_mmu_memory_cache *shadow_page_cache;
> struct kvm_mmu_memory_cache *shadowed_info_cache;
> - bool count_shadow_page_allocation;
> };
>
> static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm,
> @@ -2196,8 +2195,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm,
> struct kvm_mmu_page *sp;
>
> sp = kvm_mmu_memory_cache_alloc(caches->page_header_cache);
> - sp->spt = mmu_sp_memory_cache_alloc(caches->shadow_page_cache,
> - caches->count_shadow_page_allocation);
> + sp->spt = mmu_sp_memory_cache_alloc(caches->shadow_page_cache);
> if (!role.direct)
> sp->shadowed_translation = kvm_mmu_memory_cache_alloc(caches->shadowed_info_cache);
>
> @@ -2254,7 +2252,6 @@ static struct kvm_mmu_page *kvm_mmu_get_shadow_page(struct kvm_vcpu *vcpu,
> .page_header_cache = &vcpu->arch.mmu_page_header_cache,
> .shadow_page_cache = &vcpu->arch.mmu_shadow_page_cache,
> .shadowed_info_cache = &vcpu->arch.mmu_shadowed_info_cache,
> - .count_shadow_page_allocation = true,
> };
>
> return __kvm_mmu_get_shadow_page(vcpu->kvm, vcpu, &caches, gfn, role);
> @@ -6330,7 +6327,6 @@ static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *hu
> /* Direct SPs do not require a shadowed_info_cache. */
> caches.page_header_cache = &kvm->arch.split_page_header_cache;
> caches.shadow_page_cache = &kvm->arch.split_shadow_page_cache;
> - caches.count_shadow_page_allocation = true;
>
> /* Safe to pass NULL for vCPU since requesting a direct SP. */
> return __kvm_mmu_get_shadow_page(kvm, NULL, &caches, gfn, role);
> @@ -7101,10 +7097,9 @@ void kvm_mmu_pre_destroy_vm(struct kvm *kvm)
> kthread_stop(kvm->arch.nx_huge_page_recovery_thread);
> }
>
> -void *mmu_sp_memory_cache_alloc(struct kvm_mmu_memory_cache *shadow_page_cache,
> - bool count_allocation)
> +void *mmu_sp_memory_cache_alloc(struct kvm_mmu_memory_cache *shadow_page_cache)
> {
> - if (count_allocation && shadow_page_cache->nobjs)
> + if (shadow_page_cache->nobjs)
> percpu_counter_dec(&kvm_total_unused_cached_pages);
> return kvm_mmu_memory_cache_alloc(shadow_page_cache);
> }
> diff --git a/arch/x86/kvm/mmu/mmu_internal.h b/arch/x86/kvm/mmu/mmu_internal.h
> index 798cfbf0a36b..a607314348e3 100644
> --- a/arch/x86/kvm/mmu/mmu_internal.h
> +++ b/arch/x86/kvm/mmu/mmu_internal.h
> @@ -338,7 +338,6 @@ void *mmu_memory_cache_alloc(struct kvm_mmu_memory_cache *mc);
>
> void track_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
> void untrack_possible_nx_huge_page(struct kvm *kvm, struct kvm_mmu_page *sp);
> -void *mmu_sp_memory_cache_alloc(struct kvm_mmu_memory_cache *cache,
> - bool count_allocation);
> +void *mmu_sp_memory_cache_alloc(struct kvm_mmu_memory_cache *cache);
>
> #endif /* __KVM_X86_MMU_INTERNAL_H */
> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c
> index fa6eb1e9101e..d1e85012a008 100644
> --- a/arch/x86/kvm/mmu/tdp_mmu.c
> +++ b/arch/x86/kvm/mmu/tdp_mmu.c
> @@ -265,8 +265,7 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp(struct kvm_vcpu *vcpu)
> struct kvm_mmu_page *sp;
>
> sp = kvm_mmu_memory_cache_alloc(&vcpu->arch.mmu_page_header_cache);
> - sp->spt = mmu_sp_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache,
> - true);
> + sp->spt = mmu_sp_memory_cache_alloc(&vcpu->arch.mmu_shadow_page_cache);
>
> return sp;
> }