Introduce gmap_enable_cmm_already_locked() and its locking wrapper gmap_enable_cmm() to attempt to allocate all page tables of a running guest. This means that all hugepages will be mapped with 4k pages regardless of the hpage parameter. Existing mappings with large pages will be split. If any memory allocation fails, ignore the error and give up. This is only a best effort. Cc: stable@vger.kernel.org # 7.0+: prerequisite for the next patch Signed-off-by: Claudio Imbrenda --- arch/s390/kvm/gmap/gmap.c | 48 +++++++++++++++++++++++++++++++++++++++ arch/s390/kvm/gmap/gmap.h | 14 ++++++++++++ 2 files changed, 62 insertions(+) diff --git a/arch/s390/kvm/gmap/gmap.c b/arch/s390/kvm/gmap/gmap.c index ef1243ad4e54..c7af1ad9f7c1 100644 --- a/arch/s390/kvm/gmap/gmap.c +++ b/arch/s390/kvm/gmap/gmap.c @@ -1527,3 +1527,51 @@ struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare } return new; } + +#if KVM_S390_MANAGES_S390_GUEST +/* Bail out for any allocation failure. This is only a best effort to allocate all page tables. */ +void gmap_enable_cmm_already_locked(struct kvm_s390_mmu_cache *mc, struct gmap *gmap) +{ + struct kvm_memory_slot *ms; + struct kvm_memslots *slots; + gfn_t gfn, start, end; + union crste *crstep; + union pte *ptep; + int flags, bkt; + + lockdep_assert_held(&gmap->kvm->srcu); + lockdep_assert_held(&gmap->kvm->slots_arch_lock); + + if (uses_cmm(gmap)) + return; + + set_bit(GMAP_FLAG_USES_CMM, &gmap->flags); + + slots = kvm_memslots(gmap->kvm); + if (!slots || kvm_memslots_empty(slots)) + return; + + if (WARN_ON_ONCE(is_ucontrol(gmap))) + return; + + flags = DAT_WALK_ALLOC | DAT_WALK_SPLIT; + + kvm_for_each_memslot(ms, bkt, slots) { + start = ALIGN(ms->base_gfn, _PAGE_ENTRIES); + end = ALIGN_DOWN(ms->base_gfn + ms->npages, _PAGE_ENTRIES); + + for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) { + if (kvm_s390_mmu_cache_topup(mc)) + return; + + guard(write_lock)(&gmap->kvm->mmu_lock); + + if (uses_skeys(gmap)) + flags |= DAT_WALK_USES_SKEYS; + + dat_entry_walk(mc, gfn, gmap->asce, flags, TABLE_TYPE_PAGE_TABLE, + &crstep, &ptep); + } + } +} +#endif /* KVM_S390_MANAGES_S390_GUEST */ diff --git a/arch/s390/kvm/gmap/gmap.h b/arch/s390/kvm/gmap/gmap.h index 8f47f29c89fd..fe96268b6de7 100644 --- a/arch/s390/kvm/gmap/gmap.h +++ b/arch/s390/kvm/gmap/gmap.h @@ -102,6 +102,7 @@ void gmap_ucas_unmap(struct gmap *gmap, gfn_t c_gfn, unsigned long count); #if KVM_S390_MANAGES_S390_GUEST int gmap_enable_skeys(struct gmap *gmap); +void gmap_enable_cmm_already_locked(struct kvm_s390_mmu_cache *mc, struct gmap *gmap); #endif /* KVM_S390_MANAGES_S390_GUEST */ int gmap_pv_destroy_range(struct gmap *gmap, gfn_t start, gfn_t end, bool interruptible); @@ -345,4 +346,17 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed return sg->guest_asce.val == asce.val && sg->edat_level == edat_level; } +#if KVM_S390_MANAGES_S390_GUEST +static inline void gmap_enable_cmm(struct kvm_s390_mmu_cache *mc, struct gmap *gmap) +{ + /* CMMA already enabled, nothing to do */ + if (uses_cmm(gmap)) + return; + + guard(srcu)(&gmap->kvm->srcu); + guard(mutex)(&gmap->kvm->slots_arch_lock); + + gmap_enable_cmm_already_locked(mc, gmap); +} +#endif /* KVM_S390_MANAGES_S390_GUEST */ #endif /* ARCH_KVM_GMAP_GMAP_H */ -- 2.56.1