On Mon, Mar 06, 2023 at 02:41:22PM -0800, Vipin Sharma wrote: > Add macros and function to make common logic for struct > kvm_mmu_memory_cache{} declaration and initialization. > > Any user which wants different values in struct kvm_mmu_memory_cache{} > will overwrite the default values explicitly after the initialization. > > Suggested-by: David Matlack <dmatlack@xxxxxxxxxx> > Signed-off-by: Vipin Sharma <vipinsh@xxxxxxxxxx> > --- > arch/arm64/kvm/arm.c | 1 + > arch/arm64/kvm/mmu.c | 3 ++- > arch/riscv/kvm/mmu.c | 9 +++++---- > arch/riscv/kvm/vcpu.c | 1 + MIPS also has cache (git grep "struct kvm_mmu_memory_cache"). > arch/x86/kvm/mmu/mmu.c | 8 ++++++++ > include/linux/kvm_types.h | 10 ++++++++++ > 6 files changed, 27 insertions(+), 5 deletions(-) > > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 3bd732eaf087..2b3d88e4ace8 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -330,6 +330,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > vcpu->arch.target = -1; > bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); > > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_page_cache); > vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; > > /* > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 7113587222ff..8a56f071ca66 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -895,7 +895,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > { > phys_addr_t addr; > int ret = 0; > - struct kvm_mmu_memory_cache cache = { .gfp_zero = __GFP_ZERO }; > + KVM_MMU_MEMORY_CACHE(cache); nit: DEFINE_KVM_MMU_MEMORY_CACHE() (Based on similar existing macros in the kernel, e.g. DEFINE_MUTEX(), DEFINE_TIMER().) > struct kvm_pgtable *pgt = kvm->arch.mmu.pgt; > enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_DEVICE | > KVM_PGTABLE_PROT_R | > @@ -904,6 +904,7 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > if (is_protected_kvm_enabled()) > return -EPERM; > > + cache.gfp_zero = __GFP_ZERO; > size += offset_in_page(guest_ipa); > guest_ipa &= PAGE_MASK; > > diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c > index 78211aed36fa..bdd8c17958dd 100644 > --- a/arch/riscv/kvm/mmu.c > +++ b/arch/riscv/kvm/mmu.c > @@ -351,10 +351,11 @@ int kvm_riscv_gstage_ioremap(struct kvm *kvm, gpa_t gpa, > int ret = 0; > unsigned long pfn; > phys_addr_t addr, end; > - struct kvm_mmu_memory_cache pcache = { > - .gfp_custom = (in_atomic) ? GFP_ATOMIC | __GFP_ACCOUNT : 0, > - .gfp_zero = __GFP_ZERO, > - }; > + KVM_MMU_MEMORY_CACHE(pcache); > + > + pcache.gfp_zero = __GFP_ZERO; > + if (in_atomic) > + pcache.gfp_custom = GFP_ATOMIC | __GFP_ACCOUNT; > > end = (gpa + size + PAGE_SIZE - 1) & PAGE_MASK; > pfn = __phys_to_pfn(hpa); > diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c > index 7d010b0be54e..bc743e9122d1 100644 > --- a/arch/riscv/kvm/vcpu.c > +++ b/arch/riscv/kvm/vcpu.c > @@ -163,6 +163,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) > > /* Mark this VCPU never ran */ > vcpu->arch.ran_atleast_once = false; > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_page_cache); > vcpu->arch.mmu_page_cache.gfp_zero = __GFP_ZERO; > bitmap_zero(vcpu->arch.isa, RISCV_ISA_EXT_MAX); > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index a4bf2e433030..b706087ef74e 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -5961,15 +5961,20 @@ int kvm_mmu_create(struct kvm_vcpu *vcpu) > { > int ret; > > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_pte_list_desc_cache); > vcpu->arch.mmu_pte_list_desc_cache.kmem_cache = pte_list_desc_cache; > vcpu->arch.mmu_pte_list_desc_cache.gfp_zero = __GFP_ZERO; > > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_page_header_cache); > vcpu->arch.mmu_page_header_cache.kmem_cache = mmu_page_header_cache; > vcpu->arch.mmu_page_header_cache.gfp_zero = __GFP_ZERO; > > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_shadow_page_cache); > vcpu->arch.mmu_shadow_page_cache.gfp_zero = __GFP_ZERO; > mutex_init(&vcpu->arch.mmu_shadow_page_cache_lock); > > + INIT_KVM_MMU_MEMORY_CACHE(&vcpu->arch.mmu_shadowed_info_cache); > + > vcpu->arch.mmu = &vcpu->arch.root_mmu; > vcpu->arch.walk_mmu = &vcpu->arch.root_mmu; > > @@ -6131,11 +6136,14 @@ int kvm_mmu_init_vm(struct kvm *kvm) > node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot; > kvm_page_track_register_notifier(kvm, node); > > + INIT_KVM_MMU_MEMORY_CACHE(&kvm->arch.split_page_header_cache); > kvm->arch.split_page_header_cache.kmem_cache = mmu_page_header_cache; > kvm->arch.split_page_header_cache.gfp_zero = __GFP_ZERO; > > + INIT_KVM_MMU_MEMORY_CACHE(&kvm->arch.split_shadow_page_cache); > kvm->arch.split_shadow_page_cache.gfp_zero = __GFP_ZERO; > > + INIT_KVM_MMU_MEMORY_CACHE(&kvm->arch.split_desc_cache); > kvm->arch.split_desc_cache.kmem_cache = pte_list_desc_cache; > kvm->arch.split_desc_cache.gfp_zero = __GFP_ZERO; > > diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h > index 2728d49bbdf6..192516eeccac 100644 > --- a/include/linux/kvm_types.h > +++ b/include/linux/kvm_types.h > @@ -98,6 +98,16 @@ struct kvm_mmu_memory_cache { > int capacity; > void **objects; > }; > + > +#define KVM_MMU_MEMORY_CACHE_INIT() { } > + > +#define KVM_MMU_MEMORY_CACHE(_name) \ > + struct kvm_mmu_memory_cache _name = KVM_MMU_MEMORY_CACHE_INIT() nit: There's an extra tab here. > + > +static inline void INIT_KVM_MMU_MEMORY_CACHE(struct kvm_mmu_memory_cache *cache) > +{ > + *cache = (struct kvm_mmu_memory_cache)KVM_MMU_MEMORY_CACHE_INIT(); > +} > #endif > > #define HALT_POLL_HIST_COUNT 32 > -- > 2.40.0.rc0.216.gc4246ad0f0-goog >